Files
FWS-PacManage/plugins/aur-provider/aur-manager.cpp
T
BlackAngelTV f1fbd3cd8a feat(flatpak): report the download flatpak never announces
Installing a .flatpakref looked frozen: flatpak prints two lines and
then stays silent for the minutes a runtime takes to come down, so the
window repeated the same sentence over a stalled bar and the journal
recorded nothing at all.

It has no meter to print — measured here, on a real 658 MB install, it
draws nothing on a pipe nor on a full terminal, only on the deltas the
CDN happens to serve — so the provider now measures the transfer itself:
the bytes landing in flatpak's own staging directory, reported as they
grow. When even those stop, the window takes over and says which step is
running and for how long, in the status bar and in the journal.

Along the way the runner reads with read() instead of stdio so in-place
updates are not held back by a buffer, strips the ANSI escapes some
tools colour their output with (the AUR plugin's own copy is gone), and
turns a rewritten meter line into a journal entry every 10 %.
2026-09-27 21:33:41 +02:00

214 lines
7.4 KiB
C++

#include "aur-manager.h"
#include "progress-tracker.h"
#include <array>
#include <memory>
#include <stdexcept>
#include <sstream>
#include <cstdlib>
#include <vector>
// Helper to run shell commands and capture output.
static std::string exec(const char* cmd) {
std::array<char, 8192> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> 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<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>(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<PackageInfo> AurManager::parseSearchOutput(const std::string& output) {
std::vector<PackageInfo> 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<PackageInfo> AurManager::parseInstalledOutput(const std::string& output) {
std::vector<PackageInfo> 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<PackageInfo> AurManager::parseUpdatesOutput(const std::string& output) {
std::vector<PackageInfo> 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<PackageInfo> 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<PackageInfo> 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<PackageInfo> 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;
}