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
+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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user