-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
69 lines (61 loc) · 1.38 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
package backupfs
import (
"io/fs"
"path/filepath"
"sort"
)
func readDirNames(fs FS, dirname string) ([]string, error) {
f, err := fs.Open(dirname)
if err != nil {
return nil, err
}
names, err := f.Readdirnames(-1)
_ = f.Close()
if err != nil {
return nil, err
}
sort.Strings(names)
return names, nil
}
func walk(fs FS, path string, info fs.FileInfo, walkFn filepath.WalkFunc) error {
err := walkFn(path, info, nil)
if err != nil {
if info.IsDir() && err == filepath.SkipDir {
return nil
}
return err
}
if !info.IsDir() {
return nil
}
names, err := readDirNames(fs, path)
if err != nil {
return walkFn(path, info, err)
}
for _, name := range names {
filename := filepath.Join(path, name)
fileInfo, err := fs.Lstat(filename)
if err != nil {
if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
return err
}
} else {
err = walk(fs, filename, fileInfo, walkFn)
if err != nil {
if !fileInfo.IsDir() || err != filepath.SkipDir {
return err
}
}
}
}
return nil
}
// Walk walks the file tree rooted at root, calling walkFn for each file
// or directory in the tree, including root. All errors that arise visiting
func Walk(fsys FS, root string, walkFn filepath.WalkFunc) error {
info, err := fsys.Lstat(root)
if err != nil {
return walkFn(root, nil, err)
}
return walk(fsys, root, info, walkFn)
}