E2: correzioni della revisione delle copie esterne
- Nome libero scelto prima di scrivere, libero sul disco e nel database; una riga orfana sul nome diventa «Tolto» (guardando il suo percorso, anche se differisce solo per le maiuscole): prima la copia finiva «Non riuscito» con il file già scritto, o toglieva la copia buona. - «Controlla le copie» rilegge la radice e riguarda i file mancanti; con il disco staccato si ferma e mette in pausa senza segnare nulla. - Su Linux lo spostamento sul nome finale non sostituisce mai un file (renameat2 RENAME_NOREPLACE, poi link e unlink). - Le copie in coda di un lavoro interrotto diventano «Interrotta» e si riprendono; il permesso dei lavori lunghi si tiene già nel piano. - «Superato» con il file di nuovo in libreria: filtro, piano e «Copia di nuovo» d'accordo; l'adozione ravviva la riga invece di fallire. - «Una copia sola» con una regola sola in filtro, piano, visualizzatore e copiatore (solo copie di un altro account in cartelle in uso). - Controllo della cartella per identità vera (percorso finale, realpath, dispositivo di mountinfo): riconosce collegamenti e bind mount di una sottocartella della libreria. - Sfoglia delle cartelle con tempi massimi; «Nessuna cartella» solo se non ce n'è; «In copia…» nel visualizzatore; numero e fuoco stabili in «Copia di nuovo anche…»; una sola lettura per volta nella Dashboard. - Prove dell'interfaccia indipendenti dall'ora e dal file scelto; prove nuove in ExportReviewTests, ExportFolderCheckTests, ExportRootTests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
28c7f814b1
commit
36ad5c6f9d
28 files changed
+1314
-112
No files matched your search
@@ -0,0 +1,108 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Febe.Core.Library;
|
||||
|
||||
/// <summary>What <see cref="NoReplaceRename.TryMove"/> did.</summary>
|
||||
internal enum NoReplaceOutcome
|
||||
{
|
||||
/// <summary>The file has its new name.</summary>
|
||||
Moved,
|
||||
|
||||
/// <summary>The new name exists: nothing changed.</summary>
|
||||
Taken,
|
||||
|
||||
/// <summary>The file system cannot do it atomically: nothing changed, the caller checks and moves.</summary>
|
||||
Unsupported,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An atomic rename that never replaces an existing name, on Linux and the other Unix systems (rule 12: Febe never
|
||||
/// overwrites a file). On Unix <c>File.Move(overwrite: false)</c> checks and then calls rename(2), which replaces: another
|
||||
/// program, or another Febe writing to the same folder (spec §4.10 «due istanze sulla stessa cartella»), could create the
|
||||
/// name in between. Only the platform's own C library is called (BCL interop, no external component).
|
||||
/// <list type="number">
|
||||
/// <item>Linux: <c>renameat2(…, RENAME_NOREPLACE)</c>; EEXIST means the name is taken;</item>
|
||||
/// <item>without it (an older kernel, a file system that refuses the flag, another Unix): <c>link</c> then
|
||||
/// <c>unlink</c> of the temporary name, where link refuses an existing name with EEXIST;</item>
|
||||
/// <item>without hard links either (vfat, some CIFS mounts): <see cref="NoReplaceOutcome.Unsupported"/>.</item>
|
||||
/// </list>
|
||||
/// Not for Windows, where <c>MoveFileEx</c> without <c>MOVEFILE_REPLACE_EXISTING</c> already refuses an existing name.
|
||||
/// </summary>
|
||||
internal static class NoReplaceRename
|
||||
{
|
||||
private const int AtFdCwd = -100;
|
||||
private const uint RenameNoReplace = 1;
|
||||
private const int Eexist = 17; // the same value on Linux, macOS and the BSDs
|
||||
|
||||
private static volatile bool _noRenameAt2;
|
||||
private static volatile bool _noLink;
|
||||
|
||||
/// <summary>Renames <paramref name="source"/> to <paramref name="destination"/> unless that name exists.</summary>
|
||||
public static NoReplaceOutcome TryMove(string source, string destination)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(source);
|
||||
ArgumentException.ThrowIfNullOrEmpty(destination);
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
return NoReplaceOutcome.Unsupported;
|
||||
}
|
||||
|
||||
if (OperatingSystem.IsLinux() && !_noRenameAt2)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (RenameAt2(AtFdCwd, source, AtFdCwd, destination, RenameNoReplace) == 0)
|
||||
{
|
||||
return NoReplaceOutcome.Moved;
|
||||
}
|
||||
|
||||
if (Marshal.GetLastPInvokeError() == Eexist)
|
||||
{
|
||||
return NoReplaceOutcome.Taken;
|
||||
}
|
||||
|
||||
// EINVAL (the file system refuses the flag), ENOSYS (an older kernel) or another error: try a link.
|
||||
}
|
||||
catch (Exception ex) when (ex is EntryPointNotFoundException or DllNotFoundException)
|
||||
{
|
||||
_noRenameAt2 = true; // a C library without renameat2
|
||||
}
|
||||
}
|
||||
|
||||
if (_noLink)
|
||||
{
|
||||
return NoReplaceOutcome.Unsupported;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (Link(source, destination) == 0)
|
||||
{
|
||||
_ = Unlink(source); // when this fails the caller's clean-up removes the temporary name
|
||||
return NoReplaceOutcome.Moved;
|
||||
}
|
||||
|
||||
// EPERM, ENOTSUP, EXDEV…: no hard links here. The caller checks and moves, and reports a real error itself.
|
||||
return Marshal.GetLastPInvokeError() == Eexist ? NoReplaceOutcome.Taken : NoReplaceOutcome.Unsupported;
|
||||
}
|
||||
catch (Exception ex) when (ex is EntryPointNotFoundException or DllNotFoundException)
|
||||
{
|
||||
_noLink = true;
|
||||
return NoReplaceOutcome.Unsupported;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("libc", EntryPoint = "renameat2", SetLastError = true)]
|
||||
private static extern int RenameAt2(
|
||||
int oldDirectory,
|
||||
[MarshalAs(UnmanagedType.LPUTF8Str)] string oldPath,
|
||||
int newDirectory,
|
||||
[MarshalAs(UnmanagedType.LPUTF8Str)] string newPath,
|
||||
uint flags);
|
||||
|
||||
[DllImport("libc", EntryPoint = "link", SetLastError = true)]
|
||||
private static extern int Link([MarshalAs(UnmanagedType.LPUTF8Str)] string oldPath, [MarshalAs(UnmanagedType.LPUTF8Str)] string newPath);
|
||||
|
||||
[DllImport("libc", EntryPoint = "unlink", SetLastError = true)]
|
||||
private static extern int Unlink([MarshalAs(UnmanagedType.LPUTF8Str)] string path);
|
||||
}
|
||||
Reference in new issue
Block a user