sonda/internal/library/autoscan.go
tgiachi 89f1b5275a feat: scan the libraries by themselves when their music changed
Every 15 minutes (library.auto_scan_minutes, SONDA_AUTO_SCAN_MINUTES, 0 turns it off)
Sonda takes the signature of each library, a hash of the names of everything in it folder
by folder, which costs only the names of the files and so suits a NAS that sleeps. A
library whose signature changed (a song or a folder came, went or was renamed) is
scanned the usual way; every library is also scanned at least once a day, to find a song
tagged again where it stands.

It does not scan while another scan runs, tries again at the next look when a scan failed,
leaves alone a folder that cannot be read (a NAS that is not mounted), and ignores the
housekeeping folders of a NAS.
2026-11-16 13:05:09 +01:00

199 lines
5.7 KiB
Go

package library
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"path/filepath"
"time"
)
// forceEvery is how long a library may go without a scan even when its folders look the same: a song that is tagged
// again keeps its place and its name, so only a scan of the file finds it.
const forceEvery = 24 * time.Hour
// Signature says how a folder looks: a hash of the names of everything in it, folder by folder, and of the time of each
// folder. It reads the names and nothing else (no file is opened or measured), which is why it is cheap enough to
// ask often of a NAS, and it changes when a song or a folder comes, goes or is renamed. It does not change when a
// file is changed where it stands. The housekeeping folders of a NAS (see isSystemDir) and the links inside the folder
// are left out, as the scanner leaves them out; a link as the folder itself is followed. A folder that cannot be read is
// an error.
func Signature(ctx context.Context, root string) (string, error) {
top, err := filepath.EvalSymlinks(root)
if err != nil {
return "", err
}
info, err := os.Stat(top)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", fmt.Errorf("%s is not a directory", root)
}
h := sha256.New()
var walk func(dir, rel string) error
walk = func(dir, rel string) error {
if err := ctx.Err(); err != nil {
return err
}
entries, err := os.ReadDir(dir)
if err != nil {
if rel == "" {
return err
}
fmt.Fprintf(h, "! %s\n", rel) // a folder that cannot be read is a state of its own
return nil
}
var mtime int64
if st, err := os.Stat(dir); err == nil {
mtime = st.ModTime().UnixNano()
}
fmt.Fprintf(h, "d %s %d\n", rel, mtime)
for _, e := range entries { // os.ReadDir sorts them by name
if e.IsDir() && isSystemDir(e.Name()) {
continue
}
kind := 'f'
switch {
case e.Type()&os.ModeSymlink != 0:
kind = 'l'
case e.IsDir():
kind = 'd'
}
fmt.Fprintf(h, "%c %s\n", kind, e.Name())
}
for _, e := range entries {
if e.IsDir() && e.Type()&os.ModeSymlink == 0 && !isSystemDir(e.Name()) {
if err := walk(filepath.Join(dir, e.Name()), rel+e.Name()+"/"); err != nil {
return err
}
}
}
return nil
}
if err := walk(top, ""); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// scans is what the watcher asks of the manager of the scans: *Manager.
type scans interface {
StartLibrary(ctx context.Context, id int64, opts ...ScanOption) error
Status() Status
}
// AutoScanner scans the libraries by itself when their music changed. Every `interval` it takes the Signature of each
// library and scans the ones that do not look as they did the last time (the first time, all of them), and every library
// at least once a day. A scan is the usual one: it reads only the files that are new or changed. Nothing is scanned
// while another scan runs, and a scan that failed is tried again at the next look.
type AutoScanner struct {
scans scans
libs LibrarySource
interval time.Duration
// first is how long after the start the first look comes: the server is not busy with it while it starts.
first time.Duration
now func() time.Time
poll time.Duration // how often it asks whether a scan it started is over
// OnScan, when set, is told about each library it scans and about each one it could not look at.
OnScan func(lib Library, signatureErr, scanErr error)
seen map[int64]seenLibrary
}
type seenLibrary struct {
sig string
at time.Time
}
// NewAutoScanner builds an AutoScanner that looks every interval.
func NewAutoScanner(s scans, libs LibrarySource, interval time.Duration) *AutoScanner {
return &AutoScanner{
scans: s, libs: libs, interval: interval, first: 30 * time.Second,
now: time.Now, poll: time.Second, seen: map[int64]seenLibrary{},
}
}
// Run looks at the libraries until ctx ends.
func (a *AutoScanner) Run(ctx context.Context) {
timer := time.NewTimer(a.first)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return
case <-timer.C:
a.tick(ctx)
timer.Reset(a.interval)
}
}
}
// tick looks at every library once.
func (a *AutoScanner) tick(ctx context.Context) {
libs, err := a.libs.ListLibraries(ctx)
if err != nil {
return
}
present := map[int64]bool{}
for _, l := range libs {
present[l.ID] = true
}
for id := range a.seen {
if !present[id] {
delete(a.seen, id) // a library that is gone: a new one with its number starts from nothing
}
}
for _, lib := range libs {
if ctx.Err() != nil {
return
}
sig, err := Signature(ctx, lib.Path)
if err != nil {
// Not there (a NAS that is not mounted): nothing is known about it, and the scan would only say so.
a.told(lib, err, nil)
continue
}
prev, ok := a.seen[lib.ID]
if ok && prev.sig == sig && a.now().Sub(prev.at) < forceEvery {
continue
}
switch err := a.scans.StartLibrary(ctx, lib.ID); {
case errors.Is(err, ErrScanRunning):
return // another scan has the turn: the rest wait for the next look
case err != nil:
a.told(lib, nil, err)
continue
}
scanErr := a.wait(ctx)
a.told(lib, nil, scanErr)
if scanErr == nil && ctx.Err() == nil {
// The signature is the one taken before the scan: what changed while it ran is seen at the next look.
a.seen[lib.ID] = seenLibrary{sig: sig, at: a.now()}
}
}
}
// wait holds until the scan that was started is over, and says how it ended.
func (a *AutoScanner) wait(ctx context.Context) error {
for a.scans.Status().Running {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(a.poll):
}
}
if msg := a.scans.Status().LastError; msg != "" {
return errors.New(msg)
}
return nil
}
func (a *AutoScanner) told(lib Library, sigErr, scanErr error) {
if a.OnScan != nil {
a.OnScan(lib, sigErr, scanErr)
}
}