Replace custom inode-based file comparison with os.SameFile. (#36562)

Remove inode.go, inode_freebsd.go, and inode_windows.go, eliminating platform-specific inode lookup logic.
Replace sameFile function with os.SameFile, simplifying file comparison logic.
pull/36571/head
gofastasf 1 year ago committed by GitHub
parent ea140f6dc8
commit c3b339e69a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -30,11 +30,17 @@ func copyDir(dst, src string) error {
// destination with the path without the src on it.
dstPath := filepath.Join(dst, path[len(src):])
// we don't want to try and copy the same file over itself.
if eq, err := sameFile(path, dstPath); eq {
// Call os.Stat on dstPath to obtain os.FileInfo since os.SameFile
// requires FileInfo objects for comparison.
dstInfo, err := os.Stat(dstPath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else if os.SameFile(info, dstInfo) {
// The destination file exists and is the same as the source file;
// skip copying.
return nil
} else if err != nil {
return err
}
// If we have a directory, make that subdirectory, then continue
@ -86,33 +92,3 @@ func copyDir(dst, src string) error {
return filepath.Walk(src, walkFn)
}
// sameFile tried to determine if to paths are the same file.
// If the paths don't match, we lookup the inode on supported systems.
func sameFile(a, b string) (bool, error) {
if a == b {
return true, nil
}
aIno, err := inode(a)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
bIno, err := inode(b)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if aIno > 0 && aIno == bIno {
return true, nil
}
return false, nil
}

@ -1,25 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
//go:build linux || darwin || openbsd || netbsd || solaris || dragonfly
// +build linux darwin openbsd netbsd solaris dragonfly
package configload
import (
"fmt"
"os"
"syscall"
)
// lookup the inode of a file on posix systems
func inode(path string) (uint64, error) {
stat, err := os.Stat(path)
if err != nil {
return 0, err
}
if st, ok := stat.Sys().(*syscall.Stat_t); ok {
return st.Ino, nil
}
return 0, fmt.Errorf("could not determine file inode")
}

@ -1,25 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
//go:build freebsd
// +build freebsd
package configload
import (
"fmt"
"os"
"syscall"
)
// lookup the inode of a file on posix systems
func inode(path string) (uint64, error) {
stat, err := os.Stat(path)
if err != nil {
return 0, err
}
if st, ok := stat.Sys().(*syscall.Stat_t); ok {
return uint64(st.Ino), nil
}
return 0, fmt.Errorf("could not determine file inode")
}

@ -1,12 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
//go:build windows
// +build windows
package configload
// no syscall.Stat_t on windows, return 0 for inodes
func inode(path string) (uint64, error) {
return 0, nil
}
Loading…
Cancel
Save