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
@@ -12,7 +12,9 @@ namespace Febe.Core.Library;
|
||||
/// <param name="Options">Per-mount options (<c>rw</c>, <c>ro</c>, <c>noatime</c>…).</param>
|
||||
/// <param name="FileSystemType">File system type (<c>ext4</c>, <c>vfat</c>, <c>cifs</c>…).</param>
|
||||
/// <param name="Source">Mount source (<c>/dev/sdb1</c>, <c>//server/share</c>…), unescaped.</param>
|
||||
public sealed record MountInfoEntry(int Id, int ParentId, string Root, string MountPoint, IReadOnlyList<string> Options, string FileSystemType, string Source)
|
||||
/// <param name="Device">The device of the file system, <c>major:minor</c> (field 3): two mounts with the same device show
|
||||
/// folders of one file system, so <see cref="Root"/> tells whether one is inside the other.</param>
|
||||
public sealed record MountInfoEntry(int Id, int ParentId, string Root, string MountPoint, IReadOnlyList<string> Options, string FileSystemType, string Source, string? Device = null)
|
||||
{
|
||||
/// <summary>The mount is read-only (<c>ro</c>, as for a Docker volume mounted with <c>:ro</c>).</summary>
|
||||
public bool ReadOnly => Options.Contains("ro", StringComparer.Ordinal);
|
||||
@@ -61,7 +63,8 @@ public static class MountInfoTable
|
||||
point,
|
||||
fields[5].Split(',', StringSplitOptions.RemoveEmptyEntries),
|
||||
fields[separator + 1],
|
||||
MountTable.Unescape(fields[separator + 2])));
|
||||
MountTable.Unescape(fields[separator + 2]),
|
||||
fields[2]));
|
||||
}
|
||||
|
||||
return entries;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -330,11 +330,33 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(finalPath)!);
|
||||
|
||||
// On Unix File.Move(overwrite: false) checks for the destination and then renames, and a rename replaces:
|
||||
// two writers of this process could overwrite each other. The check and the rename are therefore done under
|
||||
// one lock (Febe is the only writer of its library). On Windows the move itself refuses an existing name.
|
||||
// another writer (of this process, another Febe on the same export folder, another program) could take the
|
||||
// name in between. On Unix the move is therefore an atomic rename that refuses an existing name
|
||||
// (NoReplaceRename); only where the file system cannot do that, the check and the move are done under one
|
||||
// lock, which still keeps the writers of this process apart. On Windows the move itself refuses an existing
|
||||
// name.
|
||||
lock (MoveGate)
|
||||
{
|
||||
if (File.Exists(finalPath) || Directory.Exists(finalPath))
|
||||
if (Directory.Exists(finalPath))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
var outcome = NoReplaceRename.TryMove(tempPath, finalPath);
|
||||
if (outcome == NoReplaceOutcome.Moved)
|
||||
{
|
||||
return finalPath;
|
||||
}
|
||||
|
||||
if (outcome == NoReplaceOutcome.Taken)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (File.Exists(finalPath))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user