From c7e62d448ca45c25841f57d1dcb9056b9d73da37 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 7 Nov 2021 01:31:00 +0100 Subject: [PATCH] LibCore: Use ErrorOr for Core::File::remove() This function returns a subclass of Error, which is now possible. --- Userland/Applications/SpaceAnalyzer/main.cpp | 2 +- Userland/DevTools/HackStudio/HackStudioWidget.cpp | 7 +++---- Userland/Libraries/LibCore/File.cpp | 10 +++++----- Userland/Libraries/LibCore/File.h | 10 +++++++--- Userland/Utilities/rm.cpp | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Userland/Applications/SpaceAnalyzer/main.cpp b/Userland/Applications/SpaceAnalyzer/main.cpp index 43bc47ff7c5..31b5fc12641 100644 --- a/Userland/Applications/SpaceAnalyzer/main.cpp +++ b/Userland/Applications/SpaceAnalyzer/main.cpp @@ -353,7 +353,7 @@ int main(int argc, char* argv[]) auto retry_message_result = GUI::MessageBox::show(window, String::formatted("Failed to delete \"{}\": {}. Retry?", deletion_result.error().file, - deletion_result.error().error_code.string()), + static_cast(deletion_result.error())), "Deletion failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::YesNo); diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index 3236c93fea2..4e19862d03c 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -546,17 +546,16 @@ NonnullRefPtr HackStudioWidget::create_delete_action() } bool is_directory = S_ISDIR(st.st_mode); - auto result = Core::File::remove(file, Core::File::RecursionMode::Allowed, false); - if (result.is_error()) { + if (auto result = Core::File::remove(file, Core::File::RecursionMode::Allowed, false); !result.is_error()) { auto& error = result.error(); if (is_directory) { GUI::MessageBox::show(window(), - String::formatted("Removing directory {} from the project failed: {}", error.file, error.error_code), + String::formatted("Removing directory {} from the project failed: {}", error.file, static_cast(error)), "Removal failed", GUI::MessageBox::Type::Error); } else { GUI::MessageBox::show(window(), - String::formatted("Removing file {} from the project failed: {}", error.file, error.error_code), + String::formatted("Removing file {} from the project failed: {}", error.file, static_cast(error)), "Removal failed", GUI::MessageBox::Type::Error); } diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index dc21704019a..7a9803b58f2 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -512,19 +512,19 @@ ErrorOr File::link_file(String const& dst_path, String const& src_path) return {}; } -Result File::remove(String const& path, RecursionMode mode, bool force) +ErrorOr File::remove(String const& path, RecursionMode mode, bool force) { struct stat path_stat; if (lstat(path.characters(), &path_stat) < 0) { if (!force) - return RemoveError { path, OSError(errno) }; + return RemoveError { path, errno }; return {}; } if (S_ISDIR(path_stat.st_mode) && mode == RecursionMode::Allowed) { auto di = DirIterator(path, DirIterator::SkipParentAndBaseDir); if (di.has_error()) - return RemoveError { path, OSError(di.error()) }; + return RemoveError { path, di.error() }; while (di.has_next()) { auto result = remove(di.next_full_path(), RecursionMode::Allowed, true); @@ -533,10 +533,10 @@ Result File::remove(String const& path, RecursionMode m } if (rmdir(path.characters()) < 0 && !force) - return RemoveError { path, OSError(errno) }; + return RemoveError { path, errno }; } else { if (unlink(path.characters()) < 0 && !force) - return RemoveError { path, OSError(errno) }; + return RemoveError { path, errno }; } return {}; diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h index 1b66536b63a..87603881d01 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -72,11 +72,15 @@ public: static String read_link(String const& link_path); static ErrorOr link_file(String const& dst_path, String const& src_path); - struct RemoveError { + struct RemoveError : public Error { + RemoveError(String f, int error_code) + : Error(error_code) + , file(move(f)) + { + } String file; - OSError error_code; }; - static Result remove(String const& path, RecursionMode, bool force); + static ErrorOr remove(String const& path, RecursionMode, bool force); virtual bool open(OpenMode) override; diff --git a/Userland/Utilities/rm.cpp b/Userland/Utilities/rm.cpp index c1650aa12b9..a6e08b87f64 100644 --- a/Userland/Utilities/rm.cpp +++ b/Userland/Utilities/rm.cpp @@ -40,7 +40,7 @@ int main(int argc, char** argv) auto result = Core::File::remove(path, recursive ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed, force); if (result.is_error()) { - warnln("rm: cannot remove '{}': {}", path, result.error().error_code); + warnln("rm: cannot remove '{}': {}", path, static_cast(result.error())); had_errors = true; }