46 lines
1.2 KiB
C++
46 lines
1.2 KiB
C++
|
#include "Log.h"
|
||
|
#include "fs/VFS.h"
|
||
|
#include "memory/MemoryManager.h"
|
||
|
#include "sys/Syscall.h"
|
||
|
#include "thread/Scheduler.h"
|
||
|
|
||
|
Result<u64> sys_open(Registers*, SyscallArgs args)
|
||
|
{
|
||
|
u64 path_address = args[0];
|
||
|
if (!MemoryManager::validate_userspace_string(path_address)) return err(EFAULT);
|
||
|
|
||
|
const char* path = (const char*)path_address;
|
||
|
int flags = (int)args[1];
|
||
|
|
||
|
Thread* current = Scheduler::current();
|
||
|
|
||
|
kinfoln("open: trying to open file %s, flags %d", path, flags);
|
||
|
|
||
|
auto inode = TRY(VFS::resolve_path(path));
|
||
|
|
||
|
int fd = TRY(current->allocate_fd(0));
|
||
|
|
||
|
current->fd_table->fds[fd] = FileDescriptor { inode, 0, flags };
|
||
|
|
||
|
kinfoln("open: allocated file descriptor %d for inode %zu", fd, inode->inode_number());
|
||
|
|
||
|
return (u64)fd;
|
||
|
}
|
||
|
|
||
|
Result<u64> sys_close(Registers*, SyscallArgs args)
|
||
|
{
|
||
|
int fd = (int)args[0];
|
||
|
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
|
||
|
|
||
|
Thread* current = Scheduler::current();
|
||
|
|
||
|
if (!current->fd_table->fds[fd].has_value()) return err(EBADF);
|
||
|
|
||
|
kinfoln("close: closing file descriptor %d (was referencing inode %zu)", fd,
|
||
|
current->fd_table->fds[fd]->inode->inode_number());
|
||
|
|
||
|
current->fd_table->fds[fd] = {};
|
||
|
|
||
|
return 0;
|
||
|
}
|