From: Tulio A M Mendes Date: Sat, 7 Feb 2026 17:35:49 +0000 (-0300) Subject: vfs: add basic path lookup (vfs_lookup) X-Git-Url: https://projects.tadryanom.me/sitemap.xml?a=commitdiff_plain;h=52091e1df1aa4c257ea9af383eae24d034b5abf8;p=AdrOS.git vfs: add basic path lookup (vfs_lookup) --- diff --git a/Makefile b/Makefile index 2faa327..59d22ce 100644 --- a/Makefile +++ b/Makefile @@ -129,6 +129,11 @@ run: iso -serial file:serial.log -monitor none -no-reboot -no-shutdown \ $(QEMU_DFLAGS) +cppcheck: + @cppcheck --version >/dev/null + @cppcheck --quiet --enable=warning,performance,portability --error-exitcode=1 \ + -I include $(SRC_DIR) + $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(dir $@) @echo " CC $<" @@ -142,4 +147,4 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.S clean: rm -rf build $(KERNEL_NAME) -.PHONY: all clean iso run +.PHONY: all clean iso run cppcheck diff --git a/include/fs.h b/include/fs.h index e1fd4f6..b9c43e0 100644 --- a/include/fs.h +++ b/include/fs.h @@ -29,6 +29,8 @@ uint32_t vfs_write(fs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buf void vfs_open(fs_node_t* node); void vfs_close(fs_node_t* node); +fs_node_t* vfs_lookup(const char* path); + // Global root of the filesystem extern fs_node_t* fs_root; diff --git a/src/kernel/elf.c b/src/kernel/elf.c index d964248..087756c 100644 --- a/src/kernel/elf.c +++ b/src/kernel/elf.c @@ -81,7 +81,7 @@ int elf32_load_user_from_initrd(const char* filename, uintptr_t* entry_out, uint if (!filename || !entry_out || !user_stack_top_out) return -1; if (!fs_root) return -1; - fs_node_t* node = fs_root->finddir ? fs_root->finddir(fs_root, (char*)filename) : NULL; + fs_node_t* node = vfs_lookup(filename); if (!node) { uart_print("[ELF] file not found: "); uart_print(filename); diff --git a/src/kernel/fs.c b/src/kernel/fs.c index e90bf82..d3a5fed 100644 --- a/src/kernel/fs.c +++ b/src/kernel/fs.c @@ -1,5 +1,7 @@ #include "fs.h" +#include "utils.h" + fs_node_t* fs_root = NULL; uint32_t vfs_read(fs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer) { @@ -23,3 +25,37 @@ void vfs_close(fs_node_t* node) { if (node->close) node->close(node); } + +fs_node_t* vfs_lookup(const char* path) { + if (!path || !fs_root) return NULL; + + if (strcmp(path, "/") == 0) return fs_root; + + const char* p = path; + while (*p == '/') p++; + if (*p == 0) return fs_root; + + fs_node_t* cur = fs_root; + + char part[128]; + while (*p != 0) { + size_t i = 0; + while (*p != 0 && *p != '/') { + if (i + 1 < sizeof(part)) { + part[i++] = *p; + } + p++; + } + part[i] = 0; + + while (*p == '/') p++; + + if (part[0] == 0) continue; + + if (!cur || !cur->finddir) return NULL; + cur = cur->finddir(cur, part); + if (!cur) return NULL; + } + + return cur; +} diff --git a/src/kernel/shell.c b/src/kernel/shell.c index 1539161..cd38db5 100644 --- a/src/kernel/shell.c +++ b/src/kernel/shell.c @@ -47,7 +47,16 @@ void execute_command(char* cmd) { uart_print("No filesystem mounted.\n"); } else { char* fname = cmd + 4; - fs_node_t* file = fs_root->finddir(fs_root, fname); + fs_node_t* file = NULL; + if (fname[0] == '/') { + file = vfs_lookup(fname); + } else { + char abs[132]; + abs[0] = '/'; + abs[1] = 0; + strcpy(abs + 1, fname); + file = vfs_lookup(abs); + } if (file) { uart_print("Reading "); uart_print(fname); uart_print("...\n"); uint8_t* buf = (uint8_t*)kmalloc(file->length + 1);