PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
file_table.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/config.h>
4#include <kernel/fs/file.h>
5#include <kernel/sync/lock.h>
6
7#include <sys/bitmap.h>
8
9/**
10 * @brief File Table
11 * @defgroup kernel_fs_file_table File Table
12 * @ingroup kernel_fs
13 *
14 * The file table is a per-process structure that keeps track of all open files for a process.
15 *
16 * @{
17 */
18
19/**
20 * @brief File table structure.
21 * @struct file_table_t
22 */
29
30/**
31 * @brief Initialize a file table.
32 *
33 * @param table The file table to initialize.
34 */
35void file_table_init(file_table_t* table);
36
37/**
38 * @brief Deinitialize a file table.
39 *
40 * This will close all open files in the table.
41 *
42 * @param table The file table to deinitialize.
43 */
45
46/**
47 * @brief Get a file from its file descriptor.
48 *
49 * @param table The file table.
50 * @param fd The file descriptor.
51 * @return On success, a new reference to the file. On failure, returns `NULL` and `errno` is set to:
52 * - `EINVAL`: Invalid parameters.
53 * - `EBADF`: The file descriptor is invalid.
54 */
56
57/**
58 * @brief Allocate a new file descriptor for a file.
59 *
60 * @param table The file table.
61 * @param file The file to associate with the new file descriptor.
62 * @return On success, the allocated file descriptor. On failure, `ERR` and `errno` is set to:
63 * - `EINVAL`: Invalid parameters.
64 * - `EMFILE`: Too many open files.
65 */
67
68/**
69 * @brief Free a file descriptor.
70 *
71 * If the file has no other references, it will be closed.
72 *
73 * @param table The file table.
74 * @param fd The file descriptor to free.
75 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
76 * - `EINVAL`: Invalid parameters.
77 * - `EBADF`: The file descriptor is invalid.
78 */
80
81/**
82 * @brief Free a range of file descriptors.
83 *
84 * If the files have no other references, they will be closed.
85 *
86 * @param table The file table.
87 * @param min The minimum file descriptor to free, inclusive.
88 * @param max The maximum file descriptor to free, exclusive.
89 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
90 * - `EINVAL`: Invalid parameters.
91 */
93
94/**
95 * @brief Set a specific file descriptor to a file.
96 *
97 * If the file descriptor is already in use, the old file will be closed.
98 *
99 * @param table The file table.
100 * @param fd The file descriptor to set.
101 * @param file The file to associate with the file descriptor.
102 * @return On success, `fd`. On failure, `ERR` and `errno` is set to:
103 * - `EINVAL`: Invalid parameters.
104 * - `EBADF`: The file descriptor is invalid.
105 */
107
108/**
109 * @brief Duplicate a file descriptor.
110 *
111 * Allocates a new file descriptor that refers to the same file as `oldFd`.
112 *
113 * @param table The file table.
114 * @param oldFd The file descriptor to duplicate.
115 * @return On success, the new file descriptor. On failure, `ERR` and `errno` is set to:
116 * - `EINVAL`: Invalid parameters.
117 * - `EBADF`: The file descriptor is invalid.
118 * - `EMFILE`: Too many open files.
119 */
120fd_t file_table_dup(file_table_t* table, fd_t oldFd);
121
122/**
123 * @brief Duplicate a file descriptor to a specific file descriptor.
124 *
125 * If `newFd` is already in use, the old file will be closed.
126 *
127 * @param table The file table.
128 * @param oldFd The file descriptor to duplicate.
129 * @param newFd The file descriptor to duplicate to.
130 * @return On success, `newFd`. On failure, `ERR` and `errno` is set to:
131 * - `EINVAL`: Invalid parameters.
132 * - `EBADF`: One of the file descriptors is invalid.
133 * - `EMFILE`: Too many open files.
134 */
135fd_t file_table_dup2(file_table_t* table, fd_t oldFd, fd_t newFd);
136
137/**
138 * @brief Copy a file table, closing any overlapping file descriptors.
139 *
140 * @param dest The destination file table.
141 * @param src The source file table.
142 * @param min The minimum file descriptor to copy, inclusive.
143 * @param max The maximum file descriptor to copy, exclusive.
144 * @return On success, the number of copied file descriptors. On failure, `ERR` and `errno` is set to:
145 * - `EINVAL`: Invalid parameters.
146 */
148
149/**
150 * @brief Close all files in the file table.
151 *
152 * @param table The file table.
153 */
155
156/** @} */
fd_t file_table_dup2(file_table_t *table, fd_t oldFd, fd_t newFd)
Duplicate a file descriptor to a specific file descriptor.
Definition file_table.c:172
uint64_t file_table_free(file_table_t *table, fd_t fd)
Free a file descriptor.
Definition file_table.c:72
file_t * file_table_get(file_table_t *table, fd_t fd)
Get a file from its file descriptor.
Definition file_table.c:31
fd_t file_table_alloc(file_table_t *table, file_t *file)
Allocate a new file descriptor for a file.
Definition file_table.c:50
uint64_t file_table_copy(file_table_t *dest, file_table_t *src, fd_t min, fd_t max)
Copy a file table, closing any overlapping file descriptors.
Definition file_table.c:204
void file_table_close_all(file_table_t *table)
Close all files in the file table.
Definition file_table.c:235
void file_table_init(file_table_t *table)
Initialize a file table.
Definition file_table.c:7
fd_t file_table_set(file_table_t *table, fd_t fd, file_t *file)
Set a specific file descriptor to a file.
Definition file_table.c:117
void file_table_deinit(file_table_t *table)
Deinitialize a file table.
Definition file_table.c:17
uint64_t file_table_free_range(file_table_t *table, fd_t min, fd_t max)
Free a range of file descriptors.
Definition file_table.c:94
fd_t file_table_dup(file_table_t *table, fd_t oldFd)
Duplicate a file descriptor.
Definition file_table.c:144
#define CONFIG_MAX_FD
Maximum file descriptor configuration.
Definition config.h:47
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
static list_t files
Definition file.c:9
static dentry_t * file
Definition log_file.c:22
static pmm_bitmap_t bitmap
Definition pmm.c:39
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
File structure.
Definition file.h:39
File table structure.
Definition file_table.h:24
BITMAP_DEFINE(bitmap, CONFIG_MAX_FD)
lock_t lock
Definition file_table.h:27
A simple ticket lock implementation.
Definition lock.h:43