kernel: Implement creating file inodes in tmpfs directories

This commit is contained in:
apio 2023-02-25 19:22:05 +01:00
parent a65f4311aa
commit 01b6294f76
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 51 additions and 2 deletions

View File

@ -14,8 +14,12 @@ namespace VFS
class Inode class Inode
{ {
public: public:
// Directory-specific methods
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0; 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 FileSystem& fs() const = 0;
virtual ~Inode() = default; virtual ~Inode() = default;
@ -33,6 +37,11 @@ namespace VFS
return err(ENOTDIR); return err(ENOTDIR);
} }
Result<SharedPtr<Inode>> create_file(const char*) override
{
return err(ENOTDIR);
}
InodeType type() const override InodeType type() const override
{ {
return InodeType::RegularFile; return InodeType::RegularFile;

View File

@ -25,6 +25,8 @@ namespace TmpFS
Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode() Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode()
{ {
SharedPtr<DirInode> inode = TRY(make_shared<DirInode>()); SharedPtr<DirInode> inode = TRY(make_shared<DirInode>());
TRY(inode->add_entry(inode, "."));
inode->set_fs(*this, {}); inode->set_fs(*this, {});
inode->set_inode_number(m_next_inode_number, {}); inode->set_inode_number(m_next_inode_number, {});
TRY(m_inodes.try_append(inode)); TRY(m_inodes.try_append(inode));
@ -39,7 +41,29 @@ namespace TmpFS
Result<SharedPtr<VFS::Inode>> DirInode::find(const char* name) const Result<SharedPtr<VFS::Inode>> DirInode::find(const char* name) const
{ {
ignore(name); for (const auto& entry : m_entries)
todo(); {
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;
} }
} }

View File

@ -2,6 +2,7 @@
#include "fs/VFS.h" #include "fs/VFS.h"
#include <luna/Atomic.h> #include <luna/Atomic.h>
#include <luna/Badge.h> #include <luna/Badge.h>
#include <luna/OwnedStringView.h>
#include <luna/Vector.h> #include <luna/Vector.h>
namespace TmpFS namespace TmpFS
@ -96,10 +97,22 @@ namespace TmpFS
return VFS::InodeType::Directory; 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; virtual ~DirInode() = default;
private: private:
VFS::FileSystem* m_fs; VFS::FileSystem* m_fs;
usize m_inode_number; usize m_inode_number;
struct Entry
{
SharedPtr<VFS::Inode> inode;
OwnedStringView name;
};
Vector<Entry> m_entries;
}; };
} }

View File

@ -61,6 +61,9 @@ Result<void> init()
VFS::Inode& root_inode = VFS::root_inode(); VFS::Inode& root_inode = VFS::root_inode();
kinfoln("root inode number: %zu", root_inode.inode_number()); 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; TarStream::Entry entry;
while (TRY(g_initrd.read_next_entry(entry))) while (TRY(g_initrd.read_next_entry(entry)))
{ {