Skip to content

Commit

Permalink
os: adjust error in Stat on windows
Browse files Browse the repository at this point in the history
Current code could return a non-nil os.FileInfo even if there is an error.
This is a bit incompatible with Stat on other OSes.

Change-Id: I37b608da234f957bb89b82509649de78ccc70bbb
Reviewed-on: https://go-review.googlesource.com/40330
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
hirochachacha authored and bradfitz committed Apr 11, 2017
1 parent e367ba9 commit 5a8eae6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/os/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,45 @@ func TestStat(t *testing.T) {
}
}

func TestStatError(t *testing.T) {
defer chtmpdir(t)()

path := "no-such-file"
Remove(path) // Just in case

fi, err := Stat(path)
if err == nil {
t.Fatal("got nil, want error")
}
if fi != nil {
t.Errorf("got %v, want nil", fi)
}
if perr, ok := err.(*PathError); !ok {
t.Errorf("got %T, want %T", err, perr)
}

testenv.MustHaveSymlink(t)

link := "symlink"
Remove(link) // Just in case
err = Symlink(path, link)
if err != nil {
t.Fatal(err)
}
defer Remove(link)

fi, err = Stat(link)
if err == nil {
t.Fatal("got nil, want error")
}
if fi != nil {
t.Errorf("got %v, want nil", fi)
}
if perr, ok := err.(*PathError); !ok {
t.Errorf("got %T, want %T", err, perr)
}
}

func TestFstat(t *testing.T) {
path := sfdir + "/" + sfname
file, err1 := Open(path)
Expand Down
4 changes: 2 additions & 2 deletions src/os/stat_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ func Stat(name string) (FileInfo, error) {
for i := 0; i < 255; i++ {
fi, err = Lstat(name)
if err != nil {
return fi, err
return nil, err
}
if fi.Mode()&ModeSymlink == 0 {
return fi, nil
}
newname, err := Readlink(name)
if err != nil {
return fi, err
return nil, err
}
if isAbs(newname) {
name = newname
Expand Down

0 comments on commit 5a8eae6

Please sign in to comment.