Some code refactoring in UpdateChecker class.

This commit is contained in:
LoRd_MuldeR 2018-10-17 21:26:47 +02:00
parent bd7b3653cd
commit 21b35870bd
2 changed files with 106 additions and 117 deletions

View File

@ -141,6 +141,7 @@ namespace MUtils
bool checkSignature(const QString &file, const QString &signature); bool checkSignature(const QString &file, const QString &signature);
bool tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick); 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);
}; };
} }

View File

@ -204,12 +204,15 @@ void MUtils::UpdateChecker::checkForUpdates(void)
const int networkStatus = OS::network_status(); const int networkStatus = OS::network_status();
if(networkStatus == OS::NETWORK_TYPE_NON) if(networkStatus == OS::NETWORK_TYPE_NON)
{
if (!MUtils::OS::arguments().contains("--ignore-network-status"))
{ {
log("Operating system reports that the computer is currently offline !!!"); log("Operating system reports that the computer is currently offline !!!");
setProgress(m_maxProgress); setProgress(m_maxProgress);
setStatus(UpdateStatus_ErrorNoConnection); setStatus(UpdateStatus_ErrorNoConnection);
return; return;
} }
}
msleep(500); msleep(500);
setProgress(1); setProgress(1);
@ -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 << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
args << "-o" << output.fileName() << url.toString(); 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) bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
@ -601,87 +604,7 @@ bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &t
args << "-A" << USER_AGENT_STR; args << "-A" << USER_AGENT_STR;
args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname); 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)
{
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())
{
return false;
}
bool bAborted = false;
timer.start(qMax((timeout + (timeout / 2)), 1500));
while (process.state() != QProcess::NotRunning)
{
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)))))
{
log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
qWarning("WARNING: cURL process %s!", bCancelled ? "cancelled" : "timed out");
break; /*abort process*/
}
}
timer.stop();
timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
if (bAborted)
{
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);
}
}
if (!bAborted)
{
const int exitCode = process.exitCode();
switch (exitCode)
{
case -1:
case 0: log(QLatin1String("DONE: Transfer completed successfully."), ""); break;
case 6: log(QLatin1String("ERROR: Remote host could not be resolved!"), ""); break;
case 7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), ""); break;
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*/
} }
bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature) bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
@ -705,45 +628,110 @@ bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &s
} }
} }
QStringList args;
args << QStringList() << "--homedir" << ".";
args << "--keyring" << QFileInfo(keyRingPath).fileName();
args << QFileInfo(signature).fileName();
args << QFileInfo(file).fileName();
const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
if (exitCode != INT_MAX)
{
log(QString().sprintf("Exited with code %d", exitCode));
}
if (removeKeyring)
{
remove_file(keyRingPath);
}
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)
{
switch (exitCode)
{
case -1:
case 0: log(QLatin1String("DONE: Transfer completed successfully."), ""); break;
case 6: log(QLatin1String("ERROR: Remote host could not be resolved!"), ""); break;
case 7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), ""); break;
case 28: log(QLatin1String("ERROR: Operation timed out !!!"), ""); break;
default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), ""); break;
}
}
return (exitCode == 0); /*completed*/
}
int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
{
QProcess process; QProcess process;
init_process(process, QFileInfo(file).absolutePath()); init_process(process, workingDir, true, NULL, m_environment.data());
QEventLoop loop; QEventLoop loop;
connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit())); connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit())); connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
connect(&process, SIGNAL(readyRead()), &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 (!process.waitForStarted())
{ {
if (removeKeyring) log("PROCESS FAILED TO START !!!", "");
{ qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
remove_file(keyRingPath); return INT_MAX; /*failed to start*/
}
return false;
} }
bool bAborted = false;
timer.start(qMax(timeout, 1500));
while (process.state() != QProcess::NotRunning) while (process.state() != QProcess::NotRunning)
{ {
loop.exec(); loop.exec();
while (process.canReadLine()) while (process.canReadLine())
{ {
const QString line = QString::fromLatin1(process.readLine()).simplified(); const QString line = QString::fromLatin1(process.readLine()).simplified();
if (!line.isEmpty()) if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">")))
{ {
log(line); log(line);
} }
} }
} const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
if (removeKeyring)
{ {
remove_file(keyRingPath); log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
break; /*abort process*/
}
} }
log(QString().sprintf("Exited with code %d", process.exitCode())); timer.stop();
return (process.exitCode() == 0); timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
if (bAborted)
{
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);
}
}
return bAborted ? INT_MAX : process.exitCode();
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////