image-collection: Add the new FlatpakImageCollection class

The new FlatpakImageCollection class represents a set of
FlatpakImageSource loaded from the image sideload repository.
This commit is contained in:
Sebastian Wick 2025-08-28 13:21:13 +02:00
parent 9551f7d402
commit feb2220a46
5 changed files with 213 additions and 6 deletions

View File

@ -52,11 +52,12 @@ typedef enum {
FLATPAK_RUN_FLAG_CLEAR_ENV = (1 << 22),
} FlatpakRunFlags;
typedef struct FlatpakDir FlatpakDir;
typedef struct FlatpakDeploy FlatpakDeploy;
typedef struct _FlatpakImageSource FlatpakImageSource;
typedef struct FlatpakOciRegistry FlatpakOciRegistry;
typedef struct _FlatpakOciManifest FlatpakOciManifest;
typedef struct _FlatpakOciImage FlatpakOciImage;
typedef struct FlatpakDir FlatpakDir;
typedef struct FlatpakDeploy FlatpakDeploy;
typedef struct FlatpakImageCollection FlatpakImageCollection;
typedef struct _FlatpakImageSource FlatpakImageSource;
typedef struct FlatpakOciRegistry FlatpakOciRegistry;
typedef struct _FlatpakOciManifest FlatpakOciManifest;
typedef struct _FlatpakOciImage FlatpakOciImage;
#endif /* __FLATPAK_COMMON_TYPES_H__ */

View File

@ -0,0 +1,50 @@
/*
* Copyright © 2024 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Owen Taylor <otaylor@redhat.com>
*/
#ifndef __FLATPAK_IMAGE_COLLECTION_H__
#define __FLATPAK_IMAGE_COLLECTION_H__
#include <glib.h>
#include <gio/gio.h>
#include <flatpak-common-types-private.h>
#include <flatpak-image-source-private.h>
#define FLATPAK_TYPE_IMAGE_COLLECTION flatpak_image_collection_get_type ()
#define FLATPAK_IMAGE_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FLATPAK_TYPE_IMAGE_COLLECTION, FlatpakImageCollection))
#define FLATPAK_IS_IMAGE_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FLATPAK_TYPE_IMAGE_COLLECTION))
GType flatpak_image_collection_get_type (void);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakImageCollection, g_object_unref)
FlatpakImageCollection *flatpak_image_collection_new (const char *location,
GCancellable *cancellable,
GError **error);
FlatpakImageSource *flatpak_image_collection_lookup_ref (FlatpakImageCollection *self,
const char *ref);
FlatpakImageSource *flatpak_image_collection_lookup_digest (FlatpakImageCollection *self,
const char *digest);
GPtrArray *flatpak_image_collection_get_sources (FlatpakImageCollection *self);
#endif /* __FLATPAK_IMAGE_COLLECTION_H__ */

View File

@ -0,0 +1,154 @@
/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
* Copyright © 2024 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Owen Taylor <otaylor@redhat.com>
*/
#include <glib/gi18n-lib.h>
#include "flatpak-image-collection-private.h"
#include "flatpak-oci-registry-private.h"
struct FlatpakImageCollection
{
GObject parent;
GPtrArray *sources;
};
typedef struct
{
GObjectClass parent_class;
} FlatpakImageCollectionClass;
G_DEFINE_TYPE (FlatpakImageCollection, flatpak_image_collection, G_TYPE_OBJECT)
static void
flatpak_image_collection_finalize (GObject *object)
{
FlatpakImageCollection *self = FLATPAK_IMAGE_COLLECTION (object);
g_ptr_array_free (self->sources, TRUE);
G_OBJECT_CLASS (flatpak_image_collection_parent_class)->finalize (object);
}
static void
flatpak_image_collection_class_init (FlatpakImageCollectionClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = flatpak_image_collection_finalize;
}
static void
flatpak_image_collection_init (FlatpakImageCollection *self)
{
self->sources = g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
}
FlatpakImageCollection *
flatpak_image_collection_new (const char *location,
GCancellable *cancellable,
GError **error)
{
g_autoptr(FlatpakImageCollection) self = NULL;
g_autoptr(FlatpakOciRegistry) registry = NULL;
g_autoptr(FlatpakOciIndex) index = NULL;
gsize i;
self = g_object_new (FLATPAK_TYPE_IMAGE_COLLECTION, NULL);
if (g_str_has_prefix (location, "oci:"))
{
g_autoptr(GFile) dir = g_file_new_for_path (location + 4);
g_autofree char *uri = g_file_get_uri (dir);
registry = flatpak_oci_registry_new (uri, FALSE, -1, cancellable, error);
if (registry == NULL)
return NULL;
}
else if (g_str_has_prefix (location, "oci-archive:"))
{
g_autoptr(GFile) file = g_file_new_for_path (location + 12);
registry = flatpak_oci_registry_new_for_archive (file, cancellable, error);
if (registry == NULL)
return NULL;
}
else
{
flatpak_fail (error, "Can't parse image collection location %s", location);
return NULL;
}
index = flatpak_oci_registry_load_index (registry, cancellable, error);
if (index == NULL)
return NULL;
for (i = 0; index->manifests[i] != NULL; i++)
{
g_autoptr(GError) local_error = NULL;
FlatpakOciManifestDescriptor *descriptor = index->manifests[i];
g_autoptr(FlatpakImageSource) image_source = flatpak_image_source_new (registry, NULL,
descriptor->parent.digest,
cancellable, &local_error);
if (image_source == NULL)
{
g_info ("Can't load manifest in image collection: %s", local_error->message);
continue;
}
g_ptr_array_add (self->sources, g_steal_pointer (&image_source));
}
return g_steal_pointer (&self);
}
FlatpakImageSource *
flatpak_image_collection_lookup_ref (FlatpakImageCollection *self,
const char *ref)
{
for (guint i = 0; i < self->sources->len; i++)
{
FlatpakImageSource *source = g_ptr_array_index (self->sources, i);
if (strcmp (flatpak_image_source_get_ref (source), ref) == 0)
return g_object_ref (source);
}
return NULL;
}
FlatpakImageSource *
flatpak_image_collection_lookup_digest (FlatpakImageCollection *self,
const char *digest)
{
for (guint i = 0; i < self->sources->len; i++)
{
FlatpakImageSource *source = g_ptr_array_index (self->sources, i);
if (strcmp (flatpak_image_source_get_digest (source), digest) == 0)
return g_object_ref (source);
}
return NULL;
}
GPtrArray *
flatpak_image_collection_get_sources (FlatpakImageCollection *self)
{
return g_ptr_array_ref (self->sources);
}

View File

@ -190,6 +190,7 @@ sources = [
'flatpak-error.c',
'flatpak-exports.c',
'flatpak-glib-backports.c',
'flatpak-image-collection.c',
'flatpak-image-source.c',
'flatpak-installation.c',
'flatpak-installed-ref.c',

View File

@ -46,6 +46,7 @@ libflatpak_doc = gnome.gtkdoc(
'flatpak-document-dbus-generated.h',
'flatpak-enum-types.h',
'flatpak-exports-private.h',
'flatpak-image-collection-private.h',
'flatpak-image-source-private.h',
'flatpak-installed-ref-private.h',
'flatpak-json-oci-private.h',