sonda/internal/library/libraries.go
tgiachi eb23ee426a fix(library): make the libraries upgrade and validation robust
- fix(migration): roots that differ only in case (/Music and /music are two
  directories on Linux) used to make migration 0005 fail on the unique name;
  the later ones now get a numeric suffix
- feat(library): at startup, log a warning for every library whose directory
  cannot be read or that overlaps another, because migrated rows skip
  validation and may still hold roots that were removed from the old config
- fix(library): creating or updating a library is now serialised, so two
  concurrent requests cannot both pass the overlap check
- fix(library): changing a library's directory or deleting it is refused with
  409 scan_running while a scan runs; the scan works from the list it started
  with and would write under a stale path
- fix(library): library names clash ignoring case and accents the same way on
  SQLite and PostgreSQL, checked in Go
- fix(library): changing only the path no longer revalidates an untouched
  name, which can be longer than the limit after a migration
- test: roots differing in case on both databases; concurrent creates with a
  slow store, verified to fail without the lock; the busy guard; Problems
2026-11-11 13:34:44 +01:00

239 lines
7.7 KiB
Go

package library
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"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
// mu makes validate-then-write atomic, so two admins creating libraries at
// the same moment cannot both pass the overlap check. Sonda is a
// single-instance server, so an in-process lock is enough.
mu sync.Mutex
busy func() bool
}
// NewLibraries builds the service.
func NewLibraries(store LibraryStore) *Libraries { return &Libraries{store: store} }
// SetBusy registers a function that tells whether a scan is running. While it
// is, a library's directory cannot change and a library cannot be deleted: the
// scan works from the list it started with and would write under a stale path.
func (s *Libraries) SetBusy(fn func() bool) {
s.mu.Lock()
defer s.mu.Unlock()
s.busy = fn
}
func (s *Libraries) scanning() bool { return s.busy != nil && s.busy() }
// 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) {
s.mu.Lock()
defer s.mu.Unlock()
name, path, err := s.validate(ctx, name, path, 0, true)
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) {
s.mu.Lock()
defer s.mu.Unlock()
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
}
// An untouched name is not validated again: a migrated library is named
// after its path, which can be longer than the limit for new names.
newName, newPath, err = s.validate(ctx, newName, newPath, id, name != nil)
if err != nil {
return Library{}, err
}
if newPath != cur.Path && s.scanning() {
return Library{}, ErrScanRunning
}
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 {
s.mu.Lock()
defer s.mu.Unlock()
if s.scanning() {
return ErrScanRunning
}
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. The
// name is only checked when checkName is true.
func (s *Libraries) validate(ctx context.Context, name, path string, skipID int64, checkName bool) (string, string, error) {
name = strings.TrimSpace(name)
if checkName {
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
}
// Done here, not only by the database: SQLite's NOCASE folds ASCII
// letters only, PostgreSQL's lower() folds accents too.
if checkName && strings.ToLower(o.Name) == strings.ToLower(name) {
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))
}
// Problems lists libraries that look wrong: a directory that cannot be read, or
// libraries that overlap and would index the same files twice. Migrated data
// skips validation, so this is meant to run at startup and be logged.
func (s *Libraries) Problems(ctx context.Context) ([]string, error) {
libs, err := s.store.ListLibraries(ctx)
if err != nil {
return nil, fmt.Errorf("list libraries: %w", err)
}
var problems []string
reals := make([]string, len(libs))
for i, l := range libs {
real, err := filepath.EvalSymlinks(l.Path)
if err != nil {
problems = append(problems, fmt.Sprintf("library %q: directory %s cannot be read: %v", l.Name, l.Path, err))
continue
}
reals[i] = real
}
for i := range libs {
for j := i + 1; j < len(libs); j++ {
a, b := reals[i], reals[j]
switch {
case a == "" || b == "":
// already reported as unreadable
case a == b:
problems = append(problems, fmt.Sprintf("libraries %q and %q point at the same directory: files are indexed twice", libs[i].Name, libs[j].Name))
case within(a, b):
problems = append(problems, fmt.Sprintf("library %q is inside library %q: files are indexed twice", libs[i].Name, libs[j].Name))
case within(b, a):
problems = append(problems, fmt.Sprintf("library %q is inside library %q: files are indexed twice", libs[j].Name, libs[i].Name))
}
}
}
return problems, nil
}