diff options
| author | Marcel Braun <marcel.braun@ost.ch> | 2026-04-28 09:32:53 +0200 |
|---|---|---|
| committer | Marcel Braun <marcel.braun@ost.ch> | 2026-04-28 09:32:53 +0200 |
| commit | 9d2ec7c3999a550a5c5cdbc2bd952452cd4b7fc0 (patch) | |
| tree | b1e9347c3f03302afb1d0851eefba25dbf0f1c82 /kernel/src/filesystem/open_file_table.cpp | |
| parent | f6f10575f75ac23d06e1d94f7861611503daa7af (diff) | |
| parent | d349812c2e1e6a7d62f53d1c959137794e8a648d (diff) | |
| download | teachos-9d2ec7c3999a550a5c5cdbc2bd952452cd4b7fc0.tar.xz teachos-9d2ec7c3999a550a5c5cdbc2bd952452cd4b7fc0.zip | |
Merge branch 'refactoring' into 'develop-BA-FS26'
Refactoring
See merge request teachos/kernel!27
Diffstat (limited to 'kernel/src/filesystem/open_file_table.cpp')
| -rw-r--r-- | kernel/src/filesystem/open_file_table.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/kernel/src/filesystem/open_file_table.cpp b/kernel/src/filesystem/open_file_table.cpp new file mode 100644 index 0000000..e47d229 --- /dev/null +++ b/kernel/src/filesystem/open_file_table.cpp @@ -0,0 +1,98 @@ +#include <kernel/filesystem/open_file_table.hpp> + +#include <kernel/filesystem/open_file_descriptor.hpp> + +#include <kapi/system.hpp> + +#include <kstd/memory> + +#include <algorithm> +#include <cstddef> +#include <optional> + +namespace +{ + constinit auto static global_open_file_table = std::optional<kernel::filesystem::open_file_table>{}; +} // namespace + +namespace kernel::filesystem +{ + auto open_file_table::init() -> void + { + if (global_open_file_table) + { + kapi::system::panic("[FILESYSTEM] Open file table has already been initialized."); + } + + global_open_file_table.emplace(open_file_table{}); + } + + auto open_file_table::get() -> open_file_table & + { + if (!global_open_file_table) + { + kapi::system::panic("[FILESYSTEM] Open file table has not been initialized."); + } + + return *global_open_file_table; + } + + auto open_file_table::add_file(kstd::shared_ptr<open_file_descriptor> const & file_descriptor) -> int + { + if (!file_descriptor) + { + return -1; + } + + auto it = std::ranges::find_if(m_open_files, [](auto const & open_file) { return open_file == nullptr; }); + if (it != m_open_files.end()) + { + *it = file_descriptor; + return static_cast<int>(it - m_open_files.begin()); + } + + m_open_files.push_back(file_descriptor); + return static_cast<int>(m_open_files.size() - 1); + } + + auto open_file_table::get_file(int fd) const -> kstd::shared_ptr<open_file_descriptor> + { + if (fd < 0) + { + return nullptr; + } + + auto const index = static_cast<size_t>(fd); + if (index >= m_open_files.size()) + { + return nullptr; + } + + return m_open_files.at(index); + } + + auto open_file_table::remove_file(int fd) -> int + { + if (fd < 0) + { + return -1; + } + + auto const index = static_cast<size_t>(fd); + if (index >= m_open_files.size()) + { + return -1; + } + + m_open_files.at(index) = nullptr; + return 0; + } +} // namespace kernel::filesystem + +namespace kernel::tests::filesystem::open_file_table +{ + auto deinit() -> void + { + global_open_file_table.reset(); + } +} // namespace kernel::tests::filesystem::open_file_table |
