kernel: Start working on a VFS implementation using OOP and SharedPtr
This commit is contained in:
parent
71b981175e
commit
c422d11682
@ -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
|
||||
)
|
||||
|
6
kernel/src/fs/VFS.cpp
Normal file
6
kernel/src/fs/VFS.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "fs/VFS.h"
|
||||
|
||||
namespace VFS
|
||||
{
|
||||
SharedPtr<FileSystem> root_fs;
|
||||
}
|
37
kernel/src/fs/VFS.h
Normal file
37
kernel/src/fs/VFS.h
Normal 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);
|
||||
}
|
33
kernel/src/fs/tmpfs/FileSystem.cpp
Normal file
33
kernel/src/fs/tmpfs/FileSystem.cpp
Normal 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;
|
||||
}
|
||||
}
|
51
kernel/src/fs/tmpfs/FileSystem.h
Normal file
51
kernel/src/fs/tmpfs/FileSystem.h
Normal 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;
|
||||
};
|
||||
}
|
@ -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<void> init()
|
||||
Thread::init();
|
||||
Scheduler::init();
|
||||
|
||||
VFS::root_fs = TRY(TmpFS::FileSystem::create());
|
||||
|
||||
TarStream::Entry entry;
|
||||
while (TRY(g_initrd.read_next_entry(entry)))
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user