Skip to content

Commit

Permalink
os: use Stat instead of Lstat in Symlink
Browse files Browse the repository at this point in the history
Windows implementation of Symlink uses CreateSymbolicLink Windows
API. The API requires to identify the target type: file or
directory. Current Symlink implementation  uses Lstat to determine
symlink type, but Lstat will not be able to determine correct
result if destination is symlink. Replace Lstat call with Stat.

Fixes #28432

Change-Id: Ibee6d8ac21e2246bf8d0a019c4c66d38b09887d4
Reviewed-on: https://go-review.googlesource.com/c/145217
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
alexbrainman committed Oct 31, 2018
1 parent fde4b9e commit 5cc8089
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/os/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func Symlink(oldname, newname string) error {
destpath = dirname(newname) + `\` + oldname
}

fi, err := Lstat(destpath)
fi, err := Stat(destpath)
isdir := err == nil && fi.IsDir()

n, err := syscall.UTF16PtrFromString(fixLongPath(newname))
Expand Down
16 changes: 16 additions & 0 deletions src/os/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ func TestDirAndSymlinkStats(t *testing.T) {
}
testSymlinkStats(t, dirlink, true)
testSymlinkSameFile(t, dir, dirlink)

linklink := filepath.Join(tmpdir, "linklink")
err = os.Symlink(dirlink, linklink)
if err != nil {
t.Fatal(err)
}
testSymlinkStats(t, linklink, true)
testSymlinkSameFile(t, dir, linklink)
}

func TestFileAndSymlinkStats(t *testing.T) {
Expand All @@ -230,6 +238,14 @@ func TestFileAndSymlinkStats(t *testing.T) {
}
testSymlinkStats(t, filelink, false)
testSymlinkSameFile(t, file, filelink)

linklink := filepath.Join(tmpdir, "linklink")
err = os.Symlink(filelink, linklink)
if err != nil {
t.Fatal(err)
}
testSymlinkStats(t, linklink, false)
testSymlinkSameFile(t, file, linklink)
}

// see issue 27225 for details
Expand Down

0 comments on commit 5cc8089

Please sign in to comment.