From 6a9ff5307590ffb05be5ce9c05fe8cc021a538d0 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Mon, 8 Jul 2019 10:26:22 -0400 Subject: [PATCH] app/composeutil: Split out JSON metadata reading Split out from `rpmostree_composeutil_read_json_metadata_from_file` the part that actually converts to `GVariant` and inserts into the hash table. Closes: #1865 Approved by: cgwalters --- src/app/rpmostree-composeutil.c | 36 ++++++++++++++++++++------------- src/app/rpmostree-composeutil.h | 5 ++++- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/app/rpmostree-composeutil.c b/src/app/rpmostree-composeutil.c index 9289e8dcec..2f71e24a55 100644 --- a/src/app/rpmostree-composeutil.c +++ b/src/app/rpmostree-composeutil.c @@ -317,6 +317,26 @@ rpmostree_composeutil_get_treespec (RpmOstreeContext *ctx, return rpmostree_treespec_new_from_keyfile (treespec, error); } +gboolean +rpmostree_composeutil_read_json_metadata (JsonNode *root, + GHashTable *metadata, + GError **error) +{ + g_autoptr(GVariant) jsonmetav = json_gvariant_deserialize (root, "a{sv}", error); + if (!jsonmetav) + 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)); + } + + return TRUE; +} + /* compose tree accepts JSON metadata via file; convert it * to a hash table of a{sv}; suitable for further extension. */ @@ -327,25 +347,13 @@ rpmostree_composeutil_read_json_metadata_from_file (const char *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) - { - 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)); - } + return rpmostree_composeutil_read_json_metadata (metarootval, metadata, error); } /* Convert hash table of metadata into finalized GVariant */ diff --git a/src/app/rpmostree-composeutil.h b/src/app/rpmostree-composeutil.h index 1c5afd1e05..8ceefb1e5e 100644 --- a/src/app/rpmostree-composeutil.h +++ b/src/app/rpmostree-composeutil.h @@ -50,7 +50,10 @@ rpmostree_composeutil_get_treespec (RpmOstreeContext *ctx, JsonObject *treedata, gboolean bind_selinux, GError **error); - +gboolean +rpmostree_composeutil_read_json_metadata (JsonNode *root, + GHashTable *metadata, + GError **error); gboolean rpmostree_composeutil_read_json_metadata_from_file (const char *path, GHashTable *metadata,