kernel: Add a getdents() syscall
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-03-28 21:28:56 +02:00
parent 8eb4d693ac
commit 0847cfcb65
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 57 additions and 1 deletions

View File

@ -33,6 +33,7 @@ set(SOURCES
src/sys/mkdir.cpp
src/sys/mknod.cpp
src/sys/waitpid.cpp
src/sys/getdents.cpp
src/fs/VFS.cpp
src/fs/tmpfs/FileSystem.cpp
src/fs/devices/DeviceRegistry.cpp

View File

@ -0,0 +1,40 @@
#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();
auto& descriptor = *TRY(current->resolve_fd(fd));
if (descriptor.inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
usize nwrite = 0;
while (nwrite < count)
{
auto maybe_entry = descriptor.inode->get(descriptor.offset);
if (!maybe_entry.has_value()) break;
descriptor.offset++;
auto entry = maybe_entry.release_value();
luna_dirent kent;
kent.inode = entry.inode->inode_number();
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;
}

View File

@ -0,0 +1,14 @@
/* bits/getdents.h: Definitions used for the getdents() system call. */
#ifndef _BITS_GETDENTS_H
#define _BITS_GETDENTS_H
#include <sys/types.h>
typedef struct
{
ino_t inode;
char name[129];
} luna_dirent;
#endif

View File

@ -15,6 +15,7 @@ typedef __u16_t mode_t;
typedef __u64_t useconds_t;
typedef __i64_t off_t;
typedef __u64_t dev_t;
typedef __u64_t ino_t;
typedef off_t fpos_t;

View File

@ -2,7 +2,7 @@
#define enumerate_syscalls(_e) \
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \
_e(lseek) _e(mkdir) _e(exec) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl)
_e(lseek) _e(mkdir) _e(exec) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents)
enum Syscalls
{