summaryrefslogtreecommitdiffstats
path: root/installerbuilder/libinstaller/getrepositorymetainfojob.cpp
blob: 56bce2f8e43b6e405a6c5eaa201e9213bb17c411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**************************************************************************
**
** This file is part of Qt SDK**
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).*
**
** Contact:  Nokia Corporation qt-info@nokia.com**
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception version
** 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you are unsure which license is appropriate for your use, please contact
** (qt-info@nokia.com).
**
**************************************************************************/
#include "getrepositorymetainfojob.h"

#include "common/errors.h"
#include "common/utils.h"
#include "cryptosignatureverifier.h"
#include "lib7z_facade.h"
#include "messageboxhandler.h"
#include "qinstallerglobal.h"

#include <KDUpdater/FileDownloader>
#include <KDUpdater/FileDownloaderFactory>
#include <KDUpdater/SignatureVerifier>
#include <KDUpdater/SignatureVerificationResult>
#include <KDUpdater/kdupdatercrypto.h>

#include <QtCore/QFile>
#include <QtCore/QTimer>
#include <QtCore/QUrl>

#include <QtGui/QMessageBox>

#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>

using namespace KDUpdater;
using namespace QInstaller;


// -- GetRepositoryMetaInfoJob

GetRepositoryMetaInfoJob::GetRepositoryMetaInfoJob(const QByteArray &publicKey, QObject *parent)
    : KDJob(parent),
    m_canceled(false),
    m_silentRetries(3),
    m_retriesLeft(m_silentRetries),
    m_publicKey(publicKey),
    m_downloader()
{
    setCapabilities(Cancelable);
}

GetRepositoryMetaInfoJob::~GetRepositoryMetaInfoJob()
{
    delete m_downloader;
}

Repository GetRepositoryMetaInfoJob::repository() const
{
    return m_repository;
}

void GetRepositoryMetaInfoJob::setRepository(const Repository &r)
{
    m_repository = r;
    verbose() << "Setting repository with URL : " << r.url().toString() << std::endl;
}

int GetRepositoryMetaInfoJob::silentRetries() const
{
    return m_silentRetries;
}

void GetRepositoryMetaInfoJob::setSilentRetries(int retries)
{
    m_silentRetries = retries;
}

void GetRepositoryMetaInfoJob::doStart()
{
    m_retriesLeft = m_silentRetries;
    startUpdatesXmlDownload();
}

void GetRepositoryMetaInfoJob::doCancel()
{
    m_canceled = true;
    if (m_downloader)
        m_downloader->cancelDownload();
}

QString GetRepositoryMetaInfoJob::temporaryDirectory() const
{
    return m_temporaryDirectory;
}

QString GetRepositoryMetaInfoJob::releaseTemporaryDirectory() const
{
    m_tempDirDeleter.releaseAll();
    return m_temporaryDirectory;
}

// updates.xml download

void GetRepositoryMetaInfoJob::startUpdatesXmlDownload()
{
    if (m_downloader) {
        m_downloader->deleteLater();
        m_downloader = 0;
    }

    const QUrl url = m_repository.url();
    if (url.isEmpty()) {
        emitFinishedWithError(QInstaller::InvalidUrl, tr("empty repository URL"));
        return;
    }
    if (!url.isValid()) {
        emitFinishedWithError(QInstaller::InvalidUrl, tr("invalid repository URL"));
        return;
    }
    m_downloader = FileDownloaderFactory::instance().create(url.scheme(), 0, QUrl(), this);
    if (!m_downloader) {
        emitFinishedWithError(QInstaller::InvalidUrl, tr("URL scheme not supported: %1 (%2)")
            .arg(url.scheme(), url.toString()));
        return;
    }

    m_downloader->setUrl(QUrl(url.toString() + QLatin1String("/Updates.xml")));
    m_downloader->setAutoRemoveDownloadedFile(false);
    connect(m_downloader, SIGNAL(downloadCompleted()), this, SLOT(updatesXmlDownloadFinished()));
    connect(m_downloader, SIGNAL(downloadCanceled()), this, SLOT(updatesXmlDownloadCanceled()));
    connect(m_downloader, SIGNAL(downloadAborted(QString)), this,
        SLOT(updatesXmlDownloadError(QString)), Qt::QueuedConnection);
    m_downloader->download();
}

void GetRepositoryMetaInfoJob::updatesXmlDownloadCanceled()
{
    emitFinishedWithError(KDJob::Canceled, m_downloader->errorString());
}

