diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c index 17f3c7b4..7f0eceff 100644 --- a/src/libostree/ostree-repo.c +++ b/src/libostree/ostree-repo.c @@ -1375,6 +1375,86 @@ create_empty_gvariant_dict (void) return g_variant_builder_end (&builder); } +static gboolean +enumerate_refs_recurse (OstreeRepo *repo, + GFile *base, + GFile *dir, + GHashTable *refs, + GCancellable *cancellable, + GError **error) +{ + gboolean ret = FALSE; + GFileInfo *file_info = NULL; + GFileEnumerator *enumerator = NULL; + GFile *child = NULL; + GError *temp_error = NULL; + + enumerator = g_file_enumerate_children (dir, OSTREE_GIO_FAST_QUERYINFO, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + cancellable, error); + if (!enumerator) + goto out; + + while ((file_info = g_file_enumerator_next_file (enumerator, cancellable, &temp_error)) != NULL) + { + g_clear_object (&child); + child = g_file_get_child (dir, g_file_info_get_name (file_info)); + if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY) + { + if (!enumerate_refs_recurse (repo, base, child, refs, cancellable, error)) + goto out; + } + else if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_REGULAR) + { + char *contents; + gsize len; + + if (!g_file_load_contents (child, cancellable, &contents, &len, NULL, error)) + goto out; + + g_strchomp (contents); + + g_hash_table_insert (refs, g_file_get_relative_path (base, child), contents); + } + + g_clear_object (&file_info); + } + if (temp_error != NULL) + { + g_propagate_error (error, temp_error); + goto out; + } + + ret = TRUE; + out: + g_clear_object (&file_info); + g_clear_object (&child); + return ret; +} + +gboolean +ostree_repo_list_all_refs (OstreeRepo *repo, + GHashTable **out_all_refs, + GCancellable *cancellable, + GError **error) +{ + gboolean ret = FALSE; + GHashTable *ret_all_refs = NULL; + GFile *heads_dir = NULL; + + ret_all_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + heads_dir = g_file_resolve_relative_path (ostree_repo_get_path (repo), "refs/heads"); + if (!enumerate_refs_recurse (repo, heads_dir, heads_dir, ret_all_refs, cancellable, error)) + goto out; + + ret = TRUE; + ot_transfer_out_value (out_all_refs, &ret_all_refs); + out: + g_clear_object (&heads_dir); + 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 19494464..ca1fecf1 100644 --- a/src/libostree/ostree-repo.h +++ b/src/libostree/ostree-repo.h @@ -130,6 +130,11 @@ gboolean ostree_repo_write_ref (OstreeRepo *self, const char *rev, GError **error); +gboolean ostree_repo_list_all_refs (OstreeRepo *repo, + GHashTable **out_all_refs, + GCancellable *cancellable, + GError **error); + gboolean ostree_repo_load_variant (OstreeRepo *self, OstreeObjectType expected_type, const char *sha256, diff --git a/src/ostree/ot-builtin-prune.c b/src/ostree/ot-builtin-prune.c index 5bfc74a4..d747d690 100644 --- a/src/ostree/ot-builtin-prune.c +++ b/src/ostree/ot-builtin-prune.c @@ -225,84 +225,6 @@ object_iter_callback (OstreeRepo *repo, g_free (key); } -static gboolean -add_refs_recurse (OstreeRepo *repo, - GFile *base, - GFile *dir, - GHashTable *refs, - GCancellable *cancellable, - GError **error) -{ - gboolean ret = FALSE; - GFileInfo *file_info = NULL; - GFileEnumerator *enumerator = NULL; - GFile *child = NULL; - GError *temp_error = NULL; - - enumerator = g_file_enumerate_children (dir, OSTREE_GIO_FAST_QUERYINFO, - G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, - cancellable, error); - if (!enumerator) - goto out; - - while ((file_info = g_file_enumerator_next_file (enumerator, cancellable, &temp_error)) != NULL) - { - g_clear_object (&child); - child = g_file_get_child (dir, g_file_info_get_name (file_info)); - if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY) - { - if (!add_refs_recurse (repo, base, child, refs, cancellable, error)) - goto out; - } - else if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_REGULAR) - { - char *contents; - gsize len; - - if (!g_file_load_contents (child, cancellable, &contents, &len, NULL, error)) - goto out; - - g_strchomp (contents); - - g_hash_table_insert (refs, g_file_get_relative_path (base, child), contents); - } - - g_clear_object (&file_info); - } - if (temp_error != NULL) - { - g_propagate_error (error, temp_error); - goto out; - } - - ret = TRUE; - out: - g_clear_object (&file_info); - g_clear_object (&child); - return ret; -} - -static gboolean -list_all_refs (OstreeRepo *repo, - GHashTable **out_all_refs, - GCancellable *cancellable, - GError **error) -{ - gboolean ret = FALSE; - GHashTable *ret_all_refs = NULL; - GFile *heads_dir = NULL; - - ret_all_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - - heads_dir = g_file_resolve_relative_path (ostree_repo_get_path (repo), "refs/heads"); - if (!add_refs_recurse (repo, heads_dir, heads_dir, ret_all_refs, cancellable, error)) - goto out; - - ret = TRUE; - ot_transfer_out_value (out_all_refs, &ret_all_refs); - out: - return ret; -} gboolean ostree_builtin_prune (int argc, char **argv, GFile *repo_path, GError **error) @@ -333,7 +255,7 @@ ostree_builtin_prune (int argc, char **argv, GFile *repo_path, GError **error) data.n_reachable = 0; data.n_unreachable = 0; - if (!list_all_refs (repo, &all_refs, cancellable, error)) + if (!ostree_repo_list_all_refs (repo, &all_refs, cancellable, error)) goto out; g_hash_table_iter_init (&hash_iter, all_refs); diff --git a/tests/t0000-basic.sh b/tests/t0000-basic.sh index 2a00b91a..c3cd7a51 100755 --- a/tests/t0000-basic.sh +++ b/tests/t0000-basic.sh @@ -19,7 +19,7 @@ set -e -echo "1..24" +echo "1..25" . libtest.sh @@ -181,3 +181,7 @@ EOF cd ${test_tmpdir}/checkout-test2-4 $OSTREE commit -b test2 -s "with statoverride" --statoverride=../test-statoverride.txt echo "ok commit statoverridde" + +cd ${test_tmpdir} +$OSTREE prune +echo "ok prune didn't fail"