Imported Upstream version 3.2.0
[debian/amanda] / server-src / xfer-source-holding.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2009 Zmanda, Inc.  All Rights Reserved.
4  *
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.
8  *
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
12  * for more details.
13  *
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
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 #include "amanda.h"
23 #include "xfer-server.h"
24 #include "xfer-device.h"
25
26 /*
27  * Class declaration
28  *
29  * This declaration is entirely private; nothing but xfer_source_holding() references
30  * it directly.
31  */
32
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)
40
41 static GObjectClass *parent_class = NULL;
42
43 /*
44  * Main object structure
45  */
46
47 typedef struct XferSourceHolding {
48     XferElement __parent__;
49
50     int fd;
51     char *next_filename;
52
53     XferElement *dest_taper;
54 } XferSourceHolding;
55
56 /*
57  * Class definition
58  */
59
60 typedef struct {
61     XferElementClass __parent__;
62 } XferSourceHoldingClass;
63
64 /*
65  * Implementation
66  */
67
68 static gboolean
69 start_new_chunk(
70     XferSourceHolding *self)
71 {
72     char *hdrbuf = NULL;
73     dumpfile_t hdr;
74     size_t bytes_read;
75
76     /* try to close an already-open file */
77     if (self->fd != -1) {
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);
82             return FALSE;
83         }
84
85         self->fd = -1;
86     }
87
88     /* if we have no next filename, then we're at EOF */
89     if (!self->next_filename) {
90         return FALSE;
91     }
92
93     /* otherwise, open up the next file */
94     self->fd = open(self->next_filename, O_RDONLY);
95     if (self->fd < 0) {
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);
100         return FALSE;
101     }
102
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;
107
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;
114         }
115         if (IS_XFER_DEST_TAPER(iter))
116             self->dest_taper = iter;
117     }
118
119     /* tell a XferDestTaper about the new file */
120     if (self->dest_taper) {
121         struct stat st;
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);
127             return FALSE;
128         }
129
130         xfer_dest_taper_cache_inform(self->dest_taper,
131             self->next_filename,
132             DISK_BLOCK_BYTES,
133             st.st_size - DISK_BLOCK_BYTES);
134     }
135
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) {
140         g_free(hdrbuf);
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);
145         return FALSE;
146     }
147
148     parse_file_header(hdrbuf, &hdr, DISK_BLOCK_BYTES);
149     g_free(hdrbuf);
150     hdrbuf = NULL;
151
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);
158         return FALSE;
159     }
160
161     g_free(self->next_filename);
162     if (hdr.cont_filename[0]) {
163         self->next_filename = g_strdup(hdr.cont_filename);
164     } else {
165         self->next_filename = NULL;
166     }
167     dumpfile_free_data(&hdr);
168
169     return TRUE;
170 }
171
172 /* pick an arbitrary block size for reading */
173 #define HOLDING_BLOCK_SIZE (1024*128)
174
175 static gpointer
176 pull_buffer_impl(
177     XferElement *elt,
178     size_t *size)
179 {
180     XferSourceHolding *self = (XferSourceHolding *)elt;
181     char *buf = NULL;
182     size_t bytes_read;
183
184     if (elt->cancelled)
185         goto return_eof;
186
187     if (self->fd == -1) {
188         if (!start_new_chunk(self))
189             goto return_eof;
190     }
191
192     buf = g_malloc(HOLDING_BLOCK_SIZE);
193
194     while (1) {
195         bytes_read = full_read(self->fd, buf, HOLDING_BLOCK_SIZE);
196         if (bytes_read > 0) {
197             *size = bytes_read;
198             return buf;
199         }
200
201         /* did an error occur? */
202         if (errno != 0) {
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);
206             goto return_eof;
207         }
208
209         if (!start_new_chunk(self))
210             goto return_eof;
211     }
212
213 return_eof:
214     g_free(buf);
215     *size = 0;
216     return NULL;
217 }
218
219 static void
220 instance_init(
221     XferElement *elt)
222 {
223     XferSourceHolding *self = (XferSourceHolding *)elt;
224
225     elt->can_generate_eof = TRUE;
226     self->fd = -1;
227 }
228
229 static void
230 finalize_impl(
231     GObject * obj_self)
232 {
233     XferSourceHolding *self = (XferSourceHolding *)obj_self;
234
235     if (self->next_filename)
236         g_free(self->next_filename);
237
238     if (self->fd != -1)
239         close(self->fd); /* ignore error; we were probably already cancelled */
240
241     G_OBJECT_CLASS(parent_class)->finalize(obj_self);
242 }
243
244 static void
245 class_init(
246     XferSourceHoldingClass * selfc)
247 {
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},
253     };
254
255     klass->pull_buffer = pull_buffer_impl;
256
257     klass->perl_class = "Amanda::Xfer::Source::Holding";
258     klass->mech_pairs = mech_pairs;
259
260     goc->finalize = finalize_impl;
261
262     parent_class = g_type_class_peek_parent(selfc);
263 }
264
265 GType
266 xfer_source_holding_get_type (void)
267 {
268     static GType type = 0;
269
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),
279             0 /* n_preallocs */,
280             (GInstanceInitFunc) instance_init,
281             NULL
282         };
283
284         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceHolding", &info, 0);
285     }
286
287     return type;
288 }
289
290 /* create an element of this class; prototype is in xfer-element.h */
291 XferElement *
292 xfer_source_holding(
293     const char *filename)
294 {
295     XferSourceHolding *self = (XferSourceHolding *)g_object_new(XFER_SOURCE_HOLDING_TYPE, NULL);
296     XferElement *elt = XFER_ELEMENT(self);
297
298     self->next_filename = g_strdup(filename);
299
300     return elt;
301 }
302