fix(copies): close the eighth verification round of owner review 6
- A merge's Renders stage never deletes what sits at a journaled render target: a file or folder it did not copy there (a render of the kept sequence made after a failed run), or a path another render of the kept sequence names, makes the copy take a new free name instead. - A merge's Verify stage records the hash it read for each kept frame the index had none for, the Details stage leaves the versions the verify filed out of Main, and Two versions also lists a whole-second instant holding two frames of one size and other content (owner Q3). - The Integrate journal keeps the losers the plan listed as last copies; a loser that became the only copy while the job ran is never trashed: the journal goes and the job fails as a stale plan. - Download originals of a server sequence whose earlier download was unlinked or kept apart makes a new local sequence under a key of the job, instead of filling the old one, re-linking it and overwriting its details. - An upload never links again from its checkpoint a server sequence the user kept apart since the attempt that made it. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
123ceba845
commit
aad8ae46dd
11 files changed
+536
-36
No files matched your search
@@ -450,16 +450,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
if (inside && (File.Exists(source) || Directory.Exists(source)))
|
||||
{
|
||||
// Inside the folder that goes to the trash: copied (verified) into the kept sequence's exports first.
|
||||
if (!journal.RenderTargets.TryGetValue(step.RenderId, out var target))
|
||||
{
|
||||
target = keepPaths.ToRelative(Free(keepPaths.Sequence(keep.Folder).Exports, Path.GetFileName(source)));
|
||||
journal.RenderTargets[step.RenderId] = target;
|
||||
Save();
|
||||
}
|
||||
var destination = keepPaths.FromRelative(target);
|
||||
if (Directory.Exists(source)) await CopyFolderAsync(source, destination).ConfigureAwait(false);
|
||||
else await CopyRenderAsync(source, destination, render.Sha256, step.FileName).ConfigureAwait(false);
|
||||
output = target;
|
||||
output = await CopyInAsync(step, render, source, keep).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -482,43 +473,94 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the render <paramref name="source"/> (a file or an image-sequence folder) into the kept sequence's exports
|
||||
/// under the name journaled for it, and returns that archive-relative path. Nothing this job did not copy is ever
|
||||
/// overwritten or deleted: when the journaled name holds something else by now (a render of the kept sequence made
|
||||
/// after a failed run freed the name) or another render of the kept sequence names it, the copy takes a new free name,
|
||||
/// journaled before it is made.
|
||||
/// </summary>
|
||||
private async Task<string> CopyInAsync(RenderStep step, Render render, string source, Sequence keep)
|
||||
{
|
||||
var exports = keepPaths.Sequence(keep.Folder).Exports;
|
||||
var taken = (await _db.Renders.ListBySequenceAsync(journal.KeepId, _ct).ConfigureAwait(false))
|
||||
.Where(r => r.Id != step.RenderId).Select(r => Path.GetFullPath(SyncService.RenderFile(keepArchive, r)))
|
||||
.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
var folder = Directory.Exists(source);
|
||||
if (!journal.RenderTargets.TryGetValue(step.RenderId, out var target)
|
||||
|| taken.Contains(Path.GetFullPath(keepPaths.FromRelative(target)))
|
||||
|| !await OursAsync(source, keepPaths.FromRelative(target), render.Sha256, folder).ConfigureAwait(false))
|
||||
{
|
||||
target = keepPaths.ToRelative(Free(exports, Path.GetFileName(source), taken));
|
||||
journal.RenderTargets[step.RenderId] = target;
|
||||
Save();
|
||||
}
|
||||
var destination = keepPaths.FromRelative(target);
|
||||
if (folder) await CopyFolderAsync(source, destination).ConfigureAwait(false);
|
||||
else await CopyRenderAsync(source, destination, render.Sha256, step.FileName).ConfigureAwait(false);
|
||||
return target;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether <paramref name="destination"/> holds nothing, or only what this job copies there: the render itself (by its
|
||||
/// recorded SHA-256, else by the source's content) or, for a folder, files that each are the source's file of that name.
|
||||
/// VerifiedCopy never leaves a partial file, so a run that stopped midway left whole files or none.
|
||||
/// </summary>
|
||||
private async Task<bool> OursAsync(string source, string destination, string? sha, bool folder)
|
||||
{
|
||||
if (!File.Exists(destination) && !Directory.Exists(destination)) return true;
|
||||
if (!folder)
|
||||
{
|
||||
if (!File.Exists(destination)) return false;
|
||||
var held = await ContentHash.Sha256Async(destination, null, _ct).ConfigureAwait(false);
|
||||
var expected = sha is { Length: > 0 } ? sha : await ContentHash.Sha256Async(source, null, _ct).ConfigureAwait(false);
|
||||
return string.Equals(held, expected, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
if (!Directory.Exists(destination)) return false;
|
||||
foreach (var file in Directory.EnumerateFiles(destination, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
var original = Path.Combine(source, Path.GetRelativePath(destination, file));
|
||||
if (!File.Exists(original) || new FileInfo(original).Length != new FileInfo(file).Length
|
||||
|| !string.Equals(await ContentHash.Sha256Async(file, null, _ct).ConfigureAwait(false),
|
||||
await ContentHash.Sha256Async(original, null, _ct).ConfigureAwait(false), StringComparison.Ordinal))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Copies a render file to <paramref name="destination"/> (<see cref="OursAsync"/> said what is there is this copy).</summary>
|
||||
private async Task CopyRenderAsync(string source, string destination, string? sha, string name)
|
||||
{
|
||||
if (File.Exists(destination))
|
||||
{
|
||||
// A run that copied it before its row moved: kept when it is the same file.
|
||||
if (sha is { Length: > 0 } && string.Equals(await ContentHash.Sha256Async(destination, null, _ct).ConfigureAwait(false), sha, StringComparison.OrdinalIgnoreCase))
|
||||
return;
|
||||
File.Delete(destination);
|
||||
}
|
||||
// A run that copied it before its row moved: the same file, kept.
|
||||
if (File.Exists(destination)) return;
|
||||
var copied = await VerifiedCopy.CopyAsync(source, destination, verify: true, null, _ct).ConfigureAwait(false);
|
||||
if (sha is { Length: > 0 } && !string.Equals(copied, sha, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// This job's own copy, of a render that changed since it was recorded.
|
||||
File.Delete(destination);
|
||||
throw new InvalidOperationException(Loc.L("Errors.Copies.ChangedFile", name));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Copies the files of a render folder that <paramref name="destination"/> lacks (<see cref="OursAsync"/> checked the others).</summary>
|
||||
private async Task CopyFolderAsync(string source, string destination)
|
||||
{
|
||||
foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
var target = Path.Combine(destination, Path.GetRelativePath(source, file));
|
||||
if (File.Exists(target) && new FileInfo(target).Length == new FileInfo(file).Length
|
||||
&& string.Equals(await ContentHash.Sha256Async(target, null, _ct).ConfigureAwait(false),
|
||||
await ContentHash.Sha256Async(file, null, _ct).ConfigureAwait(false), StringComparison.Ordinal))
|
||||
continue;
|
||||
if (File.Exists(target)) File.Delete(target);
|
||||
if (File.Exists(target)) continue;
|
||||
await VerifiedCopy.CopyAsync(file, target, verify: true, null, _ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static string Free(string folder, string name)
|
||||
/// <summary>A name in <paramref name="folder"/> that nothing on disk and no render of <paramref name="taken"/> uses.</summary>
|
||||
private static string Free(string folder, string name, IReadOnlySet<string> taken)
|
||||
{
|
||||
var stem = Path.GetFileNameWithoutExtension(name);
|
||||
var extension = Path.GetExtension(name);
|
||||
var candidate = Path.Combine(folder, name);
|
||||
for (var n = 2; File.Exists(candidate) || Directory.Exists(candidate); n++) candidate = Path.Combine(folder, $"{stem} ({n}){extension}");
|
||||
for (var n = 2; File.Exists(candidate) || Directory.Exists(candidate) || taken.Contains(Path.GetFullPath(candidate)); n++)
|
||||
candidate = Path.Combine(folder, $"{stem} ({n}){extension}");
|
||||
return candidate;
|
||||
}
|
||||
|
||||
@@ -537,14 +579,22 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
var total = keepFrames.Count + dropFrames.Count + flagged.Count;
|
||||
var done = 0;
|
||||
|
||||
// What the kept sequence holds now, hashed again.
|
||||
// What the kept sequence holds now, hashed again. A frame the index had no hash for gets this one (owner Q3): a
|
||||
// photo the merge keeps in two versions is listed under Two versions only once both are hashed.
|
||||
var held = new Dictionary<string, Guid>(StringComparer.Ordinal);
|
||||
var first = new List<HashedFile>();
|
||||
foreach (var frame in keepFrames.Where(f => (f.Flags & CopySides.UnusableFlags) == 0))
|
||||
{
|
||||
_ct.ThrowIfCancellationRequested();
|
||||
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Verify, frame.FileName, ++done, total)).ConfigureAwait(false);
|
||||
if (await HashAsync(keepPaths.FromRelative(frame.RelativePath)).ConfigureAwait(false) is { } sha) held.TryAdd(sha, frame.Id);
|
||||
var path = keepPaths.FromRelative(frame.RelativePath);
|
||||
var stamp = frame.Sha256 is { Length: > 0 } ? null : FileStamp.Of(path);
|
||||
if (await HashAsync(path).ConfigureAwait(false) is not { } sha) continue;
|
||||
held.TryAdd(sha, frame.Id);
|
||||
if (stamp is { } read) first.Add(new HashedFile(frame.Id, sha, read));
|
||||
}
|
||||
if (first.Count > 0 && await _db.Sequences.GetAsync(journal.KeepId, _ct).ConfigureAwait(false) is { } keepRow)
|
||||
await owner.Archives.Verifier.RecordHashesAsync(keepArchive, keepPaths, keepRow, first, _ct).ConfigureAwait(false);
|
||||
|
||||
var keepRemoved = (await _db.FrameRemovals.ListBySequenceAsync(journal.KeepId, _ct).ConfigureAwait(false))
|
||||
.Select(r => r.Sha256.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
|
||||
@@ -786,11 +836,13 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
|
||||
private async Task DetailsAsync()
|
||||
{
|
||||
if (journal.Payload is null || journal.DetailsDone) return;
|
||||
if (journal.DetailsDone) return;
|
||||
var payload = await PayloadAsync().ConfigureAwait(false);
|
||||
if (payload is not { } write) return;
|
||||
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.Details)).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await new SequencePayloadWriter(_db, owner.Archives).ApplyAsync(journal.KeepId, journal.Payload, journal.KeepSettingsVersion, _ct).ConfigureAwait(false);
|
||||
await new SequencePayloadWriter(_db, owner.Archives).ApplyAsync(journal.KeepId, write.Payload, write.SettingsVersion, _ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (SyncConflictChangedException)
|
||||
{
|
||||
@@ -803,6 +855,43 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What the Details stage writes, and at which settings version of the kept sequence: the plan's payload (at the
|
||||
/// version the plan read), with the second versions the verify filed (photos the plan compared by size and time only,
|
||||
/// another photo after all) left out of Main as the plan leaves its own out (owner Q3). Without a planned payload,
|
||||
/// the kept sequence's settings as they are now with those versions left out, at the version read with them. Null
|
||||
/// when nothing is written.
|
||||
/// </summary>
|
||||
private async Task<(SyncPayload Payload, int SettingsVersion)?> PayloadAsync()
|
||||
{
|
||||
var versions = journal.SecondVersions.Select(id => journal.Verified.GetValueOrDefault(id)?.Sha256).OfType<string>()
|
||||
.Select(s => s.ToLowerInvariant()).Distinct(StringComparer.Ordinal).ToList();
|
||||
var planned = journal.Payload is { } p ? (p, journal.KeepSettingsVersion) : ((SyncPayload, int)?)null;
|
||||
if (versions.Count == 0) return planned;
|
||||
SyncPayload basis;
|
||||
int version;
|
||||
if (planned is { } known)
|
||||
{
|
||||
(basis, version) = known;
|
||||
}
|
||||
else
|
||||
{
|
||||
var keep = await _db.Sequences.GetAsync(journal.KeepId, _ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
basis = CopyPayload.Portable(SyncPayload.Of(keep), await SequencePayloadWriter.PhotosAsync(_db, journal.KeepId, null, _ct).ConfigureAwait(false));
|
||||
version = keep.SettingsVersion;
|
||||
}
|
||||
try
|
||||
{
|
||||
var excluded = CopyPayload.ExcludeVersions(basis, versions);
|
||||
return planned is null && excluded.SameAs(basis) ? null : (excluded, version);
|
||||
}
|
||||
catch (Exception ex) when (ex is System.Text.Json.JsonException or InvalidDataException)
|
||||
{
|
||||
// Settings that are not a project of this Chronos: the versions stay included, as the plan leaves its own.
|
||||
return planned;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- projects ----
|
||||
|
||||
private async Task ProjectsAsync()
|
||||
|
||||
Reference in new issue
Block a user