[ostree] core: Add helper functions to convert (checksum, objtype) pair to/from string



commit 70afd6011f4eac75022010cb4687dc804631217a
Author: Colin Walters <walters verbum org>
Date:   Wed Mar 14 09:37:00 2012 -0400

    core: Add helper functions to convert (checksum, objtype) pair to/from string

 src/libostree/ostree-core.c   |   20 ++++++++++++++++++++
 src/libostree/ostree-core.h   |    7 +++++++
 src/libostree/ostree-repo.c   |   20 +++-----------------
 src/ostree/ot-builtin-prune.c |   23 ++++++++---------------
 4 files changed, 38 insertions(+), 32 deletions(-)
---
diff --git a/src/libostree/ostree-core.c b/src/libostree/ostree-core.c
index 5f938d1..263988c 100644
--- a/src/libostree/ostree-core.c
+++ b/src/libostree/ostree-core.c
@@ -565,6 +565,26 @@ ostree_object_type_from_string (const char *str)
 }
 
 char *
+ostree_object_to_string (const char *checksum,
+                         OstreeObjectType objtype)
+{
+  return g_strconcat (checksum, ".", ostree_object_type_to_string (objtype), NULL);
+}
+
+void
+ostree_object_from_string (const char *str,
+                           gchar     **out_checksum,
+                           OstreeObjectType *out_objtype)
+{
+  const char *dot;
+
+  dot = strrchr (str, '.');
+  g_assert (dot != NULL);
+  *out_checksum = g_strndup (str, dot - str);
+  *out_objtype = ostree_object_type_from_string (dot + 1);
+}
+
+char *
 ostree_get_relative_object_path (const char *checksum,
                                  OstreeObjectType type)
 {
diff --git a/src/libostree/ostree-core.h b/src/libostree/ostree-core.h
index e6893c9..a3abb3f 100644
--- a/src/libostree/ostree-core.h
+++ b/src/libostree/ostree-core.h
@@ -108,6 +108,13 @@ const char * ostree_object_type_to_string (OstreeObjectType objtype);
 
 OstreeObjectType ostree_object_type_from_string (const char *str);
 
+char * ostree_object_to_string (const char *checksum,
+                                OstreeObjectType objtype);
+
+void ostree_object_from_string (const char *str,
+                                gchar     **out_checksum,
+                                OstreeObjectType *out_objtype);
+
 char *ostree_get_relative_object_path (const char        *checksum,
                                        OstreeObjectType   type);
 
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index 05ebbc4..4c99d88 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -752,13 +752,6 @@ ostree_repo_get_file_object_path (OstreeRepo   *self,
   return ostree_repo_get_object_path (self, checksum, get_objtype_for_repo_file (self));
 }
 
-static char *
-create_checksum_and_objtype (const char *checksum,
-                             OstreeObjectType objtype)
-{
-  return g_strconcat (checksum, ".", ostree_object_type_to_string (objtype), NULL);
-}
-
 static GFile *
 get_pending_object_path (OstreeRepo       *self,
                          const char       *checksum,
@@ -847,7 +840,7 @@ insert_into_transaction (OstreeRepo        *self,
   OstreeRepoPrivate *priv = GET_PRIVATE (self);
   char *key;
 
-  key = create_checksum_and_objtype (checksum, objtype);
+  key = ostree_object_to_string (checksum, objtype);
   /* Takes ownership */
   g_hash_table_replace (priv->pending_transaction, key, NULL);
 }
@@ -1165,22 +1158,15 @@ ostree_repo_commit_transaction (OstreeRepo     *self,
   GHashTableIter iter;
   gpointer key, value;
   char *checksum = NULL;
+  OstreeObjectType objtype;
 
   g_return_val_if_fail (priv->in_transaction == TRUE, FALSE);
 
   g_hash_table_iter_init (&iter, priv->pending_transaction);
   while (g_hash_table_iter_next (&iter, &key, &value))
     {
-      const char *checksum_and_type = key;
-      const char *type_str;
-      OstreeObjectType objtype;
-
-      type_str = strrchr (checksum_and_type, '.');
-      g_assert (type_str);
       g_free (checksum);
-      checksum = g_strndup (checksum_and_type, type_str - checksum_and_type);
-
-      objtype = ostree_object_type_from_string (type_str + 1);
+      ostree_object_from_string ((char*)key, &checksum, &objtype);
 
       g_clear_object (&f);
       f = get_pending_object_path (self, checksum, objtype);
diff --git a/src/ostree/ot-builtin-prune.c b/src/ostree/ot-builtin-prune.c
index 30f21c4..5090036 100644
--- a/src/ostree/ot-builtin-prune.c
+++ b/src/ostree/ot-builtin-prune.c
@@ -69,13 +69,6 @@ typedef struct {
   guint n_unreachable;
 } OtPruneData;
 
-static char *
-create_checksum_and_objtype (const char *checksum,
-                             OstreeObjectType objtype)
-{
-  return g_strconcat (checksum, ".", ostree_object_type_to_string (objtype), NULL);
-}
-
 static gboolean
 compute_reachable_objects_from_dir_contents (OstreeRepo      *repo,
                                              const char      *sha256,
@@ -93,7 +86,7 @@ compute_reachable_objects_from_dir_contents (OstreeRepo      *repo,
   if (!ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_DIR_TREE, sha256, &tree, error))
     goto out;
 
-  key = create_checksum_and_objtype (sha256, OSTREE_OBJECT_TYPE_DIR_TREE);
+  key = ostree_object_to_string (sha256, OSTREE_OBJECT_TYPE_DIR_TREE);
   g_hash_table_replace (inout_reachable, key, key);
 
   /* PARSE OSTREE_SERIALIZED_TREE_VARIANT */
@@ -107,14 +100,14 @@ compute_reachable_objects_from_dir_contents (OstreeRepo      *repo,
       g_variant_get_child (files_variant, i, "(&s&s)", &filename, &checksum);
       if (ostree_repo_get_mode (repo) == OSTREE_REPO_MODE_BARE)
         {
-          key = create_checksum_and_objtype (checksum, OSTREE_OBJECT_TYPE_RAW_FILE);
+          key = ostree_object_to_string (checksum, OSTREE_OBJECT_TYPE_RAW_FILE);
           g_hash_table_replace (inout_reachable, key, key);
         }
       else
         {
-          key = create_checksum_and_objtype (checksum, OSTREE_OBJECT_TYPE_ARCHIVED_FILE_META);
+          key = ostree_object_to_string (checksum, OSTREE_OBJECT_TYPE_ARCHIVED_FILE_META);
           g_hash_table_replace (inout_reachable, key, key);
-          key = create_checksum_and_objtype (checksum, OSTREE_OBJECT_TYPE_ARCHIVED_FILE_CONTENT);
+          key = ostree_object_to_string (checksum, OSTREE_OBJECT_TYPE_ARCHIVED_FILE_CONTENT);
           g_hash_table_replace (inout_reachable, key, key);
         }
     }
@@ -134,7 +127,7 @@ compute_reachable_objects_from_dir_contents (OstreeRepo      *repo,
                                                         cancellable, error))
         goto out;
 
-      key = create_checksum_and_objtype (meta_checksum, OSTREE_OBJECT_TYPE_DIR_META);
+      key = ostree_object_to_string (meta_checksum, OSTREE_OBJECT_TYPE_DIR_META);
       g_hash_table_replace (inout_reachable, key, key);
     }
 
@@ -166,7 +159,7 @@ compute_reachable_objects_from_commit (OstreeRepo      *repo,
       if (!ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, sha256, &commit, error))
         goto out;
 
-      key = create_checksum_and_objtype (sha256, OSTREE_OBJECT_TYPE_COMMIT);
+      key = ostree_object_to_string (sha256, OSTREE_OBJECT_TYPE_COMMIT);
       g_hash_table_replace (inout_reachable, key, key);
 
       /* PARSE OSTREE_SERIALIZED_COMMIT_VARIANT */
@@ -184,7 +177,7 @@ compute_reachable_objects_from_commit (OstreeRepo      *repo,
         goto out;
 
       g_variant_get_child (commit, 7, "&s", &meta_checksum);
-      key = create_checksum_and_objtype (meta_checksum, OSTREE_OBJECT_TYPE_DIR_META);
+      key = ostree_object_to_string (meta_checksum, OSTREE_OBJECT_TYPE_DIR_META);
       g_hash_table_replace (inout_reachable, key, key);
     }
 
@@ -205,7 +198,7 @@ object_iter_callback (OstreeRepo    *repo,
   OtPruneData *data = user_data;
   char *key;
 
-  key = create_checksum_and_objtype (checksum, objtype);
+  key = ostree_object_to_string (checksum, objtype);
 
   if (!g_hash_table_lookup_extended (data->reachable, key, NULL, NULL))
     {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]