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