[ostree/wip/fetch-meta: 2/2] WIP: Fetch detached metadata



commit 6c38da9835cfde4cb92fbdb2a8b205fe5e06ec8d
Author: Colin Walters <walters verbum org>
Date:   Tue Sep 24 13:23:19 2013 -0400

    WIP: Fetch detached metadata

 src/libostree/ostree-repo-commit.c |   10 +++-
 src/libostree/ostree-repo-pull.c   |   86 +++++++++++++++++++++++++++++-------
 src/ostree/ot-builtin-show.c       |    8 +++-
 tests/pull-test.sh                 |    8 +++
 4 files changed, 92 insertions(+), 20 deletions(-)
---
diff --git a/src/libostree/ostree-repo-commit.c b/src/libostree/ostree-repo-commit.c
index a2bd263..fe44956 100644
--- a/src/libostree/ostree-repo-commit.c
+++ b/src/libostree/ostree-repo-commit.c
@@ -1256,8 +1256,10 @@ GFile *
 _ostree_repo_get_commit_metadata_loose_path (OstreeRepo        *self,
                                              const char        *checksum)
 {
-  gs_free char *commit_path = ostree_get_relative_object_path (checksum, OSTREE_OBJECT_TYPE_COMMIT, FALSE);
-  return ot_gfile_resolve_path_printf (self->repodir, "%smeta", commit_path);
+  char buf[_OSTREE_LOOSE_PATH_MAX];
+  _ostree_loose_path_with_suffix (buf, checksum, OSTREE_OBJECT_TYPE_COMMIT, self->mode,
+                                  "meta");
+  return g_file_resolve_relative_path (self->objects_dir, buf);
 }
 
 /**
@@ -1328,6 +1330,10 @@ ostree_repo_write_commit_detached_metadata (OstreeRepo      *self,
   gs_unref_object GFile *metadata_path =
     _ostree_repo_get_commit_metadata_loose_path (self, checksum);
 
+  if (!_ostree_repo_ensure_loose_objdir_at (self->objects_dir_fd, checksum,
+                                            cancellable, error))
+    goto out;
+
   if (!g_file_replace_contents (metadata_path,
                                 g_variant_get_data (metadata),
                                 g_variant_get_size (metadata),
diff --git a/src/libostree/ostree-repo-pull.c b/src/libostree/ostree-repo-pull.c
index 306bfe9..68462e1 100644
--- a/src/libostree/ostree-repo-pull.c
+++ b/src/libostree/ostree-repo-pull.c
@@ -56,6 +56,7 @@
 #include "config.h"
 
 #include "ostree.h"
+#include "ostree-core-private.h"
 #include "ostree-repo-private.h"
 #include "ostree-fetcher.h"
 #include "otutil.h"
@@ -65,6 +66,7 @@ typedef struct {
     PULL_MSG_SCAN_IDLE,
     PULL_MSG_MAIN_IDLE,
     PULL_MSG_FETCH,
+    PULL_MSG_FETCH_DETACHED_METADATA,
     PULL_MSG_SCAN,
     PULL_MSG_QUIT
   } t;
@@ -121,6 +123,7 @@ typedef struct {
   OtPullData  *pull_data;
   GVariant    *object;
   GFile       *temp_path;
+  gboolean     is_detached_meta;
 } FetchObjectData;
 
 static SoupURI *
@@ -232,6 +235,7 @@ pull_worker_message_new (int msgtype, gpointer data)
       break;
     case PULL_MSG_SCAN:
     case PULL_MSG_FETCH:
+    case PULL_MSG_FETCH_DETACHED_METADATA:
       msg->d.item = data;
       break;
     case PULL_MSG_QUIT:
@@ -666,23 +670,52 @@ meta_fetch_on_complete (GObject           *object,
   GError *local_error = NULL;
   GError **error = &local_error;
 
+  ostree_object_name_deserialize (fetch_data->object, &checksum, &objtype);
+  g_debug ("fetch of %s complete", ostree_object_to_string (checksum, objtype));
+
   fetch_data->temp_path = ostree_fetcher_request_uri_with_partial_finish ((OstreeFetcher*)object, result, 
error);
   if (!fetch_data->temp_path)
-    goto out;
-
-  ostree_object_name_deserialize (fetch_data->object, &checksum, &objtype);
+    {
+      if (!g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        goto out;
+      else if (fetch_data->is_detached_meta)
+        {
+          /* There isn't any detached metadata, just fetch the commit */
+          g_clear_error (&local_error);
+          ot_waitable_queue_push (pull_data->metadata_objects_to_fetch,
+                                  pull_worker_message_new (PULL_MSG_FETCH,
+                                                           g_variant_ref (fetch_data->object)));
+        }
 
