Skip to content

Commit 83206c7

Browse files
author
Зишан Мирза
committed
refactor: use openfile
1 parent f229f83 commit 83206c7

File tree

7 files changed

+25
-49
lines changed

7 files changed

+25
-49
lines changed

libc/src/time/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ add_entrypoint_object(
6060
.timezone
6161
libc.hdr.types.time_t
6262
libc.include.time
63+
libc.src.stdio.fopen
6364
)
6465

6566
add_entrypoint_object(
@@ -74,6 +75,7 @@ add_entrypoint_object(
7475
.timezone
7576
libc.hdr.types.time_t
7677
libc.include.time
78+
libc.src.stdio.fopen
7779
)
7880

7981
add_entrypoint_object(
@@ -88,6 +90,7 @@ add_entrypoint_object(
8890
.timezone
8991
libc.hdr.types.time_t
9092
libc.include.time
93+
libc.src.stdio.fopen
9194
)
9295

9396
add_entrypoint_object(
@@ -102,6 +105,7 @@ add_entrypoint_object(
102105
.timezone
103106
libc.hdr.types.time_t
104107
libc.include.time
108+
libc.src.stdio.fopen
105109
)
106110

107111
add_entrypoint_object(
@@ -116,6 +120,7 @@ add_entrypoint_object(
116120
.timezone
117121
libc.hdr.types.time_t
118122
libc.include.time
123+
libc.src.stdio.fopen
119124
)
120125

121126
add_entrypoint_object(

libc/src/time/ctime.cpp

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

libc/src/time/time_utils.cpp

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

99
#include "src/time/time_utils.h"
10+
#include "src/__support/File/file.h"
11+
#include "src/stdio/fseek.h"
1012
#include <fcntl.h>
1113
#include <unistd.h>
1214

@@ -26,24 +28,21 @@ static int64_t computeRemainingYears(int64_t daysPerYears,
2628
}
2729

2830
volatile int file_usage = 0;
29-
volatile int fd = -1;
3031

31-
void release_file(int fd) {
32+
void release_file(ErrorOr<File *> error_or_file) {
3233
file_usage = 0;
33-
close(fd);
34+
error_or_file.value()->close();
3435
}
3536

36-
void acquire_file(char *filename) {
37+
ErrorOr<File *> acquire_file(char *filename) {
3738
while (1) {
3839
if (file_usage == 0) {
3940
file_usage = 1;
4041
break;
4142
}
4243
}
4344

44-
if ((fd = open(filename, O_RDONLY)) < 0) {
45-
release_file(fd);
46-
}
45+
return LIBC_NAMESPACE::openfile(filename, "rb");
4746
}
4847

4948
char *get_env_var(const char *input) {
@@ -214,18 +213,12 @@ timezone::tzset *get_localtime(struct tm *tm) {
214213
}
215214
}
216215

217-
acquire_file(tz_filename);
218-
219-
size_t filesize;
220-
filesize = static_cast<size_t>(lseek(fd, 0, SEEK_END));
221-
if (filesize < 0) {
222-
close(fd);
223-
return nullptr;
224-
}
225-
lseek(fd, 0, 0);
216+
ErrorOr<File *> error_or_file = acquire_file(tz_filename);
217+
File *file = error_or_file.value();
226218

227-
timezone::tzset *ptr_tzset = timezone::get_tzset(fd, filesize);
219+
timezone::tzset *ptr_tzset = timezone::get_tzset(file);
228220
if (ptr_tzset == nullptr) {
221+
release_file(file);
229222
return nullptr;
230223
}
231224

@@ -239,7 +232,7 @@ timezone::tzset *get_localtime(struct tm *tm) {
239232
}
240233

241234
if (file_usage == 1) {
242-
release_file(fd);
235+
release_file(file);
243236
}
244237

245238
return ptr_tzset;

libc/src/time/time_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ LIBC_INLINE struct tm *localtime_internal(const time_t *timer, struct tm *buf) {
174174
#ifdef LIBC_TARGET_ARCH_IS_X86_64
175175
timezone::tzset *ptr = get_localtime(buf);
176176
buf->tm_hour += ptr->global_offset;
177-
buf->tm_isdst += ptr->global_isdst;
177+
buf->tm_isdst = ptr->global_isdst;
178178
#endif
179179

180180
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 "test/UnitTest/Test.h"

0 commit comments

Comments
 (0)