Skip to content

Commit

Permalink
app/composeutil: Refactor reading JSON metadata from file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jlebon authored and rh-atomic-bot committed Jul 9, 2019
1 parent c94bd08 commit 40bb310
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
11 changes: 8 additions & 3 deletions src/app/rpmostree-compose-builtin-rojig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions src/app/rpmostree-compose-builtin-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
47 changes: 22 additions & 25 deletions src/app/rpmostree-composeutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
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;
}
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;

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));
}
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;
}

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 */
Expand Down
7 changes: 4 additions & 3 deletions src/app/rpmostree-composeutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 40bb310

Please sign in to comment.