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
This commit is contained in:
Jonathan Lebon 2019-07-08 10:23:33 -04:00 committed by Atomic Bot
parent c94bd08b02
commit 40bb310e97
4 changed files with 42 additions and 34 deletions

View File

@ -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)

View File

@ -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)
{

View File

@ -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 */

View File

@ -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,