1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibCore: Let File::remove return a normal ErrorOr

Having the file path in there is nice, but it makes us incompatible with
comfortable error propagation in everything that isn't File::remove.
This commit is contained in:
Tim Schumacher 2022-12-23 13:59:27 +01:00 committed by Tim Flynn
parent 7fa78b2456
commit 355e761a02
4 changed files with 10 additions and 18 deletions

View file

@ -368,8 +368,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (deletion_result.is_error()) { if (deletion_result.is_error()) {
auto retry_message_result = GUI::MessageBox::show(window, auto retry_message_result = GUI::MessageBox::show(window,
DeprecatedString::formatted("Failed to delete \"{}\": {}. Retry?", DeprecatedString::formatted("Failed to delete \"{}\": {}. Retry?",
deletion_result.error().file, selected_node_path,
static_cast<Error const&>(deletion_result.error())), deletion_result.error()),
"Deletion failed"sv, "Deletion failed"sv,
GUI::MessageBox::Type::Error, GUI::MessageBox::Type::Error,
GUI::MessageBox::InputType::YesNo); GUI::MessageBox::InputType::YesNo);

View file

@ -685,12 +685,12 @@ NonnullRefPtr<GUI::Action> HackStudioWidget::create_delete_action()
auto& error = result.error(); auto& error = result.error();
if (is_directory) { if (is_directory) {
GUI::MessageBox::show(window(), GUI::MessageBox::show(window(),
DeprecatedString::formatted("Removing directory {} from the project failed: {}", error.file, static_cast<Error const&>(error)), DeprecatedString::formatted("Removing directory {} from the project failed: {}", file, error),
"Removal failed"sv, "Removal failed"sv,
GUI::MessageBox::Type::Error); GUI::MessageBox::Type::Error);
} else { } else {
GUI::MessageBox::show(window(), GUI::MessageBox::show(window(),
DeprecatedString::formatted("Removing file {} from the project failed: {}", error.file, static_cast<Error const&>(error)), DeprecatedString::formatted("Removing file {} from the project failed: {}", file, error),
"Removal failed"sv, "Removal failed"sv,
GUI::MessageBox::Type::Error); GUI::MessageBox::Type::Error);
} }

View file

@ -549,19 +549,19 @@ ErrorOr<void> File::link_file(DeprecatedString const& dst_path, DeprecatedString
return {}; return {};
} }
ErrorOr<void, File::RemoveError> File::remove(DeprecatedString const& path, RecursionMode mode, bool force) ErrorOr<void> File::remove(DeprecatedString const& path, RecursionMode mode, bool force)
{ {
struct stat path_stat; struct stat path_stat;
if (lstat(path.characters(), &path_stat) < 0) { if (lstat(path.characters(), &path_stat) < 0) {
if (!force) if (!force)
return RemoveError { path, errno }; return Error::from_errno(errno);
return {}; return {};
} }
if (S_ISDIR(path_stat.st_mode) && mode == RecursionMode::Allowed) { if (S_ISDIR(path_stat.st_mode) && mode == RecursionMode::Allowed) {
auto di = DirIterator(path, DirIterator::SkipParentAndBaseDir); auto di = DirIterator(path, DirIterator::SkipParentAndBaseDir);
if (di.has_error()) if (di.has_error())
return RemoveError { path, di.error() }; return Error::from_errno(di.error());
while (di.has_next()) { while (di.has_next()) {
auto result = remove(di.next_full_path(), RecursionMode::Allowed, true); auto result = remove(di.next_full_path(), RecursionMode::Allowed, true);
@ -570,10 +570,10 @@ ErrorOr<void, File::RemoveError> File::remove(DeprecatedString const& path, Recu
} }
if (rmdir(path.characters()) < 0 && !force) if (rmdir(path.characters()) < 0 && !force)
return RemoveError { path, errno }; return Error::from_errno(errno);
} else { } else {
if (unlink(path.characters()) < 0 && !force) if (unlink(path.characters()) < 0 && !force)
return RemoveError { path, errno }; return Error::from_errno(errno);
} }
return {}; return {};

View file

@ -93,15 +93,7 @@ public:
static ErrorOr<DeprecatedString> read_link(DeprecatedString const& link_path); static ErrorOr<DeprecatedString> read_link(DeprecatedString const& link_path);
static ErrorOr<void> link_file(DeprecatedString const& dst_path, DeprecatedString const& src_path); static ErrorOr<void> link_file(DeprecatedString const& dst_path, DeprecatedString const& src_path);
struct RemoveError : public Error { static ErrorOr<void> remove(DeprecatedString const& path, RecursionMode, bool force);
RemoveError(DeprecatedString f, int error_code)
: Error(error_code)
, file(move(f))
{
}
DeprecatedString file;
};
static ErrorOr<void, RemoveError> remove(DeprecatedString const& path, RecursionMode, bool force);
virtual bool open(OpenMode) override; virtual bool open(OpenMode) override;