2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
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.
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
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
19 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
20 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
27 #include "xfer-device.h"
32 * This declaration is entirely private; nothing but xfer_dest_device() references
36 GType xfer_dest_device_get_type(void);
37 #define XFER_DEST_DEVICE_TYPE (xfer_dest_device_get_type())
38 #define XFER_DEST_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_device_get_type(), XferDestDevice)
39 #define XFER_DEST_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_device_get_type(), XferDestDevice const)
40 #define XFER_DEST_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_device_get_type(), XferDestDeviceClass)
41 #define IS_XFER_DEST_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_device_get_type ())
42 #define XFER_DEST_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_device_get_type(), XferDestDeviceClass)
44 static GObjectClass *parent_class = NULL;
47 * Main object structure
50 typedef struct XferDestDevice {
51 XferElement __parent__;
55 gboolean cancel_at_leom;
67 XferElementClass __parent__;
68 } XferDestDeviceClass;
80 XferElement *elt = XFER_ELEMENT(self);
82 if (!device_write_block(self->device, size, data)) {
83 xfer_cancel_with_error(elt, "%s: %s",
84 self->device->device_name, device_error_or_status(self->device));
85 wait_until_xfer_cancelled(elt->xfer);
90 if (self->cancel_at_leom && self->device->is_eom) {
91 xfer_cancel_with_error(elt, "%s: LEOM detected", self->device->device_name);
92 wait_until_xfer_cancelled(elt->xfer);
105 XferDestDevice *self = XFER_DEST_DEVICE(elt);
106 gpointer to_free = buf;
110 /* write out the partial buffer, if there's anything in it */
111 if (self->partial_length) {
112 if (!do_block(self, self->block_size, self->partial)) {
115 self->partial_length = 0;
118 device_finish_file(self->device);
122 /* set up the block buffer, now that we can depend on having a blocksize
124 if (!self->partial) {
125 self->partial = g_malloc(self->device->block_size);
126 self->block_size = self->device->block_size;
127 self->partial_length = 0;
130 /* if we already have data in the buffer, add the new data to it */
131 if (self->partial_length != 0) {
132 gsize to_copy = min(self->block_size - self->partial_length, len);
133 memmove(self->partial + self->partial_length, buf, to_copy);
134 buf = (gpointer)(to_copy + (char *)buf);
136 self->partial_length += to_copy;
139 /* and if the buffer is now full, write the block */
140 if (self->partial_length == self->block_size) {
141 if (!do_block(self, self->block_size, self->partial)) {
145 self->partial_length = 0;
148 /* write any whole blocks directly from the push buffer */
149 while (len >= self->block_size) {
150 if (!do_block(self, self->block_size, buf)) {
155 buf = (gpointer)(self->block_size + (char *)buf);
156 len -= self->block_size;
159 /* and finally store any leftover data in the partial buffer */
161 memmove(self->partial, buf, len);
162 self->partial_length = len;
172 XferDestDevice *self = XFER_DEST_DEVICE(elt);
173 self->partial = NULL;
180 XferDestDevice *self = XFER_DEST_DEVICE(obj_self);
183 g_free(self->partial);
189 XferDestDeviceClass * selfc)
191 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
192 GObjectClass *goc = (GObjectClass*) klass;
193 static xfer_element_mech_pair_t mech_pairs[] = {
194 { XFER_MECH_PUSH_BUFFER, XFER_MECH_NONE, 0, 0},
195 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
198 klass->push_buffer = push_buffer_impl;
200 klass->perl_class = "Amanda::Xfer::Dest::Device";
201 klass->mech_pairs = mech_pairs;
203 goc->finalize = finalize_impl;
205 parent_class = g_type_class_peek_parent(selfc);
209 xfer_dest_device_get_type (void)
211 static GType type = 0;
213 if G_UNLIKELY(type == 0) {
214 static const GTypeInfo info = {
215 sizeof (XferDestDeviceClass),
216 (GBaseInitFunc) NULL,
217 (GBaseFinalizeFunc) NULL,
218 (GClassInitFunc) class_init,
219 (GClassFinalizeFunc) NULL,
220 NULL /* class_data */,
221 sizeof (XferDestDevice),
223 (GInstanceInitFunc) instance_init,
227 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferDestDevice", &info, 0);
233 /* create an element of this class; prototype is in xfer-device.h */
237 gboolean cancel_at_leom)
239 XferDestDevice *self = (XferDestDevice *)g_object_new(XFER_DEST_DEVICE_TYPE, NULL);
240 XferElement *elt = XFER_ELEMENT(self);
242 g_assert(device != NULL);
244 self->device = device;
245 self->cancel_at_leom = cancel_at_leom;