PatchworkOS  19e446b
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 */
23typedef struct file_table
24{
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 Close all files in the file table.
83 *
84 * @param table The file table.
85 */
87
88/**
89 * @brief Close all files in the file table with the specified mode.
90 *
91 * @param table The file table.
92 * @param mode The mode to close files with.
93 */
95
96/**
97 * @brief Free a range of file descriptors.
98 *
99 * If the files have no other references, they will be closed.
100 *
101 * @param table The file table.
102 * @param min The minimum file descriptor to free, inclusive.
103 * @param max The maximum file descriptor to free, exclusive.
104 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
105 * - `EINVAL`: Invalid parameters.
106 */
108
109/**
110 * @brief Set a specific file descriptor to a file.
111 *
112 * If the file descriptor is already in use, the old file will be closed.
113 *
114 * @param table The file table.
115 * @param fd The file descriptor to set.
116 * @param file The file to associate with the file descriptor.
117 * @return On success, `fd`. On failure, `ERR` and `errno` is set to:
118 * - `EINVAL`: Invalid parameters.
119 * - `EBADF`: The file descriptor is invalid.
120 */
121fd_t file_table_set(file_table_t* table, fd_t fd, file_t* file);
122
123/**
124 * @brief Duplicate a file descriptor.
125 *
126 * Allocates a new file descriptor that refers to the same file as `oldFd`.
127 *
128 * @param table The file table.
129 * @param oldFd The file descriptor to duplicate.
130 * @return On success, the new file descriptor. On failure, `ERR` and `errno` is set to:
131 * - `EINVAL`: Invalid parameters.
132 * - `EBADF`: The file descriptor is invalid.
133 * - `EMFILE`: Too many open files.
134 */
135fd_t file_table_dup(file_table_t* table, fd_t oldFd);
136
137/**
138 * @brief Duplicate a file descriptor to a specific file descriptor.
139 *
140 * If `newFd` is already in use, the old file will be closed.
141 *
142 * @param table The file table.
143 * @param oldFd The file descriptor to duplicate.
144 * @param newFd The file descriptor to duplicate to.
145 * @return On success, `newFd`. On failure, `ERR` and `errno` is set to:
146 * - `EINVAL`: Invalid parameters.
147 * - `EBADF`: One of the file descriptors is invalid.
148 * - `EMFILE`: Too many open files.
149 */
150fd_t file_table_dup2(file_table_t* table, fd_t oldFd, fd_t newFd);
151
152/**
153 * @brief Copy a file table, closing any overlapping file descriptors.
154 *
155 * @param dest The destination file table.
156 * @param src The source file table.
157 * @param min The minimum file descriptor to copy, inclusive.
158 * @param max The maximum file descriptor to copy, exclusive.
159 * @return On success, the number of copied file descriptors. On failure, `ERR` and `errno` is set to:
160 * - `EINVAL`: Invalid parameters.
161 */
163
164/** @} */
#define CONFIG_MAX_FD
Maximum file descriptor configuration.
Definition config.h:51
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:214
uint64_t file_table_close(file_table_t *table, fd_t fd)
Free a file descriptor.
Definition file_table.c:74
file_t * file_table_get(file_table_t *table, fd_t fd)
Get a file from its file descriptor.
Definition file_table.c:33
void file_table_close_mode(file_table_t *table, mode_t mode)
Close all files in the file table with the specified mode.
Definition file_table.c:116
uint64_t file_table_close_range(file_table_t *table, fd_t min, fd_t max)
Free a range of file descriptors.
Definition file_table.c:136
fd_t file_table_open(file_table_t *table, file_t *file)
Allocate a new file descriptor for a file.
Definition file_table.c:52
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:246
void file_table_close_all(file_table_t *table)
Close all files in the file table.
Definition file_table.c:96
void file_table_init(file_table_t *table)
Initialize a file table.
Definition file_table.c:9
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:159
void file_table_deinit(file_table_t *table)
Deinitialize a file table.
Definition file_table.c:19
fd_t file_table_dup(file_table_t *table, fd_t oldFd)
Duplicate a file descriptor.
Definition file_table.c:186
mode_t
Path flags and permissions.
Definition path.h:79
__UINT64_TYPE__ fd_t
File descriptor type.
Definition fd_t.h:10
static list_t files
Definition file.c:9
__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:44