refactor(tree): split the plugin host from the providers it loads
plugins/ held two unrelated things: the host that discovers and installs plugins, and the plugins themselves. The host moves to plugins/ with the built-in pacman provider beside it in providers/, where the external providers already live — so plugins/ is now one thing, and the name says which. The external plugin projects only change folder, not layout: providers/ holds aur-provider, flatpak-provider, example-provider and example-addon.
This commit is contained in:
15 files changed
+39
-39
No files matched your search
@@ -0,0 +1,329 @@
|
||||
#include "plugin-package.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QSaveFile>
|
||||
#include <QSettings>
|
||||
|
||||
#include "app-logging.h"
|
||||
|
||||
// The per-user plugin directory, shared with PluginManager's search paths: what
|
||||
// a package installs lands exactly where a hand-copied library would, so the
|
||||
// registry needs nothing else to pick it up.
|
||||
static const char* kUserPluginDir = "/.local/share/PacManager/plugins";
|
||||
|
||||
// The manifest a package must carry, and the name the format is versioned
|
||||
// under. A package declaring another version is refused rather than guessed at.
|
||||
static const char* kManifestName = "plugin.json";
|
||||
|
||||
PluginPackage::PluginPackage(QObject* parent) : QObject(parent) {}
|
||||
|
||||
QString PluginPackage::suffix() {
|
||||
return QStringLiteral(".fwspacm");
|
||||
}
|
||||
|
||||
bool PluginPackage::isPackagePath(const QString& path) {
|
||||
return path.trimmed().endsWith(suffix(), Qt::CaseInsensitive);
|
||||
}
|
||||
|
||||
QString PluginPackage::librarySuffix() {
|
||||
#ifdef Q_OS_WIN
|
||||
return QStringLiteral(".dll");
|
||||
#elif defined(Q_OS_MACOS)
|
||||
return QStringLiteral(".dylib");
|
||||
#else
|
||||
return QStringLiteral(".so");
|
||||
#endif
|
||||
}
|
||||
|
||||
QString PluginPackage::installDirectory() const {
|
||||
if (!m_installDirectory.isEmpty()) return m_installDirectory;
|
||||
return QDir::homePath() + QLatin1String(kUserPluginDir);
|
||||
}
|
||||
|
||||
void PluginPackage::setInstallDirectory(const QString& path) {
|
||||
m_installDirectory = path;
|
||||
}
|
||||
|
||||
QString PluginPackage::installedKey(const QString& id) {
|
||||
return QStringLiteral("packages/installed/%1").arg(id);
|
||||
}
|
||||
|
||||
QString PluginPackage::installedKeyPrefix() {
|
||||
return QStringLiteral("packages/installed/");
|
||||
}
|
||||
|
||||
bool PluginPackage::isInstalled(const QString& id) const {
|
||||
return !installedPath(id).isEmpty();
|
||||
}
|
||||
|
||||
QString PluginPackage::installedPath(const QString& id) const {
|
||||
QSettings settings;
|
||||
const QString path = settings.value(installedKey(id)).toString();
|
||||
// A file deleted behind our back is not installed any more: the state
|
||||
// follows the disk, which is also what makes an uninstall of a missing
|
||||
// library a no-op rather than a failure.
|
||||
if (path.isEmpty() || !QFile::exists(path)) return QString();
|
||||
return path;
|
||||
}
|
||||
|
||||
QStringList PluginPackage::installedIds() const {
|
||||
QSettings settings;
|
||||
QStringList ids;
|
||||
const QString prefix = installedKeyPrefix();
|
||||
// allKeys(), not childKeys(): the ids sit one level below the prefix, and
|
||||
// childKeys() would only report the "installed" group itself.
|
||||
for (const QString& key : settings.allKeys()) {
|
||||
if (!key.startsWith(prefix)) continue;
|
||||
const QString id = key.mid(prefix.size());
|
||||
if (id.isEmpty() || !isInstalled(id)) continue;
|
||||
ids << id;
|
||||
}
|
||||
ids.sort();
|
||||
return ids;
|
||||
}
|
||||
|
||||
QString PluginPackage::targetFileName(const Manifest& manifest, const ZipArchive& archive,
|
||||
QString* error) {
|
||||
auto fail = [error](const QString& message) {
|
||||
if (error) *error = message;
|
||||
return QString();
|
||||
};
|
||||
|
||||
const QString suffix = librarySuffix();
|
||||
|
||||
// A manifest naming its library is trusted, but only as far as a name that
|
||||
// is safe to write: the entry is looked up in the archive, and what lands
|
||||
// in the plugin directory is a bare file name, never a path.
|
||||
if (!manifest.library.isEmpty()) {
|
||||
if (!ZipArchive::isSafeEntryName(manifest.library)
|
||||
|| manifest.library.contains(QLatin1Char('/'))
|
||||
|| manifest.library.contains(QLatin1Char('\\'))) {
|
||||
return fail(QStringLiteral("Nom de bibliothèque refusé : %1.")
|
||||
.arg(manifest.library));
|
||||
}
|
||||
if (!archive.contains(manifest.library)) {
|
||||
return fail(QStringLiteral("L'archive ne contient pas %1.")
|
||||
.arg(manifest.library));
|
||||
}
|
||||
// The directory is scanned for shared libraries, so an entry that is
|
||||
// not one would be written and then ignored.
|
||||
if (!manifest.library.endsWith(suffix, Qt::CaseInsensitive)) {
|
||||
return fail(QStringLiteral("%1 n'est pas une bibliothèque partagée (%2).")
|
||||
.arg(manifest.library, suffix));
|
||||
}
|
||||
return manifest.library;
|
||||
}
|
||||
|
||||
// Otherwise the library is the archive's single shared-library entry. More
|
||||
// than one would be ambiguous: picking one silently is how the wrong file
|
||||
// gets installed.
|
||||
QStringList candidates;
|
||||
for (const QString& name : archive.fileNames()) {
|
||||
if (name.endsWith(suffix, Qt::CaseInsensitive)) candidates << name;
|
||||
}
|
||||
if (candidates.isEmpty()) {
|
||||
return fail(QStringLiteral("L'archive ne contient aucune bibliothèque partagée (%1).")
|
||||
.arg(suffix));
|
||||
}
|
||||
if (candidates.size() > 1) {
|
||||
return fail(QStringLiteral("L'archive contient plusieurs bibliothèques : %1. "
|
||||
"Nommez celle à installer avec \"library\".")
|
||||
.arg(candidates.join(QStringLiteral(", "))));
|
||||
}
|
||||
return candidates.first();
|
||||
}
|
||||
|
||||
PluginPackage::Manifest PluginPackage::parseManifest(const QByteArray& json,
|
||||
const ZipArchive& archive, QString* error) {
|
||||
Manifest manifest;
|
||||
|
||||
auto fail = [error](const QString& message) {
|
||||
if (error) *error = message;
|
||||
return Manifest{};
|
||||
};
|
||||
|
||||
QJsonParseError parseError{};
|
||||
const QJsonDocument document = QJsonDocument::fromJson(json, &parseError);
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
return fail(QStringLiteral("Manifeste illisible : %1").arg(parseError.errorString()));
|
||||
}
|
||||
if (!document.isObject()) {
|
||||
return fail(QStringLiteral("Manifeste illisible : objet JSON attendu."));
|
||||
}
|
||||
|
||||
const QJsonObject root = document.object();
|
||||
manifest.format = root.value(QStringLiteral("format")).toInt(0);
|
||||
manifest.id = root.value(QStringLiteral("id")).toString().trimmed();
|
||||
manifest.name = root.value(QStringLiteral("name")).toString().trimmed();
|
||||
manifest.description = root.value(QStringLiteral("description")).toString().trimmed();
|
||||
manifest.version = root.value(QStringLiteral("version")).toString().trimmed();
|
||||
manifest.author = root.value(QStringLiteral("author")).toString().trimmed();
|
||||
manifest.kind = root.value(QStringLiteral("kind")).toString().trimmed().toLower();
|
||||
manifest.library = root.value(QStringLiteral("library")).toString().trimmed();
|
||||
manifest.sha256 = root.value(QStringLiteral("sha256")).toString().trimmed().toLower();
|
||||
|
||||
// The format is versioned so a future layout can be told apart from this
|
||||
// one; guessing would mean reading a package as something it is not.
|
||||
if (manifest.format != 1) {
|
||||
return fail(QStringLiteral("Format de paquet non pris en charge (%1).")
|
||||
.arg(manifest.format));
|
||||
}
|
||||
if (manifest.id.isEmpty()) {
|
||||
return fail(QStringLiteral("Manifeste sans \"id\"."));
|
||||
}
|
||||
// The id becomes a settings key, so the characters that would need quoting
|
||||
// there (a slash among them) are refused rather than sanitised into
|
||||
// something that would later mismatch.
|
||||
static const QString kForbidden = QStringLiteral("/\\\"");
|
||||
for (const QChar c : kForbidden) {
|
||||
if (manifest.id.contains(c)) {
|
||||
return fail(QStringLiteral("Identifiant refusé : %1").arg(manifest.id));
|
||||
}
|
||||
}
|
||||
// An unstated kind stays empty: the real kind is the one the library
|
||||
// declares once the registry loads it.
|
||||
if (manifest.kind != QLatin1String("provider") && manifest.kind != QLatin1String("addon")) {
|
||||
manifest.kind.clear();
|
||||
}
|
||||
|
||||
// Refused here rather than at write time, so an unusable package is
|
||||
// reported before anything is extracted.
|
||||
QString nameError;
|
||||
const QString target = targetFileName(manifest, archive, &nameError);
|
||||
if (target.isEmpty()) return fail(nameError);
|
||||
manifest.library = target;
|
||||
|
||||
if (error) error->clear();
|
||||
return manifest;
|
||||
}
|
||||
|
||||
PluginPackage::Contents PluginPackage::read(const QString& path, QString* error) {
|
||||
auto fail = [error](const QString& message) {
|
||||
if (error) *error = message;
|
||||
return Contents{};
|
||||
};
|
||||
|
||||
ZipArchive archive;
|
||||
if (!archive.open(path, error)) return fail(error ? *error : QString());
|
||||
|
||||
// The manifest is matched case-insensitively, so a package zipped on
|
||||
// Windows (where the name may have been uppercased) still installs.
|
||||
int manifestIndex = archive.indexOf(QLatin1String(kManifestName));
|
||||
if (manifestIndex < 0) {
|
||||
for (const QString& name : archive.fileNames()) {
|
||||
if (name.compare(QLatin1String(kManifestName), Qt::CaseInsensitive) == 0) {
|
||||
manifestIndex = archive.indexOf(name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (manifestIndex < 0) {
|
||||
return fail(QStringLiteral("Paquet invalide : %1 est introuvable.")
|
||||
.arg(QLatin1String(kManifestName)));
|
||||
}
|
||||
|
||||
const QByteArray manifestJson = archive.read(archive.entries()[static_cast<size_t>(manifestIndex)].name, error);
|
||||
if (error && !error->isEmpty()) return fail(*error);
|
||||
|
||||
Manifest manifest = parseManifest(manifestJson, archive, error);
|
||||
if (error && !error->isEmpty()) return fail(*error);
|
||||
|
||||
QByteArray library = archive.read(manifest.library, error);
|
||||
if (error && !error->isEmpty()) return fail(*error);
|
||||
if (library.isEmpty()) {
|
||||
return fail(QStringLiteral("La bibliothèque %1 est vide.").arg(manifest.library));
|
||||
}
|
||||
|
||||
// The checksum is the one guarantee about *which* bytes run: a package whose
|
||||
// library does not match what its manifest announces is never written.
|
||||
if (!manifest.sha256.isEmpty()) {
|
||||
const QString digest = QString::fromLatin1(
|
||||
QCryptographicHash::hash(library, QCryptographicHash::Sha256).toHex());
|
||||
if (digest.compare(manifest.sha256, Qt::CaseInsensitive) != 0) {
|
||||
return fail(QStringLiteral("Somme de contrôle invalide (attendu %1).")
|
||||
.arg(manifest.sha256));
|
||||
}
|
||||
}
|
||||
|
||||
Contents contents;
|
||||
contents.manifest = manifest;
|
||||
contents.library = library;
|
||||
contents.libraryName = manifest.library;
|
||||
if (error) error->clear();
|
||||
return contents;
|
||||
}
|
||||
|
||||
void PluginPackage::install(const QString& path) {
|
||||
const QString trimmed = path.trimmed();
|
||||
if (trimmed.isEmpty()) {
|
||||
emit installFinished(QString(), false, QStringLiteral("Aucun paquet à installer."));
|
||||
return;
|
||||
}
|
||||
if (!isPackagePath(trimmed)) {
|
||||
emit installFinished(QFileInfo(trimmed).completeBaseName(), false,
|
||||
QStringLiteral("Ce fichier n'est pas un paquet %1.")
|
||||
.arg(suffix()));
|
||||
return;
|
||||
}
|
||||
|
||||
QString error;
|
||||
const Contents contents = read(trimmed, &error);
|
||||
if (contents.library.isEmpty()) {
|
||||
const QString id = contents.manifest.id.isEmpty()
|
||||
? QFileInfo(trimmed).completeBaseName()
|
||||
: contents.manifest.id;
|
||||
emit installFinished(id, false, error);
|
||||
return;
|
||||
}
|
||||
|
||||
const QString directory = installDirectory();
|
||||
if (!QDir().mkpath(directory)) {
|
||||
emit installFinished(contents.manifest.id, false,
|
||||
QStringLiteral("Impossible de créer %1.").arg(directory));
|
||||
return;
|
||||
}
|
||||
|
||||
const QString target = QDir(directory).filePath(contents.libraryName);
|
||||
// Written through QSaveFile so the library only ever appears in the scanned
|
||||
// directory complete, under its final name. A rejected commit (a read-only
|
||||
// home, a full disk) leaves the previous version in place rather than a
|
||||
// truncated file.
|
||||
QSaveFile file(target);
|
||||
if (!file.open(QIODevice::WriteOnly) || file.write(contents.library) != contents.library.size()
|
||||
|| !file.commit()) {
|
||||
emit installFinished(contents.manifest.id, false,
|
||||
QStringLiteral("Impossible d'écrire %1.").arg(target));
|
||||
return;
|
||||
}
|
||||
|
||||
QSettings settings;
|
||||
settings.setValue(installedKey(contents.manifest.id), target);
|
||||
qCInfo(pacmgrPlugin) << "plugin package installed:" << contents.manifest.id << target;
|
||||
emit installFinished(contents.manifest.id, true, target);
|
||||
}
|
||||
|
||||
void PluginPackage::remove(const QString& id) {
|
||||
// Only a library this class wrote is removed: a plugin the user copied into
|
||||
// the directory by hand is not ours to delete.
|
||||
const QString path = installedPath(id);
|
||||
if (path.isEmpty()) {
|
||||
emit removeFinished(id, false,
|
||||
QStringLiteral("Ce plugin n'a pas été installé depuis un paquet."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (QFile::exists(path) && !QFile::remove(path)) {
|
||||
emit removeFinished(id, false, QStringLiteral("Impossible de supprimer %1.").arg(path));
|
||||
return;
|
||||
}
|
||||
|
||||
QSettings settings;
|
||||
settings.remove(installedKey(id));
|
||||
qCInfo(pacmgrPlugin) << "plugin package removed:" << id << path;
|
||||
emit removeFinished(id, true, path);
|
||||
}
|
||||
Reference in new issue
Block a user