[libc-commits] [libc] d098bf2 - [libc] Implement renameat in stdio (#225739)
via libc-commits
libc-commits at lists.llvm.org
Thu Sep 24 04:03:41 PDT 2026
Author: Aman Maurya
Date: 2026-09-24T13:03:34+02:00
New Revision: d098bf235dacecec02d4c2ccc0ff4bc6a00461d2
URL: https://github.com/llvm/llvm-project/commit/d098bf235dacecec02d4c2ccc0ff4bc6a00461d2
DIFF: https://github.com/llvm/llvm-project/commit/d098bf235dacecec02d4c2ccc0ff4bc6a00461d2.diff
LOG: [libc] Implement renameat in stdio (#225739)
Implement the standard POSIX.1-2008 / POSIX.1-2024 function renameat in
<stdio.h>.
Fixes #225679
Added:
libc/src/__support/OSUtil/linux/syscall_wrappers/renameat.h
libc/src/stdio/linux/renameat.cpp
libc/src/stdio/renameat.h
libc/test/src/stdio/renameat_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/stdio.yaml
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/stdio/CMakeLists.txt
libc/src/stdio/linux/CMakeLists.txt
libc/test/src/stdio/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 56477ffaf9766..3966b0badf262 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -266,6 +266,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.printf
libc.src.stdio.remove
libc.src.stdio.rename
+ libc.src.stdio.renameat
libc.src.stdio.scanf
libc.src.stdio.vscanf
libc.src.stdio.snprintf
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index dd088f147a0c9..95c9acce089ee 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -289,6 +289,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.printf
libc.src.stdio.remove
libc.src.stdio.rename
+ libc.src.stdio.renameat
libc.src.stdio.scanf
libc.src.stdio.vscanf
libc.src.stdio.snprintf
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a5f80888a1ee7..d68fd1b5e8312 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -289,6 +289,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.printf
libc.src.stdio.remove
libc.src.stdio.rename
+ libc.src.stdio.renameat
libc.src.stdio.scanf
libc.src.stdio.vscanf
libc.src.stdio.snprintf
diff --git a/libc/include/stdio.yaml b/libc/include/stdio.yaml
index bb28dadd29b3b..c01197955ef6e 100644
--- a/libc/include/stdio.yaml
+++ b/libc/include/stdio.yaml
@@ -353,6 +353,15 @@ functions:
arguments:
- type: const char *
- type: const char *
+ - name: renameat
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
+ - type: const char *
+ - type: int
+ - type: const char *
- name: scanf
standards:
- stdc
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index e90dbfa9f2603..3089da6fc4985 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1068,6 +1068,18 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ renameat
+ HDRS
+ renameat.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
+
add_header_library(
lseek
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/renameat.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/renameat.h
new file mode 100644
index 0000000000000..519f94723ad69
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/renameat.h
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for renameat.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RENAMEAT_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RENAMEAT_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> renameat(int olddirfd, const char *oldpath,
+ int newdirfd, const char *newpath) {
+#if defined(SYS_renameat2)
+ return syscall_checked<int>(SYS_renameat2, olddirfd, oldpath, newdirfd,
+ newpath, 0);
+#elif defined(SYS_renameat)
+ return syscall_checked<int>(SYS_renameat, olddirfd, oldpath, newdirfd,
+ newpath);
+#else
+#error "renameat and renameat2 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RENAMEAT_H
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index a0d4e86674b95..d475ad5d7708c 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -271,6 +271,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.rename
)
+add_entrypoint_object(
+ renameat
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.renameat
+)
+
add_entrypoint_object(
fdopen
ALIAS
diff --git a/libc/src/stdio/linux/CMakeLists.txt b/libc/src/stdio/linux/CMakeLists.txt
index 6631f2ca4814a..d4b79e978e063 100644
--- a/libc/src/stdio/linux/CMakeLists.txt
+++ b/libc/src/stdio/linux/CMakeLists.txt
@@ -23,6 +23,20 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ renameat
+ SRCS
+ renameat.cpp
+ HDRS
+ ../renameat.h
+ DEPENDS
+ libc.src.__support.OSUtil.linux.syscall_wrappers.renameat
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
fdopen
SRCS
diff --git a/libc/src/stdio/linux/renameat.cpp b/libc/src/stdio/linux/renameat.cpp
new file mode 100644
index 0000000000000..b628c8ec42787
--- /dev/null
+++ b/libc/src/stdio/linux/renameat.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of renameat.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/renameat.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/renameat.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, renameat,
+ (int olddirfd, const char *oldpath, int newdirfd,
+ const char *newpath)) {
+ auto result = linux_syscalls::renameat(olddirfd, oldpath, newdirfd, newpath);
+ if (!result) {
+ libc_errno = result.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdio/renameat.h b/libc/src/stdio/renameat.h
new file mode 100644
index 0000000000000..39692f6272c63
--- /dev/null
+++ b/libc/src/stdio/renameat.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header of renameat.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_RENAMEAT_H
+#define LLVM_LIBC_SRC_STDIO_RENAMEAT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int renameat(int olddirfd, const char *oldpath, int newdirfd,
+ const char *newpath);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDIO_RENAMEAT_H
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index e0ca1ab2ca162..28b07cd984e9e 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -546,6 +546,29 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
libc.test.UnitTest.ErrnoSetterMatcher
)
+ add_libc_test(
+ renameat_test
+ SUITE
+ libc_stdio_unittests
+ SRCS
+ renameat_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fcntl_macros
+ libc.hdr.sys_stat_macros
+ libc.hdr.unistd_macros
+ libc.src.__support.CPP.scope
+ libc.src.errno.errno
+ libc.src.fcntl.open
+ libc.src.stdio.renameat
+ libc.src.unistd.access
+ libc.src.unistd.close
+ libc.src.unistd.unlink
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+ )
+
+
add_libc_test(
fdopen_test
SUITE
diff --git a/libc/test/src/stdio/renameat_test.cpp b/libc/test/src/stdio/renameat_test.cpp
new file mode 100644
index 0000000000000..57949f9d17d01
--- /dev/null
+++ b/libc/test/src/stdio/renameat_test.cpp
@@ -0,0 +1,99 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for renameat.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/unistd_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/__support/libc_errno.h"
+#include "src/fcntl/open.h"
+#include "src/stdio/renameat.h"
+#include "src/unistd/access.h"
+#include "src/unistd/close.h"
+#include "src/unistd/unlink.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcRenameatTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcRenameatTest, CreateAndRenameFileWithAtFdcwd) {
+ constexpr const char *FILENAME0 = "renameat.test.file0";
+ auto TEST_FILEPATH0 = libc_make_test_file_path(FILENAME0);
+ constexpr const char *FILENAME1 = "renameat.test.file1";
+ auto TEST_FILEPATH1 = libc_make_test_file_path(FILENAME1);
+
+ int fd = LIBC_NAMESPACE::open(TEST_FILEPATH0, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+
+ LIBC_NAMESPACE::cpp::scope_exit cleanup_files([&] {
+ LIBC_NAMESPACE::unlink(TEST_FILEPATH0);
+ LIBC_NAMESPACE::unlink(TEST_FILEPATH1);
+ LIBC_NAMESPACE::libc_errno = 0;
+ });
+
+ ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH0, F_OK), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::renameat(AT_FDCWD, TEST_FILEPATH0, AT_FDCWD,
+ TEST_FILEPATH1),
+ Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH1, F_OK), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::access(TEST_FILEPATH0, F_OK), Fails(ENOENT));
+}
+
+TEST_F(LlvmLibcRenameatTest, CreateAndRenameWithDirFd) {
+ auto TEST_DIR = libc_make_test_file_path("testdata");
+ constexpr const char *BASENAME0 = "renameat_dir0.test";
+ constexpr const char *BASENAME1 = "renameat_dir1.test";
+ auto PATH0 = libc_make_test_file_path("testdata/renameat_dir0.test");
+ auto PATH1 = libc_make_test_file_path("testdata/renameat_dir1.test");
+
+ int dirfd = LIBC_NAMESPACE::open(TEST_DIR, O_DIRECTORY);
+ ASSERT_GT(dirfd, 0);
+ LIBC_NAMESPACE::cpp::scope_exit cleanup_dir(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(dirfd), Succeeds(0)); });
+
+ int fd = LIBC_NAMESPACE::open(PATH0, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_GT(fd, 0);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+
+ LIBC_NAMESPACE::cpp::scope_exit cleanup_files([&] {
+ LIBC_NAMESPACE::unlink(PATH0);
+ LIBC_NAMESPACE::unlink(PATH1);
+ LIBC_NAMESPACE::libc_errno = 0;
+ });
+
+ ASSERT_THAT(LIBC_NAMESPACE::access(PATH0, F_OK), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::renameat(dirfd, BASENAME0, dirfd, BASENAME1),
+ Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::access(PATH1, F_OK), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::access(PATH0, F_OK), Fails(ENOENT));
+}
+
+TEST_F(LlvmLibcRenameatTest, BadDirFd) {
+ ASSERT_THAT(LIBC_NAMESPACE::renameat(-1, "some-file", -1, "other-file"),
+ Fails(EBADF));
+}
+
+TEST_F(LlvmLibcRenameatTest, RenameNonExistent) {
+ constexpr const char *FILENAME1 = "renameat.test.nonexistent";
+ auto TEST_FILEPATH1 = libc_make_test_file_path(FILENAME1);
+
+ ASSERT_THAT(LIBC_NAMESPACE::renameat(AT_FDCWD, "non-existent-source",
+ AT_FDCWD, TEST_FILEPATH1),
+ Fails(ENOENT));
+}
More information about the libc-commits
mailing list