From c422d11682254e4fa2b47d62cc259a85d37558d4 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 3 Feb 2023 22:18:52 +0100 Subject: [PATCH] kernel: Start working on a VFS implementation using OOP and SharedPtr --- kernel/CMakeLists.txt | 2 ++ kernel/src/fs/VFS.cpp | 6 ++++ kernel/src/fs/VFS.h | 37 ++++++++++++++++++++++ kernel/src/fs/tmpfs/FileSystem.cpp | 33 +++++++++++++++++++ kernel/src/fs/tmpfs/FileSystem.h | 51 ++++++++++++++++++++++++++++++ kernel/src/main.cpp | 3 ++ 6 files changed, 132 insertions(+) create mode 100644 kernel/src/fs/VFS.cpp create mode 100644 kernel/src/fs/VFS.h create mode 100644 kernel/src/fs/tmpfs/FileSystem.cpp create mode 100644 kernel/src/fs/tmpfs/FileSystem.h diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 96029772..7cc661f6 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -26,6 +26,8 @@ set(SOURCES src/sys/clock_gettime.cpp src/sys/allocate_memory.cpp src/sys/usleep.cpp + src/fs/VFS.cpp + src/fs/tmpfs/FileSystem.cpp src/InitRD.cpp src/ELF.cpp ) diff --git a/kernel/src/fs/VFS.cpp b/kernel/src/fs/VFS.cpp new file mode 100644 index 00000000..dfa9b609 --- /dev/null +++ b/kernel/src/fs/VFS.cpp @@ -0,0 +1,6 @@ +#include "fs/VFS.h" + +namespace VFS +{ + SharedPtr root_fs; +} diff --git a/kernel/src/fs/VFS.h b/kernel/src/fs/VFS.h new file mode 100644 index 00000000..ea9a8084 --- /dev/null +++ b/kernel/src/fs/VFS.h @@ -0,0 +1,37 @@ +#pragma once +#include + +namespace VFS +{ + enum class InodeType + { + RegularFile, + Directory + }; + + class FileSystem; + + class Inode + { + public: + virtual Result find(const char* name) const = 0; + + virtual FileSystem& fs() const = 0; + + virtual ~Inode() = default; + + InodeType type; + }; + + class FileSystem + { + public: + virtual Inode& root() const = 0; + + virtual ~FileSystem() = default; + }; + + extern SharedPtr root_fs; + + Result resolve_path(const char* path); +} diff --git a/kernel/src/fs/tmpfs/FileSystem.cpp b/kernel/src/fs/tmpfs/FileSystem.cpp new file mode 100644 index 00000000..45631fc4 --- /dev/null +++ b/kernel/src/fs/tmpfs/FileSystem.cpp @@ -0,0 +1,33 @@ +#include "fs/tmpfs/FileSystem.h" +#include + +namespace TmpFS +{ + Result> FileSystem::create() + { + SharedPtr root = TRY(make_shared()); + SharedPtr fs = TRY(adopt_shared(new (std::nothrow) FileSystem())); + root->set_fs(*fs, {}); + TRY(fs->set_root(root)); + return (SharedPtr)fs; + } + + FileSystem::FileSystem() + { + } + + Result FileSystem::set_root(SharedPtr root) + { + m_root_inode = root; + return m_inodes.try_append(root); + } + + Inode::Inode() + { + } + + void Inode::set_fs(FileSystem& fs, Badge) + { + m_fs = &fs; + } +} diff --git a/kernel/src/fs/tmpfs/FileSystem.h b/kernel/src/fs/tmpfs/FileSystem.h new file mode 100644 index 00000000..81a4b996 --- /dev/null +++ b/kernel/src/fs/tmpfs/FileSystem.h @@ -0,0 +1,51 @@ +#pragma once +#include "fs/VFS.h" +#include +#include + +namespace TmpFS +{ + class FileSystem : public VFS::FileSystem + { + public: + VFS::Inode& root() const override + { + return *m_root_inode; + } + + static Result> create(); + + virtual ~FileSystem() = default; + + private: + FileSystem(); + + Result set_root(SharedPtr root); + + SharedPtr m_root_inode; + Vector> m_inodes; + }; + + class Inode : public VFS::Inode + { + public: + Inode(); + + void set_fs(FileSystem& fs, Badge); + + Result find(const char*) const override + { + return err(ENOTSUP); + } + + VFS::FileSystem& fs() const override + { + return *m_fs; + } + + virtual ~Inode() = default; + + private: + VFS::FileSystem* m_fs; + }; +} diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index c2635455..b743d553 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -7,6 +7,7 @@ #include "arch/Timer.h" #include "boot/Init.h" #include "config.h" +#include "fs/tmpfs/FileSystem.h" #include "memory/Heap.h" #include "memory/KernelVM.h" #include "memory/MemoryManager.h" @@ -55,6 +56,8 @@ Result init() Thread::init(); Scheduler::init(); + VFS::root_fs = TRY(TmpFS::FileSystem::create()); + TarStream::Entry entry; while (TRY(g_initrd.read_next_entry(entry))) {