aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/file_descriptor_table.cpp
diff options
context:
space:
mode:
authormarcel.braun <marcel.braun@ost.ch>2026-03-17 19:36:20 +0100
committermarcel.braun <marcel.braun@ost.ch>2026-03-17 19:36:20 +0100
commit3ace886a9e9f044cd48de51f0a15aceb02bfa9b2 (patch)
tree1dc00e8802ab8fb60809b1f55ae7baadf9e430e1 /kernel/src/filesystem/file_descriptor_table.cpp
parent59504cfd677dd3e9d9ddb0deea4df7614efedb84 (diff)
downloadteachos-3ace886a9e9f044cd48de51f0a15aceb02bfa9b2.tar.xz
teachos-3ace886a9e9f044cd48de51f0a15aceb02bfa9b2.zip
Clean up project folder structure
Diffstat (limited to 'kernel/src/filesystem/file_descriptor_table.cpp')
-rw-r--r--kernel/src/filesystem/file_descriptor_table.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/kernel/src/filesystem/file_descriptor_table.cpp b/kernel/src/filesystem/file_descriptor_table.cpp
new file mode 100644
index 0000000..814322e
--- /dev/null
+++ b/kernel/src/filesystem/file_descriptor_table.cpp
@@ -0,0 +1,82 @@
+#include "kernel/filesystem/file_descriptor_table.hpp"
+
+#include "kapi/system.hpp"
+
+#include "kernel/filesystem/open_file_description.hpp"
+
+#include <algorithm>
+#include <cstddef>
+#include <optional>
+
+namespace filesystem
+{
+ namespace
+ {
+ constinit auto static global_file_descriptor_table = std::optional<file_descriptor_table>{};
+ } // namespace
+
+ auto file_descriptor_table::init() -> void
+ {
+ if (global_file_descriptor_table)
+ {
+ kapi::system::panic("[FILESYSTEM] File descriptor table has already been initialized.");
+ }
+
+ global_file_descriptor_table.emplace(file_descriptor_table{});
+ }
+
+ auto file_descriptor_table::get() -> file_descriptor_table &
+ {
+ if (!global_file_descriptor_table)
+ {
+ kapi::system::panic("[FILESYSTEM] File descriptor table has not been initialized.");
+ }
+
+ return *global_file_descriptor_table;
+ }
+
+ auto file_descriptor_table::add_file(open_file_description & file_description) -> int
+ {
+ auto it = std::ranges::find_if(m_open_files, [](auto & open_file) { return !open_file.has_value(); });
+ if (it != m_open_files.end())
+ {
+ *it = file_description;
+ return static_cast<int>(it - m_open_files.begin());
+ }
+
+ m_open_files.push_back(file_description);
+ return static_cast<int>(m_open_files.size() - 1);
+ }
+
+ auto file_descriptor_table::get_file(int fd) const -> std::optional<open_file_description>
+ {
+ if (fd < 0)
+ {
+ return std::nullopt;
+ }
+
+ auto const index = static_cast<size_t>(fd);
+ if (index >= m_open_files.size() || !m_open_files.at(fd).has_value())
+ {
+ return std::nullopt;
+ }
+
+ return m_open_files.at(fd);
+ }
+
+ auto file_descriptor_table::remove_file(int fd) -> void
+ {
+ if (fd < 0)
+ {
+ return;
+ }
+
+ auto const index = static_cast<size_t>(fd);
+ if (index >= m_open_files.size())
+ {
+ return;
+ }
+
+ m_open_files.at(fd).reset();
+ }
+} // namespace filesystem \ No newline at end of file