Imported Upstream version 3.2.0
[debian/amanda] / device-src / xfer-source-device.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 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 "device.h"
25 #include "property.h"
26 #include "xfer-device.h"
27
28 /*
29  * Class declaration
30  *
31  * This declaration is entirely private; nothing but xfer_source_device() references
32  * it directly.
33  */
34
35 GType xfer_source_device_get_type(void);
36 #define XFER_SOURCE_DEVICE_TYPE (xfer_source_device_get_type())
37 #define XFER_SOURCE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_device_get_type(), XferSourceDevice)
38 #define XFER_SOURCE_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_device_get_type(), XferSourceDevice const)
39 #define XFER_SOURCE_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_device_get_type(), XferSourceDeviceClass)
40 #define IS_XFER_SOURCE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_device_get_type ())
41 #define XFER_SOURCE_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_device_get_type(), XferSourceDeviceClass)
42
43 static GObjectClass *parent_class = NULL;
44
45 /*
46  * Main object structure
47  */
48
49 typedef struct XferSourceDevice {
50     XferElement __parent__;
51
52     Device *device;
53     size_t block_size;
54     gboolean cancelled;
55 } XferSourceDevice;
56
57 /*
58  * Class definition
59  */
60
61 typedef struct {
62     XferElementClass __parent__;
63 } XferSourceDeviceClass;
64
65 /*
66  * Implementation
67  */
68
69 static gpointer
70 pull_buffer_impl(
71     XferElement *elt,
72     size_t *size)
73 {
74     XferSourceDevice *self = (XferSourceDevice *)elt;
75     gpointer buf = NULL;
76     int result;
77     int devsize;
78
79     /* indicate EOF on an cancel */
80     if (elt->cancelled) {
81         *size = 0;
82         return NULL;
83     }
84
85     /* get the device block size */
86     if (self->block_size == 0) {
87         self->block_size = self->device->block_size;
88     }
89
90     do {
91         buf = g_malloc(self->block_size);
92         devsize = (int)self->block_size;
93         result = device_read_block(self->device, buf, &devsize);
94         *size = devsize;
95
96         /* if the buffer was too small, loop around again */
97         if (result == 0) {
98             g_assert(*size > self->block_size);
99             self->block_size = devsize;
100             amfree(buf);
101         }
102     } while (result == 0);
103
104     if (result < 0) {
105         amfree(buf);
106
107         /* if we're not at EOF, it's an error */
108         if (!self->device->is_eof) {
109             xfer_cancel_with_error(elt,
110                 _("error reading from %s: %s"),
111                 self->device->device_name,
112                 device_error_or_status(self->device));
113             wait_until_xfer_cancelled(elt->xfer);
114         }
115
116         *size = 0;
117         return NULL;
118     }
119
120     return buf;
121 }
122
123 static void
124 instance_init(
125     XferElement *elt)
126 {
127     elt->can_generate_eof = TRUE;
128 }
129
130 static void
131 class_init(
132     XferSourceDeviceClass * selfc)
133 {
134     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
135     static xfer_element_mech_pair_t mech_pairs[] = {
136         { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 0, 1},
137         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
138     };
139
140     klass->pull_buffer = pull_buffer_impl;
141
142     klass->perl_class = "Amanda::Xfer::Source::Device";
143     klass->mech_pairs = mech_pairs;
144
145     parent_class = g_type_class_peek_parent(selfc);
146 }
147
148 GType
149 xfer_source_device_get_type (void)
150 {
151     static GType type = 0;
152
153     if G_UNLIKELY(type == 0) {
154         static const GTypeInfo info = {
155             sizeof (XferSourceDeviceClass),
156             (GBaseInitFunc) NULL,
157             (GBaseFinalizeFunc) NULL,
158             (GClassInitFunc) class_init,
159             (GClassFinalizeFunc) NULL,
160             NULL /* class_data */,
161             sizeof (XferSourceDevice),
162             0 /* n_preallocs */,
163             (GInstanceInitFunc) instance_init,
164             NULL
165         };
166
167         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceDevice", &info, 0);
168     }
169
170     return type;
171 }
172
173 /* create an element of this class; prototype is in xfer-device.h */
174 XferElement *
175 xfer_source_device(
176     Device *device)
177 {
178     XferSourceDevice *self = (XferSourceDevice *)g_object_new(XFER_SOURCE_DEVICE_TYPE, NULL);
179     XferElement *elt = XFER_ELEMENT(self);
180
181     g_assert(device != NULL);
182
183     self->device = device;
184
185     return elt;
186 }