2023-02-03 21:18:52 +00:00
|
|
|
#include "fs/VFS.h"
|
2023-03-10 21:18:48 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include <luna/PathParser.h>
|
2023-02-03 21:18:52 +00:00
|
|
|
|
|
|
|
namespace VFS
|
|
|
|
{
|
|
|
|
SharedPtr<FileSystem> root_fs;
|
2023-02-25 17:05:25 +00:00
|
|
|
|
|
|
|
Inode& root_inode()
|
|
|
|
{
|
2023-03-10 21:18:48 +00:00
|
|
|
return *root_fs->root_inode();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<SharedPtr<Inode>> resolve_path(const char* path)
|
|
|
|
{
|
|
|
|
auto parser = TRY(PathParser::create(path));
|
|
|
|
|
2023-03-11 00:13:44 +00:00
|
|
|
SharedPtr<Inode> current_inode = root_fs->root_inode();
|
2023-03-10 21:18:48 +00:00
|
|
|
|
2023-03-11 00:13:44 +00:00
|
|
|
// FIXME: Properly handle relative paths.
|
2023-03-10 21:18:48 +00:00
|
|
|
|
|
|
|
const char* section;
|
2023-03-11 09:23:46 +00:00
|
|
|
while (parser.next().try_set_value(section)) { current_inode = TRY(current_inode->find(section)); }
|
2023-03-10 21:18:48 +00:00
|
|
|
|
|
|
|
return current_inode;
|
2023-02-25 17:05:25 +00:00
|
|
|
}
|
2023-03-11 00:13:44 +00:00
|
|
|
|
|
|
|
Result<SharedPtr<Inode>> create_directory(const char* path)
|
|
|
|
{
|
|
|
|
auto parser = TRY(PathParser::create(path));
|
|
|
|
auto parent_path = TRY(parser.dirname());
|
|
|
|
|
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars()));
|
|
|
|
|
|
|
|
auto child_name = TRY(parser.basename());
|
|
|
|
|
2023-03-11 09:23:46 +00:00
|
|
|
// kdbgln("vfs: creating directory '%s' in parent '%s'", child_name.chars(), parent_path.chars());
|
2023-03-11 00:13:44 +00:00
|
|
|
|
|
|
|
return parent_inode->create_subdirectory(child_name.chars());
|
|
|
|
}
|
2023-03-11 09:23:46 +00:00
|
|
|
|
|
|
|
Result<SharedPtr<Inode>> create_file(const char* path)
|
|
|
|
{
|
|
|
|
auto parser = TRY(PathParser::create(path));
|
|
|
|
auto parent_path = TRY(parser.dirname());
|
|
|
|
|
|
|
|
auto parent_inode = TRY(resolve_path(parent_path.chars()));
|
|
|
|
|
|
|
|
auto child_name = TRY(parser.basename());
|
|
|
|
|
|
|
|
// kdbgln("vfs: creating file '%s' in parent '%s'", child_name.chars(), parent_path.chars());
|
|
|
|
|
|
|
|
return parent_inode->create_file(child_name.chars());
|
|
|
|
}
|
2023-02-03 21:18:52 +00:00
|
|
|
}
|