os: rewrite PathError.Path in dirFS.ReadLink to relative name
All other dirFS methods (Open, ReadFile, ReadDir, Stat, Lstat) rewrite
PathError.Path to the relative name on error. Do the same in
dirFS.ReadLink to be consistent and so callers of
DirFS("/prefix").ReadLink("file") see PathError{Path: "file"} instead of
PathError{Path: "/prefix/file"}.
Change-Id: Id49cb43d79f459c855f94e5544e4c727fe68a7b4
Reviewed-on: https://go-review.googlesource.com/c/go/+/796540
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/src/os/file.go b/src/os/file.go
index 8085724..e23a08b 100644
--- a/src/os/file.go
+++ b/src/os/file.go
@@ -839,7 +839,13 @@
if err != nil {
return "", &PathError{Op: "readlink", Path: name, Err: err}
}
- return Readlink(fullname)
+ f, err := Readlink(fullname)
+ if err != nil {
+ // See comment in dirFS.Open.
+ err.(*PathError).Path = name
+ return "", err
+ }
+ return f, nil
}
// join returns the path for name in dir.
diff --git a/src/os/file_test.go b/src/os/file_test.go
index f56a34d..fec9007 100644
--- a/src/os/file_test.go
+++ b/src/os/file_test.go
@@ -49,6 +49,19 @@
t.Errorf("fs.ReadLink(fsys, %q) = %q, %v; want %q, <nil>", name, got, err, want)
}
}
+
+ const nonesuch = "dir/nonesuch"
+ _, err := fs.ReadLink(fsys, nonesuch)
+ if err == nil {
+ t.Fatal("fs.ReadLink of nonexistent file succeeded")
+ }
+ pe, ok := err.(*PathError)
+ if !ok {
+ t.Fatalf("fs.ReadLink error type = %T; want *PathError", err)
+ }
+ if pe.Path != nonesuch {
+ t.Errorf("fs.ReadLink(%q) error path = %q; want %q", nonesuch, pe.Path, nonesuch)
+ }
}
func TestDirFSLstat(t *testing.T) {
@@ -78,6 +91,19 @@
t.Errorf("fs.Lstat(fsys, %q).Mode().Type() = %v, %v; want %v, <nil>", name, got, err, want)
}
}
+
+ const nonesuch = "dir/nonesuch"
+ _, err := fs.Lstat(fsys, nonesuch)
+ if err == nil {
+ t.Fatal("fs.Lstat of nonexistent file succeeded")
+ }
+ pe, ok := err.(*PathError)
+ if !ok {
+ t.Fatalf("fs.Lstat error type = %T; want *PathError", err)
+ }
+ if pe.Path != nonesuch {
+ t.Errorf("fs.Lstat(%q) error path = %q; want %q", nonesuch, pe.Path, nonesuch)
+ }
}
func TestDirFSWalkDir(t *testing.T) {