Groups come from the index. The check job reads the files of the groups and keeps their SHA-256, skipping a file that changed since the scan. Clean refuses unless deleting is on and no scan runs, works out again that the tracks are copies of one song, checks every file is still what the scan saw (so the last copy is never removed, and a link or a path that leaves the library is refused) before removing any, removes each file through an os.Root, then takes its track out of the index and moves its listens. Tested with real files, including the refusals; three of the safety rules were broken on purpose to see the tests fail.
80 lines
2.7 KiB
Go
80 lines
2.7 KiB
Go
package duplicates
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
// openRoot opens the folder of a library so that nothing done through it can leave it: a ../ or a
|
|
// link that points outside is refused by the system at the moment of the call, with no gap between
|
|
// checking a path and using it. It returns ErrChanged when the folder is not there (a disk that is
|
|
// not mounted).
|
|
func openRoot(root string) (*os.Root, error) {
|
|
clean := filepath.Clean(root)
|
|
if !filepath.IsAbs(clean) {
|
|
return nil, fmt.Errorf("%w: the library folder is not an absolute path", ErrChanged)
|
|
}
|
|
r, err := os.OpenRoot(clean)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return nil, fmt.Errorf("%w: the library folder is not there", ErrChanged)
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open the library folder: %w", err)
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// relPath is the path of a file inside its library, in the system's own form. An empty or absolute
|
|
// path is not one.
|
|
func relPath(rec FileRecord) (string, error) {
|
|
rel := filepath.FromSlash(rec.RelPath)
|
|
if rel == "" || filepath.IsAbs(rel) {
|
|
return "", fmt.Errorf("%w: the path of the file is not inside the library", ErrChanged)
|
|
}
|
|
return rel, nil
|
|
}
|
|
|
|
// isEscape says whether an error from an os.Root is the refusal to leave the root (it has no exported value).
|
|
func isEscape(err error) bool {
|
|
return err != nil && strings.Contains(err.Error(), "path escapes from parent")
|
|
}
|
|
|
|
// sameAsScanned checks, through the root, that the path is a regular file (not a link, not a
|
|
// folder) with the size and date the index has for it. Anything else is ErrChanged.
|
|
func sameAsScanned(root *os.Root, rel string, rec FileRecord) error {
|
|
info, err := root.Lstat(rel)
|
|
switch {
|
|
case errors.Is(err, fs.ErrNotExist), isEscape(err):
|
|
return fmt.Errorf("%w: the file is not there", ErrChanged)
|
|
case err != nil:
|
|
return fmt.Errorf("look at the file: %w", err)
|
|
}
|
|
if !info.Mode().IsRegular() {
|
|
return fmt.Errorf("%w: it is not a regular file any more", ErrChanged)
|
|
}
|
|
if info.Size() != rec.SizeBytes || info.ModTime().Unix() != rec.MTimeUnix {
|
|
return fmt.Errorf("%w: its size or date is not what the scan saw", ErrChanged)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// removeFile deletes one file inside the root, and turns the ways it can fail into the answers the API gives.
|
|
func removeFile(root *os.Root, rel string) error {
|
|
err := root.Remove(rel)
|
|
switch {
|
|
case err == nil:
|
|
return nil
|
|
case errors.Is(err, fs.ErrNotExist):
|
|
return fmt.Errorf("%w: the file is gone", ErrChanged)
|
|
case isEscape(err):
|
|
return fmt.Errorf("%w: the path leaves the library", ErrChanged)
|
|
case errors.Is(err, fs.ErrPermission), errors.Is(err, syscall.EROFS):
|
|
return fmt.Errorf("%w: %v", ErrReadOnly, err)
|
|
}
|
|
return fmt.Errorf("remove the file: %w", err)
|
|
}
|