[orbis-kernel] Impl sys_getdirentries/sys_getdents

This commit is contained in:
Ivan Chikish 2023-08-14 23:58:28 +03:00
parent 8179a638ad
commit 8f0a90d24b
5 changed files with 89 additions and 4 deletions

View file

@ -1,6 +1,8 @@
#pragma once
#include "KernelAllocator.hpp"
#include "error/ErrorCode.hpp"
#include "stat.hpp"
#include "utils/Rc.hpp"
#include "utils/SharedMutex.hpp"
#include <cstdint>
@ -43,5 +45,6 @@ struct File : RcBase {
const FileOps *ops = nullptr;
Ref<RcBase> device;
std::uint64_t nextOff = 0;
utils::kvector<Dirent> dirEntries;
};
} // namespace orbis

View file

@ -23,4 +23,25 @@ struct Stat {
int32_t lspare;
timespec birthtim; // time of file creation
};
struct Dirent {
uint32_t fileno;
uint16_t reclen;
uint8_t type;
uint8_t namlen;
char name[256];
};
enum {
kDtUnknown = 0,
kDtFifo = 1,
kDtChr = 2,
kDtDir = 4,
kDtBlk = 6,
kDtReg = 8,
kDtLnk = 10,
kDtSock = 12,
kDtWht = 14,
};
} // namespace orbis

View file

@ -304,13 +304,36 @@ orbis::SysResult orbis::sys_rmdir(Thread *thread, ptr<char> path) {
orbis::SysResult orbis::sys_getdirentries(Thread *thread, sint fd,
ptr<char> buf, uint count,
ptr<slong> basep) {
ORBIS_LOG_ERROR(__FUNCTION__, fd, (void *)buf, count, basep);
thread->where();
ORBIS_LOG_WARNING(__FUNCTION__, fd, (void *)buf, count, basep);
Ref<File> file = thread->tproc->fileDescriptors.get(fd);
if (file == nullptr) {
return ErrorCode::BADF;
}
std::lock_guard lock(file->mtx);
const orbis::Dirent *entries = file->dirEntries.data();
auto pos = file->nextOff / sizeof(orbis::Dirent); // TODO
auto rmax = count / sizeof(orbis::Dirent);
auto next = std::min<uint64_t>(file->dirEntries.size(), pos + rmax);
file->nextOff = next * sizeof(orbis::Dirent);
SysResult result{};
result = uwrite((orbis::Dirent *)buf, entries + pos, next - pos);
if (result.isError())
return result;
if (basep) {
result = uwrite(basep, slong(file->nextOff));
if (result.isError())
return result;
}
thread->retval[0] = (next - pos) * sizeof(orbis::Dirent);
return {};
}
orbis::SysResult orbis::sys_getdents(Thread *thread, sint fd, ptr<char> buf,
size_t count) {
return ErrorCode::NOSYS;
ORBIS_LOG_WARNING(__FUNCTION__, fd, (void *)buf, count);
return orbis::sys_getdirentries(thread, fd, buf, count, nullptr);
}
orbis::SysResult orbis::sys_umask(Thread *thread, sint newmask) {
return ErrorCode::NOSYS;