diff --git a/src/ostree/ot-admin-builtin-undeploy.c b/src/ostree/ot-admin-builtin-undeploy.c index edf89bf4..9196e5bf 100644 --- a/src/ostree/ot-admin-builtin-undeploy.c +++ b/src/ostree/ot-admin-builtin-undeploy.c @@ -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, diff --git a/src/ostree/ot-admin-functions.c b/src/ostree/ot-admin-functions.c index ff623574..0c8c28f2 100644 --- a/src/ostree/ot-admin-functions.c +++ b/src/ostree/ot-admin-functions.c @@ -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]); +} + diff --git a/src/ostree/ot-admin-functions.h b/src/ostree/ot-admin-functions.h index ea147c84..21e4a7d7 100644 --- a/src/ostree/ot-admin-functions.h +++ b/src/ostree/ot-admin-functions.h @@ -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