aboutsummaryrefslogtreecommitdiff
path: root/kernel/filesystem/src/file_descriptor_table.cpp
diff options
context:
space:
mode:
authorLukas Oesch <lukasoesch20@gmail.com>2026-03-07 13:55:40 +0100
committerLukas Oesch <lukasoesch20@gmail.com>2026-03-17 16:42:49 +0100
commit2f4001cdb1f528d8a0d255d81ac3a8b9aa522fac (patch)
treec609c06cb272516ada077bfc95ad47d14f8c2b0b /kernel/filesystem/src/file_descriptor_table.cpp
parent79aaf6113a9eca042ccc6f656fac6c7c243c608b (diff)
downloadteachos-2f4001cdb1f528d8a0d255d81ac3a8b9aa522fac.tar.xz
teachos-2f4001cdb1f528d8a0d255d81ac3a8b9aa522fac.zip
implement first draft of a file_descriptor_table and open_file_description
Diffstat (limited to 'kernel/filesystem/src/file_descriptor_table.cpp')
-rw-r--r--kernel/filesystem/src/file_descriptor_table.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/kernel/filesystem/src/file_descriptor_table.cpp b/kernel/filesystem/src/file_descriptor_table.cpp
new file mode 100644
index 0000000..0c79431
--- /dev/null
+++ b/kernel/filesystem/src/file_descriptor_table.cpp
@@ -0,0 +1,81 @@
+#include "filesystem/file_descriptor_table.hpp"
+
+#include "kapi/system.hpp"
+
+#include "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 & f) -> int
+ {
+ auto it = std::ranges::find(m_open_files, nullptr);
+ if (it != m_open_files.end())
+ {
+ *it = &f;
+ return static_cast<int>(it - m_open_files.begin());
+ }
+
+ return -1;
+ }
+
+ auto file_descriptor_table::get_file(int fd) -> open_file_description &
+ {
+ if (fd < 0)
+ {
+ kapi::system::panic("[FILESYSTEM] get_file called with negative descriptor.");
+ }
+
+ auto const index = static_cast<size_t>(fd);
+ if (index >= m_open_files.size() || m_open_files[index] == nullptr)
+ {
+ kapi::system::panic("[FILESYSTEM] get_file called with invalid descriptor.");
+ }
+
+ return *m_open_files[index];
+ }
+
+ auto file_descriptor_table::remove_file(int fd) -> void
+ {
+ if (fd < 0)
+ {
+ kapi::system::panic("[FILESYSTEM] remove_file called with negative descriptor.");
+ }
+
+ 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.");
+ }
+
+ m_open_files[index] = nullptr;
+ }
+} // namespace filesystem \ No newline at end of file