Open juancarlospaco opened 3 years ago
renameat2
RENAME_EXCHANGE
renameat
std/os
swapDir
Solution: Fallback to current implementation iff MUSL or non-Linux.
#include <fcntl.h> #include <stdio.h> #include <sys/syscall.h> #include <unistd.h> /* MUSL Fallback */ #ifndef RENAME_EXCHANGE #define RENAME_EXCHANGE 2 #endif int main(int argc, char** argv) { /* MUSL Fallback */ if (syscall(SYS_renameat2, AT_FDCWD, argv[1], AT_FDCWD, argv[2], RENAME_EXCHANGE)) { perror(NULL); return 1; } else { return 0; } }
$ mkdir old $ mkdir new $ gcc -o moveit moveit.c $ ./moveit "old" "new"
You can use strace ./moveit "old" "new" or similar to see differences with rename API.
strace ./moveit "old" "new"
rename
/cc @timotheecour
renameat2
withRENAME_EXCHANGE
for Atomic IO.renameat2
is the same asrenameat
but allows additional arguments.RENAME_EXCHANGE
is a constant int flag forrenameat2
to atomically exchange an old path with a new path.std/os
only.swapDir
?.Cons
renameat2
andRENAME_EXCHANGE
is Linux only, but thats easy to detect in Nim.renameat2
norRENAME_EXCHANGE
, but thats easy to detect in Nim.Solution: Fallback to current implementation iff MUSL or non-Linux.
Links
Sample
You can use
strace ./moveit "old" "new"
or similar to see differences withrename
API./cc @timotheecour