/* * mv-sync: atomic rename that fails if the destination already exists. * * Uses renameat2(2) with RENAME_NOREPLACE (Linux 3.15+). * * Exit codes: * 0 success * 1 rename failed (reason on stderr) * 2 wrong number of arguments */ #define _GNU_SOURCE #include #include #include #include #include #include #ifndef RENAME_NOREPLACE #define RENAME_NOREPLACE (1 << 0) #endif int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "usage: mv-sync \n"); return 2; } long ret = syscall(SYS_renameat2, AT_FDCWD, argv[1], AT_FDCWD, argv[2], RENAME_NOREPLACE); if (ret != 0) { fprintf(stderr, "mv-sync: rename \"%s\" -> \"%s\": %s\n", argv[1], argv[2], strerror(errno)); return 1; } return 0; }