-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultfs.c
349 lines (271 loc) · 7.6 KB
/
defaultfs.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "log.h"
#include "defaultfs.h"
#include "elfs.h"
telf_ctx *ctx;
/* file */
static telf_status
defaultfs_getattr(void *obj_hdl,
telf_stat *st)
{
telf_status ret;
telf_status rc;
telf_obj *obj = obj_hdl;
elf_obj_lock(obj);
DEBUG("name:%s data=%p", obj->name, obj->data);
memcpy(st, &obj->st, sizeof *st);
st->st_nlink = 1;
ret = ELF_SUCCESS;
end:
elf_obj_unlock(obj);
DEBUG("ret=%s (%d)", elf_status_to_str(ret), ret);
return ret;
}
static telf_status
defaultfs_open(char *path,
telf_open_flags flags,
void **objp)
{
telf_status ret;
telf_status rc;
telf_obj *obj;
char *buf;
size_t buf_len;
telf_default_content *content;
int locked = 0;
rc = elf_namei(ctx, path, &obj);
if (ELF_SUCCESS != rc) {
ret = rc;
goto end;
}
elf_obj_lock(obj);
locked = 1;
DEBUG("name:%s data=%p", obj->name, obj->data);
content = malloc(sizeof *content);
if (! content) {
ERR("malloc: %s", strerror(errno));
ret = ELF_ENOMEM;
goto end;
}
rc = obj->fill_func(obj, &content->buf, &content->buf_len);
if (ELF_SUCCESS != rc) {
ret = rc;
goto end;
}
if (obj->data)
obj->free_func(obj->data);
obj->data = content;
ret = ELF_SUCCESS;
end:
if (locked)
elf_obj_unlock(obj);
if (objp)
*objp = obj;
DEBUG("obj->data=%p, ret=%s (%d)",
obj->data, elf_status_to_str(ret), ret);
return ret;
}
static telf_status
defaultfs_release(void *obj_hdl)
{
telf_obj *obj = obj_hdl;
elf_obj_lock(obj);
DEBUG("name:%s data=%p", obj->name, obj->data);
if (obj->free_func) {
obj->free_func(obj->data);
obj->data = NULL;
}
elf_obj_unlock(obj);
return ELF_SUCCESS;
}
static telf_status
defaultfs_read(void *obj_hdl,
char *buf,
size_t size,
off_t offset,
ssize_t *sizep)
{
telf_obj *obj = obj_hdl;
telf_default_content *content = NULL;
telf_status ret;
telf_status rc;
elf_obj_lock(obj);
DEBUG("name:%s data=%p", obj->name, obj->data);
content = obj->data;
/* FUSE might release() the object before read(), true story bro' */
if (! content) {
content = malloc(sizeof *content);
if (! content) {
ERR("malloc: %s", strerror(errno));
ret = ELF_ENOMEM;
goto end;
}
memset(content, 0, sizeof *content);
rc = obj->fill_func(obj, &content->buf, &content->buf_len);
if (ELF_SUCCESS != rc) {
ret = rc;
goto end;
}
obj->data = content;
}
if (size > content->buf_len)
size = content->buf_len;
memcpy(buf, content->buf + offset, size);
ret = ELF_SUCCESS;
end:
if (sizep)
*sizep = size;
elf_obj_unlock(obj);
DEBUG("ret=%s (%d)", elf_status_to_str(ret), ret);
return ret;
}
static telf_status
defaultfs_write(void *obj,
const char *buf,
size_t size,
off_t offset,
ssize_t *sizep)
{
return ELF_SUCCESS;
}
/* directory */
static telf_status
defaultfs_opendir(char *path,
void **objp)
{
return ELF_SUCCESS;
}
typedef struct {
char name[128]; /* section/segment name */
} telf_dirent;
typedef struct elf_dir_hdl {
void *(*get_entryname_func)(struct elf_dir_hdl *, char **);
telf_ctx *ctx;
telf_obj *obj;
int cursor;
int n_entries;
} telf_dir_hdl;
static void *
direntname(telf_dir_hdl *dir_hdl,
char **namep)
{
char *name = NULL;
telf_obj *entry = NULL;
static char *dots[] = { ".", ".." };
switch (dir_hdl->cursor) {
case 0: /* handle "." */
entry = dir_hdl->obj;
name = dots[dir_hdl->cursor];
break;
case 1: /* handle ".." */
entry = dir_hdl->obj->parent;
name = dots[dir_hdl->cursor];
break;
default: /* handle ordinary entry... */
entry = list_get_nth(dir_hdl->obj->entries, dir_hdl->cursor - 2);
if (! entry)
goto end;
name = entry->name;
}
end:
if (namep)
*namep = name;
return entry;
}
static int
dir_ctor(telf_ctx *ctx,
telf_obj *obj,
telf_dir_hdl *dir)
{
dir->ctx = ctx;
dir->cursor = 0;
dir->obj = obj;
dir->n_entries = list_get_size(obj->entries) + 2; // for "." and ".."
dir->get_entryname_func = direntname;
}
static int
readdir_getdirent(void *hdl,
telf_dirent *dirent)
{
telf_dir_hdl *dir_hdl = hdl;
char *name = NULL;
void *addr = NULL;
if (dir_hdl->cursor >= dir_hdl->n_entries + 2)
return -1;
addr = dir_hdl->get_entryname_func(dir_hdl, &name);
if (! name)
return -1;
if (*name)
sprintf(dirent->name, "%s", name);
else
sprintf(dirent->name, "noname.%p", addr);
dir_hdl->cursor++;
return ELF_SUCCESS;
}
static telf_status
defaultfs_readdir(void *obj_hdl,
void *data,
fuse_fill_dir_t fill)
{
telf_obj *obj = obj_hdl;
telf_status ret;
int rc;
telf_dir_hdl *dir_hdl = NULL;
telf_dirent dirent;
int locked = 0;
DEBUG("%s", obj->name);
dir_hdl = alloca(sizeof *dir_hdl);
if (! dir_hdl) {
ERR("alloca: %s", strerror(errno));
ret = ELF_ENOMEM;
goto err;
}
memset(&dirent, 0, sizeof dirent);
elf_obj_lock(obj);
locked = 1;
dir_ctor(ctx, obj, dir_hdl);
while (0 == readdir_getdirent(dir_hdl, &dirent)) {
if (fill(data, dirent.name, NULL, 0))
break;
}
ret = ELF_SUCCESS;
err:
if (locked)
elf_obj_unlock(obj);
return ret;
}
static telf_status
defaultfs_releasedir(void *obj)
{
return ELF_SUCCESS;
}
static telf_status
defaultfs_readlink(void *obj,
char **bufp,
size_t *buf_lenp)
{
return ELF_SUCCESS;
}
telf_fs_driver *
defaultfs_driver_new(void)
{
telf_fs_driver *driver = NULL;
driver = malloc(sizeof *driver);
if (! driver) {
ERR("malloc: %s", strerror(errno));
return NULL;
}
driver->data = NULL;
driver->getattr = defaultfs_getattr;
driver->open = defaultfs_open;
driver->release = defaultfs_release;
driver->read = defaultfs_read;
driver->write = defaultfs_write;
driver->opendir = defaultfs_opendir;
driver->readdir = defaultfs_readdir;
driver->releasedir = defaultfs_releasedir;
driver->readlink = defaultfs_readlink;
return driver;
}