Luna/kernel/src/sys/stat.cpp
apio 3a51807fa6
All checks were successful
continuous-integration/drone/push Build is passing
kernel+stat: Handle pipes correctly in stat()
2023-05-23 20:54:25 +02:00

51 lines
1.5 KiB
C++

#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/atfile.h>
#include <bits/modes.h>
#include <bits/struct_stat.h>
static mode_t make_mode(mode_t mode, VFS::InodeType type)
{
mode_t result = mode;
switch (type)
{
case VFS::InodeType::RegularFile: result |= S_IFREG; break;
case VFS::InodeType::Directory: result |= S_IFDIR; break;
case VFS::InodeType::Device: result |= S_IFCHR; break;
case VFS::InodeType::Symlink: result |= S_IFLNK; break;
case VFS::InodeType::FIFO: result |= S_IFIFO; break;
default: break;
}
return result;
}
Result<u64> sys_fstatat(Registers*, SyscallArgs args)
{
int dirfd = (int)args[0];
auto path = TRY(MemoryManager::strdup_from_user(args[1]));
stat* st = (stat*)args[2];
int flags = (int)args[3];
Thread* current = Scheduler::current();
auto inode = TRY(current->resolve_atfile(dirfd, path, flags & AT_EMPTY_PATH, !(flags & AT_SYMLINK_NOFOLLOW)));
stat kstat;
kstat.st_ino = inode->inode_number();
kstat.st_mode = make_mode(inode->mode(), inode->type());
kstat.st_nlink = 1; // FIXME: Count hard links to files.
kstat.st_uid = inode->uid();
kstat.st_gid = inode->gid();
kstat.st_size = inode->size();
kstat.st_dev = inode->fs() ? inode->fs()->host_device_id() : 0;
kstat.st_rdev = inode->device_id();
if (!MemoryManager::copy_to_user_typed(st, &kstat)) return err(EFAULT);
return 0;
}