mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:37:45 +00:00
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:
parent
eae21c7e31
commit
8f2521ce52
6 changed files with 109 additions and 31 deletions
|
@ -20,4 +20,4 @@ set(SOURCES
|
||||||
)
|
)
|
||||||
|
|
||||||
serenity_app(HexEditor ICON app-hex-editor)
|
serenity_app(HexEditor ICON app-hex-editor)
|
||||||
target_link_libraries(HexEditor LibGUI LibConfig)
|
target_link_libraries(HexEditor LibGUI LibConfig LibFileSystemAccessClient)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -7,10 +8,12 @@
|
||||||
#include "HexEditor.h"
|
#include "HexEditor.h"
|
||||||
#include "SearchResultsModel.h"
|
#include "SearchResultsModel.h"
|
||||||
#include <AK/Debug.h>
|
#include <AK/Debug.h>
|
||||||
|
#include <AK/ScopeGuard.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibGUI/Action.h>
|
#include <LibGUI/Action.h>
|
||||||
#include <LibGUI/Clipboard.h>
|
#include <LibGUI/Clipboard.h>
|
||||||
#include <LibGUI/Menu.h>
|
#include <LibGUI/Menu.h>
|
||||||
|
#include <LibGUI/MessageBox.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGUI/Scrollbar.h>
|
#include <LibGUI/Scrollbar.h>
|
||||||
#include <LibGUI/TextEditor.h>
|
#include <LibGUI/TextEditor.h>
|
||||||
|
@ -91,6 +94,13 @@ bool HexEditor::write_to_file(const String& path)
|
||||||
return false;
|
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());
|
int rc = ftruncate(fd, m_buffer.size());
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
perror("ftruncate");
|
perror("ftruncate");
|
||||||
|
@ -109,7 +119,6 @@ bool HexEditor::write_to_file(const String& path)
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
close(fd);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -34,6 +35,7 @@ public:
|
||||||
void set_buffer(const ByteBuffer&);
|
void set_buffer(const ByteBuffer&);
|
||||||
void fill_selection(u8 fill_byte);
|
void fill_selection(u8 fill_byte);
|
||||||
bool write_to_file(const String& path);
|
bool write_to_file(const String& path);
|
||||||
|
bool write_to_file(int fd);
|
||||||
|
|
||||||
void select_all();
|
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()); }
|
bool has_selection() const { return !(m_selection_start == -1 || m_selection_end == -1 || (m_selection_end - m_selection_start) < 0 || m_buffer.is_empty()); }
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <Applications/HexEditor/HexEditorWindowGML.h>
|
#include <Applications/HexEditor/HexEditorWindowGML.h>
|
||||||
#include <LibConfig/Client.h>
|
#include <LibConfig/Client.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
|
#include <LibFileSystemAccessClient/Client.h>
|
||||||
#include <LibGUI/Action.h>
|
#include <LibGUI/Action.h>
|
||||||
#include <LibGUI/BoxLayout.h>
|
#include <LibGUI/BoxLayout.h>
|
||||||
#include <LibGUI/Button.h>
|
#include <LibGUI/Button.h>
|
||||||
|
@ -84,10 +85,13 @@ HexEditorWidget::HexEditorWidget()
|
||||||
});
|
});
|
||||||
|
|
||||||
m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
|
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;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (m_document_dirty) {
|
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);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
open_file(open_path.value());
|
open_file(*response.fd, *response.chosen_file);
|
||||||
});
|
});
|
||||||
|
|
||||||
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
|
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
|
||||||
if (!m_path.is_empty()) {
|
if (m_path.is_empty())
|
||||||
if (!m_editor->write_to_file(m_path)) {
|
return m_save_as_action->activate();
|
||||||
GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
|
||||||
} else {
|
auto response = FileSystemAccessClient::Client::the().request_file(window()->window_id(), m_path, Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
|
||||||
m_document_dirty = false;
|
|
||||||
update_title();
|
if (response.error != 0) {
|
||||||
}
|
if (response.error != -1)
|
||||||
|
GUI::MessageBox::show_error(window(), String::formatted("Unable to save file: {}", strerror(response.error)));
|
||||||
return;
|
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&) {
|
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);
|
auto response = FileSystemAccessClient::Client::the().save_file(window()->window_id(), m_name, m_extension);
|
||||||
if (!save_path.has_value()) {
|
|
||||||
dbgln("GUI::FilePicker: Cancel button clicked");
|
if (response.error != 0) {
|
||||||
|
if (response.error != -1)
|
||||||
|
GUI::MessageBox::show_error(window(), String::formatted("Saving \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
|
||||||
return;
|
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);
|
GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_document_dirty = false;
|
m_document_dirty = false;
|
||||||
set_path(save_path.value());
|
set_path(*response.chosen_file);
|
||||||
dbgln("Wrote document to {}", save_path.value());
|
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&) {
|
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());
|
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);
|
VERIFY(path.starts_with("/"sv));
|
||||||
if (!file->open(Core::OpenMode::ReadOnly)) {
|
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);
|
GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
|
||||||
return;
|
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_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.
|
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);
|
set_path(path);
|
||||||
|
@ -377,6 +402,13 @@ void HexEditorWidget::drop_event(GUI::DropEvent& event)
|
||||||
if (urls.is_empty())
|
if (urls.is_empty())
|
||||||
return;
|
return;
|
||||||
window()->move_to_front();
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -21,7 +22,7 @@ class HexEditorWidget final : public GUI::Widget {
|
||||||
C_OBJECT(HexEditorWidget)
|
C_OBJECT(HexEditorWidget)
|
||||||
public:
|
public:
|
||||||
virtual ~HexEditorWidget() override;
|
virtual ~HexEditorWidget() override;
|
||||||
void open_file(const String& path);
|
void open_file(int fd, String const& path);
|
||||||
void initialize_menubar(GUI::Window&);
|
void initialize_menubar(GUI::Window&);
|
||||||
bool request_close();
|
bool request_close();
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -8,6 +9,7 @@
|
||||||
#include <LibConfig/Client.h>
|
#include <LibConfig/Client.h>
|
||||||
#include <LibGUI/Icon.h>
|
#include <LibGUI/Icon.h>
|
||||||
#include <LibGUI/Menubar.h>
|
#include <LibGUI/Menubar.h>
|
||||||
|
#include <LibGUI/MessageBox.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
@ -22,11 +24,6 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
Config::pledge_domains("HexEditor");
|
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 app_icon = GUI::Icon::default_icon("app-hex-editor");
|
||||||
|
|
||||||
auto window = GUI::Window::construct();
|
auto window = GUI::Window::construct();
|
||||||
|
@ -41,12 +38,49 @@ int main(int argc, char** argv)
|
||||||
return GUI::Window::CloseRequestDecision::StayOpen;
|
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);
|
hex_editor_widget.initialize_menubar(*window);
|
||||||
window->show();
|
window->show();
|
||||||
window->set_icon(app_icon.bitmap_for_size(16));
|
window->set_icon(app_icon.bitmap_for_size(16));
|
||||||
|
|
||||||
if (argc >= 2)
|
if (!file_to_edit.is_empty()) {
|
||||||
hex_editor_widget.open_file(argv[1]);
|
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();
|
return app->exec();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue