]> 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 81a618d4b5a4ff056e8bc71d1f488bad691b0331..1e0f2d9a352b16e332b43206f112b7091d2efde4 100644 (file)
@@ -7,8 +7,11 @@
 #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 e339e2add1e0f5b2eba668161e944b1838c68aa6..50c8edeff5c308eeffb141cda9dc41f758b1efa4 100644 (file)
@@ -2,6 +2,7 @@
 #include "utils.h"
 #include "heap.h"
 #include "uart_console.h"
+#include "errno.h"
 
 #define TAR_BLOCK 512
 
@@ -75,7 +76,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);
@@ -111,7 +112,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;
@@ -133,6 +134,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];
@@ -153,10 +156,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 de72c38506b4b905409e6db1534658c7e6acb0ac..5eccd63f49f46635bdbc16ed602aa704f5d58de1 100644 (file)
@@ -1,6 +1,7 @@
 #include "fs.h"
 
 #include "utils.h"
+#include "errno.h"
 
 fs_node_t* fs_root = NULL;
 
@@ -49,8 +50,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 f3efc33a13779564a13ed7cb75e1de25e3f8de47..601df79f833dcf347af8792e325630bcb5ff5854 100644 (file)
@@ -1,5 +1,6 @@
 #include "tmpfs.h"
 
+#include "errno.h"
 #include "heap.h"
 #include "utils.h"
 
@@ -161,14 +162,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;
@@ -180,7 +181,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;
@@ -192,8 +193,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;
@@ -201,7 +202,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;
     }