aboutsummaryrefslogtreecommitdiff
path: root/src/os/file.go
diff options
context:
space:
mode:
authorCherry Zhang <cherryyz@google.com>2020-10-28 09:12:20 -0400
committerCherry Zhang <cherryyz@google.com>2020-10-28 09:12:20 -0400
commita16e30d162c1c7408db7821e7b9513cefa09c6ca (patch)
treeaf752ba9ba44c547df39bb0af9bff79f610ba9d5 /src/os/file.go
parent91e4d2d57bc341dd82c98247117114c851380aef (diff)
parentcf6cfba4d5358404dd890f6025e573a4b2156543 (diff)
downloadgo-a16e30d162c1c7408db7821e7b9513cefa09c6ca.tar.gz
go-a16e30d162c1c7408db7821e7b9513cefa09c6ca.zip
[dev.link] all: merge branch 'master' into dev.linkdev.link
Clean merge. Change-Id: Ia7b2808bc649790198d34c226a61d9e569084dc5
Diffstat (limited to 'src/os/file.go')
-rw-r--r--src/os/file.go31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/os/file.go b/src/os/file.go
index 05d2f83283..835d44ab8c 100644
--- a/src/os/file.go
+++ b/src/os/file.go
@@ -45,6 +45,7 @@ import (
"internal/poll"
"internal/testlog"
"io"
+ "io/fs"
"runtime"
"syscall"
"time"
@@ -127,7 +128,7 @@ func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
}
if off < 0 {
- return 0, &PathError{"readat", f.name, errors.New("negative offset")}
+ return 0, &PathError{Op: "readat", Path: f.name, Err: errors.New("negative offset")}
}
for len(b) > 0 {
@@ -203,7 +204,7 @@ func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
}
if off < 0 {
- return 0, &PathError{"writeat", f.name, errors.New("negative offset")}
+ return 0, &PathError{Op: "writeat", Path: f.name, Err: errors.New("negative offset")}
}
for len(b) > 0 {
@@ -253,7 +254,7 @@ func (f *File) WriteString(s string) (n int, err error) {
// If there is an error, it will be of type *PathError.
func Mkdir(name string, perm FileMode) error {
if runtime.GOOS == "windows" && isWindowsNulName(name) {
- return &PathError{"mkdir", name, syscall.ENOTDIR}
+ return &PathError{Op: "mkdir", Path: name, Err: syscall.ENOTDIR}
}
longName := fixLongPath(name)
e := ignoringEINTR(func() error {
@@ -261,7 +262,7 @@ func Mkdir(name string, perm FileMode) error {
})
if e != nil {
- return &PathError{"mkdir", name, e}
+ return &PathError{Op: "mkdir", Path: name, Err: e}
}
// mkdir(2) itself won't handle the sticky bit on *BSD and Solaris
@@ -291,7 +292,7 @@ func setStickyBit(name string) error {
func Chdir(dir string) error {
if e := syscall.Chdir(dir); e != nil {
testlog.Open(dir) // observe likely non-existent directory
- return &PathError{"chdir", dir, e}
+ return &PathError{Op: "chdir", Path: dir, Err: e}
}
if log := testlog.Logger(); log != nil {
wd, err := Getwd()
@@ -366,7 +367,7 @@ func (f *File) wrapErr(op string, err error) error {
if err == poll.ErrFileClosing {
err = ErrClosed
}
- return &PathError{op, f.name, err}
+ return &PathError{Op: op, Path: f.name, Err: err}
}
// TempDir returns the default directory to use for temporary files.
@@ -608,3 +609,21 @@ func isWindowsNulName(name string) bool {
}
return true
}
+
+// DirFS returns a file system (an fs.FS) for the tree of files rooted at the directory dir.
+func DirFS(dir string) fs.FS {
+ return dirFS(dir)
+}
+
+type dirFS string
+
+func (dir dirFS) Open(name string) (fs.File, error) {
+ if !fs.ValidPath(name) {
+ return nil, &PathError{Op: "open", Path: name, Err: ErrInvalid}
+ }
+ f, err := Open(string(dir) + "/" + name)
+ if err != nil {
+ return nil, err // nil fs.File
+ }
+ return f, nil
+}