-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.c
288 lines (222 loc) · 4.46 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#ifdef __APPLE__
#include <sys/disk.h>
#endif
#ifdef __linux__
#include <linux/fs.h>
#endif
#include "utils.h"
static int blockdevice_size(int fd, off_t *size_ptr)
#if defined(__APPLE__)
{
uint32_t size;
uint64_t count;
if (ioctl(fd, DKIOCGETBLOCKSIZE, &size) < 0) {
u_log_errno("getting device block size failed");
return -1;
}
if (ioctl(fd, DKIOCGETBLOCKCOUNT, &count) < 0) {
u_log_errno("getting device block count failed");
return -1;
}
*size_ptr = (off_t)(size * count);
return 0;
}
#elif defined(__linux__)
{
uint64_t tmp;
if (ioctl(fd, BLKGETSIZE64, &tmp) < 0) {
u_log_errno("getting block device size failed");
return -1;
}
*size_ptr = (off_t)tmp;
return 0;
}
#else
#error "blockdevice_size not implemented for target"
#endif
must_check int readall(int fd, uint8_t *buf, size_t len)
{
size_t read_bytes = 0;
while (read_bytes < len) {
ssize_t ret;
ret = read(fd, &buf[read_bytes], len - read_bytes);
if (ret < 0) {
if (errno == EINTR)
continue;
u_log_errno("read failed");
return -1;
} else if (ret == 0) {
u_log(ERR, "short read, expected %zu, got %zu", len, read_bytes);
return -1;
}
read_bytes += ret;
}
return 0;
}
must_check int preadall(int fd, uint8_t *buf, size_t len, off_t offset)
{
size_t read_bytes = 0;
while (read_bytes < len) {
ssize_t ret;
ret = pread(fd, &buf[read_bytes], len - read_bytes, offset);
if (ret < 0) {
if (errno == EINTR)
continue;
u_log_errno("read failed");
return -1;
} else if (ret == 0) {
u_log(ERR, "short read, expected %zu, got %zu", len, read_bytes);
return -1;
}
read_bytes += ret;
offset += ret;
}
return 0;
}
must_check int writeall(int fd, const uint8_t *buf, size_t len)
{
size_t written_bytes = 0;
while (written_bytes < len) {
ssize_t ret;
ret = write(fd, &buf[written_bytes], len - written_bytes);
if (ret < 0) {
if (errno == EINTR)
continue;
u_log_errno("write failed");
return -1;
}
u_assert(ret != 0);
written_bytes += ret;
}
return 0;
}
must_check int pwriteall(int fd, const uint8_t *buf, size_t len, off_t offset)
{
size_t written_bytes = 0;
while (written_bytes < len) {
ssize_t ret;
ret = pwrite(fd, &buf[written_bytes], len - written_bytes, offset);
if (ret < 0) {
if (errno == EINTR)
continue;
u_log_errno("write failed");
return -1;
}
u_assert(ret != 0);
written_bytes += ret;
offset += ret;
}
return 0;
}
int fd_size(int fd, off_t *size_out)
{
u_assert(fd >= 0);
u_assert(size_out);
struct stat s;
if (fstat(fd, &s) < 0) {
u_log_errno("stat failed");
return -1;
}
if (S_ISREG(s.st_mode)) {
*size_out = s.st_size;
} else if (S_ISBLK(s.st_mode)) {
return blockdevice_size(fd, size_out);
} else {
u_log(ERR, "unsupported file type: 0%o", s.st_mode & S_IFMT);
return -1;
}
return 0;
}
void *slurp_file(const char *path, bool text, off_t *size_out)
{
int fd;
void *ret = NULL;
fd = open(path, O_RDONLY);
if (fd < 0) {
u_log_errno("opening '%s' failed", path);
return NULL;
}
off_t size;
if (fd_size(fd, &size) < 0) {
u_log(ERR, "getting file size of '%s' failed", path);
goto out_fd;
}
void *buf;
buf = malloc(text ? size + 1 : size);
if (!buf) {
u_log_errno("could not allocate %zu bytes", (size_t)size);
goto out_fd;
}
if (readall(fd, buf, size) < 0) {
u_log(ERR, "reading file '%s' failed", path);
goto out_buf;
}
if (text)
((char*)buf)[size] = 0;
if (size_out)
*size_out = size;
ret = buf;
out_buf:
if (ret == NULL)
free(buf);
out_fd:
close(fd);
return ret;
}
time_t time_monotonic(void)
{
struct timespec ts;
u_assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
return ts.tv_sec;
}
static int16_t nibble(char c)
{
if ('0' <= c && c <= '9')
return c - '0';
c |= 0x20;
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
return -1;
}
int parse_hex(uint8_t *out, const char *in)
{
for (size_t i = 0; in[i]; i++) {
int16_t n = nibble(in[i]);
if (n < 0) {
u_log(ERR, "invalid hex nibble '%c'", in[i]);
return -1;
}
if (i % 2 == 0) {
*out = (uint8_t)n << 4;
} else {
*out |= (uint8_t)n;
out++;
}
}
return 0;
}
void chomp(char *s)
{
char *p, *q;
q = NULL;
p = s;
while (*p) {
if (*p == '\r' || *p == '\n') {
q = p;
while (*p == '\r' || *p == '\n')
p++;
} else {
p++;
}
}
if (q)
*q = 0;
}