diff options
Diffstat (limited to 'src/libs/installer/elevatedexecuteoperation.cpp')
-rw-r--r-- | src/libs/installer/elevatedexecuteoperation.cpp | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/src/libs/installer/elevatedexecuteoperation.cpp b/src/libs/installer/elevatedexecuteoperation.cpp index fb1778fe0..87810211e 100644 --- a/src/libs/installer/elevatedexecuteoperation.cpp +++ b/src/libs/installer/elevatedexecuteoperation.cpp @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2021 The Qt Company Ltd. +** Copyright (C) 2023 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Installer Framework. @@ -35,7 +35,7 @@ #include <QtCore/QDebug> #include <QtCore/QProcessEnvironment> -#include <QtCore/QRegExp> +#include <QtCore/QRegularExpression> #include <QtCore/QThread> using namespace QInstaller; @@ -65,10 +65,12 @@ public: private: bool needsRerunWithReplacedVariables(QStringList &arguments, const OperationType type); + void setErrorMessage(const QString &message); - +private: QProcessWrapper *process; bool showStandardError; + QString m_customErrorMessage; }; ElevatedExecuteOperation::ElevatedExecuteOperation(PackageManagerCore *core) @@ -120,13 +122,12 @@ int ElevatedExecuteOperation::Private::run(QStringList &arguments, const Operati args.removeAll(workingDirectoryArgument); } - QString customErrorMessage; QStringList filteredCustomErrorMessage = args.filter(QLatin1String("errormessage="), Qt::CaseInsensitive); if (!filteredCustomErrorMessage.isEmpty()) { QString customErrorMessageArgument = filteredCustomErrorMessage.at(0); - customErrorMessage = customErrorMessageArgument; - customErrorMessage.replace(QLatin1String("errormessage="), QString(), Qt::CaseInsensitive); + m_customErrorMessage = customErrorMessageArgument; + m_customErrorMessage.replace(QLatin1String("errormessage="), QString(), Qt::CaseInsensitive); args.removeAll(customErrorMessageArgument); } @@ -137,9 +138,10 @@ int ElevatedExecuteOperation::Private::run(QStringList &arguments, const Operati QList< int > allowedExitCodes; - QRegExp re(QLatin1String("^\\{((-?\\d+,)*-?\\d+)\\}$")); - if (re.exactMatch(args.first())) { - const QStringList numbers = re.cap(1).split(QLatin1Char(',')); + static const QRegularExpression re(QLatin1String("^\\{((-?\\d+,)*-?\\d+)\\}$")); + const QRegularExpressionMatch match = re.match(args.first()); + if (match.hasMatch()) { + const QStringList numbers = match.captured(1).split(QLatin1Char(',')); for(QStringList::const_iterator it = numbers.constBegin(); it != numbers.constEnd(); ++it) allowedExitCodes.push_back(it->toInt()); args.pop_front(); @@ -156,7 +158,8 @@ int ElevatedExecuteOperation::Private::run(QStringList &arguments, const Operati const bool success = QProcessWrapper::startDetached(args.front(), args.mid(1)); if (!success) { q->setError(UserDefinedError); - q->setErrorString(tr("Cannot start detached: \"%1\"").arg(callstr)); + setErrorMessage(tr("Cannot start detached: \"%1\"").arg(callstr)); + returnValue = Error; } return returnValue; @@ -201,8 +204,7 @@ int ElevatedExecuteOperation::Private::run(QStringList &arguments, const Operati int returnValue = NoError; if (!success) { q->setError(UserDefinedError); - //TODO: pass errorString() through the wrapper */ - q->setErrorString(tr("Cannot start: \"%1\": %2").arg(callstr, + setErrorMessage(tr("Cannot start: \"%1\": %2").arg(callstr, process->errorString())); if (!needsRerunWithReplacedVariables(arguments, type)) { returnValue = Error; @@ -220,30 +222,25 @@ int ElevatedExecuteOperation::Private::run(QStringList &arguments, const Operati q->setValue(QLatin1String("ExitCode"), process->exitCode()); - if (process->exitStatus() == QProcessWrapper::CrashExit) { - q->setError(UserDefinedError); - q->setErrorString(tr("Program crashed: \"%1\"").arg(callstr)); - returnValue = Error; - } + if (success) { + const QByteArray standardErrorOutput = process->readAllStandardError(); + // in error case it would be useful to see something in verbose output + if (!standardErrorOutput.isEmpty()) + qCWarning(QInstaller::lcInstallerInstallLog).noquote() << standardErrorOutput; - if (!allowedExitCodes.contains(process->exitCode()) && returnValue != NeedsRerun) { - if (!needsRerunWithReplacedVariables(arguments, type)) { + if (process->exitStatus() == QProcessWrapper::CrashExit) { q->setError(UserDefinedError); - if (customErrorMessage.isEmpty()) { - q->setErrorString(tr("Execution failed (Unexpected exit code: %1): \"%2\"") + setErrorMessage(tr("Program crashed: \"%1\"").arg(callstr)); + returnValue = Error; + } else if (!allowedExitCodes.contains(process->exitCode()) && returnValue != NeedsRerun) { + if (!needsRerunWithReplacedVariables(arguments, type)) { + q->setError(UserDefinedError); + setErrorMessage(tr("Execution failed (Unexpected exit code: %1): \"%2\"") .arg(QString::number(process->exitCode()), callstr)); + returnValue = Error; } else { - q->setErrorString(customErrorMessage); + returnValue = NeedsRerun; } - - QByteArray standardErrorOutput = process->readAllStandardError(); - // in error case it would be useful to see something in verbose output - if (!standardErrorOutput.isEmpty()) - qCWarning(QInstaller::lcInstallerInstallLog).noquote() << standardErrorOutput; - - returnValue = Error; - } else { - returnValue = NeedsRerun; } } Q_ASSERT(process); @@ -276,6 +273,13 @@ bool ElevatedExecuteOperation::Private::needsRerunWithReplacedVariables(QStringL return rerun; } +void ElevatedExecuteOperation::Private::setErrorMessage(const QString &message) +{ + if (m_customErrorMessage.isEmpty()) + q->setErrorString(message); + else + q->setErrorString(m_customErrorMessage); +} /*! Cancels the ElevatedExecuteOperation. This methods tries to terminate the process gracefully by calling QProcessWrapper::terminate. After 10 seconds, the process gets killed. |