mirror of
https://github.com/navidrome/navidrome.git
synced 2026-01-03 06:15:22 +00:00
* feat: Add selective folder scanning capability Implement targeted scanning of specific library/folder pairs without full recursion. This enables efficient rescanning of individual folders when changes are detected, significantly reducing scan time for large libraries. Key changes: - Add ScanTarget struct and ScanFolders API to Scanner interface - Implement CLI flag --targets for specifying libraryID:folderPath pairs - Add FolderRepository.GetByPaths() for batch folder info retrieval - Create loadSpecificFolders() for non-recursive directory loading - Scope GC operations to affected libraries only (with TODO for full impl) - Add comprehensive tests for selective scanning behavior The selective scan: - Only processes specified folders (no subdirectory recursion) - Maintains library isolation - Runs full maintenance pipeline scoped to affected libraries - Supports both full and quick scan modes Examples: navidrome scan --targets "1:Music/Rock,1:Music/Jazz" navidrome scan --full --targets "2:Classical" * feat(folder): replace GetByPaths with GetFolderUpdateInfo for improved folder updates retrieval Signed-off-by: Deluan <deluan@navidrome.org> * test: update parseTargets test to handle folder names with spaces Signed-off-by: Deluan <deluan@navidrome.org> * refactor(folder): remove unused LibraryPath struct and update GC logging message Signed-off-by: Deluan <deluan@navidrome.org> * refactor(folder): enhance external scanner to support target-specific scanning Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): simplify scanner methods Signed-off-by: Deluan <deluan@navidrome.org> * feat(watcher): implement folder scanning notifications with deduplication Signed-off-by: Deluan <deluan@navidrome.org> * refactor(watcher): add resolveFolderPath function for testability Signed-off-by: Deluan <deluan@navidrome.org> * feat(watcher): implement path ignoring based on .ndignore patterns Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): implement IgnoreChecker for managing .ndignore patterns Signed-off-by: Deluan <deluan@navidrome.org> * refactor(ignore_checker): rename scanner to lineScanner for clarity Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): enhance ScanTarget struct with String method for better target representation Signed-off-by: Deluan <deluan@navidrome.org> * fix(scanner): validate library ID to prevent negative values Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): simplify GC method by removing library ID parameter Signed-off-by: Deluan <deluan@navidrome.org> * feat(scanner): update folder scanning to include all descendants of specified folders Signed-off-by: Deluan <deluan@navidrome.org> * feat(subsonic): allow selective scan in the /startScan endpoint Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): update CallScan to handle specific library/folder pairs Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): streamline scanning logic by removing scanAll method Signed-off-by: Deluan <deluan@navidrome.org> * test: enhance mockScanner for thread safety and improve test reliability Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): move scanner.ScanTarget to model.ScanTarget Signed-off-by: Deluan <deluan@navidrome.org> * refactor: move scanner types to model,implement MockScanner Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): update scanner interface and implementations to use model.Scanner Signed-off-by: Deluan <deluan@navidrome.org> * refactor(folder_repository): normalize target path handling by using filepath.Clean Signed-off-by: Deluan <deluan@navidrome.org> * test(folder_repository): add comprehensive tests for folder retrieval and child exclusion Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): simplify selective scan logic using slice.Filter Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): streamline phase folder and album creation by removing unnecessary library parameter Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): move initialization logic from phase_1 to the scanner itself Signed-off-by: Deluan <deluan@navidrome.org> * refactor(tests): rename selective scan test file to scanner_selective_test.go Signed-off-by: Deluan <deluan@navidrome.org> * feat(configuration): add DevSelectiveWatcher configuration option Signed-off-by: Deluan <deluan@navidrome.org> * feat(watcher): enhance .ndignore handling for folder deletions and file changes Signed-off-by: Deluan <deluan@navidrome.org> * docs(scanner): comments Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scanner): enhance walkDirTree to support target folder scanning Signed-off-by: Deluan <deluan@navidrome.org> * fix(scanner, watcher): handle errors when pushing ignore patterns for folders Signed-off-by: Deluan <deluan@navidrome.org> * Update scanner/phase_1_folders.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * refactor(scanner): replace parseTargets function with direct call to scanner.ParseTargets Signed-off-by: Deluan <deluan@navidrome.org> * test(scanner): add tests for ScanBegin and ScanEnd functionality Signed-off-by: Deluan <deluan@navidrome.org> * fix(library): update PRAGMA optimize to check table sizes without ANALYZE Signed-off-by: Deluan <deluan@navidrome.org> * test(scanner): refactor tests Signed-off-by: Deluan <deluan@navidrome.org> * feat(ui): add selective scan options and update translations Signed-off-by: Deluan <deluan@navidrome.org> * feat(ui): add quick and full scan options for individual libraries Signed-off-by: Deluan <deluan@navidrome.org> * feat(ui): add Scan buttonsto the LibraryList Signed-off-by: Deluan <deluan@navidrome.org> * feat(scan): update scanning parameters from 'path' to 'target' for selective scans. * refactor(scan): move ParseTargets function to model package * test(scan): suppress unused return value from SetUserLibraries in tests * feat(gc): enhance garbage collection to support selective library purging Signed-off-by: Deluan <deluan@navidrome.org> * fix(scanner): prevent race condition when scanning deleted folders When the watcher detects changes in a folder that gets deleted before the scanner runs (due to the 10-second delay), the scanner was prematurely removing these folders from the tracking map, preventing them from being marked as missing. The issue occurred because `newFolderEntry` was calling `popLastUpdate` before verifying the folder actually exists on the filesystem. Changes: - Move fs.Stat check before newFolderEntry creation in loadDir to ensure deleted folders remain in lastUpdates for finalize() to handle - Add early existence check in walkDirTree to skip non-existent target folders with a warning log - Add unit test verifying non-existent folders aren't removed from lastUpdates prematurely - Add integration test for deleted folder scenario with ScanFolders Fixes the issue where deleting entire folders (e.g., /music/AC_DC) wouldn't mark tracks as missing when using selective folder scanning. * refactor(scan): streamline folder entry creation and update handling Signed-off-by: Deluan <deluan@navidrome.org> * feat(scan): add '@Recycle' (QNAP) to ignored directories list Signed-off-by: Deluan <deluan@navidrome.org> * fix(log): improve thread safety in logging level management * test(scan): move unit tests for ParseTargets function Signed-off-by: Deluan <deluan@navidrome.org> * review Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: deluan <deluan.quintao@mechanical-orchard.com>
294 lines
11 KiB
Go
294 lines
11 KiB
Go
package scanner_test
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing/fstest"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/conf/configtest"
|
|
"github.com/navidrome/navidrome/core"
|
|
"github.com/navidrome/navidrome/core/artwork"
|
|
"github.com/navidrome/navidrome/core/metrics"
|
|
"github.com/navidrome/navidrome/core/storage/storagetest"
|
|
"github.com/navidrome/navidrome/db"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
"github.com/navidrome/navidrome/persistence"
|
|
"github.com/navidrome/navidrome/scanner"
|
|
"github.com/navidrome/navidrome/server/events"
|
|
"github.com/navidrome/navidrome/tests"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("ScanFolders", Ordered, func() {
|
|
var ctx context.Context
|
|
var lib model.Library
|
|
var ds model.DataStore
|
|
var s model.Scanner
|
|
var fsys storagetest.FakeFS
|
|
|
|
BeforeAll(func() {
|
|
ctx = request.WithUser(GinkgoT().Context(), model.User{ID: "123", IsAdmin: true})
|
|
tmpDir := GinkgoT().TempDir()
|
|
conf.Server.DbPath = filepath.Join(tmpDir, "test-selective-scan.db?_journal_mode=WAL")
|
|
log.Warn("Using DB at " + conf.Server.DbPath)
|
|
db.Db().SetMaxOpenConns(1)
|
|
})
|
|
|
|
BeforeEach(func() {
|
|
DeferCleanup(configtest.SetupConfig())
|
|
conf.Server.MusicFolder = "fake:///music"
|
|
conf.Server.DevExternalScanner = false
|
|
|
|
db.Init(ctx)
|
|
DeferCleanup(func() {
|
|
Expect(tests.ClearDB()).To(Succeed())
|
|
})
|
|
|
|
ds = persistence.New(db.Db())
|
|
|
|
// Create the admin user in the database to match the context
|
|
adminUser := model.User{
|
|
ID: "123",
|
|
UserName: "admin",
|
|
Name: "Admin User",
|
|
IsAdmin: true,
|
|
NewPassword: "password",
|
|
}
|
|
Expect(ds.User(ctx).Put(&adminUser)).To(Succeed())
|
|
|
|
s = scanner.New(ctx, ds, artwork.NoopCacheWarmer(), events.NoopBroker(),
|
|
core.NewPlaylists(ds), metrics.NewNoopInstance())
|
|
|
|
lib = model.Library{ID: 1, Name: "Fake Library", Path: "fake:///music"}
|
|
Expect(ds.Library(ctx).Put(&lib)).To(Succeed())
|
|
|
|
// Initialize fake filesystem
|
|
fsys = storagetest.FakeFS{}
|
|
storagetest.Register("fake", &fsys)
|
|
})
|
|
|
|
Describe("Adding tracks to the library", func() {
|
|
It("scans specified folders recursively including all subdirectories", func() {
|
|
rock := template(_t{"albumartist": "Rock Artist", "album": "Rock Album"})
|
|
jazz := template(_t{"albumartist": "Jazz Artist", "album": "Jazz Album"})
|
|
pop := template(_t{"albumartist": "Pop Artist", "album": "Pop Album"})
|
|
createFS(fstest.MapFS{
|
|
"rock/track1.mp3": rock(track(1, "Rock Track 1")),
|
|
"rock/track2.mp3": rock(track(2, "Rock Track 2")),
|
|
"rock/subdir/track3.mp3": rock(track(3, "Rock Track 3")),
|
|
"jazz/track4.mp3": jazz(track(1, "Jazz Track 1")),
|
|
"jazz/subdir/track5.mp3": jazz(track(2, "Jazz Track 2")),
|
|
"pop/track6.mp3": pop(track(1, "Pop Track 1")),
|
|
})
|
|
|
|
// Scan only the "rock" and "jazz" folders (including their subdirectories)
|
|
targets := []model.ScanTarget{
|
|
{LibraryID: lib.ID, FolderPath: "rock"},
|
|
{LibraryID: lib.ID, FolderPath: "jazz"},
|
|
}
|
|
|
|
warnings, err := s.ScanFolders(ctx, false, targets)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(warnings).To(BeEmpty())
|
|
|
|
// Verify all tracks in rock and jazz folders (including subdirectories) were imported
|
|
allFiles, err := ds.MediaFile(ctx).GetAll()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Should have 5 tracks (all rock and jazz tracks including subdirectories)
|
|
Expect(allFiles).To(HaveLen(5))
|
|
|
|
// Get the file paths
|
|
paths := slice.Map(allFiles, func(mf model.MediaFile) string {
|
|
return filepath.ToSlash(mf.Path)
|
|
})
|
|
|
|
// Verify the correct files were scanned (including subdirectories)
|
|
Expect(paths).To(ContainElements(
|
|
"rock/track1.mp3",
|
|
"rock/track2.mp3",
|
|
"rock/subdir/track3.mp3",
|
|
"jazz/track4.mp3",
|
|
"jazz/subdir/track5.mp3",
|
|
))
|
|
|
|
// Verify files in the pop folder were NOT scanned
|
|
Expect(paths).ToNot(ContainElement("pop/track6.mp3"))
|
|
})
|
|
})
|
|
|
|
Describe("Deleting folders", func() {
|
|
Context("when a child folder is deleted", func() {
|
|
var (
|
|
revolver, help func(...map[string]any) *fstest.MapFile
|
|
artistFolderID string
|
|
album1FolderID string
|
|
album2FolderID string
|
|
album1TrackIDs []string
|
|
album2TrackIDs []string
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
// Setup template functions for creating test files
|
|
revolver = storagetest.Template(_t{"albumartist": "The Beatles", "album": "Revolver", "year": 1966})
|
|
help = storagetest.Template(_t{"albumartist": "The Beatles", "album": "Help!", "year": 1965})
|
|
|
|
// Initial filesystem with nested folders
|
|
fsys.SetFiles(fstest.MapFS{
|
|
"The Beatles/Revolver/01 - Taxman.mp3": revolver(storagetest.Track(1, "Taxman")),
|
|
"The Beatles/Revolver/02 - Eleanor Rigby.mp3": revolver(storagetest.Track(2, "Eleanor Rigby")),
|
|
"The Beatles/Help!/01 - Help!.mp3": help(storagetest.Track(1, "Help!")),
|
|
"The Beatles/Help!/02 - The Night Before.mp3": help(storagetest.Track(2, "The Night Before")),
|
|
})
|
|
|
|
// First scan - import everything
|
|
_, err := s.ScanAll(ctx, true)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Verify initial state - all folders exist
|
|
folders, err := ds.Folder(ctx).GetAll(model.QueryOptions{Filters: squirrel.Eq{"library_id": lib.ID}})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(folders).To(HaveLen(4)) // root, Artist, Album1, Album2
|
|
|
|
// Store folder IDs for later verification
|
|
for _, f := range folders {
|
|
switch f.Name {
|
|
case "The Beatles":
|
|
artistFolderID = f.ID
|
|
case "Revolver":
|
|
album1FolderID = f.ID
|
|
case "Help!":
|
|
album2FolderID = f.ID
|
|
}
|
|
}
|
|
|
|
// Verify all tracks exist
|
|
allTracks, err := ds.MediaFile(ctx).GetAll()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(allTracks).To(HaveLen(4))
|
|
|
|
// Store track IDs for later verification
|
|
for _, t := range allTracks {
|
|
if t.Album == "Revolver" {
|
|
album1TrackIDs = append(album1TrackIDs, t.ID)
|
|
} else if t.Album == "Help!" {
|
|
album2TrackIDs = append(album2TrackIDs, t.ID)
|
|
}
|
|
}
|
|
|
|
// Verify no tracks are missing initially
|
|
for _, t := range allTracks {
|
|
Expect(t.Missing).To(BeFalse())
|
|
}
|
|
})
|
|
|
|
It("should mark child folder and its tracks as missing when parent is scanned", func() {
|
|
// Delete the child folder (Help!) from the filesystem
|
|
fsys.SetFiles(fstest.MapFS{
|
|
"The Beatles/Revolver/01 - Taxman.mp3": revolver(storagetest.Track(1, "Taxman")),
|
|
"The Beatles/Revolver/02 - Eleanor Rigby.mp3": revolver(storagetest.Track(2, "Eleanor Rigby")),
|
|
// "The Beatles/Help!" folder and its contents are DELETED
|
|
})
|
|
|
|
// Run selective scan on the parent folder (Artist)
|
|
// This simulates what the watcher does when a child folder is deleted
|
|
_, err := s.ScanFolders(ctx, false, []model.ScanTarget{
|
|
{LibraryID: lib.ID, FolderPath: "The Beatles"},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Verify the deleted child folder is now marked as missing
|
|
deletedFolder, err := ds.Folder(ctx).Get(album2FolderID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(deletedFolder.Missing).To(BeTrue(), "Deleted child folder should be marked as missing")
|
|
|
|
// Verify the deleted folder's tracks are marked as missing
|
|
for _, trackID := range album2TrackIDs {
|
|
track, err := ds.MediaFile(ctx).Get(trackID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(track.Missing).To(BeTrue(), "Track in deleted folder should be marked as missing")
|
|
}
|
|
|
|
// Verify the parent folder is still present and not marked as missing
|
|
parentFolder, err := ds.Folder(ctx).Get(artistFolderID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(parentFolder.Missing).To(BeFalse(), "Parent folder should not be marked as missing")
|
|
|
|
// Verify the sibling folder and its tracks are still present and not missing
|
|
siblingFolder, err := ds.Folder(ctx).Get(album1FolderID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(siblingFolder.Missing).To(BeFalse(), "Sibling folder should not be marked as missing")
|
|
|
|
for _, trackID := range album1TrackIDs {
|
|
track, err := ds.MediaFile(ctx).Get(trackID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(track.Missing).To(BeFalse(), "Track in sibling folder should not be marked as missing")
|
|
}
|
|
})
|
|
|
|
It("should mark deeply nested child folders as missing", func() {
|
|
// Add a deeply nested folder structure
|
|
fsys.SetFiles(fstest.MapFS{
|
|
"The Beatles/Revolver/01 - Taxman.mp3": revolver(storagetest.Track(1, "Taxman")),
|
|
"The Beatles/Revolver/02 - Eleanor Rigby.mp3": revolver(storagetest.Track(2, "Eleanor Rigby")),
|
|
"The Beatles/Help!/01 - Help!.mp3": help(storagetest.Track(1, "Help!")),
|
|
"The Beatles/Help!/02 - The Night Before.mp3": help(storagetest.Track(2, "The Night Before")),
|
|
"The Beatles/Help!/Bonus/01 - Bonus Track.mp3": help(storagetest.Track(99, "Bonus Track")),
|
|
"The Beatles/Help!/Bonus/Nested/01 - Deep Track.mp3": help(storagetest.Track(100, "Deep Track")),
|
|
})
|
|
|
|
// Rescan to import the new nested structure
|
|
_, err := s.ScanAll(ctx, true)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Verify nested folders were created
|
|
allFolders, err := ds.Folder(ctx).GetAll(model.QueryOptions{Filters: squirrel.Eq{"library_id": lib.ID}})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(len(allFolders)).To(BeNumerically(">", 4), "Should have more folders with nested structure")
|
|
|
|
// Now delete the entire Help! folder including nested children
|
|
fsys.SetFiles(fstest.MapFS{
|
|
"The Beatles/Revolver/01 - Taxman.mp3": revolver(storagetest.Track(1, "Taxman")),
|
|
"The Beatles/Revolver/02 - Eleanor Rigby.mp3": revolver(storagetest.Track(2, "Eleanor Rigby")),
|
|
// All Help! subfolders are deleted
|
|
})
|
|
|
|
// Run selective scan on parent
|
|
_, err = s.ScanFolders(ctx, false, []model.ScanTarget{
|
|
{LibraryID: lib.ID, FolderPath: "The Beatles"},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Verify all Help! folders (including nested ones) are marked as missing
|
|
missingFolders, err := ds.Folder(ctx).GetAll(model.QueryOptions{
|
|
Filters: squirrel.And{
|
|
squirrel.Eq{"library_id": lib.ID},
|
|
squirrel.Eq{"missing": true},
|
|
},
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(len(missingFolders)).To(BeNumerically(">", 0), "At least one folder should be marked as missing")
|
|
|
|
// Verify all tracks in deleted folders are marked as missing
|
|
allTracks, err := ds.MediaFile(ctx).GetAll()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(allTracks).To(HaveLen(6))
|
|
|
|
for _, track := range allTracks {
|
|
if track.Album == "Help!" {
|
|
Expect(track.Missing).To(BeTrue(), "All tracks in deleted Help! folder should be marked as missing")
|
|
} else if track.Album == "Revolver" {
|
|
Expect(track.Missing).To(BeFalse(), "Tracks in Revolver folder should not be marked as missing")
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})
|
|
})
|