image-source: Replace flatpak_oci_parse_commit_labels with getters

Instead of having one function with a pile of out arguments in
arbitrary order, add getters to FlatpakImageSource.
This commit is contained in:
Owen W. Taylor
2024-10-28 12:35:33 -04:00
committed by Sebastian Wick
parent 59ad08e78c
commit 5950438ca7
7 changed files with 125 additions and 127 deletions

View File

@@ -59,30 +59,22 @@ import_oci (OstreeRepo *repo, GFile *file,
GCancellable *cancellable, GError **error)
{
g_autofree char *commit_checksum = NULL;
g_autofree char *target_ref = NULL;
g_autoptr(FlatpakImageSource) image_source = NULL;
GHashTable *labels;
const char *ref;
image_source = flatpak_image_source_new_local (file, opt_ref, cancellable, error);
if (image_source == NULL)
return NULL;
labels = flatpak_image_source_get_labels (image_source);
flatpak_oci_parse_commit_labels (labels, NULL, NULL, NULL,
&target_ref, NULL, NULL, NULL);
if (target_ref == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"The OCI image didn't specify a ref, use --ref to specify one");
return NULL;
}
ref = flatpak_image_source_get_ref (image_source);
commit_checksum = flatpak_pull_from_oci (repo, image_source, NULL,
NULL, target_ref, FLATPAK_PULL_FLAGS_NONE, NULL, NULL, cancellable, error);
NULL, ref, FLATPAK_PULL_FLAGS_NONE,
NULL, NULL, cancellable, error);
if (commit_checksum == NULL)
return NULL;
g_print (_("Importing %s (%s)\n"), target_ref, commit_checksum);
g_print (_("Importing %s (%s)\n"), ref, commit_checksum);
return g_strdup (commit_checksum);
}

View File

@@ -1092,12 +1092,7 @@ flatpak_remote_state_fetch_commit_object_oci (FlatpakRemoteState *self,
VarRefInfoRef latest_rev_info;
VarMetadataRef metadata;
const char *oci_repository = NULL;
GHashTable *labels;
g_autofree char *subject = NULL;
g_autofree char *body = NULL;
g_autofree char *manifest_ref = NULL;
g_autofree char *parent = NULL;
guint64 timestamp = 0;
const char *parent = NULL;
g_autoptr(GVariantBuilder) metadata_builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
g_autoptr(GVariant) metadata_v = NULL;
@@ -1122,30 +1117,26 @@ flatpak_remote_state_fetch_commit_object_oci (FlatpakRemoteState *self,
if (image_source == NULL)
return NULL;
labels = flatpak_image_source_get_labels (image_source);
if (labels)
flatpak_oci_parse_commit_labels (labels, &timestamp,
&subject, &body,
&manifest_ref, NULL, &parent,
metadata_builder);
if (g_strcmp0 (manifest_ref, ref) != 0)
if (g_strcmp0 (flatpak_image_source_get_ref (image_source), ref) != 0)
{
flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA, _("Commit has no requested ref %s in ref binding metadata"), ref);
return NULL;
}
flatpak_image_source_build_commit_metadata (image_source, metadata_builder);
metadata_v = g_variant_ref_sink (g_variant_builder_end (metadata_builder));
parent = flatpak_image_source_get_parent_commit (image_source);
/* This isn't going to be exactly the same as the reconstructed one from the pull, because we don't have the contents, but its useful to get metadata */
return
g_variant_ref_sink (g_variant_new ("(@a{sv}@ay@a(say)sst@ay@ay)",
metadata_v,
parent ? ostree_checksum_to_bytes_v (parent) : g_variant_new_from_data (G_VARIANT_TYPE ("ay"), NULL, 0, FALSE, NULL, NULL),
g_variant_new_array (G_VARIANT_TYPE ("(say)"), NULL, 0),
subject, body,
GUINT64_TO_BE (timestamp),
flatpak_image_source_get_commit_subject (image_source),
flatpak_image_source_get_commit_body (image_source),
GUINT64_TO_BE (flatpak_image_source_get_commit_timestamp (image_source)),
ostree_checksum_to_bytes_v ("0000000000000000000000000000000000000000000000000000000000000000"),
ostree_checksum_to_bytes_v ("0000000000000000000000000000000000000000000000000000000000000000")));
}

View File

