VFS: Add virtual method get() for getdents() and make existence checking occur in add_entry()
This commit is contained in:
parent
0d54d0ece1
commit
8eb4d693ac
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <luna/SharedPtr.h>
|
||||
#include <luna/StaticString.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace VFS
|
||||
@ -12,6 +13,14 @@ namespace VFS
|
||||
};
|
||||
|
||||
class FileSystem;
|
||||
class Inode;
|
||||
|
||||
struct DirectoryEntry
|
||||
{
|
||||
public:
|
||||
SharedPtr<Inode> inode;
|
||||
StaticString<128> name;
|
||||
};
|
||||
|
||||
class Inode
|
||||
{
|
||||
@ -19,6 +28,8 @@ namespace VFS
|
||||
// Directory-specific methods
|
||||
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_subdirectory(const char* name) = 0;
|
||||
@ -60,6 +71,11 @@ namespace VFS
|
||||
return err(ENOTDIR);
|
||||
}
|
||||
|
||||
Option<DirectoryEntry> get(usize) const override
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<SharedPtr<Inode>> create_file(const char*) override
|
||||
{
|
||||
return err(ENOTDIR);
|
||||
@ -96,6 +112,11 @@ namespace VFS
|
||||
return err(ENOTDIR);
|
||||
}
|
||||
|
||||
Option<DirectoryEntry> get(usize) const override
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<SharedPtr<Inode>> create_file(const char*) override
|
||||
{
|
||||
return err(ENOTDIR);
|
||||
|
@ -63,9 +63,18 @@ namespace TmpFS
|
||||
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)
|
||||
{
|
||||
Entry entry { inode, name };
|
||||
if (find(name).has_value()) return err(EEXIST);
|
||||
|
||||
VFS::DirectoryEntry entry { inode, name };
|
||||
|
||||
TRY(m_entries.try_append(move(entry)));
|
||||
|
||||
@ -74,8 +83,6 @@ namespace TmpFS
|
||||
|
||||
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());
|
||||
|
||||
TRY(add_entry(inode, name));
|
||||
@ -85,8 +92,6 @@ namespace TmpFS
|
||||
|
||||
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));
|
||||
|
||||
TRY(add_entry(inode, name));
|
||||
|
@ -185,6 +185,7 @@ namespace TmpFS
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
@ -251,12 +252,6 @@ namespace TmpFS
|
||||
|
||||
SharedPtr<VFS::Inode> m_self;
|
||||
|
||||
struct Entry
|
||||
{
|
||||
SharedPtr<VFS::Inode> inode;
|
||||
StaticString<128> name;
|
||||
};
|
||||
|
||||
Vector<Entry> m_entries;
|
||||
Vector<VFS::DirectoryEntry> m_entries;
|
||||
};
|
||||
}
|
||||
|
@ -24,8 +24,6 @@ Result<u64> sys_mknod(Registers*, SyscallArgs args)
|
||||
|
||||
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));
|
||||
TRY(parent->add_entry(inode, basename.chars()));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user