[ostree/wip/repo-file: 5/7] repo-file: Base OstreeRepoFile on trees instead of commits



commit afd06109006d27c8d61ca798ae9b7e2c9466d1f3
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Sep 6 23:27:45 2013 -0400

    repo-file: Base OstreeRepoFile on trees instead of commits
    
    We want an OstreeRepoFile to be the way to represent a filesystem
    tree inside an ostree repository, so change ostree_repo_file_new_root
    to take two tree IDs, and add a convenience method to replace
    the commit usages.

 src/libostree/ostree-repo-file.c |  156 +++++++++++++++++++++----------------
 src/libostree/ostree-repo-file.h |   19 +++--
 src/libostree/ostree-repo.c      |    5 +-
 src/ostree/ot-admin-deploy.c     |    5 +-
 src/ostree/ot-builtin-checkout.c |    7 +-
 5 files changed, 111 insertions(+), 81 deletions(-)
---
diff --git a/src/libostree/ostree-repo-file.c b/src/libostree/ostree-repo-file.c
index 994d554..71f945e 100644
--- a/src/libostree/ostree-repo-file.c
+++ b/src/libostree/ostree-repo-file.c
@@ -33,10 +33,6 @@ struct OstreeRepoFile
   GObject parent_instance;
 
   OstreeRepo *repo;
-
-  char *commit;
-  GError *commit_resolve_error;
-  
   OstreeRepoFile *parent;
   int index;
   char *name;
@@ -65,8 +61,6 @@ ostree_repo_file_finalize (GObject *object)
   g_free (self->cached_file_checksum);
   g_free (self->tree_contents_checksum);
   g_free (self->tree_metadata_checksum);
-  g_free (self->commit);
-  g_clear_error (&self->commit_resolve_error);
   g_free (self->name);
 
   G_OBJECT_CLASS (ostree_repo_file_parent_class)->finalize (object);
@@ -113,28 +107,81 @@ set_error_noent (GFile *self, GError **error)
 /**
  * ostree_repo_file_new_root:
  * @repo: Containing repo
- * @commit: SHA256 checksum
+ * @contents_checksum: SHA256 checksum for the contents
+ * @metadata_checksum: SHA256 checksum for the metadata
  *
- * Returns: (transfer full): A new #OstreeRepoFile corresponding to commit contents
+ * Returns: (transfer full): A new #OstreeRepoFile corresponding to
+ * the tree contents
  */
 GFile * 
