using System.Runtime.InteropServices; namespace Febe.Core.Library; /// What did. internal enum NoReplaceOutcome { /// The file has its new name. Moved, /// The new name exists: nothing changed. Taken, /// The file system cannot do it atomically: nothing changed, the caller checks and moves. Unsupported, } /// /// 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 File.Move(overwrite: false) 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). /// /// Linux: renameat2(…, RENAME_NOREPLACE); EEXIST means the name is taken; /// without it (an older kernel, a file system that refuses the flag, another Unix): link then /// unlink of the temporary name, where link refuses an existing name with EEXIST; /// without hard links either (vfat, some CIFS mounts): . /// /// Not for Windows, where MoveFileEx without MOVEFILE_REPLACE_EXISTING already refuses an existing name. /// 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; /// Renames to unless that name exists. 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); }