Skip to content

Commit 39d9932

Browse files
committed
[libc] Ensured proper formatting of files
1 parent f59b93c commit 39d9932

File tree

5 files changed

+63
-67
lines changed

5 files changed

+63
-67
lines changed

libc/src/sys/time/linux/utimes.cpp

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//===-- Linux implementation of utimes -------------------------------------===//
1+
//===-- Linux implementation of utimes ------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7-
//===----------------------------------------------------------------------===/
7+
//===----------------------------------------------------------------------===//
88

99
#include "src/sys/time/utimes.h"
1010

@@ -19,57 +19,58 @@
1919
#include <sys/syscall.h>
2020

2121
namespace LIBC_NAMESPACE_DECL {
22-
23-
LLVM_LIBC_FUNCTION(int, utimes, (const char *path, const struct timeval times[2])) {
24-
int ret;
22+
23+
LLVM_LIBC_FUNCTION(int, utimes,
24+
(const char *path, const struct timeval times[2])) {
25+
int ret;
2526

2627
#ifdef SYS_utimes
27-
// No need to define a timespec struct, use the syscall directly.
28-
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimes, path, times);
28+
// No need to define a timespec struct, use the syscall directly.
29+
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimes, path, times);
2930
#elif defined(SYS_utimensat)
30-
//the utimensat syscall requires a timespec struct, not timeval.
31-
struct timespec ts[2];
32-
struct timespec *ts_ptr = nullptr; // default value if times is NULL
33-
34-
// convert the microsec values in timeval struct times
35-
// to nanosecond values in timespec struct ts
36-
if (times != NULL) {
37-
38-
// ensure consistent values
39-
if ((times[0].tv_usec < 0 || times[1].tv_usec < 0) ||
40-
(times[0].tv_usec >= 1000000 || times[1].tv_usec >= 1000000)) {
41-
libc_errno = EINVAL;
42-
return -1;
43-
}
44-
45-
// set seconds in ts
46-
ts[0].tv_sec = times[0].tv_sec;
47-
ts[1].tv_sec = times[1].tv_sec;
48-
49-
// convert u-seconds to nanoseconds
50-
ts[0].tv_nsec = times[0].tv_usec * 1000;
51-
ts[1].tv_nsec = times[1].tv_usec * 1000;
52-
53-
ts_ptr = ts;
31+
// the utimensat syscall requires a timespec struct, not timeval.
32+
struct timespec ts[2];
33+
struct timespec *ts_ptr = nullptr; // default value if times is NULL
34+
35+
// convert the microsec values in timeval struct times
36+
// to nanosecond values in timespec struct ts
37+
if (times != NULL) {
38+
39+
// ensure consistent values
40+
if ((times[0].tv_usec < 0 || times[1].tv_usec < 0) ||
41+
(times[0].tv_usec >= 1000000 || times[1].tv_usec >= 1000000)) {
42+
libc_errno = EINVAL;
43+
return -1;
5444
}
55-
56-
// If times was NULL, ts_ptr remains NULL, which utimensat interprets
57-
// as setting times to the current time.
58-
59-
// utimensat syscall.
60-
// flags=0 means don't follow symlinks (like utimes)
61-
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimensat, AT_FDCWD, path,
62-
ts_ptr, 0);
45+
46+
// set seconds in ts
47+
ts[0].tv_sec = times[0].tv_sec;
48+
ts[1].tv_sec = times[1].tv_sec;
49+
50+
// convert u-seconds to nanoseconds
51+
ts[0].tv_nsec = times[0].tv_usec * 1000;
52+
ts[1].tv_nsec = times[1].tv_usec * 1000;
53+
54+
ts_ptr = ts;
55+
}
56+
57+
// If times was NULL, ts_ptr remains NULL, which utimensat interprets
58+
// as setting times to the current time.
59+
60+
// utimensat syscall.
61+
// flags=0 means don't follow symlinks (like utimes)
62+
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimensat, AT_FDCWD, path, ts_ptr,
63+
0);
6364

6465
#else
6566
#error "utimensat and utimes syscalls not available."
6667
#endif // SYS_utimensat
67-
68-
if (ret < 0) {
69-
libc_errno = -ret;
70-
return -1;
71-
}
72-
73-
return 0;
68+
69+
if (ret < 0) {
70+
libc_errno = -ret;
71+
return -1;
7472
}
73+
74+
return 0;
7575
}
76+
} // namespace LIBC_NAMESPACE_DECL

libc/src/sys/time/utimes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Implementation header for utimes -----------------------------------===//
1+
//===-- Implementation header for utimes ----------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -18,4 +18,4 @@ int utimes(const char *path, const struct timeval times[2]);
1818

