diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 2b46a459..12cf3a52 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -40,7 +40,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S
| `Render` | Render plan (timing modes), preparation pass, geometry, `RenderPipeline`, self-test suite |
| `Export` | Preset resolution, encoder selection with fallbacks, output naming |
| `Video` (+ `Mp4`, `Avi`, `Bitstream`, `Yuv`, `Managed`) | `IVideoEncoderProvider`, MP4 muxer and inspector, AVI-MJPEG, NV12/P010, managed encoders (MJPEG-AVI, GIF, APNG, image sequences) |
-| `Archiving` | Archive layout and paths, sidecars (`sequence.json`, `project.json`), scanner, index snapshot, leases, thumbnail packs, verify, adopt, rename plans (`RenamePlanner`: folder, title and file templates, `RenameTemplates`, the plan's fingerprint; `FileRenames`: a sequence's new file names; `FileRenameJournal`: two-phase renames through temporary names, `.chronos/rename-files.json`; `FrameCompanions`: the XMP files that follow a photo, one rule for the trash and Reorganize, matched whatever their case and each folder listed once by `FrameCompanions.Index`) |
+| `Archiving` | Archive layout and paths, sidecars (`sequence.json`, `project.json`), scanner, index snapshot, leases, thumbnail packs, verify, adopt, rename plans (`RenamePlanner`: folder, title and file templates, `RenameTemplates`, the plan's fingerprint; `FileRenames`: a sequence's new file names; `FileRenameJournal`: two-phase renames through temporary names, `.chronos/rename-files.json`, a recovery safe to run again from wherever it stopped, a new name taken meanwhile sending that photo and its companions back to their old names or a free `_2`; `FrameCompanions`: the XMP files that follow a photo, one rule for the trash and Reorganize, matched whatever their case and each folder listed once by `FrameCompanions.Index`) |
| `Import` | Card scan, session detection, import plan, hash-verified copy, card eraser (re-hash before delete) |
| `Naming` | Folder/file templates and tokens; `FrameNaming` (the naming context and unique file name of a frame, shared by import, upload and Reorganize) |
| `Model` | Domain entities and enums (§5) shared by Data, Server and clients |
@@ -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` (mass rename and Reorganize: the dry run reads each sidecar for a file template; steps move the folder, then the title, then the files under the plan's journal in `journal/`, a plan its job held left to that job; the file renames of a step checked against the planned frames version, then sidecar, index (`FrameRepository.RenameFilesAsync`) and the sequence's journal), `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; one whose original is missing goes without a file or tombstone; a frame that stays and may be a twin is hashed, its hash kept), positions parked and placed back, proxies and thumbnails moved to follow their frames; `FrameTrash.Restore`: restore re-hashes each file and says why each kept photo stayed (`KeptReason`), under a journal of its own into the sequence that another writer settles only under the entry's lease, into a new sequence through the ingest when theirs is gone, each original staged inside its own entry and the record rewritten after each photo filed), 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` (mass rename and Reorganize: the dry run reads each sidecar for a file template; each step checked under its leases before anything of it moves (where its sequences are, what is inside and at its new folder, a sidecar of a newer format, the title it was planned from, its file renames by digest at the planned frames version), then the folder, every index row, then the files under the plan's journal in `journal/` (a sequence whose files are found changed or in use there keeps their names, and the step completes); the journal keeps the tally of the whole plan, and a plan its job held (paused, cancelled or failed) is left to that job, only its half-done file renames completed at an archive open; the file renames of a step: sidecar, index (`FrameRepository.RenameFilesAsync`) and the sequence's journal), `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; one whose original is missing goes without a file or tombstone; a frame that stays and may be a twin is hashed, its hash kept), positions parked and placed back, proxies and thumbnails moved to follow their frames; `FrameTrash.Restore`: restore re-hashes each file and says why each kept photo stayed (`KeptReason`), under a journal of its own into the sequence that another writer settles only under the entry's lease, into a new sequence through the ingest when theirs is gone, each original staged inside its own entry and the record rewritten after each photo filed), 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`) |
@@ -115,7 +115,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S
| `Backup` | `Housekeeping/BackupJobHandler` (via `ArchiveService.CreateJobHandlers`; an SQLite index file only) | yes | SQLite only |
| `Cleanup` | `Housekeeping/CleanupJobHandler` (shared steps, plus the host's from `ArchiveServiceOptions.CleanupSteps`) | yes | yes |
| `EmptyTrash` | `Archiving/Jobs/EmptyTrashJobHandler` (erases exactly a confirmed plan's trash entries; refuses a stale fingerprint) | yes | yes |
-| `Move` | `Archiving/Jobs/MoveJobHandler` (Reorganize: plans again from the templates, applies the plan only while it is the one confirmed, holds it when paused or cancelled) | yes | yes |
+| `Move` | `Archiving/Jobs/MoveJobHandler` (Reorganize: plans again from the templates, applies the plan only while it is the one confirmed, holds it when paused, cancelled or failed, tells each sequence changed as its step completes) | yes | yes |
| `Adopt` | none: done synchronously (`ArchiveService` actions) | — | — |
A job engine claims only kinds it has handlers for. Light analysis is queued by the engine (`JobContext` follow-up)
diff --git a/i18n/en/Archive.resx b/i18n/en/Archive.resx
index 073e95ed..6a661956 100644
--- a/i18n/en/Archive.resx
+++ b/i18n/en/Archive.resx
@@ -87,6 +87,8 @@
{0} was written by a newer Chronos (format {1}).long: error sentence
The archive {0} is offline.long: error sentence
The archive {0} is read-only.long: error sentence
+ {0:N0} sequence kept its file names: a photo was in use.long: error sentence of a Reorganize job
+ {0:N0} sequences kept their file names: a photo was in use.long: error sentence of a Reorganize job
{0:N0} sequence was not reorganized as planned: it changed after the plan.long: error sentence of a Reorganize job
{0:N0} sequences were not reorganized as planned: they changed after the plan.long: error sentence of a Reorganize job
{0} changed after the plan was made.long: error sentence
@@ -155,7 +157,9 @@
{0}: made {1}, reused {2}, unreadable {3}, waiting {4}long: job log line; {4} frames whose proxy this host waits for from a client or render node
Proxies for the server: {0} sent, {1} not sentlong: job log line; {0} proxies sent; {1} proxies this PC could not make or the server refused
Left as it was, changed after the plan: {0}long: job log line; {0} sequence title or folder
+ File names kept, a photo was in use: {0}long: job log line; {0} sequence title or folder
File names kept, photos changed after the plan: {0}long: job log line; {0} sequence title or folder
+ Title and file names kept, changed after the plan: {0}long: job log line; {0} sequence title or folder
Sequences changed: {0}, files renamed: {1}long: job log line of a Reorganize; {0} sequences, {1} photos
The reorganization was completed when the archive was openedlong: job log line
Report only: nothing was changed.long: job log line
diff --git a/i18n/it/Archive.it.resx b/i18n/it/Archive.it.resx
index 242aa020..bb9bae0d 100644
--- a/i18n/it/Archive.it.resx
+++ b/i18n/it/Archive.it.resx
@@ -82,6 +82,8 @@
{0} è stato scritto da un Chronos più recente (formato {1}).
L'archivio {0} non è raggiungibile.
L'archivio {0} è di sola lettura.
+ {0:N0} sequenza ha mantenuto i nomi dei file: una foto era in uso.
+ {0:N0} sequenze hanno mantenuto i nomi dei file: una foto era in uso.
{0:N0} sequenza non è stata riorganizzata come previsto: è cambiata dopo il piano.
{0:N0} sequenze non sono state riorganizzate come previsto: sono cambiate dopo il piano.
{0} è cambiato dopo la creazione del piano.
@@ -150,7 +152,9 @@
{0}: create {1}, riusate {2}, illeggibili {3}, in attesa {4}
Proxy per il server: {0} inviati, {1} non inviati
Lasciata com'era, cambiata dopo il piano: {0}
+ Nomi dei file mantenuti, una foto era in uso: {0}
Nomi dei file mantenuti, foto cambiate dopo il piano: {0}
+ Titolo e nomi dei file mantenuti, cambiata dopo il piano: {0}
Sequenze cambiate: {0}, file rinominati: {1}
La riorganizzazione è stata completata all'apertura dell'archivio
Solo resoconto: nulla è stato modificato.
diff --git a/src/Chronos.Core/Archiving/FileRenameJournal.cs b/src/Chronos.Core/Archiving/FileRenameJournal.cs
index f0669627..55df0543 100644
--- a/src/Chronos.Core/Archiving/FileRenameJournal.cs
+++ b/src/Chronos.Core/Archiving/FileRenameJournal.cs
@@ -1,3 +1,4 @@
+using System.Globalization;
using System.Text.Json;
using Chronos.Core.Diagnostics;
using Chronos.Core.IO;
@@ -63,10 +64,20 @@ public sealed record FileRenameJournal(int FramesVersion, int Phase, IReadOnlyLi
}
}
+ ///
+ /// Deletes the journal once its files are where it says; one whose temporary files are not all gone stays (logged), so
+ /// no photo is ever left at a temporary name without a record of it: the next completes it.
+ ///
public static void Delete(SequencePaths sequence)
{
ArgumentNullException.ThrowIfNull(sequence);
- if (File.Exists(sequence.FileRenameJournalPath)) File.Delete(sequence.FileRenameJournalPath);
+ if (!File.Exists(sequence.FileRenameJournalPath)) return;
+ if (Read(sequence) is { } journal && journal.Files.FirstOrDefault(f => Exists(sequence, f.Temp)) is { } left)
+ {
+ Log.Error("archive", $"The rename journal of {sequence.RelativeFolder} stays: {sequence.ArchiveRelative(left.Temp)} is still at its temporary name");
+ return;
+ }
+ File.Delete(sequence.FileRenameJournalPath);
}
///
@@ -112,37 +123,98 @@ public sealed record FileRenameJournal(int FramesVersion, int Phase, IReadOnlyLi
/// Completes a rename a crash interrupted (the caller holds the sequence's lease): the files go on to their new names,
/// and names them (), unless it does already. Returns
/// the sidecar to write, or null when it is right as it is; the journal is left in place, it once
- /// the result is written. A file found nowhere is logged, and its frame names the new file all the same (a verify then
- /// flags it missing, as it would have at its old name).
+ /// the result is written. Safe to run again from wherever it stopped (a failure, a crash, a sidecar that could not be
+ /// written): once every file is at its temporary name the journal says phase 2, as does, so no file
+ /// that reached its new name is ever moved again. A new name something else took meanwhile is never written over: the
+ /// files of that frame go back to their old names, or to the first free _2, _3… of them, recorded in the
+ /// journal before they move, and the frame names where its photo is (logged). A file found nowhere is logged, and its
+ /// frame names the new file all the same (a verify then flags it missing, as it would have at its old name).
///
+ /// A file could not be moved (open elsewhere); the journal stays, and the next recovery goes on from there.
public static SequenceManifest? Recover(SequencePaths sequence, SequenceManifest manifest)
{
ArgumentNullException.ThrowIfNull(sequence);
ArgumentNullException.ThrowIfNull(manifest);
if (Read(sequence) is not { } journal) return null;
- foreach (var file in journal.Files)
+ if (journal.Phase == 1)
{
- // In phase 2 no file is at its old name any more: what is there now came since, and stays.
- if (journal.Phase == 1 && Exists(sequence, file.From) && !Exists(sequence, file.Temp)) Move(sequence, file.From, file.Temp);
+ foreach (var file in journal.Files)
+ {
+ // What is at an old name beside its file's temporary one came since, and stays.
+ if (Exists(sequence, file.From) && !Exists(sequence, file.Temp)) Move(sequence, file.From, file.Temp);
+ }
+ // Every file is at its temporary name (or nowhere): from here on no old name is moved from again.
+ journal = journal with { Phase = 2 };
+ Write(sequence, journal);
}
+ journal = Redirected(sequence, journal);
foreach (var file in journal.Files)
{
- if (Exists(sequence, file.Temp) && !Exists(sequence, file.To)) Move(sequence, file.Temp, file.To);
+ if (Exists(sequence, file.Temp)) Move(sequence, file.Temp, file.To);
else if (!Exists(sequence, file.To)) Log.Warning("archive", $"{sequence.ArchiveRelative(file.To)} was not found after an interrupted rename");
}
Log.Warning("archive", $"An interrupted rename of the files of {sequence.RelativeFolder} was completed");
return Applied(manifest, journal);
}
+ ///
+ /// The journal with a new name for every file still at its temporary name of a frame one of whose new names something
+ /// else took since (a file, or a folder): all the files of that frame (its photo and companions, which stay together) go
+ /// back to their old names, or to the first _2, _3… of the photo's old name that is free for all of them,
+ /// none ever a name the rename gives another file. Written before anything moves, so a recovery run again finds them.
+ ///
+ private static FileRenameJournal Redirected(SequencePaths sequence, FileRenameJournal journal)
+ {
+ var claimed = new HashSet(journal.Files.Select(f => f.To), StringComparer.OrdinalIgnoreCase);
+ List? files = null;
+ foreach (var frame in journal.Files.Select((f, i) => (File: f, Index: i)).GroupBy(f => f.File.FrameId))
+ {
+ var waiting = frame.Where(f => Exists(sequence, f.File.Temp)).ToList();
+ if (!waiting.Any(f => Taken(sequence, f.File.To))) continue;
+ var photo = frame.FirstOrDefault(f => !f.File.Companion).File ?? frame.First().File;
+ List names;
+ for (var n = 1; ; n++)
+ {
+ names = waiting.ConvertAll(f => Numbered(f.File, photo, n));
+ if (names.TrueForAll(name => !claimed.Contains(name) && !Taken(sequence, name)) && names.Distinct(StringComparer.OrdinalIgnoreCase).Count() == names.Count) break;
+ }
+ files ??= [.. journal.Files];
+ for (var i = 0; i < waiting.Count; i++)
+ {
+ var (file, index) = waiting[i];
+ claimed.Add(names[i]);
+ files[index] = file with { To = names[i] };
+ }
+ Log.Error("archive", $"{string.Join(", ", waiting.Where(f => Taken(sequence, f.File.To)).Select(f => sequence.ArchiveRelative(f.File.To)))} taken while "
+ + $"a rename was interrupted; the files of that photo go to {string.Join(", ", names.Select(sequence.ArchiveRelative))}");
+ }
+ if (files is null) return journal;
+ journal = journal with { Files = files };
+ Write(sequence, journal);
+ return journal;
+ }
+
+ ///
+ /// The old name of ( = 1), or the photo's old name with _n and the
+ /// companion's tail after it (IMG_0001_2.CR2, IMG_0001_2.xmp).
+ ///
+ private static string Numbered(FileRenameEntry file, FileRenameEntry photo, int n)
+ {
+ if (n == 1) return file.From;
+ var stem = photo.From[..^Path.GetExtension(photo.From).Length] + "_" + n.ToString(CultureInfo.InvariantCulture);
+ return file.Companion ? stem + FrameCompanions.Tail(file.From, photo.From) : stem + Path.GetExtension(photo.From);
+ }
+
///
/// naming the new files of (frame order unchanged) one frames
- /// version further; null when no frame names an old file any more (the sidecar was written already).
+ /// version further; null when no frame names an old file any more (the sidecar was written already). A file a recovery
+ /// sent back to its old name () leaves its frame as it is.
///
public static SequenceManifest? Applied(SequenceManifest manifest, FileRenameJournal journal)
{
ArgumentNullException.ThrowIfNull(manifest);
ArgumentNullException.ThrowIfNull(journal);
- var renamed = journal.Files.Where(f => !f.Companion).ToDictionary(f => f.FrameId, f => f);
+ var renamed = journal.Files.Where(f => !f.Companion && !string.Equals(f.From, f.To, StringComparison.Ordinal)).ToDictionary(f => f.FrameId, f => f);
var changed = false;
var frames = manifest.Frames.Select(f =>
{
@@ -186,5 +258,8 @@ public sealed record FileRenameJournal(int FramesVersion, int Phase, IReadOnlyLi
private static bool Exists(SequencePaths sequence, string file) => File.Exists(sequence.File(file));
+ /// Whether a name is held, by a file or a folder: no file can be moved there.
+ private static bool Taken(SequencePaths sequence, string file) => Path.Exists(sequence.File(file));
+
private static void Move(SequencePaths sequence, string from, string to) => File.Move(sequence.File(from), sequence.File(to), overwrite: false);
}
diff --git a/src/Chronos.Core/Archiving/FileRenames.cs b/src/Chronos.Core/Archiving/FileRenames.cs
index f538673b..fa9f0fea 100644
--- a/src/Chronos.Core/Archiving/FileRenames.cs
+++ b/src/Chronos.Core/Archiving/FileRenames.cs
@@ -1,3 +1,6 @@
+using System.Globalization;
+using System.Security.Cryptography;
+using System.Text;
using Chronos.Core.Naming;
namespace Chronos.Core.Archiving;
@@ -14,7 +17,7 @@ public enum RenameBlock
/// Photos whose file is not there.
Missing,
- /// The sequence's sidecar cannot be read, or belongs to another sequence.
+ /// The sequence's sidecar cannot be read, is of a newer format this build does not rewrite, or belongs to another sequence.
Unreadable,
}
@@ -34,6 +37,14 @@ public sealed record FileRename(Guid FrameId, string From, string To, bool Compa
public sealed record FileRenamePlan(IReadOnlyList Moves, int Frames, IReadOnlyList Samples, IReadOnlyList Blocked)
{
public static FileRenamePlan None { get; } = new([], 0, [], []);
+
+ /// A digest of every move in order (frame, old and new name, companion or not): two plans with the same one rename the same files the same way.
+ public string Digest()
+ {
+ var text = new StringBuilder();
+ foreach (var move in Moves) text.Append(CultureInfo.InvariantCulture, $"{move.FrameId:N}|{move.From}|{move.To}|{move.Companion}\n");
+ return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(text.ToString())));
+ }
}
///
diff --git a/src/Chronos.Core/Archiving/MassRename.cs b/src/Chronos.Core/Archiving/MassRename.cs
index 62df6060..2464337c 100644
--- a/src/Chronos.Core/Archiving/MassRename.cs
+++ b/src/Chronos.Core/Archiving/MassRename.cs
@@ -41,6 +41,12 @@ public sealed record RenameStep(Guid SequenceId, string FromFolder, string ToFol
/// The first renamed frames (), old and new name relative to the sequence folder.
public IReadOnlyList FileSamples { get; init; } = [];
+ ///
+ /// Every file rename of the step, as a digest (): the apply renames the files only
+ /// while they plan to exactly these. Null when no file gets a new name (or in a plan journaled before it existed).
+ ///
+ public string? FileDigest { get; init; }
+
/// Frames that keep their file names, and why.
public IReadOnlyList Blocked { get; init; } = [];
}
@@ -124,7 +130,7 @@ public sealed record RenamePlan(Guid Id, Guid ArchiveId, IReadOnlyList').Append(sample.To).Append(';');
foreach (var blocked in s.Blocked) text.Append(CultureInfo.InvariantCulture, $"{blocked.Reason}:{blocked.Frames};");
text.Append('\n');
@@ -133,11 +139,30 @@ public sealed record RenamePlan(Guid Id, Guid ArchiveId, IReadOnlyList
+/// What the completed steps of a plan did, over every run that applied them (): the
+/// sequences changed, the frames whose file got a new name, and the sequences not changed as planned because they changed
+/// since: left as they were (), their file names kept (), their title and file
+/// names kept (), or their file names kept because a photo was in use ().
+///
+public sealed record RenameTally
+{
+ public IReadOnlyList Changed { get; init; } = [];
+ public int Files { get; init; }
+ public IReadOnlyList Dropped { get; init; } = [];
+ public IReadOnlyList FilesKept { get; init; } = [];
+ public IReadOnlyList TitlesKept { get; init; } = [];
+ public IReadOnlyList FilesBusy { get; init; } = [];
+}
+
/// A plan being applied, with the steps already completed; lets an interrupted rename resume (BRIEF §7).
-/// The job applying the plan was paused or cancelled between two steps: an archive open leaves the plan to
-/// that job, which completes it when it runs again.
+/// The job applying the plan was paused, cancelled or failed between two steps: an archive open leaves the
+/// plan to that job, which completes it when it is resumed or retried.
public sealed record RenameJournal(RenamePlan Plan, IReadOnlyList Completed, bool Held = false)
{
+ /// What the completed steps did, so a resumed run reports the whole plan.
+ public RenameTally Done { get; init; } = new();
+
public static string PathFor(ArchivePaths archive, Guid planId) => Path.Combine(archive.JournalDirectory, $"rename-{planId:N}.json");
public static void Write(ArchivePaths archive, RenameJournal journal)
@@ -252,6 +277,7 @@ public static class RenamePlanner
FramesVersion = plan.Frames > 0 ? files.Manifest.Versions.Frames : 0,
FileChanges = plan.Frames,
FileSamples = plan.Samples,
+ FileDigest = plan.Frames > 0 ? plan.Digest() : null,
Blocked = plan.Blocked,
};
}
diff --git a/src/Chronos.Data/Archiving/Jobs/MoveJobHandler.cs b/src/Chronos.Data/Archiving/Jobs/MoveJobHandler.cs
index 7cc55943..3a77d7a8 100644
--- a/src/Chronos.Data/Archiving/Jobs/MoveJobHandler.cs
+++ b/src/Chronos.Data/Archiving/Jobs/MoveJobHandler.cs
@@ -1,3 +1,4 @@
+using System.Text.Json;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.Localization;
@@ -10,13 +11,16 @@ namespace Chronos.Data.Archiving.Jobs;
/// : a Reorganize (owner review 2, R7; BRIEF §12.5) of existing sequences with new folder, file
/// and title templates (). The job plans again from the templates and applies the plan only
/// when it is still the one confirmed (its fingerprint and the frames versions its file names were planned at;
-/// otherwise, nothing changed), through , whose journal makes
-/// every step resumable. Progress counts the plan's steps (sequences); checkpoint: plan:<id> once the plan's
-/// journal is written. Paused or cancelled, the job stops between two sequences and holds the plan
-/// (), which an archive open then leaves to it; resumed or retried, it completes the rest when
-/// it still fits. A plan whose journal is gone completed meanwhile (at an archive open). Sequences left as they were, or
-/// whose files kept their names, because they changed after the plan are logged and fail the job at the end, after the
-/// others changed. Each sequence changed is told to ().
+/// otherwise, nothing changed; a sequence of it gone since is named in the log),
+/// through , whose journal makes every step resumable. Progress counts the plan's steps
+/// (sequences); checkpoint: plan:<id> once the plan's journal is written. Paused, cancelled or failed, the job
+/// stops between two sequences and holds the plan (), which an archive open then leaves to it
+/// (completing only file renames a step left half-way); resumed or retried, it completes the rest when it still fits. A
+/// plan whose journal is gone completed meanwhile (at an archive open). Sequences left as they were, or that kept their
+/// title or file names, because they changed after the plan or a photo was in use are logged and fail the job at the end,
+/// after the others changed. Each sequence changed is told to
+/// () as soon as its step is journaled, so a pause or a failure later never keeps it untold; the log
+/// counts the whole plan, over every run of the job.
///
internal sealed class MoveJobHandler(ChronosDatabase db, MassRenamer renamer, Func? sequenceChanged) : IJobHandler
{
@@ -50,7 +54,7 @@ internal sealed class MoveJobHandler(ChronosDatabase db, MassRenamer renamer, Fu
}
else
{
- plan = await renamer.PlanAsync(archive, [.. parameters.Sequences.Select(s => s.Id)],
+ plan = await renamer.PlanAsync(archive, await PresentAsync(context, archive, parameters).ConfigureAwait(false),
new RenameTemplates(parameters.FolderTemplate, parameters.FileTemplate, parameters.TitleTemplate), context.Cancellation).ConfigureAwait(false);
if (!IsConfirmed(plan, parameters)) throw new StaleRenamePlanException(Guid.Empty);
var total = Steps(plan);
@@ -59,7 +63,7 @@ internal sealed class MoveJobHandler(ChronosDatabase db, MassRenamer renamer, Fu
{
planId = p.Id;
return context.CheckpointAsync(Checkpoint(p.Id), 0, total);
- }, n => context.CheckpointAsync(Checkpoint(plan.Id), n, total), context.Cancellation).ConfigureAwait(false);
+ }, (n, changed) => StepDoneAsync(context, plan.Id, n, total, changed), context.Cancellation).ConfigureAwait(false);
}
}
catch (OperationCanceledException) when (planId is { } id && context.Cancellation.IsCancellationRequested)
@@ -70,12 +74,16 @@ internal sealed class MoveJobHandler(ChronosDatabase db, MassRenamer renamer, Fu
Log.Info("archive", $"Reorganize {id} held by its job {context.Job.Id}");
throw;
}
- context.Log(Loc.L("Archive.Log.Reorganized", result.Changed.Count, result.Files));
- foreach (var id in result.Dropped) context.Log(Loc.L("Archive.Log.ReorganizeDropped", Name(plan, id)));
- foreach (var id in result.FilesKept) context.Log(Loc.L("Archive.Log.ReorganizeFilesKept", Name(plan, id)));
- await TellAsync(result.Changed).ConfigureAwait(false);
- var kept = result.Dropped.Count + result.FilesKept.Count;
+ catch (Exception ex) when (planId is { } id && !(ex is OperationCanceledException && context.Cancellation.IsCancellationRequested))
+ {
+ // Failed (a sequence changed, busy, or a folder that could not move): the rest waits for this job's retry, never for an archive open.
+ HoldFailed(context, archive, id);
+ throw;
+ }
+ LogDone(context, plan, result);
+ var kept = result.Dropped.Count + result.FilesKept.Count + result.TitlesKept.Count;
if (kept > 0) throw new InvalidOperationException(Loc.Plural("Archive.Error.ReorganizeKept", kept));
+ if (result.FilesBusy.Count > 0) throw new InvalidOperationException(Loc.Plural("Archive.Error.ReorganizeBusy", result.FilesBusy.Count));
}
/// Completes a plan this job started; null when its journal went meanwhile (completed at an archive open).
@@ -83,10 +91,57 @@ internal sealed class MoveJobHandler(ChronosDatabase db, MassRenamer renamer, Fu
{
var total = Steps(journal.Plan);
await context.ReportAsync(journal.Completed.Count, total).ConfigureAwait(false);
- return await renamer.ResumeAsync(archive, journal.Plan.Id, n => context.CheckpointAsync(Checkpoint(journal.Plan.Id), n, total), context.Cancellation)
+ return await renamer.ResumeAsync(archive, journal.Plan.Id, (n, changed) => StepDoneAsync(context, journal.Plan.Id, n, total, changed), context.Cancellation)
.ConfigureAwait(false);
}
+ ///
+ /// The sequences of the confirmed plan the index still has in the archive; each one gone since (deleted while the job
+ /// waited in the queue) is named in the log, and the plan made without it is then not the one confirmed.
+ ///
+ private async Task> PresentAsync(JobContext context, Archive archive, MoveJobParameters parameters)
+ {
+ var present = new List(parameters.Sequences.Count);
+ foreach (var sequence in parameters.Sequences)
+ {
+ if (await db.Sequences.GetAsync(sequence.Id, context.Cancellation).ConfigureAwait(false) is { } row && row.ArchiveId == archive.Id) present.Add(sequence.Id);
+ else context.Log(Loc.L("Archive.Log.SequenceMissing", sequence.Id));
+ }
+ return present;
+ }
+
+ /// After each step: the checkpoint, then the sequences that step changed are told.
+ private async ValueTask StepDoneAsync(JobContext context, Guid planId, int done, int total, IReadOnlyList changed)
+ {
+ await context.CheckpointAsync(Checkpoint(planId), done, total).ConfigureAwait(false);
+ await TellAsync(changed).ConfigureAwait(false);
+ }
+
+ /// Holds the plan of a failed run and logs what its completed steps did; a hold that cannot be written is logged.
+ private static void HoldFailed(JobContext context, Archive archive, Guid planId)
+ {
+ try
+ {
+ if (!MassRenamer.Hold(archive, planId) || RenameJournal.Read(new ArchivePaths(archive.RootPath), planId) is not { } journal) return;
+ Log.Info("archive", $"Reorganize {planId} held by its failed job {context.Job.Id}");
+ LogDone(context, journal.Plan, RenameResult.Of(journal.Done));
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException)
+ {
+ Log.Warning("archive", $"Reorganize {planId} could not be held after its job failed", ex);
+ }
+ }
+
+ /// The job log of what the plan did: the counts, then each sequence not changed as planned, and why.
+ private static void LogDone(JobContext context, RenamePlan plan, RenameResult result)
+ {
+ context.Log(Loc.L("Archive.Log.Reorganized", result.Changed.Count, result.Files));
+ foreach (var id in result.Dropped) context.Log(Loc.L("Archive.Log.ReorganizeDropped", Name(plan, id)));
+ foreach (var id in result.TitlesKept) context.Log(Loc.L("Archive.Log.ReorganizeTitleKept", Name(plan, id)));
+ foreach (var id in result.FilesKept) context.Log(Loc.L("Archive.Log.ReorganizeFilesKept", Name(plan, id)));
+ foreach (var id in result.FilesBusy) context.Log(Loc.L("Archive.Log.ReorganizeFilesBusy", Name(plan, id)));
+ }
+
/// Whether the plan made now is the one confirmed: the same fingerprint, and the frames versions its file names were planned at.
private static bool IsConfirmed(RenamePlan plan, MoveJobParameters parameters)
{
diff --git a/src/Chronos.Data/Archiving/MassRenamer.cs b/src/Chronos.Data/Archiving/MassRenamer.cs
index 9f73ac82..7d512d8f 100644
--- a/src/Chronos.Data/Archiving/MassRenamer.cs
+++ b/src/Chronos.Data/Archiving/MassRenamer.cs
@@ -8,8 +8,9 @@ using Chronos.Data.Repositories;
namespace Chronos.Data.Archiving;
///
-/// The plan no longer matches the archive: a sequence moved or vanished since it was made, or one was indexed inside a
-/// folder the plan moves, or the photos of a sequence whose files it renames changed.
+/// The plan no longer matches the archive: a sequence moved or vanished since it was made, one was indexed inside a
+/// folder the plan moves, something took a folder it moves one into, a title it changes was edited, or the photos of a
+/// sequence whose files it renames changed.
///
public sealed class StaleRenamePlanException(Guid sequenceId)
: InvalidOperationException(Loc.L("Archive.Error.StalePlan"))
@@ -18,11 +19,23 @@ public sealed class StaleRenamePlanException(Guid sequenceId)
}
///
-/// What applying a plan did: the sequences changed (in the order of the plan), the frames whose files got a new name, the
-/// sequences left as they were because they changed since the plan (, only when resuming) and those
-/// whose files kept their names because their photos changed since (; their folder and title changed).
+/// What applying a plan did, over every run that applied it: the sequences changed (in the order of the plan), the frames
+/// whose files got a new name, and the sequences not changed as planned: left as they were because they changed since the
+/// plan (, only when resuming), whose files kept their names because their photos changed since
+/// (; their folder and title changed), whose title and file names stayed because their title was
+/// edited since or their sidecar is of a newer format (, only when resuming; their folder moved),
+/// and whose files kept their names because a photo was in use (; their folder and title changed).
///
-public sealed record RenameResult(IReadOnlyList Changed, int Files, IReadOnlyList Dropped, IReadOnlyList FilesKept);
+public sealed record RenameResult(IReadOnlyList Changed, int Files, IReadOnlyList Dropped, IReadOnlyList FilesKept,
+ IReadOnlyList TitlesKept, IReadOnlyList FilesBusy)
+{
+ /// The result a plan's journal records for its completed steps ().
+ public static RenameResult Of(RenameTally tally)
+ {
+ ArgumentNullException.ThrowIfNull(tally);
+ return new(tally.Changed, tally.Files, tally.Dropped, tally.FilesKept, tally.TitlesKept, tally.FilesBusy);
+ }
+}
///
/// Mass rename of sequence folders, titles and file names (BRIEF §7, §12.5; Reorganize, owner review 2, R7):
@@ -45,6 +58,19 @@ public sealed class MassRenamer
_indexer = indexer;
}
+ /// How far a sequence of a step still fits the plan, checked under the step's leases before anything of the step moves.
+ private enum Fit
+ {
+ /// As planned.
+ Whole,
+
+ /// Its photos changed since the plan (or its sidecar is of a newer format): its folder and title change, its files keep their names.
+ Files,
+
+ /// Its title was edited since the plan (or its sidecar is of a newer format): its title and file names stay.
+ Title,
+ }
+
/// New folder template ({title} = the new title), or null to keep folders.
/// New title template ({title} = the current title), or null to keep titles.
/// Sequences nested in a folder that moves are part of the plan (they move with it); see .
@@ -91,15 +117,22 @@ public sealed class MassRenamer
private static RenameSubject Subject(Sequence s) => new(s.Id, s.Folder, s.Title, s.FirstCapture, s.CameraMake, s.CameraModel, s.Lens);
- /// The sidecar of a sequence and the files beside its frames; null (logged) when it cannot be read or is another sequence's.
+ ///
+ /// The sidecar of a sequence and the files beside its frames; null (logged) when it cannot be read, is another
+ /// sequence's, or is of a newer format this build never rewrites (its files cannot get new names here).
+ ///
private static SequenceFiles? ReadFiles(ArchivePaths paths, Sequence sequence)
{
var folder = paths.Sequence(sequence.Folder);
try
{
var manifest = ManifestStore.ReadSequence(folder.ManifestPath);
- if (manifest.Id == sequence.Id) return new SequenceFiles(manifest, FileRenames.ListPresent(folder, manifest));
- Log.Warning("archive", $"{folder.ManifestPath} belongs to sequence {manifest.Id}, not {sequence.Id}; its files keep their names");
+ if (manifest.Id != sequence.Id)
+ Log.Warning("archive", $"{folder.ManifestPath} belongs to sequence {manifest.Id}, not {sequence.Id}; its files keep their names");
+ else if (manifest.SchemaVersion > SequenceManifest.CurrentSchemaVersion)
+ Log.Warning("archive", $"{folder.ManifestPath} was written by a newer Chronos (format {manifest.SchemaVersion}); its files keep their names");
+ else
+ return new SequenceFiles(manifest, FileRenames.ListPresent(folder, manifest));
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException)
{
@@ -110,14 +143,17 @@ public sealed class MassRenamer
/// Applies a plan; each completed step is journaled before the next starts.
/// A sequence is no longer where the plan expects it, a folder that moves holds
- /// a sequence the plan does not take along, or the photos of a sequence whose files it renames changed; nothing of that
- /// step was changed (a step found stale only once its folder moved keeps its folder and title, and its journal has the
- /// next open complete the rest).
- /// A file could not be renamed (open elsewhere); the step's files went back to their names. Or
- /// () a sequence is busy, or holds a restore from the trash whose entry is busy and
- /// not settled yet; nothing of that step changed.
+ /// a sequence the plan does not take along, something took a folder the plan moves one into, a title it changes was
+ /// edited, or the photos of a sequence whose files it renames changed; nothing of that step was changed (each is
+ /// checked under the step's leases before anything of it moves), the steps before it were.
+ /// A sequence whose title or file names the plan changes has a sidecar of a newer
+ /// format, which this build never rewrites; nothing of that step was changed.
+ /// A folder could not be moved, or () a sequence is
+ /// busy, or holds a restore from the trash whose entry is busy and not settled yet; nothing of that step changed. A
+ /// file that could not be renamed (open elsewhere) does not fail the step: the sequence keeps its file names
+ /// ().
public Task ApplyAsync(Archive archive, RenamePlan plan, IProgress? progress = null, CancellationToken cancellationToken = default) =>
- ApplyAsync(archive, plan, null, progress is null ? null : n =>
+ ApplyAsync(archive, plan, null, progress is null ? null : (n, _) =>
{
progress.Report(n);
return ValueTask.CompletedTask;
@@ -125,8 +161,8 @@ public sealed class MassRenamer
///
/// Told once the plan's journal is written, before any step (a job saves the plan's id as its checkpoint).
- /// Told after each step with the number of steps done.
- public Task ApplyAsync(Archive archive, RenamePlan plan, Func? started, Func? stepDone,
+ /// Told after each step is journaled, with the number of steps done and the sequences that step changed.
+ public Task ApplyAsync(Archive archive, RenamePlan plan, Func? started, Func, ValueTask>? stepDone,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(plan);
@@ -137,9 +173,12 @@ public sealed class MassRenamer
/// Completes the plan from its journal: one an interruption left (as
/// does), or one its job held, whose remaining steps are checked as a plan applied anew (a sequence changed since refuses
/// the rest, and that journal goes). Null when there is no journal: the plan completed (here, or at an archive open).
+ /// The result covers the whole plan, the steps of earlier runs included.
///
+ /// Told after each step is journaled, with the number of steps done and the sequences that step changed.
/// A held plan's remaining steps no longer match the archive; nothing more changed.
- public async Task ResumeAsync(Archive archive, Guid planId, Func? stepDone = null, CancellationToken cancellationToken = default)
+ public async Task ResumeAsync(Archive archive, Guid planId, Func, ValueTask>? stepDone = null,
+ CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(archive);
if (RenameJournal.Read(new ArchivePaths(archive.RootPath), planId) is not { } journal) return null;
@@ -147,8 +186,8 @@ public sealed class MassRenamer
}
///
- /// Marks the plan held (): its job was paused or cancelled between
- /// two steps, so an archive open leaves the rest to that job. False when it has no journal (it completed).
+ /// Marks the plan held (): its job was paused, cancelled or
+ /// failed between two steps, so an archive open leaves the rest to that job. False when it has no journal (it completed).
///
public static bool Hold(Archive archive, Guid planId)
{
@@ -159,19 +198,53 @@ public sealed class MassRenamer
return true;
}
- /// Completes renames interrupted by a crash (not those their job held); returns how many plans were resumed.
+ ///
+ /// Completes renames interrupted by a crash (not those their job held); returns how many plans were resumed. The file
+ /// renames a held plan's step left half-way are completed all the same (), so no photo
+ /// waits at a temporary name for that job.
+ ///
public async Task ResumePendingAsync(Archive archive, CancellationToken cancellationToken = default)
{
var paths = new ArchivePaths(archive.RootPath);
- var pending = RenameJournal.ListPending(paths).Where(j => j.Plan.ArchiveId == archive.Id && !j.Held).ToList();
+ var journals = RenameJournal.ListPending(paths).Where(j => j.Plan.ArchiveId == archive.Id).ToList();
+ foreach (var held in journals.Where(j => j.Held)) await SettleFilesAsync(paths, held, cancellationToken).ConfigureAwait(false);
+ var pending = journals.Where(j => !j.Held).ToList();
foreach (var journal in pending) await RunAsync(archive, journal, resuming: true, null, null, cancellationToken).ConfigureAwait(false);
return pending.Count;
}
- /// Steps whose sequence the index no longer knows are dropped instead of failing the rename:
- /// their sidecars still carry the ids, so the next scan indexes them wherever they ended up.
+ ///
+ /// Completes, each under its sequence's lease, the file renames a crash or a failure left in the steps of a plan that are
+ /// not completed (: the files go on to their new names and the sidecar names them;
+ /// the index follows when the plan resumes, or at the next scan). One that cannot be completed yet is logged and left
+ /// to the next writer of that sequence.
+ ///
+ private async Task SettleFilesAsync(ArchivePaths paths, RenameJournal journal, CancellationToken cancellationToken)
+ {
+ var completed = journal.Completed.ToHashSet();
+ foreach (var step in journal.Plan.Steps.Where(s => s.FileChanges > 0 && !completed.Contains(s.SequenceId)))
+ {
+ var sequence = Where(paths, step);
+ if (!File.Exists(sequence.FileRenameJournalPath)) continue;
+ try
+ {
+ await using (await _writer.LockAsync(paths, step.SequenceId, cancellationToken).ConfigureAwait(false))
+ {
+ SequenceWriter.ReadLocked(sequence, step.SequenceId);
+ }
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or NewerFormatException)
+ {
+ Log.Warning("archive", $"The interrupted rename of the files of {sequence.RelativeFolder} could not be completed yet", ex);
+ }
+ }
+ }
+
+ /// Steps whose sequence the index no longer knows, or that no longer fit the plan, are dropped
+ /// (or keep what no longer fits) instead of failing the rename: their sidecars still carry the ids, so the next scan
+ /// indexes them wherever they ended up.
private async Task RunAsync(Archive archive, RenameJournal journal, bool resuming, Func? started,
- Func? stepDone, CancellationToken cancellationToken)
+ Func, ValueTask>? stepDone, CancellationToken cancellationToken)
{
var paths = new ArchivePaths(archive.RootPath);
ArchiveVolume.EnsureWritable(paths);
@@ -181,11 +254,14 @@ public sealed class MassRenamer
var steps = journal.Plan.Steps.Where(s => s.Changes).ToList();
if (!resuming)
{
+ // Checked as the files are: file renames a crash or a failure left half-way are completed first.
+ await SettleFilesAsync(paths, journal, cancellationToken).ConfigureAwait(false);
try
{
await RefuseStaleAsync(archive, paths, journal.Plan, [.. steps.Where(s => !completed.Contains(s.SequenceId))], cancellationToken).ConfigureAwait(false);
}
- catch (StaleRenamePlanException) when (completed.Count > 0 || File.Exists(RenameJournal.PathFor(paths, journal.Plan.Id)))
+ catch (Exception ex) when (ex is StaleRenamePlanException or NewerFormatException
+ && (completed.Count > 0 || File.Exists(RenameJournal.PathFor(paths, journal.Plan.Id))))
{
// A held plan that no longer fits: what it applied stays, the rest never will.
RenameJournal.Delete(paths, journal.Plan.Id);
@@ -195,7 +271,7 @@ public sealed class MassRenamer
}
RenameJournal.Write(paths, journal);
if (started is not null) await started(journal.Plan).ConfigureAwait(false);
- var result = new Tally();
+ var result = new Tally(journal.Done);
foreach (var step in steps)
{
if (completed.Contains(step.SequenceId)) continue;
@@ -204,45 +280,62 @@ public sealed class MassRenamer
IReadOnlyList carried = step.MovesFolder && !step.Carried
? [.. steps.Where(c => c.Carried && !completed.Contains(c.SequenceId) && Carrier(steps, c) == step)]
: [];
+ var before = result.Changed.Count;
await ApplyStepAsync(archive, paths, journal.Plan.FileTemplate, step, carried, resuming, result, cancellationToken).ConfigureAwait(false);
completed.Add(step.SequenceId);
foreach (var c in carried) completed.Add(c.SequenceId);
- RenameJournal.Write(paths, journal with { Completed = [.. completed] });
- if (stepDone is not null) await stepDone(completed.Count).ConfigureAwait(false);
+ journal = journal with { Completed = [.. completed], Done = result.Record() };
+ RenameJournal.Write(paths, journal);
+ if (stepDone is not null) await stepDone(completed.Count, result.Changed.GetRange(before, result.Changed.Count - before)).ConfigureAwait(false);
}
RenameJournal.Delete(paths, journal.Plan.Id);
- return new RenameResult(result.Changed, result.Files, result.Dropped, result.FilesKept);
+ return RenameResult.Of(result.Record());
}
///
- /// A plan made before another change is refused before anything moves: a sequence moved, one was indexed inside a
- /// folder that moves (a scan adopted a subfolder copied in since the dry run), or the photos of a sequence whose files
- /// it renames changed (another frames version in its sidecar).
+ /// A plan made before another change is refused before anything moves: a sequence moved, a title it changes was edited,
+ /// one was indexed inside a folder that moves (a scan adopted a subfolder copied in since the dry run), something took a
+ /// folder it moves one into (), the photos of a sequence whose files it renames changed
+ /// (), or a sidecar it rewrites is of a newer format. A step a failed run left half-way (its
+ /// folder at its new place) still fits. Read without the leases, each step checks the same again under its own
+ /// (), so a held plan resumed by its job either fits here or is given up.
///
+ /// A sidecar the plan rewrites is of a newer format.
private async Task RefuseStaleAsync(Archive archive, ArchivePaths paths, RenamePlan plan, IReadOnlyList remaining, CancellationToken cancellationToken)
{
+ var locations = await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false);
+ var template = plan.FileTemplate is null ? null : PathTemplate.Parse(plan.FileTemplate);
foreach (var step in remaining)
{
- if ((await _db.Sequences.GetAsync(step.SequenceId, cancellationToken).ConfigureAwait(false))?.Folder != step.FromFolder)
+ var row = await _db.Sequences.GetAsync(step.SequenceId, cancellationToken).ConfigureAwait(false);
+ if (row is null || (row.Folder != step.FromFolder && row.Folder != step.ToFolder) || (step.Retitles && !Planned(step, row.Title))
+ || (!step.Carried && TargetTaken(paths, step, locations)))
+ throw new StaleRenamePlanException(step.SequenceId);
+ var files = template is not null && step.FileChanges > 0;
+ if (!step.Retitles && !files) continue;
+ // A sidecar that cannot be read refuses file renames planned from it; a title alone fails under the step's lease, with its reason.
+ var sequence = Where(paths, step);
+ if (ReadSidecar(sequence, step.SequenceId) is not { } manifest)
+ {
+ if (files) throw new StaleRenamePlanException(step.SequenceId);
+ continue;
+ }
+ if (manifest.SchemaVersion > SequenceManifest.CurrentSchemaVersion) throw new NewerFormatException(sequence.ManifestPath, manifest.SchemaVersion);
+ if ((step.Retitles && !TitleStands(step, row, manifest)) || (files && !FilesStand(sequence, template!, step, manifest)))
throw new StaleRenamePlanException(step.SequenceId);
}
var planned = plan.Steps.Select(s => s.SequenceId).ToHashSet();
- if (await StrangerInsideAsync(archive, remaining.Where(s => s.MovesFolder && !s.Carried), planned, cancellationToken).ConfigureAwait(false) is { } stranger)
+ if (StrangerInside(locations, remaining.Where(s => s.MovesFolder && !s.Carried), planned) is { } stranger)
throw new StaleRenamePlanException(stranger.Id);
- if (plan.FileTemplate is null) return;
- foreach (var step in remaining.Where(s => s.FileChanges > 0))
- {
- if (FramesVersionOf(paths.Sequence(step.FromFolder), step.SequenceId) != step.FramesVersion) throw new StaleRenamePlanException(step.SequenceId);
- }
}
- /// The frames version of a sequence's sidecar (read without its lease); null when it cannot be read or is another sequence's.
- private static int? FramesVersionOf(SequencePaths sequence, Guid sequenceId)
+ /// The sidecar of a sequence (read without its lease); null when it cannot be read or is another sequence's.
+ private static SequenceManifest? ReadSidecar(SequencePaths sequence, Guid sequenceId)
{
try
{
var manifest = ManifestStore.ReadSequence(sequence.ManifestPath);
- return manifest.Id == sequenceId ? manifest.Versions.Frames : null;
+ return manifest.Id == sequenceId ? manifest : null;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException)
{
@@ -254,15 +347,29 @@ public sealed class MassRenamer
private static RenameStep? Carrier(IReadOnlyList steps, RenameStep carried) =>
steps.Where(m => !m.Carried && m.MovesFolder && RenamePlanner.IsInside(m.FromFolder, carried.FromFolder)).MaxBy(m => m.FromFolder.Length);
+ /// Where a step's sequence is now: its folder moves first, so a resumed step may find it at its new place already.
+ private static SequencePaths Where(ArchivePaths paths, RenameStep step)
+ {
+ var sequence = paths.Sequence(step.FromFolder);
+ return step.MovesFolder && !File.Exists(sequence.ManifestPath) ? paths.Sequence(step.ToFolder) : sequence;
+ }
+
+ /// Whether a step's folder moved already (an interrupted run moved it before every index row followed).
+ private static bool Moved(ArchivePaths paths, RenameStep step) => step.MovesFolder && !Directory.Exists(paths.FromRelative(step.FromFolder));
+
///
/// Applies a step, and the steps of the sequences its folder carries, under all their leases: sequence leases, then
/// for a move or file renames the thumbnail leases, each by sequence id (the order trash and pack compaction take them
/// in). No writer of a carried sequence (ingest, an editor save, a thumbnail pass) works in the folder while it moves or
- /// its files are renamed, or before its index row follows. Once the folder moved the step goes to its end whatever
- /// the caller cancels.
+ /// its files are renamed, or before its index row follows. Whatever can refuse the step is checked under the leases
+ /// before anything of it moves (where its sequences are, what is inside and at its new folder, then each sidecar,
+ /// settled: ). Then the folder moves, every index row follows it, and only then are the files
+ /// renamed; a sequence whose files are found changed or in use there keeps its file names, and the step goes to its
+ /// end whatever the caller cancels.
///
- /// A step whose sequence the index no longer has where the plan says is dropped instead of
- /// failing the rename: its sidecar still carries the id, so the next scan indexes it wherever it ended up.
+ /// A step whose sequence the index no longer has where the plan says, or that no longer fits it,
+ /// is dropped (or keeps what no longer fits) instead of failing the rename: its sidecar still carries the id, so the
+ /// next scan indexes it wherever it ended up.
private async Task ApplyStepAsync(Archive archive, ArchivePaths paths, string? fileTemplate, RenameStep step, IReadOnlyList carried,
bool resuming, Tally result, CancellationToken cancellationToken)
{
@@ -292,20 +399,32 @@ public sealed class MassRenamer
if (await StandsAsync(c, resuming, cancellationToken).ConfigureAwait(false)) follow.Add(c);
else result.Dropped.Add(c.SequenceId);
}
- if (!await CarriesAllInsideAsync(archive, paths, step, carried, resuming, cancellationToken).ConfigureAwait(false))
+ IReadOnlyList locations = step.MovesFolder ? await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false) : [];
+ if (!CarriesAllInside(paths, step, carried, locations, resuming) || !TargetFree(paths, step, locations, resuming))
{
result.Dropped.Add(step.SequenceId);
result.Dropped.AddRange(follow.Select(c => c.SequenceId));
return;
}
- foreach (var s in follow.Prepend(step)) Settle(paths, s);
- RenamePlanner.MoveFolder(paths, step);
- // Moved with their carrier: only their sidecars and index rows follow, and the names of their files.
- foreach (var s in follow.Prepend(step))
+ var fits = new List<(RenameStep Step, Fit Fit)>(follow.Count + 1);
+ foreach (var s in follow.Prepend(step)) fits.Add((s, await FitAsync(paths, fileTemplate, s, Settle(paths, s), resuming, cancellationToken).ConfigureAwait(false)));
+ // Its title no longer fits while nothing of it moved: it stays as it was, with the sequences it would carry.
+ if (fits[0].Fit == Fit.Title && !Moved(paths, step))
{
- await RecordAsync(archive, paths, s, CancellationToken.None).ConfigureAwait(false);
- var renamed = await RenameFilesAsync(archive, paths, fileTemplate, s, resuming, result, CancellationToken.None).ConfigureAwait(false);
- if (s.MovesFolder || s.Retitles || renamed) result.Changed.Add(s.SequenceId);
+ result.Dropped.Add(step.SequenceId);
+ result.Dropped.AddRange(follow.Select(c => c.SequenceId));
+ return;
+ }
+ RenamePlanner.MoveFolder(paths, step);
+ // Every index row follows the folder before any file is renamed, so nothing below can leave one behind it.
+ foreach (var (s, fit) in fits) await RecordAsync(archive, paths, s, s.Retitles && fit != Fit.Title, CancellationToken.None).ConfigureAwait(false);
+ foreach (var (s, fit) in fits)
+ {
+ var renamed = fit == Fit.Whole && fileTemplate is not null
+ && await RenameFilesAsync(archive, paths, fileTemplate, s, result, CancellationToken.None).ConfigureAwait(false);
+ if (fit == Fit.Title) result.TitlesKept.Add(s.SequenceId);
+ else if (fit == Fit.Files) result.FilesKept.Add(s.SequenceId);
+ if (s.MovesFolder || (s.Retitles && fit != Fit.Title) || renamed) result.Changed.Add(s.SequenceId);
}
}
finally
@@ -320,18 +439,78 @@ public sealed class MassRenamer
/// sequence, as every writer does (): an interrupted rename of its files, photo
/// deletion, restore from the trash or ingest. A restore whose trash entry is busy is not settled yet, and the step
/// waits for it () as a deletion of the sequence does: its journal names the
- /// files it moved by the names they had, so neither the folder nor those names change under it.
+ /// files it moved by the names they had, so neither the folder nor those names change under it. Returns the sidecar as
+ /// settled.
///
/// A restore from the trash into the sequence is not settled; nothing of the step changed.
- private static void Settle(ArchivePaths paths, RenameStep step)
+ private static SequenceManifest Settle(ArchivePaths paths, RenameStep step)
{
- // Where the sequence is now: its folder moves first, so a resumed step may find it at its new place already.
- var sequence = paths.Sequence(step.FromFolder);
- if (step.MovesFolder && !File.Exists(sequence.ManifestPath)) sequence = paths.Sequence(step.ToFolder);
- SequenceWriter.ReadLocked(sequence, step.SequenceId);
+ var sequence = Where(paths, step);
+ var manifest = SequenceWriter.ReadLocked(sequence, step.SequenceId);
FrameTrash.EnsureSettled(sequence);
+ return manifest;
}
+ ///
+ /// How far a sequence of a step still fits the plan, under the step's leases with its sidecar settled
+ /// () and before anything of the step moves: a sidecar of a newer format is never rewritten,
+ /// a title edited since the plan is never written over (), and the files are renamed only as the
+ /// plan renames them (). When not resuming, what no longer fits refuses the step.
+ ///
+ /// Not resuming, and the step would rewrite a sidecar of a newer format.
+ /// Not resuming, and its title or photos changed since the plan.
+ private async Task FitAsync(ArchivePaths paths, string? fileTemplate, RenameStep step, SequenceManifest manifest, bool resuming,
+ CancellationToken cancellationToken)
+ {
+ var sequence = Where(paths, step);
+ var files = fileTemplate is not null && step.FileChanges > 0;
+ if ((step.Retitles || files) && manifest.SchemaVersion > SequenceManifest.CurrentSchemaVersion)
+ {
+ if (!resuming) throw new NewerFormatException(sequence.ManifestPath, manifest.SchemaVersion);
+ Log.Warning("archive", $"{sequence.RelativeFolder} keeps its title and file names: its sidecar was written by a newer Chronos");
+ return step.Retitles ? Fit.Title : Fit.Files;
+ }
+ if (step.Retitles && await _db.Sequences.GetAsync(step.SequenceId, cancellationToken).ConfigureAwait(false) is { } row && !TitleStands(step, row, manifest))
+ {
+ if (!resuming) throw new StaleRenamePlanException(step.SequenceId);
+ Log.Warning("archive", $"{sequence.RelativeFolder} keeps its title and file names: its title was edited after the plan was made");
+ return Fit.Title;
+ }
+ if (files && !FilesStand(sequence, PathTemplate.Parse(fileTemplate!), step, manifest))
+ {
+ if (!resuming) throw new StaleRenamePlanException(step.SequenceId);
+ Log.Warning("archive", $"The files of {sequence.RelativeFolder} keep their names: its photos changed after the plan was made");
+ return Fit.Files;
+ }
+ return Fit.Whole;
+ }
+
+ ///
+ /// Whether a sequence's title is still one the plan knows (the one it was made from, or the one it gives, which an
+ /// interrupted run may have recorded): the index's, and the sidecar's unless the index is ahead of it (an edit only the
+ /// index has, as the server's API makes). An edit made since, here or by another process sharing the archive, is kept.
+ ///
+ private static bool TitleStands(RenameStep step, Sequence row, SequenceManifest manifest) =>
+ Planned(step, row.Title) && (row.SettingsVersion > manifest.Versions.Settings || Planned(step, manifest.Title));
+
+ private static bool Planned(RenameStep step, string? title) =>
+ string.Equals(title, step.FromTitle, StringComparison.Ordinal) || string.Equals(title, step.ToTitle, StringComparison.Ordinal);
+
+ ///
+ /// Whether the files of a step's sequence still rename as planned: planned again from and the
+ /// files beside its frames, they rename exactly the files the plan renamed () at its
+ /// frames version, or none is left to rename (an interrupted run renamed them). A photo hashed, back or added since,
+ /// or a file copied in beside them, would give other names than the dry run showed.
+ ///
+ private static bool FilesStand(SequencePaths sequence, PathTemplate template, RenameStep step, SequenceManifest manifest)
+ {
+ var plan = FileRenames.Plan(manifest, step.ToTitle, template, FileRenames.ListPresent(sequence, manifest));
+ return plan.Moves.Count == 0 || Matches(step, plan, manifest);
+ }
+
+ private static bool Matches(RenameStep step, FileRenamePlan plan, SequenceManifest manifest) =>
+ manifest.Versions.Frames == step.FramesVersion && (step.FileDigest is null || string.Equals(step.FileDigest, plan.Digest(), StringComparison.Ordinal));
+
/// The index still has the step's sequence where the plan says (or where it already took it); otherwise stale.
/// Not resuming, and the sequence moved or vanished since the plan was made.
private async Task StandsAsync(RenameStep step, bool resuming, CancellationToken cancellationToken)
@@ -350,26 +529,49 @@ public sealed class MassRenamer
/// moved yet (a folder moved before the interruption goes on; the next scan indexes the newcomer where it ended up).
///
/// Not resuming, and such a sequence is inside the folder.
- private async Task CarriesAllInsideAsync(Archive archive, ArchivePaths paths, RenameStep step, IReadOnlyList carried, bool resuming,
- CancellationToken cancellationToken)
+ private static bool CarriesAllInside(ArchivePaths paths, RenameStep step, IReadOnlyList carried, IReadOnlyList locations,
+ bool resuming)
{
if (!step.MovesFolder) return true;
- var taken = carried.Select(c => c.SequenceId).ToHashSet();
- if (await StrangerInsideAsync(archive, [step], taken, cancellationToken).ConfigureAwait(false) is not { } stranger) return true;
+ if (StrangerInside(locations, [step], carried.Select(c => c.SequenceId).ToHashSet()) is not { } stranger) return true;
if (!resuming) throw new StaleRenamePlanException(stranger.Id);
- if (!Directory.Exists(paths.Sequence(step.FromFolder).Folder)) return true;
+ if (Moved(paths, step)) return true;
Log.Warning("archive", $"Rename of {step.FromFolder} dropped: {stranger.Folder} was indexed inside it after the plan was made");
return false;
}
- /// A sequence the index has inside the folder of one of that is not in ; null when there is none.
- private async Task StrangerInsideAsync(Archive archive, IEnumerable moving, IReadOnlySet taken,
- CancellationToken cancellationToken)
+ ///
+ /// Whether the new folder of a moving step is still free (), checked again under the step's
+ /// leases just before the folder moves: one taken since the plan refuses the step, or drops it when resuming; nothing
+ /// of it moved either way, and no folder is ever moved into another.
+ ///
+ /// Not resuming, and something took the new folder.
+ private static bool TargetFree(ArchivePaths paths, RenameStep step, IReadOnlyList locations, bool resuming)
+ {
+ if (step.Carried || !TargetTaken(paths, step, locations)) return true;
+ if (!resuming) throw new StaleRenamePlanException(step.SequenceId);
+ Log.Warning("archive", $"Rename of {step.FromFolder} dropped: {step.ToFolder} was taken after the plan was made");
+ return false;
+ }
+
+ ///
+ /// Whether something took the new folder of a step whose folder has not moved yet: a folder there on disk, or a sequence
+ /// the index has there, around it or inside it (a restore from the trash, an upload or an import since the plan). A
+ /// change of case only is the step's own folder.
+ ///
+ private static bool TargetTaken(ArchivePaths paths, RenameStep step, IReadOnlyList locations)
+ {
+ if (!step.MovesFolder || step.FromFolder.Equals(step.ToFolder, StringComparison.OrdinalIgnoreCase) || Moved(paths, step)) return false;
+ return Directory.Exists(paths.FromRelative(step.ToFolder)) || locations.Any(l => l.Id != step.SequenceId
+ && (l.Folder.Equals(step.ToFolder, StringComparison.OrdinalIgnoreCase) || RenamePlanner.IsInside(l.Folder, step.ToFolder)
+ || RenamePlanner.IsInside(step.ToFolder, l.Folder)));
+ }
+
+ /// A sequence of inside the folder of one of that is not in ; null when there is none.
+ private static SequenceLocation? StrangerInside(IReadOnlyList locations, IEnumerable moving, IReadOnlySet taken)
{
var folders = moving.Select(s => s.FromFolder).ToList();
- if (folders.Count == 0) return null;
- return (await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false))
- .FirstOrDefault(l => !taken.Contains(l.Id) && folders.Any(f => RenamePlanner.IsInside(f, l.Folder)));
+ return folders.Count == 0 ? null : locations.FirstOrDefault(l => !taken.Contains(l.Id) && folders.Any(f => RenamePlanner.IsInside(f, l.Folder)));
}
///
@@ -378,17 +580,16 @@ public sealed class MassRenamer
/// committed meanwhile stays, and frames another process listed in the sidecar are left to the next scan, for which
/// the folder keeps no stamps unless the index held it as it was
/// (). A step
- /// that only renames files has nothing to record here.
+ /// that only renames files, or keeps its title, has nothing to record here.
///
- private async Task RecordAsync(Archive archive, ArchivePaths paths, RenameStep step, CancellationToken cancellationToken)
+ /// Whether the step's title is written (false: the title stays, edited since the plan or in a sidecar of a newer format).
+ private async Task RecordAsync(Archive archive, ArchivePaths paths, RenameStep step, bool retitle, CancellationToken cancellationToken)
{
- if (!step.MovesFolder && !step.Retitles) return;
+ if (!step.MovesFolder && !retitle) return;
var sequence = paths.Sequence(step.ToFolder);
var manifest = SequenceWriter.ReadLocked(sequence, step.SequenceId);
// The folder moved with its files' stamps, which the index recorded at its old place.
var inStep = await _indexer.InStepAsync(archive, step.FromFolder, sequence, manifest, cancellationToken).ConfigureAwait(false);
- // A step that only moves the folder keeps the title, which an edit may have changed since the plan.
- var retitle = step.Retitles;
if (retitle && manifest.Title != step.ToTitle)
{
manifest = manifest with
@@ -410,30 +611,33 @@ public sealed class MassRenamer
}
///
- /// The file renames of a step, under the leases its step holds, once its folder is in place (Reorganize, owner review 2,
- /// R7). The renames are planned again from the sidecar read under the lease (, with the
- /// step's title, the one the dry run showed), which names exactly what the dry run showed while its frames version is the plan's: another one
- /// refuses the step (), or, when resuming, leaves its files as they are. The files
- /// move in two phases through temporary names under the sequence's journal (), then the
- /// sidecar names them (the frame order unchanged, frames version + 1), then the index (),
- /// then the journal goes. When nothing moves here but the index names other files than the sidecar, a writer completed
- /// the renames after a crash () before the index followed, and the index is synced
- /// from the sidecar. Proxies (named by frame index), thumbnails (by index and content hash), renders and exports need
- /// nothing.
+ /// The file renames of a step, under the leases its step holds, once its folder is in place and every index row of the
+ /// step followed it (Reorganize, owner review 2, R7). The renames are planned again from the sidecar read under the
+ /// lease (, with the step's title, the one the dry run showed) and made only while they are
+ /// exactly the plan's (, checked before the folder moved already); found otherwise here (a writer
+ /// that takes no lease) the files keep their names, and so do they when one is in use (every file goes back to its
+ /// name): the step goes on either way. The files move in two phases through temporary names under the sequence's
+ /// journal (), then the sidecar names them (the frame order unchanged, frames version + 1),
+ /// then the index (), then the journal goes. A file that could not go back
+ /// after such a failure has the journal complete the renames at once, as the next writer would. When nothing moves here
+ /// but the index names other files than the sidecar, a writer completed the renames after a crash
+ /// () before the index followed, and the index is synced from the sidecar.
+ /// Proxies (named by frame index), thumbnails (by index and content hash), renders and exports need nothing.
///
/// False when the step renames no file, or its files kept their names.
- private async Task RenameFilesAsync(Archive archive, ArchivePaths paths, string? fileTemplate, RenameStep step, bool resuming, Tally result,
+ /// A file could neither go on to its new name nor back; the sequence's journal stays, and
+ /// its next writer (or the next archive open, for a plan its job holds) completes the renames.
+ private async Task RenameFilesAsync(Archive archive, ArchivePaths paths, string fileTemplate, RenameStep step, Tally result,
CancellationToken cancellationToken)
{
- if (fileTemplate is null || step.FileChanges == 0) return false;
+ if (step.FileChanges == 0) return false;
var sequence = paths.Sequence(step.ToFolder);
var manifest = SequenceWriter.ReadLocked(sequence, step.SequenceId);
var inStep = await _indexer.InStepAsync(archive, sequence, manifest, cancellationToken).ConfigureAwait(false);
var plan = FileRenames.Plan(manifest, step.ToTitle, PathTemplate.Parse(fileTemplate), FileRenames.ListPresent(sequence, manifest));
var before = manifest.Versions.Frames;
- if (plan.Moves.Count > 0 && before != step.FramesVersion)
+ if (plan.Moves.Count > 0 && !Matches(step, plan, manifest))
{
- if (!resuming) throw new StaleRenamePlanException(step.SequenceId);
Log.Warning("archive", $"The files of {sequence.RelativeFolder} keep their names: its photos changed after the plan was made");
result.FilesKept.Add(step.SequenceId);
return false;
@@ -442,9 +646,25 @@ public sealed class MassRenamer
{
var journal = FileRenameJournal.Create(before, plan.Moves, Guid.NewGuid().ToString("N")[..8]);
FileRenameJournal.Write(sequence, journal);
- journal = FileRenameJournal.Run(sequence, journal);
- manifest = FileRenameJournal.Applied(manifest, journal) ?? manifest;
- ManifestStore.WriteSequence(sequence.ManifestPath, manifest);
+ try
+ {
+ journal = FileRenameJournal.Run(sequence, journal);
+ manifest = FileRenameJournal.Applied(manifest, journal) ?? manifest;
+ ManifestStore.WriteSequence(sequence.ManifestPath, manifest);
+ }
+ catch (Exception ex) when ((ex is IOException or UnauthorizedAccessException) && FileRenameJournal.Read(sequence) is null)
+ {
+ // A file open elsewhere, and every file went back to its name: nothing of the sequence's files changed.
+ Log.Warning("archive", $"The files of {sequence.RelativeFolder} keep their names: one of them is in use", ex);
+ result.FilesBusy.Add(step.SequenceId);
+ return false;
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ // One could not go back: its journal completes the renames instead, now, and the sidecar names the files.
+ Log.Warning("archive", $"The files of {sequence.RelativeFolder} could not all go back after a failed rename; it is completed instead", ex);
+ manifest = SequenceWriter.ReadLocked(sequence, step.SequenceId);
+ }
result.Files += plan.Frames;
Log.Info("archive", $"{sequence.RelativeFolder}: {plan.Frames} file(s) renamed, {plan.Moves.Count - plan.Frames} companion(s) with them");
await _db.Frames.RenameFilesAsync(manifest.Id, await BehindAsync(sequence, manifest, cancellationToken).ConfigureAwait(false), before,
@@ -479,12 +699,25 @@ public sealed class MassRenamer
last = rows[^1].Index;
}
}
- /// What a run did, as it goes.
- private sealed class Tally
+
+ /// What a plan's runs did, as they go: from what its completed steps did already ().
+ private sealed class Tally(RenameTally done)
{
- public List Changed { get; } = [];
- public List Dropped { get; } = [];
- public List FilesKept { get; } = [];
- public int Files { get; set; }
+ public List Changed { get; } = [.. done.Changed];
+ public List Dropped { get; } = [.. done.Dropped];
+ public List FilesKept { get; } = [.. done.FilesKept];
+ public List TitlesKept { get; } = [.. done.TitlesKept];
+ public List FilesBusy { get; } = [.. done.FilesBusy];
+ public int Files { get; set; } = done.Files;
+
+ public RenameTally Record() => new()
+ {
+ Changed = [.. Changed],
+ Files = Files,
+ Dropped = [.. Dropped],
+ FilesKept = [.. FilesKept],
+ TitlesKept = [.. TitlesKept],
+ FilesBusy = [.. FilesBusy],
+ };
}
}
diff --git a/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs b/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs
index ddd355c0..ca710206 100644
--- a/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs
+++ b/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs
@@ -261,4 +261,110 @@ public sealed class ReorganizeTests(TestContext context)
Assert.FileMissing(sequence.File("originals/4.jpg"));
Assert.True(journal.Files.All(f => !File.Exists(sequence.File(f.Temp))));
}
+
+ ///
+ /// A rename of (a chain of names that free up, a swap, a cycle of three) a crash interrupted
+ /// in its first phase, once its first file reached its temporary name; each file holds its frame's id.
+ ///
+ private static (SequenceManifest Manifest, FileRenameJournal Journal) Interrupted(SequencePaths sequence, string shape)
+ {
+ string[][] names = shape switch
+ {
+ "chain" => [["originals/2.jpg", "originals/3.jpg", "originals/4.jpg"], ["originals/1.jpg", "originals/2.jpg", "originals/3.jpg"]],
+ "swap" => [["originals/a.jpg", "originals/b.jpg"], ["originals/b.jpg", "originals/a.jpg"]],
+ _ => [["originals/a.jpg", "originals/b.jpg", "originals/c.jpg"], ["originals/b.jpg", "originals/c.jpg", "originals/a.jpg"]],
+ };
+ var (from, to) = (names[0], names[1]);
+ var manifest = Sequence(from);
+ foreach (var f in manifest.Frames) Write(sequence, f.File, f.Id.ToString());
+ var journal = FileRenameJournal.Create(manifest.Versions.Frames, [.. manifest.Frames.Select((f, i) => new FileRename(f.Id, f.File, to[i]))], "t4");
+ FileRenameJournal.Write(sequence, journal);
+ File.Move(sequence.File(journal.Files[0].From), sequence.File(journal.Files[0].Temp));
+ return (manifest, journal);
+ }
+
+ private static void AssertEveryFrameHoldsItsPhoto(SequencePaths sequence, SequenceManifest recovered)
+ {
+ foreach (var f in recovered.Frames) Assert.Equal(f.Id.ToString(), Read(sequence, f.File));
+ Assert.Empty(Directory.EnumerateFiles(sequence.Originals, ".chronos-rename-*"));
+ }
+
+ [Theory]
+ [Case("chain")]
+ [Case("swap")]
+ [Case("cycle")]
+ public void ARecoveryRunAgainLeavesEveryFileWithItsFrame(string shape)
+ {
+ var sequence = Folder(out _);
+ var (manifest, _) = Interrupted(sequence, shape);
+ // Completed, but the sidecar that follows was never written (a full disk, a crash before it): the journal is found again.
+ var first = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
+ Assert.Equal(2, Assert.NotNull(FileRenameJournal.Read(sequence)).Phase);
+ var again = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
+ Assert.SequenceEqual(first.Frames.Select(f => f.File), again.Frames.Select(f => f.File));
+ Assert.False(manifest.Frames.Select(f => f.File).SequenceEqual(again.Frames.Select(f => f.File)), "every frame has its new name");
+ AssertEveryFrameHoldsItsPhoto(sequence, again);
+ }
+
+ [Test]
+ [WindowsOnly]
+ public void ARecoveryCutShortWhileFilesTakeTheirNewNamesGoesOnFromThere()
+ {
+ var sequence = Folder(out _);
+ var (manifest, journal) = Interrupted(sequence, "chain");
+ // The last file reached its temporary name too, and another program holds it: the recovery stops at it.
+ File.Move(sequence.File(journal.Files[2].From), sequence.File(journal.Files[2].Temp));
+ using (new FileStream(sequence.File(journal.Files[2].Temp), FileMode.Open, FileAccess.Read, FileShare.None))
+ Assert.Throws(() => FileRenameJournal.Recover(sequence, manifest));
+ Assert.Equal("originals/1.jpg", journal.Files[0].To);
+ Assert.Equal(manifest.Frames[0].Id.ToString(), Read(sequence, "originals/1.jpg"));
+
+ // Run again, it goes on from there: no file that took its new name moves again.
+ var recovered = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
+ Assert.SequenceEqual(["originals/1.jpg", "originals/2.jpg", "originals/3.jpg"], recovered.Frames.Select(f => f.File));
+ AssertEveryFrameHoldsItsPhoto(sequence, recovered);
+ }
+
+ [Test]
+ public void ANewNameTakenMeanwhileNeverStrandsAPhoto()
+ {
+ var sequence = Folder(out _);
+ var (manifest, journal) = Interrupted(sequence, "chain");
+ // Copied in under the name the second file gets after the crash: never written over. Its own old name is the third
+ // file's new one, so it takes the first free number of that name.
+ Write(sequence, "originals/2.jpg", "other");
+ FileRenameJournal.Delete(sequence);
+ Assert.NotNull(FileRenameJournal.Read(sequence));
+
+ var recovered = Assert.NotNull(FileRenameJournal.Recover(sequence, manifest));
+ Assert.SequenceEqual(["originals/1.jpg", "originals/3_2.jpg", "originals/3.jpg"], recovered.Frames.Select(f => f.File));
+ Assert.Equal("other", Read(sequence, "originals/2.jpg"));
+ AssertEveryFrameHoldsItsPhoto(sequence, recovered);
+ // The new name is in the journal, so a recovery run again finds every file where it is.
+ Assert.Equal("originals/3_2.jpg", Assert.NotNull(FileRenameJournal.Read(sequence)).Files[1].To);
+ Assert.SequenceEqual(recovered.Frames.Select(f => f.File), Assert.NotNull(FileRenameJournal.Recover(sequence, manifest)).Frames.Select(f => f.File));
+ AssertEveryFrameHoldsItsPhoto(sequence, recovered);
+ Assert.Equal(journal.Files.Count, Directory.EnumerateFiles(sequence.Originals).Count() - 1);
+ }
+
+ [Test]
+ public void APhotoWhoseCompanionsNewNameIsTakenStaysWithItsCompanion()
+ {
+ var sequence = Folder(out _);
+ var manifest = Sequence("originals/A.CR2");
+ Write(sequence, "originals/A.CR2", "photo");
+ Write(sequence, "originals/A.xmp", "sidecar");
+ var id = manifest.Frames[0].Id;
+ FileRename[] moves = [new(id, "originals/A.CR2", "originals/0001.CR2"), new(id, "originals/A.xmp", "originals/0001.xmp", Companion: true)];
+ var journal = FileRenameJournal.Create(manifest.Versions.Frames, moves, "t5") with { Phase = 2 };
+ FileRenameJournal.Write(sequence, journal);
+ foreach (var f in journal.Files) File.Move(sequence.File(f.From), sequence.File(f.Temp));
+ Write(sequence, "originals/0001.xmp", "other");
+
+ // Both go back to their names, together; the frame keeps its file.
+ Assert.Null(FileRenameJournal.Recover(sequence, manifest));
+ Assert.Equal(("photo", "sidecar", "other"), (Read(sequence, "originals/A.CR2"), Read(sequence, "originals/A.xmp"), Read(sequence, "originals/0001.xmp")));
+ Assert.FileMissing(sequence.File("originals/0001.CR2"));
+ Assert.Empty(Directory.EnumerateFiles(sequence.Originals, ".chronos-rename-*"));
+ }
}
diff --git a/tests/Chronos.Data.Tests/Archiving/ReorganizeTests.cs b/tests/Chronos.Data.Tests/Archiving/ReorganizeTests.cs
index 74082a48..3657c0de 100644
--- a/tests/Chronos.Data.Tests/Archiving/ReorganizeTests.cs
+++ b/tests/Chronos.Data.Tests/Archiving/ReorganizeTests.cs
@@ -1,3 +1,4 @@
+using System.Collections.Concurrent;
using System.Security.Cryptography;
using Chronos.Core.Archiving;
using Chronos.Core.Import;
@@ -5,6 +6,9 @@ using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Naming;
using Chronos.Data.Archiving;
+using Chronos.Data.Archiving.Jobs;
+using Chronos.Data.Jobs;
+using Chronos.Data.Repositories;
using Chronos.Testing;
using static Chronos.Data.Tests.Archiving.ArchiveFixture;
@@ -274,7 +278,7 @@ public sealed class ReorganizeTests(TestContext context)
// The job stops after the first sequence (paused) and holds its plan: an archive open leaves the rest alone.
using var cts = new CancellationTokenSource();
- await Assert.ThrowsAsync(() => fx.Service.Renamer.ApplyAsync(fx.Archive, plan, null, _ =>
+ await Assert.ThrowsAsync(() => fx.Service.Renamer.ApplyAsync(fx.Archive, plan, null, (_, _) =>
{
cts.Cancel();
return ValueTask.CompletedTask;
@@ -285,14 +289,322 @@ public sealed class ReorganizeTests(TestContext context)
Assert.SequenceEqual(untouched, await FilesAsync(fx, second));
Assert.True(Assert.NotNull(RenameJournal.Read(fx.Paths, plan.Id)).Held);
- // Resumed from its checkpoint, the job completes it; once its journal is gone a run finds the plan complete.
+ // Resumed from its checkpoint, the job completes it and counts the whole plan; once its journal is gone a run finds the plan complete.
var engine = await fx.StartEngineAsync();
var request = ArchiveJobs.Reorganize(fx.Archive, plan, templates);
var resumed = await fx.WaitForAsync((await fx.EnqueueWithCheckpointAsync(engine, request, "plan:" + plan.Id.ToString("N"))).Id, JobState.Done);
- Assert.Contains(Loc.L("Archive.Log.Reorganized", 1, 2), resumed.LogTail);
+ Assert.Contains(Loc.L("Archive.Log.Reorganized", 2, 4), resumed.LogTail);
Assert.SequenceEqual(["originals/001.JPG", "originals/002.JPG"], await FilesAsync(fx, second));
Assert.Null(RenameJournal.Read(fx.Paths, plan.Id));
var later = await fx.WaitForAsync((await fx.EnqueueWithCheckpointAsync(engine, request, "plan:" + plan.Id.ToString("N"))).Id, JobState.Done);
Assert.Contains(Loc.L("Archive.Log.ReorganizedEarlier"), later.LogTail);
}
+
+ ///
+ /// An uploaded sequence with a nested one adopted from a subfolder of its folder ("extra"), planned to move to
+ /// "moved/{date}" with new file names: the nested one is carried.
+ ///
+ private static async Task<(Guid Parent, Guid Nested, RenamePlan Plan)> CarrierAsync(ArchiveFixture fx)
+ {
+ var parent = await UploadAsync(fx, 2);
+ var folder = (await fx.Db.Sequences.GetAsync(parent))!.Folder;
+ Photos.WriteSeries(Path.Combine(fx.Paths.FromRelative(folder), "extra"), Start.AddDays(3), 2, firstSeed: 50);
+ await fx.ScanAsync();
+ var nested = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == folder + "/extra").Id;
+ var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [parent], new RenameTemplates(Folder: "moved/{date}", File: "{n:000}"), fx.Ct);
+ Assert.SequenceEqual([(parent, false, 2), (nested, true, 0)], plan.Steps.Select(s => (s.SequenceId, s.Carried, s.FileChanges)));
+ return (parent, nested, plan);
+ }
+
+ /// The folder of a sequence as the index has it, after checking that every frame the index lists is on disk there.
+ private static async Task FolderAsync(ArchiveFixture fx, Guid id)
+ {
+ var sequence = Assert.NotNull(await fx.Db.Sequences.GetAsync(id));
+ foreach (var frame in await fx.Db.Frames.ListBySequenceAsync(id))
+ {
+ Assert.True(frame.RelativePath.StartsWith(sequence.Folder + "/", StringComparison.Ordinal), frame.RelativePath);
+ Assert.FileExists(fx.Paths.FromRelative(frame.RelativePath));
+ }
+ return sequence.Folder;
+ }
+
+ [Test]
+ public async Task AStepWhosePhotosChangedIsRefusedUnderItsLeasesBeforeAnythingMoves()
+ {
+ await using var fx = await CreateAsync(context);
+ var (parent, nested, plan) = await CarrierAsync(fx);
+ var from = await FolderAsync(fx, parent);
+
+ // A photo uploaded once the plan's own checks passed, before its step: nothing of that step moves.
+ var stale = await Assert.ThrowsAsync(() => fx.Service.Renamer.ApplyAsync(fx.Archive, plan,
+ async _ => await UploadAsync(fx, 1, firstSeed: 9, firstSecond: 5), null, fx.Ct));
+ Assert.Equal(parent, stale.SequenceId);
+ var before = await FilesAsync(fx, parent);
+ Assert.Equal((from, from + "/extra"), (await FolderAsync(fx, parent), await FolderAsync(fx, nested)));
+ Assert.Empty(Assert.NotNull(RenameJournal.Read(fx.Paths, plan.Id)).Completed);
+
+ // Resumed after an interruption, the folder moves with the one inside it, whose index row follows, and the files keep their names.
+ var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
+ Assert.SequenceEqual([parent], result.FilesKept);
+ Assert.SequenceEqual([parent, nested], result.Changed);
+ Assert.Equal(("moved/2026-09-28", "moved/2026-09-28/extra"), (await FolderAsync(fx, parent), await FolderAsync(fx, nested)));
+ Assert.SequenceEqual(before, await FilesAsync(fx, parent));
+ Assert.Empty(RenameJournal.ListPending(fx.Paths));
+ }
+
+ [Test]
+ public async Task AFileThatCannotTakeItsNewNameKeepsTheNamesAndTheStepCompletes()
+ {
+ await using var fx = await CreateAsync(context);
+ var (parent, nested, plan) = await CarrierAsync(fx);
+ var before = await FilesAsync(fx, parent);
+ // A folder holds the name the second photo gets: that file cannot move there, so every file goes back to its name.
+ var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(parent))!.Folder);
+ Directory.CreateDirectory(folder.File(plan.Steps[0].FileSamples[1].To));
+
+ var result = await fx.Service.ApplyRenameAsync(plan, null, fx.Ct);
+ Assert.SequenceEqual([parent], result.FilesBusy);
+ Assert.SequenceEqual([parent, nested], result.Changed);
+ Assert.Equal(0, result.Files);
+ // The folder moved, the one inside it with it, both index rows followed, and the files kept their names.
+ Assert.Equal(("moved/2026-09-28", "moved/2026-09-28/extra"), (await FolderAsync(fx, parent), await FolderAsync(fx, nested)));
+ Assert.SequenceEqual(before, await FilesAsync(fx, parent));
+ Assert.Empty(RenameJournal.ListPending(fx.Paths));
+ }
+
+ [Test]
+ public async Task AFailedJobHoldsItsPlanAndTellsTheSequencesItChanged()
+ {
+ await using var fx = await CreateAsync(context);
+ var first = await UploadAsync(fx, 2);
+ var second = await UploadAsync(fx, 2, firstSeed: 20, firstSecond: 3600, key: "upload-2");
+ var templates = new RenameTemplates(File: "{n:000}");
+ var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [first, second], templates, fx.Ct);
+ var told = new ConcurrentQueue<(Guid, string)>();
+ var service = new ArchiveService(fx.Db, new ArchiveServiceOptions
+ {
+ Owner = "jobs",
+ LeaseWait = TimeSpan.FromSeconds(TestTiming.Budget(20)),
+ SequenceChanged = (id, change, _) =>
+ {
+ told.Enqueue((id, change));
+ return Task.CompletedTask;
+ },
+ });
+ await using var engine = new JobEngine(fx.Db, service.CreateJobHandlers(),
+ new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
+ await engine.StartAsync(fx.Ct);
+
+ // An editor holds the first sequence while the job starts its plan (planned again: its own id); a photo reaches the second meanwhile.
+ Job job;
+ using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{first:N}", "editor", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
+ {
+ job = await engine.EnqueueAsync(ArchiveJobs.Reorganize(fx.Archive, plan, templates), cancellationToken: fx.Ct);
+ var until = DateTime.UtcNow.AddSeconds(TestTiming.Budget(15));
+ while (RenameJournal.ListPending(fx.Paths).Count == 0)
+ {
+ Assert.True(DateTime.UtcNow < until, "the job never started its plan");
+ await Task.Delay(20, fx.Ct);
+ }
+ await UploadAsync(fx, 1, firstSeed: 30, firstSecond: 3605, key: "upload-2");
+ }
+
+ var failed = await fx.WaitForAsync(job.Id, JobState.Failed, (int)TestTiming.Budget(60));
+ Assert.Equal(Loc.L("Archive.Error.StalePlan"), failed.Error);
+ Assert.Contains(Loc.L("Archive.Log.Reorganized", 1, 2), failed.LogTail);
+ Assert.SequenceEqual([(first, MoveJobHandler.Change)], told);
+ var journal = RenameJournal.ListPending(fx.Paths).Single();
+ Assert.True(journal.Held, "only the job's retry goes on with it");
+ Assert.SequenceEqual([first], journal.Completed);
+ Assert.SequenceEqual(["originals/001.JPG", "originals/002.JPG"], await FilesAsync(fx, first));
+ var untouched = await FilesAsync(fx, second);
+ Assert.Count(3, untouched);
+
+ // An archive open leaves it alone; the retry checks the rest as a plan applied anew and gives it up.
+ Assert.Equal(0, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct));
+ Assert.SequenceEqual(untouched, await FilesAsync(fx, second));
+ var retried = await fx.WaitForAsync((await fx.EnqueueWithCheckpointAsync(engine, ArchiveJobs.Reorganize(fx.Archive, plan, templates),
+ "plan:" + journal.Plan.Id.ToString("N"))).Id, JobState.Failed, (int)TestTiming.Budget(60));
+ Assert.Equal(Loc.L("Archive.Error.StalePlan"), retried.Error);
+ Assert.Empty(RenameJournal.ListPending(fx.Paths));
+ Assert.SequenceEqual(untouched, await FilesAsync(fx, second));
+ Assert.Count(1, told);
+ }
+
+ [Test]
+ public async Task FileRenamesLeftHalfWayAreCompletedAtAnOpenWhileTheirPlanWaitsForItsJob()
+ {
+ await using var fx = await CreateAsync(context);
+ var id = await ChainAsync(fx);
+ var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], new RenameTemplates(File: "{n:0}", Title: "Roof"), fx.Ct);
+ // Its job failed inside the step once every file reached its temporary name: the plan held, the sequence's journal left.
+ RenameJournal.Write(fx.Paths, new RenameJournal(plan, [], Held: true));
+ var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(id))!.Folder);
+ var manifest = ManifestStore.ReadSequence(folder.ManifestPath);
+ var moves = FileRenames.Plan(manifest, "Roof", PathTemplate.Parse("{n:0}"), FileRenames.ListPresent(folder, manifest)).Moves;
+ var journal = FileRenameJournal.Create(manifest.Versions.Frames, moves, "held") with { Phase = 2 };
+ FileRenameJournal.Write(folder, journal);
+ foreach (var file in journal.Files) File.Move(folder.File(file.From), folder.File(file.Temp));
+
+ // Every photo goes on to its new name and the sidecar names it; the rest (the title) waits for the job.
+ await fx.Service.OpenAsync(fx.Archive.RootPath, fx.Ct);
+ Assert.SequenceEqual(["originals/1.JPG", "originals/2.JPG", "originals/3.JPG"], ManifestStore.ReadSequence(folder.ManifestPath).Frames.Select(f => f.File));
+ Assert.Null(FileRenameJournal.Read(folder));
+ Assert.Empty(Directory.EnumerateFiles(folder.Originals, ".chronos-rename-*"));
+ Assert.True(Assert.NotNull(RenameJournal.Read(fx.Paths, plan.Id)).Held);
+ Assert.Null((await fx.Db.Sequences.GetAsync(id))!.Title);
+
+ // Its job's retry completes it: the index follows the sidecar and the title changes.
+ var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
+ Assert.SequenceEqual([id], result.Changed);
+ Assert.SequenceEqual(["originals/1.JPG", "originals/2.JPG", "originals/3.JPG"], await FilesAsync(fx, id));
+ Assert.Equal("Roof", (await fx.Db.Sequences.GetAsync(id))!.Title);
+ Assert.Empty(RenameJournal.ListPending(fx.Paths));
+ }
+
+ [Test]
+ public async Task ASequenceDeletedBeforeTheJobRanIsNamedAndThePlanRefused()
+ {
+ await using var fx = await CreateAsync(context);
+ var first = await UploadAsync(fx, 2);
+ var second = await UploadAsync(fx, 2, firstSeed: 20, firstSecond: 3600, key: "upload-2");
+ var templates = new RenameTemplates(File: "{n:000}");
+ var request = ArchiveJobs.Reorganize(fx.Archive, await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [first, second], templates, fx.Ct), templates);
+ var before = await FilesAsync(fx, first);
+ // Deleted while the job waited in the queue.
+ await fx.Service.Trash.DeleteAsync(await fx.Service.Trash.PlanAsync([second], null, fx.Ct), null, null, fx.Ct);
+
+ var engine = await fx.StartEngineAsync();
+ var failed = await fx.WaitForAsync((await engine.EnqueueAsync(request, cancellationToken: fx.Ct)).Id, JobState.Failed);
+ Assert.Equal(Loc.L("Archive.Error.StalePlan"), failed.Error);
+ Assert.Contains(Loc.L("Archive.Log.SequenceMissing", second), failed.LogTail);
+ Assert.SequenceEqual(before, await FilesAsync(fx, first));
+ Assert.Empty(RenameJournal.ListPending(fx.Paths));
+ }
+
+ [Test]
+ public async Task ATitleEditedAfterThePlanIsNeverWrittenOver()
+ {
+ await using var fx = await CreateAsync(context);
+ var first = await UploadAsync(fx, 1);
+ var second = await UploadAsync(fx, 1, firstSeed: 20, firstSecond: 3600, key: "upload-2");
+ foreach (var id in new[] { first, second }) await fx.Service.SetSequenceInfoAsync(id, "Roof", [], 0, null, fx.Ct);
+ var templates = new RenameTemplates(Title: "{title} 2026");
+ var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [first, second], templates, fx.Ct);
+
+ // Retitled once the first sequence is done: refused before anything of it changes.
+ var stale = await Assert.ThrowsAsync(() => fx.Service.Renamer.ApplyAsync(fx.Archive, plan, null,
+ async (_, _) => await fx.Service.SetSequenceInfoAsync(second, "Roof north", [], 0, null, fx.Ct), fx.Ct));
+ Assert.Equal(second, stale.SequenceId);
+ Assert.Equal(("Roof 2026", "Roof north"), ((await fx.Db.Sequences.GetAsync(first))!.Title, (await fx.Db.Sequences.GetAsync(second))!.Title));
+ await FilesAsync(fx, second);
+
+ // Held by its job, the rest is refused as a plan applied anew.
+ Assert.True(MassRenamer.Hold(fx.Archive, plan.Id));
+ await Assert.ThrowsAsync(() => fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
+ Assert.Null(RenameJournal.Read(fx.Paths, plan.Id));
+
+ // Interrupted, the rest resumes without it; an edit only the index has (the server's API) counts as well.
+ var again = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [second], templates, fx.Ct);
+ RenameJournal.Write(fx.Paths, new RenameJournal(again, []));
+ await fx.Db.Sequences.EditAsync(second, new SequenceEdit { Title = "Roof south" }, null, fx.Ct);
+ var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, again.Id, null, fx.Ct));
+ Assert.SequenceEqual([second], result.Dropped);
+ Assert.Empty(result.Changed);
+ var row = Assert.NotNull(await fx.Db.Sequences.GetAsync(second));
+ Assert.Equal(("Roof south", "Roof north"), (row.Title, ManifestStore.ReadSequence(fx.Paths.Sequence(row.Folder).ManifestPath).Title));
+ }
+
+ [Test]
+ public async Task ASidecarOfANewerFormatIsNeverRewrittenNorAreItsFilesRenamed()
+ {
+ await using var fx = await CreateAsync(context);
+ var id = await UploadAsync(fx, 2);
+ var before = await FilesAsync(fx, id);
+ var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(id))!.Folder);
+ var templates = new RenameTemplates(File: "{n:000}");
+ var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], templates, fx.Ct);
+ Assert.Equal(2, plan.Steps.Single().FileChanges);
+
+ // A newer Chronos sharing the archive rewrote the sidecar after the dry run: refused before any file moves.
+ var json = File.ReadAllText(folder.ManifestPath);
+ Assert.Contains("\"schemaVersion\":1", json);
+ var newer = json.Replace("\"schemaVersion\":1", $"\"schemaVersion\":{SequenceManifest.CurrentSchemaVersion + 1}", StringComparison.Ordinal);
+ File.WriteAllText(folder.ManifestPath, newer);
+ await Assert.ThrowsAsync(() => fx.Service.ApplyRenameAsync(plan, null, fx.Ct));
+ Assert.SequenceEqual(before, await FilesAsync(fx, id));
+ // So is its step, under its lease, when the sidecar changes once the plan's own checks passed.
+ File.WriteAllText(folder.ManifestPath, json);
+ await Assert.ThrowsAsync(() => fx.Service.Renamer.ApplyAsync(fx.Archive, plan, _ =>
+ {
+ File.WriteAllText(folder.ManifestPath, newer);
+ return ValueTask.CompletedTask;
+ }, null, fx.Ct));
+ Assert.SequenceEqual(before, await FilesAsync(fx, id));
+ Assert.Null(FileRenameJournal.Read(folder));
+
+ // Interrupted there, the plan resumes at the next open without its files; a new dry run renames none of them.
+ await fx.Service.OpenAsync(fx.Archive.RootPath, fx.Ct);
+ Assert.Empty(RenameJournal.ListPending(fx.Paths));
+ Assert.SequenceEqual(before, await FilesAsync(fx, id));
+ var again = (await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], templates, fx.Ct)).Steps.Single();
+ Assert.Equal((0, RenameBlock.Unreadable), (again.FileChanges, again.Blocked.Single().Reason));
+ }
+
+ [Test]
+ public async Task AFolderTakenAfterThePlanIsNeverMovedInto()
+ {
+ await using var fx = await CreateAsync(context);
+ var first = await UploadAsync(fx, 1);
+ var second = await UploadAsync(fx, 1, firstSeed: 20, firstSecond: 86400, key: "upload-2");
+ var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [first, second], new RenameTemplates(Folder: "moved/{date}"), fx.Ct);
+ Assert.SequenceEqual(["moved/2026-09-28", "moved/2026-09-29"], plan.Steps.Select(s => s.ToFolder));
+ var from = await FolderAsync(fx, second);
+ var stranger = fx.Paths.FromRelative("moved/2026-09-29/note.txt");
+
+ // Once the first moved, something takes the second's new folder (a restore from the trash, a copy).
+ await Assert.ThrowsAsync(() => fx.Service.Renamer.ApplyAsync(fx.Archive, plan, null, (_, _) =>
+ {
+ Directory.CreateDirectory(Path.GetDirectoryName(stranger)!);
+ File.WriteAllText(stranger, "kept");
+ return ValueTask.CompletedTask;
+ }, fx.Ct));
+ Assert.Equal(("moved/2026-09-28", from), (await FolderAsync(fx, first), await FolderAsync(fx, second)));
+
+ // Held, the rest is refused before anything moves; interrupted, it is dropped.
+ Assert.True(MassRenamer.Hold(fx.Archive, plan.Id));
+ await Assert.ThrowsAsync(() => fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
+ RenameJournal.Write(fx.Paths, new RenameJournal(plan, [first]));
+ var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
+ Assert.SequenceEqual([second], result.Dropped);
+ Assert.Equal(from, await FolderAsync(fx, second));
+ Assert.Equal("kept", File.ReadAllText(stranger));
+ Assert.SequenceEqual(["note.txt"], Directory.EnumerateFileSystemEntries(Path.GetDirectoryName(stranger)!).Select(Path.GetFileName));
+ Assert.Empty(RenameJournal.ListPending(fx.Paths));
+ }
+
+ [Test]
+ public async Task PhotosHashedAfterThePlanKeepTheNamesItShowed()
+ {
+ await using var fx = await CreateAsync(context);
+ var id = await UploadAsync(fx, 3);
+ var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(id))!.Folder);
+ var manifest = ManifestStore.ReadSequence(folder.ManifestPath);
+ // The second photo was never verified: the dry run says it keeps its name.
+ ManifestStore.WriteSequence(folder.ManifestPath, manifest with { Frames = [manifest.Frames[0], manifest.Frames[1] with { Sha256 = null }, manifest.Frames[2]] });
+ var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, [id], new RenameTemplates(File: "{n:000}"), fx.Ct);
+ Assert.Equal((2, RenameBlock.Unhashed), (plan.Steps.Single().FileChanges, plan.Steps.Single().Blocked.Single().Reason));
+
+ // A verify hashes it afterwards, which leaves the frames version as it is: the plan would now rename it too.
+ ManifestStore.WriteSequence(folder.ManifestPath, manifest);
+ var before = await FilesAsync(fx, id);
+ await Assert.ThrowsAsync(() => fx.Service.ApplyRenameAsync(plan, null, fx.Ct));
+ Assert.SequenceEqual(before, await FilesAsync(fx, id));
+
+ // Interrupted before its step, it resumes without renaming any of them.
+ RenameJournal.Write(fx.Paths, new RenameJournal(plan, []));
+ var result = Assert.NotNull(await fx.Service.Renamer.ResumeAsync(fx.Archive, plan.Id, null, fx.Ct));
+ Assert.SequenceEqual([id], result.FilesKept);
+ Assert.SequenceEqual(before, await FilesAsync(fx, id));
+ }
}