From 80db0f331d9deac936f43788a52fe2c629742d48 Mon Sep 17 00:00:00 2001 From: Kevin Lange Date: Tue, 29 Nov 2011 13:28:36 -0600 Subject: [PATCH] [vfs] Proper path canonicalization available --- kernel/core/vfs.c | 68 +++++++++++++++++++++++++++++++++++++++++++++ kernel/include/fs.h | 1 + 2 files changed, 69 insertions(+) diff --git a/kernel/core/vfs.c b/kernel/core/vfs.c index 91c1f978..e9e63fae 100644 --- a/kernel/core/vfs.c +++ b/kernel/core/vfs.c @@ -1,5 +1,6 @@ #include #include +#include fs_node_t *fs_root = 0; @@ -50,6 +51,73 @@ fs_node_t *finddir_fs(fs_node_t *node, char *name) { } } +/* + * Canonicalize a path. + */ +char * +canonicalize_path(char *cwd, char *input) { + list_t * out = list_create(); + + if (strlen(input) && input[0] != '/') { + char * path = malloc((strlen(cwd) + 1) * sizeof(char)); + memcpy(path, cwd, strlen(cwd) + 1); + + char * pch; + char * save; + pch = strtok_r(path,"/",&save); + + while (pch != NULL) { + char * s = malloc(sizeof(char) * (strlen(pch) + 1)); + memcpy(s, pch, strlen(pch) + 1); + list_insert(out, s); + pch = strtok_r(NULL,"/",&save); + } + free(path); + } + + char * path = malloc((strlen(input) + 1) * sizeof(char)); + memcpy(path, input, strlen(input) + 1); + char * pch; + char * save; + pch = strtok_r(path,"/",&save); + while (pch != NULL) { + if (!strcmp(pch,"..")) { + node_t * n = list_pop(out); + free(n->value); + free(n); + } else if (!strcmp(pch,".")) { + /* pass */ + } else { + char * s = malloc(sizeof(char) * (strlen(pch) + 1)); + memcpy(s, pch, strlen(pch) + 1); + list_insert(out, s); + } + pch = strtok_r(NULL, "/", &save); + } + free(path); + + size_t size = 0; + + foreach(item, out) { + size += strlen(item->value) + 1; + } + + char * output = malloc(sizeof(char) * (size + 1)); + char * output_offset = output; + foreach(item, out) { + output_offset[0] = '/'; + output_offset++; + memcpy(output_offset, item->value, strlen(item->value) + 1); + output_offset += strlen(item->value); + } + + list_destroy(out); + list_free(out); + free(out); + + return output; +} + /* * Retreive the node for the requested path * HACK FIXME XXX TODO diff --git a/kernel/include/fs.h b/kernel/include/fs.h index 613b37dd..d3e138e9 100644 --- a/kernel/include/fs.h +++ b/kernel/include/fs.h @@ -51,5 +51,6 @@ void close_fs(fs_node_t * node); struct dirent *readdir_fs(fs_node_t * node, uint32_t index); fs_node_t *finddir_fs(fs_node_t * node, char *name); fs_node_t *kopen(const char *filename, uint32_t flags); +char * canonicalize_path(char *cwd, char *input); #endif