PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
cwd.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/path.h>
4#include <kernel/sync/lock.h>
5
6/**
7 * @brief Current Working Directory
8 * @defgroup kernel_fs_cwd Current Working Directory
9 * @ingroup kernel_fs
10 *
11 * The current working directory (CWD) is a per-process structure to track the current location in the filesystem for
12 * the process.
13 *
14 * @{
15 */
16
17typedef struct cwd
18{
21} cwd_t;
22
23/**
24 * @brief Initialize a CWD structure.
25 *
26 * Will by default lazily resolve to the root path of the kernel process's namespace until set to another path.
27 *
28 * @param cwd The CWD structure to initialize.
29 */
30void cwd_init(cwd_t* cwd);
31
32/**
33 * @brief Deinitialize a CWD structure.
34 *
35 * @param cwd The CWD structure to deinitialize.
36 */
37void cwd_deinit(cwd_t* cwd);
38
39/**
40 * @brief Get the current working directory.
41 *
42 * @note If the cwd has not been set, this will return the root path of the kernel process's namespace. This is to solve
43 * a circular dependency where the kernel process needs to be initialized before the vfs.
44 *
45 * @param cwd The CWD structure.
46 * @return The current working directory path.
47 */
48path_t cwd_get(cwd_t* cwd);
49
50/**
51 * @brief Set the current working directory.
52 *
53 * @param cwd The CWD structure.
54 * @param newPath The new current working directory.
55 */
56void cwd_set(cwd_t* cwd, const path_t* newPath);
57
58/**
59 * @brief Clear the current working directory.
60 *
61 * Needed as a process might have its working directory inside its own `/proc/[pid]` directory which, since that
62 * directory holds references to the process itself, would result in a memory leak.
63 *
64 * @param cwd The CWD structure.
65 */
66void cwd_clear(cwd_t* cwd);
67
68/** @} */
void cwd_set(cwd_t *cwd, const path_t *newPath)
Set the current working directory.
Definition cwd.c:44
void cwd_init(cwd_t *cwd)
Initialize a CWD structure.
Definition cwd.c:5
void cwd_clear(cwd_t *cwd)
Clear the current working directory.
Definition cwd.c:51
path_t cwd_get(cwd_t *cwd)
Get the current working directory.
Definition cwd.c:18
void cwd_deinit(cwd_t *cwd)
Deinitialize a CWD structure.
Definition cwd.c:11
Definition cwd.h:18
path_t path
Definition cwd.h:19
lock_t lock
Definition cwd.h:20
A simple ticket lock implementation.
Definition lock.h:43
Path structure.
Definition path.h:125