diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index ba7c362..076f1d3 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -89,7 +89,7 @@ func run() error { return err } scanner := library.NewScanner(libraryRepo, library.TaglibReader{}, library.NewHub(), cfg.Library.ScanWorkers) - scans := library.NewManager(scanner, cfg.Library.Roots) + scans := library.NewManager(scanner, libraryRepo) logScanResults(logger, scans) router := httpapi.NewRouter(httpapi.Deps{ @@ -98,7 +98,8 @@ func run() error { Tokens: tokens, Listening: listening.NewService(storage.NewListeningRepo(db)), Scan: scanControl{ctx: ctx, m: scans}, - Streams: streaming.NewService(libraryRepo, cfg.Library.Roots), + Libraries: library.NewLibraries(libraryRepo), + Streams: streaming.NewService(libraryRepo), }) // No WriteTimeout: audio streaming (later) needs long responses. @@ -113,7 +114,6 @@ func run() error { zap.String("config", path), zap.String("env", cfg.Log.Env), zap.String("listen", cfg.Server.Listen), - zap.Strings("library_roots", cfg.Library.Roots), zap.String("database_driver", cfg.Database.Driver), // never log the DSN: it holds the password ) diff --git a/cmd/sondad/scan.go b/cmd/sondad/scan.go index 908c7fb..a4d0207 100644 --- a/cmd/sondad/scan.go +++ b/cmd/sondad/scan.go @@ -15,8 +15,9 @@ type scanControl struct { m *library.Manager } -func (c scanControl) Start() error { return c.m.Start(c.ctx) } -func (c scanControl) Status() library.Status { return c.m.Status() } +func (c scanControl) Start() error { return c.m.Start(c.ctx) } +func (c scanControl) StartLibrary(id int64) error { return c.m.StartLibrary(c.ctx, id) } +func (c scanControl) Status() library.Status { return c.m.Status() } // logScanResults writes one log line per finished scan. File paths are left // out on purpose: audio paths are private. diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index 10100b0..d126c34 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -15,6 +15,7 @@ import ( "go.uber.org/zap" "codeberg.org/squidd/sonda/internal/auth" + "codeberg.org/squidd/sonda/internal/library" "codeberg.org/squidd/sonda/internal/listening" "codeberg.org/squidd/sonda/internal/storage" "codeberg.org/squidd/sonda/internal/streaming" @@ -62,7 +63,8 @@ func newTestAPIWith(t *testing.T, mutate func(*Deps)) http.Handler { Tokens: tokens, Listening: listening.NewService(storage.NewListeningRepo(db)), Scan: &fakeScan{}, - Streams: streaming.NewService(noFiles{}, nil), + Libraries: library.NewLibraries(storage.NewLibraryRepo(db)), + Streams: streaming.NewService(noFiles{}), } mutate(&deps) return NewRouter(deps) diff --git a/internal/api/http/handlers_libraries.go b/internal/api/http/handlers_libraries.go new file mode 100644 index 0000000..ecfefb0 --- /dev/null +++ b/internal/api/http/handlers_libraries.go @@ -0,0 +1,108 @@ +package httpapi + +import ( + "errors" + "net/http" + "time" + + "github.com/gin-gonic/gin" + + "codeberg.org/squidd/sonda/internal/library" +) + +// librariesHandlers are the admin-only routes that manage libraries. +type librariesHandlers struct{ svc libraryService } + +type libraryJSON struct { + ID int64 `json:"id"` + Name string `json:"name"` + Path string `json:"path"` + CreatedAt string `json:"created_at"` +} + +func toLibraryJSON(l library.Library) libraryJSON { + return libraryJSON{ID: l.ID, Name: l.Name, Path: l.Path, CreatedAt: l.CreatedAt.UTC().Format(time.RFC3339)} +} + +func writeLibraryError(c *gin.Context, err error) { + switch { + case errors.Is(err, library.ErrInvalidLibrary): + writeError(c, http.StatusBadRequest, "invalid_input", err.Error()) + case errors.Is(err, library.ErrLibraryExists): + writeError(c, http.StatusConflict, "library_exists", err.Error()) + case errors.Is(err, library.ErrLibraryNotFound): + writeError(c, http.StatusNotFound, "library_not_found", err.Error()) + default: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + } +} + +func (h *librariesHandlers) list(c *gin.Context) { + libs, err := h.svc.List(c.Request.Context()) + if err != nil { + writeLibraryError(c, err) + return + } + out := make([]libraryJSON, len(libs)) + for i, l := range libs { + out[i] = toLibraryJSON(l) + } + c.JSON(http.StatusOK, gin.H{"libraries": out}) +} + +type createLibraryRequest struct { + Name string `json:"name"` + Path string `json:"path"` +} + +func (h *librariesHandlers) create(c *gin.Context) { + var req createLibraryRequest + if !bindJSON(c, &req) { + return + } + lib, err := h.svc.Create(c.Request.Context(), req.Name, req.Path) + if err != nil { + writeLibraryError(c, err) + return + } + c.JSON(http.StatusCreated, toLibraryJSON(lib)) +} + +type updateLibraryRequest struct { + Name *string `json:"name"` // pointers: an absent field keeps the current value + Path *string `json:"path"` +} + +func (h *librariesHandlers) update(c *gin.Context) { + id, ok := pathID(c) + if !ok { + return + } + var req updateLibraryRequest + if !bindJSON(c, &req) { + return + } + if req.Name == nil && req.Path == nil { + writeError(c, http.StatusBadRequest, "invalid_input", "send name, path or both") + return + } + lib, err := h.svc.Update(c.Request.Context(), id, req.Name, req.Path) + if err != nil { + writeLibraryError(c, err) + return + } + c.JSON(http.StatusOK, toLibraryJSON(lib)) +} + +func (h *librariesHandlers) remove(c *gin.Context) { + id, ok := pathID(c) + if !ok { + return + } + if err := h.svc.Delete(c.Request.Context(), id); err != nil { + writeLibraryError(c, err) + return + } + c.Status(http.StatusNoContent) +} diff --git a/internal/api/http/handlers_library.go b/internal/api/http/handlers_library.go index 39041a3..704ae31 100644 --- a/internal/api/http/handlers_library.go +++ b/internal/api/http/handlers_library.go @@ -13,7 +13,7 @@ import ( type libraryHandlers struct{ scan scanService } type summaryJSON struct { - Roots int `json:"roots"` + Libraries int `json:"libraries"` Found int `json:"found"` Added int `json:"added"` Updated int `json:"updated"` @@ -35,10 +35,26 @@ type scanStatusJSON struct { // startScan launches a background scan of the configured roots. func (h *libraryHandlers) startScan(c *gin.Context) { - err := h.scan.Start() + writeScanStarted(c, h.scan.Start()) +} + +// startLibraryScan launches a background scan of one library. +func (h *libraryHandlers) startLibraryScan(c *gin.Context) { + id, ok := pathID(c) + if !ok { + return + } + writeScanStarted(c, h.scan.StartLibrary(id)) +} + +func writeScanStarted(c *gin.Context, err error) { switch { case errors.Is(err, library.ErrScanRunning): writeError(c, http.StatusConflict, "scan_running", err.Error()) + case errors.Is(err, library.ErrNoLibraries): + writeError(c, http.StatusConflict, "no_libraries", "add a library before scanning") + case errors.Is(err, library.ErrLibraryNotFound): + writeError(c, http.StatusNotFound, "library_not_found", err.Error()) case err != nil: _ = c.Error(err) writeError(c, http.StatusInternalServerError, "internal", "internal error") diff --git a/internal/api/http/libraries_test.go b/internal/api/http/libraries_test.go new file mode 100644 index 0000000..ffa9afd --- /dev/null +++ b/internal/api/http/libraries_test.go @@ -0,0 +1,178 @@ +package httpapi + +import ( + "net/http" + "os" + "path/filepath" + "strconv" + "testing" +) + +func libPath(id float64) string { + return "/api/v1/libraries/" + strconv.FormatInt(int64(id), 10) +} + +func mkdirT(t *testing.T, path string) string { + t.Helper() + if err := os.MkdirAll(path, 0o755); err != nil { + t.Fatal(err) + } + return path +} + +func TestLibraryRoutesAreAdminOnly(t *testing.T) { + h := newTestAPIWithScan(t, &fakeScan{}) + admin := registerAndLogin(t, h) + _, bob := addUser(t, h, admin, "bob", false) + + calls := []struct{ method, path string }{ + {"GET", "/api/v1/libraries"}, + {"POST", "/api/v1/libraries"}, + {"PATCH", "/api/v1/libraries/1"}, + {"DELETE", "/api/v1/libraries/1"}, + {"POST", "/api/v1/libraries/1/scan"}, + } + for _, c := range calls { + if r := do(t, h, c.method, c.path, "", nil); r.Code != http.StatusUnauthorized { + t.Errorf("%s %s without token: %d", c.method, c.path, r.Code) + } + r := do(t, h, c.method, c.path, bob, map[string]any{"name": "x", "path": "/tmp"}) + if r.Code != http.StatusForbidden || errCode(r) != "admin_required" { + t.Errorf("%s %s as non-admin: %d %v", c.method, c.path, r.Code, r.Body) + } + } +} + +func TestLibraryLifecycle(t *testing.T) { + h := newTestAPI(t) + admin := registerAndLogin(t, h) + base := t.TempDir() + jazz := mkdirT(t, filepath.Join(base, "jazz")) + rock := mkdirT(t, filepath.Join(base, "rock")) + + r := do(t, h, "POST", "/api/v1/libraries", admin, map[string]string{"name": " Jazz ", "path": jazz}) + if r.Code != http.StatusCreated || r.Body["name"] != "Jazz" || r.Body["path"] != jazz || r.Body["id"] == nil { + t.Fatalf("create: %d %v", r.Code, r.Body) + } + id, _ := r.Body["id"].(float64) + + r = do(t, h, "GET", "/api/v1/libraries", admin, nil) + libs, _ := r.Body["libraries"].([]any) + if r.Code != http.StatusOK || len(libs) != 1 { + t.Fatalf("list: %d %v", r.Code, r.Body) + } + if first := libs[0].(map[string]any); first["created_at"] == nil || first["name"] != "Jazz" { + t.Errorf("listed library = %v", first) + } + + // Rename only, then move only, then both. + r = do(t, h, "PATCH", libPath(id), admin, map[string]string{"name": "Jazz Collection"}) + if r.Code != http.StatusOK || r.Body["name"] != "Jazz Collection" || r.Body["path"] != jazz { + t.Fatalf("rename: %d %v", r.Code, r.Body) + } + r = do(t, h, "PATCH", libPath(id), admin, map[string]string{"path": rock}) + if r.Code != http.StatusOK || r.Body["path"] != rock || r.Body["name"] != "Jazz Collection" { + t.Fatalf("move: %d %v", r.Code, r.Body) + } + + if r := do(t, h, "DELETE", libPath(id), admin, nil); r.Code != http.StatusNoContent { + t.Fatalf("delete: %d %v", r.Code, r.Body) + } + if r := do(t, h, "DELETE", libPath(id), admin, nil); r.Code != http.StatusNotFound || errCode(r) != "library_not_found" { + t.Errorf("delete twice: %d %v", r.Code, r.Body) + } + r = do(t, h, "GET", "/api/v1/libraries", admin, nil) + if libs, _ := r.Body["libraries"].([]any); len(libs) != 0 { + t.Errorf("libraries after delete: %v", libs) + } +} + +func TestLibraryValidationErrors(t *testing.T) { + h := newTestAPI(t) + admin := registerAndLogin(t, h) + base := t.TempDir() + parent := mkdirT(t, filepath.Join(base, "music")) + child := mkdirT(t, filepath.Join(parent, "jazz")) + other := mkdirT(t, filepath.Join(base, "other")) + file := filepath.Join(base, "file.txt") + if err := os.WriteFile(file, []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + + if r := do(t, h, "POST", "/api/v1/libraries", admin, map[string]string{"name": "Music", "path": parent}); r.Code != http.StatusCreated { + t.Fatal(r.Code, r.Body) + } + + tests := []struct { + name string + body any + status int + code string + }{ + {"empty name", map[string]string{"name": " ", "path": other}, 400, "invalid_input"}, + {"relative path", map[string]string{"name": "x", "path": "music/jazz"}, 400, "invalid_input"}, + {"filesystem root", map[string]string{"name": "x", "path": "/"}, 400, "invalid_input"}, + {"missing directory", map[string]string{"name": "x", "path": filepath.Join(base, "nope")}, 400, "invalid_input"}, + {"a file", map[string]string{"name": "x", "path": file}, 400, "invalid_input"}, + {"inside another library", map[string]string{"name": "x", "path": child}, 400, "invalid_input"}, + {"around another library", map[string]string{"name": "x", "path": base}, 400, "invalid_input"}, + {"same name", map[string]string{"name": "MUSIC", "path": other}, 409, "library_exists"}, + {"same path", map[string]string{"name": "x", "path": parent}, 409, "library_exists"}, + {"malformed json", "{oops", 400, "invalid_input"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := do(t, h, "POST", "/api/v1/libraries", admin, tt.body) + if r.Code != tt.status || errCode(r) != tt.code { + t.Fatalf("%d %v, want %d %s", r.Code, r.Body, tt.status, tt.code) + } + }) + } +} + +func TestLibraryPatchValidation(t *testing.T) { + h := newTestAPI(t) + admin := registerAndLogin(t, h) + base := t.TempDir() + a := mkdirT(t, filepath.Join(base, "a")) + + r := do(t, h, "POST", "/api/v1/libraries", admin, map[string]string{"name": "A", "path": a}) + id, _ := r.Body["id"].(float64) + + if r := do(t, h, "PATCH", libPath(id), admin, map[string]any{}); r.Code != http.StatusBadRequest { + t.Errorf("empty patch: %d", r.Code) + } + if r := do(t, h, "PATCH", libPath(id), admin, map[string]string{"name": ""}); r.Code != http.StatusBadRequest { + t.Errorf("empty name: %d", r.Code) + } + if r := do(t, h, "PATCH", "/api/v1/libraries/999", admin, map[string]string{"name": "x"}); r.Code != http.StatusNotFound || errCode(r) != "library_not_found" { + t.Errorf("unknown library: %d %v", r.Code, r.Body) + } + if r := do(t, h, "PATCH", "/api/v1/libraries/abc", admin, map[string]string{"name": "x"}); r.Code != http.StatusBadRequest { + t.Errorf("bad id: %d", r.Code) + } +} + +func TestScanOneLibrary(t *testing.T) { + scan := &fakeScan{} + h := newTestAPIWithScan(t, scan) + admin := registerAndLogin(t, h) + + if r := do(t, h, "POST", "/api/v1/libraries/7/scan", admin, nil); r.Code != http.StatusAccepted || scan.lastOnly != 7 { + t.Fatalf("scan one: %d %v (library %d)", r.Code, r.Body, scan.lastOnly) + } + if r := do(t, h, "POST", "/api/v1/libraries/7/scan", admin, nil); r.Code != http.StatusConflict || errCode(r) != "scan_running" { + t.Errorf("second scan: %d %v", r.Code, r.Body) + } + if r := do(t, h, "POST", "/api/v1/libraries/999/scan", admin, nil); r.Code != http.StatusNotFound || errCode(r) != "library_not_found" { + t.Errorf("unknown library: %d %v", r.Code, r.Body) + } +} + +func TestScanAllWithoutLibraries(t *testing.T) { + h := newTestAPIWithScan(t, &fakeScan{noLibs: true}) + admin := registerAndLogin(t, h) + if r := do(t, h, "POST", "/api/v1/library/scan", admin, nil); r.Code != http.StatusConflict || errCode(r) != "no_libraries" { + t.Fatalf("%d %v", r.Code, r.Body) + } +} diff --git a/internal/api/http/library_test.go b/internal/api/http/library_test.go index b9824c6..f41d81d 100644 --- a/internal/api/http/library_test.go +++ b/internal/api/http/library_test.go @@ -9,12 +9,17 @@ import ( ) type fakeScan struct { - running bool - starts int - status library.Status + running bool + noLibs bool + starts int + lastOnly int64 // library of the last StartLibrary call + status library.Status } func (f *fakeScan) Start() error { + if f.noLibs { + return library.ErrNoLibraries + } if f.running { return library.ErrScanRunning } @@ -23,6 +28,19 @@ func (f *fakeScan) Start() error { return nil } +func (f *fakeScan) StartLibrary(id int64) error { + if id == 999 { + return library.ErrLibraryNotFound + } + if f.running { + return library.ErrScanRunning + } + f.running = true + f.starts++ + f.lastOnly = id + return nil +} + func (f *fakeScan) Status() library.Status { s := f.status s.Running = f.running @@ -59,7 +77,7 @@ func TestScanStartAndConflict(t *testing.T) { func TestScanStatus(t *testing.T) { scan := &fakeScan{status: library.Status{ LastFinishedAt: time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC), - Last: &library.Summary{Roots: 1, Found: 10, Added: 7, Failed: 1}, + Last: &library.Summary{Libraries: 1, Found: 10, Added: 7, Failed: 1}, LastError: "boom", }} h := newTestAPIWithScan(t, scan) diff --git a/internal/api/http/router.go b/internal/api/http/router.go index ab160dc..4e8b3a8 100644 --- a/internal/api/http/router.go +++ b/internal/api/http/router.go @@ -47,9 +47,17 @@ type streamService interface { type scanService interface { Start() error + StartLibrary(id int64) error Status() library.Status } +type libraryService interface { + List(ctx context.Context) ([]library.Library, error) + Create(ctx context.Context, name, path string) (library.Library, error) + Update(ctx context.Context, id int64, name, path *string) (library.Library, error) + Delete(ctx context.Context, id int64) error +} + // Deps are the services the API needs. type Deps struct { Logger *zap.Logger @@ -57,6 +65,7 @@ type Deps struct { Tokens tokenParser Listening listenService Scan scanService + Libraries libraryService Streams streamService } @@ -96,6 +105,12 @@ func NewRouter(d Deps) *gin.Engine { // Admins only. admin := protected.Group("", requireAdmin(d.Auth)) admin.POST("/library/scan", lib.startScan) + libs := &librariesHandlers{svc: d.Libraries} + admin.GET("/libraries", libs.list) + admin.POST("/libraries", libs.create) + admin.PATCH("/libraries/:id", libs.update) + admin.DELETE("/libraries/:id", libs.remove) + admin.POST("/libraries/:id/scan", lib.startLibraryScan) u := &usersHandlers{svc: d.Auth} admin.GET("/users", u.list) admin.POST("/users", u.create) diff --git a/internal/api/http/stream_test.go b/internal/api/http/stream_test.go index 3999db4..d6d29f9 100644 --- a/internal/api/http/stream_test.go +++ b/internal/api/http/stream_test.go @@ -47,7 +47,7 @@ func newStreamFixture(t *testing.T) streamFixture { 2: {Root: root, RelPath: "gone.mp3"}, 3: {Root: root, RelPath: "../escape.mp3"}, } - h := newTestAPIWith(t, func(d *Deps) { d.Streams = streaming.NewService(src, []string{root}) }) + h := newTestAPIWith(t, func(d *Deps) { d.Streams = streaming.NewService(src) }) return streamFixture{h: h, token: registerAndLogin(t, h), data: data} } diff --git a/internal/config/config.go b/internal/config/config.go index 4eb758d..f45b5fc 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "os" - "path/filepath" "time" "github.com/pelletier/go-toml/v2" @@ -31,10 +30,10 @@ type Server struct { Listen string `toml:"listen"` } -// Library holds the music directories. They are treated as read-only. +// Library holds scanner settings. The libraries themselves (name and path) live +// in the database and are managed through the API. type Library struct { - Roots []string `toml:"roots"` - ScanWorkers int `toml:"scan_workers"` // parallel tag readers per scan + ScanWorkers int `toml:"scan_workers"` // parallel tag readers per scan } // Database selects and configures the storage backend. @@ -103,7 +102,7 @@ func Load(path string) (Config, error) { dec := toml.NewDecoder(bytes.NewReader(data)) dec.DisallowUnknownFields() if err := dec.Decode(&cfg); err != nil { - return Config{}, fmt.Errorf("parse config %s: %w", path, err) + return Config{}, explainDecodeError(path, err) } } @@ -169,24 +168,22 @@ func (c Config) Validate() error { if c.Library.ScanWorkers < 1 { errs = append(errs, errors.New("library.scan_workers must be at least 1")) } - if len(c.Library.Roots) == 0 { - errs = append(errs, errors.New("library.roots needs at least one directory")) - } - for _, root := range c.Library.Roots { - if !filepath.IsAbs(root) { - // Roots are stored with every indexed file and used to serve it: - // a relative one would depend on the directory sondad starts in. - errs = append(errs, fmt.Errorf("library.roots: %q must be an absolute path", root)) - continue - } - info, err := os.Stat(root) - switch { - case err != nil: - errs = append(errs, fmt.Errorf("library.roots: %w", err)) - case !info.IsDir(): - errs = append(errs, fmt.Errorf("library.roots: %s is not a directory", root)) - } - } return errors.Join(errs...) } + +// explainDecodeError turns the strict decoder's "unknown key" into something +// useful for the one key that used to exist. +func explainDecodeError(path string, err error) error { + var missing *toml.StrictMissingError + if errors.As(err, &missing) { + for _, e := range missing.Errors { + key := e.Key() + if len(key) == 2 && key[0] == "library" && key[1] == "roots" { + return fmt.Errorf("parse config %s: library.roots was removed; "+ + "remove it and add each directory as a library with POST /api/v1/libraries", path) + } + } + } + return fmt.Errorf("parse config %s: %w", path, err) +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go index e26c24b..527ca70 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -52,7 +52,7 @@ func TestLoadFileOverridesDefaults(t *testing.T) { listen = "0.0.0.0:9000" [library] -roots = ["/a", "/b"] +scan_workers = 9 [providers.lastfm] api_key = "k" @@ -64,8 +64,8 @@ api_key = "k" if cfg.Server.Listen != "0.0.0.0:9000" { t.Errorf("listen = %q", cfg.Server.Listen) } - if len(cfg.Library.Roots) != 2 { - t.Errorf("roots = %v", cfg.Library.Roots) + if cfg.Library.ScanWorkers != 9 { + t.Errorf("scan workers = %d", cfg.Library.ScanWorkers) } if cfg.Providers.LastFM.APIKey != "k" { t.Errorf("api key = %q", cfg.Providers.LastFM.APIKey) @@ -113,22 +113,12 @@ func TestLoadMissingFile(t *testing.T) { } func TestValidate(t *testing.T) { - good := t.TempDir() - file := filepath.Join(good, "f") - if err := os.WriteFile(file, nil, 0o600); err != nil { - t.Fatal(err) - } - tests := []struct { name string mutate func(*Config) wantErr string }{ {"ok", func(c *Config) {}, ""}, - {"no roots", func(c *Config) { c.Library.Roots = nil }, "library.roots"}, - {"root missing", func(c *Config) { c.Library.Roots = []string{filepath.Join(good, "nope")} }, "library.roots"}, - {"relative root", func(c *Config) { c.Library.Roots = []string{"music"} }, "absolute"}, - {"root is file", func(c *Config) { c.Library.Roots = []string{file} }, "not a directory"}, {"bad env", func(c *Config) { c.Log.Env = "staging" }, "log.env"}, {"empty listen", func(c *Config) { c.Server.Listen = "" }, "server.listen"}, {"empty db", func(c *Config) { c.Database.Path = "" }, "database.path"}, @@ -151,7 +141,6 @@ func TestValidate(t *testing.T) { if err != nil { t.Fatal(err) } - cfg.Library.Roots = []string{good} cfg.Auth.JWTSecret = strings.Repeat("s", 32) tt.mutate(&cfg) err = cfg.Validate() @@ -167,3 +156,16 @@ func TestValidate(t *testing.T) { }) } } + +// library.roots moved into the database; an old config must fail with a hint, +// not with a bare "unknown key". +func TestLoadExplainsRemovedLibraryRoots(t *testing.T) { + p := writeFile(t, t.TempDir(), "[library]\nroots = [\"/music\"]\n") + _, err := Load(p) + if err == nil { + t.Fatal("expected an error for library.roots") + } + if !strings.Contains(err.Error(), "library.roots") || !strings.Contains(err.Error(), "/api/v1/libraries") { + t.Errorf("error = %v, want a pointer to the libraries API", err) + } +} diff --git a/internal/library/libraries.go b/internal/library/libraries.go new file mode 100644 index 0000000..ffe5757 --- /dev/null +++ b/internal/library/libraries.go @@ -0,0 +1,157 @@ +package library + +import ( + "context" + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "strings" + "time" + "unicode" + "unicode/utf8" +) + +// Errors returned by the Libraries service. +var ( + ErrLibraryNotFound = errors.New("library not found") + ErrLibraryExists = errors.New("a library with this name or path already exists") + ErrInvalidLibrary = errors.New("invalid library") +) + +const maxLibraryNameLen = 100 + +// Library is a named directory of music. +type Library struct { + ID int64 + Name string + Path string // absolute directory + CreatedAt time.Time +} + +// LibraryStore persists libraries. +type LibraryStore interface { + ListLibraries(ctx context.Context) ([]Library, error) + // GetLibrary returns ErrLibraryNotFound for an unknown id. + GetLibrary(ctx context.Context, id int64) (Library, error) + // CreateLibrary and UpdateLibrary return ErrLibraryExists when the name + // (case-insensitive) or the path is already used by another library. + CreateLibrary(ctx context.Context, name, path string) (Library, error) + UpdateLibrary(ctx context.Context, id int64, name, path string) (Library, error) + // DeleteLibrary removes the library and its indexed files, never the music + // on disk. It returns ErrLibraryNotFound for an unknown id. + DeleteLibrary(ctx context.Context, id int64) error +} + +// Libraries manages the list of libraries and keeps it sane: every path is an +// absolute, existing directory, and no library sits inside or around another +// (that would index the same files twice). +type Libraries struct{ store LibraryStore } + +// NewLibraries builds the service. +func NewLibraries(store LibraryStore) *Libraries { return &Libraries{store: store} } + +// List returns every library. +func (s *Libraries) List(ctx context.Context) ([]Library, error) { + return s.store.ListLibraries(ctx) +} + +// Create adds a library. +func (s *Libraries) Create(ctx context.Context, name, path string) (Library, error) { + name, path, err := s.validate(ctx, name, path, 0) + if err != nil { + return Library{}, err + } + return s.store.CreateLibrary(ctx, name, path) +} + +// Update changes the name and/or the path of a library. A nil argument keeps +// the current value. Files keep their identity when the path changes. +func (s *Libraries) Update(ctx context.Context, id int64, name, path *string) (Library, error) { + cur, err := s.store.GetLibrary(ctx, id) + if err != nil { + return Library{}, err + } + newName, newPath := cur.Name, cur.Path + if name != nil { + newName = *name + } + if path != nil { + newPath = *path + } + newName, newPath, err = s.validate(ctx, newName, newPath, id) + if err != nil { + return Library{}, err + } + return s.store.UpdateLibrary(ctx, id, newName, newPath) +} + +// Delete removes a library from the index. The music on disk is untouched. +func (s *Libraries) Delete(ctx context.Context, id int64) error { + return s.store.DeleteLibrary(ctx, id) +} + +// validate checks a name and a path, ignoring the library skipID when it +// looks for overlaps. It returns the trimmed name and the cleaned path. +func (s *Libraries) validate(ctx context.Context, name, path string, skipID int64) (string, string, error) { + name = strings.TrimSpace(name) + if name == "" || utf8.RuneCountInString(name) > maxLibraryNameLen { + return "", "", fmt.Errorf("%w: name must be 1-%d characters", ErrInvalidLibrary, maxLibraryNameLen) + } + for _, r := range name { + if unicode.IsControl(r) { + return "", "", fmt.Errorf("%w: name has control characters", ErrInvalidLibrary) + } + } + + if path == "" || !filepath.IsAbs(path) { + return "", "", fmt.Errorf("%w: path must be absolute", ErrInvalidLibrary) + } + path = filepath.Clean(path) + if filepath.Dir(path) == path { + return "", "", fmt.Errorf("%w: the filesystem root cannot be a library", ErrInvalidLibrary) + } + real, err := filepath.EvalSymlinks(path) + if errors.Is(err, fs.ErrNotExist) { + return "", "", fmt.Errorf("%w: directory does not exist", ErrInvalidLibrary) + } + if err != nil { + return "", "", fmt.Errorf("%w: cannot read the directory: %v", ErrInvalidLibrary, err) + } + info, err := os.Stat(real) + if err != nil || !info.IsDir() { + return "", "", fmt.Errorf("%w: path is not a directory", ErrInvalidLibrary) + } + + others, err := s.store.ListLibraries(ctx) + if err != nil { + return "", "", fmt.Errorf("list libraries: %w", err) + } + for _, o := range others { + if o.ID == skipID { + continue + } + if o.Path == path { + return "", "", ErrLibraryExists + } + // Compare real paths so a symlink cannot hide an overlap. A library + // whose directory has vanished is compared by its stored path. + otherReal, err := filepath.EvalSymlinks(o.Path) + if err != nil { + otherReal = o.Path + } + if real == otherReal { + return "", "", ErrLibraryExists + } + if within(real, otherReal) || within(otherReal, real) { + return "", "", fmt.Errorf("%w: overlaps the library %q", ErrInvalidLibrary, o.Name) + } + } + return name, path, nil +} + +// within reports whether child is strictly inside parent. +func within(child, parent string) bool { + return strings.HasPrefix(child, parent+string(filepath.Separator)) +} diff --git a/internal/library/libraries_test.go b/internal/library/libraries_test.go new file mode 100644 index 0000000..ac1e8e2 --- /dev/null +++ b/internal/library/libraries_test.go @@ -0,0 +1,272 @@ +package library + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" +) + +// memLibraries is an in-memory LibraryStore that enforces unique names and paths. +type memLibraries struct { + mu sync.Mutex + next int64 + libs []Library +} + +func (m *memLibraries) ListLibraries(context.Context) ([]Library, error) { + m.mu.Lock() + defer m.mu.Unlock() + return append([]Library(nil), m.libs...), nil +} + +func (m *memLibraries) GetLibrary(_ context.Context, id int64) (Library, error) { + m.mu.Lock() + defer m.mu.Unlock() + for _, l := range m.libs { + if l.ID == id { + return l, nil + } + } + return Library{}, ErrLibraryNotFound +} + +func (m *memLibraries) clash(name, path string, skip int64) bool { + for _, l := range m.libs { + if l.ID != skip && (strings.EqualFold(l.Name, name) || l.Path == path) { + return true + } + } + return false +} + +func (m *memLibraries) CreateLibrary(_ context.Context, name, path string) (Library, error) { + m.mu.Lock() + defer m.mu.Unlock() + if m.clash(name, path, 0) { + return Library{}, ErrLibraryExists + } + m.next++ + l := Library{ID: m.next, Name: name, Path: path, CreatedAt: time.Now()} + m.libs = append(m.libs, l) + return l, nil +} + +func (m *memLibraries) UpdateLibrary(_ context.Context, id int64, name, path string) (Library, error) { + m.mu.Lock() + defer m.mu.Unlock() + for i, l := range m.libs { + if l.ID == id { + if m.clash(name, path, id) { + return Library{}, ErrLibraryExists + } + m.libs[i].Name, m.libs[i].Path = name, path + return m.libs[i], nil + } + } + return Library{}, ErrLibraryNotFound +} + +func (m *memLibraries) DeleteLibrary(_ context.Context, id int64) error { + m.mu.Lock() + defer m.mu.Unlock() + for i, l := range m.libs { + if l.ID == id { + m.libs = append(m.libs[:i], m.libs[i+1:]...) + return nil + } + } + return ErrLibraryNotFound +} + +func newLibraries() (*Libraries, *memLibraries) { + st := &memLibraries{} + return NewLibraries(st), st +} + +func mkdir(t *testing.T, path string) string { + t.Helper() + if err := os.MkdirAll(path, 0o755); err != nil { + t.Fatal(err) + } + return path +} + +func TestCreateLibrary(t *testing.T) { + s, _ := newLibraries() + dir := t.TempDir() + + lib, err := s.Create(context.Background(), " Jazz ", dir+"/") + if err != nil { + t.Fatal(err) + } + if lib.Name != "Jazz" || lib.Path != dir || lib.ID == 0 { + t.Errorf("library = %+v, want trimmed name and cleaned path %q", lib, dir) + } +} + +func TestCreateLibraryValidation(t *testing.T) { + dir := t.TempDir() + file := filepath.Join(dir, "file.txt") + if err := os.WriteFile(file, []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + tests := []struct { + name, libName, path string + }{ + {"empty name", "", dir}, + {"blank name", " ", dir}, + {"long name", strings.Repeat("n", 101), dir}, + {"control character in name", "bad\x00name", dir}, + {"empty path", "x", ""}, + {"relative path", "x", "music/jazz"}, + {"filesystem root", "x", "/"}, + {"path does not exist", "x", filepath.Join(dir, "nope")}, + {"path is a file", "x", file}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s, _ := newLibraries() + if _, err := s.Create(context.Background(), tt.libName, tt.path); !errors.Is(err, ErrInvalidLibrary) { + t.Fatalf("err = %v, want ErrInvalidLibrary", err) + } + }) + } +} + +func TestCreateLibraryDuplicates(t *testing.T) { + s, _ := newLibraries() + ctx := context.Background() + a, b := mkdir(t, filepath.Join(t.TempDir(), "a")), mkdir(t, filepath.Join(t.TempDir(), "b")) + + if _, err := s.Create(ctx, "Music", a); err != nil { + t.Fatal(err) + } + if _, err := s.Create(ctx, "MUSIC", b); !errors.Is(err, ErrLibraryExists) { + t.Errorf("same name, other case: err = %v", err) + } + if _, err := s.Create(ctx, "Other", a); !errors.Is(err, ErrLibraryExists) { + t.Errorf("same path: err = %v", err) + } + if _, err := s.Create(ctx, "Other", a+"/"); !errors.Is(err, ErrLibraryExists) { + t.Errorf("same path with a trailing slash: err = %v", err) + } +} + +// Nested libraries would index the same files twice. +func TestCreateLibraryRejectsOverlap(t *testing.T) { + s, _ := newLibraries() + ctx := context.Background() + base := t.TempDir() + parent := mkdir(t, filepath.Join(base, "music")) + child := mkdir(t, filepath.Join(parent, "jazz")) + sibling := mkdir(t, filepath.Join(base, "music2")) // shares a string prefix, not a directory + + if _, err := s.Create(ctx, "Music", parent); err != nil { + t.Fatal(err) + } + if _, err := s.Create(ctx, "Jazz", child); !errors.Is(err, ErrInvalidLibrary) { + t.Errorf("inside an existing library: err = %v", err) + } + if _, err := s.Create(ctx, "Sibling", sibling); err != nil { + t.Errorf("a sibling with a common prefix must be allowed: %v", err) + } + if _, err := s.Create(ctx, "Everything", base); !errors.Is(err, ErrInvalidLibrary) { + t.Errorf("around existing libraries: err = %v", err) + } +} + +func TestCreateLibraryRejectsOverlapThroughSymlinks(t *testing.T) { + s, _ := newLibraries() + ctx := context.Background() + base := t.TempDir() + parent := mkdir(t, filepath.Join(base, "music")) + child := mkdir(t, filepath.Join(parent, "jazz")) + link := filepath.Join(base, "shortcut") + if err := os.Symlink(child, link); err != nil { + t.Skip("symlinks not supported:", err) + } + + if _, err := s.Create(ctx, "Music", parent); err != nil { + t.Fatal(err) + } + if _, err := s.Create(ctx, "Shortcut", link); !errors.Is(err, ErrInvalidLibrary) { + t.Errorf("a symlink into an existing library: err = %v", err) + } +} + +func TestUpdateLibrary(t *testing.T) { + s, _ := newLibraries() + ctx := context.Background() + base := t.TempDir() + a, b := mkdir(t, filepath.Join(base, "a")), mkdir(t, filepath.Join(base, "b")) + inside := mkdir(t, filepath.Join(a, "inside")) + + first, _ := s.Create(ctx, "First", a) + second, _ := s.Create(ctx, "Second", b) + str := func(v string) *string { return &v } + + // Rename only. + got, err := s.Update(ctx, first.ID, str("Renamed"), nil) + if err != nil || got.Name != "Renamed" || got.Path != a { + t.Fatalf("rename: %+v err=%v", got, err) + } + // Moving to its own current path is not an overlap with itself. + if _, err := s.Update(ctx, first.ID, nil, str(a)); err != nil { + t.Errorf("same path: %v", err) + } + // A new path in an unrelated place. + c := mkdir(t, filepath.Join(base, "c")) + if got, err = s.Update(ctx, first.ID, nil, str(c)); err != nil || got.Path != c { + t.Fatalf("move: %+v err=%v", got, err) + } + // Into another library, or onto its name or path. + if _, err := s.Update(ctx, first.ID, nil, str(b)); !errors.Is(err, ErrLibraryExists) { + t.Errorf("onto another library's path: err = %v", err) + } + if _, err := s.Update(ctx, second.ID, str("renamed"), nil); !errors.Is(err, ErrLibraryExists) { + t.Errorf("onto another library's name: err = %v", err) + } + if _, err := s.Update(ctx, first.ID, nil, str(filepath.Join(b, "deeper"))); !errors.Is(err, ErrInvalidLibrary) { + t.Errorf("a missing directory: err = %v", err) + } + _ = inside + if _, err := s.Update(ctx, first.ID, str(""), nil); !errors.Is(err, ErrInvalidLibrary) { + t.Errorf("empty name: err = %v", err) + } + if _, err := s.Update(ctx, 999, str("x"), nil); !errors.Is(err, ErrLibraryNotFound) { + t.Errorf("unknown id: err = %v", err) + } +} + +func TestDeleteLibrary(t *testing.T) { + s, st := newLibraries() + ctx := context.Background() + lib, _ := s.Create(ctx, "Music", t.TempDir()) + + if err := s.Delete(ctx, lib.ID); err != nil { + t.Fatal(err) + } + if len(st.libs) != 0 { + t.Error("library not deleted") + } + if err := s.Delete(ctx, lib.ID); !errors.Is(err, ErrLibraryNotFound) { + t.Errorf("deleting twice: err = %v", err) + } +} + +func TestListLibraries(t *testing.T) { + s, _ := newLibraries() + ctx := context.Background() + base := t.TempDir() + _, _ = s.Create(ctx, "A", mkdir(t, filepath.Join(base, "a"))) + _, _ = s.Create(ctx, "B", mkdir(t, filepath.Join(base, "b"))) + libs, err := s.List(ctx) + if err != nil || len(libs) != 2 { + t.Fatalf("libs = %+v err=%v", libs, err) + } +} diff --git a/internal/library/manager.go b/internal/library/manager.go index 868a334..7c309da 100644 --- a/internal/library/manager.go +++ b/internal/library/manager.go @@ -3,12 +3,22 @@ package library import ( "context" "errors" + "fmt" "sync" "time" ) -// ErrScanRunning is returned by Start while a scan is in progress. -var ErrScanRunning = errors.New("a scan is already running") +// Errors returned when a scan cannot start. +var ( + ErrScanRunning = errors.New("a scan is already running") + ErrNoLibraries = errors.New("there are no libraries to scan") +) + +// LibrarySource lists the libraries a scan can cover. LibraryStore satisfies it. +type LibrarySource interface { + ListLibraries(ctx context.Context) ([]Library, error) + GetLibrary(ctx context.Context, id int64) (Library, error) +} // Status describes the scan state for the API. type Status struct { @@ -19,16 +29,21 @@ type Status struct { LastError string } -// Manager runs at most one background scan of the configured roots. +// Manager runs at most one background scan at a time. type Manager struct { - scanner *Scanner - roots []string + scanner *Scanner + libraries LibrarySource mu sync.Mutex status Status onFinish func(Summary, error) } +// NewManager builds a Manager that scans libraries from the given source. +func NewManager(scanner *Scanner, libraries LibrarySource) *Manager { + return &Manager{scanner: scanner, libraries: libraries} +} + // OnFinish registers a function called once after every scan, with the result // of that scan. Unlike hub events it is never dropped. Set it before Start. // A new scan may start while a callback is still running, so callbacks of @@ -39,14 +54,31 @@ func (m *Manager) OnFinish(fn func(Summary, error)) { m.onFinish = fn } -// NewManager builds a Manager for the given roots. -func NewManager(scanner *Scanner, roots []string) *Manager { - return &Manager{scanner: scanner, roots: roots} +// Start scans every library in the background. ctx bounds the scan, so pass a +// context that lives as long as the application, not an HTTP request. +// It returns ErrNoLibraries when none are defined. +func (m *Manager) Start(ctx context.Context) error { + libs, err := m.libraries.ListLibraries(ctx) + if err != nil { + return fmt.Errorf("list libraries: %w", err) + } + if len(libs) == 0 { + return ErrNoLibraries + } + return m.start(ctx, libs) } -// Start launches a scan in the background. ctx bounds the scan, so pass a -// context that lives as long as the application, not an HTTP request. -func (m *Manager) Start(ctx context.Context) error { +// StartLibrary scans a single library in the background. +// It returns ErrLibraryNotFound for an unknown id. +func (m *Manager) StartLibrary(ctx context.Context, id int64) error { + lib, err := m.libraries.GetLibrary(ctx, id) + if err != nil { + return err + } + return m.start(ctx, []Library{lib}) +} + +func (m *Manager) start(ctx context.Context, libs []Library) error { m.mu.Lock() defer m.mu.Unlock() if m.status.Running { @@ -54,12 +86,12 @@ func (m *Manager) Start(ctx context.Context) error { } m.status.Running = true m.status.StartedAt = time.Now() - go m.run(ctx) + go m.run(ctx, libs) return nil } -func (m *Manager) run(ctx context.Context) { - sum, err := m.scanner.Scan(ctx, m.roots) +func (m *Manager) run(ctx context.Context, libs []Library) { + sum, err := m.scanner.Scan(ctx, libs) m.mu.Lock() m.status.Running = false diff --git a/internal/library/scanner.go b/internal/library/scanner.go index e387da0..4a2b5a6 100644 --- a/internal/library/scanner.go +++ b/internal/library/scanner.go @@ -14,9 +14,9 @@ import ( var audioExts = map[string]bool{".mp3": true, ".flac": true} -// Scanner indexes the audio files under a set of roots. +// Scanner indexes the audio files of a set of libraries. // -// Per root it walks the tree (cheap, stat only), decides what changed, then +// Per library it walks the tree (cheap, stat only), decides what changed, then // reads tags with a pool of workers. A single goroutine writes to the store. type Scanner struct { store Store @@ -34,18 +34,18 @@ func NewScanner(store Store, reader TagReader, hub *Hub, workers int) *Scanner { return &Scanner{store: store, reader: reader, hub: hub, workers: workers, now: time.Now} } -// Scan indexes every root in turn. An unreadable root is reported in the -// returned error and the other roots are still scanned. -func (s *Scanner) Scan(ctx context.Context, roots []string) (Summary, error) { - total := Summary{Roots: len(roots)} +// Scan indexes every library in turn. An unreadable library is reported in the +// returned error and the other libraries are still scanned. +func (s *Scanner) Scan(ctx context.Context, libs []Library) (Summary, error) { + total := Summary{Libraries: len(libs)} s.publish(Event{Type: ScanStarted}) var errs []error - for _, root := range roots { - sum, err := s.scanRoot(ctx, root) + for _, lib := range libs { + sum, err := s.scanLibrary(ctx, lib) total.add(sum) if err != nil { - errs = append(errs, fmt.Errorf("%s: %w", root, err)) + errs = append(errs, fmt.Errorf("%s: %w", lib.Name, err)) if ctx.Err() != nil { break } @@ -77,10 +77,10 @@ type parseResult struct { err error } -func (s *Scanner) scanRoot(ctx context.Context, root string) (Summary, error) { +func (s *Scanner) scanLibrary(ctx context.Context, lib Library) (Summary, error) { var sum Summary - walked, err := s.walk(ctx, root, &sum) + walked, err := s.walk(ctx, lib, &sum) if err != nil { // The root itself is unreadable (for example an unmounted NAS). // Do not touch the database: nothing is known about its files. @@ -88,23 +88,23 @@ func (s *Scanner) scanRoot(ctx context.Context, root string) (Summary, error) { } sum.Found = len(walked.files) - existing, err := s.store.ExistingFiles(ctx, root) + existing, err := s.store.ExistingFiles(ctx, lib.ID) 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. + // deletion. Delete the library to really empty it. 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? "+ + return sum, fmt.Errorf("directory 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 - if err := s.parseAndSave(ctx, root, p.parse, &sum); err != nil { + if err := s.parseAndSave(ctx, lib, p.parse, &sum); err != nil { return sum, err } if err := ctx.Err(); err != nil { @@ -113,7 +113,7 @@ func (s *Scanner) scanRoot(ctx context.Context, root string) (Summary, error) { for _, r := range p.renames { if err := s.store.RenameFile(ctx, r.id, r.newRel); err != nil { - s.fail(root, r.newRel, err, &sum) + s.fail(lib, r.newRel, err, &sum) continue } sum.Renamed++ @@ -133,13 +133,13 @@ func (s *Scanner) scanRoot(ctx context.Context, root string) (Summary, error) { return sum, nil } -func (s *Scanner) fail(root, rel string, err error, sum *Summary) { +func (s *Scanner) fail(lib Library, rel string, err error, sum *Summary) { sum.Failed++ - s.publish(Event{Type: FileFailed, Root: root, RelPath: rel, Error: err.Error()}) + s.publish(Event{Type: FileFailed, LibraryID: lib.ID, Library: lib.Name, RelPath: rel, Error: err.Error()}) } // parseAndSave reads tags in parallel and stores results from this goroutine. -func (s *Scanner) parseAndSave(ctx context.Context, root string, jobs []parseJob, sum *Summary) error { +func (s *Scanner) parseAndSave(ctx context.Context, lib Library, jobs []parseJob, sum *Summary) error { if len(jobs) == 0 { return nil } @@ -153,7 +153,7 @@ func (s *Scanner) parseAndSave(ctx context.Context, root string, jobs []parseJob go func() { defer wg.Done() for j := range jobCh { - meta, err := s.reader.Read(filepath.Join(root, filepath.FromSlash(j.file.RelPath))) + meta, err := s.reader.Read(filepath.Join(lib.Path, filepath.FromSlash(j.file.RelPath))) resCh <- parseResult{job: j, meta: meta, err: err} } }() @@ -182,13 +182,13 @@ func (s *Scanner) parseAndSave(ctx context.Context, root string, jobs []parseJob rel := r.job.file.RelPath if r.err != nil { sum.Failed++ - s.publish(Event{Type: FileFailed, Root: root, RelPath: rel, Error: r.err.Error(), Done: done, Total: len(jobs)}) + s.publish(Event{Type: FileFailed, LibraryID: lib.ID, Library: lib.Name, RelPath: rel, Error: r.err.Error(), Done: done, Total: len(jobs)}) continue } - added, err := s.store.SaveFile(ctx, root, ParsedFile{FileInfo: r.job.file, Metadata: r.meta}, r.job.existingID) + added, err := s.store.SaveFile(ctx, lib.ID, ParsedFile{FileInfo: r.job.file, Metadata: r.meta}, r.job.existingID) if err != nil { sum.Failed++ - s.publish(Event{Type: FileFailed, Root: root, RelPath: rel, Error: err.Error(), Done: done, Total: len(jobs)}) + s.publish(Event{Type: FileFailed, LibraryID: lib.ID, Library: lib.Name, RelPath: rel, Error: err.Error(), Done: done, Total: len(jobs)}) continue } if added { @@ -196,7 +196,7 @@ func (s *Scanner) parseAndSave(ctx context.Context, root string, jobs []parseJob } else { sum.Updated++ } - s.publish(Event{Type: FileScanned, Root: root, RelPath: rel, Done: done, Total: len(jobs)}) + s.publish(Event{Type: FileScanned, LibraryID: lib.ID, Library: lib.Name, RelPath: rel, Done: done, Total: len(jobs)}) } return ctx.Err() } @@ -213,13 +213,13 @@ type walkResult struct { failedFiles map[string]bool } -func (s *Scanner) walk(ctx context.Context, root string, sum *Summary) (walkResult, error) { +func (s *Scanner) walk(ctx context.Context, lib Library, sum *Summary) (walkResult, error) { var out walkResult - // The configured root may itself be a symlink (for example /srv/music -> + // The library path 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) + walkRoot, err := filepath.EvalSymlinks(lib.Path) if err != nil { return out, err } @@ -228,7 +228,7 @@ func (s *Scanner) walk(ctx context.Context, root string, sum *Summary) (walkResu return out, err } if !info.IsDir() { - return out, fmt.Errorf("%s is not a directory", root) + return out, fmt.Errorf("%s is not a directory", lib.Path) } err = filepath.WalkDir(walkRoot, func(path string, d fs.DirEntry, err error) error { @@ -245,7 +245,7 @@ func (s *Scanner) walk(ctx context.Context, root string, sum *Summary) (walkResu rel = filepath.ToSlash(rel) if err != nil { - s.fail(root, rel, err, sum) + s.fail(lib, rel, err, sum) if d != nil && d.IsDir() { out.failedDirs = append(out.failedDirs, rel+"/") } else { @@ -267,7 +267,7 @@ func (s *Scanner) walk(ctx context.Context, root string, sum *Summary) (walkResu } info, ierr := d.Info() if ierr != nil { - s.fail(root, rel, ierr, sum) + s.fail(lib, rel, ierr, sum) if !errors.Is(ierr, fs.ErrNotExist) { out.markFileFailed(rel) } diff --git a/internal/library/scanner_test.go b/internal/library/scanner_test.go index dd7a46a..41ff0c6 100644 --- a/internal/library/scanner_test.go +++ b/internal/library/scanner_test.go @@ -21,11 +21,11 @@ type fakeRow struct { type fakeStore struct { mu sync.Mutex nextID int64 - rows map[string]map[string]*fakeRow // root -> rel path -> row + rows map[int64]map[string]*fakeRow // library id -> rel path -> row saves int } -func newFakeStore() *fakeStore { return &fakeStore{rows: map[string]map[string]*fakeRow{}} } +func newFakeStore() *fakeStore { return &fakeStore{rows: map[int64]map[string]*fakeRow{}} } func (s *fakeStore) byID(id int64) *fakeRow { for _, files := range s.rows { @@ -38,17 +38,17 @@ func (s *fakeStore) byID(id int64) *fakeRow { return nil } -func (s *fakeStore) ExistingFiles(_ context.Context, root string) ([]ExistingFile, error) { +func (s *fakeStore) ExistingFiles(_ context.Context, libraryID int64) ([]ExistingFile, error) { s.mu.Lock() defer s.mu.Unlock() var out []ExistingFile - for _, r := range s.rows[root] { + for _, r := range s.rows[libraryID] { out = append(out, r.ExistingFile) } return out, nil } -func (s *fakeStore) SaveFile(_ context.Context, root string, f ParsedFile, existingID int64) (bool, error) { +func (s *fakeStore) SaveFile(_ context.Context, libraryID int64, f ParsedFile, existingID int64) (bool, error) { s.mu.Lock() defer s.mu.Unlock() s.saves++ @@ -57,11 +57,11 @@ func (s *fakeStore) SaveFile(_ context.Context, root string, f ParsedFile, exist r.Size, r.MTimeUnix, r.Missing, r.Parsed = f.Size, f.MTimeUnix, false, f return false, nil } - if s.rows[root] == nil { - s.rows[root] = map[string]*fakeRow{} + if s.rows[libraryID] == nil { + s.rows[libraryID] = map[string]*fakeRow{} } s.nextID++ - s.rows[root][f.RelPath] = &fakeRow{ + s.rows[libraryID][f.RelPath] = &fakeRow{ ExistingFile: ExistingFile{ID: s.nextID, FileInfo: f.FileInfo}, Parsed: f, } @@ -102,10 +102,52 @@ func (s *fakeStore) Restore(_ context.Context, ids []int64) error { return nil } +// row looks a file up by the library path used in the test. func (s *fakeStore) row(root, rel string) *fakeRow { s.mu.Lock() defer s.mu.Unlock() - return s.rows[root][rel] + return s.rows[libOf(root).ID][rel] +} + +// ---- libraries ---- + +var ( + libMu sync.Mutex + libIDs = map[string]int64{} +) + +// libOf gives every directory used in a test a stable library. +func libOf(root string) Library { + libMu.Lock() + defer libMu.Unlock() + id, ok := libIDs[root] + if !ok { + id = int64(len(libIDs) + 1) + libIDs[root] = id + } + return Library{ID: id, Name: filepath.Base(root), Path: root} +} + +func libsOf(roots ...string) []Library { + out := make([]Library, len(roots)) + for i, r := range roots { + out[i] = libOf(r) + } + return out +} + +// fakeSource is a LibrarySource over a fixed list. +type fakeSource struct{ libs []Library } + +func (f fakeSource) ListLibraries(context.Context) ([]Library, error) { return f.libs, nil } + +func (f fakeSource) GetLibrary(_ context.Context, id int64) (Library, error) { + for _, l := range f.libs { + if l.ID == id { + return l, nil + } + } + return Library{}, ErrLibraryNotFound } // fakeReader returns metadata derived from the file name and counts calls. @@ -158,7 +200,7 @@ func newScanner(store Store, reader TagReader, workers int) (*Scanner, *Hub) { func mustScan(t *testing.T, s *Scanner, roots ...string) Summary { t.Helper() - sum, err := s.Scan(context.Background(), roots) + sum, err := s.Scan(context.Background(), libsOf(roots...)) if err != nil { t.Fatal(err) } @@ -366,7 +408,7 @@ func TestUnreadableRootDoesNotMarkFilesMissing(t *testing.T) { if err := os.RemoveAll(root); err != nil { t.Fatal(err) } - if _, err := s.Scan(context.Background(), []string{root}); err == nil { + if _, err := s.Scan(context.Background(), libsOf(root)); err == nil { t.Fatal("expected an error for an unreadable root") } if store.row(root, "a.mp3").Missing { @@ -408,7 +450,7 @@ func TestSymlinkRootToAFileIsRejected(t *testing.T) { t.Skip("symlinks not supported:", err) } s, _ := newScanner(newFakeStore(), &fakeReader{}, 1) - if _, err := s.Scan(context.Background(), []string{link}); err == nil { + if _, err := s.Scan(context.Background(), libsOf(link)); err == nil { t.Fatal("a root that is not a directory must be an error") } } @@ -425,7 +467,7 @@ func TestEmptyRootDoesNotMarkIndexedFilesMissing(t *testing.T) { // 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}) + sum, err := s.Scan(context.Background(), libsOf(root)) if err == nil { t.Fatal("expected an error: root is empty but files are indexed") } @@ -436,7 +478,7 @@ func TestEmptyRootDoesNotMarkIndexedFilesMissing(t *testing.T) { func TestEmptyRootWithNothingIndexedIsFine(t *testing.T) { s, _ := newScanner(newFakeStore(), &fakeReader{}, 1) - sum, err := s.Scan(context.Background(), []string{t.TempDir()}) + sum, err := s.Scan(context.Background(), libsOf(t.TempDir())) if err != nil || sum.Found != 0 { t.Fatalf("fresh empty library: %+v err=%v", sum, err) } @@ -531,7 +573,7 @@ func TestCancelledScanDoesNotMarkMissing(t *testing.T) { _ = os.Remove(filepath.Join(root, "a.mp3")) ctx, cancel := context.WithCancel(context.Background()) cancel() - if _, err := s.Scan(ctx, []string{root}); !errors.Is(err, context.Canceled) { + if _, err := s.Scan(ctx, libsOf(root)); !errors.Is(err, context.Canceled) { t.Fatalf("err = %v, want context.Canceled", err) } if store.row(root, "a.mp3").Missing { @@ -580,7 +622,7 @@ func TestMultipleRootsAreIndependent(t *testing.T) { s, _ := newScanner(store, &fakeReader{}, 2) sum := mustScan(t, s, a, b) - if sum.Added != 2 || sum.Roots != 2 { + if sum.Added != 2 || sum.Libraries != 2 { t.Fatalf("summary = %+v", sum) } if store.row(a, "same/name.mp3") == nil || store.row(b, "same/name.mp3") == nil { @@ -595,7 +637,7 @@ func TestOneBadRootDoesNotStopTheOthers(t *testing.T) { store := newFakeStore() s, _ := newScanner(store, &fakeReader{}, 1) - sum, err := s.Scan(context.Background(), []string{bad, good}) + sum, err := s.Scan(context.Background(), libsOf(bad, good)) if err == nil { t.Fatal("expected an error mentioning the bad root") } @@ -651,7 +693,7 @@ func TestManagerRunsOneScanAtATime(t *testing.T) { write(t, filepath.Join(root, "a.mp3"), "1") reader := &fakeReader{delay: 100 * time.Millisecond} s, _ := newScanner(newFakeStore(), reader, 1) - m := NewManager(s, []string{root}) + m := NewManager(s, fakeSource{libs: libsOf(root)}) if err := m.Start(context.Background()); err != nil { t.Fatal(err) @@ -680,7 +722,7 @@ func TestManagerCallsOnFinishOnce(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "a.mp3"), "1") s, _ := newScanner(newFakeStore(), &fakeReader{}, 1) - m := NewManager(s, []string{root}) + m := NewManager(s, fakeSource{libs: libsOf(root)}) type finished struct { sum Summary @@ -706,3 +748,48 @@ func TestManagerCallsOnFinishOnce(t *testing.T) { case <-time.After(100 * time.Millisecond): } } + +func TestManagerWithoutLibraries(t *testing.T) { + s, _ := newScanner(newFakeStore(), &fakeReader{}, 1) + m := NewManager(s, fakeSource{}) + if err := m.Start(context.Background()); !errors.Is(err, ErrNoLibraries) { + t.Fatalf("err = %v, want ErrNoLibraries", err) + } + if m.Status().Running { + t.Error("a scan that could not start must not look running") + } +} + +func TestManagerScansOneLibrary(t *testing.T) { + a, b := t.TempDir(), t.TempDir() + write(t, filepath.Join(a, "a.mp3"), "1") + write(t, filepath.Join(b, "b.mp3"), "22") + + store := newFakeStore() + s, _ := newScanner(store, &fakeReader{}, 1) + m := NewManager(s, fakeSource{libs: libsOf(a, b)}) + + if err := m.StartLibrary(context.Background(), 999); !errors.Is(err, ErrLibraryNotFound) { + t.Fatalf("unknown library: err = %v", err) + } + if m.Status().Running { + t.Fatal("an unknown library must not start a scan") + } + + if err := m.StartLibrary(context.Background(), libOf(a).ID); err != nil { + t.Fatal(err) + } + deadline := time.Now().Add(3 * time.Second) + for m.Status().Running && time.Now().Before(deadline) { + time.Sleep(10 * time.Millisecond) + } + if store.row(a, "a.mp3") == nil { + t.Error("the requested library was not scanned") + } + if store.row(b, "b.mp3") != nil { + t.Error("another library was scanned too") + } + if st := m.Status(); st.Last == nil || st.Last.Libraries != 1 { + t.Errorf("status = %+v", st) + } +} diff --git a/internal/library/special_unix_test.go b/internal/library/special_unix_test.go index 04f9940..7bec50a 100644 --- a/internal/library/special_unix_test.go +++ b/internal/library/special_unix_test.go @@ -27,7 +27,7 @@ func TestSpecialFilesAreSkipped(t *testing.T) { } done := make(chan result, 1) go func() { - sum, err := s.Scan(context.Background(), []string{root}) + sum, err := s.Scan(context.Background(), libsOf(root)) done <- result{sum, err} }() diff --git a/internal/library/types.go b/internal/library/types.go index 1d6c47c..9aa4439 100644 --- a/internal/library/types.go +++ b/internal/library/types.go @@ -65,10 +65,10 @@ type ParsedFile struct { // Store is what the scanner needs from persistence. All writes come from a // single goroutine, so implementations need no extra locking for the scanner. type Store interface { - ExistingFiles(ctx context.Context, root string) ([]ExistingFile, error) + ExistingFiles(ctx context.Context, libraryID int64) ([]ExistingFile, error) // SaveFile creates the file (existingID == 0) or updates it, together with // its track, album and artists. It reports whether a new file was created. - SaveFile(ctx context.Context, root string, f ParsedFile, existingID int64) (added bool, err error) + SaveFile(ctx context.Context, libraryID int64, f ParsedFile, existingID int64) (added bool, err error) // RenameFile points an indexed file at a new relative path and clears its missing flag. RenameFile(ctx context.Context, id int64, newRelPath string) error // MarkMissing flags files that were not found. Rows are never deleted. @@ -89,21 +89,22 @@ const ( ) // Event is one progress notification. Done and Total count files being -// parsed in the current root. +// parsed in the current library. type Event struct { - Type EventType - Time time.Time - Root string - RelPath string - Error string - Done int - Total int - Summary *Summary // only on ScanFinished + Type EventType + Time time.Time + LibraryID int64 + Library string // library name + RelPath string + Error string + Done int + Total int + Summary *Summary // only on ScanFinished } // Summary counts what a scan did. type Summary struct { - Roots int + Libraries int Found int // audio files seen on disk Added int Updated int diff --git a/internal/storage/library.go b/internal/storage/library.go index ffc3e55..6765691 100644 --- a/internal/storage/library.go +++ b/internal/storage/library.go @@ -55,7 +55,7 @@ type audioFileModel struct { ID int64 `bun:"id,pk,autoincrement"` TrackID int64 `bun:"track_id,notnull"` - Root string `bun:"root,notnull"` + LibraryID int64 `bun:"library_id,notnull"` RelPath string `bun:"rel_path,notnull"` Format string `bun:"format,notnull"` SizeBytes int64 `bun:"size_bytes,notnull"` @@ -65,16 +65,30 @@ type audioFileModel struct { MissingAt sql.NullInt64 `bun:"missing_at"` } -// LibraryRepo stores the scanned catalog. It implements library.Store. +type libraryModel struct { + bun.BaseModel `bun:"table:libraries"` + + ID int64 `bun:"id,pk,autoincrement"` + Name string `bun:"name,notnull"` + Path string `bun:"path,notnull"` + CreatedAt int64 `bun:"created_at,notnull"` +} + +func (m libraryModel) toLibrary() library.Library { + return library.Library{ID: m.ID, Name: m.Name, Path: m.Path, CreatedAt: time.Unix(m.CreatedAt, 0)} +} + +// LibraryRepo stores the scanned catalog and the libraries themselves. It +// implements library.Store, library.LibraryStore and streaming.Source. type LibraryRepo struct{ db *bun.DB } // NewLibraryRepo builds a LibraryRepo. func NewLibraryRepo(db *bun.DB) *LibraryRepo { return &LibraryRepo{db: db} } -// ExistingFiles lists the files indexed under root, including missing ones. -func (r *LibraryRepo) ExistingFiles(ctx context.Context, root string) ([]library.ExistingFile, error) { +// ExistingFiles lists the files indexed in a library, including missing ones. +func (r *LibraryRepo) ExistingFiles(ctx context.Context, libraryID int64) ([]library.ExistingFile, error) { var rows []audioFileModel - if err := r.db.NewSelect().Model(&rows).Where("root = ?", root).Scan(ctx); err != nil { + if err := r.db.NewSelect().Model(&rows).Where("library_id = ?", libraryID).Scan(ctx); err != nil { return nil, fmt.Errorf("select audio files: %w", err) } out := make([]library.ExistingFile, len(rows)) @@ -90,7 +104,7 @@ func (r *LibraryRepo) ExistingFiles(ctx context.Context, root string) ([]library // SaveFile creates or updates one file with its track, album and artists in a // single transaction. MusicBrainz ids are never invented: they stay NULL. -func (r *LibraryRepo) SaveFile(ctx context.Context, root string, f library.ParsedFile, existingID int64) (bool, error) { +func (r *LibraryRepo) SaveFile(ctx context.Context, libraryID int64, f library.ParsedFile, existingID int64) (bool, error) { var added bool err := r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { artistIDs := make([]int64, 0, len(f.Artists)) @@ -125,7 +139,7 @@ func (r *LibraryRepo) SaveFile(ctx context.Context, root string, f library.Parse if _, err := tx.NewInsert().Model(track).Exec(ctx); err != nil { return fmt.Errorf("insert track: %w", err) } - file := newAudioFile(root, f, track.ID) + file := newAudioFile(libraryID, f, track.ID) if _, err := tx.NewInsert().Model(file).Exec(ctx); err != nil { return fmt.Errorf("insert audio file: %w", err) } @@ -159,9 +173,9 @@ func (r *LibraryRepo) SaveFile(ctx context.Context, root string, f library.Parse return added, nil } -func newAudioFile(root string, f library.ParsedFile, trackID int64) *audioFileModel { +func newAudioFile(libraryID int64, f library.ParsedFile, trackID int64) *audioFileModel { return &audioFileModel{ - TrackID: trackID, Root: root, RelPath: f.RelPath, Format: f.Format, + TrackID: trackID, LibraryID: libraryID, RelPath: f.RelPath, Format: f.Format, SizeBytes: f.Size, MTimeUnix: f.MTimeUnix, SampleRate: f.SampleRate, BitRate: f.BitRate, } } @@ -207,21 +221,99 @@ func (r *LibraryRepo) Restore(ctx context.Context, ids []int64) error { } // PlayableFile returns the file to play for a track: the oldest one that was -// not flagged missing. It returns streaming.ErrNotFound when the track does not -// exist or has no such file. It implements streaming.Source. +// not flagged missing, with the path of its library. It returns +// streaming.ErrNotFound when the track does not exist or has no such file. It +// implements streaming.Source. func (r *LibraryRepo) PlayableFile(ctx context.Context, trackID int64) (streaming.File, error) { - var row audioFileModel - err := r.db.NewSelect().Model(&row). - Where("track_id = ?", trackID). - Where("missing_at IS NULL"). - OrderExpr("id").Limit(1).Scan(ctx) + var row struct { + Path string `bun:"path"` + RelPath string `bun:"rel_path"` + } + err := r.db.NewSelect(). + TableExpr("audio_files AS f"). + ColumnExpr("l.path AS path, f.rel_path AS rel_path"). + Join("JOIN libraries AS l ON l.id = f.library_id"). + Where("f.track_id = ?", trackID). + Where("f.missing_at IS NULL"). + OrderExpr("f.id").Limit(1). + Scan(ctx, &row) if errors.Is(err, sql.ErrNoRows) { return streaming.File{}, streaming.ErrNotFound } if err != nil { return streaming.File{}, fmt.Errorf("select playable file: %w", err) } - return streaming.File{Root: row.Root, RelPath: row.RelPath}, nil + return streaming.File{Root: row.Path, RelPath: row.RelPath}, nil +} + +// ---- libraries ---- + +// ListLibraries returns every library, oldest first. +func (r *LibraryRepo) ListLibraries(ctx context.Context) ([]library.Library, error) { + var rows []libraryModel + if err := r.db.NewSelect().Model(&rows).OrderExpr("id").Scan(ctx); err != nil { + return nil, fmt.Errorf("list libraries: %w", err) + } + out := make([]library.Library, len(rows)) + for i, row := range rows { + out[i] = row.toLibrary() + } + return out, nil +} + +// GetLibrary returns one library, or library.ErrLibraryNotFound. +func (r *LibraryRepo) GetLibrary(ctx context.Context, id int64) (library.Library, error) { + var row libraryModel + err := r.db.NewSelect().Model(&row).Where("id = ?", id).Limit(1).Scan(ctx) + if errors.Is(err, sql.ErrNoRows) { + return library.Library{}, library.ErrLibraryNotFound + } + if err != nil { + return library.Library{}, fmt.Errorf("select library: %w", err) + } + return row.toLibrary(), nil +} + +// CreateLibrary adds a library. Names are unique case-insensitively, paths exactly. +func (r *LibraryRepo) CreateLibrary(ctx context.Context, name, path string) (library.Library, error) { + row := &libraryModel{Name: name, Path: path, CreatedAt: time.Now().Unix()} + if _, err := r.db.NewInsert().Model(row).Exec(ctx); err != nil { + if isUniqueViolation(err) { + return library.Library{}, library.ErrLibraryExists + } + return library.Library{}, fmt.Errorf("insert library: %w", err) + } + return row.toLibrary(), nil +} + +// UpdateLibrary changes the name and path of a library. Its files keep their +// identity because they point at the library by id. +func (r *LibraryRepo) UpdateLibrary(ctx context.Context, id int64, name, path string) (library.Library, error) { + res, err := r.db.NewUpdate().Model((*libraryModel)(nil)). + Set("name = ?", name).Set("path = ?", path).Where("id = ?", id).Exec(ctx) + if err != nil { + if isUniqueViolation(err) { + return library.Library{}, library.ErrLibraryExists + } + return library.Library{}, fmt.Errorf("update library: %w", err) + } + if n, _ := res.RowsAffected(); n == 0 { + return library.Library{}, library.ErrLibraryNotFound + } + return r.GetLibrary(ctx, id) +} + +// DeleteLibrary removes a library and, through the foreign key, its indexed +// files. Tracks stay, so listening history is kept. Music on disk is untouched. +func (r *LibraryRepo) DeleteLibrary(ctx context.Context, id int64) error { + res, err := r.db.NewDelete().Model((*libraryModel)(nil)).Where("id = ?", id).Exec(ctx) + if err != nil { + return fmt.Errorf("delete library: %w", err) + } + if n, _ := res.RowsAffected(); n == 0 { + return library.ErrLibraryNotFound + } + return nil } // RefreshMatchKeys recomputes name_key and title_key for every artist and album diff --git a/internal/storage/library_test.go b/internal/storage/library_test.go index 8d59a33..3137e5c 100644 --- a/internal/storage/library_test.go +++ b/internal/storage/library_test.go @@ -31,6 +31,16 @@ func parsed(rel, title, album string, artists ...string) library.ParsedFile { } } +// mkLib creates a library row and returns its id. +func mkLib(t *testing.T, repo *LibraryRepo, name string) int64 { + t.Helper() + lib, err := repo.CreateLibrary(context.Background(), name, "/lib/"+name) + if err != nil { + t.Fatal(err) + } + return lib.ID +} + func count(t *testing.T, db *bun.DB, query string, args ...any) int { t.Helper() var n int @@ -44,13 +54,14 @@ func TestLibraryRepoSaveNewFile(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() repo := NewLibraryRepo(db) + lib := mkLib(t, repo, "music") - added, err := repo.SaveFile(ctx, "/music", parsed("a/1.flac", "Song", "Album", "Radiohead"), 0) + added, err := repo.SaveFile(ctx, lib, parsed("a/1.flac", "Song", "Album", "Radiohead"), 0) if err != nil || !added { t.Fatalf("added=%v err=%v", added, err) } // Same artist (different case) and same album are reused, not duplicated. - if _, err := repo.SaveFile(ctx, "/music", parsed("a/2.flac", "Other", "album", "RADIOHEAD"), 0); err != nil { + if _, err := repo.SaveFile(ctx, lib, parsed("a/2.flac", "Other", "album", "RADIOHEAD"), 0); err != nil { t.Fatal(err) } @@ -63,7 +74,7 @@ func TestLibraryRepoSaveNewFile(t *testing.T) { if n := count(t, db, "SELECT COUNT(*) FROM tracks"); n != 2 { t.Errorf("tracks = %d, want 2", n) } - if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE root = ?", "/music"); n != 2 { + if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE library_id = ?", lib); n != 2 { t.Errorf("audio_files = %d, want 2", n) } if n := count(t, db, "SELECT COUNT(*) FROM track_artists"); n != 2 { @@ -87,16 +98,17 @@ func TestLibraryRepoMatchesUnicodeNamesCaseInsensitively(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() repo := NewLibraryRepo(db) - if _, err := repo.SaveFile(ctx, "/m", parsed("1.flac", "Hyperballad", "Post", "Björk"), 0); err != nil { + lib := mkLib(t, repo, "m") + if _, err := repo.SaveFile(ctx, lib, parsed("1.flac", "Hyperballad", "Post", "Björk"), 0); err != nil { t.Fatal(err) } - if _, err := repo.SaveFile(ctx, "/m", parsed("2.flac", "Army of Me", "POST", "BJÖRK"), 0); err != nil { + if _, err := repo.SaveFile(ctx, lib, parsed("2.flac", "Army of Me", "POST", "BJÖRK"), 0); err != nil { t.Fatal(err) } - if _, err := repo.SaveFile(ctx, "/m", parsed("3.flac", "Isobel", "Ünïcode Album", "Åsa"), 0); err != nil { + if _, err := repo.SaveFile(ctx, lib, parsed("3.flac", "Isobel", "Ünïcode Album", "Åsa"), 0); err != nil { t.Fatal(err) } - if _, err := repo.SaveFile(ctx, "/m", parsed("4.flac", "Other", "ÜNÏCODE ALBUM", "ÅSA"), 0); err != nil { + if _, err := repo.SaveFile(ctx, lib, parsed("4.flac", "Other", "ÜNÏCODE ALBUM", "ÅSA"), 0); err != nil { t.Fatal(err) } @@ -115,6 +127,7 @@ func TestLibraryRepoRefreshMatchKeysFixesOldRows(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() repo := NewLibraryRepo(db) + lib := mkLib(t, repo, "m") // Simulate what the migration backfill leaves behind on SQLite. if _, err := db.Exec("INSERT INTO artists (name, name_key) VALUES ('BJÖRK', 'bjÖrk')"); err != nil { @@ -131,7 +144,7 @@ func TestLibraryRepoRefreshMatchKeysFixesOldRows(t *testing.T) { t.Fatal(err) } - if _, err := repo.SaveFile(ctx, "/m", parsed("1.flac", "Hyperballad", "post", "Björk"), 0); err != nil { + if _, err := repo.SaveFile(ctx, lib, parsed("1.flac", "Hyperballad", "post", "Björk"), 0); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM artists"); n != 1 { @@ -146,7 +159,8 @@ func TestLibraryRepoRefreshMatchKeysFixesOldRows(t *testing.T) { func TestLibraryRepoTrackWithoutAlbum(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { repo := NewLibraryRepo(db) - if _, err := repo.SaveFile(context.Background(), "/m", parsed("x.flac", "Loose", "", "Someone"), 0); err != nil { + lib := mkLib(t, repo, "m") + if _, err := repo.SaveFile(context.Background(), lib, parsed("x.flac", "Loose", "", "Someone"), 0); err != nil { t.Fatal(err) } if n := count(t, db, "SELECT COUNT(*) FROM albums"); n != 0 { @@ -162,10 +176,11 @@ func TestLibraryRepoUpdateKeepsIdentityAndReplacesCredits(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() repo := NewLibraryRepo(db) - if _, err := repo.SaveFile(ctx, "/m", parsed("s.flac", "Old", "Album", "Artist A"), 0); err != nil { + lib := mkLib(t, repo, "m") + if _, err := repo.SaveFile(ctx, lib, parsed("s.flac", "Old", "Album", "Artist A"), 0); err != nil { t.Fatal(err) } - existing, err := repo.ExistingFiles(ctx, "/m") + existing, err := repo.ExistingFiles(ctx, lib) if err != nil || len(existing) != 1 { t.Fatalf("existing = %v err=%v", existing, err) } @@ -173,7 +188,7 @@ func TestLibraryRepoUpdateKeepsIdentityAndReplacesCredits(t *testing.T) { upd := parsed("s.flac", "New", "Album", "Artist B") upd.Size, upd.MTimeUnix = 200, 1_800_000_000 - added, err := repo.SaveFile(ctx, "/m", upd, id) + added, err := repo.SaveFile(ctx, lib, upd, id) if err != nil || added { t.Fatalf("update: added=%v err=%v", added, err) } @@ -193,25 +208,26 @@ func TestLibraryRepoUpdateKeepsIdentityAndReplacesCredits(t *testing.T) { if n := count(t, db, `SELECT COUNT(*) FROM track_artists ta JOIN artists a ON a.id = ta.artist_id WHERE a.name = 'Artist A'`); n != 0 { t.Error("old credit still present") } - after, _ := repo.ExistingFiles(ctx, "/m") + after, _ := repo.ExistingFiles(ctx, lib) if after[0].ID != id || after[0].Size != 200 || after[0].MTimeUnix != 1_800_000_000 { t.Errorf("after update = %+v", after[0]) } }) } -func TestLibraryRepoSameRelPathUnderTwoRoots(t *testing.T) { +func TestLibraryRepoSameRelPathUnderTwoLibraries(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() repo := NewLibraryRepo(db) - for _, root := range []string{"/a", "/b"} { - if _, err := repo.SaveFile(ctx, root, parsed("same.flac", "S", "", "X"), 0); err != nil { - t.Fatalf("root %s: %v", root, err) + a, b := mkLib(t, repo, "a"), mkLib(t, repo, "b") + for _, lib := range []int64{a, b} { + if _, err := repo.SaveFile(ctx, lib, parsed("same.flac", "S", "", "X"), 0); err != nil { + t.Fatalf("library %d: %v", lib, err) } } - for _, root := range []string{"/a", "/b"} { - if ex, _ := repo.ExistingFiles(ctx, root); len(ex) != 1 { - t.Errorf("root %s has %d files, want 1", root, len(ex)) + for _, lib := range []int64{a, b} { + if ex, _ := repo.ExistingFiles(ctx, lib); len(ex) != 1 { + t.Errorf("library %d has %d files, want 1", lib, len(ex)) } } }) @@ -221,16 +237,17 @@ func TestLibraryRepoMissingRestoreRename(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() repo := NewLibraryRepo(db) - if _, err := repo.SaveFile(ctx, "/m", parsed("a.flac", "A", "", "X"), 0); err != nil { + lib := mkLib(t, repo, "m") + if _, err := repo.SaveFile(ctx, lib, parsed("a.flac", "A", "", "X"), 0); err != nil { t.Fatal(err) } - ex, _ := repo.ExistingFiles(ctx, "/m") + ex, _ := repo.ExistingFiles(ctx, lib) id := ex[0].ID if err := repo.MarkMissing(ctx, []int64{id}, time.Unix(1_900_000_000, 0)); err != nil { t.Fatal(err) } - ex, _ = repo.ExistingFiles(ctx, "/m") + ex, _ = repo.ExistingFiles(ctx, lib) if len(ex) != 1 || !ex[0].Missing { t.Fatalf("after MarkMissing: %+v (row must be kept, flagged)", ex) } @@ -241,7 +258,7 @@ func TestLibraryRepoMissingRestoreRename(t *testing.T) { if err := repo.Restore(ctx, []int64{id}); err != nil { t.Fatal(err) } - if ex, _ = repo.ExistingFiles(ctx, "/m"); ex[0].Missing { + if ex, _ = repo.ExistingFiles(ctx, lib); ex[0].Missing { t.Error("Restore did not clear the missing flag") } @@ -249,7 +266,7 @@ func TestLibraryRepoMissingRestoreRename(t *testing.T) { if err := repo.RenameFile(ctx, id, "sub/b.flac"); err != nil { t.Fatal(err) } - ex, _ = repo.ExistingFiles(ctx, "/m") + ex, _ = repo.ExistingFiles(ctx, lib) if ex[0].ID != id || ex[0].RelPath != "sub/b.flac" || ex[0].Missing { t.Errorf("after rename: %+v", ex[0]) } @@ -265,7 +282,8 @@ func TestLibraryRepoPlayableFile(t *testing.T) { t.Fatalf("unknown track: err = %v, want ErrNotFound", err) } - if _, err := repo.SaveFile(ctx, "/music", parsed("a/1.flac", "Song", "", "X"), 0); err != nil { + lib := mkLib(t, repo, "music") // path /lib/music + if _, err := repo.SaveFile(ctx, lib, parsed("a/1.flac", "Song", "", "X"), 0); err != nil { t.Fatal(err) } var trackID, fileID int64 @@ -274,13 +292,13 @@ func TestLibraryRepoPlayableFile(t *testing.T) { } f, err := repo.PlayableFile(ctx, trackID) - if err != nil || f.Root != "/music" || f.RelPath != "a/1.flac" { + if err != nil || f.Root != "/lib/music" || f.RelPath != "a/1.flac" { t.Fatalf("PlayableFile = %+v err=%v", f, err) } // A second encoding of the same track. - if _, err := db.Exec(`INSERT INTO audio_files (track_id, root, rel_path, format, size_bytes, mtime_unix) - VALUES (?, '/music', 'a/1.mp3', 'mp3', 1, 1)`, trackID); err != nil { + if _, err := db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix) + VALUES (?, ?, 'a/1.mp3', 'mp3', 1, 1)`, trackID, lib); err != nil { t.Fatal(err) } // The first file goes missing: the other one is served. @@ -320,9 +338,14 @@ func TestScannerEndToEndOnBothBackends(t *testing.T) { t.Fatal(err) } } - s := library.NewScanner(NewLibraryRepo(db), stubReader{}, library.NewHub(), 4) + repo := NewLibraryRepo(db) + lib, err := repo.CreateLibrary(context.Background(), "e2e", root) + if err != nil { + t.Fatal(err) + } + s := library.NewScanner(repo, stubReader{}, library.NewHub(), 4) scan := func() library.Summary { - sum, err := s.Scan(context.Background(), []string{root}) + sum, err := s.Scan(context.Background(), []library.Library{lib}) if err != nil { t.Fatal(err) } @@ -353,3 +376,123 @@ func TestScannerEndToEndOnBothBackends(t *testing.T) { } }) } + +func TestLibraryRepoCRUD(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + repo := NewLibraryRepo(db) + + if libs, err := repo.ListLibraries(ctx); err != nil || len(libs) != 0 { + t.Fatalf("empty list: %v err=%v", libs, err) + } + a, err := repo.CreateLibrary(ctx, "Jazz", "/music/jazz") + if err != nil || a.ID == 0 || a.Name != "Jazz" || a.Path != "/music/jazz" || a.CreatedAt.IsZero() { + t.Fatalf("create: %+v err=%v", a, err) + } + b, _ := repo.CreateLibrary(ctx, "Rock", "/music/rock") + + libs, err := repo.ListLibraries(ctx) + if err != nil || len(libs) != 2 || libs[0].ID != a.ID || libs[1].ID != b.ID { + t.Fatalf("list: %+v err=%v", libs, err) + } + got, err := repo.GetLibrary(ctx, a.ID) + if err != nil || got.Name != "Jazz" { + t.Fatalf("get: %+v err=%v", got, err) + } + + upd, err := repo.UpdateLibrary(ctx, a.ID, "Jazz Collection", "/mnt/jazz") + if err != nil || upd.Name != "Jazz Collection" || upd.Path != "/mnt/jazz" || upd.ID != a.ID { + t.Fatalf("update: %+v err=%v", upd, err) + } + }) +} + +func TestLibraryRepoRejectsDuplicatesAndUnknownIDs(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + repo := NewLibraryRepo(db) + a, _ := repo.CreateLibrary(ctx, "Jazz", "/music/jazz") + b, _ := repo.CreateLibrary(ctx, "Rock", "/music/rock") + + if _, err := repo.CreateLibrary(ctx, "jazz", "/other"); !errors.Is(err, library.ErrLibraryExists) { + t.Errorf("same name in another case: %v", err) + } + if _, err := repo.CreateLibrary(ctx, "Other", "/music/jazz"); !errors.Is(err, library.ErrLibraryExists) { + t.Errorf("same path: %v", err) + } + if _, err := repo.UpdateLibrary(ctx, b.ID, "JAZZ", "/music/rock"); !errors.Is(err, library.ErrLibraryExists) { + t.Errorf("update onto another name: %v", err) + } + if _, err := repo.UpdateLibrary(ctx, b.ID, "Rock", "/music/jazz"); !errors.Is(err, library.ErrLibraryExists) { + t.Errorf("update onto another path: %v", err) + } + if _, err := repo.UpdateLibrary(ctx, a.ID, "Jazz", "/music/jazz"); err != nil { + t.Errorf("update to its own values: %v", err) + } + + if _, err := repo.GetLibrary(ctx, 999); !errors.Is(err, library.ErrLibraryNotFound) { + t.Errorf("get: %v", err) + } + if _, err := repo.UpdateLibrary(ctx, 999, "x", "/x"); !errors.Is(err, library.ErrLibraryNotFound) { + t.Errorf("update: %v", err) + } + if err := repo.DeleteLibrary(ctx, 999); !errors.Is(err, library.ErrLibraryNotFound) { + t.Errorf("delete: %v", err) + } + }) +} + +// Moving a library's folder is a one-row change: its files keep their identity. +func TestLibraryRepoPathChangeKeepsFiles(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + repo := NewLibraryRepo(db) + lib := mkLib(t, repo, "m") + if _, err := repo.SaveFile(ctx, lib, parsed("a.flac", "A", "", "X"), 0); err != nil { + t.Fatal(err) + } + before, _ := repo.ExistingFiles(ctx, lib) + + if _, err := repo.UpdateLibrary(ctx, lib, "m", "/moved/here"); err != nil { + t.Fatal(err) + } + after, _ := repo.ExistingFiles(ctx, lib) + if len(after) != 1 || after[0].ID != before[0].ID { + t.Fatalf("files after the move: %+v, want the same one", after) + } + var trackID int64 + _ = db.QueryRow("SELECT track_id FROM audio_files").Scan(&trackID) + f, err := repo.PlayableFile(ctx, trackID) + if err != nil || f.Root != "/moved/here" { + t.Errorf("playable file after the move: %+v err=%v", f, err) + } + }) +} + +// Deleting a library drops its index entries; tracks (and with them the +// listening history) stay. +func TestLibraryRepoDeleteRemovesFilesButKeepsTracks(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + repo := NewLibraryRepo(db) + keep, drop := mkLib(t, repo, "keep"), mkLib(t, repo, "drop") + for _, lib := range []int64{keep, drop} { + if _, err := repo.SaveFile(ctx, lib, parsed("x.flac", "T", "", "X"), 0); err != nil { + t.Fatal(err) + } + } + + if err := repo.DeleteLibrary(ctx, drop); err != nil { + t.Fatal(err) + } + if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE library_id = ?", drop); n != 0 { + t.Errorf("files of the deleted library = %d, want 0", n) + } + if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE library_id = ?", keep); n != 1 { + t.Errorf("files of the other library = %d, want 1", n) + } + if n := count(t, db, "SELECT COUNT(*) FROM tracks"); n != 2 { + t.Errorf("tracks = %d, want 2: history must survive", n) + } + }) +} diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go index d21f2aa..ca3a207 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/storage_test.go @@ -147,7 +147,7 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) { if err := Migrate(context.Background(), db, fsys); err != nil { t.Fatalf("second Migrate: %v", err) } - for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events"} { + for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries"} { if !tableExists(t, db, driver, table) { t.Errorf("table %s missing", table) } @@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) { if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil { t.Fatal(err) } - if applied != 4 { - t.Errorf("applied migrations = %d, want 4", applied) + if applied != 5 { + t.Errorf("applied migrations = %d, want 5", applied) } }) } @@ -188,28 +188,40 @@ func TestSchemaAllowsUnknownMBIDsButRejectsDuplicates(t *testing.T) { }) } -func TestAudioFilePathIsUniquePerRoot(t *testing.T) { +func TestAudioFilePathIsUniquePerLibrary(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { if _, err := db.Exec("INSERT INTO tracks (title) VALUES ('t')"); err != nil { t.Fatal(err) } - insert := func(root, rel string) error { + var a, b int64 + for name, dst := range map[string]*int64{"a": &a, "b": &b} { + err := db.QueryRow("INSERT INTO libraries (name, path, created_at) VALUES (?, ?, 1) RETURNING id", + name, "/music/"+name).Scan(dst) + if err != nil { + t.Fatal(err) + } + } + insert := func(lib int64, rel string) error { _, err := db.Exec( - `INSERT INTO audio_files (track_id, root, rel_path, format, size_bytes, mtime_unix) - VALUES (1, ?, ?, 'mp3', 1, 1)`, root, rel) + `INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix) + VALUES (1, ?, ?, 'mp3', 1, 1)`, lib, rel) return err } - if err := insert("/music/a", "x/y.mp3"); err != nil { + if err := insert(a, "x/y.mp3"); err != nil { t.Fatal(err) } - // Same relative path under another root is a different file. - if err := insert("/music/b", "x/y.mp3"); err != nil { - t.Fatalf("same rel_path in another root should be allowed: %v", err) + // Same relative path in another library is a different file. + if err := insert(b, "x/y.mp3"); err != nil { + t.Fatalf("same rel_path in another library should be allowed: %v", err) } - // Same root and same relative path is a duplicate. - if err := insert("/music/a", "x/y.mp3"); err == nil { - t.Fatal("expected unique violation on (root, rel_path)") + // Same library and same relative path is a duplicate. + if err := insert(a, "x/y.mp3"); err == nil { + t.Fatal("expected unique violation on (library_id, rel_path)") + } + // A file must belong to a real library. + if err := insert(999, "z.mp3"); err == nil { + t.Fatal("expected foreign key violation for an unknown library") } }) } diff --git a/internal/storage/upgrade_test.go b/internal/storage/upgrade_test.go new file mode 100644 index 0000000..ef98e92 --- /dev/null +++ b/internal/storage/upgrade_test.go @@ -0,0 +1,118 @@ +package storage + +import ( + "context" + "io/fs" + "strconv" + "testing" + "testing/fstest" + + "codeberg.org/squidd/sonda/migrations" +) + +// upTo returns the migrations of driver numbered up to n, to build a database +// as an older version of Sonda would have left it. +func upTo(t *testing.T, driver string, n int) fs.FS { + t.Helper() + full, err := migrations.For(driver) + if err != nil { + t.Fatal(err) + } + entries, err := fs.ReadDir(full, ".") + if err != nil { + t.Fatal(err) + } + out := fstest.MapFS{} + for _, e := range entries { + num, err := strconv.Atoi(e.Name()[:4]) + if err != nil { + t.Fatal(err) + } + if num <= n { + data, _ := fs.ReadFile(full, e.Name()) + out[e.Name()] = &fstest.MapFile{Data: data} + } + } + return out +} + +// Upgrading a database written before libraries existed: each distinct root +// becomes a library and every file keeps its id, track and missing flag. +func TestMigration0005UpgradesExistingData(t *testing.T) { + for _, driver := range availableDrivers() { + t.Run(driver, func(t *testing.T) { + ctx := context.Background() + db := openBackend(t, driver) + if err := Migrate(ctx, db, upTo(t, driver, 4)); err != nil { + t.Fatal(err) + } + + if _, err := db.Exec("INSERT INTO tracks (title) VALUES ('a'), ('b'), ('c')"); err != nil { + t.Fatal(err) + } + rows := `INSERT INTO audio_files (track_id, root, rel_path, format, size_bytes, mtime_unix, missing_at) VALUES + (1, '/music/a', 'x.mp3', 'mp3', 10, 100, NULL), + (2, '/music/b', 'x.mp3', 'mp3', 20, 200, NULL), + (3, '/music/a', 'y.mp3', 'mp3', 30, 300, 5)` + if _, err := db.Exec(rows); err != nil { + t.Fatal(err) + } + + full, _ := migrations.For(driver) + if err := Migrate(ctx, db, full); err != nil { + t.Fatalf("upgrade: %v", err) + } + + if n := count(t, db, "SELECT COUNT(*) FROM libraries"); n != 2 { + t.Errorf("libraries = %d, want 2 (one per distinct root)", n) + } + if n := count(t, db, "SELECT COUNT(*) FROM libraries WHERE name = path"); n != 2 { + t.Error("a migrated library should be named after its path") + } + if n := count(t, db, "SELECT COUNT(*) FROM audio_files"); n != 3 { + t.Errorf("audio files = %d, want 3: none may be lost", n) + } + if n := count(t, db, `SELECT COUNT(*) FROM audio_files f JOIN libraries l ON l.id = f.library_id WHERE l.path = '/music/a'`); n != 2 { + t.Errorf("files linked to /music/a = %d, want 2", n) + } + if n := count(t, db, `SELECT COUNT(*) FROM audio_files f JOIN libraries l ON l.id = f.library_id WHERE l.path = '/music/b' AND f.track_id = 2 AND f.size_bytes = 20`); n != 1 { + t.Error("the file of /music/b was not linked correctly") + } + if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE id = 3 AND track_id = 3 AND missing_at = 5"); n != 1 { + t.Error("id, track or missing flag changed during the upgrade") + } + + if _, err := db.Exec("SELECT root FROM audio_files"); err == nil { + t.Error("the root column still exists") + } + // The new uniqueness rule is in place. + var lib int64 + if err := db.QueryRow("SELECT library_id FROM audio_files WHERE id = 1").Scan(&lib); err != nil { + t.Fatal(err) + } + dup := `INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix) VALUES (1, ?, 'x.mp3', 'mp3', 1, 1)` + if _, err := db.Exec(dup, lib); err == nil { + t.Error("duplicate (library_id, rel_path) was accepted after the upgrade") + } + }) + } +} + +func TestMigration0005OnAnEmptyDatabase(t *testing.T) { + for _, driver := range availableDrivers() { + t.Run(driver, func(t *testing.T) { + ctx := context.Background() + db := openBackend(t, driver) + if err := Migrate(ctx, db, upTo(t, driver, 4)); err != nil { + t.Fatal(err) + } + full, _ := migrations.For(driver) + if err := Migrate(ctx, db, full); err != nil { + t.Fatal(err) + } + if n := count(t, db, "SELECT COUNT(*) FROM libraries"); n != 0 { + t.Errorf("libraries = %d, want 0", n) + } + }) + } +} diff --git a/internal/streaming/fifo_unix_test.go b/internal/streaming/fifo_unix_test.go index 8edfb60..9a0c55c 100644 --- a/internal/streaming/fifo_unix_test.go +++ b/internal/streaming/fifo_unix_test.go @@ -24,7 +24,7 @@ func TestOpenRefusesFIFOsWithoutHanging(t *testing.T) { } for _, rel := range []string{"pipe.mp3", "pipelink.mp3"} { - s := service(root, map[int64]File{1: {Root: root, RelPath: rel}}) + s := service(map[int64]File{1: {Root: root, RelPath: rel}}) type result struct { st *Stream diff --git a/internal/streaming/streaming.go b/internal/streaming/streaming.go index 26b1567..30da4c4 100644 --- a/internal/streaming/streaming.go +++ b/internal/streaming/streaming.go @@ -1,9 +1,10 @@ // Package streaming opens audio files for playback. // // The only job here is to turn a track id into an open, verified file. The -// stored path is treated as untrusted: it must belong to a configured library -// root, and the file is opened through os.Root so that nothing, not even a -// symlink swapped in at the last moment, can lead outside that root. +// relative path is treated as untrusted and the file is opened through os.Root, +// so that nothing, not even a symlink swapped in at the last moment, can lead +// outside the library directory. The library directory itself comes from the +// libraries table, which only admins can change. package streaming import ( @@ -24,10 +25,10 @@ var ( ErrUnsafePath = errors.New("audio path is outside the library") ) -// File is where a track's audio lives: a configured library root plus a path -// relative to it, exactly as the scanner stored them. +// File is where a track's audio lives: the directory of its library plus a +// path relative to it, exactly as the scanner stored it. type File struct { - Root string + Root string // absolute library directory RelPath string // slash-separated } @@ -52,20 +53,15 @@ func (s *Stream) Close() error { return s.File.Close() } // Service opens tracks for playback. type Service struct { source Source - roots []string // cleaned; config validation requires them to be absolute } -// NewService builds a Service. Only files under the given roots can be opened. -func NewService(source Source, roots []string) *Service { - cleaned := make([]string, len(roots)) - for i, r := range roots { - cleaned[i] = filepath.Clean(r) - } - return &Service{source: source, roots: cleaned} +// NewService builds a Service that plays files found by source. +func NewService(source Source) *Service { + return &Service{source: source} } // Open returns the audio file of a track, after verifying that it is a regular -// file inside one of the configured roots. +// file inside its library directory. // // The file is opened through an os.Root, which refuses to leave the root at // open time: a symlink swapped in after any earlier check cannot escape, and @@ -121,12 +117,11 @@ func (s *Service) Open(ctx context.Context, trackID int64) (*Stream, error) { }, nil } -// validate checks the stored root and relative path and returns the cleaned -// relative path to open under the root. +// validate checks the library directory and the relative path and returns the +// cleaned relative path to open under the directory. func (s *Service) validate(f File) (string, error) { - root := filepath.Clean(f.Root) - if !filepath.IsAbs(root) || !s.allowed(root) { - return "", fmt.Errorf("%w: root is not a configured library root", ErrUnsafePath) + if !filepath.IsAbs(filepath.Clean(f.Root)) { + return "", fmt.Errorf("%w: library directory is not absolute", ErrUnsafePath) } rel := filepath.FromSlash(f.RelPath) @@ -140,15 +135,6 @@ func (s *Service) validate(f File) (string, error) { return rel, nil } -func (s *Service) allowed(root string) bool { - for _, r := range s.roots { - if r == root { - return true - } - } - return false -} - func contentType(path string) string { switch strings.ToLower(filepath.Ext(path)) { case ".mp3": diff --git a/internal/streaming/streaming_test.go b/internal/streaming/streaming_test.go index 18f22be..9172e39 100644 --- a/internal/streaming/streaming_test.go +++ b/internal/streaming/streaming_test.go @@ -35,15 +35,14 @@ func write(t *testing.T, path, content string) { } } -// service builds a Service whose only allowed root is root. -func service(root string, files map[int64]File) *Service { - return NewService(fakeSource{files: files}, []string{root}) +func service(files map[int64]File) *Service { + return NewService(fakeSource{files: files}) } func TestOpenServesTheFile(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "Artist/song.mp3"), "audio-bytes") - s := service(root, map[int64]File{1: {Root: root, RelPath: "Artist/song.mp3"}}) + s := service(map[int64]File{1: {Root: root, RelPath: "Artist/song.mp3"}}) st, err := s.Open(context.Background(), 1) if err != nil { @@ -65,7 +64,7 @@ func TestContentTypeFollowsTheExtension(t *testing.T) { write(t, filepath.Join(root, "a.FLAC"), "x") write(t, filepath.Join(root, "b.mp3"), "x") write(t, filepath.Join(root, "c.xyz"), "x") - s := service(root, map[int64]File{ + s := service(map[int64]File{ 1: {Root: root, RelPath: "a.FLAC"}, 2: {Root: root, RelPath: "b.mp3"}, 3: {Root: root, RelPath: "c.xyz"}, @@ -84,7 +83,7 @@ func TestContentTypeFollowsTheExtension(t *testing.T) { } func TestOpenUnknownTrack(t *testing.T) { - s := service(t.TempDir(), nil) + s := service(nil) if _, err := s.Open(context.Background(), 42); !errors.Is(err, ErrNotFound) { t.Fatalf("err = %v, want ErrNotFound", err) } @@ -92,7 +91,7 @@ func TestOpenUnknownTrack(t *testing.T) { func TestOpenPropagatesSourceErrors(t *testing.T) { boom := errors.New("db down") - s := NewService(fakeSource{err: boom}, []string{t.TempDir()}) + s := NewService(fakeSource{err: boom}) if _, err := s.Open(context.Background(), 1); !errors.Is(err, boom) { t.Fatalf("err = %v, want the source error", err) } @@ -100,7 +99,7 @@ func TestOpenPropagatesSourceErrors(t *testing.T) { func TestOpenMissingFileOnDisk(t *testing.T) { root := t.TempDir() - s := service(root, map[int64]File{1: {Root: root, RelPath: "gone.mp3"}}) + s := service(map[int64]File{1: {Root: root, RelPath: "gone.mp3"}}) if _, err := s.Open(context.Background(), 1); !errors.Is(err, ErrFileMissing) { t.Fatalf("err = %v, want ErrFileMissing", err) } @@ -121,12 +120,11 @@ func TestOpenRejectsUnsafePaths(t *testing.T) { {"absolute path", File{Root: root, RelPath: filepath.Join(outside, "secret.mp3")}}, {"empty path", File{Root: root, RelPath: ""}}, {"the root itself", File{Root: root, RelPath: "."}}, - {"root not configured", File{Root: outside, RelPath: "secret.mp3"}}, {"relative root", File{Root: "relative/dir", RelPath: "ok.mp3"}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - s := service(root, map[int64]File{1: tt.file}) + s := service(map[int64]File{1: tt.file}) st, err := s.Open(context.Background(), 1) if err == nil { st.Close() @@ -155,7 +153,7 @@ func TestOpenRejectsSymlinkEscapingTheRoot(t *testing.T) { } for _, rel := range []string{"link.mp3", "dirlink/secret.mp3"} { - s := service(root, map[int64]File{1: {Root: root, RelPath: rel}}) + s := service(map[int64]File{1: {Root: root, RelPath: rel}}) st, err := s.Open(context.Background(), 1) if err == nil { data, _ := io.ReadAll(st.File) @@ -174,7 +172,7 @@ func TestOpenAllowsRelativeSymlinkThatStaysInsideTheRoot(t *testing.T) { if err := os.Symlink("real", filepath.Join(root, "alias")); err != nil { t.Skip("symlinks not supported:", err) } - s := service(root, map[int64]File{1: {Root: root, RelPath: "alias/song.mp3"}}) + s := service(map[int64]File{1: {Root: root, RelPath: "alias/song.mp3"}}) st, err := s.Open(context.Background(), 1) if err != nil { t.Fatalf("relative symlink inside the root rejected: %v", err) @@ -191,7 +189,7 @@ func TestOpenRefusesAbsoluteSymlinksEvenInsideTheRoot(t *testing.T) { if err := os.Symlink(filepath.Join(root, "real"), filepath.Join(root, "alias")); err != nil { t.Skip("symlinks not supported:", err) } - s := service(root, map[int64]File{1: {Root: root, RelPath: "alias/song.mp3"}}) + s := service(map[int64]File{1: {Root: root, RelPath: "alias/song.mp3"}}) st, err := s.Open(context.Background(), 1) if err == nil { st.Close() @@ -209,7 +207,7 @@ func TestOpenAcceptsASymlinkedRoot(t *testing.T) { if err := os.Symlink(real, link); err != nil { t.Skip("symlinks not supported:", err) } - s := service(link, map[int64]File{1: {Root: link, RelPath: "song.mp3"}}) + s := service(map[int64]File{1: {Root: link, RelPath: "song.mp3"}}) st, err := s.Open(context.Background(), 1) if err != nil { t.Fatalf("a symlinked root is a normal setup: %v", err) @@ -222,7 +220,7 @@ func TestOpenRejectsDirectories(t *testing.T) { if err := os.MkdirAll(filepath.Join(root, "album.mp3"), 0o755); err != nil { t.Fatal(err) } - s := service(root, map[int64]File{1: {Root: root, RelPath: "album.mp3"}}) + s := service(map[int64]File{1: {Root: root, RelPath: "album.mp3"}}) st, err := s.Open(context.Background(), 1) if err == nil { st.Close() @@ -236,8 +234,8 @@ func TestOpenRejectsDirectories(t *testing.T) { func TestRootsAreComparedAfterCleaning(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "song.mp3"), "ok") - // The configured root has no trailing slash, the stored one does. - s := service(root, map[int64]File{1: {Root: root + "/", RelPath: "song.mp3"}}) + // A stored directory with a trailing slash is still the same directory. + s := service(map[int64]File{1: {Root: root + "/", RelPath: "song.mp3"}}) st, err := s.Open(context.Background(), 1) if err != nil { t.Fatalf("trailing slash on the stored root: %v", err) diff --git a/migrations/postgres/0005_libraries.sql b/migrations/postgres/0005_libraries.sql new file mode 100644 index 0000000..af29ad4 --- /dev/null +++ b/migrations/postgres/0005_libraries.sql @@ -0,0 +1,27 @@ +-- Named media libraries. audio_files points at a library by id instead of +-- repeating the root path in every row. + +CREATE TABLE libraries ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name TEXT NOT NULL, + path TEXT NOT NULL UNIQUE, -- absolute directory + created_at BIGINT NOT NULL -- unix seconds +); + +-- Case-insensitive uniqueness (SQLite uses COLLATE NOCASE for the same effect). +CREATE UNIQUE INDEX libraries_name_lower ON libraries (lower(name)); + +-- Existing data: every distinct root becomes a library. The path doubles as +-- the name, which is always unique; admins can rename it afterwards. +INSERT INTO libraries (name, path, created_at) + SELECT DISTINCT root, root, extract(epoch FROM now())::bigint FROM audio_files; + +ALTER TABLE audio_files ADD COLUMN library_id BIGINT REFERENCES libraries(id) ON DELETE CASCADE; +UPDATE audio_files f SET library_id = l.id FROM libraries l WHERE l.path = f.root; +ALTER TABLE audio_files ALTER COLUMN library_id SET NOT NULL; + +-- Dropping the column also drops UNIQUE (root, rel_path). +ALTER TABLE audio_files DROP COLUMN root; +ALTER TABLE audio_files ADD CONSTRAINT audio_files_library_rel_path_key UNIQUE (library_id, rel_path); + +CREATE INDEX idx_audio_files_library ON audio_files(library_id); diff --git a/migrations/sqlite/0005_libraries.sql b/migrations/sqlite/0005_libraries.sql new file mode 100644 index 0000000..aa6cad4 --- /dev/null +++ b/migrations/sqlite/0005_libraries.sql @@ -0,0 +1,44 @@ +-- Named media libraries. audio_files points at a library by id instead of +-- repeating the root path in every row. + +CREATE TABLE libraries ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL UNIQUE COLLATE NOCASE, + path TEXT NOT NULL UNIQUE, -- absolute directory + created_at INTEGER NOT NULL -- unix seconds +); + +-- Existing data: every distinct root becomes a library. The path doubles as +-- the name, which is always unique; admins can rename it afterwards. +INSERT INTO libraries (name, path, created_at) + SELECT DISTINCT root, root, unixepoch() FROM audio_files; + +-- SQLite cannot drop a column that is part of a UNIQUE constraint, so the +-- table is rebuilt. +CREATE TABLE audio_files_new ( + id INTEGER PRIMARY KEY, + track_id INTEGER NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, + library_id INTEGER NOT NULL REFERENCES libraries(id) ON DELETE CASCADE, + rel_path TEXT NOT NULL, -- relative to the library path + format TEXT NOT NULL, + size_bytes INTEGER NOT NULL, + mtime_unix INTEGER NOT NULL, + sample_rate INTEGER, + bitrate INTEGER, + content_hash TEXT, + missing_at INTEGER, -- unix time the file was last seen missing + UNIQUE (library_id, rel_path) +); + +INSERT INTO audio_files_new + (id, track_id, library_id, rel_path, format, size_bytes, mtime_unix, + sample_rate, bitrate, content_hash, missing_at) + SELECT f.id, f.track_id, l.id, f.rel_path, f.format, f.size_bytes, f.mtime_unix, + f.sample_rate, f.bitrate, f.content_hash, f.missing_at + FROM audio_files f JOIN libraries l ON l.path = f.root; + +DROP TABLE audio_files; +ALTER TABLE audio_files_new RENAME TO audio_files; + +CREATE INDEX idx_audio_files_track ON audio_files(track_id); +CREATE INDEX idx_audio_files_library ON audio_files(library_id); diff --git a/sonda.example.toml b/sonda.example.toml index 6ddd803..cdae860 100644 --- a/sonda.example.toml +++ b/sonda.example.toml @@ -6,8 +6,8 @@ [server] listen = "127.0.0.1:8080" # localhost by default +# Libraries (name + directory) are managed through the API: POST /api/v1/libraries. [library] -roots = ["/path/to/music"] # read-only, at least one required scan_workers = 4 # parallel tag readers during a scan [database]