From 22a32e2ab9fcb779a7359cec9b0244f262661288 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Wed, 11 Mar 2026 18:15:54 +0100 Subject: use optional instead of pointer, improve error handling (do not just panic, return std::nullopt) --- .../include/filesystem/file_descriptor_table.hpp | 7 +++++-- kernel/filesystem/src/file_descriptor_table.cpp | 24 ++++++++-------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/kernel/filesystem/include/filesystem/file_descriptor_table.hpp b/kernel/filesystem/include/filesystem/file_descriptor_table.hpp index a865d32..44fd428 100644 --- a/kernel/filesystem/include/filesystem/file_descriptor_table.hpp +++ b/kernel/filesystem/include/filesystem/file_descriptor_table.hpp @@ -4,6 +4,7 @@ #include "open_file_description.hpp" #include +#include namespace filesystem { @@ -15,13 +16,15 @@ namespace filesystem ~file_descriptor_table() = default; auto add_file(open_file_description & f) -> int; - auto get_file(int fd) -> open_file_description &; + auto get_file(int fd) -> std::optional; auto remove_file(int fd) -> void; private: file_descriptor_table() = default; - std::array m_open_files{}; // TODO BA-FS26 use kstd::vector when available + // TODO BA-FS26 use kstd::shared_ptr when available + // TODO BA-FS26 use kstd::vector when available + std::array, 32> m_open_files{}; }; } // namespace filesystem diff --git a/kernel/filesystem/src/file_descriptor_table.cpp b/kernel/filesystem/src/file_descriptor_table.cpp index 645da9f..286678c 100644 --- a/kernel/filesystem/src/file_descriptor_table.cpp +++ b/kernel/filesystem/src/file_descriptor_table.cpp @@ -37,29 +37,27 @@ namespace filesystem auto file_descriptor_table::add_file(open_file_description & f) -> int { - auto it = std::ranges::find(m_open_files, nullptr); + auto it = std::ranges::find_if(m_open_files, [](auto & open_file) { return !open_file.has_value(); }); if (it != m_open_files.end()) { - *it = &f; + *it = f; return static_cast(it - m_open_files.begin()); } return -1; } - auto file_descriptor_table::get_file(int fd) -> open_file_description & + auto file_descriptor_table::get_file(int fd) -> std::optional { - // TODO BA-FS26 do not panic, its an user error if (fd < 0) { - kapi::system::panic("[FILESYSTEM] get_file called with negative descriptor."); + return std::nullopt; } - // TODO BA-FS26 do not panic, its an user error auto const index = static_cast(fd); - if (index >= m_open_files.size() || m_open_files[index] == nullptr) + if (index >= m_open_files.size() || !m_open_files[index].has_value()) { - kapi::system::panic("[FILESYSTEM] get_file called with invalid descriptor."); + return std::nullopt; } return *m_open_files[index]; @@ -67,21 +65,17 @@ namespace filesystem auto file_descriptor_table::remove_file(int fd) -> void { - // TODO BA-FS26 do not panic, its an user error if (fd < 0) { - kapi::system::panic("[FILESYSTEM] remove_file called with negative descriptor."); + return; } - // TODO BA-FS26 do not panic, its an user error auto const index = static_cast(fd); if (index >= m_open_files.size()) { - kapi::system::panic("[FILESYSTEM] remove_file called with out-of-range descriptor."); + return; } - // TODO BA-FS26 - // delete m_open_files[index]; - m_open_files[index] = nullptr; + m_open_files[index].reset(); } } // namespace filesystem \ No newline at end of file -- cgit v1.2.3