[libc-commits] [libc] [libc][stdio] implement rename via SYS_renameat2 (PR #86140)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Mar 21 08:57:22 PDT 2024


https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/86140

>From dc08bafe20106d535cb3b2b8ad8e663867259a89 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 21 Mar 2024 08:51:29 -0700
Subject: [PATCH 1/3] [libc][stdio] implement rename via SYS_renameat2

SYS_rename may be unavailable on architectures such as aarch64 and riscv.
rename can be implemented in terms of SYS_rename, SYS_renameat, or
SYS_renameat2. I don't have a full picture of the history here, but it seems
that SYS_renameat might also be unavailable on some platforms.

`man 2 rename` mentions that SYS_renameat2 was added in Linux 3.15.  We don't
need to support such ancient kernel versions.

Link: #84980
Link: #85068
---
 libc/src/stdio/linux/rename.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/stdio/linux/rename.cpp b/libc/src/stdio/linux/rename.cpp
index f3d684249ad28e..afa025864fb1c5 100644
--- a/libc/src/stdio/linux/rename.cpp
+++ b/libc/src/stdio/linux/rename.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "include/llvm-libc-macros/linux/fcntl-macros.h"
 #include "src/stdio/rename.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
@@ -15,7 +16,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(int, rename, (const char *oldpath, const char *newpath)) {
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_rename, oldpath, newpath);
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_renameat2, AT_FDCWD, oldpath, AT_FDCWD, newpath, 0);
 
   if (ret >= 0)
     return 0;

>From 3e957e963496872091f966b4c4ba08054b59cc86 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 21 Mar 2024 08:55:41 -0700
Subject: [PATCH 2/3] reformat

---
 libc/src/stdio/linux/rename.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/src/stdio/linux/rename.cpp b/libc/src/stdio/linux/rename.cpp
index afa025864fb1c5..379a6ef3c0c849 100644
--- a/libc/src/stdio/linux/rename.cpp
+++ b/libc/src/stdio/linux/rename.cpp
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "include/llvm-libc-macros/linux/fcntl-macros.h"
 #include "src/stdio/rename.h"
+#include "include/llvm-libc-macros/linux/fcntl-macros.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 #include "src/errno/libc_errno.h"
@@ -16,7 +16,8 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(int, rename, (const char *oldpath, const char *newpath)) {
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_renameat2, AT_FDCWD, oldpath, AT_FDCWD, newpath, 0);
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_renameat2, AT_FDCWD, oldpath,
+                                              AT_FDCWD, newpath, 0);
 
   if (ret >= 0)
     return 0;

>From 42ded6cd3f2b248f4c6ae7c14eaca366335d2ab7 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 21 Mar 2024 08:57:08 -0700
Subject: [PATCH 3/3] reformat test

---
 libc/test/src/stdio/rename_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/src/stdio/rename_test.cpp b/libc/test/src/stdio/rename_test.cpp
index 3ed39fe8c0eb97..a5dd734c636169 100644
--- a/libc/test/src/stdio/rename_test.cpp
+++ b/libc/test/src/stdio/rename_test.cpp
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "include/llvm-libc-macros/linux/unistd-macros.h"
 #include "include/llvm-libc-macros/linux/sys-stat-macros.h"
+#include "include/llvm-libc-macros/linux/unistd-macros.h"
 #include "src/errno/libc_errno.h"
 #include "src/fcntl/open.h"
 #include "src/stdio/rename.h"



More information about the libc-commits mailing list