+pkg os, func OpenInRoot(string, string) (*File, error) #67002
pkg os, func OpenRoot(string) (*Root, error) #67002
pkg os, method (*Root) Close() error #67002
pkg os, method (*Root) Create(string) (*File, error) #67002
"slices"
)
+// OpenInRoot opens the file name in the directory dir.
+// It is equivalent to OpenRoot(dir) followed by opening the file in the root.
+//
+// OpenInRoot returns an error if any component of the name
+// references a location outside of dir.
+//
+// See [Root] for details and limitations.
+func OpenInRoot(dir, name string) (*File, error) {
+ r, err := OpenRoot(dir)
+ if err != nil {
+ return nil, err
+ }
+ defer r.Close()
+ return r.Open(name)
+}
+
// Root may be used to only access files within a single directory tree.
//
// Methods on Root can only access files and directories beneath a root directory.
}
}
}
+
+func TestOpenInRoot(t *testing.T) {
+ dir := makefs(t, []string{
+ "file",
+ "link => ../ROOT/file",
+ })
+ f, err := os.OpenInRoot(dir, "file")
+ if err != nil {
+ t.Fatalf("OpenInRoot(`file`) = %v, want success", err)
+ }
+ f.Close()
+ for _, name := range []string{
+ "link",
+ "../ROOT/file",
+ dir + "/file",
+ } {
+ f, err := os.OpenInRoot(dir, name)
+ if err == nil {
+ f.Close()
+ t.Fatalf("OpenInRoot(%q) = nil, want error", name)
+ }
+ }
+}