@@ -52,6 +52,15 @@ FlatpakOciManifest *flatpak_image_source_get_manifest (FlatpakImageSource
size_t flatpak_image_source_get_manifest_size (FlatpakImageSource *self);
FlatpakOciImage *flatpak_image_source_get_image_config (FlatpakImageSource *self);
GHashTable *flatpak_image_source_get_labels (FlatpakImageSource *self);
const char *flatpak_image_source_get_ref (FlatpakImageSource *self);
const char *flatpak_image_source_get_metadata (FlatpakImageSource *self);
const char *flatpak_image_source_get_commit (FlatpakImageSource *self);
const char *flatpak_image_source_get_parent_commit (FlatpakImageSource *self);
guint64 flatpak_image_source_get_commit_timestamp (FlatpakImageSource *self);
const char *flatpak_image_source_get_commit_subject (FlatpakImageSource *self);
const char *flatpak_image_source_get_commit_body (FlatpakImageSource *self);
void flatpak_image_source_build_commit_metadata (FlatpakImageSource *self,
GVariantBuilder *metadata_builder);
#endif /* __FLATPAK_IMAGE_SOURCE_H__ */

View File

@@ -74,7 +74,6 @@ flatpak_image_source_new (FlatpakOciRegistry *registry,
{
g_autoptr(FlatpakImageSource) self = NULL;
g_autoptr(FlatpakOciVersioned) versioned = NULL;
GHashTable *labels;
self = g_object_new (FLATPAK_TYPE_IMAGE_SOURCE, NULL);
self->registry = g_object_ref (registry);
@@ -110,8 +109,7 @@ flatpak_image_source_new (FlatpakOciRegistry *registry,
if (self->image_config == NULL)
return NULL;
labels = flatpak_image_source_get_labels (self);
if (!g_hash_table_contains (labels, "org.flatpak.ref"))
if (flatpak_image_source_get_ref (self) == NULL)
{
flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA, _("No org.flatpak.ref found in image"));
return NULL;
@@ -223,8 +221,94 @@ flatpak_image_source_get_image_config (FlatpakImageSource *self)
return self->image_config;
}
GHashTable *
flatpak_image_source_get_labels (FlatpakImageSource *self)
const char *
flatpak_image_source_get_ref (FlatpakImageSource *self)
{
return flatpak_oci_image_get_labels (self->image_config);
GHashTable *labels = flatpak_oci_image_get_labels (self->image_config);
return g_hash_table_lookup (labels, "org.flatpak.ref");
}
const char *
flatpak_image_source_get_metadata (FlatpakImageSource *self)
{
GHashTable *labels = flatpak_oci_image_get_labels (self->image_config);
return g_hash_table_lookup (labels, "org.flatpak.metadata");
}
const char *
flatpak_image_source_get_commit (FlatpakImageSource *self)
{
GHashTable *labels = flatpak_oci_image_get_labels (self->image_config);
return g_hash_table_lookup (labels, "org.flatpak.commit");
}
const char *
flatpak_image_source_get_parent_commit (FlatpakImageSource *self)
{
GHashTable *labels = flatpak_oci_image_get_labels (self->image_config);
return g_hash_table_lookup (labels, "org.flatpak.parent-commit");
}
guint64
flatpak_image_source_get_commit_timestamp (FlatpakImageSource *self)
{
GHashTable *labels = flatpak_oci_image_get_labels (self->image_config);
const char *oci_timestamp = g_hash_table_lookup (labels, "org.flatpak.timestamp");
if (oci_timestamp != NULL)
return g_ascii_strtoull (oci_timestamp, NULL, 10);
else
return 0;
}
const char *
flatpak_image_source_get_commit_subject (FlatpakImageSource *self)
{
GHashTable *labels = flatpak_oci_image_get_labels (self->image_config);
return g_hash_table_lookup (labels, "org.flatpak.subject");
}
const char *
flatpak_image_source_get_commit_body (FlatpakImageSource *self)
{
GHashTable *labels = flatpak_oci_image_get_labels (self->image_config);
return g_hash_table_lookup (labels, "org.flatpak.body");
}
void
flatpak_image_source_build_commit_metadata (FlatpakImageSource *self,
GVariantBuilder *metadata_builder)
{
GHashTable *labels = flatpak_oci_image_get_labels (self->image_config);
GHashTableIter iter;
const char *key;
const char *value;
g_hash_table_iter_init (&iter, labels);
while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&value))
{
g_autoptr(GVariant) data = NULL;
uint8_t *bin;
size_t bin_len;
if (!g_str_has_prefix (key, "org.flatpak.commit-metadata."))
continue;
key += strlen ("org.flatpak.commit-metadata.");
bin = g_base64_decode (value, &bin_len);
data = g_variant_ref_sink (g_variant_new_from_data (G_VARIANT_TYPE ("v"),
bin,
bin_len,
FALSE,
g_free,
bin));
g_variant_builder_add (metadata_builder, "{s@v}", key, data);
}
}

View File

@@ -246,14 +246,6 @@ void flatpak_oci_add_labels_for_commit (GHashTable *labels,
const char *ref,
const char *commit,
GVariant *commit_data);
void flatpak_oci_parse_commit_labels (GHashTable *labels,
guint64 *out_timestamp,
char **out_subject,
char **out_body,
char **out_ref,
char **out_commit,
char **out_parent_commit,
GVariantBuilder *metadata_builder);
#define FLATPAK_TYPE_OCI_SIGNATURE flatpak_oci_signature_get_type ()
G_DECLARE_FINAL_TYPE (FlatpakOciSignature, flatpak_oci_signature, FLATPAK, OCI_SIGNATURE, FlatpakJson)

View File

@@ -927,72 +927,6 @@ flatpak_oci_add_labels_for_commit (GHashTable *labels,
}
}
void
flatpak_oci_parse_commit_labels (GHashTable *labels,
guint64 *out_timestamp,
char **out_subject,
char **out_body,
char **out_ref,
char **out_commit,
char **out_parent_commit,
GVariantBuilder *metadata_builder)
{
const char *oci_timestamp, *oci_subject, *oci_body, *oci_parent_commit, *oci_commit, *oci_ref;
GHashTableIter iter;
gpointer _key, _value;
oci_ref = g_hash_table_lookup (labels, "org.flatpak.ref");
/* Early return if this is not a flatpak manifest */
if (oci_ref == NULL)
return;
if (oci_ref != NULL && out_ref != NULL && *out_ref == NULL)
*out_ref = g_strdup (oci_ref);
oci_commit = g_hash_table_lookup (labels, "org.flatpak.commit");
if (oci_commit != NULL && out_commit != NULL && *out_commit == NULL)
*out_commit = g_strdup (oci_commit);
oci_parent_commit = g_hash_table_lookup (labels, "org.flatpak.parent-commit");
if (oci_parent_commit != NULL && out_parent_commit != NULL && *out_parent_commit == NULL)
*out_parent_commit = g_strdup (oci_parent_commit);
oci_timestamp = g_hash_table_lookup (labels, "org.flatpak.timestamp");
if (oci_timestamp != NULL && out_timestamp != NULL && *out_timestamp == 0)
*out_timestamp = g_ascii_strtoull (oci_timestamp, NULL, 10);
oci_subject = g_hash_table_lookup (labels, "org.flatpak.subject");
if (oci_subject != NULL && out_subject != NULL && *out_subject == NULL)
*out_subject = g_strdup (oci_subject);
oci_body = g_hash_table_lookup (labels, "org.flatpak.body");
if (oci_body != NULL && out_body != NULL && *out_body == NULL)
*out_body = g_strdup (oci_body);
if (metadata_builder)
{
g_hash_table_iter_init (&iter, labels);
while (g_hash_table_iter_next (&iter, &_key, &_value))
{
const char *key = _key;
const char *value = _value;
guchar *bin;
gsize bin_len;
g_autoptr(GVariant) data = NULL;
if (!g_str_has_prefix (key, "org.flatpak.commit-metadata."))
continue;
key += strlen ("org.flatpak.commit-metadata.");
bin = g_base64_decode (value, &bin_len);
data = g_variant_ref_sink (g_variant_new_from_data (G_VARIANT_TYPE ("v"), bin, bin_len, FALSE,
g_free, bin));
g_variant_builder_add (metadata_builder, "{s@v}", key, data);
}
}
}
G_DEFINE_TYPE (FlatpakOciSignature, flatpak_oci_signature, FLATPAK_TYPE_JSON);

