aboutsummaryrefslogtreecommitdiff
path: root/internal/scanner/walker.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/scanner/walker.go')
-rw-r--r--internal/scanner/walker.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/scanner/walker.go b/internal/scanner/walker.go
new file mode 100644
index 0000000..76268e1
--- /dev/null
+++ b/internal/scanner/walker.go
@@ -0,0 +1,74 @@
+package scanner
+
+import (
+ "context"
+ "io/fs"
+ "path/filepath"
+)
+
+// Intentionally unexported and scans exactly one root
+func (s *Scanner) walk(ctx context.Context, root string) ([]File, error) {
+ files := make([]File, 0)
+
+ err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ default:
+ }
+
+ if err != nil {
+ return err
+ }
+
+ if d.Type()&fs.ModeSymlink != 0 {
+ if d.IsDir() {
+ return filepath.SkipDir
+ }
+
+ return nil
+ }
+
+ if d.IsDir() {
+ if shouldSkipDir(d.Name()) {
+ return filepath.SkipDir
+ }
+
+ return nil
+ }
+
+ // Ignore symbolic links.
+ //
+ // Following symlinks can lead to cycles, duplicate scans and
+ // traversal outside configured roots.
+ if shouldSkipFile(d.Name()) {
+ return nil
+ }
+
+ if !isImage(path) {
+ return nil
+ }
+
+ info, err := d.Info()
+ if err != nil {
+ return err
+ }
+
+ rel, err := filepath.Rel(root, path)
+ if err != nil {
+ return err
+ }
+
+ files = append(files, File{
+ Path: path,
+ Root: root,
+ RelativePath: rel,
+ Size: info.Size(),
+ ModTime: info.ModTime(),
+ })
+
+ return nil
+ })
+
+ return files, err
+}