-ostree_repo_file_new_root (OstreeRepo  *repo,
-                            const char  *commit)
+ostree_repo_file_new_root (OstreeRepo *repo,
+                           const char *contents_checksum,
+                           const char *metadata_checksum)
 {
   OstreeRepoFile *self;
 
   g_return_val_if_fail (repo != NULL, NULL);
-  g_return_val_if_fail (commit != NULL, NULL);
-  g_return_val_if_fail (strlen (commit) == 64, NULL);
+  g_return_val_if_fail (contents_checksum != NULL, NULL);
+  g_return_val_if_fail (strlen (contents_checksum) == 64, NULL);
+  g_return_val_if_fail (metadata_checksum != NULL, NULL);
+  g_return_val_if_fail (strlen (metadata_checksum) == 64, NULL);
 
   self = g_object_new (OSTREE_TYPE_REPO_FILE, NULL);
   self->repo = g_object_ref (repo);
-  self->commit = g_strdup (commit);
+  self->tree_contents_checksum = g_strdup (contents_checksum);
+  self->tree_metadata_checksum = g_strdup (metadata_checksum);
 
   return G_FILE (self);
 }
 
 /**
+ * ostree_repo_file_new_for_commit:
+ * @repo: An #OstreeRepo
+ * @commit: A checksum pointing to a commit object
+ * @error: Error
+ *
+ * Looks up @commit in @repo, and constructs an #OstreeRepoFile
+ * for the trees referenced by @commit.
+ *
+ * Returns: (transfer full): a new #OstreeRepoFile corresponding to
+ * the commit
+ */
+GFile *
+ostree_repo_file_new_for_commit (OstreeRepo  *repo,
+                                 const char  *commit,
+                                 GError     **error)
+{
+  GFile *ret = NULL;
+  gs_unref_variant GVariant *commit_v = NULL;
+  gs_unref_variant GVariant *tree_contents_csum_v = NULL;
+  gs_unref_variant GVariant *tree_metadata_csum_v = NULL;
+  char tree_contents_csum[65];
+  char tree_metadata_csum[65];
+
+  g_return_val_if_fail (repo != NULL, NULL);
+  g_return_val_if_fail (commit != NULL, NULL);
+  g_return_val_if_fail (strlen (commit) == 64, NULL);
+
+  if (!ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT,
+                                 commit, &commit_v, error))
+    goto out;
+
+  /* PARSE OSTREE_OBJECT_TYPE_COMMIT */
+  g_variant_get_child (commit_v, 6, "@ay", &tree_contents_csum_v);
+  ostree_checksum_inplace_from_bytes (g_variant_get_data (tree_contents_csum_v),
+                                      tree_contents_csum);
+
+  g_variant_get_child (commit_v, 7, "@ay", &tree_metadata_csum_v);
+  ostree_checksum_inplace_from_bytes (g_variant_get_data (tree_metadata_csum_v),
+                                      tree_metadata_csum);
+
+  ret = ostree_repo_file_new_root (repo, tree_contents_csum, tree_metadata_csum);
+
+ out:
+  return ret;
+}
+
+/**
  * ostree_repo_file_new_child:
  * @parent:
  * @name: Name for new child
@@ -143,7 +190,7 @@ ostree_repo_file_new_root (OstreeRepo  *repo,
  */
 GFile *
 ostree_repo_file_new_child (OstreeRepoFile *parent,
-                             const char  *name)
+                            const char     *name)
 {
   OstreeRepoFile *self;
   size_t len;
@@ -160,47 +207,27 @@ ostree_repo_file_new_child (OstreeRepoFile *parent,
 }
 
 static gboolean
-do_resolve_commit (OstreeRepoFile  *self,
-                   GError         **error)
+do_resolve (OstreeRepoFile  *self,
+            GError         **error)
 {
   gboolean ret = FALSE;
-  gs_unref_variant GVariant *commit = NULL;
   gs_unref_variant GVariant *root_contents = NULL;
   gs_unref_variant GVariant *root_metadata = NULL;
-  gs_unref_variant GVariant *tree_contents_csum_v = NULL;
-  gs_unref_variant GVariant *tree_metadata_csum_v = NULL;
-  char tmp_checksum[65];
 
   g_assert (self->parent == NULL);
 
-  if (!ostree_repo_load_variant (self->repo, OSTREE_OBJECT_TYPE_COMMIT,
-                                 self->commit, &commit, error))
-    goto out;
-
-  /* PARSE OSTREE_OBJECT_TYPE_COMMIT */
-  g_variant_get_child (commit, 6, "@ay", &tree_contents_csum_v);
-
-  ostree_checksum_inplace_from_bytes (g_variant_get_data (tree_contents_csum_v), tmp_checksum);
-
   if (!ostree_repo_load_variant (self->repo, OSTREE_OBJECT_TYPE_DIR_TREE,
-                                 tmp_checksum,
-                                 &root_contents, error))
+                                 self->tree_contents_checksum, &root_contents, error))
     goto out;
 
-  g_variant_get_child (commit, 7, "@ay", &tree_metadata_csum_v);
-  ostree_checksum_inplace_from_bytes (g_variant_get_data (tree_metadata_csum_v), tmp_checksum);
-
   if (!ostree_repo_load_variant (self->repo, OSTREE_OBJECT_TYPE_DIR_META,
-                                 tmp_checksum,
-                                 &root_metadata, error))
+                                 self->tree_metadata_checksum, &root_metadata, error))
     goto out;
   
   self->tree_metadata = root_metadata;
   root_metadata = NULL;
   self->tree_contents = root_contents;
   root_contents = NULL;
-  self->tree_contents_checksum = ostree_checksum_from_bytes_v (tree_contents_csum_v);
-  self->tree_metadata_checksum = ostree_checksum_from_bytes_v (tree_metadata_csum_v);
 
   ret = TRUE;
  out:
