]> Cypherpunks repositories - gostls13.git/commitdiff
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)
committerDamien Neil <dneil@google.com>
Mon, 25 Aug 2025 15:33:58 +0000 (08:33 -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.

Fixes #73868

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>

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

index effcdeab433e780e451b8b7edcdbaa8c6558e6bf..f9fbd11575e6a058683a49080863cef359e3ed46 100644 (file)
@@ -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)
+       }
+}
index 4d6fc19a080434a6dbf46ab3c058afc57947be2f..c891e81b793bb5c3c4853107a817fa33a816d635 100644 (file)
@@ -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.
index afc645d8f88822b3e0bf4dd7beaa833b62ba7983..033a119862d2928f9cbc083adf052f474a347ee3 100644 (file)
@@ -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.