-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFILE.h
322 lines (268 loc) · 7.96 KB
/
FILE.h
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#ifndef LIBRDX_FILE_H
#define LIBRDX_FILE_H
#include "01.h"
#include "BUF.h"
#include "OK.h"
#include "PRO.h"
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#endif
#define __USE_XOPEN_EXTENDED 1
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/mman.h>
#include <sys/uio.h>
#include <unistd.h>
#include "ftw.h"
con ok64 FILEagain = 0xf4953a5ae5b72;
con ok64 FILEerror = 0xf4953a9db6cf6;
con ok64 FILEbadarg = 0x3d254e9a5a25dab;
con ok64 FILEfail = 0x3d254eaa5b70;
con ok64 FILEnosync = 0x3d254ecb3dfdca7;
con ok64 FILEnoopen = 0x3d254ecb3cf4a72;
con ok64 FILEnoclse = 0x3d254ecb39f0de9;
con ok64 FILEnostat = 0x3d254ecb3df8978;
con ok64 FILEwrong = 0xf4953bbdb3cab;
con ok64 FILEnoresz = 0x3d254ecb3da9dfe;
con ok64 FILEend = 0xf4953a9ca8;
con ok64 FILEnone = 0x3d254ecb3ca9;
con ok64 FILEaccess = 0x3d254e9679e9df7;
con ok64 FILEloop = 0x3d254ec33cf4;
con ok64 FILEname = 0x3d254eca5c69;
con ok64 FILEbad = 0xf4953a6968;
#define FILEok(fd) (fd >= 0)
/*
typedef int *FILE;
#define aFILE(name) \
int _##name = FILE_CLOSED; \
FILE name = &_##name;
*/
#ifdef IOV_MAX
#define FILEmaxiov IOV_MAX
#elif defined __IOV_MAX
#define FILEmaxiov __IOV_MAX
#else
#define FILEmaxiov 1024
#endif
#define FILE_CLOSED -1
#ifndef MAP_FILE
#define MAP_FILE 0
#endif
typedef u8 const *path[2];
fun ok64 FILEerrno() {
switch (errno) {
case EAGAIN:
return FILEagain;
default:
return FILEerror;
}
}
con size_t FILEmaxpathlen = 1024;
#define aFILEpath(n, p) \
char n[1024]; \
{ \
size_t sz = $size(p); \
test(sz < FILEmaxpathlen, FILEbadarg); \
memcpy(n, *p, sz); \
n[sz] = 0; \
}
ok64 FILEcreate(int *fd, const path name);
ok64 FILEopen(int *fd, const path name, int flags);
ok64 FILEopenat(int *fd, int const *dirfd, const path name, int flags);
ok64 FILEsync(int const *fd);
ok64 FILEclose(int *fd);
ok64 FILEstat(struct stat *ret, const path name);
ok64 FILEsize(size_t *size, int const *fd);
ok64 FILEisdir(const path name);
ok64 FILEresize(int const *fd, size_t new_size);
ok64 FILErename(const path oldname, const path newname);
// Drains the data to the file; if the slice is non empty on return, see errno!
fun ok64 FILEfeed(int fd, u8 const **data) {
ssize_t re = write(fd, *data, $size(data));
if (re <= 0) return FILEfail;
*data += re;
return OK;
}
fun int FILE2iovec(struct iovec *io, $$u8c datav) {
int l = 0;
while (l < $len(datav) && l < FILEmaxiov) {
u8c$ data = $at(datav, l);
io[l].iov_base = (void *)data[0];
io[l].iov_len = $len(data);
++l;
}
return l;
}
fun void $$u8cdrained($$u8c datav, size_t re) {
while (re > 0 && !$empty(datav)) {
u8c$ data = $at(datav, 0);
if (re < $len(data)) {
data[0] += re;
re = 0;
} else {
re -= $len(data);
++datav[0];
}
}
}
fun ok64 FILEfeedv(int fd, $$u8c datav) {
struct iovec io[FILEmaxiov];
int l = FILE2iovec(io, datav);
ssize_t re = writev(fd, io, l);
if (re <= 0) return FILEfail;
$$u8cdrained(datav, re);
return OK;
}
fun ok64 FILEfeedall(int fd, uint8_t const *const *data) {
if (!FILEok(fd) || !$ok(data)) return FILEbadarg;
a$dup(u8 const, d, data);
ok64 ret = OK;
while (!$empty(d) && OK == ret) {
ret = FILEfeed(fd, d);
}
return ret;
}
fun ok64 FILEdrain(u8 **into, int fd) {
ssize_t ret = read(fd, *into, $size(into));
if (ret <= 0) {
if (ret == 0) return FILEend;
return FILEfail; // TODO
}
*into += ret;
return OK;
}
fun ok64 FILEdrainv($$u8 datav, int fd) {
struct iovec io[FILEmaxiov];
int l = FILE2iovec(io, ($u8c$)datav);
ssize_t re = readv(fd, io, l);
if (re <= 0) return FILEfail;
$$u8cdrained(($u8c$)datav, re);
return OK;
}
fun ok64 FILEdrainall(u8 **into, int fd) {
ok64 o;
do {
o = FILEdrain(into, fd);
} while ($len(into) > 0 && o == OK);
if (o == FILEend) o = OK;
return o;
}
/*
fun proc Fpwrite1(int fd, path data, size_t offset) {
args(fd >= 0 && offset >= 0, "%i %p %lu", fd, data, offset);
size_t wn = pwrite(fd, *data, $size(data), offset);
callcv(wn >= 0, FFAILWRIT0);
data[$HEAD] += wn;
done;
}
fun proc Fpwrite(int fd, path data, size_t offset) {
args(fd >= 0 && offset >= 0, "%i %p %lu", fd, data, offset);
size_t eoff = offset + $size(data);
while (!$empty(data)) {
call(Fpwrite1(fd, data, eoff - $size(data)));
}
done;
}
fun proc Fpread1(int fd, path into, size_t offset) {
args(fd >= 0 && offset >= 0, "%i %p %lu", fd, into, offset);
size_t wn = pread(fd, *into, $size(into), (off_t)offset);
callcv(wn >= 0, FFAILREAD0);
into[$HEAD] += wn;
done;
}
fun proc Fpread(int fd, path into, size_t offset) {
args(fd >= 0 && offset >= 0, "%i %p %lu", fd, into, offset);
size_t eoff = offset + $size(into);
while (!$empty(into)) {
call(Fpread1(fd, into, eoff - $size(into)));
}
done;
}
*/
fun ok64 FILEmakedir(path const name) {
sane($ok(name));
aFILEpath(p, name);
int rc = mkdir(p, S_IRWXU);
testc(rc == 0, FILEfail);
done;
}
ok64 FILErmrf(path const name);
fun pro(FILEunlink, path const name) {
sane($ok(name));
aFILEpath(p, name);
int rc = unlink(p);
testc(rc == 0, FILEfail);
done;
}
fun int flags2prot(int flags) {
int prot = PROT_READ;
if ((flags & O_RDWR) == O_RDWR) prot |= PROT_WRITE;
return prot;
}
ok64 FILEmap(Bu8 buf, int const *fd, int mode);
// Memory-map a file for reading.
fun ok64 FILEmapro2(Bu8 buf, int *fd) { return FILEmap(buf, fd, PROT_READ); }
fun ok64 FILEmapro(Bu8 buf, $cu8c path) {
if (buf == nil || Bok(buf) || !$ok(path)) return FILEbadarg;
int fd = FILE_CLOSED;
ok64 o = FILEopen(&fd, path, O_RDONLY);
if (o == OK) {
o = FILEmap(buf, &fd, PROT_READ);
FILEclose(&fd);
}
return o;
}
// Memory-map a file for reading and writing.
fun ok64 FILEmaprw(Bu8 buf, int *fd, $cu8c path) {
if (buf == nil || Bok(buf) || !$ok(path) || fd == nil) return FILEbadarg;
ok64 o = FILEopen(fd, path, O_RDWR);
if (o == OK) o = FILEmap(buf, fd, PROT_READ | PROT_WRITE);
if (o != OK) FILEclose(fd);
return o;
}
// Memory-map a file for reading and writing.
fun ok64 FILEmapnew(Bu8 buf, int *fd, $cu8c path, size_t size) {
if (buf == nil || Bok(buf) || !$ok(path) || fd == nil) return FILEbadarg;
ok64 o = FILEcreate(fd, path);
if (o == OK) o = FILEresize(fd, size);
if (o == OK) o = FILEmap(buf, fd, PROT_READ | PROT_WRITE);
if (o != OK) FILEclose(fd);
return o;
}
// Unmaps the buffer.
ok64 FILEunmap(Bu8 buf);
// Resize the file and update the mapping.
fun ok64 FILEremap(Bu8 buf, int const *fd, size_t new_size) {
if (!Bok(buf) || fd == nil) return FILEbadarg;
ok64 o = FILEunmap(buf);
if (o == OK) o = FILEresize(fd, new_size);
if (o == OK) o = FILEmap(buf, fd, PROT_WRITE | PROT_READ);
// TODO mremap()
return o;
}
// Extend the mapped file by 1/4
fun ok64 FILEremap125(Bu8 buf, int const *fd) {
size_t new_size = roundup(Bsize(buf) * 5 / 4, PAGESIZE);
return FILEremap(buf, fd, new_size);
}
fun ok64 FILEout(u8 const *const *txt) {
a$dup(u8 const, dup, txt);
return FILEfeedall(STDOUT_FILENO, dup);
}
fun ok64 FILEerr(u8 const *const *txt) {
a$dup(u8 const, dup, txt);
return FILEfeedall(STDERR_FILENO, dup);
}
static u8 _NL[2] = {'\n', 0};
con u8 *const NL[2] = {_NL, _NL + 1};
// todo buffered print
#define $print FILEout
#define $println(s) FILEout(s), FILEout(NL)
#define FILEfeedf(fd, fmt, ...) \
{ \
aBpad(u8, _pad, PAGESIZE); \
$feedf(Bu8idle(_pad), fmt, __VA_ARGS__); \
FILEfeed(fd, Bu8cdata(_pad)); \
}
#endif // ABC_F_H