src/ostree: Don't delete refs having aliases
authorSinny Kumari <sinny@redhat.com>
Thu, 4 Oct 2018 13:48:05 +0000 (19:18 +0530)
committerAtomic Bot <atomic-devel@projectatomic.io>
Thu, 11 Oct 2018 13:41:32 +0000 (13:41 +0000)
Deleting a ref with aliases makes them dangling. In such
cases, display an error message to the user.

Fixes #1597

Signed-off-by: Sinny Kumari <sinny@redhat.com>
Closes: #1749
Approved by: cgwalters

src/ostree/ot-builtin-refs.c
tests/test-refs.sh

index 5c2214cc6f9ed5d1c3be395db646e70ba1aa8f24..f88d08a66bc80297d949dfc0ddef39053c403afe 100644 (file)
@@ -146,8 +146,10 @@ static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellab
 
   /* If we're doing aliasing, we need the full list of aliases mostly to allow
    * replacing existing aliases.
+   * If we are deleting a ref, we want to make sure that it doesn't have
+   * any corresponding aliases.
    */
-  if (opt_alias)
+  if (opt_alias || opt_delete)
     {
       if (!ostree_repo_list_refs_ext (repo, NULL, &ref_aliases,
                                       OSTREE_REPO_LIST_REFS_EXT_ALIASES,
@@ -245,7 +247,18 @@ static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellab
 
           if (!ostree_parse_refspec (refspec, &remote, &ref, error))
             goto out;
-          
+
+          /* Look for alias if it exists for a ref we want to delete */
+          GLNX_HASH_TABLE_FOREACH_KV (ref_aliases, const char *,
+                                      ref_alias, const char *, value)
+            {
+              if (!strcmp (ref, value))
+                {
+                  g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                               "Ref '%s' has an active alias: '%s'", ref, ref_alias);
+                  goto out;
+                }
+            }
           if (!ostree_repo_set_ref_immediate (repo, remote, ref, NULL,
                                               cancellable, error))
             goto out;
index f4fe1833bbcb798a60837b19a376efe95214f26f..1730423d4323d0ec13425f3b6803cc2f5f80b6e7 100755 (executable)
@@ -192,6 +192,11 @@ done
 ${CMD_PREFIX} ostree --repo=repo refs -A > refs.txt
 assert_file_has_content_literal refs.txt 'exampleos/x86_64/stable/server -> exampleos/x86_64/27/server'
 
+# Test that we don't delete a ref having aliases
+if ${CMD_PREFIX} ostree --repo=repo refs --delete exampleos/x86_64/27/server; then
+    assert_not_reached "refs --delete unexpectedly succeeded in deleting a ref containing alias!"
+fi
+
 ${CMD_PREFIX} ostree --repo=repo summary -u
 
 echo "ok ref symlink"