E2: motore delle copie esterne (porta, destinazioni, piano, copia)

Prima metà della tappa E2 «Copie esterne» (#58), parte Core e Data:

- porta del lavoro lungo: importazione, copia e ricostruzione mai insieme;
- radice /esporta in Docker con mountinfo, controllo delle cartelle con
  il file di prova, destinazioni con «Esporta questo account» acceso se
  Alberto non ha mai scelto (decisione del 27/11/2026);
- filtro «Copie», segni delle tessere e conteggi dell'account sui soli
  file dovuti (D3);
- percorsi «Come la libreria», segmento dell'account nella cartella
  generale, 240 caratteri, storia in evidenza;
- piano di «Copia in…», un lavoro per destinazione, copia in .part con
  SHA-256 riletto dal disco, nome libero, mai sovrascrivere, adozione
  per percorso, pausa della destinazione su prova, spazio e FAT32,
  attesa fuori dalle sessioni del motore, ripresa e recupero;
- .part rimossi solo nelle cartelle degli elementi di lavori non finiti;
- «Tolto» e «Superato» (agganci in importazione, scaricamenti,
  doppioni e ricostruzione), mai ricopiati da soli;
- rapporti e testi in italiano, prove del lavoro di copia.

Interfaccia, API e composizione restano alla parte successiva.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-27 10:55:29 +01:00
1 parent 213b89a618
commit a112c92893
41 files changed
+5370 -27

No files matched your search

+58
View File
@@ -27,6 +27,21 @@ public sealed record SafeWriteOptions
/// <summary>Bytes written so far.</summary>
public IProgress<long>? Progress { get; init; }
/// <summary>
/// The root folder must already exist: <see cref="ExportRootMissingException"/> before anything is created (spec §4.10
/// «La radice di una destinazione non si crea mai»: a missing disk or share must not become a local folder).
/// Subfolders are still created. The root could vanish between the check and the creation of a subfolder: on Windows
/// a missing drive or share throws anyway, and in Docker a mount cannot vanish inside a running container.
/// </summary>
public bool RequireExistingRoot { get; init; }
/// <summary>
/// After the flush, read the temporary file back from the disk and compare its SHA-256 with the one computed while
/// writing, before the final name is decided (copies to external disks, spec §4.10). On a mismatch the temporary file
/// is removed and an <see cref="IOException"/> says so.
/// </summary>
public bool VerifyFromDisk { get; init; }
}
/// <summary>The content exceeded <see cref="SafeWriteOptions.MaxBytes"/>; nothing was kept.</summary>
@@ -47,6 +62,24 @@ public sealed class FileTooLargeException : IOException
}
}
/// <summary>The root folder of a write with <see cref="SafeWriteOptions.RequireExistingRoot"/> does not exist; nothing was created.</summary>
public sealed class ExportRootMissingException : DirectoryNotFoundException
{
public ExportRootMissingException()
{
}
public ExportRootMissingException(string message)
: base(message)
{
}
public ExportRootMissingException(string message, Exception innerException)
: base(message, innerException)
{
}
}
/// <summary>
/// Writes library files safely (spec §6.5 point 4, rule 12):
/// <list type="number">
@@ -136,6 +169,11 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
options ??= new SafeWriteOptions();
var root = Path.GetFullPath(rootFolder);
var tempFolder = string.IsNullOrEmpty(tempDirectory) ? root : LibraryLayout.FullPath(root, tempDirectory);
if (options.RequireExistingRoot && !Directory.Exists(root))
{
throw new ExportRootMissingException($"La cartella {root} non c'è: Febe non la crea, perché un disco o una condivisione assente non deve diventare una cartella locale.");
}
Directory.CreateDirectory(tempFolder);
var tempPath = Path.Combine(tempFolder, TempPrefix + Guid.NewGuid().ToString("N")[..12] + TempSuffix);
using var reservation = ReserveTemporaryFile(tempPath);
@@ -144,6 +182,10 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
{
var digest = await WriteTempAsync(source, tempPath, options, ct).ConfigureAwait(false);
ct.ThrowIfCancellationRequested();
if (options.VerifyFromDisk)
{
await VerifyFromDiskAsync(tempPath, digest, ct).ConfigureAwait(false);
}
var desired = await decide(digest, ct).ConfigureAwait(false);
if (desired is null)
@@ -247,6 +289,22 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
return new FileDigest(total, Convert.ToHexStringLower(hash.GetHashAndReset()));
}
/// <summary>Reads the temporary file back and compares its SHA-256 with the one computed while writing.</summary>
private static async Task VerifyFromDiskAsync(string tempPath, FileDigest digest, CancellationToken ct)
{
string onDisk;
await using (var stream = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan))
{
onDisk = Convert.ToHexStringLower(await SHA256.HashDataAsync(stream, ct).ConfigureAwait(false));
}
if (!string.Equals(onDisk, digest.Sha256, StringComparison.Ordinal))
{
TryDelete(tempPath); // our own temporary file: the caller's finally would remove it too
throw new IOException("La copia scritta sul disco non corrisponde: il disco potrebbe avere problemi.");
}
}
/// <summary>Sanitized segments of a relative path ('/' or '\' separated). «..» becomes «_»: nothing escapes the root.</summary>
private static List<string> Segments(string relativePath)
{