File indexing completed on 2024-05-19 05:29:03

0001 /*
0002  *   SPDX-FileCopyrightText: 2017 Jan Grulich <jgrulich@redhat.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "FlatpakFetchDataJob.h"
0008 #include "FlatpakResource.h"
0009 
0010 namespace FlatpakRunnables
0011 {
0012 FlatpakRemoteRef *findRemoteRef(FlatpakResource *app, GCancellable *cancellable)
0013 {
0014     if (app->origin().isEmpty()) {
0015         qWarning("Failed to get metadata file because of missing origin");
0016         return nullptr;
0017     }
0018 
0019     g_autoptr(GError) localError = nullptr;
0020     const auto kind = app->resourceType() == FlatpakResource::DesktopApp ? FLATPAK_REF_KIND_APP : FLATPAK_REF_KIND_RUNTIME;
0021     const QByteArray origin = app->origin().toUtf8(), name = app->flatpakName().toUtf8(), arch = app->arch().toUtf8(), branch = app->branch().toUtf8();
0022     auto ret = flatpak_installation_fetch_remote_ref_sync_full(app->installation(),
0023                                                                origin.constData(),
0024                                                                kind,
0025                                                                name.constData(),
0026                                                                arch.constData(),
0027                                                                branch.constData(),
0028                                                                FLATPAK_QUERY_FLAGS_ONLY_CACHED,
0029                                                                cancellable,
0030                                                                &localError);
0031     if (localError) {
0032         qWarning() << "Failed to find remote ref:" << localError->message;
0033     }
0034     return ret;
0035 }
0036 
0037 QByteArray fetchMetadata(FlatpakResource *app, GCancellable *cancellable)
0038 {
0039     FlatpakRemoteRef *remoteRef = findRemoteRef(app, cancellable);
0040     if (!remoteRef) {
0041         if (!g_cancellable_is_cancelled(cancellable)) {
0042             qDebug() << "failed to find the remote" << app->name();
0043         }
0044 
0045         return {};
0046     }
0047 
0048     g_autoptr(GBytes) data = flatpak_remote_ref_get_metadata(remoteRef);
0049     gsize len = 0;
0050     auto buff = g_bytes_get_data(data, &len);
0051     const QByteArray metadataContent((const char *)buff, len);
0052 
0053     if (metadataContent.isEmpty()) {
0054         qWarning() << "Failed to get metadata file: empty metadata";
0055         return {};
0056     }
0057     return metadataContent;
0058 }
0059 
0060 }