From dd027bbd285936d0c2d321e6f1ea9909b13dba22 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 3 Nov 2025 19:16:28 -0800 Subject: [PATCH 1/2] Replace Realpath with more efficient GetFileAttributesEx --- internal/vfs/internal/internal.go | 14 +++++++------- internal/vfs/osvfs/os.go | 4 ++-- internal/vfs/osvfs/symlink_other.go | 10 ++++++++++ internal/vfs/osvfs/symlink_windows.go | 28 +++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 internal/vfs/osvfs/symlink_other.go create mode 100644 internal/vfs/osvfs/symlink_windows.go diff --git a/internal/vfs/internal/internal.go b/internal/vfs/internal/internal.go index c761e20388..5376a91182 100644 --- a/internal/vfs/internal/internal.go +++ b/internal/vfs/internal/internal.go @@ -13,8 +13,8 @@ import ( ) type Common struct { - RootFor func(root string) fs.FS - Realpath func(path string) string + RootFor func(root string) fs.FS + IsSymlinkOrJunction func(path string) bool } func RootLength(p string) int { @@ -93,12 +93,12 @@ func (vfs *Common) GetAccessibleEntries(path string) (result vfs.Entries) { continue } - if entryType&fs.ModeIrregular != 0 && vfs.Realpath != nil { - // Could be a Windows junction. Try Realpath. - // TODO(jakebailey): use syscall.Win32FileAttributeData instead + if entryType&fs.ModeIrregular != 0 && vfs.IsSymlinkOrJunction != nil { + // Could be a Windows junction or other reparse point. + // Check using the OS-specific helper. fullPath := path + "/" + entry.Name() - if realpath := vfs.Realpath(fullPath); fullPath != realpath { - if stat := vfs.Stat(realpath); stat != nil { + if vfs.IsSymlinkOrJunction(fullPath) { + if stat := vfs.Stat(fullPath); stat != nil { addToResult(entry.Name(), stat.Mode()) } } diff --git a/internal/vfs/osvfs/os.go b/internal/vfs/osvfs/os.go index d811d6ff71..fd37954693 100644 --- a/internal/vfs/osvfs/os.go +++ b/internal/vfs/osvfs/os.go @@ -21,8 +21,8 @@ func FS() vfs.FS { var osVFS vfs.FS = &osFS{ common: internal.Common{ - RootFor: os.DirFS, - Realpath: osFSRealpath, + RootFor: os.DirFS, + IsSymlinkOrJunction: isSymlinkOrJunction, }, } diff --git a/internal/vfs/osvfs/symlink_other.go b/internal/vfs/osvfs/symlink_other.go new file mode 100644 index 0000000000..19909e5cf5 --- /dev/null +++ b/internal/vfs/osvfs/symlink_other.go @@ -0,0 +1,10 @@ +//go:build !windows + +package osvfs + +// isSymlinkOrJunction always returns false on non-Windows platforms. +// On Unix-like systems, symlinks are already properly detected by the +// fs.ModeSymlink bit in the directory entry type, so this check is not needed. +func isSymlinkOrJunction(path string) bool { + return false +} diff --git a/internal/vfs/osvfs/symlink_windows.go b/internal/vfs/osvfs/symlink_windows.go new file mode 100644 index 0000000000..9a4ffc921d --- /dev/null +++ b/internal/vfs/osvfs/symlink_windows.go @@ -0,0 +1,28 @@ +package osvfs + +import ( + "syscall" + "unsafe" +) + +// isSymlinkOrJunction checks if the given path is a symlink or junction point +// on Windows by checking the FILE_ATTRIBUTE_REPARSE_POINT attribute. +// This is more efficient than calling realpath and comparing paths. +func isSymlinkOrJunction(path string) bool { + pathUTF16, err := syscall.UTF16PtrFromString(path) + if err != nil { + return false + } + + var data syscall.Win32FileAttributeData + err = syscall.GetFileAttributesEx( + pathUTF16, + syscall.GetFileExInfoStandard, + (*byte)(unsafe.Pointer(&data)), + ) + if err != nil { + return false + } + + return data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 +} From 84a02445d1914f6c35193348581cce52560e0847 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 3 Nov 2025 19:29:40 -0800 Subject: [PATCH 2/2] Nil it out, comment --- internal/vfs/osvfs/symlink_other.go | 5 +---- internal/vfs/osvfs/symlink_windows.go | 1 - 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/internal/vfs/osvfs/symlink_other.go b/internal/vfs/osvfs/symlink_other.go index 19909e5cf5..799c60e22f 100644 --- a/internal/vfs/osvfs/symlink_other.go +++ b/internal/vfs/osvfs/symlink_other.go @@ -2,9 +2,6 @@ package osvfs -// isSymlinkOrJunction always returns false on non-Windows platforms. // On Unix-like systems, symlinks are already properly detected by the // fs.ModeSymlink bit in the directory entry type, so this check is not needed. -func isSymlinkOrJunction(path string) bool { - return false -} +var isSymlinkOrJunction func(path string) bool diff --git a/internal/vfs/osvfs/symlink_windows.go b/internal/vfs/osvfs/symlink_windows.go index 9a4ffc921d..66581fccce 100644 --- a/internal/vfs/osvfs/symlink_windows.go +++ b/internal/vfs/osvfs/symlink_windows.go @@ -7,7 +7,6 @@ import ( // isSymlinkOrJunction checks if the given path is a symlink or junction point // on Windows by checking the FILE_ATTRIBUTE_REPARSE_POINT attribute. -// This is more efficient than calling realpath and comparing paths. func isSymlinkOrJunction(path string) bool { pathUTF16, err := syscall.UTF16PtrFromString(path) if err != nil {