diff options
| author | Lukas Oesch <lukasoesch20@gmail.com> | 2026-03-11 18:15:54 +0100 |
|---|---|---|
| committer | Lukas Oesch <lukasoesch20@gmail.com> | 2026-03-17 16:42:53 +0100 |
| commit | 22a32e2ab9fcb779a7359cec9b0244f262661288 (patch) | |
| tree | 8e76bb110bf821fe4ddf093a8776716b917d5f8c /kernel/filesystem/src/file_descriptor_table.cpp | |
| parent | 7430778a773cfdb97bed3c77c17f7c737706ba6a (diff) | |
| download | teachos-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/src/file_descriptor_table.cpp')
| -rw-r--r-- | kernel/filesystem/src/file_descriptor_table.cpp | 24 |
1 files changed, 9 insertions, 15 deletions
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 |
