fix(pacman): report database locks and refresh failures
A concurrent manager holding /var/lib/pacman/db.lck, or a partially failed `pacman -Sy`, surfaced as a generic failure with no explanation. Keep the last error, tell the user which lock is involved and how to recover, and stop treating a sync error as success. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
1 parent
f1702932cd
commit
edc3ef2925
3 files changed
+134
-3
No files matched your search
@@ -48,6 +48,19 @@ public:
|
||||
// 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);
|
||||
|
||||
// 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.
|
||||
@@ -70,4 +83,5 @@ private:
|
||||
bool isPackageInstalled(const std::string& name);
|
||||
|
||||
std::string m_password;
|
||||
std::string m_lastError;
|
||||
};
|
||||
+92
-3
@@ -7,6 +7,9 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
|
||||
// Helper to run shell commands and capture output
|
||||
static std::string exec(const char* cmd) {
|
||||
@@ -32,6 +35,26 @@ static std::string shellQuote(const std::string& s) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Keep only the last few lines of a command output for error dialogs, so a
|
||||
// failure shows the real cause without dumping pages of progress lines.
|
||||
static std::string lastOutputLines(const std::string& output, int keep = 5) {
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
std::vector<std::string> lines;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty()) lines.push_back(line);
|
||||
}
|
||||
std::string out;
|
||||
const size_t start = lines.size() > static_cast<size_t>(keep)
|
||||
? lines.size() - keep
|
||||
: 0;
|
||||
for (size_t i = start; i < lines.size(); ++i) {
|
||||
if (!out.empty()) out += "\n";
|
||||
out += lines[i];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Read a (current/Total) package counter out of a pacman line; see definition
|
||||
// below. Declared first because execPrivileged uses it.
|
||||
static int percentFromLine(const std::string& line);
|
||||
@@ -158,10 +181,16 @@ bool PacmanManager::install(const std::string& packageName,
|
||||
std::string output =
|
||||
execPrivileged("pacman -S --noconfirm " + packageName, m_password, progressCallback);
|
||||
|
||||
m_lastError.clear();
|
||||
// A wrong/expired password must not be silently reused: drop it so the
|
||||
// next attempt prompts again.
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -175,8 +204,14 @@ bool PacmanManager::remove(const std::string& packageName,
|
||||
std::string output =
|
||||
execPrivileged("pacman -R --noconfirm " + packageName, m_password, progressCallback);
|
||||
|
||||
m_lastError.clear();
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -195,8 +230,14 @@ bool PacmanManager::update(const std::string& packageName,
|
||||
m_password, progressCallback);
|
||||
}
|
||||
|
||||
m_lastError.clear();
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -265,14 +306,28 @@ bool PacmanManager::refresh(ProgressCallback progressCallback) {
|
||||
std::string output =
|
||||
execPrivileged("pacman -Sy --noconfirm", m_password, progressCallback);
|
||||
|
||||
m_lastError.clear();
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
|
||||
// `pacman -Sy` has no easy success code; treat a completed run as success
|
||||
// unless pacman itself reported an explicit error.
|
||||
return output.find("error:") == std::string::npos;
|
||||
// `pacman -Sy` exits 0 even when only some DBs synced; surface the real
|
||||
// output so a network failure is not reported as success.
|
||||
if (output.find("error:") != std::string::npos
|
||||
|| output.find("erreur") != std::string::npos
|
||||
|| output.find("failed to synchronize") != std::string::npos
|
||||
|| output.find("synchronisation") != std::string::npos) {
|
||||
m_lastError = "Échec du rafraîchissement des bases pacman.\n"
|
||||
"Détail :\n" + lastOutputLines(output);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> PacmanManager::parseInstalledOutput(const std::string& output) {
|
||||
@@ -395,6 +450,40 @@ bool PacmanManager::authFailed(const std::string& output) {
|
||||
|| output.find("Authentication failure") != std::string::npos;
|
||||
}
|
||||
|
||||
// True when pacman refused to run because its database is locked
|
||||
// (/var/lib/pacman/db.lck): another pacman/pamac/yay run is in progress, or
|
||||
// a previous run crashed without releasing the lock.
|
||||
bool PacmanManager::dbLocked(const std::string& output) {
|
||||
return output.find("could not lock database") != std::string::npos
|
||||
|| output.find("unable to lock database") != std::string::npos
|
||||
|| output.find("verrouillage de la base") != std::string::npos
|
||||
|| output.find("db.lck") != std::string::npos
|
||||
|| output.find("failed to synchronize all databases") != std::string::npos
|
||||
|| output.find("synchronisation de toutes les bases") != std::string::npos;
|
||||
}
|
||||
|
||||
// Short user-facing hint shown when the DB is locked. Points to the lock
|
||||
// file when it is stale (no other pacman running) so the user can remove
|
||||
// it; otherwise tells them to wait for the other run to finish.
|
||||
std::string PacmanManager::dbLockHint() {
|
||||
// No Qt here (this file also builds into the Qt-free unit tests):
|
||||
// a lock file that exists with no other manager running is stale.
|
||||
const bool lockExists = std::ifstream("/var/lib/pacman/db.lck").good();
|
||||
const bool otherRunning =
|
||||
system("pgrep -x pacman > /dev/null 2>&1") == 0
|
||||
|| system("pgrep -x pamac > /dev/null 2>&1") == 0
|
||||
|| system("pgrep -x yay > /dev/null 2>&1") == 0
|
||||
|| system("pgrep -x paru > /dev/null 2>&1") == 0;
|
||||
if (lockExists && !otherRunning) {
|
||||
return "La base pacman est verrouillée par un fichier résiduel "
|
||||
"(/var/lib/pacman/db.lck) alors qu'aucune autre mise à jour "
|
||||
"ne tourne. Supprimez-le avec : sudo rm /var/lib/pacman/db.lck";
|
||||
}
|
||||
return "Un autre gestionnaire de paquets (pacman, pamac, yay, octopi…) "
|
||||
"utilise déjà la base. Attendez la fin de l'autre opération puis "
|
||||
"réessayez.";
|
||||
}
|
||||
|
||||
void PacmanManager::setPassword(const std::string& password) {
|
||||
m_password = password;
|
||||
}
|
||||
|
||||
@@ -113,6 +113,29 @@ static void testAuthFailed_ignoresPacmanErrors() {
|
||||
assert(!PacmanManager::authFailed("warning: firefox is up to date"));
|
||||
}
|
||||
|
||||
static void testDbLocked_detectsEnglishLock() {
|
||||
assert(PacmanManager::dbLocked(
|
||||
"error: failed to synchronize all databases (unable to lock database)"));
|
||||
}
|
||||
|
||||
static void testDbLocked_detectsFrenchLock() {
|
||||
assert(PacmanManager::dbLocked(
|
||||
"erreur : la synchronisation de toutes les bases de données a échoué "
|
||||
"(verrouillage de la base de données impossible)"));
|
||||
}
|
||||
|
||||
static void testDbLocked_detectsLockFile() {
|
||||
assert(PacmanManager::dbLocked(
|
||||
"error: failed to init transaction (unable to lock database)\n"
|
||||
" if you're sure a package manager is not already running, you can "
|
||||
"remove /var/lib/pacman/db.lck"));
|
||||
}
|
||||
|
||||
static void testDbLocked_ignoresUnrelatedErrors() {
|
||||
assert(!PacmanManager::dbLocked("error: target not found: firefox"));
|
||||
assert(!PacmanManager::dbLocked("Mise à jour terminée"));
|
||||
}
|
||||
|
||||
int main() {
|
||||
testParseInstalledOutput_parsesSimpleLines();
|
||||
testParseInstalledOutput_keepsHyphensInNames();
|
||||
@@ -132,6 +155,11 @@ int main() {
|
||||
testAuthFailed_detectsMissingPassword();
|
||||
testAuthFailed_ignoresPacmanErrors();
|
||||
|
||||
testDbLocked_detectsEnglishLock();
|
||||
testDbLocked_detectsFrenchLock();
|
||||
testDbLocked_detectsLockFile();
|
||||
testDbLocked_ignoresUnrelatedErrors();
|
||||
|
||||
std::cout << "All pacman-manager tests passed." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user