-
Notifications
You must be signed in to change notification settings - Fork 730
Detect Windows junctions with GetFileAttributesEx #2013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //go:build !windows | ||
|
|
||
| package osvfs | ||
|
|
||
| // 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. | ||
| var isSymlinkOrJunction func(path string) bool |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package osvfs | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
| func isSymlinkOrJunction(path string) bool { | ||
| pathUTF16, err := syscall.UTF16PtrFromString(path) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| var data syscall.Win32FileAttributeData | ||
| err = syscall.GetFileAttributesEx( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to prepend There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, crap, converting those is a whole thing that is hard and not available to us outside stdlib. I'd need to do something like I did in realpath func realpath(path string) (string, error) {
var h windows.Handle
if len(path) < 248 {
var err error
h, err = openMetadata(path)
if err != nil {
return "", err
}
defer windows.CloseHandle(h) //nolint:errcheck
} else {
// For long paths, defer to os.Open to run the path through fixLongPath.
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
// Works on directories too since https://go.dev/cl/405275.
h = windows.Handle(f.Fd())
}Guess I'll wait on things until I figure that out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't block this on it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's pretty easy to hit this on the use cases where it matters, e.g. pnpm ones, so I should definitely check it in afraid |
||
| pathUTF16, | ||
| syscall.GetFileExInfoStandard, | ||
| (*byte)(unsafe.Pointer(&data)), | ||
| ) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| return data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If something is a junction, maybe we don't even need to stat it and just say that it's a dir? (I don't believe junctions/reparse points can be files, in which case I could rename funcs too)