]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
vfs: add basic path lookup (vfs_lookup)
authorTulio A M Mendes <[email protected]>
Sat, 7 Feb 2026 17:35:49 +0000 (14:35 -0300)
committerTulio A M Mendes <[email protected]>
Sat, 7 Feb 2026 17:35:49 +0000 (14:35 -0300)
Makefile
include/fs.h
src/kernel/elf.c
src/kernel/fs.c
src/kernel/shell.c

index 2ffeb5cc00c53557fc421b52a2d9e67bfcfb6351..c04b9b68f35578162dcb65aa34b94f1aef95e8d4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -138,6 +138,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      $<"
@@ -151,4 +156,4 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.S
 clean:
        rm -rf build $(KERNEL_NAME)
 
-.PHONY: all clean iso run
+.PHONY: all clean iso run cppcheck
index c5a29371bd8292efc6a21ad7e57a31c5f8bdb9d2..0f7ef0041c625a4ef4cb00f9b10f34a6120712dc 100644 (file)
@@ -38,6 +38,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;
 
index 4340491b71a86a87bb661a3ad4c1f517b88b385e..7b6a56291a5ab0d6a8b4507ee2cdc2eea86f272b 100644 (file)
@@ -90,7 +90,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);
index 378443e9cceb3823dbc1f9b0f5c371160875b43b..542d79ebfb48b7cfefb61e216f82418ca547afe7 100644 (file)
@@ -9,6 +9,8 @@
 
 #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) {
@@ -32,3 +34,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;
+}
index 9da260b331aeefe49c8a2acc903b7ee409138149..f7a0a95f9ac7de64a4a6ea4289bbc00532e4fb67 100644 (file)
@@ -56,7 +56,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);