lib: Add a private helper to abort txns, use in sysroot cleanup

Steal some code from flatpak for this, which allows porting a few more things to
new style. I started on a public API version of this but was trying to roll some
other things into it and it snowballed. Let's do this version since it's easy
for now.

While here I changed things so that `generate_deployment_refs()` now just uses
`_set_ref_immediate()` rather than requring a txn.

Also, AFAICS there was no test coverage of `generate_deployment_refs()`; I tried
commenting it out and at least `admin-test.sh` passed. Add some coverage of this
- I verified that with this commenting out bits of that function cause the test
to fail.

Closes: #1132
Approved by: jlebon
This commit is contained in:
Colin Walters authored and Atomic Bot committed 2017-09-08 16:25:06 +00:00
1 parent 6be4dfe66e
commit 3594bb2d0f
3 files changed
+57 -41

No files matched your search

+23 -39
View File
@@ -21,6 +21,7 @@
#include "config.h"
#include "otutil.h"
#include "ostree-repo-private.h"
#include "ostree-linuxfsutil.h"
#include "ostree-sysroot-private.h"
@@ -335,10 +336,7 @@ cleanup_old_deployments (OstreeSysroot *self,
return TRUE;
}
/* libostree holds a ref for each deployment's exact checksum to avoid it being
* GC'd even if the origin ref changes. This function resets those refs
* to match active deployments.
*/
/* Delete the ref bindings for a non-active boot version */
static gboolean
cleanup_ref_prefix (OstreeRepo *repo,
int bootversion,
@@ -346,30 +344,24 @@ cleanup_ref_prefix (OstreeRepo *repo,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
g_autofree char *prefix = g_strdup_printf ("ostree/%d/%d", bootversion, subbootversion);
g_autoptr(GHashTable) refs = NULL;
if (!ostree_repo_list_refs_ext (repo, prefix, &refs, OSTREE_REPO_LIST_REFS_EXT_NONE, cancellable, error))
goto out;
if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error))
goto out;
return FALSE;
GLNX_HASH_TABLE_FOREACH (refs, const char *, ref)
{
ostree_repo_transaction_set_refspec (repo, ref, NULL);
if (!ostree_repo_set_ref_immediate (repo, NULL, ref, NULL, cancellable, error))
return FALSE;
}
if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error))
goto out;
ret = TRUE;
out:
ostree_repo_abort_transaction (repo, cancellable, NULL);
return ret;
return TRUE;
}
/* libostree holds a ref for each deployment's exact checksum to avoid it being
* GC'd even if the origin ref changes. This function resets those refs
* to match active deployments.
*/
static gboolean
generate_deployment_refs (OstreeSysroot *self,
OstreeRepo *repo,
@@ -379,46 +371,38 @@ generate_deployment_refs (OstreeSysroot *self,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
int cleanup_bootversion;
int cleanup_subbootversion;
guint i;
cleanup_bootversion = (bootversion == 0) ? 1 : 0;
cleanup_subbootversion = (subbootversion == 0) ? 1 : 0;
int cleanup_bootversion = (bootversion == 0) ? 1 : 0;
int cleanup_subbootversion = (subbootversion == 0) ? 1 : 0;
if (!cleanup_ref_prefix (repo, cleanup_bootversion, 0,
cancellable, error))
goto out;
return FALSE;
if (!cleanup_ref_prefix (repo, cleanup_bootversion, 1,
cancellable, error))
goto out;
return FALSE;
if (!cleanup_ref_prefix (repo, bootversion, cleanup_subbootversion,
cancellable, error))
goto out;
return FALSE;
for (i = 0; i < deployments->len; i++)
g_autoptr(_OstreeRepoAutoTransaction) txn =
_ostree_repo_auto_transaction_start (repo, cancellable, error);
if (!txn)
return FALSE;
for (guint i = 0; i < deployments->len; i++)
{
OstreeDeployment *deployment = deployments->pdata[i];
g_autofree char *refname = g_strdup_printf ("ostree/%d/%d/%u",
bootversion, subbootversion,
i);
if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error))
goto out;
ostree_repo_transaction_set_refspec (repo, refname, ostree_deployment_get_csum (deployment));
if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error))
goto out;
}
if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error))
return FALSE;
ret = TRUE;
out:
ostree_repo_abort_transaction (repo, cancellable, NULL);
return ret;
return TRUE;
}
static gboolean