Commit 8a778717 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

flatpak: Allow falling back to the appstream id claimed by flatpak

If we couldn't find it anyway, see what flatpak is telling us in its own
metadata.
parent a3d93b54
......@@ -47,6 +47,7 @@
#include <QRegularExpression>
#include <glib.h>
#include <optional>
#include <sys/stat.h>
DISCOVER_BACKEND_PLUGIN(FlatpakBackend)
......@@ -182,6 +183,42 @@ static FlatpakResource::Id idForInstalledRef(FlatpakInstalledRef *ref, const QSt
return {appId, branch, arch};
}
static std::optional<AppStream::Metadata> metadataFromBytes(GBytes *appstreamGz, GCancellable *cancellable)
{
g_autoptr(GError) localError = nullptr;
g_autoptr(GZlibDecompressor) decompressor = nullptr;
g_autoptr(GInputStream) streamGz = nullptr;
g_autoptr(GInputStream) streamData = nullptr;
g_autoptr(GBytes) appstream = nullptr;
/* decompress data */
decompressor = g_zlib_decompressor_new(G_ZLIB_COMPRESSOR_FORMAT_GZIP);
streamGz = g_memory_input_stream_new_from_bytes(appstreamGz);
if (!streamGz) {
return {};
}
streamData = g_converter_input_stream_new(streamGz, G_CONVERTER(decompressor));
appstream = g_input_stream_read_bytes(streamData, 0x100000, cancellable, &localError);
if (!appstream) {
qWarning() << "Failed to extract appstream metadata from bundle:" << localError->message;
return {};
}
gsize len = 0;
gconstpointer data = g_bytes_get_data(appstream, &len);
AppStream::Metadata metadata;
metadata.setFormatStyle(AppStream::Metadata::FormatStyleCollection);
AppStream::Metadata::MetadataError error = metadata.parse(QString::fromUtf8((char *)data, len), AppStream::Metadata::FormatKindXml);
if (error != AppStream::Metadata::MetadataErrorNoError) {
qWarning() << "Failed to parse appstream metadata: " << error;
return {};
}
return metadata;
}
FlatpakBackend::FlatpakBackend(QObject *parent)
: AbstractResourcesBackend(parent)
, m_updater(new StandardBackendUpdater(this))
......@@ -383,6 +420,20 @@ FlatpakResource *FlatpakBackend::getAppForInstalledRef(FlatpakInstallation *inst
});
}
if (comps.isEmpty()) {
g_autoptr(GBytes) metadata = flatpak_installed_ref_load_appdata(ref, 0, 0);
auto meta = metadataFromBytes(metadata, m_cancellable);
const auto componentsProvided = meta->components();
if (!componentsProvided.isEmpty() && name != componentsProvided.constFirst().id()) {
qDebug() << "mismatch between flatpak and appstream" << name << componentsProvided.constFirst().id();
comps = source->m_pool->componentsById(componentsProvided.constFirst().id());
}
if (comps.isEmpty()) {
comps = componentsProvided;
}
}
if (comps.count() >= 1) {
Q_ASSERT(comps.count() == 1);
cid = comps.constFirst();
......@@ -488,38 +539,12 @@ void FlatpakBackend::addAppFromFlatpakBundle(const QUrl &url, ResultsStream *str
appstreamGz = flatpak_bundle_ref_get_appstream(bundleRef);
if (appstreamGz) {
g_autoptr(GZlibDecompressor) decompressor = nullptr;
g_autoptr(GInputStream) streamGz = nullptr;
g_autoptr(GInputStream) streamData = nullptr;
g_autoptr(GBytes) appstream = nullptr;
/* decompress data */
decompressor = g_zlib_decompressor_new(G_ZLIB_COMPRESSOR_FORMAT_GZIP);
streamGz = g_memory_input_stream_new_from_bytes(appstreamGz);
if (!streamGz) {
return;
}
streamData = g_converter_input_stream_new(streamGz, G_CONVERTER(decompressor));
appstream = g_input_stream_read_bytes(streamData, 0x100000, m_cancellable, &localError);
if (!appstream) {
qWarning() << "Failed to extract appstream metadata from bundle:" << localError->message;
return;
}
gsize len = 0;
gconstpointer data = g_bytes_get_data(appstream, &len);
AppStream::Metadata metadata;
metadata.setFormatStyle(AppStream::Metadata::FormatStyleCollection);
AppStream::Metadata::MetadataError error = metadata.parse(QString::fromUtf8((char *)data, len), AppStream::Metadata::FormatKindXml);
if (error != AppStream::Metadata::MetadataErrorNoError) {
qWarning() << "Failed to parse appstream metadata: " << error;
const auto metadata = metadataFromBytes(appstreamGz, m_cancellable);
if (!metadata.has_value()) {
return;
}
const QList<AppStream::Component> components = metadata.components();
const QList<AppStream::Component> components = metadata->components();
if (components.size()) {
asComponent = components.first();
} else {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment