Skip to content

Commit

Permalink
implementing inode equivalent on windows
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Bustamante <[email protected]>
  • Loading branch information
jjbustamante committed Nov 14, 2023
1 parent 29f4a1d commit 75173e4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func WriteDirToTar(tw TarWriter, srcDir, basePath string, uid, gid int, mode int

header.Name = getHeaderNameFromBaseAndRelPath(basePath, relPath)
if hasHardlinks(fi, file) {
inode, err := getInodeFromStat(fi.Sys())
inode, err := getInodeFromStat(fi.Sys(), file)
if err != nil {
return err
}

Check warning on line 226 in pkg/archive/archive.go

View check run for this annotation

Codecov / codecov/patch

pkg/archive/archive.go#L225-L226

Added lines #L225 - L226 were not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion pkg/archive/archive_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func hasHardlinks(fi os.FileInfo, path string) bool {
return fi.Sys().(*syscall.Stat_t).Nlink > 1
}

func getInodeFromStat(stat interface{}) (inode uint64, err error) {
func getInodeFromStat(stat interface{}, path string) (inode uint64, err error) {
s, ok := stat.(*syscall.Stat_t)
if ok {
inode = s.Ino
Expand Down
64 changes: 37 additions & 27 deletions pkg/archive/archive_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,50 @@ func hasHardlinks(fi os.FileInfo, path string) bool {
case *syscall.ByHandleFileInformation:
numberOfLinks = v.NumberOfLinks

Check warning on line 15 in pkg/archive/archive_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/archive/archive_windows.go#L14-L15

Added lines #L14 - L15 were not covered by tests
default:
// We need to open the file and create an instance of a ByHandleFileInformation
fPath, err := syscall.UTF16PtrFromString(path)
if err != nil {
return false
}

handle, err := syscall.CreateFile(
fPath,
windows.FILE_READ_ATTRIBUTES,
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE,
nil,
syscall.OPEN_EXISTING,
syscall.FILE_FLAG_BACKUP_SEMANTICS,
0)
if err != nil {
return false
if info, err := open(path); err == nil {
numberOfLinks = info.NumberOfLinks
}
defer syscall.CloseHandle(handle)

// Get the number of links
var info syscall.ByHandleFileInformation
err = syscall.GetFileInformationByHandle(handle, &info)
if err != nil {
return false
}
numberOfLinks = info.NumberOfLinks
}
return numberOfLinks > 1
}

func getInodeFromStat(stat interface{}) (inode uint64, err error) {
func getInodeFromStat(stat interface{}, path string) (inode uint64, err error) {
s, ok := stat.(*syscall.ByHandleFileInformation)
if ok {
inode = uint64(s.VolumeSerialNumber)
inode = (uint64(s.FileIndexHigh) << 32) | uint64(s.FileIndexLow)

Check warning on line 27 in pkg/archive/archive_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/archive/archive_windows.go#L27

Added line #L27 was not covered by tests
} else {
s, err = open(path)
if err != nil {
inode = (uint64(s.FileIndexHigh) << 32) | uint64(s.FileIndexLow)
}

Check warning on line 32 in pkg/archive/archive_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/archive/archive_windows.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}
return
}

func open(path string) (*syscall.ByHandleFileInformation, error) {
// We need to open the file and create an instance of a ByHandleFileInformation
fPath, err := syscall.UTF16PtrFromString(path)
if err != nil {
return nil, err
}

Check warning on line 42 in pkg/archive/archive_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/archive/archive_windows.go#L41-L42

Added lines #L41 - L42 were not covered by tests

handle, err := syscall.CreateFile(
fPath,
windows.FILE_READ_ATTRIBUTES,
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE,
nil,
syscall.OPEN_EXISTING,
syscall.FILE_FLAG_BACKUP_SEMANTICS,
0)
if err != nil {
return nil, err
}

Check warning on line 54 in pkg/archive/archive_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/archive/archive_windows.go#L53-L54

Added lines #L53 - L54 were not covered by tests
defer syscall.CloseHandle(handle)

var info syscall.ByHandleFileInformation
err = syscall.GetFileInformationByHandle(handle, &info)
if err != nil {
return nil, err
}

Check warning on line 61 in pkg/archive/archive_windows.go

View check run for this annotation

Codecov / codecov/patch

pkg/archive/archive_windows.go#L60-L61

Added lines #L60 - L61 were not covered by tests
return &info, nil
}

0 comments on commit 75173e4

Please sign in to comment.