Start working on a VFS implementation #22
@ -14,8 +14,12 @@ namespace VFS
|
||||
class Inode
|
||||
{
|
||||
public:
|
||||
// Directory-specific methods
|
||||
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
|
||||
|
||||
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
|
||||
|
||||
// Generic methods
|
||||
virtual FileSystem& fs() const = 0;
|
||||
|
||||
virtual ~Inode() = default;
|
||||
@ -33,6 +37,11 @@ namespace VFS
|
||||
return err(ENOTDIR);
|
||||
}
|
||||
|
||||
Result<SharedPtr<Inode>> create_file(const char*) override
|
||||
{
|
||||
return err(ENOTDIR);
|
||||
}
|
||||
|
||||
InodeType type() const override
|
||||
{
|
||||
return InodeType::RegularFile;
|
||||
|
@ -25,6 +25,8 @@ namespace TmpFS
|
||||
Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode()
|
||||
{
|
||||
SharedPtr<DirInode> inode = TRY(make_shared<DirInode>());
|
||||
TRY(inode->add_entry(inode, "."));
|
||||
|
||||
inode->set_fs(*this, {});
|
||||
inode->set_inode_number(m_next_inode_number, {});
|
||||
TRY(m_inodes.try_append(inode));
|
||||
@ -39,7 +41,29 @@ namespace TmpFS
|
||||
|
||||
Result<SharedPtr<VFS::Inode>> DirInode::find(const char* name) const
|
||||
{
|
||||
ignore(name);
|
||||
todo();
|
||||
for (const auto& entry : m_entries)
|
||||
{
|
||||
if (!strcmp(name, entry.name.chars())) return entry.inode;
|
||||
}
|
||||
|
||||
return err(ENOENT);
|
||||
}
|
||||
|
||||
Result<void> DirInode::add_entry(SharedPtr<VFS::Inode> inode, const char* name)
|
||||
{
|
||||
Entry entry = { inode, TRY(OwnedStringView::from_string_literal(name)) };
|
||||
|
||||
TRY(m_entries.try_append(move(entry)));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<SharedPtr<VFS::Inode>> DirInode::create_file(const char* name)
|
||||
{
|
||||
auto inode = TRY(m_fs->create_file_inode());
|
||||
|
||||
TRY(add_entry(inode, name));
|
||||
|
||||
return inode;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "fs/VFS.h"
|
||||
#include <luna/Atomic.h>
|
||||
#include <luna/Badge.h>
|
||||
#include <luna/OwnedStringView.h>
|
||||
#include <luna/Vector.h>
|
||||
|
||||
namespace TmpFS
|
||||
@ -96,10 +97,22 @@ namespace TmpFS
|
||||
return VFS::InodeType::Directory;
|
||||
}
|
||||
|
||||
Result<SharedPtr<VFS::Inode>> create_file(const char* name) override;
|
||||
|
||||
Result<void> add_entry(SharedPtr<VFS::Inode> inode, const char* name);
|
||||
|
||||
virtual ~DirInode() = default;
|
||||
|
||||
private:
|
||||
VFS::FileSystem* m_fs;
|
||||
usize m_inode_number;
|
||||
|
||||
struct Entry
|
||||
{
|
||||
SharedPtr<VFS::Inode> inode;
|
||||
OwnedStringView name;
|
||||
};
|
||||
|
||||
Vector<Entry> m_entries;
|
||||
};
|
||||
}
|
||||
|
@ -61,6 +61,9 @@ Result<void> init()
|
||||
VFS::Inode& root_inode = VFS::root_inode();
|
||||
kinfoln("root inode number: %zu", root_inode.inode_number());
|
||||
|
||||
TRY(root_inode.create_file("usr"));
|
||||
kinfoln("root inode's 'usr' entry inode number: %zu", TRY(root_inode.find("usr"))->inode_number());
|
||||
|
||||
TarStream::Entry entry;
|
||||
while (TRY(g_initrd.read_next_entry(entry)))
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user