diff options
| author | Damien Neil <dneil@google.com> | 2025-08-22 10:47:01 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2025-09-22 09:47:39 -0700 |
| commit | f75bcffa4a739811a10f5f08096aac93e148971e (patch) | |
| tree | 565397596b958735b2ac29cd7a9ab9383ef1a435 | |
| parent | 7d570090a9705a74127304cbe0d900fc1b813302 (diff) | |
| download | go-f75bcffa4a739811a10f5f08096aac93e148971e.tar.xz | |
[release-branch.go1.25] os: set full name for Roots created with Root.OpenRoot
Set the Name for a Root created within a Root to be the
concatenation of the parent's path and the name used to open the child.
This matches the behavior for files opened within a Root
with Root.Open.
For #73868
Fixes #75139
Change-Id: Idf4021602ac25556721b7ef6924dec652c7bf4db
Reviewed-on: https://go-review.googlesource.com/c/go/+/698376
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
(cherry picked from commit ed7f804775725149088a71108efd0b20ef9f206f)
Reviewed-on: https://go-review.googlesource.com/c/go/+/704277
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
| -rw-r--r-- | src/os/root_test.go | 33 | ||||
| -rw-r--r-- | src/os/root_unix.go | 2 | ||||
| -rw-r--r-- | src/os/root_windows.go | 2 |
3 files changed, 35 insertions, 2 deletions
diff --git a/src/os/root_test.go b/src/os/root_test.go index effcdeab43..f9fbd11575 100644 --- a/src/os/root_test.go +++ b/src/os/root_test.go @@ -1919,3 +1919,36 @@ func TestRootWriteReadFile(t *testing.T) { t.Fatalf("root.ReadFile(%q) = %q, %v; want %q, nil", name, got, err, want) } } + +func TestRootName(t *testing.T) { + dir := t.TempDir() + root, err := os.OpenRoot(dir) + if err != nil { + t.Fatal(err) + } + defer root.Close() + if got, want := root.Name(), dir; got != want { + t.Errorf("root.Name() = %q, want %q", got, want) + } + + f, err := root.Create("file") + if err != nil { + t.Fatal(err) + } + defer f.Close() + if got, want := f.Name(), filepath.Join(dir, "file"); got != want { + t.Errorf(`root.Create("file").Name() = %q, want %q`, got, want) + } + + if err := root.Mkdir("dir", 0o777); err != nil { + t.Fatal(err) + } + subroot, err := root.OpenRoot("dir") + if err != nil { + t.Fatal(err) + } + defer subroot.Close() + if got, want := subroot.Name(), filepath.Join(dir, "dir"); got != want { + t.Errorf(`root.OpenRoot("dir").Name() = %q, want %q`, got, want) + } +} diff --git a/src/os/root_unix.go b/src/os/root_unix.go index 4d6fc19a08..c891e81b79 100644 --- a/src/os/root_unix.go +++ b/src/os/root_unix.go @@ -75,7 +75,7 @@ func openRootInRoot(r *Root, name string) (*Root, error) { if err != nil { return nil, &PathError{Op: "openat", Path: name, Err: err} } - return newRoot(fd, name) + return newRoot(fd, joinPath(r.Name(), name)) } // rootOpenFileNolog is Root.OpenFile. diff --git a/src/os/root_windows.go b/src/os/root_windows.go index 523ee48d13..2ec09cf2d3 100644 --- a/src/os/root_windows.go +++ b/src/os/root_windows.go @@ -123,7 +123,7 @@ func openRootInRoot(r *Root, name string) (*Root, error) { if err != nil { return nil, &PathError{Op: "openat", Path: name, Err: err} } - return newRoot(fd, name) + return newRoot(fd, joinPath(r.Name(), name)) } // rootOpenFileNolog is Root.OpenFile. |