void GetRepositoryMetaInfoJob::updatesXmlDownloadFinished()
{
    emit infoMessage(this, tr("Retrieving component meta information..."));

    const QString fn = m_downloader->downloadedFileName();
    Q_ASSERT(!fn.isEmpty());
    Q_ASSERT(QFile::exists(fn));

    try {
        m_temporaryDirectory = createTemporaryDirectory(QLatin1String("remoterepo"));
        m_tempDirDeleter.add(m_temporaryDirectory);
    } catch (const QInstaller::Error& e) {
        emitFinishedWithError(QInstaller::ExtractionError, e.message());
        return;
    }
    QFile file(fn);
    const QString updatesXmlPath = m_temporaryDirectory + QLatin1String("/Updates.xml");
    if (!file.rename(updatesXmlPath)) {
        emitFinishedWithError(QInstaller::DownloadError,
            tr("Could not move Updates.xml to target location: %1").arg(file.errorString()));
        return;
    }

    QFile updatesFile(updatesXmlPath);
    if (!updatesFile.open(QIODevice::ReadOnly)) {
        emitFinishedWithError(QInstaller::DownloadError,
            tr("Could not open Updates.xml for reading: %1").arg(updatesFile.errorString()));
        return;
    }

    QString err;
    QDomDocument doc;
    if (!doc.setContent(&updatesFile, &err)) {
        const QString msg =  tr("Could not fetch a valid version of Updates.xml from repository %1, Error: %2")
            .arg(m_repository.url().toString(), err);
        verbose() << msg << std::endl;

        if (!m_repository.required()) {
            emitFinishedWithError(QInstaller::UserIgnoreError, msg);
            return;
        }

        const QMessageBox::StandardButton b =
            MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(),
            QLatin1String("updatesXmlDownloadError"), tr("Download Error"), msg, QMessageBox::Cancel);

        if (b == QMessageBox::Cancel || b == QMessageBox::NoButton) {
            emitFinishedWithError(KDJob::Canceled, msg);
            return;
        }
    }

    emit infoMessage(this, tr("Parsing component meta information..."));

    const QDomElement root = doc.documentElement();
    const QDomNodeList children = root.childNodes();
    for (int i = 0; i < children.count(); ++i) {
        const QDomElement el = children.at(i).toElement();
        if (el.isNull())
            continue;
        if (el.tagName() == QLatin1String("PackageUpdate")) {
            const QDomNodeList c2 = el.childNodes();
            for (int j = 0; j < c2.count(); ++j)
                if (c2.at(j).toElement().tagName() == QLatin1String("Name"))
                    m_packageNames << c2.at(j).toElement().text();
                else if (c2.at(j).toElement().tagName() == QLatin1String("Version"))
                    m_packageVersions << c2.at(j).toElement().text();
                else if (c2.at(j).toElement().tagName() == QLatin1String("SHA1"))
                    m_packageHash << c2.at(j).toElement().text();
        }
    }

    setTotalAmount(m_packageNames.count() + 1);
    setProcessedAmount(1);
    if (m_packageNames.isEmpty()) {
        emitFinished();
        return;
    }

    emit infoMessage(this, tr("Finished updating component meta information..."));

    fetchNextMetaInfo();
}

void GetRepositoryMetaInfoJob::updatesXmlDownloadError(const QString &err)
{
    if (m_retriesLeft <= 0) {
        const QString msg = tr("Could not fetch Updates.xml from repository %1, Error: %2")
            .arg(m_repository.url().toString(), err);
        verbose() << msg << std::endl;

        if (!m_repository.required()) {
            emitFinishedWithError(QInstaller::UserIgnoreError, msg);
            return;
        }

        QMessageBox::StandardButtons buttons = QMessageBox::Retry | QMessageBox::Cancel;
        const QMessageBox::StandardButton b =
            MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(),
            QLatin1String("updatesXmlDownloadError"), tr("Download Error"), msg, buttons);

        if (b == QMessageBox::Cancel || b == QMessageBox::NoButton) {
            emitFinishedWithError(KDJob::Canceled, msg);
            return;
        }
    }

    m_retriesLeft--;
    QTimer::singleShot(1500, this, SLOT(startUpdatesXmlDownload()));
}

// meta data download

