2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009 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__;
53 XferElement *dest_taper;
61 XferElementClass __parent__;
62 } XferSourceHoldingClass;
70 XferSourceHolding *self)
76 /* try to close an already-open file */
78 if (close(self->fd) < 0) {
79 xfer_cancel_with_error(XFER_ELEMENT(self),
80 "while closing holding file: %s", strerror(errno));
81 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
88 /* if we have no next filename, then we're at EOF */
89 if (!self->next_filename) {
93 /* otherwise, open up the next file */
94 self->fd = open(self->next_filename, O_RDONLY);
96 xfer_cancel_with_error(XFER_ELEMENT(self),
97 "while opening holding file '%s': %s",
98 self->next_filename, strerror(errno));
99 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
103 /* get a downstream XferDestTaper, if one exists. This check happens
104 * for each chunk, but chunks are large, so that's OK. */
105 if (!self->dest_taper) {
106 XferElement *elt = (XferElement *)self;
108 /* the xfer may have inserted glue between this element and
109 * the XferDestTaper. Glue does not change the bytestream, so
110 * it does not interfere with cache_inform calls. */
111 XferElement *iter = elt->downstream;
112 while (iter && IS_XFER_ELEMENT_GLUE(iter)) {
113 iter = iter->downstream;
115 if (IS_XFER_DEST_TAPER(iter))
116 self->dest_taper = iter;
119 /* tell a XferDestTaper about the new file */
120 if (self->dest_taper) {
122 if (fstat(self->fd, &st) < 0) {
123 xfer_cancel_with_error(XFER_ELEMENT(self),
124 "while finding size of holding file '%s': %s",
125 self->next_filename, strerror(errno));
126 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
130 xfer_dest_taper_cache_inform(self->dest_taper,
133 st.st_size - DISK_BLOCK_BYTES);
136 /* read the header from the file and determine the filename of the next chunk */
137 hdrbuf = g_malloc(DISK_BLOCK_BYTES);
138 bytes_read = full_read(self->fd, hdrbuf, DISK_BLOCK_BYTES);
139 if (bytes_read < DISK_BLOCK_BYTES) {
141 xfer_cancel_with_error(XFER_ELEMENT(self),
142 "while reading header from holding file '%s': %s",
143 self->next_filename, strerror(errno));
144 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
148 parse_file_header(hdrbuf, &hdr, DISK_BLOCK_BYTES);
152 if (hdr.type != F_DUMPFILE && hdr.type != F_CONT_DUMPFILE) {
153 dumpfile_free_data(&hdr);
154 xfer_cancel_with_error(XFER_ELEMENT(self),
155 "unexpected header type %d in holding file '%s'",
156 hdr.type, self->next_filename);
157 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
161 g_free(self->next_filename);
162 if (hdr.cont_filename[0]) {
163 self->next_filename = g_strdup(hdr.cont_filename);
165 self->next_filename = NULL;
167 dumpfile_free_data(&hdr);
172 /* pick an arbitrary block size for reading */
173 #define HOLDING_BLOCK_SIZE (1024*128)
180 XferSourceHolding *self = (XferSourceHolding *)elt;
187 if (self->fd == -1) {
188 if (!start_new_chunk(self))
192 buf = g_malloc(HOLDING_BLOCK_SIZE);
195 bytes_read = full_read(self->fd, buf, HOLDING_BLOCK_SIZE);
196 if (bytes_read > 0) {
201 /* did an error occur? */
203 xfer_cancel_with_error(XFER_ELEMENT(self),
204 "while reading holding file: %s", strerror(errno));
205 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
209 if (!start_new_chunk(self))
223 XferSourceHolding *self = (XferSourceHolding *)elt;
225 elt->can_generate_eof = TRUE;
233 XferSourceHolding *self = (XferSourceHolding *)obj_self;
235 if (self->next_filename)
236 g_free(self->next_filename);
239 close(self->fd); /* ignore error; we were probably already cancelled */
241 G_OBJECT_CLASS(parent_class)->finalize(obj_self);
246 XferSourceHoldingClass * selfc)
248 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
249 GObjectClass *goc = G_OBJECT_CLASS(selfc);
250 static xfer_element_mech_pair_t mech_pairs[] = {
251 { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 1, 0},
252 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
255 klass->pull_buffer = pull_buffer_impl;
257 klass->perl_class = "Amanda::Xfer::Source::Holding";
258 klass->mech_pairs = mech_pairs;
260 goc->finalize = finalize_impl;
262 parent_class = g_type_class_peek_parent(selfc);
266 xfer_source_holding_get_type (void)
268 static GType type = 0;
270 if G_UNLIKELY(type == 0) {
271 static const GTypeInfo info = {
272 sizeof (XferSourceHoldingClass),
273 (GBaseInitFunc) NULL,
274 (GBaseFinalizeFunc) NULL,
275 (GClassInitFunc) class_init,
276 (GClassFinalizeFunc) NULL,
277 NULL /* class_data */,
278 sizeof (XferSourceHolding),
280 (GInstanceInitFunc) instance_init,
284 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceHolding", &info, 0);
290 /* create an element of this class; prototype is in xfer-element.h */
293 const char *filename)
295 XferSourceHolding *self = (XferSourceHolding *)g_object_new(XFER_SOURCE_HOLDING_TYPE, NULL);
296 XferElement *elt = XFER_ELEMENT(self);
298 self->next_filename = g_strdup(filename);