diff options
Diffstat (limited to 'kernel/include/kernel/filesystem/open_file_table.hpp')
| -rw-r--r-- | kernel/include/kernel/filesystem/open_file_table.hpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/kernel/include/kernel/filesystem/open_file_table.hpp b/kernel/include/kernel/filesystem/open_file_table.hpp new file mode 100644 index 0000000..7e754ac --- /dev/null +++ b/kernel/include/kernel/filesystem/open_file_table.hpp @@ -0,0 +1,66 @@ +#ifndef TEACH_OS_KERNEL_FILESYSTEM_OPEN_FILE_TABLE_HPP +#define TEACH_OS_KERNEL_FILESYSTEM_OPEN_FILE_TABLE_HPP + +#include <kernel/filesystem/open_file_descriptor.hpp> + +#include <kstd/memory> +#include <kstd/unikstd.h> +#include <kstd/vector> + +#include <cstddef> + +namespace kernel::filesystem +{ + /** + @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 + { + /** + @brief Initialize the global open file table. This method creates the singleton instance of the open file table. + @warning Panics if called more than once. + */ + auto static init() -> void; + + /** + @brief Get the global open file table instance. + @return A reference to the global open file table. + @warning Panics if the open file table has not been initialized. + */ + auto static get() -> open_file_table &; + + /** + @brief Destructor for the open file table. + */ + ~open_file_table() = default; + + /** + @brief Add a file to the open file table. + @param fd The file descriptor to add. + @return The file descriptor index assigned to the file, or -1 on failure. + */ + auto add_file(kstd::shared_ptr<open_file_descriptor> const & fd) -> kstd::ssize_t; + + /** + @brief Get a file from the open file table. + @param fd The file descriptor index to retrieve. + @return A pointer to the requested file descriptor, or a null pointer if not found. + */ + [[nodiscard]] auto file(size_t fd) const -> kstd::shared_ptr<open_file_descriptor>; + + /** + @brief Remove a file from the open file table. + @param fd The file descriptor index to remove. + @return 0 on success, or -1 on failure. + */ + auto remove_file(size_t fd) -> kstd::ssize_t; + + private: + open_file_table() = default; + + kstd::vector<kstd::shared_ptr<open_file_descriptor>> m_open_files{}; + }; +} // namespace kernel::filesystem + +#endif
\ No newline at end of file |
