Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 8 февр. 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_PATH_H_
23 #define LSP_PLUG_IN_IO_PATH_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/runtime/LSPString.h>
27 #include <lsp-plug.in/common/status.h>
28 #include <lsp-plug.in/lltl/types.h>
29 #include <stdarg.h>
30
31 namespace lsp
32 {
33 namespace io
34 {
35 typedef struct fattr_t
36 {
37 enum ftype_t {
38 FT_BLOCK,
39 FT_CHARACTER,
40 FT_DIRECTORY,
41 FT_FIFO,
42 FT_SYMLINK,
43 FT_REGULAR,
44 FT_SOCKET,
45 FT_UNKNOWN
46 };
47
48 ftype_t type; // File type
49 size_t blk_size; // Block size
50 wsize_t size; // File size
51 wsize_t inode; // Index node
52 wsize_t ctime; // Creation time in milliseconds
53 wsize_t mtime; // Modification time in milliseconds
54 wsize_t atime; // Access time in milliseconds
55 } fattr_t;
56
57 /**
58 * Path object. All char * pointers are treated as UTF-8 strings unless the
59 * special case is described.
60 */
61 class Path
62 {
63 private:
64 LSPString sPath;
65
66 private:
67 inline void fixup_path();
68 status_t compute_relative(Path *base);
69
70 public:
71 explicit Path();
72 Path(const Path &) = delete;
73 Path(Path &&) = delete;
74 ~Path();
75
76 Path & operator = (const Path &) = delete;
77 Path & operator = (Path &&) = delete;
78
79 Path *clone() const;
80
81 public:
82 // Setting and getting the path value
83 /**
84 * Set the native-encoded string path to the Path object
85 * @param path path to set
86 * @param charset character set to use
87 * @return status of operation
88 */
89 status_t set_native(const char *path, const char *charset = NULL);
90
91 /**
92 * Assign string path to the Path object
93 * @param path path to set
94 * @return status of operation
95 */
96 status_t set(const char *path);
97 status_t set(const LSPString *path);
98 status_t set(const Path *path);
99
100 /**
101 * Construct path of two parts
102 * @param path path to set
103 * @param child the child element to append to the path
104 * @return status of operation
105 */
106 status_t set(const char *path, const char *child);
107 status_t set(const char *path, const LSPString *child);
108 status_t set(const char *path, const Path *child);
109
110 status_t set(const LSPString *path, const char *child);
111 status_t set(const LSPString *path, const LSPString *child);
112 status_t set(const LSPString *path, const Path *child);
113
114 status_t set(const Path *path, const char *child);
115 status_t set(const Path *path, const LSPString *child);
116 status_t set(const Path *path, const Path *child);
117
118 /**
119 * Get the current path value
120 * @return current path value
121 */
122 inline const char *get() const { return sPath.get_utf8(); }
123
124 /**
125 * Store the path value in the provided character buffer
126 * @param path target buffer to store value
127 * @param maxlen maximum length of character buffer
128 * @return status of operation
129 */
130 status_t get(char *path, size_t maxlen) const;
131 /**
132 * Store the path value in the string
133 * @param path target string object to store value
134 * @return status of operation
135 */
136 status_t get(LSPString *path) const;
137 /**
138 * Store the path value in the string
139 * @param path target Path object to store value
140 * @return status of operation
141 */
142 status_t get(Path *path) const;
143
144 public:
145 // Operations with last element in the path
146 /**
147 * Replace last element of the path with the new value
148 * @param path new value to use as the end of the path
149 * @return status of operation
150 */
151 status_t set_last(const char *path);
152 /**
153 * Replace last element of the path with the new value
154 * @param path new value to use as the end of the path
155 * @return status of operation
156 */
157 status_t set_last(const LSPString *path);
158 /**
159 * Replace last element of the path with the new value
160 * @param path new value to use as the end of the path
161 * @return status of operation
162 */
163 status_t set_last(const Path *path);
164
165 /**
166 * Get last element of the path
167 * @param path character buffer to store character data
168 * @param maxlen maximum length of the buffer
169 * @return status of operation
170 */
171 status_t get_last(char *path, size_t maxlen) const;
172 /**
173 * Get last element of the path
174 * @param path string object to store the value
175 * @return status of operation
176 */
177 status_t get_last(LSPString *path) const;
178 /**
179 * Get last element of the path
180 * @param path Path object to store the value
181 * @return status of operation
182 */
183 status_t get_last(Path *path) const;
184
185 /**
186 * Remove last element from the path object
187 * @return status of operation
188 */
189 status_t remove_last();
190 /**
191 * Remove last element from the path object and return the removed element
192 * @param path character buffer to store the value
193 * @param maxlen maximum length of the buffer
194 * @return status of operation
195 */
196 status_t remove_last(char *path, size_t maxlen);
197 /**
198 * Remove last element from the path object and return the removed element
199 * @param path string to store the value
200 * @return status of operation
201 */
202 status_t remove_last(LSPString *path);
203 /**
204 * Remove last element from the path object and return the removed element
205 * @param path string to store the value
206 * @return status of operation
207 */
208 status_t remove_last(Path *path);
209
210 /**
211 * Remove the last element from the path object and return the result value
212 * @param path character buffer to store the value
213 * @param maxlen maximum length of the buffer
214 * @return status of operation
215 */
216 status_t without_last(char *path, size_t maxlen) const;
217 /**
218 * Remove the last element from the path object and return the result value
219 * @param path string to store the value
220 * @return status of operation
221 */
222 status_t without_last(LSPString *path) const;
223 /**
224 * Remove the last element from the path object and return the result value
225 * @param path path object to store the value
226 * @return status of operation
227 */
228 status_t without_last(Path *path) const;
229
230 public:
231 // Operations with first element in the path
232 /**
233 * Get first element of the path
234 * @param path character buffer to store character data
235 * @param maxlen maximum length of the buffer
236 * @return status of operation
237 */
238 status_t get_first(char *path, size_t maxlen) const;
239 /**
240 * Get first element of the path
241 * @param path string object to store the value
242 * @return status of operation
243 */
244 status_t get_first(LSPString *path) const;
245 /**
246 * Get first element of the path
247 * @param path Path object to store the value
248 * @return status of operation
249 */
250 status_t get_first(Path *path) const;
251
252 /**
253 * Remove first element from the path object
254 * @return status of operation
255 */
256 status_t remove_first();
257 /**
258 * Remove first element from the path object and return the removed element
259 * @param path character buffer to store the value
260 * @param maxlen maximum length of the buffer
261 * @return status of operation
262 */
263 status_t remove_first(char *path, size_t maxlen);
264 /**
265 * Remove first element from the path object and return the removed element
266 * @param path string to store the value
267 * @return status of operation
268 */
269 status_t remove_first(LSPString *path);
270 /**
271 * Remove first element from the path object and return the removed element
272 * @param path string to store the value
273 * @return status of operation
274 */
275 status_t remove_first(Path *path);
276
277 /**
278 * Remove the first element from the path object and return the result value
279 * @param path character buffer to store the value
280 * @param maxlen maximum length of the buffer
281 * @return status of operation
282 */
283 status_t without_first(char *path, size_t maxlen) const;
284 /**
285 * Remove the first element from the path object and return the result value
286 * @param path string to store the value
287 * @return status of operation
288 */
289 status_t without_first(LSPString *path) const;
290 /**
291 * Remove the first element from the path object and return the result value
292 * @param path path object to store the value
293 * @return status of operation
294 */
295 status_t without_first(Path *path) const;
296
297 public:
298 status_t get_ext(char *path, size_t maxlen) const;
299 status_t get_ext(LSPString *path) const;
300 status_t get_ext(Path *path) const;
301
302 status_t get_noext(char *path, size_t maxlen) const;
303 status_t get_noext(LSPString *path) const;
304 status_t get_noext(Path *path) const;
305
306 status_t get_last_noext(char *path, size_t maxlen) const;
307 status_t get_last_noext(LSPString *path) const;
308 status_t get_last_noext(Path *path) const;
309
310 status_t get_parent(char *path, size_t maxlen) const;
311 status_t get_parent(LSPString *path) const;
312 status_t get_parent(Path *path) const;
313
314 status_t set_parent(const char *path);
315 status_t set_parent(const LSPString *path);
316 status_t set_parent(const Path *path);
317
318 status_t concat(const char *path);
319 status_t concat(const LSPString *path);
320 status_t concat(const Path *path);
321
322 status_t append_child(const char *path);
323 status_t append_child(const LSPString *path);
324 status_t append_child(const Path *path);
325
326 status_t append(const char *path);
327 status_t append(const LSPString *path);
328 status_t append(const Path *path);
329
330 status_t remove_base();
331 status_t remove_base(const char *path);
332 status_t remove_base(const LSPString *path);
333 status_t remove_base(const Path *path);
334
335 status_t remove_root();
336
337 ssize_t fmt(const char *fmt...);
338 ssize_t fmt(const LSPString *fmt...);
339 ssize_t vfmt(const char *fmt, va_list args);
340 ssize_t vfmt(const LSPString *fmt, va_list args);
341
342 bool is_absolute() const;
343 bool is_relative() const;
344 bool is_canonical() const;
345 bool is_root() const;
346 bool is_dot() const;
347 bool is_dotdot() const;
348 bool is_dots() const;
349 inline bool is_empty() const { return sPath.is_empty(); }
350
351 inline void clear() { sPath.clear(); }
352
353 inline void swap(Path *path) { sPath.swap(&path->sPath); }
354
355 status_t canonicalize();
356
357 status_t root();
358 status_t current();
359 status_t parent();
360
361 status_t get_canonical(char *path, size_t maxlen) const;
362 status_t get_canonical(LSPString *path) const;
363 status_t get_canonical(Path *path) const;
364
365 status_t as_relative(const char *path);
366 status_t as_relative(const LSPString *path);
367 status_t as_relative(const Path *path);
368
369 bool equals(const Path *path) const;
370 bool equals(const LSPString *path) const;
371 bool equals(const char *path) const;
372
373 inline const LSPString *as_string() const { return &sPath; }
374 inline const char *as_utf8() const { return sPath.get_utf8(); }
375 inline const char *as_native(const char *charset = NULL) const { return sPath.get_native(charset); }
376 inline void take(Path *src) { sPath.take(&src->sPath); }
377 inline size_t length() const { return sPath.length(); }
378 void take(LSPString *src);
379 void drain(LSPString *dst);
380
381 public:
382 // Some kind of file operations
383 /**
384 * Return information about the file
385 * @param attr pointer to structure to store the information
386 * @return status of operaiton
387 */
388 status_t stat(fattr_t *attr) const;
389
390 /**
391 * Return information about the file, do not follow symbolic links
392 * @param attr pointer to structure to store the information
393 * @return status of operaiton
394 */
395 status_t sym_stat(fattr_t *attr) const;
396
397 /**
398 * Get the final path of the physical file/device. That means, if the path name is a symbolic link
399 * or a chain of symbolic links, the function reads them until the object associated with the path name
400 * becomes the non-symbolic link.
401 *
402 * @param path variable to store the path
403 * @return status of operation
404 */
405 status_t final_path(LSPString *path) const;
406
407 /**
408 * Get the final path of the physical file/device. That means, if the path name is a symbolic link
409 * or a chain of symbolic links, the function reads them until the object associated with the path name
410 * becomes the non-symbolic link.
411 *
412 * @param path variable to store the path
413 * @return status of operation
414 */
415 status_t final_path(Path *path) const;
416
417 /**
418 * Convert current path to the final path of the physical file/device. That means, if the path name
419 * is a symbolic link or a chain of symbolic links, the function reads them until the object associated
420 * with the path name becomes the non-symbolic link.
421 * @return status of operation
422 */
423 status_t to_final_path();
424
425 /**
426 * Obtain the size of the file
427 * @return file size or negative error code
428 */
429 wssize_t size() const;
430
431 /**
432 * Check that path entry exists on file system
433 * @return true if path entry exists on file system
434 */
435 bool exists() const;
436 /**
437 * Check that path entry exists on file system and is a regular file
438 * @return true if path entry exists on file system and is a regular file
439 */
440 bool is_reg() const;
441 /**
442 * Check that path entry exists on file system and is a directory
443 * @return true if path entry exists on file system and is a directory
444 */
445 bool is_dir() const;
446 /**
447 * Check that path entry exists on file system and is a block device
448 * @return true if path entry exists on file system and is a block device
449 */
450 bool is_block_dev() const;
451 /**
452 * Check that path entry exists on file system and is a character device
453 * @return true if path entry exists on file system and is a character device
454 */
455 bool is_char_dev() const;
456 /**
457 * Check that path entry exists on file system and is a FIFO
458 * @return true if path entry exists on file system and is a FIFO
459 */
460 bool is_fifo() const;
461 /**
462 * Check that path entry exists on file system and is a symbolic link
463 * @return true if path entry exists on file system and is a symbolic link
464 */
465 bool is_symlink() const;
466 /**
467 * Check that path entry exists on file system and is a socket
468 * @return true if path entry exists on file system and is a socket
469 */
470 bool is_socket() const;
471
472 /**
473 * Create directory associated with the path name
474 * @return status of operation
475 */
476 status_t mkdir() const;
477 /**
478 * Create directory associated with the path name
479 * @param recursive flag that allows to perform recursive directory creation
480 * @return status of operation
481 */
482 status_t mkdir(bool recursive) const;
483 /**
484 * Create parent directory associated with the path name
485 * @return status of operation
486 */
487 status_t mkparent() const;
488 /**
489 * Create parent directory associated with the path name
490 * @param recursive flag that allows to perform recursive directory creation
491 * @return status of operation
492 */
493 status_t mkparent(bool recursive) const;
494 /**
495 * Remove the file associated with the path
496 * @return status of operation
497 */
498 status_t remove() const;
499
500 /**
501 * Rename or move the file associated with the path name to another path
502 * @param dst destination file name
503 * @return status of operation
504 */
505 status_t rename(const char *dst) const;
506 /**
507 * Rename or move the file associated with the path name to another path
508 * @param dst destination file name
509 * @return status of operation
510 */
511 status_t rename(const LSPString *dst) const;
512 /**
513 * Rename or move the file associated with the path name to another path
514 * @param dst destination file name
515 * @return status of operation
516 */
517 status_t rename(const io::Path *dst) const;
518
519 public:
520 inline size_t hash() const { return sPath.hash(); }
521 inline size_t compare_to(const io::Path *dst) const { return sPath.compare_to(&dst->sPath); }
522
523 public:
524 static bool is_dot(const LSPString *path);
525 static bool is_dot(const io::Path *path);
526 static bool is_dot(const char *path);
527
528 static bool is_dotdot(const LSPString *path);
529 static bool is_dotdot(const io::Path *path);
530 static bool is_dotdot(const char *path);
531
532 static bool is_dots(const LSPString *path);
533 static bool is_dots(const io::Path *path);
534 static bool is_dots(const char *path);
535
536 static bool valid_file_name(const LSPString *fname);
537 static bool valid_path_name(const LSPString *fname);
538 };
539 } /* namespace io */
540
541 // LLTL specialization for Path class
542 namespace lltl
543 {
544 // Non-const specification
545 template <>
546 struct hash_spec<io::Path>: public hash_iface
547 {
548 static size_t hash_func(const void *ptr, size_t size);
549
550 explicit hash_spec()
551 {
552 hash = hash_func;
553 }
554 };
555
556 template <>
557 struct compare_spec<io::Path>: public compare_iface
558 {
559 static ssize_t cmp_func(const void *a, const void *b, size_t size);
560
561 explicit compare_spec()
562 {
563 compare = cmp_func;
564 }
565 };
566
567 template <>
568 struct allocator_spec<io::Path>: public allocator_iface
569 {
570 static void *clone_func(const void *src, size_t size);
571 static void free_func(void *ptr);
572
573 explicit allocator_spec()
574 {
575 clone = clone_func;
576 free = free_func;
577 }
578 };
579
580 // Const specification
581 template <>
582 struct hash_spec<const io::Path>: public hash_iface
583 {
584 static size_t hash_func(const void *ptr, size_t size);
585
586 explicit hash_spec()
587 {
588 hash = hash_func;
589 }
590 };
591
592 template <>
593 struct compare_spec<const io::Path>: public compare_iface
594 {
595 static ssize_t cmp_func(const void *a, const void *b, size_t size);
596
597 explicit compare_spec()
598 {
599 compare = cmp_func;
600 }
601 };
602
603 template <>
604 struct allocator_spec<const io::Path>: public allocator_iface
605 {
606 static void *clone_func(const void *src, size_t size);
607 static void free_func(void *ptr);
608
609 explicit allocator_spec()
610 {
611 clone = clone_func;
612 free = free_func;
613 }
614 };
615 } /* namespace lltl */
616 } /* namespace lsp */
617
618 #endif /* LSP_PLUG_IN_IO_PATH_H_ */