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
|
#include <camellia/syscalls.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <user/lib/fs/dir.h>
void dir_start(struct dirbuild *db, long offset, char *buf, size_t buflen) {
db->offset = offset;
db->buf = buf;
db->bpos = 0;
db->blen = buflen;
db->error = 0;
if (offset < 0)
db->error = -ENOSYS; // TODO
}
bool dir_append(struct dirbuild *db, const char *name) {
return dir_appendl(db, name, strlen(name));
}
bool dir_appendl(struct dirbuild *db, const char *name, size_t len) {
if (db->error) return true;
if (len > (size_t)LONG_MAX) {
db->error = -1;
return true;
}
len++; // account for the null byte
if (db->offset < (long)len) {
name += db->offset;
len -= db->offset;
db->offset = 0;
// TODO no buffer overrun check
memcpy(db->buf + db->bpos, name, len - 1);
db->buf[db->bpos + len - 1] = '\0';
db->bpos += len;
} else {
db->offset -= len;
}
return false;
}
bool dir_append_from(struct dirbuild *db, handle_t h) {
if (db->error) return true;
if (db->bpos == db->blen)
return false;
int ret = _syscall_read(h, db->buf + db->bpos, db->blen - db->bpos, db->offset);
if (ret < 0) {
db->error = ret;
return true;
}
if (ret == 0) {
// TODO no idea how much we've overread
db->error = -ENOSYS;
return true;
}
// TODO deduplicate
db->offset = 0;
db->bpos += ret;
return false;
}
long dir_finish(struct dirbuild *db) {
return db->error ? db->error : db->bpos;
}
|