fix(copies): close the sixth verification round of owner review 6
A merge that finds the other changed drops its journal so the retry plans again. Unlink and Keep apart name the local side and leave a link that now joins another sequence alone; Link only and the Integrate job refuse to touch a link or a pair another integration holds. A sweep of 404s asks the server once. To trash... trashes from the kept sequence after a swap and every trash re-checks for new last copies. The Compare dialog and Settings choices fit in every language, Details segments say which value they take, twins need their sibling at the other home, and a linked row keeps three buttons. Captures updated for the dialog and Settings scenes. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bf7701d279
commit
38764972ad
77 files changed
+965
-123
No files matched your search
@@ -300,6 +300,16 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
|
||||
private InvalidOperationException Changed(string title) => new(Loc.L("Errors.Copies.Changed", title));
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Changed"/> for a change that never goes back (a frames version, a photo gone from the kept sequence):
|
||||
/// retrying this journal would fail the same way, so it goes and the next run plans again.
|
||||
/// </summary>
|
||||
private InvalidOperationException Stale(string title)
|
||||
{
|
||||
ForgetJournal();
|
||||
return Changed(title);
|
||||
}
|
||||
|
||||
/// <summary>Asks for this run's journal and staging to go as it fails (<see cref="AbandonAsync"/>): what it planned no longer holds.</summary>
|
||||
private void ForgetJournal() => _forget = true;
|
||||
|
||||
@@ -344,7 +354,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
private async Task EnsureDropUnchangedAsync(int expected)
|
||||
{
|
||||
var drop = await _db.Sequences.GetAsync(journal.DropId, _ct).ConfigureAwait(false);
|
||||
if (drop is null || drop.FramesVersion != expected) throw Changed(_dropTitle);
|
||||
if (drop is null || drop.FramesVersion != expected) throw Stale(_dropTitle);
|
||||
}
|
||||
|
||||
// ---- copy ----
|
||||
@@ -401,7 +411,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
catch (PhotoRemovedException)
|
||||
{
|
||||
File.Delete(staged);
|
||||
if (planned) throw Changed(_keepTitle); // deleted from the kept sequence since the plan: a last copy the plan did not list
|
||||
if (planned) throw Stale(_keepTitle); // deleted from the kept sequence since the plan: a last copy the plan did not list
|
||||
// A photo the plan could not compare by content (unhashed) that the kept sequence deleted: a last copy the
|
||||
// plan did not list. Retrying this journal would fail the same way: it goes, and the user plans again.
|
||||
ForgetJournal();
|
||||
@@ -516,7 +526,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
private async Task VerifyAsync()
|
||||
{
|
||||
var drop = await _db.Sequences.GetAsync(journal.DropId, _ct).ConfigureAwait(false) ?? throw Changed(_dropTitle);
|
||||
if (drop.FramesVersion != journal.DropFramesVersion) throw Changed(_dropTitle);
|
||||
if (drop.FramesVersion != journal.DropFramesVersion) throw Stale(_dropTitle);
|
||||
EnsureDropReadable(drop);
|
||||
await EnsureFolderReadAsync(drop).ConfigureAwait(false);
|
||||
var keepFrames = await ProcessingFrames.ListAsync(_db, journal.KeepId, _ct).ConfigureAwait(false);
|
||||
@@ -920,8 +930,8 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
ForgetJournal();
|
||||
throw Changed(_dropTitle);
|
||||
}
|
||||
var expected = journal.VerifiedDropFramesVersion ?? throw Changed(_dropTitle);
|
||||
if (drop.FramesVersion != expected) throw Changed(_dropTitle);
|
||||
var expected = journal.VerifiedDropFramesVersion ?? throw Stale(_dropTitle);
|
||||
if (drop.FramesVersion != expected) throw Stale(_dropTitle);
|
||||
EnsureDropReadable(drop);
|
||||
if (beforeLinks) await EnsureFolderReadAsync(drop).ConfigureAwait(false);
|
||||
return expected;
|
||||
@@ -998,7 +1008,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
// folder) never lets the rest through unread.
|
||||
var dropFrames = await ProcessingFrames.ListAsync(_db, journal.DropId, _ct).ConfigureAwait(false);
|
||||
if (dropFrames.Any(f => (f.Flags & CopySides.UnusableFlags) == 0 && !journal.Verified.ContainsKey(f.Id) && !journal.Settled.Contains(f.Id)))
|
||||
throw Changed(_dropTitle);
|
||||
throw Stale(_dropTitle);
|
||||
// A frame flagged Missing or Corrupt since the verify, its file there: never hashed as such, so planned again.
|
||||
if (dropFrames.Any(f => (f.Flags & CopySides.UnusableFlags) != 0 && !journal.FlaggedVerified.Contains(f.Id)
|
||||
&& File.Exists(_dropPaths.FromRelative(f.RelativePath))))
|
||||
@@ -1028,7 +1038,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
&& (frame.Sha256 is null || string.Equals(frame.Sha256, verified.Sha256, StringComparison.OrdinalIgnoreCase)))
|
||||
continue;
|
||||
if (hashes.Contains(verified.Sha256)) continue;
|
||||
throw Changed(_keepTitle);
|
||||
throw Stale(_keepTitle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -231,7 +231,7 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
return;
|
||||
case CopyState.Linked or CopyState.Integrated when copy is { Kind: CopyKind.LocalServer, Server: { } server, RemoteId: { } remoteId }:
|
||||
// Kept apart from the server's copy it is linked to: the link goes (the server row stays as an observation).
|
||||
await _sync.UnlinkAsync(server, remoteId, ct).ConfigureAwait(false);
|
||||
await _sync.UnlinkAsync(server, remoteId, copy.LocalId, ct).ConfigureAwait(false);
|
||||
if (await StateAsync(copyId, ct).ConfigureAwait(false) == CopyState.KeptApart) return;
|
||||
break;
|
||||
}
|
||||
@@ -259,6 +259,10 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
throw new ArgumentException($"Pair {copyId} is not a pair with a sequence of {remote.Server}.", nameof(copyId));
|
||||
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
var local = await Db.Sequences.GetAsync(copy.LocalId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
// Another pair of either side being integrated links them its own way: this link waits for that job.
|
||||
if ((await Store.ListByServerAsync(server, ct).ConfigureAwait(false))
|
||||
.Any(c => c is { Kind: CopyKind.LocalServer, State: CopyState.Integrating } && (c.RemoteId == remoteId || c.LocalId == local.Id)))
|
||||
throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
var mine = await _sync.LinkOfAsync(server, local.Id, ct).ConfigureAwait(false);
|
||||
var theirs = await _sync.Links.GetAsync(server, remoteId, ct).ConfigureAwait(false);
|
||||
if ((mine is not null && mine.RemoteId != remoteId) || (theirs?.LocalId is { } holder && holder != local.Id))
|
||||
@@ -289,7 +293,15 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
return;
|
||||
}
|
||||
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
if (copy.State is CopyState.Linked or CopyState.Integrated && remote is not null && string.Equals(remote.Server, server, StringComparison.Ordinal))
|
||||
if (copy.State is not (CopyState.Linked or CopyState.Integrated))
|
||||
{
|
||||
// No link holds this pair (kept apart, or a candidate a re-link demoted): only its own row changes; the server
|
||||
// sequence's link, held by another copy or none, is not this pair's to drop.
|
||||
if (copy.State != CopyState.KeptApart && !await Store.SetStateAsync(copyId, copy.State, CopyState.KeptApart, Answer(copy), ct).ConfigureAwait(false))
|
||||
throw new StaleCopyPlanException();
|
||||
return;
|
||||
}
|
||||
if (remote is not null && string.Equals(remote.Server, server, StringComparison.Ordinal))
|
||||
{
|
||||
// The answer is measured against what the two hold now, not at the last comparison.
|
||||
try
|
||||
@@ -301,10 +313,13 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
Log.Warning(LogCategory, $"Pair {copyId} could not be compared again before its Unlink; its last comparison is kept", ex);
|
||||
}
|
||||
}
|
||||
await _sync.UnlinkAsync(server, remoteId, ct).ConfigureAwait(false);
|
||||
// No link held the pair (a candidate): it is kept apart all the same.
|
||||
if (await StateAsync(copyId, ct).ConfigureAwait(false) is { } state && state != CopyState.KeptApart)
|
||||
await Store.SetStateAsync(copyId, state, CopyState.KeptApart, Answer(copy), ct).ConfigureAwait(false);
|
||||
await _sync.UnlinkAsync(server, remoteId, copy.LocalId, ct).ConfigureAwait(false);
|
||||
// The link had gone meanwhile (another copy holds the server sequence now): this pair is kept apart all the same,
|
||||
// from the state read, never over an integration that began since.
|
||||
var state = await StateAsync(copyId, ct).ConfigureAwait(false);
|
||||
if (state == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
if (state is { } now && now != CopyState.KeptApart && !await Store.SetStateAsync(copyId, now, CopyState.KeptApart, Answer(copy), ct).ConfigureAwait(false))
|
||||
throw new StaleCopyPlanException();
|
||||
}
|
||||
|
||||
public async Task KeepHereAsync(Guid copyId, CancellationToken ct)
|
||||
@@ -352,6 +367,7 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
var row = await Db.Sequences.GetAsync(sequenceId, ct).ConfigureAwait(false);
|
||||
if (row?.FramesVersion != plan.Plan.Sequence.FramesVersion || (plan.SettingsVersion is { } settings && row.SettingsVersion != settings))
|
||||
throw new StaleCopyPlanException();
|
||||
await EnsureNoNewLastCopiesAsync([plan.Sha256], plan.LastCopies, [sequenceId], ct).ConfigureAwait(false);
|
||||
await _archives.FrameTrash.DeleteAsync(plan.Plan, _context(null).PcName, followUps?.Invoke(), ct).ConfigureAwait(false);
|
||||
Log.Info(LogCategory, $"Sequence {sequenceId}: one version of a photo kept in two went to its archive trash");
|
||||
if (plan.Sha256 is { Length: > 0 } dropped) await KeepInMainAsync(sequenceId, plan.PartnerSha256, dropped, plan.SettingsVersion, ct).ConfigureAwait(false);
|
||||
@@ -444,20 +460,56 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
|
||||
// ---------------- Deleted on one side ----------------
|
||||
|
||||
public async Task<TrashHerePlan> PlanTrashHereAsync(Guid copyId, IReadOnlyCollection<string> photoKeys, CancellationToken ct)
|
||||
public async Task<TrashHerePlan> PlanTrashHereAsync(Guid copyId, Guid fromSequence, IReadOnlyCollection<string> photoKeys, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(photoKeys);
|
||||
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
|
||||
var sequence = await Db.Sequences.GetAsync(copy.LocalId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
var here = await _sides.LocalAsync(copy.LocalId, ct).ConfigureAwait(false);
|
||||
_ = OtherOf(copy, fromSequence);
|
||||
var sequence = await Db.Sequences.GetAsync(fromSequence, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
var here = await _sides.LocalAsync(fromSequence, ct).ConfigureAwait(false);
|
||||
var frames = FramesOf(here, SyncTombstones.FromDtos(here.Removed, null), photoKeys);
|
||||
if (frames.Count == 0) throw new InvalidOperationException(Loc.L("Errors.Copies.NotThere"));
|
||||
var plan = await _archives.FrameTrash.PlanDeleteAsync(copy.LocalId, [.. frames.Select(f => f.FrameId)], sequence.FramesVersion, ct).ConfigureAwait(false);
|
||||
var plan = await _archives.FrameTrash.PlanDeleteAsync(fromSequence, [.. frames.Select(f => f.FrameId)], sequence.FramesVersion, ct).ConfigureAwait(false);
|
||||
// The other side deleted these photos: what no other sequence of this index holds goes with its last copy.
|
||||
var lasts = await LastCopiesAsync(frames, CopySide.ThisPc, [copy.LocalId], ct).ConfigureAwait(false);
|
||||
var lasts = await LastCopiesAsync(frames, CopySide.ThisPc, [fromSequence], ct).ConfigureAwait(false);
|
||||
return new TrashHerePlan(plan, lasts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The pair's other local sequence when <paramref name="fromSequence"/> is one of two copies of this PC; null when it is
|
||||
/// the local copy of a server pair. Any other sequence is not this pair's.
|
||||
/// </summary>
|
||||
private static Guid? OtherOf(FoundCopy copy, Guid fromSequence)
|
||||
{
|
||||
if (copy.Kind == CopyKind.LocalLocal && copy.OtherId is { } other)
|
||||
{
|
||||
if (fromSequence == copy.LocalId) return other;
|
||||
if (fromSequence == other) return copy.LocalId;
|
||||
}
|
||||
else if (fromSequence == copy.LocalId)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
throw new ArgumentException($"Sequence {fromSequence} is not a side of pair {copy.Id}.", nameof(fromSequence));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// At the moment of a delete: the photos planned (by content) that no sequence outside <paramref name="except"/> holds
|
||||
/// now must all be among the last copies the plan listed; one that became a last copy since (another sequence trashed
|
||||
/// meanwhile) makes the plan stale, so the next confirmation names it.
|
||||
/// </summary>
|
||||
private async Task EnsureNoNewLastCopiesAsync(IEnumerable<string?> hashes, IReadOnlyList<LastCopy> listed, IReadOnlyCollection<Guid> except, CancellationToken ct)
|
||||
{
|
||||
var keys = hashes.OfType<string>().Where(h => h.Length > 0).Select(h => h.ToLowerInvariant()).Distinct(StringComparer.Ordinal).ToList();
|
||||
if (keys.Count == 0) return;
|
||||
var named = listed.Select(l => l.Key).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
var elsewhere = await _sides.HeldElsewhereAsync(keys, except, ct).ConfigureAwait(false);
|
||||
var now = keys.Count(k => !named.Contains(k) && !elsewhere.Contains(k));
|
||||
if (now == 0) return;
|
||||
Log.Info(LogCategory, $"{now} photo(s) became last copies since the plan; nothing went to a trash");
|
||||
throw new StaleCopyPlanException();
|
||||
}
|
||||
|
||||
public async Task<DropVersionPlan> PlanDropVersionAsync(Guid copyId, string key, ISyncRemote? remote, CancellationToken ct)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(key);
|
||||
@@ -514,6 +566,8 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
Log.Info(LogCategory, $"Pair {plan.CopyId}: the version kept is no longer on both sides; nothing went to a trash");
|
||||
throw new StaleCopyPlanException(Loc.L("Errors.Copies.OtherVersionGone"));
|
||||
}
|
||||
Guid[] pairSides = copy.OtherId is { } o ? [copy.LocalId, o] : [copy.LocalId];
|
||||
await EnsureNoNewLastCopiesAsync(FramesOf(here, tombstones, [plan.Key]).Take(1).Select(f => f.Sha256), plan.LastCopies, pairSides, ct).ConfigureAwait(false);
|
||||
// The local plans are checked before the server moves anything, so a stale one moves nothing anywhere.
|
||||
foreach (var local in new[] { plan.Here, plan.Other }.OfType<FrameDeletePlan>())
|
||||
{
|
||||
@@ -550,15 +604,25 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
return moved;
|
||||
}
|
||||
|
||||
public async Task<int> TrashHereAsync(Guid copyId, FrameDeletePlan plan, ISyncRemote? remote, CancellationToken ct)
|
||||
public async Task<int> TrashHereAsync(Guid copyId, TrashHerePlan trash, ISyncRemote? remote, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(plan);
|
||||
ArgumentNullException.ThrowIfNull(trash);
|
||||
var plan = trash.Plan;
|
||||
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
|
||||
if (plan.Sequence.Id != copy.LocalId) throw new ArgumentException($"The plan is not of pair {copyId}'s sequence.", nameof(plan));
|
||||
var from = plan.Sequence.Id;
|
||||
Guid? otherLocal;
|
||||
try
|
||||
{
|
||||
otherLocal = OtherOf(copy, from);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
throw new ArgumentException($"The plan is not of pair {copyId}'s sequence.", nameof(trash), ex);
|
||||
}
|
||||
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
// The other side as it is now: each photo must still be deleted there, and not held there again (restored from its trash).
|
||||
CopySideInput there;
|
||||
if (copy.Kind == CopyKind.LocalServer)
|
||||
if (otherLocal is null)
|
||||
{
|
||||
if (remote is null || !string.Equals(remote.Server, copy.Server, StringComparison.Ordinal))
|
||||
throw new ArgumentException("A pair with a server sequence is checked over that server's session.", nameof(remote));
|
||||
@@ -566,9 +630,9 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
}
|
||||
else
|
||||
{
|
||||
there = await _sides.LocalAsync(copy.OtherId!.Value, ct).ConfigureAwait(false);
|
||||
there = await _sides.LocalAsync(otherLocal.Value, ct).ConfigureAwait(false);
|
||||
}
|
||||
var here = await _sides.LocalAsync(copy.LocalId, ct).ConfigureAwait(false);
|
||||
var here = await _sides.LocalAsync(from, ct).ConfigureAwait(false);
|
||||
var tombstones = SyncTombstones.FromDtos(here.Removed, there.Removed);
|
||||
var held = Keys(there, tombstones);
|
||||
var refused = plan.Frames.Count(f => !tombstones.DeletedRemotely(f.Sha256) || held.Contains(tombstones.Canonical(f.Sha256) ?? string.Empty));
|
||||
@@ -577,6 +641,7 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
Log.Info(LogCategory, $"Pair {copyId}: {refused} photo(s) are no longer deleted on the other side; none went to the trash");
|
||||
throw new StaleCopyPlanException(Loc.L("Errors.Copies.NotThere"));
|
||||
}
|
||||
await EnsureNoNewLastCopiesAsync(plan.Frames.Select(f => f.Sha256), trash.LastCopies, [from], ct).ConfigureAwait(false);
|
||||
var result = await _archives.FrameTrash.DeleteAsync(plan, _context(remote).PcName, followUps?.Invoke(), ct).ConfigureAwait(false);
|
||||
Log.Info(LogCategory, $"{plan.Frames.Count} photo(s) of {result.Sequence.DisplayTitle} deleted on the other side went to the trash");
|
||||
await RecountAsync(copy, remote, ct).ConfigureAwait(false);
|
||||
@@ -622,6 +687,8 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
Log.Info(LogCategory, $"Pair {plan.CopyId}: {refused} photo(s) are no longer deleted on this PC; none went to the server's trash");
|
||||
throw new StaleCopyPlanException(Loc.L("Errors.Copies.NotThere"));
|
||||
}
|
||||
await EnsureNoNewLastCopiesAsync(FramesOf(there, tombstones, plan.Keys).Where(f => plan.FrameIds.Contains(f.FrameId)).Select(f => f.Sha256),
|
||||
plan.LastCopies, [], ct).ConfigureAwait(false);
|
||||
var done = await ServerAsync(() => remote.DeleteFramesAsync(remoteId, new FrameDeleteRequest(plan.FrameIds, plan.FramesVersion, DryRun: false), ct))
|
||||
.ConfigureAwait(false);
|
||||
Log.Info(LogCategory, $"{done.Photos} photo(s) of server sequence {remoteId} deleted on this PC went to the server's trash");
|
||||
@@ -643,12 +710,14 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
var twins = new List<CopyTwin>();
|
||||
foreach (var pair in pairs)
|
||||
{
|
||||
var here = projects.FirstOrDefault(p => p.Home.IsArchive && p.Home.SequenceId == pair.LocalId).Name;
|
||||
var there = projects.FirstOrDefault(p => !p.Home.IsArchive && p.Home.SequenceId == pair.RemoteId && identity.Same(p.Home.Server, pair.Server)).Name;
|
||||
if (here is null && there is null) continue;
|
||||
// Every project of each home: the picker marks only those whose sibling the other home holds.
|
||||
List<string> here = [.. projects.Where(p => p.Home.IsArchive && p.Home.SequenceId == pair.LocalId).Select(p => p.Name)];
|
||||
List<string> there = [.. projects.Where(p => !p.Home.IsArchive && p.Home.SequenceId == pair.RemoteId && identity.Same(p.Home.Server, pair.Server))
|
||||
.Select(p => p.Name)];
|
||||
if (here.Count == 0 || there.Count == 0) continue;
|
||||
var local = await Db.Sequences.GetAsync(pair.LocalId, ct).ConfigureAwait(false);
|
||||
twins.Add(new CopyTwin(pair.Id, pair.LocalId, local is null ? string.Empty : archives.GetValueOrDefault(local.ArchiveId) ?? string.Empty, here, pair.Server!,
|
||||
pair.RemoteId!.Value, there, ServerName(pair.Server!)));
|
||||
pair.RemoteId!.Value, there, ServerName(pair.Server!), _context(null).PcName));
|
||||
}
|
||||
return twins;
|
||||
}
|
||||
|
||||
@@ -96,6 +96,7 @@ public sealed class CopyReviewBuilder(ChronosDatabase db, ProjectService project
|
||||
return new CopyReview(copy, hereFacts, thereFacts, photos, details, projectsView, renders, can)
|
||||
{
|
||||
ExcludedHere = CopyPayload.ExcludedKeys(CopyPayload.Portable(herePayload, herePhotos)),
|
||||
ExcludedThere = CopyPayload.ExcludedKeys(CopyPayload.Portable(therePayload, therePhotos)),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,9 @@ public sealed record CopyReview(FoundCopy Copy, CopyFacts Here, CopyFacts There,
|
||||
{
|
||||
/// <summary>The photos (by key) this PC's Main leaves out: of a photo kept in two versions, the one Main does not use.</summary>
|
||||
public IReadOnlySet<string> ExcludedHere { get; init; } = new HashSet<string>(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>The photos (by key) the other side's Main leaves out (a merge keeping the pair's other sequence marks its Main's versions).</summary>
|
||||
public IReadOnlySet<string> ExcludedThere { get; init; } = new HashSet<string>(StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>Whether Integrate (or Merge), Link only and Delete on server are possible, and why not (<see cref="CopyBlock"/>s).</summary>
|
||||
@@ -40,9 +43,29 @@ public sealed record CopyCapabilities(bool Integrate, bool LinkOnly, bool Delete
|
||||
public sealed record FoundCopyView(FoundCopy Copy, CopyFacts Here, CopyFacts There);
|
||||
|
||||
/// <summary>
|
||||
/// The same time-lapse with workspace projects at two homes (a linked or integrated pair): the Edit picker marks each as the
|
||||
/// other's twin. LocalHome names the local copy's archive; a project name is null when that home has no workspace project.
|
||||
/// ServerName is the name shown for Server (the key itself when no name is known).
|
||||
/// The same time-lapse with workspace projects at two homes (a linked or integrated pair): the Edit picker marks a project
|
||||
/// as a twin only when the other home holds its sibling (<see cref="CounterpartOf"/>). LocalHome names the local copy's
|
||||
/// archive; LocalProjects and ServerProjects are every workspace project name of each home (empty when it has none).
|
||||
/// ServerName is the name shown for Server (the key itself when no name is known); PcName is this PC's, which a project
|
||||
/// copied across to the server is suffixed with on a clash.
|
||||
/// </summary>
|
||||
public sealed record CopyTwin(Guid CopyId, Guid LocalId, string LocalHome, string? LocalProject, string Server, Guid RemoteId, string? ServerProject,
|
||||
string ServerName);
|
||||
public sealed record CopyTwin(Guid CopyId, Guid LocalId, string LocalHome, IReadOnlyList<string> LocalProjects, string Server, Guid RemoteId,
|
||||
IReadOnlyList<string> ServerProjects, string ServerName, string PcName)
|
||||
{
|
||||
/// <summary>
|
||||
/// The project of the other home that is the sibling of <paramref name="name"/> (a project at the server home when
|
||||
/// <paramref name="atServerHome"/>): the same name, or the name Copy across gives it on a clash ("Vertical (STUDIO-PC)",
|
||||
/// "Vertical (Studio NAS)"), either way round, ignoring case. Null when the other home has none.
|
||||
/// <paramref name="serverName"/>: another name the server is shown by (the signed-in server's own).
|
||||
/// </summary>
|
||||
public string? CounterpartOf(string name, bool atServerHome, string? serverName = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(name);
|
||||
var suffixes = new[] { PcName, ServerName, serverName }.Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => $" ({s!.Trim()})").ToList();
|
||||
var others = atServerHome ? LocalProjects : ServerProjects;
|
||||
return others.FirstOrDefault(o => Same(o, name))
|
||||
?? others.FirstOrDefault(o => suffixes.Any(s => Same(o, name + s) || Same(name, o + s)));
|
||||
|
||||
static bool Same(string a, string b) => string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
@@ -44,11 +44,18 @@ public interface ICopyEngine
|
||||
|
||||
Task UnlinkAsync(Guid copyId, ISyncRemote? remote, CancellationToken ct);
|
||||
|
||||
/// <summary>The dry run of moving photos of the local copy (by photo key) to its archive trash, with the last copies among them.</summary>
|
||||
Task<TrashHerePlan> PlanTrashHereAsync(Guid copyId, IReadOnlyCollection<string> photoKeys, CancellationToken ct);
|
||||
/// <summary>
|
||||
/// The dry run of moving photos (by photo key) of <paramref name="fromSequence"/> to its archive trash, with the last copies
|
||||
/// among them: the pair's local copy, or for two copies of this PC either one (a merge trashes from the kept one); the
|
||||
/// other side is the pair's other sequence.
|
||||
/// </summary>
|
||||
Task<TrashHerePlan> PlanTrashHereAsync(Guid copyId, Guid fromSequence, IReadOnlyCollection<string> photoKeys, CancellationToken ct);
|
||||
|
||||
/// <summary>Applies <paramref name="plan"/>, refusing photos no longer deleted on the other side; returns the photos moved.</summary>
|
||||
Task<int> TrashHereAsync(Guid copyId, FrameDeletePlan plan, ISyncRemote? remote, CancellationToken ct);
|
||||
/// <summary>
|
||||
/// Applies <paramref name="plan"/>, refusing photos no longer deleted on the other side (or held there again) and a plan
|
||||
/// whose photos became last copies it did not list; returns the photos moved.
|
||||
/// </summary>
|
||||
Task<int> TrashHereAsync(Guid copyId, TrashHerePlan plan, ISyncRemote? remote, CancellationToken ct);
|
||||
|
||||
/// <summary>The server's dry run of moving photos of the server copy (by photo key) to the server's trash.</summary>
|
||||
Task<TrashTherePlan> PlanTrashThereAsync(Guid copyId, ISyncRemote remote, IReadOnlyCollection<string> photoKeys, CancellationToken ct);
|
||||
|
||||
@@ -227,8 +227,14 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
|
||||
{
|
||||
case IntegrateStage.Link: await LinkAsync().ConfigureAwait(false); break;
|
||||
case IntegrateStage.Photos: await PhotosAsync().ConfigureAwait(false); break;
|
||||
case IntegrateStage.Trash: await TrashAsync().ConfigureAwait(false); break;
|
||||
case IntegrateStage.Details: await DetailsAsync().ConfigureAwait(false); break;
|
||||
case IntegrateStage.Trash:
|
||||
await EnsureStillOursAsync().ConfigureAwait(false);
|
||||
await TrashAsync().ConfigureAwait(false);
|
||||
break;
|
||||
case IntegrateStage.Details:
|
||||
await EnsureStillOursAsync().ConfigureAwait(false);
|
||||
await DetailsAsync().ConfigureAwait(false);
|
||||
break;
|
||||
case IntegrateStage.Projects: await ProjectsAsync().ConfigureAwait(false); break;
|
||||
}
|
||||
journal.Stage++;
|
||||
@@ -244,10 +250,37 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
|
||||
{
|
||||
var link = await owner.Sync.Links.GetAsync(journal.Server, journal.RemoteId, _ct).ConfigureAwait(false);
|
||||
if (link?.LocalId == journal.LocalId) return;
|
||||
// The user gave either side another link since the journal was written: that newer link is never undone; the
|
||||
// journal goes and a retry plans again (and shows the link taken).
|
||||
var mine = await owner.Sync.LinkOfAsync(journal.Server, journal.LocalId, _ct).ConfigureAwait(false);
|
||||
if (link?.LocalId is not null || (mine is not null && mine.RemoteId != journal.RemoteId)) Stale();
|
||||
var local = await _db.Sequences.GetAsync(journal.LocalId, _ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
await owner.Sync.LinkAsync(journal.Server, journal.RemoteId, local, _ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>The journal no longer holds: it goes, and <see cref="StaleCopyPlanException"/> is thrown.</summary>
|
||||
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
|
||||
private void Stale()
|
||||
{
|
||||
try
|
||||
{
|
||||
IntegrateJournal.Delete(paths, journal.JobId);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("sync", $"The stale journal of Integrate {journal.JobId} was not removed", ex);
|
||||
}
|
||||
throw new StaleCopyPlanException();
|
||||
}
|
||||
|
||||
/// <summary>Before a stage that trashes or writes: the pair is still this job's and the link still joins its two sides.</summary>
|
||||
private async Task EnsureStillOursAsync()
|
||||
{
|
||||
var copy = await owner.Copies.GetAsync(journal.CopyId, _ct).ConfigureAwait(false);
|
||||
var link = await owner.Sync.Links.GetAsync(journal.Server, journal.RemoteId, _ct).ConfigureAwait(false);
|
||||
if (copy is not { State: CopyState.Integrating } || copy.JobId != journal.JobId || link?.LocalId != journal.LocalId) Stale();
|
||||
}
|
||||
|
||||
// ---- photos ----
|
||||
|
||||
private async Task PhotosAsync()
|
||||
|
||||
@@ -186,7 +186,7 @@ public sealed class SyncService
|
||||
var sweep = gone.Count(l => l.LocalId is not null) >= SweepLinks && found == 0;
|
||||
if (sweep) Log.Warning(LogCategory, $"Every linked sequence of {remote.Server} answered 404 at once ({gone.Count}): none is marked deleted on the server");
|
||||
foreach (var link in gone) await ForgetAsync(link, sweep ? null : refreshId, cancellationToken).ConfigureAwait(false);
|
||||
await AskGoneAgainAsync(remote, listing, refreshId, cancellationToken).ConfigureAwait(false);
|
||||
await AskGoneAgainAsync(remote, listing, refreshId, confirm: !sweep, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -250,20 +250,24 @@ public sealed class SyncService
|
||||
/// <summary>
|
||||
/// Unlinks a server sequence from its local copy (owner review 6, #73): the link stays as an observation of the server's
|
||||
/// sequence, the local copy reads as this PC's only, and the pair is kept apart (asked again only by a large later change).
|
||||
/// Nothing happens when the server sequence has no local copy.
|
||||
/// Nothing happens when the server sequence is not linked to <paramref name="localId"/> (no local copy, or another one:
|
||||
/// that pair's link and state are not this one's to change). A pair being integrated is refused.
|
||||
/// </summary>
|
||||
public async Task UnlinkAsync(string server, Guid remoteId, CancellationToken cancellationToken = default)
|
||||
public async Task UnlinkAsync(string server, Guid remoteId, Guid localId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (await _links.GetAsync(server, remoteId, cancellationToken).ConfigureAwait(false) is not { LocalId: { } localId } link) return;
|
||||
if (await _links.GetAsync(server, remoteId, cancellationToken).ConfigureAwait(false) is not { LocalId: { } linked } link || linked != localId) return;
|
||||
var row = await Copies.FindRemoteAsync(localId, server, remoteId, cancellationToken).ConfigureAwait(false);
|
||||
if (row is { State: CopyState.Integrating }) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
await _links.UpsertAsync(Unlinked(link), cancellationToken).ConfigureAwait(false);
|
||||
await _links.SetLocalStateAsync(localId, SyncState.LocalOnly, cancellationToken).ConfigureAwait(false);
|
||||
var row = await Copies.FindRemoteAsync(localId, server, remoteId, cancellationToken).ConfigureAwait(false)
|
||||
?? await Copies.EnsureLinkedAsync(server, remoteId, localId, cancellationToken).ConfigureAwait(false);
|
||||
await Copies.SetStateAsync(row.Id, null, CopyState.KeptApart, new CopyAnswer(row.Fingerprint, row.Score.Shared, null, false, DateTimeOffset.UtcNow),
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
row ??= await Copies.EnsureLinkedAsync(server, remoteId, localId, cancellationToken).ConfigureAwait(false);
|
||||
// From the state read: a pair that entered an integration since is left to it.
|
||||
if (!await Copies.SetStateAsync(row.Id, row.State, CopyState.KeptApart,
|
||||
new CopyAnswer(row.Fingerprint, row.Score.Shared, null, false, DateTimeOffset.UtcNow), cancellationToken).ConfigureAwait(false))
|
||||
Log.Warning(LogCategory, $"{link.Title} unlinked; its pair changed meanwhile and keeps its state");
|
||||
Log.Info(LogCategory, $"{link.Title} unlinked from its copy on this PC");
|
||||
}
|
||||
finally
|
||||
@@ -794,7 +798,7 @@ public sealed class SyncService
|
||||
/// pair read "deleted on server". A refresh's first 404 forgot the link (the pair is asked about again); a job's 404
|
||||
/// kept it (the pair stays Linked or Integrated).
|
||||
/// </summary>
|
||||
private async Task AskGoneAgainAsync(ISyncRemote remote, IReadOnlyList<SequenceDto> listing, Guid refreshId, CancellationToken cancellationToken)
|
||||
private async Task AskGoneAgainAsync(ISyncRemote remote, IReadOnlyList<SequenceDto> listing, Guid refreshId, bool confirm, CancellationToken cancellationToken)
|
||||
{
|
||||
var listed = listing.Select(d => d.Id).ToHashSet();
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
@@ -806,7 +810,8 @@ public sealed class SyncService
|
||||
await BackAsync(pair, remoteId).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
if (pair.RemoteGoneAt is { } at && now - at < GoneRecheck) continue;
|
||||
// A sweep of 404s (the server serves something else now) confirms no earlier hit either.
|
||||
if (!confirm || (pair.RemoteGoneAt is { } at && now - at < GoneRecheck)) continue;
|
||||
try
|
||||
{
|
||||
await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
Reference in new issue
Block a user