@@ -217,12 +244,12 @@ do_resolve_nonroot (OstreeRepoFile     *self,
   gs_unref_variant GVariant *container = NULL;
   gs_unref_variant GVariant *tree_contents = NULL;
   gs_unref_variant GVariant *tree_metadata = NULL;
-  gs_unref_variant GVariant *content_csum_v = NULL;
+  gs_unref_variant GVariant *contents_csum_v = NULL;
   gs_unref_variant GVariant *metadata_csum_v = NULL;
   gs_free char *tmp_checksum = NULL;
 
   i = ostree_repo_file_tree_find_child (self->parent, self->name, &is_dir, &container);
-  
+
   if (i < 0)
     {
       set_error_noent ((GFile*)self, error);
@@ -239,10 +266,10 @@ do_resolve_nonroot (OstreeRepoFile     *self,
       g_clear_pointer (&files_variant, (GDestroyNotify) g_variant_unref);
 
       g_variant_get_child (container, i, "(&s ay@ay)",
-                           &name, &content_csum_v, &metadata_csum_v);
+                           &name, &contents_csum_v, &metadata_csum_v);
 
       g_free (tmp_checksum);
-      tmp_checksum = ostree_checksum_from_bytes_v (content_csum_v);
+      tmp_checksum = ostree_checksum_from_bytes_v (contents_csum_v);
       if (!ostree_repo_load_variant (self->repo, OSTREE_OBJECT_TYPE_DIR_TREE,
                                      tmp_checksum, &tree_contents,
                                      error))
@@ -259,7 +286,7 @@ do_resolve_nonroot (OstreeRepoFile     *self,
       tree_contents = NULL;
       self->tree_metadata = tree_metadata;
       tree_metadata = NULL;
-      self->tree_contents_checksum = ostree_checksum_from_bytes_v (content_csum_v);
+      self->tree_contents_checksum = ostree_checksum_from_bytes_v (contents_csum_v);
       self->tree_metadata_checksum = ostree_checksum_from_bytes_v (metadata_csum_v);
     }
   else
@@ -274,36 +301,25 @@ gboolean
 ostree_repo_file_ensure_resolved (OstreeRepoFile  *self,
                                    GError         **error)
 {
-  if (self->commit_resolve_error != NULL)
-    goto out;
+  gboolean ret = FALSE;
 
   if (self->parent == NULL)
     {
       if (self->tree_contents == NULL)
-        (void)do_resolve_commit (self, &(self->commit_resolve_error));
+        if (!do_resolve (self, error))
+          goto out;
     }
   else if (self->index == -1)
     {
       if (!ostree_repo_file_ensure_resolved (self->parent, error))
         goto out;
-      (void)do_resolve_nonroot (self, &(self->commit_resolve_error));
-    }
-  
- out:
-  if (self->commit_resolve_error)
-    {
-      if (error)
-       *error = g_error_copy (self->commit_resolve_error);
-      return FALSE;
+      if (!do_resolve_nonroot (self, error))
+        goto out;
     }
-  else
-    return TRUE;
-}
 
-const char *
-ostree_repo_file_get_commit (OstreeRepoFile  *self)
-{
-  return ostree_repo_file_get_root (self)->commit;
+  ret = TRUE;
+ out:
+  return ret;
 }
 
 gboolean
@@ -518,7 +534,10 @@ ostree_repo_file_get_uri (GFile *file)
   path = gs_file_get_path_cached (file);
   uri_path = g_filename_to_uri (path, NULL, NULL);
   g_assert (g_str_has_prefix (uri_path, "file://"));
-  ret = g_strconcat ("ostree://", self->commit, uri_path+strlen("file://"), NULL);
+  ret = g_strconcat ("ostree://",
+                     self->tree_contents_checksum, "/", self->tree_metadata_checksum,
+                     uri_path+strlen("file://"),
+                     NULL);
   g_free (uri_path);
 
   return ret;
@@ -546,7 +565,7 @@ ostree_repo_file_dup (GFile *file)
   if (self->parent)
     return ostree_repo_file_new_child (self->parent, self->name);
   else
-    return ostree_repo_file_new_root (self->repo, self->commit);
+    return ostree_repo_file_new_root (self->repo, self->tree_contents_checksum, 
self->tree_metadata_checksum);
 }
 
 static guint
@@ -557,7 +576,7 @@ ostree_repo_file_hash (GFile *file)
   if (self->parent)
     return g_file_hash (self->parent) + g_str_hash (self->name);
   else
-    return g_str_hash (self->commit);
+    return g_str_hash (self->tree_contents_checksum) + g_str_hash (self->tree_metadata_checksum);
 }
 
 static gboolean
@@ -569,12 +588,13 @@ ostree_repo_file_equal (GFile *file1,
 
   if (self1->parent && self2->parent)
     {
-      return g_str_equal (self1->name, self2->name)
-        && g_file_equal ((GFile*)self1->parent, (GFile*)self2->parent);
+      return (g_str_equal (self1->name, self2->name) &&
+              g_file_equal ((GFile*)self1->parent, (GFile*)self2->parent));
     }
   else if (!self1->parent && !self2->parent)
     {
-      return g_str_equal (self1->commit, self2->commit);
+      return (g_str_equal (self1->tree_contents_checksum, self2->tree_contents_checksum) &&
+              g_str_equal (self1->tree_metadata_checksum, self2->tree_metadata_checksum));
     }
   else
     return FALSE;
diff --git a/src/libostree/ostree-repo-file.h b/src/libostree/ostree-repo-file.h
index f554be1..eb5615f 100644
--- a/src/libostree/ostree-repo-file.h
+++ b/src/libostree/ostree-repo-file.h
@@ -42,11 +42,16 @@ struct _OstreeRepoFileClass
 
 GType   ostree_repo_file_get_type (void) G_GNUC_CONST;
 
-GFile * ostree_repo_file_new_root (OstreeRepo  *repo,
-                                    const char  *commit);
+GFile * ostree_repo_file_new_root (OstreeRepo *repo,
+                                   const char *contents_checksum,
+                                   const char *metadata_checksum);
+
+GFile * ostree_repo_file_new_for_commit (OstreeRepo  *repo,
+                                         const char  *commit,
+                                         GError     **error);
 
 GFile * ostree_repo_file_new_child (OstreeRepoFile *parent,
-                                     const char  *name);
+                                    const char     *name);
 
 gboolean ostree_repo_file_ensure_resolved (OstreeRepoFile  *self,
                                             GError         **error);