void GetRepositoryMetaInfoJob::fetchNextMetaInfo()
{
    emit infoMessage(this, tr("Retrieving component information from remote repository..."));

    if (m_canceled) {
        metaDownloadCanceled();
        return;
    }

    if (m_packageNames.isEmpty() && m_currentPackageName.isEmpty()) {
        emitFinished();
        return;
    }

    QString next = m_currentPackageName;
    QString nextVersion = m_currentPackageVersion;
    if (next.isEmpty()) {
        m_retriesLeft = m_silentRetries;
        next = m_packageNames.takeLast();
        nextVersion = m_packageVersions.takeLast();
    }

    verbose() << "fetching metadata of " << next << " in version " << nextVersion << std::endl;

    bool online = true;
    if (m_repository.url().scheme().isEmpty())
        online = false;

    const QString repoUrl = m_repository.url().toString();
    const QUrl url = QString::fromLatin1("%1/%2/%3meta.7z").arg(repoUrl, next,
        online ? nextVersion : QLatin1String(""));

    // must be deleteLater: this code is executed from slots listening to m_downloader, avoid
    // reentrancy issues
    m_downloader->deleteLater();
    if (!m_publicKey.isEmpty()) {
        const CryptoSignatureVerifier verifier(m_publicKey);
        const QUrl sigUrl = QString::fromLatin1("%1/%2/meta.7z.sig").arg(repoUrl, next);
        m_downloader = FileDownloaderFactory::instance().create(url.scheme(), &verifier, sigUrl, this);
    } else {
        m_downloader = FileDownloaderFactory::instance().create(url.scheme(), this);
    }

    if (!m_downloader) {
        qWarning() << "Scheme not supported:" << next;
        m_currentPackageName.clear();
        m_currentPackageVersion.clear();
        QMetaObject::invokeMethod(this, "fetchNextMetaInfo", Qt::QueuedConnection);
        return;
    }

    m_currentPackageName = next;
    m_currentPackageVersion = nextVersion;
    m_downloader->setUrl(url);
    m_downloader->setAutoRemoveDownloadedFile(true);

    connect(m_downloader, SIGNAL(downloadCanceled()), this, SLOT(metaDownloadCanceled()));
    connect(m_downloader, SIGNAL(downloadCompleted()), this, SLOT(metaDownloadFinished()));
    connect(m_downloader, SIGNAL(downloadAborted(QString)), this, SLOT(metaDownloadError(QString)),
        Qt::QueuedConnection);

    m_downloader->download();
}

void GetRepositoryMetaInfoJob::metaDownloadCanceled()
{
    emitFinishedWithError(KDJob::Canceled, m_downloader->errorString());
}

void GetRepositoryMetaInfoJob::metaDownloadFinished()
{
    const QString fn = m_downloader->downloadedFileName();
    Q_ASSERT(!fn.isEmpty());

    QFile arch(fn);
    if (!arch.open(QIODevice::ReadOnly)) {
        emitFinishedWithError(QInstaller::ExtractionError,
            tr("Could not open meta info archive %1: %2").arg(fn, arch.errorString()));
        return;
    }

    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();
        if (expectedFileHash != realFileHash) {
            metaDownloadError(tr("Bad hash"));
            return;
        }
        m_currentPackageName.clear();
        m_packageHash.pop_back();
    }

    try {
        // TODO: make this threaded
        Lib7z::extractArchive(&arch, m_temporaryDirectory);
        if (!arch.remove())
            qWarning("Could not delete file %s: %s", qPrintable(fn), qPrintable(arch.errorString()));
    } catch (const Lib7z::SevenZipException& e) {
        emitFinishedWithError(QInstaller::ExtractionError,
            tr("Could not open meta info archive %1: %2").arg(fn, e.message()));
        return;
    }

    fetchNextMetaInfo();
}

void GetRepositoryMetaInfoJob::metaDownloadError(const QString &err)
{
    if (err == QObject::tr("Bad signature"))
        emit infoMessage(this, tr("The RSA signature of one component could not be verified."));

    if (err == QObject::tr("Bad hash"))
        emit infoMessage(this, tr("The hash of one component does not match the expected one."));

    if (m_retriesLeft <= 0) {
        const QString msg = tr("Could not download meta information for component %1, Error: %2")
            .arg(m_currentPackageName, err);
        verbose() << msg << std::endl;

        if (!m_repository.required()) {
            emitFinishedWithError(QInstaller::UserIgnoreError, msg);
            return;
        }

        QMessageBox::StandardButtons buttons = QMessageBox::Retry | QMessageBox::Cancel;
        const QMessageBox::StandardButton b =
            MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(),
            QLatin1String("updatesXmlDownloadError"), tr("Download Error"), msg, buttons);

        if (b == QMessageBox::Cancel || b == QMessageBox::NoButton) {
            emitFinishedWithError(KDJob::Canceled, msg);
            return;
        }
    }

    m_retriesLeft--;
    QTimer::singleShot(1500, this, SLOT(fetchNextMetaInfo()));
}