fix(archive): crash-safe photo restore, hidden half deletions, tombstones for every photo

The review of owner review 2 phase 3 found that the photo trash could destroy or
strand originals outside the Empty job and let deleted photos come back.

- A restore into a new sequence stages each original inside its own entry
  (<entry>/.restoring), never in a staging folder other restores share or
  maintenance sweeps; any failure moves it back, the commit runs to its end once
  the file has left, a cancel stops between photos with the record kept true,
  and the next restore of the entry puts back what a crash left staged. Nothing
  erases a staging folder any more.
- A restore into an existing sequence writes .chronos/restore.pending.json before
  its first move; SequenceWriter.ReadLocked settles it (files back into the entry
  before the sidecar was written, record rewritten after), cancellation is
  checked once before the journal, and a companion that cannot follow puts its
  photo back whole. Only an intact frame keeps a trashed copy out.
- A deletion's record keeps a .pending name until its sidecar is written, so the
  trash neither lists, restores nor empties half an entry; a failed sidecar
  write undoes the moves, and scans settle any of the three journals.
- Photos never hashed are hashed as they go and get their tombstone; a photo
  with neither hash nor original blocks the plan (FrameDeleteBlock.Unhashable).
  Sidecars rebuilt from the photos, and sequences restored without one, keep
  their tombstones; the index drops a tombstone whose content a frame holds.
- Emptying re-reads and re-measures each entry under its lease before erasing.
- Once the sidecar is written a deletion or restore stands when the index cannot
  follow (logged; the next scan resyncs it).
- XMP companions are listed once per directory, case-insensitively; a restore
  into a new sequence picks a name its companions can follow, and one that still
  cannot is kept in the record as a leftover.
- thumbs.pack is re-keyed with the frames on delete and restore, so the queued
  pass reuses the thumbnails; the server checks the fingerprint of thumbnails of
  frames never hashed too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 0bb631dd83d082e2569b98d127d827adc2e7f899)
This commit is contained in:
Alby96 committed 2026-10-27 12:45:33 +01:00
1 parent 6ea611d949
commit eb607d47aa
16 files changed
+1524 -316

No files matched your search

