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