summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2017-03-13 15:40:24 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2017-03-20 18:32:40 +0000
commitc2f5a999bfd865696b5456858e3cae984fba46f5 (patch)
tree4f3ea276df014f62aa8dc00c1d459af5774fe924
parent0b214566a244df2da1fcd2db09b5560a9b3d7280 (diff)
downloadostree-c2f5a999bfd865696b5456858e3cae984fba46f5.tar.gz
lib: Add a private copy of checksum-instream
The current `OstreeChecksumInputStream` is public due to a historical mistake. I'd like to add an OpenSSL checksum backend, but that's harder without breaking this API. Let's ignore it and create a new private version, so it's easier to do the GLib/OpenSSL abstraction in one place. Closes: #738 Approved by: jlebon
-rw-r--r--Makefile-otutil.am2
-rw-r--r--src/libostree/ostree-repo-commit.c23
-rw-r--r--src/libotutil/ot-checksum-instream.c129
-rw-r--r--src/libotutil/ot-checksum-instream.h64
-rw-r--r--src/libotutil/otutil.h1
5 files changed, 206 insertions, 13 deletions
diff --git a/Makefile-otutil.am b/Makefile-otutil.am
index ee892a70..5b6d4e48 100644
--- a/Makefile-otutil.am
+++ b/Makefile-otutil.am
@@ -22,6 +22,8 @@ noinst_LTLIBRARIES += libotutil.la
libotutil_la_SOURCES = \
src/libotutil/ot-checksum-utils.c \
src/libotutil/ot-checksum-utils.h \
+ src/libotutil/ot-checksum-instream.c \
+ src/libotutil/ot-checksum-instream.h \
src/libotutil/ot-fs-utils.c \
src/libotutil/ot-fs-utils.h \
src/libotutil/ot-keyfile-utils.c \
diff --git a/src/libostree/ostree-repo-commit.c b/src/libostree/ostree-repo-commit.c
index f6129ca6..d02154f8 100644
--- a/src/libostree/ostree-repo-commit.c
+++ b/src/libostree/ostree-repo-commit.c
@@ -619,12 +619,11 @@ write_object (OstreeRepo *self,
OstreeRepoMode repo_mode;
g_autofree char *temp_filename = NULL;
g_autofree guchar *ret_csum = NULL;
- glnx_unref_object OstreeChecksumInputStream *checksum_input = NULL;
+ glnx_unref_object OtChecksumInstream *checksum_input = NULL;
g_autoptr(GInputStream) file_input = NULL;
g_autoptr(GFileInfo) file_info = NULL;
g_autoptr(GVariant) xattrs = NULL;
gboolean have_obj;
- GChecksum *checksum = NULL;
gboolean temp_file_is_regular;
gboolean temp_file_is_symlink;
glnx_fd_close int temp_fd = -1;
@@ -654,11 +653,7 @@ write_object (OstreeRepo *self,
repo_mode = ostree_repo_get_mode (self);
if (out_csum)
- {
- checksum = g_checksum_new (G_CHECKSUM_SHA256);
- if (input)
- checksum_input = ostree_checksum_input_stream_new (input, checksum);
- }
+ checksum_input = ot_checksum_instream_new (input, G_CHECKSUM_SHA256);
if (objtype == OSTREE_OBJECT_TYPE_FILE)
{
@@ -778,11 +773,11 @@ write_object (OstreeRepo *self,
temp_file_is_regular = TRUE;
}
- if (!checksum)
+ if (!checksum_input)
actual_checksum = expected_checksum;
else
{
- actual_checksum = g_checksum_get_string (checksum);
+ actual_checksum = ot_checksum_instream_get_string (checksum_input);
if (expected_checksum && strcmp (actual_checksum, expected_checksum) != 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
@@ -892,16 +887,18 @@ write_object (OstreeRepo *self,
else
self->txn_stats.content_objects_total++;
g_mutex_unlock (&self->txn_stats_lock);
-
- if (checksum)
- ret_csum = ot_csum_from_gchecksum (checksum);
+
+ if (checksum_input)
+ {
+ g_assert (actual_checksum);
+ ret_csum = ostree_checksum_to_bytes (actual_checksum);
+ }
ret = TRUE;
ot_transfer_out_value(out_csum, &ret_csum);
out:
if (temp_filename)
(void) unlinkat (self->tmp_dir_fd, temp_filename, 0);
- g_clear_pointer (&checksum, (GDestroyNotify) g_checksum_free);
return ret;
}
diff --git a/src/libotutil/ot-checksum-instream.c b/src/libotutil/ot-checksum-instream.c
new file mode 100644
index 00000000..14d5b5f0
--- /dev/null
+++ b/src/libotutil/ot-checksum-instream.c
@@ -0,0 +1,129 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2017 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 "ot-checksum-instream.h"
+
+G_DEFINE_TYPE (OtChecksumInstream, ot_checksum_instream, G_TYPE_FILTER_INPUT_STREAM)
+
+struct _OtChecksumInstreamPrivate {
+ GChecksum *checksum;
+};
+
+static gssize ot_checksum_instream_read (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ GCancellable *cancellable,
+ GError **error);
+
+static void
+ot_checksum_instream_finalize (GObject *object)
+{
+ OtChecksumInstream *self = (OtChecksumInstream*)object;
+
+ g_checksum_free (self->priv->checksum);
+
+ G_OBJECT_CLASS (ot_checksum_instream_parent_class)->finalize (object);
+}
+
+static void
+ot_checksum_instream_class_init (OtChecksumInstreamClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (OtChecksumInstreamPrivate));
+
+ object_class->finalize = ot_checksum_instream_finalize;
+ stream_class->read_fn = ot_checksum_instream_read;
+}
+
+static void
+ot_checksum_instream_init (OtChecksumInstream *self)
+{
+ self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, OT_TYPE_CHECKSUM_INSTREAM, OtChecksumInstreamPrivate);
+
+}
+
+OtChecksumInstream *
+ot_checksum_instream_new (GInputStream *base,
+ GChecksumType checksum_type)
+{
+ OtChecksumInstream *stream;
+
+ g_return_val_if_fail (G_IS_INPUT_STREAM (base), NULL);
+
+ stream = g_object_new (OT_TYPE_CHECKSUM_INSTREAM,
+ "base-stream", base,
+ NULL);
+ stream->priv->checksum = g_checksum_new (checksum_type);
+
+ return (OtChecksumInstream*) (stream);
+}
+
+static gssize
+ot_checksum_instream_read (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ GCancellable *cancellable,
+ GError **error)
+{
+ OtChecksumInstream *self = (OtChecksumInstream*) stream;
+ GFilterInputStream *fself = (GFilterInputStream*) self;
+ gssize res = -1;
+
+ res = g_input_stream_read (fself->base_stream,
+ buffer,
+ count,
+ cancellable,
+ error);
+ if (res > 0)
+ g_checksum_update (self->priv->checksum, buffer, res);
+
+ return res;
+}
+
+void
+ot_checksum_instream_get_digest (OtChecksumInstream *stream,
+ guint8 *buffer,
+ gsize *digest_len)
+{
+ g_checksum_get_digest (stream->priv->checksum, buffer, digest_len);
+}
+
+guint8*
+ot_checksum_instream_dup_digest (OtChecksumInstream *stream,
+ gsize *ret_len)
+{
+ gsize len = 32;
+ guchar *ret = g_malloc (len);
+ g_checksum_get_digest (stream->priv->checksum, ret, &len);
+ g_assert (len == 32);
+ if (ret_len)
+ *ret_len = len;
+ return ret;
+}
+
+char *
+ot_checksum_instream_get_string (OtChecksumInstream *stream)
+{
+ return g_strdup (g_checksum_get_string (stream->priv->checksum));
+}
diff --git a/src/libotutil/ot-checksum-instream.h b/src/libotutil/ot-checksum-instream.h
new file mode 100644
index 00000000..78a5d44e
--- /dev/null
+++ b/src/libotutil/ot-checksum-instream.h
@@ -0,0 +1,64 @@
+/* -*- 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.
+ *
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define OT_TYPE_CHECKSUM_INSTREAM (ot_checksum_instream_get_type ())
+#define OT_CHECKSUM_INSTREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), OT_TYPE_CHECKSUM_INPUT_STREAM, OtChecksumInstream))
+#define OT_CHECKSUM_INSTREAM_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), OT_TYPE_CHECKSUM_INPUT_STREAM, OtChecksumInstreamClass))
+#define OT_IS_CHECKSUM_INSTREAM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), OT_TYPE_CHECKSUM_INPUT_STREAM))
+#define OT_IS_CHECKSUM_INSTREAM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), OT_TYPE_CHECKSUM_INPUT_STREAM))
+#define OT_CHECKSUM_INSTREAM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), OT_TYPE_CHECKSUM_INPUT_STREAM, OtChecksumInstreamClass))
+
+typedef struct _OtChecksumInstream OtChecksumInstream;
+typedef struct _OtChecksumInstreamClass OtChecksumInstreamClass;
+typedef struct _OtChecksumInstreamPrivate OtChecksumInstreamPrivate;
+
+struct _OtChecksumInstream
+{
+ GFilterInputStream parent_instance;
+
+ /*< private >*/
+ OtChecksumInstreamPrivate *priv;
+};
+
+struct _OtChecksumInstreamClass
+{
+ GFilterInputStreamClass parent_class;
+};
+
+GType ot_checksum_instream_get_type (void) G_GNUC_CONST;
+
+OtChecksumInstream * ot_checksum_instream_new (GInputStream *stream, GChecksumType checksum);
+
+void ot_checksum_instream_get_digest (OtChecksumInstream *stream,
+ guint8 *buffer,
+ gsize *digest_len);
+
+guint8* ot_checksum_instream_dup_digest (OtChecksumInstream *stream,
+ gsize *ret_len);
+char * ot_checksum_instream_get_string (OtChecksumInstream *stream);
+
+G_END_DECLS
diff --git a/src/libotutil/otutil.h b/src/libotutil/otutil.h
index c66d5634..f6158de8 100644
--- a/src/libotutil/otutil.h
+++ b/src/libotutil/otutil.h
@@ -53,5 +53,6 @@
#include <ot-checksum-utils.h>
#include <ot-gpg-utils.h>
#include <ot-log-utils.h>
+#include <ot-checksum-instream.h>
void ot_ptrarray_add_many (GPtrArray *a, ...) G_GNUC_NULL_TERMINATED;