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

This commit is contained in:
Andreas Kling 2021-11-07 02:15:10 +01:00
parent fac2550143
commit a7f1f1c34b
Notes: sideshowbarker 2024-07-18 08:59:31 +09:00
24 changed files with 42 additions and 48 deletions

View file

@ -8,6 +8,7 @@
#include <LibTest/JavaScriptTestRunner.h>
#include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
#include <LibWasm/Types.h>
#include <string.h>
TEST_ROOT("Userland/Libraries/LibWasm/Tests");
@ -16,7 +17,7 @@ TESTJS_GLOBAL_FUNCTION(read_binary_wasm_file, readBinaryWasmFile)
auto filename = TRY(vm.argument(0).to_string(global_object));
auto file = Core::File::open(filename, Core::OpenMode::ReadOnly);
if (file.is_error())
return vm.throw_completion<JS::TypeError>(global_object, file.error().string());
return vm.throw_completion<JS::TypeError>(global_object, strerror(file.error().code()));
auto contents = file.value()->read_all();
auto array = JS::Uint8Array::create(global_object, contents.size());
contents.span().copy_to(array->data());

View file

@ -11,6 +11,7 @@
#include <LibGUI/Painter.h>
#include <LibGfx/BitmapFont.h>
#include <LibGfx/Palette.h>
#include <string.h>
static int x_offset;
static int y_offset;

View file

@ -23,6 +23,7 @@
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
HexEditor::HexEditor()

View file

@ -159,7 +159,7 @@ Result<void, String> Image::write_to_file(const String& file_path) const
auto file_or_error = Core::File::open(file_path, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate));
if (file_or_error.is_error())
return String { file_or_error.error().string() };
return String { strerror(file_or_error.error().code()) };
if (!file_or_error.value()->write(builder.string_view()))
return String { file_or_error.value()->error_string() };

View file

