Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/io/File.h

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 6 мар. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_IO_FILE_H_
23 #define LSP_PLUG_IN_IO_FILE_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/common/types.h>
27 #include <lsp-plug.in/common/status.h>
28 #include <lsp-plug.in/runtime/LSPString.h>
29 #include <lsp-plug.in/io/Path.h>
30 #include <lsp-plug.in/stdlib/stdio.h>
31
32 #define IO_FILE_DEFAULT_BUF_SIZE 0x1000
33
34 namespace lsp
35 {
36 namespace io
37 {
38 /**
39 * Binary file interface, allows to perform read/write access
40 * to the file on local file system
41 */
42 class File
43 {
44 public:
45 enum seek_t {
46 FSK_SET,
47 FSK_CUR,
48 FSK_END
49 };
50
51 enum mode_t {
52 FM_READ = 1 << 0, // Open for reading
53 FM_WRITE = 1 << 1, // Open for writing
54 FM_CREATE = 1 << 2, // Create file if not exists
55 FM_TRUNC = 1 << 3, // Truncate file
56 FM_DIRECT = 1 << 4, // Do not use buffered input/output if possible
57 FM_EXCL = 1 << 5, // Do not create file if it already exists, return STATUS_ALREADY_EXISTS as error
58 FM_LOCK = 1 << 6, // Obtain an exclusive lock on a file
59 FM_NOWAIT = 1 << 7, // Do not wait for acquiring the exclusive lock
60
61 FM_READWRITE = FM_READ | FM_WRITE,
62 FM_WRITE_NEW = FM_CREATE | FM_WRITE | FM_TRUNC,
63 FM_READWRITE_NEW = FM_CREATE | FM_READ | FM_WRITE | FM_TRUNC
64 };
65
66 protected:
67 status_t nErrorCode;
68
69 protected:
70 inline status_t set_error(status_t error) { return nErrorCode = error; }
71
72 public:
73 explicit File();
74 File(const File &) = delete;
75 File(File &&) = delete;
76 virtual ~File();
77
78 File & operator = (const File &) = delete;
79 File & operator = (File &&) = delete;
80
81 public:
82 /**
83 * Read binary file
84 * @param dst target buffer to perform read
85 * @param count amount of bytes to read
86 * @return number of bytes read or negative status of operation,
87 * on end of file -STATUS_EOF is returned
88 */
89 virtual ssize_t read(void *dst, size_t count);
90
91 /**
92 * Perform positioned read of binary file
93 * @param pos offset in bytes relative to the beginning of the file
94 * @param dst target buffer to perform read
95 * @param count amount of bytes to read
96 * @return number of bytes read or status of operation
97 */
98 virtual ssize_t pread(wsize_t pos, void *dst, size_t count);
99
100 /**
101 * Write binary file.
102 * The implementation should write the most possible amount
103 * of bytes before exit. Such behaviour will simplify the caller's
104 * implementation.
105 * @param src source buffer to perform write
106 * @param count number of bytes to write
107 * @return status of operation
108 */
109 virtual ssize_t write(const void *src, size_t count);
110
111 /**
112 * Perform positioned write of binary file
113 * @param pos offset in bytes relative to the beginning of the file
114 * @param src source buffer to perform write
115 * @param count number of bytes to write
116 * @return status of operation
117 */
118 virtual ssize_t pwrite(wsize_t pos, const void *src, size_t count);
119
120 /**
121 * Perform seek to the specified position
122 * @param pos position to perform seek
123 * @param type seek type
124 * @return status of operation
125 */
126 virtual status_t seek(wssize_t pos, size_t type);
127
128 /**
129 * Obtain current file's position
130 * @return current file's position or negative error code
131 */
132 virtual wssize_t position();
133
134 /**
135 * Obtain current file's size
136 * @return current file's size or negative error code
137 */
138 virtual wssize_t size();
139
140 /**
141 * Get file attributes
142 * @param attr file attributes
143 * @return status of operation
144 */
145 virtual status_t stat(fattr_t *attr);
146
147 /**
148 * Truncate the file
149 * @param length the final file length
150 * @return status of operation
151 */
152 virtual status_t truncate(wsize_t length);
153
154 /**
155 * Return last error code
156 * @return last error code
157 */
158 inline status_t last_error() const { return nErrorCode; };
159
160 /**
161 * Return true if last read operations reached end of file
162 * @return true if last read operations reached end of file
163 */
164 inline bool eof() const { return nErrorCode == STATUS_EOF; };
165
166 /**
167 * Flush usespace file buffer
168 * @return status of operation
169 */
170 virtual status_t flush();
171
172 /**
173 * Sync file with the underlying storage
174 * @return status of operation
175 */
176 virtual status_t sync();
177
178 /**
179 * Close file
180 * @return status of operation
181 */
182 virtual status_t close();
183
184 public:
185
186 /**
187 * Obtain file information. If the file is symbolic link,
188 * return information about the symbolic link
189 * @param path UTF-8 encoded path to file
190 * @param attr structure to return file attributes
191 * @return status of operation
192 */
193 static status_t stat(const char *path, fattr_t *attr);
194
195 /**
196 * Obtain file information. If the file is symbolic link,
197 * return information about the symbolic link
198 * @param path path to file
199 * @param attr structure to return file attributes
200 * @return status of operation
201 */
202 static status_t stat(const LSPString *path, fattr_t *attr);
203
204 /**
205 * Obtain file information. If the file is symbolic link,
206 * return information about the symbolic link
207 * @param path path to file
208 * @param attr structure to return file attributes
209 * @return status of operation
210 */
211 static status_t stat(const Path *path, fattr_t *attr);
212
213 /**
214 * Obtain file information
215 * @param fd native file descriptor
216 * @param attr structure to return file attributes
217 * @return status of operation
218 */
219 static status_t stat(fhandle_t fd, fattr_t *attr);
220
221 /**
222 * Obtain file information
223 * @param fd stdio file descriptor
224 * @param attr structure to return file attributes
225 * @return status of operation
226 */
227 static status_t stat(FILE *fd, fattr_t *attr);
228
229 /**
230 * Obtain file information. If the file is symbolic link,
231 * return information about the file the link refers to
232 * @param path UTF-8 encoded path to file
233 * @param attr structure to return file attributes
234 * @return status of operation
235 */
236 static status_t sym_stat(const char *path, fattr_t *attr);
237
238 /**
239 * Obtain file information. If the file is symbolic link,
240 * return information about the file the link refers to
241 * @param path path to file
242 * @param attr structure to return file attributes
243 * @return status of operation
244 */
245 static status_t sym_stat(const LSPString *path, fattr_t *attr);
246
247 /**
248 * Obtain file information. If the file is symbolic link,
249 * return information about the file the link refers to
250 * @param path path to file
251 * @param attr structure to return file attributes
252 * @return status of operation
253 */
254 static status_t sym_stat(const Path *path, fattr_t *attr);
255
256 /**
257 * Delete file
258 * @param path path to file
259 * @return status of operation
260 */
261 static status_t remove(const char *path);
262
263 /**
264 * Delete file
265 * @param path path to file
266 * @return status of operation
267 */
268 static status_t remove(const LSPString *path);
269
270 /**
271 * Delete file
272 * @param path path to file
273 * @return status of operation
274 */
275 static status_t remove(const Path *path);
276
277 /**
278 * Rename the file. If the destination file exists, it will be replaced
279 *
280 * @param from source name
281 * @param to destination name
282 * @return status of operation
283 */
284 static status_t rename(const char *from, const char *to);
285 static status_t rename(const LSPString *from, const char *to);
286 static status_t rename(const Path *from, const char *to);
287
288 static status_t rename(const char *from, const LSPString *to);
289 static status_t rename(const LSPString *from, const LSPString *to);
290 static status_t rename(const Path *from, const LSPString *to);
291
292 static status_t rename(const char *from, const Path *to);
293 static status_t rename(const LSPString *from, const Path *to);
294 static status_t rename(const Path *from, const Path *to);
295
296 /**
297 * Copy the file.
298 * @param from source file path
299 * @param to destination file path
300 * @param io_buf_size the size of the IO buffer
301 * @return number of bytes copied or negative error code
302 */
303 static wssize_t copy(const char *from, const char *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
304 static wssize_t copy(const LSPString *from, const char *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
305 static wssize_t copy(const Path *from, const char *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
306
307 static wssize_t copy(const char *from, const LSPString *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
308 static wssize_t copy(const LSPString *from, const LSPString *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
309 static wssize_t copy(const Path *from, const LSPString *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
310
311 static wssize_t copy(const char *from, const Path *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
312 static wssize_t copy(const LSPString *from, const Path *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
313 static wssize_t copy(const Path *from, const Path *to, size_t io_buf_size = IO_FILE_DEFAULT_BUF_SIZE);
314
315 /**
316 * Create parent directory
317 * @param path path to the directory
318 * @return status of operation
319 */
320 static status_t mkparent(const char *path);
321 static status_t mkparent(const LSPString *path);
322 static status_t mkparent(const io::Path *path);
323
324 static status_t mkparent(const char *path, bool recursive);
325 static status_t mkparent(const LSPString *path, bool recursive);
326 static status_t mkparent(const io::Path *path, bool recursive);
327 };
328
329 } /* namespace io */
330 } /* namespace lsp */
331
332 #endif /* LSP_PLUG_IN_IO_FILE_H_ */