[ostree/wip/transaction: 9/9] ostree: Move ref writing to be transaction-based
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ostree/wip/transaction: 9/9] ostree: Move ref writing to be transaction-based
- Date: Thu, 5 Sep 2013 21:33:44 +0000 (UTC)
commit c9a42b032c15c0200d91d1d94f3c361117a3a87b
Author: Jasper St. Pierre <jstpierre mecheye net>
Date: Thu Sep 5 17:25:26 2013 -0400
ostree: Move ref writing to be transaction-based
src/libostree/ostree-repo-commit.c | 36 ++++++++++++
src/libostree/ostree-repo-private.h | 6 ++
src/libostree/ostree-repo-pull.c | 12 ++--
src/libostree/ostree-repo-refs.c | 104 ++++++++++++++---------------------
src/libostree/ostree-repo.h | 18 +++---
src/ostree/ot-admin-cleanup.c | 22 ++++++-
src/ostree/ot-builtin-commit.c | 6 +-
src/ostree/ot-builtin-pull-local.c | 12 ++--
src/ostree/ot-builtin-reset.c | 11 +++-
src/ostree/ot-builtin-write-refs.c | 11 +++-
10 files changed, 142 insertions(+), 96 deletions(-)
---
diff --git a/src/libostree/ostree-repo-commit.c b/src/libostree/ostree-repo-commit.c
index eec521c..a3d831f 100644
--- a/src/libostree/ostree-repo-commit.c
+++ b/src/libostree/ostree-repo-commit.c
@@ -622,6 +622,38 @@ cleanup_tmpdir (OstreeRepo *self,
return ret;
}
+static void
+ensure_txn_refs (OstreeRepo *self)
+{
+ if (self->txn_refs == NULL)
+ self->txn_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+}
+
+void
+ostree_repo_transaction_write_refspec (OstreeRepo *self,
+ const char *refspec,
+ const char *sha1)
+{
+ g_return_if_fail (self->in_transaction == TRUE);
+
+ ensure_txn_refs (self);
+
+ g_hash_table_replace (self->txn_refs, g_strdup (refspec), g_strdup (sha1));
+}
+
+void
+ostree_repo_transaction_write_ref (OstreeRepo *self,
+ const char *remote,
+ const char *ref,
+ const char *sha1)
+{
+ g_return_if_fail (self->in_transaction == TRUE);
+
+ ensure_txn_refs (self);
+
+ g_hash_table_replace (self->txn_refs, g_strdup_printf ("%s:%s", remote, ref), g_strdup (sha1));
+}
+
gboolean
ostree_repo_commit_transaction (OstreeRepo *self,
OstreeRepoTransactionStats *out_stats,
@@ -640,6 +672,10 @@ ostree_repo_commit_transaction (OstreeRepo *self,
self->in_transaction = FALSE;
+ if (self->txn_refs)
+ if (!_ostree_repo_update_refs (self, self->txn_refs, cancellable, error))
+ goto out;
+
if (!ot_gfile_ensure_unlinked (self->transaction_lock_path, cancellable, error))
goto out;
diff --git a/src/libostree/ostree-repo-private.h b/src/libostree/ostree-repo-private.h
index 3bb1f00..cf1de9b 100644
--- a/src/libostree/ostree-repo-private.h
+++ b/src/libostree/ostree-repo-private.h
@@ -38,6 +38,7 @@ struct OstreeRepo {
GFile *config_file;
GFile *transaction_lock_path;
+ GHashTable *txn_refs;
GMutex txn_stats_lock;
OstreeRepoTransactionStats txn_stats;
@@ -91,6 +92,11 @@ _ostree_repo_write_directory_meta (OstreeRepo *self,
guchar **out_csum,
GCancellable *cancellable,
GError **error);
+gboolean
+_ostree_repo_update_refs (OstreeRepo *self,
+ GHashTable *refs,
+ GCancellable *cancellable,
+ GError **error);
G_END_DECLS
diff --git a/src/libostree/ostree-repo-pull.c b/src/libostree/ostree-repo-pull.c
index 0a42e3f..913eca6 100644
--- a/src/libostree/ostree-repo-pull.c
+++ b/src/libostree/ostree-repo-pull.c
@@ -1332,8 +1332,6 @@ ostree_repo_pull (OstreeRepo *self,
if (!run_mainloop_monitor_fetcher (pull_data))
goto out;
- if (!ostree_repo_commit_transaction (pull_data->repo, NULL, cancellable, error))
- goto out;
g_hash_table_iter_init (&hash_iter, updated_refs);
while (g_hash_table_iter_next (&hash_iter, &key, &value))
@@ -1354,13 +1352,15 @@ ostree_repo_pull (OstreeRepo *self,
}
else
{
- if (!ostree_repo_write_ref (pull_data->repo, pull_data->remote_name, ref, checksum, error))
- goto out;
-
+ ostree_repo_transaction_write_ref (pull_data->repo, pull_data->remote_name, ref, checksum);
+
g_print ("remote %s is now %s\n", remote_ref, checksum);
}
}
-
+
+ if (!ostree_repo_commit_transaction (pull_data->repo, NULL, cancellable, error))
+ goto out;
+
end_time = g_get_monotonic_time ();
bytes_transferred = ostree_fetcher_bytes_transferred (pull_data->fetcher);
diff --git a/src/libostree/ostree-repo-refs.c b/src/libostree/ostree-repo-refs.c
index cff496f..ccc5093 100644
--- a/src/libostree/ostree-repo-refs.c
+++ b/src/libostree/ostree-repo-refs.c
@@ -63,6 +63,7 @@ static gboolean
write_checksum_file (GFile *parentdir,
const char *name,
const char *sha256,
+ GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
@@ -98,7 +99,7 @@ write_checksum_file (GFile *parentdir,
{
child = g_file_get_child (parent, (char*)components->pdata[i]);
- if (!gs_file_ensure_directory (child, FALSE, NULL, error))
+ if (!gs_file_ensure_directory (child, FALSE, cancellable, error))
goto out;
g_clear_object (&parent);
@@ -107,13 +108,13 @@ write_checksum_file (GFile *parentdir,
}
child = g_file_get_child (parent, components->pdata[components->len - 1]);
- if ((out = (GOutputStream*)g_file_replace (child, NULL, FALSE, 0, NULL, error)) == NULL)
+ if ((out = (GOutputStream*)g_file_replace (child, NULL, FALSE, 0, cancellable, error)) == NULL)
goto out;
- if (!g_output_stream_write_all (out, sha256, strlen (sha256), &bytes_written, NULL, error))
+ if (!g_output_stream_write_all (out, sha256, strlen (sha256), &bytes_written, cancellable, error))
goto out;
- if (!g_output_stream_write_all (out, "\n", 1, &bytes_written, NULL, error))
+ if (!g_output_stream_write_all (out, "\n", 1, &bytes_written, cancellable, error))
goto out;
- if (!g_output_stream_close (out, NULL, error))
+ if (!g_output_stream_close (out, cancellable, error))
goto out;
ret = TRUE;
@@ -613,33 +614,21 @@ write_ref_summary (OstreeRepo *self,
return ret;
}
-/**
- * ostree_repo_write_ref:
- * @self: Repo
- * @remote: (allow-none): Optional remote name
- * @name: Name of ref, e.g. foo/bar/baz
- * @rev: (allow-none); ASCII SHA256 checksum; if %NULL, then delete @name
- * @error: Error
- *
- * If @rev is not %NULL, then it as the target of ref named @name; if
- * @remote is provided, the ref will appear originate from that
- * remote.
- *
- * Otherwise, if @rev is %NULL, then delete the ref @name if it exists.
- *
- * This function merely changes the ref target; it is possible to use
- * it to target earlier commits.
- */
-gboolean
-ostree_repo_write_ref (OstreeRepo *self,
- const char *remote,
- const char *name,
- const char *rev,
- GError **error)
+static gboolean
+write_refspec (OstreeRepo *self,
+ const char *refspec,
+ const char *rev,
+ GCancellable *cancellable,
+ GError **error)
{
gboolean ret = FALSE;
+ gs_free char *remote = NULL;
+ gs_free char *name = NULL;
gs_unref_object GFile *dir = NULL;
+ if (!ostree_parse_refspec (refspec, &remote, &name, error))
+ goto out;
+
if (remote == NULL)
dir = g_object_ref (self->local_heads_dir);
else
@@ -648,7 +637,7 @@ ostree_repo_write_ref (OstreeRepo *self,
if (rev != NULL)
{
- if (!gs_file_ensure_directory (dir, FALSE, NULL, error))
+ if (!gs_file_ensure_directory (dir, FALSE, cancellable, error))
goto out;
}
}
@@ -656,26 +645,17 @@ ostree_repo_write_ref (OstreeRepo *self,
if (rev == NULL)
{
gs_unref_object GFile *child = g_file_resolve_relative_path (dir, name);
-
- if (g_file_query_exists (child, NULL))
+
+ if (g_file_query_exists (child, cancellable))
{
- if (!gs_file_unlink (child, NULL, error))
+ if (!gs_file_unlink (child, cancellable, error))
goto out;
}
}
else
{
- if (!write_checksum_file (dir, name, rev, error))
+ if (!write_checksum_file (dir, name, rev, cancellable, error))
goto out;
-
- if (rev != NULL)
- {
- if (self->mode == OSTREE_REPO_MODE_ARCHIVE_Z2)
- {
- if (!write_ref_summary (self, NULL, error))
- goto out;
- }
- }
}
ret = TRUE;
@@ -683,31 +663,31 @@ ostree_repo_write_ref (OstreeRepo *self,
return ret;
}
-/**
- * ostree_repo_write_refspec:
- * @self: Repo
- * @refspec: Optional remote with name of ref, e.g. remotename:foo/bar/baz
- * @rev: (allow-none); ASCII SHA256 checksum; if %NULL, then delete @refspec
- * @error: Error
- *
- * Like ostree_repo_write_ref(), but takes concatenated @refspec
- * format as input instead of separate remote and name arguments.
- */
gboolean
-ostree_repo_write_refspec (OstreeRepo *self,
- const char *refspec,
- const char *rev,
- GError **error)
+_ostree_repo_update_refs (OstreeRepo *self,
+ GHashTable *refs,
+ GCancellable *cancellable,
+ GError **error)
{
gboolean ret = FALSE;
- gs_free char *remote = NULL;
- gs_free char *ref = NULL;
+ GHashTableIter hash_iter;
+ gpointer key, value;
- if (!ostree_parse_refspec (refspec, &remote, &ref, error))
- goto out;
+ g_hash_table_iter_init (&hash_iter, refs);
+ while (g_hash_table_iter_next (&hash_iter, &key, &value))
+ {
+ const char *refspec = key;
+ const char *rev = value;
- if (!ostree_repo_write_ref (self, remote, ref, rev, error))
- goto out;
+ if (!write_refspec (self, refspec, rev, cancellable, error))
+ goto out;
+ }
+
+ if (self->mode == OSTREE_REPO_MODE_ARCHIVE_Z2)
+ {
+ if (!write_ref_summary (self, cancellable, error))
+ goto out;
+ }
ret = TRUE;
out:
diff --git a/src/libostree/ostree-repo.h b/src/libostree/ostree-repo.h
index b3c5506..347e2ca 100644
--- a/src/libostree/ostree-repo.h
+++ b/src/libostree/ostree-repo.h
@@ -172,16 +172,14 @@ gboolean ostree_repo_resolve_rev (OstreeRepo *self,
char **out_rev,
GError **error);
-gboolean ostree_repo_write_ref (OstreeRepo *self,
- const char *remote,
- const char *name,
- const char *rev,
- GError **error);
-
-gboolean ostree_repo_write_refspec (OstreeRepo *self,
- const char *refspec,
- const char *rev,
- GError **error);
+void ostree_repo_transaction_write_refspec (OstreeRepo *self,
+ const char *refspec,
+ const char *sha1);
+
+void ostree_repo_transaction_write_ref (OstreeRepo *self,
+ const char *remote,
+ const char *refspec,
+ const char *sha1);
gboolean ostree_repo_list_refs (OstreeRepo *self,
const char *refspec_prefix,
diff --git a/src/ostree/ot-admin-cleanup.c b/src/ostree/ot-admin-cleanup.c
index 8b2920b..6abda12 100644
--- a/src/ostree/ot-admin-cleanup.c
+++ b/src/ostree/ot-admin-cleanup.c
@@ -400,17 +400,24 @@ cleanup_ref_prefix (OstreeRepo *repo,
if (!ostree_repo_list_refs (repo, prefix, &refs, cancellable, error))
goto out;
+ if (!ostree_repo_prepare_transaction (repo, FALSE, NULL, cancellable, error))
+ goto out;
+
g_hash_table_iter_init (&hashiter, refs);
while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
{
const char *suffix = hashkey;
gs_free char *ref = g_strconcat (prefix, "/", suffix, NULL);
- if (!ostree_repo_write_refspec (repo, ref, NULL, error))
- goto out;
+ ostree_repo_transaction_write_refspec (repo, ref, NULL);
}
+ if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error))
+ goto out;
+
ret = TRUE;
out:
+ if (!ret)
+ ostree_repo_abort_transaction (repo, cancellable, NULL);
return ret;
}
@@ -451,8 +458,13 @@ generate_deployment_refs_and_prune (GFile *sysroot,
gs_free char *refname = g_strdup_printf ("ostree/%d/%d/%u",
bootversion, subbootversion,
i);
- if (!ostree_repo_write_refspec (repo, refname, ot_deployment_get_csum (deployment),
- error))
+
+ if (!ostree_repo_prepare_transaction (repo, FALSE, NULL, cancellable, error))
+ goto out;
+
+ ostree_repo_transaction_write_refspec (repo, refname, ot_deployment_get_csum (deployment));
+
+ if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error))
goto out;
}
@@ -468,6 +480,8 @@ generate_deployment_refs_and_prune (GFile *sysroot,
ret = TRUE;
out:
+ if (!ret)
+ ostree_repo_abort_transaction (repo, cancellable, error);
return ret;
}
diff --git a/src/ostree/ot-builtin-commit.c b/src/ostree/ot-builtin-commit.c
index 22d6d89..b3bb57c 100644
--- a/src/ostree/ot-builtin-commit.c
+++ b/src/ostree/ot-builtin-commit.c
@@ -420,14 +420,12 @@ ostree_builtin_commit (int argc, char **argv, OstreeRepo *repo, GCancellable *ca
&commit_checksum, cancellable, error))
goto out;
+ ostree_repo_transaction_write_ref (repo, NULL, opt_branch, commit_checksum);
+
if (!ostree_repo_commit_transaction (repo, &stats, cancellable, error))
goto out;
in_transaction = FALSE;
-
- if (!ostree_repo_write_ref (repo, NULL, opt_branch, commit_checksum, error))
- goto out;
-
}
else
{
diff --git a/src/ostree/ot-builtin-pull-local.c b/src/ostree/ot-builtin-pull-local.c
index 98ba6cd..f2b286c 100644
--- a/src/ostree/ot-builtin-pull-local.c
+++ b/src/ostree/ot-builtin-pull-local.c
@@ -290,9 +290,6 @@ ostree_builtin_pull_local (int argc, char **argv, OstreeRepo *repo, GCancellable
gs_console_end_status_line (data->console, NULL, NULL);
}
- if (!ostree_repo_commit_transaction (data->dest_repo, NULL, NULL, error))
- goto out;
-
g_print ("Writing %u refs\n", g_hash_table_size (refs_to_clone));
g_hash_table_iter_init (&hash_iter, refs_to_clone);
@@ -301,11 +298,12 @@ ostree_builtin_pull_local (int argc, char **argv, OstreeRepo *repo, GCancellable
const char *name = key;
const char *checksum = value;
- if (!ostree_repo_write_ref (data->dest_repo, opt_remote, name, checksum,
- error))
- goto out;
+ ostree_repo_transaction_write_ref (data->dest_repo, opt_remote, name, checksum);
}
+ if (!ostree_repo_commit_transaction (data->dest_repo, NULL, NULL, error))
+ goto out;
+
ret = TRUE;
out:
g_clear_pointer (&data->threadpool, (GDestroyNotify) g_thread_pool_free);
@@ -315,5 +313,7 @@ ostree_builtin_pull_local (int argc, char **argv, OstreeRepo *repo, GCancellable
g_object_unref (data->dest_repo);
if (context)
g_option_context_free (context);
+ if (!ret)
+ ostree_repo_abort_transaction (repo, cancellable, NULL);
return ret;
}
diff --git a/src/ostree/ot-builtin-reset.c b/src/ostree/ot-builtin-reset.c
index b307f35..6a46b64 100644
--- a/src/ostree/ot-builtin-reset.c
+++ b/src/ostree/ot-builtin-reset.c
@@ -118,12 +118,19 @@ ostree_builtin_reset (int argc,
if (!check_revision_is_parent (repo, current, checksum, cancellable, error))
goto out;
- if (!ostree_repo_write_ref (repo, NULL, ref, checksum, error))
- goto out;
+ if (!ostree_repo_prepare_transaction (repo, FALSE, NULL, cancellable, error))
+ goto out;
+
+ ostree_repo_transaction_write_ref (repo, NULL, ref, checksum);
+
+ if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error))
+ goto out;
ret = TRUE;
out:
if (context)
g_option_context_free (context);
+ if (!ret)
+ ostree_repo_abort_transaction (repo, cancellable, NULL);
return ret;
}
diff --git a/src/ostree/ot-builtin-write-refs.c b/src/ostree/ot-builtin-write-refs.c
index 958bf8e..8d06162 100644
--- a/src/ostree/ot-builtin-write-refs.c
+++ b/src/ostree/ot-builtin-write-refs.c
@@ -53,6 +53,9 @@ ostree_builtin_write_refs (int argc, char **argv, OstreeRepo *repo, GCancellable
instream = (GInputStream*)g_unix_input_stream_new (0, FALSE);
datastream = g_data_input_stream_new (instream);
+ if (!ostree_repo_prepare_transaction (repo, FALSE, NULL, cancellable, error))
+ goto out;
+
while ((line = g_data_input_stream_read_line (datastream, &len,
cancellable, &temp_error)) != NULL)
{
@@ -70,8 +73,7 @@ ostree_builtin_write_refs (int argc, char **argv, OstreeRepo *repo, GCancellable
if (!ostree_validate_structureof_checksum_string (spc + 1, error))
goto out;
- if (!ostree_repo_write_ref (repo, NULL, ref, spc + 1, error))
- goto out;
+ ostree_repo_transaction_write_ref (repo, NULL, ref, spc + 1);
g_free (line);
}
@@ -81,9 +83,14 @@ ostree_builtin_write_refs (int argc, char **argv, OstreeRepo *repo, GCancellable
goto out;
}
+ if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error))
+ goto out;
+
ret = TRUE;
out:
if (context)
g_option_context_free (context);
+ if (!ret)
+ ostree_repo_abort_transaction (repo, cancellable, NULL);
return ret;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]