View File

@@ -3610,28 +3610,18 @@ flatpak_pull_from_oci (OstreeRepo *repo,
g_autofree char *old_diffid = NULL;
g_autofree char *commit_checksum = NULL;
const char *parent = NULL;
g_autofree char *subject = NULL;
g_autofree char *body = NULL;
g_autofree char *manifest_ref = NULL;
const char *manifest_ref = NULL;
g_autofree char *full_ref = NULL;
const char *diffid;
guint64 timestamp = 0;
FlatpakOciPullProgressData progress_data = { progress_cb, progress_user_data };
g_autoptr(GVariantBuilder) metadata_builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
g_autoptr(GVariant) metadata = NULL;
GHashTable *labels;
int n_layers;
int i;
g_assert (ref != NULL);
g_assert (g_str_has_prefix (digest, "sha256:"));
labels = flatpak_oci_image_get_labels (image_config);
if (labels)
flatpak_oci_parse_commit_labels (labels, &timestamp,
&subject, &body,
&manifest_ref, NULL, NULL,
metadata_builder);
manifest_ref = flatpak_image_source_get_ref (image_source);
if (manifest_ref == NULL)
{
@@ -3639,12 +3629,18 @@ flatpak_pull_from_oci (OstreeRepo *repo,
return NULL;
}
if (strcmp (manifest_ref, ref) != 0)
if (ref == NULL)
{
ref = manifest_ref;
}
else if (g_strcmp0 (manifest_ref, ref) != 0)
{
flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA, _("Wrong ref (%s) specified for OCI image %s, expected %s"), manifest_ref, digest, ref);
return NULL;
}
flatpak_image_source_build_commit_metadata (image_source, metadata_builder);
g_variant_builder_add (metadata_builder, "{s@v}", "xa.alt-id",
g_variant_new_variant (g_variant_new_string (digest + strlen ("sha256:"))));
@@ -3818,11 +3814,11 @@ flatpak_pull_from_oci (OstreeRepo *repo,
metadata = g_variant_ref_sink (g_variant_builder_end (metadata_builder));
if (!ostree_repo_write_commit_with_time (repo,
parent,
subject,
body,
flatpak_image_source_get_commit_subject (image_source),
flatpak_image_source_get_commit_body (image_source),
metadata,
OSTREE_REPO_FILE (archive_root),
timestamp,
flatpak_image_source_get_commit_timestamp (image_source),
&commit_checksum,
cancellable, error))
goto error;