From 5b43345b7d441facf2ab1a40492ec97d5e7c61c0 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 25 Feb 2023 19:22:05 +0100 Subject: [PATCH] kernel: Implement creating file inodes in tmpfs directories --- kernel/src/fs/VFS.h | 9 +++++++++ kernel/src/fs/tmpfs/FileSystem.cpp | 28 ++++++++++++++++++++++++++-- kernel/src/fs/tmpfs/FileSystem.h | 13 +++++++++++++ kernel/src/main.cpp | 3 +++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/kernel/src/fs/VFS.h b/kernel/src/fs/VFS.h index 92c4d98e..9c89f273 100644 --- a/kernel/src/fs/VFS.h +++ b/kernel/src/fs/VFS.h @@ -14,8 +14,12 @@ namespace VFS class Inode { public: + // Directory-specific methods virtual Result> find(const char* name) const = 0; + virtual Result> 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> create_file(const char*) override + { + return err(ENOTDIR); + } + InodeType type() const override { return InodeType::RegularFile; diff --git a/kernel/src/fs/tmpfs/FileSystem.cpp b/kernel/src/fs/tmpfs/FileSystem.cpp index 3d55ffa7..4606743e 100644 --- a/kernel/src/fs/tmpfs/FileSystem.cpp +++ b/kernel/src/fs/tmpfs/FileSystem.cpp @@ -25,6 +25,8 @@ namespace TmpFS Result> FileSystem::create_dir_inode() { SharedPtr inode = TRY(make_shared()); + 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> 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 DirInode::add_entry(SharedPtr inode, const char* name) + { + Entry entry = { inode, TRY(OwnedStringView::from_string_literal(name)) }; + + TRY(m_entries.try_append(move(entry))); + + return {}; + } + + Result> DirInode::create_file(const char* name) + { + auto inode = TRY(m_fs->create_file_inode()); + + TRY(add_entry(inode, name)); + + return inode; } } diff --git a/kernel/src/fs/tmpfs/FileSystem.h b/kernel/src/fs/tmpfs/FileSystem.h index c6ac85ae..044a26a5 100644 --- a/kernel/src/fs/tmpfs/FileSystem.h +++ b/kernel/src/fs/tmpfs/FileSystem.h @@ -2,6 +2,7 @@ #include "fs/VFS.h" #include #include +#include #include namespace TmpFS @@ -96,10 +97,22 @@ namespace TmpFS return VFS::InodeType::Directory; } + Result> create_file(const char* name) override; + + Result add_entry(SharedPtr inode, const char* name); + virtual ~DirInode() = default; private: VFS::FileSystem* m_fs; usize m_inode_number; + + struct Entry + { + SharedPtr inode; + OwnedStringView name; + }; + + Vector m_entries; }; } diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index b65c997d..3427cf6e 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -61,6 +61,9 @@ Result 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))) {