@@ -68,12 +73,11 @@ void ostree_repo_file_tree_set_metadata (OstreeRepoFile *self,
 const char *ostree_repo_file_tree_get_contents_checksum (OstreeRepoFile  *self);
 const char *ostree_repo_file_tree_get_metadata_checksum (OstreeRepoFile  *self);
 
-gboolean ostree_repo_file_is_tree (OstreeRepoFile  *self);
+GVariant *ostree_repo_file_tree_get_contents (OstreeRepoFile *self);
+GVariant *ostree_repo_file_tree_get_metadata (OstreeRepoFile *self);
 
 const char * ostree_repo_file_get_checksum (OstreeRepoFile  *self);
 
-const char * ostree_repo_file_get_commit (OstreeRepoFile  *self);
-
 GFile *ostree_repo_file_nontree_get_local (OstreeRepoFile  *self);
 
 int     ostree_repo_file_tree_find_child  (OstreeRepoFile  *self,
@@ -89,8 +93,5 @@ gboolean ostree_repo_file_tree_query_child (OstreeRepoFile  *self,
                                              GCancellable    *cancellable,
                                              GError         **error);
 
-GVariant *ostree_repo_file_tree_get_contents (OstreeRepoFile  *self);
-GVariant *ostree_repo_file_tree_get_metadata (OstreeRepoFile  *self);
-
 G_END_DECLS
 
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index bb52798..03cc05d 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -1348,7 +1348,10 @@ ostree_repo_read_commit (OstreeRepo *self,
   if (!ostree_repo_resolve_rev (self, rev, FALSE, &resolved_rev, error))
     goto out;
 
-  ret_root = ostree_repo_file_new_root (self, resolved_rev);
+  ret_root = ostree_repo_file_new_for_commit (self, resolved_rev, error);
+  if (!ret_root)
+    goto out;
+
   if (!ostree_repo_file_ensure_resolved ((OstreeRepoFile*)ret_root, error))
     goto out;
 
diff --git a/src/ostree/ot-admin-deploy.c b/src/ostree/ot-admin-deploy.c
index 9379b50..9d4059e 100644
--- a/src/ostree/ot-admin-deploy.c
+++ b/src/ostree/ot-admin-deploy.c
@@ -216,7 +216,10 @@ checkout_deployment_tree (GFile             *sysroot,
   gs_unref_object GFile *deploy_target_path = NULL;
   gs_unref_object GFile *deploy_parent = NULL;
 
-  root = (OstreeRepoFile*)ostree_repo_file_new_root (repo, csum);
+  root = (OstreeRepoFile*)ostree_repo_file_new_for_commit (repo, csum, error);
+  if (!root)
+    goto out;
+
   if (!ostree_repo_file_ensure_resolved (root, error))
     goto out;
 
diff --git a/src/ostree/ot-builtin-checkout.c b/src/ostree/ot-builtin-checkout.c
index 873e2c5..15a6b8c 100644
--- a/src/ostree/ot-builtin-checkout.c
+++ b/src/ostree/ot-builtin-checkout.c
@@ -60,10 +60,13 @@ process_one_checkout (OstreeRepo           *repo,
   gs_unref_object OstreeRepoFile *subtree = NULL;
   gs_unref_object GFileInfo *file_info = NULL;
 
-  root = (OstreeRepoFile*)ostree_repo_file_new_root (repo, resolved_commit);
+  root = (OstreeRepoFile*)ostree_repo_file_new_for_commit (repo, resolved_commit, error);
+  if (!root)
+    goto out;
+
   if (!ostree_repo_file_ensure_resolved (root, error))
     goto out;
-  
+
   if (subpath)
     subtree = (OstreeRepoFile*)g_file_resolve_relative_path ((GFile*)root, subpath);
   else


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