implement fs.ReadDirFile

This commit is contained in:
satotake 2022-07-15 21:25:51 +09:00
parent ff7f3e04de
commit 092443c53b
1 changed files with 24 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package afero
import (
"io/fs"
"os"
"path/filepath"
"syscall"
@ -14,6 +15,10 @@ type FilePredicateFs struct {
source Fs
}
var (
_ fs.ReadDirFile = (*PredicateFile)(nil)
)
func NewFilePredicateFs(source Fs, pred func(string) bool) Fs {
return &FilePredicateFs{source: source, pred: pred}
}
@ -191,6 +196,25 @@ func (f *PredicateFile) Readdir(c int) (fi []os.FileInfo, err error) {
return fi, nil
}
func (f *PredicateFile) ReadDir(n int) (filtered []fs.DirEntry, err error) {
var entreis []fs.DirEntry
if rdf, ok := f.f.(fs.ReadDirFile); ok {
entreis, err = rdf.ReadDir(n)
} else {
entreis, err = readDirFile{f.f}.ReadDir(n)
}
if err != nil {
return nil, err
}
for _, e := range entreis {
if e.IsDir() || f.pred(filepath.Join(f.f.Name(), e.Name())) {
filtered = append(filtered, e)
}
}
return filtered, nil
}
func (f *PredicateFile) Readdirnames(c int) (n []string, err error) {
fi, err := f.Readdir(c)
if err != nil {