summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkh1 <karsten.heimrich@nokia.com>2012-09-28 14:46:17 +0200
committerKarsten Heimrich <karsten.heimrich@digia.com>2012-10-01 12:13:06 +0200
commitc8cc7d5b521c29e0968ce11dd9c63de0ce260c49 (patch)
treef5d90f7a7a8daa86848ff1ede9ebc91abbce52aa
parenta1b2010ce87bd878f78acf01a5677875f34254ef (diff)
Merge "buffered" calculateHash function and reuse.
Change-Id: I294038888bd47a139b8c3df68e298e66e5ec2202 Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
-rw-r--r--src/libs/installer/downloadarchivesjob.cpp13
-rw-r--r--src/libs/installer/getrepositorymetainfojob.cpp7
-rw-r--r--src/libs/installer/utils.cpp11
-rw-r--r--src/libs/installer/utils.h1
-rw-r--r--src/libs/kdtools/kdupdaterfiledownloader.cpp24
-rw-r--r--src/libs/kdtools/kdupdaterfiledownloader.h4
-rw-r--r--src/libs/kdtools/kdupdaterfiledownloader_p.h1
-rw-r--r--tools/common/repositorygen.cpp10
8 files changed, 19 insertions, 52 deletions
diff --git a/src/libs/installer/downloadarchivesjob.cpp b/src/libs/installer/downloadarchivesjob.cpp
index f33108442..f44c5c1f8 100644
--- a/src/libs/installer/downloadarchivesjob.cpp
+++ b/src/libs/installer/downloadarchivesjob.cpp
@@ -35,6 +35,7 @@
#include "component.h"
#include "messageboxhandler.h"
#include "packagemanagercore.h"
+#include "utils.h"
#include "kdupdaterfiledownloader.h"
#include "kdupdaterfiledownloaderfactory.h"
@@ -227,16 +228,8 @@ void DownloadArchivesJob::registerFile()
if (m_core->testChecksum()) {
QFile archiveFile(tempFile);
if (archiveFile.open(QFile::ReadOnly)) {
- static QByteArray buffer(1024 * 1024, '\0');
- QCryptographicHash hash(QCryptographicHash::Sha1);
- while (true) {
- const qint64 numRead = archiveFile.read(buffer.data(), buffer.size());
- if (numRead <= 0)
- break;
- hash.addData(buffer.constData(), numRead);
- }
-
- const QByteArray archiveHash = hash.result().toHex();
+ const QByteArray archiveHash = QInstaller::calculateHash(&archiveFile, QCryptographicHash::Sha1)
+ .toHex();
if ((archiveHash != m_currentHash) && (!m_canceled)) {
//TODO: Maybe we should try to download the file again automatically
const QMessageBox::Button res =
diff --git a/src/libs/installer/getrepositorymetainfojob.cpp b/src/libs/installer/getrepositorymetainfojob.cpp
index 22fd3c0fd..689f4a4ed 100644
--- a/src/libs/installer/getrepositorymetainfojob.cpp
+++ b/src/libs/installer/getrepositorymetainfojob.cpp
@@ -36,6 +36,7 @@
#include "messageboxhandler.h"
#include "packagemanagercore_p.h"
#include "qinstallerglobal.h"
+#include "utils.h"
#include "kdupdaterfiledownloader.h"
#include "kdupdaterfiledownloaderfactory.h"
@@ -466,10 +467,8 @@ void GetRepositoryMetaInfoJob::metaDownloadFinished()
if (!m_packageHash.isEmpty()) {
// verify file hash
- QByteArray expectedFileHash = m_packageHash.back().toLatin1();
- QByteArray archContent = arch.readAll();
- QByteArray realFileHash = QString::fromLatin1(QCryptographicHash::hash(archContent,
- QCryptographicHash::Sha1).toHex()).toLatin1();
+ const QByteArray expectedFileHash = m_packageHash.back().toLatin1();
+ const QByteArray realFileHash = QInstaller::calculateHash(&arch, QCryptographicHash::Sha1).toHex();
if (expectedFileHash != realFileHash) {
emit infoMessage(this, tr("The hash of one component does not match the expected one."));
metaDownloadError(tr("Bad hash."));
diff --git a/src/libs/installer/utils.cpp b/src/libs/installer/utils.cpp
index 4bd93430f..ffe705ba4 100644
--- a/src/libs/installer/utils.cpp
+++ b/src/libs/installer/utils.cpp
@@ -103,13 +103,11 @@ std::ostream &QInstaller::operator<<(std::ostream &os, const QString &string)
return os << qPrintable(string);
}
-//TODO from kdupdaterfiledownloader.cpp, use that one once merged
QByteArray QInstaller::calculateHash(QIODevice *device, QCryptographicHash::Algorithm algo)
{
Q_ASSERT(device);
QCryptographicHash hash(algo);
- QByteArray buffer;
- buffer.resize(512 * 1024);
+ static QByteArray buffer(1024 * 1024, '\0');
while (true) {
const qint64 numRead = device->read(buffer.data(), buffer.size());
if (numRead <= 0)
@@ -119,6 +117,13 @@ QByteArray QInstaller::calculateHash(QIODevice *device, QCryptographicHash::Algo
return QByteArray(); // never reached
}
+QByteArray QInstaller::calculateHash(const QString &path, QCryptographicHash::Algorithm algo)
+{
+ QFile file(path);
+ if (!file.open(QIODevice::ReadOnly))
+ return QByteArray();
+ return calculateHash(&file, algo);
+}
QString QInstaller::replaceVariables(const QHash<QString, QString> &vars, const QString &str)
{
diff --git a/src/libs/installer/utils.h b/src/libs/installer/utils.h
index cbf2e95e7..d4c64f579 100644
--- a/src/libs/installer/utils.h
+++ b/src/libs/installer/utils.h
@@ -50,6 +50,7 @@ QT_END_NAMESPACE
namespace QInstaller {
QByteArray INSTALLER_EXPORT calculateHash(QIODevice *device, QCryptographicHash::Algorithm algo);
+ QByteArray INSTALLER_EXPORT calculateHash(const QString &path, QCryptographicHash::Algorithm algo);
QString INSTALLER_EXPORT replaceVariables(const QHash<QString,QString> &vars, const QString &str);
QString INSTALLER_EXPORT replaceWindowsEnvironmentVariables(const QString &str);
diff --git a/src/libs/kdtools/kdupdaterfiledownloader.cpp b/src/libs/kdtools/kdupdaterfiledownloader.cpp
index 818c8ac70..ec2293a2c 100644
--- a/src/libs/kdtools/kdupdaterfiledownloader.cpp
+++ b/src/libs/kdtools/kdupdaterfiledownloader.cpp
@@ -35,7 +35,6 @@
#include <QUrl>
#include <QTemporaryFile>
#include <QFileInfo>
-#include <QCryptographicHash>
#include <QThreadPool>
#include <QDebug>
#include <QSslError>
@@ -50,29 +49,6 @@ static double calcProgress(qint32 done, qint32 total)
return total ? (double(done) / double(total)) : 0;
}
-QByteArray KDUpdater::calculateHash(QIODevice* device, QCryptographicHash::Algorithm algo)
-{
- Q_ASSERT(device);
- QCryptographicHash hash(algo);
- QByteArray buffer;
- buffer.resize(512 * 1024);
- while (true) {
- const qint64 numRead = device->read(buffer.data(), buffer.size());
- if (numRead <= 0)
- return hash.result();
- hash.addData(buffer.constData(), numRead);
- }
- return QByteArray(); // never reached
-}
-
-QByteArray KDUpdater::calculateHash(const QString &path, QCryptographicHash::Algorithm algo)
-{
- QFile file(path);
- if (!file.open(QIODevice::ReadOnly))
- return QByteArray();
- return calculateHash(&file, algo);
-}
-
// -- HashVerificationJob
diff --git a/src/libs/kdtools/kdupdaterfiledownloader.h b/src/libs/kdtools/kdupdaterfiledownloader.h
index 6408b7216..6f6d03cc5 100644
--- a/src/libs/kdtools/kdupdaterfiledownloader.h
+++ b/src/libs/kdtools/kdupdaterfiledownloader.h
@@ -28,15 +28,11 @@
#include <QtCore/QObject>
#include <QtCore/QUrl>
-#include <QtCore/QCryptographicHash>
#include <QtNetwork/QAuthenticator>
namespace KDUpdater {
-KDTOOLS_EXPORT QByteArray calculateHash(QIODevice *device, QCryptographicHash::Algorithm algo);
-KDTOOLS_EXPORT QByteArray calculateHash(const QString &path, QCryptographicHash::Algorithm algo);
-
class HashVerificationJob;
class FileDownloaderProxyFactory;
diff --git a/src/libs/kdtools/kdupdaterfiledownloader_p.h b/src/libs/kdtools/kdupdaterfiledownloader_p.h
index 81b3c2c98..c47663bd1 100644
--- a/src/libs/kdtools/kdupdaterfiledownloader_p.h
+++ b/src/libs/kdtools/kdupdaterfiledownloader_p.h
@@ -25,7 +25,6 @@
#include "kdupdaterfiledownloader.h"
-#include <QtCore/QCryptographicHash>
#include <QtNetwork/QNetworkReply>
// these classes are not a part of the public API
diff --git a/tools/common/repositorygen.cpp b/tools/common/repositorygen.cpp
index dadb651f2..0f49466f3 100644
--- a/tools/common/repositorygen.cpp
+++ b/tools/common/repositorygen.cpp
@@ -36,10 +36,10 @@
#include <lib7z_facade.h>
#include <settings.h>
#include <qinstallerglobal.h>
+#include <utils.h>
#include <kdupdater.h>
-#include <QtCore/QCryptographicHash>
#include <QtCore/QDirIterator>
#include <QtScript/QScriptEngine>
@@ -541,8 +541,7 @@ void QInstallerTools::compressMetaDirectories(const QString &repoDir, const QStr
QFile tmp(tmpTarget);
tmp.open(QFile::ReadOnly);
- QByteArray fileToCheck = tmp.readAll();
- QByteArray sha1Sum = QCryptographicHash::hash(fileToCheck, QCryptographicHash::Sha1);
+ const QByteArray sha1Sum = QInstaller::calculateHash(&tmp, QCryptographicHash::Sha1);
writeSHA1ToNodeWithName(doc, elements, sha1Sum, path);
const QString finalTarget = absPath + QLatin1String("/") + fn;
if (!tmp.rename(finalTarget))
@@ -615,12 +614,11 @@ void QInstallerTools::copyComponentData(const QString &packageDir, const QString
try {
QInstaller::openForRead(&archiveFile, archiveFile.fileName());
- const QByteArray archiveData = archiveFile.readAll();
+ const QByteArray hashOfArchiveData = QInstaller::calculateHash(&archiveFile,
+ QCryptographicHash::Sha1).toHex();
archiveFile.close();
QInstaller::openForWrite(&archiveHashFile, archiveHashFile.fileName());
- const QByteArray hashOfArchiveData = QCryptographicHash::hash(archiveData,
- QCryptographicHash::Sha1).toHex();
archiveHashFile.write(hashOfArchiveData);
qDebug() << "Generated sha1 hash:" << hashOfArchiveData;
infos[i].copiedArchives.append(archiveHashFile.fileName());