forked from zhanglimao/trivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
92 lines (76 loc) · 2.17 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
81
82
83
84
85
86
87
88
89
90
91
92
package walker
import (
"os"
"path"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar"
"github.com/zhyocean/trivy/pkg/fanal/analyzer"
"github.com/zhyocean/trivy/pkg/fanal/utils"
"github.com/zhyocean/trivy/pkg/log"
)
var (
// These variables are exported so that a tool importing Trivy as a library can override these values.
AppDirs = []string{".git"}
SystemDirs = []string{"proc", "sys", "dev"}
)
const (
defaultSizeThreshold = int64(200) << 20 // 200MB
slowSizeThreshold = int64(100) << 10 // 10KB
)
type WalkFunc func(filePath string, info os.FileInfo, opener analyzer.Opener) error
type walker struct {
skipFiles []string
skipDirs []string
slow bool
}
func newWalker(skipFiles, skipDirs []string, slow bool) walker {
var cleanSkipFiles, cleanSkipDirs []string
for _, skipFile := range skipFiles {
skipFile = filepath.ToSlash(filepath.Clean(skipFile))
skipFile = strings.TrimLeft(skipFile, "/")
cleanSkipFiles = append(cleanSkipFiles, skipFile)
}
for _, skipDir := range append(skipDirs, SystemDirs...) {
skipDir = filepath.ToSlash(filepath.Clean(skipDir))
skipDir = strings.TrimLeft(skipDir, "/")
cleanSkipDirs = append(cleanSkipDirs, skipDir)
}
return walker{
skipFiles: cleanSkipFiles,
skipDirs: cleanSkipDirs,
slow: slow,
}
}
func (w *walker) shouldSkipFile(filePath string) bool {
filePath = strings.TrimLeft(filePath, "/")
// skip files
for _, pattern := range w.skipFiles {
match, err := doublestar.Match(pattern, filePath)
if err != nil {
return false // return early if bad pattern
} else if match {
log.Logger.Debugf("Skipping file: %s", filePath)
return true
}
}
return false
}
func (w *walker) shouldSkipDir(dir string) bool {
dir = strings.TrimLeft(dir, "/")
// Skip application dirs (relative path)
base := filepath.Base(dir)
if utils.StringInSlice(base, AppDirs) {
return true
}
// Skip system dirs and specified dirs (absolute path)
for _, pattern := range w.skipDirs {
if match, err := path.Match(pattern, dir); err != nil {
return false // return early if bad pattern
} else if match {
log.Logger.Debugf("Skipping directory: %s", dir)
return true
}
}
return false
}