Skip to content
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

os: Symbolic links to empty directories broken on Windows #13658

Closed
taruti opened this issue Dec 17, 2015 · 2 comments
Closed

os: Symbolic links to empty directories broken on Windows #13658

taruti opened this issue Dec 17, 2015 · 2 comments

Comments

@taruti
Copy link
Contributor

@taruti taruti commented Dec 17, 2015

Opening a symlink pointing to an empty directory fails - os.Open returns nil,nil.

On Windows opening a directory ends up in file_windows.go openDir function. If the directory is empty then ERROR_FILE_NOT_FOUND is returned and it goes to the if brances. Then syscall.GetFileAttributesEx is called.

However GetFileAttributesEx is like Lstat - not Stat.

As per https://msdn.microsoft.com/en-us/library/windows/desktop/aa365682%28v=vs.85%29.aspx#getfileattributes "If the path points to a symbolic link, the function returns attributes for the symbolic link.".

Thus the if fa.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY == 0 { succeeds and it returns nil, nil.

@alexbrainman

This comment has been minimized.

Copy link
Member

@alexbrainman alexbrainman commented Dec 17, 2015

I don't understand what the problem is. I run this test:

diff --git a/src/os/os_test.go b/src/os/os_test.go
index 5689e77..7a0e221 100644
--- a/src/os/os_test.go
+++ b/src/os/os_test.go
@@ -1757,3 +1757,48 @@ func TestRemoveAllRace(t *testing.T) {
  close(hold) // let workers race to remove root
  wg.Wait()
 }
+
+func TestOpenSymlinkToEmptyDirectory(t *testing.T) {
+ switch runtime.GOOS {
+ case "android", "nacl", "plan9":
+ t.Skipf("skipping on %s", runtime.GOOS)
+ }
+ if !supportsSymlinks {
+ t.Skip("skipping because symlinks are not supported")
+ }
+
+ tmpDir, err := ioutil.TempDir("", "OpenSymlink")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer RemoveAll(tmpDir)
+
+ dir := filepath.Join(tmpDir, "dir")
+ err = Mkdir(dir, 0755)
+ if err != nil {
+ t.Fatal(err)
+ }
+ link := filepath.Join(tmpDir, "link")
+ err = Symlink(dir, link)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ f, err := Open(link)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+
+ if f == nil {
+ t.Fatal("Open returns nil for *File")
+ }
+
+ names, err := f.Readdirnames(-1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(names) != 0 {
+ t.Fatalf("Readdirnames expected to return no names, but %v returned", names)
+ }
+}

and it succeeds. Why?

Alex

@taruti

This comment has been minimized.

Copy link
Contributor Author

@taruti taruti commented Dec 18, 2015

Looking at this further:

Windows symlinks are typed.

I.e. 1) Create a symlink to a non-directory, 2) target is deleted, and there is now an empty directory, 3) target file system thinks this is fine, 4) Symlink now should point to a directory and try to open it -> everything fails.

This seems like a limitation in Windows, closing it.

@taruti taruti closed this Dec 18, 2015
@golang golang locked and limited conversation to collaborators Dec 29, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.