diff options
author | Russ Cox <rsc@golang.org> | 2020-07-06 11:26:56 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2020-10-20 17:53:07 +0000 |
commit | 7a131acfd142f0fc7612365078b9f00e371fc0e2 (patch) | |
tree | 710fa5c5503dea43b36d1da6d958b9ee05567fce /src/io | |
parent | 10a1a1a37c007adef8425d273e6b276547982889 (diff) | |
download | go-7a131acfd142f0fc7612365078b9f00e371fc0e2.tar.gz go-7a131acfd142f0fc7612365078b9f00e371fc0e2.zip |
io/fs: add ReadDir and ReadDirFS
Add ReadDir helper function, ReadDirFS interface, and test.
Add ReadDir method to fstest.MapFS.
Add testing of ReadDir method to fstest.TestFS.
For #41190.
Change-Id: Ib860770ec7433ba77b29e626682b238f1b3bf54f
Reviewed-on: https://go-review.googlesource.com/c/go/+/243914
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/fs/readdir.go | 47 | ||||
-rw-r--r-- | src/io/fs/readdir_test.go | 35 |
2 files changed, 82 insertions, 0 deletions
diff --git a/src/io/fs/readdir.go b/src/io/fs/readdir.go new file mode 100644 index 0000000000..3a5aa6d86a --- /dev/null +++ b/src/io/fs/readdir.go @@ -0,0 +1,47 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fs + +import ( + "errors" + "sort" +) + +// ReadDirFS is the interface implemented by a file system +// that provides an optimized implementation of ReadDir. +type ReadDirFS interface { + FS + + // ReadDir reads the named directory + // and returns a list of directory entries sorted by filename. + ReadDir(name string) ([]DirEntry, error) +} + +// ReadDir reads the named directory +// and returns a list of directory entries sorted by filename. +// +// If fs implements ReadDirFS, ReadDir calls fs.ReadDir. +// Otherwise ReadDir calls fs.Open and uses ReadDir and Close +// on the returned file. +func ReadDir(fsys FS, name string) ([]DirEntry, error) { + if fsys, ok := fsys.(ReadDirFS); ok { + return fsys.ReadDir(name) + } + + file, err := fsys.Open(name) + if err != nil { + return nil, err + } + defer file.Close() + + dir, ok := file.(ReadDirFile) + if !ok { + return nil, &PathError{Op: "readdir", Path: name, Err: errors.New("not implemented")} + } + + list, err := dir.ReadDir(-1) + sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() }) + return list, err +} diff --git a/src/io/fs/readdir_test.go b/src/io/fs/readdir_test.go new file mode 100644 index 0000000000..46a4bc2788 --- /dev/null +++ b/src/io/fs/readdir_test.go @@ -0,0 +1,35 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fs_test + +import ( + . "io/fs" + "testing" +) + +type readDirOnly struct{ ReadDirFS } + +func (readDirOnly) Open(name string) (File, error) { return nil, ErrNotExist } + +func TestReadDir(t *testing.T) { + check := func(desc string, dirs []DirEntry, err error) { + t.Helper() + if err != nil || len(dirs) != 1 || dirs[0].Name() != "hello.txt" { + var names []string + for _, d := range dirs { + names = append(names, d.Name()) + } + t.Errorf("ReadDir(%s) = %v, %v, want %v, nil", desc, names, err, []string{"hello.txt"}) + } + } + + // Test that ReadDir uses the method when present. + dirs, err := ReadDir(readDirOnly{testFsys}, ".") + check("readDirOnly", dirs, err) + + // Test that ReadDir uses Open when the method is not present. + dirs, err = ReadDir(openOnly{testFsys}, ".") + check("openOnly", dirs, err) +} |