]> Cypherpunks repositories - gostls13.git/commitdiff
os: return a *PathError from Readdirnames and Readdir on POSIX platforms
authorBryan C. Mills <bcmills@google.com>
Wed, 13 May 2020 18:59:29 +0000 (14:59 -0400)
committerBryan C. Mills <bcmills@google.com>
Tue, 25 Aug 2020 04:39:40 +0000 (04:39 +0000)
Previously, Readdirnames returned a *PathError on Windows and Plan 9,
but a *SyscallError on POSIX systems.

In contrast, similar methods (such as Stat) return a *PathError on all platforms.

Fixes #38923

Change-Id: I26395905b1e723933f07b792c7aeee7c335949cd
Reviewed-on: https://go-review.googlesource.com/c/go/+/233917
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/os/dir_darwin.go
src/os/dir_unix.go
src/os/os_test.go

index 87797e2ddaf885f79dc892af641584b248e3d5fe..476af6862e66447540be4e36789fe8884d28eaed 100644 (file)
@@ -28,7 +28,7 @@ func (f *File) readdirnames(n int) (names []string, err error) {
        if f.dirinfo == nil {
                dir, call, errno := f.pfd.OpenDir()
                if errno != nil {
-                       return nil, wrapSyscallError(call, errno)
+                       return nil, &PathError{call, f.name, errno}
                }
                f.dirinfo = &dirInfo{
                        dir: dir,
@@ -46,11 +46,11 @@ func (f *File) readdirnames(n int) (names []string, err error) {
        var dirent syscall.Dirent
        var entptr *syscall.Dirent
        for len(names) < size || n == -1 {
-               if res := readdir_r(d.dir, &dirent, &entptr); res != 0 {
-                       if syscall.Errno(res) == syscall.EINTR {
+               if errno := readdir_r(d.dir, &dirent, &entptr); errno != 0 {
+                       if errno == syscall.EINTR {
                                continue
                        }
-                       return names, wrapSyscallError("readdir", syscall.Errno(res))
+                       return names, &PathError{"readdir", f.name, errno}
                }
                if entptr == nil { // EOF
                        break
@@ -84,4 +84,4 @@ func (f *File) readdirnames(n int) (names []string, err error) {
 func closedir(dir uintptr) (err error)
 
 //go:linkname readdir_r syscall.readdir_r
-func readdir_r(dir uintptr, entry *syscall.Dirent, result **syscall.Dirent) (res int)
+func readdir_r(dir uintptr, entry *syscall.Dirent, result **syscall.Dirent) (res syscall.Errno)
index e0c4989756b1a0ec983d21e19241a7f2e143b439..58ec406ab87804f0a7953f35b1b589d4156b0bc0 100644 (file)
@@ -50,7 +50,7 @@ func (f *File) readdirnames(n int) (names []string, err error) {
                        d.nbuf, errno = f.pfd.ReadDirent(d.buf)
                        runtime.KeepAlive(f)
                        if errno != nil {
-                               return names, wrapSyscallError("readdirent", errno)
+                               return names, &PathError{"readdirent", f.name, errno}
                        }
                        if d.nbuf <= 0 {
                                break // EOF
index e8c64510f500d4d5cf256baf17443dd0e940c5dc..520916d8800d416d9fa428dc8539efa939c746c7 100644 (file)
@@ -688,6 +688,10 @@ func TestReaddirOfFile(t *testing.T) {
        if err == nil {
                t.Error("Readdirnames succeeded; want non-nil error")
        }
+       var pe *PathError
+       if !errors.As(err, &pe) || pe.Path != f.Name() {
+               t.Errorf("Readdirnames returned %q; want a PathError with path %q", err, f.Name())
+       }
        if len(names) > 0 {
                t.Errorf("unexpected dir names in regular file: %q", names)
        }