PatchworkOS
Loading...
Searching...
No Matches
path.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/utils/map.h>
4
5#include <alloca.h>
6#include <ctype.h>
7#include <stdbool.h>
8#include <stdint.h>
9#include <sys/io.h>
10
11typedef struct path path_t;
12typedef struct mount mount_t;
13typedef struct dentry dentry_t;
14typedef struct namespace namespace_t;
15
53#define PATH_DEFER(path) __attribute__((cleanup(path_defer_cleanup))) path_t* CONCAT(i, __COUNTER__) = (path)
54
66#define PATH_VALID_CHAR(ch) \
67 (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-. ()[]{}~!@#$%^&?',;=+", (ch)))
68
74#define PATH_HANDLE_DOTDOT_MAX_ITER 1000
75
82typedef enum
83{
85 PATH_NONBLOCK = 1 << 0,
86 PATH_APPEND = 1 << 1,
87 PATH_CREATE = 1 << 2,
89 PATH_TRUNCATE = 1 << 4,
94
99typedef enum
100{
104
109typedef struct path
110{
113} path_t;
114
121typedef struct pathname
122{
123 char string[MAX_PATH];
126} pathname_t;
127
131void path_flags_init(void);
132
143#define PATHNAME_IS_VALID(pathname) ((pathname) != NULL && (pathname)->isValid)
144
156#define PATHNAME(string) \
157 ({ \
158 pathname_t* pathname = alloca(sizeof(pathname_t)); \
159 pathname_init(pathname, string); \
160 pathname; \
161 })
162
172uint64_t pathname_init(pathname_t* pathname, const char* string);
173
182#define PATH_EMPTY \
183 (path_t) \
184 { \
185 .mount = NULL, .dentry = NULL \
186 }
187
195#define PATH_CREATE(inMount, inDentry) \
196 (path_t) \
197 { \
198 .mount = REF(inMount), .dentry = REF(inDentry), \
199 }
200
210void path_set(path_t* path, mount_t* mount, dentry_t* dentry);
211
220void path_copy(path_t* dest, const path_t* src);
221
229void path_put(path_t* path);
230
241uint64_t path_walk_single_step(path_t* outPath, const path_t* parent, const char* name, walk_flags_t flags,
242 namespace_t* ns);
243
254uint64_t path_walk(path_t* outPath, const pathname_t* pathname, const path_t* start, walk_flags_t flags,
255 namespace_t* ns);
256
268uint64_t path_walk_parent(path_t* outPath, const pathname_t* pathname, const path_t* start, char* outLastName,
269 walk_flags_t flags, namespace_t* ns);
270
280uint64_t path_to_name(const path_t* path, pathname_t* pathname);
281
282static inline void path_defer_cleanup(path_t** path)
283{
284 if (*path != NULL)
285 {
286 path_put(*path);
287 }
288}
289
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
static mount_t * mount
Definition acpi.c:15
static void path_defer_cleanup(path_t **path)
Definition path.h:282
uint64_t path_walk_parent(path_t *outPath, const pathname_t *pathname, const path_t *start, char *outLastName, walk_flags_t flags, namespace_t *ns)
Traverse a pathname to its parent and get the last component name.
Definition path.c:511
void path_put(path_t *path)
Put a path.
Definition path.c:257
#define PATH_CREATE(inMount, inDentry)
Helper to create a path.
Definition path.h:195
uint64_t path_walk(path_t *outPath, const pathname_t *pathname, const path_t *start, walk_flags_t flags, namespace_t *ns)
Traverse a pathname from a specified starting path.
Definition path.c:376
uint64_t path_walk_single_step(path_t *outPath, const path_t *parent, const char *name, walk_flags_t flags, namespace_t *ns)
Traverse a single component from a parent path.
Definition path.c:329
path_flags_t
Path flags.
Definition path.h:83
walk_flags_t
Flags for walking a path.
Definition path.h:100
void path_set(path_t *path, mount_t *mount, dentry_t *dentry)
Set a path.
Definition path.c:207
void path_flags_init(void)
Initialize path flags resolution.
Definition path.c:66
uint64_t pathname_init(pathname_t *pathname, const char *string)
Initialize a pathname.
Definition path.c:100
void path_copy(path_t *dest, const path_t *src)
Copy a path.
Definition path.c:232
uint64_t path_to_name(const path_t *path, pathname_t *pathname)
Convert a path to a pathname.
Definition path.c:583
@ PATH_NONBLOCK
Definition path.h:85
@ PATH_EXCLUSIVE
Definition path.h:88
@ PATH_NONE
Definition path.h:84
@ PATH_TRUNCATE
Definition path.h:89
@ PATH_DIRECTORY
Definition path.h:90
@ PATH_RECURSIVE
Definition path.h:91
@ PATH_APPEND
Definition path.h:86
@ PATH_FLAGS_AMOUNT
Definition path.h:92
@ WALK_NONE
No flags.
Definition path.h:101
@ WALK_NEGATIVE_IS_OK
If a negative dentry is ok, if not specified then it is considered an error.
Definition path.h:102
#define NULL
Pointer error value.
Definition NULL.h:23
static void start()
Definition main.c:542
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Directory entry structure.
Definition dentry.h:83
Mount structure.
Definition mount.h:36
Namespace structure.
Path structure.
Definition path.h:110
mount_t * mount
Definition path.h:111
dentry_t * dentry
Definition path.h:112
Pathname structure.
Definition path.h:122
path_flags_t flags
Definition path.h:124
bool isValid
Definition path.h:125