fix(archive): weigh replaced originals against the archive, not this index

Owner review 2, phase 4b, fix round 2 (trash and sync).

- Emptying a Replaced entry decided "conversion deleted" from this host's index
  alone, so a conversion another host made, or one this index had not read yet,
  could be taken for deleted and its originals erased without re-hashing it. The
  survey now reads the archive: the sequence's sidecar where the index or the
  entry says it is, in the trash, or found by walking the archive's folders. Its
  Replaced tombstones are followed and every conversion it holds is re-hashed. A
  conversion counts as deleted only when the sidecar shows it (a Deleted
  tombstone, or the frame gone while every frame is hashed and no photo of the
  folder is unlisted), when the sequence is in the trash, or when no sidecar of it
  exists and this index has no row either. Whatever the archive cannot tell, or a
  converted frame whose file changed since, keeps the entry, and the job log says
  why (changed after the plan, sequence busy, conversion no longer matches,
  conversion not located).
- The erase weighs a Replaced entry under the entry's and the sequence's leases
  and settles the sequence first (an undo waiting for that entry's lease
  completes), so the entry it reads again and fingerprints is the one it erases.
  Read before the settle, a consumed entry's folder vanished under the erase.
- Each sidecar is indexed once per survey: one conversion's entry (every frame it
  swapped) is weighed in one pass, not one pass over the sidecar per photo.
- The desktop Empty confirmation names every entry whose conversion was deleted
  first, with why, wherever the trash lists it; the detail scrolls when long. The
  panel lists them first as well.
- Download originals queues the passes a frame swapped to another encoding
  cleared (light analysis, the deep analysis it had, thumbnails, proxies), as any
  replacement does.
- The job log lines of uploads and downloads are resources (en, it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 27d9a512183e64da9a3d7fa69a267470ca3ce744)
This commit is contained in:
Alby96 committed 2026-11-02 10:33:34 +01:00
1 parent c669d0504d
commit 48a0ce3e02
15 files changed
+834 -164

No files matched your search

