From: Jes Cok Date: Fri, 24 Nov 2023 10:02:34 +0000 (+0000) Subject: io/fs, os: unify PathError.Path for dirFS.{ReadFile, ReadDir} X-Git-Tag: go1.22rc1~153 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d961b12be9001cf8dbf8f52847607dbf84d94f8d;p=gostls13.git io/fs, os: unify PathError.Path for dirFS.{ReadFile, ReadDir} 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 Run-TryBot: Jes Cok LUCI-TryBot-Result: Go LUCI Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Bryan Mills --- diff --git a/src/io/fs/readdir_test.go b/src/io/fs/readdir_test.go index a2b2c121ff..4c409ae7a0 100644 --- a/src/io/fs/readdir_test.go +++ b/src/io/fs/readdir_test.go @@ -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) + } +} diff --git a/src/io/fs/readfile_test.go b/src/io/fs/readfile_test.go index 07219c1445..3c521f6142 100644 --- a/src/io/fs/readfile_test.go +++ b/src/io/fs/readfile_test.go @@ -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) + } +} diff --git a/src/os/file.go b/src/os/file.go index 37a30ccf04..6fd0550eeb 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -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) {