forked from aquasecurity/fanal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
64 lines (56 loc) · 1.67 KB
/
fs.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
package walker
import (
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/saracen/walker"
"golang.org/x/xerrors"
)
// WalkDir walks the file tree rooted at root, calling WalkFunc for each file or
// directory in the tree, including root, but a directory to be ignored will be skipped.
func WalkDir(root string, f WalkFunc) error {
// walk function called for every path found
walkFn := func(pathname string, fi os.FileInfo) error {
if !fi.Mode().IsRegular() {
return nil
} else if isIgnored(pathname) {
return filepath.SkipDir
}
pathname = filepath.Clean(pathname)
if err := f(pathname, fi, fileOnceOpener(pathname)); err != nil {
return xerrors.Errorf("failed to analyze file: %w", err)
}
return nil
}
// error function called for every error encountered
errorCallbackOption := walker.WithErrorCallback(func(pathname string, err error) error {
// ignore permission errors
if os.IsPermission(err) {
return nil
}
// halt traversal on any other error
return xerrors.Errorf("unknown error with %s: %w", pathname, err)
})
// Multiple goroutines stat the filesystem concurrently. The provided
// walkFn must be safe for concurrent use.
if err := walker.Walk(root, walkFn, errorCallbackOption); err != nil {
return err
}
return nil
}
// fileOnceOpener opens a file once and the content is shared so that some analyzers can use the same data
func fileOnceOpener(filePath string) func() ([]byte, error) {
var once sync.Once
var b []byte
var err error
return func() ([]byte, error) {
once.Do(func() {
b, err = ioutil.ReadFile(filePath)
})
if err != nil {
return nil, xerrors.Errorf("unable to read file: %w", err)
}
return b, nil
}
}