Commit 87bd0b2f authored by Hiroshi Ioka's avatar Hiroshi Ioka Committed by Alex Brainman

os: handle relative symlinks correctly in Stat on windows

Walk relative symlinks in windows os.Stat from
symlink path instead of from current directory.

Fixes #19870

Change-Id: I0a27473d11485f073084b1f19b30c5b3a2fbc0f7
Reviewed-on: https://go-review.googlesource.com/39932Reviewed-by: default avatarAlex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 7e1832d0
......@@ -1705,6 +1705,43 @@ func TestStatStdin(t *testing.T) {
}
}
func TestStatRelativeSymlink(t *testing.T) {
testenv.MustHaveSymlink(t)
tmpdir, err := ioutil.TempDir("", "TestStatRelativeSymlink")
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tmpdir)
target := filepath.Join(tmpdir, "target")
f, err := Create(target)
if err != nil {
t.Fatal(err)
}
defer f.Close()
st, err := f.Stat()
if err != nil {
t.Fatal(err)
}
link := filepath.Join(tmpdir, "link")
err = Symlink(filepath.Base(target), link)
if err != nil {
t.Fatal(err)
}
st1, err := Stat(link)
if err != nil {
t.Fatal(err)
}
if !SameFile(st, st1) {
t.Error("Stat doesn't follow relative symlink")
}
}
func TestReadAtEOF(t *testing.T) {
f := newFile("TestReadAtEOF", t)
defer Remove(f.Name())
......
......@@ -71,10 +71,15 @@ func Stat(name string) (FileInfo, error) {
if fi.Mode()&ModeSymlink == 0 {
return fi, nil
}
name, err = Readlink(name)
newname, err := Readlink(name)
if err != nil {
return fi, err
}
if isAbs(newname) {
name = newname
} else {
name = dirname(name) + `\` + newname
}
}
return nil, &PathError{"Stat", name, syscall.ELOOP}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment