2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
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.
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
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
18 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
23 #include "xfer-server.h"
24 #include "xfer-device.h"
29 * This declaration is entirely private; nothing but xfer_source_holding() references
33 GType xfer_source_holding_get_type(void);
34 #define XFER_SOURCE_HOLDING_TYPE (xfer_source_holding_get_type())
35 #define XFER_SOURCE_HOLDING(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_holding_get_type(), XferSourceHolding)
36 #define XFER_SOURCE_HOLDING_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_holding_get_type(), XferSourceHolding const)
37 #define XFER_SOURCE_HOLDING_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_holding_get_type(), XferSourceHoldingClass)
38 #define IS_XFER_SOURCE_HOLDING(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_holding_get_type ())
39 #define XFER_SOURCE_HOLDING_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_holding_get_type(), XferSourceHoldingClass)
41 static GObjectClass *parent_class = NULL;
44 * Main object structure
47 typedef struct XferSourceHolding {
48 XferElement __parent__;
54 XferElement *dest_taper;
62 XferElementClass __parent__;
63 } XferSourceHoldingClass;
71 XferSourceHolding *self)
77 /* try to close an already-open file */
79 if (close(self->fd) < 0) {
80 xfer_cancel_with_error(XFER_ELEMENT(self),
81 "while closing holding file: %s", strerror(errno));
82 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
89 /* if we have no next filename, then we're at EOF */
90 if (!self->next_filename) {
94 /* otherwise, open up the next file */
95 self->fd = open(self->next_filename, O_RDONLY);
97 xfer_cancel_with_error(XFER_ELEMENT(self),
98 "while opening holding file '%s': %s",
99 self->next_filename, strerror(errno));
100 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
104 /* get a downstream XferDestTaper, if one exists. This check happens
105 * for each chunk, but chunks are large, so that's OK. */
106 if (!self->dest_taper) {
107 XferElement *elt = (XferElement *)self;
109 /* the xfer may have inserted glue between this element and
110 * the XferDestTaper. Glue does not change the bytestream, so
111 * it does not interfere with cache_inform calls. */
112 XferElement *iter = elt->downstream;
113 while (iter && IS_XFER_ELEMENT_GLUE(iter)) {
114 iter = iter->downstream;
116 if (IS_XFER_DEST_TAPER(iter))
117 self->dest_taper = iter;
120 /* tell a XferDestTaper about the new file */
121 if (self->dest_taper) {
123 if (fstat(self->fd, &st) < 0) {
124 xfer_cancel_with_error(XFER_ELEMENT(self),
125 "while finding size of holding file '%s': %s",
126 self->next_filename, strerror(errno));
127 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
131 xfer_dest_taper_cache_inform(self->dest_taper,
134 st.st_size - DISK_BLOCK_BYTES);
137 /* read the header from the file and determine the filename of the next chunk */
138 hdrbuf = g_malloc(DISK_BLOCK_BYTES);
139 bytes_read = full_read(self->fd, hdrbuf, DISK_BLOCK_BYTES);
140 if (bytes_read < DISK_BLOCK_BYTES) {
142 xfer_cancel_with_error(XFER_ELEMENT(self),
143 "while reading header from holding file '%s': %s",
144 self->next_filename, strerror(errno));
145 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
149 parse_file_header(hdrbuf, &hdr, DISK_BLOCK_BYTES);
153 if (hdr.type != F_DUMPFILE && hdr.type != F_CONT_DUMPFILE) {
154 dumpfile_free_data(&hdr);
155 xfer_cancel_with_error(XFER_ELEMENT(self),
156 "unexpected header type %d in holding file '%s'",
157 hdr.type, self->next_filename);
158 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
162 g_free(self->next_filename);
163 if (hdr.cont_filename[0]) {
164 self->next_filename = g_strdup(hdr.cont_filename);
166 self->next_filename = NULL;
168 dumpfile_free_data(&hdr);
173 /* pick an arbitrary block size for reading */
174 #define HOLDING_BLOCK_SIZE (1024*128)
181 XferSourceHolding *self = (XferSourceHolding *)elt;
188 if (self->fd == -1) {
189 if (!start_new_chunk(self))
193 buf = g_malloc(HOLDING_BLOCK_SIZE);
196 bytes_read = full_read(self->fd, buf, HOLDING_BLOCK_SIZE);
197 if (bytes_read > 0) {
199 self->bytes_read += bytes_read;
203 /* did an error occur? */
205 xfer_cancel_with_error(XFER_ELEMENT(self),
206 "while reading holding file: %s", strerror(errno));
207 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
211 if (!start_new_chunk(self))
225 XferSourceHolding *self = (XferSourceHolding *)elt;
227 elt->can_generate_eof = TRUE;
235 XferSourceHolding *self = (XferSourceHolding *)obj_self;
237 if (self->next_filename)
238 g_free(self->next_filename);
241 close(self->fd); /* ignore error; we were probably already cancelled */
243 G_OBJECT_CLASS(parent_class)->finalize(obj_self);
248 XferSourceHoldingClass * selfc)
250 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
251 GObjectClass *goc = G_OBJECT_CLASS(selfc);
252 static xfer_element_mech_pair_t mech_pairs[] = {
253 { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 1, 0},
254 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
257 klass->pull_buffer = pull_buffer_impl;
259 klass->perl_class = "Amanda::Xfer::Source::Holding";
260 klass->mech_pairs = mech_pairs;
262 goc->finalize = finalize_impl;
264 parent_class = g_type_class_peek_parent(selfc);
268 xfer_source_holding_get_type (void)
270 static GType type = 0;
272 if G_UNLIKELY(type == 0) {
273 static const GTypeInfo info = {
274 sizeof (XferSourceHoldingClass),
275 (GBaseInitFunc) NULL,
276 (GBaseFinalizeFunc) NULL,
277 (GClassInitFunc) class_init,
278 (GClassFinalizeFunc) NULL,
279 NULL /* class_data */,
280 sizeof (XferSourceHolding),
282 (GInstanceInitFunc) instance_init,
286 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceHolding", &info, 0);
292 /* create an element of this class; prototype is in xfer-element.h */
295 const char *filename)
297 XferSourceHolding *self = (XferSourceHolding *)g_object_new(XFER_SOURCE_HOLDING_TYPE, NULL);
298 XferElement *elt = XFER_ELEMENT(self);
300 self->next_filename = g_strdup(filename);
301 self->bytes_read = 0;
307 xfer_source_holding_get_bytes_read(
310 XferSourceHolding *self = (XferSourceHolding *)elt;
312 return self->bytes_read;