]>
git.proxmox.com Git - mirror_zfs.git/blob - tests/zfs-tests/cmd/renameat2.c
1 /* SPDX-License-Identifier: CDDL-1.0 OR MPL-2.0 */
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright (C) 2019 Aleksa Sarai <cyphar@cyphar.com>
25 * Copyright (C) 2019 SUSE LLC
29 * mv(1) doesn't currently support RENAME_{EXCHANGE,WHITEOUT} so this is a very
30 * simple renameat2(2) wrapper for the OpenZFS self-tests.
39 #include <sys/syscall.h>
43 #define SYS_renameat2 __NR_renameat2
44 #elif defined(__x86_64__)
45 #define SYS_renameat2 316
46 #elif defined(__i386__)
47 #define SYS_renameat2 353
48 #elif defined(__arm__) || defined(__aarch64__)
49 #define SYS_renameat2 382
51 #error "SYS_renameat2 not known for this architecture."
55 #ifndef RENAME_NOREPLACE
56 #define RENAME_NOREPLACE (1 << 0) /* Don't overwrite target */
58 #ifndef RENAME_EXCHANGE
59 #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
61 #ifndef RENAME_WHITEOUT
62 #define RENAME_WHITEOUT (1 << 2) /* Whiteout source */
65 /* glibc doesn't provide renameat2 wrapper, let's use our own */
67 sys_renameat2(int olddirfd
, const char *oldpath
,
68 int newdirfd
, const char *newpath
, unsigned int flags
)
70 int ret
= syscall(SYS_renameat2
, olddirfd
, oldpath
, newdirfd
, newpath
,
72 return ((ret
< 0) ? -errno
: ret
);
78 fprintf(stderr
, "usage: renameat2 [-Cnwx] src dst\n");
85 int err
= sys_renameat2(AT_FDCWD
, ".", AT_FDCWD
, ".", RENAME_EXCHANGE
);
90 main(int argc
, char **argv
)
94 unsigned int flags
= 0;
96 while ((ch
= getopt(argc
, argv
, "Cnwx")) >= 0) {
102 flags
|= RENAME_NOREPLACE
;
105 flags
|= RENAME_WHITEOUT
;
108 flags
|= RENAME_EXCHANGE
;
124 err
= sys_renameat2(AT_FDCWD
, src
, AT_FDCWD
, dst
, flags
);
126 fprintf(stderr
, "renameat2: %s", strerror(-err
));