blob: 645da9f810452c3ee91a784461ba08beaa6acd6d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#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 &
{
// TODO BA-FS26 do not panic, its an user error
if (fd < 0)
{
kapi::system::panic("[FILESYSTEM] get_file called with negative descriptor.");
}
// 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)
{
kapi::system::panic("[FILESYSTEM] get_file called with invalid descriptor.");
}
return *m_open_files[index];
}
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.");
}
// 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.");
}
// TODO BA-FS26
// delete m_open_files[index];
m_open_files[index] = nullptr;
}
} // namespace filesystem
|