]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.24] os: set full name for Roots created with Root.OpenRoot
authorDamien Neil <dneil@google.com>
Fri, 22 Aug 2025 17:47:01 +0000 (10:47 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 16 Sep 2025 18:04:27 +0000 (11:04 -0700)
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 #75138

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/+/704278
Auto-Submit: Michael Knyszek <mknyszek@google.com>

src/os/root_test.go
src/os/root_unix.go
src/os/root_windows.go

index 908d59896d26b91ae778970d0807bb2c9fa30fc1..2cc32772de38ac2e0348f410e77cd732cc73231d 100644 (file)
@@ -1265,3 +1265,36 @@ func TestOpenInRoot(t *testing.T) {
                }
        }
 }
+
+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)
+       }
+}
index 02d3b4bdad007e0de69ebb2ffbd206bf2147cea0..c4118646141806b2e5eaa662b1f41e188dcfbcaa 100644 (file)
@@ -71,7 +71,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.
index 32dfa070b740fd2577b295dbf3457335c7f9ad0e..4467fdf95bab552fc0d83e8e0671ceea5cfe4d0f 100644 (file)
@@ -119,7 +119,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.