From 5950438ca7f859b26f89ddde87b72e0d82ac8a5e Mon Sep 17 00:00:00 2001 From: "Owen W. Taylor" Date: Mon, 28 Oct 2024 12:35:33 -0400 Subject: [PATCH] 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. --- app/flatpak-builtins-build-import-bundle.c | 18 ++-- common/flatpak-dir.c | 25 ++---- common/flatpak-image-source-private.h | 11 ++- common/flatpak-image-source.c | 96 ++++++++++++++++++++-- common/flatpak-json-oci-private.h | 8 -- common/flatpak-json-oci.c | 66 --------------- common/flatpak-oci-registry.c | 28 +++---- 7 files changed, 125 insertions(+), 127 deletions(-) diff --git a/app/flatpak-builtins-build-import-bundle.c b/app/flatpak-builtins-build-import-bundle.c index f3508709..d79fbbde 100644 --- a/app/flatpak-builtins-build-import-bundle.c +++ b/app/flatpak-builtins-build-import-bundle.c @@ -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); } diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c index 5ad35afc..2d1f8b3a 100644 --- a/common/flatpak-dir.c +++ b/common/flatpak-dir.c @@ -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, ×tamp, - &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"))); } diff --git a/common/flatpak-image-source-private.h b/common/flatpak-image-source-private.h index ebd856a4..fe8f02ba 100644 --- a/common/flatpak-image-source-private.h +++ b/common/flatpak-image-source-private.h @@ -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__ */ diff --git a/common/flatpak-image-source.c b/common/flatpak-image-source.c index 30e61956..42325501 100644 --- a/common/flatpak-image-source.c +++ b/common/flatpak-image-source.c @@ -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); + } } diff --git a/common/flatpak-json-oci-private.h b/common/flatpak-json-oci-private.h index edb22eeb..87573c17 100644 --- a/common/flatpak-json-oci-private.h +++ b/common/flatpak-json-oci-private.h @@ -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) diff --git a/common/flatpak-json-oci.c b/common/flatpak-json-oci.c index 42647d11..2b55b9de 100644 --- a/common/flatpak-json-oci.c +++ b/common/flatpak-json-oci.c @@ -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); diff --git a/common/flatpak-oci-registry.c b/common/flatpak-oci-registry.c index 11b5f98d..ad05c9f0 100644 --- a/common/flatpak-oci-registry.c +++ b/common/flatpak-oci-registry.c @@ -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, ×tamp, - &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;