-  g_debug ("fetch of %s complete", ostree_object_to_string (checksum, objtype));
+      goto out;
+    }
 
-  if (!ot_util_variant_map (fetch_data->temp_path, ostree_metadata_variant_type (objtype),
-                            FALSE, &metadata, error))
-    goto out;
+  if (fetch_data->is_detached_meta)
+    {
+      if (!ot_util_variant_map (fetch_data->temp_path, G_VARIANT_TYPE ("a{sv}"),
+                                FALSE, &metadata, error))
+        goto out;
+      if (!ostree_repo_write_commit_detached_metadata (pull_data->repo, checksum, metadata,
+                                                       pull_data->cancellable, error))
+        goto out;
 
-  ostree_repo_write_metadata_async (pull_data->repo, objtype, checksum, metadata,
-                                    pull_data->cancellable,
-                                    on_metadata_writed, fetch_data);
+      /* A bit ugly, we're sending a message to ourself */
+      ot_waitable_queue_push (pull_data->metadata_objects_to_fetch,
+                              pull_worker_message_new (PULL_MSG_FETCH,
+                                                       g_variant_ref (fetch_data->object)));
+    }
+  else
+    {
+      if (!ot_util_variant_map (fetch_data->temp_path, ostree_metadata_variant_type (objtype),
+                                FALSE, &metadata, error))
+        goto out;
+      
+      ostree_repo_write_metadata_async (pull_data->repo, objtype, checksum, metadata,
+                                        pull_data->cancellable,
+                                        on_metadata_writed, fetch_data);
+      pull_data->n_outstanding_metadata_write_requests++;
+    }
 
-  pull_data->n_outstanding_metadata_write_requests++;
  out:
   pull_data->n_outstanding_metadata_fetches--;
   pull_data->n_fetched_metadata++;
@@ -769,9 +802,14 @@ scan_one_metadata_object (OtPullData         *pull_data,
       char *duped_checksum = g_strdup (tmp_checksum);
       g_hash_table_insert (pull_data->requested_metadata, duped_checksum, duped_checksum);
       
-      ot_waitable_queue_push (pull_data->metadata_objects_to_fetch,
-                              pull_worker_message_new (PULL_MSG_FETCH,
-                                                       g_variant_ref (object)));
+      if (objtype == OSTREE_OBJECT_TYPE_COMMIT)
+        ot_waitable_queue_push (pull_data->metadata_objects_to_fetch,
+                                pull_worker_message_new (PULL_MSG_FETCH_DETACHED_METADATA,
+                                                         g_variant_ref (object)));
+      else
+        ot_waitable_queue_push (pull_data->metadata_objects_to_fetch,
+                                pull_worker_message_new (PULL_MSG_FETCH,
+                                                         g_variant_ref (object)));
     }
   else if (is_stored)
     {
@@ -948,7 +986,7 @@ on_metadata_objects_to_fetch_ready (gint         fd,
                                   pull_worker_message_new (PULL_MSG_MAIN_IDLE, GUINT_TO_POINTER 
(pull_data->idle_serial)));
         }
     }
