From b4a6e4d56d5e23df2b88cc401b0610351ae2d7ba Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 19 Jun 2023 11:21:58 +0200 Subject: [PATCH] libluna/PathParser: Make dirname() and basename() static functions This avoids creating a PathParser to use them, which allocates memory that won't be used. --- apps/mkdir.cpp | 3 +-- kernel/src/fs/VFS.cpp | 30 ++++++++++++------------------ kernel/src/sys/link.cpp | 16 ++++++---------- libluna/include/luna/PathParser.h | 4 ++-- libluna/src/PathParser.cpp | 8 ++++---- 5 files changed, 25 insertions(+), 36 deletions(-) diff --git a/apps/mkdir.cpp b/apps/mkdir.cpp index 8744bfc7..bc809f26 100644 --- a/apps/mkdir.cpp +++ b/apps/mkdir.cpp @@ -20,8 +20,7 @@ begin: if (rc.error() == EEXIST) return {}; if (rc.error() == ENOENT) { - PathParser parser = TRY(PathParser::create(path.chars())); - auto parent = TRY(parser.dirname()); + auto parent = TRY(PathParser::dirname(path)); TRY(mkdir_recursively(parent.view(), (0777 & ~s_umask) | S_IWUSR | S_IXUSR)); diff --git a/kernel/src/fs/VFS.cpp b/kernel/src/fs/VFS.cpp index d8e3c6d1..340eefdd 100644 --- a/kernel/src/fs/VFS.cpp +++ b/kernel/src/fs/VFS.cpp @@ -71,14 +71,13 @@ namespace VFS Result> create_directory(const char* path, Credentials auth, SharedPtr working_directory) { - auto parser = TRY(PathParser::create(path)); - auto parent_path = TRY(parser.dirname()); + auto parent_path = TRY(PathParser::dirname(path)); auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory)); if (!can_write(parent_inode, auth)) return err(EACCES); - auto child_name = TRY(parser.basename()); + auto child_name = TRY(PathParser::basename(path)); TRY(validate_filename(child_name.view())); @@ -87,14 +86,13 @@ namespace VFS Result> create_file(const char* path, Credentials auth, SharedPtr working_directory) { - auto parser = TRY(PathParser::create(path)); - auto parent_path = TRY(parser.dirname()); + auto parent_path = TRY(PathParser::dirname(path)); auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory)); if (!can_write(parent_inode, auth)) return err(EACCES); - auto child_name = TRY(parser.basename()); + auto child_name = TRY(PathParser::basename(path)); TRY(validate_filename(child_name.view())); @@ -189,9 +187,8 @@ namespace VFS Result pivot_root(const char* new_root, const char* put_old, SharedPtr working_directory) { - auto root_parser = TRY(PathParser::create(new_root)); - auto new_root_parent = TRY(root_parser.dirname()); - auto new_root_path = TRY(root_parser.basename()); + auto new_root_parent = TRY(PathParser::dirname(new_root)); + auto new_root_path = TRY(PathParser::basename(new_root)); auto new_root_parent_inode = TRY(VFS::resolve_path(new_root_parent.chars(), Credentials {}, working_directory)); auto new_root_inode = TRY(new_root_parent_inode->find(new_root_path.chars())); @@ -200,9 +197,8 @@ namespace VFS if (!new_root_inode->is_mountpoint()) return err(EINVAL); if (new_root_inode->fs() == g_root_inode->fs()) return err(EBUSY); - auto parser = TRY(PathParser::create(put_old)); - auto parent_path = TRY(parser.dirname()); - auto child = TRY(parser.basename()); + auto parent_path = TRY(PathParser::dirname(put_old)); + auto child = TRY(PathParser::basename(put_old)); kdbgln("vfs: Pivoting root from / to %s, using %s as new root", put_old, new_root); @@ -228,9 +224,8 @@ namespace VFS Result mount(const char* path, SharedPtr fs, Credentials auth, SharedPtr working_directory) { - auto parser = TRY(PathParser::create(path)); - auto parent_path = TRY(parser.dirname()); - auto child = TRY(parser.basename()); + auto parent_path = TRY(PathParser::dirname(path)); + auto child = TRY(PathParser::basename(path)); kinfoln("vfs: Mounting filesystem on target %s", path); @@ -249,9 +244,8 @@ namespace VFS Result umount(const char* path, Credentials auth, SharedPtr working_directory) { - auto parser = TRY(PathParser::create(path)); - auto parent_path = TRY(parser.dirname()); - auto child = TRY(parser.basename()); + auto parent_path = TRY(PathParser::dirname(path)); + auto child = TRY(PathParser::basename(path)); if (child.view() == "/") return err(EBUSY); diff --git a/kernel/src/sys/link.cpp b/kernel/src/sys/link.cpp index 293c6145..a9d0a09b 100644 --- a/kernel/src/sys/link.cpp +++ b/kernel/src/sys/link.cpp @@ -13,10 +13,8 @@ Result sys_unlinkat(Registers*, SyscallArgs args) Thread* current = Scheduler::current(); - PathParser parser = TRY(PathParser::create(path.chars())); - - auto dirname = TRY(parser.dirname()); - auto basename = TRY(parser.basename()); + auto dirname = TRY(PathParser::dirname(path.view())); + auto basename = TRY(PathParser::basename(path.view())); if (basename.view() == ".") return err(EINVAL); @@ -47,14 +45,13 @@ Result sys_symlinkat(Registers*, SyscallArgs args) auto* current = Scheduler::current(); - auto parser = TRY(PathParser::create(linkpath.chars())); - auto parent = TRY(parser.dirname()); + auto parent = TRY(PathParser::dirname(linkpath.view())); auto parent_inode = TRY(current->resolve_atfile(dirfd, parent, false, true)); if (!VFS::can_write(parent_inode, current->auth)) return err(EACCES); - auto child_name = TRY(parser.basename()); + auto child_name = TRY(PathParser::basename(linkpath.view())); TRY(VFS::validate_filename(child_name.view())); @@ -101,8 +98,7 @@ Result sys_linkat(Registers*, SyscallArgs args) auto* current = Scheduler::current(); - auto parser = TRY(PathParser::create(newpath.chars())); - auto parent = TRY(parser.dirname()); + auto parent = TRY(PathParser::dirname(newpath.view())); // FIXME: Use AT_SYMLINK_FOLLOW. auto target = TRY(current->resolve_atfile(olddirfd, oldpath, flags & AT_EMPTY_PATH, false)); @@ -115,7 +111,7 @@ Result sys_linkat(Registers*, SyscallArgs args) if (!VFS::can_write(parent_inode, current->auth)) return err(EACCES); - auto child_name = TRY(parser.basename()); + auto child_name = TRY(PathParser::basename(newpath.view())); TRY(VFS::validate_filename(child_name.view())); diff --git a/libluna/include/luna/PathParser.h b/libluna/include/luna/PathParser.h index 5f66372c..672b82e8 100644 --- a/libluna/include/luna/PathParser.h +++ b/libluna/include/luna/PathParser.h @@ -37,8 +37,8 @@ class PathParser return m_already_called_next ? (bool)m_strtok_saved_state : is_not_delim(*m_copy); } - Result basename(); - Result dirname(); + static Result basename(StringView path); + static Result dirname(StringView path); Option next(); diff --git a/libluna/src/PathParser.cpp b/libluna/src/PathParser.cpp index 4556ca37..5116b47c 100644 --- a/libluna/src/PathParser.cpp +++ b/libluna/src/PathParser.cpp @@ -35,9 +35,9 @@ Option PathParser::next() return result; } -Result PathParser::basename() +Result PathParser::basename(StringView path) { - char* copy = strdup(m_original); + char* copy = strdup(path.chars()); if (!copy) return err(ENOMEM); auto guard = make_scope_guard([copy] { free_impl(copy); }); @@ -48,9 +48,9 @@ Result PathParser::basename() return String::from_cstring(result); } -Result PathParser::dirname() +Result PathParser::dirname(StringView path) { - char* copy = strdup(m_original); + char* copy = strdup(path.chars()); if (!copy) return err(ENOMEM); auto guard = make_scope_guard([copy] { free_impl(copy); });