|
PatchworkOS
|
Unique location in the filesystem. More...
Data Structures | |
| struct | path_t |
| Path structure. More... | |
| struct | pathname_t |
| Pathname structure. More... | |
Macros | |
| #define | PATH_DEFER(path) __attribute__((cleanup(path_defer_cleanup))) path_t* CONCAT(i, __COUNTER__) = (path) |
| Defer path put. | |
| #define | PATH_VALID_CHAR(ch) (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-. ()[]{}~!@#$%^&?',;=+", (ch))) |
| Check if a char is valid. | |
| #define | PATH_HANDLE_DOTDOT_MAX_ITER 1000 |
Maximum iterations to handle .. in a path. | |
| #define | PATHNAME_IS_VALID(pathname) ((pathname) != NULL && (pathname)->isValid) |
| Check if a pathname is valid. | |
| #define | PATHNAME(string) |
| Helper to create a pathname. | |
| #define | PATH_EMPTY |
| Helper to create an empty path. | |
| #define | PATH_CREATE(inMount, inDentry) |
| Helper to create a path. | |
Enumerations | |
| enum | path_flags_t { PATH_NONE = 0 , PATH_NONBLOCK = 1 << 0 , PATH_APPEND = 1 << 1 , PATH_CREATE = 1 << 2 , PATH_EXCLUSIVE = 1 << 3 , PATH_TRUNCATE = 1 << 4 , PATH_DIRECTORY = 1 << 5 , PATH_RECURSIVE = 1 << 6 , PATH_FLAGS_AMOUNT = 7 } |
| Path flags. More... | |
| enum | walk_flags_t { WALK_NONE = 0 , WALK_NEGATIVE_IS_OK = 1 << 0 } |
| Flags for walking a path. More... | |
Functions | |
| void | path_flags_init (void) |
| Initialize path flags resolution. | |
| uint64_t | pathname_init (pathname_t *pathname, const char *string) |
| Initialize a pathname. | |
| void | path_set (path_t *path, mount_t *mount, dentry_t *dentry) |
| Set a path. | |
| void | path_copy (path_t *dest, const path_t *src) |
| Copy a path. | |
| void | path_put (path_t *path) |
| Put a path. | |
| 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. | |
| 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. | |
| 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. | |
| uint64_t | path_to_name (const path_t *path, pathname_t *pathname) |
| Convert a path to a pathname. | |
| static void | path_defer_cleanup (path_t **path) |
Unique location in the filesystem.
A path is a single unique location in the filesystem hierarchy. It consists of a mount and a dentry. The mount is the filesystem that the path is in and the dentry is the actual location in that filesystem.
Note how just a dentry is not enough to uniquely identify a location in the filesystem, this is because of mountpoints. A dentry can exist in multiple places if its part of a filesystem that has been mounted in multiple places.
Paths can have flags appended to them which is how we handle general file flags. Each flag starts with : and the same path can contain multiples of different or the same flag, for example /path/to/file:create:create:nonblock.
Included is a list of all available flags:
nonblock: Open the file in non-blocking mode.append: Any data written to the file will be appended to the end.create: Create the file if it does not exist.excl: Will cause the open to fail if the file already exists. Must be used with create.trunc: Truncate the file to zero length if it already exists.dir: Allow opening directories.recur: Behaviour differs, but allows for recursive operations, for example when used with remove it will remove directories and their children recursively. | #define PATH_CREATE | ( | inMount, | |
| inDentry | |||
| ) |
Helper to create a path.
| inMount | The mount of the path. |
| inDentry | The dentry of the path. |
| #define PATH_DEFER | ( | path | ) | __attribute__((cleanup(path_defer_cleanup))) path_t* CONCAT(i, __COUNTER__) = (path) |
Defer path put.
This macro will call path_put() on the given path when it goes out of scope.
| path | The path to defer. |
| #define PATH_EMPTY |
Helper to create an empty path.
Its important to always use this as some functions, for example path_copy(), will deref the existing mount and dentry in the path.
| #define PATH_HANDLE_DOTDOT_MAX_ITER 1000 |
| #define PATH_VALID_CHAR | ( | ch | ) | (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-. ()[]{}~!@#$%^&?',;=+", (ch))) |
Check if a char is valid.
A valid char is one of the following ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-. ()[]{}~!#$%^&?’,;=+`.
TODO: Replace with array lookup.
| ch | The char to check. |
| #define PATHNAME | ( | string | ) |
Helper to create a pathname.
This macro will create a pathname on the stack and initialize it with the given string.
This is also the reason we have the isValid flag in the pathname_t structure, to be able to check if this macro failed without having to return an error code, streamlining the code a bit.
| string | The string to initialize the pathname with. |
| #define PATHNAME_IS_VALID | ( | pathname | ) | ((pathname) != NULL && (pathname)->isValid) |
Check if a pathname is valid.
A valid pathname is not NULL and has its isValid flag set to true.
This flag is set in pathname_init().
| pathname | The pathname to check. |
| enum path_flags_t |
| enum walk_flags_t |
Copy a path.
Will deref the existing mount and dentry in the destination path if they are not NULL.
| dest | The destination path. |
| src | The source path. |
Definition at line 232 of file path.c.
References path_t::dentry, DEREF, path_t::mount, NULL, and REF.
Referenced by file_new(), namespace_traverse_mount(), path_to_name(), path_walk(), path_walk_parent(), path_walk_single_step(), vfs_create(), vfs_ctx_get_cwd(), vfs_ctx_init(), vfs_ctx_set_cwd(), vfs_open_lookup(), and vfs_walk_parent_and_child().
|
inlinestatic |
Definition at line 282 of file path.h.
References NULL, and path_put().
| void path_flags_init | ( | void | ) |
Initialize path flags resolution.
Definition at line 66 of file path.c.
References ERR, flagEntries, flagMap, flagShortMap, map_entry_init(), map_init(), map_insert(), map_key_buffer(), map_key_string(), NULL, panic(), and PATH_FLAGS_AMOUNT.
Referenced by vfs_init().
| void path_put | ( | path_t * | path | ) |
Put a path.
Will deref the mount and dentry in the path if they are not NULL.
| path | The path to put. |
Definition at line 257 of file path.c.
References path_t::dentry, DEREF, path_t::mount, and NULL.
Referenced by file_free(), path_defer_cleanup(), path_walk(), process_dir_init(), process_init(), socket_new(), SYSCALL_DEFINE(), sysfs_mount_new(), vfs_ctx_deinit(), and vfs_ctx_set_cwd().
Set a path.
Will deref the existing mount and dentry in the path if they are not NULL.
| path | The path to set. |
| mount | The mount to set. |
| dentry | The dentry to set. |
Definition at line 207 of file path.c.
References path_t::dentry, DEREF, path_t::mount, mount, NULL, and REF.
Referenced by namespace_get_root_path(), namespace_traverse_mount(), path_to_name(), path_walk_single_step(), socket_family_get_dir(), and sysfs_mount_new().
| uint64_t path_to_name | ( | const path_t * | path, |
| pathname_t * | pathname | ||
| ) |
Convert a path to a pathname.
The resulting pathname will be absolute.
| path | The path to convert. |
| pathname | The output pathname. |
0. On failure, ERR and errno is set. Definition at line 583 of file path.c.
References path_t::dentry, DENTRY_IS_ROOT, EINVAL, ENAMETOOLONG, ERR, errno, pathname_t::flags, pathname_t::isValid, MAX_NAME, MAX_PATH, memcpy(), memmove(), memset(), path_t::mount, mount_t::mountpoint, dentry_t::name, NULL, dentry_t::parent, mount_t::parent, path_copy(), PATH_DEFER, PATH_EMPTY, PATH_NONE, path_set(), pathname_t::string, and strnlen_s().
Referenced by process_cwd_read().
| 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.
| outPath | The output path. |
| pathname | The patname to traverse to. |
| start | The path to start at if the pathname is relative. |
| flags | Flags for the path walk. |
| ns | The namespace to access mountpoints. |
0. On failure, ERR and errno is set. Definition at line 376 of file path.c.
References assert, atomic_load, path_t::dentry, DENTRY_NEGATIVE, EBADFLAG, EINVAL, ENAMETOOLONG, ENOENT, ERR, errno, MAX_NAME, memcpy(), path_t::mount, dentry_t::mountCount, namespace_get_root_path(), namespace_traverse_mount(), next, NULL, path_copy(), PATH_DEFER, PATH_EMPTY, path_handle_dotdot(), path_put(), PATH_VALID_CHAR, path_walk_single_step(), PATHNAME_IS_VALID, start(), strcmp(), pathname_t::string, and WALK_NEGATIVE_IS_OK.
Referenced by path_walk_parent(), and vfs_walk().
| 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.
| outPath | The output parent path. |
| pathname | The pathname to traverse. |
| start | The path to start at if the pathname is relative. |
| outLastName | The output last component name. |
| flags | Flags for the path walk. |
| ns | The namespace to access mountpoints. |
0. On failure, ERR and errno is set. Definition at line 511 of file path.c.
References EINVAL, ENOENT, ERR, errno, MAX_NAME, MAX_PATH, memset(), NULL, path_copy(), path_walk(), pathname_init(), PATHNAME_IS_VALID, start(), strcmp(), pathname_t::string, strncpy(), strnlen_s(), and strrchr().
Referenced by vfs_walk_parent().
| 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.
| outPath | The output path. |
| parent | The parent path. |
| name | The name of the child dentry. |
| flags | Flags for the path walk. |
| ns | The namespace to access mountpoints. |
0. On failure, ERR and errno is set. Definition at line 329 of file path.c.
References atomic_load, path_t::dentry, DENTRY_NEGATIVE, DEREF_DEFER, EINVAL, ENOENT, ERR, errno, path_t::mount, dentry_t::mountCount, namespace_traverse_mount(), next, NULL, path_copy(), PATH_DEFER, PATH_EMPTY, path_set(), vfs_get_or_lookup_dentry(), vfs_is_name_valid(), and WALK_NEGATIVE_IS_OK.
Referenced by path_walk(), and vfs_walk_parent_and_child().
| uint64_t pathname_init | ( | pathname_t * | pathname, |
| const char * | string | ||
| ) |
Initialize a pathname.
If the string is invalid, it will error and set pathname->isValid to false.
| pathname | The pathname to initialize. |
| string | The string to initialize the pathname with. |
0. On failure, ERR and errno is set. Definition at line 100 of file path.c.
References EBADFLAG, EINVAL, ENAMETOOLONG, ERR, errno, path_flag_entry_t::flag, pathname_t::flags, isalnum(), pathname_t::isValid, MAX_NAME, MAX_PATH, memset(), NULL, path_flags_get(), PATH_NONE, PATH_VALID_CHAR, pathname_t::string, and strnlen_s().
Referenced by loader_spawn(), path_walk_parent(), and thread_copy_from_user_pathname().