[ostree/wip/libarchive: 1/2] wip



commit 67c72da1bcb88640f1103c1bdb408bb9cc4c0087
Author: Colin Walters <walters verbum org>
Date:   Wed Nov 30 20:15:00 2011 -0500

    wip

 Makefile-libostree.am                          |   11 +
 configure.ac                                   |   19 ++-
 src/libostree/ostree-libarchive-input-stream.c |  186 +++++++++++++
 src/libostree/ostree-libarchive-input-stream.h |   68 +++++
 src/libostree/ostree-repo.c                    |  351 +++++++++++++++++++++++-
 src/libostree/ostree-repo.h                    |   31 ++-
 src/ostree/ot-builtin-commit.c                 |   41 ++-
 7 files changed, 671 insertions(+), 36 deletions(-)
---
diff --git a/Makefile-libostree.am b/Makefile-libostree.am
index 98a2504..b2b42c6 100644
--- a/Makefile-libostree.am
+++ b/Makefile-libostree.am
@@ -31,5 +31,16 @@ libostree_la_SOURCES = src/libostree/ostree.h \
 	src/libostree/ostree-checkout.c \
 	src/libostree/ostree-checkout.h \
 	$(NULL)
+if USE_LIBARCHIVE
+libostree_la_SOURCES += src/libostree/ostree-libarchive-input-stream.h \
+	src/libostree/ostree-libarchive-input-stream.c \
+	$(NULL)
+endif
+
 libostree_la_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/src/libotutil -I$(srcdir)/src/libostree -DLOCALEDIR=\"$(datadir)/locale\" $(OT_DEP_GIO_UNIX_CFLAGS)
 libostree_la_LIBADD = libotutil.la $(OT_DEP_GIO_UNIX_LIBS)
+
+if USE_LIBARCHIVE
+libostree_la_CFLAGS += $(OT_DEP_LIBARCHIVE_CFLAGS)
+libostree_la_LIBADD += $(OT_DEP_LIBARCHIVE_LIBS)
+endif
diff --git a/configure.ac b/configure.ac
index 36a3cc3..5a38beb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -26,6 +26,7 @@ PKG_PROG_PKG_CONFIG
 
 GIO_DEPENDENCY="gio-unix-2.0 >= 2.28"
 SOUP_DEPENDENCY="libsoup-gnome-2.4 >= 2.34.0"
+LIBARCHIVE_DEPENDENCY="libarchive >= 2.8.0"
 
 PKG_CHECK_MODULES(OT_DEP_GIO_UNIX, $GIO_DEPENDENCY)
 
@@ -43,9 +44,25 @@ if test x$with_soup_gnome != xno; then
 	with_soup_gnome=no
     fi		
 fi
-
 AM_CONDITIONAL(USE_LIBSOUP_GNOME, test $with_soup_gnome != no)
 
