2026-07-07 00:13:24 -05:00
|
|
|
#include <vfs.h>
|
|
|
|
|
#include <process.h>
|
|
|
|
|
#include <scheduler.h>
|
2026-06-30 00:03:21 -05:00
|
|
|
|
2026-07-07 00:13:24 -05:00
|
|
|
#include <lib/memory.h>
|
2026-06-29 17:00:07 -05:00
|
|
|
|
|
|
|
|
vfs_node_t* root = NULL;
|
|
|
|
|
|
2026-06-30 00:03:21 -05:00
|
|
|
vfs_node_t* vfs_create_root() {
|
2026-06-29 17:00:07 -05:00
|
|
|
root = kalloc(sizeof(vfs_node_t));
|
2026-06-30 00:03:21 -05:00
|
|
|
memset((uint8_t*)root, 0, sizeof(vfs_node_t));
|
|
|
|
|
|
|
|
|
|
strcpy(root->name, "root");
|
|
|
|
|
|
2026-06-29 17:00:07 -05:00
|
|
|
root->flags |= VFS_DIRECTORY;
|
2026-06-30 00:03:21 -05:00
|
|
|
root->size = 0;
|
|
|
|
|
root->finddir = NULL;
|
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mount(vfs_node_t* _mnt, vfs_node_t* _fs) {
|
|
|
|
|
if (!_fs || !_mnt || !(_mnt->flags & VFS_DIRECTORY)) return;
|
2026-06-30 19:26:31 -05:00
|
|
|
_mnt->mnt = _fs;
|
2026-06-30 00:03:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static vfs_node_t* lookup(const char* path) {
|
|
|
|
|
if (path[0] != '/') return NULL;
|
|
|
|
|
vfs_node_t* current = root;
|
|
|
|
|
|
2026-06-30 19:26:31 -05:00
|
|
|
if (current->mnt) {
|
|
|
|
|
current = current->mnt;
|
2026-06-30 00:03:21 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-30 19:26:31 -05:00
|
|
|
const char* cmp = path + 1;
|
|
|
|
|
if (*cmp == '\0') return current;
|
2026-06-30 00:03:21 -05:00
|
|
|
|
|
|
|
|
if (current->finddir) {
|
2026-06-30 19:26:31 -05:00
|
|
|
return current->finddir(current, cmp);
|
2026-06-30 00:03:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int open(const char* _path, int flags) {
|
|
|
|
|
vfs_node_t* file = lookup(_path);
|
|
|
|
|
if (!file) return -1;
|
2026-07-07 00:13:24 -05:00
|
|
|
|
|
|
|
|
file_t* f = (file_t*)kalloc(sizeof(file_t));
|
|
|
|
|
f->node = file;
|
|
|
|
|
f->offset = 0;
|
|
|
|
|
f->flags = flags;
|
|
|
|
|
|
|
|
|
|
return alloc_fd(f);
|
2026-06-30 00:03:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int read(int fd, void* buf, size_t sz) {
|
2026-07-07 00:13:24 -05:00
|
|
|
process_t* curr_p = current_task();
|
|
|
|
|
if (fd < 0 || fd >= MAX_PROCESS_FDS || !curr_p->fd_table[fd]->node) {
|
2026-06-30 00:03:21 -05:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-07 00:13:24 -05:00
|
|
|
file_t* file = curr_p->fd_table[fd];
|
2026-06-30 00:03:21 -05:00
|
|
|
|
|
|
|
|
if (!file->node->read) return -1;
|
|
|
|
|
|
|
|
|
|
uint32_t bytes_read = file->node->read(file->node, file->offset, sz, buf);
|
|
|
|
|
file->offset += bytes_read;
|
|
|
|
|
|
|
|
|
|
return bytes_read;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-30 19:26:31 -05:00
|
|
|
int fstat(int fd, file_t* dst) {
|
2026-07-07 00:13:24 -05:00
|
|
|
process_t* curr_p = current_task();
|
|
|
|
|
if (fd < 0 || fd >= MAX_PROCESS_FDS || !curr_p->fd_table[fd]->node) {
|
2026-06-30 19:26:31 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-07 00:13:24 -05:00
|
|
|
file_t* src = curr_p->fd_table[fd];
|
2026-06-30 19:26:31 -05:00
|
|
|
memcpy(src, dst, sizeof(file_t));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fseek(int fd, size_t offset) {
|
2026-07-07 00:13:24 -05:00
|
|
|
process_t* curr_p = current_task();
|
|
|
|
|
if (fd < 0 || fd >= MAX_PROCESS_FDS || !curr_p->fd_table[fd]->node) {
|
2026-06-30 19:26:31 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-07 00:13:24 -05:00
|
|
|
file_t* file = curr_p->fd_table[fd];
|
2026-06-30 19:26:31 -05:00
|
|
|
file->offset = offset;
|
|
|
|
|
return 1;
|
2026-07-07 00:13:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int alloc_fd(file_t* f) {
|
|
|
|
|
process_t* curr_p = current_task();
|
|
|
|
|
if (!curr_p) return -1;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_PROCESS_FDS; i++) {
|
|
|
|
|
if (curr_p->fd_table[i] == NULL) {
|
|
|
|
|
curr_p->fd_table[i] = f;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|