#include "aur-manager.h" #include "progress-tracker.h" #include #include #include #include #include #include // Helper to run shell commands and capture output. static std::string exec(const char* cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd, "r"), pclose); if (!pipe) throw std::runtime_error("popen() failed"); while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } return result; } // Keep the tail of a command's output so the user sees the real failure. static std::string lastOutputLines(const std::string& output, int maxLines = 4) { std::istringstream iss(output); std::string line; std::vector lines; while (std::getline(iss, line)) { if (!line.empty()) lines.push_back(line); } std::string out; const size_t start = lines.size() > static_cast(maxLines) ? lines.size() - maxLines : 0; for (size_t i = start; i < lines.size(); ++i) { if (!out.empty()) out += "\n"; out += lines[i]; } return out; } std::string AurManager::helper() { // yay first (most common), paru as fallback. if (system("command -v yay > /dev/null 2>&1") == 0) return "yay"; if (system("command -v paru > /dev/null 2>&1") == 0) return "paru"; return {}; } std::vector AurManager::parseSearchOutput(const std::string& output) { std::vector results; std::istringstream iss(stripAnsi(output)); std::string header, desc; while (std::getline(iss, header)) { if (header.empty()) continue; if (!std::getline(iss, desc)) desc.clear(); // Header looks like "aur/name version [installed]". size_t slash = header.find('/'); if (slash == std::string::npos) continue; std::string rest = header.substr(slash + 1); std::istringstream ls(rest); std::string name, version; if (!(ls >> name >> version)) continue; PackageInfo pkg; pkg.name = name; pkg.version = version; pkg.description = desc; pkg.repository = "aur"; pkg.installed = header.find("[installed") != std::string::npos || header.find("(Installed") != std::string::npos; results.push_back(std::move(pkg)); } return results; } std::vector AurManager::parseInstalledOutput(const std::string& output) { std::vector results; std::istringstream iss(output); std::string line; while (std::getline(iss, line)) { if (line.empty()) continue; std::istringstream ls(line); std::string name, version; if (!(ls >> name >> version)) continue; PackageInfo pkg; pkg.name = name; pkg.version = version; pkg.repository = "aur"; pkg.installed = true; results.push_back(std::move(pkg)); } return results; } std::vector AurManager::parseUpdatesOutput(const std::string& output) { std::vector results; std::istringstream iss(output); std::string line; while (std::getline(iss, line)) { if (line.empty()) continue; // Format: "name old-version -> new-version". std::istringstream ls(line); std::string name, oldVer, arrow, newVer; if (!(ls >> name >> oldVer)) continue; ls >> arrow >> newVer; PackageInfo pkg; pkg.name = name; pkg.version = newVer.empty() ? oldVer : newVer; pkg.repository = "aur"; pkg.installed = true; pkg.updateAvailable = true; results.push_back(std::move(pkg)); } return results; } std::vector AurManager::search(const std::string& query) { const std::string h = helper(); if (h.empty()) return {}; // --color=never keeps the output parseable. return parseSearchOutput(exec((h + " -Ss --color=never " + query + " 2>&1").c_str())); } std::vector AurManager::listInstalled() { const std::string h = helper(); if (h.empty()) return {}; // -Qm lists foreign (AUR) packages only, so official repo packages stay // owned by the pacman provider and rows are not duplicated. return parseInstalledOutput(exec((h + " -Qm 2>&1").c_str())); } std::vector AurManager::listUpdates() { const std::string h = helper(); if (h.empty()) return {}; return parseUpdatesOutput(exec((h + " -Qua 2>&1").c_str())); } bool AurManager::install(const std::string& packageName, ProgressCallback progressCallback) { m_lastError.clear(); const std::string h = helper(); if (h.empty()) { m_lastError = "Aucun helper AUR (yay/paru) installe."; return false; } if (progressCallback) progressCallback("Installation AUR de " + packageName + "...", -1); // Helpers handle sudo themselves; run so the prompt reaches the user. The // output is streamed: the helpers build with makepkg and then hand over to // pacman, and both report progress as they go. std::string output = runWithProgress( h + " -S --noconfirm " + packageName + " 2>&1", progressCallback); if (system((h + " -Q " + packageName + " > /dev/null 2>&1").c_str()) != 0) { m_lastError = lastOutputLines(output); return false; } return true; } bool AurManager::remove(const std::string& packageName, ProgressCallback progressCallback) { m_lastError.clear(); const std::string h = helper(); if (h.empty()) { m_lastError = "Aucun helper AUR (yay/paru) installe."; return false; } if (progressCallback) progressCallback("Suppression de " + packageName + "...", -1); std::string output = runWithProgress( h + " -R --noconfirm " + packageName + " 2>&1", progressCallback); if (system((h + " -Q " + packageName + " > /dev/null 2>&1").c_str()) == 0) { m_lastError = lastOutputLines(output); return false; } return true; } bool AurManager::update(const std::string& packageName, ProgressCallback progressCallback) { m_lastError.clear(); const std::string h = helper(); if (h.empty()) { m_lastError = "Aucun helper AUR (yay/paru) installe."; return false; } std::string target = packageName.empty() ? "--aur" : packageName; if (progressCallback) progressCallback("Mise a jour AUR...", -1); std::string output = runWithProgress( h + " -Syu --noconfirm " + target + " 2>&1", progressCallback); if (output.find("error:") != std::string::npos || output.find("erreur") != std::string::npos) { m_lastError = lastOutputLines(output); return false; } return true; } bool AurManager::refresh(ProgressCallback progressCallback) { m_lastError.clear(); const std::string h = helper(); if (h.empty()) { m_lastError = "Aucun helper AUR (yay/paru) installe."; return false; } if (progressCallback) progressCallback("Rafraichissement AUR...", -1); std::string output = runWithProgress(h + " -Sy 2>&1", progressCallback); if (output.find("error:") != std::string::npos || output.find("erreur") != std::string::npos) { m_lastError = lastOutputLines(output); return false; } return true; }