feat(files): confirm dropped files and refuse the ones already installed

A drop — or "Ouvrir avec PacManager" — answered nothing: a Flatpak
install walked off into the background and a plugin package landed
without a sound, so the gesture read as ignored. Both now open a
dialog saying what was taken and what it starts.

A file the machine already has is refused before anything runs, and
named. The application a file would install is read out of the file
itself (the [Flatpak Ref] section of a .flatpakref, the header of a
.flatpak bundle, the manifest of a .fwspacm) and compared with what
the provider has, so nothing is started to find out: Flatpak would
otherwise skip the install and report a success the user never got,
and a plugin package would silently replace the one in place.
This commit is contained in:
BlackAngelTV committed 2026-10-09 19:22:24 +02:00
1 parent f4aa661b54
commit 55718064a6
16 files changed
+557 -9

No files matched your search

+109 -1
View File
@@ -14,6 +14,7 @@
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <set>
#include <thread>
#include <utility>
@@ -363,6 +364,15 @@ static bool endsWith(const std::string& text, const std::string& suffix) {
&& text.compare(text.size() - suffix.size(), suffix.size(), suffix) == 0;
}
// Leading and trailing blanks removed, for the INI lines of a .flatpakref.
static std::string trimmed(const std::string& text) {
std::size_t begin = 0;
std::size_t end = text.size();
while (begin < end && std::isspace(static_cast<unsigned char>(text[begin]))) ++begin;
while (end > begin && std::isspace(static_cast<unsigned char>(text[end - 1]))) --end;
return text.substr(begin, end - begin);
}
// Quote one argument for the shell popen() runs, so a dropped file whose name
// contains spaces or quotes still reaches flatpak as a single argument.
static std::string shellQuote(const std::string& value) {
@@ -536,6 +546,88 @@ std::string FlatpakManager::repoNamespace(const std::string& path) {
return name.empty() ? "flatpak-repo" : name;
}
std::string FlatpakManager::appIdFromRef(const std::string& ref) {
const std::size_t typeEnd = ref.find('/');
if (typeEnd == std::string::npos) return {};
const std::size_t idEnd = ref.find('/', typeEnd + 1);
if (idEnd == std::string::npos) return {};
// A ref is "<kind>/<id>/<arch>/<branch>", and only the two kinds flatpak
// installs name an application here.
const std::string kind = ref.substr(0, typeEnd);
if (kind != "app" && kind != "runtime") return {};
const std::string id = ref.substr(typeEnd + 1, idEnd - typeEnd - 1);
return isAppId(id) ? id : std::string();
}
std::string FlatpakManager::appIdFromRefFile(const std::string& path) {
std::ifstream file(path);
if (!file) return {};
// A .flatpakref is an INI file whose [Flatpak Ref] section names the
// application flatpak will resolve (`Name=com.example.App`); the rest of it
// describes the remote. Only that section is read.
bool inRefSection = false;
std::string line;
while (std::getline(file, line)) {
const std::string text = trimmed(line);
if (text.empty() || text[0] == '#' || text[0] == ';') continue;
if (text[0] == '[') {
inRefSection = toLower(text) == "[flatpak ref]";
continue;
}
if (!inRefSection) continue;
const std::size_t equals = text.find('=');
if (equals == std::string::npos) continue;
if (toLower(trimmed(text.substr(0, equals))) != "name") continue;
// Refused rather than handed back when it is not an application id:
// this comes straight out of a file the user dropped, and the caller
// passes it to flatpak.
const std::string id = trimmed(text.substr(equals + 1));
return isAppId(id) ? id : std::string();
}
return {};
}
std::string FlatpakManager::appIdFromBundle(const std::string& path) {
// A bundle opens with a header of its own: the magic, then the exact ref it
// installs and the application metadata, both NUL-terminated. The header is
// all that is read — the payload behind it weighs tens of megabytes and
// nothing there would answer the question.
std::ifstream file(path, std::ios::binary);
if (!file) return {};
std::array<char, 8192> buffer{};
file.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
const std::string header(buffer.data(), static_cast<std::size_t>(file.gcount()));
const std::string magic("flatpak\0", 8);
if (header.size() <= magic.size() || header.compare(0, magic.size(), magic) != 0) {
// An OCI bundle, or a file that is not one: nothing is guessed at, so
// the caller installs it the ordinary way.
return {};
}
// The ref sits behind a "ref" key. Skipping the framing between the two —
// flatpak writes sizes there of its own choosing — is what keeps this
// readable across the versions of the format.
const std::string key("ref\0", 4);
for (std::size_t at = header.find(key, magic.size()); at != std::string::npos;
at = header.find(key, at + key.size())) {
std::size_t value = at + key.size();
while (value < header.size() && header[value] == '\0') ++value;
const std::size_t end = header.find('\0', value);
if (end == std::string::npos) break;
const std::string id = appIdFromRef(header.substr(value, end - value));
if (!id.empty()) return id;
}
return {};
}
std::string FlatpakManager::installFileCommand(FileKind kind, const std::string& path,
bool userScope) {
const std::string scope = userScope ? "--user " : "";
@@ -642,7 +734,7 @@ std::vector<PackageInfo> FlatpakManager::listUpdates() {
return results;
}
bool FlatpakManager::isInstalled(const std::string& appId) {
bool FlatpakManager::isInstalled(const std::string& appId) const {
std::string cmd = "flatpak info " + appId + " > /dev/null 2>&1";
return system(cmd.c_str()) == 0;
}
@@ -942,6 +1034,22 @@ bool FlatpakManager::canInstallFile(const std::string& path) const {
return classifyFile(path) != FileKind::None;
}
// What the user is about to install, named the way flatpak names it. A
// `.flatpakrepo` answers nothing: it registers a remote and installs no
// application, so there is nothing that could already be there.
std::string FlatpakManager::fileTargetName(const std::string& path) const {
switch (classifyFile(path)) {
case FileKind::Ref: return appIdFromRefFile(path);
case FileKind::Bundle: return appIdFromBundle(path);
default: return {};
}
}
bool FlatpakManager::isPackageInstalled(const std::string& packageName) const {
if (packageName.empty()) return false;
return isInstalled(packageName);
}
bool FlatpakManager::installFile(const std::string& path,
ProgressCallback progressCallback) {
m_lastError.clear();