@@ -33,6 +33,9 @@ public static class ArchiveLayout
/// <summary>Moves of a photo deletion into the trash that were started but not yet recorded in <see cref="SequenceManifestFile"/>.</summary>
public const string RemovalJournalFile = "remove.pending.json";
/// <summary>Moves of photos restored from the trash that were started but not yet settled in <see cref="SequenceManifestFile"/> and the trash record.</summary>
public const string RestoreJournalFile = "restore.pending.json";
/// <summary>Folder names inside a sequence folder that never hold a sequence of their own.</summary>
public static bool IsReservedFolder(string name) =>
name.Equals(MetaFolder, StringComparison.OrdinalIgnoreCase)
@@ -100,6 +103,7 @@ public sealed class SequencePaths
public string ThumbsPackPath => Path.Combine(MetaDirectory, ArchiveLayout.ThumbsPackFile);
public string IngestJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.IngestJournalFile);
public string RemovalJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.RemovalJournalFile);
public string RestoreJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.RestoreJournalFile);
public string Proxy(int index) => Path.Combine(Proxies, ArchiveLayout.ProxyFileName(index));
@@ -13,12 +13,17 @@ public sealed record FrameRemovalMove(string From, string To);
/// Write-ahead record of a photo deletion (<c>.chronos/remove.pending.json</c>, owner review 2, R6): the trash entry the
/// photos move into, the frames leaving the sidecar and every file move. Written before the first move and removed once
/// <c>sequence.json</c> no longer lists the frames, so a crash in between is undone by <see cref="Recover"/>: the files go
/// back to their places, the entry's record and folder go, and the sequence is as it was.
/// back to their places, the entry's record and folder go, and the sequence is as it was. Until the sidecar is written
/// the entry's record has its <see cref="PendingRecord"/> name, which the trash never lists, so half an entry can be
/// neither restored nor emptied.
/// </summary>
/// <param name="Record">The entry's record, relative to the archive root.</param>
/// <param name="Record">The entry's record, relative to the archive root (its name once the deletion is complete).</param>
/// <param name="Folder">The entry's folder, relative to the archive root.</param>
public sealed record FrameRemovalJournal(string Record, string Folder, IReadOnlyList<Guid> Frames, IReadOnlyList<FrameRemovalMove> Moves)
{
/// <summary>The name a deletion's record has until its sidecar is written.</summary>
public static string PendingRecord(string record) => record + ".pending";
public static void Write(SequencePaths sequence, FrameRemovalJournal journal)
{
ArgumentNullException.ThrowIfNull(sequence);
@@ -35,8 +40,9 @@ public sealed record FrameRemovalJournal(string Record, string Folder, IReadOnly
/// <summary>
/// Undoes a deletion a crash interrupted before its sidecar was written: while <paramref name="manifest"/> still lists a
/// frame of the journal, each file that reached the trash moves back (one whose place is taken again stays in the trash,
/// logged, and so does the entry then), and the entry's record and its emptied folder go. A deletion whose sidecar was
/// written is complete: only the journal goes. Returns whether a deletion was undone. The caller holds the lease.
/// logged, and so does the entry then, listed under its record's own name), and the entry's record and its emptied
/// folder go. A deletion whose sidecar was written is complete: its record takes its own name, and the journal goes.
/// Returns whether a deletion was undone. The caller holds the lease.
/// </summary>
public static bool Recover(SequencePaths sequence, SequenceManifest manifest)
{
@@ -53,9 +59,9 @@ public sealed record FrameRemovalJournal(string Record, string Folder, IReadOnly
journal = null; // torn while being written: nothing was moved yet
}
var undone = false;
var archive = sequence.Archive;
if (journal is not null && manifest.Frames.Any(f => journal.Frames.Contains(f.Id)))
{
var archive = sequence.Archive;
var kept = 0;
foreach (var move in journal.Moves)
{
@@ -71,19 +77,36 @@ public sealed record FrameRemovalJournal(string Record, string Folder, IReadOnly
Directory.CreateDirectory(Path.GetDirectoryName(place)!);
File.Move(trashed, place, overwrite: false);
}
var record = archive.FromRelative(journal.Record);
if (kept == 0)
{
var record = archive.FromRelative(journal.Record);
if (File.Exists(record)) File.Delete(record);
if (File.Exists(PendingRecord(record))) File.Delete(PendingRecord(record));
RemoveEmpty(archive.FromRelative(journal.Folder));
}
else
{
// What stays in the trash is listed, restorable and erasable like any entry.
Publish(record);
}
Log.Warning("archive", $"An interrupted photo deletion in {sequence.RelativeFolder} was undone");
undone = true;
}
else if (journal is not null)
{
Publish(archive.FromRelative(journal.Record));
}
Delete(sequence);
return undone;
}
/// <summary>Gives a deletion's record its own name (<see cref="PendingRecord"/>), once its photos are out of the sidecar.</summary>
public static void Publish(string record)
{
var pending = PendingRecord(record);
if (File.Exists(pending) && !File.Exists(record)) File.Move(pending, record, overwrite: false);
}
/// <summary>Removes <paramref name="folder"/> and its subfolders when they hold no file.</summary>
public static void RemoveEmpty(string folder)
{
+24
View File
@@ -346,6 +346,30 @@ public sealed class ThumbPackWriter : IDisposable
_dirty = true;
}
/// <summary>
/// Moves each thumbnail to the frame index <paramref name="indices"/> maps its own to (old → new) and drops those it
/// does not map (frames removed), then commits: only a new index is written, the thumbnails stay where they are. Their
/// entry headers keep the old index, so a pack whose index a crash cut off reads back under those, where each one's
/// source fingerprint (<see cref="ThumbPack.IsFrom"/>) still tells it from the frame now at that index.
/// </summary>
public void Rekey(IReadOnlyDictionary<int, int> indices)
{
ArgumentNullException.ThrowIfNull(indices);
if (_entries.All(e => indices.TryGetValue(e.Key, out var to) && to == e.Key)) return;
var moved = new List<ThumbEntry>(_entries.Count);
foreach (var (index, entry) in _entries)
{
if (indices.TryGetValue(index, out var to)) moved.Add(entry with { FrameIndex = to });
}
_entries.Clear();
foreach (var entry in moved) _entries[entry.FrameIndex] = entry;
// The new index replaces the last committed one at the end of the pack.
if (_indexOffset is { } offset) _stream.SetLength(offset);
_indexOffset = null;
_dirty = true;
Commit();
}
/// <summary>Writes the index and footer and flushes to disk.</summary>
public void Commit()
{