VFS: Add virtual method get() for getdents() and make existence checking occur in add_entry()

This commit is contained in:
apio 2023-03-28 21:15:26 +02:00
parent 0d54d0ece1
commit 8eb4d693ac
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 33 additions and 14 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <luna/SharedPtr.h> #include <luna/SharedPtr.h>
#include <luna/StaticString.h>
#include <sys/types.h> #include <sys/types.h>
namespace VFS namespace VFS
@ -12,6 +13,14 @@ namespace VFS
}; };
class FileSystem; class FileSystem;
class Inode;
struct DirectoryEntry
{
public:
SharedPtr<Inode> inode;
StaticString<128> name;
};
class Inode class Inode
{ {
@ -19,6 +28,8 @@ namespace VFS
// Directory-specific methods // Directory-specific methods
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0; virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
virtual Option<DirectoryEntry> get(usize index) const = 0;
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0; virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
virtual Result<SharedPtr<Inode>> create_subdirectory(const char* name) = 0; virtual Result<SharedPtr<Inode>> create_subdirectory(const char* name) = 0;
@ -60,6 +71,11 @@ namespace VFS
return err(ENOTDIR); return err(ENOTDIR);
} }
Option<DirectoryEntry> get(usize) const override
{
return {};
}
Result<SharedPtr<Inode>> create_file(const char*) override Result<SharedPtr<Inode>> create_file(const char*) override
{ {
return err(ENOTDIR); return err(ENOTDIR);
@ -96,6 +112,11 @@ namespace VFS
return err(ENOTDIR); return err(ENOTDIR);
} }
Option<DirectoryEntry> get(usize) const override
{
return {};
}
Result<SharedPtr<Inode>> create_file(const char*) override Result<SharedPtr<Inode>> create_file(const char*) override
{ {
return err(ENOTDIR); return err(ENOTDIR);

View File

@ -63,9 +63,18 @@ namespace TmpFS
return err(ENOENT); return err(ENOENT);
} }
Option<VFS::DirectoryEntry> DirInode::get(usize index) const
{
if (index >= m_entries.size()) return {};
return m_entries[index];
}
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, name }; if (find(name).has_value()) return err(EEXIST);
VFS::DirectoryEntry entry { inode, name };
TRY(m_entries.try_append(move(entry))); TRY(m_entries.try_append(move(entry)));
@ -74,8 +83,6 @@ namespace TmpFS
Result<SharedPtr<VFS::Inode>> DirInode::create_file(const char* name) Result<SharedPtr<VFS::Inode>> DirInode::create_file(const char* name)
{ {
if (find(name).has_value()) return err(EEXIST);
auto inode = TRY(m_fs->create_file_inode()); auto inode = TRY(m_fs->create_file_inode());
TRY(add_entry(inode, name)); TRY(add_entry(inode, name));
@ -85,8 +92,6 @@ namespace TmpFS
Result<SharedPtr<VFS::Inode>> DirInode::create_subdirectory(const char* name) Result<SharedPtr<VFS::Inode>> DirInode::create_subdirectory(const char* name)
{ {
if (find(name).has_value()) return err(EEXIST);
auto inode = TRY(m_fs->create_dir_inode(m_self)); auto inode = TRY(m_fs->create_dir_inode(m_self));
TRY(add_entry(inode, name)); TRY(add_entry(inode, name));

View File

@ -185,6 +185,7 @@ namespace TmpFS
} }
Result<SharedPtr<VFS::Inode>> find(const char* name) const override; Result<SharedPtr<VFS::Inode>> find(const char* name) const override;
Option<VFS::DirectoryEntry> get(usize index) const override;
Result<usize> read(u8*, usize, usize) const override Result<usize> read(u8*, usize, usize) const override
{ {
@ -251,12 +252,6 @@ namespace TmpFS
SharedPtr<VFS::Inode> m_self; SharedPtr<VFS::Inode> m_self;
struct Entry Vector<VFS::DirectoryEntry> m_entries;
{
SharedPtr<VFS::Inode> inode;
StaticString<128> name;
};
Vector<Entry> m_entries;
}; };
} }

View File

@ -24,8 +24,6 @@ Result<u64> sys_mknod(Registers*, SyscallArgs args)
auto parent = TRY(VFS::resolve_path(dirname.chars())); auto parent = TRY(VFS::resolve_path(dirname.chars()));
if (parent->find(basename.chars()).has_value()) return err(EEXIST);
auto inode = TRY(parent->fs().create_device_inode(maj, min)); auto inode = TRY(parent->fs().create_device_inode(maj, min));
TRY(parent->add_entry(inode, basename.chars())); TRY(parent->add_entry(inode, basename.chars()));