Luna/kernel/src/sys/getdents.cpp
apio e2a77bb3da
Some checks failed
continuous-integration/drone/push Build is failing
kernel+libc: Add pledge support
2023-08-12 21:38:25 +02:00

42 lines
1.1 KiB
C++

#include "Pledge.h"
#include "fs/VFS.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/getdents.h>
Result<u64> sys_getdents(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
luna_dirent* ent = (luna_dirent*)args[1];
usize count = (usize)args[2];
Thread* current = Scheduler::current();
TRY(check_pledge(current, Promise::p_stdio));
auto& descriptor = *TRY(current->resolve_fd(fd));
if (descriptor.inode()->type() != VFS::InodeType::Directory) return err(ENOTDIR);
usize nwrite = 0;
while (nwrite < count)
{
VFS::DirectoryEntry entry;
bool ok = descriptor.inode()->get(descriptor.offset).try_set_value(entry);
if (!ok) break;
descriptor.offset++;
luna_dirent kent;
kent.inode = entry.inode->metadata().inum;
strlcpy(kent.name, entry.name.chars(), entry.name.length() + 1);
if (!MemoryManager::copy_to_user_typed(ent, &kent)) return err(EFAULT);
ent++;
nwrite++;
}
return nwrite;
}