From 5feca3da725e33b70cf8f27b7e923c727224c11e Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 10 Dec 2024 14:02:06 +0100 Subject: [PATCH] macdeployqt: port to QSaveFile ... to avoid loosing file content in case of write errors. Change-Id: If3f7af1e7260e3b9c3df201ed869988b0bd6df2a Reviewed-by: Volker Hilsheimer Reviewed-by: Joerg Bornemann --- src/tools/macdeployqt/shared/shared.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/tools/macdeployqt/shared/shared.cpp b/src/tools/macdeployqt/shared/shared.cpp index 19137d15944..fedc8bfd092 100644 --- a/src/tools/macdeployqt/shared/shared.cpp +++ b/src/tools/macdeployqt/shared/shared.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "shared.h" #ifdef Q_OS_DARWIN @@ -134,18 +135,21 @@ void patch_debugInInfoPlist(const QString &infoPlistPath) { // Older versions of qmake may have the "_debug" binary as // the value for CFBundleExecutable. Remove it. - QFile infoPlist(infoPlistPath); - if (infoPlist.open(QIODevice::ReadOnly)) { + if (QFile infoPlist(infoPlistPath); infoPlist.open(QIODevice::ReadOnly)) { QByteArray contents = infoPlist.readAll(); infoPlist.close(); - if (infoPlist.open(QIODevice::WriteOnly | QIODevice::Truncate)) { + QSaveFile writableInfoPlist(infoPlistPath); + bool success = writableInfoPlist.open(QIODevice::WriteOnly | QIODevice::Truncate); + if (success) { contents.replace("_debug", ""); // surely there are no legit uses of "_debug" in an Info.plist - infoPlist.write(contents); - } else { - LogError() << "failed to write Info.plist file" << infoPlistPath; + writableInfoPlist.write(contents); + success = writableInfoPlist.commit(); + } + if (!success) { + LogError() << "Failed to write Info.plist file" << infoPlistPath; } } else { - LogError() << "failed to read Info.plist file" << infoPlistPath; + LogError() << "Failed to read Info.plist file" << infoPlistPath; } } @@ -1228,8 +1232,7 @@ void createQtConf(const QString &appBundlePath) QDir().mkpath(filePath); - QFile qtconf(fileName); - if (qtconf.exists() && !alwaysOwerwriteEnabled) { + if (QFile::exists(fileName) && !alwaysOwerwriteEnabled) { LogWarning(); LogWarning() << fileName << "already exists, will not overwrite."; LogWarning() << "To make sure the plugins are loaded from the correct location,"; @@ -1239,7 +1242,8 @@ void createQtConf(const QString &appBundlePath) return; } - if (qtconf.open(QIODevice::WriteOnly) && qtconf.write(contents) != -1) { + if (QSaveFile qtconf(fileName); qtconf.open(QIODevice::WriteOnly) + && qtconf.write(contents) != -1 && qtconf.commit()) { LogNormal() << "Created configuration file:" << fileName; LogNormal() << "This file sets the plugin search path to" << appBundlePath + "/Contents/PlugIns"; }