From 21b49d7549689b19e2da2c979a32e3170700dc20 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Wed, 11 Nov 2026 12:19:34 +0100 Subject: [PATCH] fix(library): never mark a root's files missing when the root looks empty - fix: follow a root that is itself a symlink (e.g. /srv/music -> /mnt/nas/music). Before, the walk saw a non-directory, found nothing and flagged every indexed file as missing. Symlinks inside a root are still skipped - fix: when a root is empty but files are indexed (typical for an unmounted NAS mount point), return an error instead of flagging the whole library - test: symlinked root, symlink to a file, empty root with and without indexed files --- internal/library/scanner.go | 43 ++++++++++++++++++-- internal/library/scanner_test.go | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/internal/library/scanner.go b/internal/library/scanner.go index 28b04e5..44710d3 100644 --- a/internal/library/scanner.go +++ b/internal/library/scanner.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/fs" + "os" "path/filepath" "strings" "sync" @@ -91,6 +92,15 @@ func (s *Scanner) scanRoot(ctx context.Context, root string) (Summary, error) { if err != nil { return sum, fmt.Errorf("list indexed files: %w", err) } + // An unmounted NAS usually leaves an empty but readable folder. Treat + // "nothing found although files are indexed" as a problem, not as a mass + // deletion. Remove the root from the config to really empty a library. + if len(walked.files) == 0 && len(walked.failedDirs) == 0 { + if n := countPresent(existing); n > 0 { + return sum, fmt.Errorf("root is empty but %d indexed files exist: is the volume mounted? "+ + "refusing to mark them missing", n) + } + } p := makePlan(walked, existing) sum.Unchanged = p.unchanged @@ -202,14 +212,30 @@ type walkResult struct { func (s *Scanner) walk(ctx context.Context, root string, sum *Summary) (walkResult, error) { var out walkResult - err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + + // The configured root may itself be a symlink (for example /srv/music -> + // /mnt/nas/music). WalkDir would treat it as a plain file and find nothing, + // so walk the resolved directory. Symlinks *inside* the root are still skipped. + walkRoot, err := filepath.EvalSymlinks(root) + if err != nil { + return out, err + } + info, err := os.Stat(walkRoot) + if err != nil { + return out, err + } + if !info.IsDir() { + return out, fmt.Errorf("%s is not a directory", root) + } + + err = filepath.WalkDir(walkRoot, func(path string, d fs.DirEntry, err error) error { if cerr := ctx.Err(); cerr != nil { return cerr } - if path == root { + if path == walkRoot { return err // nil, or the reason the root cannot be read } - rel, rerr := filepath.Rel(root, path) + rel, rerr := filepath.Rel(walkRoot, path) if rerr != nil { return rerr } @@ -325,6 +351,17 @@ func makePlan(w walkResult, existing []ExistingFile) plan { return p } +// countPresent counts indexed files that were not already flagged missing. +func countPresent(existing []ExistingFile) int { + n := 0 + for _, e := range existing { + if !e.Missing { + n++ + } + } + return n +} + func underAny(rel string, prefixes []string) bool { for _, p := range prefixes { if strings.HasPrefix(rel, p) { diff --git a/internal/library/scanner_test.go b/internal/library/scanner_test.go index 8a8418e..0104ea9 100644 --- a/internal/library/scanner_test.go +++ b/internal/library/scanner_test.go @@ -321,6 +321,7 @@ func TestMissingFileIsMarkedThenRestored(t *testing.T) { root := t.TempDir() p := filepath.Join(root, "a.mp3") write(t, p, "1") + write(t, filepath.Join(root, "keep.mp3"), "22") // the library must not become empty ts := time.Unix(1_700_000_000, 0) _ = os.Chtimes(p, ts, ts) @@ -373,6 +374,74 @@ func TestUnreadableRootDoesNotMarkFilesMissing(t *testing.T) { } } +func TestSymlinkedRootIsFollowed(t *testing.T) { + real := t.TempDir() + write(t, filepath.Join(real, "a.mp3"), "1") + write(t, filepath.Join(real, "sub/b.mp3"), "22") + link := filepath.Join(t.TempDir(), "music") + if err := os.Symlink(real, link); err != nil { + t.Skip("symlinks not supported:", err) + } + + store := newFakeStore() + s, _ := newScanner(store, &fakeReader{}, 2) + sum := mustScan(t, s, link) + if sum.Found != 2 || sum.Added != 2 { + t.Fatalf("first scan through a symlinked root: %+v", sum) + } + if store.row(link, "sub/b.mp3") == nil { + t.Error("files must be stored under the configured root path") + } + + // A rescan must see the same files, not an empty tree. + sum = mustScan(t, s, link) + if sum.Unchanged != 2 || sum.Missing != 0 { + t.Fatalf("rescan through a symlinked root: %+v", sum) + } +} + +func TestSymlinkRootToAFileIsRejected(t *testing.T) { + dir := t.TempDir() + write(t, filepath.Join(dir, "x.mp3"), "1") + link := filepath.Join(t.TempDir(), "music") + if err := os.Symlink(filepath.Join(dir, "x.mp3"), link); err != nil { + t.Skip("symlinks not supported:", err) + } + s, _ := newScanner(newFakeStore(), &fakeReader{}, 1) + if _, err := s.Scan(context.Background(), []string{link}); err == nil { + t.Fatal("a root that is not a directory must be an error") + } +} + +func TestEmptyRootDoesNotMarkIndexedFilesMissing(t *testing.T) { + root := t.TempDir() + write(t, filepath.Join(root, "a.mp3"), "1") + write(t, filepath.Join(root, "b.mp3"), "22") + + store := newFakeStore() + s, _ := newScanner(store, &fakeReader{}, 1) + mustScan(t, s, root) + + // An unmounted NAS typically leaves an empty, perfectly readable folder. + _ = os.Remove(filepath.Join(root, "a.mp3")) + _ = os.Remove(filepath.Join(root, "b.mp3")) + sum, err := s.Scan(context.Background(), []string{root}) + if err == nil { + t.Fatal("expected an error: root is empty but files are indexed") + } + if sum.Missing != 0 || store.row(root, "a.mp3").Missing || store.row(root, "b.mp3").Missing { + t.Fatalf("an empty root must not flag the library as missing: %+v", sum) + } +} + +func TestEmptyRootWithNothingIndexedIsFine(t *testing.T) { + s, _ := newScanner(newFakeStore(), &fakeReader{}, 1) + sum, err := s.Scan(context.Background(), []string{t.TempDir()}) + if err != nil || sum.Found != 0 { + t.Fatalf("fresh empty library: %+v err=%v", sum, err) + } +} + func TestUnreadableSubdirDoesNotMarkItsFilesMissing(t *testing.T) { if os.Geteuid() == 0 { t.Skip("permissions are not enforced for root")