29 lines
700 B
C++
29 lines
700 B
C++
|
#include "Log.h"
|
||
|
#include "fs/VFS.h"
|
||
|
#include "memory/MemoryManager.h"
|
||
|
#include "sys/Syscall.h"
|
||
|
#include "thread/Scheduler.h"
|
||
|
|
||
|
Result<u64> sys_read(Registers*, SyscallArgs args)
|
||
|
{
|
||
|
int fd = (int)args[0];
|
||
|
u8* buf = (u8*)args[1];
|
||
|
usize size = (usize)args[2];
|
||
|
|
||
|
if (!MemoryManager::validate_user_write(buf, size)) return err(EFAULT);
|
||
|
|
||
|
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
|
||
|
|
||
|
Thread* current = Scheduler::current();
|
||
|
|
||
|
Option<FileDescriptor>& descriptor = current->fd_table->fds[fd];
|
||
|
|
||
|
if (!descriptor.has_value()) return err(EBADF);
|
||
|
|
||
|
usize nread = TRY(descriptor->inode->read(buf, descriptor->offset, size));
|
||
|
|
||
|
descriptor->offset += nread;
|
||
|
|
||
|
return nread;
|
||
|
}
|