File tree 2 files changed +29
-0
lines changed 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -652,6 +652,18 @@ func (dir dirFS) Open(name string) (fs.File, error) {
652
652
return f , nil
653
653
}
654
654
655
+ // The ReadFile method calls the [ReadFile] function for the file
656
+ // with the given name in the directory. The function provides
657
+ // robust handling for small files and special file systems.
658
+ // Through this method, dirFS implements [io/fs.ReadFileFS].
659
+ func (dir dirFS ) ReadFile (name string ) ([]byte , error ) {
660
+ fullname , err := dir .join (name )
661
+ if err != nil {
662
+ return nil , & PathError {Op : "readfile" , Path : name , Err : err }
663
+ }
664
+ return ReadFile (fullname )
665
+ }
666
+
655
667
func (dir dirFS ) Stat (name string ) (fs.FileInfo , error ) {
656
668
fullname , err := dir .join (name )
657
669
if err != nil {
Original file line number Diff line number Diff line change @@ -3114,6 +3114,23 @@ func TestReadFileProc(t *testing.T) {
3114
3114
}
3115
3115
}
3116
3116
3117
+ func TestDirFSReadFileProc (t * testing.T ) {
3118
+ t .Parallel ()
3119
+
3120
+ fsys := DirFS ("/" )
3121
+ name := "proc/sys/fs/pipe-max-size"
3122
+ if _ , err := fs .Stat (fsys , name ); err != nil {
3123
+ t .Skip ()
3124
+ }
3125
+ data , err := fs .ReadFile (fsys , name )
3126
+ if err != nil {
3127
+ t .Fatal (err )
3128
+ }
3129
+ if len (data ) == 0 || data [len (data )- 1 ] != '\n' {
3130
+ t .Fatalf ("read %s: not newline-terminated: %q" , name , data )
3131
+ }
3132
+ }
3133
+
3117
3134
func TestWriteStringAlloc (t * testing.T ) {
3118
3135
if runtime .GOOS == "js" {
3119
3136
t .Skip ("js allocates a lot during File.WriteString" )
You can’t perform that action at this time.
0 commit comments