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