aboutsummaryrefslogtreecommitdiff
path: root/kernel/filesystem
diff options
context:
space:
mode:
authorLukas Oesch <lukasoesch20@gmail.com>2026-03-11 18:15:54 +0100
committerLukas Oesch <lukasoesch20@gmail.com>2026-03-17 16:42:53 +0100
commit22a32e2ab9fcb779a7359cec9b0244f262661288 (patch)
tree8e76bb110bf821fe4ddf093a8776716b917d5f8c /kernel/filesystem
parent7430778a773cfdb97bed3c77c17f7c737706ba6a (diff)
downloadteachos-22a32e2ab9fcb779a7359cec9b0244f262661288.tar.xz
teachos-22a32e2ab9fcb779a7359cec9b0244f262661288.zip
use optional instead of pointer, improve error handling (do not just panic, return std::nullopt)
Diffstat (limited to 'kernel/filesystem')
-rw-r--r--kernel/filesystem/include/filesystem/file_descriptor_table.hpp7
-rw-r--r--kernel/filesystem/src/file_descriptor_table.cpp24
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 <array>
+#include <optional>
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<open_file_description>;
auto remove_file(int fd) -> void;
private:
file_descriptor_table() = default;
- std::array<open_file_description *, 32> 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<std::optional<open_file_description>, 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<int>(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<open_file_description>
{
- // 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<size_t>(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<size_t>(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