From 24ca5ef2ae9e62743c978639945fd18d1e88ac83 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 14 Apr 2021 20:43:53 -0400 Subject: [PATCH] repo: Make locking APIs public Doing anything even somewhat sophisticated requires this; turns out our own `ostree prune` CLI wants this, e.g. https://github.com/ostreedev/ostree/issues/2337 Closes: https://github.com/ostreedev/ostree/issues/2286 --- apidoc/ostree-sections.txt | 2 ++ src/libostree/libostree-devel.sym | 2 ++ src/libostree/ostree-repo-commit.c | 8 ++++---- src/libostree/ostree-repo-private.h | 16 ---------------- src/libostree/ostree-repo.c | 20 +++++++++++--------- src/libostree/ostree-repo.h | 25 +++++++++++++++++++++++++ tests/test-core.js | 8 ++++++++ 7 files changed, 52 insertions(+), 29 deletions(-) diff --git a/apidoc/ostree-sections.txt b/apidoc/ostree-sections.txt index b09ba6f997..232882071f 100644 --- a/apidoc/ostree-sections.txt +++ b/apidoc/ostree-sections.txt @@ -319,6 +319,8 @@ ostree_repo_get_min_free_space_bytes ostree_repo_get_config ostree_repo_get_dfd ostree_repo_get_default_repo_finders +ostree_repo_lock_pop +ostree_repo_lock_push ostree_repo_hash ostree_repo_equal ostree_repo_copy_config diff --git a/src/libostree/libostree-devel.sym b/src/libostree/libostree-devel.sym index 8cc11c658c..a25aa5868f 100644 --- a/src/libostree/libostree-devel.sym +++ b/src/libostree/libostree-devel.sym @@ -29,6 +29,8 @@ global: ostree_repo_write_regfile; ostree_content_writer_get_type; ostree_content_writer_finish; + ostree_repo_lock_push; + ostree_repo_lock_pop; } LIBOSTREE_2021.1; /* Stub section for the stable release *after* this development one; don't diff --git a/src/libostree/ostree-repo-commit.c b/src/libostree/ostree-repo-commit.c index 1ac53259f2..dd5cd8626b 100644 --- a/src/libostree/ostree-repo-commit.c +++ b/src/libostree/ostree-repo-commit.c @@ -1684,8 +1684,8 @@ ostree_repo_prepare_transaction (OstreeRepo *self, memset (&self->txn.stats, 0, sizeof (OstreeRepoTransactionStats)); - self->txn_locked = _ostree_repo_lock_push (self, OSTREE_REPO_LOCK_SHARED, - cancellable, error); + self->txn_locked = ostree_repo_lock_push (self, OSTREE_REPO_LOCK_SHARED, + cancellable, error); if (!self->txn_locked) return FALSE; @@ -2341,7 +2341,7 @@ ostree_repo_commit_transaction (OstreeRepo *self, if (self->txn_locked) { - if (!_ostree_repo_lock_pop (self, cancellable, error)) + if (!ostree_repo_lock_pop (self, cancellable, error)) return FALSE; self->txn_locked = FALSE; } @@ -2399,7 +2399,7 @@ ostree_repo_abort_transaction (OstreeRepo *self, if (self->txn_locked) { - if (!_ostree_repo_lock_pop (self, cancellable, error)) + if (!ostree_repo_lock_pop (self, cancellable, error)) return FALSE; self->txn_locked = FALSE; } diff --git a/src/libostree/ostree-repo-private.h b/src/libostree/ostree-repo-private.h index 14b29c0bba..a32c73792d 100644 --- a/src/libostree/ostree-repo-private.h +++ b/src/libostree/ostree-repo-private.h @@ -506,22 +506,6 @@ _ostree_repo_maybe_regenerate_summary (OstreeRepo *self, GCancellable *cancellable, GError **error); -/* Locking APIs are currently private. - * See https://github.com/ostreedev/ostree/pull/1555 - */ -typedef enum { - OSTREE_REPO_LOCK_SHARED, - OSTREE_REPO_LOCK_EXCLUSIVE -} OstreeRepoLockType; - -gboolean _ostree_repo_lock_push (OstreeRepo *self, - OstreeRepoLockType lock_type, - GCancellable *cancellable, - GError **error); -gboolean _ostree_repo_lock_pop (OstreeRepo *self, - GCancellable *cancellable, - GError **error); - typedef OstreeRepo OstreeRepoAutoLock; OstreeRepoAutoLock * _ostree_repo_auto_lock_push (OstreeRepo *self, diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c index b2347b47c7..3f6e2a87ba 100644 --- a/src/libostree/ostree-repo.c +++ b/src/libostree/ostree-repo.c @@ -444,7 +444,7 @@ pop_repo_lock (OstreeRepo *self, return TRUE; } -/* +/** * ostree_repo_lock_push: * @self: a #OstreeRepo * @lock_type: the type of lock to acquire @@ -470,9 +470,10 @@ pop_repo_lock (OstreeRepo *self, * %TRUE is returned. * * Returns: %TRUE on success, otherwise %FALSE with @error set + * Since: 2021.2 */ gboolean -_ostree_repo_lock_push (OstreeRepo *self, +ostree_repo_lock_push (OstreeRepo *self, OstreeRepoLockType lock_type, GCancellable *cancellable, GError **error) @@ -538,8 +539,8 @@ _ostree_repo_lock_push (OstreeRepo *self, } } -/* - * _ostree_repo_lock_pop: +/** + * ostree_repo_lock_pop: * @self: a #OstreeRepo * @cancellable: a #GCancellable * @error: a #GError @@ -560,11 +561,12 @@ _ostree_repo_lock_push (OstreeRepo *self, * %TRUE is returned. * * Returns: %TRUE on success, otherwise %FALSE with @error set + * Since: 2021.2 */ gboolean -_ostree_repo_lock_pop (OstreeRepo *self, - GCancellable *cancellable, - GError **error) +ostree_repo_lock_pop (OstreeRepo *self, + GCancellable *cancellable, + GError **error) { g_return_val_if_fail (self != NULL, FALSE); g_return_val_if_fail (OSTREE_IS_REPO (self), FALSE); @@ -655,7 +657,7 @@ _ostree_repo_auto_lock_push (OstreeRepo *self, GCancellable *cancellable, GError **error) { - if (!_ostree_repo_lock_push (self, lock_type, cancellable, error)) + if (!ostree_repo_lock_push (self, lock_type, cancellable, error)) return NULL; return (OstreeRepoAutoLock *)self; } @@ -677,7 +679,7 @@ _ostree_repo_auto_lock_cleanup (OstreeRepoAutoLock *lock) g_autoptr(GError) error = NULL; int errsv = errno; - if (!_ostree_repo_lock_pop (repo, NULL, &error)) + if (!ostree_repo_lock_pop (repo, NULL, &error)) g_critical ("Cleanup repo lock failed: %s", error->message); errno = errsv; diff --git a/src/libostree/ostree-repo.h b/src/libostree/ostree-repo.h index f94d70be1d..b3166c907f 100644 --- a/src/libostree/ostree-repo.h +++ b/src/libostree/ostree-repo.h @@ -1498,6 +1498,31 @@ gboolean ostree_repo_regenerate_summary (OstreeRepo *self, GCancellable *cancellable, GError **error); + +/** + * OstreeRepoLockType: + * @OSTREE_REPO_LOCK_SHARED: A "read only" lock; multiple readers are allowed. + * @OSTREE_REPO_LOCK_EXCLUSIVE: A writable lock at most one writer can be active, and zero readers. + * + * Flags controlling repository locking. + * + * Since: 2021.2 + */ +typedef enum { + OSTREE_REPO_LOCK_SHARED, + OSTREE_REPO_LOCK_EXCLUSIVE +} OstreeRepoLockType; + +_OSTREE_PUBLIC +gboolean ostree_repo_lock_push (OstreeRepo *self, + OstreeRepoLockType lock_type, + GCancellable *cancellable, + GError **error); +_OSTREE_PUBLIC +gboolean ostree_repo_lock_pop (OstreeRepo *self, + GCancellable *cancellable, + GError **error); + /** * OSTREE_REPO_METADATA_REF: * diff --git a/tests/test-core.js b/tests/test-core.js index 2c7162be52..7d871c1ffe 100755 --- a/tests/test-core.js +++ b/tests/test-core.js @@ -134,4 +134,12 @@ w.write(inline_content.slice(10), null) let actual_checksum = w.finish(null) assertEquals(actual_checksum, networks_checksum) +// Basic locking API sanity test +repo.lock_push(OSTree.RepoLockType.SHARED, null); +repo.lock_push(OSTree.RepoLockType.SHARED, null); +repo.lock_pop(null); +repo.lock_pop(null); +repo.lock_push(OSTree.RepoLockType.EXCLUSIVE, null); +repo.lock_pop(null); + print("ok test-core");