diff options
author | dzwdz | 2022-07-30 20:18:51 +0200 |
---|---|---|
committer | dzwdz | 2022-07-30 20:18:51 +0200 |
commit | fdeac40f867ca0c43ade53cce4b77bb146b10aae (patch) | |
tree | 9845e8708cb12d7e27a633d37a83b40eab7ea396 /src/user/app/init/driver/tmpfs.c | |
parent | b26399ad055aae9ef4b01694967515235568cd85 (diff) |
user/fs: create a shared library for handling directory reads
Diffstat (limited to 'src/user/app/init/driver/tmpfs.c')
-rw-r--r-- | src/user/app/init/driver/tmpfs.c | 30 |
1 files changed, 8 insertions, 22 deletions
diff --git a/src/user/app/init/driver/tmpfs.c b/src/user/app/init/driver/tmpfs.c index 7bda37f..5db78f1 100644 --- a/src/user/app/init/driver/tmpfs.c +++ b/src/user/app/init/driver/tmpfs.c @@ -4,6 +4,7 @@ #include <stddef.h> #include <stdlib.h> #include <unistd.h> +#include <user/lib/fs/dir.h> struct node { const char *name; @@ -40,8 +41,9 @@ static struct node *tmpfs_open(const char *path, struct fs_wait_response *res) { node = malloc(sizeof *node); memset(node, 0, sizeof *node); - char *namebuf = malloc(res->len); + char *namebuf = malloc(res->len + 1); memcpy(namebuf, path, res->len); + namebuf[res->len] = '\0'; node->name = namebuf; node->namelen = res->len; node->next = root; @@ -65,27 +67,11 @@ void tmpfs_drv(void) { case VFSOP_READ: ptr = (void*)res.id; if (ptr == &special_root) { - // TODO directory offset handling - size_t buf_pos = 0; - size_t to_skip = res.offset; - - for (struct node *iter = root; iter; iter = iter->next) { - if (iter->namelen <= to_skip) { - to_skip -= iter->namelen; - continue; - } - - if (iter->namelen + buf_pos - to_skip >= sizeof(buf)) { - memcpy(buf + buf_pos, iter->name + to_skip, sizeof(buf) - buf_pos - to_skip); - buf_pos = sizeof(buf); - break; - } - memcpy(buf + buf_pos, iter->name + to_skip, iter->namelen - to_skip); - buf_pos += iter->namelen - to_skip; - buf[buf_pos++] = '\0'; - to_skip = 0; - } - _syscall_fs_respond(buf, buf_pos, 0); + struct dirbuild db; + dir_start(&db, res.offset, buf, sizeof buf); + for (struct node *iter = root; iter; iter = iter->next) + dir_append(&db, iter->name); + _syscall_fs_respond(buf, dir_finish(&db), 0); } else { fs_normslice(&res.offset, &res.len, ptr->size, false); _syscall_fs_respond(ptr->buf + res.offset, res.len, 0); |