From 40bb310e97b9ac341f7776c0c49da740387323e8 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Mon, 8 Jul 2019 10:23:33 -0400 Subject: [PATCH] app/composeutil: Refactor reading JSON metadata from file Instead of relying on `rpmostree_composeutil_read_json_metadata` to initialize the metadata hash table, initialize it explicitly in `context_new()` function and only call the util function if we were passed a file with `--add-metadata-from-json`. Accordingly rename the function `rpmostree_composeutil_read_json_metadata_from_file`. Closes: #1865 Approved by: cgwalters --- src/app/rpmostree-compose-builtin-rojig.c | 11 ++++-- src/app/rpmostree-compose-builtin-tree.c | 11 ++++-- src/app/rpmostree-composeutil.c | 47 +++++++++++------------ src/app/rpmostree-composeutil.h | 7 ++-- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/app/rpmostree-compose-builtin-rojig.c b/src/app/rpmostree-compose-builtin-rojig.c index 42ca89f5..4ac6ce79 100644 --- a/src/app/rpmostree-compose-builtin-rojig.c +++ b/src/app/rpmostree-compose-builtin-rojig.c @@ -276,9 +276,14 @@ rpm_ostree_rojig_compose_new (const char *treefile_path, if (!self->repo) return glnx_prefix_error (error, "Creating repo-build"); - self->metadata = rpmostree_composeutil_read_json_metadata (opt_metadata_json, error); - if (!self->metadata) - return FALSE; + self->metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, + (GDestroyNotify)g_variant_unref); + if (opt_metadata_json) + { + if (!rpmostree_composeutil_read_json_metadata_from_file (opt_metadata_json, + self->metadata, error)) + return FALSE; + } self->corectx = rpmostree_context_new_tree (self->cachedir_dfd, self->repo, cancellable, error); if (!self->corectx) diff --git a/src/app/rpmostree-compose-builtin-tree.c b/src/app/rpmostree-compose-builtin-tree.c index 1ab6f25b..65554fc5 100644 --- a/src/app/rpmostree-compose-builtin-tree.c +++ b/src/app/rpmostree-compose-builtin-tree.c @@ -682,9 +682,14 @@ rpm_ostree_compose_context_new (const char *treefile_pathstr, self->treefile_path = g_file_new_for_path (treefile_pathstr); - self->metadata = rpmostree_composeutil_read_json_metadata (opt_metadata_json, error); - if (!self->metadata) - return FALSE; + self->metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, + (GDestroyNotify)g_variant_unref); + if (opt_metadata_json) + { + if (!rpmostree_composeutil_read_json_metadata_from_file (opt_metadata_json, + self->metadata, error)) + return FALSE; + } if (opt_metadata_strings) { diff --git a/src/app/rpmostree-composeutil.c b/src/app/rpmostree-composeutil.c index 01176bc2..9289e8dc 100644 --- a/src/app/rpmostree-composeutil.c +++ b/src/app/rpmostree-composeutil.c @@ -320,35 +320,32 @@ rpmostree_composeutil_get_treespec (RpmOstreeContext *ctx, /* compose tree accepts JSON metadata via file; convert it * to a hash table of a{sv}; suitable for further extension. */ -GHashTable * -rpmostree_composeutil_read_json_metadata (const char *path, - GError **error) +gboolean +rpmostree_composeutil_read_json_metadata_from_file (const char *path, + GHashTable *metadata, + GError **error) { - g_autoptr(GHashTable) metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_variant_unref); - if (path) + const char *errprefix = glnx_strjoina ("While parsing JSON file ", path); + GLNX_AUTO_PREFIX_ERROR (errprefix, error); + glnx_unref_object JsonParser *jparser = json_parser_new (); + if (!json_parser_load_from_file (jparser, path, error)) + return FALSE; + + JsonNode *metarootval = json_parser_get_root (jparser); + g_autoptr(GVariant) jsonmetav = json_gvariant_deserialize (metarootval, "a{sv}", error); + if (!jsonmetav) { - glnx_unref_object JsonParser *jparser = json_parser_new (); - if (!json_parser_load_from_file (jparser, path, error)) - return FALSE; - - JsonNode *metarootval = json_parser_get_root (jparser); - g_autoptr(GVariant) jsonmetav = json_gvariant_deserialize (metarootval, "a{sv}", error); - if (!jsonmetav) - { - g_prefix_error (error, "Parsing %s: ", path); - return FALSE; - } - - GVariantIter viter; - g_variant_iter_init (&viter, jsonmetav); - { char *key; - GVariant *value; - while (g_variant_iter_loop (&viter, "{sv}", &key, &value)) - g_hash_table_replace (metadata, g_strdup (key), g_variant_ref (value)); - } + g_prefix_error (error, "Parsing %s: ", path); + return FALSE; } - return g_steal_pointer (&metadata); + GVariantIter viter; + g_variant_iter_init (&viter, jsonmetav); + { char *key; + GVariant *value; + while (g_variant_iter_loop (&viter, "{sv}", &key, &value)) + g_hash_table_replace (metadata, g_strdup (key), g_variant_ref (value)); + } } /* Convert hash table of metadata into finalized GVariant */ diff --git a/src/app/rpmostree-composeutil.h b/src/app/rpmostree-composeutil.h index b41f54f2..1c5afd1e 100644 --- a/src/app/rpmostree-composeutil.h +++ b/src/app/rpmostree-composeutil.h @@ -51,9 +51,10 @@ rpmostree_composeutil_get_treespec (RpmOstreeContext *ctx, gboolean bind_selinux, GError **error); -GHashTable * -rpmostree_composeutil_read_json_metadata (const char *path, - GError **error); +gboolean +rpmostree_composeutil_read_json_metadata_from_file (const char *path, + GHashTable *metadata, + GError **error); GVariant * rpmostree_composeutil_finalize_metadata (GHashTable *metadata,