From 9b8f35259c375f6911b76c38ec37e6d422b26bbe Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Tue, 29 Jun 2021 13:11:03 +0200 Subject: [PATCH] AK: Remove the LexicalPath::is_valid() API Since this is always set to true on the non-default constructor and subsequently never modified, it is somewhat pointless. Furthermore, there are arguably no invalid relative paths. --- AK/LexicalPath.cpp | 2 +- AK/LexicalPath.h | 3 +-- AK/URL.cpp | 2 +- Tests/AK/TestLexicalPath.cpp | 6 ------ .../FileManager/PropertiesWindow.cpp | 2 -- .../HackStudio/Dialogs/NewProjectDialog.cpp | 12 ++---------- Userland/Libraries/LibCore/FileWatcher.cpp | 18 +----------------- Userland/Services/WindowServer/Cursor.cpp | 4 ---- Userland/Shell/Builtin.cpp | 18 ++++-------------- Userland/Utilities/mktemp.cpp | 13 ++++--------- 10 files changed, 14 insertions(+), 66 deletions(-) diff --git a/AK/LexicalPath.cpp b/AK/LexicalPath.cpp index 3e3e92b7462..803cfa56765 100644 --- a/AK/LexicalPath.cpp +++ b/AK/LexicalPath.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Max Wipfli * * SPDX-License-Identifier: BSD-2-Clause */ @@ -15,7 +16,6 @@ LexicalPath::LexicalPath(String s) : m_string(move(s)) { canonicalize(); - m_is_valid = true; } void LexicalPath::canonicalize() diff --git a/AK/LexicalPath.h b/AK/LexicalPath.h index 7e2b2b7c133..7efbe04b1a9 100644 --- a/AK/LexicalPath.h +++ b/AK/LexicalPath.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Max Wipfli * * SPDX-License-Identifier: BSD-2-Clause */ @@ -16,7 +17,6 @@ public: LexicalPath() = default; explicit LexicalPath(String); - bool is_valid() const { return m_is_valid; } bool is_absolute() const { return m_is_absolute; } String const& string() const { return m_string; } @@ -53,7 +53,6 @@ private: String m_basename; String m_title; String m_extension; - bool m_is_valid { false }; bool m_is_absolute { false }; }; diff --git a/AK/URL.cpp b/AK/URL.cpp index 4f1d96f0f0a..b3f46ad3a28 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -162,7 +162,7 @@ u16 URL::default_port_for_scheme(StringView const& scheme) URL URL::create_with_file_scheme(String const& path, String const& fragment, String const& hostname) { LexicalPath lexical_path(path); - if (!lexical_path.is_valid() || !lexical_path.is_absolute()) + if (!lexical_path.is_absolute()) return {}; URL url; diff --git a/Tests/AK/TestLexicalPath.cpp b/Tests/AK/TestLexicalPath.cpp index b9ba6aebe19..da76bcbb997 100644 --- a/Tests/AK/TestLexicalPath.cpp +++ b/Tests/AK/TestLexicalPath.cpp @@ -9,15 +9,9 @@ #include #include -TEST_CASE(construct) -{ - EXPECT_EQ(LexicalPath().is_valid(), false); -} - TEST_CASE(basic) { LexicalPath path("/abc/def/ghi.txt"); - EXPECT_EQ(path.is_valid(), true); EXPECT_EQ(path.basename(), "ghi.txt"); EXPECT_EQ(path.title(), "ghi"); EXPECT_EQ(path.extension(), "txt"); diff --git a/Userland/Applications/FileManager/PropertiesWindow.cpp b/Userland/Applications/FileManager/PropertiesWindow.cpp index 10682d9de11..a43a3b8dd44 100644 --- a/Userland/Applications/FileManager/PropertiesWindow.cpp +++ b/Userland/Applications/FileManager/PropertiesWindow.cpp @@ -28,7 +28,6 @@ PropertiesWindow::PropertiesWindow(const String& path, bool disable_rename, Wind : Window(parent_window) { auto lexical_path = LexicalPath(path); - VERIFY(lexical_path.is_valid()); auto& main_widget = set_main_widget(); main_widget.set_layout(); @@ -103,7 +102,6 @@ PropertiesWindow::PropertiesWindow(const String& path, bool disable_rename, Wind perror("readlink"); } else { auto link_directory = LexicalPath(link_destination); - VERIFY(link_directory.is_valid()); auto link_parent = URL::create_with_file_protocol(link_directory.dirname(), link_directory.basename()); properties.append({ "Link target:", link_destination, Optional(link_parent) }); } diff --git a/Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.cpp b/Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.cpp index 512d34db6ea..95f262f0dbc 100644 --- a/Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.cpp +++ b/Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.cpp @@ -182,18 +182,10 @@ Optional NewProjectDialog::get_project_full_path() auto create_in = m_create_in_input->text(); auto maybe_project_name = get_available_project_name(); - if (!maybe_project_name.has_value()) { - return {}; - } - - auto project_name = maybe_project_name.value(); - auto full_path = LexicalPath(String::formatted("{}/{}", create_in, project_name)); - - // Do not permit otherwise invalid paths. - if (!full_path.is_valid()) + if (!maybe_project_name.has_value()) return {}; - return full_path.string(); + return LexicalPath::join(create_in, *maybe_project_name).string(); } void NewProjectDialog::do_create_project() diff --git a/Userland/Libraries/LibCore/FileWatcher.cpp b/Userland/Libraries/LibCore/FileWatcher.cpp index cab5fec4921..85abe928037 100644 --- a/Userland/Libraries/LibCore/FileWatcher.cpp +++ b/Userland/Libraries/LibCore/FileWatcher.cpp @@ -74,13 +74,7 @@ static Optional get_event_from_fd(int fd, HashMapname_length > 0) { String child_name { event->name, event->name_length - 1 }; - auto lexical_path = LexicalPath::join(path, child_name); - if (!lexical_path.is_valid()) { - dbgln_if(FILE_WATCHER_DEBUG, "get_event_from_fd: Reading from wd {}: Invalid child name '{}'", fd, child_name); - return {}; - } - - result.event_path = lexical_path.string(); + result.event_path = LexicalPath::join(path, child_name).string(); } else { result.event_path = path; } @@ -100,11 +94,6 @@ Result FileWatcherBase::add_watch(String path, FileWatcherEvent::T free(buf); } - if (!lexical_path.is_valid()) { - dbgln_if(FILE_WATCHER_DEBUG, "add_watch: path '{}' invalid", path); - return false; - } - auto const& canonical_path = lexical_path.string(); if (m_path_to_wd.find(canonical_path) != m_path_to_wd.end()) { dbgln_if(FILE_WATCHER_DEBUG, "add_watch: path '{}' is already being watched", canonical_path); @@ -145,11 +134,6 @@ Result FileWatcherBase::remove_watch(String path) free(buf); } - if (!lexical_path.is_valid()) { - dbgln_if(FILE_WATCHER_DEBUG, "remove_watch: path '{}' invalid", path); - return false; - } - auto const& canonical_path = lexical_path.string(); auto it = m_path_to_wd.find(canonical_path); if (it == m_path_to_wd.end()) { diff --git a/Userland/Services/WindowServer/Cursor.cpp b/Userland/Services/WindowServer/Cursor.cpp index 7c759e97f96..2a192b1fa3c 100644 --- a/Userland/Services/WindowServer/Cursor.cpp +++ b/Userland/Services/WindowServer/Cursor.cpp @@ -14,10 +14,6 @@ namespace WindowServer { CursorParams CursorParams::parse_from_filename(const StringView& cursor_path, const Gfx::IntPoint& default_hotspot) { LexicalPath path(cursor_path); - if (!path.is_valid()) { - dbgln("Cannot parse invalid cursor path, use default cursor params"); - return { default_hotspot }; - } auto file_title = path.title(); auto last_dot_in_title = StringView(file_title).find_last_of('.'); if (!last_dot_in_title.has_value() || last_dot_in_title.value() == 0) { diff --git a/Userland/Shell/Builtin.cpp b/Userland/Shell/Builtin.cpp index 8c0d7d50a8a..60e499e43c7 100644 --- a/Userland/Shell/Builtin.cpp +++ b/Userland/Shell/Builtin.cpp @@ -643,10 +643,6 @@ int Shell::builtin_popd(int argc, const char** argv) } LexicalPath lexical_path(path.characters()); - if (!lexical_path.is_valid()) { - warnln("LexicalPath failed to canonicalize '{}'", path); - return 1; - } const char* real_path = lexical_path.string().characters(); @@ -729,16 +725,10 @@ int Shell::builtin_pushd(int argc, const char** argv) } } - LexicalPath lexical_path(path_builder.to_string()); - if (!lexical_path.is_valid()) { - warnln("LexicalPath failed to canonicalize '{}'", path_builder.string_view()); - return 1; - } - - const char* real_path = lexical_path.string().characters(); + auto real_path = LexicalPath::canonicalized_path(path_builder.to_string()); struct stat st; - int rc = stat(real_path, &st); + int rc = stat(real_path.characters(), &st); if (rc < 0) { warnln("stat({}) failed: {}", real_path, strerror(errno)); return 1; @@ -750,13 +740,13 @@ int Shell::builtin_pushd(int argc, const char** argv) } if (should_switch) { - int rc = chdir(real_path); + int rc = chdir(real_path.characters()); if (rc < 0) { warnln("chdir({}) failed: {}", real_path, strerror(errno)); return 1; } - cwd = lexical_path.string(); + cwd = real_path; } return 0; diff --git a/Userland/Utilities/mktemp.cpp b/Userland/Utilities/mktemp.cpp index c1d1f984491..8d82e9eb762 100644 --- a/Userland/Utilities/mktemp.cpp +++ b/Userland/Utilities/mktemp.cpp @@ -97,20 +97,15 @@ int main(int argc, char** argv) return 1; } - LexicalPath target_path(String::formatted("{}/{}", target_directory, file_template)); - if (!target_path.is_valid()) { - if (!quiet) - warnln("Invalid template path {}", target_path.string().characters()); - return 1; - } + auto target_path = LexicalPath::join(target_directory, file_template).string(); - char* final_path = make_temp(target_path.string().characters(), create_directory, dry_run); + char* final_path = make_temp(target_path.characters(), create_directory, dry_run); if (!final_path) { if (!quiet) { if (create_directory) - warnln("Failed to create directory via template {}", target_path.string().characters()); + warnln("Failed to create directory via template {}", target_path.characters()); else - warnln("Failed to create file via template {}", target_path.string().characters()); + warnln("Failed to create file via template {}", target_path.characters()); } return 1; }