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

+144
View File
@@ -0,0 +1,144 @@
using System.Security.Cryptography;
using Febe.Core.Abstractions;
using Febe.Core.Domain;
using Febe.Core.Library;
using Febe.Core.Storage;
namespace Febe.Core.Export;
/// <summary>
/// What the export needs from the rest of Febe (spec §4.10). Built once by the composition (the web layer's export
/// hub); the layout is a function because on Windows the library folder can change while Febe runs.
/// </summary>
public sealed record ExportEnvironment
{
public required IFebeStorage Storage { get; init; }
/// <summary>The library folder and account folders now.</summary>
public required Func<LibraryLayout> Layout { get; init; }
/// <summary>The Data folder: a destination may not overlap it.</summary>
public required string DataFolder { get; init; }
public required IClock Clock { get; init; }
/// <summary>Docker <c>/importa</c>: a destination may not overlap it. Null on Windows.</summary>
public string? ImportRoot { get; init; }
/// <summary>Where destinations may be chosen (Docker: <c>/esporta</c>).</summary>
public ExportRootOptions Root { get; init; } = ExportRootOptions.AnyFolder;
/// <summary>
/// The full path of a library file present on disk, or null. Null: the library layout (as the web layer's
/// <c>LibraryFileResolver</c>). Use <see cref="ResolveSourcePath"/>.
/// </summary>
public Func<Account, Media, string?>? ResolveSource { get; init; }
/// <summary>Reading and hashing files (library files and files found in a destination).</summary>
public IExportFileSystem FileSystem { get; init; } = ExportFileSystem.Instance;
/// <summary>Free space and the FAT32 limit of a destination.</summary>
public IExportDisk Disk { get; init; } = new SystemExportDisk(new ProcMountInfo());
/// <summary>Copies never run during an engine session.</summary>
public IEngineSessionState Session { get; init; } = NoEngineSession.Instance;
/// <summary>Shared with import and rebuild: never together (spec §4.10). Null: nothing else to keep out (tests).</summary>
public LongWorkGate? Gate { get; init; }
/// <summary>Time allowed to a check of a destination folder (a disconnected share must not block).</summary>
public TimeSpan FolderCheckTimeout { get; init; } = TimeSpan.FromSeconds(10);
/// <summary>How often a waiting job looks whether the engine session ended.</summary>
public TimeSpan SessionPoll { get; init; } = TimeSpan.FromSeconds(5);
/// <summary>A group of copies, checked with a probe before it starts: at most this many files…</summary>
public int GroupFiles { get; init; } = 100;
/// <summary>…or this many planned bytes, whichever comes first.</summary>
public long GroupBytes { get; init; } = 1L << 30;
/// <summary>The checker of destination folders of this environment.</summary>
public ExportFolderCheck CreateFolderCheck() => new(Root.Mounts, FolderCheckTimeout);
/// <summary>The protected folders now (Data, library, account folders, <c>/importa</c>) and the extra ones.</summary>
public Task<ExportFolderContext> FolderContextAsync(IReadOnlyList<ProtectedFolder>? extra, CancellationToken ct) =>
ExportFolderCheck.ContextAsync(Storage, Layout(), DataFolder, ImportRoot, Root, extra, ct);
/// <summary>The full path of a library file present on disk, or null (<see cref="ResolveSource"/> or the layout).</summary>
public string? ResolveSourcePath(Account account, Media media)
{
ArgumentNullException.ThrowIfNull(account);
ArgumentNullException.ThrowIfNull(media);
if (ResolveSource is { } resolve)
{
return resolve(account, media);
}
if (media.RelativePath is null || !EntryStateRules.IsPresent(media.State))
{
return null;
}
try
{
var path = Layout().FullPath(account, media.RelativePath);
return FileSystem.FileExists(path) ? path : null;
}
catch (ArgumentException)
{
return null;
}
}
}
/// <summary>File operations of the export (a seam, like <c>IImportFileSystem</c>).</summary>
public interface IExportFileSystem
{
/// <summary>Opens a file for reading only.</summary>
Stream OpenRead(string path);
/// <summary>SHA-256 (lowercase hex) of a file as it is on disk now.</summary>
Task<string> ComputeSha256Async(string path, CancellationToken ct);
bool FileExists(string path);
/// <summary>Size of a file, or null when it is not there.</summary>
long? FileLength(string path);
}
/// <summary>The real file system.</summary>
public sealed class ExportFileSystem : IExportFileSystem
{
private const int BufferSize = 128 * 1024;
private ExportFileSystem()
{
}
public static ExportFileSystem Instance { get; } = new();
public Stream OpenRead(string path) =>
new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
public async Task<string> ComputeSha256Async(string path, CancellationToken ct)
{
await using var stream = OpenRead(path);
return Convert.ToHexStringLower(await SHA256.HashDataAsync(stream, ct).ConfigureAwait(false));
}
public bool FileExists(string path) => File.Exists(path);
public long? FileLength(string path)
{
try
{
var info = new FileInfo(path);
return info.Exists ? info.Length : null;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
{
return null;
}
}
}