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