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 <QJsonValue>
#include <QRegularExpression>
#include <QSaveFile>
#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";
}