blob: 167769360e30fd38aacd79d39c9bd07ae681d1b8 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From dc651fe2e6b16087c519c0bd0bf943cb7c53c807 Mon Sep 17 00:00:00 2001
2In-Reply-To: <20240423234355.2414567-1-Tony.Ambardar@gmail.com>
3References: <20240423234355.2414567-1-Tony.Ambardar@gmail.com>
4From: Tony Ambardar <Tony.Ambardar@gmail.com>
5Date: Sat, 20 Apr 2024 21:30:13 -0700
6Subject: [PATCH v3] add renameat2 linux syscall wrapper
7To: musl@lists.openwall.com
8Cc: Rich Felker <dalias@libc.org>
9
10This syscall is available since Linux 3.15 and also implemented in glibc
11from version 2.28. It is commonly used in filesystem or security contexts.
12
13Constants RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
14_GNU_SOURCE as with glibc.
15
16Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
17---
18v2 -> v3:
19 * call SYS_renameat first if applicable
20 * drop unneeded error code handling
21
22v1 -> v2:
23 * align related constants
24 * drop 'int' from 'unsigned int'
25 * add fallback to SYS_renameat where applicable
26---
27 include/stdio.h | 7 +++++++
28 src/linux/renameat2.c | 11 +++++++++++
29 2 files changed, 18 insertions(+)
30 create mode 100644 src/linux/renameat2.c
31
32--- a/include/stdio.h
33+++ b/include/stdio.h
34@@ -158,6 +158,13 @@ char *ctermid(char *);
35 #define L_ctermid 20
36 #endif
37
38+#if defined(_GNU_SOURCE)
39+#define RENAME_NOREPLACE (1 << 0)
40+#define RENAME_EXCHANGE (1 << 1)
41+#define RENAME_WHITEOUT (1 << 2)
42+
43+int renameat2(int, const char *, int, const char *, unsigned);
44+#endif
45
46 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
47 || defined(_BSD_SOURCE)
48--- /dev/null
49+++ b/src/linux/renameat2.c
50@@ -0,0 +1,11 @@
51+#define _GNU_SOURCE
52+#include <stdio.h>
53+#include "syscall.h"
54+
55+int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned flags)
56+{
57+#ifdef SYS_renameat
58+ if (!flags) return syscall(SYS_renameat, oldfd, old, newfd, new);
59+#endif
60+ return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
61+}