[libc] [llvm] [libc] Implemented utimes (Issue #133953) (PR #134167)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 2 17:07:04 PDT 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- libc/src/sys/time/linux/utimes.cpp libc/src/sys/time/utimes.h libc/test/src/sys/time/utimes_test.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libc/src/sys/time/linux/utimes.cpp b/libc/src/sys/time/linux/utimes.cpp
index 12257f0f8..dee64eff0 100644
--- a/libc/src/sys/time/linux/utimes.cpp
+++ b/libc/src/sys/time/linux/utimes.cpp
@@ -1,4 +1,5 @@
-//===-- Linux implementation of utimes -------------------------------------===//
+//===-- Linux implementation of utimes
+//-------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -16,65 +17,66 @@
#include "src/errno/libc_errno.h"
#include <cerrno>
-#include <sys/syscall.h>
-#include <sys/stat.h>
#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
namespace LIBC_NAMESPACE_DECL {
-
- LLVM_LIBC_FUNCTION(int, utimes, (const char *path, const struct timeval times[2])) {
- int ret;
-
- #ifdef SYS_utimensat
- //the utimensat syscall requires a timespec struct, not timeval.
- struct timespec ts[2];
- struct timespec *ts_ptr = nullptr; // default value if times is NULL
-
- // convert the microsec values in timeval struct times
- // to nanosecond values in timespec struct ts
- if (times != NULL) {
-
- // ensure consistent values
- if ((times[0].tv_usec < 0 || times[1].tv_usec < 0) ||
- (times[0].tv_usec >= 1000000 || times[1].tv_usec >= 1000000)) {
- libc_errno = EINVAL;
- return -1;
- }
-
- // set seconds in ts
- ts[0].tv_sec = times[0].tv_sec;
- ts[1].tv_sec = times[1].tv_sec;
-
- // convert u-seconds to nanoseconds
- ts[0].tv_nsec = times[0].tv_usec * 1000;
- ts[1].tv_nsec = times[1].tv_usec * 1000;
-
- ts_ptr = ts;
- }
-
- // If times was NULL, ts_ptr remains NULL, which utimensat interprets
- // as setting times to the current time.
-
- // 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);
-
- #elif defined(SYS_utimes)
- // No need to define a timespec struct, use the syscall directly.
- ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimes, path, times);
- #else
- #error "utimensat and utimes syscalls not available."
- // To avoid compilation errors when neither is defined, return an error.
- libc_errno = ENOSYS; // Function not implemented
- return -1;
- #endif // SYS_utimensat
-
- if (ret < 0) {
- libc_errno = -ret;
+
+LLVM_LIBC_FUNCTION(int, utimes,
+ (const char *path, const struct timeval times[2])) {
+ int ret;
+
+#ifdef SYS_utimensat
+ // the utimensat syscall requires a timespec struct, not timeval.
+ struct timespec ts[2];
+ struct timespec *ts_ptr = nullptr; // default value if times is NULL
+
+ // convert the microsec values in timeval struct times
+ // to nanosecond values in timespec struct ts
+ if (times != NULL) {
+
+ // ensure consistent values
+ if ((times[0].tv_usec < 0 || times[1].tv_usec < 0) ||
+ (times[0].tv_usec >= 1000000 || times[1].tv_usec >= 1000000)) {
+ libc_errno = EINVAL;
return -1;
}
-
- return 0;
+
+ // set seconds in ts
+ ts[0].tv_sec = times[0].tv_sec;
+ ts[1].tv_sec = times[1].tv_sec;
+
+ // convert u-seconds to nanoseconds
+ ts[0].tv_nsec = times[0].tv_usec * 1000;
+ ts[1].tv_nsec = times[1].tv_usec * 1000;
+
+ ts_ptr = ts;
+ }
+
+ // If times was NULL, ts_ptr remains NULL, which utimensat interprets
+ // as setting times to the current time.
+
+ // 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);
+
+#elif defined(SYS_utimes)
+ // No need to define a timespec struct, use the syscall directly.
+ ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimes, path, times);
+#else
+#error "utimensat and utimes syscalls not available."
+ // To avoid compilation errors when neither is defined, return an error.
+ libc_errno = ENOSYS; // Function not implemented
+ return -1;
+#endif // SYS_utimensat
+
+ if (ret < 0) {
+ libc_errno = -ret;
+ return -1;
}
+
+ return 0;
}
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/time/utimes.h b/libc/src/sys/time/utimes.h
index 6dc33aae5..21813dda0 100644
--- a/libc/src/sys/time/utimes.h
+++ b/libc/src/sys/time/utimes.h
@@ -1,4 +1,5 @@
-//===-- Implementation header for utimes -----------------------------------===//
+//===-- Implementation header for utimes
+//-----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/sys/time/utimes_test.cpp b/libc/test/src/sys/time/utimes_test.cpp
index e644d5060..ce02c3617 100644
--- a/libc/test/src/sys/time/utimes_test.cpp
+++ b/libc/test/src/sys/time/utimes_test.cpp
@@ -8,74 +8,74 @@
// temp file related stuff
#include "src/fcntl/open.h" // to open
-#include "src/unistd/close.h" // to close
#include "src/sys/stat/stat.h" // for info
+#include "src/unistd/close.h" // to close
#include "src/unistd/unlink.h" // to delete
// testing error handling
-#include "test/UnitTest/Test.h"
-#include "src/errno/libc_errno.h"
+#include "src/errno/libc_errno.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
// dependencies for the tests themselves
+#include "hdr/fcntl_macros.h"
#include "hdr/types/struct_timeval.h"
#include <cerrno>
#include <fcntl.h>
-#include "hdr/fcntl_macros.h"
// the utimes function
-#include "src/sys/time/utimes.h"
-constexpr const char* TEST_FILE = "testdata/utimes.test";
+#include "src/sys/time/utimes.h"
+constexpr const char *TEST_FILE = "testdata/utimes.test";
-// SUCCESS: Takes a file and successfully updates
+// SUCCESS: Takes a file and successfully updates
// its last access and modified times.
-TEST(LlvmLibcUtimesTest, ChangeTimesSpecific){
+TEST(LlvmLibcUtimesTest, ChangeTimesSpecific) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
-
+
// make a dummy timeval struct
struct timeval times[2];
times[0].tv_sec = 54321;
times[0].tv_usec = 12345;
times[1].tv_sec = 43210;
times[1].tv_usec = 23456;
-
+
// ensure utimes succeeds
ASSERT_THAT(LIBC_NAMESPACE::utimes(TEST_FILE, times), Succeeds(0));
// verify the times values against stat of the TEST_FILE
struct stat statbuf;
ASSERT_EQ(stat(TEST_FILE, &statbuf), 0);
-
+
// seconds
ASSERT_EQ(statbuf.st_atim.tv_sec, times[0].tv_sec);
ASSERT_EQ(statbuf.st_mtim.tv_sec, times[1].tv_sec);
- //microseconds
+ // microseconds
ASSERT_EQ(statbuf.st_atim.tv_nsec, times[0].tv_usec * 1000);
- ASSERT_EQ(statbuf.st_mtim.tv_nsec, times[1].tv_usec * 1000);
+ ASSERT_EQ(statbuf.st_mtim.tv_nsec, times[1].tv_usec * 1000);
}
// FAILURE: Invalid values in the timeval struct
// to check that utimes rejects it.
-TEST(LlvmLibcUtimesTest, InvalidMicroseconds){
- using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
-
- // make a dummy timeval struct
+TEST(LlvmLibcUtimesTest, InvalidMicroseconds) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+
+ // make a dummy timeval struct
// populated with bad usec values
struct timeval times[2];
times[0].tv_sec = 54321;
times[0].tv_usec = 4567;
times[1].tv_sec = 43210;
- times[1].tv_usec = 1000000; //invalid
-
+ times[1].tv_usec = 1000000; // invalid
+
// ensure utimes fails
ASSERT_THAT(LIBC_NAMESPACE::utimes(TEST_FILE, times), Fails(EINVAL));
-
- // check for failure on
+
+ // check for failure on
// the other possible bad values
times[0].tv_sec = 54321;
- times[0].tv_usec = -4567; //invalid
+ times[0].tv_usec = -4567; // invalid
times[1].tv_sec = 43210;
times[1].tv_usec = 1000;
-
+
// ensure utimes fails once more
- ASSERT_THAT(LIBC_NAMESPACE::utimes(TEST_FILE, times), Fails(EINVAL));
+ ASSERT_THAT(LIBC_NAMESPACE::utimes(TEST_FILE, times), Fails(EINVAL));
}
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/134167
More information about the llvm-commits
mailing list