TmpFS: Use a fixed char array in DirInode's Entry instead of OwnedStringView

This commit is contained in:
apio 2023-02-27 15:04:29 +01:00
parent f88dc050a9
commit 8ba571a30e
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 15 additions and 7 deletions

View File

@ -57,7 +57,7 @@ namespace VFS
virtual Result<SharedPtr<Inode>> create_file_inode() = 0; virtual Result<SharedPtr<Inode>> create_file_inode() = 0;
virtual Result<SharedPtr<Inode>> create_dir_inode() = 0; virtual Result<SharedPtr<Inode>> create_dir_inode(SharedPtr<Inode> parent) = 0;
virtual ~FileSystem() = default; virtual ~FileSystem() = default;
}; };

View File

@ -1,5 +1,6 @@
#include "fs/tmpfs/FileSystem.h" #include "fs/tmpfs/FileSystem.h"
#include <luna/Alloc.h> #include <luna/Alloc.h>
#include <luna/CString.h>
#include <luna/Ignore.h> #include <luna/Ignore.h>
namespace TmpFS namespace TmpFS
@ -7,7 +8,7 @@ namespace TmpFS
Result<SharedPtr<VFS::FileSystem>> FileSystem::create() Result<SharedPtr<VFS::FileSystem>> FileSystem::create()
{ {
SharedPtr<FileSystem> fs = TRY(adopt_shared(new (std::nothrow) FileSystem())); SharedPtr<FileSystem> fs = TRY(adopt_shared(new (std::nothrow) FileSystem()));
SharedPtr<VFS::Inode> root = TRY(fs->create_dir_inode()); SharedPtr<VFS::Inode> root = TRY(fs->create_dir_inode({}));
fs->set_root(root); fs->set_root(root);
return (SharedPtr<VFS::FileSystem>)fs; return (SharedPtr<VFS::FileSystem>)fs;
} }
@ -22,10 +23,11 @@ namespace TmpFS
return (SharedPtr<VFS::Inode>)inode; return (SharedPtr<VFS::Inode>)inode;
} }
Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode() Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode(SharedPtr<VFS::Inode> parent)
{ {
SharedPtr<DirInode> inode = TRY(make_shared<DirInode>()); SharedPtr<DirInode> inode = TRY(make_shared<DirInode>());
TRY(inode->add_entry(inode, ".")); TRY(inode->add_entry(inode, "."));
TRY(inode->add_entry(parent ? parent : (SharedPtr<VFS::Inode>)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, {});
@ -43,7 +45,7 @@ namespace TmpFS
{ {
for (const auto& entry : m_entries) for (const auto& entry : m_entries)
{ {
if (!strcmp(name, entry.name.chars())) return entry.inode; if (!strcmp(name, entry.name)) return entry.inode;
} }
return err(ENOENT); return err(ENOENT);
@ -51,7 +53,9 @@ namespace TmpFS
Result<void> DirInode::add_entry(SharedPtr<VFS::Inode> inode, const char* name) Result<void> DirInode::add_entry(SharedPtr<VFS::Inode> inode, const char* name)
{ {
Entry entry = { inode, TRY(OwnedStringView::from_string_literal(name)) }; Entry entry;
entry.inode = inode;
strlcpy(entry.name, name, sizeof(entry.name));
TRY(m_entries.try_append(move(entry))); TRY(m_entries.try_append(move(entry)));

View File

@ -16,7 +16,7 @@ namespace TmpFS
} }
Result<SharedPtr<VFS::Inode>> create_file_inode() override; Result<SharedPtr<VFS::Inode>> create_file_inode() override;
Result<SharedPtr<VFS::Inode>> create_dir_inode() override; Result<SharedPtr<VFS::Inode>> create_dir_inode(SharedPtr<VFS::Inode> parent) override;
static Result<SharedPtr<VFS::FileSystem>> create(); static Result<SharedPtr<VFS::FileSystem>> create();
@ -110,7 +110,7 @@ namespace TmpFS
struct Entry struct Entry
{ {
SharedPtr<VFS::Inode> inode; SharedPtr<VFS::Inode> inode;
OwnedStringView name; char name[128];
}; };
Vector<Entry> m_entries; Vector<Entry> m_entries;

View File

@ -61,7 +61,11 @@ 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());
kinfoln("root inode's '.' entry inode number: %zu", TRY(root_inode.find("."))->inode_number());
kinfoln("root inode's '..' entry inode number: %zu", TRY(root_inode.find(".."))->inode_number());
TRY(root_inode.create_file("usr")); TRY(root_inode.create_file("usr"));
kinfoln("root inode's 'usr' entry inode number: %zu", TRY(root_inode.find("usr"))->inode_number()); kinfoln("root inode's 'usr' entry inode number: %zu", TRY(root_inode.find("usr"))->inode_number());
TarStream::Entry entry; TarStream::Entry entry;