5a4aad836625c593341876f951845439898b757f
[debian/amanda] / xfer-src / source-pattern.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008, 2009, 2011 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_source_pattern() references
30  * it directly.
31  */
32
33 GType xfer_source_pattern_get_type(void);
34 #define XFER_SOURCE_PATTERN_TYPE (xfer_source_pattern_get_type())
35 #define XFER_SOURCE_PATTERN(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_pattern_get_type(), XferSourcePattern)
36 #define XFER_SOURCE_PATTERN_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_pattern_get_type(), XferSourcePattern const)
37 #define XFER_SOURCE_PATTERN_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_pattern_get_type(), XferSourcePatternClass)
38 #define IS_XFER_SOURCE_PATTERN(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_pattern_get_type ())
39 #define XFER_SOURCE_PATTERN_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_pattern_get_type(), XferSourcePatternClass)
40
41 static GObjectClass *parent_class = NULL;
42
43 /*
44  * Main object structure
45  */
46
47 typedef struct XferSourcePattern {
48     XferElement __parent__;
49
50     gboolean limited_length;
51     guint64 length;
52     size_t pattern_buffer_length;
53     size_t current_offset;
54     char * pattern;
55 } XferSourcePattern;
56
57 /*
58  * Class definition
59  */
60
61 typedef struct {
62     XferElementClass __parent__;
63 } XferSourcePatternClass;
64
65 /*
66  * Implementation
67  */
68
69 /*
70  * Utility function to fill a buffer buf of size len with the pattern defined
71  * for this instance. We start each copying from the offset where we left the
72  * previous one.
73  *
74  * Note that performance is important: amtapetype uses this very source and
75  * source-random.c to determine whether a tape device uses compression, and
76  * expects the two to run about the same speed.  This is why this is a byte
77  * loop and does not use mempcy() and friends: the random source is also byte
78  * per byte.
79  */
80
81 static void fill_buffer_with_pattern(XferSourcePattern *self, char *buf,
82     size_t len)
83 {
84     char *src = self->pattern, *dst = buf;
85     size_t plen = self->pattern_buffer_length, offset = self->current_offset;
86     size_t pos = 0;
87
88     src += offset;
89
90     while (pos < len) {
91         *dst++ = *src++;
92         pos++, offset++;
93
94         if (G_LIKELY(offset < plen))
95             continue;
96
97         offset = 0;
98         src = self->pattern;
99     }
100
101     self->current_offset = offset;
102 }
103
104 static gpointer
105 pull_buffer_impl(
106     XferElement *elt,
107     size_t *size)
108 {
109     XferSourcePattern *self = (XferSourcePattern *)elt;
110     char *rval;
111
112     /* indicate EOF on an cancel */
113     if (elt->cancelled) {
114         *size = 0;
115         return NULL;
116     }
117
118     if (self->limited_length) {
119         if (self->length == 0) {
120             *size = 0;
121             return NULL;
122         }
123
124         *size = MIN(10240, self->length);
125         self->length -= *size;
126     } else {
127         *size = 10240;
128     }
129
130     rval = malloc(*size);
131
132     fill_buffer_with_pattern(self, rval, *size);
133
134     return rval;
135 }
136
137 static void
138 instance_init(
139     XferElement *elt)
140 {
141     elt->can_generate_eof = TRUE;
142 }
143
144 static void
145 class_init(
146     XferSourcePatternClass * selfc)
147 {
148     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
149     static xfer_element_mech_pair_t mech_pairs[] = {
150         { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, XFER_NROPS(1), XFER_NTHREADS(0) },
151         { XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0) },
152     };
153
154     klass->pull_buffer = pull_buffer_impl;
155
156     klass->perl_class = "Amanda::Xfer::Source::Pattern";
157     klass->mech_pairs = mech_pairs;
158
159     parent_class = g_type_class_peek_parent(selfc);
160 }
161
162 GType
163 xfer_source_pattern_get_type (void)
164 {
165     static GType type = 0;
166
167     if G_UNLIKELY(type == 0) {
168         static const GTypeInfo info = {
169             sizeof (XferSourcePatternClass),
170             (GBaseInitFunc) NULL,
171             (GBaseFinalizeFunc) NULL,
172             (GClassInitFunc) class_init,
173             (GClassFinalizeFunc) NULL,
174             NULL /* class_data */,
175             sizeof (XferSourcePattern),
176             0 /* n_preallocs */,
177             (GInstanceInitFunc) instance_init,
178             NULL
179         };
180
181         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourcePattern", &info, 0);
182     }
183
184     return type;
185 }
186
187 /* create an element of this class; prototype is in xfer-element.h */
188 XferElement * xfer_source_pattern(guint64 length, void * pattern,
189                                   size_t pattern_length) {
190     XferSourcePattern *xsp =
191         (XferSourcePattern *)g_object_new(XFER_SOURCE_PATTERN_TYPE, NULL);
192     XferElement *elt = XFER_ELEMENT(xsp);
193
194     xsp->length = length;
195     xsp->limited_length = (length > 0);
196     xsp->pattern = g_memdup(pattern, pattern_length);
197     xsp->pattern_buffer_length = pattern_length;
198     xsp->current_offset = 0;
199
200     return elt;
201 }