E5: «Riconosci» va avanti su un file illeggibile e non tocca nulla

Un file della cartella che non si legge (aperto da un altro programma,
senza permessi) rende «Non riuscito» solo il suo elemento: prima fermava
tutto il lavoro, e «Riprendi» si fermava sempre lì. «Annulla» e il
riavvio non tolgono più i .part nelle cartelle di un «Riconosci», come
dice la specifica. Una riga orfana «Superato» ricollegata diventa
«Copiato», come dice l'elemento; una riga orfana «Tolto» resta «Tolto»
anche per il file, come già per i fotogrammi (risposta 51). I nomi
numerati si guardano tutti fino a «nome (50)», anche dopo un buco. I
byte della libreria di un «Riconosci», che non scrive, non contano più
per la prova della cartella: una prova per gruppo di file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-04 19:51:12 +01:00
1 parent 309f4d0402
commit 11e9dd276c
7 files changed
+317 -26

No files matched your search

@@ -573,6 +573,190 @@ public sealed class ExportRecognizeTests
Assert.IsFalse((await bed.Db.Exports.ListRunsAsync(ct)).Any(r => r.Kind == ExportRunKind.Recognize), "a refused start records nothing");
}
/// <summary>
/// A file of the destination that cannot be read (held open by another program, no permission) does not stop the run:
/// its item is «Non riuscito» with the error and no copy row, and the next file is recognised.
/// </summary>
[TestMethod]
public async Task AnUnreadableFileInTheFolderFailsItsItemAndTheRunGoesOn()
{
var ct = TestContext.CancellationToken;
UnreadableExportFileSystem? files = null;
await using var bed = await ExportTestBed.CreateAsync(env => env with { FileSystem = files = new UnreadableExportFileSystem(p => p.Contains("usb", StringComparison.Ordinal) && p.Contains("_1.jpg", StringComparison.Ordinal) && p.Contains("u01", StringComparison.Ordinal)) });
var locked = await bed.PostAsync(bed.Marta, "u01", 21);
var other = await bed.PostAsync(bed.Marta, "u02", 22);
var usb = await bed.GeneralAsync(ct: ct);
PutCopy(bed, bed.Marta, locked.Media[0]);
PutCopy(bed, bed.Marta, other.Media[0]);
var run = await RecognizeAsync(bed, usb, ct);
Assert.AreEqual(ExportRunState.Completed, run.State);
Assert.IsGreaterThan(0, files!.Refused);
var items = (await bed.ItemsAsync(run.Id, ct)).ToDictionary(i => i.MediaId!.Value);
Assert.AreEqual(ExportItemState.Failed, items[locked.Media[0].Id].State);
StringAssert.StartsWith(items[locked.Media[0].Id].LastError, "Errore di lettura o scrittura");
Assert.AreEqual(ExportItemState.Adopted, items[other.Media[0].Id].State);
Assert.AreEqual(other.Media[0].Id, (await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct)).Single().MediaId, "no row for the file that could not be read");
}
/// <summary>
/// «Annulla» and a restart leave the <c>.part</c> files in the folders of a «Riconosci» alone (§4.10: it removes no
/// file, not even the <c>.part</c> of others); a copy job of the same folders cleans its own.
/// </summary>
[TestMethod]
public async Task DiscardAndRestartLeaveThePartFilesOfARecognizeRun()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
var post = await bed.PostAsync(bed.Marta, "p01", 23);
var usb = await bed.GeneralAsync(ct: ct);
var folder = Path.Combine(bed.Usb, "marta.demo", "altrove");
Directory.CreateDirectory(folder);
async Task<(ExportRun Run, string Stray)> UnfinishedAsync(ExportRunKind kind, string name)
{
var run = await bed.Db.Exports.CreateRunAsync(new ExportRun { DestinationId = usb.Id, Kind = kind, State = ExportRunState.Interrupted, StartedAt = bed.Clock.Now }, ct);
await bed.Db.Exports.AddItemsAsync([new ExportItem { RunId = run.Id, MediaId = post.Media[0].Id, AccountId = bed.Marta.Id, SourceSha256 = post.Media[0].Sha256, RelativePath = "marta.demo/altrove/" + name + ".jpg" }], ct);
var stray = Path.Combine(folder, SafeFileWriter.TempPrefix + name + ".part");
await File.WriteAllBytesAsync(stray, [1], ct);
return (run, stray);
}
var discarded = await UnfinishedAsync(ExportRunKind.Recognize, "annullato");
await bed.Service.DiscardAsync(discarded.Run.Id, ct);
Assert.AreEqual(ExportRunState.Cancelled, (await bed.RunAsync(discarded.Run.Id, ct)).State);
Assert.IsTrue(File.Exists(discarded.Stray), "«Annulla» of a «Riconosci» removes nothing");
var restarted = await UnfinishedAsync(ExportRunKind.Recognize, "riavvio");
await bed.Service.RecoverAsync(ct);
await bed.Service.WaitAsync(ct);
Assert.IsTrue(File.Exists(restarted.Stray), "a restart removes nothing of a «Riconosci»");
// Control: «Annulla» of a copy job removes the .part files in its items' folders.
var copy = await UnfinishedAsync(ExportRunKind.Copy, "copia");
await bed.Service.DiscardAsync(copy.Run.Id, ct);
Assert.IsFalse(File.Exists(copy.Stray));
}
/// <summary>
/// An orphan «Superato» row (its account removed) whose bytes are at its path: «Riconosci» links it to the account
/// added again and the row is «Copiato», as its item says.
/// </summary>
[TestMethod]
public async Task AnOrphanSupersededRowIsRelinkedAsCopied()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
await bed.PostAsync(bed.Marta, "o01", 24);
var usb = await bed.GeneralAsync(ct: ct);
var first = await bed.CopyAccountAsync(bed.Marta, ct: ct);
var copyId = (await bed.ItemsAsync(first.Runs.Single().Id, ct)).Single().CopyId!.Value;
var copy = (await bed.Db.Exports.GetCopyAsync(copyId, ct))!;
await bed.Db.Exports.UpdateCopyAsync(copy with { State = ExportCopyState.Superseded, SupersededBySha256 = Make.Sha(779), StateChangedAt = bed.Clock.Now }, ct);
await bed.Db.Accounts.DeleteAsync(bed.Marta.Id, ct);
Directory.Delete(bed.Library.Layout.AccountFolder(bed.Marta), recursive: true);
var again = await bed.Db.Accounts.AddAsync(Make.Account("marta.demo", "999"), ct);
await bed.PostAsync(again, "o01", 24);
var run = await RecognizeAsync(bed, usb, ct);
var item = (await bed.ItemsAsync(run.Id, ct)).Single();
Assert.AreEqual((ExportItemState.Adopted, (long?)copyId), (item.State, item.CopyId));
var row = (await bed.Db.Exports.GetCopyAsync(copyId, ct))!;
Assert.AreEqual((again.Id, ExportCopyState.Copied, (string?)null), (row.AccountId, row.State, row.SupersededBySha256));
Assert.AreEqual(0, (await bed.Service.PlanAsync(new ExportJobRequest(new ExportSelection.Account(again.Id)), ct)).Files, "it counts as copied");
}
/// <summary>
/// An orphan «Tolto» row stops the file from being recognised for the account added again, as it does for a frame
/// (answer 51: a «Tolto» stays «Tolto»).
/// </summary>
[TestMethod]
public async Task AnOrphanRemovedFileStaysRemoved()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
var post = await bed.PostAsync(bed.Marta, "o02", 25);
var usb = await bed.GeneralAsync(ct: ct);
var first = await bed.CopyAccountAsync(bed.Marta, ct: ct);
var copyId = (await bed.ItemsAsync(first.Runs.Single().Id, ct)).Single().CopyId!.Value;
var file = bed.GeneralCopy(bed.Marta, post.Media[0]);
File.Move(file, file + ".via");
await bed.Service.CheckCopiesAsync(usb.Id, ct);
await bed.Service.WaitAsync(ct);
Assert.AreEqual(ExportCopyState.Removed, (await bed.Db.Exports.GetCopyAsync(copyId, ct))!.State);
await bed.Db.Accounts.DeleteAsync(bed.Marta.Id, ct);
Directory.Delete(bed.Library.Layout.AccountFolder(bed.Marta), recursive: true);
var again = await bed.Db.Accounts.AddAsync(Make.Account("marta.demo", "999"), ct);
await bed.PostAsync(again, "o02", 25);
File.Move(file + ".via", file); // back, byte for byte
var run = await RecognizeAsync(bed, usb, ct);
Assert.IsEmpty(await bed.ItemsAsync(run.Id, ct), "not even looked for");
var row = (await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct)).Single();
Assert.AreEqual((copyId, ExportCopyState.Removed, (long?)null), (row.Id, row.State, row.AccountId));
}
/// <summary>
/// The numbered names are all looked at up to «nome (50)»: a copy at «nome (3)» is found even when «nome (2)» is not
/// there (a copy deleted in the middle).
/// </summary>
[TestMethod]
public async Task ANumberedSiblingAfterAGapIsAdopted()
{
var ct = TestContext.CancellationToken;
await using var bed = await ExportTestBed.CreateAsync();
var post = await bed.PostAsync(bed.Marta, "g01", 26);
var media = post.Media[0];
var usb = await bed.GeneralAsync(ct: ct);
var planned = "marta.demo/" + media.RelativePath;
var taken = Full(bed, planned);
Directory.CreateDirectory(Path.GetDirectoryName(taken)!);
await File.WriteAllBytesAsync(taken, [1, 2, 3], ct);
var third = ExportPaths.NumberedName(bed.Usb, planned, 3);
File.Copy(bed.LibraryFile(bed.Marta, media), Full(bed, third));
var run = await RecognizeAsync(bed, usb, ct);
var item = (await bed.ItemsAsync(run.Id, ct)).Single();
Assert.AreEqual((ExportItemState.Adopted, third), (item.State, item.RelativePath));
}
/// <summary>
/// A «Riconosci» checks the folder once per group of files, not at every gigabyte of library files it only looked
/// for: it writes nothing, so its bytes do not count toward the next probe; a copy job's do.
/// </summary>
[TestMethod]
public async Task ARecognizeRunProbesOncePerGroupOfFiles()
{
var ct = TestContext.CancellationToken;
var probes = 0;
await using var bed = await ExportTestBed.CreateAsync(env => env with
{
GroupBytes = 1,
WriteProbe = path =>
{
Interlocked.Increment(ref probes);
File.WriteAllBytes(path, "Febe"u8.ToArray());
},
});
await bed.PostAsync(bed.Marta, "q01", 27, 28, 29);
var usb = await bed.GeneralAsync(ct: ct);
var before = Volatile.Read(ref probes);
var run = await RecognizeAsync(bed, usb, ct);
Assert.HasCount(3, await bed.ItemsAsync(run.Id, ct));
Assert.AreEqual(before + 1, Volatile.Read(ref probes), "one probe for the group of three files");
// Control: a copy job of the same files probes before each one (one byte per group).
before = Volatile.Read(ref probes);
await bed.CopyAccountAsync(bed.Marta, ct: ct);
Assert.AreEqual(before + 3, Volatile.Read(ref probes));
}
// Helpers -------------------------------------------------------------------------------------------------------
/// <summary>«Riconosci» and waits for its job.</summary>
@@ -617,6 +801,32 @@ public sealed class ExportRecognizeTests
.Select(f => $"{Path.GetRelativePath(folder, f)}:{new FileInfo(f).Length}:{File.GetLastWriteTimeUtc(f).Ticks}")
.Order(StringComparer.Ordinal)];
/// <summary>The real file system, but hashing a chosen file fails as a file held open by another program.</summary>
private sealed class UnreadableExportFileSystem(Func<string, bool> refuses) : IExportFileSystem
{
private readonly IExportFileSystem _inner = ExportFileSystem.Instance;
private int _refused;
public int Refused => Volatile.Read(ref _refused);
public Stream OpenRead(string path) => _inner.OpenRead(path);
public Task<string> ComputeSha256Async(string path, CancellationToken ct)
{
if (refuses(path))
{
Interlocked.Increment(ref _refused);
throw new IOException("Il file è usato da un altro processo.");
}
return _inner.ComputeSha256Async(path, ct);
}
public bool FileExists(string path) => _inner.FileExists(path);
public long? FileLength(string path) => _inner.FileLength(path);
}
/// <summary>The real file system, counting every read of a file (opened, or hashed).</summary>
private sealed class CountingExportFileSystem : IExportFileSystem
{