HexEditor: Propagate errors when using "Save as"

This commit is contained in:
Lucas CHOLLET 2023-01-14 22:04:20 -05:00 committed by Andrew Kaster
parent 3d914247cc
commit 2bba743c24
Notes: sideshowbarker 2024-07-17 07:43:05 +09:00
5 changed files with 25 additions and 31 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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;
}