Skip to content

[libc] Enable utimes function for riscv #139181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2025

Conversation

mikhailramalho
Copy link
Member

RV32 uses SYS_utimensat_time64 instead of SYS_utimensat but the call is the same.

RV32 uses SYS_utimensat_time64 instead of SYS_utimensat but the call is
the same.
@llvmbot
Copy link
Member

llvmbot commented May 9, 2025

@llvm/pr-subscribers-libc

Author: Mikhail R. Gadelha (mikhailramalho)

Changes

RV32 uses SYS_utimensat_time64 instead of SYS_utimensat but the call is the same.


Full diff: https://github.com/llvm/llvm-project/pull/139181.diff

3 Files Affected:

  • (modified) libc/config/linux/riscv/entrypoints.txt (+1-1)
  • (modified) libc/include/sys/syscall.h.def (+4)
  • (modified) libc/src/sys/time/linux/utimes.cpp (+15-7)
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 30d9d00dfefc9..0ee53e26a9286 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -290,7 +290,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.statvfs.statvfs
 
     # sys/utimes.h entrypoints
-    # libc.src.sys.time.utimes
+    libc.src.sys.time.utimes
 
     # sys/utsname.h entrypoints
     libc.src.sys.utsname.uname
diff --git a/libc/include/sys/syscall.h.def b/libc/include/sys/syscall.h.def
index 03c19eb0885ed..6f309c8bab331 100644
--- a/libc/include/sys/syscall.h.def
+++ b/libc/include/sys/syscall.h.def
@@ -2301,6 +2301,10 @@
 #define SYS_utimes __NR_utimes
 #endif
 
+#ifdef __NR_utimensat_time64
+#define SYS_utimensat_time64 __NR_utimensat_time64
+#endif
+
 #ifdef __NR_utrap_install
 #define SYS_utrap_install __NR_utrap_install
 #endif
diff --git a/libc/src/sys/time/linux/utimes.cpp b/libc/src/sys/time/linux/utimes.cpp
index e6e3d073a81a4..aed04160d5f9d 100644
--- a/libc/src/sys/time/linux/utimes.cpp
+++ b/libc/src/sys/time/linux/utimes.cpp
@@ -10,6 +10,7 @@
 
 #include "hdr/fcntl_macros.h"
 #include "hdr/types/struct_timeval.h"
+#include "hdr/types/struct_timespec.h"
 
 #include "src/__support/OSUtil/syscall.h"
 #include "src/__support/common.h"
@@ -20,14 +21,24 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
+#if SYS_utimes
+constexpr auto UTIMES_SYSCALL_ID = SYS_utimes;
+#elif defined(SYS_utimensat)
+constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat;
+#elif defined(SYS_utimensat_time64)
+constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat_time64;
+#else
+#error "utimes, utimensat, utimensat_time64,  syscalls not available."
+#endif
+
 LLVM_LIBC_FUNCTION(int, utimes,
                    (const char *path, const struct timeval times[2])) {
   int ret;
 
 #ifdef SYS_utimes
   // No need to define a timespec struct, use the syscall directly.
-  ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimes, path, times);
-#elif defined(SYS_utimensat)
+  ret = LIBC_NAMESPACE::syscall_impl<int>(UTIMES_SYSCALL_ID, path, times);
+#elif defined(SYS_utimensat) || defined(SYS_utimensat_time64)
   // the utimensat syscall requires a timespec struct, not timeval.
   struct timespec ts[2];
   struct timespec *ts_ptr = nullptr; // default value if times is nullptr
@@ -59,11 +70,8 @@ LLVM_LIBC_FUNCTION(int, utimes,
 
   // utimensat syscall.
   // flags=0 means don't follow symlinks (like utimes)
-  ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimensat, AT_FDCWD, path, ts_ptr,
-                                          0);
-
-#else
-#error "utimensat and utimes syscalls not available."
+  ret = LIBC_NAMESPACE::syscall_impl<int>(UTIMES_SYSCALL_ID, AT_FDCWD, path,
+                                          ts_ptr, 0);
 #endif // SYS_utimensat
 
   if (ret < 0) {

Copy link

github-actions bot commented May 9, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Signed-off-by: Mikhail R. Gadelha <[email protected]>
Signed-off-by: Mikhail R. Gadelha <[email protected]>
@mikhailramalho mikhailramalho merged commit 36b4ffe into llvm:main May 13, 2025
4 of 5 checks passed
@mikhailramalho mikhailramalho deleted the libc-riscv-utimes branch May 13, 2025 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants