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
@@ -1,191 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "package-manager.h"
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
|
||||
// Native Arch Linux package manager, integrated directly into the application
|
||||
class PacmanManager : public IPackageManager {
|
||||
public:
|
||||
std::string getName() const override { return "pacman"; }
|
||||
std::string getVersion() const override { return "1.0.0"; }
|
||||
std::string getDescription() const override {
|
||||
return "Gestionnaire de paquets natif d'Arch Linux";
|
||||
}
|
||||
|
||||
bool canSearch() const override { return true; }
|
||||
bool canInstall() const override { return true; }
|
||||
bool canRemove() const override { return true; }
|
||||
bool canUpdate() const override { return true; }
|
||||
bool canListInstalled() const override { return true; }
|
||||
|
||||
bool isAvailable() const override {
|
||||
return system("which pacman > /dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
// pacman operations run through sudo and need the session password.
|
||||
bool requiresPassword() const override { return true; }
|
||||
|
||||
// Parse `pacman -Q` output (one "name version" line per package).
|
||||
// Exposed as static so the parsing can be unit-tested without invoking pacman.
|
||||
static std::vector<PackageInfo> parseInstalledOutput(const std::string& output);
|
||||
|
||||
// Parse `pacman -Q` output into a set of installed package names, used to
|
||||
// mark search results as installed without one subprocess per result.
|
||||
// Exposed as static so the parsing can be unit-tested without invoking pacman.
|
||||
static std::set<std::string> parseInstalledNames(const std::string& output);
|
||||
|
||||
// Parse `pacman -Qi` output (one block per package) into a name -> installed size map.
|
||||
// Exposed as static so the parsing can be unit-tested without invoking pacman.
|
||||
static std::map<std::string, std::string> parseInstalledSizes(const std::string& output);
|
||||
|
||||
// Parse `pacman -Sl` output ("repo name version" per line) into a name -> repository map.
|
||||
// Exposed as static so the parsing can be unit-tested without invoking pacman.
|
||||
static std::map<std::string, std::string> parseSyncRepos(const std::string& output);
|
||||
|
||||
// Merge `pacman -Qdt` (orphans) and `pacman -Qm` (foreign) output into
|
||||
// cleanup candidates tagged with their reason: orphans are safe to
|
||||
// bulk-remove, foreign packages are listed for review only since nothing
|
||||
// proves they are unused. Exposed as static so it can be unit-tested
|
||||
// without invoking pacman.
|
||||
static std::vector<PackageInfo> parseCleanupOutput(const std::string& orphans,
|
||||
const std::string& foreign);
|
||||
|
||||
// `pacman -Rns` command removing every given package together with the
|
||||
// dependencies it dragged in, or empty when the list is empty. Exposed as
|
||||
// static so the command can be unit-tested without invoking pacman.
|
||||
static std::string removePackagesCommand(const std::vector<std::string>& packages);
|
||||
|
||||
// Command that trims the package cache, given whether paccache
|
||||
// (pacman-contrib) is installed on this system. Exposed as static so both
|
||||
// branches can be unit-tested without invoking pacman.
|
||||
static std::string cleanCacheCommand(bool paccacheAvailable);
|
||||
|
||||
// Detect a sudo authentication failure (wrong or missing password) in the
|
||||
// output of a privileged command. Exposed as static so it can be
|
||||
// unit-tested without invoking sudo.
|
||||
static bool authFailed(const std::string& output);
|
||||
|
||||
// Detect a locked pacman database (db.lck) in the output, either because
|
||||
// another manager runs concurrently or because a stale lock file remains.
|
||||
// Exposed as static so it can be unit-tested without invoking pacman.
|
||||
static bool dbLocked(const std::string& output);
|
||||
|
||||
// What currently keeps pacman's database locked. The UI uses it to name the
|
||||
// process in its dialog and offer to close it, instead of telling the user
|
||||
// to wait for a manager it cannot identify.
|
||||
struct LockHolder {
|
||||
bool lockFilePresent = false; // /var/lib/pacman/db.lck exists
|
||||
int pid = 0; // process holding it (0 when none found)
|
||||
std::string name; // process name, when known
|
||||
bool stale = false; // lock file present, but nobody holds it
|
||||
};
|
||||
|
||||
// Pid of the process currently keeping `path` open, or 0 when nobody does.
|
||||
// Detection walks /proc, which names the process even when it is not one of
|
||||
// the managers we know about. Exposed so it can be unit-tested against a
|
||||
// temporary file instead of pacman's real lock.
|
||||
static int lockHolderPid(const std::string& path);
|
||||
|
||||
// Inspect pacman's lock file and its holder. lockHolderPid() answers for a
|
||||
// manager run by the user; the known managers are looked up with pgrep as a
|
||||
// fallback, since /proc/<pid>/fd is not readable when they run as root.
|
||||
static LockHolder detectLockHolder();
|
||||
|
||||
// Human-readable description of what holds the database ("pamac-manager
|
||||
// (PID 4321)", "un verrou résiduel..."), empty when nothing does.
|
||||
static std::string describeLockHolder(const LockHolder& holder);
|
||||
|
||||
// True when pacman's database is free, i.e. no lock file is present.
|
||||
static bool databaseIsFree();
|
||||
|
||||
// Waits up to timeoutMs for the database to become free, so a manager that
|
||||
// is finishing right now does not turn into an error dialog.
|
||||
static bool waitForDatabase(int timeoutMs);
|
||||
|
||||
// True when the last failed operation failed on a locked database, so the UI
|
||||
// can offer the recovery without matching the (translated) message.
|
||||
bool lastErrorWasLock() const { return m_lastErrorWasLock; }
|
||||
|
||||
// Stops the process holding the lock — or removes a stale lock file — and
|
||||
// waits for the database to become free. SIGTERM comes first so pacman can
|
||||
// abort its transaction and release the lock itself; SIGKILL follows when it
|
||||
// does not react. Returns false, with lastError() filled, when the lock
|
||||
// outlives the attempt.
|
||||
bool releaseDatabaseLock(const LockHolder& holder,
|
||||
ProgressCallback progressCallback = nullptr);
|
||||
|
||||
// User-facing hint explaining the lock and how to recover. Checks the
|
||||
// lock file and other running managers on the live system.
|
||||
static std::string dbLockHint();
|
||||
|
||||
// Last error detail of the most recent failed privileged operation
|
||||
// (e.g. db lock hint), surfaced in the UI instead of a generic message.
|
||||
std::string lastError() const override { return m_lastError; }
|
||||
|
||||
// Credential handling for privileged (sudo) operations. The password is
|
||||
// kept in memory only for the session (never persisted) and piped to
|
||||
// `sudo -S` for each privileged command.
|
||||
void setPassword(const std::string& password);
|
||||
void clearPassword();
|
||||
bool hasPassword() const;
|
||||
|
||||
std::vector<PackageInfo> search(const std::string& query) override;
|
||||
bool install(const std::string& packageName,
|
||||
ProgressCallback progressCallback) override;
|
||||
bool remove(const std::string& packageName,
|
||||
ProgressCallback progressCallback) override;
|
||||
bool update(const std::string& packageName = "",
|
||||
ProgressCallback progressCallback = nullptr) override;
|
||||
std::vector<PackageInfo> listInstalled() override;
|
||||
std::vector<PackageInfo> listUpdates() override;
|
||||
bool refresh(ProgressCallback progressCallback = nullptr) override;
|
||||
|
||||
// Cleanup: pacman is the one provider that knows about packages no longer
|
||||
// required by anything (-Qdt) and about legacy installs coming from
|
||||
// outside the sync databases (-Qm).
|
||||
bool canListCleanup() const override { return true; }
|
||||
bool canCleanCache() const override { return true; }
|
||||
std::vector<PackageInfo> listCleanupCandidates() override;
|
||||
bool removePackages(const std::vector<std::string>& packages,
|
||||
ProgressCallback progressCallback = nullptr) override;
|
||||
bool cleanCache(ProgressCallback progressCallback = nullptr) override;
|
||||
|
||||
private:
|
||||
bool isPackageInstalled(const std::string& name);
|
||||
|
||||
// Run a one-off command (kill, rm) and return its exit status, through sudo
|
||||
// with the session password when asked. Used by the lock recovery, which
|
||||
// needs the status rather than the output.
|
||||
int runCommand(const std::string& command, bool withSudo) const;
|
||||
bool terminateProcess(int pid, const char* signal) const;
|
||||
bool removeLockFile() const;
|
||||
|
||||
// Cache for the read-only queries several views ask for in a row: a search
|
||||
// runs `pacman -Q`, the Installed page runs `-Q`, `-Qi` and `-Sl`, and the
|
||||
// dashboard re-runs them on every visit. Without it that is a dozen
|
||||
// subprocess spawns for a single click. The window is short enough that a
|
||||
// package installed outside the app still shows up almost immediately, and
|
||||
// every mutating operation drops the cache outright.
|
||||
struct CachedQuery {
|
||||
std::string output;
|
||||
std::chrono::steady_clock::time_point fetched{};
|
||||
bool valid = false;
|
||||
};
|
||||
|
||||
std::string cachedQuery(CachedQuery& slot, const char* command);
|
||||
void invalidateQueries();
|
||||
|
||||
std::mutex m_queryMutex;
|
||||
CachedQuery m_installedQuery; // pacman -Q
|
||||
CachedQuery m_installedDetailsQuery; // pacman -Qi
|
||||
CachedQuery m_syncReposQuery; // pacman -Sl
|
||||
CachedQuery m_updatesQuery; // pacman -Qu
|
||||
|
||||
std::string m_password;
|
||||
std::string m_lastError;
|
||||
// Set with m_lastError when the failure was a locked database.
|
||||
bool m_lastErrorWasLock = false;
|
||||
};
|
||||
@@ -1,73 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "provider-plugin.h"
|
||||
#include <QSettings>
|
||||
#include <QLibrary>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Registry of every plugin known to the application. It owns each plugin
|
||||
// (built-in and external), loads shared-library plugins dropped in the plugin
|
||||
// directories at runtime (see PluginManager::scanDirectory) and tracks which
|
||||
// are enabled in the settings. A plugin is either a package-manager provider
|
||||
// (implements IPackageManager) or a generic addon (implements IPlugin) — both
|
||||
// kinds share the same loading, lifecycle and enable/disable flow.
|
||||
class PluginManager {
|
||||
public:
|
||||
struct PluginEntry {
|
||||
PluginInfo info;
|
||||
IPlugin* plugin = nullptr; // non-null for addon plugins
|
||||
IPackageManager* manager = nullptr; // non-null for provider plugins
|
||||
QLibrary* library = nullptr; // owned by the PluginManager; null for built-ins
|
||||
};
|
||||
|
||||
PluginManager();
|
||||
~PluginManager();
|
||||
|
||||
// Register a provider compiled into the application (e.g. pacman).
|
||||
// Takes ownership of the manager.
|
||||
void registerBuiltin(PluginInfo info, IPackageManager* manager);
|
||||
|
||||
// Scan the plugin directories for external plugins and load them.
|
||||
void discoverExternal();
|
||||
|
||||
// Drop external plugins and rescan their directories; built-in plugins
|
||||
// are kept (their instances stay valid).
|
||||
void reload();
|
||||
|
||||
// Re-run isAvailable() on every plugin (e.g. after the app installed a
|
||||
// missing tool) so the Plugins page reflects reality.
|
||||
void refreshAvailability();
|
||||
|
||||
// All known plugins, in registration order.
|
||||
const std::vector<PluginEntry>& plugins() const;
|
||||
|
||||
// Provider plugins that are enabled (in settings) and whose tool is on the
|
||||
// system.
|
||||
std::vector<const PluginEntry*> availableProviders() const;
|
||||
|
||||
// Same as availableProviders() but as plain manager pointers, convenient
|
||||
// for running aggregated operations across every provider.
|
||||
std::vector<IPackageManager*> availableManagers() const;
|
||||
|
||||
// A single available+enabled provider matching id, or nullptr.
|
||||
const PluginEntry* findAvailable(const std::string& id) const;
|
||||
|
||||
// Addon plugins that are enabled and available (e.g. to hand the main
|
||||
// window to via IPlugin::onUiReady).
|
||||
std::vector<IPlugin*> availableAddons() const;
|
||||
|
||||
bool isEnabled(const std::string& id) const;
|
||||
void setEnabled(const std::string& id, bool enabled);
|
||||
|
||||
// Manager for a known provider id (enabled or not), or nullptr.
|
||||
IPackageManager* managerFor(const std::string& id) const;
|
||||
|
||||
private:
|
||||
bool loadPluginFile(const QString& filePath);
|
||||
void scanDirectory(const QString& dir);
|
||||
void unloadExternal();
|
||||
|
||||
std::vector<PluginEntry> m_plugins;
|
||||
QSettings m_settings;
|
||||
};
|
||||
@@ -1,127 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "zip-archive.h"
|
||||
|
||||
// A plugin package (.fwspacm): a ZIP archive holding a plugin and the manifest
|
||||
// describing it.
|
||||
//
|
||||
// The package is built by a plugin author with the script at the repo root
|
||||
// (make-plugin-package.py) and installed by dropping the file anywhere on the
|
||||
// window. There is no catalogue and no network: what the user installs is a
|
||||
// file they already have, which is the whole point of the format.
|
||||
//
|
||||
// Layout of the archive:
|
||||
//
|
||||
// plugin.json the manifest (required)
|
||||
// <library> the shared library (.so / .dll / .dylib)
|
||||
//
|
||||
// The manifest names what the plugin is, so the application can tell the user
|
||||
// what it is about to run before it is written anywhere:
|
||||
//
|
||||
// { "format": 1, "id": "flatpak", "name": "Flatpak", "kind": "provider",
|
||||
// "version": "1.0.0", "author": "FWS", "description": "…",
|
||||
// "library": "libflatpak-provider.so", "sha256": "…" }
|
||||
//
|
||||
// Installing a package runs third-party code inside this process, so the
|
||||
// reader stays deliberately strict: the archive has to be a ZIP, the manifest
|
||||
// has to be a well-formed object, the library has to be one of the entries, its
|
||||
// name has to be safe to write, and its checksum — when the manifest announces
|
||||
// one — has to match before anything lands in the plugin directory. The library
|
||||
// is written through QSaveFile, so it appears there complete, under its final
|
||||
// name, and never half-installed.
|
||||
class PluginPackage : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
// What the manifest declares about a plugin. Everything but the id is
|
||||
// there for the user: the plugin itself still reports its own identity
|
||||
// through the C ABI once loaded, and the registry stays the authority on
|
||||
// what is actually installed.
|
||||
struct Manifest {
|
||||
int format = 0;
|
||||
QString id;
|
||||
QString name;
|
||||
QString description;
|
||||
QString version;
|
||||
QString author;
|
||||
QString kind; // "provider" or "addon"; empty when unstated
|
||||
QString library; // file name to write in the plugin directory
|
||||
QString sha256; // optional checksum of that library
|
||||
};
|
||||
|
||||
// Outcome of reading a package, before anything is written: what the
|
||||
// manifest says and which library it points at.
|
||||
struct Contents {
|
||||
Manifest manifest;
|
||||
QByteArray library;
|
||||
QString libraryName; // name the library is written under
|
||||
};
|
||||
|
||||
explicit PluginPackage(QObject* parent = nullptr);
|
||||
|
||||
// ---- Format ----
|
||||
// The extension a package file carries.
|
||||
static QString suffix();
|
||||
// Whether a path looks like a package. Used by the window to accept a drop
|
||||
// without having to open the file first.
|
||||
static bool isPackagePath(const QString& path);
|
||||
|
||||
// ---- Installation ----
|
||||
// Directory packages are installed into (created on demand). Defaults to the
|
||||
// per-user plugin directory PluginManager already scans; overridable so the
|
||||
// tests never touch the user's real one.
|
||||
QString installDirectory() const;
|
||||
void setInstallDirectory(const QString& path);
|
||||
|
||||
// Installs a package, then emits installFinished(). The file is only
|
||||
// written once the manifest, the library and the checksum have all been
|
||||
// accepted, so a refused package leaves the plugin directory untouched.
|
||||
void install(const QString& path);
|
||||
// Removes a package this class installed, and emits removeFinished(). A
|
||||
// library the user copied in by hand is never touched.
|
||||
void remove(const QString& id);
|
||||
|
||||
// True when a package with that id was installed here and its file is
|
||||
// still on disk.
|
||||
bool isInstalled(const QString& id) const;
|
||||
// Absolute path of the library installed for that id (empty when none).
|
||||
QString installedPath(const QString& id) const;
|
||||
// Every id installed from a package, for the page that lists them.
|
||||
QStringList installedIds() const;
|
||||
|
||||
// Reads a package without writing anything: `error` is filled (and the
|
||||
// result empty) when the file is not a usable package. Pure logic, so it is
|
||||
// unit-tested without touching the disk.
|
||||
static Contents read(const QString& path, QString* error);
|
||||
|
||||
// Parses a manifest. `error` is filled when the document is not a manifest
|
||||
// or when it lacks what an install needs (an id, and a library the archive
|
||||
// actually contains).
|
||||
static Manifest parseManifest(const QByteArray& json, const ZipArchive& archive,
|
||||
QString* error);
|
||||
|
||||
// The name a library entry is written under in the plugin directory. An
|
||||
// entry the manifest does not name is found by its library suffix, so a
|
||||
// package does not have to agree with the script on the name.
|
||||
static QString targetFileName(const Manifest& manifest, const ZipArchive& archive,
|
||||
QString* error);
|
||||
|
||||
// The platform's shared-library suffix.
|
||||
static QString librarySuffix();
|
||||
|
||||
signals:
|
||||
void installFinished(const QString& id, bool success, const QString& message);
|
||||
void removeFinished(const QString& id, bool success, const QString& message);
|
||||
|
||||
private:
|
||||
// Settings key remembering the library installed for a package id.
|
||||
static QString installedKey(const QString& id);
|
||||
// Settings key prefix listing every installed package.
|
||||
static QString installedKeyPrefix();
|
||||
|
||||
QString m_installDirectory; // empty = the per-user plugin directory
|
||||
};
|
||||
Reference in new issue
Block a user