summaryrefslogtreecommitdiff
path: root/src/kernel
diff options
context:
space:
mode:
authordzwdz2022-08-05 14:01:42 +0200
committerdzwdz2022-08-05 14:01:42 +0200
commit27124aab29d54ba9f228dee18a48e903e222812a (patch)
tree7e4848516eca5ad581cd2d5282d28797eae0472b /src/kernel
parent749a150e37fbfdaf33a8d6738e95306e6d95e8b5 (diff)
move path_simplify to shared code, move its tests to userland
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/syscalls.c2
-rw-r--r--src/kernel/tests/vfs.c63
-rw-r--r--src/kernel/vfs/path.c58
-rw-r--r--src/kernel/vfs/path.h9
4 files changed, 1 insertions, 131 deletions
diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c
index 0ab6106..3096c42 100644
--- a/src/kernel/syscalls.c
+++ b/src/kernel/syscalls.c
@@ -1,3 +1,4 @@
+#include <camellia/path.h>
#include <camellia/errno.h>
#include <camellia/execbuf.h>
#include <camellia/flags.h>
@@ -8,7 +9,6 @@
#include <kernel/panic.h>
#include <kernel/pipe.h>
#include <kernel/proc.h>
-#include <kernel/vfs/path.h>
#include <shared/mem.h>
#include <stdint.h>
diff --git a/src/kernel/tests/vfs.c b/src/kernel/tests/vfs.c
index 7b353e8..9f7c6da 100644
--- a/src/kernel/tests/vfs.c
+++ b/src/kernel/tests/vfs.c
@@ -2,70 +2,8 @@
#include <kernel/tests/base.h>
#include <kernel/tests/tests.h>
#include <kernel/vfs/mount.h>
-#include <kernel/vfs/path.h>
#include <shared/mem.h>
-TEST(path_simplify) {
-#define TEST_WRAPPER(argument, result) do { \
- int len = path_simplify(argument, buf, sizeof(argument) - 1); \
- if (result == NULL) { \
- TEST_COND(len < 0); \
- } else { \
- if (len == sizeof(result) - 1) { \
- TEST_COND(0 == memcmp(result, buf, len)); \
- } else { \
- TEST_COND(false); \
- } \
- } \
- } while (0)
-
- char buf[256];
-
- // some easy cases first
- TEST_WRAPPER("/", "/");
- TEST_WRAPPER("/.", "/");
- TEST_WRAPPER("//", "/");
- TEST_WRAPPER("/asdf", "/asdf");
- TEST_WRAPPER("/asdf/", "/asdf/");
- TEST_WRAPPER("/asdf//", "/asdf/");
- TEST_WRAPPER("/asdf/./", "/asdf/");
- TEST_WRAPPER("/a/./b", "/a/b");
- TEST_WRAPPER("/a/./b/", "/a/b/");
-
- // some slightly less easy cases
- TEST_WRAPPER("/asdf/..", "/");
- TEST_WRAPPER("/asdf/../", "/");
- TEST_WRAPPER("/asdf/.", "/asdf/");
- TEST_WRAPPER("/asdf//.", "/asdf/");
-
- TEST_WRAPPER("/foo/bar/..", "/foo/");
- TEST_WRAPPER("/foo/bar/../baz", "/foo/baz");
- TEST_WRAPPER("/foo/bar/../baz/", "/foo/baz/");
- TEST_WRAPPER("/foo/bar/xyz/..", "/foo/bar/");
- TEST_WRAPPER("/foo/bar/xyz/../", "/foo/bar/");
-
- // going under the root or close to it
- TEST_WRAPPER("/..", NULL);
- TEST_WRAPPER("/../asdf", NULL);
- TEST_WRAPPER("/../asdf/", NULL);
- TEST_WRAPPER("/./a/../..", NULL);
- TEST_WRAPPER("/a/a/../..", "/");
- TEST_WRAPPER("/a/../a/..", "/");
- TEST_WRAPPER("/a/../../a", NULL);
- TEST_WRAPPER("/../a/../a", NULL);
- TEST_WRAPPER("/../../a/a", NULL);
- TEST_WRAPPER("/////../..", NULL);
- TEST_WRAPPER("//a//../..", NULL);
-
- // relative paths aren't allowed
- TEST_WRAPPER("relative", NULL);
- TEST_WRAPPER("some/stuff", NULL);
- TEST_WRAPPER("./stuff", NULL);
- TEST_WRAPPER("../stuff", NULL);
- TEST_WRAPPER("", NULL);
-#undef TEST_WRAPPER
-}
-
TEST(vfs_mount_resolve) {
struct vfs_mount *mount = NULL;
@@ -107,6 +45,5 @@ TEST(vfs_mount_resolve) {
}
void tests_vfs(void) {
- TEST_RUN(path_simplify);
TEST_RUN(vfs_mount_resolve);
}
diff --git a/src/kernel/vfs/path.c b/src/kernel/vfs/path.c
deleted file mode 100644
index e8ce49c..0000000
--- a/src/kernel/vfs/path.c
+++ /dev/null
@@ -1,58 +0,0 @@
-#include <kernel/panic.h>
-#include <kernel/vfs/path.h>
-#include <shared/mem.h>
-
-int path_simplify(const char *in, char *out, size_t len) {
- if (len == 0) return -1; // empty paths are invalid
- if (in[0] != '/') return -1; // so are relative paths
-
- int seg_len; // the length of the current path segment
- int out_pos = 0;
- bool directory = 0;
-
- for (size_t i = 0; i < len; i += seg_len + 1) {
- assert(in[i] == '/');
-
- seg_len = 0;
- directory = false;
- for (size_t j = i + 1; j < len; j++) {
- if (in[j] == '/') {
- directory = true;
- break;
- }
- seg_len++;
- }
-
- /* example iteration, illustrated with terrible ASCII art
- *
- * |i=5 |next i = i + seg_len + 1 = 10
- * v v
- * /some/path/asdf
- * |--|
- * seg_len = 4
- * (segment starts at i+1) */
-
- if (seg_len == 0 || (seg_len == 1 && in[i + 1] == '.')) {
- /* // or /./ */
- directory = true;
- } else if (seg_len == 2 && in[i + 1] == '.' && in[i + 2] == '.') {
- /* /../ */
- directory = true;
-
- /* try to backtrack to the last slash */
- while (--out_pos >= 0 && out[out_pos] != '/');
- if (out_pos < 0) return -1;
- } else {
- /* a normal segment, e.g. /asdf/ */
- out[out_pos] = '/';
- memcpy(&out[out_pos + 1], &in[i + 1], seg_len);
- out_pos += seg_len + 1;
- }
-
- }
-
- /* paths to directories should have a trailing slash */
- if (directory) out[out_pos++] = '/';
-
- return out_pos;
-}
diff --git a/src/kernel/vfs/path.h b/src/kernel/vfs/path.h
deleted file mode 100644
index 7484619..0000000
--- a/src/kernel/vfs/path.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#pragma once
-#include <camellia/path.h>
-#include <stddef.h>
-
-/** Reduce a path to its simplest form.
- *
- * @return length of the string in *out, always less than len. Negative if the path was invalid.
- */
-int path_simplify(const char *in, char *out, size_t len);