Imported Upstream version 3.2.0
[debian/amanda] / xfer-src / source-pattern.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008,2009 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 static gpointer
70 pull_buffer_impl(
71     XferElement *elt,
72     size_t *size)
73 {
74     XferSourcePattern *self = (XferSourcePattern *)elt;
75     char *rval;
76     char *s, *d;
77     size_t l;
78     size_t offset;
79
80     /* indicate EOF on an cancel */
81     if (elt->cancelled || (self->limited_length && self->length == 0)) {
82         *size = 0;
83         return NULL;
84     }
85
86     if (self->limited_length) {
87         if (self->length == 0) {
88             *size = 0;
89             return NULL;
90         }
91
92         *size = MIN(10240, self->length);
93         self->length -= *size;
94     } else {
95         *size = 10240;
96     }
97
98     rval = malloc(*size);
99
100     /* fill the buffer "manually", instead of using fancy memcpy techniques, so
101      * that this runs at about the same speed as the random source */
102     l = *size;
103     s = self->pattern;
104     offset = self->current_offset;
105     d = rval;
106     while (l--) {
107         *(d++) = *(s + offset++);
108         if (offset >= self->pattern_buffer_length) offset = 0;
109     }
110     self->current_offset = offset;
111
112     return rval;
113 }
114
115 static void
116 instance_init(
117     XferElement *elt)
118 {
119     elt->can_generate_eof = TRUE;
120 }
121
122 static void
123 class_init(
124     XferSourcePatternClass * selfc)
125 {
126     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
127     static xfer_element_mech_pair_t mech_pairs[] = {
128         { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 1, 0},
129         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
130     };
131
132     klass->pull_buffer = pull_buffer_impl;
133
134     klass->perl_class = "Amanda::Xfer::Source::Pattern";
135     klass->mech_pairs = mech_pairs;
136
137     parent_class = g_type_class_peek_parent(selfc);
138 }
139
140 GType
141 xfer_source_pattern_get_type (void)
142 {
143     static GType type = 0;
144
145     if G_UNLIKELY(type == 0) {
146         static const GTypeInfo info = {
147             sizeof (XferSourcePatternClass),
148             (GBaseInitFunc) NULL,
149             (GBaseFinalizeFunc) NULL,
150             (GClassInitFunc) class_init,
151             (GClassFinalizeFunc) NULL,
152             NULL /* class_data */,
153             sizeof (XferSourcePattern),
154             0 /* n_preallocs */,
155             (GInstanceInitFunc) instance_init,
156             NULL
157         };
158
159         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourcePattern", &info, 0);
160     }
161
162     return type;
163 }
164
165 /* create an element of this class; prototype is in xfer-element.h */
166 XferElement * xfer_source_pattern(guint64 length, void * pattern,
167                                   size_t pattern_length) {
168     XferSourcePattern *xsp =
169         (XferSourcePattern *)g_object_new(XFER_SOURCE_PATTERN_TYPE, NULL);
170     XferElement *elt = XFER_ELEMENT(xsp);
171
172     xsp->length = length;
173     xsp->limited_length = (length > 0);
174     xsp->pattern = g_memdup(pattern, pattern_length);
175     xsp->pattern_buffer_length = pattern_length;
176     xsp->current_offset = 0;
177
178     return elt;
179 }