admin: (cleanup) Add internal API to find a deployment given an index

At some point, we might want to expose a uniform way to refer
to deployments by an index.  At the moment undeploy is the only
command that does.

I plan to introduce another command which optionally takes an index,
so prepare a helper function for this.
This commit is contained in:
Colin Walters 2015-01-16 12:57:39 -05:00
parent ce957f8649
commit 0eac91a253
3 changed files with 37 additions and 14 deletions

View File

@ -62,20 +62,10 @@ ot_admin_builtin_undeploy (int argc, char **argv, GCancellable *cancellable, GEr
deploy_index_str = argv[1];
deploy_index = atoi (deploy_index_str);
if (deploy_index < 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"Invalid index %d", deploy_index);
goto out;
}
if (deploy_index >= current_deployments->len)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"Out of range index %d, expected < %d", deploy_index, current_deployments->len);
goto out;
}
target_deployment = g_object_ref (current_deployments->pdata[deploy_index]);
target_deployment = ot_admin_get_indexed_deployment (sysroot, deploy_index, error);
if (!target_deployment)
goto out;
if (target_deployment == ostree_sysroot_get_booted_deployment (sysroot))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,

View File

@ -71,3 +71,30 @@ ot_admin_checksum_version (GVariant *checksum)
return g_strdup (ret);
}
OstreeDeployment *
ot_admin_get_indexed_deployment (OstreeSysroot *sysroot,
int index,
GError **error)
{
gs_unref_ptrarray GPtrArray *current_deployments =
ostree_sysroot_get_deployments (sysroot);
if (index < 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"Invalid index %d", index);
return NULL;
}
if (index >= current_deployments->len)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"Out of range deployment index %d, expected < %d", index,
current_deployments->len);
return NULL;
}
return g_object_ref (current_deployments->pdata[index]);
}

View File

@ -36,5 +36,11 @@ ot_admin_require_booted_deployment_or_osname (OstreeSysroot *sysroot,
char *
ot_admin_checksum_version (GVariant *checksum);
OstreeDeployment *
ot_admin_get_indexed_deployment (OstreeSysroot *sysroot,
int index,
GError **error);
G_END_DECLS