summaryrefslogtreecommitdiffstats
path: root/src/application-lib/packagedatabase.cpp
blob: 57a16fdf4b8f4e8dc9fd1b84b2c6fada4fb30695 (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
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QDir>
#include <QFile>
#include <QDataStream>

#include "packagedatabase.h"
#include "packageinfo.h"
#include "yamlpackagescanner.h"
#include "installationreport.h"
#include "exception.h"
#include "logging.h"
#include "configcache.h"
#include "filesystemmountwatcher.h"

#include <memory>
#include <cstdlib>

using namespace Qt::StringLiterals;

QT_BEGIN_NAMESPACE_AM

// the templated adaptor class needed to instantiate ConfigCache<PackageInfo> in parse() below
template<> class ConfigCacheAdaptor<PackageInfo>
{
public:
    PackageInfo *loadFromSource(QIODevice *source, const QString &fileName)
    {
        return YamlPackageScanner().scan(source, fileName);
    }
    PackageInfo *loadFromCache(QDataStream &ds)
    {
        return PackageInfo::readFromDataStream(ds);
    }
    void saveToCache(QDataStream &ds, const PackageInfo *pi)
    {
        pi->writeToDataStream(ds);
    }

    void preProcessSourceContent(QByteArray &, const QString &) { }
    void merge(PackageInfo *, const PackageInfo *) { }
};


PackageDatabase::PackageDatabase(const QStringList &builtInPackagesDirs,
                                 const QString &installedPackagesDir, const QString &installedPackagesMountPoint)
    : m_builtInPackagesDirs(builtInPackagesDirs)
    , m_installedPackagesDir(installedPackagesDir)
    , m_installedPackagesMountPoint(installedPackagesMountPoint)
{
    qCDebug(LogInstaller) << "Loading built-in apps from:" << m_builtInPackagesDirs;
    qCDebug(LogInstaller) << "Loading installed apps from:" << m_installedPackagesDir;
}

PackageDatabase::PackageDatabase(const QString &singlePackagePath)
    : m_singlePackagePath(singlePackagePath)
{
    Q_ASSERT(!singlePackagePath.isEmpty());
}

PackageDatabase::~PackageDatabase()
{
    qDeleteAll(m_builtInPackages);
    qDeleteAll(m_installedPackages);
}

QString PackageDatabase::installedPackagesDir() const
{
    return m_installedPackagesDir;
}

void PackageDatabase::enableLoadFromCache()
{
    if (m_parsed)
        qCWarning(LogSystem) << "PackageDatabase cannot change the caching mode after the initial load";
    m_loadFromCache = true;
}

void PackageDatabase::enableSaveToCache()
{
    if (m_parsed)
        qCWarning(LogSystem) << "PackageDatabase cannot change the caching mode after the initial load";
    m_saveToCache = true;
}

bool PackageDatabase::builtInHasRemovableUpdate(PackageInfo *packageInfo) const
{
    if (!packageInfo || packageInfo->isBuiltIn() || !m_installedPackages.contains(packageInfo))
        return false;
    for (const auto *pi : m_builtInPackages) {
        if (pi->id() == packageInfo->id())
            return true;
    }
    return false;
}

QStringList PackageDatabase::findManifestsInDir(const QDir &manifestDir, bool scanningBuiltInApps)
{
    QStringList files;

    auto flags = scanningBuiltInApps ? QDir::Dirs | QDir::NoDotAndDotDot
                                     : QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks;
    const QDir baseDir(manifestDir);
    const QStringList pkgDirNames = baseDir.entryList(flags);

    for (const QString &pkgDirName : pkgDirNames) {
        try {
            // ignore left-overs from the installer
            if (pkgDirName.endsWith(u'+') || pkgDirName.endsWith(u'-'))
                continue;

            // ignore filesystem problems
            QDir pkgDir = baseDir.absoluteFilePath(pkgDirName);
            if (!pkgDir.exists())
                continue;

            // ignore directory names with weird/forbidden characters
            QString pkgIdError;
            if (!PackageInfo::isValidApplicationId(pkgDirName, &pkgIdError))
                throw Exception("not a valid package-id: %1").arg(pkgIdError);

            if (!pkgDir.exists(u"info.yaml"_s))
                throw Exception("couldn't find an info.yaml manifest");
            if (!scanningBuiltInApps && !pkgDir.exists(u".installation-report.yaml"_s))
                throw Exception("found a non-built-in package without an installation report");

            QString manifestPath = pkgDir.absoluteFilePath(u"info.yaml"_s);
            files << manifestPath;

        } catch (const Exception &e) {
            qCDebug(LogSystem) << "Ignoring package" << pkgDirName << ":" << e.what();
        }
    }
    return files;
}

void PackageDatabase::parse(PackageLocations packageLocations)
{
    if (m_parsed)
        throw Exception("PackageDatabase::parse() has been called multiple times");
    m_parsed = true;

    if (!m_singlePackagePath.isEmpty()) {
        try {
            m_builtInPackages.append(PackageInfo::fromManifest(m_singlePackagePath));
        } catch (const Exception &e) {
            throw Exception("Failed to load manifest for package: %1").arg(e.errorString());
        }
        m_parsedPackageLocations = Builtin | Installed;
    } else {
        AbstractConfigCache::Options cacheOptions = AbstractConfigCache::IgnoreBroken;
        if (!m_loadFromCache)
            cacheOptions |= AbstractConfigCache::ClearCache;
        if (!m_loadFromCache && !m_saveToCache)
            cacheOptions |= AbstractConfigCache::NoCache;

        if ((packageLocations & Builtin) && !(m_parsedPackageLocations & Builtin)) {
            QStringList manifestFiles;
            for (const QString &dir : std::as_const(m_builtInPackagesDirs))
                manifestFiles << findManifestsInDir(dir, true);

            ConfigCache<PackageInfo> cache(manifestFiles, u"appdb-builtin"_s, { 'P','K','G','B' },
                                           PackageInfo::dataStreamVersion(), cacheOptions);
            cache.parse();

            for (int i = 0; i < manifestFiles.size(); ++i) {
                QString manifestFile = manifestFiles.at(i);
                QDir pkgDir = QFileInfo(manifestFile).dir();
                std::unique_ptr<PackageInfo> pkg(cache.takeResult(i));

                if (!pkg) { // the YAML file was not parseable and we ignore broken manifests
                    qCWarning(LogSystem) << "The file" << manifestFile << "is not a valid manifest YAML"
                                            " file and will be ignored.";
                    continue;
                }

                if (pkg->id() != pkgDir.dirName()) {
                    throw Exception("an info.yaml for packages must be in a directory that has"
                                    " the same name as the package's id: found id '%1' in directory '%2'")
                        .arg(pkg->id(), pkgDir.path());
                }
                pkg->setBuiltIn(true);
                m_builtInPackages.append(pkg.release());
            }
            m_parsedPackageLocations |= Builtin;
        }
        if ((packageLocations & Installed) && !(m_parsedPackageLocations & Installed)) {
            if (m_installedPackagesDir.isEmpty()) {
                m_parsedPackageLocations |= Installed;
            } else {
                if (!m_installedPackagesMountPoint.isEmpty()) {
                    if (!m_installedPackagesMountWatcher) {
                        m_installedPackagesMountWatcher = new FileSystemMountWatcher(this);
                        connect(m_installedPackagesMountWatcher, &FileSystemMountWatcher::mountChanged,
                                this, [this](const QString &mountPoint, const QString &device) {
                            if (mountPoint == m_installedPackagesMountPoint && !device.isEmpty()) {
                                if (!(m_parsedPackageLocations & Installed)) {
                                    // we are not in main() anymore: we can't just throw

                                    try {
                                        parseInstalled();
                                    } catch (const Exception &e) {
                                        qCCritical(LogInstaller) << "Failed to parse the package meta-data after the device"
                                                                 << device << "was mounted onto" << mountPoint << ":"
                                                                 << e.what();
                                        std::abort(); // there is no qCFatal()
                                    }
                                    emit installedPackagesParsed();
                                }
                                m_installedPackagesMountWatcher->deleteLater();
                                m_installedPackagesMountWatcher = nullptr;
                            }
                        });
                        m_installedPackagesMountWatcher->addMountPoint(m_installedPackagesMountPoint);
                        if (m_installedPackagesMountWatcher->currentMountPoints().contains(m_installedPackagesMountPoint)) {
                            // we don't need the watcher, but we had to set it up to avoid a race condition
                            delete m_installedPackagesMountWatcher;
                            m_installedPackagesMountWatcher = nullptr;
                        }
                    }
                }

                // scan immediately, if we don't have to wait for the mountpoint
                if (!m_installedPackagesMountWatcher)
                    parseInstalled();
            }
        }
    }
}

PackageDatabase::PackageLocations PackageDatabase::parsedPackageLocations() const
{
    return m_parsedPackageLocations;
}

void PackageDatabase::parseInstalled()
{
    Q_ASSERT(m_parsed && !(m_parsedPackageLocations & Installed));

    QStringList manifestFiles = findManifestsInDir(m_installedPackagesDir, false);

    AbstractConfigCache::Options cacheOptions = AbstractConfigCache::IgnoreBroken;
    if (!m_loadFromCache)
        cacheOptions |= AbstractConfigCache::ClearCache;
    if (!m_loadFromCache && !m_saveToCache)
        cacheOptions |= AbstractConfigCache::NoCache;

    ConfigCache<PackageInfo> cache(manifestFiles, u"appdb-installed"_s, { 'P','K','G','I' },
                                   PackageInfo::dataStreamVersion(), cacheOptions);
    cache.parse();

    for (int i = 0; i < manifestFiles.size(); ++i) {
        QString manifestFile = manifestFiles.at(i);
        QDir pkgDir = QFileInfo(manifestFile).dir();

        try {
            std::unique_ptr<PackageInfo> pkg(cache.takeResult(i));

            if (!pkg) { // the YAML file was not parseable and we ignore broken manifests
                qCWarning(LogSystem) << "The file" << manifestFile << "is not a valid manifest YAML"
                                        " file and will be ignored.";
                continue;
            }

            if (pkg->id() != pkgDir.dirName()) {
                throw Exception("an info.yaml for packages must be in a directory that has"
                                " the same name as the package's id: found id '%1' in directory '%2'")
                    .arg(pkg->id(), pkgDir.path());
            }

            QFile f(pkgDir.absoluteFilePath(u".installation-report.yaml"_s));
            if (!f.open(QFile::ReadOnly))
                throw Exception(f, "failed to open the installation report");

            auto report = std::make_unique<InstallationReport>(pkg->id());
            try {
                report->deserialize(&f);
            } catch (const Exception &e) {
                throw Exception("Failed to deserialize the installation report %1: %2")
                        .arg(f.fileName()).arg(e.errorString());
            }

            pkg->setInstallationReport(report.release());
            pkg->setBaseDir(pkgDir.path());
            m_installedPackages.append(pkg.release());

        } catch (const Exception &e) {
            qCWarning(LogInstaller) << "Ignoring broken package at" << pkgDir.absolutePath()
                                    << ":" << e.what();
        }
    }
    m_parsedPackageLocations |= Installed;
}

void PackageDatabase::addPackageInfo(PackageInfo *package)
{
    m_installedPackages.append(package);
}

void PackageDatabase::removePackageInfo(PackageInfo *package)
{
    if (m_installedPackages.removeAll(package))
        delete package;
}

QVector<PackageInfo *> PackageDatabase::installedPackages() const
{
    return m_installedPackages;
}

QVector<PackageInfo *> PackageDatabase::builtInPackages() const
{
    return m_builtInPackages;
}

QT_END_NAMESPACE_AM

#include "moc_packagedatabase.cpp"