]> 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 2faa327b37df25ba6baed18e09fe0feb4889d2ff..59d22cefde96e3be8ccc880fb3f52050b51912f9 100644 (file)
--- 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
index e1fd4f65b29dee26a9dfae200c876e9bc689f85a..b9c43e003f65b694c05261e820625c50acb23aab 100644 (file)
@@ -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;
 
index d964248f156561dec013e41083d548eeafbe646c..087756c36376c851a023f617f43f4d5f0922f017 100644 (file)
@@ -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);
index e90bf826be363ae325e372aa1632b5df8dbdf82c..d3a5fedab68fbc6a66239557e6ed7f89b7b76310 100644 (file)
@@ -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;
+}
index 153916148132783bc7ba43bb74792ee73f3d2de9..cd38db5a6357939e97fd038e845de847d573f779 100644 (file)
@@ -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);