From 94bcb5bea8b8f093ef960bac064bd9287f82acde Mon Sep 17 00:00:00 2001 From: Valtteri Koskivuori Date: Thu, 2 Feb 2023 17:10:29 +0200 Subject: [PATCH] Userland: Propagate errors from file detail providers in file utility They already return ErrorOr, so we can just use TRY() instead of manually checking for errors. --- Userland/Utilities/file.cpp | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/Userland/Utilities/file.cpp b/Userland/Utilities/file.cpp index 7d304f8de2..b6638b4d1e 100644 --- a/Userland/Utilities/file.cpp +++ b/Userland/Utilities/file.cpp @@ -29,13 +29,9 @@ static ErrorOr> description_only(StringView description, String // FIXME: Ideally Gfx::ImageDecoder could tell us the image type directly. static ErrorOr> image_details(StringView description, StringView path) { - auto file_or_error = Core::MappedFile::map(path); - if (file_or_error.is_error()) - return OptionalNone {}; - - auto& mapped_file = *file_or_error.value(); + auto mapped_file = TRY(Core::MappedFile::map(path)); auto mime_type = Core::guess_mime_type_based_on_filename(path); - auto image_decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(mapped_file.bytes(), mime_type); + auto image_decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(mapped_file->bytes(), mime_type); if (!image_decoder) return OptionalNone {}; @@ -97,15 +93,11 @@ static ErrorOr> audio_details(StringView description, StringVie static ErrorOr> gzip_details(StringView description, StringView path) { - auto file_or_error = Core::MappedFile::map(path); - if (file_or_error.is_error()) + auto mapped_file = TRY(Core::MappedFile::map(path)); + if (!Compress::GzipDecompressor::is_likely_compressed(mapped_file->bytes())) return OptionalNone {}; - auto& mapped_file = *file_or_error.value(); - if (!Compress::GzipDecompressor::is_likely_compressed(mapped_file.bytes())) - return OptionalNone {}; - - auto gzip_details = Compress::GzipDecompressor::describe_header(mapped_file.bytes()); + auto gzip_details = Compress::GzipDecompressor::describe_header(mapped_file->bytes()); if (!gzip_details.has_value()) return OptionalNone {}; @@ -114,11 +106,8 @@ static ErrorOr> gzip_details(StringView description, StringView static ErrorOr> elf_details(StringView description, StringView path) { - auto file_or_error = Core::MappedFile::map(path); - if (file_or_error.is_error()) - return OptionalNone {}; - auto& mapped_file = *file_or_error.value(); - auto elf_data = mapped_file.bytes(); + auto mapped_file = TRY(Core::MappedFile::map(path)); + auto elf_data = mapped_file->bytes(); ELF::Image elf_image(elf_data); if (!elf_image.is_valid()) return OptionalNone {};