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.
This commit is contained in:
Matthew Barnes 2015-09-08 10:31:22 -04:00
parent 7267c1ec05
commit a22c592a78
4 changed files with 92 additions and 77 deletions

View File

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

View File

@ -3,6 +3,17 @@
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/" xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
<!-- Deployment dictionary keys:
'id' (type 's')
'osname' (type 's')
'serial' (type 'i')
'checksum' (type 's')
'version' (type 's')
'timestamp' (type 't')
'origin' (type 's')
'signatures' (type 'av')
-->
<interface name="org.projectatomic.rpmostree1.Sysroot">
<!-- The booted OSName -->
<property name="Booted" type="o" access="read"/>
@ -23,24 +34,14 @@
<arg name="object_path" type="o" direction="out"/>
</method>
<!-- All deployments in boot order
deployment format:
s - Deployment id
s - OSName
i - Serial
s - Checksum
s - Version
t - Timestamp
s - Origin Refspec
av - Signature information
-->
<property name="Deployments" type="a(ssisstsav)" access="read"/>
<!-- Array of all deployments in boot order -->
<property name="Deployments" type="aa{sv}" access="read"/>
</interface>
<interface name="org.projectatomic.rpmostree1.OS">
<property name="BootedDeployment" type="(ssisstsav)" access="read"/>
<property name="DefaultDeployment" type="(ssisstsav)" access="read"/>
<property name="RollbackDeployment" type="(ssisstsav)" access="read"/>
<property name="BootedDeployment" type="a{sv}" access="read"/>
<property name="DefaultDeployment" type="a{sv}" access="read"/>
<property name="RollbackDeployment" type="a{sv}" access="read"/>
<property name="UpgradeOrigin" type="s" access="read"/>
<property name="HasCachedUpdateRpmDiff" type="b" access="read"/>

View File

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

View File

@ -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; i<deployments->len; 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);