+AC_ARG_WITH(libarchive,
+	    AS_HELP_STRING([--without-libarchive], [Do not use libarchive]),
+	    :, with_libarchive=maybe)
+if test x$with_libarchive != xno; then
+    PKG_CHECK_EXISTS($LIBARCHIVE_DEPENDENCY, have_libarchive=yes, have_libarchive=no)
+    if test x$have_libarchive = xno && test x$with_libarchive != xmaybe; then
+       AC_MSG_ERROR([libarchive is enabled but could not be found])
+    fi
+    if test x$have_libarchive = xyes; then
+        AC_DEFINE(HAVE_LIBARCHIVE, 1, [Define if we have libarchive.pc])
+	PKG_CHECK_MODULES(OT_DEP_LIBARCHIVE, $LIBARCHIVE_DEPENDENCY)
+    else
+	with_libarchive=no
+    fi		
+fi
+AM_CONDITIONAL(USE_LIBARCHIVE, test $with_libarchive != no)
+
 AM_PATH_PYTHON
 
 AC_CONFIG_FILES([
diff --git a/src/libostree/ostree-libarchive-input-stream.c b/src/libostree/ostree-libarchive-input-stream.c
new file mode 100644
index 0000000..f27a7a0
--- /dev/null
+++ b/src/libostree/ostree-libarchive-input-stream.c
@@ -0,0 +1,186 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ * 
+ * Copyright (C) 2011 Colin Walters <walters verbum org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+
+#include <archive.h>
+#include <gio/gio.h>
+#include "ostree-libarchive-input-stream.h"
+
+enum {
+  PROP_0,
+  PROP_ARCHIVE
+};
+
+G_DEFINE_TYPE (OstreeLibarchiveInputStream, ostree_libarchive_input_stream, G_TYPE_INPUT_STREAM)
+
+struct _OstreeLibarchiveInputStreamPrivate {
+  struct archive *archive;
+};
+
+static void     ostree_libarchive_input_stream_set_property (GObject              *object,
+						  guint                 prop_id,
+						  const GValue         *value,
+						  GParamSpec           *pspec);
+static void     ostree_libarchive_input_stream_get_property (GObject              *object,
+						  guint                 prop_id,
+						  GValue               *value,
+						  GParamSpec           *pspec);
+static gssize   ostree_libarchive_input_stream_read         (GInputStream         *stream,
+						  void                 *buffer,
+						  gsize                 count,
+						  GCancellable         *cancellable,
+						  GError              **error);
+static gboolean ostree_libarchive_input_stream_close        (GInputStream         *stream,
+						  GCancellable         *cancellable,
+						  GError              **error);
+
+static void
+ostree_libarchive_input_stream_finalize (GObject *object)
+{
+  G_OBJECT_CLASS (ostree_libarchive_input_stream_parent_class)->finalize (object);
+}
+
+static void
+ostree_libarchive_input_stream_class_init (OstreeLibarchiveInputStreamClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
+  
+  g_type_class_add_private (klass, sizeof (OstreeLibarchiveInputStreamPrivate));
+
+  gobject_class->get_property = ostree_libarchive_input_stream_get_property;
+  gobject_class->set_property = ostree_libarchive_input_stream_set_property;
+  gobject_class->finalize = ostree_libarchive_input_stream_finalize;
+
+  stream_class->read_fn = ostree_libarchive_input_stream_read;
+  stream_class->close_fn = ostree_libarchive_input_stream_close;
+
+  /**
+   * OstreeLibarchiveInputStream:archive:
+   *
+   * The archive that the stream reads from.
+   */
+  g_object_class_install_property (gobject_class,
+				   PROP_ARCHIVE,
+				   g_param_spec_pointer ("archive",
+							 "", "",
+							 G_PARAM_READWRITE |
+							 G_PARAM_CONSTRUCT_ONLY |
+							 G_PARAM_STATIC_STRINGS));
+
+}
+
+static void
+ostree_libarchive_input_stream_set_property (GObject         *object,
+					     guint            prop_id,
+					     const GValue    *value,
+					     GParamSpec      *pspec)
+{
+  OstreeLibarchiveInputStream *self;
+  
+  self = OSTREE_LIBARCHIVE_INPUT_STREAM (object);
+
+  switch (prop_id)
+    {
+    case PROP_ARCHIVE:
+      self->priv->archive = g_value_get_pointer (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+ostree_libarchive_input_stream_get_property (GObject    *object,
+					     guint       prop_id,
+					     GValue     *value,
+					     GParamSpec *pspec)
+{
+  OstreeLibarchiveInputStream *self;
+
+  self = OSTREE_LIBARCHIVE_INPUT_STREAM (object);
+
+  switch (prop_id)
+    {
+    case PROP_ARCHIVE:
+      g_value_set_pointer (value, self->priv->archive);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ostree_libarchive_input_stream_init (OstreeLibarchiveInputStream *self)
+{
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+					    OSTREE_TYPE_LIBARCHIVE_INPUT_STREAM,
+					    OstreeLibarchiveInputStreamPrivate);
+
+}
+
+GInputStream *
+ostree_libarchive_input_stream_new (struct archive *a)
+{
+  OstreeLibarchiveInputStream *stream;
+
+  stream = g_object_new (OSTREE_TYPE_LIBARCHIVE_INPUT_STREAM,
+			 "archive", a,
+			 NULL);
+
+  return G_INPUT_STREAM (stream);
+}
+
+static gssize
+ostree_libarchive_input_stream_read (GInputStream  *stream,
+				     void          *buffer,
+				     gsize          count,
+				     GCancellable  *cancellable,
+				     GError       **error)
+{
+  OstreeLibarchiveInputStream *self;
+  gssize res = -1;
+
+  self = OSTREE_LIBARCHIVE_INPUT_STREAM (stream);
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return -1;
+
+  res = archive_read_data (self->priv->archive, buffer, count);
+  if (res < 0)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "%s", archive_error_string (self->priv->archive));
+    }
+
+  return res;
+}
+
+static gboolean
+ostree_libarchive_input_stream_close (GInputStream  *stream,
+				      GCancellable  *cancellable,
+				      GError       **error)
+{
+  return TRUE;
+}
+
diff --git a/src/libostree/ostree-libarchive-input-stream.h b/src/libostree/ostree-libarchive-input-stream.h
new file mode 100644
index 0000000..e31b1ec
--- /dev/null
+++ b/src/libostree/ostree-libarchive-input-stream.h
@@ -0,0 +1,68 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2011 Colin Walters <walters verbum org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Alexander Larsson <alexl redhat com>
+ */
+
+#ifndef __OSTREE_LIBARCHIVE_INPUT_STREAM_H__
+#define __OSTREE_LIBARCHIVE_INPUT_STREAM_H__
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define OSTREE_TYPE_LIBARCHIVE_INPUT_STREAM         (ostree_libarchive_input_stream_get_type ())
+#define OSTREE_LIBARCHIVE_INPUT_STREAM(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), OSTREE_TYPE_LIBARCHIVE_INPUT_STREAM, OstreeLibarchiveInputStream))
+#define OSTREE_LIBARCHIVE_INPUT_STREAM_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), OSTREE_TYPE_LIBARCHIVE_INPUT_STREAM, OstreeLibarchiveInputStreamClass))
+#define OSTREE_IS_LIBARCHIVE_INPUT_STREAM(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), OSTREE_TYPE_LIBARCHIVE_INPUT_STREAM))
+#define OSTREE_IS_LIBARCHIVE_INPUT_STREAM_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), OSTREE_TYPE_LIBARCHIVE_INPUT_STREAM))
+#define OSTREE_LIBARCHIVE_INPUT_STREAM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), OSTREE_TYPE_LIBARCHIVE_INPUT_STREAM, OstreeLibarchiveInputStreamClass))
+
+typedef struct _OstreeLibarchiveInputStream         OstreeLibarchiveInputStream;
+typedef struct _OstreeLibarchiveInputStreamClass    OstreeLibarchiveInputStreamClass;
+typedef struct _OstreeLibarchiveInputStreamPrivate  OstreeLibarchiveInputStreamPrivate;
+
+struct _OstreeLibarchiveInputStream
+{
+  GInputStream parent_instance;
+
+  /*< private >*/
+  OstreeLibarchiveInputStreamPrivate *priv;
+};
+
+struct _OstreeLibarchiveInputStreamClass
+{
+  GInputStreamClass parent_class;
+
+  /*< private >*/
+  /* Padding for future expansion */
+  void (*_g_reserved1) (void);
+  void (*_g_reserved2) (void);
+  void (*_g_reserved3) (void);
+  void (*_g_reserved4) (void);
+  void (*_g_reserved5) (void);
+};
+
+GType          ostree_libarchive_input_stream_get_type     (void) G_GNUC_CONST;
+
+GInputStream * ostree_libarchive_input_stream_new          (struct archive  *a);
+
+G_END_DECLS
+
+#endif /* __OSTREE_LIBARCHIVE_INPUT_STREAM_H__ */
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index 3d3cfbe..7299be9 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -31,6 +31,12 @@
 #include <gio/gunixoutputstream.h>
 #include <gio/gunixinputstream.h>
 
+#ifdef HAVE_LIBARCHIVE
+#include <archive.h>
+#include <archive_entry.h>
+#include "ostree-libarchive-input-stream.h"
+#endif
+
 enum {
   PROP_0,
 
@@ -722,6 +728,9 @@ import_directory_meta (OstreeRepo   *self,
   GChecksum *ret_checksum = NULL;
   GVariant *dirmeta = NULL;
 
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
   dirmeta = ostree_create_directory_metadata (file_info, xattrs);
   
   if (!import_gvariant_object (self, OSTREE_SERIALIZED_DIRMETA_VARIANT, 
@@ -1301,16 +1310,16 @@ import_directory_recurse (OstreeRepo           *self,
 }
 
 gboolean      
-ostree_repo_commit (OstreeRepo *self,
-                    const char   *branch,
-                    const char   *parent,
-                    const char   *subject,
-                    const char   *body,
-                    GVariant     *metadata,
-                    GFile        *dir,
-                    GChecksum   **out_commit,
-                    GCancellable *cancellable,
-                    GError      **error)
+ostree_repo_commit_directory (OstreeRepo *self,
+                              const char   *branch,
+                              const char   *parent,
+                              const char   *subject,
+                              const char   *body,
+                              GVariant     *metadata,
+                              GFile        *dir,
+                              GChecksum   **out_commit,
+                              GCancellable *cancellable,
+                              GError      **error)
 {
   OstreeRepoPrivate *priv = GET_PRIVATE (self);
   gboolean ret = FALSE;
@@ -1346,7 +1355,329 @@ ostree_repo_commit (OstreeRepo *self,
   ot_clear_checksum (&root_metadata_checksum);
   ot_clear_checksum (&root_contents_checksum);
   return ret;
+}
+
+#ifdef HAVE_LIBARCHIVE
+
+static void
+propagate_libarchive_error (GError      **error,
+                            struct archive *a)
+{
+  g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+               "%s", archive_error_string (a));
+}
+
+static GFileInfo *
+file_info_from_archive_entry (struct archive_entry  *entry)
+{
+  GFileInfo *info = g_file_info_new ();
+  guint32 mode;
+  guint32 file_type;
+
+  mode = archive_entry_mode (entry);
+  file_type = ot_gfile_type_for_mode (mode);
+  g_file_info_set_attribute_boolean (info, "standard::is-symlink", S_ISLNK (mode));
+  g_file_info_set_attribute_uint32 (info, "standard::type", file_type);
+  g_file_info_set_attribute_uint32 (info, "unix::uid", archive_entry_uid (entry));
+  g_file_info_set_attribute_uint32 (info, "unix::gid", archive_entry_gid (entry));
+  g_file_info_set_attribute_uint32 (info, "unix::mode", mode);
+
+  if (file_type == G_FILE_TYPE_REGULAR)
+    {
+      g_file_info_set_attribute_uint64 (info, "standard::size", (guint64) archive_entry_size (entry));
+    }
+  else if (file_type == G_FILE_TYPE_SYMBOLIC_LINK)
+    {
+      g_file_info_set_attribute_byte_string (info, "standard::symlink-target", archive_entry_symlink (entry));
+    }
+  else if (file_type == G_FILE_TYPE_SPECIAL)
+    {
+      g_file_info_set_attribute_uint32 (info, "unix::rdev", archive_entry_rdev (entry));
+    }
+
+  return info;
+}
+
+static gboolean
+import_libarchive_entry_file_to_packed (OstreeRepo           *self,
+                                        struct archive       *a,
+                                        struct archive_entry *entry,
+                                        GFileInfo            *file_info,
+                                        GChecksum           **out_checksum,
+                                        GCancellable         *cancellable,
+                                        GError              **error)
+{
+  gboolean ret = FALSE;
+  OstreeRepoPrivate *priv = GET_PRIVATE (self);
+  GFile *temp_file = NULL;
+  GInputStream *archive_stream = NULL;
+  GOutputStream *temp_out = NULL;
+  GChecksum *ret_checksum = NULL;
+  gboolean did_exist;
+  
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
+  if (!ostree_create_temp_regular_file (priv->tmp_dir,
+                                        "archive-tmp-", NULL,
+                                        &temp_file, &temp_out,
+                                        cancellable, error))
+        goto out;
+  
+  if (S_ISREG (g_file_info_get_attribute_uint32 (file_info, "unix::mode")))
+    archive_stream = ostree_libarchive_input_stream_new (a);
+      
+  if (!ostree_pack_file_for_input (temp_out, file_info, archive_stream, 
+                                   NULL, &ret_checksum, cancellable, error))
+    goto out;
+
+  if (!g_output_stream_close (temp_out, cancellable, error))
+    goto out;
+  
+  if (!link_object_trusted (self, temp_file, g_checksum_get_string (ret_checksum),
+                            OSTREE_OBJECT_TYPE_FILE,
+                            FALSE, &did_exist, cancellable, error))
+    goto out;
+
+  ret = TRUE;
+  *out_checksum = ret_checksum;
+  ret_checksum = NULL;
+ out:
+  if (temp_file)
+    (void) unlink (ot_gfile_get_path_cached (temp_file));
+  g_clear_object (&temp_file);
+  g_clear_object (&temp_out);
+  g_clear_object (&archive_stream);
+  ot_clear_checksum (&ret_checksum);
+  return ret;
+}
+
+static gboolean
+import_libarchive_entry_file (OstreeRepo           *self,
+                              struct archive       *a,
+                              struct archive_entry *entry,
+                              GFileInfo            *file_info,
+                              GChecksum           **out_checksum,
+                              GCancellable         *cancellable,
+                              GError              **error)
+{
+  gboolean ret = FALSE;
+  OstreeRepoPrivate *priv = GET_PRIVATE (self);
+  GFile *temp_file = NULL;
+  GInputStream *archive_stream = NULL;
+  GChecksum *ret_checksum = NULL;
+  gboolean did_exist;
+  guint32 mode;
+  
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
+  mode = g_file_info_get_attribute_uint32 (file_info, "unix::mode");
+  if (S_ISREG (mode))
+    archive_stream = ostree_libarchive_input_stream_new (a);
+  
+  if (!ostree_create_temp_file_from_input (priv->tmp_dir, "file-", NULL,
+                                           file_info, NULL, archive_stream, 
+                                           OSTREE_OBJECT_TYPE_FILE, &temp_file,
+                                           &ret_checksum, cancellable, error))
+    goto out;
+  
+  if (!link_object_trusted (self, temp_file, g_checksum_get_string (ret_checksum),
+                            OSTREE_OBJECT_TYPE_FILE, FALSE, &did_exist,
+                            cancellable, error))
+    goto out;
+
+  ret = TRUE;
+  *out_checksum = ret_checksum;
+  ret_checksum = NULL;
+ out:
+  if (temp_file)
+    (void) unlink (ot_gfile_get_path_cached (temp_file));
+  g_clear_object (&temp_file);
+  g_clear_object (&archive_stream);
+  ot_clear_checksum (&ret_checksum);
+  return ret;
+}
+  
+static gboolean
+import_libarchive (OstreeRepo           *self,
+                   GFile                *archive_f,
+                   GChecksum           **out_contents_checksum,
+                   GChecksum           **out_metadata_checksum,
+                   GCancellable         *cancellable,
+                   GError              **error)
+{
+  gboolean ret = FALSE;
+  OstreeRepoPrivate *priv = GET_PRIVATE (self);
+  GChecksum *ret_contents_checksum = NULL;
+  GChecksum *ret_metadata_checksum = NULL;
+  struct archive *a;
+  struct archive_entry *entry;
+  GFileInfo *file_info = NULL;
+  GHashTable *file_checksums = NULL;
+  GHashTable *dir_metadata_checksums = NULL;
+  GHashTable *dir_contents_checksums = NULL;
+  GHashTable *dir_contents = NULL;
+  GChecksum *tmp_checksum = NULL;
+  char *parent_path = NULL;
+
+  a = archive_read_new ();
+  archive_read_support_format_all (a);
+  if (archive_read_open_filename (a, ot_gfile_get_path_cached (archive_f), 8192) != ARCHIVE_OK)
+    {
+      propagate_libarchive_error (error, a);
+      goto out;
+    }
+
+  file_checksums = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+  dir_metadata_checksums = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+  dir_contents_checksums = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+  dir_contents = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_ptr_array_unref);
+
+  g_hash_table_insert (dir_contents, g_strdup ("/"), g_ptr_array_new_with_free_func (g_free));
+
+  while (archive_read_next_header (a, &entry) == ARCHIVE_OK)
+    {
+      const char *pathname;
+      guint32 mode;
+      GPtrArray *parent_contents;
+
+      g_clear_object (&file_info);
+      file_info = file_info_from_archive_entry (entry);
+      pathname = archive_entry_pathname (entry); 
+      g_free (parent_path);
+      parent_path = g_path_get_dirname (pathname);
+      if (strcmp (parent_path, ".") == 0)
+        *parent_path = '/';
+
+      parent_contents = g_hash_table_lookup (dir_contents, parent_path);
+      if (!parent_contents)
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
+                       "No such file or directory: %s", parent_path);
+          goto out;
+        }
+
+      mode = archive_entry_mode (entry);
+
+      ot_clear_checksum (&tmp_checksum);
+
+      if (S_ISDIR (mode))
+        {
+          GPtrArray *contents = g_hash_table_lookup (dir_contents, pathname);
+          
+          if (contents)
+            {
+              g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
+                           "Directory already exists: %s", pathname);
+              goto out;
+            }
+
+          g_hash_table_insert (dir_contents, g_path_get_basename (pathname),
+                               g_ptr_array_new_with_free_func (g_free));
+
+          if (!import_directory_meta (self, file_info, NULL, &tmp_checksum, cancellable, error))
+            goto out;
+
+          g_hash_table_insert (dir_metadata_checksums,
+                               g_strdup (pathname),
+                               g_strdup (g_checksum_get_string (tmp_checksum)));
+        }
+      else 
+        {
+          if (priv->archive)
+            {
+              if (!import_libarchive_entry_file_to_packed (self, a, entry, file_info, &tmp_checksum, cancellable, error))
+                goto out;
+            }
+          else
+            {
+              if (!import_libarchive_entry_file (self, a, entry, file_info, &tmp_checksum, cancellable, error))
+                goto out;
+            }
+
+          g_hash_table_insert (file_checksums,
+                               g_strdup (pathname),
+                               g_strdup (g_checksum_get_string (tmp_checksum)));
+          g_ptr_array_add (parent_contents, g_path_get_basename (pathname));
+        }
+    }
+  if (archive_read_finish(a) != ARCHIVE_OK)
+    {
+      propagate_libarchive_error (error, a);
+      goto out;
+    }
+
+  ret = TRUE;
+  *out_contents_checksum = ret_contents_checksum;
+  ret_contents_checksum = NULL;
+  *out_metadata_checksum = ret_metadata_checksum;
+  ret_metadata_checksum = NULL;
+ out:
+  g_hash_table_destroy (file_checksums);
+  g_hash_table_destroy (dir_metadata_checksums);
+  g_hash_table_destroy (dir_contents_checksums);
+  g_hash_table_destroy (dir_contents);
+  g_clear_object (&file_info);
+  g_free (parent_path);
+  ot_clear_checksum (&tmp_checksum);
+  return ret;
+}
+#endif
   
+gboolean      
+ostree_repo_commit_tarfile (OstreeRepo *self,
+                            const char   *branch,
+                            const char   *parent,
+                            const char   *subject,
+                            const char   *body,
+                            GVariant     *metadata,
+                            GFile        *path,
+                            GChecksum   **out_commit,
+                            GCancellable *cancellable,
+                            GError      **error)
+{
+#ifdef HAVE_LIBARCHIVE
+  OstreeRepoPrivate *priv = GET_PRIVATE (self);
+  gboolean ret = FALSE;
+  GChecksum *ret_commit_checksum = NULL;
+  GChecksum *root_metadata_checksum = NULL;
+  GChecksum *root_contents_checksum = NULL;
+  char *current_head = NULL;
+
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  g_return_val_if_fail (priv->inited, FALSE);
+  g_return_val_if_fail (branch != NULL, FALSE);
+  g_return_val_if_fail (subject != NULL, FALSE);
+  g_return_val_if_fail (metadata == NULL || g_variant_is_of_type (metadata, G_VARIANT_TYPE ("a{sv}")), FALSE);
+
+  if (parent == NULL)
+    parent = branch;
+
+  if (!ostree_repo_resolve_rev (self, parent, TRUE, &current_head, error))
+    goto out;
+
+  if (!import_libarchive (self, path, &root_contents_checksum, &root_metadata_checksum, cancellable, error))
+    goto out;
+
+  if (!import_commit (self, branch, current_head, subject, body, metadata,
+                      root_contents_checksum, root_metadata_checksum, &ret_commit_checksum, error))
+    goto out;
+  
+  ret = TRUE;
+  *out_commit = ret_commit_checksum;
+  ret_commit_checksum = NULL;
+ out:
+  ot_clear_checksum (&ret_commit_checksum);
+  g_free (current_head);
+  ot_clear_checksum (&root_metadata_checksum);
+  ot_clear_checksum (&root_contents_checksum);
+  return ret;
+#else
+  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+               "This version of ostree is not compiled with libarchive support");
+  return FALSE;
+#endif
 }
 
 static gboolean
diff --git a/src/libostree/ostree-repo.h b/src/libostree/ostree-repo.h
index 43d14d2..dc2aac3 100644
--- a/src/libostree/ostree-repo.h
+++ b/src/libostree/ostree-repo.h
@@ -111,16 +111,27 @@ gboolean      ostree_repo_load_variant_checked (OstreeRepo  *self,
                                                 GVariant     **out_variant,
                                                 GError       **error);
 
-gboolean      ostree_repo_commit (OstreeRepo   *self,
-                                  const char   *branch,
-                                  const char   *parent,
-                                  const char   *subject,
-                                  const char   *body,
-                                  GVariant     *metadata,
-                                  GFile        *base,
-                                  GChecksum   **out_commit,
-                                  GCancellable *cancellable,
-                                  GError      **error);
+gboolean      ostree_repo_commit_directory (OstreeRepo   *self,
+                                            const char   *branch,
+                                            const char   *parent,
+                                            const char   *subject,
+                                            const char   *body,
+                                            GVariant     *metadata,
+                                            GFile        *base,
+                                            GChecksum   **out_commit,
+                                            GCancellable *cancellable,
+                                            GError      **error);
+
+gboolean      ostree_repo_commit_tarfile (OstreeRepo   *self,
+                                          const char   *branch,
+                                          const char   *parent,
+                                          const char   *subject,
+                                          const char   *body,
+                                          GVariant     *metadata,
+                                          GFile        *base,
+                                          GChecksum   **out_commit,
+                                          GCancellable *cancellable,
+                                          GError      **error);
 
 gboolean      ostree_repo_checkout (OstreeRepo *self,
                                     const char   *ref,
diff --git a/src/ostree/ot-builtin-commit.c b/src/ostree/ot-builtin-commit.c
index 3c064a0..96287b2 100644
--- a/src/ostree/ot-builtin-commit.c
+++ b/src/ostree/ot-builtin-commit.c
@@ -35,6 +35,7 @@ static char *subject;
 static char *body;
 static char *parent;
 static char *branch;
+static gboolean tar;
 
 static GOptionEntry options[] = {
   { "subject", 's', 0, G_OPTION_ARG_STRING, &subject, "One line subject", "subject" },
@@ -43,6 +44,7 @@ static GOptionEntry options[] = {
   { "metadata-variant", 0, 0, G_OPTION_ARG_FILENAME, &metadata_bin_path, "File containing serialized variant, in host endianness", "path" },
   { "branch", 'b', 0, G_OPTION_ARG_STRING, &branch, "Branch", "branch" },
   { "parent", 'p', 0, G_OPTION_ARG_STRING, &parent, "Parent commit", "commit" },
+  { "tar", 0, 0, G_OPTION_ARG_NONE, &tar, "Given argument is a tar file", NULL },
   { NULL }
 };
 
@@ -52,35 +54,35 @@ ostree_builtin_commit (int argc, char **argv, const char *repo_path, GError **er
   GOptionContext *context;
   gboolean ret = FALSE;
   OstreeRepo *repo = NULL;
-  char *dirpath = NULL;
-  GFile *dir = NULL;
+  char *argpath = NULL;
+  GFile *arg = NULL;
   GChecksum *commit_checksum = NULL;
   GVariant *metadata = NULL;
   GMappedFile *metadata_mappedf = NULL;
   GFile *metadata_f = NULL;
 
-  context = g_option_context_new ("[DIR] - Commit a new revision");
+  context = g_option_context_new ("[ARG] - Commit a new revision");
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
 
   if (argc > 1)
-    dirpath = g_strdup (argv[1]);
+    argpath = g_strdup (argv[1]);
   else
-    dirpath = g_get_current_dir ();
+    argpath = g_get_current_dir ();
 
-  if (g_str_has_suffix (dirpath, "/"))
-    dirpath[strlen (dirpath) - 1] = '\0';
+  if (g_str_has_suffix (argpath, "/"))
+    argpath[strlen (argpath) - 1] = '\0';
 
-  if (!*dirpath)
+  if (!*argpath)
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "Invalid empty directory");
+                   "Invalid empty argument");
       goto out;
     }
 
-  dir = ot_gfile_new_for_path (dirpath);
+  arg = ot_gfile_new_for_path (argpath);
 
   if (metadata_text_path || metadata_bin_path)
     {
@@ -124,15 +126,24 @@ ostree_builtin_commit (int argc, char **argv, const char *repo_path, GError **er
       goto out;
     }
 
-  if (!ostree_repo_commit (repo, branch, parent, subject, body, metadata,
-                           dir, &commit_checksum, NULL, error))
-    goto out;
+  if (!tar)
+    {
+      if (!ostree_repo_commit_directory (repo, branch, parent, subject, body, metadata,
+                                         arg, &commit_checksum, NULL, error))
+        goto out;
+    }
+  else
+    {
+      if (!ostree_repo_commit_tarfile (repo, branch, parent, subject, body, metadata,
+                                       arg, &commit_checksum, NULL, error))
+        goto out;
+    }
 
   ret = TRUE;
   g_print ("%s\n", g_checksum_get_string (commit_checksum));
  out:
-  g_free (dirpath);
-  g_clear_object (&dir);
+  g_free (argpath);
+  g_clear_object (&arg);
   if (metadata_mappedf)
     g_mapped_file_unref (metadata_mappedf);
   if (context)



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