From 6084848e5a05666cabab43bd96d2419b879fd405 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 16 Nov 2024 15:38:40 +0100 Subject: [PATCH] fs: fix O_NOFOLLOW for metadata handles on Windows --- internal/fs/const_windows.go | 6 ++++-- internal/fs/file.go | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/fs/const_windows.go b/internal/fs/const_windows.go index 4c29e0b9d..b2b1bab86 100644 --- a/internal/fs/const_windows.go +++ b/internal/fs/const_windows.go @@ -5,8 +5,10 @@ package fs // TODO honor flags when opening files -// O_NOFOLLOW is a noop on Windows. -const O_NOFOLLOW int = 0 +// O_NOFOLLOW is currently only interpreted by FS.OpenFile in metadataOnly mode and ignored by OpenFile. +// The value of the constant is invented and only for use within this fs package. It must not be used in other contexts. +// It must not conflict with the other O_* values from go/src/syscall/types_windows.go +const O_NOFOLLOW int = 0x40000000 // O_DIRECTORY is a noop on Windows. const O_DIRECTORY int = 0 diff --git a/internal/fs/file.go b/internal/fs/file.go index 81ee4bc7a..57f1a996a 100644 --- a/internal/fs/file.go +++ b/internal/fs/file.go @@ -3,6 +3,7 @@ package fs import ( "fmt" "os" + "runtime" ) // MkdirAll creates a directory named path, along with any necessary parents, @@ -47,6 +48,9 @@ func Lstat(name string) (os.FileInfo, error) { // methods on the returned File can be used for I/O. // If there is an error, it will be of type *PathError. func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { + if runtime.GOOS == "windows" { + flag &^= O_NOFOLLOW + } return os.OpenFile(fixpath(name), flag, perm) }