From: Tulio A M Mendes Date: Sun, 8 Feb 2026 03:12:50 +0000 (-0300) Subject: posix: standardize -errno in VFS/tmpfs/initrd X-Git-Url: https://projects.tadryanom.me/sitemap.xml?a=commitdiff_plain;h=4b61a041816d6759600de090ec5e19b741ef2163;p=AdrOS.git posix: standardize -errno in VFS/tmpfs/initrd 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). --- diff --git a/include/errno.h b/include/errno.h index 81a618d..1e0f2d9 100644 --- a/include/errno.h +++ b/include/errno.h @@ -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 diff --git a/src/drivers/initrd.c b/src/drivers/initrd.c index e339e2a..50c8ede 100644 --- a/src/drivers/initrd.c +++ b/src/drivers/initrd.c @@ -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) { diff --git a/src/kernel/fs.c b/src/kernel/fs.c index de72c38..5eccd63 100644 --- a/src/kernel/fs.c +++ b/src/kernel/fs.c @@ -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)); diff --git a/src/kernel/tmpfs.c b/src/kernel/tmpfs.c index f3efc33..601df79 100644 --- a/src/kernel/tmpfs.c +++ b/src/kernel/tmpfs.c @@ -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; }