Skip to content

Commit 2af42e5

Browse files
committed
refactor(kernel:path): remove PATHNAME() macro.
1 parent 79e1d77 commit 2af42e5

6 files changed

Lines changed: 92 additions & 54 deletions

File tree

include/kernel/fs/path.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -138,44 +138,11 @@ typedef struct pathname
138138
{
139139
char string[MAX_PATH];
140140
mode_t mode;
141-
bool isValid;
142141
} pathname_t;
143142

144-
/**
145-
* @brief Check if a pathname is valid.
146-
*
147-
* A valid pathname is not `NULL` and has its `isValid` flag set to true.
148-
*
149-
* This flag is set in `pathname_init()`.
150-
*
151-
* @param pathname The pathname to check.
152-
* @return true if the pathname is valid, false otherwise.
153-
*/
154-
#define PATHNAME_IS_VALID(pathname) ((pathname) != NULL && (pathname)->isValid)
155-
156-
/**
157-
* @brief Helper to create a pathname.
158-
*
159-
* This macro will create a pathname on the stack and initialize it with the given string.
160-
*
161-
* This is also the reason we have the `isValid` flag in the `pathname_t` structure, to be able to check if this macro
162-
* failed without having to return an error code, streamlining the code a bit.
163-
*
164-
* @param string The string to initialize the pathname with.
165-
* @return The initialized pathname.
166-
*/
167-
#define PATHNAME(string) \
168-
({ \
169-
pathname_t* pathname = alloca(sizeof(pathname_t)); \
170-
pathname_init(pathname, string); \
171-
pathname; \
172-
})
173-
174143
/**
175144
* @brief Initialize a pathname.
176145
*
177-
* If the string is invalid, it will error and set pathname->isValid to false.
178-
*
179146
* @param pathname The pathname to initialize.
180147
* @param string The string to initialize the pathname with.
181148
* @return On success, `0`. On failure, `ERR` and `errno` is set to:

src/kernel/fs/path.c

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ uint64_t pathname_init(pathname_t* pathname, const char* string)
123123

124124
memset(pathname->string, 0, MAX_PATH);
125125
pathname->mode = MODE_NONE;
126-
pathname->isValid = false;
127126

128127
if (string == NULL)
129128
{
@@ -169,7 +168,6 @@ uint64_t pathname_init(pathname_t* pathname, const char* string)
169168

170169
if (string[index] != ':')
171170
{
172-
pathname->isValid = true;
173171
return 0;
174172
}
175173

@@ -216,7 +214,6 @@ uint64_t pathname_init(pathname_t* pathname, const char* string)
216214
pathname->mode |= mode;
217215
}
218216

219-
pathname->isValid = true;
220217
return 0;
221218
}
222219

@@ -595,7 +592,7 @@ uint64_t path_step(path_t* path, mode_t mode, const char* name, namespace_t* ns)
595592

596593
uint64_t path_walk(path_t* path, const pathname_t* pathname, namespace_t* ns)
597594
{
598-
if (path == NULL || !PATHNAME_IS_VALID(pathname) || ns == NULL)
595+
if (path == NULL || pathname == NULL || ns == NULL)
599596
{
600597
errno = EINVAL;
601598
return ERR;
@@ -635,7 +632,7 @@ uint64_t path_walk(path_t* path, const pathname_t* pathname, namespace_t* ns)
635632

636633
uint64_t path_walk_parent(path_t* path, const pathname_t* pathname, char* outLastName, namespace_t* ns)
637634
{
638-
if (path == NULL || !PATHNAME_IS_VALID(pathname) || outLastName == NULL || ns == NULL)
635+
if (path == NULL || pathname == NULL || outLastName == NULL || ns == NULL)
639636
{
640637
errno = EINVAL;
641638
return ERR;
@@ -693,7 +690,7 @@ uint64_t path_walk_parent(path_t* path, const pathname_t* pathname, char* outLas
693690
uint64_t path_walk_parent_and_child(const path_t* from, path_t* outParent, path_t* outChild, const pathname_t* pathname,
694691
namespace_t* ns)
695692
{
696-
if (from == NULL || outParent == NULL || outChild == NULL || !PATHNAME_IS_VALID(pathname) || ns == NULL)
693+
if (from == NULL || outParent == NULL || outChild == NULL || pathname == NULL || ns == NULL)
697694
{
698695
errno = EINVAL;
699696
return ERR;
@@ -781,8 +778,6 @@ uint64_t path_to_name(const path_t* path, pathname_t* pathname)
781778
memmove(buffer, ptr, totalLen + 1);
782779

783780
pathname->mode = MODE_NONE;
784-
pathname->isValid = true;
785-
786781
return 0;
787782
}
788783

@@ -838,4 +833,50 @@ uint64_t mode_check(mode_t* mode, mode_t maxPerms)
838833
}
839834

840835
return 0;
841-
}
836+
}
837+
838+
#ifdef _TESTING_
839+
840+
#include <kernel/utils/test.h>
841+
842+
TEST_DEFINE(path)
843+
{
844+
pathname_t pathname;
845+
846+
TEST_ASSERT(pathname_init(&pathname, "/usr/bin/init") == 0);
847+
TEST_ASSERT(strcmp(pathname.string, "/usr/bin/init") == 0);
848+
TEST_ASSERT(pathname.mode == MODE_NONE);
849+
850+
TEST_ASSERT(pathname_init(&pathname, "/dev/sda:read:write") == 0);
851+
TEST_ASSERT(strcmp(pathname.string, "/dev/sda") == 0);
852+
TEST_ASSERT((pathname.mode & (MODE_READ | MODE_WRITE)) == (MODE_READ | MODE_WRITE));
853+
854+
TEST_ASSERT(pathname_init(&pathname, "/tmp/file:c:w") == 0);
855+
TEST_ASSERT(strcmp(pathname.string, "/tmp/file") == 0);
856+
TEST_ASSERT((pathname.mode & (MODE_CREATE | MODE_WRITE)) == (MODE_CREATE | MODE_WRITE));
857+
858+
TEST_ASSERT(pathname_init(&pathname, "/var/log:append:c") == 0);
859+
TEST_ASSERT(strcmp(pathname.string, "/var/log") == 0);
860+
TEST_ASSERT((pathname.mode & (MODE_APPEND | MODE_CREATE)) == (MODE_APPEND | MODE_CREATE));
861+
862+
TEST_ASSERT(pathname_init(&pathname, "/file:rw") == 0);
863+
TEST_ASSERT(strcmp(pathname.string, "/file") == 0);
864+
TEST_ASSERT((pathname.mode & (MODE_READ | MODE_WRITE)) == (MODE_READ | MODE_WRITE));
865+
866+
TEST_ASSERT(pathname_init(&pathname, "/home/user/fi?le") == ERR);
867+
TEST_ASSERT(errno == EINVAL);
868+
869+
TEST_ASSERT(pathname_init(&pathname, "/home:invalid") == ERR);
870+
TEST_ASSERT(errno == EINVAL);
871+
872+
TEST_ASSERT(pathname_init(&pathname, "") == 0);
873+
TEST_ASSERT(strcmp(pathname.string, "") == 0);
874+
875+
TEST_ASSERT(pathname_init(&pathname, ":read") == 0);
876+
TEST_ASSERT(strcmp(pathname.string, "") == 0);
877+
TEST_ASSERT(pathname.mode == MODE_READ);
878+
879+
return 0;
880+
}
881+
882+
#endif

src/kernel/fs/sysfs.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ void sysfs_init(void)
9595
path_t target = cwd_get(&process->cwd, ns);
9696
PATH_DEFER(&target);
9797

98-
if (path_walk(&target, PATHNAME("/sys"), ns) == ERR)
98+
pathname_t pathname;
99+
if (pathname_init(&pathname, "/sys") == ERR)
100+
{
101+
panic(NULL, "Failed to init pathname for /sys");
102+
}
103+
104+
if (path_walk(&target, &pathname, ns) == ERR)
99105
{
100106
panic(NULL, "Failed to walk to /sys");
101107
}

src/kernel/fs/vfs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static uint64_t vfs_open_lookup(path_t* path, const pathname_t* pathname, namesp
155155

156156
file_t* vfs_open(const pathname_t* pathname, process_t* process)
157157
{
158-
if (!PATHNAME_IS_VALID(pathname) || process == NULL)
158+
if (pathname == NULL || process == NULL)
159159
{
160160
errno = EINVAL;
161161
return NULL;
@@ -166,7 +166,7 @@ file_t* vfs_open(const pathname_t* pathname, process_t* process)
166166

167167
uint64_t vfs_open2(const pathname_t* pathname, file_t* files[2], process_t* process)
168168
{
169-
if (!PATHNAME_IS_VALID(pathname) || files == NULL || process == NULL)
169+
if (pathname == NULL || files == NULL || process == NULL)
170170
{
171171
errno = EINVAL;
172172
return ERR;
@@ -223,7 +223,7 @@ uint64_t vfs_open2(const pathname_t* pathname, file_t* files[2], process_t* proc
223223

224224
file_t* vfs_openat(const path_t* from, const pathname_t* pathname, process_t* process)
225225
{
226-
if (!PATHNAME_IS_VALID(pathname) || process == NULL)
226+
if (pathname == NULL || process == NULL)
227227
{
228228
errno = EINVAL;
229229
return NULL;
@@ -948,7 +948,7 @@ size_t vfs_getdents(file_t* file, dirent_t* buffer, size_t count)
948948

949949
uint64_t vfs_stat(const pathname_t* pathname, stat_t* buffer, process_t* process)
950950
{
951-
if (!PATHNAME_IS_VALID(pathname) || buffer == NULL || process == NULL)
951+
if (pathname == NULL || buffer == NULL || process == NULL)
952952
{
953953
errno = EINVAL;
954954
return ERR;
@@ -1013,7 +1013,7 @@ uint64_t vfs_stat(const pathname_t* pathname, stat_t* buffer, process_t* process
10131013

10141014
uint64_t vfs_link(const pathname_t* oldPathname, const pathname_t* newPathname, process_t* process)
10151015
{
1016-
if (!PATHNAME_IS_VALID(oldPathname) || !PATHNAME_IS_VALID(newPathname) || process == NULL)
1016+
if (oldPathname == NULL || newPathname == NULL || process == NULL)
10171017
{
10181018
errno = EINVAL;
10191019
return ERR;
@@ -1134,7 +1134,7 @@ size_t vfs_readlink(inode_t* symlink, char* buffer, size_t count)
11341134

11351135
uint64_t vfs_symlink(const pathname_t* oldPathname, const pathname_t* newPathname, process_t* process)
11361136
{
1137-
if (!PATHNAME_IS_VALID(oldPathname) || !PATHNAME_IS_VALID(newPathname) || process == NULL)
1137+
if (oldPathname == NULL || newPathname == NULL || process == NULL)
11381138
{
11391139
errno = EINVAL;
11401140
return ERR;
@@ -1196,7 +1196,7 @@ uint64_t vfs_symlink(const pathname_t* oldPathname, const pathname_t* newPathnam
11961196

11971197
uint64_t vfs_remove(const pathname_t* pathname, process_t* process)
11981198
{
1199-
if (!PATHNAME_IS_VALID(pathname) || process == NULL)
1199+
if (pathname == NULL || process == NULL)
12001200
{
12011201
errno = EINVAL;
12021202
return ERR;

src/kernel/module/module.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,13 @@ typedef struct
371371
static uint64_t module_file_read(module_file_t* outFile, const path_t* dirPath, process_t* process,
372372
const char* filename)
373373
{
374-
file_t* file = vfs_openat(dirPath, PATHNAME(filename), process);
374+
pathname_t pathname;
375+
if (pathname_init(&pathname, filename) == ERR)
376+
{
377+
return ERR;
378+
}
379+
380+
file_t* file = vfs_openat(dirPath, &pathname, process);
375381
if (file == NULL)
376382
{
377383
return ERR;
@@ -589,7 +595,13 @@ static uint64_t module_cache_build(void)
589595
process_t* process = process_current();
590596
assert(process != NULL);
591597

592-
file_t* dir = vfs_open(PATHNAME(MODULE_DIR), process);
598+
pathname_t moduleDir;
599+
if (pathname_init(&moduleDir, MODULE_DIR) == ERR)
600+
{
601+
return ERR;
602+
}
603+
604+
file_t* dir = vfs_open(&moduleDir, process);
593605
if (dir == NULL)
594606
{
595607
return ERR;
@@ -1075,7 +1087,13 @@ uint64_t module_device_attach(const char* type, const char* name, module_load_fl
10751087
return 0;
10761088
}
10771089

1078-
file_t* dir = vfs_open(PATHNAME(MODULE_DIR), process_current());
1090+
pathname_t moduleDir;
1091+
if (pathname_init(&moduleDir, MODULE_DIR) == ERR)
1092+
{
1093+
return ERR;
1094+
}
1095+
1096+
file_t* dir = vfs_open(&moduleDir, process_current());
10791097
if (dir == NULL)
10801098
{
10811099
return ERR;

src/kernel/sched/loader.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ void loader_exec(void)
4747

4848
uintptr_t* addrs = NULL;
4949

50-
file = vfs_open(PATHNAME(process->argv[0]), process);
50+
pathname_t pathname;
51+
if (pathname_init(&pathname, process->argv[0]) == ERR)
52+
{
53+
goto cleanup;
54+
}
55+
56+
file = vfs_open(&pathname, process);
5157
if (file == NULL)
5258
{
5359
goto cleanup;

0 commit comments

Comments
 (0)