aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/scanner/config.go5
-rw-r--r--internal/scanner/extensions.go2
-rw-r--r--internal/scanner/file.go4
-rw-r--r--internal/scanner/filter.go2
-rw-r--r--internal/scanner/scanner.go14
-rw-r--r--internal/scanner/walker.go5
6 files changed, 27 insertions, 5 deletions
diff --git a/internal/scanner/config.go b/internal/scanner/config.go
index 9a93f26..89e5311 100644
--- a/internal/scanner/config.go
+++ b/internal/scanner/config.go
@@ -1,6 +1,9 @@
package scanner
-// Config configures a scanner
+// Config configures a scanner.
+//
+// Zero values should produce sensible defaults where possible.
type Config struct {
+ // Roots contains the filesystem roots that will be scanned.
Roots []string
}
diff --git a/internal/scanner/extensions.go b/internal/scanner/extensions.go
index fe6efab..20c94d8 100644
--- a/internal/scanner/extensions.go
+++ b/internal/scanner/extensions.go
@@ -5,6 +5,8 @@ import (
"strings"
)
+// supportedExtensions contains every image extension recognised
+// by the scanner.
var supportedExtensions = map[string]struct{}{
".jpg": {},
".jpeg": {},
diff --git a/internal/scanner/file.go b/internal/scanner/file.go
index e48cf2a..d0937b6 100644
--- a/internal/scanner/file.go
+++ b/internal/scanner/file.go
@@ -5,8 +5,8 @@ import "time"
// File represents a discovered file on disk.
//
// It contains only filesystem information.
-// Higher-level packages enrich it with metadata such as EXIF,
-// dimensions, IDs and thumbnails.
+// Metadata such as EXIF, thumbnails and IDs are added by higher
+// level packages.
type File struct {
Path string
Root string
diff --git a/internal/scanner/filter.go b/internal/scanner/filter.go
index a6144dd..c7e3c24 100644
--- a/internal/scanner/filter.go
+++ b/internal/scanner/filter.go
@@ -2,10 +2,12 @@ package scanner
import "strings"
+// shouldSkipDir reports whether a directory should be skipped.
func shouldSkipDir(name string) bool {
return strings.HasPrefix(name, ".") && name != "."
}
+// shouldSkipFile reports whether a file should be skipped.
func shouldSkipFile(name string) bool {
return strings.HasPrefix(name, ".")
}
diff --git a/internal/scanner/scanner.go b/internal/scanner/scanner.go
index d304605..2c766fe 100644
--- a/internal/scanner/scanner.go
+++ b/internal/scanner/scanner.go
@@ -5,10 +5,15 @@ import (
"path/filepath"
)
+// Scanner discovers image files from one or more filesystem roots
+//
+// Scanner is safe for concurrent use after construction,
+// provided Scan is not called concurrently
type Scanner struct {
config Config
}
+// New creates a Scanner using the supplied configuration.
func New(config Config) *Scanner {
clean := make([]string, len(config.Roots))
for i, root := range config.Roots {
@@ -21,9 +26,14 @@ func New(config Config) *Scanner {
}
}
-// Scan walks every configured root and returns the discovered files.
+// Scan recursively walks every configured root and returns the
+// discovered image files.
+//
+// Scan respects context cancellation.
+//
+// Returned files are deduplicated by absolute path.
//
-// The returned slice is not guaranteed to be sorted.
+// Results are not sorted.
func (s *Scanner) Scan(ctx context.Context) ([]File, error) {
files := make([]File, 0)
seen := make(map[string]struct{})
diff --git a/internal/scanner/walker.go b/internal/scanner/walker.go
index 20a1766..76268e1 100644
--- a/internal/scanner/walker.go
+++ b/internal/scanner/walker.go
@@ -6,6 +6,7 @@ import (
"path/filepath"
)
+// Intentionally unexported and scans exactly one root
func (s *Scanner) walk(ctx context.Context, root string) ([]File, error) {
files := make([]File, 0)
@@ -36,6 +37,10 @@ func (s *Scanner) walk(ctx context.Context, root string) ([]File, error) {
return nil
}
+ // Ignore symbolic links.
+ //
+ // Following symlinks can lead to cycles, duplicate scans and
+ // traversal outside configured roots.
if shouldSkipFile(d.Name()) {
return nil
}