summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-checksum-input-stream.c
blob: f06a95d89e0a76d4c8e770f82acb008c696ad141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2011 Colin Walters <walters@verbum.org>
 * Copyright (C) 2022 Igalia S.L.
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "ostree-checksum-input-stream.h"

enum
{
  PROP_0,
  PROP_CHECKSUM
};

struct _OstreeChecksumInputStreamPrivate
{
  GChecksum *checksum;
};

G_DEFINE_TYPE_WITH_PRIVATE (OstreeChecksumInputStream, ostree_checksum_input_stream,
                            G_TYPE_FILTER_INPUT_STREAM)

static void ostree_checksum_input_stream_set_property (GObject *object, guint prop_id,
                                                       const GValue *value, GParamSpec *pspec);
static void ostree_checksum_input_stream_get_property (GObject *object, guint prop_id,
                                                       GValue *value, GParamSpec *pspec);
static gssize ostree_checksum_input_stream_read (GInputStream *stream, void *buffer, gsize count,
                                                 GCancellable *cancellable, GError **error);

static void
ostree_checksum_input_stream_class_init (OstreeChecksumInputStreamClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);

  gobject_class->get_property = ostree_checksum_input_stream_get_property;
  gobject_class->set_property = ostree_checksum_input_stream_set_property;

  stream_class->read_fn = ostree_checksum_input_stream_read;

  /*
   * OstreeChecksumInputStream:checksum:
   *
   * The checksum that the stream updates.
   */
  g_object_class_install_property (
      gobject_class, PROP_CHECKSUM,
      g_param_spec_pointer ("checksum", "", "",
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}

static void
ostree_checksum_input_stream_set_property (GObject *object, guint prop_id, const GValue *value,
                                           GParamSpec *pspec)
{
  OstreeChecksumInputStream *self;

  self = OSTREE_CHECKSUM_INPUT_STREAM (object);

  switch (prop_id)
    {
    case PROP_CHECKSUM:
      self->priv->checksum = g_value_get_pointer (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
ostree_checksum_input_stream_get_property (GObject *object, guint prop_id, GValue *value,
                                           GParamSpec *pspec)
{
  OstreeChecksumInputStream *self;

  self = OSTREE_CHECKSUM_INPUT_STREAM (object);

  switch (prop_id)
    {
    case PROP_CHECKSUM:
      g_value_set_pointer (value, self->priv->checksum);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
ostree_checksum_input_stream_init (OstreeChecksumInputStream *self)
{
  self->priv = ostree_checksum_input_stream_get_instance_private (self);
}

OstreeChecksumInputStream *
ostree_checksum_input_stream_new (GInputStream *base, GChecksum *checksum)
{
  g_assert (G_IS_INPUT_STREAM (base));

  OstreeChecksumInputStream *stream = g_object_new (
      OSTREE_TYPE_CHECKSUM_INPUT_STREAM, "base-stream", base, "checksum", checksum, NULL);

  return stream;
}

static gssize
ostree_checksum_input_stream_read (GInputStream *stream, void *buffer, gsize count,
                                   GCancellable *cancellable, GError **error)
{
  OstreeChecksumInputStream *self = (OstreeChecksumInputStream *)stream;
  GFilterInputStream *fself = (GFilterInputStream *)self;
  gssize res = -1;

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return -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;
}