fix(copies): a changed photo no longer wedges a merge's resumed verify
A merge's Verify stage journals a second version before filing it. When the copy then failed or read other bytes (the file rewritten by an editor or a sync tool, a drive dropping out before an edit), every retry read new bytes for that journalled frame and stopped with changed, keeping the journal, so the merge failed the same way until cancelled. - When the kept sequence does not hold the journalled bytes, nothing was filed: the frame leaves SecondVersions and VersionShas and is verified as on a first visit, which files the photo as it is now. - When it holds them (filed, then the exclusion failed), they are left out of Main and the merge stops and drops its journal, so the next run plans again instead of a cancel leaving them in Main for good. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6db4c2e80c
commit
65b7cdf91b
5 files changed
+115
-3
No files matched your search
@@ -633,6 +633,82 @@ public abstract class SequenceMergerTests(TestContext context) : DatabaseTest(co
|
||||
AssertVersions(await new CopySides(fx.Db).VersionsAsync(keep.Id, Ct), Sha(mine, 0), Sha(theirs, 0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionWhoseFileChangedBeforeItWasFiledIsFiledAsItIsOnResume()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (mine, theirs) = EqualSizeSeeds(0);
|
||||
var edited = EqualSizeSeed(0, theirs, mine);
|
||||
var keep = await SequenceAsync(fx, "keep", [(mine, 0, "IMG_0001.JPG")]);
|
||||
var drop = await SequenceAsync(fx, "drop", [(theirs, 0, "IMG_0001.JPG")]);
|
||||
await new DbAccess(fx.Db.Database).ExecuteAsync("UPDATE frames SET sha256 = NULL WHERE sequence_id = $1 OR sequence_id = $2", [keep.Id, drop.Id], Ct);
|
||||
var path = fx.Paths.FromRelative((await fx.Db.Frames.ListBySequenceAsync(drop.Id)).Single().RelativePath);
|
||||
var copy = await PairAsync(fx, keep.Id, drop.Id);
|
||||
var merger = Merger(fx);
|
||||
var (engine, handler) = Engine(fx, merger, new FakeProjects(), new FakeWorkspace());
|
||||
await engine.StartAsync(Ct);
|
||||
// The file is rewritten (an editor saving a rotation) between its hash and its copy: the copy reads other bytes.
|
||||
var rewritten = false;
|
||||
handler.BeforeFileVersion = _ =>
|
||||
{
|
||||
if (rewritten) return Task.CompletedTask;
|
||||
rewritten = true;
|
||||
File.WriteAllBytes(path, Photos.Jpeg(Start, edited));
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
var plan = await merger.PlanAsync(copy.Id, keep.Id, drop.Id, await ChoicesAsync(fx, keep.Id, drop.Id, CopyPhotoPolicy.Union), NoContext, Ct);
|
||||
var job = await merger.EnqueueAsync(engine, new FoundCopyStore(fx.Db), plan, keep.DisplayTitle, Ct);
|
||||
await fx.WaitForAsync(job.Id, JobState.Failed);
|
||||
Assert.True((await HashesAsync(fx, keep.Id)).SetEquals([Sha(mine, 0)]), "nothing was filed");
|
||||
|
||||
await engine.ResumeAsync(job.Id, Ct);
|
||||
await fx.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
// Nothing was filed: the retry files the photo as it is now, as a second version left out of Main.
|
||||
Assert.Null(await fx.Db.Sequences.GetAsync(drop.Id));
|
||||
Assert.True((await HashesAsync(fx, keep.Id)).SetEquals([Sha(mine, 0), Sha(edited, 0)]));
|
||||
AssertVersions(await new CopySides(fx.Db).VersionsAsync(keep.Id, Ct), Sha(mine, 0), Sha(edited, 0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionFiledBeforeItsFileChangedIsLeftOutOfMainAndTheMergePlannedAgain()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (mine, theirs) = EqualSizeSeeds(0);
|
||||
var edited = EqualSizeSeed(0, theirs, mine);
|
||||
var keep = await SequenceAsync(fx, "keep", [(mine, 0, "IMG_0001.JPG")]);
|
||||
var drop = await SequenceAsync(fx, "drop", [(theirs, 0, "IMG_0001.JPG")]);
|
||||
await new DbAccess(fx.Db.Database).ExecuteAsync("UPDATE frames SET sha256 = NULL WHERE sequence_id = $1 OR sequence_id = $2", [keep.Id, drop.Id], Ct);
|
||||
var dropFrame = (await fx.Db.Frames.ListBySequenceAsync(drop.Id)).Single();
|
||||
var path = fx.Paths.FromRelative(dropFrame.RelativePath);
|
||||
var copy = await PairAsync(fx, keep.Id, drop.Id);
|
||||
var merger = Merger(fx);
|
||||
var (engine, handler) = Engine(fx, merger, new FakeProjects(), new FakeWorkspace());
|
||||
await engine.StartAsync(Ct);
|
||||
// Filed at the verify, then the exclusion fails; the file is rewritten before the retry.
|
||||
var failedOnce = false;
|
||||
handler.BeforeLeaveOutOfMain = _ =>
|
||||
{
|
||||
if (failedOnce) return Task.CompletedTask;
|
||||
failedOnce = true;
|
||||
File.WriteAllBytes(path, Photos.Jpeg(Start, edited));
|
||||
throw new IOException("project.json is in use");
|
||||
};
|
||||
var plan = await merger.PlanAsync(copy.Id, keep.Id, drop.Id, await ChoicesAsync(fx, keep.Id, drop.Id, CopyPhotoPolicy.Union), NoContext, Ct);
|
||||
var job = await merger.EnqueueAsync(engine, new FoundCopyStore(fx.Db), plan, keep.DisplayTitle, Ct);
|
||||
await fx.WaitForAsync(job.Id, JobState.Failed);
|
||||
Assert.True((await HashesAsync(fx, keep.Id)).SetEquals([Sha(mine, 0), Sha(theirs, 0)]));
|
||||
|
||||
await engine.ResumeAsync(job.Id, Ct);
|
||||
var failed = await fx.WaitForAsync(job.Id, JobState.Failed);
|
||||
|
||||
// The filed version is left out of Main, the other stays, and the journal goes: the next run plans again.
|
||||
Assert.Equal(Chronos.Core.Localization.Loc.L("Errors.Copies.ChangedFile", dropFrame.FileName), failed.Error);
|
||||
Assert.NotNull(await fx.Db.Sequences.GetAsync(drop.Id));
|
||||
AssertVersions(await new CopySides(fx.Db).VersionsAsync(keep.Id, Ct), Sha(mine, 0), Sha(theirs, 0));
|
||||
Assert.False(File.Exists(MergeJournal.PathFor(fx.Paths, job.Id)), "the next run plans again");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASecondVersionCopiedInByAMergeAbandonedBeforeItsDetailsStaysOutOfMain()
|
||||
{
|
||||
@@ -743,6 +819,14 @@ public abstract class SequenceMergerTests(TestContext context) : DatabaseTest(co
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A seed other than <paramref name="like"/> and <paramref name="not"/> whose photo at <paramref name="ms"/> has the same size as <paramref name="like"/>'s.</summary>
|
||||
private static int EqualSizeSeed(int ms, int like, params int[] not)
|
||||
{
|
||||
var length = Photos.Jpeg(Start.AddMilliseconds(ms), like).Length;
|
||||
for (var seed = 1; ; seed++)
|
||||
if (seed != like && !not.Contains(seed) && Photos.Jpeg(Start.AddMilliseconds(ms), seed).Length == length) return seed;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnUnhashedPhotoOfTheKeptOneWithTheSameContentIsNoLastCopy()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user