From 3a352078ded1138ab424c2830edf7d893b4b42e9 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 14 Sep 2024 19:44:51 +0300 Subject: [PATCH] Kernel: Make VirtualFileSystem::File non copyable --- kernel/include/kernel/FS/VirtualFileSystem.h | 25 +++++++++++++++++++- kernel/include/kernel/Process.h | 2 +- kernel/kernel/Networking/TCPSocket.cpp | 2 +- kernel/kernel/Networking/UNIX/Socket.cpp | 2 +- kernel/kernel/Process.cpp | 6 ++--- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/kernel/include/kernel/FS/VirtualFileSystem.h b/kernel/include/kernel/FS/VirtualFileSystem.h index 2257e3b4..7581aa6e 100644 --- a/kernel/include/kernel/FS/VirtualFileSystem.h +++ b/kernel/include/kernel/FS/VirtualFileSystem.h @@ -24,6 +24,29 @@ namespace Kernel struct File { + File() = default; + explicit File(BAN::RefPtr inode) + : inode(BAN::move(inode)) + { } + explicit File(BAN::RefPtr inode, BAN::String&& canonical_path) + : inode(BAN::move(inode)) + , canonical_path(BAN::move(canonical_path)) + { } + + File(const File&) = delete; + File(File&& other) + : inode(BAN::move(other.inode)) + , canonical_path(BAN::move(other.canonical_path)) + { } + + File& operator=(const File&) = delete; + File& operator=(File&& other) + { + inode = BAN::move(other.inode); + canonical_path = BAN::move(other.canonical_path); + return *this; + } + BAN::RefPtr inode; BAN::String canonical_path; }; @@ -31,7 +54,7 @@ namespace Kernel BAN::ErrorOr file_from_relative_path(const File& parent, const Credentials&, BAN::StringView, int); BAN::ErrorOr file_from_absolute_path(const Credentials& credentials, BAN::StringView path, int flags) { - return file_from_relative_path({ .inode = root_inode(), .canonical_path = {} }, credentials, path, flags); + return file_from_relative_path(File(root_inode()), credentials, path, flags); } private: diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index f4f41eb0..9fb5be9f 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -101,7 +101,7 @@ namespace Kernel BAN::ErrorOr sys_getpid() const { return pid(); } - BAN::ErrorOr open_inode(BAN::RefPtr, int flags); + BAN::ErrorOr open_inode(VirtualFileSystem::File&&, int flags); BAN::ErrorOr create_file_or_dir(BAN::StringView name, mode_t mode); BAN::ErrorOr open_file(BAN::StringView path, int oflag, mode_t = 0); diff --git a/kernel/kernel/Networking/TCPSocket.cpp b/kernel/kernel/Networking/TCPSocket.cpp index 5eb4abf2..c2a22909 100644 --- a/kernel/kernel/Networking/TCPSocket.cpp +++ b/kernel/kernel/Networking/TCPSocket.cpp @@ -123,7 +123,7 @@ namespace Kernel memcpy(address, &connection.target.address, *address_len); } - return TRY(Process::current().open_inode(return_inode, O_RDWR | flags)); + return TRY(Process::current().open_inode(VirtualFileSystem::File(return_inode, ""_sv), O_RDWR | flags)); } BAN::ErrorOr TCPSocket::connect_impl(const sockaddr* address, socklen_t address_len) diff --git a/kernel/kernel/Networking/UNIX/Socket.cpp b/kernel/kernel/Networking/UNIX/Socket.cpp index 63bdd078..c2df930b 100644 --- a/kernel/kernel/Networking/UNIX/Socket.cpp +++ b/kernel/kernel/Networking/UNIX/Socket.cpp @@ -104,7 +104,7 @@ namespace Kernel strncpy(sockaddr_un.sun_path, pending->m_bound_path.data(), copy_len); } - return TRY(Process::current().open_inode(return_inode, O_RDWR | flags)); + return TRY(Process::current().open_inode(VirtualFileSystem::File(return_inode, ""_sv), O_RDWR | flags)); } BAN::ErrorOr UnixDomainSocket::connect_impl(const sockaddr* address, socklen_t address_len) diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index ddbd4d05..8e975a6b 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -875,11 +875,11 @@ namespace Kernel return false; } - BAN::ErrorOr Process::open_inode(BAN::RefPtr inode, int flags) + BAN::ErrorOr Process::open_inode(VirtualFileSystem::File&& file, int flags) { - ASSERT(inode); + ASSERT(file.inode); LockGuard _(m_process_lock); - return TRY(m_open_file_descriptors.open(VirtualFileSystem::File { .inode = inode, .canonical_path = {} }, flags)); + return TRY(m_open_file_descriptors.open(BAN::move(file), flags)); } BAN::ErrorOr Process::open_file(BAN::StringView path, int flags, mode_t mode)