-  else if (msg->t == PULL_MSG_FETCH)
+  else if (msg->t == PULL_MSG_FETCH || msg->t == PULL_MSG_FETCH_DETACHED_METADATA)
     {
       const char *checksum;
       gs_free char *objpath = NULL;
@@ -956,10 +994,23 @@ on_metadata_objects_to_fetch_ready (gint         fd,
       SoupURI *obj_uri = NULL;
       gboolean is_meta;
       FetchObjectData *fetch_data;
+      gboolean is_detached_meta;
+
+      is_detached_meta = msg->t == PULL_MSG_FETCH_DETACHED_METADATA;
       
       ostree_object_name_deserialize (msg->d.item, &checksum, &objtype);
-      objpath = ostree_get_relative_object_path (checksum, objtype, TRUE);
-      obj_uri = suburi_new (pull_data->base_uri, objpath, NULL);
+      if (is_detached_meta)
+        {
+          char buf[_OSTREE_LOOSE_PATH_MAX];
+          _ostree_loose_path_with_suffix (buf, checksum, OSTREE_OBJECT_TYPE_COMMIT,
+                                          pull_data->remote_mode, "meta");
+          obj_uri = suburi_new (pull_data->base_uri, "objects", buf, NULL);
+        }
+      else
+        {
+          objpath = ostree_get_relative_object_path (checksum, objtype, TRUE);
+          obj_uri = suburi_new (pull_data->base_uri, objpath, NULL);
+        }
 
       is_meta = OSTREE_OBJECT_TYPE_IS_META (objtype);
       if (is_meta)
@@ -975,6 +1026,7 @@ on_metadata_objects_to_fetch_ready (gint         fd,
       fetch_data = g_new (FetchObjectData, 1);
       fetch_data->pull_data = pull_data;
       fetch_data->object = g_variant_ref (msg->d.item);
+      fetch_data->is_detached_meta = is_detached_meta;
       ostree_fetcher_request_uri_with_partial_async (pull_data->fetcher, obj_uri, pull_data->cancellable,
                                         is_meta ? meta_fetch_on_complete : content_fetch_on_complete, 
fetch_data);
       soup_uri_free (obj_uri);
diff --git a/src/ostree/ot-builtin-show.c b/src/ostree/ot-builtin-show.c
index 09aaead..6409ba1 100644
--- a/src/ostree/ot-builtin-show.c
+++ b/src/ostree/ot-builtin-show.c
@@ -124,10 +124,16 @@ do_print_metadata_key (OstreeRepo     *repo,
       if (!ostree_repo_read_commit_detached_metadata (repo, resolved_rev, &metadata,
                                                       NULL, error))
         goto out;
+      if (metadata == NULL)
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
+                       "No detached metadata for commit %s", resolved_rev);
+          goto out;
+        }
     }
   
   if (!g_variant_lookup (metadata, key, "&s", &value))
-    goto out;
+        goto out;
 
   g_print ("%s\n", value);
 
diff --git a/tests/pull-test.sh b/tests/pull-test.sh
index a049dd7..dd93b88 100755
--- a/tests/pull-test.sh
+++ b/tests/pull-test.sh
@@ -31,3 +31,11 @@ cd checkout-origin-main
 assert_file_has_content firstfile '^first$'
 assert_file_has_content baz/cow '^moo$'
 echo "ok pull contents"
+
+cd ${test_tmpdir}
+ostree --repo=ostree-srv/gnomerepo commit -b main -s "Metadata string" 
--add-detached-metadata-string=SIGNATURE=HANCOCK --tree=ref=main
+${CMD_PREFIX} ostree --repo=repo pull origin main
+${CMD_PREFIX} ostree --repo=repo fsck
+$OSTREE show --print-detached-metadata-key=SIGNATURE main > main-meta
+assert_file_has_content main-meta "HANCOCK"
+echo "ok pull detached metadata"


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