From 3e5645d45331bb03a6daf254a989628f1ff9000b Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 30 Sep 2023 19:13:11 +0300 Subject: [PATCH] Kernel: Add API for RamDirectoryInodes to delete containing inodes --- kernel/include/kernel/FS/Inode.h | 2 ++ kernel/include/kernel/FS/RamFS/Inode.h | 1 + kernel/kernel/FS/Inode.cpp | 9 +++++++++ kernel/kernel/FS/RamFS/Inode.cpp | 13 +++++++++++++ 4 files changed, 25 insertions(+) diff --git a/kernel/include/kernel/FS/Inode.h b/kernel/include/kernel/FS/Inode.h index 46d469a21d..9c4991d24d 100644 --- a/kernel/include/kernel/FS/Inode.h +++ b/kernel/include/kernel/FS/Inode.h @@ -89,6 +89,7 @@ namespace Kernel BAN::ErrorOr> find_inode(BAN::StringView); BAN::ErrorOr list_next_inodes(off_t, DirectoryEntryList*, size_t); BAN::ErrorOr create_file(BAN::StringView, mode_t, uid_t, gid_t); + BAN::ErrorOr delete_inode(BAN::StringView); // Link API BAN::ErrorOr link_target(); @@ -104,6 +105,7 @@ namespace Kernel virtual BAN::ErrorOr> find_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); } virtual BAN::ErrorOr list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) { return BAN::Error::from_errno(ENOTSUP); } virtual BAN::ErrorOr create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) { return BAN::Error::from_errno(ENOTSUP); } + virtual BAN::ErrorOr delete_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); } // Link API virtual BAN::ErrorOr link_target_impl() { return BAN::Error::from_errno(ENOTSUP); } diff --git a/kernel/include/kernel/FS/RamFS/Inode.h b/kernel/include/kernel/FS/RamFS/Inode.h index eca6852071..2b81923834 100644 --- a/kernel/include/kernel/FS/RamFS/Inode.h +++ b/kernel/include/kernel/FS/RamFS/Inode.h @@ -92,6 +92,7 @@ namespace Kernel virtual BAN::ErrorOr> find_inode_impl(BAN::StringView) override; virtual BAN::ErrorOr list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) override; virtual BAN::ErrorOr create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override; + virtual BAN::ErrorOr delete_inode_impl(BAN::StringView) override; private: static constexpr size_t m_name_max = NAME_MAX; diff --git a/kernel/kernel/FS/Inode.cpp b/kernel/kernel/FS/Inode.cpp index 91d850618c..1e68a4d9f6 100644 --- a/kernel/kernel/FS/Inode.cpp +++ b/kernel/kernel/FS/Inode.cpp @@ -84,6 +84,15 @@ namespace Kernel return create_file_impl(name, mode, uid, gid); } + BAN::ErrorOr Inode::delete_inode(BAN::StringView name) + { + LockGuard _(m_lock); + Thread::TerminateBlocker blocker(Thread::current()); + if (!mode().ifdir()) + return BAN::Error::from_errno(ENOTDIR); + return delete_inode_impl(name); + } + BAN::ErrorOr Inode::link_target() { LockGuard _(m_lock); diff --git a/kernel/kernel/FS/RamFS/Inode.cpp b/kernel/kernel/FS/RamFS/Inode.cpp index f53146279e..528dec4fa5 100644 --- a/kernel/kernel/FS/RamFS/Inode.cpp +++ b/kernel/kernel/FS/RamFS/Inode.cpp @@ -227,6 +227,19 @@ namespace Kernel return {}; } + BAN::ErrorOr RamDirectoryInode::delete_inode_impl(BAN::StringView name) + { + for (size_t i = 0; i < m_entries.size(); i++) + { + if (name == m_entries[i].name) + { + m_entries.remove(i); + return {}; + } + } + return BAN::Error::from_errno(ENOENT); + } + /* RAM SYMLINK INODE