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