Luna/kernel/src/fs/VFS.cpp

100 lines
2.9 KiB
C++
Raw Normal View History

#include "fs/VFS.h"
#include "Log.h"
#include "thread/Thread.h"
#include <bits/modes.h>
#include <luna/PathParser.h>
namespace VFS
{
SharedPtr<FileSystem> root_fs;
Inode& root_inode()
{
return *root_fs->root_inode();
}
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{
if (*path == '\0') return err(ENOENT);
auto parser = TRY(PathParser::create(path));
SharedPtr<Inode> current_inode;
if (parser.is_absolute() || !working_directory) current_inode = root_fs->root_inode();
else
current_inode = working_directory;
const char* section;
while (parser.next().try_set_value(section))
{
if (!can_execute(current_inode, auth)) return err(EACCES);
current_inode = TRY(current_inode->find(section));
}
return current_inode;
}
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{
auto parser = TRY(PathParser::create(path));
auto parent_path = TRY(parser.dirname());
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
if (!can_write(parent_inode, auth)) return err(EACCES);
auto child_name = TRY(parser.basename());
return parent_inode->create_subdirectory(child_name.chars());
}
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{
auto parser = TRY(PathParser::create(path));
auto parent_path = TRY(parser.dirname());
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
if (!can_write(parent_inode, auth)) return err(EACCES);
auto child_name = TRY(parser.basename());
return parent_inode->create_file(child_name.chars());
}
bool can_execute(SharedPtr<Inode> inode, Credentials auth)
{
if (inode->uid() == auth.euid) { return inode->mode() & S_IXUSR; }
if (inode->gid() == auth.egid) { return inode->mode() & S_IXGRP; }
return inode->mode() & S_IXOTH;
}
bool can_write(SharedPtr<Inode> inode, Credentials auth)
{
if (inode->uid() == auth.euid) { return inode->mode() & S_IWUSR; }
if (inode->gid() == auth.egid) { return inode->mode() & S_IWGRP; }
return inode->mode() & S_IWOTH;
}
bool can_read(SharedPtr<Inode> inode, Credentials auth)
{
if (inode->uid() == auth.euid) { return inode->mode() & S_IRUSR; }
if (inode->gid() == auth.egid) { return inode->mode() & S_IRGRP; }
return inode->mode() & S_IROTH;
}
bool is_setuid(SharedPtr<Inode> inode)
{
return inode->mode() & S_ISUID;
}
bool is_setgid(SharedPtr<Inode> inode)
{
return inode->mode() & S_ISGID;
}
}