Rename host library to master throughout
The two libraries are now "master" and "slave" everywhere: UI text, API JSON fields (master_root, rel_master, master_size, ...), Go identifiers, database columns, quality verdicts, quarantine subfolders and docs. Existing databases are upgraded by a new migration that renames the columns in place and rewrites stored "host" values, so pairs, history, cached verdicts and restorable quarantine entries carry over. Network "host" usages (SplitHostPort, the Origin/Host check) are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d06b6d546d
commit
5b088bb4a6
19 files changed
+339
-326
No files matched your search
+22
-22
@@ -26,8 +26,8 @@ const QuarantineDirName = ".playlink-trash"
|
||||
|
||||
var (
|
||||
ErrBusy = errors.New("another job is already running for this library pair")
|
||||
ErrDowngrade = errors.New("the host copy is lower quality than the slave copy; confirm the downgrade to continue")
|
||||
ErrConfirm = errors.New("this action modifies the host library and must be confirmed")
|
||||
ErrDowngrade = errors.New("the master copy is lower quality than the slave copy; confirm the downgrade to continue")
|
||||
ErrConfirm = errors.New("this action modifies the master library and must be confirmed")
|
||||
)
|
||||
|
||||
type Engine struct {
|
||||
@@ -163,7 +163,7 @@ func (e *Engine) startJob(pairID int64, kind string, run func(ctx context.Contex
|
||||
|
||||
type libs struct {
|
||||
pair store.Pair
|
||||
host fsops.Root
|
||||
master fsops.Root
|
||||
slave fsops.Root
|
||||
quarantine string
|
||||
dev uint64
|
||||
@@ -187,23 +187,23 @@ func (e *Engine) withinAllowed(p string) bool {
|
||||
// called before every scan and operation so configuration drift (a remount,
|
||||
// a moved folder) is caught before anything is touched.
|
||||
func (e *Engine) open(p store.Pair) (*libs, error) {
|
||||
host, err := fsops.NewRoot(p.HostRoot)
|
||||
master, err := fsops.NewRoot(p.MasterRoot)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("host library: %w", err)
|
||||
return nil, fmt.Errorf("master library: %w", err)
|
||||
}
|
||||
slave, err := fsops.NewRoot(p.SlaveRoot)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("slave library: %w", err)
|
||||
}
|
||||
for _, r := range []string{host.Path, slave.Path} {
|
||||
for _, r := range []string{master.Path, slave.Path} {
|
||||
if !e.withinAllowed(r) {
|
||||
return nil, fmt.Errorf("%s is outside the allowed roots %v", r, e.allowedRoots)
|
||||
}
|
||||
}
|
||||
if fsops.Within(host.Path, slave.Path) || fsops.Within(slave.Path, host.Path) {
|
||||
return nil, errors.New("host and slave libraries must not overlap or contain each other")
|
||||
if fsops.Within(master.Path, slave.Path) || fsops.Within(slave.Path, master.Path) {
|
||||
return nil, errors.New("master and slave libraries must not overlap or contain each other")
|
||||
}
|
||||
hi, err := fsops.Lstat(host.Path)
|
||||
mi, err := fsops.Lstat(master.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -211,17 +211,17 @@ func (e *Engine) open(p store.Pair) (*libs, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if hi.Dev != si.Dev {
|
||||
return nil, fmt.Errorf("host and slave are on different filesystems (device %d vs %d): %w. "+
|
||||
"In Docker, mount a single common parent folder instead of two separate volumes", hi.Dev, si.Dev, fsops.ErrCrossDevice)
|
||||
if mi.Dev != si.Dev {
|
||||
return nil, fmt.Errorf("master and slave are on different filesystems (device %d vs %d): %w. "+
|
||||
"In Docker, mount a single common parent folder instead of two separate volumes", mi.Dev, si.Dev, fsops.ErrCrossDevice)
|
||||
}
|
||||
|
||||
q := filepath.Clean(p.QuarantineDir)
|
||||
if !filepath.IsAbs(q) {
|
||||
return nil, errors.New("quarantine directory must be absolute")
|
||||
}
|
||||
if fsops.Within(host.Path, q) {
|
||||
return nil, errors.New("quarantine directory must not be inside the host library")
|
||||
if fsops.Within(master.Path, q) {
|
||||
return nil, errors.New("quarantine directory must not be inside the master library")
|
||||
}
|
||||
if q == slave.Path || !e.withinAllowed(q) {
|
||||
return nil, errors.New("quarantine directory must be inside the allowed roots and not the slave root itself")
|
||||
@@ -236,7 +236,7 @@ func (e *Engine) open(p store.Pair) (*libs, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if qi.Dev != hi.Dev {
|
||||
if qi.Dev != mi.Dev {
|
||||
return nil, errors.New("quarantine directory must be on the same filesystem as the libraries")
|
||||
}
|
||||
|
||||
@@ -250,7 +250,7 @@ func (e *Engine) open(p store.Pair) (*libs, error) {
|
||||
if len(exts) == 0 {
|
||||
return nil, errors.New("no file extensions configured")
|
||||
}
|
||||
return &libs{pair: p, host: host, slave: slave, quarantine: q, dev: hi.Dev, exts: exts}, nil
|
||||
return &libs{pair: p, master: master, slave: slave, quarantine: q, dev: mi.Dev, exts: exts}, nil
|
||||
}
|
||||
|
||||
// NormalizePair fills defaults and validates a pair. It returns non-fatal
|
||||
@@ -260,7 +260,7 @@ func (e *Engine) NormalizePair(p *store.Pair) ([]string, error) {
|
||||
if p.Name == "" {
|
||||
return nil, errors.New("name is required")
|
||||
}
|
||||
p.HostRoot = filepath.Clean(strings.TrimSpace(p.HostRoot))
|
||||
p.MasterRoot = filepath.Clean(strings.TrimSpace(p.MasterRoot))
|
||||
p.SlaveRoot = filepath.Clean(strings.TrimSpace(p.SlaveRoot))
|
||||
if strings.TrimSpace(p.QuarantineDir) == "" {
|
||||
p.QuarantineDir = filepath.Join(p.SlaveRoot, QuarantineDirName)
|
||||
@@ -288,7 +288,7 @@ func (e *Engine) NormalizePair(p *store.Pair) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
// Store resolved paths so later containment checks compare like with like.
|
||||
p.HostRoot, p.SlaveRoot, p.QuarantineDir = l.host.Path, l.slave.Path, l.quarantine
|
||||
p.MasterRoot, p.SlaveRoot, p.QuarantineDir = l.master.Path, l.slave.Path, l.quarantine
|
||||
return layoutWarnings(l), nil
|
||||
}
|
||||
|
||||
@@ -312,20 +312,20 @@ func layoutWarnings(l *libs) []string {
|
||||
}
|
||||
return
|
||||
}
|
||||
hd, _ := list(l.host.Path)
|
||||
md, _ := list(l.master.Path)
|
||||
sd, sf := list(l.slave.Path)
|
||||
if len(sd) == 0 && sf == 0 {
|
||||
warns = append(warns, "The slave library is empty. Use Build to pick songs from the host library to hardlink into it.")
|
||||
warns = append(warns, "The slave library is empty. Use Build to pick songs from the master library to hardlink into it.")
|
||||
return warns
|
||||
}
|
||||
shared := 0
|
||||
for d := range sd {
|
||||
if hd[d] {
|
||||
if md[d] {
|
||||
shared++
|
||||
}
|
||||
}
|
||||
if len(sd) > 0 && shared*2 < len(sd) {
|
||||
warns = append(warns, fmt.Sprintf("Only %d of %d top-level folders in the slave library also exist in the host library. "+
|
||||
warns = append(warns, fmt.Sprintf("Only %d of %d top-level folders in the slave library also exist in the master library. "+
|
||||
"Both libraries should use the same folder layout, or most files will not match.", shared, len(sd)))
|
||||
}
|
||||
return warns
|
||||
|
||||
@@ -19,13 +19,13 @@ import (
|
||||
)
|
||||
|
||||
type env struct {
|
||||
t *testing.T
|
||||
root string
|
||||
host string
|
||||
slave string
|
||||
st *store.Store
|
||||
e *Engine
|
||||
pair store.Pair
|
||||
t *testing.T
|
||||
root string
|
||||
master string
|
||||
slave string
|
||||
st *store.Store
|
||||
e *Engine
|
||||
pair store.Pair
|
||||
}
|
||||
|
||||
func setup(t *testing.T, dryRun bool) *env {
|
||||
@@ -34,8 +34,8 @@ func setup(t *testing.T, dryRun bool) *env {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ev := &env{t: t, root: root, host: filepath.Join(root, "Albums"), slave: filepath.Join(root, "Singles")}
|
||||
for _, d := range []string{ev.host, ev.slave, filepath.Join(root, "db")} {
|
||||
ev := &env{t: t, root: root, master: filepath.Join(root, "Albums"), slave: filepath.Join(root, "Singles")}
|
||||
for _, d := range []string{ev.master, ev.slave, filepath.Join(root, "db")} {
|
||||
if err := os.MkdirAll(d, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func setup(t *testing.T, dryRun bool) *env {
|
||||
}
|
||||
ev.st = st
|
||||
ev.e = New(st, probe.Prober{}, []string{root}, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
ev.pair = store.Pair{Name: "test", HostRoot: ev.host, SlaveRoot: ev.slave, RetentionDays: 30}
|
||||
ev.pair = store.Pair{Name: "test", MasterRoot: ev.master, SlaveRoot: ev.slave, RetentionDays: 30}
|
||||
if _, err := ev.e.NormalizePair(&ev.pair); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -136,7 +136,7 @@ var ctx = context.Background()
|
||||
func TestIdenticalLinkAndUndo(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
rel := "bbno$/Bag or Die/edamame.flac"
|
||||
h := ev.write(ev.host, rel, "same-bytes")
|
||||
h := ev.write(ev.master, rel, "same-bytes")
|
||||
s := ev.write(ev.slave, rel, "same-bytes")
|
||||
ev.scan()
|
||||
|
||||
@@ -152,7 +152,7 @@ func TestIdenticalLinkAndUndo(t *testing.T) {
|
||||
t.Fatal("identical link must not quarantine")
|
||||
}
|
||||
if !sameInode(t, h, s) {
|
||||
t.Fatal("slave is not a hardlink of host")
|
||||
t.Fatal("slave is not a hardlink of master")
|
||||
}
|
||||
if ev.match(rel).Status != store.StatusLinked {
|
||||
t.Fatal("match not marked linked")
|
||||
@@ -169,7 +169,7 @@ func TestIdenticalLinkAndUndo(t *testing.T) {
|
||||
func TestDifferentLinkQuarantinesAndUndoRestores(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
rel := "Artist/Album/song.flac"
|
||||
h := ev.write(ev.host, rel, "host-version-longer")
|
||||
h := ev.write(ev.master, rel, "master-version-longer")
|
||||
s := ev.write(ev.slave, rel, "slave-version")
|
||||
ev.scan()
|
||||
|
||||
@@ -190,7 +190,7 @@ func TestDifferentLinkQuarantinesAndUndoRestores(t *testing.T) {
|
||||
if err := ev.e.Undo(ctx, op.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ev.read(s) != "slave-version" || ev.read(h) != "host-version-longer" {
|
||||
if ev.read(s) != "slave-version" || ev.read(h) != "master-version-longer" {
|
||||
t.Fatal("undo did not restore the original")
|
||||
}
|
||||
if _, err := os.Stat(op.QuarantinePath); !errors.Is(err, os.ErrNotExist) {
|
||||
@@ -203,12 +203,12 @@ func TestDifferentLinkQuarantinesAndUndoRestores(t *testing.T) {
|
||||
|
||||
func TestSimilarExtensionLinkRenamesAndUndo(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
h := ev.write(ev.host, "A/B/01 - Song.flac", "flac-data")
|
||||
h := ev.write(ev.master, "A/B/01 - Song.flac", "flac-data")
|
||||
s := ev.write(ev.slave, "A/B/01 - Song.mp3", "mp3")
|
||||
ev.scan()
|
||||
|
||||
m := ev.match("A/B/01 - Song.mp3")
|
||||
if m.Status != store.StatusSimilar || m.Kind != store.KindExtension || m.RelHost != "A/B/01 - Song.flac" {
|
||||
if m.Status != store.StatusSimilar || m.Kind != store.KindExtension || m.RelMaster != "A/B/01 - Song.flac" {
|
||||
t.Fatalf("unexpected match %+v", m)
|
||||
}
|
||||
op, err := ev.e.Link(ctx, m.ID, LinkOptions{})
|
||||
@@ -217,7 +217,7 @@ func TestSimilarExtensionLinkRenamesAndUndo(t *testing.T) {
|
||||
}
|
||||
newSlave := filepath.Join(ev.slave, "A/B/01 - Song.flac")
|
||||
if !sameInode(t, h, newSlave) {
|
||||
t.Fatal("slave should now hold the host's .flac as a hardlink")
|
||||
t.Fatal("slave should now hold the master's .flac as a hardlink")
|
||||
}
|
||||
if _, err := os.Stat(s); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatal("old .mp3 should be moved to quarantine")
|
||||
@@ -232,16 +232,16 @@ func TestSimilarExtensionLinkRenamesAndUndo(t *testing.T) {
|
||||
t.Fatal("linked .flac should be removed on undo")
|
||||
}
|
||||
if ev.read(h) != "flac-data" {
|
||||
t.Fatal("host must be untouched")
|
||||
t.Fatal("master must be untouched")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFuzzyTitleMatch(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
ev.write(ev.host, "A/B/03. My Song!.flac", "x")
|
||||
ev.write(ev.master, "A/B/03. My Song!.flac", "x")
|
||||
ev.write(ev.slave, "A/B/My Song.mp3", "y")
|
||||
ev.write(ev.slave, "A/B/My Song (Live).mp3", "z")
|
||||
ev.write(ev.host, "A/Other Album/Lonely.flac", "x")
|
||||
ev.write(ev.master, "A/Other Album/Lonely.flac", "x")
|
||||
ev.write(ev.slave, "A/Single/Lonely.flac", "x")
|
||||
ev.scan()
|
||||
if m := ev.match("A/B/My Song.mp3"); m.Kind != store.KindFuzzy {
|
||||
@@ -258,7 +258,7 @@ func TestFuzzyTitleMatch(t *testing.T) {
|
||||
func TestDowngradeGuardAndPromote(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
rel := "A/B/song.flac"
|
||||
h := ev.write(ev.host, rel, "lossy-host")
|
||||
h := ev.write(ev.master, rel, "lossy-master")
|
||||
s := ev.write(ev.slave, rel, "lossless-slave!")
|
||||
ev.scan()
|
||||
m := ev.match(rel)
|
||||
@@ -280,15 +280,15 @@ func TestDowngradeGuardAndPromote(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !sameInode(t, h, s) || ev.read(h) != "lossless-slave!" {
|
||||
t.Fatal("host should now be the slave's file")
|
||||
t.Fatal("master should now be the slave's file")
|
||||
}
|
||||
if ev.read(op.QuarantinePath) != "lossy-host" {
|
||||
t.Fatal("host original not quarantined")
|
||||
if ev.read(op.QuarantinePath) != "lossy-master" {
|
||||
t.Fatal("master original not quarantined")
|
||||
}
|
||||
if err := ev.e.Undo(ctx, op.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ev.read(h) != "lossy-host" || ev.read(s) != "lossless-slave!" || sameInode(t, h, s) {
|
||||
if ev.read(h) != "lossy-master" || ev.read(s) != "lossless-slave!" || sameInode(t, h, s) {
|
||||
t.Fatal("promote undo failed")
|
||||
}
|
||||
}
|
||||
@@ -296,7 +296,7 @@ func TestDowngradeGuardAndPromote(t *testing.T) {
|
||||
func TestChangedSinceScanIsRefused(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
rel := "A/B/c.flac"
|
||||
ev.write(ev.host, rel, "aaa")
|
||||
ev.write(ev.master, rel, "aaa")
|
||||
s := ev.write(ev.slave, rel, "bbb")
|
||||
ev.scan()
|
||||
m := ev.match(rel)
|
||||
@@ -315,7 +315,7 @@ func TestChangedSinceScanIsRefused(t *testing.T) {
|
||||
func TestDryRunChangesNothing(t *testing.T) {
|
||||
ev := setup(t, true)
|
||||
rel := "A/B/d.flac"
|
||||
h := ev.write(ev.host, rel, "same")
|
||||
h := ev.write(ev.master, rel, "same")
|
||||
s := ev.write(ev.slave, rel, "same")
|
||||
ev.scan()
|
||||
op, err := ev.e.Link(ctx, ev.match(rel).ID, LinkOptions{})
|
||||
@@ -337,7 +337,7 @@ func TestAutoLinkRespectsSettleTime(t *testing.T) {
|
||||
if err := ev.st.SavePair(ctx, &ev.pair); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h := ev.write(ev.host, "A/B/e.flac", "same")
|
||||
h := ev.write(ev.master, "A/B/e.flac", "same")
|
||||
s := ev.write(ev.slave, "A/B/e.flac", "same")
|
||||
ev.scan()
|
||||
if sameInode(t, h, s) {
|
||||
@@ -355,9 +355,9 @@ func TestAutoLinkRespectsSettleTime(t *testing.T) {
|
||||
|
||||
func TestBuildAndRemove(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
h1 := ev.write(ev.host, "bbno$/Album/one.flac", "1")
|
||||
ev.write(ev.host, "bbno$/Album/two.flac", "2")
|
||||
ev.write(ev.host, "bbno$/Album/cover.jpg", "img")
|
||||
h1 := ev.write(ev.master, "bbno$/Album/one.flac", "1")
|
||||
ev.write(ev.master, "bbno$/Album/two.flac", "2")
|
||||
ev.write(ev.master, "bbno$/Album/cover.jpg", "img")
|
||||
ev.write(ev.slave, "bbno$/Album/two.flac", "user's own different file")
|
||||
|
||||
if _, err := ev.e.StartBuild(ev.pair.ID, []string{"bbno$"}); err != nil {
|
||||
@@ -389,7 +389,7 @@ func TestBuildAndRemove(t *testing.T) {
|
||||
t.Fatal("linked file should be removed")
|
||||
}
|
||||
if ev.read(h1) != "1" {
|
||||
t.Fatal("host must be untouched")
|
||||
t.Fatal("master must be untouched")
|
||||
}
|
||||
if ev.read(filepath.Join(ev.slave, "bbno$/Album/two.flac")) != "user's own different file" {
|
||||
t.Fatal("remove must never delete a non-linked file")
|
||||
@@ -404,7 +404,7 @@ func TestSymlinkEscapeIgnored(t *testing.T) {
|
||||
if err := os.Symlink(outside, filepath.Join(ev.slave, "Evil", "Album")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ev.write(ev.host, "Evil/Album/secret.flac", "x")
|
||||
ev.write(ev.master, "Evil/Album/secret.flac", "x")
|
||||
ev.scan()
|
||||
if _, err := ev.st.MatchBySlave(ctx, ev.pair.ID, "Evil/Album/secret.flac"); err == nil {
|
||||
t.Fatal("files behind a symlinked directory must not be scanned")
|
||||
@@ -421,7 +421,7 @@ func TestSymlinkEscapeIgnored(t *testing.T) {
|
||||
func TestPurgeAndOverlapValidation(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
rel := "A/B/f.flac"
|
||||
ev.write(ev.host, rel, "one")
|
||||
ev.write(ev.master, rel, "one")
|
||||
ev.write(ev.slave, rel, "two!")
|
||||
ev.scan()
|
||||
op, err := ev.e.Link(ctx, ev.match(rel).ID, LinkOptions{})
|
||||
@@ -438,11 +438,11 @@ func TestPurgeAndOverlapValidation(t *testing.T) {
|
||||
t.Fatal("undo after purge must fail")
|
||||
}
|
||||
|
||||
bad := store.Pair{Name: "bad", HostRoot: ev.host, SlaveRoot: filepath.Join(ev.host, "A")}
|
||||
bad := store.Pair{Name: "bad", MasterRoot: ev.master, SlaveRoot: filepath.Join(ev.master, "A")}
|
||||
if _, err := ev.e.NormalizePair(&bad); err == nil {
|
||||
t.Fatal("nested libraries must be rejected")
|
||||
}
|
||||
outside := store.Pair{Name: "bad", HostRoot: ev.host, SlaveRoot: t.TempDir()}
|
||||
outside := store.Pair{Name: "bad", MasterRoot: ev.master, SlaveRoot: t.TempDir()}
|
||||
if _, err := ev.e.NormalizePair(&outside); err == nil {
|
||||
t.Fatal("paths outside allowed roots must be rejected")
|
||||
}
|
||||
@@ -485,7 +485,7 @@ func TestAnyLayoutMatchesWithoutWarnings(t *testing.T) {
|
||||
ev := setup(t, false)
|
||||
// Flat files, deeper Genre/Artist/Album/Disc nesting: all fine as long as both sides agree.
|
||||
for _, rel := range []string{"loose.flac", "Rock/Band/Album/Disc 1/01 - Song.flac", "Band/Song.flac"} {
|
||||
ev.write(ev.host, rel, "same:"+rel)
|
||||
ev.write(ev.master, rel, "same:"+rel)
|
||||
ev.write(ev.slave, rel, "same:"+rel)
|
||||
}
|
||||
ev.scan()
|
||||
|
||||
+79
-79
@@ -19,15 +19,15 @@ import (
|
||||
)
|
||||
|
||||
type LinkOptions struct {
|
||||
// AllowDowngrade confirms linking when the host copy scored lower.
|
||||
// AllowDowngrade confirms linking when the master copy scored lower.
|
||||
AllowDowngrade bool
|
||||
Trigger string
|
||||
}
|
||||
|
||||
// snapshot holds the verified state of both files right before an operation.
|
||||
type snapshot struct {
|
||||
hostAbs, slaveAbs string
|
||||
hi, si fsops.Info
|
||||
masterAbs, slaveAbs string
|
||||
mi, si fsops.Info
|
||||
}
|
||||
|
||||
// preflight re-validates a match against the filesystem: both files must be
|
||||
@@ -36,28 +36,28 @@ type snapshot struct {
|
||||
func preflight(l *libs, m store.Match) (snapshot, error) {
|
||||
var s snapshot
|
||||
var err error
|
||||
if s.hostAbs, err = l.host.Resolve(m.RelHost); err != nil {
|
||||
if s.masterAbs, err = l.master.Resolve(m.RelMaster); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if s.slaveAbs, err = l.slave.Resolve(m.RelSlave); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if s.hi, err = fsops.RegularFile(s.hostAbs); err != nil {
|
||||
return s, fmt.Errorf("host file: %w", err)
|
||||
if s.mi, err = fsops.RegularFile(s.masterAbs); err != nil {
|
||||
return s, fmt.Errorf("master file: %w", err)
|
||||
}
|
||||
if s.si, err = fsops.RegularFile(s.slaveAbs); err != nil {
|
||||
return s, fmt.Errorf("slave file: %w", err)
|
||||
}
|
||||
if s.hi.Ino != m.HostIno || !s.hi.Unchanged(m.HostSize, m.HostMtime) {
|
||||
return s, fmt.Errorf("host %w", fsops.ErrChanged)
|
||||
if s.mi.Ino != m.MasterIno || !s.mi.Unchanged(m.MasterSize, m.MasterMtime) {
|
||||
return s, fmt.Errorf("master %w", fsops.ErrChanged)
|
||||
}
|
||||
if s.si.Ino != m.SlaveIno || !s.si.Unchanged(m.SlaveSize, m.SlaveMtime) {
|
||||
return s, fmt.Errorf("slave %w", fsops.ErrChanged)
|
||||
}
|
||||
if s.hi.Dev != s.si.Dev || s.hi.Dev != l.dev {
|
||||
if s.mi.Dev != s.si.Dev || s.mi.Dev != l.dev {
|
||||
return s, fsops.ErrCrossDevice
|
||||
}
|
||||
if s.hi.SameInode(s.si) {
|
||||
if s.mi.SameInode(s.si) {
|
||||
return s, errors.New("already linked")
|
||||
}
|
||||
return s, nil
|
||||
@@ -128,7 +128,7 @@ func (e *Engine) finish(ctx context.Context, o *store.Operation, err error) erro
|
||||
e.log.Warn("operation failed", "op", o.ID, "action", o.Action, "slave", o.RelSlave, "err", err)
|
||||
} else {
|
||||
o.Status = store.OpDone
|
||||
e.log.Info("operation done", "op", o.ID, "action", o.Action, "slave", o.RelSlave, "host", o.RelHost)
|
||||
e.log.Info("operation done", "op", o.ID, "action", o.Action, "slave", o.RelSlave, "master", o.RelMaster)
|
||||
}
|
||||
if ferr := e.st.FinishOp(ctx, o.ID, o.Status, o.Error); ferr != nil && err == nil {
|
||||
return ferr
|
||||
@@ -142,10 +142,10 @@ func verdictOf(m store.Match) probe.Verdict {
|
||||
return v
|
||||
}
|
||||
|
||||
// Link replaces the slave file with a hardlink of the host file.
|
||||
// Link replaces the slave file with a hardlink of the master file.
|
||||
//
|
||||
// Identical files are re-compared byte for byte right before linking and no
|
||||
// quarantine copy is kept (the data exists in the host). Non-identical files
|
||||
// quarantine copy is kept (the data exists in the master). Non-identical files
|
||||
// are quarantined first so the operation can be undone.
|
||||
func (e *Engine) Link(ctx context.Context, matchID int64, opt LinkOptions) (store.Operation, error) {
|
||||
e.mu.Lock()
|
||||
@@ -176,7 +176,7 @@ func (e *Engine) Link(ctx context.Context, matchID int64, opt LinkOptions) (stor
|
||||
return store.Operation{}, err
|
||||
}
|
||||
if identical {
|
||||
same, err := fsops.SameContent(s.hostAbs, s.slaveAbs)
|
||||
same, err := fsops.SameContent(s.masterAbs, s.slaveAbs)
|
||||
if err != nil {
|
||||
return store.Operation{}, err
|
||||
}
|
||||
@@ -186,7 +186,7 @@ func (e *Engine) Link(ctx context.Context, matchID int64, opt LinkOptions) (stor
|
||||
}
|
||||
}
|
||||
|
||||
newRel := filepath.Join(filepath.Dir(m.RelSlave), filepath.Base(m.RelHost))
|
||||
newRel := filepath.Join(filepath.Dir(m.RelSlave), filepath.Base(m.RelMaster))
|
||||
newAbs, err := l.slave.Resolve(newRel)
|
||||
if err != nil {
|
||||
return store.Operation{}, err
|
||||
@@ -198,7 +198,7 @@ func (e *Engine) Link(ctx context.Context, matchID int64, opt LinkOptions) (stor
|
||||
}
|
||||
}
|
||||
|
||||
o := store.Operation{PairID: l.pair.ID, Action: store.ActLink, RelHost: m.RelHost, RelSlave: m.RelSlave,
|
||||
o := store.Operation{PairID: l.pair.ID, Action: store.ActLink, RelMaster: m.RelMaster, RelSlave: m.RelSlave,
|
||||
RelNew: newRel, Trigger: opt.Trigger}
|
||||
if identical {
|
||||
o.Bytes = s.si.Size // space freed
|
||||
@@ -226,7 +226,7 @@ func (e *Engine) Link(ctx context.Context, matchID int64, opt LinkOptions) (stor
|
||||
}
|
||||
}
|
||||
if !rename {
|
||||
if err := fsops.ReplaceWithLink(s.hostAbs, s.hi, s.slaveAbs); err != nil {
|
||||
if err := fsops.ReplaceWithLink(s.masterAbs, s.mi, s.slaveAbs); err != nil {
|
||||
if q != "" {
|
||||
if cur, e2 := fsops.Lstat(s.slaveAbs); e2 == nil && cur.SameInode(s.si) {
|
||||
dropQuarantine(l, q, s.si) // original untouched
|
||||
@@ -237,7 +237,7 @@ func (e *Engine) Link(ctx context.Context, matchID int64, opt LinkOptions) (stor
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := fsops.LinkNew(s.hostAbs, s.hi, newAbs); err != nil {
|
||||
if err := fsops.LinkNew(s.masterAbs, s.mi, newAbs); err != nil {
|
||||
if q != "" {
|
||||
dropQuarantine(l, q, s.si)
|
||||
e.clearQuarantine(ctx, o.ID)
|
||||
@@ -259,7 +259,7 @@ func (e *Engine) clearQuarantine(ctx context.Context, opID int64) {
|
||||
}
|
||||
|
||||
// Promote is the override path: the slave copy is better, so it replaces the
|
||||
// host file (whose original is quarantined) and both libraries share it.
|
||||
// master file (whose original is quarantined) and both libraries share it.
|
||||
func (e *Engine) Promote(ctx context.Context, matchID int64, confirmed bool) (store.Operation, error) {
|
||||
if !confirmed {
|
||||
return store.Operation{}, ErrConfirm
|
||||
@@ -282,20 +282,20 @@ func (e *Engine) Promote(ctx context.Context, matchID int64, confirmed bool) (st
|
||||
if err != nil {
|
||||
return store.Operation{}, err
|
||||
}
|
||||
newHostRel := filepath.Join(filepath.Dir(m.RelHost), filepath.Base(m.RelSlave))
|
||||
newHostAbs, err := l.host.Resolve(newHostRel)
|
||||
newMasterRel := filepath.Join(filepath.Dir(m.RelMaster), filepath.Base(m.RelSlave))
|
||||
newMasterAbs, err := l.master.Resolve(newMasterRel)
|
||||
if err != nil {
|
||||
return store.Operation{}, err
|
||||
}
|
||||
rename := newHostRel != m.RelHost
|
||||
rename := newMasterRel != m.RelMaster
|
||||
if rename {
|
||||
if _, err := os.Lstat(newHostAbs); err == nil {
|
||||
return store.Operation{}, fmt.Errorf("host already has %s: %w", newHostRel, fsops.ErrExists)
|
||||
if _, err := os.Lstat(newMasterAbs); err == nil {
|
||||
return store.Operation{}, fmt.Errorf("master already has %s: %w", newMasterRel, fsops.ErrExists)
|
||||
}
|
||||
}
|
||||
|
||||
o := store.Operation{PairID: l.pair.ID, Action: store.ActPromote, RelHost: m.RelHost, RelSlave: m.RelSlave,
|
||||
RelNew: newHostRel, QuarantineSide: "host", Trigger: "manual"}
|
||||
o := store.Operation{PairID: l.pair.ID, Action: store.ActPromote, RelMaster: m.RelMaster, RelSlave: m.RelSlave,
|
||||
RelNew: newMasterRel, QuarantineSide: "master", Trigger: "manual"}
|
||||
if err := e.begin(ctx, &o); err != nil {
|
||||
return o, err
|
||||
}
|
||||
@@ -303,31 +303,31 @@ func (e *Engine) Promote(ctx context.Context, matchID int64, confirmed bool) (st
|
||||
return o, nil
|
||||
}
|
||||
err = func() error {
|
||||
q := quarantinePath(l, o.ID, "host", m.RelHost)
|
||||
if err := quarantine(s.hostAbs, s.hi, q); err != nil {
|
||||
q := quarantinePath(l, o.ID, "master", m.RelMaster)
|
||||
if err := quarantine(s.masterAbs, s.mi, q); err != nil {
|
||||
return fmt.Errorf("quarantine: %w", err)
|
||||
}
|
||||
o.QuarantinePath = q
|
||||
if _, err := e.st.DB.ExecContext(ctx, `UPDATE operations SET quarantine_path = ? WHERE id = ?`, q, o.ID); err != nil {
|
||||
dropQuarantine(l, q, s.hi)
|
||||
dropQuarantine(l, q, s.mi)
|
||||
return err
|
||||
}
|
||||
if !rename {
|
||||
if err := fsops.ReplaceWithLink(s.slaveAbs, s.si, s.hostAbs); err != nil {
|
||||
if cur, e2 := fsops.Lstat(s.hostAbs); e2 == nil && cur.SameInode(s.hi) {
|
||||
dropQuarantine(l, q, s.hi)
|
||||
if err := fsops.ReplaceWithLink(s.slaveAbs, s.si, s.masterAbs); err != nil {
|
||||
if cur, e2 := fsops.Lstat(s.masterAbs); e2 == nil && cur.SameInode(s.mi) {
|
||||
dropQuarantine(l, q, s.mi)
|
||||
e.clearQuarantine(ctx, o.ID)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := fsops.LinkNew(s.slaveAbs, s.si, newHostAbs); err != nil {
|
||||
dropQuarantine(l, q, s.hi)
|
||||
if err := fsops.LinkNew(s.slaveAbs, s.si, newMasterAbs); err != nil {
|
||||
dropQuarantine(l, q, s.mi)
|
||||
e.clearQuarantine(ctx, o.ID)
|
||||
return err
|
||||
}
|
||||
return removeOriginal(s.hostAbs, s.hi)
|
||||
return removeOriginal(s.masterAbs, s.mi)
|
||||
}()
|
||||
err = e.finish(ctx, &o, err)
|
||||
e.refresh(ctx, l, m.RelSlave)
|
||||
@@ -368,15 +368,15 @@ func expand(ctx context.Context, l *libs, root fsops.Root, rels []string) ([]str
|
||||
return sortedKeys(set), nil
|
||||
}
|
||||
|
||||
// StartBuild hardlinks the chosen host files/folders into the slave library,
|
||||
// mirroring the host path. Existing slave files are never overwritten.
|
||||
// StartBuild hardlinks the chosen master files/folders into the slave library,
|
||||
// mirroring the master path. Existing slave files are never overwritten.
|
||||
func (e *Engine) StartBuild(pairID int64, rels []string) (*Job, error) {
|
||||
return e.startJob(pairID, "build", func(ctx context.Context, j *Job) error {
|
||||
l, err := e.loadPair(ctx, pairID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
files, err := expand(ctx, l, l.host, rels)
|
||||
files, err := expand(ctx, l, l.master, rels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -395,7 +395,7 @@ func (e *Engine) StartBuild(pairID int64, rels []string) (*Job, error) {
|
||||
func (e *Engine) buildOne(ctx context.Context, j *Job, l *libs, rel string) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
hostAbs, err := l.host.Resolve(rel)
|
||||
masterAbs, err := l.master.Resolve(rel)
|
||||
if err != nil {
|
||||
j.skip(rel + ": " + err.Error())
|
||||
return
|
||||
@@ -405,20 +405,20 @@ func (e *Engine) buildOne(ctx context.Context, j *Job, l *libs, rel string) {
|
||||
j.skip(rel + ": " + err.Error())
|
||||
return
|
||||
}
|
||||
hi, err := fsops.RegularFile(hostAbs)
|
||||
mi, err := fsops.RegularFile(masterAbs)
|
||||
if err != nil {
|
||||
j.skip(rel + ": " + err.Error())
|
||||
return
|
||||
}
|
||||
if si, err := fsops.Lstat(slaveAbs); err == nil {
|
||||
if si.SameInode(hi) {
|
||||
if si.SameInode(mi) {
|
||||
j.count("already linked")
|
||||
} else {
|
||||
j.skip(rel + ": a different file already exists in the slave library (scan, then use Review)")
|
||||
}
|
||||
return
|
||||
}
|
||||
o := store.Operation{PairID: l.pair.ID, Action: store.ActBuild, RelHost: rel, RelSlave: rel, RelNew: rel, Trigger: "build"}
|
||||
o := store.Operation{PairID: l.pair.ID, Action: store.ActBuild, RelMaster: rel, RelSlave: rel, RelNew: rel, Trigger: "build"}
|
||||
if err := e.begin(ctx, &o); err != nil {
|
||||
j.skip(rel + ": " + err.Error())
|
||||
return
|
||||
@@ -429,7 +429,7 @@ func (e *Engine) buildOne(ctx context.Context, j *Job, l *libs, rel string) {
|
||||
}
|
||||
err = fsops.MkdirAllLike(filepath.Dir(slaveAbs))
|
||||
if err == nil {
|
||||
err = fsops.LinkNew(hostAbs, hi, slaveAbs)
|
||||
err = fsops.LinkNew(masterAbs, mi, slaveAbs)
|
||||
}
|
||||
if err = e.finish(ctx, &o, err); err != nil {
|
||||
j.skip(rel + ": " + err.Error())
|
||||
@@ -439,7 +439,7 @@ func (e *Engine) buildOne(ctx context.Context, j *Job, l *libs, rel string) {
|
||||
e.refresh(ctx, l, rel)
|
||||
}
|
||||
|
||||
// StartRemove deletes slave files that are hardlinks of the host file at the
|
||||
// StartRemove deletes slave files that are hardlinks of the master file at the
|
||||
// same path. Anything that is not such a link is left alone.
|
||||
func (e *Engine) StartRemove(pairID int64, rels []string) (*Job, error) {
|
||||
return e.startJob(pairID, "remove", func(ctx context.Context, j *Job) error {
|
||||
@@ -466,15 +466,15 @@ func (e *Engine) StartRemove(pairID int64, rels []string) (*Job, error) {
|
||||
func (e *Engine) removeOne(ctx context.Context, j *Job, l *libs, rel string) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
hostAbs, err1 := l.host.Resolve(rel)
|
||||
masterAbs, err1 := l.master.Resolve(rel)
|
||||
slaveAbs, err2 := l.slave.Resolve(rel)
|
||||
if err1 != nil || err2 != nil {
|
||||
j.skip(rel + ": invalid path")
|
||||
return
|
||||
}
|
||||
hi, err := fsops.RegularFile(hostAbs)
|
||||
mi, err := fsops.RegularFile(masterAbs)
|
||||
if err != nil {
|
||||
j.skip(rel + ": no host file at the same path, so it is not removed")
|
||||
j.skip(rel + ": no master file at the same path, so it is not removed")
|
||||
return
|
||||
}
|
||||
si, err := fsops.RegularFile(slaveAbs)
|
||||
@@ -482,11 +482,11 @@ func (e *Engine) removeOne(ctx context.Context, j *Job, l *libs, rel string) {
|
||||
j.skip(rel + ": " + err.Error())
|
||||
return
|
||||
}
|
||||
if !si.SameInode(hi) {
|
||||
j.skip(rel + ": not a hardlink of the host file, so it is not removed")
|
||||
if !si.SameInode(mi) {
|
||||
j.skip(rel + ": not a hardlink of the master file, so it is not removed")
|
||||
return
|
||||
}
|
||||
o := store.Operation{PairID: l.pair.ID, Action: store.ActRemove, RelHost: rel, RelSlave: rel, Trigger: "build"}
|
||||
o := store.Operation{PairID: l.pair.ID, Action: store.ActRemove, RelMaster: rel, RelSlave: rel, Trigger: "build"}
|
||||
if err := e.begin(ctx, &o); err != nil {
|
||||
j.skip(rel + ": " + err.Error())
|
||||
return
|
||||
@@ -495,7 +495,7 @@ func (e *Engine) removeOne(ctx context.Context, j *Job, l *libs, rel string) {
|
||||
j.count("planned")
|
||||
return
|
||||
}
|
||||
err = fsops.RemoveLink(slaveAbs, hi)
|
||||
err = fsops.RemoveLink(slaveAbs, mi)
|
||||
if err = e.finish(ctx, &o, err); err != nil {
|
||||
j.skip(rel + ": " + err.Error())
|
||||
return
|
||||
@@ -533,7 +533,7 @@ func (e *Engine) Undo(ctx context.Context, opID int64) error {
|
||||
}
|
||||
return p
|
||||
}
|
||||
hostAbs := res(l.host, o.RelHost)
|
||||
masterAbs := res(l.master, o.RelMaster)
|
||||
slaveAbs := res(l.slave, o.RelSlave)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -541,24 +541,24 @@ func (e *Engine) Undo(ctx context.Context, opID int64) error {
|
||||
|
||||
switch o.Action {
|
||||
case store.ActBuild:
|
||||
hi, err := fsops.RegularFile(hostAbs)
|
||||
mi, err := fsops.RegularFile(masterAbs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fsops.RemoveLink(slaveAbs, hi); err != nil {
|
||||
if err := fsops.RemoveLink(slaveAbs, mi); err != nil {
|
||||
return err
|
||||
}
|
||||
pruneEmpty(filepath.Dir(slaveAbs), l.slave.Path)
|
||||
|
||||
case store.ActRemove:
|
||||
hi, err := fsops.RegularFile(hostAbs)
|
||||
mi, err := fsops.RegularFile(masterAbs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fsops.MkdirAllLike(filepath.Dir(slaveAbs)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fsops.LinkNew(hostAbs, hi, slaveAbs); err != nil {
|
||||
if err := fsops.LinkNew(masterAbs, mi, slaveAbs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -567,7 +567,7 @@ func (e *Engine) Undo(ctx context.Context, opID int64) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hi, err := fsops.RegularFile(hostAbs)
|
||||
mi, err := fsops.RegularFile(masterAbs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -575,8 +575,8 @@ func (e *Engine) Undo(ctx context.Context, opID int64) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !cur.SameInode(hi) {
|
||||
return errors.New("the slave file is no longer linked to the host file; refusing to undo")
|
||||
if !cur.SameInode(mi) {
|
||||
return errors.New("the slave file is no longer linked to the master file; refusing to undo")
|
||||
}
|
||||
renamed := o.RelNew != o.RelSlave
|
||||
if renamed {
|
||||
@@ -586,7 +586,7 @@ func (e *Engine) Undo(ctx context.Context, opID int64) error {
|
||||
}
|
||||
if o.QuarantinePath == "" {
|
||||
// Identical link: give the slave its own copy of the bytes again.
|
||||
if err := fsops.CopyReplace(hostAbs, slaveAbs, hi.Mode); err != nil {
|
||||
if err := fsops.CopyReplace(masterAbs, slaveAbs, mi.Mode); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
@@ -605,14 +605,14 @@ func (e *Engine) Undo(ctx context.Context, opID int64) error {
|
||||
dropQuarantine(l, o.QuarantinePath, qi)
|
||||
}
|
||||
if renamed {
|
||||
if err := fsops.RemoveLink(newAbs, hi); err != nil {
|
||||
if err := fsops.RemoveLink(newAbs, mi); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
e.refresh(ctx, l, o.RelNew)
|
||||
|
||||
case store.ActPromote:
|
||||
newHostAbs := res(l.host, o.RelNew)
|
||||
newMasterAbs := res(l.master, o.RelNew)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -620,25 +620,25 @@ func (e *Engine) Undo(ctx context.Context, opID int64) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cur, err := fsops.RegularFile(newHostAbs)
|
||||
cur, err := fsops.RegularFile(newMasterAbs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !cur.SameInode(si) {
|
||||
return errors.New("the host file changed after promotion; refusing to undo")
|
||||
return errors.New("the master file changed after promotion; refusing to undo")
|
||||
}
|
||||
qi, err := e.quarantined(l, o.QuarantinePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if o.RelNew == o.RelHost {
|
||||
err = fsops.ReplaceWithLink(o.QuarantinePath, qi, hostAbs)
|
||||
if o.RelNew == o.RelMaster {
|
||||
err = fsops.ReplaceWithLink(o.QuarantinePath, qi, masterAbs)
|
||||
} else {
|
||||
if _, e2 := os.Lstat(hostAbs); e2 == nil {
|
||||
return fmt.Errorf("%s: %w", o.RelHost, fsops.ErrExists)
|
||||
if _, e2 := os.Lstat(masterAbs); e2 == nil {
|
||||
return fmt.Errorf("%s: %w", o.RelMaster, fsops.ErrExists)
|
||||
}
|
||||
if err = fsops.LinkNew(o.QuarantinePath, qi, hostAbs); err == nil {
|
||||
err = fsops.RemoveLink(newHostAbs, si)
|
||||
if err = fsops.LinkNew(o.QuarantinePath, qi, masterAbs); err == nil {
|
||||
err = fsops.RemoveLink(newMasterAbs, si)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@@ -732,8 +732,8 @@ func (e *Engine) Recover(ctx context.Context) {
|
||||
}
|
||||
for _, o := range ops {
|
||||
if p, err := e.st.Pair(ctx, o.PairID); err == nil {
|
||||
for _, root := range []string{p.SlaveRoot, p.HostRoot} {
|
||||
for _, rel := range []string{o.RelSlave, o.RelNew, o.RelHost} {
|
||||
for _, root := range []string{p.SlaveRoot, p.MasterRoot} {
|
||||
for _, rel := range []string{o.RelSlave, o.RelNew, o.RelMaster} {
|
||||
if rel == "" {
|
||||
continue
|
||||
}
|
||||
@@ -763,15 +763,15 @@ type TreeEntry struct {
|
||||
Linked int `json:"linked,omitempty"`
|
||||
}
|
||||
|
||||
// Tree lists a host directory and shows how much of it is in the slave.
|
||||
// Tree lists a master directory and shows how much of it is in the slave.
|
||||
func (e *Engine) Tree(ctx context.Context, pairID int64, rel string) ([]TreeEntry, error) {
|
||||
l, err := e.loadPair(ctx, pairID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dir := l.host.Path
|
||||
dir := l.master.Path
|
||||
if rel != "" {
|
||||
if dir, err = l.host.Resolve(rel); err != nil {
|
||||
if dir, err = l.master.Resolve(rel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -779,7 +779,7 @@ func (e *Engine) Tree(ctx context.Context, pairID int64, rel string) ([]TreeEntr
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
linked, _ := e.st.LinkedHostPaths(ctx, pairID, rel)
|
||||
linked, _ := e.st.LinkedMasterPaths(ctx, pairID, rel)
|
||||
perChild := map[string]int{}
|
||||
for _, p := range linked {
|
||||
sub := p
|
||||
@@ -807,16 +807,16 @@ func (e *Engine) Tree(ctx context.Context, pairID int64, rel string) ([]TreeEntr
|
||||
if !l.hasExt(name) {
|
||||
continue
|
||||
}
|
||||
hi, err := fsops.Lstat(filepath.Join(dir, name))
|
||||
mi, err := fsops.Lstat(filepath.Join(dir, name))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
te.Size = hi.Size
|
||||
te.Size = mi.Size
|
||||
te.State = "missing"
|
||||
if sp, err := l.slave.Resolve(child); err == nil {
|
||||
if si, err := fsops.Lstat(sp); err == nil {
|
||||
te.State = "conflict"
|
||||
if si.SameInode(hi) {
|
||||
if si.SameInode(mi) {
|
||||
te.State = "linked"
|
||||
}
|
||||
}
|
||||
|
||||
+45
-45
@@ -97,17 +97,17 @@ func firstComponent(rel string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// hostIndex supports the fallback lookups for slave files without an exact
|
||||
// masterIndex supports the fallback lookups for slave files without an exact
|
||||
// path counterpart.
|
||||
type hostIndex struct {
|
||||
type masterIndex struct {
|
||||
files map[string]fsops.Info
|
||||
lower map[string]string // lower(rel) -> rel
|
||||
byDir map[string][]string // lower(dir) -> rels
|
||||
byTitle map[string][]string // lower(top folder)|normTitle -> rels
|
||||
}
|
||||
|
||||
func buildIndex(files map[string]fsops.Info) *hostIndex {
|
||||
ix := &hostIndex{files: files, lower: map[string]string{}, byDir: map[string][]string{}, byTitle: map[string][]string{}}
|
||||
func buildIndex(files map[string]fsops.Info) *masterIndex {
|
||||
ix := &masterIndex{files: files, lower: map[string]string{}, byDir: map[string][]string{}, byTitle: map[string][]string{}}
|
||||
for _, rel := range sortedKeys(files) {
|
||||
ix.lower[strings.ToLower(rel)] = rel
|
||||
d := strings.ToLower(filepath.Dir(rel))
|
||||
@@ -118,7 +118,7 @@ func buildIndex(files map[string]fsops.Info) *hostIndex {
|
||||
return ix
|
||||
}
|
||||
|
||||
// losslessRank orders extension candidates so a lossless host file is
|
||||
// losslessRank orders extension candidates so a lossless master file is
|
||||
// preferred when several share the same title.
|
||||
func extRank(rel string) int {
|
||||
switch strings.ToLower(filepath.Ext(rel)) {
|
||||
@@ -130,8 +130,8 @@ func extRank(rel string) int {
|
||||
return 2
|
||||
}
|
||||
|
||||
// find locates the host counterpart of a slave file.
|
||||
func (ix *hostIndex) find(relSlave string) (string, string) {
|
||||
// find locates the master counterpart of a slave file.
|
||||
func (ix *masterIndex) find(relSlave string) (string, string) {
|
||||
if _, ok := ix.files[relSlave]; ok {
|
||||
return relSlave, store.KindPath
|
||||
}
|
||||
@@ -173,40 +173,40 @@ func (ix *hostIndex) find(relSlave string) (string, string) {
|
||||
|
||||
// classify fills in status, probes and verdict for a match, reusing cached
|
||||
// results from prev when neither file changed.
|
||||
func (e *Engine) classify(ctx context.Context, l *libs, m *store.Match, hi, si fsops.Info, prev *store.Match) {
|
||||
func (e *Engine) classify(ctx context.Context, l *libs, m *store.Match, mi, si fsops.Info, prev *store.Match) {
|
||||
m.SlaveIno, m.SlaveSize, m.SlaveMtime = si.Ino, si.Size, si.MtimeNs
|
||||
m.HostIno, m.HostSize, m.HostMtime = 0, 0, 0
|
||||
m.HostProbe, m.SlaveProbe, m.Verdict, m.Note = nil, nil, nil, ""
|
||||
m.MasterIno, m.MasterSize, m.MasterMtime = 0, 0, 0
|
||||
m.MasterProbe, m.SlaveProbe, m.Verdict, m.Note = nil, nil, nil, ""
|
||||
if m.Kind == store.KindNone {
|
||||
m.Status = store.StatusUnmatched
|
||||
return
|
||||
}
|
||||
m.HostIno, m.HostSize, m.HostMtime = hi.Ino, hi.Size, hi.MtimeNs
|
||||
m.MasterIno, m.MasterSize, m.MasterMtime = mi.Ino, mi.Size, mi.MtimeNs
|
||||
|
||||
unchanged := prev != nil && prev.RelHost == m.RelHost &&
|
||||
prev.HostIno == hi.Ino && prev.HostSize == hi.Size && prev.HostMtime == hi.MtimeNs &&
|
||||
unchanged := prev != nil && prev.RelMaster == m.RelMaster &&
|
||||
prev.MasterIno == mi.Ino && prev.MasterSize == mi.Size && prev.MasterMtime == mi.MtimeNs &&
|
||||
prev.SlaveIno == si.Ino && prev.SlaveSize == si.Size && prev.SlaveMtime == si.MtimeNs
|
||||
if prev != nil && prev.RelHost == m.RelHost {
|
||||
if prev != nil && prev.RelMaster == m.RelMaster {
|
||||
m.Ignored = prev.Ignored
|
||||
}
|
||||
|
||||
switch {
|
||||
case hi.Dev != si.Dev:
|
||||
case mi.Dev != si.Dev:
|
||||
m.Status, m.Note = store.StatusError, "files are on different filesystems"
|
||||
return
|
||||
case hi.SameInode(si):
|
||||
case mi.SameInode(si):
|
||||
m.Status = store.StatusLinked
|
||||
return
|
||||
}
|
||||
|
||||
pathKind := m.Kind == store.KindPath || m.Kind == store.KindPathCase
|
||||
if pathKind && hi.Size == si.Size {
|
||||
if pathKind && mi.Size == si.Size {
|
||||
if unchanged && (prev.Status == store.StatusIdentical || prev.Status == store.StatusDifferent) {
|
||||
m.Status = prev.Status
|
||||
} else {
|
||||
hostAbs := filepath.Join(l.host.Path, m.RelHost)
|
||||
masterAbs := filepath.Join(l.master.Path, m.RelMaster)
|
||||
slaveAbs := filepath.Join(l.slave.Path, m.RelSlave)
|
||||
same, err := fsops.SameContent(hostAbs, slaveAbs)
|
||||
same, err := fsops.SameContent(masterAbs, slaveAbs)
|
||||
switch {
|
||||
case err != nil:
|
||||
m.Status, m.Note = store.StatusError, "compare failed: "+err.Error()
|
||||
@@ -227,14 +227,14 @@ func (e *Engine) classify(ctx context.Context, l *libs, m *store.Match, hi, si f
|
||||
}
|
||||
|
||||
// Manual review: gather codec details for both sides.
|
||||
if unchanged && len(prev.HostProbe) > 0 && len(prev.SlaveProbe) > 0 {
|
||||
m.HostProbe, m.SlaveProbe, m.Verdict = prev.HostProbe, prev.SlaveProbe, prev.Verdict
|
||||
if unchanged && len(prev.MasterProbe) > 0 && len(prev.SlaveProbe) > 0 {
|
||||
m.MasterProbe, m.SlaveProbe, m.Verdict = prev.MasterProbe, prev.SlaveProbe, prev.Verdict
|
||||
return
|
||||
}
|
||||
hp := e.prober.Probe(ctx, filepath.Join(l.host.Path, m.RelHost))
|
||||
mp := e.prober.Probe(ctx, filepath.Join(l.master.Path, m.RelMaster))
|
||||
sp := e.prober.Probe(ctx, filepath.Join(l.slave.Path, m.RelSlave))
|
||||
v := probe.Compare(hp, sp)
|
||||
m.HostProbe, _ = json.Marshal(hp)
|
||||
v := probe.Compare(mp, sp)
|
||||
m.MasterProbe, _ = json.Marshal(mp)
|
||||
m.SlaveProbe, _ = json.Marshal(sp)
|
||||
m.Verdict, _ = json.Marshal(v)
|
||||
}
|
||||
@@ -255,9 +255,9 @@ func (e *Engine) scan(ctx context.Context, j *Job, pairID int64, trigger string)
|
||||
}
|
||||
warns := layoutWarnings(l)
|
||||
|
||||
j.phase("Indexing host library", 0)
|
||||
hostFiles := map[string]fsops.Info{}
|
||||
hs, err := walk(ctx, l, l.host, hostFiles)
|
||||
j.phase("Indexing master library", 0)
|
||||
masterFiles := map[string]fsops.Info{}
|
||||
ms, err := walk(ctx, l, l.master, masterFiles)
|
||||
if err != nil {
|
||||
_ = e.st.FinishScan(ctx, pairID, started.Unix(), err.Error(), warns)
|
||||
return err
|
||||
@@ -269,7 +269,7 @@ func (e *Engine) scan(ctx context.Context, j *Job, pairID int64, trigger string)
|
||||
_ = e.st.FinishScan(ctx, pairID, started.Unix(), err.Error(), warns)
|
||||
return err
|
||||
}
|
||||
ix := buildIndex(hostFiles)
|
||||
ix := buildIndex(masterFiles)
|
||||
prev, err := e.st.AllMatches(ctx, pairID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -284,12 +284,12 @@ func (e *Engine) scan(ctx context.Context, j *Job, pairID int64, trigger string)
|
||||
}
|
||||
si := slaveFiles[rel]
|
||||
m := store.Match{PairID: pairID, RelSlave: rel}
|
||||
m.RelHost, m.Kind = ix.find(rel)
|
||||
m.RelMaster, m.Kind = ix.find(rel)
|
||||
var pm *store.Match
|
||||
if p, ok := prev[rel]; ok {
|
||||
pm = &p
|
||||
}
|
||||
e.classify(ctx, l, &m, hostFiles[m.RelHost], si, pm)
|
||||
e.classify(ctx, l, &m, masterFiles[m.RelMaster], si, pm)
|
||||
if err := e.st.UpsertMatch(ctx, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -305,12 +305,12 @@ func (e *Engine) scan(ctx context.Context, j *Job, pairID int64, trigger string)
|
||||
// Layout warnings based on what was actually found.
|
||||
if ss.files > 0 {
|
||||
if n := kinds[store.KindElsewhere] + kinds[store.KindNone]; n*4 > ss.files {
|
||||
warns = append(warns, fmt.Sprintf("%d of %d slave files have no counterpart at the same path in the host library. "+
|
||||
warns = append(warns, fmt.Sprintf("%d of %d slave files have no counterpart at the same path in the master library. "+
|
||||
"Check that both libraries use the same folder layout.", n, ss.files))
|
||||
}
|
||||
}
|
||||
if hs.symlinks+ss.symlinks > 0 {
|
||||
warns = append(warns, fmt.Sprintf("Ignored %d symlinks (playlink never follows or replaces symlinks).", hs.symlinks+ss.symlinks))
|
||||
if ms.symlinks+ss.symlinks > 0 {
|
||||
warns = append(warns, fmt.Sprintf("Ignored %d symlinks (playlink never follows or replaces symlinks).", ms.symlinks+ss.symlinks))
|
||||
}
|
||||
if e.prober.Bin == "" {
|
||||
warns = append(warns, "ffprobe was not found, so codec and quality details are unavailable for manual review.")
|
||||
@@ -319,7 +319,7 @@ func (e *Engine) scan(ctx context.Context, j *Job, pairID int64, trigger string)
|
||||
return err
|
||||
}
|
||||
j.update(func(j *Job) {
|
||||
j.Message = fmt.Sprintf("Scanned %d host and %d slave files in %s", hs.files, ss.files, time.Since(started).Round(time.Second))
|
||||
j.Message = fmt.Sprintf("Scanned %d master and %d slave files in %s", ms.files, ss.files, time.Since(started).Round(time.Second))
|
||||
})
|
||||
|
||||
if l.pair.AutoLinkIdentical {
|
||||
@@ -367,7 +367,7 @@ func (e *Engine) linkIDs(ctx context.Context, j *Job, pairID int64, ids []int64,
|
||||
j.tick()
|
||||
continue
|
||||
}
|
||||
if settle > 0 && (m.SlaveMtime > cutoff || m.HostMtime > cutoff) {
|
||||
if settle > 0 && (m.SlaveMtime > cutoff || m.MasterMtime > cutoff) {
|
||||
j.skip(m.RelSlave + ": modified recently, waiting for it to settle")
|
||||
j.tick()
|
||||
continue
|
||||
@@ -415,24 +415,24 @@ func (e *Engine) refresh(ctx context.Context, l *libs, relSlave string) {
|
||||
if p, err := e.st.MatchBySlave(ctx, l.pair.ID, relSlave); err == nil {
|
||||
pm = &p
|
||||
}
|
||||
var hi fsops.Info
|
||||
if hp, err := l.host.Resolve(relSlave); err == nil {
|
||||
if in, err := fsops.RegularFile(hp); err == nil {
|
||||
hi = in
|
||||
m.RelHost, m.Kind = relSlave, store.KindPath
|
||||
var mi fsops.Info
|
||||
if mp, err := l.master.Resolve(relSlave); err == nil {
|
||||
if in, err := fsops.RegularFile(mp); err == nil {
|
||||
mi = in
|
||||
m.RelMaster, m.Kind = relSlave, store.KindPath
|
||||
}
|
||||
}
|
||||
if m.Kind == "" {
|
||||
if pm != nil && pm.RelHost != "" {
|
||||
if in, err := fsops.RegularFile(filepath.Join(l.host.Path, pm.RelHost)); err == nil {
|
||||
hi = in
|
||||
m.RelHost, m.Kind = pm.RelHost, pm.Kind
|
||||
if pm != nil && pm.RelMaster != "" {
|
||||
if in, err := fsops.RegularFile(filepath.Join(l.master.Path, pm.RelMaster)); err == nil {
|
||||
mi = in
|
||||
m.RelMaster, m.Kind = pm.RelMaster, pm.Kind
|
||||
}
|
||||
}
|
||||
}
|
||||
if m.Kind == "" {
|
||||
m.Kind = store.KindNone
|
||||
}
|
||||
e.classify(ctx, l, &m, hi, si, pm)
|
||||
e.classify(ctx, l, &m, mi, si, pm)
|
||||
_ = e.st.UpsertMatch(ctx, &m)
|
||||
}
|
||||
Reference in new issue
Block a user