Debian Codegraph @

device-src/xfer-dest-device.c

raw
1 /*
2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
4 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Contact information: Carbonite Inc., 756 N Pastoria Ave
21 * Sunnyvale, CA 94085, or: http://www.zmanda.com
22 */
23
24 #include "amanda.h"
25 #include "amxfer.h"
26 #include "device.h"
27 #include "property.h"
28 #include "xfer-device.h"
29
30 /*
31 * Class declaration
32 *
33 * This declaration is entirely private; nothing but xfer_dest_device() references
34 * it directly.
35 */
36
37 GType xfer_dest_device_get_type(void);
38 #define XFER_DEST_DEVICE_TYPE (xfer_dest_device_get_type())
39 #define XFER_DEST_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_device_get_type(), XferDestDevice)
40 #define XFER_DEST_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_device_get_type(), XferDestDevice const)
41 #define XFER_DEST_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_device_get_type(), XferDestDeviceClass)
42 #define IS_XFER_DEST_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_device_get_type ())
43 #define XFER_DEST_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_device_get_type(), XferDestDeviceClass)
44
45 static GObjectClass *parent_class = NULL;
46
47 /*
48 * Main object structure
49 */
50
51 typedef struct XferDestDevice {
52 XferElement __parent__;
53
54 Device *device;
55
56 gboolean cancel_at_leom;
57
58 gpointer partial;
59 gsize block_size;
60 gsize partial_length;
61 } XferDestDevice;
62
63 /*
64 * Class definition
65 */
66
67 typedef struct {
68 XferElementClass __parent__;
69 } XferDestDeviceClass;
70
71 /*
72 * Implementation
73 */
74
75 static gboolean
76 do_block(
77 XferDestDevice *self,
78 guint size,
79 gpointer data)
80 {
81 XferElement *elt = XFER_ELEMENT(self);
82
83 if (device_write_block(self->device, size, data) != WRITE_SUCCEED) {
84 xfer_cancel_with_error(elt, "%s: %s",
85 self->device->device_name, device_error_or_status(self->device));
86 wait_until_xfer_cancelled(elt->xfer);
87 return FALSE;
88 }
89
90 /* check for LEOM */
91 if (self->cancel_at_leom && self->device->is_eom) {
92 xfer_cancel_with_error(elt, "%s: LEOM detected", self->device->device_name);
93 wait_until_xfer_cancelled(elt->xfer);
94 return FALSE;
95 }
96
97 return TRUE;
98 }
99
100 static void
101 push_buffer_impl(
102 XferElement *elt,
103 gpointer buf,
104 size_t len)
105 {
106 XferDestDevice *self = XFER_DEST_DEVICE(elt);
107 gpointer to_free = buf;
108
109 /* Handle EOF */
110 if (!buf) {
111 /* write out the partial buffer, if there's anything in it */
112 if (self->partial_length) {
113 if (!do_block(self, self->block_size, self->partial)) {
114 return;
115 }
116 self->partial_length = 0;
117 }
118
119 device_finish_file(self->device);
120 return;
121 }
122
123 /* set up the block buffer, now that we can depend on having a blocksize
124 * from the device */
125 if (!self->partial) {
126 self->partial = g_try_malloc(self->device->block_size);
127 if (self->partial == NULL) {
128 xfer_cancel_with_error(elt, "%s: Cannot allocate memory",
129 self->device->device_name);
130 wait_until_xfer_cancelled(elt->xfer);
131 return;
132 }
133 self->block_size = self->device->block_size;
134 self->partial_length = 0;
135 }
136
137 /* if we already have data in the buffer, add the new data to it */
138 if (self->partial_length != 0) {
139 gsize to_copy = min(self->block_size - self->partial_length, len);
140 memmove((char *)self->partial + self->partial_length, buf, to_copy);
141 buf = (gpointer)(to_copy + (char *)buf);
142 len -= to_copy;
143 self->partial_length += to_copy;
144 }
145
146 /* and if the buffer is now full, write the block */
147 if (self->partial_length == self->block_size) {
148 if (!do_block(self, self->block_size, self->partial)) {
149 g_free(to_free);
150 return;
151 }
152 self->partial_length = 0;
153 }
154
155 /* write any whole blocks directly from the push buffer */
156 while (len >= self->block_size) {
157 if (!do_block(self, self->block_size, buf)) {
158 g_free(to_free);
159 return;
160 }
161
162 buf = (gpointer)(self->block_size + (char *)buf);
163 len -= self->block_size;
164 }
165
166 /* and finally store any leftover data in the partial buffer */
167 if (len) {
168 memmove(self->partial, buf, len);
169 self->partial_length = len;
170 }
171
172 g_free(to_free);
173 }
174
175 static void
176 instance_init(
177 XferElement *elt)
178 {
179 XferDestDevice *self = XFER_DEST_DEVICE(elt);
180 self->partial = NULL;
181 }
182
183 static void
184 finalize_impl(
185 GObject * obj_self)
186 {
187 XferDestDevice *self = XFER_DEST_DEVICE(obj_self);
188
189 if (self->partial) {
190 g_free(self->partial);
191 }
192 }
193
194 static void
195 class_init(
196 XferDestDeviceClass * selfc)
197 {
198 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
199 GObjectClass *goc = (GObjectClass*) klass;
200 static xfer_element_mech_pair_t mech_pairs[] = {
201 { XFER_MECH_PUSH_BUFFER, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0), XFER_NALLOC(0) },
202 { XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0), XFER_NALLOC(0) }
203 };
204
205 klass->push_buffer = push_buffer_impl;
206
207 klass->perl_class = "Amanda::Xfer::Dest::Device";
208 klass->mech_pairs = mech_pairs;
209
210 goc->finalize = finalize_impl;
211
212 parent_class = g_type_class_peek_parent(selfc);
213 }
214
215 GType
216 xfer_dest_device_get_type (void)
217 {
218 static GType type = 0;
219
220 if (G_UNLIKELY(type == 0)) {
221 static const GTypeInfo info = {
222 sizeof (XferDestDeviceClass),
223 (GBaseInitFunc) NULL,
224 (GBaseFinalizeFunc) NULL,
225 (GClassInitFunc) class_init,
226 (GClassFinalizeFunc) NULL,
227 NULL /* class_data */,
228 sizeof (XferDestDevice),
229 0 /* n_preallocs */,
230 (GInstanceInitFunc) instance_init,
231 NULL
232 };
233
234 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferDestDevice", &info, 0);
235 }
236
237 return type;
238 }
239
240 /* create an element of this class; prototype is in xfer-device.h */
241 XferElement *
242 xfer_dest_device(
243 Device *device,
244 gboolean cancel_at_leom)
245 {
246 XferDestDevice *self = (XferDestDevice *)g_object_new(XFER_DEST_DEVICE_TYPE, NULL);
247 XferElement *elt = XFER_ELEMENT(self);
248
249 g_assert(device != NULL);
250
251 self->device = device;
252 self->cancel_at_leom = cancel_at_leom;
253
254 return elt;
255 }