From eb607d47aa40cac61e7e890c9150e59bce2d562d Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 27 Oct 2026 12:23:04 +0100 Subject: [PATCH] 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 (/.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 (cherry picked from commit 0bb631dd83d082e2569b98d127d827adc2e7f899) --- docs/ARCHITECTURE.md | 2 +- src/Chronos.Core/Archiving/ArchiveLayout.cs | 4 + .../Archiving/FrameRemovalJournal.cs | 35 +- src/Chronos.Core/Archiving/ThumbPack.cs | 24 + src/Chronos.Data/Archiving/ArchiveIndexer.cs | 10 +- src/Chronos.Data/Archiving/ArchiveIngest.cs | 2 +- .../Archiving/FrameTrash.Restore.cs | 545 +++++++++++++++++ src/Chronos.Data/Archiving/FrameTrash.cs | 524 ++++++++--------- src/Chronos.Data/Archiving/SequenceMapping.cs | 21 +- .../Archiving/SequenceTrash.Empty.cs | 30 +- src/Chronos.Data/Archiving/SequenceTrash.cs | 21 +- src/Chronos.Data/Archiving/SequenceWriter.cs | 6 +- .../Storage/ArchiveAssetLocator.cs | 3 +- .../Archiving/ThumbPackTests.cs | 35 ++ .../Archiving/FrameTrashTests.cs | 548 ++++++++++++++++++ .../Storage/ArchiveModuleTests.cs | 30 + 16 files changed, 1524 insertions(+), 316 deletions(-) create mode 100644 src/Chronos.Data/Archiving/FrameTrash.Restore.cs diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index f8708158..5ec84a9a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -58,7 +58,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S | `Jobs` | `JobEngine` (one job at a time; reviews the priority queue and makes a lower running job yield, requeued with its checkpoint; no history purge, the housekeeping schedule's clean-up does it), `JobPriorityQueue` (a level per job kind, the claim order, settings round trip), `IJobHandler`, `IJobHandlerProvider`, `JobContext` (progress, checkpoints, follow-up jobs that never interrupt their parent), `JobThroughput`/`JobEta` (rate and bounded time left) | | `Scheduling` | `Scheduler` (one per host: seeds the host's system schedules, claims due runs by compare-and-set, catch-up, clock changes, create/edit/reset/delete/run now as API v1 DTOs), `ScheduleRecurrence` (the in-house `every:`/`daily:`/`weekly:`/`monthly:` form, wall-clock times across DST), `ScheduleActions` (action names, parameters, availability, the archive jobs a run queues), `IScheduleHost` and `SystemSchedule` (what the server and the desktop supply), `ScheduleOutcome`, `ScheduleMapping` (model ↔ DTO) | | `Housekeeping` | `BackupJobHandler`, `CleanupJobHandler`, `ICleanupStep` with the shared steps (`CleanupSteps`: job history, audit log, trash report that never deletes); hosts add their own steps through `ArchiveServiceOptions.CleanupSteps` | -| `Archiving` | `ArchiveService` (create/adopt/open, archive settings written to `archive.json` then the index), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages` (thumbnail and proxy passes, several photos at once through `OrderedWork`, which hands results back in frame order), `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `GprCompanions` (the JPEG of each GoPro GPR, found in the index when an import kept it in another sequence), `ArchiveMaintenance`, `MassRenamer`, `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails; lists sequence and photo entries; `SequenceTrash.Empty`: the emptying's dry run, fingerprint and erase), `FrameTrash` (photos of a sequence into `trash/photos` with a dry run, a journal that undoes an interrupted deletion, tombstones, positions parked and placed back, proxies renamed to follow their frames; restore re-hashes each file, into a new sequence through the ingest when theirs is gone), job handlers | +| `Archiving` | `ArchiveService` (create/adopt/open, archive settings written to `archive.json` then the index), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages` (thumbnail and proxy passes, several photos at once through `OrderedWork`, which hands results back in frame order), `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `GprCompanions` (the JPEG of each GoPro GPR, found in the index when an import kept it in another sequence), `ArchiveMaintenance`, `MassRenamer`, `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails; lists sequence and photo entries; `SequenceTrash.Empty`: the emptying's dry run, fingerprint and erase, each entry checked against its fingerprint again under its lease), `FrameTrash` (photos of a sequence into `trash/photos` with a dry run, a journal that undoes an interrupted deletion whose entry stays unlisted until its sidecar is written, tombstones (a photo never hashed is hashed as it goes), positions parked and placed back, proxies and thumbnails moved to follow their frames; `FrameTrash.Restore`: restore re-hashes each file, under a journal of its own into the sequence, into a new sequence through the ingest when theirs is gone, each original staged inside its own entry), job handlers | | `Processing` | `ProcessingService`, `SequenceAnalyser`, `DeepReports` (a saved deep report while it still describes the frames), `SequenceRender`, `ProjectStore`, `ProjectBundles` (a `.chronos` file for a sequence, and the sequence a file names), `RenderNodeClient`, job handlers | | `Sync` | `SyncService` (sync table of one server: compare, link by content, settle conflicts, enqueue transfers), `SyncRules` (status, settings merge and differences, no I/O), `SyncTombstones` (photos either side removed are not offered back, a replaced photo counts as its replacement), `SyncPositions` (the settings' frame positions translated by photo to the frames of the copy they go to), `SyncLinkStore` (`sync_links`, `sync_state`), `SyncTransfers` and `SyncJobHandler` (resumable, hash-verified uploads and downloads as jobs; downloads commit through the archive ingest), `SyncAgent` (background comparison, server events, backoff, offline proxies), `ProxyCache` and `RemoteSequence` (server sequences opened with proxies), `ISyncRemote` (the server calls; `ChronosSyncRemote` over `ChronosClient`) | diff --git a/src/Chronos.Core/Archiving/ArchiveLayout.cs b/src/Chronos.Core/Archiving/ArchiveLayout.cs index ec1e2dc3..3b4a01f1 100644 --- a/src/Chronos.Core/Archiving/ArchiveLayout.cs +++ b/src/Chronos.Core/Archiving/ArchiveLayout.cs @@ -33,6 +33,9 @@ public static class ArchiveLayout /// Moves of a photo deletion into the trash that were started but not yet recorded in . public const string RemovalJournalFile = "remove.pending.json"; + /// Moves of photos restored from the trash that were started but not yet settled in and the trash record. + public const string RestoreJournalFile = "restore.pending.json"; + /// Folder names inside a sequence folder that never hold a sequence of their own. 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)); diff --git a/src/Chronos.Core/Archiving/FrameRemovalJournal.cs b/src/Chronos.Core/Archiving/FrameRemovalJournal.cs index fe307720..95c4b36b 100644 --- a/src/Chronos.Core/Archiving/FrameRemovalJournal.cs +++ b/src/Chronos.Core/Archiving/FrameRemovalJournal.cs @@ -13,12 +13,17 @@ public sealed record FrameRemovalMove(string From, string To); /// Write-ahead record of a photo deletion (.chronos/remove.pending.json, 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 /// sequence.json no longer lists the frames, so a crash in between is undone by : 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 name, which the trash never lists, so half an entry can be +/// neither restored nor emptied. /// -/// The entry's record, relative to the archive root. +/// The entry's record, relative to the archive root (its name once the deletion is complete). /// The entry's folder, relative to the archive root. public sealed record FrameRemovalJournal(string Record, string Folder, IReadOnlyList Frames, IReadOnlyList Moves) { + /// The name a deletion's record has until its sidecar is written. + 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 /// /// Undoes a deletion a crash interrupted before its sidecar was written: while 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. /// 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; } + /// Gives a deletion's record its own name (), once its photos are out of the sidecar. + public static void Publish(string record) + { + var pending = PendingRecord(record); + if (File.Exists(pending) && !File.Exists(record)) File.Move(pending, record, overwrite: false); + } + /// Removes and its subfolders when they hold no file. public static void RemoveEmpty(string folder) { diff --git a/src/Chronos.Core/Archiving/ThumbPack.cs b/src/Chronos.Core/Archiving/ThumbPack.cs index d208a178..cf226f37 100644 --- a/src/Chronos.Core/Archiving/ThumbPack.cs +++ b/src/Chronos.Core/Archiving/ThumbPack.cs @@ -346,6 +346,30 @@ public sealed class ThumbPackWriter : IDisposable _dirty = true; } + /// + /// Moves each thumbnail to the frame index 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 () still tells it from the frame now at that index. + /// + public void Rekey(IReadOnlyDictionary indices) + { + ArgumentNullException.ThrowIfNull(indices); + if (_entries.All(e => indices.TryGetValue(e.Key, out var to) && to == e.Key)) return; + var moved = new List(_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(); + } + /// Writes the index and footer and flushes to disk. public void Commit() { diff --git a/src/Chronos.Data/Archiving/ArchiveIndexer.cs b/src/Chronos.Data/Archiving/ArchiveIndexer.cs index e5fae2a7..2cb7300c 100644 --- a/src/Chronos.Data/Archiving/ArchiveIndexer.cs +++ b/src/Chronos.Data/Archiving/ArchiveIndexer.cs @@ -327,7 +327,8 @@ public sealed class ArchiveIndexer ManifestStore.WriteSequence(sequence.ManifestPath, manifest); bytes = null; } - if (reconcile || File.Exists(sequence.IngestJournalPath)) + // A journal of an ingest, a photo deletion or a restore a crash interrupted is settled under the lease first (ReadLocked). + if (reconcile || File.Exists(sequence.IngestJournalPath) || File.Exists(sequence.RemovalJournalPath) || File.Exists(sequence.RestoreJournalPath)) { // Photos added, gone or back clear what covered the frames, and the scan's follow-ups make it again. manifest = await UpdateAndSyncAsync(archive, paths, sequence, manifest.Id, m => Reconcile(sequence, m, cancellationToken), cancellationToken) @@ -414,12 +415,17 @@ public sealed class ArchiveIndexer return rows.Any(row => !listed.TryGetValue(row.Index, out var id) || id != row.FrameId); } - /// A sidecar for a folder that has none; ids of a sequence the index already knew at this folder are kept. + /// + /// A sidecar for a folder that has none; ids and tombstones of a sequence the index already knew at this folder are + /// kept, so a photo deleted from it is not offered back once the index is synced from the new sidecar. + /// private async Task AdoptAsync(SequencePaths sequence, Locations locations, CancellationToken cancellationToken) { var known = locations.AtFolder(sequence.RelativeFolder); var manifest = await WithKnownFrameIdsAsync(SequenceAdopter.Build(known ?? Guid.CreateVersion7(), sequence, _writer.Options.Owner, cancellationToken), sequence, cancellationToken).ConfigureAwait(false); + if (known is { } id && await _db.FrameRemovals.ListBySequenceAsync(id, cancellationToken).ConfigureAwait(false) is { Count: > 0 } removals) + manifest = manifest with { Removed = SequenceMapping.ToTombstones(removals) }; Directory.CreateDirectory(sequence.MetaDirectory); // Another process may adopt the same folder at the same moment: the first sidecar wins. return ManifestStore.TryCreateSequence(sequence.ManifestPath, manifest) ? manifest : ManifestStore.ReadSequence(sequence.ManifestPath); diff --git a/src/Chronos.Data/Archiving/ArchiveIngest.cs b/src/Chronos.Data/Archiving/ArchiveIngest.cs index bf3a2824..21f60bf9 100644 --- a/src/Chronos.Data/Archiving/ArchiveIngest.cs +++ b/src/Chronos.Data/Archiving/ArchiveIngest.cs @@ -436,7 +436,7 @@ public sealed class ArchiveIngest : IArchiveIngest } /// The frame still holds its content: not flagged missing or corrupt, and its file is there with its size. - private static bool IsIntact(SequencePaths sequence, SidecarFrame frame) + internal static bool IsIntact(SequencePaths sequence, SidecarFrame frame) { if ((frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0) return false; var file = new FileInfo(sequence.File(frame.File)); diff --git a/src/Chronos.Data/Archiving/FrameTrash.Restore.cs b/src/Chronos.Data/Archiving/FrameTrash.Restore.cs new file mode 100644 index 00000000..e7959759 --- /dev/null +++ b/src/Chronos.Data/Archiving/FrameTrash.Restore.cs @@ -0,0 +1,545 @@ +using System.Globalization; +using System.Runtime.ExceptionServices; +using System.Text.Json; +using Chronos.Core.Archiving; +using Chronos.Core.Diagnostics; +using Chronos.Core.IO; +using Chronos.Core.Metadata; +using Chronos.Core.Model; +using Chronos.Data.Jobs; +using SequenceFrames = Chronos.Core.Archiving.SequenceFrames; + +namespace Chronos.Data.Archiving; + +public sealed partial class FrameTrash +{ + /// + /// Folder inside a photo entry where a restore into a new sequence stages each original for the ingest: never a folder + /// another restore shares, never swept by maintenance (it is not the archive's staging), and emptied back into the entry + /// by the next restore of the entry when a crash left something there. + /// + private const string RestoringFolder = ".restoring"; + + /// + /// Brings the photos of a trash entry back. Each file is re-hashed against its record: a file that no longer matches + /// (or is missing) stays in the trash with its photo, which is reported, as is a photo whose content an intact frame of + /// the sequence holds again. The others move back to their places (a name taken since, or one of its companions', + /// gets _2, _3…) with the same frame records, inserted by capture time, and their tombstones go; a + /// journal () settles a restore a crash interrupted, and once the first file moves + /// the restore runs to its end whatever the caller cancels. When the sequence is gone (deleted, or never indexed in + /// this archive), they go into a new sequence through the archive ingest, titled and placed as theirs was; a cancel + /// there stops before the next photo, with the record kept true. The entry goes once nothing is left in it. + /// + /// The entry's (photos-…). + /// The entry is not in the trash. + public async Task RestoreAsync(Archive archive, string name, JobEngine? followUps = null, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(archive); + ArgumentException.ThrowIfNullOrWhiteSpace(name); + var paths = new ArchivePaths(archive.RootPath); + ArchiveVolume.EnsureWritable(paths); + var entryFolder = Path.Combine(Directory(paths), DiskName(name)); + var recordPath = entryFolder + RecordExtension; + using (await LeaseFile.AcquireAsync(paths.LocksDirectory, SequenceTrash.EntryLease(name), _writer.Options.Owner, _writer.Options.LeaseWait, + _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false)) + { + if (!System.IO.Directory.Exists(entryFolder) || ReadRecord(recordPath) is not { } listed) + throw new DirectoryNotFoundException($"{name} is not in the trash of {archive.Name}."); + var sequence = await _db.Sequences.GetAsync(listed.SequenceId, cancellationToken).ConfigureAwait(false); + var into = sequence is not null && sequence.ArchiveId == archive.Id ? sequence : null; + // What an earlier restore a crash interrupted left half-way is settled first, so the record read next is true. + Unstage(entryFolder); + if (into is not null && File.Exists(paths.Sequence(into.Folder).RestoreJournalPath)) + await _writer.UpdateAsync(paths, into.Id, into.Folder, _ => null, cancellationToken).ConfigureAwait(false); + if (!System.IO.Directory.Exists(entryFolder) || ReadRecord(recordPath) is not { } record) + throw new DirectoryNotFoundException($"{name} is not in the trash of {archive.Name}."); + var (intact, kept) = await CheckAsync(entryFolder, record, cancellationToken).ConfigureAwait(false); + // Nothing that matches its record: the entry stays as it is. + if (intact.Count == 0) throw new InvalidDataException(Core.Localization.Loc.L("Archive.Error.TrashMismatch")); + var had = into?.Status ?? SequenceStatus.None; + PhotoRestoreResult result; + List stays; + var stopped = false; + if (into is not null) + { + // The record is settled under the sequence's lease, with the sidecar. + (result, var left) = await IntoSequenceAsync(archive, paths, into, recordPath, entryFolder, intact, cancellationToken).ConfigureAwait(false); + stays = [.. kept, .. left]; + } + else + { + var restore = await IntoNewSequenceAsync(archive, paths, record, entryFolder, intact, cancellationToken).ConfigureAwait(false); + stays = [.. kept, .. restore.Left]; + Rewrite(recordPath, entryFolder, record with { Photos = stays, Leftovers = Merge(record.Leftovers, restore.Leftovers) }); + restore.Failure?.Throw(); + stopped = restore.Stopped; + if (stopped && restore.Restored == 0) throw new OperationCanceledException(cancellationToken); + if (restore.SequenceId is not { } id) throw new IOException(Core.Localization.Loc.L("Archive.Error.TrashMismatch")); + var row = await _db.Sequences.GetAsync(id, CancellationToken.None).ConfigureAwait(false) + ?? throw new InvalidOperationException($"The photos of {record.SequenceTitle} were restored but their sequence is not indexed."); + result = new PhotoRestoreResult(row, restore.Restored, [], true); + } + Log.Info("archive", $"{record.SequenceTitle}: {result.Restored} photo(s) restored from the trash, {stays.Count} kept there"); + var all = result with { Kept = [.. stays.Select(p => p.Frame.Original ?? Path.GetFileName(p.Frame.File))] }; + if (result.Restored > 0) await QueueAsync(followUps, archive, all.Sequence, had, CancellationToken.None).ConfigureAwait(false); + // A cancel stopped a restore into a new sequence before its last photo: those not done stay in the trash, listed. + if (stopped) throw new OperationCanceledException(cancellationToken); + return all; + } + } + + /// Each photo whose files all match their record (size and SHA-256), and those that stay in the trash. + private static async Task<(List Intact, List Kept)> CheckAsync(string entryFolder, PhotoTrashRecord record, + CancellationToken cancellationToken) + { + var intact = new List(); + var kept = new List(); + foreach (var photo in record.Photos) + { + var ok = photo.Files.Count > 0; + foreach (var file in photo.Files) + { + var path = Path.Combine(entryFolder, file.Path); + var info = new FileInfo(path); + if (!info.Exists || info.Length != file.Size + || (file.Sha256 is { } sha && !string.Equals(sha, await ContentHash.Sha256Async(path, null, cancellationToken).ConfigureAwait(false), + StringComparison.OrdinalIgnoreCase))) + { + Log.Warning("archive", $"{file.Path} of trash entry {Path.GetFileName(entryFolder)} does not match its record; it stays in the trash"); + ok = false; + break; + } + } + (ok ? intact : kept).Add(photo); + } + return (intact, kept); + } + + /// + /// The photos back into their sequence, under its lease: the names chosen and the journal written before the first + /// move, then every move to its end, the sidecar, and the record settled (). Returns what came + /// back and the photos left in the trash. + /// + private async Task<(PhotoRestoreResult, List)> IntoSequenceAsync(Archive archive, ArchivePaths paths, Sequence sequence, string recordPath, + string entryFolder, List intact, CancellationToken cancellationToken) + { + var folder = paths.Sequence(sequence.Folder); + var left = new List(); + int restored; + SequenceManifest updated; + var indexed = true; + await using (await _writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) + { + var manifest = SequenceWriter.ReadLocked(folder, sequence.Id); + var inStep = await _indexer.InStepAsync(archive, folder, manifest, cancellationToken).ConfigureAwait(false); + var photosChanged = !inStep && await _indexer.PhotosChangedAsync(archive, folder, cancellationToken).ConfigureAwait(false); + var ids = manifest.Frames.Select(f => f.Id).ToHashSet(); + var holders = manifest.Frames.Where(f => f.Sha256 is not null).ToLookup(f => f.Sha256!, StringComparer.OrdinalIgnoreCase); + var taken = manifest.Frames.Select(f => f.File).ToHashSet(StringComparer.OrdinalIgnoreCase); + var planned = new List<(TrashedPhoto Photo, SidecarFrame Frame, RestoredPhoto Moves)>(); + foreach (var photo in intact) + { + // In the sequence again (restored before, or filed again since): the trash keeps its copy. Only a frame that + // still holds the content counts; a missing or damaged one leaves the trash's copy to come back. + if (ids.Contains(photo.Frame.Id) || (photo.Frame.Sha256 is { } sha && holders[sha].Any(f => ArchiveIngest.IsIntact(folder, f)))) + { + left.Add(photo); + continue; + } + var original = photo.Files[0]; + var tails = photo.Files.Skip(1).Select(c => CompanionTail(c.From, original.From)).ToList(); + var file = FreeName(folder, original.From, tails, taken); + var stem = file[..^Path.GetExtension(file).Length]; + taken.Add(file); + // A companion follows its photo's name: IMG_0001_2.xmp beside IMG_0001_2.JPG. + List moves = [new(file, paths.ToRelative(Path.Combine(entryFolder, original.Path)))]; + for (var c = 0; c < tails.Count; c++) + { + taken.Add(stem + tails[c]); + moves.Add(new FrameRemovalMove(stem + tails[c], paths.ToRelative(Path.Combine(entryFolder, photo.Files[c + 1].Path)))); + } + planned.Add((photo, photo.Frame with { File = file, Flags = photo.Frame.Flags & ~FrameFlags.Missing }, new RestoredPhoto(photo.Frame.Id, moves))); + } + if (planned.Count == 0) return (new PhotoRestoreResult(sequence, 0, [], false), left); + // Checked once: from the journal on, the restore runs to its end whatever the caller cancels meanwhile. + cancellationToken.ThrowIfCancellationRequested(); + var journal = new FrameRestoreJournal(paths.ToRelative(recordPath), paths.ToRelative(entryFolder), [.. planned.Select(p => p.Moves)]); + journal.Write(folder); + var back = new List(); + foreach (var (photo, frame, moves) in planned) + { + if (MoveIn(paths, folder, moves.Moves)) back.Add(frame); + else left.Add(photo); + } + updated = manifest; + if (back.Count > 0) + { + var returned = back.Where(f => f.Sha256 is not null).Select(f => f.Sha256!).ToHashSet(StringComparer.OrdinalIgnoreCase); + var tombstones = manifest.Removed?.Where(r => !returned.Contains(r.Sha256)).ToList(); + updated = SequenceFrames.Insert(manifest, back) with + { + Status = manifest.Status & ~Uncovered, + Removed = tombstones is { Count: > 0 } ? tombstones : null, + }; + try + { + ManifestStore.WriteSequence(folder.ManifestPath, updated); + } + catch (Exception ex) + { + // The sidecar lists none of them: their files go back into the entry, whose record is as it was. + Log.Warning("archive", $"{sequence.DisplayTitle}: the sidecar could not take the photos restored; they go back into the trash", ex); + Settle(folder, manifest, journal); + throw; + } + } + Settle(folder, updated, journal); + restored = back.Count; + if (restored > 0) + { + indexed = await IndexAsync(archive, folder, updated, photosChanged, sequence.DisplayTitle).ConfigureAwait(false); + ShiftProxies(folder, manifest.Frames, updated.Frames, []); + ShiftThumbnails(paths, folder, sequence.Id, manifest.Frames, updated.Frames); + } + } + var row = restored > 0 ? await RowAsync(sequence, updated, indexed).ConfigureAwait(false) : sequence; + return (new PhotoRestoreResult(row, restored, [], false), left); + } + + /// + /// Moves one photo's files from its entry into the sequence, the original first. When one cannot follow, those that + /// moved go back, the companions first, so the photo stays whole in the trash; an original that cannot go back either + /// stays restored, and its companions that are still in the entry then stay there (leftovers of its record). Returns + /// whether the original is in the sequence. + /// + private static bool MoveIn(ArchivePaths paths, SequencePaths folder, IReadOnlyList moves) + { + var done = 0; + try + { + for (; done < moves.Count; done++) MoveFile(paths.FromRelative(moves[done].To), folder.File(moves[done].From)); + return true; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning("archive", $"{moves[done].To} could not move back into {folder.RelativeFolder}; its photo stays in the trash", ex); + for (var i = done - 1; i >= 0; i--) + { + try + { + MoveFile(folder.File(moves[i].From), paths.FromRelative(moves[i].To)); + } + catch (Exception again) when (again is IOException or UnauthorizedAccessException) + { + Log.Error("archive", $"{folder.ArchiveRelative(moves[i].From)} could not go back into the trash; its photo stays restored", again); + return true; + } + } + return false; + } + } + + /// + /// Settles a restore into a sequence () as , the sidecar + /// read or written under the sequence's lease, has it: each photo the sidecar lists again leaves the entry's record (a + /// companion of it still in the entry stays there as one of the record's leftovers), and each file of the others that + /// reached the sequence goes back into the entry, whose record still lists it (unless the entry was emptied since: the + /// file then stays where it is, logged, for a scan to adopt). Then the journal goes. A failure (logged) keeps the + /// journal, and the next writer of the sequence settles it again. + /// + internal static void Settle(SequencePaths sequence, SequenceManifest manifest, FrameRestoreJournal journal) + { + var archive = sequence.Archive; + var recordPath = archive.FromRelative(journal.Record); + var entryFolder = archive.FromRelative(journal.Folder); + var listed = manifest.Frames.Select(f => f.Id).ToHashSet(); + try + { + var record = ReadRecord(recordPath); + var back = new HashSet(); + var leftovers = new HashSet(StringComparer.Ordinal); + foreach (var photo in journal.Photos) + { + if (listed.Contains(photo.Frame)) + { + back.Add(photo.Frame); + leftovers.UnionWith(photo.Moves.Where(m => File.Exists(archive.FromRelative(m.To))).Select(m => m.To)); + continue; + } + foreach (var move in photo.Moves) + { + var place = sequence.File(move.From); + var trashed = archive.FromRelative(move.To); + if (!File.Exists(place) || File.Exists(trashed)) continue; + if (record is null) + { + Log.Warning("archive", $"{sequence.ArchiveRelative(move.From)} stays in its sequence: its trash entry is gone"); + continue; + } + MoveFile(place, trashed); + } + } + if (record is not null && back.Count > 0) + { + var extra = record.Photos.Where(p => back.Contains(p.Frame.Id)).SelectMany(p => p.Files) + .Where(f => leftovers.Contains(archive.ToRelative(Path.Combine(entryFolder, f.Path)))); + Rewrite(recordPath, entryFolder, record with + { + Photos = [.. record.Photos.Where(p => !back.Contains(p.Frame.Id))], + Leftovers = Merge(record.Leftovers, extra), + }); + } + FrameRestoreJournal.Delete(sequence); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning("archive", $"A restore from the trash into {sequence.RelativeFolder} is not settled yet; the next writer of the sequence settles it", ex); + } + } + + /// Settles the restore a crash interrupted (), if any; the caller holds the sequence's lease. + internal static void RecoverRestore(SequencePaths sequence, SequenceManifest manifest) + { + if (!File.Exists(sequence.RestoreJournalPath)) return; + if (FrameRestoreJournal.Read(sequence) is { } journal) + { + Log.Warning("archive", $"An interrupted restore from the trash into {sequence.RelativeFolder} is settled"); + Settle(sequence, manifest, journal); + } + else + { + FrameRestoreJournal.Delete(sequence); // torn while being written: nothing was moved yet + } + } + + /// + /// What a restore into a new sequence did: the photos left in the trash, the companions that did not follow theirs, and + /// what stopped it before its last photo (a failure other than a file's, or a cancel). + /// + private sealed record NewSequenceRestore(Guid? SequenceId, int Restored, List Left, List Leftovers, ExceptionDispatchInfo? Failure, + bool Stopped); + + /// + /// The photos of a sequence that is gone into a new one, through the archive ingest (naming templates, a sidecar of its + /// own), one photo at a time so one that fails stays in the trash alone: titled as theirs was, placed at its folder when + /// that is free, and keyed by its id, so every entry of that sequence comes back into the same new one. Each original + /// is staged inside its own entry (); once it leaves the entry its commit runs to the end, + /// and when it is not filed, whatever the failure, it goes back. Companions follow their photo under a name chosen so + /// that theirs are free. A cancel, or a failure other than a file's, stops before the next photo. + /// + private async Task IntoNewSequenceAsync(Archive archive, ArchivePaths paths, PhotoTrashRecord record, string entryFolder, + List intact, CancellationToken cancellationToken) + { + var left = new List(); + var leftovers = new List(); + var staging = Path.Combine(entryFolder, RestoringFolder); + var key = $"trash:{record.SequenceId:N}"; + var target = new SequenceTarget(key, record.SequenceTitle, record.SequenceFolder.Length > 0 ? record.SequenceFolder : null, null); + var sequenceId = await _db.Sequences.FindBySessionKeyAsync(archive.Id, key, cancellationToken).ConfigureAwait(false); + SequencePaths? folder = null; + var names = new HashSet(StringComparer.OrdinalIgnoreCase); + var restored = 0; + ExceptionDispatchInfo? failure = null; + var stopped = false; + foreach (var photo in intact) + { + stopped |= failure is null && cancellationToken.IsCancellationRequested; + if (failure is not null || stopped) + { + left.Add(photo); + continue; + } + if (folder is null && sequenceId is { } known && await _db.Sequences.GetAsync(known, CancellationToken.None).ConfigureAwait(false) is { } row) + folder = paths.Sequence(row.Folder); + var original = photo.Files[0]; + var tails = photo.Files.Skip(1).Select(c => CompanionTail(c.From, original.From)).ToList(); + var planned = FreeName(folder, ArchiveLayout.OriginalsFolder + "/" + Path.GetFileName(original.From), tails, names); + var inEntry = Path.Combine(entryFolder, original.Path); + var path = Path.Combine(staging, original.Path); + try + { + MoveFile(inEntry, path); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning("archive", $"{original.Path} could not be staged for a restore; it stays in the trash", ex); + left.Add(photo); + continue; + } + IngestResult? result = null; + try + { + var frame = new StagedFrame(path, photo.Frame.Original ?? Path.GetFileName(original.From), original.Size, photo.Frame.Sha256 ?? original.Sha256!, + MetadataReader.Read(path, CancellationToken.None), photo.Frame.CapturedAt, Path.GetFileName(planned)); + // The original has left the entry: its commit runs to the end whatever the caller cancels meanwhile. + result = (await _ingest.CommitSessionAsync(archive, target, [frame], CancellationToken.None).ConfigureAwait(false))[0]; + } + catch (Exception ex) + { + Log.Warning("archive", $"{original.Path} could not be restored into a new sequence", ex); + if (ex is not (IOException or UnauthorizedAccessException or InvalidDataException)) failure = ExceptionDispatchInfo.Capture(ex); + } + if (File.Exists(path)) + { + // Not filed (a failure, or a duplicate of a photo the new sequence holds): it returns to the entry, or, when it + // cannot, the next restore of the entry puts it back. + try + { + MoveFile(path, inEntry); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Error("archive", $"{original.Path} stays staged in its trash entry; the next restore puts it back", ex); + } + left.Add(photo); + continue; + } + restored++; + if (result is null) + { + // Filed although its commit failed afterwards (the ingest's journal completes it): where is not known, so its + // companions stay in the trash. + leftovers.AddRange(photo.Files.Skip(1)); + continue; + } + sequenceId ??= result.SequenceId; + var placed = paths.FromRelative(result.RelativePath); + var stem = placed[..^Path.GetExtension(placed).Length]; + names.Add(ArchiveLayout.OriginalsFolder + "/" + Path.GetFileName(placed)); + foreach (var companion in photo.Files.Skip(1)) + { + var beside = stem + CompanionTail(companion.From, original.From); + names.Add(ArchiveLayout.OriginalsFolder + "/" + Path.GetFileName(beside)); + try + { + MoveFile(Path.Combine(entryFolder, companion.Path), beside); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + // Taken there since its name was chosen, or not movable: it stays in the trash, counted in the record. + Log.Warning("archive", $"{companion.Path} could not follow its photo to {result.RelativePath}; it stays in the trash", ex); + leftovers.Add(companion); + } + } + } + // A photo filed by a commit that failed afterwards may be the first in the new sequence. + if (sequenceId is null && restored > 0) + sequenceId = await _db.Sequences.FindBySessionKeyAsync(archive.Id, key, CancellationToken.None).ConfigureAwait(false); + return new NewSequenceRestore(sequenceId, restored, left, leftovers, failure, stopped); + } + + /// Puts back into an entry what a restore into a new sequence left staged in it (a crash, or a file that could not move back). + private static void Unstage(string entryFolder) + { + var staging = Path.Combine(entryFolder, RestoringFolder); + if (!System.IO.Directory.Exists(staging)) return; + foreach (var file in System.IO.Directory.EnumerateFiles(staging, "*", SearchOption.AllDirectories).ToList()) + { + var place = Path.Combine(entryFolder, Path.GetRelativePath(staging, file)); + if (File.Exists(place)) + { + Log.Warning("archive", $"{file} stays staged: {place} exists"); + continue; + } + MoveFile(file, place); + Log.Warning("archive", $"{Path.GetRelativePath(entryFolder, place)} of trash entry {Path.GetFileName(entryFolder)} was staged by a restore that stopped; it is back"); + } + FrameRemovalJournal.RemoveEmpty(staging); + } + + /// Rewrites an entry's record with what it still holds; when nothing is left in its folder, the record goes too. + private static void Rewrite(string recordPath, string entryFolder, PhotoTrashRecord record) + { + FrameRemovalJournal.RemoveEmpty(entryFolder); + // Only an entry left without a file goes; anything still in its folder keeps it listed, never erased by a restore. + if (record.Photos.Count == 0 && record.Leftovers is not { Count: > 0 } && !System.IO.Directory.Exists(entryFolder)) + { + File.Delete(recordPath); + return; + } + AtomicFile.WriteAllText(recordPath, JsonSerializer.Serialize(record with + { + Bytes = record.Photos.Sum(p => p.Files.Sum(f => f.Size)) + (record.Leftovers?.Sum(f => f.Size) ?? 0), + }, TrashJson.Default.PhotoTrashRecord)); + } + + /// The leftovers of a record with , each file once; null when there are none. + private static List? Merge(IReadOnlyList? leftovers, IEnumerable more) + { + var all = (leftovers ?? []).Concat(more).DistinctBy(f => f.Path, StringComparer.OrdinalIgnoreCase).ToList(); + return all.Count > 0 ? all : null; + } + + /// What a companion's name adds to its photo's stem (.xmp, .CR2.xmp). + private static string CompanionTail(string companion, string photo) + { + var stem = Path.GetFileNameWithoutExtension(Path.GetFileName(photo)); + var name = Path.GetFileName(companion); + return name.StartsWith(stem, StringComparison.OrdinalIgnoreCase) ? name[stem.Length..] : Path.GetExtension(name); + } + + /// + /// when it and its companions' names ( after its stem) are free in + /// and on disk in (none yet: in only), else + /// stem_2.ext, stem_3.ext… + /// + private static string FreeName(SequencePaths? folder, string file, IReadOnlyList tails, HashSet taken) + { + var extension = Path.GetExtension(file); + var stem = file[..^extension.Length]; + var candidate = file; + for (var n = 2; Taken(candidate); n++) + candidate = string.Create(CultureInfo.InvariantCulture, $"{stem}_{n}{extension}"); + return candidate; + + bool Taken(string name) + { + var named = name[..^extension.Length]; + return Used(name) || tails.Any(t => Used(named + t)); + } + + bool Used(string name) => taken.Contains(name) || (folder is not null && File.Exists(folder.File(name))); + } +} + +/// The files of one photo a restore moves from its trash entry into its sequence, the original first. +/// The photo's frame id, which the sidecar lists once the photo is back. +/// Each file: its place in the sequence folder, +/// the file in the entry (relative to the archive root). +internal sealed record RestoredPhoto(Guid Frame, IReadOnlyList Moves); + +/// +/// Write-ahead record of photos restored from the trash into their sequence (.chronos/restore.pending.json, owner +/// review 2, R6): the entry they come from and each photo's file moves. Written under the sequence's lease before the +/// first move and removed once the restore is settled (: the record without the photos the +/// sidecar lists, the files of the others back in the entry), so the next writer of the sequence settles a restore a crash +/// interrupted (). +/// +/// The entry's record, relative to the archive root. +/// The entry's folder, relative to the archive root. +internal sealed record FrameRestoreJournal(string Record, string Folder, IReadOnlyList Photos) +{ + public void Write(SequencePaths sequence) + { + System.IO.Directory.CreateDirectory(sequence.MetaDirectory); + AtomicFile.WriteAllBytes(sequence.RestoreJournalPath, JsonSerializer.SerializeToUtf8Bytes(this, TrashJson.Default.FrameRestoreJournal)); + } + + /// The journal of a sequence; null when it is torn (written by a process that died while writing it). + public static FrameRestoreJournal? Read(SequencePaths sequence) + { + try + { + return JsonSerializer.Deserialize(AtomicFile.ReadAllBytes(sequence.RestoreJournalPath), TrashJson.Default.FrameRestoreJournal); + } + catch (JsonException) + { + return null; + } + } + + public static void Delete(SequencePaths sequence) + { + if (File.Exists(sequence.RestoreJournalPath)) File.Delete(sequence.RestoreJournalPath); + } +} diff --git a/src/Chronos.Data/Archiving/FrameTrash.cs b/src/Chronos.Data/Archiving/FrameTrash.cs index e54eb2c3..7c850258 100644 --- a/src/Chronos.Data/Archiving/FrameTrash.cs +++ b/src/Chronos.Data/Archiving/FrameTrash.cs @@ -3,7 +3,6 @@ using System.Text.Json; using Chronos.Core.Archiving; using Chronos.Core.Diagnostics; using Chronos.Core.IO; -using Chronos.Core.Metadata; using Chronos.Core.Model; using Chronos.Data.Archiving.Jobs; using Chronos.Data.Jobs; @@ -27,6 +26,12 @@ public enum FrameDeleteBlock /// Every frame of the sequence would go: delete the sequence instead, which keeps its settings and renders. WouldEmpty, + + /// + /// A photo named was never hashed and its original is missing (): no tombstone + /// could tell sync it was deleted, so a copy elsewhere would come back. A verify or a scan settles it first. + /// + Unhashable, } /// The dry run of deleting photos of one sequence into the archive's trash (owner review 2, R6). @@ -36,6 +41,9 @@ public enum FrameDeleteBlock public sealed record FrameDeletePlan(Sequence Sequence, IReadOnlyList Frames, long Bytes, IReadOnlyList Blocked) { public bool CanDelete => Blocked.Count == 0 && Frames.Count > 0; + + /// The frames named that block the plan as . + public IReadOnlyList Unhashable { get; init; } = []; } /// What a photo deletion did: the trash entry made and the sequence as it is now. @@ -51,13 +59,15 @@ public sealed record PhotoRestoreResult(Sequence Sequence, int Restored, IReadOn /// Deleting photos of a sequence without erasing anything (owner review 2, R6; BRIEF §0.2 rule 8): /// is the dry run; moves each original (and its XMP companion) under the sequence's lease into /// .chronos/trash/photos/<sequence>-<time>/originals next to a record of the frames, writes the sidecar -/// without them (frames version + 1) and with their tombstones, and indexes it: what covered the frames is cleared and -/// queued again, and the project's positions follow their photos (those on the photos that went wait as pending frames). -/// A journal undoes a deletion a crash interrupted. re-hashes each file against its record and -/// moves the matching ones back with the same frame records, dropping their tombstones; into a new sequence through the -/// archive ingest when theirs is gone. +/// without them (frames version + 1) and with a tombstone of each (a photo never hashed is hashed first), and indexes it: +/// what covered the frames is cleared and queued again, the proxies and thumbnails of the frames that stay follow their +/// new index, and the project's positions follow their photos (those on the photos that went wait as pending frames). +/// A journal undoes a deletion a crash interrupted, whose entry the trash does not list meanwhile. +/// re-hashes each file against its record and moves the matching ones back with the same frame +/// records, dropping their tombstones, under a journal of its own; into a new sequence through the archive ingest when +/// theirs is gone. /// -public sealed class FrameTrash +public sealed partial class FrameTrash { /// Folder of the photo entries inside the trash. public const string FolderName = "photos"; @@ -109,24 +119,52 @@ public sealed class FrameTrash var wanted = frameIds.ToHashSet(); var frames = all.Where(f => wanted.Contains(f.Id)).ToList(); var blocked = new List(); - if (ArchiveVolume.Probe(new ArchivePaths(archive.RootPath)) != ArchiveAvailability.Online) blocked.Add(FrameDeleteBlock.Offline); + var paths = new ArchivePaths(archive.RootPath); + var online = ArchiveVolume.Probe(paths) == ArchiveAvailability.Online; + if (!online) blocked.Add(FrameDeleteBlock.Offline); var busy = await _trash.BusyAsync(cancellationToken).ConfigureAwait(false); if (busy.Contains(sequence.Id) || busy.Contains(sequence.ArchiveId)) blocked.Add(FrameDeleteBlock.Busy); if (frames.Count != wanted.Count || (expectedFramesVersion is { } expected && expected != sequence.FramesVersion)) blocked.Add(FrameDeleteBlock.FramesChanged); if (frames.Count > 0 && frames.Count == all.Count) blocked.Add(FrameDeleteBlock.WouldEmpty); - return new FrameDeletePlan(sequence, frames, frames.Sum(f => f.Size), blocked); + // A photo without a hash is hashed as it goes; one whose original is gone as well could leave no tombstone. + var unhashable = online ? Unhashable(paths, frames) : []; + if (unhashable.Count > 0) blocked.Add(FrameDeleteBlock.Unhashable); + return new FrameDeletePlan(sequence, frames, frames.Sum(f => f.Size), blocked) { Unhashable = unhashable }; + } + + /// + /// The frames never hashed whose original is not on disk, in index order; a directory holding many of them is listed + /// once rather than asked about each (a share answers one listing faster than thousands of lookups). + /// + private static List Unhashable(ArchivePaths paths, IReadOnlyList frames) + { + const int Lookups = 64; + var names = OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; + var missing = new List(); + foreach (var directory in frames.Where(f => f.Sha256 is null).GroupBy(f => Path.GetDirectoryName(paths.FromRelative(f.RelativePath)) ?? string.Empty)) + { + if (directory.Count() <= Lookups || !System.IO.Directory.Exists(directory.Key)) + { + missing.AddRange(directory.Where(f => !File.Exists(paths.FromRelative(f.RelativePath)))); + continue; + } + var present = System.IO.Directory.EnumerateFiles(directory.Key).Select(p => Path.GetFileName(p)).ToHashSet(names); + missing.AddRange(directory.Where(f => !present.Contains(Path.GetFileName(paths.FromRelative(f.RelativePath))))); + } + return [.. missing.OrderBy(f => f.Index)]; } /// /// Moves exactly the planned photos into the trash and takes them out of the sequence, then queues on /// (null: nothing) what the frames need again: the light analysis, a background deep analysis /// when the sequence had one, and the thumbnails and proxies the host keeps (always on a server; on a desktop those it - /// had). A kind the engine does not run, or one already queued for the sequence, is left out. + /// had). A kind the engine does not run, or one already queued for the sequence, is left out. Once the sidecar is + /// written the deletion stands: an index that cannot follow (logged) is resynced by the next scan. /// /// Who asked (user name or client), kept in the trash record. /// The plan is blocked, or the sequence changed since it was made; nothing moved. /// The archive went offline; nothing moved. - /// A file is open elsewhere; nothing moved. + /// A file is open elsewhere, or the sidecar could not be written; nothing moved. public async Task DeleteAsync(FrameDeletePlan plan, string? deletedBy = null, JobEngine? followUps = null, CancellationToken cancellationToken = default) { @@ -144,6 +182,7 @@ public sealed class FrameTrash TrashEntry entry; SequenceManifest updated; + bool indexed; await using (await _writer.LockAsync(paths, id, cancellationToken).ConfigureAwait(false)) { var manifest = SequenceWriter.ReadLocked(folder, id); @@ -163,110 +202,54 @@ public sealed class FrameTrash var name = EntryName(trash, id); var entryFolder = Path.Combine(trash, name); var recordPath = entryFolder + RecordExtension; - var photos = await TrashedAsync(folder, removed, kept, cancellationToken).ConfigureAwait(false); + var photos = await TrashedAsync(folder, id, removed, kept, cancellationToken).ConfigureAwait(false); + var held = await HeldAsync(folder, kept, photos, cancellationToken).ConfigureAwait(false); var now = DateTimeOffset.UtcNow; var record = new PhotoTrashRecord(TrashKind.Photos, id, revised.Sequence.DisplayTitle, revised.Sequence.Folder, photos, photos.Sum(p => p.Files.Sum(f => f.Size)), now, deletedBy); var moves = photos.SelectMany(p => p.Files).Select(f => new FrameRemovalMove(f.From, paths.ToRelative(Path.Combine(entryFolder, f.Path)))).ToList(); + updated = SequenceFrames.WithFrames(manifest, kept) with + { + Status = manifest.Status & ~Uncovered, + Removed = WithTombstones(manifest.Removed, photos, held, now), + }; cancellationToken.ThrowIfCancellationRequested(); // The journal first: a crash from here until the sidecar is written puts every file back (ReadLocked). FrameRemovalJournal.Write(folder, new FrameRemovalJournal(paths.ToRelative(recordPath), paths.ToRelative(entryFolder), [.. planned], moves)); - AtomicFile.WriteAllText(recordPath, JsonSerializer.Serialize(record, TrashJson.Default.PhotoTrashRecord)); + // Under a name the trash does not list until the sidecar is written: half an entry is never restored or emptied. + AtomicFile.WriteAllText(FrameRemovalJournal.PendingRecord(recordPath), JsonSerializer.Serialize(record, TrashJson.Default.PhotoTrashRecord)); System.IO.Directory.CreateDirectory(entryFolder); var done = new List(); try { foreach (var move in moves) { - var target = paths.FromRelative(move.To); - System.IO.Directory.CreateDirectory(Path.GetDirectoryName(target)!); - File.Move(folder.File(move.From), target, overwrite: false); + MoveFile(folder.File(move.From), paths.FromRelative(move.To)); done.Add(move); } + ManifestStore.WriteSequence(folder.ManifestPath, updated); } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + catch (Exception ex) { - // A file is open elsewhere: the ones that moved go back, and nothing changed. + // A file is open elsewhere, or the sidecar cannot be written: the ones that moved go back, and nothing changed. + Log.Warning("archive", $"{revised.Sequence.DisplayTitle}: photos could not move to the trash; those that moved go back", ex); Undo(paths, folder, done, recordPath, entryFolder); throw; } - - updated = SequenceFrames.WithFrames(manifest, kept) with - { - Status = manifest.Status & ~Uncovered, - Removed = WithTombstones(manifest.Removed, removed, kept, now), - }; - ManifestStore.WriteSequence(folder.ManifestPath, updated); - FrameRemovalJournal.Delete(folder); + Publish(folder, recordPath); Log.Info("archive", $"{revised.Sequence.DisplayTitle}: {removed.Count} photo(s) moved to the trash ({name}, {record.Bytes} bytes)"); // The files have moved: the index follows to the end, whatever the caller cancels meanwhile. - await _indexer.SyncAsync(archive, folder, updated, null, Uncovered, photosChanged, CancellationToken.None).ConfigureAwait(false); + indexed = await IndexAsync(archive, folder, updated, photosChanged, revised.Sequence.DisplayTitle).ConfigureAwait(false); ShiftProxies(folder, manifest.Frames, updated.Frames, removed); + ShiftThumbnails(paths, folder, id, manifest.Frames, updated.Frames); entry = Entry(archive.Id, name, record); } - var sequence = await _db.Sequences.GetAsync(id, CancellationToken.None).ConfigureAwait(false) ?? revised.Sequence; + var sequence = await RowAsync(revised.Sequence, updated, indexed).ConfigureAwait(false); await QueueAsync(followUps, archive, sequence, had, CancellationToken.None).ConfigureAwait(false); return new FrameDeleteResult(entry, sequence); } - /// - /// Brings the photos of a trash entry back. Each file is re-hashed against its record: a file that no longer matches - /// (or is missing) stays in the trash with its photo, which is reported, as is a photo whose content the sequence holds - /// again. The others move back to their places (a name taken since gets _2, _3…) with the same frame - /// records, inserted by capture time, and their tombstones go. When the sequence is gone (deleted, or never indexed in - /// this archive), they go into a new sequence through the archive ingest, titled and placed as theirs was. The entry - /// goes once nothing is left in it. - /// - /// The entry's (photos-…). - /// The entry is not in the trash. - public async Task RestoreAsync(Archive archive, string name, JobEngine? followUps = null, CancellationToken cancellationToken = default) - { - ArgumentNullException.ThrowIfNull(archive); - ArgumentException.ThrowIfNullOrWhiteSpace(name); - var paths = new ArchivePaths(archive.RootPath); - ArchiveVolume.EnsureWritable(paths); - var folderName = DiskName(name); - var entryFolder = Path.Combine(Directory(paths), folderName); - var recordPath = entryFolder + RecordExtension; - using (await LeaseFile.AcquireAsync(paths.LocksDirectory, SequenceTrash.EntryLease(name), _writer.Options.Owner, _writer.Options.LeaseWait, - _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false)) - { - if (!System.IO.Directory.Exists(entryFolder) || ReadRecord(recordPath) is not { } record) - throw new DirectoryNotFoundException($"{name} is not in the trash of {archive.Name}."); - var (intact, kept) = await CheckAsync(entryFolder, record, cancellationToken).ConfigureAwait(false); - // Nothing that matches its record: the entry stays as it is. - if (intact.Count == 0) throw new InvalidDataException(Core.Localization.Loc.L("Archive.Error.TrashMismatch")); - var sequence = await _db.Sequences.GetAsync(record.SequenceId, cancellationToken).ConfigureAwait(false); - var had = sequence?.Status ?? SequenceStatus.None; - PhotoRestoreResult result; - List left; - if (sequence is not null && sequence.ArchiveId == archive.Id) - (result, left) = await IntoSequenceAsync(archive, paths, sequence, entryFolder, intact, cancellationToken).ConfigureAwait(false); - else - (result, left) = await IntoNewSequenceAsync(archive, paths, record, entryFolder, intact, cancellationToken).ConfigureAwait(false); - var stays = kept.Concat(left).ToList(); - FrameRemovalJournal.RemoveEmpty(entryFolder); - // Only an entry left without a file goes; anything still in its folder keeps it listed, never erased by a restore. - if (stays.Count == 0 && !System.IO.Directory.Exists(entryFolder)) - { - File.Delete(recordPath); - } - else - { - AtomicFile.WriteAllText(recordPath, JsonSerializer.Serialize(record with - { - Photos = stays, - Bytes = stays.Sum(p => p.Files.Sum(f => f.Size)), - }, TrashJson.Default.PhotoTrashRecord)); - } - Log.Info("archive", $"{record.SequenceTitle}: {result.Restored} photo(s) restored from the trash, {stays.Count} kept there"); - var all = result with { Kept = [.. stays.Select(p => p.Frame.Original ?? Path.GetFileName(p.Frame.File))] }; - if (result.Restored > 0) await QueueAsync(followUps, archive, all.Sequence, had, CancellationToken.None).ConfigureAwait(false); - return all; - } - } - - /// The photo entries of an archive's trash (records whose folder is gone are left out). + /// The photo entries of an archive's trash (records whose folder is gone, or of a deletion still under way, are left out). internal static IReadOnlyList List(Archive archive, CancellationToken cancellationToken) { var folder = Directory(new ArchivePaths(archive.RootPath)); @@ -275,6 +258,7 @@ public sealed class FrameTrash foreach (var path in System.IO.Directory.EnumerateFiles(folder, "*" + RecordExtension)) { cancellationToken.ThrowIfCancellationRequested(); + if (!path.EndsWith(RecordExtension, StringComparison.OrdinalIgnoreCase)) continue; var name = Path.GetFileNameWithoutExtension(path); if (!System.IO.Directory.Exists(Path.Combine(folder, name)) || ReadRecord(path) is not { } record) continue; entries.Add(Entry(archive.Id, name, record)); @@ -286,6 +270,10 @@ public sealed class FrameTrash internal static PhotoTrashRecord? ReadEntry(ArchivePaths paths, string name) => IsPhotoEntry(name) ? ReadRecord(Path.Combine(Directory(paths), DiskName(name)) + RecordExtension) : null; + /// A photo entry as the trash lists it, read from its record now; null when it is not one or cannot be read. + internal static TrashEntry? ReadListed(ArchivePaths paths, Guid archiveId, string name) => + ReadEntry(paths, name) is { } record ? Entry(archiveId, DiskName(name), record) : null; + /// The entry's folder and record on disk. internal static (string Folder, string Record) Location(ArchivePaths paths, string name) { @@ -302,14 +290,17 @@ public sealed class FrameTrash /// /// The files of each frame that goes: its original (with the hash it was archived with, or one computed now for a frame /// never hashed) under the entry's originals/, and an XMP companion (IMG_0001.xmp, IMG_0001.CR2.xmp) - /// no frame that stays shares. A frame whose original is missing goes without a file. + /// no frame that stays shares. A frame whose original is missing goes without a file; one never hashed as well refuses + /// the plan (). /// - private static async Task> TrashedAsync(SequencePaths folder, List removed, List kept, + /// A frame never hashed has lost its original since the plan. + private static async Task> TrashedAsync(SequencePaths folder, Guid sequenceId, List removed, List kept, CancellationToken cancellationToken) { var used = new HashSet(StringComparer.OrdinalIgnoreCase); var claimed = new HashSet(StringComparer.OrdinalIgnoreCase); var keptStems = kept.Select(f => Stem(f.File)).ToHashSet(StringComparer.OrdinalIgnoreCase); + var companions = new CompanionIndex(folder); var photos = new List(removed.Count); foreach (var frame in removed) { @@ -323,7 +314,7 @@ public sealed class FrameTrash files.Add(new TrashedFile(frame.File, Unique(used, "originals/" + Inside(frame.File)), original.Length, sha)); if (!keptStems.Contains(Stem(frame.File))) { - foreach (var companion in Companions(folder, frame.File).Where(claimed.Add)) + foreach (var companion in companions.Of(frame.File).Where(claimed.Add)) { var path = folder.File(companion); files.Add(new TrashedFile(companion, Unique(used, "originals/" + Inside(companion)), new FileInfo(path).Length, @@ -331,25 +322,60 @@ public sealed class FrameTrash } } } + else if (sha is null) + { + throw new StaleDeletePlanException(sequenceId); + } photos.Add(new TrashedPhoto(frame with { Sha256 = sha }, files, null)); } return photos; } - /// The XMP files beside a photo that name it (stem.xmp or file.xmp), relative to the sequence folder. - private static IEnumerable Companions(SequencePaths folder, string file) + /// + /// The SHA-256 the frames that stay hold: those of their sidecar, and that of each one never hashed whose size is a + /// deleted photo's (hashed now), so a duplicate that stays keeps its content out of the tombstones. + /// + private static async Task> HeldAsync(SequencePaths folder, List kept, List photos, + CancellationToken cancellationToken) { - var directory = Path.GetDirectoryName(folder.File(file))!; - if (!System.IO.Directory.Exists(directory)) yield break; - var prefix = file.Contains('/') ? file[..(file.LastIndexOf('/') + 1)] : string.Empty; - var name = Path.GetFileName(file); - var stem = Path.GetFileNameWithoutExtension(name); - foreach (var candidate in System.IO.Directory.EnumerateFiles(directory, "*.xmp")) + var held = kept.Where(f => f.Sha256 is not null).Select(f => f.Sha256!).ToHashSet(StringComparer.OrdinalIgnoreCase); + var sizes = photos.Where(p => p.Files.Count > 0).Select(p => p.Files[0].Size).ToHashSet(); + foreach (var frame in kept.Where(f => f.Sha256 is null && sizes.Contains(f.Size))) { - var candidateName = Path.GetFileName(candidate); - if (!Path.GetExtension(candidateName).Equals(".xmp", StringComparison.OrdinalIgnoreCase)) continue; - var named = Path.GetFileNameWithoutExtension(candidateName); - if (named.Equals(stem, StringComparison.OrdinalIgnoreCase) || named.Equals(name, StringComparison.OrdinalIgnoreCase)) yield return prefix + candidateName; + cancellationToken.ThrowIfCancellationRequested(); + var file = new FileInfo(folder.File(frame.File)); + if (file.Exists && sizes.Contains(file.Length)) + held.Add(await ContentHash.Sha256Async(file.FullName, null, cancellationToken).ConfigureAwait(false)); + } + return held; + } + + /// + /// The XMP files of a sequence's photo directories, each directory listed once (a deletion of thousands of photos does + /// not list it again for each) and case-insensitively on every system (IMG_0001.XMP too). + /// + private sealed class CompanionIndex(SequencePaths folder) + { + private static readonly EnumerationOptions Listing = new() { MatchCasing = MatchCasing.CaseInsensitive, AttributesToSkip = 0 }; + private readonly Dictionary> _directories = new(StringComparer.Ordinal); + + /// The XMP files beside a photo that name it (stem.xmp or file.xmp), relative to the sequence folder. + public IEnumerable Of(string file) + { + var prefix = file.Contains('/') ? file[..(file.LastIndexOf('/') + 1)] : string.Empty; + if (!_directories.TryGetValue(prefix, out var named)) + { + var directory = Path.GetDirectoryName(folder.File(file))!; + named = (System.IO.Directory.Exists(directory) ? System.IO.Directory.EnumerateFiles(directory, "*.xmp", Listing) : []) + .Select(p => Path.GetFileName(p)) + .Where(n => Path.GetExtension(n).Equals(".xmp", StringComparison.OrdinalIgnoreCase)) + .ToLookup(n => Path.GetFileNameWithoutExtension(n), StringComparer.OrdinalIgnoreCase); + _directories[prefix] = named; + } + var name = Path.GetFileName(file); + var stem = Path.GetFileNameWithoutExtension(name); + var matches = stem.Equals(name, StringComparison.OrdinalIgnoreCase) ? named[stem] : named[stem].Concat(named[name]); + return matches.Select(n => prefix + n); } } @@ -376,16 +402,15 @@ public sealed class FrameTrash } /// - /// The tombstones after a deletion: those of before, and one per SHA-256 of the photos that went that no frame staying - /// holds (a duplicate that stays keeps the content in the sequence). A photo without a hash leaves none. + /// The tombstones after a deletion: those of before, and one per SHA-256 of the photos that went (hashed as they went + /// when the sidecar had none) that no frame staying holds (: a duplicate that stays keeps the + /// content in the sequence). /// - private static List? WithTombstones(IReadOnlyList? before, List removed, List kept, - DateTimeOffset at) + private static List? WithTombstones(IReadOnlyList? before, List photos, HashSet held, DateTimeOffset at) { - var held = kept.Where(f => f.Sha256 is not null).Select(f => f.Sha256!).ToHashSet(StringComparer.OrdinalIgnoreCase); var tombstones = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var tombstone in before ?? []) tombstones[tombstone.Sha256] = tombstone; - foreach (var sha in removed.Select(f => f.Sha256).OfType()) + foreach (var sha in photos.Select(p => p.Frame.Sha256).OfType()) { if (!held.Contains(sha)) tombstones[sha] = new RemovedPhoto { Sha256 = sha.ToLowerInvariant(), Reason = RemovalReason.Deleted, At = at }; } @@ -407,11 +432,63 @@ public sealed class FrameTrash return; } } - File.Delete(recordPath); + File.Delete(FrameRemovalJournal.PendingRecord(recordPath)); FrameRemovalJournal.RemoveEmpty(entryFolder); FrameRemovalJournal.Delete(folder); } + /// + /// Lists a deletion whose sidecar is written: its record takes its own name and the journal goes. A failure (logged) + /// keeps the journal, and the next writer of the sequence lists the entry (). + /// + private static void Publish(SequencePaths folder, string recordPath) + { + try + { + FrameRemovalJournal.Publish(recordPath); + FrameRemovalJournal.Delete(folder); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning("archive", $"Trash record {recordPath} keeps its pending name; the next writer of {folder.RelativeFolder} lists it", ex); + } + } + + /// + /// Indexes a sidecar this class wrote, to the end whatever the caller cancels. False (logged) when the index cannot + /// follow: the files have moved and the sidecar says so, and the folder's stamps stay unrecorded, so the next scan + /// resyncs the sequence from it. + /// + private async Task IndexAsync(Archive archive, SequencePaths folder, SequenceManifest manifest, bool photosChanged, string title) + { + try + { + await _indexer.SyncAsync(archive, folder, manifest, null, Uncovered, photosChanged, CancellationToken.None).ConfigureAwait(false); + return true; + } + catch (Exception ex) when (ex is DatabaseException or IOException or UnauthorizedAccessException) + { + Log.Warning("archive", $"{title}: the index did not follow the photos moved; the next scan resyncs it", ex); + return false; + } + } + + /// The sequence's row now; when the index did not follow (or cannot be read), the row as the sidecar has it. + private async Task RowAsync(Sequence before, SequenceManifest manifest, bool indexed) + { + var fallback = before with { FramesVersion = manifest.Versions.Frames, FrameCount = manifest.Frames.Count }; + if (!indexed) return fallback; + try + { + return await _db.Sequences.GetAsync(before.Id, CancellationToken.None).ConfigureAwait(false) ?? fallback; + } + catch (DatabaseException ex) + { + Log.Warning("archive", $"{before.DisplayTitle}: its row could not be read again", ex); + return fallback; + } + } + /// /// Renames the proxies of the frames whose index changed (BRIEF §8.5: proxies are named by frame index) so they stay /// theirs, and deletes those of the frames removed: the passes queued afterwards reuse them instead of making the @@ -448,7 +525,37 @@ public sealed class FrameTrash } } - /// Queues what the frames need again (see ), as an orphans fix does. + /// + /// Moves the thumbnails of the frames whose index changed to their new index in thumbs.pack (BRIEF §8.5: the pack + /// is keyed by frame index) and drops those of the frames removed, as does for proxies, so the + /// thumbnail pass queued afterwards reuses them instead of drawing the rest of the sequence again. Only the pack's index + /// is rewritten. A thumbnail writer holding the pack meanwhile (its lease) leaves it as it is, and so does a failure + /// (logged): each thumbnail carries its original's fingerprint, so one in the wrong place is drawn again. + /// + private void ShiftThumbnails(ArchivePaths paths, SequencePaths folder, Guid sequenceId, IReadOnlyList before, + IReadOnlyList after) + { + if (!File.Exists(folder.ThumbsPackPath)) return; + var now = after.ToDictionary(f => f.Id, f => f.I); + var indices = before.Where(f => now.ContainsKey(f.Id)).ToDictionary(f => f.I, f => now[f.Id]); + try + { + using var lease = LeaseFile.TryAcquire(paths.LocksDirectory, $"thumbs-{sequenceId:N}", _writer.Options.Owner, _writer.Options.LeaseDuration); + if (lease is null) + { + Log.Info("archive", $"Thumbnails of {folder.RelativeFolder} are being written; the thumbnail pass draws those that moved again"); + return; + } + using var pack = ThumbPackWriter.Open(folder.ThumbsPackPath); + pack.Rekey(indices); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning("archive", $"Thumbnails of {folder.RelativeFolder} did not follow their frames; the thumbnail pass draws them again", ex); + } + } + + /// Queues what the frames need again (see ), as an orphans fix does; an index that cannot take them is logged. private async Task QueueAsync(JobEngine? followUps, Archive archive, Sequence sequence, SequenceStatus had, CancellationToken cancellationToken) { if (followUps is null) return; @@ -459,202 +566,43 @@ public sealed class FrameTrash if (images || (had & SequenceStatus.ThumbsBuilt) != 0) requests.Add(ArchiveJobs.Thumbnails([sequence.Id], label)); if ((images || (had & SequenceStatus.ProxiesBuilt) != 0) && archive.ProxyLongEdge != ProxyPolicy.Disabled) requests.Add(ArchiveJobs.Proxies([sequence.Id], label)); - foreach (var request in requests) + try { - if (!followUps.Runs(request.Kind) || await _db.Jobs.IsQueuedAsync(request.Kind, sequence.Id, cancellationToken).ConfigureAwait(false)) continue; - await followUps.EnqueueAsync(request, cancellationToken: cancellationToken).ConfigureAwait(false); + foreach (var request in requests) + { + if (!followUps.Runs(request.Kind) || await _db.Jobs.IsQueuedAsync(request.Kind, sequence.Id, cancellationToken).ConfigureAwait(false)) continue; + await followUps.EnqueueAsync(request, cancellationToken: cancellationToken).ConfigureAwait(false); + } + } + catch (DatabaseException ex) + { + Log.Warning("archive", $"{label}: the passes its frames need again were not queued; the next scan queues them", ex); } } - /// Each photo whose files all match their record (size and SHA-256), and those that stay in the trash. - private static async Task<(List Intact, List Kept)> CheckAsync(string entryFolder, PhotoTrashRecord record, - CancellationToken cancellationToken) - { - var intact = new List(); - var kept = new List(); - foreach (var photo in record.Photos) - { - var ok = photo.Files.Count > 0; - foreach (var file in photo.Files) - { - var path = Path.Combine(entryFolder, file.Path); - var info = new FileInfo(path); - if (!info.Exists || info.Length != file.Size - || (file.Sha256 is { } sha && !string.Equals(sha, await ContentHash.Sha256Async(path, null, cancellationToken).ConfigureAwait(false), - StringComparison.OrdinalIgnoreCase))) - { - Log.Warning("archive", $"{file.Path} of trash entry {Path.GetFileName(entryFolder)} does not match its record; it stays in the trash"); - ok = false; - break; - } - } - (ok ? intact : kept).Add(photo); - } - return (intact, kept); - } - - /// The photos back into their sequence, under its lease; returns what came back and the photos left in the trash. - private async Task<(PhotoRestoreResult, List)> IntoSequenceAsync(Archive archive, ArchivePaths paths, Sequence sequence, string entryFolder, - List intact, CancellationToken cancellationToken) - { - var folder = paths.Sequence(sequence.Folder); - var left = new List(); - await using (await _writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) - { - var manifest = SequenceWriter.ReadLocked(folder, sequence.Id); - var inStep = await _indexer.InStepAsync(archive, folder, manifest, cancellationToken).ConfigureAwait(false); - var photosChanged = !inStep && await _indexer.PhotosChangedAsync(archive, folder, cancellationToken).ConfigureAwait(false); - var ids = manifest.Frames.Select(f => f.Id).ToHashSet(); - var held = manifest.Frames.Where(f => f.Sha256 is not null).Select(f => f.Sha256!).ToHashSet(StringComparer.OrdinalIgnoreCase); - var taken = manifest.Frames.Select(f => f.File).ToHashSet(StringComparer.OrdinalIgnoreCase); - var back = new List(); - foreach (var photo in intact) - { - cancellationToken.ThrowIfCancellationRequested(); - // In the sequence again (restored before, or filed again since): the trash keeps its copy. - if (ids.Contains(photo.Frame.Id) || (photo.Frame.Sha256 is { } sha && held.Contains(sha))) - { - left.Add(photo); - continue; - } - var original = photo.Files[0]; - var tails = photo.Files.Skip(1).Select(c => CompanionTail(c.From, original.From)).ToList(); - var file = FreeName(folder, original.From, tails, taken); - var stem = file[..^Path.GetExtension(file).Length]; - try - { - MoveIn(Path.Combine(entryFolder, original.Path), folder.File(file)); - // A companion follows its photo's name: IMG_0001_2.xmp beside IMG_0001_2.JPG. - for (var c = 0; c < tails.Count; c++) MoveIn(Path.Combine(entryFolder, photo.Files[c + 1].Path), folder.File(stem + tails[c])); - } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) - { - Log.Warning("archive", $"{original.Path} could not move back into {folder.RelativeFolder}; it stays in the trash", ex); - left.Add(photo); - continue; - } - taken.Add(file); - back.Add(photo.Frame with { File = file, Flags = photo.Frame.Flags & ~FrameFlags.Missing }); - } - if (back.Count == 0) return (new PhotoRestoreResult(sequence, 0, [], false), left); - var restored = back.Where(f => f.Sha256 is not null).Select(f => f.Sha256!).ToHashSet(StringComparer.OrdinalIgnoreCase); - var tombstones = manifest.Removed?.Where(r => !restored.Contains(r.Sha256)).ToList(); - var updated = SequenceFrames.Insert(manifest, back) with - { - Status = manifest.Status & ~Uncovered, - Removed = tombstones is { Count: > 0 } ? tombstones : null, - }; - ManifestStore.WriteSequence(folder.ManifestPath, updated); - await _indexer.SyncAsync(archive, folder, updated, null, Uncovered, photosChanged, CancellationToken.None).ConfigureAwait(false); - ShiftProxies(folder, manifest.Frames, updated.Frames, []); - } - var row = await _db.Sequences.GetAsync(sequence.Id, CancellationToken.None).ConfigureAwait(false) ?? sequence; - return (new PhotoRestoreResult(row, intact.Count - left.Count, [], false), left); - } - - /// - /// The photos of a sequence that is gone into a new one, through the archive ingest (staging, naming templates, a - /// sidecar of its own), one photo at a time so one that fails stays in the trash alone: titled as theirs was, placed at - /// its folder when that is free, and keyed by its id, so every entry of that sequence comes back into the same new - /// one. Companions follow their photo. - /// - private async Task<(PhotoRestoreResult, List)> IntoNewSequenceAsync(Archive archive, ArchivePaths paths, PhotoTrashRecord record, - string entryFolder, List intact, CancellationToken cancellationToken) - { - var left = new List(); - var staging = Path.Combine(paths.StagingDirectory, $"trash-{record.SequenceId:N}"); - System.IO.Directory.CreateDirectory(staging); - var target = new SequenceTarget($"trash:{record.SequenceId:N}", record.SequenceTitle, record.SequenceFolder.Length > 0 ? record.SequenceFolder : null, null); - Guid? sequenceId = null; - var restored = 0; - foreach (var photo in intact) - { - cancellationToken.ThrowIfCancellationRequested(); - var original = photo.Files[0]; - var inEntry = Path.Combine(entryFolder, original.Path); - var path = Path.Combine(staging, Guid.NewGuid().ToString("N") + Path.GetExtension(original.From)); - File.Move(inEntry, path, overwrite: false); - IngestResult result; - try - { - var frame = new StagedFrame(path, photo.Frame.Original ?? Path.GetFileName(original.From), original.Size, photo.Frame.Sha256 ?? original.Sha256!, - MetadataReader.Read(path, cancellationToken), photo.Frame.CapturedAt, Path.GetFileName(original.From)); - result = (await _ingest.CommitSessionAsync(archive, target, [frame], cancellationToken).ConfigureAwait(false))[0]; - } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException) - { - Log.Warning("archive", $"{original.Path} could not be restored into a new sequence; it stays in the trash", ex); - if (File.Exists(path)) File.Move(path, inEntry, overwrite: false); - left.Add(photo); - continue; - } - sequenceId ??= result.SequenceId; - if (File.Exists(path)) - { - // A duplicate of a photo the new sequence holds stays in staging: it returns to the entry. - File.Move(path, inEntry, overwrite: false); - left.Add(photo); - continue; - } - restored++; - var placed = paths.FromRelative(result.RelativePath); - foreach (var companion in photo.Files.Skip(1)) - { - // Beside its photo; a name taken there (another file of that name) keeps the companion in the trash. - var beside = placed[..^Path.GetExtension(placed).Length] + CompanionTail(companion.From, original.From); - if (!File.Exists(beside)) MoveIn(Path.Combine(entryFolder, companion.Path), beside); - } - } - SequenceTrash.Erase(staging); - if (sequenceId is not { } id) throw new IOException(Core.Localization.Loc.L("Archive.Error.TrashMismatch")); - var sequence = await _db.Sequences.GetAsync(id, CancellationToken.None).ConfigureAwait(false) - ?? throw new InvalidOperationException($"The photos of {record.SequenceTitle} were restored but their sequence is not indexed."); - return (new PhotoRestoreResult(sequence, restored, [], true), left); - } - - /// What a companion's name adds to its photo's stem (.xmp, .CR2.xmp). - private static string CompanionTail(string companion, string photo) - { - var stem = Path.GetFileNameWithoutExtension(Path.GetFileName(photo)); - var name = Path.GetFileName(companion); - return name.StartsWith(stem, StringComparison.OrdinalIgnoreCase) ? name[stem.Length..] : Path.GetExtension(name); - } - - /// - /// when it is free in the sidecar and on disk with its companions ( after - /// its stem), else stem_2.ext, stem_3.ext… - /// - private static string FreeName(SequencePaths folder, string file, IReadOnlyList tails, HashSet taken) - { - var extension = Path.GetExtension(file); - var stem = file[..^extension.Length]; - var candidate = file; - for (var n = 2; taken.Contains(candidate) || File.Exists(folder.File(candidate)) || tails.Any(t => File.Exists(folder.File(Stem(candidate, extension) + t))); n++) - candidate = string.Create(CultureInfo.InvariantCulture, $"{stem}_{n}{extension}"); - return candidate; - - static string Stem(string name, string extension) => name[..^extension.Length]; - } - - private static void MoveIn(string from, string to) + /// Moves a file, making its folder first; never over another file. + private static void MoveFile(string from, string to) { System.IO.Directory.CreateDirectory(Path.GetDirectoryName(to)!); File.Move(from, to, overwrite: false); } - /// The sequence id and the time, with a number when an entry of that second exists already. + /// The sequence id and the time, with a number when an entry of that second exists already (or is being made). private static string EntryName(string trash, Guid sequenceId) { var stamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture); var name = $"{sequenceId:N}-{stamp}"; - for (var n = 2; System.IO.Directory.Exists(Path.Combine(trash, name)) || File.Exists(Path.Combine(trash, name) + RecordExtension); n++) + for (var n = 2; Taken(Path.Combine(trash, name)); n++) name = string.Create(CultureInfo.InvariantCulture, $"{sequenceId:N}-{stamp}-{n}"); return name; + + static bool Taken(string entry) => + System.IO.Directory.Exists(entry) || File.Exists(entry + RecordExtension) || File.Exists(FrameRemovalJournal.PendingRecord(entry + RecordExtension)); } private static TrashEntry Entry(Guid archiveId, string folderName, PhotoTrashRecord record) => new(archiveId, EntryPrefix + folderName, record.SequenceId, record.SequenceTitle, record.SequenceFolder, record.Photos.Count, - record.Photos.Sum(p => p.Files.Count), record.Bytes, record.DeletedAt, record.DeletedBy, record.Kind); + record.Photos.Sum(p => p.Files.Count) + (record.Leftovers?.Count ?? 0), record.Bytes, record.DeletedAt, record.DeletedBy, record.Kind); private static PhotoTrashRecord? ReadRecord(string path) { @@ -674,8 +622,10 @@ public sealed class FrameTrash /// What the trash keeps next to deleted photos (owner review 2, R6): the frame records, where each file was and is, the /// sequence they came from. entries (a later conversion) name each photo's replacement. /// +/// Files of the entry whose photo is back in a sequence without them (a companion whose name was +/// taken there): counted and erased with the entry, never restored. internal sealed record PhotoTrashRecord(TrashKind Kind, Guid SequenceId, string SequenceTitle, string SequenceFolder, IReadOnlyList Photos, - long Bytes, DateTimeOffset DeletedAt, string? DeletedBy); + long Bytes, DateTimeOffset DeletedAt, string? DeletedBy, IReadOnlyList? Leftovers = null); /// One photo of a photo entry: its frame record, its files (the original first) and, when replaced, the replacement's SHA-256. internal sealed record TrashedPhoto(SidecarFrame Frame, IReadOnlyList Files, string? Replacement); diff --git a/src/Chronos.Data/Archiving/SequenceMapping.cs b/src/Chronos.Data/Archiving/SequenceMapping.cs index d511421c..55f220d1 100644 --- a/src/Chronos.Data/Archiving/SequenceMapping.cs +++ b/src/Chronos.Data/Archiving/SequenceMapping.cs @@ -41,12 +41,21 @@ internal static class SequenceMapping public static List ToFrames(SequenceManifest manifest, SequencePaths sequence) => manifest.Frames.Select(f => ToFrame(f, manifest.Id, sequence)).ToList(); - /// The sidecar's tombstones as index rows (owner review 2, R6). - public static List ToRemovals(SequenceManifest manifest) => - manifest.Removed is not { Count: > 0 } removed - ? [] - : removed.Where(r => !string.IsNullOrEmpty(r.Sha256)) - .Select(r => new FrameRemoval(manifest.Id, r.Sha256.ToLowerInvariant(), r.Replacement?.ToLowerInvariant(), r.Reason, r.At)).ToList(); + /// + /// The sidecar's tombstones as index rows (owner review 2, R6), without those whose content a frame of the sidecar + /// holds: that photo is in the sequence again (adopted back by a scan, say), and sync offers it like any other. + /// + public static List ToRemovals(SequenceManifest manifest) + { + if (manifest.Removed is not { Count: > 0 } removed) return []; + var held = manifest.Frames.Where(f => f.Sha256 is not null).Select(f => f.Sha256!).ToHashSet(StringComparer.OrdinalIgnoreCase); + return removed.Where(r => !string.IsNullOrEmpty(r.Sha256) && !held.Contains(r.Sha256)) + .Select(r => new FrameRemoval(manifest.Id, r.Sha256.ToLowerInvariant(), r.Replacement?.ToLowerInvariant(), r.Reason, r.At)).ToList(); + } + + /// Index rows of tombstones as a sidecar carries them: a sidecar rebuilt from the photos takes them back. + public static List ToTombstones(IEnumerable removals) => + removals.Select(r => new RemovedPhoto { Sha256 = r.Sha256, Replacement = r.ReplacementSha256, Reason = r.Reason, At = r.RemovedAt }).ToList(); /// The current row; its render, analysis, sync and ownership fields are kept, and so are its /// title, tags, rating and notes when its settings version is ahead of the sidecar's (edited in the index only). diff --git a/src/Chronos.Data/Archiving/SequenceTrash.Empty.cs b/src/Chronos.Data/Archiving/SequenceTrash.Empty.cs index e7621db8..4e886bc2 100644 --- a/src/Chronos.Data/Archiving/SequenceTrash.Empty.cs +++ b/src/Chronos.Data/Archiving/SequenceTrash.Empty.cs @@ -80,7 +80,8 @@ public sealed partial class SequenceTrash /// each moved aside whole and then deleted (a listing never shows half an entry). From the start, the trash must still /// hold exactly the planned entries (), else nothing is erased; a run /// resumed after a stop checks each remaining entry against its own fingerprint, keeps one that changed and skips one - /// that is gone. A entry is erased only while every replacement still matches its + /// that is gone, and each entry is checked against its fingerprint once more under its lease, just before it goes. A + /// entry is erased only while every replacement still matches its /// SHA-256 in the sequence (re-hashed now); otherwise it is kept. Leftovers of an interrupted erase are deleted first. /// /// Entries done so far (the resume point) and their number. @@ -110,7 +111,7 @@ public sealed partial class SequenceTrash refused.Add(entry.Title); Log.Warning("archive", $"Trash entry {entry.Name} changed since the emptying was planned; it is kept"); } - else if (await EraseAsync(paths, entry, cancellationToken).ConfigureAwait(false)) + else if (await EraseAsync(paths, entry, item.Fingerprint, cancellationToken).ConfigureAwait(false)) { erased++; bytes += entry.Bytes; @@ -125,14 +126,23 @@ public sealed partial class SequenceTrash return new TrashEmptyResult(erased, bytes, refused); } - /// Erases one entry under its lease; false (logged) when a replaced entry's replacements no longer match, or it changed. - private async Task EraseAsync(ArchivePaths paths, TrashEntry entry, CancellationToken cancellationToken) + /// + /// Erases one entry under its lease, once its record read and its folder measured again under it still give the + /// planned (another host sharing the archive may have restored the entry and deleted the + /// same sequence again since); false (logged) when it changed, or a replaced entry's replacements no longer match. + /// + private async Task EraseAsync(ArchivePaths paths, TrashEntry entry, string fingerprint, CancellationToken cancellationToken) { using (await LeaseFile.AcquireAsync(paths.LocksDirectory, EntryLease(entry.Name), _writer.Options.Owner, _writer.Options.LeaseWait, _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false)) { var (folder, record) = Location(paths, entry); if (!Directory.Exists(folder) || !File.Exists(record)) return false; + if (Reread(paths, entry, cancellationToken) is not { } now || EntryFingerprint(now) != fingerprint) + { + Log.Warning("archive", $"Trash entry {entry.Name} changed since the emptying was planned; it is kept"); + return false; + } if (entry.Kind == TrashKind.Replaced && !await ReplacementsHoldAsync(paths, entry, cancellationToken).ConfigureAwait(false)) { Log.Warning("archive", $"Trash entry {entry.Name} is kept: a replacement of its originals no longer matches"); @@ -176,6 +186,18 @@ public sealed partial class SequenceTrash return true; } + /// An entry as the trash lists it, from its record and folder as they are now; null when its record cannot be read. + private static TrashEntry? Reread(ArchivePaths paths, TrashEntry entry, CancellationToken cancellationToken) + { + var (folder, recordPath) = Location(paths, entry); + var now = FrameTrash.IsPhotoEntry(entry.Name) + ? FrameTrash.ReadListed(paths, entry.ArchiveId, entry.Name) + : ReadRecord(recordPath) is { } record ? Entry(entry.ArchiveId, Path.GetFileName(entry.Name), record) : null; + if (now is null) return null; + var (files, bytes) = Measure(folder, cancellationToken); + return now with { Files = files, Bytes = bytes }; + } + /// The folder and record of an entry of either kind. private static (string Folder, string Record) Location(ArchivePaths paths, TrashEntry entry) { diff --git a/src/Chronos.Data/Archiving/SequenceTrash.cs b/src/Chronos.Data/Archiving/SequenceTrash.cs index c9f26ca5..24502cd9 100644 --- a/src/Chronos.Data/Archiving/SequenceTrash.cs +++ b/src/Chronos.Data/Archiving/SequenceTrash.cs @@ -246,7 +246,7 @@ public sealed partial class SequenceTrash Directory.Move(source, sequence.Folder); try { - var manifest = File.Exists(sequence.ManifestPath) ? SequenceWriter.ReadLocked(sequence, id) : Adopt(sequence, record.Sequence, cancellationToken); + var manifest = File.Exists(sequence.ManifestPath) ? SequenceWriter.ReadLocked(sequence, id) : Adopt(sequence, record, cancellationToken); // The index step runs to the end once it starts: a cancel cannot leave half the rows written. await IndexAsync(archive, sequence, manifest, record, CancellationToken.None).ConfigureAwait(false); } @@ -266,10 +266,12 @@ public sealed partial class SequenceTrash /// /// A sidecar for a restored folder that lost its own, built as a scan adopts a folder: its frames get new ids, the - /// sequence keeps its id, and the title, tags, rating and notes of the record go into it so sidecar and index agree. + /// sequence keeps its id, and the title, tags, rating, notes and tombstones of the record go into it so sidecar and + /// index agree, and a photo deleted before stays deleted. /// - private SequenceManifest Adopt(SequencePaths sequence, Sequence kept, CancellationToken cancellationToken) + private SequenceManifest Adopt(SequencePaths sequence, TrashRecord record, CancellationToken cancellationToken) { + var kept = record.Sequence; var built = SequenceAdopter.Build(kept.Id, sequence, _writer.Options.Owner, cancellationToken); var manifest = built with { @@ -278,6 +280,7 @@ public sealed partial class SequenceTrash Rating = kept.Rating, Notes = kept.Notes, Versions = built.Versions with { Settings = System.Math.Max(built.Versions.Settings, kept.SettingsVersion) }, + Removed = record.Removed is { Count: > 0 } removed ? removed : null, }; Directory.CreateDirectory(sequence.MetaDirectory); ManifestStore.WriteSequence(sequence.ManifestPath, manifest); @@ -364,8 +367,9 @@ public sealed partial class SequenceTrash Directory.CreateDirectory(trash); var name = EntryName(paths, sequence.Id, new HashSet()); var target = Path.Combine(trash, name); + var tombstones = await _db.FrameRemovals.ListBySequenceAsync(sequence.Id, cancellationToken).ConfigureAwait(false); var record = new TrashRecord(row, await _db.Renders.ListBySequenceAsync(sequence.Id, cancellationToken).ConfigureAwait(false), step.Files, - step.Bytes, DateTimeOffset.UtcNow, deletedBy); + step.Bytes, DateTimeOffset.UtcNow, deletedBy, tombstones.Count > 0 ? SequenceMapping.ToTombstones(tombstones) : null); cancellationToken.ThrowIfCancellationRequested(); // The record first: a crash before the move leaves a record without a folder, which the listing ignores. var recordPath = target + RecordExtension; @@ -570,11 +574,16 @@ public sealed partial class SequenceTrash private sealed record ArchiveContext(ArchivePaths Paths, ArchiveAvailability Availability, IReadOnlyList Locations); } -/// What the trash keeps next to a deleted folder: the index row and renders the sidecar does not carry. -internal sealed record TrashRecord(Sequence Sequence, IReadOnlyList Renders, int Files, long Bytes, DateTimeOffset DeletedAt, string? DeletedBy); +/// +/// What the trash keeps next to a deleted folder: the index row and renders the sidecar does not carry, and the +/// sequence's tombstones, which a sidecar rebuilt from the photos on restore takes back. +/// +internal sealed record TrashRecord(Sequence Sequence, IReadOnlyList Renders, int Files, long Bytes, DateTimeOffset DeletedAt, string? DeletedBy, + IReadOnlyList? Removed = null); [JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, UseStringEnumConverter = true, IgnoreReadOnlyProperties = true)] [JsonSerializable(typeof(TrashRecord))] [JsonSerializable(typeof(PhotoTrashRecord))] +[JsonSerializable(typeof(FrameRestoreJournal))] internal sealed partial class TrashJson : JsonSerializerContext; diff --git a/src/Chronos.Data/Archiving/SequenceWriter.cs b/src/Chronos.Data/Archiving/SequenceWriter.cs index 5bdb515c..cbec772d 100644 --- a/src/Chronos.Data/Archiving/SequenceWriter.cs +++ b/src/Chronos.Data/Archiving/SequenceWriter.cs @@ -88,8 +88,9 @@ internal sealed class SequenceWriter(ArchiveServiceOptions options) } /// - /// Reads the sidecar of a locked sequence, undoing an interrupted photo deletion () and - /// completing an interrupted ingest batch first. + /// Reads the sidecar of a locked sequence, undoing an interrupted photo deletion (), + /// settling an interrupted restore from the trash () and completing an interrupted + /// ingest batch first. /// /// The sidecar in the folder belongs to another sequence. public static SequenceManifest ReadLocked(SequencePaths sequence, Guid sequenceId) @@ -98,6 +99,7 @@ internal sealed class SequenceWriter(ArchiveServiceOptions options) if (manifest.Id != sequenceId) throw new InvalidDataException($"{sequence.ManifestPath} belongs to sequence {manifest.Id}, not {sequenceId}."); FrameRemovalJournal.Recover(sequence, manifest); + FrameTrash.RecoverRestore(sequence, manifest); if (IngestJournal.Recover(sequence, manifest) is { } recovered) { ManifestStore.WriteSequence(sequence.ManifestPath, recovered); diff --git a/src/Chronos.Server/Storage/ArchiveAssetLocator.cs b/src/Chronos.Server/Storage/ArchiveAssetLocator.cs index 49516f35..df09f50c 100644 --- a/src/Chronos.Server/Storage/ArchiveAssetLocator.cs +++ b/src/Chronos.Server/Storage/ArchiveAssetLocator.cs @@ -35,7 +35,8 @@ internal sealed class ArchiveAssetLocator : IFrameAssetLocator { using var pack = ThumbPackReader.Open(paths.ThumbsPackPath); if (!pack.Entries.TryGetValue(frame.Index, out var entry) || entry.Offset + entry.Length > pack.Length) return null; - if (frame.Sha256 is { Length: ThumbPack.HashLength * 2 } sha && !ThumbPack.IsFrom(entry.SourceHash, sha, SequenceFile(paths, frame), frame.Size)) return null; + // A frame never hashed is told by its file name and size: after a renumbering its index may hold its neighbour's. + if (!ThumbPack.IsFrom(entry.SourceHash, frame.Sha256, SequenceFile(paths, frame), frame.Size)) return null; return new AssetLocation(paths.ThumbsPackPath, entry.Offset, entry.Length, Jpeg); } diff --git a/tests/Chronos.Core.Tests/Archiving/ThumbPackTests.cs b/tests/Chronos.Core.Tests/Archiving/ThumbPackTests.cs index 8f69764a..49c7a8ec 100644 --- a/tests/Chronos.Core.Tests/Archiving/ThumbPackTests.cs +++ b/tests/Chronos.Core.Tests/Archiving/ThumbPackTests.cs @@ -36,6 +36,41 @@ public sealed class ThumbPackTests(TestContext context) Assert.Null(reader.Read(100)); } + [Test] + public void RekeyingMovesThumbnailsToTheirFramesNewIndexWithoutCopyingThem() + { + var path = NewPack(); + Fill(path, 5); + var length = new FileInfo(path).Length; + using (var writer = ThumbPackWriter.Open(path)) + { + // Frame 1 deleted: the frames after it move down one; nothing changes when nothing moves. + writer.Rekey(new Dictionary { [0] = 0, [1] = 1, [2] = 2, [3] = 3, [4] = 4 }); + Assert.Equal(length, new FileInfo(path).Length); + writer.Rekey(new Dictionary { [0] = 0, [2] = 1, [3] = 2, [4] = 3 }); + } + Assert.True(new FileInfo(path).Length < length, "only the index is written again"); + using (var reader = ThumbPackReader.Open(path)) + { + Assert.Equal(4, reader.Entries.Count); + Assert.BytesEqual(Payload(0), reader.Read(0)); + Assert.BytesEqual(Payload(2), reader.Read(1)); + Assert.BytesEqual(Hash(4), reader.Entries[3].SourceHash); + Assert.BytesEqual(Payload(4), reader.Read(3)); + Assert.Null(reader.Read(4)); + } + + // A frame put back at index 1 later: the others move up, and the writer appends after the new index. + using (var writer = ThumbPackWriter.Open(path)) + { + writer.Rekey(new Dictionary { [0] = 0, [1] = 2, [2] = 3, [3] = 4 }); + writer.Append(1, 320, 213, Hash(1), Payload(1)); + } + using var again = ThumbPackReader.Open(path); + Assert.Equal(5, again.Entries.Count); + for (var i = 0; i < 5; i++) Assert.BytesEqual(Payload(i), again.Read(i)); + } + [Test] public void AppendingAgainReplacesAFrameAndKeepsTheOthers() { diff --git a/tests/Chronos.Data.Tests/Archiving/FrameTrashTests.cs b/tests/Chronos.Data.Tests/Archiving/FrameTrashTests.cs index 9d9ed2c1..4efc1249 100644 --- a/tests/Chronos.Data.Tests/Archiving/FrameTrashTests.cs +++ b/tests/Chronos.Data.Tests/Archiving/FrameTrashTests.cs @@ -53,6 +53,45 @@ public abstract class FrameTrashTests(TestContext context) : DatabaseTest(contex private static string Sha(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path))); + private static string EntryFolder(ArchiveFixture fx, TrashEntry entry) => FrameTrash.Location(fx.Paths, entry.Name).Folder; + + private static async Task DeleteSequenceAsync(ArchiveFixture fx, Guid id) => + await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([id], null, fx.Ct), null, null, fx.Ct); + + /// A sequence a scan adopted in place from photos written into : its frames are not hashed. + private static async Task<(Sequence Sequence, List Frames)> AdoptedAsync(ArchiveFixture fx, string folder, int count, int seed = 1) + { + Photos.WriteSeries(Path.Combine(fx.Archive.RootPath, folder), Start, count, seed); + await fx.ScanAsync(); + var id = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == folder).Id; + return ((await fx.Db.Sequences.GetAsync(id))!, [.. await fx.Db.Frames.ListBySequenceAsync(id)]); + } + + /// The journal a restore of into its sequence writes before its first move (names free: as they were). + private static FrameRestoreJournal RestoreJournal(ArchiveFixture fx, TrashEntry entry) + { + var (folder, record) = FrameTrash.Location(fx.Paths, entry.Name); + var photos = FrameTrash.ReadEntry(fx.Paths, entry.Name)!.Photos; + return new FrameRestoreJournal(fx.Paths.ToRelative(record), fx.Paths.ToRelative(folder), + [.. photos.Select(p => new RestoredPhoto(p.Frame.Id, [.. p.Files.Select(f => new FrameRemovalMove(f.From, fx.Paths.ToRelative(Path.Combine(folder, f.Path))))]))]); + } + + /// Makes every on fail, as a lost database connection would. + private async Task FailAsync(ArchiveFixture fx, string table, string operation) + { + await using var session = await fx.Db.Database.OpenAsync(fx.Ct); + await session.ExecuteScriptAsync(ForDialect( + $"CREATE TRIGGER fail_{table} BEFORE {operation} ON {table} BEGIN SELECT RAISE(ABORT, 'injected'); END;", + $"CREATE FUNCTION fail_{table}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected'; END $$; " + + $"CREATE TRIGGER fail_{table} BEFORE {operation} ON {table} FOR EACH ROW EXECUTE FUNCTION fail_{table}();"), fx.Ct); + } + + private async Task HealAsync(ArchiveFixture fx, string table) + { + await using var session = await fx.Db.Database.OpenAsync(fx.Ct); + await session.ExecuteScriptAsync(ForDialect($"DROP TRIGGER fail_{table};", $"DROP TRIGGER fail_{table} ON {table};"), fx.Ct); + } + [Test] public async Task TheDryRunCountsThePhotosAndChangesNothing() { @@ -426,6 +465,515 @@ public abstract class FrameTrashTests(TestContext context) : DatabaseTest(contex Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == FrameTrash.EntryPrefix + other); } + [Test] + public async Task CompanionsGoWithTheirPhotoAndFollowItsNameBack() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 4); + var second = fx.Paths.FromRelative(frames[1].RelativePath); + var third = fx.Paths.FromRelative(frames[2].RelativePath); + // An upper-case extension (listed on Linux too), and one named after the whole file name. + var upper = Path.ChangeExtension(second, ".XMP"); + var whole = third + ".xmp"; + File.WriteAllText(upper, "2"); + File.WriteAllText(whole, "3"); + + var deleted = await DeleteAsync(fx, sequence, frames[1], frames[2]); + + Assert.FileMissing(upper); + Assert.FileMissing(whole); + Assert.Equal((2, 4), (deleted.Entry.Frames, deleted.Entry.Files)); + var entryFolder = EntryFolder(fx, deleted.Entry); + Assert.Equal("2", File.ReadAllText(Path.Combine(entryFolder, "originals", Path.GetFileName(upper)))); + Assert.Equal("3", File.ReadAllText(Path.Combine(entryFolder, "originals", Path.GetFileName(whole)))); + + // A file took the second photo's name since: it comes back as _2, and its XMP follows that name. + File.WriteAllBytes(second, Photos.Jpeg(Start, 999)); + var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct); + + Assert.Equal(2, restored.Photos); + Assert.Empty(restored.Kept); + var now = await fx.Db.Frames.ListBySequenceAsync(sequence.Id); + Assert.SequenceEqual(frames.Select(f => f.Id), now.Select(f => f.Id)); + var renamed = fx.Paths.FromRelative(now[1].RelativePath); + Assert.Equal($"{Path.GetFileNameWithoutExtension(second)}_2{Path.GetExtension(second)}", Path.GetFileName(renamed)); + Assert.Equal(frames[1].Sha256, Sha(renamed)); + Assert.Equal("2", File.ReadAllText(Path.ChangeExtension(renamed, ".XMP"))); + Assert.Equal(frames[2].RelativePath, now[2].RelativePath); + Assert.Equal("3", File.ReadAllText(whole)); + Assert.False(Directory.Exists(entryFolder)); + } + + [Test] + public async Task APhotoRestoredIntoANewSequenceTakesANameItsCompanionCanFollow() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + var name = fx.Paths.FromRelative(frames[1].RelativePath); + File.WriteAllText(Path.ChangeExtension(name, ".xmp"), "first"); + var first = await DeleteAsync(fx, sequence, frames[1]); + // Another photo filed under the same name since, with an XMP of its own, deleted in turn. + var (path, size, sha) = fx.Stage(Start.AddSeconds(10), 300); + var again = await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, sequence.Id, null, null, path, frames[1].OriginalFileName!, size, sha, null, null), fx.Ct); + Assert.Equal(frames[1].RelativePath, again.RelativePath); + File.WriteAllText(Path.ChangeExtension(name, ".xmp"), "second"); + var row = (await fx.Db.Sequences.GetAsync(sequence.Id))!; + var second = await DeleteAsync(fx, row, (await fx.Db.Frames.GetAsync(again.FrameId))!); + await DeleteSequenceAsync(fx, sequence.Id); + + var one = await fx.Service.RestoreEntryAsync(fx.Archive.Id, first.Entry.Name, null, fx.Ct); + var two = await fx.Service.RestoreEntryAsync(fx.Archive.Id, second.Entry.Name, null, fx.Ct); + + Assert.Equal(one.Sequence.Id, two.Sequence.Id); + var back = await fx.Db.Frames.ListBySequenceAsync(one.Sequence.Id); + Assert.Count(2, back); + foreach (var (frame, xmp) in back.OrderBy(f => f.RelativePath, StringComparer.Ordinal).Zip(["first", "second"])) + { + var file = fx.Paths.FromRelative(frame.RelativePath); + Assert.Equal(frame.Sha256, Sha(file)); + Assert.Equal(xmp, File.ReadAllText(Path.ChangeExtension(file, ".xmp"))); + } + Assert.DoesNotContain(TrashKind.Photos, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Select(e => e.Kind)); + } + + [Test] + public async Task TwoRestoresOfOneDeletedSequenceAtOnceLoseNothing() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 4); + var first = await DeleteAsync(fx, sequence, frames[1]); + var second = await DeleteAsync(fx, (await fx.Db.Sequences.GetAsync(sequence.Id))!, frames[2]); + await DeleteSequenceAsync(fx, sequence.Id); + + var restores = await Task.WhenAll( + Task.Run(() => fx.Service.RestoreEntryAsync(fx.Archive.Id, first.Entry.Name, null, fx.Ct)), + Task.Run(() => fx.Service.RestoreEntryAsync(fx.Archive.Id, second.Entry.Name, null, fx.Ct))); + + Assert.Equal(restores[0].Sequence.Id, restores[1].Sequence.Id); + var back = await fx.Db.Frames.ListBySequenceAsync(restores[0].Sequence.Id); + Assert.SequenceEqual([frames[1].Sha256, frames[2].Sha256], back.Select(f => f.Sha256)); + foreach (var frame in back) Assert.Equal(frame.Sha256, Sha(fx.Paths.FromRelative(frame.RelativePath))); + Assert.False(Directory.Exists(EntryFolder(fx, first.Entry))); + Assert.False(Directory.Exists(EntryFolder(fx, second.Entry))); + } + + [Test] + public async Task AnOriginalAStoppedRestoreLeftStagedSurvivesMaintenanceAndComesBack() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 4); + var deleted = await DeleteAsync(fx, sequence, frames[1], frames[2]); + await DeleteSequenceAsync(fx, sequence.Id); + var entryFolder = EntryFolder(fx, deleted.Entry); + // A restore into a new sequence stopped (the service, or a crash) with an original staged in its entry, days ago. + var name = Path.GetFileName(frames[2].RelativePath); + var staged = Path.Combine(entryFolder, ".restoring", "originals", name); + Directory.CreateDirectory(Path.GetDirectoryName(staged)!); + File.Move(Path.Combine(entryFolder, "originals", name), staged); + File.SetLastWriteTimeUtc(staged, DateTime.UtcNow.AddDays(-3)); + + await fx.Service.Maintenance.RunAsync(fx.Archive, dryRun: false, 0, _ => ValueTask.CompletedTask, fx.Ct); + + Assert.Equal(frames[2].Sha256, Sha(staged)); + var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct); + + Assert.Equal(2, restored.Photos); + Assert.Empty(restored.Kept); + var back = await fx.Db.Frames.ListBySequenceAsync(restored.Sequence.Id); + Assert.SequenceEqual([frames[1].Sha256, frames[2].Sha256], back.Select(f => f.Sha256)); + foreach (var frame in back) Assert.Equal(frame.Sha256, Sha(fx.Paths.FromRelative(frame.RelativePath))); + Assert.False(Directory.Exists(entryFolder)); + Assert.False(Directory.Exists(fx.Paths.StagingDirectory) && Directory.EnumerateDirectories(fx.Paths.StagingDirectory, "trash-*").Any()); + } + + [Test] + public async Task ARestoreIntoANewSequenceThatFailsPutsTheOriginalBack() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + var deleted = await DeleteAsync(fx, sequence, frames[1]); + await DeleteSequenceAsync(fx, sequence.Id); + var entryFolder = EntryFolder(fx, deleted.Entry); + var original = Path.Combine(entryFolder, "originals", Path.GetFileName(frames[1].RelativePath)); + await FailAsync(fx, "sequences", "INSERT"); + + // The ingest fails after the original left the entry, with no file error: it goes back all the same. + await Assert.ThrowsAsync(() => fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct)); + + Assert.Equal(frames[1].Sha256, Sha(original)); + Assert.False(Directory.Exists(Path.Combine(entryFolder, ".restoring"))); + var listed = Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == deleted.Entry.Name); + Assert.Equal((1, deleted.Entry.Bytes), (listed.Frames, listed.Bytes)); + + await HealAsync(fx, "sequences"); + var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct); + Assert.Equal(1, restored.Photos); + Assert.Equal(frames[1].Sha256, (await fx.Db.Frames.ListBySequenceAsync(restored.Sequence.Id)).Single().Sha256); + Assert.False(Directory.Exists(entryFolder)); + } + + [Test] + public async Task ARestoreACrashInterruptedBeforeTheSidecarIsUndoneByTheNextWriter() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 4); + var folder = fx.Paths.Sequence(sequence.Folder); + var deleted = await DeleteAsync(fx, sequence, frames[1], frames[2]); + // The journal was written and one of two originals moved back when the process died; the sidecar lists neither. + var journal = RestoreJournal(fx, deleted.Entry); + journal.Write(folder); + var moved = journal.Photos[0].Moves[0]; + File.Move(fx.Paths.FromRelative(moved.To), folder.File(moved.From)); + + // Any writer of the sequence reads its sidecar under the lease first. + await fx.Service.SetSequenceInfoAsync(sequence.Id, "Harbour", [], 0, null, fx.Ct); + + Assert.FileMissing(folder.RestoreJournalPath); + Assert.FileMissing(folder.File(moved.From)); + Assert.Equal(frames[1].Sha256, Sha(fx.Paths.FromRelative(moved.To))); + Assert.Count(2, ManifestStore.ReadSequence(folder.ManifestPath).Frames); + Assert.Equal(2, Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == deleted.Entry.Name).Frames); + + var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct); + Assert.Equal(2, restored.Photos); + Assert.SequenceEqual(frames.Select(f => f.Id), (await fx.Db.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id)); + } + + [Test] + public async Task ARestoreACrashInterruptedAfterTheSidecarIsCompletedByTheNextWriter() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 4); + var folder = fx.Paths.Sequence(sequence.Folder); + var deleted = await DeleteAsync(fx, sequence, frames[1], frames[2]); + var entryFolder = EntryFolder(fx, deleted.Entry); + // Every file moved back and the sidecar was written when the process died; the trash record still lists the photos. + var journal = RestoreJournal(fx, deleted.Entry); + journal.Write(folder); + foreach (var move in journal.Photos.SelectMany(p => p.Moves)) File.Move(fx.Paths.FromRelative(move.To), folder.File(move.From)); + var manifest = ManifestStore.ReadSequence(folder.ManifestPath); + var back = SequenceFrames.Insert(manifest, [.. FrameTrash.ReadEntry(fx.Paths, deleted.Entry.Name)!.Photos.Select(p => p.Frame)]); + ManifestStore.WriteSequence(folder.ManifestPath, back with { Removed = null }); + + // The journal makes a scan read the folder under the lease, which settles the restore and indexes its frames. + await fx.ScanAsync(); + + Assert.FileMissing(folder.RestoreJournalPath); + Assert.DoesNotContain(deleted.Entry.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Select(e => e.Name)); + Assert.False(Directory.Exists(entryFolder)); + Assert.FileMissing(FrameTrash.Location(fx.Paths, deleted.Entry.Name).Record); + Assert.SequenceEqual(frames.Select(f => f.Id), (await fx.Db.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id)); + foreach (var frame in frames) Assert.Equal(frame.Sha256, Sha(fx.Paths.FromRelative(frame.RelativePath))); + } + + [Test] + public async Task ARestoreCancelledBeforeItsFirstMoveLeavesEverythingAsItWas() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + var folder = fx.Paths.Sequence(sequence.Folder); + var deleted = await DeleteAsync(fx, sequence, frames[1]); + using var cancel = CancellationTokenSource.CreateLinkedTokenSource(fx.Ct); + + // Another process holds the sequence: the restore waits for it, and is cancelled meanwhile. + using (Assert.NotNull(LeaseFile.TryAcquire(fx.Paths.LocksDirectory, $"seq-{sequence.Id:N}", "other"))) + { + var restore = fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, cancel.Token); + cancel.CancelAfter(TimeSpan.FromMilliseconds(200)); + await Assert.ThrowsAsync(() => restore); + } + + Assert.FileMissing(folder.RestoreJournalPath); + Assert.FileMissing(fx.Paths.FromRelative(frames[1].RelativePath)); + Assert.Equal(frames[1].Sha256, Sha(Path.Combine(EntryFolder(fx, deleted.Entry), "originals", Path.GetFileName(frames[1].RelativePath)))); + Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == deleted.Entry.Name); + Assert.Equal(1, (await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct)).Photos); + } + + [Test] + public async Task APhotoWhoseCompanionCannotFollowStaysWholeInTheTrash() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + var folder = fx.Paths.Sequence(sequence.Folder); + var photo = fx.Paths.FromRelative(frames[1].RelativePath); + File.WriteAllText(Path.ChangeExtension(photo, ".xmp"), "edits"); + var deleted = await DeleteAsync(fx, sequence, frames[1]); + // Something that is no file holds the companion's place: the original moves back in, the XMP cannot follow it. + Directory.CreateDirectory(Path.ChangeExtension(photo, ".xmp")); + + var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct); + + Assert.Equal(0, restored.Photos); + Assert.SequenceEqual([frames[1].OriginalFileName!], restored.Kept); + Assert.FileMissing(photo); + Assert.FileMissing(folder.RestoreJournalPath); + var entryFolder = EntryFolder(fx, deleted.Entry); + Assert.Equal(frames[1].Sha256, Sha(Path.Combine(entryFolder, "originals", Path.GetFileName(photo)))); + Assert.Equal("edits", File.ReadAllText(Path.Combine(entryFolder, "originals", Path.GetFileName(Path.ChangeExtension(photo, ".xmp"))))); + Assert.Count(2, ManifestStore.ReadSequence(folder.ManifestPath).Frames); + Assert.True(await fx.Db.FrameRemovals.IsRemovedAsync(sequence.Id, frames[1].Sha256!)); + var listed = Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == deleted.Entry.Name); + Assert.Equal((1, 2), (listed.Frames, listed.Files)); + } + + [Test] + public async Task AnInterruptedDeletionIsNeitherListedNorEmptiedAndAScanUndoesIt() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + var folder = fx.Paths.Sequence(sequence.Folder); + // As a deletion leaves it when the process dies after its first move: journal, record under its pending name, one file moved. + var name = $"{sequence.Id:N}-20261027120000"; + var entry = Path.Combine(FrameTrash.Directory(fx.Paths), name); + var record = entry + ".json"; + var sidecar = ManifestStore.ReadSequence(folder.ManifestPath).Frames.Take(2).ToList(); + var files = sidecar.Select(f => new TrashedFile(f.File, "originals/" + Path.GetFileName(f.File), f.Size, f.Sha256)).ToList(); + Directory.CreateDirectory(Path.Combine(entry, "originals")); + File.WriteAllText(FrameRemovalJournal.PendingRecord(record), JsonSerializer.Serialize(new PhotoTrashRecord(TrashKind.Photos, sequence.Id, sequence.DisplayTitle, + sequence.Folder, [.. sidecar.Zip(files, (f, t) => new TrashedPhoto(f, [t], null))], files.Sum(f => f.Size), DateTimeOffset.UtcNow, "alberto"), + TrashJson.Default.PhotoTrashRecord)); + var moves = files.Select(f => new FrameRemovalMove(f.From, fx.Paths.ToRelative(Path.Combine(entry, f.Path)))).ToList(); + FrameRemovalJournal.Write(folder, new FrameRemovalJournal(fx.Paths.ToRelative(record), fx.Paths.ToRelative(entry), [.. sidecar.Select(f => f.Id)], moves)); + File.Move(folder.File(moves[0].From), fx.Paths.FromRelative(moves[0].To)); + + Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)); + Assert.Empty((await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct)).Entries); + await Assert.ThrowsAsync(() => fx.Service.RestoreEntryAsync(fx.Archive.Id, FrameTrash.EntryPrefix + name, null, fx.Ct)); + + // The journal makes a scan read the folder under the lease, which undoes the deletion. + await fx.ScanAsync(); + + foreach (var frame in frames) Assert.Equal(frame.Sha256, Sha(fx.Paths.FromRelative(frame.RelativePath))); + Assert.FileMissing(folder.RemovalJournalPath); + Assert.FileMissing(FrameRemovalJournal.PendingRecord(record)); + Assert.False(Directory.Exists(entry)); + Assert.Count(3, await fx.Db.Frames.ListBySequenceAsync(sequence.Id)); + } + + [Test] + public async Task ADeletionWhoseSidecarWasWrittenIsListedByTheNextWriter() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + var folder = fx.Paths.Sequence(sequence.Folder); + var deleted = await DeleteAsync(fx, sequence, frames[1]); + var (entry, record) = FrameTrash.Location(fx.Paths, deleted.Entry.Name); + // As a deletion leaves it when the process dies after its sidecar was written: the record not yet under its own name. + File.Move(record, FrameRemovalJournal.PendingRecord(record)); + FrameRemovalJournal.Write(folder, new FrameRemovalJournal(fx.Paths.ToRelative(record), fx.Paths.ToRelative(entry), [frames[1].Id], [])); + Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)); + + await fx.Service.SetSequenceInfoAsync(sequence.Id, "Harbour", [], 0, null, fx.Ct); + + Assert.FileMissing(folder.RemovalJournalPath); + Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == deleted.Entry.Name); + } + + [Test] + [WindowsOnly] + public async Task ADeletionWhoseSidecarCannotBeWrittenPutsEveryPhotoBack() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + var folder = fx.Paths.Sequence(sequence.Folder); + var plan = await fx.Service.FrameTrash.PlanDeleteAsync(sequence.Id, [frames[0].Id, frames[2].Id], sequence.FramesVersion, fx.Ct); + + // Another program holds the sidecar open: it cannot be replaced. + Exception error; + using (new FileStream(folder.ManifestPath, FileMode.Open, FileAccess.Read, FileShare.Read)) + error = await Assert.ThrowsAsync(() => fx.Service.FrameTrash.DeleteAsync(plan, "alberto", null, fx.Ct)); + + Assert.True(error is IOException or UnauthorizedAccessException, error.ToString()); + foreach (var frame in frames) Assert.Equal(frame.Sha256, Sha(fx.Paths.FromRelative(frame.RelativePath))); + Assert.FileMissing(folder.RemovalJournalPath); + Assert.Empty(Directory.EnumerateFileSystemEntries(FrameTrash.Directory(fx.Paths))); + Assert.Count(3, ManifestStore.ReadSequence(folder.ManifestPath).Frames); + } + + [Test] + public async Task APhotoNeverHashedIsHashedAsItGoesAndLeavesItsTombstone() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await AdoptedAsync(fx, "Shoot", 4); + Assert.True(frames.All(f => f.Sha256 is null), "adopted, not verified"); + var sha = Sha(fx.Paths.FromRelative(frames[1].RelativePath)); + + var deleted = await DeleteAsync(fx, sequence, frames[1]); + + var folder = fx.Paths.Sequence(sequence.Folder); + Assert.SequenceEqual([sha], Assert.NotNull(ManifestStore.ReadSequence(folder.ManifestPath).Removed).Select(r => r.Sha256)); + Assert.True(await fx.Db.FrameRemovals.IsRemovedAsync(sequence.Id, sha)); + Assert.Equal(sha, Sha(Path.Combine(EntryFolder(fx, deleted.Entry), "originals", Path.GetFileName(frames[1].RelativePath)))); + + // One whose original is gone as well could leave none: the plan refuses it, and says which. + File.Delete(fx.Paths.FromRelative(frames[3].RelativePath)); + var row = (await fx.Db.Sequences.GetAsync(sequence.Id))!; + var refused = await fx.Service.FrameTrash.PlanDeleteAsync(sequence.Id, [frames[2].Id, frames[3].Id], row.FramesVersion, fx.Ct); + Assert.SequenceEqual([FrameDeleteBlock.Unhashable], refused.Blocked); + Assert.SequenceEqual([frames[3].Id], refused.Unhashable.Select(f => f.Id)); + await Assert.ThrowsAsync(() => fx.Service.FrameTrash.DeleteAsync(refused, null, null, fx.Ct)); + } + + [Test] + public async Task ADuplicateThatStaysNeverHashedKeepsItsContentOutOfTheTombstones() + { + var fx = await FixtureAsync(); + var shoot = Path.Combine(fx.Archive.RootPath, "Twins"); + var files = Photos.WriteSeries(shoot, Start, 3); + File.Copy(files[1], Path.Combine(shoot, "IMG_0009.JPG")); + await fx.ScanAsync(); + var id = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "Twins").Id; + var frames = await fx.Db.Frames.ListBySequenceAsync(id); + var twin = frames.Single(f => f.RelativePath.EndsWith("IMG_0002.JPG", StringComparison.Ordinal)); + + await DeleteAsync(fx, (await fx.Db.Sequences.GetAsync(id))!, twin); + + Assert.Null(ManifestStore.ReadSequence(fx.Paths.Sequence("Twins").ManifestPath).Removed); + Assert.False(await fx.Db.FrameRemovals.IsRemovedAsync(id, Sha(Path.Combine(shoot, "IMG_0009.JPG")))); + } + + [Test] + public async Task ARestoreBringsBackAPhotoWhoseOnlyTwinInTheSequenceIsMissing() + { + var fx = await FixtureAsync(); + var shoot = Path.Combine(fx.Archive.RootPath, "Twins"); + var files = Photos.WriteSeries(shoot, Start, 3); + File.Copy(files[1], Path.Combine(shoot, "IMG_0009.JPG")); + await fx.ScanAsync(); + var id = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "Twins").Id; + await fx.Service.Verifier.VerifyAsync(fx.Archive, [id], default, (_, _, _) => ValueTask.CompletedTask, null, fx.Ct); + var twins = (await fx.Db.Frames.ListBySequenceAsync(id)).Where(f => f.Sha256 == Sha(files[1])).ToList(); + Assert.Count(2, twins); + var deleted = await DeleteAsync(fx, (await fx.Db.Sequences.GetAsync(id))!, twins[1]); + Assert.False(await fx.Db.FrameRemovals.IsRemovedAsync(id, twins[1].Sha256!), "its twin holds the content"); + // The twin that stayed loses its file since; a scan flags it missing. + File.Delete(fx.Paths.FromRelative(twins[0].RelativePath)); + await fx.ScanAsync(); + Assert.True((await fx.Db.Frames.GetAsync(twins[0].Id))!.Flags.HasFlag(FrameFlags.Missing)); + + var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct); + + Assert.Equal(1, restored.Photos); + Assert.Empty(restored.Kept); + var back = Assert.NotNull(await fx.Db.Frames.GetAsync(twins[1].Id)); + Assert.Equal(twins[1].Sha256, Sha(fx.Paths.FromRelative(back.RelativePath))); + Assert.False(back.Flags.HasFlag(FrameFlags.Missing)); + Assert.False(Directory.Exists(EntryFolder(fx, deleted.Entry))); + } + + [Test] + public async Task ASidecarRebuiltFromThePhotosKeepsTheTombstones() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + await DeleteAsync(fx, sequence, frames[0]); + var folder = fx.Paths.Sequence(sequence.Folder); + File.WriteAllText(folder.ManifestPath, "{ \"damaged\": "); + + await fx.ScanAsync(); + + Assert.True(await fx.Db.FrameRemovals.IsRemovedAsync(sequence.Id, frames[0].Sha256!)); + Assert.SequenceEqual([frames[0].Sha256], Assert.NotNull(ManifestStore.ReadSequence(folder.ManifestPath).Removed).Select(r => r.Sha256)); + + // A sequence restored from the trash without its sidecar takes them back from its record. + await DeleteSequenceAsync(fx, sequence.Id); + var entry = (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single(e => e.Kind == TrashKind.Sequence); + File.Delete(Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), entry.Name, ".chronos", "sequence.json")); + await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct); + + Assert.True(await fx.Db.FrameRemovals.IsRemovedAsync(sequence.Id, frames[0].Sha256!)); + } + + [Test] + public async Task ADeletionStandsWhenTheIndexCannotFollowAndTheNextScanResyncsIt() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 3); + await FailAsync(fx, "frame_removals", "INSERT"); + + var result = await DeleteAsync(fx, sequence, frames[1]); + + Assert.FileMissing(fx.Paths.FromRelative(frames[1].RelativePath)); + Assert.Equal((sequence.FramesVersion + 1, 2), (result.Sequence.FramesVersion, result.Sequence.FrameCount)); + Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == result.Entry.Name); + Assert.Equal(sequence.FramesVersion, (await fx.Db.Sequences.GetAsync(sequence.Id))!.FramesVersion); + + await HealAsync(fx, "frame_removals"); + await fx.ScanAsync(); + + Assert.Equal(result.Sequence.FramesVersion, (await fx.Db.Sequences.GetAsync(sequence.Id))!.FramesVersion); + Assert.SequenceEqual([frames[0].Id, frames[2].Id], (await fx.Db.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id)); + Assert.True(await fx.Db.FrameRemovals.IsRemovedAsync(sequence.Id, frames[1].Sha256!)); + } + + [Test] + public async Task ThumbnailsFollowTheirFramesThroughADeletionAndARestore() + { + var fx = await FixtureAsync(); + var (sequence, frames) = await UploadedAsync(fx, 5); + async Task ThumbnailsAsync() => + await fx.Service.Images.BuildThumbnailsAsync(fx.Archive, (await fx.Db.Sequences.GetAsync(sequence.Id))!, false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + Assert.Equal(5, (await ThumbnailsAsync()).Built); + + var deleted = await DeleteAsync(fx, (await fx.Db.Sequences.GetAsync(sequence.Id))!, frames[1]); + var after = await ThumbnailsAsync(); + Assert.Equal((0, 4), (after.Built, after.Reused)); + + await fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct); + var again = await ThumbnailsAsync(); + Assert.Equal((1, 4), (again.Built, again.Reused)); + } + + [Test] + public async Task AnEntryRestoredAndDeletedAgainWhileTheTrashEmptiesIsKept() + { + var fx = await FixtureAsync(); + var (a, _) = await UploadedAsync(fx, 2); + var (b, _) = await UploadedAsync(fx, 2, "upload-2", 200); + await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([a.Id, b.Id], null, fx.Ct), null, null, fx.Ct); + var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct); + var items = plan.Entries.OrderBy(e => e.Name, StringComparer.Ordinal).Select(e => new EmptyTrashItem(e.Name, SequenceTrash.EntryFingerprint(e))).ToList(); + var later = items[1].Name; + + // Another host sharing the archive restores the second entry and deletes its sequence again once the first is erased. + var result = await fx.Service.Trash.EmptyAsync(fx.Archive, new EmptyTrashParameters(items, plan.Fingerprint), 0, async (done, _) => + { + if (done != 1) return; + var back = await fx.Service.RestoreAsync(fx.Archive.Id, later, fx.Ct); + await DeleteSequenceAsync(fx, back.Id); + }, fx.Ct); + + Assert.Equal(1, result.Erased); + Assert.Count(1, result.Refused); + var kept = Assert.Contains(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct), e => e.Name == later); + Assert.True(kept.Files > 0); + Assert.FileExists(Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), later, ".chronos", "sequence.json")); + } + + [Test] + public async Task AResumedEmptyingKeepsAnEntryThatChangedSinceThePlan() + { + var fx = await FixtureAsync(); + var (a, _) = await UploadedAsync(fx, 2); + var (b, _) = await UploadedAsync(fx, 2, "upload-2", 200); + await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([a.Id, b.Id], null, fx.Ct), null, null, fx.Ct); + var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct); + var names = plan.Entries.Select(e => e.Name).Order(StringComparer.Ordinal).ToList(); + File.WriteAllBytes(Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), names[1], "added.bin"), [1, 2, 3]); + var engine = await fx.StartEngineAsync(); + + // An earlier run erased the first entry and stopped; the second changed since the plan. + var job = await fx.EnqueueWithCheckpointAsync(engine, ArchiveJobs.EmptyTrash(fx.Archive, plan), "done:1"); + var failed = await fx.WaitForAsync(job.Id, JobState.Failed); + + Assert.Equal(Chronos.Core.Localization.Loc.Plural("Archive.Error.TrashKept", 1), failed.Error); + var listed = (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Select(e => e.Name).ToList(); + Assert.Contains(names[0], listed); + Assert.Contains(names[1], listed); + Assert.FileExists(Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), names[1], "added.bin")); + } + /// An entry of originals a conversion replaced (a later phase), its one photo replaced by . private static string WriteReplaced(ArchiveFixture fx, Sequence sequence, string stamp, string replacement) { diff --git a/tests/Chronos.Server.Tests/Storage/ArchiveModuleTests.cs b/tests/Chronos.Server.Tests/Storage/ArchiveModuleTests.cs index 145424e8..cabfb3ad 100644 --- a/tests/Chronos.Server.Tests/Storage/ArchiveModuleTests.cs +++ b/tests/Chronos.Server.Tests/Storage/ArchiveModuleTests.cs @@ -208,6 +208,36 @@ public sealed class ArchiveModuleTests(TestContext context) Assert.True(Assert.NotNull(await server.Database.Sequences.GetAsync(roof.Id)).Status.HasFlag(SequenceStatus.ThumbsBuilt), "built"); } + [Test] + public async Task AThumbnailOfAnotherPhotoAtAFramesIndexIsNotServedEvenBeforeAVerify() + { + await using var server = await TestServer.StartAsync(context, options => + { + Directory.CreateDirectory(Path.Combine(options.ArchivePath, "roof")); + File.WriteAllBytes(Path.Combine(options.ArchivePath, "roof", "IMG_0001.JPG"), Photo()); + File.WriteAllBytes(Path.Combine(options.ArchivePath, "roof", "IMG_0002.JPG"), Photo(seed: 3)); + }, ArchiveModule.Register); + var archive = server.Services.GetRequiredService(); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + await UntilIndexedAsync(server, archive.Id, "roof"); + await UntilScannedAsync(server); + await UntilImagesAsync(server, editor, archive.Id, "roof"); + await UntilIdleAsync(server); + var roof = (await server.Database.Sequences.ListLocationsAsync(archive.Id)).Single(l => l.Folder == "roof"); + var frames = await server.Database.Frames.ListBySequenceAsync(roof.Id); + Assert.True(frames.All(f => f.Sha256 is null), "adopted, not verified"); + + // The pack holds each photo's thumbnail at the other's index (a renumbering it did not follow). + using (var pack = ThumbPackWriter.Open(new ArchivePaths(server.Options.ArchivePath).Sequence("roof").ThumbsPackPath)) + pack.Rekey(new Dictionary { [0] = 1, [1] = 0 }); + + foreach (var frame in frames) + { + using var thumb = await editor.GetAsync($"/api/v1/frames/{frame.Id}/thumb"); + Assert.Equal(HttpStatusCode.NotFound, thumb.StatusCode); + } + } + [Test] public async Task SettingsADesktopChangedInArchiveJsonReachUploadsAndTheAdminPanel() {