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>
90 lines
3.3 KiB
C#
90 lines
3.3 KiB
C#
using Febe.Core.Domain;
|
|
using Febe.Core.Library;
|
|
using Febe.Core.Storage;
|
|
|
|
namespace Febe.Core.Export;
|
|
|
|
/// <summary>
|
|
/// The <c>.part</c> files a crash left in a destination (spec §4.10): removed only in the folders where the items of
|
|
/// an unfinished job were to be written, never by a scan of the destination, never in a folder of no item. Only our
|
|
/// own temporary names (<c>.febe-*.part</c>, the probes included) are touched.
|
|
/// </summary>
|
|
public static class ExportLeftovers
|
|
{
|
|
private const int PageSize = 1000;
|
|
|
|
/// <summary>Removes our temporary files in the folders of the items of <paramref name="run"/>. Returns how many.</summary>
|
|
public static async Task<int> CleanAsync(ExportEnvironment env, ExportRun run, ExportDestination destination, CancellationToken ct)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(env);
|
|
ArgumentNullException.ThrowIfNull(run);
|
|
ArgumentNullException.ThrowIfNull(destination);
|
|
if (ExportRoots.MountProblem(env.Root, destination.RootPath) is not null || !await AnswersAsync(env, destination.RootPath, ct).ConfigureAwait(false))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
var folders = new HashSet<string>(OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
|
|
long after = 0;
|
|
while (true)
|
|
{
|
|
var page = await env.Storage.Exports.ListItemsAsync(new ExportItemQuery { RunId = run.Id, AfterId = after, Limit = PageSize }, ct).ConfigureAwait(false);
|
|
if (page.Count == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
after = page[^1].Id;
|
|
foreach (var item in page)
|
|
{
|
|
if (item.RelativePath is { } relative && item.State is ExportItemState.Pending or ExportItemState.Failed)
|
|
{
|
|
try
|
|
{
|
|
if (Path.GetDirectoryName(LibraryLayout.FullPath(destination.RootPath, relative)) is { } folder)
|
|
{
|
|
folders.Add(folder);
|
|
}
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
// A path that would leave the root: nothing of ours can be there.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var removed = 0;
|
|
foreach (var folder in folders)
|
|
{
|
|
ct.ThrowIfCancellationRequested();
|
|
try
|
|
{
|
|
removed += await Task.Run(() => SafeFileWriter.CleanupTemporaryFiles(folder, recursive: false), ct).WaitAsync(env.FolderCheckTimeout, ct).ConfigureAwait(false);
|
|
}
|
|
catch (TimeoutException)
|
|
{
|
|
return removed; // the disk stopped answering: the next resume tries again
|
|
}
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
|
{
|
|
// Best effort: a leftover .part takes some space, nothing more.
|
|
}
|
|
}
|
|
|
|
return removed;
|
|
}
|
|
|
|
private static async Task<bool> AnswersAsync(ExportEnvironment env, string root, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return await Task.Run(() => Directory.Exists(root), ct).WaitAsync(env.FolderCheckTimeout, ct).ConfigureAwait(false);
|
|
}
|
|
catch (TimeoutException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|