BinEditor: Set error string more systematically when saving

Change-Id: I95375348fb6ab44d35c30e4484efb0025f9b108f
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
hjk
2024-09-20 09:18:47 +02:00
parent e772fbab6b
commit 914b7fab4f

View File

@@ -157,7 +157,7 @@ public:
void addData(quint64 addr, const QByteArray &data); void addData(quint64 addr, const QByteArray &data);
void updateContents(); void updateContents();
bool save(QString *errorString, const FilePath &oldFilePath, const FilePath &newFilePath); expected_str<void> save(const FilePath &oldFilePath, const FilePath &newFilePath);
void clear(); void clear();
void undo(); void undo();
@@ -615,7 +615,7 @@ void BinEditorDocument::setModified(bool modified)
emit changed(); emit changed();
} }
bool BinEditorDocument::save(QString *errorString, const FilePath &oldFilePath, const FilePath &newFilePath) expected_str<void> BinEditorDocument::save(const FilePath &oldFilePath, const FilePath &newFilePath)
{ {
if (oldFilePath != newFilePath) { if (oldFilePath != newFilePath) {
// Get a unique temporary file name // Get a unique temporary file name
@@ -624,15 +624,20 @@ bool BinEditorDocument::save(QString *errorString, const FilePath &oldFilePath,
const auto result = TemporaryFilePath::create( const auto result = TemporaryFilePath::create(
newFilePath.stringAppended("_XXXXXX.new")); newFilePath.stringAppended("_XXXXXX.new"));
if (!result) if (!result)
return false; return make_unexpected(result.error());
tmpName = (*result)->filePath(); tmpName = (*result)->filePath();
} }
if (!oldFilePath.copyFile(tmpName))
return false; if (expected_str<void> res = oldFilePath.copyFile(tmpName); !res)
if (newFilePath.exists() && !newFilePath.removeFile()) return res;
return false;
if (!tmpName.renameFile(newFilePath)) if (newFilePath.exists()) {
return false; if (expected_str<void> res = newFilePath.removeFile(); !res)
return res;
}
if (expected_str<void> res = tmpName.renameFile(newFilePath); !res)
return res;
} }
FileSaver saver(newFilePath, QIODevice::ReadWrite); // QtBug: WriteOnly truncates. FileSaver saver(newFilePath, QIODevice::ReadWrite); // QtBug: WriteOnly truncates.
@@ -654,11 +659,13 @@ bool BinEditorDocument::save(QString *errorString, const FilePath &oldFilePath,
if (!saver.hasError()) if (!saver.hasError())
saver.setResult(output->resize(size)); saver.setResult(output->resize(size));
} }
if (!saver.finalize(errorString))
return false; QString errorString;
if (!saver.finalize(&errorString))
return make_unexpected(errorString);
setModified(false); setModified(false);
return true; return {};
} }
void BinEditorDocument::setSizes(quint64 startAddr, qint64 range, int blockSize) void BinEditorDocument::setSizes(quint64 startAddr, qint64 range, int blockSize)
@@ -2190,11 +2197,13 @@ bool BinEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType
bool BinEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave) bool BinEditorDocument::saveImpl(QString *errorString, const FilePath &filePath, bool autoSave)
{ {
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
if (save(errorString, this->filePath(), filePath)) { if (expected_str<void> res = save(this->filePath(), filePath); !res) {
setFilePath(filePath); if (errorString)
return true; *errorString = res.error();
return false;
} }
return false; setFilePath(filePath);
return true;
} }
class BinEditorImpl final : public IEditor, public EditorService class BinEditorImpl final : public IEditor, public EditorService