From 71a1a7414ab01aa18a818f81f887ee7751006fd2 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Mon, 23 Nov 2026 13:05:48 -0500 Subject: [PATCH] fix(library): a file back at a waiting track's path brings it back at once The watcher scans single files and never reconciles, and a file restored with its old mtime takes the unchanged skip, so a track whose file came back at the same path waited in Needs a file for the next full scan. The scan now clears the mark when the path it reads belongs to a waiting track, and tells clients it plays again. Co-Authored-By: Claude Opus 5.5 --- internal/library/scanner.go | 29 ++++++++++++++- internal/library/scanner_test.go | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/internal/library/scanner.go b/internal/library/scanner.go index 3cee17d5..9857b407 100644 --- a/internal/library/scanner.go +++ b/internal/library/scanner.go @@ -257,6 +257,22 @@ func (s *Scanner) enumerate( return paths, errs } +// clearReturned takes the missing mark (and any removal reason) off a track +// whose file is back at its path, and tells clients it plays again. Counted +// as Restored, like the reconcile pass's clears. +func (s *Scanner) clearReturned(ctx context.Context, q *dbq.Queries, id pgtype.UUID, stats *Stats) error { + ids := []pgtype.UUID{id} + if err := logTrackChanges(ctx, q, ids); err != nil { + return fmt.Errorf("log returned change: %w", err) + } + n, err := q.ClearTracksMissing(ctx, ids) + if err != nil { + return fmt.Errorf("clear missing mark: %w", err) + } + stats.Restored += int(n) + return nil +} + // scanFile upserts a single audio file. Returns the album ID the track // belongs to and whether the file was added/updated (false = skipped as // unchanged), so watcher-driven callers can enrich just the changed albums. @@ -276,6 +292,16 @@ func (s *Scanner) scanFile( if err != nil && !errors.Is(err, pgx.ErrNoRows) { return pgtype.UUID{}, false, fmt.Errorf("lookup: %w", err) } + // A file at the path of a track waiting for one is that track's file + // back. The full scan's reconcile clears these too, but the watcher scans + // single files and never reconciles, and a file restored with its old + // mtime takes the skip below, so without this the track would wait for the + // next full scan. + if knownTrack && existing.MissingSince.Valid { + if err := s.clearReturned(ctx, q, existing.ID, stats); err != nil { + return pgtype.UUID{}, false, err + } + } // Incremental skip: only when the file hasn't changed AND we already have a // real duration AND the row's tag-derived columns were written by the // current extraction logic. The duration clause backfills rows that @@ -497,10 +523,11 @@ func (s *Scanner) ScanFiles(ctx context.Context, paths []string) ([]pgtype.UUID, } } } - if stats.Added > 0 || stats.Updated > 0 { + if stats.Added > 0 || stats.Updated > 0 || stats.Restored > 0 { s.logger.Info("library watch: scan batch", "added", stats.Added, "updated", stats.Updated, + "restored", stats.Restored, "skipped", stats.Skipped, "errored", stats.Errored, ) diff --git a/internal/library/scanner_test.go b/internal/library/scanner_test.go index fbceb4dc..d29433a9 100644 --- a/internal/library/scanner_test.go +++ b/internal/library/scanner_test.go @@ -10,6 +10,7 @@ import ( "path/filepath" "testing" + "github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgxpool" "git.fabledsword.com/bvandeusen/minstrel/internal/db" @@ -485,3 +486,65 @@ func TestScanner_SameTitledAlbumsOfTwoGroupsAreTwoAlbums_Integration(t *testing. t.Errorf("after the re-read, albums by group = %v, want the green track on its own album", got) } } + +// TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration: a file +// put back at the path of a track waiting for one (an admin removed it, or it +// went missing) brings the track back as soon as the watcher scans it, even +// with its old mtime, which takes the unchanged-file skip. The track keeps its +// id, and clients are told it plays again. +func TestScanFiles_ClearsTheMarkWhenAFileReturnsToItsPath_Integration(t *testing.T) { + pool := newPool(t) + ctx := context.Background() + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + + root := t.TempDir() + path := filepath.Join(root, "Artist R/Album R/03 - Returning.mp3") + writeTestMP3(t, path, map[string]string{ + "TIT2": "Returning", "TPE1": "Artist R", "TALB": "Album R", "TRCK": "3", + }) + scanner := New(pool, logger, []string{root}, nil) + if _, err := scanner.ScanFiles(ctx, []string{path}); err != nil { + t.Fatalf("first scan: %v", err) + } + q := dbq.New(pool) + before, err := q.GetTrackByPath(ctx, path) + if err != nil { + t.Fatalf("track not indexed: %v", err) + } + + // Removed from Reports: the row waits, and the file is back unchanged. + if _, err := q.MarkTracksRemoved(ctx, dbq.MarkTracksRemovedParams{ + Reason: string(RemovedForReport), Ids: []pgtype.UUID{before.ID}, + }); err != nil { + t.Fatalf("mark removed: %v", err) + } + var changesBefore int + if err := pool.QueryRow(ctx, "SELECT count(*) FROM library_changes").Scan(&changesBefore); err != nil { + t.Fatalf("count changes: %v", err) + } + + if _, err := scanner.ScanFiles(ctx, []string{path}); err != nil { + t.Fatalf("watcher scan: %v", err) + } + + after, err := q.GetTrackByID(ctx, before.ID) + if err != nil { + t.Fatalf("track gone: %v", err) + } + if after.MissingSince.Valid { + t.Errorf("track still waits for a file it has: missing_since=%v", after.MissingSince.Time) + } + if after.RemovedReason != nil { + t.Errorf("removed_reason = %q, want none once the file is back", *after.RemovedReason) + } + if after.FilePath != path { + t.Errorf("file_path = %q, want %q", after.FilePath, path) + } + var changesAfter int + if err := pool.QueryRow(ctx, "SELECT count(*) FROM library_changes").Scan(&changesAfter); err != nil { + t.Fatalf("count changes: %v", err) + } + if changesAfter <= changesBefore { + t.Error("no library change logged, so clients would still show the track as unplayable") + } +}