feat(providers): add flatpak provider
Implement IPackageManager over the flatpak CLI: search, installed list, updates, install/remove/update and appstream refresh. Runs per-user so it needs no sudo. Parsing is exposed as statics so it can be unit-tested without the flatpak binary. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
3 files changed
+283
No files matched your search
@@ -0,0 +1,230 @@
|
||||
#include "flatpak-manager.h"
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <cctype>
|
||||
|
||||
// Helper to run shell commands and capture output.
|
||||
static std::string exec(const char* cmd) {
|
||||
std::array<char, 256> 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;
|
||||
}
|
||||
|
||||
// Stream the lines of a command's output to the progress callback, if any.
|
||||
static void streamOutput(const std::string& output,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (!progressCallback) return;
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty()) progressCallback(line);
|
||||
}
|
||||
}
|
||||
|
||||
// A flatpak application ID is a dotted identifier with at least two dots whose
|
||||
// segments start with an alphanumeric character (e.g. org.mozilla.firefox).
|
||||
// This deliberately rejects version strings such as "128.0.2".
|
||||
static bool isAppId(const std::string& token) {
|
||||
int dots = 0;
|
||||
bool first = true;
|
||||
for (size_t i = 0; i < token.size(); ++i) {
|
||||
char c = token[i];
|
||||
if (c == '.') {
|
||||
++dots;
|
||||
first = true; // next segment must start with an alnum again
|
||||
} else if (std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_') {
|
||||
if (first && !std::isalnum(static_cast<unsigned char>(c))) return false;
|
||||
first = false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return dots >= 2 && !first;
|
||||
}
|
||||
|
||||
// Split a flatpak --columns line. flatpak emits tab-separated fields with that
|
||||
// flag; fall back to whitespace splitting when tabs are absent.
|
||||
static std::vector<std::string> splitColumns(const std::string& line) {
|
||||
std::vector<std::string> fields;
|
||||
std::istringstream ss(line);
|
||||
std::string field;
|
||||
if (line.find('\t') != std::string::npos) {
|
||||
while (std::getline(ss, field, '\t')) {
|
||||
if (!field.empty()) fields.push_back(field);
|
||||
}
|
||||
} else {
|
||||
while (ss >> field) fields.push_back(field);
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
// `flatpak search` prints one aligned row per app:
|
||||
// Name Description Application ID Version Branch Remotes
|
||||
// The description may contain spaces, so the application ID is located by
|
||||
// shape (dotted identifier) and everything between Name and it is joined back.
|
||||
std::vector<PackageInfo> FlatpakManager::parseSearchOutput(const std::string& output) {
|
||||
std::vector<PackageInfo> results;
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
|
||||
bool firstLine = true;
|
||||
while (std::getline(iss, line)) {
|
||||
if (firstLine) {
|
||||
firstLine = false;
|
||||
continue; // Skip the column header.
|
||||
}
|
||||
if (line.empty()) continue;
|
||||
|
||||
std::vector<std::string> tokens = splitColumns(line);
|
||||
if (tokens.empty()) continue;
|
||||
|
||||
int appIdx = -1;
|
||||
for (int i = 1; i < static_cast<int>(tokens.size()); ++i) {
|
||||
if (isAppId(tokens[i])) {
|
||||
appIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (appIdx == -1) continue; // Not an application row.
|
||||
|
||||
PackageInfo pkg;
|
||||
// Use the application ID as the package name so install/remove/update
|
||||
// work directly with the CLI; the human-readable name is kept at the
|
||||
// front of the description (e.g. "Firefox — Fast and privacy-aware").
|
||||
pkg.name = tokens[appIdx];
|
||||
pkg.version = (appIdx + 1 < static_cast<int>(tokens.size())) ? tokens[appIdx + 1] : "";
|
||||
pkg.repository = tokens.back();
|
||||
|
||||
for (int i = 1; i < appIdx; ++i) {
|
||||
if (!pkg.description.empty()) pkg.description += " ";
|
||||
pkg.description += tokens[i];
|
||||
}
|
||||
if (!tokens[0].empty() && tokens[0] != pkg.name) {
|
||||
pkg.description = tokens[0] + " — " + pkg.description;
|
||||
}
|
||||
results.push_back(pkg);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> FlatpakManager::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::vector<std::string> fields = splitColumns(line);
|
||||
if (fields.empty()) continue;
|
||||
|
||||
PackageInfo pkg;
|
||||
pkg.name = fields[0];
|
||||
if (fields.size() > 1) pkg.version = fields[1];
|
||||
if (fields.size() > 2) pkg.repository = fields[2];
|
||||
pkg.installed = true;
|
||||
results.push_back(pkg);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> FlatpakManager::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;
|
||||
|
||||
std::vector<std::string> fields = splitColumns(line);
|
||||
if (fields.empty()) continue;
|
||||
|
||||
PackageInfo pkg;
|
||||
pkg.name = fields[0];
|
||||
if (fields.size() > 1) pkg.version = fields[1];
|
||||
pkg.installed = true;
|
||||
pkg.updateAvailable = true;
|
||||
results.push_back(pkg);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> FlatpakManager::search(const std::string& query) {
|
||||
std::string cmd = "flatpak search " + query + " 2>&1";
|
||||
return parseSearchOutput(exec(cmd.c_str()));
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> FlatpakManager::listInstalled() {
|
||||
return parseInstalledOutput(
|
||||
exec("flatpak list --app --columns=application,version,origin 2>&1"));
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> FlatpakManager::listUpdates() {
|
||||
return parseUpdatesOutput(
|
||||
exec("flatpak remote-ls --updates --columns=application,version 2>&1"));
|
||||
}
|
||||
|
||||
bool FlatpakManager::isInstalled(const std::string& appId) {
|
||||
std::string cmd = "flatpak info " + appId + " > /dev/null 2>&1";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool FlatpakManager::install(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Installation de " + packageName + "...");
|
||||
|
||||
std::string output = exec(
|
||||
("flatpak install --noninteractive --assumeyes " + packageName + " 2>&1").c_str());
|
||||
streamOutput(output, progressCallback);
|
||||
|
||||
// Any "error:" line from flatpak means the install failed.
|
||||
return output.find("error:") == std::string::npos && isInstalled(packageName);
|
||||
}
|
||||
|
||||
bool FlatpakManager::remove(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Suppression de " + packageName + "...");
|
||||
|
||||
std::string output = exec(
|
||||
("flatpak uninstall --noninteractive --assumeyes " + packageName + " 2>&1").c_str());
|
||||
streamOutput(output, progressCallback);
|
||||
|
||||
return output.find("error:") == std::string::npos && !isInstalled(packageName);
|
||||
}
|
||||
|
||||
bool FlatpakManager::update(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
std::string output;
|
||||
if (packageName.empty()) {
|
||||
if (progressCallback) progressCallback("Mise à jour de toutes les applications...");
|
||||
output = exec("flatpak update --noninteractive --assumeyes 2>&1");
|
||||
} else {
|
||||
if (progressCallback) progressCallback("Mise à jour de " + packageName + "...");
|
||||
output = exec(
|
||||
("flatpak update --noninteractive --assumeyes " + packageName + " 2>&1").c_str());
|
||||
}
|
||||
streamOutput(output, progressCallback);
|
||||
|
||||
return output.find("error:") == std::string::npos;
|
||||
}
|
||||
|
||||
bool FlatpakManager::refresh(std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Rafraîchissement des métadonnées Flatpak...");
|
||||
|
||||
// Re-download the appstream metadata of every remote so search and
|
||||
// update detection reflect the latest published versions.
|
||||
std::string output = exec("flatpak update --appstream 2>&1");
|
||||
streamOutput(output, progressCallback);
|
||||
|
||||
return output.find("error:") == std::string::npos;
|
||||
}
|
||||
Reference in new issue
Block a user