-
Notifications
You must be signed in to change notification settings - Fork 342
/
walk.go
52 lines (43 loc) · 1.14 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
package hdfs
import (
"os"
"path/filepath"
"sort"
)
// 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 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 (c *Client) Walk(root string, walkFn filepath.WalkFunc) error {
return c.walk(root, walkFn)
}
func (c *Client) walk(path string, walkFn filepath.WalkFunc) error {
file, err := c.Open(path)
var info os.FileInfo
if file != nil {
info = file.Stat()
}
err = walkFn(path, info, err)
if err != nil {
if info != nil && info.IsDir() && err == filepath.SkipDir {
return nil
}
return err
}
if info == nil || !info.IsDir() {
return nil
}
names, err := file.Readdirnames(0)
if err != nil {
return walkFn(path, info, err)
}
sort.Strings(names)
for _, name := range names {
err = c.walk(filepath.ToSlash(filepath.Join(path, name)), walkFn)
if err != nil {
return err
}
}
return nil
}