github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/walker/walk.go (about) 1 package walker 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 8 "github.com/bmatcuk/doublestar/v4" 9 10 "github.com/devseccon/trivy/pkg/fanal/analyzer" 11 "github.com/devseccon/trivy/pkg/fanal/utils" 12 "github.com/devseccon/trivy/pkg/log" 13 ) 14 15 var ( 16 // These variables are exported so that a tool importing Trivy as a library can override these values. 17 AppDirs = []string{".git"} 18 SystemDirs = []string{ 19 "proc", 20 "sys", 21 "dev", 22 } 23 ) 24 25 const defaultSizeThreshold = int64(200) << 20 // 200MB 26 27 type WalkFunc func(filePath string, info os.FileInfo, opener analyzer.Opener) error 28 29 type walker struct { 30 skipFiles []string 31 skipDirs []string 32 } 33 34 func newWalker(skipFiles, skipDirs []string) walker { 35 var cleanSkipFiles, cleanSkipDirs []string 36 for _, skipFile := range skipFiles { 37 skipFile = filepath.ToSlash(filepath.Clean(skipFile)) 38 skipFile = strings.TrimLeft(skipFile, "/") 39 cleanSkipFiles = append(cleanSkipFiles, skipFile) 40 } 41 42 for _, skipDir := range append(skipDirs, SystemDirs...) { 43 skipDir = filepath.ToSlash(filepath.Clean(skipDir)) 44 skipDir = strings.TrimLeft(skipDir, "/") 45 cleanSkipDirs = append(cleanSkipDirs, skipDir) 46 } 47 48 return walker{ 49 skipFiles: cleanSkipFiles, 50 skipDirs: cleanSkipDirs, 51 } 52 } 53 54 func (w *walker) shouldSkipFile(filePath string) bool { 55 filePath = strings.TrimLeft(filePath, "/") 56 57 // skip files 58 for _, pattern := range w.skipFiles { 59 match, err := doublestar.Match(pattern, filePath) 60 if err != nil { 61 return false // return early if bad pattern 62 } else if match { 63 log.Logger.Debugf("Skipping file: %s", filePath) 64 return true 65 } 66 } 67 return false 68 } 69 70 func (w *walker) shouldSkipDir(dir string) bool { 71 dir = strings.TrimLeft(dir, "/") 72 73 // Skip application dirs (relative path) 74 base := filepath.Base(dir) 75 if utils.StringInSlice(base, AppDirs) { 76 return true 77 } 78 79 // Skip system dirs and specified dirs (absolute path) 80 for _, pattern := range w.skipDirs { 81 if match, err := doublestar.Match(pattern, dir); err != nil { 82 return false // return early if bad pattern 83 } else if match { 84 log.Logger.Debugf("Skipping directory: %s", dir) 85 return true 86 } 87 } 88 89 return false 90 }