-
Notifications
You must be signed in to change notification settings - Fork 735
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dd027bb
Replace Realpath with more efficient GetFileAttributesEx
jakebailey 84a0244
Nil it out, comment
jakebailey 2527991
Tessts
jakebailey 8e945e4
Fix tests a little
jakebailey 7c7e29b
lint
jakebailey bbe9968
Install microsoft go fork to see if CI catches it
jakebailey 1b1dc85
Pull in fix from go-infra PR
jakebailey 5d59ec8
Uncomment fix now that CI proves it matters
jakebailey d30517a
Move impl
jakebailey 726a811
Update comments
jakebailey 6c9d09c
one more last thing
jakebailey 62f3af0
Merge branch 'main' into jabaile/windows-junctions
jakebailey cffb450
Merge branch 'main' into jabaile/windows-junctions
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package osvfs | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func mklink(tb testing.TB, target, link string, isDir bool) { | ||
| tb.Helper() | ||
|
|
||
| if runtime.GOOS == "windows" && isDir { | ||
| // Don't use os.Symlink on Windows, as it creates a "real" symlink, not a junction. | ||
| assert.NilError(tb, exec.Command("cmd", "/c", "mklink", "/J", link, target).Run()) | ||
| } else { | ||
| err := os.Symlink(target, link) | ||
| if err != nil && !isDir && runtime.GOOS == "windows" && strings.Contains(err.Error(), "A required privilege is not held by the client") { | ||
| tb.Log(err) | ||
| tb.Skip("file symlink support is not enabled without elevation or developer mode") | ||
| } | ||
| assert.NilError(tb, err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| //go:build !windows | ||
|
|
||
| package osvfs | ||
|
|
||
| // Only Windows has reparse points; leave this nil for other OSes. | ||
| var isReparsePoint func(path string) bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package osvfs | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "syscall" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| func isReparsePoint(path string) bool { | ||
| if len(path) >= 248 { | ||
| path = `\\?\` + path | ||
| } | ||
|
|
||
| pathUTF16, err := syscall.UTF16PtrFromString(path) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| var data syscall.Win32FileAttributeData | ||
| err = syscall.GetFileAttributesEx( | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pathUTF16, | ||
| syscall.GetFileExInfoStandard, | ||
| (*byte)(unsafe.Pointer(&data)), | ||
| ) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| return data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| package osvfs | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestIsReparsePoint(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tmp := t.TempDir() | ||
|
|
||
| t.Run("regular file", func(t *testing.T) { | ||
| t.Parallel() | ||
| file := filepath.Join(tmp, "regular.txt") | ||
| assert.NilError(t, os.WriteFile(file, []byte("hello"), 0o666)) | ||
| assert.Equal(t, isReparsePoint(file), false) | ||
| }) | ||
|
|
||
| t.Run("regular directory", func(t *testing.T) { | ||
| t.Parallel() | ||
| dir := filepath.Join(tmp, "regular-dir") | ||
| assert.NilError(t, os.MkdirAll(dir, 0o777)) | ||
| assert.Equal(t, isReparsePoint(dir), false) | ||
| }) | ||
|
|
||
| t.Run("junction point", func(t *testing.T) { | ||
| t.Parallel() | ||
| target := filepath.Join(tmp, "junction-target") | ||
| link := filepath.Join(tmp, "junction-link") | ||
| assert.NilError(t, os.MkdirAll(target, 0o777)) | ||
| mklink(t, target, link, true) | ||
| assert.Equal(t, isReparsePoint(link), true) | ||
| }) | ||
|
|
||
| t.Run("file symlink", func(t *testing.T) { | ||
| t.Parallel() | ||
| target := filepath.Join(tmp, "symlink-target.txt") | ||
| link := filepath.Join(tmp, "symlink-link.txt") | ||
| assert.NilError(t, os.WriteFile(target, []byte("hello"), 0o666)) | ||
| mklink(t, target, link, false) | ||
| assert.Equal(t, isReparsePoint(link), true) | ||
| }) | ||
|
|
||
| t.Run("directory symlink", func(t *testing.T) { | ||
| t.Parallel() | ||
| target := filepath.Join(tmp, "dir-symlink-target") | ||
| link := filepath.Join(tmp, "dir-symlink-link") | ||
| assert.NilError(t, os.MkdirAll(target, 0o777)) | ||
| mklink(t, target, link, false) | ||
| assert.Equal(t, isReparsePoint(link), true) | ||
| }) | ||
|
|
||
| t.Run("nonexistent path", func(t *testing.T) { | ||
| t.Parallel() | ||
| nonexistent := filepath.Join(tmp, "does-not-exist") | ||
| assert.Equal(t, isReparsePoint(nonexistent), false) | ||
| }) | ||
|
|
||
| t.Run("empty path", func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, isReparsePoint(""), false) | ||
| }) | ||
|
|
||
| t.Run("invalid path with null byte", func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, isReparsePoint("invalid\x00path"), false) | ||
| }) | ||
| } | ||
|
|
||
| func TestIsReparsePointLongPath(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tmp := t.TempDir() | ||
|
|
||
| // Create a deeply nested path that exceeds 248 characters | ||
| longPathBase := tmp | ||
| pathComponent := "very_long_directory_name_to_exceed_max_path_limit_abcdefghijklmnopqrstuvwxyz" | ||
|
|
||
| for len(longPathBase) < 250 { | ||
| longPathBase = filepath.Join(longPathBase, pathComponent) | ||
| } | ||
|
|
||
| target := filepath.Join(longPathBase, "target") | ||
| link := filepath.Join(longPathBase, "link") | ||
|
|
||
| // Use \\?\ prefix to enable long path support for mklink | ||
| longTarget := `\\?\` + target | ||
| longLink := `\\?\` + link | ||
|
|
||
| assert.NilError(t, os.MkdirAll(longTarget, 0o777)) | ||
| assert.NilError(t, exec.Command("cmd", "/c", "mklink", "/J", longLink, longTarget).Run()) | ||
|
|
||
| // With long path support enabled, this should work even for paths >= 248 chars | ||
| assert.Equal(t, isReparsePoint(link), true) | ||
| } | ||
|
|
||
| func TestIsReparsePointNestedInSymlink(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tmp := t.TempDir() | ||
|
|
||
| // Create a structure: target/inner-target, link -> target, then check link/inner-link | ||
| target := filepath.Join(tmp, "target") | ||
| innerTarget := filepath.Join(target, "inner-target") | ||
| assert.NilError(t, os.MkdirAll(innerTarget, 0o777)) | ||
|
|
||
| link := filepath.Join(tmp, "link") | ||
| mklink(t, target, link, true) | ||
|
|
||
| // Create a junction inside the target | ||
| innerLink := filepath.Join(target, "inner-link") | ||
| mklink(t, innerTarget, innerLink, true) | ||
|
|
||
| // Check the junction through the symlink path | ||
| nestedPath := filepath.Join(link, "inner-link") | ||
| assert.Equal(t, isReparsePoint(nestedPath), true) | ||
| } | ||
|
|
||
| func TestIsReparsePointRelativePath(t *testing.T) { //nolint:paralleltest // Cannot use t.Parallel() with t.Chdir() | ||
| tmp := t.TempDir() | ||
| t.Chdir(tmp) | ||
|
|
||
| target := "target-rel" | ||
| link := "link-rel" | ||
| assert.NilError(t, os.MkdirAll(target, 0o777)) | ||
| mklink(t, target, link, true) | ||
|
|
||
| assert.Equal(t, isReparsePoint(link), true) | ||
| assert.Equal(t, isReparsePoint(target), false) | ||
| } | ||
|
|
||
| func BenchmarkIsSymlinkOrJunction(b *testing.B) { | ||
| tmp := b.TempDir() | ||
|
|
||
| regularFile := filepath.Join(tmp, "regular.txt") | ||
| assert.NilError(b, os.WriteFile(regularFile, []byte("hello"), 0o666)) | ||
|
|
||
| target := filepath.Join(tmp, "target") | ||
| link := filepath.Join(tmp, "link") | ||
| assert.NilError(b, os.MkdirAll(target, 0o777)) | ||
| assert.NilError(b, exec.Command("cmd", "/c", "mklink", "/J", link, target).Run()) | ||
|
|
||
| b.Run("regular file", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| for b.Loop() { | ||
| isReparsePoint(regularFile) | ||
| } | ||
| }) | ||
|
|
||
| b.Run("junction", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| for b.Loop() { | ||
| isReparsePoint(link) | ||
| } | ||
| }) | ||
|
|
||
| b.Run("nonexistent", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| nonexistent := filepath.Join(tmp, "does-not-exist") | ||
| for b.Loop() { | ||
| isReparsePoint(nonexistent) | ||
| } | ||
| }) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.