| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package filecheck |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | func discoverFilesOrDirs(includePaths []string, fn func(absPath string, fi os.FileInfo) bool) []string { |
| 14 | var paths []string |
| 15 | |
| 16 | for _, path := range includePaths { |
| 17 | if !hasMeta(path) { |
| 18 | paths = append(paths, path) |
| 19 | continue |
| 20 | } |
| 21 | |
| 22 | ps, _ := filepath.Glob(path) |
| 23 | for _, path := range ps { |
| 24 | if fi, err := os.Lstat(path); err == nil && fn(path, fi) { |
| 25 | paths = append(paths, path) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | } |
| 30 | |
| 31 | slices.Sort(paths) |
| 32 | paths = slices.Compact(paths) |
| 33 | |
| 34 | return paths |
| 35 | } |
| 36 | |
| 37 | func hasMeta(path string) bool { |
| 38 | magicChars := `*?[` |
| 39 | if runtime.GOOS != "windows" { |
| 40 | magicChars = `*?[\` |
| 41 | } |
| 42 | return strings.ContainsAny(path, magicChars) |
| 43 | } |