Imported Upstream version 2.6.1p2
[debian/amanda] / xfer-src / source-device.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008 Zmanda Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "amxfer.h"
21 #include "amanda.h"
22 #include "device.h"
23
24 /*
25  * Class declaration
26  *
27  * This declaration is entirely private; nothing but xfer_source_device() references
28  * it directly.
29  */
30
31 GType xfer_source_device_get_type(void);
32 #define XFER_SOURCE_DEVICE_TYPE (xfer_source_device_get_type())
33 #define XFER_SOURCE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_device_get_type(), XferSourceDevice)
34 #define XFER_SOURCE_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_device_get_type(), XferSourceDevice const)
35 #define XFER_SOURCE_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_device_get_type(), XferSourceDeviceClass)
36 #define IS_XFER_SOURCE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_device_get_type ())
37 #define XFER_SOURCE_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_device_get_type(), XferSourceDeviceClass)
38
39 static GObjectClass *parent_class = NULL;
40
41 /*
42  * Main object structure
43  */
44
45 typedef struct XferSourceDevice {
46     XferElement __parent__;
47
48     Device *device;
49     size_t block_size;
50     gboolean cancelled;
51 } XferSourceDevice;
52
53 /*
54  * Class definition
55  */
56
57 typedef struct {
58     XferElementClass __parent__;
59 } XferSourceDeviceClass;
60
61 /*
62  * Implementation
63  */
64
65 static gpointer
66 pull_buffer_impl(
67     XferElement *elt,
68     size_t *size)
69 {
70     XferSourceDevice *self = (XferSourceDevice *)elt;
71     gpointer buf;
72     int result;
73     int devsize;
74
75     /* indicate EOF on an cancel */
76     if (elt->cancelled) {
77         *size = 0;
78         return NULL;
79     }
80
81     /* get the device block size */
82     if (self->block_size == 0) {
83         self->block_size = self->device->block_size;
84     }
85
86     do {
87         buf = g_malloc(self->block_size);
88         devsize = (int)self->block_size;
89         result = device_read_block(self->device, buf, &devsize);
90         *size = devsize;
91
92         /* if the buffer was too small, loop around again */
93         if (result == 0) {
94             g_assert(*size > self->block_size);
95             self->block_size = devsize;
96             amfree(buf);
97         }
98     } while (result == 0);
99
100     if (result < 0) {
101         /* if we're not at EOF, it's an error */
102         if (!self->device->is_eof) {
103             xfer_element_handle_error(elt,
104                 _("error reading from %s: %s"),
105                 self->device->device_name,
106                 device_error_or_status(self->device));
107         }
108
109         *size = 0;
110         return NULL;
111     }
112
113     return buf;
114 }
115
116 static void
117 instance_init(
118     XferElement *elt)
119 {
120     elt->can_generate_eof = TRUE;
121 }
122
123 static void
124 class_init(
125     XferSourceDeviceClass * selfc)
126 {
127     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
128     static xfer_element_mech_pair_t mech_pairs[] = {
129         { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 0, 1},
130         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
131     };
132
133     klass->pull_buffer = pull_buffer_impl;
134
135     klass->perl_class = "Amanda::Xfer::Source::Device";
136     klass->mech_pairs = mech_pairs;
137
138     parent_class = g_type_class_peek_parent(selfc);
139 }
140
141 GType
142 xfer_source_device_get_type (void)
143 {
144     static GType type = 0;
145
146     if G_UNLIKELY(type == 0) {
147         static const GTypeInfo info = {
148             sizeof (XferSourceDeviceClass),
149             (GBaseInitFunc) NULL,
150             (GBaseFinalizeFunc) NULL,
151             (GClassInitFunc) class_init,
152             (GClassFinalizeFunc) NULL,
153             NULL /* class_data */,
154             sizeof (XferSourceDevice),
155             0 /* n_preallocs */,
156             (GInstanceInitFunc) instance_init,
157             NULL
158         };
159
160         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceDevice", &info, 0);
161     }
162
163     return type;
164 }
165
166 /* create an element of this class; prototype is in xfer-element.h */
167 XferElement *
168 xfer_source_device(
169     Device *device)
170 {
171     XferSourceDevice *self = (XferSourceDevice *)g_object_new(XFER_SOURCE_DEVICE_TYPE, NULL);
172     XferElement *elt = XFER_ELEMENT(self);
173
174     g_assert(device != NULL);
175
176     self->device = device;
177
178     return elt;
179 }