sonda/internal/duplicates/service.go
tgiachi ce109a14a3 fix(duplicates): move the playlist items of a removed copy to the one that stays
playlist_items.track_id is ON DELETE CASCADE (added by the playlists work on main), so deleting a
duplicate would have taken the song out of every user's playlists, as it would have done with the
listens. RemoveCopy now repoints the items first, in the same transaction.

Also merges main (playlists) into the branch.
2026-11-12 17:01:36 +01:00

237 lines
7.2 KiB
Go

package duplicates
import (
"context"
"fmt"
"os"
"slices"
"time"
"go.uber.org/zap"
)
// Store is what the service reads and writes in the index. internal/storage implements it.
type Store interface {
Candidates(ctx context.Context) ([]Candidate, error)
FileOf(ctx context.Context, trackID int64) (FileRecord, error)
FileByID(ctx context.Context, fileID int64) (FileRecord, error)
SaveHash(ctx context.Context, fileID, sizeBytes, mtimeUnix int64, hash string) (bool, error)
RemoveCopy(ctx context.Context, keepTrackID, removeTrackID int64) error
}
// Options say what the service may do.
type Options struct {
// AllowDelete is library.allow_delete. Without it nothing is ever removed.
AllowDelete bool
// ScanRunning says whether a scan is running; a removal waits for it to end. Nil means never.
ScanRunning func() bool
// Logger records each removal, by track and without the path: audio paths are private.
Logger *zap.Logger
}
// Service finds duplicate songs and removes the extra copies.
type Service struct {
store Store
allow bool
scanning func() bool
log *zap.Logger
now func() time.Time
}
// NewService builds a Service.
func NewService(store Store, o Options) *Service {
log := o.Logger
if log == nil {
log = zap.NewNop()
}
return &Service{store: store, allow: o.AllowDelete, scanning: o.ScanRunning, log: log, now: time.Now}
}
// Page is some of the groups, with how many there are in all.
type Page struct {
Groups []Group
Total int
// DeletionAllowed says whether Clean can work: with it off, the groups can still be seen.
DeletionAllowed bool
}
// Groups returns the groups of copies in the library, worked out from the index and never from the disk.
func (s *Service) Groups(ctx context.Context, limit, offset int) (Page, error) {
all, err := s.groups(ctx)
if err != nil {
return Page{}, err
}
page := Page{Total: len(all), DeletionAllowed: s.allow}
if offset < len(all) {
page.Groups = all[offset:min(offset+limit, len(all))]
}
return page, nil
}
func (s *Service) groups(ctx context.Context) ([]Group, error) {
candidates, err := s.store.Candidates(ctx)
if err != nil {
return nil, err
}
return Build(candidates, DurationTolerance), nil
}
// maxRemovals is the most copies of one song that one request may remove.
const maxRemovals = 100
// Result says what a removal did.
type Result struct {
Removed int
FreedBytes int64
}
// Clean removes the copies `remove` of a song from the disk and the index, keeping `keep`. Nothing
// is removed unless every check passes, and the checks are made here, again, whatever the client
// saw: deleting is allowed, no scan is running, the tracks are copies of one song by the rule of
// Build, the copy to keep is still the file that was scanned (so it is never the last copy that is
// removed), and so is every copy to remove (a file that changed, a link, or a path that leaves the
// library is refused). The files are checked all together before the first is removed. Each removed
// file takes its track out of the index, and its listens and playlist items go to the track that stays.
//
// On an error part way (the folder turned out to be read-only on the second file), the Result says
// what was already removed.
func (s *Service) Clean(ctx context.Context, keep int64, remove []int64) (Result, error) {
var res Result
if !s.allow {
return res, ErrDisabled
}
if s.scanning != nil && s.scanning() {
return res, ErrScanRunning
}
if err := checkRequest(keep, remove); err != nil {
return res, err
}
keepFile, err := s.store.FileOf(ctx, keep)
if err != nil {
return res, err
}
files := make(map[int64]FileRecord, len(remove))
for _, id := range remove {
rec, err := s.store.FileOf(ctx, id)
if err != nil {
return res, err
}
files[id] = rec
}
if keepFile.Missing {
return res, fmt.Errorf("%w: the copy to keep is flagged as missing", ErrChanged)
}
for _, rec := range files {
if rec.Missing {
return res, fmt.Errorf("%w: a copy to remove is flagged as missing", ErrChanged)
}
}
if err := s.checkSameSong(ctx, keep, remove); err != nil {
return res, err
}
// Look at every file before touching any.
roots := map[string]*os.Root{}
defer func() {
for _, r := range roots {
_ = r.Close()
}
}()
rootOf := func(path string) (*os.Root, error) {
if r, ok := roots[path]; ok {
return r, nil
}
r, err := openRoot(path)
if err != nil {
return nil, err
}
roots[path] = r
return r, nil
}
inspect := func(rec FileRecord) (*os.Root, string, error) {
root, err := rootOf(rec.Root)
if err != nil {
return nil, "", err
}
rel, err := relPath(rec)
if err != nil {
return nil, "", err
}
return root, rel, sameAsScanned(root, rel, rec)
}
if _, _, err := inspect(keepFile); err != nil {
return res, err
}
type target struct {
id int64
root *os.Root
rel string
}
targets := make([]target, 0, len(remove))
for _, id := range remove {
root, rel, err := inspect(files[id])
if err != nil {
return res, err
}
targets = append(targets, target{id, root, rel})
}
for _, t := range targets {
if err := removeFile(t.root, t.rel); err != nil {
return res, err
}
// The file is gone, so the index must follow whether or not the client is still there: a request
// that was cancelled now would leave a track whose file does not exist. If the index still cannot
// follow, the next scan flags the path as missing, and nothing is left broken; the error says so.
if err := s.store.RemoveCopy(context.WithoutCancel(ctx), keep, t.id); err != nil {
s.log.Error("a duplicate file was removed but its track is still in the index; a scan will flag it as missing",
zap.Int64("track_id", t.id), zap.Int64("kept_track_id", keep), zap.Error(err))
return res, fmt.Errorf("the file was removed but the index could not be updated: %w", err)
}
res.Removed++
res.FreedBytes += files[t.id].SizeBytes
s.log.Info("a duplicate file was removed", zap.Int64("track_id", t.id), zap.Int64("kept_track_id", keep))
}
return res, nil
}
// checkRequest refuses a request that makes no sense, before anything is read: nothing to remove,
// too much, the copy to keep among those to remove, or a copy named twice.
func checkRequest(keep int64, remove []int64) error {
if len(remove) == 0 || len(remove) > maxRemovals {
return fmt.Errorf("%w: name between 1 and %d copies to remove", ErrInvalid, maxRemovals)
}
seen := map[int64]bool{}
for _, id := range remove {
if id == keep {
return fmt.Errorf("%w: the copy to keep is among those to remove", ErrInvalid)
}
if seen[id] {
return fmt.Errorf("%w: a copy is named twice", ErrInvalid)
}
seen[id] = true
}
return nil
}
// checkSameSong works out the groups again and checks that every copy to remove is in the group of
// the copy to keep.
func (s *Service) checkSameSong(ctx context.Context, keep int64, remove []int64) error {
groups, err := s.groups(ctx)
if err != nil {
return err
}
for _, g := range groups {
if !slices.ContainsFunc(g.Files, func(f Candidate) bool { return f.TrackID == keep }) {
continue
}
for _, id := range remove {
if !slices.ContainsFunc(g.Files, func(f Candidate) bool { return f.TrackID == id }) {
return ErrInvalid
}
}
return nil
}
return ErrInvalid
}