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).
#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
#include "utils.h"
#include "heap.h"
#include "uart_console.h"
+#include "errno.h"
#define TAR_BLOCK 512
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);
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;
int cur = root_idx;
const char* p = path;
+ if (!path || !leaf_out || leaf_out_sz == 0) return -EINVAL;
+
while (*p == '/') p++;
char part[128];
}
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) {
#include "fs.h"
#include "utils.h"
+#include "errno.h"
fs_node_t* fs_root = NULL;
}
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));
#include "tmpfs.h"
+#include "errno.h"
#include "heap.h"
#include "utils.h"
}
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;
f->data = (uint8_t*)kmalloc(len);
if (!f->data) {
kfree(f);
- return -1;
+ return -ENOMEM;
}
memcpy(f->data, data, len);
f->cap = len;
}
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;
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;
}