diff options
author | Ian Moffett <ian@osmora.org> | 2025-09-19 00:42:14 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-09-19 00:42:14 -0400 |
commit | 6a0e4e3da5cf17635d094008c7cc5562b1f081e1 (patch) | |
tree | 95dfc7821f205d1658d63b769457018431904160 /src/sys | |
parent | 70dc7ad8924cf3e30e06b02698b1294fe9553538 (diff) |
kern: vfs: Add path component counter
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/include/os/vfs.h | 11 | ||||
-rw-r--r-- | src/sys/os/vfs_subr.c | 66 |
2 files changed, 77 insertions, 0 deletions
diff --git a/src/sys/include/os/vfs.h b/src/sys/include/os/vfs.h index ec7eb77..a46bb18 100644 --- a/src/sys/include/os/vfs.h +++ b/src/sys/include/os/vfs.h @@ -54,4 +54,15 @@ int vfs_init(void); */ int vfs_by_index(uint16_t index, struct fs_info **resp); +/* + * Count the number of components within a path and + * return a negative value if the path is invalid. + * + * @path: Path to check + * + * Returns the number of components on success, + * otherwise a less than zero value on failure. + */ +int vfs_cmp_cnt(const char *path); + #endif /* !_OS_VFS_H_ */ diff --git a/src/sys/os/vfs_subr.c b/src/sys/os/vfs_subr.c index d950b53..f9f4a60 100644 --- a/src/sys/os/vfs_subr.c +++ b/src/sys/os/vfs_subr.c @@ -31,9 +31,34 @@ #include <sys/errno.h> #include <os/vnode.h> #include <os/kalloc.h> +#include <os/vfs.h> #include <string.h> /* + * Returns a value of zero if a character value + * within a path is valid, otherwise a less than + * zero value on error. + */ +static int +vfs_pathc_valid(char c) +{ + /* Handle [A-Za-z] */ + if (c >= 'a' && c <= 'z') + return 0; + if (c >= 'A' && c <= 'Z') + return 0; + + /* Handle [0-9] and '/' */ + if (c >= '0' && c <= '9') + return 0; + if (c == '/') + return 0; + + return -1; + +} + +/* * Allocate a new vnode */ int @@ -67,3 +92,44 @@ vfs_vrel(struct vnode *vp, int flags) kfree(vp); return 0; } + + +/* + * Count number of components + */ +int +vfs_cmp_cnt(const char *path) +{ + const char *p; + int error, cnt = -1; + + if (path == NULL) { + return -EINVAL; + } + + if (*path != '/') { + return -ENOENT; + } + + p = path; + cnt = 0; + + while (*p != '\0') { + while (*p == '/') + ++p; + while (*p != '/' && *p != '\0') + error = vfs_pathc_valid(*p++); + + /* Invalid character? */ + if (error < 0) + return -EINVAL; + + /* Break if we got NUL */ + if (*p == '\0') + break; + + ++p, ++cnt; + } + + return cnt + 1; +} |