Imported Upstream version 3.3.3
[debian/amanda] / device-src / xfer-dest-device.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2009-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 "device.h"
26 #include "property.h"
27 #include "xfer-device.h"
28
29 /*
30  * Class declaration
31  *
32  * This declaration is entirely private; nothing but xfer_dest_device() references
33  * it directly.
34  */
35
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)
43
44 static GObjectClass *parent_class = NULL;
45
46 /*
47  * Main object structure
48  */
49
50 typedef struct XferDestDevice {
51     XferElement __parent__;
52
53     Device *device;
54
55     gboolean cancel_at_leom;
56
57     gpointer partial;
58     gsize block_size;
59     gsize partial_length;
60 } XferDestDevice;
61
62 /*
63  * Class definition
64  */
65
66 typedef struct {
67     XferElementClass __parent__;
68 } XferDestDeviceClass;
69
70 /*
71  * Implementation
72  */
73
74 static gboolean
75 do_block(
76     XferDestDevice *self,
77     guint size,
78     gpointer data)
79 {
80     XferElement *elt = XFER_ELEMENT(self);
81
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);
86         return FALSE;
87     }
88
89     /* check for LEOM */
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);
93         return FALSE;
94     }
95
96     return TRUE;
97 }
98
99 static void
100 push_buffer_impl(
101     XferElement *elt,
102     gpointer buf,
103     size_t len)
104 {
105     XferDestDevice *self = XFER_DEST_DEVICE(elt);
106     gpointer to_free = buf;
107
108     /* Handle EOF */
109     if (!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)) {
113                 return;
114             }
115             self->partial_length = 0;
116         }
117
118         device_finish_file(self->device);
119         return;
120     }
121
122     /* set up the block buffer, now that we can depend on having a blocksize
123      * from the device */
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;
128     }
129
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);
135         len -= to_copy;
136         self->partial_length += to_copy;
137     }
138
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)) {
142             g_free(to_free);
143             return;
144         }
145         self->partial_length = 0;
146     }
147
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)) {
151             g_free(to_free);
152             return;
153         }
154
155         buf = (gpointer)(self->block_size + (char *)buf);
156         len -= self->block_size;
157     }
158
159     /* and finally store any leftover data in the partial buffer */
160     if (len) {
161         memmove(self->partial, buf, len);
162         self->partial_length = len;
163     }
164
165     g_free(to_free);
166 }
167
168 static void
169 instance_init(
170     XferElement *elt)
171 {
172     XferDestDevice *self = XFER_DEST_DEVICE(elt);
173     self->partial = NULL;
174 }
175
176 static void
177 finalize_impl(
178     GObject * obj_self)
179 {
180     XferDestDevice *self = XFER_DEST_DEVICE(obj_self);
181
182     if (self->partial) {
183         g_free(self->partial);
184     }
185 }
186
187 static void
188 class_init(
189     XferDestDeviceClass * selfc)
190 {
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},
196     };
197
198     klass->push_buffer = push_buffer_impl;
199
200     klass->perl_class = "Amanda::Xfer::Dest::Device";
201     klass->mech_pairs = mech_pairs;
202
203     goc->finalize = finalize_impl;
204
205     parent_class = g_type_class_peek_parent(selfc);
206 }
207
208 GType
209 xfer_dest_device_get_type (void)
210 {
211     static GType type = 0;
212
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),
222             0 /* n_preallocs */,
223             (GInstanceInitFunc) instance_init,
224             NULL
225         };
226
227         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferDestDevice", &info, 0);
228     }
229
230     return type;
231 }
232
233 /* create an element of this class; prototype is in xfer-device.h */
234 XferElement *
235 xfer_dest_device(
236     Device *device,
237     gboolean cancel_at_leom)
238 {
239     XferDestDevice *self = (XferDestDevice *)g_object_new(XFER_DEST_DEVICE_TYPE, NULL);
240     XferElement *elt = XFER_ELEMENT(self);
241
242     g_assert(device != NULL);
243
244     self->device = device;
245     self->cancel_at_leom = cancel_at_leom;
246
247     return elt;
248 }