Skip to content

Commit e1adf97

Browse files
author
Зишан Мирза
committed
refactor: use openfile
1 parent 3fcbe48 commit e1adf97

File tree

7 files changed

+25
-54
lines changed

7 files changed

+25
-54
lines changed

libc/src/time/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ add_entrypoint_object(
8080
.timezone
8181
libc.hdr.types.time_t
8282
libc.include.time
83+
libc.src.stdio.fopen
8384
)
8485

8586
add_entrypoint_object(
@@ -95,6 +96,7 @@ add_entrypoint_object(
9596
.timezone
9697
libc.hdr.types.time_t
9798
libc.include.time
99+
libc.src.stdio.fopen
98100
)
99101

100102
add_entrypoint_object(
@@ -109,6 +111,7 @@ add_entrypoint_object(
109111
.timezone
110112
libc.hdr.types.time_t
111113
libc.include.time
114+
libc.src.stdio.fopen
112115
)
113116

114117
add_entrypoint_object(
@@ -123,6 +126,7 @@ add_entrypoint_object(
123126
.timezone
124127
libc.hdr.types.time_t
125128
libc.include.time
129+
libc.src.stdio.fopen
126130
)
127131

128132
add_entrypoint_object(
@@ -137,6 +141,7 @@ add_entrypoint_object(
137141
.timezone
138142
libc.hdr.types.time_t
139143
libc.include.time
144+
libc.src.stdio.fopen
140145
)
141146

142147
add_entrypoint_object(

libc/src/time/ctime.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ LLVM_LIBC_FUNCTION(char *, ctime, (const time_t *t_ptr)) {
2020
if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
2121
return nullptr;
2222
}
23+
2324
static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
2425
return time_utils::asctime(time_utils::localtime_internal(t_ptr, &tm_out),
2526
buffer, TimeConstants::ASCTIME_MAX_BYTES);

libc/src/time/time_utils.cpp

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/time/time_utils.h"
10-
#include "src/__support/CPP/limits.h" // INT_MIN, INT_MAX
11-
#include "src/__support/CPP/string_view.h"
12-
#include "src/__support/common.h"
13-
#include "src/__support/macros/config.h"
14-
#include "src/time/timezone.h"
10+
#include "src/__support/File/file.h"
11+
#include "src/stdio/fseek.h"
1512

1613
namespace LIBC_NAMESPACE_DECL {
1714
namespace time_utils {
@@ -27,24 +24,21 @@ static int64_t computeRemainingYears(int64_t daysPerYears,
2724
}
2825

2926
volatile int file_usage = 0;
30-
volatile int fd = -1;
3127

32-
void release_file(int fd) {
28+
void release_file(ErrorOr<File *> error_or_file) {
3329
file_usage = 0;
34-
close(fd);
30+
error_or_file.value()->close();
3531
}
3632

37-
void acquire_file(char *filename) {
33+
ErrorOr<File *> acquire_file(char *filename) {
3834
while (1) {
3935
if (file_usage == 0) {
4036
file_usage = 1;
4137
break;
4238
}
4339
}
4440

45-
if ((fd = open(filename, O_RDONLY)) < 0) {
46-
release_file(fd);
47-
}
41+
return LIBC_NAMESPACE::openfile(filename, "rb");
4842
}
4943

5044
char *get_env_var(const char *input) {
@@ -231,18 +225,12 @@ timezone::tzset *get_localtime(struct tm *tm) {
231225
}
232226
}
233227

234-
acquire_file(tz_filename);
235-
236-
size_t filesize;
237-
filesize = static_cast<size_t>(lseek(fd, 0, SEEK_END));
238-
if (filesize < 0) {
239-
close(fd);
240-
return nullptr;
241-
}
242-
lseek(fd, 0, 0);
228+
ErrorOr<File *> error_or_file = acquire_file(tz_filename);
229+
File *file = error_or_file.value();
243230

244-
timezone::tzset *ptr_tzset = timezone::get_tzset(fd, filesize);
231+
timezone::tzset *ptr_tzset = timezone::get_tzset(file);
245232
if (ptr_tzset == nullptr) {
233+
release_file(file);
246234
return nullptr;
247235
}
248236

@@ -256,7 +244,7 @@ timezone::tzset *get_localtime(struct tm *tm) {
256244
}
257245

258246
if (file_usage == 1) {
259-
release_file(fd);
247+
release_file(file);
260248
}
261249

262250
return ptr_tzset;

libc/src/time/time_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ LIBC_INLINE struct tm *localtime_internal(const time_t *timer, struct tm *buf) {
165165
#ifdef LIBC_TARGET_ARCH_IS_X86_64
166166
timezone::tzset *ptr = get_localtime(buf);
167167
buf->tm_hour += ptr->global_offset;
168-
buf->tm_isdst += ptr->global_isdst;
168+
buf->tm_isdst = ptr->global_isdst;
169169
#endif
170170

171171
return buf;

libc/src/time/timezone.cpp

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,8 @@
1717
namespace LIBC_NAMESPACE_DECL {
1818
namespace timezone {
1919

20-
tzset *get_tzset(int fd, size_t filesize) {
21-
unsigned char hdr[filesize];
22-
size_t t;
23-
24-
t = 0;
25-
while (t < sizeof(hdr)) {
26-
size_t r;
27-
28-
r = read(fd, hdr + t, sizeof(hdr) - t);
29-
30-
if (r < 0) {
31-
close(fd);
32-
return nullptr;
33-
}
34-
35-
if (r == 0) {
36-
break;
37-
}
38-
39-
t += r;
40-
}
41-
42-
if (t != sizeof(hdr)) {
43-
close(fd);
44-
return nullptr;
45-
}
46-
20+
tzset *get_tzset(File *file) {
21+
unsigned char hdr[TIMEZONE_HDR_SIZE + 4096];
4722
int64_t magic;
4823
unsigned char version;
4924
__int128_t reserved;
@@ -56,6 +31,8 @@ tzset *get_tzset(int fd, size_t filesize) {
5631
__uint128_t tmp;
5732
size_t i;
5833

34+
file->read(hdr, TIMEZONE_HDR_SIZE + 4096);
35+
5936
// these locations are defined in documentation
6037
// for `tzfile` and should be 44 bytes
6138
magic = (hdr[0] << 24) | (hdr[1] << 16) | (hdr[2] << 8) | hdr[3];
@@ -211,8 +188,6 @@ tzset *get_tzset(int fd, size_t filesize) {
211188

212189
result.ttinfo = ttinfo;
213190

214-
close(fd);
215-
216191
return &result;
217192
}
218193

libc/src/time/timezone.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SRC_TIME_TIMEZONE_H
1010
#define LLVM_LIBC_SRC_TIME_TIMEZONE_H
1111

12+
#include "src/__support/File/file.h"
1213
#include "src/__support/common.h"
1314
#include "src/__support/macros/config.h"
1415
#include "stddef.h"
@@ -48,7 +49,7 @@ typedef struct {
4849
int8_t global_isdst;
4950
} tzset;
5051

51-
tzset *get_tzset(int fd, size_t filesize);
52+
tzset *get_tzset(File *file);
5253

5354
} // namespace timezone
5455
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/time/ctime_r_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/File/file.h"
910
#include "src/errno/libc_errno.h"
1011
#include "src/time/ctime_r.h"
1112
#include "src/time/time_constants.h"

0 commit comments

Comments
 (0)