@@ -41,17 +41,21 @@ public static class TrashEmptyPlans
/// <summary>
/// The confirmation of erasing the planned entries for good; null when the plan holds none. An entry of replaced originals
/// whose conversion was deleted since says so (<see cref="TrashEntry.ConversionDeleted"/>): erasing it may erase the last
/// copy of those photos.
/// copy of those photos, so every such entry is named first, with why, wherever the trash lists it; the other entries
/// fill the lines left (<see cref="DeletePlans.ListedLines"/>) and the rest are counted.
/// </summary>
public static ConfirmRequest? Request(TrashEmptyPlan plan)
{
ArgumentNullException.ThrowIfNull(plan);
if (plan.Entries.Count == 0) return null;
var count = plan.Entries.Count.ToString(CultureInfo.InvariantCulture);
var lines = plan.Entries.Take(DeletePlans.ListedLines).Select(e => Loc.L("Archive.Delete.Line", e.Title,
var lastCopies = plan.Entries.Where(e => e.ConversionDeleted).ToList();
var others = plan.Entries.Where(e => !e.ConversionDeleted).ToList();
var listed = Math.Max(0, DeletePlans.ListedLines - lastCopies.Count);
var lines = lastCopies.Concat(others.Take(listed)).Select(e => Loc.L("Archive.Delete.Line", e.Title,
KindText(e.Kind) + Loc.Separator + Loc.Plural("Shell.Status.Photos", e.Frames)
+ (e.ConversionDeleted ? Loc.Separator + Loc.L("Archive.Trash.ConversionDeleted") : string.Empty), Formatting.Bytes(e.Bytes))).ToList();
if (plan.Entries.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", plan.Entries.Count - DeletePlans.ListedLines));
if (others.Count > listed) lines.Add(Loc.Plural("Archive.Delete.More", others.Count - listed));
return new ConfirmRequest
{
Heading = Loc.Plural("Archive.Trash.EmptyHeading", plan.Entries.Count),
+6 -2
View File
@@ -12,10 +12,14 @@
<StackPanel Margin="24,18,24,20">
<TextBlock Text="{Binding Heading}" Style="{StaticResource Text.Title}" TextWrapping="Wrap" TextTrimming="None" />
<TextBlock Text="{Binding Message}" Margin="0,8,0,0" Foreground="{DynamicResource Brush.TextMuted}" TextWrapping="Wrap" TextTrimming="None" LineHeight="18" />
<!-- A detail longer than the card (every trash entry that may hold the last copy of its photos is named) scrolls, so the
buttons stay on a 768 px screen. -->
<Border Style="{StaticResource Border.Card}" Margin="0,16,0,0" Padding="12,10" ToolTip="{Binding DetailToolTip}"
Visibility="{Binding Detail, Converter={x:Static ui:NullToCollapsedConverter.Instance}}">
<TextBlock x:Name="DetailText" Text="{Binding Detail}" FontFamily="{StaticResource Font.Mono}" FontSize="{StaticResource Size.Secondary}" TextWrapping="Wrap" TextTrimming="None" LineHeight="18"
FlowDirection="LeftToRight" />
<ScrollViewer MaxHeight="288" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Focusable="False">
<TextBlock x:Name="DetailText" Text="{Binding Detail}" FontFamily="{StaticResource Font.Mono}" FontSize="{StaticResource Size.Secondary}" TextWrapping="Wrap" TextTrimming="None" LineHeight="18"
FlowDirection="LeftToRight" />
</ScrollViewer>
</Border>
<StackPanel x:Name="Typing" Margin="0,16,0,0" Visibility="Collapsed">
<TextBlock Text="{Binding TypedPrompt}" Foreground="{DynamicResource Brush.TextMuted}" TextWrapping="Wrap" />
@@ -627,12 +627,13 @@ public sealed partial class FrameTrash
/// <paramref name="sha256"/> and has no file, <paramref name="staged"/> takes its place (<see cref="ReplaceAsync"/>,
/// <see cref="FrameSwap.Missing"/>): the same id and index, the user's flags, a record read from the file as filing reads
/// one (named as the frame with the file's extension), the old content as its source and a tombstone naming the new one,
/// so the photo is neither filed twice nor missing. Nothing goes to the trash. False, nothing touched, when the frame
/// changed or its file is back.
/// so the photo is neither filed twice nor missing. Nothing goes to the trash. Returns the sequence as it is now and its
/// statuses before, whose passes (<see cref="FollowUps"/>) the swap cleared as any replacement does; null, nothing
/// touched, when the frame changed or its file is back.
/// </summary>
/// <param name="staged">The verified copy, inside the archive (its staging); moved in.</param>
/// <param name="fileName">The copy's name on the other side, whose extension the frame's file takes.</param>
internal async Task<bool> ReplaceMissingAsync(Archive archive, Guid sequenceId, Guid frameId, string sha256, string staged, string stagedSha256,
internal async Task<ReplaceResult?> ReplaceMissingAsync(Archive archive, Guid sequenceId, Guid frameId, string sha256, string staged, string stagedSha256,
string fileName, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(archive);
@@ -641,10 +642,9 @@ public sealed partial class FrameTrash
var metadata = Core.Metadata.MetadataReader.Read(staged, cancellationToken);
var extension = Path.GetExtension(fileName);
var swap = new FrameSwap(frameId, paths.ToRelative(staged), [], Named, Read) { Missing = true };
var replaced = await ReplaceAsync(archive, sequenceId, null, [swap], null, null, null,
return await ReplaceAsync(archive, sequenceId, null, [swap], null, null, null,
(manifest, _, _) => Task.FromResult(manifest.Frames.Any(f => f.Id == frameId && string.Equals(f.Sha256, sha256, StringComparison.OrdinalIgnoreCase))),
cancellationToken).ConfigureAwait(false);
return replaced is not null;
string Named(SidecarFrame current, SequenceManifest _) => extension.Length > 1 ? Path.ChangeExtension(current.File, extension) : current.File;
@@ -10,8 +10,9 @@ namespace Chronos.Data.Archiving.Jobs;
/// <see cref="JobKind.EmptyTrash"/> (owner review 2, R6): erases exactly the entries of a confirmed plan
/// (<see cref="EmptyTrashParameters"/>) and fails, erasing nothing, when the trash no longer matches the plan's
/// fingerprint (<see cref="StaleTrashPlanException"/>). Checkpoint: <c>done:&lt;n&gt;</c>, the entries done; a resumed job
/// checks each remaining entry against its own fingerprint. Entries kept (a replacement that no longer matches, an entry
/// changed since) are logged and fail the job at the end, after the others are erased.
/// checks each remaining entry against its own fingerprint. Entries kept (an entry changed since, a conversion of replaced
/// originals that no longer matches, cannot be located or is busy) are logged each with why, and fail the job at the end,
/// after the others are erased.
/// </summary>
internal sealed class EmptyTrashJobHandler(ChronosDatabase db, SequenceTrash trash) : IJobHandler
{
@@ -31,7 +32,7 @@ internal sealed class EmptyTrashJobHandler(ChronosDatabase db, SequenceTrash tra
var result = await trash.EmptyAsync(archive, parameters, start,
(n, total) => context.CheckpointAsync(Prefix + n.ToString(CultureInfo.InvariantCulture), n, total), context.Cancellation).ConfigureAwait(false);
context.Log(Loc.L("Archive.Log.TrashEmptied", result.Erased, CleanupSteps.Megabytes(result.Bytes)));
foreach (var title in result.Refused) context.Log(Loc.L("Archive.Log.TrashKept", title));
foreach (var kept in result.Refused) context.Log(kept.LogLine);
if (result.Refused.Count > 0) throw new InvalidOperationException(Loc.Plural("Archive.Error.TrashKept", result.Refused.Count));
}
}
+122 -34
View File
@@ -21,8 +21,43 @@ public sealed record TrashEmptyPlan(Guid ArchiveId, IReadOnlyList<TrashEntry> En
public int Photos => Entries.Sum(e => e.Frames);
}
/// <summary>What an emptying erased, and the entries it kept (a replacement that no longer matches, an entry changed since the plan).</summary>
public sealed record TrashEmptyResult(int Erased, long Bytes, IReadOnlyList<string> Refused);
/// <summary>What an emptying erased, and the entries it kept, each with why (<see cref="TrashKept"/>).</summary>
public sealed record TrashEmptyResult(int Erased, long Bytes, IReadOnlyList<TrashKept> Refused);
/// <summary>An entry an emptying kept in the trash: its title and why.</summary>
public sealed record TrashKept(string Title, TrashKeptReason Reason)
{
/// <summary>"Kept in the trash, changed after the plan: Etna", a job log line in the current culture.</summary>
public string LogLine => Loc.L(Reason switch
{
TrashKeptReason.Mismatch => "Archive.Log.TrashKeptMismatch",
TrashKeptReason.Unknown => "Archive.Log.TrashKeptUnknown",
TrashKeptReason.Busy => "Archive.Log.TrashKeptBusy",
_ => "Archive.Log.TrashKept",
}, Title);
}
/// <summary>Why an emptying kept an entry in the trash.</summary>
public enum TrashKeptReason
{
/// <summary>The entry changed since the emptying was planned (restored, or deleted again, meanwhile), or is gone.</summary>
Changed,
/// <summary>
/// A conversion of its originals (owner review 2, R7) no longer matches its content, in its sequence or in the trash, or
/// its frame holds other content now (its file changed since the conversion).
/// </summary>
Mismatch,
/// <summary>
/// The archive cannot tell where a conversion of its originals is (a sidecar that cannot be read, a frame never hashed, a
/// sequence the index has and the archive does not): a scan of the archive settles it.
/// </summary>
Unknown,
/// <summary>Its sequence is busy: another writer holds it, or a change to it is not settled yet.</summary>
Busy,
}
/// <summary>The trash changed after the emptying was planned (an entry came, went or changed); nothing was erased.</summary>
public sealed class StaleTrashPlanException() : InvalidOperationException(Loc.L("Archive.Error.TrashChanged"));
@@ -53,7 +88,9 @@ public sealed partial class SequenceTrash
var (files, bytes) = Directory.Exists(folder) ? Measure(folder, cancellationToken) : (0, 0L);
return e with { Files = files, Bytes = bytes };
}).ToList(), cancellationToken).ConfigureAwait(false);
for (var i = 0; i < measured.Count; i++) measured[i] = await PlannedAsync(paths, measured[i], cancellationToken).ConfigureAwait(false);
// The sequences of replaced entries, found once for the whole plan.
var located = new Dictionary<Guid, Located>();
for (var i = 0; i < measured.Count; i++) measured[i] = await PlannedAsync(archive, paths, measured[i], located, cancellationToken).ConfigureAwait(false);
var unknown = names?.Where(n => !listed.Any(e => e.Name == n)).Distinct(StringComparer.Ordinal).ToList() ?? [];
return new TrashEmptyPlan(archive.Id, measured, measured.Sum(e => e.Bytes), Fingerprint(measured), unknown);
}
@@ -87,9 +124,11 @@ public sealed partial class SequenceTrash
/// hold exactly the planned entries (<see cref="EmptyTrashParameters.Fingerprint"/>), else nothing is erased; a run
/// resumed after a stop checks each remaining entry against its own fingerprint, keeps one that changed and skips one
/// that is gone, and each entry is checked against its fingerprint once more under its lease, just before it goes. A
/// <see cref="TrashKind.Replaced"/> entry is erased only while every replacement still matches its SHA-256 where it is now
/// (re-hashed now: in the sequence, or in the trash when its conversion was deleted, as the dry run said); otherwise it is
/// kept. Leftovers of an interrupted erase are deleted first.
/// <see cref="TrashKind.Replaced"/> entry is weighed under its sequence's lease as well, against the archive as it is then
/// (<see cref="Survey"/>): it is erased only while every conversion of its originals still matches its SHA-256 where the
/// archive shows it (re-hashed now: in the sequence, or a copy the trash holds when its conversion was deleted, as the
/// dry run said); otherwise, or when the archive cannot tell, it is kept, with why. Leftovers of an interrupted erase are
/// deleted first.
/// </summary>
/// <param name="progress">Entries done so far (the resume point) and their number.</param>
/// <exception cref="StaleTrashPlanException">The trash no longer holds exactly the planned entries; nothing was erased.</exception>
@@ -104,7 +143,7 @@ public sealed partial class SequenceTrash
var items = parameters.Entries;
var current = await PlanEmptyAsync(archive, [.. items.Skip(start).Select(i => i.Name)], cancellationToken).ConfigureAwait(false);
if (start == 0 && (current.Fingerprint != parameters.Fingerprint || current.Unknown.Count > 0)) throw new StaleTrashPlanException();
var refused = new List<string>();
var refused = new List<TrashKept>();
int erased = 0;
long bytes = 0;
for (var i = start; i < items.Count; i++)
@@ -115,17 +154,17 @@ public sealed partial class SequenceTrash
{
if (EntryFingerprint(entry) != item.Fingerprint)
{
refused.Add(entry.Title);
refused.Add(new TrashKept(entry.Title, TrashKeptReason.Changed));
Log.Warning("archive", $"Trash entry {entry.Name} changed since the emptying was planned; it is kept");
}
else if (await EraseAsync(archive, paths, entry, item.Fingerprint, cancellationToken).ConfigureAwait(false))
else if (await EraseAsync(archive, paths, entry, item.Fingerprint, cancellationToken).ConfigureAwait(false) is { } reason)
{
erased++;
bytes += entry.Bytes;
refused.Add(new TrashKept(entry.Title, reason));
}
else
{
refused.Add(entry.Title);
erased++;
bytes += entry.Bytes;
}
}
await progress(i + 1, items.Count).ConfigureAwait(false);
@@ -136,49 +175,98 @@ public sealed partial class SequenceTrash
/// <summary>
/// Erases one entry under its lease, once its record read and its folder measured again under it still give the
/// planned <paramref name="fingerprint"/> (another host sharing the archive may have restored the entry and deleted the
/// same sequence again since); false (logged) when it changed, or a replaced entry's replacements no longer match.
/// same sequence again since). A replaced entry is weighed under its sequence's lease too, so no writer of the sequence
/// moves its conversions meanwhile, and only once what a writer left unsettled in the sequence is settled (an undo whose
/// completion waited for this entry's lease takes photos out of it): the archive must tell where each conversion is (as
/// the dry run said, which the fingerprint holds), and each must still match. Returns null once erased, else why the
/// entry is kept (logged).
/// </summary>
private async Task<bool> EraseAsync(Archive archive, ArchivePaths paths, TrashEntry entry, string fingerprint, CancellationToken cancellationToken)
private async Task<TrashKeptReason?> EraseAsync(Archive archive, ArchivePaths paths, TrashEntry entry, string fingerprint, CancellationToken cancellationToken)
{
using (await LeaseFile.AcquireAsync(paths.LocksDirectory, EntryLease(entry.Name), _writer.Options.Owner, _writer.Options.LeaseWait,
_writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false))
{
var (folder, record) = Location(paths, entry);
if (!Directory.Exists(folder) || !File.Exists(record)) return false;
if (await RereadAsync(paths, entry, cancellationToken).ConfigureAwait(false) is not { } now || EntryFingerprint(now) != fingerprint)
if (!Directory.Exists(folder) || !File.Exists(record)) return TrashKeptReason.Changed;
IAsyncDisposable? sequence = null;
if (entry.Kind == TrashKind.Replaced)
{
Log.Warning("archive", $"Trash entry {entry.Name} changed since the emptying was planned; it is kept");
return false;
try
{
sequence = await _writer.LockAsync(paths, entry.SequenceId, cancellationToken).ConfigureAwait(false);
}
catch (LeaseUnavailableException ex)
{
Log.Warning("archive", $"Trash entry {entry.Name} is kept: its sequence is busy", ex);
return TrashKeptReason.Busy;
}
}
if (entry.Kind == TrashKind.Replaced && !await ReplacementsHoldAsync(archive, paths, entry, cancellationToken).ConfigureAwait(false))
await using (sequence)
{
Log.Warning("archive", $"Trash entry {entry.Name} is kept: a replacement of its originals no longer matches");
return false;
// Settled before the entry is read again, so what is read is what an erase takes.
var located = sequence is null ? null : await LocateAsync(archive, paths, entry, EntryLease(entry.Name), cancellationToken).ConfigureAwait(false);
var (now, survey) = Reread(archive, paths, entry, located, cancellationToken);
if (survey?.Kept is { } kept)
{
Log.Warning("archive", $"Trash entry {entry.Name} is kept ({kept}): the archive does not show the conversions of its originals whole");
return kept;
}
if (now is null || EntryFingerprint(now) != fingerprint)
{
Log.Warning("archive", $"Trash entry {entry.Name} changed since the emptying was planned; it is kept");
return TrashKeptReason.Changed;
}
if (survey is not null && !await MatchAsync(survey, cancellationToken).ConfigureAwait(false))
{
Log.Warning("archive", $"Trash entry {entry.Name} is kept: a conversion of its originals no longer matches");
return TrashKeptReason.Mismatch;
}
var aside = Path.Combine(TrashDirectory(paths), ErasingFolder, Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.GetDirectoryName(aside)!);
Directory.Move(folder, aside);
File.Delete(record);
Erase(aside);
Log.Info("archive", $"Trash entry {entry.Name} ({entry.Title}, {entry.Files} files, {entry.Bytes} bytes) erased"
+ (now.ConversionDeleted ? "; the conversion of its originals had been deleted" : string.Empty));
return null;
}
var aside = Path.Combine(TrashDirectory(paths), ErasingFolder, Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.GetDirectoryName(aside)!);
Directory.Move(folder, aside);
File.Delete(record);
Erase(aside);
Log.Info("archive", $"Trash entry {entry.Name} ({entry.Title}, {entry.Files} files, {entry.Bytes} bytes) erased"
+ (now.ConversionDeleted ? "; the conversion of its originals had been deleted" : string.Empty));
return true;
}
}
/// <summary>
/// An entry as the trash lists it and the emptying plans it (<see cref="PlannedAsync"/>), from its record and folder as
/// they are now; null when its record cannot be read.
/// An entry as the trash lists it and an erase plans it, from its record and folder as they are now, and a replaced
/// entry's survey against where its sequence was found under its lease (<paramref name="located"/>, which says whether the
/// conversion of one of its photos was deleted, <see cref="TrashEntry.ConversionDeleted"/>); a null entry when its record
/// cannot be read or its folder is gone.
/// </summary>
private async Task<TrashEntry?> RereadAsync(ArchivePaths paths, TrashEntry entry, CancellationToken cancellationToken)
private static (TrashEntry? Entry, ConversionSurvey? Survey) Reread(Archive archive, ArchivePaths paths, TrashEntry entry, Located? located,
CancellationToken cancellationToken)
{
var (folder, recordPath) = Location(paths, entry);
var now = FrameTrash.IsPhotoEntry(entry.Name)
? FrameTrash.ReadListed(paths, entry.ArchiveId, entry.Name)
: ReadRecord(recordPath) is { } record ? Entry(entry.ArchiveId, Path.GetFileName(entry.Name), record) : null;
if (now is null) return null;
if (now is null || !Directory.Exists(folder)) return (null, null);
var (files, bytes) = Measure(folder, cancellationToken);
return await PlannedAsync(paths, now with { Files = files, Bytes = bytes }, cancellationToken).ConfigureAwait(false);
now = now with { Files = files, Bytes = bytes };
if (located is null) return (now, null);
if (FrameTrash.ReadEntry(paths, entry.Name) is not { } photos) return (null, null);
var survey = Survey(archive, paths, entry.Name, photos, located, cancellationToken);
return (survey.Deleted ? now with { ConversionDeleted = true } : now, survey);
}
/// <summary>
/// <paramref name="entry"/> as an emptying plans it: a replaced entry is <see cref="TrashEntry.ConversionDeleted"/> when the
/// archive shows the conversion of one of its photos deleted (<see cref="Survey"/>), so the dry run says so. Its sequence
/// is found once per plan (<paramref name="located"/>). Reads sidecars and records; hashes nothing.
/// </summary>
private async Task<TrashEntry> PlannedAsync(Archive archive, ArchivePaths paths, TrashEntry entry, Dictionary<Guid, Located> located,
CancellationToken cancellationToken)
{
if (entry.Kind != TrashKind.Replaced || FrameTrash.ReadEntry(paths, entry.Name) is not { } record) return entry;
if (!located.TryGetValue(entry.SequenceId, out var where))
located[entry.SequenceId] = where = await LocateAsync(archive, paths, entry, null, cancellationToken).ConfigureAwait(false);
return Survey(archive, paths, entry.Name, record, where, cancellationToken).Deleted ? entry with { ConversionDeleted = true } : entry;
}
/// <summary>The folder and record of an entry of either kind.</summary>
@@ -5,101 +5,273 @@ using Chronos.Core.Model;
namespace Chronos.Data.Archiving;
/// <summary>
/// Where the replacements of a replaced entry's originals are now (owner review 2, R7), for the emptying of the trash: in
/// the sequence (the replacement, or what replaced it in turn: a conversion of a conversion, followed through the sequence's
/// Replaced tombstones), in the trash (the conversion deleted as photos or with its sequence, or the original of a later
/// conversion), or nowhere (deleted and erased).
/// Where the conversions of a replaced entry's originals are now (owner review 2, R7), for the emptying of the trash, as the
/// archive itself shows it, never as this host's index alone has it (another host sharing the archive may have converted,
/// moved or deleted since): the sequence's sidecar, found where the index or the entry says the sequence is, in the trash,
/// or by walking the archive's folders, followed through its Replaced tombstones (a conversion of a conversion), and the
/// files it names. A conversion counts as deleted only when the archive shows it: a Deleted tombstone at the end of the
/// chain, a sidecar that holds none of it while every photo of its folder is a frame it hashed, the sequence in the trash,
/// or no sequence of that id anywhere (and none in this index either). Whatever the archive cannot tell keeps the entry,
/// and so does a frame whose file changed since its conversion.
/// </summary>
public sealed partial class SequenceTrash
{
/// <summary>Links a replacement chain may have at most (a conversion of a conversion…), as sync follows them.</summary>
private const int ReplacementSteps = 8;
/// <summary>
/// <paramref name="entry"/> as an emptying plans it: a replaced entry is <see cref="TrashEntry.ConversionDeleted"/> when
/// the replacement of one of its photos is held by no frame of its sequence any more (the user deleted that photo, or the
/// whole sequence), so the dry run says so. Reads the index and the entry's record; hashes nothing.
/// </summary>
private async Task<TrashEntry> PlannedAsync(ArchivePaths paths, TrashEntry entry, CancellationToken cancellationToken)
/// <summary>Where a replaced entry's sequence is, as the archive shows it.</summary>
private enum Whereabouts
{
if (entry.Kind != TrashKind.Replaced || FrameTrash.ReadEntry(paths, entry.Name) is not { } record) return entry;
var links = await LinksAsync(record.SequenceId, cancellationToken).ConfigureAwait(false);
foreach (var photo in record.Photos)
{
if (photo.Replacement is { } replacement && await InSequenceAsync(record.SequenceId, links, replacement, cancellationToken).ConfigureAwait(false) is null)
return entry with { ConversionDeleted = true };
}
return entry;
/// <summary>In a folder of the archive, whose sidecar names it.</summary>
Live,
/// <summary>Deleted: its folder is in the trash.</summary>
Trashed,
/// <summary>Nowhere: deleted and erased.</summary>
Gone,
/// <summary>The archive cannot tell (<see cref="Located.Reason"/>).</summary>
Unknown,
}
/// <summary>What a sidecar shows of a conversion (<see cref="SequenceHoldings.Classify"/>).</summary>
private enum Holding
{
/// <summary>Frames of the sequence hold it, or what replaced it in turn.</summary>
Held,
/// <summary>The sequence no longer holds it: deleted.</summary>
Deleted,
/// <summary>Its frame is there with other content: its file changed since the conversion.</summary>
Changed,
/// <summary>The sidecar does not say.</summary>
Unknown,
}
/// <summary>
/// Whether every photo of a replaced entry may be erased now, checked where its replacement is now. One its sequence holds
/// (followed through later conversions) must hash to it there, re-read now: the originals are never erased while their
/// conversion is damaged. One its sequence no longer holds was deleted, as the confirmed dry run said
/// (<see cref="TrashEntry.ConversionDeleted"/>): a copy the trash still holds (the deleted photos, the originals of a later
/// conversion, the deleted sequence) must hash to it too, and one erased since leaves nothing to keep the originals for.
/// A replaced entry's sequence as found: live (its sidecar), in the trash (the sidecar of each deleted folder of it), gone,
/// or unknown and why.
/// </summary>
private async Task<bool> ReplacementsHoldAsync(Archive archive, ArchivePaths paths, TrashEntry entry, CancellationToken cancellationToken)
private sealed record Located(Whereabouts Where, SequenceHoldings? Live = null, IReadOnlyList<SequenceHoldings>? Trashed = null,
TrashKeptReason Reason = TrashKeptReason.Unknown)
{
if (FrameTrash.ReadEntry(paths, entry.Name) is not { } record) return false;
var links = await LinksAsync(record.SequenceId, cancellationToken).ConfigureAwait(false);
public static Located Unknown(TrashKeptReason reason = TrashKeptReason.Unknown) => new(Whereabouts.Unknown, Reason: reason);
}
/// <summary>
/// What an emptying needs to know of a replaced entry: whether the conversion of one of its photos was deleted (the dry
/// run says so, <see cref="TrashEntry.ConversionDeleted"/>), the files each photo's conversion must still hash to one of
/// (in the sequence, or the copies the trash holds), and why the entry stays when the archive cannot tell, or shows a
/// conversion changed (<see cref="Kept"/>, null when neither).
/// </summary>
private sealed record ConversionSurvey(bool Deleted, IReadOnlyList<IReadOnlyList<(string Path, string Sha256)>> MustMatch, TrashKeptReason? Kept);
/// <summary>
/// The conversions of <paramref name="record"/>'s photos (the entry <paramref name="name"/>), where the archive shows them
/// now: in the sequence as <paramref name="located"/> found it (<see cref="LocateAsync"/>), else among the copies the
/// trash holds. Reads the trash's records and sidecars; hashes nothing.
/// </summary>
private static ConversionSurvey Survey(Archive archive, ArchivePaths paths, string name, PhotoTrashRecord record, Located located,
CancellationToken cancellationToken)
{
if (located.Where == Whereabouts.Unknown) return new ConversionSurvey(false, [], located.Reason);
// Gone with its sequence, deleted or erased since.
var deleted = located.Where != Whereabouts.Live;
var mustMatch = new List<IReadOnlyList<(string Path, string Sha256)>>();
TrashCopies? copies = null;
foreach (var photo in record.Photos)
{
if (photo.Replacement is not { } replacement) return false;
if (await InSequenceAsync(record.SequenceId, links, replacement, cancellationToken).ConfigureAwait(false) is { } held)
if (photo.Replacement is not { } replacement) return new ConversionSurvey(deleted, [], TrashKeptReason.Unknown);
if (located.Live is { } live)
{
if (!await AnyHashesAsync(held.Frames.Select(f => (paths.FromRelative(f.RelativePath), held.Sha256)), cancellationToken).ConfigureAwait(false))
return false;
continue;
var (state, held) = live.Classify(photo.Frame.Id, replacement);
if (KeptFor(state) is { } kept) return new ConversionSurvey(deleted, [], kept);
if (state == Holding.Held)
{
mustMatch.Add(held);
continue;
}
deleted = true;
}
copies ??= Copies(archive, paths, entry.Name, record.SequenceId, links, cancellationToken);
else if (located.Trashed is { Count: > 0 } trashed)
{
// Its frames went with the sequence: a deleted folder's sidecar must say clearly what became of it.
var states = trashed.Select(t => t.Classify(photo.Frame.Id, replacement).State).ToList();
if (!states.Contains(Holding.Held) && states.Select(KeptFor).FirstOrDefault(r => r is not null) is { } kept)
return new ConversionSurvey(deleted, [], kept);
}
// A copy the trash still holds (the deleted photos, the originals of a later conversion, the deleted sequence) must
// match; with none left, nothing keeps the originals.
copies ??= Copies(archive, paths, name, record.SequenceId, located, cancellationToken);
var found = copies.Of(replacement);
if (found.Count > 0 && !await AnyHashesAsync(found, cancellationToken).ConfigureAwait(false)) return false;
if (found.Count > 0) mustMatch.Add(found);
}
return true;
return new ConversionSurvey(deleted, mustMatch, null);
}
/// <summary>The Replaced tombstones of a sequence in the index: each content and what replaced it (lower-case SHA-256).</summary>
private async Task<Dictionary<string, string>> LinksAsync(Guid sequenceId, CancellationToken cancellationToken)
/// <summary>Why an entry stays for what a sidecar shows of one of its conversions; null when that lets it go.</summary>
private static TrashKeptReason? KeptFor(Holding state) => state switch
{
var links = new Dictionary<string, string>(StringComparer.Ordinal);
foreach (var removal in await _db.FrameRemovals.ListBySequenceAsync(sequenceId, cancellationToken).ConfigureAwait(false))
Holding.Changed => TrashKeptReason.Mismatch,
Holding.Unknown => TrashKeptReason.Unknown,
_ => null,
};
/// <summary>
/// Where the sequence of replaced entry <paramref name="entry"/> is, as the archive shows it: in the folder this index has
/// for it, or the one the entry recorded, when its sidecar names it there; else its deleted folders in the trash; else in
/// any folder of the archive whose sidecar names it (another index moved it). Nowhere is gone only when this index has no
/// row of it either: a row the archive does not bear out is a disagreement a scan settles. A sidecar that cannot be read
/// where the sequence may be, or a folder of photos without one there, is unknown. <paramref name="heldEntryLease"/> (an
/// erase, which holds the entry's lease and the sequence's): the live sidecar is read as its writers read it, what a crash
/// left in it settled first (an undo whose completion waited for this entry's lease included), and one still unsettled
/// (another entry busy) keeps the entry as busy.
/// </summary>
private async Task<Located> LocateAsync(Archive archive, ArchivePaths paths, TrashEntry entry, string? heldEntryLease, CancellationToken cancellationToken)
{
var id = entry.SequenceId;
var row = await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false);
if (row is not null && row.ArchiveId != archive.Id) row = null;
foreach (var folder in new[] { row?.Folder, entry.Folder }.OfType<string>().Distinct(StringComparer.OrdinalIgnoreCase))
{
if (removal.Reason == RemovalReason.Replaced && removal.ReplacementSha256 is { Length: > 0 } replacement)
links[removal.Sha256.ToLowerInvariant()] = replacement.ToLowerInvariant();
var at = paths.Sequence(folder);
switch (Holder(at, id))
{
case true:
return Live(at, id, heldEntryLease);
case null:
Log.Warning("archive", $"{entry.Title}: {folder} may hold its sequence but its sidecar cannot be read; its replaced originals stay in the trash");
return Located.Unknown();
}
}
return links;
if (Trashed(paths, id, cancellationToken) is not { } trashed) return Located.Unknown();
if (trashed.Count > 0) return new Located(Whereabouts.Trashed, Trashed: trashed);
foreach (var found in await Task.Run(() => ArchiveScanner.FindSequenceFolders(paths, cancellationToken), cancellationToken).ConfigureAwait(false))
{
if (found.Sidecar is null) continue;
var at = paths.Sequence(found.RelativeFolder);
var named = SidecarId(at.ManifestPath);
if (named == id) return Live(at, id, heldEntryLease);
if (named is null)
{
Log.Warning("archive", $"The sidecar of {found.RelativeFolder} cannot be read; replaced originals of {entry.Title} stay in the trash");
return Located.Unknown();
}
}
if (row is null) return new Located(Whereabouts.Gone);
Log.Info("archive", $"{entry.Title} is in the index but not in the archive; its replaced originals stay in the trash until a scan");
return Located.Unknown();
}
/// <summary>
/// The frames of the sequence that hold <paramref name="replacement"/>, or what replaced it in turn (<paramref name="links"/>),
/// and that content; null when no frame of the sequence holds any of them (it is not in the index, or was deleted).
/// Whether <paramref name="folder"/> holds sequence <paramref name="id"/>: true when its sidecar names it, false when the
/// folder is not there, holds nothing of a sequence, or its sidecar names another one; null when it cannot be told (a
/// sidecar that cannot be read, or photos without one).
/// </summary>
private async Task<(string Sha256, IReadOnlyList<Frame> Frames)?> InSequenceAsync(Guid sequenceId, IReadOnlyDictionary<string, string> links,
string replacement, CancellationToken cancellationToken)
private static bool? Holder(SequencePaths folder, Guid id)
{
var sha = replacement.ToLowerInvariant();
var seen = new HashSet<string>(StringComparer.Ordinal);
for (var step = 0; step <= ReplacementSteps && seen.Add(sha); step++)
if (!Directory.Exists(folder.Folder)) return false;
if (!File.Exists(folder.ManifestPath)) return ArchiveScanner.ListPhotos(folder).Count > 0 ? null : false;
return SidecarId(folder.ManifestPath) is { } named ? named == id : null;
}
/// <summary>
/// The live sequence in <paramref name="folder"/> with its sidecar (<see cref="LocateAsync"/>); busy while a change to it is
/// not settled or a file of it is held open, unknown when its sidecar cannot be read.
/// </summary>
private static Located Live(SequencePaths folder, Guid id, string? heldEntryLease)
{
try
{
var holders = (await _db.Frames.FindBySha256Async(sha, cancellationToken).ConfigureAwait(false)).Where(f => f.SequenceId == sequenceId).ToList();
if (holders.Count > 0) return (sha, holders);
if (!links.TryGetValue(sha, out var next)) break;
sha = next;
SequenceManifest manifest;
if (heldEntryLease is not null)
{
manifest = SequenceWriter.ReadLocked(folder, id, heldEntryLease);
FrameTrash.EnsureSettled(folder);
FrameTrash.EnsureReplaced(folder);
}
else
{
manifest = ManifestStore.ReadSequence(folder.ManifestPath);
if (manifest.Id != id) return Located.Unknown();
}
return new Located(Whereabouts.Live, Live: new SequenceHoldings(folder, manifest));
}
catch (LeaseUnavailableException ex)
{
Log.Info("archive", $"{folder.RelativeFolder} holds a change not settled yet; its replaced originals stay in the trash: {ex.Message}");
return Located.Unknown(TrashKeptReason.Busy);
}
catch (Exception ex) when (ex is InvalidDataException or UnauthorizedAccessException or FileNotFoundException or DirectoryNotFoundException)
{
Log.Warning("archive", $"The sidecar of {folder.RelativeFolder} cannot be read; its replaced originals stay in the trash", ex);
return Located.Unknown();
}
catch (IOException ex)
{
// Open elsewhere (a change being settled could not move a file yet): another try later.
Log.Warning("archive", $"{folder.RelativeFolder} is in use; its replaced originals stay in the trash", ex);
return Located.Unknown(TrashKeptReason.Busy);
}
}
/// <summary>
/// The deleted folders of sequence <paramref name="id"/> in the trash, each with its sidecar; null when one of them has
/// no sidecar that can be read (what it holds of the conversions cannot be told), or an entry's sequence cannot be told.
/// </summary>
private static List<SequenceHoldings>? Trashed(ArchivePaths paths, Guid id, CancellationToken cancellationToken)
{
var found = new List<SequenceHoldings>();
var trash = TrashDirectory(paths);
if (!Directory.Exists(trash)) return found;
foreach (var recordPath in Directory.EnumerateFiles(trash, "*" + RecordExtension))
{
cancellationToken.ThrowIfCancellationRequested();
var folder = Path.Combine(trash, Path.GetFileNameWithoutExtension(recordPath));
if (!Directory.Exists(folder)) continue;
var deleted = paths.Sequence(paths.ToRelative(folder));
var owner = ReadRecord(recordPath)?.Sequence.Id ?? (File.Exists(deleted.ManifestPath) ? SidecarId(deleted.ManifestPath) : null);
if (owner is null)
{
Log.Warning("archive", $"Trash entry {Path.GetFileName(folder)} cannot be told apart; replaced originals of its sequence stay in the trash");
return null;
}
if (owner != id) continue;
try
{
found.Add(new SequenceHoldings(deleted, ManifestStore.ReadSequence(deleted.ManifestPath)));
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
Log.Warning("archive", $"The sidecar of trash entry {Path.GetFileName(folder)} cannot be read; the replaced originals of its photos stay in the trash", ex);
return null;
}
}
return found;
}
/// <summary>Adds the Replaced tombstones of a sidecar to <paramref name="copies"/>: each content and what replaced it.</summary>
private static void AddLinks(TrashCopies copies, SequenceManifest manifest)
{
foreach (var removed in manifest.Removed ?? [])
{
if (removed.Reason == RemovalReason.Replaced && removed.Replacement is { Length: > 0 } replacement) copies.Link(removed.Sha256, replacement);
}
return null;
}
/// <summary>
/// What the trash holds of a sequence's photos, but the entry <paramref name="except"/> (the one being erased): the files of
/// its photo entries (deleted photos, replaced originals) and of its own entry when the whole sequence is in the trash, by
/// content, and the replacements those entries name. Read from their records and sidecars; an unreadable one adds nothing.
/// its photo entries (deleted photos, replaced originals) and of its deleted folders (<paramref name="located"/>), by
/// content, and the replacements those entries and the sequence's sidecar name. Read from their records and sidecars; an
/// unreadable record adds nothing.
/// </summary>
private static TrashCopies Copies(Archive archive, ArchivePaths paths, string except, Guid sequenceId, IReadOnlyDictionary<string, string> links,
CancellationToken cancellationToken)
private static TrashCopies Copies(Archive archive, ArchivePaths paths, string except, Guid sequenceId, Located located, CancellationToken cancellationToken)
{
var copies = new TrashCopies(links);
var copies = new TrashCopies();
if (located.Live is { } live) AddLinks(copies, live.Manifest);
foreach (var listed in FrameTrash.List(archive, cancellationToken).Where(e => e.SequenceId == sequenceId && e.Name != except))
{
if (FrameTrash.ReadEntry(paths, listed.Name) is not { } record) continue;
@@ -110,33 +282,24 @@ public sealed partial class SequenceTrash
if (photo.Frame.Sha256 is { } original && photo.Replacement is { } replacement) copies.Link(original, replacement);
}
}
var trash = TrashDirectory(paths);
if (!Directory.Exists(trash)) return copies;
foreach (var recordPath in Directory.EnumerateFiles(trash, "*" + RecordExtension))
foreach (var deleted in located.Trashed ?? [])
{
cancellationToken.ThrowIfCancellationRequested();
var folder = Path.Combine(trash, Path.GetFileNameWithoutExtension(recordPath));
if (!Directory.Exists(folder) || ReadRecord(recordPath)?.Sequence.Id != sequenceId) continue;
var deleted = paths.Sequence(paths.ToRelative(folder));
SequenceManifest manifest;
try
{
manifest = ManifestStore.ReadSequence(deleted.ManifestPath);
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
Log.Warning("archive", $"The sidecar of trash entry {Path.GetFileName(folder)} cannot be read; its photos are not looked at", ex);
continue;
}
foreach (var frame in manifest.Frames.Where(f => f.Sha256 is not null)) copies.Add(frame.Sha256!, deleted.File(frame.File));
foreach (var removed in manifest.Removed ?? [])
{
if (removed.Reason == RemovalReason.Replaced && removed.Replacement is { } replacement) copies.Link(removed.Sha256, replacement);
}
foreach (var frame in deleted.Manifest.Frames.Where(f => f.Sha256 is not null)) copies.Add(frame.Sha256!, deleted.Folder.File(frame.File));
AddLinks(copies, deleted.Manifest);
}
return copies;
}
/// <summary>Whether, for each photo of a survey, one of the files its conversion must match is there and hashes to it now.</summary>
private static async Task<bool> MatchAsync(ConversionSurvey survey, CancellationToken cancellationToken)
{
foreach (var files in survey.MustMatch)
{
if (!await AnyHashesAsync(files, cancellationToken).ConfigureAwait(false)) return false;
}
return true;
}
/// <summary>Whether one of <paramref name="files"/> is there and hashes to its SHA-256 now.</summary>
private static async Task<bool> AnyHashesAsync(IEnumerable<(string Path, string Sha256)> files, CancellationToken cancellationToken)
{
@@ -148,10 +311,87 @@ public sealed partial class SequenceTrash
return false;
}
/// <summary>Copies of a sequence's photos in the trash by content, and the replacement links known (<see cref="Copies"/>).</summary>
private sealed class TrashCopies(IReadOnlyDictionary<string, string> links)
/// <summary>
/// A sequence's sidecar (live, or of a deleted folder) as a survey reads it, indexed once, so an entry of a conversion of
/// tens of thousands of photos is weighed in one pass over it: its frames by content and by id, its tombstones by content,
/// whether every frame is hashed, and (read when first needed) whether its folder holds a photo the sidecar does not list.
/// </summary>
private sealed class SequenceHoldings
{
private readonly Dictionary<string, string> _links = new(links, StringComparer.Ordinal);
private readonly Dictionary<string, List<SidecarFrame>> _byContent = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<Guid, SidecarFrame> _byId = [];
private readonly Dictionary<string, RemovedPhoto> _tombstones = new(StringComparer.OrdinalIgnoreCase);
private readonly bool _unhashed;
private bool? _unlisted;
public SequenceHoldings(SequencePaths folder, SequenceManifest manifest)
{
Folder = folder;
Manifest = manifest;
foreach (var frame in manifest.Frames)
{
_byId.TryAdd(frame.Id, frame);
if (frame.Sha256 is not { } sha)
{
_unhashed = true;
continue;
}
if (!_byContent.TryGetValue(sha, out var holders)) _byContent[sha] = holders = [];
holders.Add(frame);
}
foreach (var tombstone in manifest.Removed ?? []) _tombstones[tombstone.Sha256] = tombstone;
}
public SequencePaths Folder { get; }
public SequenceManifest Manifest { get; }
/// <summary>
/// What the sidecar shows of <paramref name="replacement"/>, the conversion that took the place of frame
/// <paramref name="frameId"/>'s photo, followed through its Replaced tombstones: the frames holding it, each file with
/// the content it must hash to; changed, when the frame is there with other content; deleted, when a Deleted tombstone
/// ends the chain, or none does and the frame is gone while every photo of the folder is a frame the sidecar hashed;
/// unknown otherwise (the frame there and never hashed again, a frame never hashed or a photo the sidecar does not list
/// that may hold it, a chain too long or looping).
/// </summary>
public (Holding State, List<(string Path, string Sha256)> Files) Classify(Guid frameId, string replacement)
{
var chain = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
RemovedPhoto? end = null;
for (var sha = replacement.ToLowerInvariant(); ;)
{
if (chain.Count > ReplacementSteps || !chain.Add(sha)) return (Holding.Unknown, []);
if (_byContent.TryGetValue(sha, out var holders))
{
var content = sha;
return (Holding.Held, [.. holders.Select(f => (Folder.File(f.File), content))]);
}
if (!_tombstones.TryGetValue(sha, out end) || end.Reason != RemovalReason.Replaced) break;
if (end.Replacement is not { Length: > 0 } next) return (Holding.Unknown, []);
sha = next.ToLowerInvariant();
}
if (_byId.TryGetValue(frameId, out var frame)) return (frame.Sha256 is null ? Holding.Unknown : Holding.Changed, []);
if (end is { Reason: RemovalReason.Deleted }) return (Holding.Deleted, []);
return _unhashed || Unlisted() ? (Holding.Unknown, []) : (Holding.Deleted, []);
}
/// <summary>
/// Whether the folder holds a photo the sidecar does not list, compared as a scan finds the photos it adopts
/// (<see cref="SequenceAdopter.FindOrphans"/>), read once.
/// </summary>
private bool Unlisted()
{
if (_unlisted is { } known) return known;
var listed = Manifest.Frames.Select(f => f.File).ToHashSet(StringComparer.Ordinal);
_unlisted = ArchiveScanner.ListPhotos(Folder).Any(p => !listed.Contains(p));
return _unlisted.Value;
}
}
/// <summary>Copies of a sequence's photos in the trash by content, and the replacement links known (<see cref="Copies"/>).</summary>
private sealed class TrashCopies
{
private readonly Dictionary<string, string> _links = new(StringComparer.Ordinal);
private readonly Dictionary<string, List<string>> _files = new(StringComparer.Ordinal);
public void Add(string sha256, string path)
+4 -3
View File
@@ -74,9 +74,10 @@ public sealed record TrashEntry(Guid ArchiveId, string Name, Guid SequenceId, st
public Guid? JobId { get; init; }
/// <summary>
/// A <see cref="TrashKind.Replaced"/> entry whose conversion was deleted since: a photo's replacement (followed through
/// later conversions) is no longer in its sequence, so erasing the entry may erase the last copy of that photo. Set by the
/// dry run of an emptying (<see cref="SequenceTrash.PlanEmptyAsync"/>), which says so; false in a plain listing.
/// A <see cref="TrashKind.Replaced"/> entry whose conversion was deleted since: the archive shows a photo's replacement
/// (followed through later conversions) deleted from its sequence, or the sequence itself deleted, so erasing the entry may
/// erase the last copy of that photo. Set by the dry run of an emptying (<see cref="SequenceTrash.PlanEmptyAsync"/>), which
/// says so; false in a plain listing.
/// </summary>
public bool ConversionDeleted { get; init; }
}
+42 -18
View File
@@ -280,7 +280,7 @@ internal sealed class SyncTransfers(SyncService sync)
// the converted file that replaced it) is not filed again, which would also drop its tombstone.
File.Delete(staged);
Interlocked.Increment(ref refused);
if (tombstones.DeletedLocally(sha)) context.Log($"{frame.FileName}: deleted on this PC, not downloaded");
if (tombstones.DeletedLocally(sha)) context.Log(Loc.L("Archive.Log.SyncDeletedHere", frame.FileName));
}
else
{
@@ -299,11 +299,13 @@ internal sealed class SyncTransfers(SyncService sync)
if (created) await sync.TakeAllAsync(remote, remoteId, ct).ConfigureAwait(false);
await sync.RecompareAsync(remote, remoteId, ct).ConfigureAwait(false);
// Originals filed into the sequence (not restores, nor frames swapped to another encoding, whose passes are queued as
// they swap) need their thumbnails and light analysis.
if (sequenceId is { } downloaded && wanted.Count > restores.Count + aliases.Count + refused)
{
var label = local!.DisplayTitle;
await EnqueueFollowUpAsync(context, ArchiveJobs.Thumbnails([downloaded], label)).ConfigureAwait(false);
await EnqueueFollowUpAsync(context, ProcessingJobs.LightAnalysis([downloaded], label)).ConfigureAwait(false);
await QueuePassesAsync(context, downloaded, [ArchiveJobs.Thumbnails([downloaded], label), ProcessingJobs.LightAnalysis([downloaded], label)])
.ConfigureAwait(false);
}
if (moved) throw new InvalidOperationException(Loc.L("Errors.Archive.FolderMoved"));
}
@@ -374,24 +376,26 @@ internal sealed class SyncTransfers(SyncService sync)
/// Gives each frame flagged missing here whose photo the server holds only in another encoding (a conversion on either
/// side, owner review 2, R7) the verified copy fetched (<see cref="FrameTrash.ReplaceMissingAsync"/>: under the sequence's
/// lease, in its folder as the index has it then, whose sidecar must be the sequence's, <see cref="SequenceWriter.ReadLocked"/>).
/// Run after the transfers, once <see cref="RestoreAsync"/> found the sequence in that folder. Returns how many took their
/// frame's place; the copy of a frame that changed meanwhile, or whose file came back, is dropped (logged). Moved, with the
/// copies not swapped yet kept in staging for the next attempt, when the sequence left that folder meanwhile.
/// Run after the transfers, once <see cref="RestoreAsync"/> found the sequence in that folder. A swap clears what covered
/// the sequence, as any replacement does, so the passes it had (<see cref="FrameTrash.FollowUps"/>: the light analysis, a
/// background deep analysis, the thumbnails and proxies) are queued after each one, whatever stops the download later.
/// Returns how many took their frame's place; the copy of a frame that changed meanwhile, or whose file came back, is
/// dropped (logged). Moved, with the copies not swapped yet kept in staging for the next attempt, when the sequence left
/// that folder meanwhile.
/// </summary>
private async Task<(int Swapped, bool Moved)> SwapAliasesAsync(JobContext context, Archive archive, Sequence local, ArchivePaths paths,
IEnumerable<AliasCopy> aliases, CancellationToken ct)
{
var swapped = 0;
// What the sequence had before the first swap cleared it: the passes that make it again.
var had = SequenceStatus.None;
foreach (var alias in aliases)
{
ReplaceResult? result;
try
{
if (await sync.Archives.FrameTrash.ReplaceMissingAsync(archive, local.Id, alias.Lost.Id, alias.Lost.Sha256!, alias.Staged, alias.Sha256,
alias.FileName, ct).ConfigureAwait(false))
{
swapped++;
continue;
}
result = await sync.Archives.FrameTrash.ReplaceMissingAsync(archive, local.Id, alias.Lost.Id, alias.Lost.Sha256!, alias.Staged, alias.Sha256,
alias.FileName, ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is InvalidDataException or DirectoryNotFoundException or FileNotFoundException)
{
@@ -400,12 +404,32 @@ internal sealed class SyncTransfers(SyncService sync)
throw;
return (swapped, true);
}
File.Delete(alias.Staged);
context.Log($"{alias.FileName}: its frame here changed meanwhile, not downloaded");
if (result is null)
{
File.Delete(alias.Staged);
context.Log(Loc.L("Archive.Log.SyncFrameChanged", alias.FileName));
continue;
}
swapped++;
had |= result.Had;
await QueuePassesAsync(context, local.Id, sync.Archives.FrameTrash.FollowUps(archive, result.Sequence, had)).ConfigureAwait(false);
}
return (swapped, false);
}
/// <summary>
/// Queues <paramref name="requests"/>, passes over the sequence <paramref name="sequenceId"/>, as follow-ups of this job;
/// one already queued for the sequence (not started yet, so it covers what changed) is left out.
/// </summary>
private async Task QueuePassesAsync(JobContext context, Guid sequenceId, IEnumerable<ArchiveJobRequest> requests)
{
foreach (var request in requests)
{
if (await sync.Database.Jobs.IsQueuedAsync(request.Kind, sequenceId, CancellationToken.None).ConfigureAwait(false)) continue;
await EnqueueFollowUpAsync(context, request).ConfigureAwait(false);
}
}
/// <summary>A verified copy fetched in another encoding of a frame flagged missing here (<see cref="SwapAliasesAsync"/>).</summary>
private sealed record AliasCopy(string Staged, string Sha256, Frame Lost, string FileName);
@@ -578,7 +602,7 @@ internal sealed class SyncTransfers(SyncService sync)
var cached = files.Count(f => f is not null);
var standIns = Enumerable.Range(0, frames.Count).Count(i => kinds[i] != ProxyCache.Proxy && !NeverProxied(frames[i]));
await sync.SetProxiesAsync(remote.Server, dto, cached, standIns, manifest.FramesVersion, ct).ConfigureAwait(false);
if (cached < frames.Count) context.Log($"{frames.Count - cached} frame(s) have neither proxy nor thumbnail on the server");
if (cached < frames.Count) context.Log(Loc.Plural("Archive.Log.SyncNoPreview", frames.Count - cached));
}
/// <summary>
@@ -628,7 +652,7 @@ internal sealed class SyncTransfers(SyncService sync)
{
if (!File.Exists(path))
{
context.Log($"{frame.FileName}: missing, not uploaded");
context.Log(Loc.L("Archive.Log.SyncMissing", frame.FileName));
return null;
}
var sha = await ChronosClient.HashFileAsync(path, cancellationToken).ConfigureAwait(false);
@@ -638,13 +662,13 @@ internal sealed class SyncTransfers(SyncService sync)
hashes[frame.Id] = sha;
if (tombstones.DeletedRemotely(sha))
{
context.Log($"{frame.FileName}: deleted on the server, not uploaded");
context.Log(Loc.L("Archive.Log.SyncDeletedThere", frame.FileName));
return notSent;
}
}
if (frame.Sha256 is { } archived && !archived.Equals(sha, StringComparison.OrdinalIgnoreCase))
{
context.Log($"{frame.FileName}: changed since it was archived, not uploaded");
context.Log(Loc.L("Archive.Log.SyncChanged", frame.FileName));
return null;
}
return await remote.UploadFileAsync(path, sequenceId, sessionKey, sha, frame.CapturedAt, cancellationToken).ConfigureAwait(false) ?? notSent;
@@ -136,8 +136,9 @@ export async function renderTrash(root, { api, setMeta, actions, onEvent }, { ba
const confirmed = await confirmDialog({
title: t("Admin.Trash.EmptyTitle"),
body: [plural("Admin.Trash.EmptyBody", plan.files, formatBytes(plan.bytes)), t("Admin.Trash.EmptyFinal")].join(" "),
// Replaced originals whose conversion was deleted since may be the last copy of their photos: the list says so.
list: plan.entries.map((e) => t("Admin.Trash.PlanItem",
// Replaced originals whose conversion was deleted since may be the last copy of their photos: the list names them
// first, each saying so, wherever the trash lists them.
list: [...plan.entries.filter((e) => e.conversionDeleted), ...plan.entries.filter((e) => !e.conversionDeleted)].map((e) => t("Admin.Trash.PlanItem",
e.conversionDeleted ? `${kindLabel(e.kind)} · ${t("Admin.Trash.ConversionDeleted")}` : kindLabel(e.kind), e.title, formatBytes(e.bytes))),
confirm: t("Admin.Trash.EmptyConfirm"),
confirmTip: t("Admin.Trash.EmptyConfirm.Tip"),