Kernel: Update ErrorOr API and add path find to VFS
These two are done on the same commit since Changes to Shell were annoying to make work with only one change
This commit is contained in:
@@ -14,6 +14,8 @@ namespace Kernel
|
||||
|
||||
virtual const BAN::RefCounted<Inode> root_inode() const override { return m_root_inode; }
|
||||
|
||||
BAN::ErrorOr<BAN::RefCounted<Inode>> from_absolute_path(BAN::StringView);
|
||||
|
||||
private:
|
||||
VirtualFileSystem(BAN::RefCounted<Inode> root_inode)
|
||||
: m_root_inode(root_inode)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <BAN/StringView.h>
|
||||
#include <BAN/Vector.h>
|
||||
#include <kernel/FS/VirtualFileSystem.h>
|
||||
|
||||
namespace Kernel
|
||||
@@ -23,4 +25,18 @@ namespace Kernel
|
||||
return s_instance != nullptr;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<BAN::RefCounted<Inode>> VirtualFileSystem::from_absolute_path(BAN::StringView path)
|
||||
{
|
||||
if (path.front() != '/')
|
||||
return BAN::Error::from_string("Path must be an absolute path");
|
||||
|
||||
auto inode = root_inode();
|
||||
auto path_parts = TRY(path.split('/'));
|
||||
|
||||
for (BAN::StringView part : path_parts)
|
||||
inode = TRY(inode->directory_find(part));
|
||||
|
||||
return inode;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -512,7 +512,7 @@ namespace Input
|
||||
{
|
||||
auto error_or = s_key_event_queue.push({ .key = key, .modifiers = modifiers, .pressed = pressed });
|
||||
if (error_or.is_error())
|
||||
dwarnln("{}", error_or.get_error());
|
||||
dwarnln("{}", error_or.error());
|
||||
}
|
||||
s_keyboard_key_buffer_size -= index + 1;
|
||||
memmove(s_keyboard_key_buffer, s_keyboard_key_buffer + index, s_keyboard_key_buffer_size);
|
||||
|
||||
@@ -268,28 +268,16 @@ argument_done:
|
||||
BAN::StringView path = (arguments.size() == 2) ? arguments[1].sv() : "/";
|
||||
if (path.front() != '/')
|
||||
return TTY_PRINTLN("ls currently works only with absolute paths");
|
||||
path = path.substring(1);
|
||||
|
||||
auto directory = VirtualFileSystem::get().root_inode();
|
||||
auto directory_or_error = VirtualFileSystem::get().from_absolute_path(path);
|
||||
if (directory_or_error.is_error())
|
||||
return TTY_PRINTLN("{}", directory_or_error.error());
|
||||
auto directory = directory_or_error.release_value();
|
||||
ASSERT(directory->ifdir());
|
||||
|
||||
if (arguments.size() == 2)
|
||||
{
|
||||
auto path_parts = MUST(arguments[1].sv().split('/'));
|
||||
for (auto part : path_parts)
|
||||
{
|
||||
auto inode_or_error = directory->directory_find(part);
|
||||
if (inode_or_error.is_error())
|
||||
return TTY_PRINTLN("{}", inode_or_error.get_error().get_message());
|
||||
directory = inode_or_error.value();
|
||||
if (!directory->ifdir())
|
||||
return TTY_PRINTLN("expected argument to be path to directory");
|
||||
}
|
||||
}
|
||||
|
||||
auto inodes_or_error = directory->directory_inodes();
|
||||
if (inodes_or_error.is_error())
|
||||
return TTY_PRINTLN("{}", inodes_or_error.get_error().get_message());
|
||||
return TTY_PRINTLN("{}", inodes_or_error.error());
|
||||
auto& inodes = inodes_or_error.value();
|
||||
|
||||
auto mode_string = [](Inode::Mode mode)
|
||||
@@ -324,22 +312,16 @@ argument_done:
|
||||
if (arguments.size() > 2)
|
||||
return TTY_PRINTLN("usage: 'cat path'");
|
||||
|
||||
auto file = VirtualFileSystem::get().root_inode();
|
||||
|
||||
auto path_parts = MUST(arguments[1].sv().split('/'));
|
||||
for (auto part : path_parts)
|
||||
{
|
||||
auto inode_or_error = file->directory_find(part);
|
||||
if (inode_or_error.is_error())
|
||||
return TTY_PRINTLN("{}", inode_or_error.get_error().get_message());
|
||||
file = inode_or_error.value();
|
||||
}
|
||||
auto file_or_error = VirtualFileSystem::get().from_absolute_path(arguments[1]);
|
||||
if (file_or_error.is_error())
|
||||
return TTY_PRINTLN("{}", file_or_error.error());
|
||||
auto file = file_or_error.release_value();
|
||||
|
||||
auto data_or_error = file->read_all();
|
||||
if (data_or_error.is_error())
|
||||
return TTY_PRINTLN("{}", data_or_error.get_error().get_message());
|
||||
return TTY_PRINTLN("{}", data_or_error.error());
|
||||
auto data = data_or_error.release_value();
|
||||
|
||||
auto& data = data_or_error.value();
|
||||
TTY_PRINTLN("{}", BAN::StringView((const char*)data.data(), data.size()));
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user