From a19690170f879ee16df5dd910064b00b92f793ca Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 21 Apr 2020 13:29:36 +0200 Subject: LibCore: Make Core::File::open() return a Result, String> It was impractical to return a RefPtr since that left us no way to extract the error string. This is usually needed for the UI, so the old static open() got basically no use. --- Libraries/LibCore/File.cpp | 4 ++-- Libraries/LibCore/File.h | 3 ++- Shell/main.cpp | 9 +++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Libraries/LibCore/File.cpp b/Libraries/LibCore/File.cpp index 782960407c..41056701f0 100644 --- a/Libraries/LibCore/File.cpp +++ b/Libraries/LibCore/File.cpp @@ -33,11 +33,11 @@ namespace Core { -RefPtr File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions) +Result, String> File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions) { auto file = File::construct(filename); if (!file->open_impl(mode, permissions)) - return nullptr; + return String(file->error_string()); return file; } diff --git a/Libraries/LibCore/File.h b/Libraries/LibCore/File.h index aa94dc35cf..1e443debef 100644 --- a/Libraries/LibCore/File.h +++ b/Libraries/LibCore/File.h @@ -26,6 +26,7 @@ #pragma once +#include #include #include @@ -36,7 +37,7 @@ class File final : public IODevice { public: virtual ~File() override; - static RefPtr open(const String& filename, IODevice::OpenMode, mode_t = 0644); + static Result, String> open(const String& filename, IODevice::OpenMode, mode_t = 0644); String filename() const { return m_filename; } void set_filename(const StringView& filename) { m_filename = filename; } diff --git a/Shell/main.cpp b/Shell/main.cpp index 612a013a4a..cba616c0ca 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -993,12 +993,13 @@ void load_history() void save_history() { - auto history_file = Core::File::open(get_history_path(), Core::IODevice::WriteOnly, 0600); - if (!history_file) + auto file_or_error = Core::File::open(get_history_path(), Core::IODevice::WriteOnly, 0600); + if (file_or_error.is_error()) return; + auto& file = *file_or_error.value(); for (const auto& line : editor.history()) { - history_file->write(line); - history_file->write("\n"); + file.write(line); + file.write("\n"); } } -- cgit v1.2.3