[ostree] core: Drop some dead code from packfile writing, expose GInputStream API too



commit 702c38739ee3efe1aaf321b9601cf420ad319659
Author: Colin Walters <walters verbum org>
Date:   Wed Nov 30 22:15:05 2011 -0500

    core: Drop some dead code from packfile writing, expose GInputStream API too
    
    We never actually dropped into the bits to write metadata as packfiles,
    because such a thing doesn't exist.
    
    Also add a GInputStream-based API for writing packfiles.

 src/libostree/ostree-core.c |  228 ++++++++++++++++++++++---------------------
 src/libostree/ostree-core.h |   16 ++-
 src/libostree/ostree-repo.c |   11 ++-
 3 files changed, 135 insertions(+), 120 deletions(-)
---
diff --git a/src/libostree/ostree-core.c b/src/libostree/ostree-core.c
index 49b3d43..ace9067 100644
--- a/src/libostree/ostree-core.c
+++ b/src/libostree/ostree-core.c
@@ -562,144 +562,150 @@ ostree_get_relative_object_path (const char *checksum,
 }
 
 gboolean
-ostree_pack_object (GOutputStream     *output,
-                    GFile             *file,
-                    OstreeObjectType  objtype,
-                    GCancellable     *cancellable,
-                    GError          **error)
+ostree_pack_file_for_input (GOutputStream     *output,
+                            GFileInfo         *finfo,
+                            GInputStream      *instream,
+                            GVariant          *xattrs,
+                            GCancellable     *cancellable,
+                            GError          **error)
 {
   gboolean ret = FALSE;
-  GFileInfo *finfo = NULL;
-  GFileInputStream *instream = NULL;
+  guint32 uid, gid, mode;
+  guint32 device = 0;
+  guint32 metadata_size_be;
+  const char *target = NULL;
+  guint64 object_size;
   gboolean pack_builder_initialized = FALSE;
   GVariantBuilder pack_builder;
   GVariant *pack_variant = NULL;
-  GVariant *xattrs = NULL;
   gsize bytes_written;
 
-  finfo = g_file_query_info (file, "standard::type,standard::size,standard::is-symlink,standard::symlink-target,unix::*",
-                             G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable, error);
-  if (!finfo)
-    goto out;
+  uid = g_file_info_get_attribute_uint32 (finfo, G_FILE_ATTRIBUTE_UNIX_UID);
+  gid = g_file_info_get_attribute_uint32 (finfo, G_FILE_ATTRIBUTE_UNIX_GID);
+  mode = g_file_info_get_attribute_uint32 (finfo, G_FILE_ATTRIBUTE_UNIX_MODE);
 
-  if (objtype == OSTREE_OBJECT_TYPE_META)
-    {
-      guint64 object_size_be = GUINT64_TO_BE ((guint64)g_file_info_get_size (finfo));
-      if (!g_output_stream_write_all (output, &object_size_be, 8, &bytes_written, cancellable, error))
-        goto out;
+  g_variant_builder_init (&pack_builder, G_VARIANT_TYPE (OSTREE_PACK_FILE_VARIANT_FORMAT));
+  pack_builder_initialized = TRUE;
+  g_variant_builder_add (&pack_builder, "u", GUINT32_TO_BE (0));
+  g_variant_builder_add (&pack_builder, "u", GUINT32_TO_BE (uid));
+  g_variant_builder_add (&pack_builder, "u", GUINT32_TO_BE (gid));
+  g_variant_builder_add (&pack_builder, "u", GUINT32_TO_BE (mode));
 
-      instream = g_file_read (file, NULL, error);
-      if (!instream)
-        goto out;
-      
-      if (g_output_stream_splice (output, (GInputStream*)instream, 0, cancellable, error) < 0)
-        goto out;
+  g_variant_builder_add (&pack_builder, "@a(ayay)", xattrs);
+
+  if (S_ISREG (mode))
+    {
+      object_size = (guint64)g_file_info_get_size (finfo);
     }
-  else
+  else if (S_ISLNK (mode))
+    {
+      target = g_file_info_get_attribute_byte_string (finfo, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
+      object_size = strlen (target);
+    }
+  else if (S_ISBLK (mode) || S_ISCHR (mode))
+    {
+      device = g_file_info_get_attribute_uint32 (finfo, G_FILE_ATTRIBUTE_UNIX_RDEV);
+      object_size = 4;
+    }
+  else if (S_ISFIFO (mode))
     {
-      guint32 uid, gid, mode;
-      guint32 device = 0;
-      guint32 metadata_size_be;
-      const char *target = NULL;
-      guint64 object_size;
+      object_size = 0;
+    }
+  else
+    g_assert_not_reached ();
 
-      uid = g_file_info_get_attribute_uint32 (finfo, G_FILE_ATTRIBUTE_UNIX_UID);
-      gid = g_file_info_get_attribute_uint32 (finfo, G_FILE_ATTRIBUTE_UNIX_GID);
-      mode = g_file_info_get_attribute_uint32 (finfo, G_FILE_ATTRIBUTE_UNIX_MODE);
+  g_variant_builder_add (&pack_builder, "t", GUINT64_TO_BE (object_size));
+  pack_variant = g_variant_builder_end (&pack_builder);
+  pack_builder_initialized = FALSE;
 
-      g_variant_builder_init (&pack_builder, G_VARIANT_TYPE (OSTREE_PACK_FILE_VARIANT_FORMAT));
-      pack_builder_initialized = TRUE;
-      g_variant_builder_add (&pack_builder, "u", GUINT32_TO_BE (0));
-      g_variant_builder_add (&pack_builder, "u", GUINT32_TO_BE (uid));
-      g_variant_builder_add (&pack_builder, "u", GUINT32_TO_BE (gid));
-      g_variant_builder_add (&pack_builder, "u", GUINT32_TO_BE (mode));
+  metadata_size_be = GUINT32_TO_BE (g_variant_get_size (pack_variant));
 
-      xattrs = ostree_get_xattrs_for_file (file, error);
-      if (!xattrs)
-        goto out;
-      g_variant_builder_add (&pack_builder, "@a(ayay)", xattrs);
+  if (!g_output_stream_write_all (output, &metadata_size_be, 4,
+                                  &bytes_written, cancellable, error))
+    goto out;
+  g_assert (bytes_written == 4);
 
-      if (S_ISREG (mode))
-        {
-          object_size = (guint64)g_file_info_get_size (finfo);
-        }
-      else if (S_ISLNK (mode))
-        {
-          target = g_file_info_get_attribute_byte_string (finfo, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
-          object_size = strlen (target);
-        }
-      else if (S_ISBLK (mode) || S_ISCHR (mode))
-        {
-          device = g_file_info_get_attribute_uint32 (finfo, G_FILE_ATTRIBUTE_UNIX_RDEV);
-          object_size = 4;
-        }
-      else if (S_ISFIFO (mode))
+  if (!g_output_stream_write_all (output, g_variant_get_data (pack_variant), g_variant_get_size (pack_variant),
+                                  &bytes_written, cancellable, error))
+    goto out;
+
+  if (S_ISREG (mode))
+    {
+      bytes_written = g_output_stream_splice (output, (GInputStream*)instream, 0, cancellable, error);
+      if (bytes_written < 0)
+        goto out;
+      if (bytes_written != object_size)
         {
-          object_size = 0;
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "File size changed unexpectedly");
+          goto out;
         }
-      else
-        g_assert_not_reached ();
-
-      g_variant_builder_add (&pack_builder, "t", GUINT64_TO_BE (object_size));
-      pack_variant = g_variant_builder_end (&pack_builder);
-      pack_builder_initialized = FALSE;
-
-      metadata_size_be = GUINT32_TO_BE (g_variant_get_size (pack_variant));
-
-      if (!g_output_stream_write_all (output, &metadata_size_be, 4,
+    }
+  else if (S_ISLNK (mode))
+    {
+      if (!g_output_stream_write_all (output, target, object_size,
                                       &bytes_written, cancellable, error))
         goto out;
-      g_assert (bytes_written == 4);
-
-      if (!g_output_stream_write_all (output, g_variant_get_data (pack_variant), g_variant_get_size (pack_variant),
+    }
+  else if (S_ISBLK (mode) || S_ISCHR (mode))
+    {
+      guint32 device_be = GUINT32_TO_BE (device);
+      g_assert (object_size == 4);
+      if (!g_output_stream_write_all (output, &device_be, object_size,
                                       &bytes_written, cancellable, error))
         goto out;
+      g_assert (bytes_written == 4);
+    }
+  else if (S_ISFIFO (mode))
+    {
+    }
+  else
+    g_assert_not_reached ();
 
-      if (S_ISREG (mode))
-        {
-          instream = g_file_read (file, NULL, error);
-          if (!instream)
-            goto out;
-          bytes_written = g_output_stream_splice (output, (GInputStream*)instream, 0, cancellable, error);
-          if (bytes_written < 0)
-            goto out;
-          if (bytes_written != object_size)
-            {
-              g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                           "File size changed unexpectedly");
-              goto out;
-            }
-        }
-      else if (S_ISLNK (mode))
-        {
-          if (!g_output_stream_write_all (output, target, object_size,
-                                          &bytes_written, cancellable, error))
-            goto out;
-        }
-      else if (S_ISBLK (mode) || S_ISCHR (mode))
-        {
-          guint32 device_be = GUINT32_TO_BE (device);
-          g_assert (object_size == 4);
-          if (!g_output_stream_write_all (output, &device_be, object_size,
-                                          &bytes_written, cancellable, error))
-            goto out;
-          g_assert (bytes_written == 4);
-        }
-      else if (S_ISFIFO (mode))
-        {
-        }
-      else
-        g_assert_not_reached ();
+  ret = TRUE;
+ out:
+  if (pack_builder_initialized)
+    g_variant_builder_clear (&pack_builder);
+  ot_clear_gvariant (&pack_variant);
+  return ret;
+}
+
+gboolean
+ostree_pack_file (GOutputStream     *output,
+                  GFile             *file,
+                  GCancellable     *cancellable,
+                  GError          **error)
+{
+  gboolean ret = FALSE;
+  GFileInfo *finfo = NULL;
+  GInputStream *instream = NULL;
+  GVariant *xattrs = NULL;
+  gsize bytes_written;
+
+  finfo = g_file_query_info (file, "standard::type,standard::size,standard::is-symlink,standard::symlink-target,unix::*",
+                             G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable, error);
+  if (!finfo)
+    goto out;
+
+  if (S_ISREG (g_file_info_get_attribute_uint32 (finfo, "unix::mode")))
+    {
+      instream = (GInputStream*)g_file_read (file, cancellable, error);
+      if (!instream)
+        goto out;
     }
   
+  xattrs = ostree_get_xattrs_for_file (file, error);
+  if (!xattrs)
+    goto out;
+  
+  if (!ostree_pack_file_for_input (output, finfo, instream, xattrs, cancellable, error))
+    goto out;
+  
   ret = TRUE;
  out:
   g_clear_object (&finfo);
   g_clear_object (&instream);
   ot_clear_gvariant (&xattrs);
-  if (pack_builder_initialized)
-    g_variant_builder_clear (&pack_builder);
-  ot_clear_gvariant (&pack_variant);
   return ret;
 }
 
diff --git a/src/libostree/ostree-core.h b/src/libostree/ostree-core.h
index 038fb51..98bb365 100644
--- a/src/libostree/ostree-core.h
+++ b/src/libostree/ostree-core.h
@@ -152,11 +152,17 @@ gboolean ostree_get_directory_metadata (GFile *dir,
  */
 #define OSTREE_PACK_FILE_VARIANT_FORMAT "(uuuua(ayay)t)"
 
-gboolean  ostree_pack_object (GOutputStream     *output,
-                              GFile             *file,
-                              OstreeObjectType  objtype,
-                              GCancellable     *cancellable,
-                              GError          **error);
+gboolean  ostree_pack_file (GOutputStream     *output,
+                            GFile             *file,
+                            GCancellable     *cancellable,
+                            GError          **error);
+
+gboolean  ostree_pack_file_for_input (GOutputStream     *output,
+                                      GFileInfo         *finfo,
+                                      GInputStream      *input,
+                                      GVariant          *xattrs,
+                                      GCancellable     *cancellable,
+                                      GError          **error);
 
 gboolean ostree_parse_packed_file (GFile            *file,
                                    GVariant    **out_metadata,
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index f58eec4..4a6f59c 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -877,7 +877,6 @@ static gboolean
 archive_file_trusted (OstreeRepo   *self,
                       GFile        *file,
                       const char   *checksum,
-                      OstreeObjectType objtype,
                       gboolean      overwrite,
                       gboolean     *did_exist,
                       GError      **error)
@@ -885,9 +884,11 @@ archive_file_trusted (OstreeRepo   *self,
   GFileOutputStream *out = NULL;
   gboolean ret = FALSE;
   GFile *dest_file = NULL;
+  GFileInfo *finfo = NULL;
+  GInputStream *input = NULL;
   GError *temp_error = NULL;
 
-  if (!prepare_dir_for_checksum_get_object_path (self, checksum, objtype, &dest_file, error))
+  if (!prepare_dir_for_checksum_get_object_path (self, checksum, OSTREE_OBJECT_TYPE_FILE, &dest_file, error))
     goto out;
 
   if (overwrite)
@@ -915,7 +916,7 @@ archive_file_trusted (OstreeRepo   *self,
 
   if (out)
     {
-      if (!ostree_pack_object ((GOutputStream*)out, file, objtype, NULL, error))
+      if (!ostree_pack_file ((GOutputStream*)out, file, NULL, error))
         goto out;
       
       if (!g_output_stream_close ((GOutputStream*)out, NULL, error))
@@ -926,6 +927,8 @@ archive_file_trusted (OstreeRepo   *self,
  out:
   g_clear_object (&dest_file);
   g_clear_object (&out);
+  g_clear_object (&finfo);
+  g_clear_object (&input);
   return ret;
 }
   
@@ -940,7 +943,7 @@ ostree_repo_store_object_trusted (OstreeRepo   *self,
 {
   OstreeRepoPrivate *priv = GET_PRIVATE (self);
   if (priv->archive && objtype == OSTREE_OBJECT_TYPE_FILE)
-    return archive_file_trusted (self, file, checksum, objtype, overwrite, did_exist, error);
+    return archive_file_trusted (self, file, checksum, overwrite, did_exist, error);
   else
     return link_object_trusted (self, file, checksum, objtype, overwrite, did_exist, error);
 }



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