From c3b339e69a46175c8ed70aae2e393031bc04dcde Mon Sep 17 00:00:00 2001 From: gofastasf Date: Mon, 24 Feb 2025 20:43:52 +0530 Subject: [PATCH] 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. --- internal/configs/configload/copy_dir.go | 44 +++++--------------- internal/configs/configload/inode.go | 25 ----------- internal/configs/configload/inode_freebsd.go | 25 ----------- internal/configs/configload/inode_windows.go | 12 ------ 4 files changed, 10 insertions(+), 96 deletions(-) delete mode 100644 internal/configs/configload/inode.go delete mode 100644 internal/configs/configload/inode_freebsd.go delete mode 100644 internal/configs/configload/inode_windows.go diff --git a/internal/configs/configload/copy_dir.go b/internal/configs/configload/copy_dir.go index b07f3bd574..ff7abdb941 100644 --- a/internal/configs/configload/copy_dir.go +++ b/internal/configs/configload/copy_dir.go @@ -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 -} diff --git a/internal/configs/configload/inode.go b/internal/configs/configload/inode.go deleted file mode 100644 index 0437b204e5..0000000000 --- a/internal/configs/configload/inode.go +++ /dev/null @@ -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") -} diff --git a/internal/configs/configload/inode_freebsd.go b/internal/configs/configload/inode_freebsd.go deleted file mode 100644 index 23b601aa91..0000000000 --- a/internal/configs/configload/inode_freebsd.go +++ /dev/null @@ -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") -} diff --git a/internal/configs/configload/inode_windows.go b/internal/configs/configload/inode_windows.go deleted file mode 100644 index 5b4735b83b..0000000000 --- a/internal/configs/configload/inode_windows.go +++ /dev/null @@ -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 -}