From 8f2521ce52abe384b67ee2693aeb1f3816dc2148 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Thu, 2 Sep 2021 07:03:44 -0400 Subject: [PATCH] HexEditor: Use FileSystemAccessClient, add unveils Most of the code here is based off the implementation of how TextEditor uses FileSystemAccessClient. --- .../Applications/HexEditor/CMakeLists.txt | 2 +- Userland/Applications/HexEditor/HexEditor.cpp | 11 ++- Userland/Applications/HexEditor/HexEditor.h | 2 + .../HexEditor/HexEditorWidget.cpp | 74 +++++++++++++------ .../Applications/HexEditor/HexEditorWidget.h | 3 +- Userland/Applications/HexEditor/main.cpp | 48 ++++++++++-- 6 files changed, 109 insertions(+), 31 deletions(-) diff --git a/Userland/Applications/HexEditor/CMakeLists.txt b/Userland/Applications/HexEditor/CMakeLists.txt index 8de66fdfea..19414e8bdb 100644 --- a/Userland/Applications/HexEditor/CMakeLists.txt +++ b/Userland/Applications/HexEditor/CMakeLists.txt @@ -20,4 +20,4 @@ set(SOURCES ) serenity_app(HexEditor ICON app-hex-editor) -target_link_libraries(HexEditor LibGUI LibConfig) +target_link_libraries(HexEditor LibGUI LibConfig LibFileSystemAccessClient) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 1fdf40c982..10ac7a2b49 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Mustafa Quraish * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,10 +8,12 @@ #include "HexEditor.h" #include "SearchResultsModel.h" #include +#include #include #include #include #include +#include #include #include #include @@ -91,6 +94,13 @@ bool HexEditor::write_to_file(const String& path) return false; } + return write_to_file(fd); +} + +bool HexEditor::write_to_file(int fd) +{ + ScopeGuard fd_guard = [fd] { close(fd); }; + int rc = ftruncate(fd, m_buffer.size()); if (rc < 0) { perror("ftruncate"); @@ -109,7 +119,6 @@ bool HexEditor::write_to_file(const String& path) update(); } - close(fd); return true; } diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index 9dbcfe7379..7c534e790d 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Mustafa Quraish * * SPDX-License-Identifier: BSD-2-Clause */ @@ -34,6 +35,7 @@ public: void set_buffer(const ByteBuffer&); void fill_selection(u8 fill_byte); bool write_to_file(const String& path); + bool write_to_file(int fd); void select_all(); bool has_selection() const { return !(m_selection_start == -1 || m_selection_end == -1 || (m_selection_end - m_selection_start) < 0 || m_buffer.is_empty()); } diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index 0e76e78223..160980e68c 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -84,10 +85,13 @@ HexEditorWidget::HexEditorWidget() }); m_open_action = GUI::CommonActions::make_open_action([this](auto&) { - Optional open_path = GUI::FilePicker::get_open_filepath(window()); + auto response = FileSystemAccessClient::Client::the().open_file(window()->window_id()); - if (!open_path.has_value()) + if (response.error != 0) { + if (response.error != -1) + GUI::MessageBox::show_error(window(), String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error))); return; + } if (m_document_dirty) { auto save_document_first_result = GUI::MessageBox::show(window(), "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel); @@ -97,38 +101,47 @@ HexEditorWidget::HexEditorWidget() return; } - open_file(open_path.value()); + open_file(*response.fd, *response.chosen_file); }); m_save_action = GUI::CommonActions::make_save_action([&](auto&) { - if (!m_path.is_empty()) { - if (!m_editor->write_to_file(m_path)) { - GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error); - } else { - m_document_dirty = false; - update_title(); - } + if (m_path.is_empty()) + return m_save_as_action->activate(); + + auto response = FileSystemAccessClient::Client::the().request_file(window()->window_id(), m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly); + + if (response.error != 0) { + if (response.error != -1) + GUI::MessageBox::show_error(window(), String::formatted("Unable to save file: {}", strerror(response.error))); return; } - m_save_as_action->activate(); + if (!m_editor->write_to_file(*response.fd)) { + GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error); + } else { + m_document_dirty = false; + update_title(); + } + return; }); m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { - Optional save_path = GUI::FilePicker::get_save_filepath(window(), m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "bin" : m_extension); - if (!save_path.has_value()) { - dbgln("GUI::FilePicker: Cancel button clicked"); + auto response = FileSystemAccessClient::Client::the().save_file(window()->window_id(), m_name, m_extension); + + if (response.error != 0) { + if (response.error != -1) + GUI::MessageBox::show_error(window(), String::formatted("Saving \"{}\" failed: {}", *response.chosen_file, strerror(response.error))); return; } - if (!m_editor->write_to_file(save_path.value())) { + if (!m_editor->write_to_file(*response.fd)) { GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error); return; } m_document_dirty = false; - set_path(save_path.value()); - dbgln("Wrote document to {}", save_path.value()); + set_path(*response.chosen_file); + dbgln("Wrote document to {}", *response.chosen_file); }); m_find_action = GUI::Action::create("&Find", { Mod_Ctrl, Key_F }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png"), [&](const GUI::Action&) { @@ -334,14 +347,26 @@ void HexEditorWidget::update_title() window()->set_title(builder.to_string()); } -void HexEditorWidget::open_file(const String& path) +void HexEditorWidget::open_file(int fd, String const& path) { - auto file = Core::File::construct(path); - if (!file->open(Core::OpenMode::ReadOnly)) { + VERIFY(path.starts_with("/"sv)); + auto file = Core::File::construct(); + + if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes) && file->error() != ENOENT) { GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error); return; } + if (file->is_device()) { + GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open device files", path), "Error", GUI::MessageBox::Type::Error); + return; + } + + if (file->is_directory()) { + GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open directories", path), "Error", GUI::MessageBox::Type::Error); + return; + } + m_document_dirty = false; m_editor->set_buffer(file->read_all()); // FIXME: On really huge files, this is never going to work. Should really create a framework to fetch data from the file on-demand. set_path(path); @@ -377,6 +402,13 @@ void HexEditorWidget::drop_event(GUI::DropEvent& event) if (urls.is_empty()) return; window()->move_to_front(); - open_file(urls.first().path()); + + // TODO: A drop event should be considered user consent for opening a file + auto file_response = FileSystemAccessClient::Client::the().request_file(window()->window_id(), urls.first().path(), Core::OpenMode::ReadOnly); + + if (file_response.error != 0) + return; + + open_file(*file_response.fd, urls.first().path()); } } diff --git a/Userland/Applications/HexEditor/HexEditorWidget.h b/Userland/Applications/HexEditor/HexEditorWidget.h index 28e338df30..150a8ce46f 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.h +++ b/Userland/Applications/HexEditor/HexEditorWidget.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Mustafa Quraish * * SPDX-License-Identifier: BSD-2-Clause */ @@ -21,7 +22,7 @@ class HexEditorWidget final : public GUI::Widget { C_OBJECT(HexEditorWidget) public: virtual ~HexEditorWidget() override; - void open_file(const String& path); + void open_file(int fd, String const& path); void initialize_menubar(GUI::Window&); bool request_close(); diff --git a/Userland/Applications/HexEditor/main.cpp b/Userland/Applications/HexEditor/main.cpp index 46d0074023..a6b645b742 100644 --- a/Userland/Applications/HexEditor/main.cpp +++ b/Userland/Applications/HexEditor/main.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Mustafa Quraish * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,6 +9,7 @@ #include #include #include +#include #include #include @@ -22,11 +24,6 @@ int main(int argc, char** argv) Config::pledge_domains("HexEditor"); - if (pledge("stdio recvfd sendfd rpath cpath wpath thread", nullptr) < 0) { - perror("pledge"); - return 1; - } - auto app_icon = GUI::Icon::default_icon("app-hex-editor"); auto window = GUI::Window::construct(); @@ -41,12 +38,49 @@ int main(int argc, char** argv) return GUI::Window::CloseRequestDecision::StayOpen; }; + String file_to_edit = (argc > 1) ? Core::File::absolute_path(argv[1]) : ""; + + if (!file_to_edit.is_empty()) { + if (Core::File::exists(file_to_edit)) { + dbgln("unveil for: {}", file_to_edit); + if (unveil(file_to_edit.characters(), "r") < 0) { + perror("unveil"); + return 1; + } + } else { + file_to_edit = {}; + } + } + + if (unveil("/res", "r") < 0) { + perror("unveil"); + return 1; + } + + if (unveil("/tmp/portal/filesystemaccess", "rw") < 0) { + perror("unveil"); + return 1; + } + + if (unveil(nullptr, nullptr) < 0) { + perror("unveil"); + return 1; + } + hex_editor_widget.initialize_menubar(*window); window->show(); window->set_icon(app_icon.bitmap_for_size(16)); - if (argc >= 2) - hex_editor_widget.open_file(argv[1]); + if (!file_to_edit.is_empty()) { + auto file = Core::File::open(file_to_edit, Core::OpenMode::ReadOnly); + + if (file.is_error()) { + GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", file_to_edit, file.error())); + return 1; + } + + hex_editor_widget.open_file(file.value()->leak_fd(), file_to_edit); + } return app->exec(); }