diff --git a/predicatefs.go b/predicatefs.go index 4d45803..12f28e7 100644 --- a/predicatefs.go +++ b/predicatefs.go @@ -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 {