38 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * Program that atomically exchanges two paths using4 * the renameat2() system call RENAME_EXCHANGE flag.5 *6 * Copyright 2022 Red Hat Inc.7 * Author: Javier Martinez Canillas <javierm@redhat.com>8 */9 10#define _GNU_SOURCE11#include <fcntl.h>12#include <stdio.h>13#include <stdlib.h>14 15void print_usage(const char *program)16{17 printf("Usage: %s [oldpath] [newpath]\n", program);18 printf("Atomically exchange oldpath and newpath\n");19}20 21int main(int argc, char *argv[])22{23 int ret;24 25 if (argc != 3) {26 print_usage(argv[0]);27 exit(EXIT_FAILURE);28 }29 30 ret = renameat2(AT_FDCWD, argv[1], AT_FDCWD, argv[2], RENAME_EXCHANGE);31 if (ret) {32 perror("rename exchange failed");33 exit(EXIT_FAILURE);34 }35 36 exit(EXIT_SUCCESS);37}38