blob: 10674ba789c7043fbec7e412db37eb0887141675 (
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
|
#ifndef TEACHOS_KERNEL_VFS_OPEN_FILE_TABLE_HPP
#define TEACHOS_KERNEL_VFS_OPEN_FILE_TABLE_HPP
#include <kernel/vfs/open_file_descriptor.hpp>
#include <kstd/memory.hpp>
#include <kstd/result.hpp>
#include <kstd/system_error.hpp>
#include <kstd/vector.hpp>
#include <cstddef>
namespace kernel::vfs
{
//! @brief A table for managing file descriptors in the filesystem.
//!
//! This class provides methods for adding, retrieving, and removing open file descriptors.
struct open_file_table
{
//! Initialize the global open file table.
//!
//! @warning This function panics if called more than once.
auto static init() -> void;
//! Get the global open file table instance.
//!
//! @warning Panics if the open file table has not been initialized.
//!
//! @return A reference to the global open file table.
auto static get() -> open_file_table &;
//! Add a file to the open file table.
//!
//! @param fd The file descriptor to add.
//! @return The file descriptor index assigned to the file on success, an error otherwise.
auto add_file(kstd::shared_ptr<open_file_descriptor> const & fd) -> kstd::result<std::size_t>;
//! Get a file from the open file table.
//!
//! @param fd The file descriptor index to retrieve.
//! @return The requested file descriptor on success, an error otherwise.
[[nodiscard]] auto file(size_t fd) const -> kstd::result<kstd::shared_ptr<open_file_descriptor>>;
//! Remove a file from the open file table.
//!
//! @param fd The file descriptor index to remove.
//! @return Nothin on success, an error otherwise.
auto remove_file(size_t fd) -> kstd::result<void>;
private:
open_file_table() = default;
kstd::vector<kstd::shared_ptr<open_file_descriptor>> m_open_files{};
};
} // namespace kernel::vfs
#endif
|