From 21b35870bd0ba6959ebdc654148f1f1cd140e20b Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Wed, 17 Oct 2018 21:26:47 +0200 Subject: [PATCH] Some code refactoring in UpdateChecker class. --- include/MUtils/UpdateChecker.h | 3 +- src/UpdateChecker.cpp | 162 +++++++++++++++++++---------------------- 2 files changed, 77 insertions(+), 88 deletions(-) diff --git a/include/MUtils/UpdateChecker.h b/include/MUtils/UpdateChecker.h index 07c1bb4..c397965 100644 --- a/include/MUtils/UpdateChecker.h +++ b/include/MUtils/UpdateChecker.h @@ -141,6 +141,7 @@ namespace MUtils bool checkSignature(const QString &file, const QString &signature); bool tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick); - bool invokeCurl(const QStringList &args, const QString &workingDir, const int timeout); + bool execCurl(const QStringList &args, const QString &workingDir, const int timeout); + int execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout); }; } diff --git a/src/UpdateChecker.cpp b/src/UpdateChecker.cpp index 54972c0..103c1ef 100644 --- a/src/UpdateChecker.cpp +++ b/src/UpdateChecker.cpp @@ -205,10 +205,13 @@ void MUtils::UpdateChecker::checkForUpdates(void) const int networkStatus = OS::network_status(); if(networkStatus == OS::NETWORK_TYPE_NON) { - log("Operating system reports that the computer is currently offline !!!"); - setProgress(m_maxProgress); - setStatus(UpdateStatus_ErrorNoConnection); - return; + if (!MUtils::OS::arguments().contains("--ignore-network-status")) + { + log("Operating system reports that the computer is currently offline !!!"); + setProgress(m_maxProgress); + setStatus(UpdateStatus_ErrorNoConnection); + return; + } } msleep(500); @@ -589,7 +592,7 @@ bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, con args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host()); args << "-o" << output.fileName() << url.toString(); - return invokeCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT); + return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT); } bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec) @@ -601,73 +604,55 @@ bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &t args << "-A" << USER_AGENT_STR; args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname); - return invokeCurl(args, temp_folder(), timeoutMsec); + return execCurl(args, temp_folder(), timeoutMsec); } -bool MUtils::UpdateChecker::invokeCurl(const QStringList &args, const QString &workingDir, const int timeout) +bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature) { - QProcess process; - init_process(process, workingDir, true, NULL, m_environment.data()); - - QEventLoop loop; - connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit())); - connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit())); - connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit())); - - QTimer timer; - timer.setSingleShot(true); - connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); - - process.start(m_binaryCurl, args); - if (!process.waitForStarted()) + if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0) { + qWarning("CheckSignature: File and signature should be in same folder!"); return false; } - bool bAborted = false; - timer.start(qMax((timeout + (timeout / 2)), 1500)); - - while (process.state() != QProcess::NotRunning) + QString keyRingPath(m_binaryKeys); + bool removeKeyring = false; + if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0) { - loop.exec(); - while (process.canReadLine()) - { - const QString line = QString::fromLatin1(process.readLine()).simplified(); - if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">"))) - { - log(line); - } - } - const bool bCancelled = MUTILS_BOOLIFY(m_cancelled); - if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125))))) + keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg"); + removeKeyring = true; + if (!QFile::copy(m_binaryKeys, keyRingPath)) { - log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", ""); - qWarning("WARNING: cURL process %s!", bCancelled ? "cancelled" : "timed out"); - break; /*abort process*/ + qWarning("CheckSignature: Failed to copy the key-ring file!"); + return false; } } - timer.stop(); - timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + QStringList args; + args << QStringList() << "--homedir" << "."; + args << "--keyring" << QFileInfo(keyRingPath).fileName(); + args << QFileInfo(signature).fileName(); + args << QFileInfo(file).fileName(); - if (bAborted) + const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT); + if (exitCode != INT_MAX) { - process.kill(); - process.waitForFinished(-1); + log(QString().sprintf("Exited with code %d", exitCode)); } - while (process.canReadLine()) + if (removeKeyring) { - const QString line = QString::fromLatin1(process.readLine()).simplified(); - if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">"))) - { - log(line); - } + remove_file(keyRingPath); } - if (!bAborted) + return (exitCode == 0); /*completed*/ +} + +bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout) +{ + const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2)); + if (exitCode != INT_MAX) { - const int exitCode = process.exitCode(); switch (exitCode) { case -1: @@ -678,72 +663,75 @@ bool MUtils::UpdateChecker::invokeCurl(const QStringList &args, const QString &w case 28: log(QLatin1String("ERROR: Operation timed out !!!"), ""); break; default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), ""); break; } - return (exitCode == 0); } - - return false; /*aborted*/ + + return (exitCode == 0); /*completed*/ } -bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature) +int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout) { - if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0) - { - qWarning("CheckSignature: File and signature should be in same folder!"); - return false; - } - - QString keyRingPath(m_binaryKeys); - bool removeKeyring = false; - if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0) - { - keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg"); - removeKeyring = true; - if (!QFile::copy(m_binaryKeys, keyRingPath)) - { - qWarning("CheckSignature: Failed to copy the key-ring file!"); - return false; - } - } - QProcess process; - init_process(process, QFileInfo(file).absolutePath()); + init_process(process, workingDir, true, NULL, m_environment.data()); QEventLoop loop; connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit())); connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit())); connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit())); - process.start(m_binaryGnuPG, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(keyRingPath).fileName() << QFileInfo(signature).fileName() << QFileInfo(file).fileName()); + QTimer timer; + timer.setSingleShot(true); + connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + process.start(programFile, args); if (!process.waitForStarted()) { - if (removeKeyring) - { - remove_file(keyRingPath); - } - return false; + log("PROCESS FAILED TO START !!!", ""); + qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName())); + return INT_MAX; /*failed to start*/ } + bool bAborted = false; + timer.start(qMax(timeout, 1500)); + while (process.state() != QProcess::NotRunning) { loop.exec(); while (process.canReadLine()) { const QString line = QString::fromLatin1(process.readLine()).simplified(); - if (!line.isEmpty()) + if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">"))) { log(line); } } + const bool bCancelled = MUTILS_BOOLIFY(m_cancelled); + if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125))))) + { + log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", ""); + qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out"); + break; /*abort process*/ + } } - if (removeKeyring) + timer.stop(); + timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + + if (bAborted) { - remove_file(keyRingPath); + process.kill(); + process.waitForFinished(-1); + } + + while (process.canReadLine()) + { + const QString line = QString::fromLatin1(process.readLine()).simplified(); + if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">"))) + { + log(line); + } } - log(QString().sprintf("Exited with code %d", process.exitCode())); - return (process.exitCode() == 0); + return bAborted ? INT_MAX : process.exitCode(); } //////////////////////////////////////////////////////////// -- 2.11.0