diff --git a/scanner/tag_scanner.go b/scanner/tag_scanner.go index 6bd654ece..43df122da 100644 --- a/scanner/tag_scanner.go +++ b/scanner/tag_scanner.go @@ -75,6 +75,16 @@ func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time, prog // Special case: if lastModifiedSince is zero, re-import all files fullScan := lastModifiedSince.IsZero() + // If the media folder is empty (no music and no subfolders), abort to avoid deleting all data from DB + empty, err := isDirEmpty(ctx, s.rootFolder) + if err != nil { + return 0, err + } + if empty && !fullScan { + log.Error(ctx, "Media Folder is empty. Aborting scan.", "folder", s.rootFolder) + return 0, nil + } + allDBDirs, err := s.getDBDirTree(ctx) if err != nil { return 0, err @@ -110,12 +120,6 @@ func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time, prog return 0, err } - // If the media folder is empty, abort to avoid deleting all data - if len(allFSDirs) <= 1 { - log.Error(ctx, "Media Folder is empty. Aborting scan.", "folder", s.rootFolder) - return 0, nil - } - deletedDirs := s.getDeletedDirs(ctx, allFSDirs, allDBDirs) if len(deletedDirs)+len(changedDirs) == 0 { log.Debug(ctx, "No changes found in Music Folder", "folder", s.rootFolder, "elapsed", time.Since(start)) @@ -155,6 +159,11 @@ func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time, prog return s.cnt.total(), err } +func isDirEmpty(ctx context.Context, dir string) (bool, error) { + children, stats, err := loadDir(ctx, dir) + return len(children) == 0 && stats.AudioFilesCount == 0, err +} + func (s *TagScanner) getRootFolderWalker(ctx context.Context) (walkResults, chan error) { start := time.Now() log.Trace(ctx, "Loading directory tree from music folder", "folder", s.rootFolder)