macdeployqt: port to QSaveFile

... to avoid loosing file content in case of write errors.

Change-Id: If3f7af1e7260e3b9c3df201ed869988b0bd6df2a
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Ivan Solovev 2024-12-10 14:02:06 +01:00
parent 5525779b2e
commit 5feca3da72

View File

@ -19,6 +19,7 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonValue> #include <QJsonValue>
#include <QRegularExpression> #include <QRegularExpression>
#include <QSaveFile>
#include "shared.h" #include "shared.h"
#ifdef Q_OS_DARWIN #ifdef Q_OS_DARWIN
@ -134,18 +135,21 @@ void patch_debugInInfoPlist(const QString &infoPlistPath)
{ {
// Older versions of qmake may have the "_debug" binary as // Older versions of qmake may have the "_debug" binary as
// the value for CFBundleExecutable. Remove it. // the value for CFBundleExecutable. Remove it.
QFile infoPlist(infoPlistPath); if (QFile infoPlist(infoPlistPath); infoPlist.open(QIODevice::ReadOnly)) {
if (infoPlist.open(QIODevice::ReadOnly)) {
QByteArray contents = infoPlist.readAll(); QByteArray contents = infoPlist.readAll();
infoPlist.close(); 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 contents.replace("_debug", ""); // surely there are no legit uses of "_debug" in an Info.plist
infoPlist.write(contents); writableInfoPlist.write(contents);
} else { success = writableInfoPlist.commit();
LogError() << "failed to write Info.plist file" << infoPlistPath; }
if (!success) {
LogError() << "Failed to write Info.plist file" << infoPlistPath;
} }
} else { } 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); QDir().mkpath(filePath);
QFile qtconf(fileName); if (QFile::exists(fileName) && !alwaysOwerwriteEnabled) {
if (qtconf.exists() && !alwaysOwerwriteEnabled) {
LogWarning(); LogWarning();
LogWarning() << fileName << "already exists, will not overwrite."; LogWarning() << fileName << "already exists, will not overwrite.";
LogWarning() << "To make sure the plugins are loaded from the correct location,"; LogWarning() << "To make sure the plugins are loaded from the correct location,";
@ -1239,7 +1242,8 @@ void createQtConf(const QString &appBundlePath)
return; 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() << "Created configuration file:" << fileName;
LogNormal() << "This file sets the plugin search path to" << appBundlePath + "/Contents/PlugIns"; LogNormal() << "This file sets the plugin search path to" << appBundlePath + "/Contents/PlugIns";
} }