From d30f8b6ed6a98d1fb942c5dc1315143eb9a901a7 Mon Sep 17 00:00:00 2001 From: Dan Kegel Date: Sun, 12 Dec 2021 15:16:52 -0800 Subject: [PATCH] os.Stat: returned error on nonexistent path did not satisfy IsNotExist Add direct test for the problem to make the fix commit clearer. Noticed while implementing MkdirTemp on mac; the upstream tests for MkdirTemp fail without this. --- src/os/os_anyos_test.go | 11 +++++++++++ src/os/stat_unix.go | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/os/os_anyos_test.go b/src/os/os_anyos_test.go index d428c49f..0c839ed5 100644 --- a/src/os/os_anyos_test.go +++ b/src/os/os_anyos_test.go @@ -4,6 +4,7 @@ package os_test import ( . "os" + "path/filepath" "strconv" "testing" "time" @@ -31,6 +32,16 @@ func TestMkdir(t *testing.T) { } } +func TestStatBadDir(t *testing.T) { + dir := TempDir() + badDir := filepath.Join(dir, "not-exist/really-not-exist") + _, err := Stat(badDir) + // TODO: PathError moved to io/fs in go 1.16; fix next line once we drop go 1.15 support. + if pe, ok := err.(*PathError); !ok || !IsNotExist(err) || pe.Path != badDir { + t.Errorf("Mkdir error = %#v; want PathError for path %q satisifying IsNotExist", err, badDir) + } +} + func writeFile(t *testing.T, fname string, flag int, text string) string { f, err := OpenFile(fname, flag, 0666) if err != nil { diff --git a/src/os/stat_unix.go b/src/os/stat_unix.go index 0f310648..9187b54e 100644 --- a/src/os/stat_unix.go +++ b/src/os/stat_unix.go @@ -19,7 +19,7 @@ func (f *File) Sync() error { func statNolog(name string) (FileInfo, error) { var fs fileStat err := ignoringEINTR(func() error { - return syscall.Stat(name, &fs.sys) + return handleSyscallError(syscall.Stat(name, &fs.sys)) }) if err != nil { return nil, &PathError{Op: "stat", Path: name, Err: err} @@ -32,7 +32,7 @@ func statNolog(name string) (FileInfo, error) { func lstatNolog(name string) (FileInfo, error) { var fs fileStat err := ignoringEINTR(func() error { - return syscall.Lstat(name, &fs.sys) + return handleSyscallError(syscall.Lstat(name, &fs.sys)) }) if err != nil { return nil, &PathError{Op: "lstat", Path: name, Err: err}