From 4942ea633b40809f285e6bbc45d0692207aa7686 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 22 Feb 2024 14:39:37 +0100 Subject: Qt Designer: Potential fix for errors running several instances on Windows Qt Designer used an additional tmp directory under the backup directory in which it wrote the backup files first and then copied them over apparently in order to make it somewhat atomic operation. Replace the mechanism by QSaveFile which should achieve the same. Task-number: QTBUG-122616 Change-Id: If90e6b95431648adabc75c05676f45ac15703113 Reviewed-by: Jarek Kobus (cherry picked from commit 222dfe93a403427aa7e6b14b8f0558331d0215a7) Reviewed-by: Qt Cherry-pick Bot --- src/designer/src/designer/qdesigner_actions.cpp | 52 +++++++------------------ 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/src/designer/src/designer/qdesigner_actions.cpp b/src/designer/src/designer/qdesigner_actions.cpp index c5841559b..e19573aa4 100644 --- a/src/designer/src/designer/qdesigner_actions.cpp +++ b/src/designer/src/designer/qdesigner_actions.cpp @@ -1047,7 +1047,6 @@ void QDesignerActions::backupForms() return; - QStringList tmpFiles; QMap backupMap; QDir backupDir(m_backupPath); for (int i = 0; i < count; ++i) { @@ -1062,36 +1061,22 @@ void QDesignerActions::backupForms() backupMap.insert(fwn, formBackupName); - QFile file(formBackupName.replace(m_backupPath, m_backupTmpPath)); - if (file.open(QFile::WriteOnly)){ - const QByteArray utf8Array = formWindowContents(fw->editor(), backupDir); - if (file.write(utf8Array, utf8Array.size()) != utf8Array.size()) { - backupMap.remove(fwn); - qdesigner_internal::designerWarning(tr("The backup file %1 could not be written: %2"). - arg(QDir::toNativeSeparators(file.fileName()), - file.errorString())); - } else - tmpFiles.append(formBackupName); - - file.close(); + bool ok = false; + QSaveFile file(formBackupName); + if (file.open(QFile::WriteOnly)) { + file.write(formWindowContents(fw->editor(), backupDir)); + ok = file.commit(); } - } - if(!tmpFiles.isEmpty()) { - const QStringList backupFiles = backupDir.entryList(QDir::Files); - for (const QString &backupFile : backupFiles) - backupDir.remove(backupFile); - - for (const QString &tmpName : std::as_const(tmpFiles)) { - QString name(tmpName); - name.replace(m_backupTmpPath, m_backupPath); - QFile tmpFile(tmpName); - if (!tmpFile.copy(name)) - qdesigner_internal::designerWarning(tr("The backup file %1 could not be written.").arg(name)); - tmpFile.remove(); + if (!ok) { + backupMap.remove(fwn); + qdesigner_internal::designerWarning(tr("The backup file %1 could not be written: %2"). + arg(QDir::toNativeSeparators(file.fileName()), + file.errorString())); } + } + if (!backupMap.isEmpty()) m_settings.setBackup(backupMap); - } } static QString fixResourceFileBackupPath(const QDesignerFormWindowInterface *fwi, @@ -1173,15 +1158,11 @@ void QDesignerActions::setWindowListSeparatorVisible(bool visible) bool QDesignerActions::ensureBackupDirectories() { - if (m_backupPath.isEmpty()) { - // create names + if (m_backupPath.isEmpty()) // create names m_backupPath = qdesigner_internal::dataDirectory() + u"/backup"_s; - m_backupTmpPath = m_backupPath + u"/tmp"_s; - } // ensure directories const QDir backupDir(m_backupPath); - const QDir backupTmpDir(m_backupTmpPath); if (!backupDir.exists()) { if (!backupDir.mkpath(m_backupPath)) { @@ -1190,13 +1171,6 @@ bool QDesignerActions::ensureBackupDirectories() { return false; } } - if (!backupTmpDir.exists()) { - if (!backupTmpDir.mkpath(m_backupTmpPath)) { - qdesigner_internal::designerWarning(tr("The temporary backup directory %1 could not be created.") - .arg(QDir::toNativeSeparators(m_backupTmpPath))); - return false; - } - } return true; } -- cgit v1.2.3