]> Cypherpunks repositories - gostls13.git/commitdiff
os: implement fs.ReadFileFS for DirFS
authorSean Liao <sean@liao.dev>
Sat, 9 Jul 2022 18:08:59 +0000 (19:08 +0100)
committerGopher Robot <gobot@golang.org>
Wed, 24 May 2023 15:04:44 +0000 (15:04 +0000)
Use the os.ReadFile implementation to handle
sysfs files not reporting size properly via stat.

Fixes #53761

Change-Id: I6f34515e8a211e3659f4f6c3598fae7ec0c86975
Reviewed-on: https://go-review.googlesource.com/c/go/+/416775
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Rob Pike <r@golang.org>
Reviewed-by: hopehook <hopehook@golangcn.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>

src/os/file.go
src/os/os_test.go

index ea81a8ba634e7bbc7a5f8ca78a5dcf4b21e60ec7..fc5ba3e4847ec9df76d42c44f85e7fc93c3df024 100644 (file)
@@ -652,6 +652,18 @@ func (dir dirFS) Open(name string) (fs.File, error) {
        return f, nil
 }
 
+// The ReadFile method calls the [ReadFile] function for the file
+// with the given name in the directory. The function provides
+// robust handling for small files and special file systems.
+// Through this method, dirFS implements [io/fs.ReadFileFS].
+func (dir dirFS) ReadFile(name string) ([]byte, error) {
+       fullname, err := dir.join(name)
+       if err != nil {
+               return nil, &PathError{Op: "readfile", Path: name, Err: err}
+       }
+       return ReadFile(fullname)
+}
+
 func (dir dirFS) Stat(name string) (fs.FileInfo, error) {
        fullname, err := dir.join(name)
        if err != nil {
index ad30756cbd30f41503c2c1246a0b05727901067e..2ffbc5b29e0d12f74b943dcdb6031f21d6943e1b 100644 (file)
@@ -3114,6 +3114,23 @@ func TestReadFileProc(t *testing.T) {
        }
 }
 
+func TestDirFSReadFileProc(t *testing.T) {
+       t.Parallel()
+
+       fsys := DirFS("/")
+       name := "proc/sys/fs/pipe-max-size"
+       if _, err := fs.Stat(fsys, name); err != nil {
+               t.Skip()
+       }
+       data, err := fs.ReadFile(fsys, name)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if len(data) == 0 || data[len(data)-1] != '\n' {
+               t.Fatalf("read %s: not newline-terminated: %q", name, data)
+       }
+}
+
 func TestWriteStringAlloc(t *testing.T) {
        if runtime.GOOS == "js" {
                t.Skip("js allocates a lot during File.WriteString")