2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009, 2010 Zmanda, Inc. All Rights Reserved.
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.
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
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
18 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
26 #include "xfer-device.h"
31 * This declaration is entirely private; nothing but xfer_dest_device() references
35 GType xfer_dest_device_get_type(void);
36 #define XFER_DEST_DEVICE_TYPE (xfer_dest_device_get_type())
37 #define XFER_DEST_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_device_get_type(), XferDestDevice)
38 #define XFER_DEST_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_device_get_type(), XferDestDevice const)
39 #define XFER_DEST_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_device_get_type(), XferDestDeviceClass)
40 #define IS_XFER_DEST_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_device_get_type ())
41 #define XFER_DEST_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_device_get_type(), XferDestDeviceClass)
43 static GObjectClass *parent_class = NULL;
46 * Main object structure
49 typedef struct XferDestDevice {
50 XferElement __parent__;
54 gboolean cancel_at_leom;
66 XferElementClass __parent__;
67 } XferDestDeviceClass;
79 XferElement *elt = XFER_ELEMENT(self);
81 if (!device_write_block(self->device, size, data)) {
82 xfer_cancel_with_error(elt, "%s: %s",
83 self->device->device_name, device_error_or_status(self->device));
84 wait_until_xfer_cancelled(elt->xfer);
89 if (self->cancel_at_leom && self->device->is_eom) {
90 xfer_cancel_with_error(elt, "%s: LEOM detected", self->device->device_name);
91 wait_until_xfer_cancelled(elt->xfer);
104 XferDestDevice *self = XFER_DEST_DEVICE(elt);
105 gpointer to_free = buf;
109 /* write out the partial buffer, if there's anything in it */
110 if (self->partial_length) {
111 if (!do_block(self, self->block_size, self->partial)) {
114 self->partial_length = 0;
117 device_finish_file(self->device);
121 /* set up the block buffer, now that we can depend on having a blocksize
123 if (!self->partial) {
124 self->partial = g_malloc(self->device->block_size);
125 self->block_size = self->device->block_size;
126 self->partial_length = 0;
129 /* if we already have data in the buffer, add the new data to it */
130 if (self->partial_length != 0) {
131 gsize to_copy = min(self->block_size - self->partial_length, len);
132 memmove(self->partial + self->partial_length, buf, to_copy);
133 buf = (gpointer)(to_copy + (char *)buf);
135 self->partial_length += to_copy;
138 /* and if the buffer is now full, write the block */
139 if (self->partial_length == self->block_size) {
140 if (!do_block(self, self->block_size, self->partial)) {
144 self->partial_length = 0;
147 /* write any whole blocks directly from the push buffer */
148 while (len >= self->block_size) {
149 if (!do_block(self, self->block_size, buf)) {
154 buf = (gpointer)(self->block_size + (char *)buf);
155 len -= self->block_size;
158 /* and finally store any leftover data in the partial buffer */
160 memmove(self->partial, buf, len);
161 self->partial_length = len;
171 XferDestDevice *self = XFER_DEST_DEVICE(elt);
172 self->partial = NULL;
179 XferDestDevice *self = XFER_DEST_DEVICE(obj_self);
182 g_free(self->partial);
188 XferDestDeviceClass * selfc)
190 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
191 GObjectClass *goc = (GObjectClass*) klass;
192 static xfer_element_mech_pair_t mech_pairs[] = {
193 { XFER_MECH_PUSH_BUFFER, XFER_MECH_NONE, 0, 0},
194 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
197 klass->push_buffer = push_buffer_impl;
199 klass->perl_class = "Amanda::Xfer::Dest::Device";
200 klass->mech_pairs = mech_pairs;
202 goc->finalize = finalize_impl;
204 parent_class = g_type_class_peek_parent(selfc);
208 xfer_dest_device_get_type (void)
210 static GType type = 0;
212 if G_UNLIKELY(type == 0) {
213 static const GTypeInfo info = {
214 sizeof (XferDestDeviceClass),
215 (GBaseInitFunc) NULL,
216 (GBaseFinalizeFunc) NULL,
217 (GClassInitFunc) class_init,
218 (GClassFinalizeFunc) NULL,
219 NULL /* class_data */,
220 sizeof (XferDestDevice),
222 (GInstanceInitFunc) instance_init,
226 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferDestDevice", &info, 0);
232 /* create an element of this class; prototype is in xfer-device.h */
236 gboolean cancel_at_leom)
238 XferDestDevice *self = (XferDestDevice *)g_object_new(XFER_DEST_DEVICE_TYPE, NULL);
239 XferElement *elt = XFER_ELEMENT(self);
241 g_assert(device != NULL);
243 self->device = device;
244 self->cancel_at_leom = cancel_at_leom;