summaryrefslogtreecommitdiffstats
path: root/src/tools/package-server/pspackages.cpp
blob: 2e3083caa55c3e9e5e1cd485485ab47282e3e1da (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QUrl>
#include <QFileSystemWatcher>
#include <QDirIterator>
#include <QTemporaryDir>
#include <QCryptographicHash>
#include <QMessageAuthenticationCode>
#include <QtAppManCommon/exception.h>
#include <QtAppManCommon/architecture.h>
#include <QtAppManCommon/unixsignalhandler.h>
#include <QtAppManPackage/packagecreator.h>
#include <QtAppManPackage/packageextractor.h>
#include <QtAppManApplication/yamlpackagescanner.h>
#include <QtAppManApplication/packageinfo.h>
#include <QtAppManApplication/installationreport.h>
#include <QtAppManCrypto/signature.h>

#include "pspackages.h"
#include "pspackages_p.h"
#include "psconfiguration.h"
#include "package-server.h"

#if defined(Q_OS_UNIX)
#  include <csignal>
#  define AM_PS_SIGNALS  { SIGTERM, SIGINT, SIGFPE, SIGSEGV, SIGPIPE, SIGABRT, SIGQUIT }
#else
#  include <windows.h>
#  define AM_PS_SIGNALS  { SIGTERM, SIGINT }
#endif


QT_USE_NAMESPACE_AM
using namespace Qt::StringLiterals;

static const QString UploadDirName   = u"upload"_s;
static const QString RemoveDirName   = u"remove"_s;
static const QString PackagesDirName = u".packages"_s;
static const QString LockFileName    = u".lock"_s;


PSPackages::PSPackages(PSConfiguration *cfg, QObject *parent)
    : QObject(parent)
    , d(new PSPackagesPrivate)
{
    d->q = this;
    d->cfg = cfg;
}


PSPackages::~PSPackages()
{ }

void PSPackages::initialize()
{
    QDir &dd = d->cfg->dataDirectory;

    // create a lock file to avoid data corruption
    d->lockFile = std::make_unique<QLockFile>(dd.absoluteFilePath(LockFileName));
    d->lockFile->setStaleLockTime(0); // this is a long-lived lock
    if (!d->lockFile->tryLock(1000)) {
        QString msg;
        if (d->lockFile->error() == QLockFile::LockFailedError) {
            msg = u" - there is an existing .lock file"_s;
            qint64 pid = 0;
            if (d->lockFile->getLockInfo(&pid, nullptr, nullptr))
                msg = msg + u" (belonging to pid %1)"_s.arg(pid);
        }
        throw Exception("could not lock the data directory %1%2")
            .arg(dd.absolutePath()).arg(msg);
    }

    // make sure to always clean up the lock file, even if we crash
    d->lockFilePath = d->lockFile->fileName().toLocal8Bit();


    UnixSignalHandler::instance()->install(UnixSignalHandler::RawSignalHandler, AM_PS_SIGNALS,
                                           [this](int sig) {
        UnixSignalHandler::instance()->resetToDefault(sig);
#if defined(Q_OS_WINDOWS)
        if (!d->lockFilePath.isEmpty())
            ::DeleteFileW((LPCWSTR) d->lockFile->fileName().utf16());
        ::raise(sig);
#else
        if (!d->lockFilePath.isEmpty())
            ::unlink(d->lockFilePath.constData());
        ::kill(0, sig);
#endif
    });

    if (!dd.mkpath(PackagesDirName)) {
        throw Exception("could not create a '%1' directory inside the data directory %2")
            .arg(PackagesDirName).arg(dd.absolutePath());
    }
    if (!dd.mkpath(UploadDirName)) {
        throw Exception("could not create an '%1' directory inside the data directory %2")
            .arg(UploadDirName).arg(dd.absolutePath());
    }
    if (!dd.mkpath(RemoveDirName)) {
        throw Exception("could not create an '%1' directory inside the data directory %2")
            .arg(RemoveDirName).arg(dd.absolutePath());
    }

    d->scanPackages();
    d->scanUploads();
    d->scanRemoves();

    auto dirWatcher = new QFileSystemWatcher({ dd.absoluteFilePath(UploadDirName),
                                              dd.absoluteFilePath(RemoveDirName) }, this);
    QObject::connect(dirWatcher, &QFileSystemWatcher::directoryChanged,
                     this, [&](const QString &dir) {
        const auto dirName = QDir(dir).dirName();
        if (dirName == RemoveDirName)
            d->scanRemoves();
        else if (dirName == UploadDirName)
            d->scanUploads();
    });
}

PSPackage *PSPackages::scan(const QString &filePath)
{
    YamlPackageScanner yps;
    QFileInfo fi(filePath);

    QTemporaryDir tempDir;
    if (!tempDir.isValid())
        throw Exception("could not create a temporary directory");

    QFile f(fi.absoluteFilePath());
    if (!f.open(QIODevice::ReadOnly))
        throw Exception(f, "could not open package file");
    if (f.size() > 100 * 1024 * 1024) // 100MiB
        throw Exception(f, "package file is too large");

    QByteArray magic = f.peek(2);
    if (magic != "\x1f\x8b") // .gz
        throw Exception(f, "wrong file type (%1)").arg(magic.toHex());

    QCryptographicHash hash(QCryptographicHash::Sha1);
    if (!hash.addData(&f))
        throw Exception(f, "could not read the file");
    const auto sha1 = hash.result();

    f.close();

    QString architecture;

    PackageExtractor pe(QUrl::fromLocalFile(fi.absoluteFilePath()), tempDir.path());
    pe.setFileExtractedCallback([&architecture, &pe](const QString &fileName) {
        QString fileArch = Architecture::identify(pe.destinationDirectory().filePath(fileName));

        if (!fileArch.isEmpty()) {
            if (architecture.isEmpty())
                architecture = fileArch;
            else if (architecture != fileArch)
                throw Exception("mixed architectures: %1 and %2").arg(architecture, fileArch);
        }
    });
    if (!pe.extract())
        throw Exception("could not extract package: %1").arg(pe.errorString());

    if (!d->cfg->developerVerificationCaCertificates.isEmpty()) {
        const InstallationReport &report = pe.installationReport();

        // check signatures
        if (report.developerSignature().isEmpty()) {
            throw Exception("no developer signature");
        } else {
            Signature sig(report.digest());
            if (!sig.verify(report.developerSignature(), d->cfg->developerVerificationCaCertificates))
                throw Exception("invalid developer signature (\"%1\")").arg(sig.errorString());
        }
    }

    std::unique_ptr<PackageInfo> pi { yps.scan(tempDir.filePath(u"info.yaml"_s)) };

    auto sp = new PSPackage;
    sp->id = pi->id();
    sp->sha1 = sha1;
    sp->filePath = filePath;
    sp->architecture = architecture;
    sp->packageInfo.reset(pi.release());
    QFile icon(tempDir.filePath(u"icon.png"_s));
    if ((icon.size() < 1 * 1024 * 1024) && icon.open(QIODevice::ReadOnly))
        sp->iconData = icon.readAll();

    return sp;
}

QStringList PSPackages::categories() const
{
    QStringList categories;

    for (const auto &pkg : std::as_const(d->packages)) {
        for (const PSPackage *sp : pkg)
            categories += sp->packageInfo->categories();
    }

    categories.removeDuplicates();
    categories.sort();
    return categories;
}

QList<PSPackage *> PSPackages::byArchitecture(const QString &architecture) const
{
    QList<PSPackage *> packages;

    for (const auto &pkg : std::as_const(d->packages)) {
        PSPackage *sp = pkg.value(architecture, nullptr);
        if (!sp && !architecture.isEmpty())
            sp = pkg.value({ }, nullptr);
        if (sp)
            packages.append(sp);
    }

    return packages;
}

PSPackage *PSPackages::byIdAndArchitecture(const QString &id, const QString &architecture) const
{
    auto *sp = d->packages.value(id).value(architecture, nullptr);
    if (!sp && !architecture.isEmpty())
        sp = d->packages.value(id).value({ }, nullptr);
    return sp;
}

int PSPackages::removeIf(const std::function<bool(PSPackage *)> &pred)
{
    int count = 0;

    for (auto iit = d->packages.begin(); iit != d->packages.end(); ) {
        for (auto ait = iit->begin(); ait != iit->end(); ) {
            PSPackage *sp = *ait;

            if (pred && pred(sp)) {
                colorOut() << ColorPrint::red << " - removing " << ColorPrint::bcyan << sp->id
                           << ColorPrint::reset << " [" << sp->architectureOrAll() << "]";
                QFile::remove(sp->filePath);
                delete sp;
                ait = iit->erase(ait); // clazy:exclude=strict-iterators
                ++count;
            } else {
                ++ait;
            }
        }
        if (iit->isEmpty())
            iit = d->packages.erase(iit); // clazy:exclude=strict-iterators
        else
            ++iit;
    }
    return count;
}


std::pair<PSPackages::UploadResult, PSPackage *> PSPackages::upload(const QString &filePath)
{
    std::pair<UploadResult, PSPackage *> result { UploadResult::NoChanges, nullptr };

    try {
        std::unique_ptr<PSPackage> scannedSp { scan(filePath) };

        QString id = scannedSp->id;
        QString architecture = scannedSp->architecture;

        const QString finalPath = d->cfg->dataDirectory.absoluteFilePath(PackagesDirName) + u'/'
                                  + id + u'_' + scannedSp->architectureOrAll() + u".ampkg"_s;

        auto existingSp = d->packages.value(id).value(architecture, nullptr);

        if (existingSp) {
            Q_ASSERT(existingSp->filePath == finalPath);

            if (existingSp->sha1 == scannedSp->sha1) {
                QFile::remove(filePath);
                result.second = existingSp;
                result.first = UploadResult::NoChanges;
            } else {
                QFile::remove(finalPath);
                if (!QFile::rename(filePath, finalPath))
                    throw Exception("could not rename from %1 to %2").arg(filePath, finalPath);

                scannedSp->filePath = finalPath;
                d->packages[id][architecture] = result.second = scannedSp.release();
                result.first = UploadResult::Updated;
                delete existingSp;
            }
        } else {
            if (!QFile::rename(filePath, finalPath))
                throw Exception("could not rename from %1 to %2").arg(filePath, finalPath);

            scannedSp->filePath = finalPath;
            d->packages[id][architecture] = result.second = scannedSp.release();
            result.first = UploadResult::Added;
        }

        const char *action = nullptr;
        auto color = ColorPrint::reset;
        const char *msg = "";

        switch (result.first) {
        case UploadResult::NoChanges:
            action = " = skipping ";
            color = ColorPrint::blue;
            msg = " (no changes)";
            break;
        case UploadResult::Updated:
            action = " ! updating ";
            color = ColorPrint::yellow;
            break;
        case UploadResult::Added:
            action = " + adding   ";
            color = ColorPrint::green;
            break;
        }
        colorOut() << color << action << ColorPrint::bcyan << result.second->id
                   << ColorPrint::reset << " [" << result.second->architectureOrAll() << "]"
                   << msg;

        return result;

    } catch (const Exception &e) {
        colorOut() << ColorPrint::red << " x failed   " << ColorPrint::bcyan << filePath
                   << ColorPrint::reset << " ("  << e.errorString() << ")";
        QFile::remove(filePath);
        throw;
    }
}

void PSPackages::storeSign(PSPackage *sp, const QString &hardwareId, QIODevice *destination)
{
    // extract to temp dir, store-sign and re-create at destination

    QTemporaryDir tempDir;
    if (!tempDir.isValid())
        throw Exception("could not create a temporary directory");

    PackageExtractor pe(QUrl::fromLocalFile(sp->filePath), tempDir.path());
    if (!pe.extract())
        throw Exception("could not extract package: %1").arg(pe.errorString());

    InstallationReport report = pe.installationReport();

    QByteArray sigDigest = report.digest();
    if (!hardwareId.isEmpty()) {
        sigDigest = QMessageAuthenticationCode::hash(sigDigest, hardwareId.toUtf8(),
                                                     QCryptographicHash::Sha256);
    }

    Signature sig(sigDigest);
    QByteArray signature = sig.create(d->cfg->storeSignCertificate, d->cfg->storeSignPassword.toUtf8());

    if (signature.isEmpty())
        throw Exception("could not create store signature: %1").arg(sig.errorString());
    report.setStoreSignature(signature);

    PackageCreator pc(tempDir.path(), destination, report);
    if (!pc.create())
        throw Exception("could not re-create package: %1").arg(pc.errorString());
}


///////////////////////////////////////////////////////////////////////////////////////////////////


void PSPackagesPrivate::scanRemoves()
{
    int fileCount = 0;
    QDirIterator dit(cfg->dataDirectory.absoluteFilePath(RemoveDirName), QDir::Files);
    while (dit.hasNext()) {
        if (++fileCount == 1)
            colorOut() << "> Scanning " << RemoveDirName;

        dit.next();
        QString matchStr = dit.fileName();
        if (matchStr.endsWith(u".ampkg"_s))
            matchStr.chop(6);

        int removeCount = q->removeIf([matchStr](PSPackage *sp) {
            return (sp->id == matchStr)
                   || (QString(sp->id + u'_' + sp->architectureOrAll()) == matchStr);
        });

        if (!removeCount) {
            colorOut() << ColorPrint::blue << " = skipping " << ColorPrint::bcyan << matchStr
                       << ColorPrint::reset << " (no match)";
        }

        QFile::remove(dit.filePath());
    }
}

void PSPackagesPrivate::scanUploads()
{
    int fileCount = 0;
    QDirIterator dit(cfg->dataDirectory.absoluteFilePath(UploadDirName), QDir::Files);
    while (dit.hasNext()) {
        if (++fileCount == 1)
            colorOut() << "> Scanning " << UploadDirName;

        try {
            q->upload(dit.next());
        } catch (const Exception &) { /* ignore */ }
    }
}

void PSPackagesPrivate::scanPackages()
{
    Q_ASSERT(packages.isEmpty());
    colorOut() << "> Scanning " << PackagesDirName;

    int fileCount = 0;
    QDirIterator dit(cfg->dataDirectory.absoluteFilePath(PackagesDirName), QDir::Files);
    while (dit.hasNext()) {
        ++fileCount;

        const QString filePath = dit.next();

        try {
            std::unique_ptr<PSPackage> scannedSp { q->scan(filePath) };

            QString id = scannedSp->id;
            QString architecture = scannedSp->architecture;
            QString architectureOrAll = scannedSp->architectureOrAll();

            const QString expectedFileName = id + u'_' + architectureOrAll + u".ampkg"_s;

            if (dit.fileName() != expectedFileName) {
                throw Exception("invalid package name: expected %1, found %2").arg(expectedFileName)
                    .arg(dit.fileName());
            }

            if (packages.value(id).value(architecture, nullptr)) {
                throw Exception("internal error: duplicate package, id: %1, architecture: %2")
                    .arg(id).arg(architecture);
            }

            packages[id][architecture] = scannedSp.release();

            colorOut() << ColorPrint::green << " + adding   " << ColorPrint::bcyan << id
                       << ColorPrint::reset << " [" << architectureOrAll << "]";

        } catch (const Exception &e) {
            colorOut() << ColorPrint::blue << " = skipping " << ColorPrint::bcyan << dit.fileName()
                       << ColorPrint::reset << " (" << e.errorString() << ")";
        }
    }

    if (!fileCount)
        colorOut() << " (" << ColorPrint::yellow << "none found" << ColorPrint::reset << ")";
}


///////////////////////////////////////////////////////////////////////////////////////////////////


QString PSPackage::architectureOrAll() const
{
    return architecture.isEmpty() ? u"all"_s  : architecture;
}

#include "moc_pspackages.cpp"