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 updateContents();
bool save(QString *errorString, const FilePath &oldFilePath, const FilePath &newFilePath);
expected_str<void> save(const FilePath &oldFilePath, const FilePath &newFilePath);
void clear();
void undo();
@@ -615,7 +615,7 @@ void BinEditorDocument::setModified(bool modified)
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) {
// Get a unique temporary file name
@@ -624,15 +624,20 @@ bool BinEditorDocument::save(QString *errorString, const FilePath &oldFilePath,
const auto result = TemporaryFilePath::create(
newFilePath.stringAppended("_XXXXXX.new"));
if (!result)
return false;
return make_unexpected(result.error());
tmpName = (*result)->filePath();
}
if (!oldFilePath.copyFile(tmpName))
return false;
if (newFilePath.exists() && !newFilePath.removeFile())
return false;
if (!tmpName.renameFile(newFilePath))
return false;
if (expected_str<void> res = oldFilePath.copyFile(tmpName); !res)
return res;
if (newFilePath.exists()) {
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.
@@ -654,11 +659,13 @@ bool BinEditorDocument::save(QString *errorString, const FilePath &oldFilePath,
if (!saver.hasError())
saver.setResult(output->resize(size));
}
if (!saver.finalize(errorString))
return false;
QString errorString;
if (!saver.finalize(&errorString))
return make_unexpected(errorString);
setModified(false);
return true;
return {};
}
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)
{
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) {
if (errorString)
*errorString = res.error();
return false;
}
setFilePath(filePath);
return true;
}
return false;
}
class BinEditorImpl final : public IEditor, public EditorService