PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
cwd.c
Go to the documentation of this file.
1#include <kernel/fs/cwd.h>
2
4
5void cwd_init(cwd_t* cwd)
6{
7 cwd->path = PATH_EMPTY;
8 lock_init(&cwd->lock);
9}
10
11void cwd_deinit(cwd_t* cwd)
12{
13 lock_acquire(&cwd->lock);
14 path_put(&cwd->path);
15 lock_release(&cwd->lock);
16}
17
19{
20 path_t result = PATH_EMPTY;
21
22 lock_acquire(&cwd->lock);
23
24 if (cwd->path.dentry == NULL || cwd->path.mount == NULL)
25 {
26 assert(cwd->path.dentry == NULL && cwd->path.mount == NULL);
27 namespace_t* kernelNs = &process_get_kernel()->ns;
28
29 if (namespace_get_root_path(kernelNs, &result) == ERR)
30 {
31 lock_release(&cwd->lock);
32 return PATH_EMPTY;
33 }
34 lock_release(&cwd->lock);
35 return result;
36 }
37
38 path_copy(&result, &cwd->path);
39 lock_release(&cwd->lock);
40
41 return result;
42}
43
44void cwd_set(cwd_t* cwd, const path_t* newPath)
45{
46 lock_acquire(&cwd->lock);
47 path_copy(&cwd->path, newPath);
48 lock_release(&cwd->lock);
49}
50
51void cwd_clear(cwd_t* cwd)
52{
53 lock_acquire(&cwd->lock);
54 path_put(&cwd->path);
55 lock_release(&cwd->lock);
56}
57
58SYSCALL_DEFINE(SYS_CHDIR, uint64_t, const char* pathString)
59{
60 thread_t* thread = sched_thread();
61 process_t* process = thread->process;
62
63 pathname_t pathname;
64 if (thread_copy_from_user_pathname(thread, &pathname, pathString) == ERR)
65 {
66 return ERR;
67 }
68
69 path_t path = cwd_get(&process->cwd);
70 PATH_DEFER(&path);
71
72 if (path_walk(&path, &pathname, &process->ns) == ERR)
73 {
74 return ERR;
75 }
76
77 if (!dentry_is_dir(path.dentry))
78 {
79 errno = ENOTDIR;
80 return ERR;
81 }
82
83 cwd_set(&process->cwd, &path);
84 return 0;
85}
#define assert(expression)
Definition assert.h:29
#define SYSCALL_DEFINE(num, returnType,...)
Macro to define a syscall.
Definition syscall.h:163
@ SYS_CHDIR
Definition syscall.h:81
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
bool dentry_is_dir(dentry_t *dentry)
Check if the inode associated with a dentry is a directory.
Definition dentry.c:268
uint64_t namespace_get_root_path(namespace_t *ns, path_t *out)
Get the root path of a namespace.
Definition namespace.c:286
void path_put(path_t *path)
Put a path.
Definition path.c:246
#define PATH_DEFER(path)
Defer path put.
Definition path.h:97
uint64_t path_walk(path_t *path, const pathname_t *pathname, namespace_t *ns)
Walk a pathname to a path.
Definition path.c:347
void path_copy(path_t *dest, const path_t *src)
Copy a path.
Definition path.c:221
#define PATH_EMPTY
Helper to create an empty path.
Definition path.h:194
process_t * process_get_kernel(void)
Gets the kernel process.
Definition process.c:1031
uint64_t thread_copy_from_user_pathname(thread_t *thread, pathname_t *pathname, const char *userPath)
Safely copy a string from user space and use it to initialize a pathname.
Definition thread.c:259
thread_t * sched_thread(void)
Retrieves the currently running thread.
Definition sched.c:612
static void lock_init(lock_t *lock)
Initializes a lock.
Definition lock.h:86
static void lock_release(lock_t *lock)
Releases a lock.
Definition lock.h:146
static void lock_acquire(lock_t *lock)
Acquires a lock, blocking until it is available.
Definition lock.h:103
#define ENOTDIR
Not a directory.
Definition errno.h:132
#define errno
Error number variable.
Definition errno.h:27
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Definition cwd.h:18
path_t path
Definition cwd.h:19
lock_t lock
Definition cwd.h:20
Path structure.
Definition path.h:125
mount_t * mount
Definition path.h:126
dentry_t * dentry
Definition path.h:127
Pathname structure.
Definition path.h:137
Process structure.
Definition process.h:205
namespace_t ns
Definition process.h:211
cwd_t cwd
Definition process.h:212
Thread of execution structure.
Definition thread.h:56
process_t * process
The parent process that the thread executes within.
Definition thread.h:57