LibCore: Use ErrorOr<T> for Core::File::remove()

This function returns a subclass of Error, which is now possible.
This commit is contained in:
Andreas Kling 2021-11-07 01:31:00 +01:00
parent e253cf694e
commit c7e62d448c
Notes: sideshowbarker 2024-07-18 01:24:03 +09:00
5 changed files with 17 additions and 14 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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 {};

View File

@ -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;

View File

@ -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;
}