VFS+TmpFS: Add support for creating subdirectories (mkdir)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-03-10 21:09:08 +01:00
parent 41203df472
commit ff770b7328
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 40 additions and 2 deletions

View File

@ -19,6 +19,8 @@ namespace VFS
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
virtual Result<SharedPtr<Inode>> create_subdirectory(const char* name) = 0;
// Generic methods
virtual FileSystem& fs() const = 0;
@ -42,6 +44,11 @@ namespace VFS
return err(ENOTDIR);
}
Result<SharedPtr<Inode>> create_subdirectory(const char*) override
{
return err(ENOTDIR);
}
InodeType type() const override
{
return InodeType::RegularFile;

View File

@ -26,13 +26,17 @@ namespace TmpFS
Result<SharedPtr<VFS::Inode>> FileSystem::create_dir_inode(SharedPtr<VFS::Inode> parent)
{
SharedPtr<DirInode> inode = TRY(make_shared<DirInode>());
TRY(inode->add_entry(inode, "."));
TRY(inode->add_entry(parent ? parent : (SharedPtr<VFS::Inode>)inode, ".."));
inode->set_self(inode, {});
inode->set_fs(*this, {});
inode->set_inode_number(m_next_inode_number, {});
TRY(m_inodes.try_append(inode));
m_next_inode_number++;
return (SharedPtr<VFS::Inode>)inode;
}
@ -68,4 +72,13 @@ namespace TmpFS
return inode;
}
Result<SharedPtr<VFS::Inode>> DirInode::create_subdirectory(const char* name)
{
auto inode = TRY(m_fs->create_dir_inode(m_self));
TRY(add_entry(inode, name));
return inode;
}
}

View File

@ -80,6 +80,11 @@ namespace TmpFS
m_inode_number = inum;
}
void set_self(SharedPtr<VFS::Inode> self, Badge<FileSystem>)
{
m_self = self;
}
Result<SharedPtr<VFS::Inode>> find(const char* name) const override;
VFS::FileSystem& fs() const override
@ -98,6 +103,7 @@ namespace TmpFS
}
Result<SharedPtr<VFS::Inode>> create_file(const char* name) override;
Result<SharedPtr<VFS::Inode>> create_subdirectory(const char* name) override;
Result<void> add_entry(SharedPtr<VFS::Inode> inode, const char* name);
@ -107,6 +113,8 @@ namespace TmpFS
VFS::FileSystem* m_fs;
usize m_inode_number;
SharedPtr<VFS::Inode> m_self;
struct Entry
{
SharedPtr<VFS::Inode> inode;

View File

@ -52,14 +52,24 @@ static Result<void> try_init_vfs()
VFS::root_fs = TRY(TmpFS::FileSystem::create());
VFS::Inode& root_inode = VFS::root_inode();
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_subdirectory("etc"));
kinfoln("root inode's 'usr' entry inode number: %zu", TRY(root_inode.find("usr"))->inode_number());
kinfoln("root inode's 'etc' entry inode number: %zu", TRY(root_inode.find("etc"))->inode_number());
auto& etc = *TRY(root_inode.find("etc"));
kinfoln("etc inode's '.' entry inode number: %zu", TRY(etc.find("."))->inode_number());
kinfoln("etc inode's '..' entry inode number: %zu", TRY(etc.find(".."))->inode_number());
TRY(etc.create_file("passwd"));
kinfoln("etc inode's 'passwd' entry inode number: %zu", TRY(etc.find("passwd"))->inode_number());
return {};
}