Compare commits
2 Commits
89958fbc74
...
cb4246b98d
Author | SHA1 | Date | |
---|---|---|---|
cb4246b98d | |||
51024f879d |
@ -27,6 +27,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)))
|
||||
{
|
||||
|
@ -117,12 +117,17 @@ template <typename T, class... Args> Result<SharedPtr<T>> make_shared(Args... ar
|
||||
return SharedPtr<T> { ptr, ref_count };
|
||||
}
|
||||
|
||||
// NOTE: ptr is deleted if any of the adopt_shared* functions fail to construct a SharedPtr.
|
||||
template <typename T> Result<SharedPtr<T>> adopt_shared(T* ptr)
|
||||
{
|
||||
using RefCount = __detail::RefCount;
|
||||
|
||||
auto guard = make_scope_guard([ptr] { delete ptr; });
|
||||
|
||||
RefCount* const ref_count = TRY(make<RefCount>());
|
||||
|
||||
guard.deactivate();
|
||||
|
||||
return SharedPtr<T> { ptr, ref_count };
|
||||
}
|
||||
|
||||
@ -138,13 +143,7 @@ template <typename T> Result<SharedPtr<T>> adopt_shared_from_owned(OwnedPtr<T>&&
|
||||
T* ptr = other.m_ptr;
|
||||
other.m_ptr = nullptr;
|
||||
|
||||
// FIXME: Should the pointee magically vanish on failure? Or go back into the OwnedPtr, even though it's been
|
||||
// moved...
|
||||
auto guard = make_scope_guard([&] { delete ptr; });
|
||||
|
||||
const SharedPtr<T> shared_ptr = TRY(adopt_shared(ptr));
|
||||
|
||||
guard.deactivate();
|
||||
|
||||
return shared_ptr;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user