From a22c592a785f3c1b48d3d805dbb06924a038bf6e Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Tue, 8 Sep 2015 10:31:22 -0400 Subject: [PATCH] daemon: Use GVariantDict ("a{sv}") for deployments Allows for future extensibility. Also some of the optional attributes can actually be optional. See the XML interface spec for the key names. --- src/app/rpmostree-builtin-status.c | 84 ++++++++++++--------- src/daemon/org.projectatomic.rpmostree1.xml | 31 ++++---- src/daemon/rpmostreed-deployment-utils.c | 45 ++++++----- src/daemon/rpmostreed-sysroot.c | 9 ++- 4 files changed, 92 insertions(+), 77 deletions(-) diff --git a/src/app/rpmostree-builtin-status.c b/src/app/rpmostree-builtin-status.c index 932370c0..62875275 100644 --- a/src/app/rpmostree-builtin-status.c +++ b/src/app/rpmostree-builtin-status.c @@ -44,17 +44,6 @@ printchar (char *s, int n) g_print ("\n"); } -enum { - ID, - OSNAME, - SERIAL, - CHECKSUM, - VERSION, - TIMESTAMP, - ORIGIN, - SIGNATURES -}; - gboolean rpmostree_builtin_status (int argc, char **argv, @@ -68,6 +57,9 @@ rpmostree_builtin_status (int argc, g_autoptr(GVariant) booted_deployment = NULL; g_autoptr(GVariant) deployments = NULL; g_autoptr(GVariant) booted_signatures = NULL; + g_autoptr(GPtrArray) deployment_dicts = NULL; + GVariantIter iter; + GVariant *child; gchar *booted_id = NULL; /* borrowed */ const guint CSUM_DISP_LEN = 10; /* number of checksum characters to display */ @@ -96,35 +88,51 @@ rpmostree_builtin_status (int argc, booted_deployment = rpmostree_os_dup_booted_deployment (os_proxy); if (booted_deployment) { - g_variant_get_child (booted_deployment, ID, "&s", &booted_id); - booted_signatures = g_variant_get_child_value (booted_deployment, SIGNATURES); + GVariantDict dict; + g_variant_dict_init (&dict, booted_deployment); + booted_signatures = g_variant_dict_lookup_value (&dict, "signatures", + G_VARIANT_TYPE ("av")); + g_variant_dict_clear (&dict); } - deployments = rpmostree_sysroot_dup_deployments (sysroot_proxy); - if (deployments) - n = g_variant_n_children (deployments); - else - n = 0; + deployment_dicts = g_ptr_array_new_with_free_func ((GDestroyNotify) g_variant_dict_unref); - for (i = 0; i < n; i++) + deployments = rpmostree_sysroot_dup_deployments (sysroot_proxy); + + g_variant_iter_init (&iter, deployments); + + while ((child = g_variant_iter_next_value (&iter)) != NULL) { + GVariantDict *dict = g_variant_dict_new (child); + + /* Takes ownership of the dictionary */ + g_ptr_array_add (deployment_dicts, dict); + /* find lengths for use in column output */ if (!opt_pretty) { - g_autoptr(GVariant) v = NULL; gchar *origin_refspec = NULL; /* borrowed */ gchar *os_name = NULL; /* borrowed */ gchar *version_string = NULL; /* borrowed */ - v = g_variant_get_child_value (deployments, i); - g_variant_get_child (v, OSNAME, "&s", &os_name); - g_variant_get_child (v, VERSION, "&s", &version_string); - g_variant_get_child (v, ORIGIN, "&s", &origin_refspec); + /* osname should always be present. */ + if (g_variant_dict_lookup (dict, "osname", "&s", &os_name)) + max_osname_len = MAX (max_osname_len, strlen (os_name)); + else + { + const char *id = NULL; + g_variant_dict_lookup (dict, "id", "&s", &id); + g_critical ("Deployment '%s' missing osname", id != NULL ? id : "?"); + } - max_osname_len = MAX (max_osname_len, strlen (os_name)); - max_refspec_len = MAX (max_refspec_len, strlen (origin_refspec)); - max_version_len = MAX (max_version_len, strlen (version_string)); + if (g_variant_dict_lookup (dict, "version", "&s", &version_string)) + max_version_len = MAX (max_version_len, strlen (version_string)); + + if (g_variant_dict_lookup (dict, "origin", "&s", &origin_refspec)) + max_refspec_len = MAX (max_refspec_len, strlen (origin_refspec)); } + + g_variant_unref (child); } if (!opt_pretty) @@ -142,13 +150,15 @@ rpmostree_builtin_status (int argc, else printchar ("=", 60); + n = deployment_dicts->len; + /* print entries for each deployment */ for (i = 0; i < n; i++) { + GVariantDict *dict; GDateTime *timestamp = NULL; g_autofree char *timestamp_string = NULL; g_autofree gchar *truncated_csum = NULL; - g_autoptr(GVariant) v = NULL; g_autoptr(GVariant) signatures = NULL; gchar *id = NULL; /* borrowed */ @@ -161,12 +171,18 @@ rpmostree_builtin_status (int argc, gint serial; gboolean is_booted = FALSE; - v = g_variant_get_child_value (deployments, i); - g_variant_get (v, "(&s&si&s&st&sav)", - &id, &os_name, &serial, - &checksum, &version_string, - &t, &origin_refspec, NULL); - signatures = g_variant_get_child_value (v, SIGNATURES); + dict = g_ptr_array_index (deployment_dicts, i); + + g_variant_dict_lookup (dict, "id", "&s", &id); + g_variant_dict_lookup (dict, "osname", "&s", &os_name); + g_variant_dict_lookup (dict, "serial", "i", &serial); + g_variant_dict_lookup (dict, "checksum", "s", &checksum); + g_variant_dict_lookup (dict, "version", "s", &version_string); + g_variant_dict_lookup (dict, "timestamp", "t", &t); + g_variant_dict_lookup (dict, "origin", "s", &origin_refspec); + signatures = g_variant_dict_lookup_value (dict, "signatures", + G_VARIANT_TYPE ("av")); + is_booted = g_strcmp0 (booted_id, id) == 0; timestamp = g_date_time_new_from_unix_utc (t); diff --git a/src/daemon/org.projectatomic.rpmostree1.xml b/src/daemon/org.projectatomic.rpmostree1.xml index aeb650d5..186ca988 100644 --- a/src/daemon/org.projectatomic.rpmostree1.xml +++ b/src/daemon/org.projectatomic.rpmostree1.xml @@ -3,6 +3,17 @@ "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> + + @@ -23,24 +34,14 @@ - - + + - - - + + + diff --git a/src/daemon/rpmostreed-deployment-utils.c b/src/daemon/rpmostreed-deployment-utils.c index fcd1be1c..3a887b7e 100644 --- a/src/daemon/rpmostreed-deployment-utils.c +++ b/src/daemon/rpmostreed-deployment-utils.c @@ -134,17 +134,11 @@ out: GVariant * rpmostreed_deployment_generate_blank_variant (void) { - GVariantBuilder builder; - g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE); - g_variant_builder_add (&builder, "s", ""); - g_variant_builder_add (&builder, "s", ""); - g_variant_builder_add (&builder, "i", -1); - g_variant_builder_add (&builder, "s", ""); - g_variant_builder_add (&builder, "s", ""); - g_variant_builder_add (&builder, "t", 0); - g_variant_builder_add (&builder, "s", ""); - g_variant_builder_add_value (&builder, g_variant_new("av", NULL)); - return g_variant_builder_end (&builder); + GVariantDict dict; + + g_variant_dict_init (&dict, NULL); + + return g_variant_dict_end (&dict); } GVariant * @@ -160,7 +154,7 @@ rpmostreed_deployment_generate_variant (OstreeDeployment *deployment, GVariant *sigs = NULL; /* floating variant */ GError *error = NULL; - GVariantBuilder builder; + GVariantDict dict; guint64 timestamp = 0; const gchar *osname = ostree_deployment_get_osname (deployment); @@ -190,20 +184,23 @@ rpmostreed_deployment_generate_variant (OstreeDeployment *deployment, if (origin_refspec) sigs = rpmostreed_deployment_gpg_results (repo, origin_refspec, csum); - if (!sigs) - sigs = g_variant_new ("av", NULL); + g_variant_dict_init (&dict, NULL); - g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE); - g_variant_builder_add (&builder, "s", id); - g_variant_builder_add (&builder, "s", osname ? osname : ""); - g_variant_builder_add (&builder, "i", serial); - g_variant_builder_add (&builder, "s", csum); - g_variant_builder_add (&builder, "s", version_commit ? version_commit : ""); - g_variant_builder_add (&builder, "t", timestamp ? timestamp : 0); - g_variant_builder_add (&builder, "s", origin_refspec ? origin_refspec : "none"); - g_variant_builder_add_value (&builder, sigs); + g_variant_dict_insert (&dict, "id", "s", id); + if (osname != NULL) + g_variant_dict_insert (&dict, "osname", "s", osname); + g_variant_dict_insert (&dict, "serial", "i", serial); + g_variant_dict_insert (&dict, "checksum", "s", csum); + if (version_commit != NULL) + g_variant_dict_insert (&dict, "version", "s", version_commit); + if (timestamp > 0) + g_variant_dict_insert (&dict, "timestamp", "t", timestamp); + if (origin_refspec != NULL) + g_variant_dict_insert (&dict, "origin", "s", origin_refspec); + if (sigs != NULL) + g_variant_dict_insert_value (&dict, "signatures", sigs); - return g_variant_builder_end (&builder); + return g_variant_dict_end (&dict); } gint diff --git a/src/daemon/rpmostreed-sysroot.c b/src/daemon/rpmostreed-sysroot.c index 3310bd62..ac267023 100644 --- a/src/daemon/rpmostreed-sysroot.c +++ b/src/daemon/rpmostreed-sysroot.c @@ -486,7 +486,7 @@ sysroot_populate_deployments (RpmostreedSysroot *self, guint i; g_debug ("loading deployments"); - g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ssisstsav)")); + g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}")); /* Add deployment interfaces */ deployments = ostree_sysroot_get_deployments (ot_sysroot); @@ -496,9 +496,10 @@ sysroot_populate_deployments (RpmostreedSysroot *self, for (i=0; ilen; i++) { - g_variant_builder_add_value (&builder, - rpmostreed_deployment_generate_variant (deployments->pdata[i], - ot_repo)); + GVariant *variant; + variant = rpmostreed_deployment_generate_variant (deployments->pdata[i], + ot_repo); + g_variant_builder_add_value (&builder, variant); } booted = ostree_sysroot_get_booted_deployment (ot_sysroot);