diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-07 01:31:00 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-08 00:35:27 +0100 |
commit | c7e62d448ca45c25841f57d1dcb9056b9d73da37 (patch) | |
tree | 05e17bbc2b3f9e63860bc1dc582e3d0bd6dd6a87 /Userland | |
parent | e253cf694e5c710303bb6560315e80e79d5664bd (diff) | |
download | serenity-c7e62d448ca45c25841f57d1dcb9056b9d73da37.zip |
LibCore: Use ErrorOr<T> for Core::File::remove()
This function returns a subclass of Error, which is now possible.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/SpaceAnalyzer/main.cpp | 2 | ||||
-rw-r--r-- | Userland/DevTools/HackStudio/HackStudioWidget.cpp | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/File.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/File.h | 10 | ||||
-rw-r--r-- | 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 43bc47ff7c..31b5fc1264 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<Error const&>(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 3236c93fea..4e19862d03 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -546,17 +546,16 @@ NonnullRefPtr<GUI::Action> 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 const&>(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 const&>(error)), "Removal failed", GUI::MessageBox::Type::Error); } diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index dc21704019..7a9803b58f 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -512,19 +512,19 @@ ErrorOr<void> File::link_file(String const& dst_path, String const& src_path) return {}; } -Result<void, File::RemoveError> File::remove(String const& path, RecursionMode mode, bool force) +ErrorOr<void, File::RemoveError> 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<void, File::RemoveError> 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 1b66536b63..87603881d0 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<void> 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<void, RemoveError> remove(String const& path, RecursionMode, bool force); + static ErrorOr<void, RemoveError> 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 c1650aa12b..a6e08b87f6 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<Error const&>(result.error())); had_errors = true; } |