fix(reorganize): never name a folder or title after a photo no longer first, and retry a kept job honestly
A Reorganize step whose photos changed after the plan kept its file names but still moved its folder and wrote the title the plan had computed from the old first photo, so a trashed first photo or an earlier photo filed since left the sequence named after a photo that is no longer first. The plan now journals its folder and title templates, and under the step's leases each folder move or retitle is expanded again from the settled sidecar: it is applied only when it gives what the plan gives (the folder itself or its " (n)"); otherwise the sequence stays as it was (Dropped, "changed after the plan"), or keeps its title and file names when an interrupted run moved its folder already (TitlesKept). Plans journaled before this change are applied as made. A Move job that ended failed with sequences kept, or whose held plan was given up, kept its plan:<id> checkpoint after the plan's journal was gone, so a retry logged "completed when the archive was opened" and ended done. Such a job now saves the checkpoint "applied": a retry plans again and applies only a plan that is still the one confirmed. A job that succeeds keeps plan:<id>, so a crash before it is marked done still finds the plan completed. Sequences a folder was to carry when that folder stayed (dropped, its title edited, its names no longer given, or a newer sidecar) were reported as "changed after the plan", and a newer-format carrier failed the job with ReorganizeKept. They are now a separate outcome, CarriedKept (journaled), logged with the new Archive.Log.ReorganizeCarriedKept (en+it) and not counted in the job's error, which follows the carrier's own reason. When a step's sidecar cannot be read or written after its folder moved, every sequence of the step is told (sequenceChanged "reorganized") before the failure is thrown, since the retry that would tell them may never come. Tests: Core APlanKeepsItsTemplatesSoTheNamesOfAFirstPhotoNoLongerFirstAreTold; Data AFolderOrTitleNamedAfterAPhotoNoLongerFirstIsNeverGiven, ARetryOfAJobThatKeptSequencesPlansAgain, ASequenceAFolderWasToCarryIsToldItStayedWithIt, AnArchiveOpensWhenAnInterruptedRenameMeetsASidecarOfANewerFormat, AResumedStepWhoseFolderMovedKeepsATitleEditedSinceAndTheNamesFromIt, and AMovedFolderWhoseSidecarCannotBeReadThereHasItsIndexRowFollowAndIsTold, which breaks the sidecar with a relative symbolic link instead of a read-only file, so it also runs on the Linux runner as root. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 130701afab224d47ea066eb7cc8debb16d1fb432)
This commit is contained in:
1 parent
37800b28ef
commit
8127373fd5
9 files changed
+455
-77
No files matched your search
@@ -22,22 +22,24 @@ public sealed class StaleRenamePlanException(Guid sequenceId)
|
||||
/// <summary>
|
||||
/// 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 (<see cref="Dropped"/>, only when resuming), whose files kept their names because their photos changed since
|
||||
/// (<see cref="FilesKept"/>, a sequence still receiving photos among them; their folder and title changed), whose title and
|
||||
/// file names stayed because their title was edited since (<see cref="TitlesKept"/>, only when resuming; their folder
|
||||
/// moved), whose files kept their names because a photo was in use or a new name was taken (<see cref="FilesBusy"/>; their
|
||||
/// folder and title changed), and whose title and file names stayed because a newer Chronos wrote their sidecar since
|
||||
/// (<see cref="Newer"/>, only when resuming; their folder moved, or, planned a new title, they stayed as they were when it
|
||||
/// had not moved yet).
|
||||
/// plan (<see cref="Dropped"/>: when resuming, or when photos changed since so that the templates now give them another
|
||||
/// folder or title), whose files kept their names because their photos changed since (<see cref="FilesKept"/>, a sequence
|
||||
/// still receiving photos among them; their folder and title changed), whose title and file names stayed because their
|
||||
/// title was edited since, or their photos changed so that the templates now give them another one (<see cref="TitlesKept"/>;
|
||||
/// their folder moved, with the folder around them or before an interruption), whose files kept their names because a
|
||||
/// photo was in use or a new name was taken (<see cref="FilesBusy"/>; their folder and title changed), whose title and file
|
||||
/// names stayed because a newer Chronos wrote their sidecar since (<see cref="Newer"/>, only when resuming; their folder
|
||||
/// moved, or, planned a new title, they stayed as they were when it had not moved yet), and that stayed as they were
|
||||
/// because the folder around them that was to carry them stayed, for a reason of its own (<see cref="CarriedKept"/>).
|
||||
/// </summary>
|
||||
public sealed record RenameResult(IReadOnlyList<Guid> Changed, int Files, IReadOnlyList<Guid> Dropped, IReadOnlyList<Guid> FilesKept,
|
||||
IReadOnlyList<Guid> TitlesKept, IReadOnlyList<Guid> FilesBusy, IReadOnlyList<Guid> Newer)
|
||||
IReadOnlyList<Guid> TitlesKept, IReadOnlyList<Guid> FilesBusy, IReadOnlyList<Guid> Newer, IReadOnlyList<Guid> CarriedKept)
|
||||
{
|
||||
/// <summary>The result a plan's journal records for its completed steps (<see cref="RenameJournal.Done"/>).</summary>
|
||||
public static RenameResult Of(RenameTally tally)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(tally);
|
||||
return new(tally.Changed, tally.Files, tally.Dropped, tally.FilesKept, tally.TitlesKept, tally.FilesBusy, tally.Newer);
|
||||
return new(tally.Changed, tally.Files, tally.Dropped, tally.FilesKept, tally.TitlesKept, tally.FilesBusy, tally.Newer, tally.CarriedKept);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +79,12 @@ public sealed class MassRenamer
|
||||
/// <summary>Its title was edited since the plan: its title and file names stay.</summary>
|
||||
Title,
|
||||
|
||||
/// <summary>
|
||||
/// Its photos changed since the plan so that the templates now give it another folder or title (a photo filed before
|
||||
/// the first, or the first one gone): its folder (unless it moved already), title and file names stay.
|
||||
/// </summary>
|
||||
Names,
|
||||
|
||||
/// <summary>Its sidecar is of a newer format, which is never rewritten (only when resuming): its title and file names stay.</summary>
|
||||
Newer,
|
||||
}
|
||||
@@ -157,7 +165,8 @@ public sealed class MassRenamer
|
||||
/// <summary>
|
||||
/// Applies a plan; each completed step is journaled before the next starts. A sequence whose photos changed since the
|
||||
/// plan (one still receiving them) refuses nothing: it keeps its file names (<see cref="RenameResult.FilesKept"/>), its
|
||||
/// folder and title change as planned, and the steps after it go on.
|
||||
/// folder and title change as planned while the templates still give them from its first photo as it is now (otherwise
|
||||
/// it stays as it was: <see cref="RenameResult.Dropped"/>), and the steps after it go on.
|
||||
/// </summary>
|
||||
/// <exception cref="StaleRenamePlanException">A sequence is no longer where the plan expects it, a folder that moves holds
|
||||
/// a sequence the plan does not take along, something took a folder the plan moves one into, or a title it changes, or
|
||||
@@ -167,9 +176,10 @@ public sealed class MassRenamer
|
||||
/// format, which this build never rewrites; nothing of that step was changed.</exception>
|
||||
/// <exception cref="IOException">A folder could not be moved, or (<see cref="LeaseUnavailableException"/>) a sequence is
|
||||
/// busy, or holds a restore from the trash whose entry is busy and not settled yet; nothing of that step changed. Once
|
||||
/// the folder of a step moved, a sidecar of the step that cannot be read or written (this, <see cref="InvalidDataException"/>
|
||||
/// or <see cref="NewerFormatException"/>) fails the step only after every index row of the step followed the folder; its
|
||||
/// title and file names wait for a retry. So does a file that could neither take its new name nor go back, when the
|
||||
/// the folder of a step moved, a sidecar of the step that cannot be read or written (this, <see cref="UnauthorizedAccessException"/>,
|
||||
/// <see cref="InvalidDataException"/> or <see cref="NewerFormatException"/>) fails the step only after every index row of
|
||||
/// the step followed the folder and each of its sequences was told (<see cref="ArchiveServiceOptions.SequenceChanged"/>);
|
||||
/// its title and file names wait for a retry. So does a file that could neither take its new name nor go back, when the
|
||||
/// sequence's journal cannot complete the renames at once either: its next writer does. A file that could not be
|
||||
/// renamed (open elsewhere, or its new name taken) does not fail the step: the sequence keeps its file names
|
||||
/// (<see cref="RenameResult.FilesBusy"/>).</exception>
|
||||
@@ -192,8 +202,9 @@ public sealed class MassRenamer
|
||||
|
||||
/// <summary>
|
||||
/// Completes the plan <paramref name="planId"/> from its journal: one an interruption left (as <see cref="ResumePendingAsync"/>
|
||||
/// 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).
|
||||
/// does), or one its job held, whose remaining steps are checked as a plan applied anew (what refuses a plan refuses the
|
||||
/// rest, and that journal goes; photos changed since do not). Null when there is no journal: the plan completed or was
|
||||
/// given up (here, or at an archive open).
|
||||
/// The result covers the whole plan, the steps of earlier runs included.
|
||||
/// </summary>
|
||||
/// <param name="stepDone">Told after each step is journaled, with the number of steps done and the sequences that step changed.</param>
|
||||
@@ -327,7 +338,7 @@ public sealed class MassRenamer
|
||||
? [.. 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);
|
||||
await ApplyStepAsync(archive, paths, journal.Plan, step, carried, resuming, result, cancellationToken).ConfigureAwait(false);
|
||||
completed.Add(step.SequenceId);
|
||||
foreach (var c in carried) completed.Add(c.SequenceId);
|
||||
journal = journal with { Completed = [.. completed], Done = result.Record() };
|
||||
@@ -409,15 +420,17 @@ public sealed class MassRenamer
|
||||
/// 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. 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: <see cref="FitAsync"/>; a sequence whose photos changed since keeps its file names). Then the folder moves,
|
||||
/// every index row follows it (a sidecar that cannot be read or written there fails the step once the others did), 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.
|
||||
/// settled: <see cref="FitAsync"/>; a sequence whose photos changed since keeps its file names, and its folder and title
|
||||
/// too when the templates now give it others). A folder that stays keeps the sequences it was to carry
|
||||
/// (<see cref="RenameResult.CarriedKept"/>). Then the folder moves, every index row follows it (a sidecar that cannot be
|
||||
/// read or written there fails the step once the others did and every sequence of the step was told), 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.
|
||||
/// </summary>
|
||||
/// <param name="resuming">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.</param>
|
||||
private async Task ApplyStepAsync(Archive archive, ArchivePaths paths, string? fileTemplate, RenameStep step, IReadOnlyList<RenameStep> carried,
|
||||
private async Task ApplyStepAsync(Archive archive, ArchivePaths paths, RenamePlan plan, RenameStep step, IReadOnlyList<RenameStep> carried,
|
||||
bool resuming, Tally result, CancellationToken cancellationToken)
|
||||
{
|
||||
var ids = carried.Select(c => c.SequenceId).Append(step.SequenceId).Order().ToList();
|
||||
@@ -434,10 +447,11 @@ public sealed class MassRenamer
|
||||
_writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
}
|
||||
// Checked before anything moves; a dropped step drops the sequences it would have carried, which stay inside it.
|
||||
// Checked before anything moves; a dropped step keeps the sequences it would have carried, which stay inside it.
|
||||
if (!await StandsAsync(step, resuming, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
result.Dropped.Add(step.SequenceId);
|
||||
result.CarriedKept.AddRange(carried.Select(c => c.SequenceId));
|
||||
return;
|
||||
}
|
||||
var follow = new List<RenameStep>(carried.Count);
|
||||
@@ -450,24 +464,24 @@ public sealed class MassRenamer
|
||||
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));
|
||||
result.CarriedKept.AddRange(follow.Select(c => c.SequenceId));
|
||||
return;
|
||||
}
|
||||
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 (edited since, or planned anew in a sidecar of a newer format) while nothing of it moved:
|
||||
// it stays as it was, with the sequences it would carry.
|
||||
foreach (var s in follow.Prepend(step)) fits.Add((s, await FitAsync(paths, plan, s, Settle(paths, s), resuming, cancellationToken).ConfigureAwait(false)));
|
||||
// Its folder or title no longer fits (its title edited since, the templates naming it otherwise now, or a new title
|
||||
// planned in a sidecar of a newer format) while nothing of it moved: it stays as it was, with the sequences it would carry.
|
||||
var first = fits[0].Fit;
|
||||
if ((first == Fit.Title || (first == Fit.Newer && step.Retitles)) && !Moved(paths, step))
|
||||
if ((first is Fit.Title or Fit.Names || (first == Fit.Newer && step.Retitles)) && !Moved(paths, step))
|
||||
{
|
||||
(first == Fit.Newer ? result.Newer : result.Dropped).Add(step.SequenceId);
|
||||
result.Dropped.AddRange(follow.Select(c => c.SequenceId));
|
||||
result.CarriedKept.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: a
|
||||
// sequence whose sidecar cannot be read or written now has its row follow all the same, and fails the step once
|
||||
// every other row did.
|
||||
// every other row did and every sequence of the step was told (the retry that would complete it may never come).
|
||||
ExceptionDispatchInfo? unrecorded = null;
|
||||
foreach (var (s, fit) in fits)
|
||||
{
|
||||
@@ -481,12 +495,17 @@ public sealed class MassRenamer
|
||||
unrecorded ??= ExceptionDispatchInfo.Capture(ex);
|
||||
}
|
||||
}
|
||||
unrecorded?.Throw();
|
||||
if (unrecorded is not null)
|
||||
{
|
||||
await TellAsync(_writer.Options.SequenceChanged, [.. fits.Select(f => f.Step.SequenceId)]).ConfigureAwait(false);
|
||||
unrecorded.Throw();
|
||||
}
|
||||
var fileTemplate = plan.FileTemplate;
|
||||
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);
|
||||
if (fit is Fit.Title or Fit.Names) result.TitlesKept.Add(s.SequenceId);
|
||||
else if (fit == Fit.Files) result.FilesKept.Add(s.SequenceId);
|
||||
else if (fit == Fit.Newer) result.Newer.Add(s.SequenceId);
|
||||
if (s.MovesFolder || (s.Retitles && TakesTitle(fit)) || renamed) result.Changed.Add(s.SequenceId);
|
||||
@@ -520,16 +539,19 @@ public sealed class MassRenamer
|
||||
/// How far a sequence of a step still fits the plan, under the step's leases with its sidecar settled
|
||||
/// (<paramref name="manifest"/>) 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 nor are names given from the old one (<see cref="TitleStands"/>),
|
||||
/// and the files are renamed only as the plan renames them (<see cref="FilesStand"/>). When not resuming, a newer format
|
||||
/// or an edited title refuses the step; photos changed since (a sequence still receiving them) never do: its files keep
|
||||
/// their names, and the rest of the step and of the plan goes on.
|
||||
/// a folder or title is given only while the templates still give it from the first photo as it is now
|
||||
/// (<see cref="NamesStand"/>), and the files are renamed only as the plan renames them (<see cref="FilesStand"/>). When
|
||||
/// not resuming, a newer format or an edited title refuses the step; photos changed since (a sequence still receiving
|
||||
/// them) never do: its files keep their names, and so do its folder and title when the templates now give it others,
|
||||
/// and the rest of the step and of the plan goes on.
|
||||
/// </summary>
|
||||
/// <exception cref="NewerFormatException">Not resuming, and the step would rewrite a sidecar of a newer format.</exception>
|
||||
/// <exception cref="StaleRenamePlanException">Not resuming, and its title was edited since the plan.</exception>
|
||||
private async Task<Fit> FitAsync(ArchivePaths paths, string? fileTemplate, RenameStep step, SequenceManifest manifest, bool resuming,
|
||||
private async Task<Fit> FitAsync(ArchivePaths paths, RenamePlan plan, RenameStep step, SequenceManifest manifest, bool resuming,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var sequence = Where(paths, step);
|
||||
var fileTemplate = plan.FileTemplate;
|
||||
var files = fileTemplate is not null && step.FileChanges > 0;
|
||||
if ((step.Retitles || files) && manifest.SchemaVersion > SequenceManifest.CurrentSchemaVersion)
|
||||
{
|
||||
@@ -543,6 +565,12 @@ public sealed class MassRenamer
|
||||
Log.Warning("archive", $"{sequence.RelativeFolder} keeps its title and file names: its title was edited after the plan was made");
|
||||
return Fit.Title;
|
||||
}
|
||||
if (!NamesStand(plan, step, manifest))
|
||||
{
|
||||
Log.Warning("archive", $"{sequence.RelativeFolder} keeps its folder, title and file names: its photos changed after the plan was made, "
|
||||
+ "and the templates now name it otherwise");
|
||||
return Fit.Names;
|
||||
}
|
||||
if (files && !FilesStand(sequence, PathTemplate.Parse(fileTemplate!), step, manifest))
|
||||
{
|
||||
Log.Warning("archive", $"The files of {sequence.RelativeFolder} keep their names: its photos changed after the plan was made");
|
||||
@@ -565,6 +593,21 @@ public sealed class MassRenamer
|
||||
private static bool Planned(RenameStep step, string? title) =>
|
||||
string.Equals(title, step.FromTitle, StringComparison.Ordinal) || string.Equals(title, step.ToTitle, StringComparison.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Whether the plan's templates still give a step's sequence the folder and the title the step gives it, from its first
|
||||
/// frame as <paramref name="manifest"/> has it now (<see cref="RenamePlanner.Names"/>, with the title the plan was made
|
||||
/// from): a photo filed before the first since the plan, or the first one gone, would have them named after a photo
|
||||
/// that is no longer first. Only what the step changes counts (a carried folder follows the folder around it); a plan
|
||||
/// journaled before it kept its templates is taken as it was made.
|
||||
/// </summary>
|
||||
private static bool NamesStand(RenamePlan plan, RenameStep step, SequenceManifest manifest)
|
||||
{
|
||||
bool retitles = step.Retitles && plan.TitleTemplate is not null, moves = step.MovesFolder && !step.Carried && plan.FolderTemplate is not null;
|
||||
if (!retitles && !moves) return true;
|
||||
var (title, folder) = RenamePlanner.Names(manifest, step.FromTitle, plan.FolderTemplate, plan.TitleTemplate);
|
||||
return (!retitles || string.Equals(title, step.ToTitle, StringComparison.Ordinal)) && (!moves || (folder is not null && RenamePlanner.Gives(folder, step.ToFolder)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the files of a step's sequence still rename as planned: planned again from <paramref name="manifest"/> and the
|
||||
/// files beside its frames, they rename exactly the files the plan renamed (<see cref="Matches"/>), or none is left to
|
||||
@@ -806,6 +849,7 @@ public sealed class MassRenamer
|
||||
public List<Guid> TitlesKept { get; } = [.. done.TitlesKept];
|
||||
public List<Guid> FilesBusy { get; } = [.. done.FilesBusy];
|
||||
public List<Guid> Newer { get; } = [.. done.Newer];
|
||||
public List<Guid> CarriedKept { get; } = [.. done.CarriedKept];
|
||||
public int Files { get; set; } = done.Files;
|
||||
|
||||
public RenameTally Record() => new()
|
||||
@@ -817,6 +861,7 @@ public sealed class MassRenamer
|
||||
TitlesKept = [.. TitlesKept],
|
||||
FilesBusy = [.. FilesBusy],
|
||||
Newer = [.. Newer],
|
||||
CarriedKept = [.. CarriedKept],
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user