From 4042b90001490d3ef7418d1c6ff83aa769213bed Mon Sep 17 00:00:00 2001 From: Sean Liao Date: Sat, 9 Jul 2022 19:08:59 +0100 Subject: [PATCH] os: implement fs.ReadFileFS for DirFS 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 Reviewed-by: Bryan Mills Auto-Submit: Ian Lance Taylor Reviewed-by: Rob Pike Reviewed-by: Ian Lance Taylor Run-TryBot: Rob Pike Reviewed-by: hopehook Run-TryBot: Ian Lance Taylor --- src/os/file.go | 12 ++++++++++++ src/os/os_test.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/os/file.go b/src/os/file.go index ea81a8ba63..fc5ba3e484 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -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 { diff --git a/src/os/os_test.go b/src/os/os_test.go index ad30756cbd..2ffbc5b29e 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -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") -- 2.50.0