Skip to content

Commit

Permalink
adding ReadDir() and Walk()
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicalTux committed Jan 16, 2020
1 parent 7cea641 commit 59c20f2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vfs
import (
"fmt"
"os"
"sort"
"strings"
)

Expand Down Expand Up @@ -44,3 +45,19 @@ func MkdirAll(fs FileSystem, path string, perm os.FileMode) error {

return nil
}

func ReadDir(fs FileSystem, path string) ([]os.FileInfo, error) {
f, err := fs.Open(path)
if err != nil {
return nil, err
}

res, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}

sort.Slice(res, func(i, j int) bool { return res[i].Name() < res[j].Name() })
return res, nil
}
40 changes: 40 additions & 0 deletions walk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package vfs

import (
"os"
"path/filepath"
)

func walk(fs FileSystem, path string, walkFn filepath.WalkFunc, info os.FileInfo, err error) error {
if err != nil {
return walkFn(path, info, err)
}
err = walkFn(path, info, nil)
if !info.IsDir() {
return err
}
if err == filepath.SkipDir {
return nil
}
// note: ReadDir returns results sorted by name
infos, err := ReadDir(fs, path)
if err != nil {
return err
}
for _, info := range infos {
name := info.Name()
if name == "." || name == ".." {
continue
}
if err := walk(fs, filepath.Join(path, info.Name()), walkFn, info, nil); err != nil {
return err
}
}
return nil
}

// Walk is the equivalent of filepath.Walk
func Walk(fs FileSystem, path string, walkFn filepath.WalkFunc) error {
info, err := fs.Lstat(path)
return walk(fs, path, walkFn, info, err)
}

0 comments on commit 59c20f2

Please sign in to comment.