]> Cypherpunks repositories - gostls13.git/commitdiff
os: adjust error in Stat on windows
authorHiroshi Ioka <hirochachacha@gmail.com>
Tue, 11 Apr 2017 00:54:08 +0000 (09:54 +0900)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 11 Apr 2017 04:09:09 +0000 (04:09 +0000)
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>
src/os/os_test.go
src/os/stat_windows.go

index fcfcc436203a21f613a12862424eb98a7eca5624..410bfc8007076855cb4fc8437c4695581e55e477 100644 (file)
@@ -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)
index fe0ca8d1b257affeef90b33d060da3c14e58d0b3..3c640ce992c904285982b05b659a8a0c3858d32f 100644 (file)
@@ -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