repo: new function ostree_repo_prune_static_deltas
authorGiuseppe Scrivano <gscrivan@redhat.com>
Tue, 15 Dec 2015 09:32:25 +0000 (10:32 +0100)
committerGiuseppe Scrivano <gscrivan@redhat.com>
Fri, 18 Dec 2015 10:21:57 +0000 (11:21 +0100)
Extract existing code from ostree_repo_prune and add an argument COMMIT,
that controls which commit purge.  If not set, the old behavior is kept.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
src/libostree/ostree-repo-prune.c
src/libostree/ostree-repo.h

index 86949f305b1de230a0149324e7f45ac5dd4bb2ef..166e67e00b7367ddd2eab6d11282fce72490895e 100644 (file)
@@ -112,6 +112,78 @@ maybe_prune_loose_object (OtPruneData        *data,
   return ret;
 }
 
+/**
+ * ostree_repo_prune_static_deltas:
+ * @self: Repo
+ * @commit: (allow-none): ASCII SHA256 checksum for commit, or %NULL for each
+ * non existing commit
+ * @cancellable: Cancellable
+ * @error: Error
+ *
+ * Prune static deltas, if COMMIT is specified then delete static delta files only
+ * targeting that commit; otherwise any static delta of non existing commits are
+ * deleted.
+ */
+gboolean
+ostree_repo_prune_static_deltas (OstreeRepo *self, const char *commit,
+                                 GCancellable      *cancellable,
+                                 GError           **error)
+{
+  gboolean ret = FALSE;
+  g_autoptr(GPtrArray) deltas = NULL;
+  guint i;
+
+  if (!ostree_repo_list_static_delta_names (self, &deltas,
+                                            cancellable, error))
+    goto out;
+
+  for (i = 0; i < deltas->len; i++)
+    {
+      const char *deltaname = deltas->pdata[i];
+      const char *dash = strchr (deltaname, '-');
+      const char *to = NULL;
+      gboolean have_commit;
+      g_autofree char *from = NULL;
+      g_autofree char *deltadir = NULL;
+
+      if (!dash)
+        {
+          to = deltaname;
+        }
+      else
+        {
+          from = g_strndup (deltaname, dash - deltaname);
+          to = dash + 1;
+        }
+
+      if (commit)
+        {
+          if (g_strcmp0 (to, commit))
+            continue;
+        }
+      else
+        {
+          if (!ostree_repo_has_object (self, OSTREE_OBJECT_TYPE_COMMIT,
+                                       to, &have_commit,
+                                       cancellable, error))
+            goto out;
+
+          if (have_commit)
+            continue;
+        }
+
+      deltadir = _ostree_get_relative_static_delta_path (from, to, NULL);
+
+      if (!gs_shutil_rm_rf_at (self->repo_dir_fd, deltadir,
+                               cancellable, error))
+        goto out;
+    }
+
+  ret = TRUE;
+ out:
+  return ret;
+}
+
 /**
  * ostree_repo_prune:
  * @self: Repo
@@ -220,47 +292,8 @@ ostree_repo_prune (OstreeRepo        *self,
         goto out;
     }
 
-  { g_autoptr(GPtrArray) deltas = NULL;
-    guint i;
-
-    if (!ostree_repo_list_static_delta_names (self, &deltas,
-                                              cancellable, error))
-      goto out;
-
-    for (i = 0; i < deltas->len; i++)
-      {
-        const char *deltaname = deltas->pdata[i];
-        const char *dash = strchr (deltaname, '-');
-        const char *to = NULL;
-        gboolean have_commit;
-        g_autofree char *from = NULL;
-        g_autofree char *deltadir = NULL;
-
-        if (!dash)
-          {
-            to = deltaname;
-          }
-        else
-          {
-            from = g_strndup (deltaname, dash - deltaname);
-            to = dash + 1;
-          }
-
-        if (!ostree_repo_has_object (self, OSTREE_OBJECT_TYPE_COMMIT,
-                                     to, &have_commit,
-                                     cancellable, error))
-          goto out;
-
-        if (have_commit)
-          continue;
-
-        deltadir = _ostree_get_relative_static_delta_path (from, to, NULL);
-
-        if (!gs_shutil_rm_rf_at (self->repo_dir_fd, deltadir,
-                                 cancellable, error))
-          goto out;
-      }
-  }
+  if (!ostree_repo_prune_static_deltas (self, NULL, cancellable, error))
+    goto out;
 
   ret = TRUE;
   *out_objects_total = (data.n_reachable_meta + data.n_unreachable_meta +
index 51a407517fc19a7ed50d9e87147c90edf0b150f1..1680de8bd8f1294d11d6fcf67e4a5343a73be655 100644 (file)
@@ -691,6 +691,11 @@ typedef enum {
   OSTREE_REPO_PRUNE_FLAGS_REFS_ONLY
 } OstreeRepoPruneFlags;
 
+gboolean
+ostree_repo_prune_static_deltas (OstreeRepo *self, const char *commit,
+                                 GCancellable      *cancellable,
+                                 GError           **error);
+
 gboolean ostree_repo_prune (OstreeRepo        *self,
                             OstreeRepoPruneFlags   flags,
                             gint               depth,