From 8eb4d693ac50705a4dfeebfeafbf273165180f29 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 28 Mar 2023 21:15:26 +0200 Subject: [PATCH] VFS: Add virtual method get() for getdents() and make existence checking occur in add_entry() --- kernel/src/fs/VFS.h | 21 +++++++++++++++++++++ kernel/src/fs/tmpfs/FileSystem.cpp | 15 ++++++++++----- kernel/src/fs/tmpfs/FileSystem.h | 9 ++------- kernel/src/sys/mknod.cpp | 2 -- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/kernel/src/fs/VFS.h b/kernel/src/fs/VFS.h index 0b5410a7..bb7dae26 100644 --- a/kernel/src/fs/VFS.h +++ b/kernel/src/fs/VFS.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include namespace VFS @@ -12,6 +13,14 @@ namespace VFS }; class FileSystem; + class Inode; + + struct DirectoryEntry + { + public: + SharedPtr inode; + StaticString<128> name; + }; class Inode { @@ -19,6 +28,8 @@ namespace VFS // Directory-specific methods virtual Result> find(const char* name) const = 0; + virtual Option get(usize index) const = 0; + virtual Result> create_file(const char* name) = 0; virtual Result> create_subdirectory(const char* name) = 0; @@ -60,6 +71,11 @@ namespace VFS return err(ENOTDIR); } + Option get(usize) const override + { + return {}; + } + Result> create_file(const char*) override { return err(ENOTDIR); @@ -96,6 +112,11 @@ namespace VFS return err(ENOTDIR); } + Option get(usize) const override + { + return {}; + } + Result> create_file(const char*) override { return err(ENOTDIR); diff --git a/kernel/src/fs/tmpfs/FileSystem.cpp b/kernel/src/fs/tmpfs/FileSystem.cpp index 0282f061..6947bfd1 100644 --- a/kernel/src/fs/tmpfs/FileSystem.cpp +++ b/kernel/src/fs/tmpfs/FileSystem.cpp @@ -63,9 +63,18 @@ namespace TmpFS return err(ENOENT); } + Option DirInode::get(usize index) const + { + if (index >= m_entries.size()) return {}; + + return m_entries[index]; + } + Result DirInode::add_entry(SharedPtr 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> 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> 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)); diff --git a/kernel/src/fs/tmpfs/FileSystem.h b/kernel/src/fs/tmpfs/FileSystem.h index e1f26eac..82d5a496 100644 --- a/kernel/src/fs/tmpfs/FileSystem.h +++ b/kernel/src/fs/tmpfs/FileSystem.h @@ -185,6 +185,7 @@ namespace TmpFS } Result> find(const char* name) const override; + Option get(usize index) const override; Result read(u8*, usize, usize) const override { @@ -251,12 +252,6 @@ namespace TmpFS SharedPtr m_self; - struct Entry - { - SharedPtr inode; - StaticString<128> name; - }; - - Vector m_entries; + Vector m_entries; }; } diff --git a/kernel/src/sys/mknod.cpp b/kernel/src/sys/mknod.cpp index 3b669aa3..31bf99f8 100644 --- a/kernel/src/sys/mknod.cpp +++ b/kernel/src/sys/mknod.cpp @@ -24,8 +24,6 @@ Result 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()));