Add file descriptors and basic cat

This commit is contained in:
2026-06-17 23:20:43 +03:00
parent 968bb02aa8
commit bcf2e5bac6
13 changed files with 207 additions and 29 deletions
+11 -1
View File
@@ -44,6 +44,16 @@ path_t *path_open(const fs_node_t *root, const path_t *source, const char *path)
return result;
}
fs_node_t *path_open_node(const fs_node_t *root, const path_t *source, const char *path) {
path_t *p = path_open(root, source, path);
if (!p) {
return NUL;
}
fs_node_t *n = fs_open_again(p->depth ? p->stack[p->depth - 1] : root);
path_close(p);
return n;
}
fs_node_t *path_open_file(const fs_node_t *root, const path_t *source, const char *path) {
path_t *p = path_open(root, source, path);
if (!p) {
@@ -65,7 +75,7 @@ fs_node_t *path_open_directory(const fs_node_t *root, const path_t *source, cons
}
if (!p->depth) {
path_close(p);
return (fs_node_t *)root;
return fs_open_again(root);
}
if (!p->stack[p->depth - 1]->is_dir) {
path_close(p);
+2
View File
@@ -12,6 +12,8 @@ typedef struct {
path_t *path_open(const fs_node_t *root, const path_t *source, const char *path);
fs_node_t *path_open_node(const fs_node_t *root, const path_t *source, const char *path);
fs_node_t *path_open_file(const fs_node_t *root, const path_t *source, const char *path);
fs_node_t *path_open_directory(const fs_node_t *root, const path_t *source, const char *path);
+1
View File
@@ -59,6 +59,7 @@ process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t
proc->root = parent->root;
proc->stdin = parent->stdin;
proc->stdout = parent->stdout;
proc->free_fd = 2;
return proc;
}
+9
View File
@@ -6,6 +6,13 @@
#define USER_RFLAGS 0x202
#define MAX_FDS 16
typedef struct {
fs_node_t *node;
uint64_t offset;
} fd_entry_t;
typedef struct process {
const struct process *parent;
uint64_t *pml4;
@@ -17,6 +24,8 @@ typedef struct process {
const path_t *cwd;
const stream_t *stdin;
const stream_t *stdout;
fd_entry_t fds[MAX_FDS];
uint64_t free_fd;
} process_t;
typedef void (*app_t)(process_t *proc, uint8_t argc, char **argv);
+57 -4
View File
@@ -7,6 +7,7 @@
#include "src/lib/layout.h"
#include "src/lib/memory.h"
#include "src/lib/string.h"
#include "src/lib/util.h"
#define MSR_EFER 0xC0000080
#define MSR_STAR 0xC0000081
@@ -33,13 +34,37 @@ void syscall_init() {
}
static uint64_t read(uint64_t fd, uint64_t max, char *to) {
(void)fd;
return current_process->stdin->read(current_process->stdin, max, to);
switch (fd) {
case 0:
return current_process->stdin->read(current_process->stdin, max, to);
case 1:
return current_process->stdout->read(current_process->stdout, max, to);
default:
if (!current_process->fds[fd].node) {
return (uint64_t)-1;
}
if (current_process->fds[fd].offset >= current_process->fds[fd].node->size) {
return 0;
}
uint64_t remaining = current_process->fds[fd].node->size - current_process->fds[fd].offset;
uint64_t to_read = remaining > max ? max : remaining;
fs_read(current_process->fds[fd].node, current_process->fds[fd].offset, to_read, to);
current_process->fds[fd].offset += to_read;
return to_read;
}
return (uint64_t)-1;
}
static uint64_t write(uint64_t fd, const char *from, uint64_t bytes) {
(void)fd;
return current_process->stdout->write(current_process->stdout, from, bytes);
switch (fd) {
case 0:
return current_process->stdin->write(current_process->stdin, from, bytes);
case 1:
return current_process->stdout->write(current_process->stdout, from, bytes);
default:
return (uint64_t)-1;
}
return (uint64_t)-1;
}
static uint64_t getcwd(uint64_t max, char *to) {
@@ -170,6 +195,30 @@ static uint64_t readdir(const char *path, uint64_t index, uint64_t max, char *to
return size;
}
static uint64_t open(const char *path) {
if (current_process->free_fd >= MAX_FDS) {
return (uint64_t)-1;
}
fs_node_t *node = path_open_node(current_process->root, current_process->cwd, path);
if (!node) {
return (uint64_t)-1;
}
current_process->fds[current_process->free_fd].node = node;
current_process->fds[current_process->free_fd].offset++;
current_process->free_fd++;
return current_process->free_fd - 1;
}
static uint64_t close(uint64_t fd) {
if (fd >= MAX_FDS || !current_process->fds[fd].node) {
return (uint64_t)-1;
}
fs_close(current_process->fds[fd].node);
current_process->fds[fd].node = NUL;
current_process->fds[fd].offset = 0;
return 0;
}
static void exit() {
process_switch_to(&current_process->kernel_rsp, current_process->parent->kernel_rsp, VIRT_TO_PHYS(current_process->parent->pml4));
}
@@ -188,6 +237,10 @@ uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t
return spawn((const char *)arg1, arg2, (const char **)arg3);
case SYSCALL_READDIR:
return readdir((const char *)arg1, arg2, arg3, (char *)arg4);
case SYSCALL_OPEN:
return open((const char *)arg1);
case SYSCALL_CLOSE:
return close(arg1);
case SYSCALL_EXIT:
exit();
return 0;
+2
View File
@@ -8,6 +8,8 @@
#define SYSCALL_CHDIR 3
#define SYSCALL_SPAWN 4
#define SYSCALL_READDIR 5
#define SYSCALL_OPEN 6
#define SYSCALL_CLOSE 7
#define SYSCALL_EXIT 60
void syscall_init();