| | | 1 | | package repometa |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "io/fs" |
| | | 5 | | "os" |
| | | 6 | | "path/filepath" |
| | | 7 | | "sort" |
| | | 8 | | ) |
| | | 9 | | |
| | | 10 | | // dirVisit is the payload the walker hands each detector for a single |
| | | 11 | | // directory. Files contains only non-directory entries (regular files |
| | | 12 | | // and other special entries), sorted lexicographically by name. |
| | | 13 | | type dirVisit struct { |
| | | 14 | | abs string |
| | | 15 | | rel string |
| | | 16 | | files []fs.DirEntry |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | type walker struct { |
| | | 20 | | root string |
| | | 21 | | cfg options |
| | | 22 | | stats ScanStats |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | func newWalker(root string, cfg options) *walker { |
| | | 26 | | return &walker{root: root, cfg: cfg} |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | type visitFunc func(dirVisit) |
| | | 30 | | |
| | 34 | 31 | | func (w *walker) walk(visit visitFunc) error { |
| | 34 | 32 | | return w.walkDir(w.root, ".", 0, visit) |
| | 34 | 33 | | } |
| | | 34 | | |
| | 104 | 35 | | func (w *walker) walkDir(abs, rel string, depth int, visit visitFunc) error { |
| | 1 | 36 | | if depth > w.cfg.maxDepth { |
| | 1 | 37 | | w.stats.DepthCapHits++ |
| | 1 | 38 | | return nil |
| | 1 | 39 | | } |
| | 19 | 40 | | if w.stats.DirsVisited >= w.cfg.maxDirs { |
| | 19 | 41 | | w.stats.DirCapHits++ |
| | 19 | 42 | | return nil |
| | 19 | 43 | | } |
| | | 44 | | |
| | 84 | 45 | | entries, err := os.ReadDir(abs) |
| | 0 | 46 | | if err != nil { |
| | 0 | 47 | | // A permission error on a subdirectory should not abort the |
| | 0 | 48 | | // whole scan; skip it and continue. Any other error at the root |
| | 0 | 49 | | // bubbles up because Scan already stat'd the root. |
| | 0 | 50 | | if rel == "." { |
| | 0 | 51 | | return err |
| | 0 | 52 | | } |
| | 0 | 53 | | return nil |
| | | 54 | | } |
| | | 55 | | |
| | 84 | 56 | | files := make([]fs.DirEntry, 0, len(entries)) |
| | 84 | 57 | | subdirs := make([]fs.DirEntry, 0) |
| | 84 | 58 | | for _, e := range entries { |
| | 157 | 59 | | name := e.Name() |
| | 1 | 60 | | if skipDirs[name] { |
| | 1 | 61 | | continue |
| | | 62 | | } |
| | 70 | 63 | | if e.IsDir() { |
| | 70 | 64 | | subdirs = append(subdirs, e) |
| | 70 | 65 | | continue |
| | | 66 | | } |
| | 1 | 67 | | if e.Type()&fs.ModeSymlink != 0 { |
| | 1 | 68 | | w.stats.SymlinksSkipped++ |
| | 1 | 69 | | continue |
| | | 70 | | } |
| | 85 | 71 | | files = append(files, e) |
| | | 72 | | } |
| | 16 | 73 | | sort.Slice(files, func(i, j int) bool { return files[i].Name() < files[j].Name() }) |
| | 44 | 74 | | sort.Slice(subdirs, func(i, j int) bool { return subdirs[i].Name() < subdirs[j].Name() }) |
| | | 75 | | |
| | 84 | 76 | | w.stats.DirsVisited++ |
| | 84 | 77 | | w.stats.FilesSeen += len(files) |
| | 84 | 78 | | |
| | 84 | 79 | | visit(dirVisit{abs: abs, rel: rel, files: files}) |
| | 84 | 80 | | |
| | 70 | 81 | | for _, sd := range subdirs { |
| | 0 | 82 | | if sd.Type()&fs.ModeSymlink != 0 { |
| | 0 | 83 | | w.stats.SymlinksSkipped++ |
| | 0 | 84 | | continue |
| | | 85 | | } |
| | 70 | 86 | | childAbs := filepath.Join(abs, sd.Name()) |
| | 70 | 87 | | childRel := sd.Name() |
| | 43 | 88 | | if rel != "." { |
| | 43 | 89 | | childRel = filepath.Join(rel, sd.Name()) |
| | 43 | 90 | | } |
| | 0 | 91 | | if err := w.walkDir(childAbs, childRel, depth+1, visit); err != nil { |
| | 0 | 92 | | return err |
| | 0 | 93 | | } |
| | | 94 | | } |
| | 84 | 95 | | return nil |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | // skipDirs is the hardcoded set of directory names never descended into. |
| | | 99 | | // These are unambiguous ecosystem artifacts / caches / tool state that |
| | | 100 | | // would explode the walk time without adding component information. |
| | | 101 | | // Names that are commonly legitimate source directories in some |
| | | 102 | | // codebases (bin, obj, out, env) are intentionally NOT listed here. |
| | | 103 | | var skipDirs = map[string]bool{ |
| | | 104 | | ".git": true, |
| | | 105 | | ".hg": true, |
| | | 106 | | ".svn": true, |
| | | 107 | | ".idea": true, |
| | | 108 | | ".vscode": true, |
| | | 109 | | ".gradle": true, |
| | | 110 | | ".mvn": true, |
| | | 111 | | ".next": true, |
| | | 112 | | ".nuxt": true, |
| | | 113 | | ".angular": true, |
| | | 114 | | ".pytest_cache": true, |
| | | 115 | | ".mypy_cache": true, |
| | | 116 | | ".ruff_cache": true, |
| | | 117 | | ".tox": true, |
| | | 118 | | ".terraform": true, |
| | | 119 | | ".direnv": true, |
| | | 120 | | "__pycache__": true, |
| | | 121 | | "node_modules": true, |
| | | 122 | | ".venv": true, |
| | | 123 | | "venv": true, |
| | | 124 | | "vendor": true, |
| | | 125 | | "target": true, |
| | | 126 | | "dist": true, |
| | | 127 | | "build": true, |
| | | 128 | | } |