From ff770b73288ad07f88cd3a0965a40e6fddc784ad Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 10 Mar 2023 21:09:08 +0100 Subject: [PATCH] VFS+TmpFS: Add support for creating subdirectories (mkdir) --- kernel/src/fs/VFS.h | 7 +++++++ kernel/src/fs/tmpfs/FileSystem.cpp | 13 +++++++++++++ kernel/src/fs/tmpfs/FileSystem.h | 8 ++++++++ kernel/src/main.cpp | 14 ++++++++++++-- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/kernel/src/fs/VFS.h b/kernel/src/fs/VFS.h index 70e2fb12..ea065ddb 100644 --- a/kernel/src/fs/VFS.h +++ b/kernel/src/fs/VFS.h @@ -19,6 +19,8 @@ namespace VFS virtual Result> create_file(const char* name) = 0; + virtual Result> create_subdirectory(const char* name) = 0; + // Generic methods virtual FileSystem& fs() const = 0; @@ -42,6 +44,11 @@ namespace VFS return err(ENOTDIR); } + Result> create_subdirectory(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 b0f2df99..64dae8a0 100644 --- a/kernel/src/fs/tmpfs/FileSystem.cpp +++ b/kernel/src/fs/tmpfs/FileSystem.cpp @@ -26,13 +26,17 @@ namespace TmpFS Result> FileSystem::create_dir_inode(SharedPtr parent) { SharedPtr inode = TRY(make_shared()); + TRY(inode->add_entry(inode, ".")); TRY(inode->add_entry(parent ? parent : (SharedPtr)inode, "..")); + inode->set_self(inode, {}); inode->set_fs(*this, {}); inode->set_inode_number(m_next_inode_number, {}); + TRY(m_inodes.try_append(inode)); m_next_inode_number++; + return (SharedPtr)inode; } @@ -68,4 +72,13 @@ namespace TmpFS return inode; } + + Result> DirInode::create_subdirectory(const char* name) + { + auto inode = TRY(m_fs->create_dir_inode(m_self)); + + TRY(add_entry(inode, name)); + + return inode; + } } diff --git a/kernel/src/fs/tmpfs/FileSystem.h b/kernel/src/fs/tmpfs/FileSystem.h index 6e1e5f39..6212daea 100644 --- a/kernel/src/fs/tmpfs/FileSystem.h +++ b/kernel/src/fs/tmpfs/FileSystem.h @@ -80,6 +80,11 @@ namespace TmpFS m_inode_number = inum; } + void set_self(SharedPtr self, Badge) + { + m_self = self; + } + Result> find(const char* name) const override; VFS::FileSystem& fs() const override @@ -98,6 +103,7 @@ namespace TmpFS } Result> create_file(const char* name) override; + Result> create_subdirectory(const char* name) override; Result add_entry(SharedPtr inode, const char* name); @@ -107,6 +113,8 @@ namespace TmpFS VFS::FileSystem* m_fs; usize m_inode_number; + SharedPtr m_self; + struct Entry { SharedPtr inode; diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 92f11c0a..b015f611 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -52,14 +52,24 @@ static Result try_init_vfs() VFS::root_fs = TRY(TmpFS::FileSystem::create()); VFS::Inode& root_inode = VFS::root_inode(); + kinfoln("root inode number: %zu", root_inode.inode_number()); kinfoln("root inode's '.' entry inode number: %zu", TRY(root_inode.find("."))->inode_number()); kinfoln("root inode's '..' entry inode number: %zu", TRY(root_inode.find(".."))->inode_number()); - TRY(root_inode.create_file("usr")); + TRY(root_inode.create_subdirectory("etc")); - kinfoln("root inode's 'usr' entry inode number: %zu", TRY(root_inode.find("usr"))->inode_number()); + kinfoln("root inode's 'etc' entry inode number: %zu", TRY(root_inode.find("etc"))->inode_number()); + + auto& etc = *TRY(root_inode.find("etc")); + + kinfoln("etc inode's '.' entry inode number: %zu", TRY(etc.find("."))->inode_number()); + kinfoln("etc inode's '..' entry inode number: %zu", TRY(etc.find(".."))->inode_number()); + + TRY(etc.create_file("passwd")); + + kinfoln("etc inode's 'passwd' entry inode number: %zu", TRY(etc.find("passwd"))->inode_number()); return {}; }