]> Cypherpunks repositories - gostls13.git/commitdiff
io/fs, os: unify PathError.Path for dirFS.{ReadFile, ReadDir}
authorJes Cok <xigua67damn@gmail.com>
Fri, 24 Nov 2023 10:02:34 +0000 (10:02 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 27 Nov 2023 16:25:41 +0000 (16:25 +0000)
Fixes #64366

Change-Id: Ie78ab2cb9e11c0766665cd37fd7a26d36a1c24fa
GitHub-Last-Rev: 3cb3bb84a0ccccc88ba30258f52f699ac1364a7b
GitHub-Pull-Request: golang/go#64372
Reviewed-on: https://go-review.googlesource.com/c/go/+/544835
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Jes Cok <xigua67damn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/io/fs/readdir_test.go
src/io/fs/readfile_test.go
src/os/file.go

index a2b2c121ffadbd32280630a75df42d6b2e12e581..4c409ae7a010e2cb9c202a2431674de39a235f3a 100644 (file)
@@ -5,6 +5,7 @@
 package fs_test
 
 import (
+       "errors"
        . "io/fs"
        "os"
        "testing"
@@ -91,3 +92,20 @@ func TestFileInfoToDirEntry(t *testing.T) {
                })
        }
 }
+
+func errorPath(err error) string {
+       var perr *PathError
+       if !errors.As(err, &perr) {
+               return ""
+       }
+       return perr.Path
+}
+
+func TestReadDirPath(t *testing.T) {
+       fsys := os.DirFS(t.TempDir())
+       _, err1 := ReadDir(fsys, "non-existent")
+       _, err2 := ReadDir(struct{ FS }{fsys}, "non-existent")
+       if s1, s2 := errorPath(err1), errorPath(err2); s1 != s2 {
+               t.Fatalf("s1: %s != s2: %s", s1, s2)
+       }
+}
index 07219c14458fe80c33d8d11cb302ed310b9087a2..3c521f61427c2ba1796d314925c1699fc5e4baf3 100644 (file)
@@ -6,6 +6,7 @@ package fs_test
 
 import (
        . "io/fs"
+       "os"
        "testing"
        "testing/fstest"
        "time"
@@ -57,3 +58,12 @@ func TestReadFile(t *testing.T) {
                t.Fatalf(`ReadFile(sub(.), "hello.txt") = %q, %v, want %q, nil`, data, err, "hello, world")
        }
 }
+
+func TestReadFilePath(t *testing.T) {
+       fsys := os.DirFS(t.TempDir())
+       _, err1 := ReadFile(fsys, "non-existent")
+       _, err2 := ReadFile(struct{ FS }{fsys}, "non-existent")
+       if s1, s2 := errorPath(err1), errorPath(err2); s1 != s2 {
+               t.Fatalf("s1: %s != s2: %s", s1, s2)
+       }
+}
index 37a30ccf041919e6493181a9dcb83bc708eb2abf..6fd0550eeb06b855f5d90b8a3d8e049c45211a01 100644 (file)
@@ -690,7 +690,15 @@ func (dir dirFS) ReadFile(name string) ([]byte, error) {
        if err != nil {
                return nil, &PathError{Op: "readfile", Path: name, Err: err}
        }
-       return ReadFile(fullname)
+       b, err := ReadFile(fullname)
+       if err != nil {
+               if e, ok := err.(*PathError); ok {
+                       // See comment in dirFS.Open.
+                       e.Path = name
+               }
+               return nil, err
+       }
+       return b, nil
 }
 
 // ReadDir reads the named directory, returning all its directory entries sorted
@@ -700,7 +708,15 @@ func (dir dirFS) ReadDir(name string) ([]DirEntry, error) {
        if err != nil {
                return nil, &PathError{Op: "readdir", Path: name, Err: err}
        }
-       return ReadDir(fullname)
+       entries, err := ReadDir(fullname)
+       if err != nil {
+               if e, ok := err.(*PathError); ok {
+                       // See comment in dirFS.Open.
+                       e.Path = name
+               }
+               return nil, err
+       }
+       return entries, nil
 }
 
 func (dir dirFS) Stat(name string) (fs.FileInfo, error) {