]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
posix: standardize -errno in VFS/tmpfs/initrd
authorTulio A M Mendes <[email protected]>
Sun, 8 Feb 2026 03:12:50 +0000 (00:12 -0300)
committerTulio A M Mendes <[email protected]>
Sun, 8 Feb 2026 03:12:50 +0000 (00:12 -0300)
Replace remaining -1 style returns in core helpers used by syscalls and early init with negative errno codes (vfs_mount, tmpfs_add_file/mkdir_p, initrd alloc/path helpers). Add missing errno constants (EEXIST/ENOTDIR/ENOSPC).

include/errno.h
src/drivers/initrd.c
src/kernel/fs.c
src/kernel/tmpfs.c

index 79df2f64db3dc508418af9d461b44ff628371ac3..faaf931279f71da2f7703e77fb69da35fe4ca36c 100644 (file)
 #define EBADF 9
 #define ECHILD 10
 #define EFAULT 14
+#define EEXIST 17
 #define ENOMEM 12
+#define ENOTDIR 20
 #define EINVAL 22
+#define ENOSPC 28
 #define EPIPE 32
 #define EMFILE 24
 #define ESPIPE 29
index 65d2b51d204fd5c5898e9464dc6f0eea8ee7bf91..4499b95ebdd0a01439b01064d61dce3ca89bebe0 100644 (file)
@@ -11,6 +11,7 @@
 #include "utils.h"
 #include "heap.h"
 #include "uart_console.h"
+#include "errno.h"
 
 #define TAR_BLOCK 512
 
@@ -84,7 +85,7 @@ static int entry_alloc(void) {
         int new_cap = (entry_cap == 0) ? 32 : entry_cap * 2;
         initrd_entry_t* new_entries = (initrd_entry_t*)kmalloc(sizeof(initrd_entry_t) * (size_t)new_cap);
         fs_node_t* new_nodes = (fs_node_t*)kmalloc(sizeof(fs_node_t) * (size_t)new_cap);
-        if (!new_entries || !new_nodes) return -1;
+        if (!new_entries || !new_nodes) return -ENOMEM;
 
         if (entries) {
             memcpy(new_entries, entries, sizeof(initrd_entry_t) * (size_t)entry_count);
@@ -120,7 +121,7 @@ static int entry_find_child(int parent, const char* name) {
 
 static int entry_add_child(int parent, const char* name, uint32_t flags) {
     int idx = entry_alloc();
-    if (idx < 0) return -1;
+    if (idx < 0) return idx;
 
     strcpy(entries[idx].name, name);
     entries[idx].flags = flags;
@@ -142,6 +143,8 @@ static int ensure_path_dirs(int root_idx, const char* path, char* leaf_out, size
     int cur = root_idx;
     const char* p = path;
 
+    if (!path || !leaf_out || leaf_out_sz == 0) return -EINVAL;
+
     while (*p == '/') p++;
 
     char part[128];
@@ -162,10 +165,10 @@ static int ensure_path_dirs(int root_idx, const char* path, char* leaf_out, size
         }
 
         cur = ensure_dir(cur, part);
-        if (cur < 0) return -1;
+        if (cur < 0) return cur;
     }
 
-    return -1;
+    return -EINVAL;
 }
 
 static uint32_t initrd_read_impl(fs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer) {
index bd449bd88fe606da6e974480779ed2afeea03a3f..43fe25234ada6c72fe5c67ddf84a6686d6862ad3 100644 (file)
@@ -10,6 +10,7 @@
 #include "fs.h"
 
 #include "utils.h"
+#include "errno.h"
 
 fs_node_t* fs_root = NULL;
 
@@ -58,8 +59,8 @@ static void normalize_mountpoint(const char* in, char* out, size_t out_sz) {
 }
 
 int vfs_mount(const char* mountpoint, fs_node_t* root) {
-    if (!root) return -1;
-    if (g_mount_count >= (int)(sizeof(g_mounts) / sizeof(g_mounts[0]))) return -1;
+    if (!root) return -EINVAL;
+    if (g_mount_count >= (int)(sizeof(g_mounts) / sizeof(g_mounts[0]))) return -ENOSPC;
 
     char mp[128];
     normalize_mountpoint(mountpoint, mp, sizeof(mp));
index c07ca9e6c12f2e034caac558edecdba200f1d4ab..ee366ca592abed4973658f96ae977d5b2c9fc38b 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "tmpfs.h"
 
+#include "errno.h"
 #include "heap.h"
 #include "utils.h"
 
@@ -170,14 +171,14 @@ fs_node_t* tmpfs_create_root(void) {
 }
 
 int tmpfs_add_file(fs_node_t* root_dir, const char* name, const uint8_t* data, uint32_t len) {
-    if (!root_dir || root_dir->flags != FS_DIRECTORY) return -1;
-    if (!name || name[0] == 0) return -1;
+    if (!root_dir || root_dir->flags != FS_DIRECTORY) return -ENOTDIR;
+    if (!name || name[0] == 0) return -EINVAL;
 
     struct tmpfs_node* dir = (struct tmpfs_node*)root_dir;
-    if (tmpfs_child_find(dir, name)) return -1;
+    if (tmpfs_child_find(dir, name)) return -EEXIST;
 
     struct tmpfs_node* f = tmpfs_node_alloc(name, FS_FILE);
-    if (!f) return -1;
+    if (!f) return -ENOMEM;
 
     f->vfs.read = &tmpfs_read_impl;
     f->vfs.write = &tmpfs_write_impl;
@@ -189,7 +190,7 @@ int tmpfs_add_file(fs_node_t* root_dir, const char* name, const uint8_t* data, u
         f->data = (uint8_t*)kmalloc(len);
         if (!f->data) {
             kfree(f);
-            return -1;
+            return -ENOMEM;
         }
         memcpy(f->data, data, len);
         f->cap = len;
@@ -201,8 +202,8 @@ int tmpfs_add_file(fs_node_t* root_dir, const char* name, const uint8_t* data, u
 }
 
 int tmpfs_mkdir_p(fs_node_t* root_dir, const char* path) {
-    if (!root_dir || root_dir->flags != FS_DIRECTORY) return -1;
-    if (!path) return -1;
+    if (!root_dir || root_dir->flags != FS_DIRECTORY) return -ENOTDIR;
+    if (!path) return -EINVAL;
 
     struct tmpfs_node* cur = (struct tmpfs_node*)root_dir;
     const char* p = path;
@@ -210,7 +211,7 @@ int tmpfs_mkdir_p(fs_node_t* root_dir, const char* path) {
 
     while (tmpfs_split_next(&p, part, sizeof(part))) {
         struct tmpfs_node* next = tmpfs_child_ensure_dir(cur, part);
-        if (!next) return -1;
+        if (!next) return -ENOMEM;
         cur = next;
     }