fix(archive): stable Reorganize folders, settle trash restores first, retranslate Reorganize

Integrating owner review 2 phase 4a (R7) onto the reviewed phase 3 code.

- RenamePlanner: a sequence already at its expanded folder, or at the " (n)" of it that
  filing or an earlier Reorganize gave it, keeps that folder (in the template's case, unless
  another sequence holds that exact name). The planner seeded its claimed folders with the
  sequence's own, so "2026-09-28 (2)" was planned to "(3)" and back to "(2)" by the next
  run; a second Reorganize with the same templates now plans nothing.
- MassRenamer: each step settles what a crash or a busy trash entry left in its sequences
  (SequenceWriter.ReadLocked) and waits, as a sequence deletion does, while a restore from
  the trash is not settled (FrameTrash.EnsureSettled), before its folder or any file name
  changes: that restore's journal names files by the names they had. Trash records keep
  restoring after a Reorganize: into the sequence where it is now, under the record's own
  name made free (_2) with its companion following.
- Jobs.Kind.Move and Admin.Job.Kind.Move now mean "Reorganize": their old "Move archive"
  translations are removed from the 36 other cultures so they are translated again.
- Tests: a second run plans nothing (planner and archive), companions found alike by the
  trash and Reorganize whatever their case, a photo deleted before a Reorganize comes back
  beside the new names, a Reorganize waits for a busy restore and settles it first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-27 14:23:32 +01:00
1 parent ad72501f11
commit 950524929c
78 files changed
+183 -75

No files matched your search

+21 -1
View File
@@ -212,7 +212,7 @@ public static class RenamePlanner
if (folder is not null && s.Folder.Length > 0 && Blank(folder.ExpandFolder(context with { Title = newTitle })) is { } expanded
&& !expanded.Equals(s.Folder, StringComparison.Ordinal))
{
newFolder = expanded.Equals(s.Folder, StringComparison.OrdinalIgnoreCase) ? expanded : Unique(expanded, claimed, existsOnDisk);
newFolder = Kept(s.Folder, expanded, taken) ?? Unique(expanded, claimed, existsOnDisk);
claimed.Add(newFolder);
}
var step = new RenameStep(s.SequenceId, s.Folder, newFolder, s.Title, newTitle);
@@ -299,6 +299,26 @@ public static class RenamePlanner
private static string? Blank(string value) => string.IsNullOrWhiteSpace(value) ? null : value;
/// <summary>
/// The folder a sequence keeps (in the template's case) when it already is <paramref name="expanded"/>, or the
/// <c>" (n)"</c> of it that filing or an earlier Reorganize gave it beside another sequence of that name; null when it is
/// neither, or when another sequence holds that name exactly (a case-sensitive file system). Without it a sequence at
/// <c>2026-09-28 (2)</c> would be planned to <c>(3)</c>, its own folder being taken, and back to <c>(2)</c> by the
/// next Reorganize.
/// </summary>
private static string? Kept(string current, string expanded, IReadOnlyList<string> occupied)
{
string? same = null;
if (current.Equals(expanded, StringComparison.OrdinalIgnoreCase)) same = expanded;
else if (current.Length > expanded.Length + 3 && current.StartsWith(expanded + " (", StringComparison.OrdinalIgnoreCase) && current[^1] == ')'
&& int.TryParse(current.AsSpan(expanded.Length + 2, current.Length - expanded.Length - 3), NumberStyles.None, CultureInfo.InvariantCulture,
out var n) && n >= 2)
same = string.Create(CultureInfo.InvariantCulture, $"{expanded} ({n})");
// "(02)" is not a number Unique gives: that folder moves to a name of its own.
if (same is null || !same.Equals(current, StringComparison.OrdinalIgnoreCase)) return null;
return same.Equals(current, StringComparison.Ordinal) || !occupied.Any(o => o.Equals(same, StringComparison.Ordinal)) ? same : null;
}
private static string Unique(string folder, HashSet<string> claimed, Func<string, bool> existsOnDisk)
{
var candidate = folder;
+21 -1
View File
@@ -113,7 +113,9 @@ public sealed class MassRenamer
/// 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).</exception>
/// <exception cref="IOException">A file could not be renamed (open elsewhere); the step's files went back to their names.</exception>
/// <exception cref="IOException">A file could not be renamed (open elsewhere); the step's files went back to their names. 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.</exception>
public Task<RenameResult> ApplyAsync(Archive archive, RenamePlan plan, IProgress<int>? progress = null, CancellationToken cancellationToken = default) =>
ApplyAsync(archive, plan, null, progress is null ? null : n =>
{
@@ -296,6 +298,7 @@ public sealed class MassRenamer
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))
@@ -312,6 +315,23 @@ public sealed class MassRenamer
}
}
/// <summary>
/// Settles, under the step's leases and before anything of it moves, what a crash or a busy trash entry left in the
/// sequence, as every writer does (<see cref="SequenceWriter.ReadLocked"/>): 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 (<see cref="FrameTrash.EnsureSettled"/>) 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.
/// </summary>
/// <exception cref="LeaseUnavailableException">A restore from the trash into the sequence is not settled; nothing of the step changed.</exception>
private static void 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);
FrameTrash.EnsureSettled(sequence);
}
/// <summary>The index still has the step's sequence where the plan says (or where it already took it); otherwise stale.</summary>
/// <exception cref="StaleRenamePlanException">Not resuming, and the sequence moved or vanished since the plan was made.</exception>
private async Task<bool> StandsAsync(RenameStep step, bool resuming, CancellationToken cancellationToken)