diff options
author | Giuseppe Scrivano <gscrivan@redhat.com> | 2015-07-22 12:16:51 +0200 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2015-07-24 12:37:42 -0400 |
commit | 133fb5ffdcba36342b10050b8e443060dd437d60 (patch) | |
tree | a9a18d43a4e72f6ca60679de48d1b16dda6aaaff | |
parent | a917c96976147595ff39c7524678331776ad3127 (diff) | |
download | ostree-133fb5ffdcba36342b10050b8e443060dd437d60.tar.gz |
libostree: new API ostree_repo_remote_list_refs
The new API permits to query a remote repository summary file and
retrieve the list of available refs.
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
-rw-r--r-- | doc/ostree-sections.txt | 1 | ||||
-rw-r--r-- | src/libostree/ostree-repo-refs.c | 75 | ||||
-rw-r--r-- | src/libostree/ostree-repo.h | 6 | ||||
-rw-r--r-- | src/ostree/ot-remote-builtin-refs.c | 36 |
4 files changed, 90 insertions, 28 deletions
diff --git a/doc/ostree-sections.txt b/doc/ostree-sections.txt index 7d412b46..a4d4f2e9 100644 --- a/doc/ostree-sections.txt +++ b/doc/ostree-sections.txt @@ -259,6 +259,7 @@ ostree_repo_write_content_async ostree_repo_write_content_finish ostree_repo_resolve_rev ostree_repo_list_refs +ostree_repo_remote_list_refs ostree_repo_load_variant ostree_repo_load_commit ostree_repo_load_variant_if_exists diff --git a/src/libostree/ostree-repo-refs.c b/src/libostree/ostree-repo-refs.c index 970f5191..23e47fc7 100644 --- a/src/libostree/ostree-repo-refs.c +++ b/src/libostree/ostree-repo-refs.c @@ -578,6 +578,81 @@ ostree_repo_list_refs (OstreeRepo *self, return ret; } +/** + * ostree_repo_remote_list_refs: + * @self: Repo + * @remote_name: Name of the remote. + * @out_all_refs: (out) (element-type utf8 utf8): Mapping from ref to checksum + * @cancellable: Cancellable + * @error: Error + * + */ +gboolean +ostree_repo_remote_list_refs (OstreeRepo *self, + const char *remote_name, + GHashTable **out_all_refs, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GBytes) summary_bytes = NULL; + gboolean ret = FALSE; + g_autoptr(GHashTable) ret_all_refs = NULL; + + if (!ostree_repo_remote_fetch_summary (self, remote_name, + &summary_bytes, NULL, + cancellable, error)) + goto out; + + if (summary_bytes == NULL) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, + "Remote refs not available; server has no summary file\n"); + goto out; + } + else + { + g_autoptr(GVariant) summary = NULL; + g_autoptr(GVariant) ref_map = NULL; + GVariantIter iter; + GVariant *child; + ret_all_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + summary = g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT, + summary_bytes, FALSE); + + ref_map = g_variant_get_child_value (summary, 0); + + g_variant_iter_init (&iter, ref_map); + while ((child = g_variant_iter_next_value (&iter)) != NULL) + { + const char *ref_name = NULL; + g_autoptr(GVariant) csum_v = NULL; + char tmp_checksum[65]; + + g_variant_get_child (child, 0, "&s", &ref_name); + + if (ref_name != NULL) + { + g_variant_get_child (child, 1, "(t@aya{sv})", NULL, &csum_v, NULL); + + ostree_checksum_inplace_from_bytes (ostree_checksum_bytes_peek (csum_v), + tmp_checksum); + g_hash_table_insert (ret_all_refs, + g_strdup (ref_name), + g_strdup (tmp_checksum)); + } + + g_variant_unref (child); + } + } + + ret = TRUE; + ot_transfer_out_value (out_all_refs, &ret_all_refs); + + out: + return ret; +} + gboolean _ostree_repo_write_ref (OstreeRepo *self, const char *remote, diff --git a/src/libostree/ostree-repo.h b/src/libostree/ostree-repo.h index 84b0deb8..992cf1e1 100644 --- a/src/libostree/ostree-repo.h +++ b/src/libostree/ostree-repo.h @@ -291,6 +291,12 @@ gboolean ostree_repo_list_refs (OstreeRepo *self, GCancellable *cancellable, GError **error); +gboolean ostree_repo_remote_list_refs (OstreeRepo *self, + const char *remote_name, + GHashTable **out_all_refs, + GCancellable *cancellable, + GError **error); + gboolean ostree_repo_load_variant (OstreeRepo *self, OstreeObjectType objtype, const char *sha256, diff --git a/src/ostree/ot-remote-builtin-refs.c b/src/ostree/ot-remote-builtin-refs.c index c3dbbf23..c1730bfa 100644 --- a/src/ostree/ot-remote-builtin-refs.c +++ b/src/ostree/ot-remote-builtin-refs.c @@ -34,8 +34,8 @@ ot_remote_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError GOptionContext *context; glnx_unref_object OstreeRepo *repo = NULL; const char *remote_name; - g_autoptr(GBytes) summary_bytes = NULL; gboolean ret = FALSE; + g_autoptr(GHashTable) refs = NULL; context = g_option_context_new ("NAME - List remote refs"); @@ -51,39 +51,19 @@ ot_remote_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError remote_name = argv[1]; - if (!ostree_repo_remote_fetch_summary (repo, remote_name, - &summary_bytes, NULL, - cancellable, error)) + if (!ostree_repo_remote_list_refs (repo, remote_name, &refs, cancellable, error)) goto out; - - if (summary_bytes == NULL) - { - g_print ("Remote refs not available; server has no summary file\n"); - } else { - g_autoptr(GVariant) summary = NULL; - g_autoptr(GVariant) ref_map = NULL; - GVariantIter iter; - GVariant *child; - - summary = g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT, - summary_bytes, FALSE); + g_autoptr(GList) ordered_keys = NULL; + GList *iter = NULL; - ref_map = g_variant_get_child_value (summary, 0); + ordered_keys = g_hash_table_get_keys (refs); + ordered_keys = g_list_sort (ordered_keys, (GCompareFunc) strcmp); - /* Ref map should already be sorted by ref name. */ - g_variant_iter_init (&iter, ref_map); - while ((child = g_variant_iter_next_value (&iter)) != NULL) + for (iter = ordered_keys; iter; iter = iter->next) { - const char *ref_name = NULL; - - g_variant_get_child (child, 0, "&s", &ref_name); - - if (ref_name != NULL) - g_print ("%s\n", ref_name); - - g_variant_unref (child); + g_print ("%s\n", (const char *) iter->data); } } |