kernel: Implement creating file inodes in tmpfs directories
This commit is contained in:
parent
a65f4311aa
commit
01b6294f76
@ -14,8 +14,12 @@ namespace VFS
|
|||||||
class Inode
|
class Inode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
// Directory-specific methods
|
||||||
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
|
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
|
||||||
|
|
||||||
|
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
|
||||||
|
|
||||||
|
// Generic methods
|
||||||
virtual FileSystem& fs() const = 0;
|
virtual FileSystem& fs() const = 0;
|
||||||
|
|
||||||
virtual ~Inode() = default;
|
virtual ~Inode() = default;
|
||||||
@ -33,6 +37,11 @@ namespace VFS
|
|||||||
return err(ENOTDIR);
|
return err(ENOTDIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<SharedPtr<Inode>> create_file(const char*) override
|
||||||
|
{
|
||||||
|
return err(ENOTDIR);
|
||||||
|
}
|
||||||
|
|
||||||
InodeType type() const override
|
InodeType type() const override
|
||||||
{
|
{
|
||||||
return InodeType::RegularFile;
|
return InodeType::RegularFile;
|
||||||
|
@ -25,6 +25,8 @@ namespace TmpFS
|
|||||||
Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode()
|
Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode()
|
||||||
{
|
{
|
||||||
SharedPtr<DirInode> inode = TRY(make_shared<DirInode>());
|
SharedPtr<DirInode> inode = TRY(make_shared<DirInode>());
|
||||||
|
TRY(inode->add_entry(inode, "."));
|
||||||
|
|
||||||
inode->set_fs(*this, {});
|
inode->set_fs(*this, {});
|
||||||
inode->set_inode_number(m_next_inode_number, {});
|
inode->set_inode_number(m_next_inode_number, {});
|
||||||
TRY(m_inodes.try_append(inode));
|
TRY(m_inodes.try_append(inode));
|
||||||
@ -39,7 +41,29 @@ namespace TmpFS
|
|||||||
|
|
||||||
Result<SharedPtr<VFS::Inode>> DirInode::find(const char* name) const
|
Result<SharedPtr<VFS::Inode>> DirInode::find(const char* name) const
|
||||||
{
|
{
|
||||||
ignore(name);
|
for (const auto& entry : m_entries)
|
||||||
todo();
|
{
|
||||||
|
if (!strcmp(name, entry.name.chars())) return entry.inode;
|
||||||
|
}
|
||||||
|
|
||||||
|
return err(ENOENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> DirInode::add_entry(SharedPtr<VFS::Inode> inode, const char* name)
|
||||||
|
{
|
||||||
|
Entry entry = { inode, TRY(OwnedStringView::from_string_literal(name)) };
|
||||||
|
|
||||||
|
TRY(m_entries.try_append(move(entry)));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<SharedPtr<VFS::Inode>> DirInode::create_file(const char* name)
|
||||||
|
{
|
||||||
|
auto inode = TRY(m_fs->create_file_inode());
|
||||||
|
|
||||||
|
TRY(add_entry(inode, name));
|
||||||
|
|
||||||
|
return inode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "fs/VFS.h"
|
#include "fs/VFS.h"
|
||||||
#include <luna/Atomic.h>
|
#include <luna/Atomic.h>
|
||||||
#include <luna/Badge.h>
|
#include <luna/Badge.h>
|
||||||
|
#include <luna/OwnedStringView.h>
|
||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
|
|
||||||
namespace TmpFS
|
namespace TmpFS
|
||||||
@ -96,10 +97,22 @@ namespace TmpFS
|
|||||||
return VFS::InodeType::Directory;
|
return VFS::InodeType::Directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<SharedPtr<VFS::Inode>> create_file(const char* name) override;
|
||||||
|
|
||||||
|
Result<void> add_entry(SharedPtr<VFS::Inode> inode, const char* name);
|
||||||
|
|
||||||
virtual ~DirInode() = default;
|
virtual ~DirInode() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
VFS::FileSystem* m_fs;
|
VFS::FileSystem* m_fs;
|
||||||
usize m_inode_number;
|
usize m_inode_number;
|
||||||
|
|
||||||
|
struct Entry
|
||||||
|
{
|
||||||
|
SharedPtr<VFS::Inode> inode;
|
||||||
|
OwnedStringView name;
|
||||||
|
};
|
||||||
|
|
||||||
|
Vector<Entry> m_entries;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,9 @@ Result<void> init()
|
|||||||
VFS::Inode& root_inode = VFS::root_inode();
|
VFS::Inode& root_inode = VFS::root_inode();
|
||||||
kinfoln("root inode number: %zu", root_inode.inode_number());
|
kinfoln("root inode number: %zu", root_inode.inode_number());
|
||||||
|
|
||||||
|
TRY(root_inode.create_file("usr"));
|
||||||
|
kinfoln("root inode's 'usr' entry inode number: %zu", TRY(root_inode.find("usr"))->inode_number());
|
||||||
|
|
||||||
TarStream::Entry entry;
|
TarStream::Entry entry;
|
||||||
while (TRY(g_initrd.read_next_entry(entry)))
|
while (TRY(g_initrd.read_next_entry(entry)))
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user