mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
HexEditor: Propagate errors when using "Save as"
This commit is contained in:
parent
3d914247cc
commit
2bba743c24
Notes:
sideshowbarker
2024-07-17 07:43:05 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/2bba743c24 Pull-request: https://github.com/SerenityOS/serenity/pull/17045 Reviewed-by: https://github.com/ADKaster ✅
5 changed files with 25 additions and 31 deletions
|
@ -58,17 +58,15 @@ void HexDocumentMemory::clear_changes()
|
|||
m_changes.clear();
|
||||
}
|
||||
|
||||
bool HexDocumentMemory::write_to_file(Core::Stream::File& file)
|
||||
ErrorOr<void> HexDocumentMemory::write_to_file(Core::Stream::File& file)
|
||||
{
|
||||
if (file.seek(0, SeekMode::SetPosition).is_error())
|
||||
return false;
|
||||
if (file.write(m_buffer).is_error())
|
||||
return false;
|
||||
TRY(file.seek(0, SeekMode::SetPosition));
|
||||
TRY(file.write(m_buffer));
|
||||
for (auto& change : m_changes) {
|
||||
file.seek(change.key, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
|
||||
file.write({ &change.value, 1 }).release_value_but_fixme_should_propagate_errors();
|
||||
TRY(file.seek(change.key, SeekMode::SetPosition));
|
||||
TRY(file.write({ &change.value, 1 }));
|
||||
}
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<HexDocumentFile>> HexDocumentFile::create(NonnullOwnPtr<Core::Stream::File> file)
|
||||
|
@ -96,30 +94,27 @@ void HexDocumentFile::write_to_file()
|
|||
m_buffer_file_pos = m_file_size + 1;
|
||||
}
|
||||
|
||||
bool HexDocumentFile::write_to_file(Core::Stream::File& file)
|
||||
ErrorOr<void> HexDocumentFile::write_to_file(Core::Stream::File& file)
|
||||
{
|
||||
if (file.truncate(size()).is_error()) {
|
||||
return false;
|
||||
}
|
||||
TRY(file.truncate(size()));
|
||||
|
||||
if (file.seek(0, SeekMode::SetPosition).is_error() || m_file->seek(0, SeekMode::SetPosition).is_error()) {
|
||||
return false;
|
||||
}
|
||||
TRY(file.seek(0, SeekMode::SetPosition));
|
||||
TRY(m_file->seek(0, SeekMode::SetPosition));
|
||||
|
||||
while (true) {
|
||||
Array<u8, 64 * KiB> buffer;
|
||||
auto copy_buffer = m_file->read(buffer).release_value_but_fixme_should_propagate_errors();
|
||||
auto copy_buffer = TRY(m_file->read(buffer));
|
||||
if (copy_buffer.size() == 0)
|
||||
break;
|
||||
file.write(copy_buffer).release_value_but_fixme_should_propagate_errors();
|
||||
TRY(file.write(copy_buffer));
|
||||
}
|
||||
|
||||
for (auto& change : m_changes) {
|
||||
file.seek(change.key, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
|
||||
file.write({ &change.value, 1 }).release_value_but_fixme_should_propagate_errors();
|
||||
TRY(file.seek(change.key, SeekMode::SetPosition));
|
||||
TRY(file.write({ &change.value, 1 }));
|
||||
}
|
||||
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
|
||||
HexDocument::Cell HexDocumentFile::get(size_t position)
|
||||
|
|
|
@ -50,7 +50,7 @@ public:
|
|||
size_t size() const override;
|
||||
Type type() const override;
|
||||
void clear_changes() override;
|
||||
bool write_to_file(Core::Stream::File& file);
|
||||
ErrorOr<void> write_to_file(Core::Stream::File& file);
|
||||
|
||||
private:
|
||||
ByteBuffer m_buffer;
|
||||
|
@ -67,7 +67,7 @@ public:
|
|||
void set_file(NonnullOwnPtr<Core::Stream::File> file);
|
||||
NonnullOwnPtr<Core::Stream::File> const& file() const;
|
||||
void write_to_file();
|
||||
bool write_to_file(Core::Stream::File& file);
|
||||
ErrorOr<void> write_to_file(Core::Stream::File& file);
|
||||
Cell get(size_t position) override;
|
||||
u8 get_unchanged(size_t position) override;
|
||||
size_t size() const override;
|
||||
|
|
|
@ -135,23 +135,22 @@ void HexEditor::set_selection(size_t position, size_t length)
|
|||
scroll_position_into_view(position);
|
||||
update_status();
|
||||
}
|
||||
bool HexEditor::save_as(NonnullOwnPtr<Core::Stream::File> new_file)
|
||||
|
||||
ErrorOr<void> HexEditor::save_as(NonnullOwnPtr<Core::Stream::File> new_file)
|
||||
{
|
||||
if (m_document->type() == HexDocument::Type::File) {
|
||||
auto& file_document = static_cast<HexDocumentFile&>(*m_document);
|
||||
if (!file_document.write_to_file(*new_file))
|
||||
return false;
|
||||
TRY(file_document.write_to_file(*new_file));
|
||||
file_document.set_file(move(new_file));
|
||||
} else {
|
||||
auto& memory_document = static_cast<HexDocumentMemory&>(*m_document);
|
||||
if (!memory_document.write_to_file(*new_file))
|
||||
return false;
|
||||
TRY(memory_document.write_to_file(*new_file));
|
||||
m_document = HexDocumentFile::create(move(new_file)).release_value_but_fixme_should_propagate_errors();
|
||||
}
|
||||
|
||||
update();
|
||||
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
|
||||
bool HexEditor::save()
|
||||
|
|
|
@ -38,7 +38,7 @@ public:
|
|||
void open_file(NonnullOwnPtr<Core::Stream::File> file);
|
||||
ErrorOr<void> fill_selection(u8 fill_byte);
|
||||
Optional<u8> get_byte(size_t position);
|
||||
bool save_as(NonnullOwnPtr<Core::Stream::File>);
|
||||
ErrorOr<void> save_as(NonnullOwnPtr<Core::Stream::File>);
|
||||
bool save();
|
||||
|
||||
bool undo();
|
||||
|
|
|
@ -146,8 +146,8 @@ HexEditorWidget::HexEditorWidget()
|
|||
if (response.is_error())
|
||||
return;
|
||||
auto file = response.release_value();
|
||||
if (!m_editor->save_as(file.release_stream())) {
|
||||
GUI::MessageBox::show(window(), "Unable to save file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error);
|
||||
if (auto result = m_editor->save_as(file.release_stream()); result.is_error()) {
|
||||
GUI::MessageBox::show(window(), DeprecatedString::formatted("Unable to save file: {}\n"sv, result.error()), "Error"sv, GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue