1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:48:10 +00:00

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.
This commit is contained in:
Max Wipfli 2021-06-29 13:11:03 +02:00 committed by Andreas Kling
parent caa9daf59e
commit 9b8f35259c
10 changed files with 14 additions and 66 deletions

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -15,7 +16,6 @@ LexicalPath::LexicalPath(String s)
: m_string(move(s)) : m_string(move(s))
{ {
canonicalize(); canonicalize();
m_is_valid = true;
} }
void LexicalPath::canonicalize() void LexicalPath::canonicalize()

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -16,7 +17,6 @@ public:
LexicalPath() = default; LexicalPath() = default;
explicit LexicalPath(String); explicit LexicalPath(String);
bool is_valid() const { return m_is_valid; }
bool is_absolute() const { return m_is_absolute; } bool is_absolute() const { return m_is_absolute; }
String const& string() const { return m_string; } String const& string() const { return m_string; }
@ -53,7 +53,6 @@ private:
String m_basename; String m_basename;
String m_title; String m_title;
String m_extension; String m_extension;
bool m_is_valid { false };
bool m_is_absolute { false }; bool m_is_absolute { false };
}; };

View file

@ -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) URL URL::create_with_file_scheme(String const& path, String const& fragment, String const& hostname)
{ {
LexicalPath lexical_path(path); LexicalPath lexical_path(path);
if (!lexical_path.is_valid() || !lexical_path.is_absolute()) if (!lexical_path.is_absolute())
return {}; return {};
URL url; URL url;

View file

@ -9,15 +9,9 @@
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/String.h> #include <AK/String.h>
TEST_CASE(construct)
{
EXPECT_EQ(LexicalPath().is_valid(), false);
}
TEST_CASE(basic) TEST_CASE(basic)
{ {
LexicalPath path("/abc/def/ghi.txt"); LexicalPath path("/abc/def/ghi.txt");
EXPECT_EQ(path.is_valid(), true);
EXPECT_EQ(path.basename(), "ghi.txt"); EXPECT_EQ(path.basename(), "ghi.txt");
EXPECT_EQ(path.title(), "ghi"); EXPECT_EQ(path.title(), "ghi");
EXPECT_EQ(path.extension(), "txt"); EXPECT_EQ(path.extension(), "txt");

View file

@ -28,7 +28,6 @@ PropertiesWindow::PropertiesWindow(const String& path, bool disable_rename, Wind
: Window(parent_window) : Window(parent_window)
{ {
auto lexical_path = LexicalPath(path); auto lexical_path = LexicalPath(path);
VERIFY(lexical_path.is_valid());
auto& main_widget = set_main_widget<GUI::Widget>(); auto& main_widget = set_main_widget<GUI::Widget>();
main_widget.set_layout<GUI::VerticalBoxLayout>(); main_widget.set_layout<GUI::VerticalBoxLayout>();
@ -103,7 +102,6 @@ PropertiesWindow::PropertiesWindow(const String& path, bool disable_rename, Wind
perror("readlink"); perror("readlink");
} else { } else {
auto link_directory = LexicalPath(link_destination); 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()); auto link_parent = URL::create_with_file_protocol(link_directory.dirname(), link_directory.basename());
properties.append({ "Link target:", link_destination, Optional(link_parent) }); properties.append({ "Link target:", link_destination, Optional(link_parent) });
} }

View file

@ -182,18 +182,10 @@ Optional<String> NewProjectDialog::get_project_full_path()
auto create_in = m_create_in_input->text(); auto create_in = m_create_in_input->text();
auto maybe_project_name = get_available_project_name(); auto maybe_project_name = get_available_project_name();
if (!maybe_project_name.has_value()) { 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())
return {}; return {};
return full_path.string(); return LexicalPath::join(create_in, *maybe_project_name).string();
} }
void NewProjectDialog::do_create_project() void NewProjectDialog::do_create_project()

View file

@ -74,13 +74,7 @@ static Optional<FileWatcherEvent> get_event_from_fd(int fd, HashMap<unsigned, St
// We trust that the kernel only sends the name when appropriate. // We trust that the kernel only sends the name when appropriate.
if (event->name_length > 0) { if (event->name_length > 0) {
String child_name { event->name, event->name_length - 1 }; String child_name { event->name, event->name_length - 1 };
auto lexical_path = LexicalPath::join(path, child_name); result.event_path = LexicalPath::join(path, child_name).string();
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();
} else { } else {
result.event_path = path; result.event_path = path;
} }
@ -100,11 +94,6 @@ Result<bool, String> FileWatcherBase::add_watch(String path, FileWatcherEvent::T
free(buf); 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(); auto const& canonical_path = lexical_path.string();
if (m_path_to_wd.find(canonical_path) != m_path_to_wd.end()) { 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); dbgln_if(FILE_WATCHER_DEBUG, "add_watch: path '{}' is already being watched", canonical_path);
@ -145,11 +134,6 @@ Result<bool, String> FileWatcherBase::remove_watch(String path)
free(buf); 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 const& canonical_path = lexical_path.string();
auto it = m_path_to_wd.find(canonical_path); auto it = m_path_to_wd.find(canonical_path);
if (it == m_path_to_wd.end()) { if (it == m_path_to_wd.end()) {

View file

@ -14,10 +14,6 @@ namespace WindowServer {
CursorParams CursorParams::parse_from_filename(const StringView& cursor_path, const Gfx::IntPoint& default_hotspot) CursorParams CursorParams::parse_from_filename(const StringView& cursor_path, const Gfx::IntPoint& default_hotspot)
{ {
LexicalPath path(cursor_path); 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 file_title = path.title();
auto last_dot_in_title = StringView(file_title).find_last_of('.'); auto last_dot_in_title = StringView(file_title).find_last_of('.');
if (!last_dot_in_title.has_value() || last_dot_in_title.value() == 0) { if (!last_dot_in_title.has_value() || last_dot_in_title.value() == 0) {

View file

@ -643,10 +643,6 @@ int Shell::builtin_popd(int argc, const char** argv)
} }
LexicalPath lexical_path(path.characters()); 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(); 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()); auto real_path = LexicalPath::canonicalized_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();
struct stat st; struct stat st;
int rc = stat(real_path, &st); int rc = stat(real_path.characters(), &st);
if (rc < 0) { if (rc < 0) {
warnln("stat({}) failed: {}", real_path, strerror(errno)); warnln("stat({}) failed: {}", real_path, strerror(errno));
return 1; return 1;
@ -750,13 +740,13 @@ int Shell::builtin_pushd(int argc, const char** argv)
} }
if (should_switch) { if (should_switch) {
int rc = chdir(real_path); int rc = chdir(real_path.characters());
if (rc < 0) { if (rc < 0) {
warnln("chdir({}) failed: {}", real_path, strerror(errno)); warnln("chdir({}) failed: {}", real_path, strerror(errno));
return 1; return 1;
} }
cwd = lexical_path.string(); cwd = real_path;
} }
return 0; return 0;

View file

@ -97,20 +97,15 @@ int main(int argc, char** argv)
return 1; return 1;
} }
LexicalPath target_path(String::formatted("{}/{}", target_directory, file_template)); auto target_path = LexicalPath::join(target_directory, file_template).string();
if (!target_path.is_valid()) {
if (!quiet)
warnln("Invalid template path {}", target_path.string().characters());
return 1;
}
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 (!final_path) {
if (!quiet) { if (!quiet) {
if (create_directory) 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 else
warnln("Failed to create file via template {}", target_path.string().characters()); warnln("Failed to create file via template {}", target_path.characters());
} }
return 1; return 1;
} }