Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Program that atomically exchanges two paths using
0004  * the renameat2() system call RENAME_EXCHANGE flag.
0005  *
0006  * Copyright 2022 Red Hat Inc.
0007  * Author: Javier Martinez Canillas <javierm@redhat.com>
0008  */
0009 
0010 #define _GNU_SOURCE
0011 #include <fcntl.h>
0012 #include <stdio.h>
0013 #include <stdlib.h>
0014 
0015 void print_usage(const char *program)
0016 {
0017     printf("Usage: %s [oldpath] [newpath]\n", program);
0018     printf("Atomically exchange oldpath and newpath\n");
0019 }
0020 
0021 int main(int argc, char *argv[])
0022 {
0023     int ret;
0024 
0025     if (argc != 3) {
0026         print_usage(argv[0]);
0027         exit(EXIT_FAILURE);
0028     }
0029 
0030     ret = renameat2(AT_FDCWD, argv[1], AT_FDCWD, argv[2], RENAME_EXCHANGE);
0031     if (ret) {
0032         perror("rename exchange failed");
0033         exit(EXIT_FAILURE);
0034     }
0035 
0036     exit(EXIT_SUCCESS);
0037 }