2f0ff6d08ed0d416205c566d1889dae6862d811e
[debian/amanda] / xfer-src / dest-null.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008 Zmanda Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "amxfer.h"
21 #include "amanda.h"
22 #include "simpleprng.h"
23
24 /*
25  * Class declaration
26  *
27  * This declaration is entirely private; nothing but xfer_dest_null() references
28  * it directly.
29  */
30
31 GType xfer_dest_null_get_type(void);
32 #define XFER_DEST_NULL_TYPE (xfer_dest_null_get_type())
33 #define XFER_DEST_NULL(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_null_get_type(), XferDestNull)
34 #define XFER_DEST_NULL_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_null_get_type(), XferDestNull const)
35 #define XFER_DEST_NULL_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_null_get_type(), XferDestNullClass)
36 #define IS_XFER_DEST_NULL(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_null_get_type ())
37 #define XFER_DEST_NULL_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_null_get_type(), XferDestNullClass)
38
39 static GObjectClass *parent_class = NULL;
40
41 /*
42  * Main object structure
43  */
44
45 typedef struct XferDestNull {
46     XferElement __parent__;
47
48     gboolean sent_info;
49
50     gboolean do_verify;
51     simpleprng_state_t prng;
52 } XferDestNull;
53
54 /*
55  * Class definition
56  */
57
58 typedef struct {
59     XferElementClass __parent__;
60 } XferDestNullClass;
61
62 /*
63  * Implementation
64  */
65
66 static void
67 push_buffer_impl(
68     XferElement *elt,
69     gpointer buf,
70     size_t len)
71 {
72     XferDestNull *self = (XferDestNull *)elt;
73
74     if (!buf)
75         return;
76
77     if (self->do_verify && !elt->cancelled) {
78         if (!simpleprng_verify_buffer(&self->prng, buf, len)) {
79             xfer_element_handle_error(elt,
80                 _("verification of incoming bytestream failed"));
81             amfree(buf);
82             return;
83         }
84     }
85
86     if (!self->sent_info) {
87         /* send a superfluous message (this is a testing XferElement,
88          * after all) */
89         XMsg *msg = xmsg_new((XferElement *)self, XMSG_INFO, 0);
90         msg->message = stralloc("Is this thing on?");
91         xfer_queue_message(XFER_ELEMENT(self)->xfer, msg);
92         self->sent_info = TRUE;
93     }
94
95     amfree(buf);
96 }
97
98 static void
99 class_init(
100     XferDestNullClass * selfc)
101 {
102     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
103     static xfer_element_mech_pair_t mech_pairs[] = {
104         { XFER_MECH_PUSH_BUFFER, XFER_MECH_NONE, 0, 0},
105         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
106     };
107
108     klass->push_buffer = push_buffer_impl;
109
110     klass->perl_class = "Amanda::Xfer::Dest::Null";
111     klass->mech_pairs = mech_pairs;
112
113     parent_class = g_type_class_peek_parent(selfc);
114 }
115
116 GType
117 xfer_dest_null_get_type (void)
118 {
119     static GType type = 0;
120
121     if G_UNLIKELY(type == 0) {
122         static const GTypeInfo info = {
123             sizeof (XferDestNullClass),
124             (GBaseInitFunc) NULL,
125             (GBaseFinalizeFunc) NULL,
126             (GClassInitFunc) class_init,
127             (GClassFinalizeFunc) NULL,
128             NULL /* class_data */,
129             sizeof (XferDestNull),
130             0 /* n_preallocs */,
131             (GInstanceInitFunc) NULL,
132             NULL
133         };
134
135         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferDestNull", &info, 0);
136     }
137
138     return type;
139 }
140
141 /* create an element of this class; prototype is in xfer-element.h */
142 XferElement *
143 xfer_dest_null(
144     guint32 prng_seed)
145 {
146     XferDestNull *self = (XferDestNull *)g_object_new(XFER_DEST_NULL_TYPE, NULL);
147     XferElement *elt = XFER_ELEMENT(self);
148
149     if (prng_seed) {
150         self->do_verify = TRUE;
151         simpleprng_seed(&self->prng, prng_seed);
152     } else {
153         self->do_verify = FALSE;
154     }
155
156     return elt;
157 }