mirror of
https://github.com/golang/go
synced 2024-11-21 23:34:42 -07:00
os: add OpenInRoot
For #67002 Change-Id: If919ee8a5e3d90e91c7848330762e3254245fba1 Reviewed-on: https://go-review.googlesource.com/c/go/+/629555 Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
a1b5394dba
commit
c315862c99
@ -1,3 +1,4 @@
|
||||
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
|
||||
|
@ -14,6 +14,22 @@ import (
|
||||
"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.
|
||||
|
@ -1157,3 +1157,26 @@ func TestRootRaceRenameDir(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user