fix(archive): Reorganize checks each step before it moves, holds a failed plan, recovers file renames safely
Owner review 2, phase 4a review findings on the renamer. - File rename recovery is safe to run again from wherever it stopped: once every file is at its temporary name the journal says phase 2, as Run does, so a second recovery (after a failure, a crash or a sidecar that could not be written) never moves a file that reached its new name. A new name something else took meanwhile is never written over: that photo and its companions go back to their old names, or to the first free _2, _3, recorded in the journal before they move, and the frame names where its photo is. A journal is never deleted while a temporary file of it is still there. - Every check that can refuse a step runs under the step's leases before anything of it moves: where its sequences are, strangers inside, its new folder taken since the plan (a restore, an upload), a sidecar of a newer format, a title edited since the plan (compare-and-set against the planned from-title, sidecar and index), and its file renames, compared by a digest of every move at the planned frames version (a verify that hashes photos does not bump the version). Once the folder moved, every index row follows it before any file is renamed, and a photo in use or files found changed keep their names: the step completes. - The same checks run up front for a held plan, so its retry either fits or is given up. - A failed move job holds its plan, as a pause does: an archive open no longer applies the rest unasked; only the job's retry does. File renames a step left half-way are still completed at an archive open. - Each sequence changed is told as its step is journaled, so a pause or a failure never keeps it untold; the plan's journal keeps the tally of every run, so the job log counts the whole plan. - A selected sequence deleted while the job waited is named in the log and the plan refused with the localized stale-plan error, not a raw ArgumentException. - The dry run renames no file of a newer-format sidecar. New log lines and error (en, it): title and file names kept, file names kept because a photo was in use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 1d1c888120edd709dbb5341c1922bf85085c4857)
This commit is contained in:
1 parent
44e5b9da9e
commit
13c2ac321c
10 files changed
+961
-135
No files matched your search
@@ -261,4 +261,110 @@ public sealed class ReorganizeTests(TestContext context)
|
||||
Assert.FileMissing(sequence.File("originals/4.jpg"));
|
||||
Assert.True(journal.Files.All(f => !File.Exists(sequence.File(f.Temp))));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A rename of <paramref name="shape"/> (a chain of names that free up, a swap, a cycle of three) a crash interrupted
|
||||
/// in its first phase, once its first file reached its temporary name; each file holds its frame's id.
|
||||
/// </summary>
|
||||
private static (SequenceManifest Manifest, FileRenameJournal Journal) Interrupted(SequencePaths sequence, string shape)
|
||||
{
|
||||
string[][] names = shape switch
|
||||
{
|
||||
"chain" => [["originals/2.jpg", "originals/3.jpg", "originals/4.jpg"], ["originals/1.jpg", "originals/2.jpg", "originals/3.jpg"]],
|
||||
"swap" => [["originals/a.jpg", "originals/b.jpg"], ["originals/b.jpg", "originals/a.jpg"]],
|
||||
_ => [["originals/a.jpg", "originals/b.jpg", "originals/c.jpg"], ["originals/b.jpg", "originals/c.jpg", "originals/a.jpg"]],
|
||||
};
|
||||
var (from, to) = (names[0], names[1]);
|
||||
var manifest = Sequence(from);
|
||||
foreach (var f in manifest.Frames) Write(sequence, f.File, f.Id.ToString());
|
||||
var journal = FileRenameJournal.Create(manifest.Versions.Frames, [.. manifest.Frames.Select((f, i) => new FileRename(f.Id, f.File, to[i]))], "t4");
|
||||
FileRenameJournal.Write(sequence, journal);
|
||||
File.Move(sequence.File(journal.Files[0].From), sequence.File(journal.Files[0].Temp));
|
||||
return (manifest, journal);
|
||||
}
|
||||
|
||||
private static void AssertEveryFrameHoldsItsPhoto(SequencePaths sequence, SequenceManifest recovered)
|
||||
{
|
||||
foreach (var f in recovered.Frames) Assert.Equal(f.Id.ToString(), Read(sequence, f.File));
|
||||
Assert.Empty(Directory.EnumerateFiles(sequence.Originals, ".chronos-rename-*"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case("chain")]
|
||||
[Case("swap")]
|
||||
[Case("cycle")]
|
||||
public void ARecoveryRunAgainLeavesEveryFileWithItsFrame(string shape)
|
||||
{
|
||||
var sequence = Folder(out _);
|
||||
var (manifest, _) = Interrupted(sequence, shape);
|
||||
// Completed, but the sidecar that follows was never written (a full disk, a crash before it): the journal is found again.
|
||||
var first = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
||||
Assert.Equal(2, Assert.NotNull(FileRenameJournal.Read(sequence)).Phase);
|
||||
var again = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
||||
Assert.SequenceEqual(first.Frames.Select(f => f.File), again.Frames.Select(f => f.File));
|
||||
Assert.False(manifest.Frames.Select(f => f.File).SequenceEqual(again.Frames.Select(f => f.File)), "every frame has its new name");
|
||||
AssertEveryFrameHoldsItsPhoto(sequence, again);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[WindowsOnly]
|
||||
public void ARecoveryCutShortWhileFilesTakeTheirNewNamesGoesOnFromThere()
|
||||
{
|
||||
var sequence = Folder(out _);
|
||||
var (manifest, journal) = Interrupted(sequence, "chain");
|
||||
// The last file reached its temporary name too, and another program holds it: the recovery stops at it.
|
||||
File.Move(sequence.File(journal.Files[2].From), sequence.File(journal.Files[2].Temp));
|
||||
using (new FileStream(sequence.File(journal.Files[2].Temp), FileMode.Open, FileAccess.Read, FileShare.None))
|
||||
Assert.Throws<IOException>(() => FileRenameJournal.Recover(sequence, manifest));
|
||||
Assert.Equal("originals/1.jpg", journal.Files[0].To);
|
||||
Assert.Equal(manifest.Frames[0].Id.ToString(), Read(sequence, "originals/1.jpg"));
|
||||
|
||||
// Run again, it goes on from there: no file that took its new name moves again.
|
||||
var recovered = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
||||
Assert.SequenceEqual(["originals/1.jpg", "originals/2.jpg", "originals/3.jpg"], recovered.Frames.Select(f => f.File));
|
||||
AssertEveryFrameHoldsItsPhoto(sequence, recovered);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ANewNameTakenMeanwhileNeverStrandsAPhoto()
|
||||
{
|
||||
var sequence = Folder(out _);
|
||||
var (manifest, journal) = Interrupted(sequence, "chain");
|
||||
// Copied in under the name the second file gets after the crash: never written over. Its own old name is the third
|
||||
// file's new one, so it takes the first free number of that name.
|
||||
Write(sequence, "originals/2.jpg", "other");
|
||||
FileRenameJournal.Delete(sequence);
|
||||
Assert.NotNull(FileRenameJournal.Read(sequence));
|
||||
|
||||
var recovered = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
|
||||
Assert.SequenceEqual(["originals/1.jpg", "originals/3_2.jpg", "originals/3.jpg"], recovered.Frames.Select(f => f.File));
|
||||
Assert.Equal("other", Read(sequence, "originals/2.jpg"));
|
||||
AssertEveryFrameHoldsItsPhoto(sequence, recovered);
|
||||
// The new name is in the journal, so a recovery run again finds every file where it is.
|
||||
Assert.Equal("originals/3_2.jpg", Assert.NotNull(FileRenameJournal.Read(sequence)).Files[1].To);
|
||||
Assert.SequenceEqual(recovered.Frames.Select(f => f.File), Assert.NotNull(FileRenameJournal.Recover(sequence, manifest)).Frames.Select(f => f.File));
|
||||
AssertEveryFrameHoldsItsPhoto(sequence, recovered);
|
||||
Assert.Equal(journal.Files.Count, Directory.EnumerateFiles(sequence.Originals).Count() - 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APhotoWhoseCompanionsNewNameIsTakenStaysWithItsCompanion()
|
||||
{
|
||||
var sequence = Folder(out _);
|
||||
var manifest = Sequence("originals/A.CR2");
|
||||
Write(sequence, "originals/A.CR2", "photo");
|
||||
Write(sequence, "originals/A.xmp", "sidecar");
|
||||
var id = manifest.Frames[0].Id;
|
||||
FileRename[] moves = [new(id, "originals/A.CR2", "originals/0001.CR2"), new(id, "originals/A.xmp", "originals/0001.xmp", Companion: true)];
|
||||
var journal = FileRenameJournal.Create(manifest.Versions.Frames, moves, "t5") with { Phase = 2 };
|
||||
FileRenameJournal.Write(sequence, journal);
|
||||
foreach (var f in journal.Files) File.Move(sequence.File(f.From), sequence.File(f.Temp));
|
||||
Write(sequence, "originals/0001.xmp", "other");
|
||||
|
||||
// Both go back to their names, together; the frame keeps its file.
|
||||
Assert.Null(FileRenameJournal.Recover(sequence, manifest));
|
||||
Assert.Equal(("photo", "sidecar", "other"), (Read(sequence, "originals/A.CR2"), Read(sequence, "originals/A.xmp"), Read(sequence, "originals/0001.xmp")));
|
||||
Assert.FileMissing(sequence.File("originals/0001.CR2"));
|
||||
Assert.Empty(Directory.EnumerateFiles(sequence.Originals, ".chronos-rename-*"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user