HexEditor: Use FileSystemAccessClient, add unveils

Most of the code here is based off the implementation of how
TextEditor uses FileSystemAccessClient.
This commit is contained in:
Mustafa Quraish 2021-09-02 07:03:44 -04:00 committed by Andreas Kling
parent eae21c7e31
commit 8f2521ce52
Notes: sideshowbarker 2024-07-18 04:48:32 +09:00
6 changed files with 109 additions and 31 deletions

View file

@ -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)

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,10 +8,12 @@
#include "HexEditor.h"
#include "SearchResultsModel.h"
#include <AK/Debug.h>
#include <AK/ScopeGuard.h>
#include <AK/StringBuilder.h>
#include <LibGUI/Action.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Scrollbar.h>
#include <LibGUI/TextEditor.h>
@ -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;
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
*
* 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()); }

View file

@ -14,6 +14,7 @@
#include <Applications/HexEditor/HexEditorWindowGML.h>
#include <LibConfig/Client.h>
#include <LibCore/File.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/Action.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
@ -84,10 +85,13 @@ HexEditorWidget::HexEditorWidget()
});
m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
Optional<String> 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<String> 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());
}
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
*
* 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();

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -8,6 +9,7 @@
#include <LibConfig/Client.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/MessageBox.h>
#include <stdio.h>
#include <unistd.h>
@ -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();
}