Avoid copying the binaries for update checker.

This commit is contained in:
LoRd_MuldeR 2017-03-30 23:10:12 +02:00
parent 9c63c30d90
commit 4d894cc37b
3 changed files with 48 additions and 43 deletions

View File

@ -26,7 +26,7 @@
#define VER_X264_MAJOR 2 #define VER_X264_MAJOR 2
#define VER_X264_MINOR 7 #define VER_X264_MINOR 7
#define VER_X264_PATCH 9 #define VER_X264_PATCH 9
#define VER_X264_BUILD 1095 #define VER_X264_BUILD 1097
#define VER_X264_PORTABLE_EDITION (0) #define VER_X264_PORTABLE_EDITION (0)

View File

@ -73,6 +73,11 @@ const UpdaterDialog::binary_t UpdaterDialog::BINARIES[] =
} \ } \
while(0) while(0)
static inline QString GETBIN(const QMap<QString, QFile*> &binaries, const QString &nameName)
{
const QFile *const file = binaries.value(nameName);
return file ? file->fileName() : QString();
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Constructor & Destructor // Constructor & Destructor
@ -129,8 +134,7 @@ UpdaterDialog::~UpdaterDialog(void)
m_thread->wait(); m_thread->wait();
} }
} }
closeFiles();
cleanFiles();
delete ui; delete ui;
} }
@ -220,12 +224,6 @@ void UpdaterDialog::keyPressEvent(QKeyEvent *event)
void UpdaterDialog::initUpdate(void) void UpdaterDialog::initUpdate(void)
{ {
//Clean up files from previous attempt
if(!m_binaries.isEmpty())
{
cleanFiles();
}
//Check binary files //Check binary files
if(!checkBinaries()) if(!checkBinaries())
{ {
@ -235,7 +233,8 @@ void UpdaterDialog::initUpdate(void)
{ {
QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl))); QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl)));
} }
close(); return; close();
return;
} }
//Make sure user does have admin access //Make sure user does have admin access
@ -248,14 +247,15 @@ void UpdaterDialog::initUpdate(void)
if(QMessageBox::critical(this, this->windowTitle(), message, tr("Discard"), tr("Ignore")) != 1) if(QMessageBox::critical(this, this->windowTitle(), message, tr("Discard"), tr("Ignore")) != 1)
{ {
ui->buttonCancel->setEnabled(true); ui->buttonCancel->setEnabled(true);
close(); return; close();
return;
} }
} }
//Create and setup thread //Create and setup thread
if(!m_thread) if(!m_thread)
{ {
m_thread.reset(new MUtils::UpdateChecker(m_binaries.value("wget.exe"), m_binaries.value("netc.exe"), m_binaries.value("gpgv.exe"), m_binaries.value("gpgv.gpg"), "Simple x264 Launcher", x264_version_build(), false)); m_thread.reset(new MUtils::UpdateChecker(GETBIN(m_binaries, "wget.exe"), GETBIN(m_binaries, "netc.exe"), GETBIN(m_binaries, "gpgv.exe"), GETBIN(m_binaries, "gpgv.gpg"), "Simple x264 Launcher", x264_version_build(), false));
connect(m_thread.data(), SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int))); connect(m_thread.data(), SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int)));
connect(m_thread.data(), SIGNAL(finished()), this, SLOT(threadFinished())); connect(m_thread.data(), SIGNAL(finished()), this, SLOT(threadFinished()));
connect(m_thread.data(), SIGNAL(terminated()), this, SLOT(threadFinished())); connect(m_thread.data(), SIGNAL(terminated()), this, SLOT(threadFinished()));
@ -481,7 +481,7 @@ void UpdaterDialog::installUpdate(void)
args << QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName()); args << QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName());
args << QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo->getBuildNo())); args << QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo->getBuildNo()));
process.start(m_binaries.value("wupd.exe"), args); process.start(GETBIN(m_binaries, "wupd.exe"), args);
if(!process.waitForStarted()) if(!process.waitForStarted())
{ {
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
@ -520,36 +520,39 @@ void UpdaterDialog::installUpdate(void)
bool UpdaterDialog::checkBinaries(void) bool UpdaterDialog::checkBinaries(void)
{ {
qDebug("[File Verification]"); qDebug("[File Verification]");
m_binaries.clear();
//Validate hashes first
const QString tempPath = MUtils::temp_folder();
for(size_t i = 0; BINARIES[i].name; i++) for(size_t i = 0; BINARIES[i].name; i++)
{ {
const QString orgName = QString::fromLatin1(BINARIES[i].name); const QString name = QString::fromLatin1(BINARIES[i].name);
const QString binPath = QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), orgName); if (!m_binaries.contains(name))
const QString outPath = MUtils::make_unique_file(tempPath, QFileInfo(orgName).baseName(), QFileInfo(orgName).suffix());
if(!checkFileHash(binPath, BINARIES[i].hash))
{ {
qWarning("Verification of '%s' has failed!", MUTILS_UTF8(orgName)); QScopedPointer<QFile> binary(new QFile(QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), name)));
return false; if (binary->open(QIODevice::ReadOnly))
} {
if(outPath.isEmpty() || (!QFile::copy(binPath, outPath))) if (checkFileHash(binary->fileName(), BINARIES[i].hash))
{ {
qWarning("Copying of '%s' has failed!", MUTILS_UTF8(orgName));
return false;
}
QFile::setPermissions(outPath, QFile::ReadOwner);
m_binaries.insert(BINARIES[i].name, outPath);
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents); QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
m_binaries.insert(name, binary.take());
} }
else
{
qWarning("Verification of '%s' has failed!", MUTILS_UTF8(name));
return false;
}
}
else
{
qWarning("File '%s' could not be opened!", MUTILS_UTF8(name));
return false;
}
}
}
qDebug("File check completed.\n");
return true; return true;
} }
bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash) bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
{ {
qDebug("Checking file: %s", filePath.toUtf8().constData()); qDebug("Checking file: %s", MUTILS_UTF8(filePath));
QScopedPointer<MUtils::Hash::Hash> checksum(MUtils::Hash::create(MUtils::Hash::HASH_BLAKE2_512, DIGEST_KEY)); QScopedPointer<MUtils::Hash::Hash> checksum(MUtils::Hash::create(MUtils::Hash::HASH_BLAKE2_512, DIGEST_KEY));
QFile file(filePath); QFile file(filePath);
if(file.open(QIODevice::ReadOnly)) if(file.open(QIODevice::ReadOnly))
@ -571,17 +574,18 @@ bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedH
} }
} }
void UpdaterDialog::cleanFiles(void) void UpdaterDialog::closeFiles(void)
{ {
const QStringList keys = m_binaries.keys(); if (!m_binaries.empty())
foreach(const QString &key, keys)
{ {
const QString fileName = m_binaries.value(key); for (QMap<QString, QFile*>::ConstIterator iter = m_binaries.constBegin(); iter != m_binaries.constEnd(); iter++)
QFile::setPermissions(fileName, QFile::ReadOwner | QFile::WriteOwner);
if(!QFile::remove(fileName))
{ {
qWarning("Failed to remove file: %s", MUTILS_UTF8(fileName)); if (QFile *const file = iter.value())
{
file->close();
delete (file);
} }
m_binaries.remove(key); }
m_binaries.clear();
} }
} }

View File

@ -25,6 +25,7 @@
#include <QMap> #include <QMap>
class QMovie; class QMovie;
class QFile;
class QElapsedTimer; class QElapsedTimer;
class SysinfoModel; class SysinfoModel;
@ -80,7 +81,7 @@ private:
bool checkBinaries(); bool checkBinaries();
bool checkFileHash(const QString &filePath, const char *expectedHash); bool checkFileHash(const QString &filePath, const char *expectedHash);
void cleanFiles(void); void closeFiles(void);
const SysinfoModel *const m_sysinfo; const SysinfoModel *const m_sysinfo;
const char *const m_updateUrl; const char *const m_updateUrl;
@ -94,6 +95,6 @@ private:
unsigned long m_updaterProcess; unsigned long m_updaterProcess;
QStringList m_logFile; QStringList m_logFile;
QMap<QString,QString> m_binaries; QMap<QString,QFile*> m_binaries;
int m_status; int m_status;
}; };