pacman restarts its "(1/24)" counter at every phase of a transaction, so the bar was fed 0 percent, 100 percent, 0 percent again a few times per update — and Flatpak/AUR buffered their whole output, which left it indeterminate for the entire run. A shared ProgressTracker now folds every counter, file percentage and package step into one value that only moves forward, the providers stream their output as it is produced, and each provider owns a slice of the bar so a multi-provider update keeps advancing. pacman -Qu is cached like the other read queries, so the sidebar badge, the updates screen and the update button share a single process. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
192 lines
9.3 KiB
C++
192 lines
9.3 KiB
C++
#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;
|
|
};
|