@ -15,6 +15,7 @@
#include <LibGUI/ColorPicker.h>
#include <LibGUI/MessageBox.h>
#include <LibGfx/Palette.h>
#include <string.h>
REGISTER_WIDGET(PixelPaint, PaletteWidget);
@ -263,7 +264,7 @@ Result<Vector<Color>, String> PaletteWidget::load_palette_path(String const& fil
{
auto file_or_error = Core::File::open(file_path, Core::OpenMode::ReadOnly);
if (file_or_error.is_error())
return String { file_or_error.error().string() };
return String { strerror(file_or_error.error().code()) };
auto& file = *file_or_error.value();
return load_palette_file(file);

View file

@ -20,6 +20,7 @@
#include <LibGUI/Widget.h>
#include <spawn.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

View file

@ -22,6 +22,7 @@
#include <LibGUI/TextBox.h>
#include <LibGUI/Wizards/WizardDialog.h>
#include <LibGUI/Wizards/WizardPage.h>
#include <string.h>
#include <unistd.h>
// This is defined in ImportDialog.cpp, we can't include it twice, since the generated symbol is exported.

View file

@ -10,6 +10,7 @@
#include "../XSV.h"
#include <AK/ByteBuffer.h>
#include <LibCore/File.h>
#include <string.h>
TEST_CASE(should_parse_valid_data)
{

View file

@ -51,7 +51,7 @@ Result<bool, String> Workbook::load(const StringView& filename)
sb.append("Failed to open ");
sb.append(filename);
sb.append(" for reading. Error: ");
sb.append(file_or_error.error().string());
sb.appendff("{}", file_or_error.error());
return sb.to_string();
}

View file

@ -27,11 +27,11 @@
namespace Core {
Result<NonnullRefPtr<File>, OSError> File::open(String filename, OpenMode mode, mode_t permissions)
ErrorOr<NonnullRefPtr<File>> File::open(String filename, OpenMode mode, mode_t permissions)
{
auto file = File::construct(move(filename));
if (!file->open_impl(mode, permissions))
return OSError(file->error());
return Error::from_errno(file->error());
return file;
}

View file

@ -7,8 +7,6 @@
#pragma once
#include <AK/Error.h>
#include <AK/OSError.h>
#include <AK/Result.h>
#include <AK/String.h>
#include <LibCore/IODevice.h>
#include <sys/stat.h>
@ -20,7 +18,7 @@ class File final : public IODevice {
public:
virtual ~File() override;
static Result<NonnullRefPtr<File>, OSError> open(String filename, OpenMode, mode_t = 0644);
static ErrorOr<NonnullRefPtr<File>> open(String filename, OpenMode, mode_t = 0644);
String filename() const { return m_filename; }
void set_filename(const String filename) { m_filename = move(filename); }

View file

@ -9,6 +9,7 @@
#include <AK/Buffered.h>
#include <AK/ByteBuffer.h>
#include <AK/Stream.h>
#include <AK/Try.h>
#include <LibCore/File.h>
namespace Core {
@ -20,28 +21,18 @@ public:
{
}
static Result<InputFileStream, OSError> open(StringView filename, OpenMode mode = OpenMode::ReadOnly, mode_t permissions = 0644)
static ErrorOr<InputFileStream> open(StringView filename, OpenMode mode = OpenMode::ReadOnly, mode_t permissions = 0644)
{
VERIFY(has_flag(mode, OpenMode::ReadOnly));
auto file_result = File::open(filename, mode, permissions);
if (file_result.is_error())
return file_result.error();
return InputFileStream { file_result.value() };
auto file = TRY(File::open(filename, mode, permissions));
return InputFileStream { move(file) };
}
static Result<Buffered<InputFileStream>, OSError> open_buffered(StringView filename, OpenMode mode = OpenMode::ReadOnly, mode_t permissions = 0644)
static ErrorOr<Buffered<InputFileStream>> open_buffered(StringView filename, OpenMode mode = OpenMode::ReadOnly, mode_t permissions = 0644)
{
VERIFY(has_flag(mode, OpenMode::ReadOnly));
auto file_result = File::open(filename, mode, permissions);
if (file_result.is_error())
return file_result.error();
return Buffered<InputFileStream> { file_result.value() };
auto file = TRY(File::open(filename, mode, permissions));
return Buffered<InputFileStream> { move(file) };
}
size_t read(Bytes bytes) override
@ -89,28 +80,18 @@ public:
{
}
static Result<OutputFileStream, OSError> open(StringView filename, OpenMode mode = OpenMode::WriteOnly, mode_t permissions = 0644)
static ErrorOr<OutputFileStream> open(StringView filename, OpenMode mode = OpenMode::WriteOnly, mode_t permissions = 0644)
{
VERIFY(has_flag(mode, OpenMode::WriteOnly));
auto file_result = File::open(filename, mode, permissions);
if (file_result.is_error())
return file_result.error();
return OutputFileStream { file_result.value() };
auto file = TRY(File::open(filename, mode, permissions));
return OutputFileStream { move(file) };
}
static Result<Buffered<OutputFileStream>, OSError> open_buffered(StringView filename, OpenMode mode = OpenMode::WriteOnly, mode_t permissions = 0644)
static ErrorOr<Buffered<OutputFileStream>> open_buffered(StringView filename, OpenMode mode = OpenMode::WriteOnly, mode_t permissions = 0644)
{
VERIFY(has_flag(mode, OpenMode::WriteOnly));
auto file_result = File::open(filename, mode, permissions);
if (file_result.is_error())
return file_result.error();
return Buffered<OutputFileStream> { file_result.value() };
auto file = TRY(File::open(filename, mode, permissions));
return Buffered<OutputFileStream> { move(file) };
}
static OutputFileStream standard_output()

View file

@ -16,6 +16,7 @@
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/PNGLoader.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

View file

@ -11,6 +11,7 @@
#include <LibCore/FileStream.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/FontStyleMapping.h>
#include <string.h>
namespace Gfx {

View file

@ -8,6 +8,7 @@
#include <AK/Debug.h>
#include <AK/HashMap.h>
#include <AK/Result.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/File.h>

View file

@ -177,9 +177,9 @@ void ResourceLoader::load(LoadRequest& request, Function<void(ReadonlyBytes, con
auto file_result = Core::File::open(url.path(), Core::OpenMode::ReadOnly);
if (file_result.is_error()) {
auto& error = file_result.error();
log_failure(request, error.string());
log_failure(request, error);
if (error_callback)
error_callback(error.string(), error.error());
error_callback(String::formatted("{}", error), error.code());
return;
}

View file

@ -34,7 +34,7 @@ static int perform_delete(Vector<String> const& sources);
static int execute_work_items(Vector<WorkItem> const& items);
static void report_error(String message);
static void report_warning(String message);
static AK::Result<AK::NonnullRefPtr<Core::File>, AK::OSError> open_destination_file(String const& destination);
static ErrorOr<NonnullRefPtr<Core::File>> open_destination_file(String const& destination);
static String deduplicate_destination_file_name(String const& destination);
int main(int argc, char** argv)
@ -379,10 +379,10 @@ int execute_work_items(Vector<WorkItem> const& items)
return 0;
}
AK::Result<AK::NonnullRefPtr<Core::File>, AK::OSError> open_destination_file(String const& destination)
ErrorOr<NonnullRefPtr<Core::File>> open_destination_file(String const& destination)
{
auto destination_file_or_error = Core::File::open(destination, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate | Core::OpenMode::MustBeNew));
if (destination_file_or_error.is_error() && destination_file_or_error.error().error() == EEXIST) {
if (destination_file_or_error.is_error() && destination_file_or_error.error().code() == EEXIST) {
return open_destination_file(deduplicate_destination_file_name(destination));
}
return destination_file_or_error;

View file

@ -9,6 +9,7 @@
#include <Services/WindowServer/ScreenLayout.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
// Must be included after LibIPC/Forward.h
#include <LibIPC/Decoder.h>

View file

@ -16,6 +16,7 @@
#include <net/if_arp.h>
#include <net/route.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>

View file

@ -8,6 +8,7 @@
#include <LibCore/File.h>
#include <LibCrypto/Checksum/Adler32.h>
#include <LibCrypto/Checksum/CRC32.h>
#include <string.h>
int main(int argc, char** argv)
{

View file

@ -5,7 +5,7 @@
*/
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <string.h>
int main(int argc, char** argv)
{

View file

@ -8,6 +8,7 @@
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <ctype.h>
#include <string.h>
static constexpr size_t LINE_LENGTH_BYTES = 16;

View file

@ -9,6 +9,7 @@
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

View file

@ -178,7 +178,7 @@ private:
auto file_name = m_input_file_chain.take_first();
auto file_or_error = Core::File::open(file_name, Core::OpenMode::ReadOnly);
if (file_or_error.is_error()) {
warnln("Input file {} could not be opened: {}", file_name, file_or_error.error().string());
warnln("Input file {} could not be opened: {}", file_name, file_or_error.error());
return {};
}
m_input_file = file_or_error.value();