fix(library): hidden files and folders are not part of the library (#8305)
A full scan, the watcher's directory watches and the files it queues all skip any name below a library root that starts with a dot. A download manager can park replaced copies in a hidden folder, and macOS leaves "._" files beside the music; neither is a track. A track already indexed from a hidden path is marked missing at the next full scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Hidden files and folders, whose names start with a dot, are not part of
|
||||
// the library. Other programs that share the music folder keep their own
|
||||
// things there: a download manager parks the copies it replaced in a hidden
|
||||
// folder until someone empties it, and macOS leaves "._" files beside every
|
||||
// track it copies. Indexing them would list a copy the person set aside, or
|
||||
// adopt it as the original track having moved. Navidrome and Jellyfin skip
|
||||
// them the same way.
|
||||
|
||||
// hiddenName reports whether a file or folder name is hidden.
|
||||
func hiddenName(name string) bool {
|
||||
return strings.HasPrefix(name, ".") && name != "." && name != ".."
|
||||
}
|
||||
|
||||
// hiddenInLibrary reports whether path is, or sits inside, a hidden file or
|
||||
// folder below one of the library roots. A root may itself be hidden; only
|
||||
// what is below it counts.
|
||||
func hiddenInLibrary(roots []string, path string) bool {
|
||||
for _, root := range roots {
|
||||
rel, err := filepath.Rel(root, path)
|
||||
if err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
continue
|
||||
}
|
||||
for _, part := range strings.Split(rel, string(filepath.Separator)) {
|
||||
if hiddenName(part) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
// libraryTree writes empty files at each path below a fresh root.
|
||||
func libraryTree(t *testing.T, files ...string) string {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
for _, f := range files {
|
||||
p := filepath.Join(root, filepath.FromSlash(f))
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(p, nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
// A full scan lists the library's audio files and none inside a hidden
|
||||
// folder or hidden itself: a download manager's set-aside copies and macOS
|
||||
// "._" files are not tracks.
|
||||
func TestEnumerate_LeavesHiddenFilesAndFoldersOut(t *testing.T) {
|
||||
root := libraryTree(t,
|
||||
"Artist/Album (2001)/01 Song.flac",
|
||||
".sluice-held/Artist/Album (2001)/01 Song.flac",
|
||||
"Artist/.cache/02 Other.mp3",
|
||||
"Artist/Album (2001)/._01 Song.flac",
|
||||
)
|
||||
s := &Scanner{paths: []string{root}, logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
||||
var stats Stats
|
||||
got, errs := s.enumerate(context.Background(), nil, &stats)
|
||||
want := filepath.Join(root, "Artist", "Album (2001)", "01 Song.flac")
|
||||
if errs != 0 || len(got) != 1 || got[0] != want {
|
||||
t.Fatalf("enumerate = %v (errors %d), want only %s", got, errs, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHiddenInLibrary(t *testing.T) {
|
||||
root := filepath.Join(string(filepath.Separator), "srv", ".music")
|
||||
for _, tc := range []struct {
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{"Artist/Album/01.flac", false},
|
||||
{".sluice-held/Artist/Album/01.flac", true},
|
||||
{".sluice-held", true},
|
||||
{"Artist/._01.flac", true},
|
||||
{"Artist/Album..Live/01.flac", false},
|
||||
{"", false},
|
||||
} {
|
||||
path := filepath.Join(root, filepath.FromSlash(tc.path))
|
||||
if got := hiddenInLibrary([]string{root}, path); got != tc.want {
|
||||
t.Errorf("hiddenInLibrary(%q) = %v, want %v (a hidden root itself does not count)", path, got, tc.want)
|
||||
}
|
||||
}
|
||||
outside := filepath.Join(string(filepath.Separator), "srv", ".other", "01.flac")
|
||||
if hiddenInLibrary([]string{root}, outside) {
|
||||
t.Errorf("a path outside every root counted as hidden in the library")
|
||||
}
|
||||
}
|
||||
|
||||
// The watcher ignores a hidden folder appearing in the library, so nothing
|
||||
// set aside in it is watched or queued for a scan.
|
||||
func TestWatcher_IgnoresAHiddenFolderAppearing(t *testing.T) {
|
||||
root := libraryTree(t, ".sluice-held/Artist/Album/01 Song.flac")
|
||||
w := &Watcher{
|
||||
scanner: &Scanner{paths: []string{root}},
|
||||
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
}
|
||||
pending := map[string]struct{}{}
|
||||
held := filepath.Join(root, ".sluice-held")
|
||||
if w.handleEvent(nil, fsnotify.Event{Name: held, Op: fsnotify.Create}, pending) || len(pending) != 0 {
|
||||
t.Fatalf("a hidden folder appearing queued %v", pending)
|
||||
}
|
||||
}
|
||||
@@ -296,6 +296,12 @@ func (s *Scanner) enumerate(
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if path != root && hiddenName(d.Name()) {
|
||||
if d.IsDir() {
|
||||
return fs.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
@@ -576,11 +582,12 @@ func (s *Scanner) ScanFiles(ctx context.Context, paths []string) ([]pgtype.UUID,
|
||||
seen := make(map[[16]byte]struct{})
|
||||
changed := make([]pgtype.UUID, 0)
|
||||
var stats Stats
|
||||
roots := s.Roots()
|
||||
for _, path := range paths {
|
||||
if ctx.Err() != nil {
|
||||
return changed, ctx.Err()
|
||||
}
|
||||
if !audioExtensions[strings.ToLower(filepath.Ext(path))] {
|
||||
if !audioExtensions[strings.ToLower(filepath.Ext(path))] || hiddenInLibrary(roots, path) {
|
||||
continue
|
||||
}
|
||||
albumID, didChange, err := s.scanFile(ctx, q, path, &stats)
|
||||
|
||||
@@ -176,6 +176,9 @@ func (w *Watcher) eventLoop(ctx context.Context, fsw *fsnotify.Watcher, batches
|
||||
func (w *Watcher) handleEvent(
|
||||
fsw *fsnotify.Watcher, event fsnotify.Event, pending map[string]struct{},
|
||||
) bool {
|
||||
if hiddenInLibrary(w.scanner.Roots(), event.Name) {
|
||||
return false
|
||||
}
|
||||
info, err := os.Stat(event.Name)
|
||||
isDir := err == nil && info.IsDir()
|
||||
isAudio := audioExtensions[strings.ToLower(filepath.Ext(event.Name))]
|
||||
@@ -230,13 +233,17 @@ func (w *Watcher) process(ctx context.Context, paths []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// addRecursive adds an fsnotify watch for root and every subdirectory under it.
|
||||
// Per-directory failures (inotify limits, permissions) are logged and skipped.
|
||||
// addRecursive adds an fsnotify watch for root and every subdirectory under it
|
||||
// that is not hidden. Per-directory failures (inotify limits, permissions) are
|
||||
// logged and skipped.
|
||||
func (w *Watcher) addRecursive(fsw *fsnotify.Watcher, root string) {
|
||||
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil || !d.IsDir() {
|
||||
return nil //nolint:nilerr // tolerate unreadable subtrees
|
||||
}
|
||||
if path != root && hiddenName(d.Name()) {
|
||||
return fs.SkipDir
|
||||
}
|
||||
if aerr := fsw.Add(path); aerr != nil {
|
||||
w.logger.Warn("library watcher: add watch failed", "path", path, "err", aerr)
|
||||
}
|
||||
@@ -249,9 +256,18 @@ func (w *Watcher) addRecursive(fsw *fsnotify.Watcher, root string) {
|
||||
func (w *Watcher) enqueueDirAudio(dir string, pending map[string]struct{}) bool {
|
||||
added := false
|
||||
_ = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() {
|
||||
if err != nil {
|
||||
return nil //nolint:nilerr // tolerate unreadable subtrees
|
||||
}
|
||||
if path != dir && hiddenName(d.Name()) {
|
||||
if d.IsDir() {
|
||||
return fs.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if audioExtensions[strings.ToLower(filepath.Ext(path))] {
|
||||
pending[path] = struct{}{}
|
||||
added = true
|
||||
|
||||
Reference in New Issue
Block a user