-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrent_treewalk.go
69 lines (61 loc) · 1.55 KB
/
concurrent_treewalk.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 main
import (
"errors"
"os"
"path"
log "github.com/sirupsen/logrus"
)
// ConcurrentTreeWalk is the object that walks through the file system directory given
type ConcurrentTreeWalk struct {
systemPath string
ignore []string
writer UsageWriter
}
// ParseTree is the main entry point implementation of the tree traversal
func (walker *ConcurrentTreeWalk) ParseTree() (TreeFile, error) {
return walker.recursiveParseTree(walker.systemPath)
}
func (walker *ConcurrentTreeWalk) recursiveParseTree(currentPath string) (TreeFile, error) {
walker.writer.PrintMemUsage()
info, err := os.Lstat(currentPath)
if err != nil {
return TreeFile{}, err
}
if stringInSlice(info.Name(), walker.ignore) {
return TreeFile{}, errors.New("Ignoring path " + info.Name())
}
fileType := "file"
if info.IsDir() {
fileType = "directory"
}
returnTree := TreeFile{
Path: currentPath,
Type: fileType,
Mode: info.Mode().String(),
Modtime: info.ModTime().String(),
Size: info.Size(),
}
if info.IsDir() {
currentDirectory, err := os.Open(currentPath)
if err != nil {
return TreeFile{}, err
}
defer currentDirectory.Close()
files, err := currentDirectory.Readdir(-1)
if err != nil {
log.Error(err)
}
for _, fi := range files {
if fi.Name() == "." || fi.Name() == ".." {
continue
}
child, error := walker.recursiveParseTree(path.Join(currentPath, fi.Name()))
if error != nil {
log.Error(error)
} else {
returnTree.Children = append(returnTree.Children, child)
}
}
}
return returnTree, nil
}