kernel: Start working on a VFS implementation using OOP and SharedPtr
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
apio 2023-02-03 22:18:52 +01:00
parent 51024f879d
commit cb4246b98d
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 132 additions and 0 deletions

View File

@ -27,6 +27,8 @@ set(SOURCES
src/sys/clock_gettime.cpp src/sys/clock_gettime.cpp
src/sys/allocate_memory.cpp src/sys/allocate_memory.cpp
src/sys/usleep.cpp src/sys/usleep.cpp
src/fs/VFS.cpp
src/fs/tmpfs/FileSystem.cpp
src/InitRD.cpp src/InitRD.cpp
src/ELF.cpp src/ELF.cpp
) )

6
kernel/src/fs/VFS.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "fs/VFS.h"
namespace VFS
{
SharedPtr<FileSystem> root_fs;
}

37
kernel/src/fs/VFS.h Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include <luna/SharedPtr.h>
namespace VFS
{
enum class InodeType
{
RegularFile,
Directory
};
class FileSystem;
class Inode
{
public:
virtual Result<Inode*> 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<FileSystem> root_fs;
Result<Inode*> resolve_path(const char* path);
}

View File

@ -0,0 +1,33 @@
#include "fs/tmpfs/FileSystem.h"
#include <luna/Alloc.h>
namespace TmpFS
{
Result<SharedPtr<VFS::FileSystem>> FileSystem::create()
{
SharedPtr<Inode> root = TRY(make_shared<Inode>());
SharedPtr<FileSystem> fs = TRY(adopt_shared(new (std::nothrow) FileSystem()));
root->set_fs(*fs, {});
TRY(fs->set_root(root));
return (SharedPtr<VFS::FileSystem>)fs;
}
FileSystem::FileSystem()
{
}
Result<void> FileSystem::set_root(SharedPtr<VFS::Inode> root)
{
m_root_inode = root;
return m_inodes.try_append(root);
}
Inode::Inode()
{
}
void Inode::set_fs(FileSystem& fs, Badge<FileSystem>)
{
m_fs = &fs;
}
}

View File

@ -0,0 +1,51 @@
#pragma once
#include "fs/VFS.h"
#include <luna/Badge.h>
#include <luna/Vector.h>
namespace TmpFS
{
class FileSystem : public VFS::FileSystem
{
public:
VFS::Inode& root() const override
{
return *m_root_inode;
}
static Result<SharedPtr<VFS::FileSystem>> create();
virtual ~FileSystem() = default;
private:
FileSystem();
Result<void> set_root(SharedPtr<VFS::Inode> root);
SharedPtr<VFS::Inode> m_root_inode;
Vector<SharedPtr<VFS::Inode>> m_inodes;
};
class Inode : public VFS::Inode
{
public:
Inode();
void set_fs(FileSystem& fs, Badge<FileSystem>);
Result<VFS::Inode*> find(const char*) const override
{
return err(ENOTSUP);
}
VFS::FileSystem& fs() const override
{
return *m_fs;
}
virtual ~Inode() = default;
private:
VFS::FileSystem* m_fs;
};
}

View File

@ -7,6 +7,7 @@
#include "arch/Timer.h" #include "arch/Timer.h"
#include "boot/Init.h" #include "boot/Init.h"
#include "config.h" #include "config.h"
#include "fs/tmpfs/FileSystem.h"
#include "memory/Heap.h" #include "memory/Heap.h"
#include "memory/KernelVM.h" #include "memory/KernelVM.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
@ -55,6 +56,8 @@ Result<void> init()
Thread::init(); Thread::init();
Scheduler::init(); Scheduler::init();
VFS::root_fs = TRY(TmpFS::FileSystem::create());
TarStream::Entry entry; TarStream::Entry entry;
while (TRY(g_initrd.read_next_entry(entry))) while (TRY(g_initrd.read_next_entry(entry)))
{ {