summaryrefslogtreecommitdiff
path: root/src/user/lib/fs/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/lib/fs/misc.c')
-rw-r--r--src/user/lib/fs/misc.c49
1 files changed, 20 insertions, 29 deletions
diff --git a/src/user/lib/fs/misc.c b/src/user/lib/fs/misc.c
index e47d930..1fb5c3b 100644
--- a/src/user/lib/fs/misc.c
+++ b/src/user/lib/fs/misc.c
@@ -19,6 +19,21 @@ bool fork2_n_mount(const char *path) {
return false;
}
+static int dir_seglen(const char *path) {
+ /* in human terms:
+ * if path contains /, return its position + 1
+ * otherwise, return strlen */
+ int len = 0;
+ while (path[len]) {
+ if (path[len] == '/') {
+ len++;
+ break;
+ }
+ len++;
+ }
+ return len;
+}
+
void fs_passthru(const char *prefix) {
struct fs_wait_response res;
const size_t buf_len = 1024;
@@ -102,19 +117,7 @@ void fs_whitelist(const char **list) {
// TODO could be precomputed too
size_t len = strlen(*iter); // inefficient, whatever
if (blen < len && !memcmp(ipath, *iter, blen))
- {
- /* inject up to the next slash */
- // TODO separate out into its own function
- int ilen = 0;
- while ((*iter)[blen + ilen]) {
- if ((*iter)[blen + ilen] == '/') {
- ilen++;
- break;
- }
- ilen++;
- }
- dir_appendl(&db, (*iter) + blen, ilen);
- }
+ dir_appendl(&db, *iter + blen, dir_seglen(*iter + blen));
}
_syscall_fs_respond(buf, dir_finish(&db), 0);
break;
@@ -144,7 +147,6 @@ void fs_dir_inject(const char *path) {
struct fs_dir_handle *data;
const size_t buf_len = 1024;
char *buf = malloc(buf_len);
- int inject_len;
struct dirbuild db;
if (!buf) exit(1);
@@ -157,22 +159,10 @@ void fs_dir_inject(const char *path) {
res.len < path_len && !memcmp(path, buf, res.len))
{
/* opening a directory that we're injecting into */
-
data = malloc(sizeof *data);
data->delegate = _syscall_open(buf, res.len, res.flags);
data->inject = path + res.len;
-
- /* inject up to the next slash */
- inject_len = 0;
- while (data->inject[inject_len]) {
- if (data->inject[inject_len] == '/') {
- inject_len++; // include the slash
- break;
- }
- inject_len++;
- }
- data->inject_len = inject_len;
-
+ data->inject_len = dir_seglen(data->inject);
_syscall_fs_respond(data, 0, 0);
} else {
_syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
@@ -187,8 +177,9 @@ void fs_dir_inject(const char *path) {
break;
case VFSOP_READ:
- // TODO optimization - min(buf_len, res.capacity)
- dir_start(&db, res.offset, buf, buf_len);
+ if (res.capacity > buf_len)
+ res.capacity = buf_len;
+ dir_start(&db, res.offset, buf, res.capacity);
dir_appendl(&db, data->inject, data->inject_len);
if (data->delegate >= 0)
dir_append_from(&db, data->delegate);