timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

renameat2 for atomic IO #810

Open juancarlospaco opened 3 years ago

juancarlospaco commented 3 years ago

Cons

Solution: Fallback to current implementation iff MUSL or non-Linux.

Links

Sample

#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.

/cc @timotheecour