forked from 3JoB/vfs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
80 lines (74 loc) · 2.3 KB
/
walk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package vfs
import (
"os"
"path/filepath"
"sort"
"strings"
)
// Walk walks the file tree rooted at root, calling walkFunc for each file or
// directory in the tree, including root. All errors that arise visiting files
// and directories are filtered by walkFn. The files are walked in lexical
// order, which makes the output deterministic but means that for very
// large directories Walk can be inefficient.
// Walk does not follow symbolic links.
func Walk(fs Filesystem, root string, walkFunc filepath.WalkFunc) error {
info, err := fs.Lstat(root)
if err != nil {
err = walkFunc(root, nil, err)
} else {
err = walk(fs, root, info, walkFunc)
}
if err == filepath.SkipDir {
return nil
}
return err
}
// readDirNames reads the directory named by dirname and returns
// a sorted list of directory entries.
func readDirNames(fs Filesystem, dirname string) ([]string, error) {
infos, err := fs.ReadDir(dirname)
if err != nil {
return nil, err
}
names := make([]string, 0, len(infos))
for _, info := range infos {
names = append(names, info.Name())
}
sort.Strings(names)
return names, nil
}
// walk recursively descends path, calling walkFunc.
func walk(fs Filesystem, path string, info os.FileInfo, walkFunc filepath.WalkFunc) error {
if !info.IsDir() {
return walkFunc(path, info, nil)
}
names, err := readDirNames(fs, path)
err1 := walkFunc(path, info, err)
// If err != nil, walk can't walk into this directory.
// err1 != nil means walkFn want walk to skip this directory or stop walking.
// Therefore, if one of err and err1 isn't nil, walk will return.
if err != nil || err1 != nil {
// The caller's behavior is controlled by the return value, which is decided
// by walkFn. walkFn may ignore err and return nil.
// If walkFn returns SkipDir, it will be handled by the caller.
// So walk should return whatever walkFn returns.
return err1
}
for _, name := range names {
filename := strings.Join([]string{path, name}, string(fs.PathSeparator()))
fileInfo, err := fs.Lstat(filename)
if err != nil {
if err := walkFunc(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
return err
}
} else {
err = walk(fs, filename, fileInfo, walkFunc)
if err != nil {
if !fileInfo.IsDir() || err != filepath.SkipDir {
return err
}
}
}
}
return nil
}