1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 17:38:12 +00:00

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
5 changed files with 17 additions and 14 deletions

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