1919
} // namespace LIBC_NAMESPACE_DECL
2020

21-
#endif // LLVM_LIBC_SRC_SYS_TIME_UTIMES_H
21+
#endif // LLVM_LIBC_SRC_SYS_TIME_UTIMES_H

libc/test/src/sys/time/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
add_custom_target(libc_sys_time_unittests)
22

3-
add_subdirectory(testdata)
4-
53
add_libc_unittest(
64
utimes_test
75
SUITE

libc/test/src/sys/time/testdata/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

libc/test/src/sys/time/utimes_test.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
//===-- Unittests for utimes
2-
//-----------------------------------------------===//
1+
//===-- Unittests for utimes ----------------------------------------------===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54
// See https://llvm.org/LICENSE.txt for license information.
65
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76
//
8-
//===-----------------------------------------------------------------------===//
7+
//===----------------------------------------------------------------------===//
98

109
#include "hdr/fcntl_macros.h"
1110
#include "hdr/types/struct_timeval.h"
@@ -15,15 +14,14 @@
1514
#include "src/sys/stat/stat.h"
1615
#include "src/sys/time/utimes.h"
1716
#include "src/unistd/close.h"
18-
#include "src/unistd/unlink.h"
1917
#include "test/UnitTest/ErrnoSetterMatcher.h"
2018
#include "test/UnitTest/Test.h"
21-
#include <fcntl.h>
19+
2220
constexpr const char *FILE_PATH = "utimes.test";
2321

24-
// SUCCESS: Takes a file and successfully updates
22+
// SUCCESS: Takes a file and successfully updates
2523
// its last access and modified times.
26-
TEST(LlvmLibcUtimesTest, ChangeTimesSpecific){
24+
TEST(LlvmLibcUtimesTest, ChangeTimesSpecific) {
2725
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2826

2927
// const char* FILE_PATH = "testdata/__utimes_changetimes.test";
@@ -39,7 +37,7 @@ TEST(LlvmLibcUtimesTest, ChangeTimesSpecific){
3937
times[0].tv_usec = 12345;
4038
times[1].tv_sec = 43210;
4139
times[1].tv_usec = 23456;
42-
40+
4341
// ensure utimes succeeds
4442
ASSERT_THAT(LIBC_NAMESPACE::utimes(FILE_PATH, times), Succeeds(0));
4543

@@ -51,7 +49,7 @@ TEST(LlvmLibcUtimesTest, ChangeTimesSpecific){
5149
ASSERT_EQ(statbuf.st_atim.tv_sec, times[0].tv_sec);
5250
ASSERT_EQ(statbuf.st_mtim.tv_sec, times[1].tv_sec);
5351

54-
//microseconds
52+
// microseconds
5553
ASSERT_EQ(statbuf.st_atim.tv_nsec, times[0].tv_usec * 1000);
5654
ASSERT_EQ(statbuf.st_mtim.tv_nsec, times[1].tv_usec * 1000);
5755

@@ -60,7 +58,7 @@ TEST(LlvmLibcUtimesTest, ChangeTimesSpecific){
6058

6159
// FAILURE: Invalid values in the timeval struct
6260
// to check that utimes rejects it.
63-
TEST(LlvmLibcUtimesTest, InvalidMicroseconds){
61+
TEST(LlvmLibcUtimesTest, InvalidMicroseconds) {
6462
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
6563
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
6664

@@ -71,25 +69,25 @@ TEST(LlvmLibcUtimesTest, InvalidMicroseconds){
7169
ASSERT_GT(fd, 0);
7270
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
7371

74-
// make a dummy timeval struct
72+
// make a dummy timeval struct
7573
// populated with bad usec values
7674
struct timeval times[2];
7775
times[0].tv_sec = 54321;
7876
times[0].tv_usec = 4567;
7977
times[1].tv_sec = 43210;
80-
times[1].tv_usec = 1000000; //invalid
81-
78+
times[1].tv_usec = 1000000; // invalid
79+
8280
// ensure utimes fails
8381
ASSERT_THAT(LIBC_NAMESPACE::utimes(FILE_PATH, times), Fails(EINVAL));
8482

85-
// check for failure on
83+
// check for failure on
8684
// the other possible bad values
8785

8886
times[0].tv_sec = 54321;
89-
times[0].tv_usec = -4567; //invalid
87+
times[0].tv_usec = -4567; // invalid
9088
times[1].tv_sec = 43210;
9189
times[1].tv_usec = 1000;
92-
90+
9391
// ensure utimes fails once more
9492
ASSERT_THAT(LIBC_NAMESPACE::utimes(FILE_PATH, times), Fails(EINVAL));
9593
ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_FILE), Succeeds(0));

0 commit comments

Comments
 (0)