Imported Upstream version 2.6.1
[debian/amanda] / server-src / taper-file-source.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2005-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 #define selfp (self->_priv)
21
22 #include "taper-file-source.h"
23
24 #include "fileheader.h"
25 #include "holding.h"
26
27 #define HOLDING_DISK_OPEN_FLAGS (O_NOCTTY | O_RDONLY)
28
29 struct _TaperFileSourcePrivate {
30     /* How many bytes have we written from the current part? */
31     guint64 current_part_pos;
32     /* Information about the files at the start of this part. */
33     dumpfile_t part_start_chunk_header;
34     int part_start_chunk_fd;
35     /* Where is the start of this part with respect to the first chunk
36        of the part? */
37     guint64 part_start_chunk_offset;
38     /* These may be the same as their part_start_chunk_ counterparts. */
39     dumpfile_t current_chunk_header;
40     int current_chunk_fd;
41     /* Current position of current_chunk_fd. */
42     guint64 current_chunk_position;
43     /* Expected number of split parts. */
44     int predicted_splits;
45 };
46 /* here are local prototypes */
47 static void taper_file_source_init (TaperFileSource * o);
48 static void taper_file_source_class_init (TaperFileSourceClass * c);
49 static ssize_t taper_file_source_read (TaperSource * pself, void * buf,
50                                             size_t count);
51 static gboolean taper_file_source_seek_to_part_start (TaperSource * pself);
52 static void taper_file_source_start_new_part (TaperSource * pself);
53 static int taper_file_source_predict_parts(TaperSource * pself);
54 static dumpfile_t * taper_file_source_get_first_header(TaperSource * pself);
55 static gboolean first_time_setup(TaperFileSource * self);
56
57 /* pointer to the class of our parent */
58 static TaperSourceClass *parent_class = NULL;
59
60 GType taper_file_source_get_type (void) {
61     static GType type = 0;
62     
63     if G_UNLIKELY(type == 0) {
64         static const GTypeInfo info = {
65             sizeof (TaperFileSourceClass),
66             (GBaseInitFunc) NULL,
67             (GBaseFinalizeFunc) NULL,
68             (GClassInitFunc) taper_file_source_class_init,
69             (GClassFinalizeFunc) NULL,
70             NULL /* class_data */,
71             sizeof (TaperFileSource),
72             0 /* n_preallocs */,
73             (GInstanceInitFunc) taper_file_source_init,
74             NULL
75         };
76         
77         type = g_type_register_static (TAPER_SOURCE_TYPE, "TaperFileSource",
78                                        &info, (GTypeFlags)0);
79     }
80     
81     return type;
82 }
83
84 static void
85 taper_file_source_finalize(GObject *obj_self)
86 {
87     TaperFileSource *self = TAPER_FILE_SOURCE (obj_self);
88     gpointer priv G_GNUC_UNUSED = self->_priv;
89     if(G_OBJECT_CLASS(parent_class)->finalize)
90         (* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
91     if(self->_priv->part_start_chunk_fd >= 0) {
92         close (self->_priv->part_start_chunk_fd);
93     }
94     if(self->_priv->current_chunk_fd >= 0) {
95         close (self->_priv->current_chunk_fd);
96     }
97     dumpfile_free_data(&(self->_priv->part_start_chunk_header));
98     dumpfile_free_data(&(self->_priv->current_chunk_header));
99     amfree(self->_priv);
100 }
101
102 static void 
103 taper_file_source_init (TaperFileSource * o G_GNUC_UNUSED)
104 {
105     o->_priv = malloc(sizeof(TaperFileSourcePrivate));
106     o->_priv->part_start_chunk_fd = -1;
107     o->_priv->current_chunk_fd = -1;
108     o->_priv->predicted_splits = -1;
109     fh_init(&o->_priv->part_start_chunk_header);
110     fh_init(&o->_priv->current_chunk_header);
111     o->holding_disk_file = NULL;
112 }
113
114 static void  taper_file_source_class_init (TaperFileSourceClass * c) {
115     GObjectClass *g_object_class = (GObjectClass*) c;
116     TaperSourceClass *taper_source_class = (TaperSourceClass *)c;
117
118     parent_class = g_type_class_ref (TAPER_SOURCE_TYPE);
119
120     taper_source_class->read = taper_file_source_read;
121     taper_source_class->seek_to_part_start =
122         taper_file_source_seek_to_part_start;
123     taper_source_class->start_new_part = taper_file_source_start_new_part;
124     taper_source_class->get_first_header = taper_file_source_get_first_header;
125     taper_source_class->predict_parts = taper_file_source_predict_parts;
126
127     g_object_class->finalize = taper_file_source_finalize;
128 }
129
130 static void compute_splits(TaperFileSource * self) {
131     guint64 total_kb;
132     int predicted_splits;
133     TaperSource * pself = (TaperSource*)self;
134
135     if (selfp->predicted_splits > 0) {
136         return;
137     }
138
139     if (pself->max_part_size <= 0) {
140         selfp->predicted_splits = 1;
141         return;
142     }
143
144     total_kb = holding_file_size(self->holding_disk_file, TRUE);
145     if (total_kb <= 0) {
146         g_fprintf(stderr, "taper: %lld KB holding file makes no sense, not precalculating splits\n",
147                 (long long)total_kb);
148         fflush(stderr);
149         selfp->predicted_splits = -1;
150         return;
151     }
152     
153     g_fprintf(stderr, "taper: Total dump size should be %jukb, part size is %jukb\n",
154             (uintmax_t)total_kb, (uintmax_t)pself->max_part_size);
155
156     predicted_splits = (total_kb * 1024) / pself->max_part_size;
157     if (predicted_splits == 0 ||
158         (0 != ((total_kb * 1024) % pself->max_part_size))) {
159         predicted_splits ++;
160     }
161     selfp->predicted_splits = predicted_splits;
162 }
163
164 static int taper_file_source_predict_parts(TaperSource * pself) {
165     TaperFileSource * self = TAPER_FILE_SOURCE(pself);
166     g_return_val_if_fail(self != NULL, -1);
167
168     compute_splits(self);
169
170     return selfp->predicted_splits;
171 }
172
173 static dumpfile_t * taper_file_source_get_first_header(TaperSource * pself) {
174     TaperFileSource * self = TAPER_FILE_SOURCE(pself);
175     g_return_val_if_fail(self != NULL, NULL);
176
177     first_time_setup(self);
178
179     if (parent_class->get_first_header) {
180         return (parent_class->get_first_header)(pself);
181     } else {
182         return NULL;
183     }
184 }
185
186 /* Open a holding disk and parse the header. Returns TRUE if
187    everything went OK. Writes the fd into fd_pointer and the header
188    into header_pointer. Both must be non-NULL. */
189 static gboolean open_holding_file(char * filename, int * fd_pointer,
190                                   dumpfile_t * header_pointer, char **errmsg) {
191     int fd;
192     size_t read_result;
193     char * header_buffer;
194
195     g_return_val_if_fail(filename != NULL, FALSE);
196     g_return_val_if_fail(fd_pointer != NULL, FALSE);
197     g_return_val_if_fail(header_pointer != NULL, FALSE);
198
199     fd = robust_open(filename, O_NOCTTY | O_RDONLY, 0);
200     if (fd < 0) {
201         *errmsg = newvstrallocf(*errmsg,
202                 "Could not open holding disk file \"%s\": %s",
203                 filename, strerror(errno));
204         return FALSE;
205     }
206
207     header_buffer = malloc(DISK_BLOCK_BYTES);
208     read_result = full_read(fd, header_buffer, DISK_BLOCK_BYTES);
209     if (read_result < DISK_BLOCK_BYTES) {
210         if (errno != 0) {
211             *errmsg = newvstrallocf(*errmsg,
212                     "Could not read header from holding disk file %s: %s",
213                     filename, strerror(errno));
214         } else {
215             *errmsg = newvstrallocf(*errmsg,
216                     "Could not read header from holding disk file %s: got EOF",
217                     filename);
218         }
219         aclose(fd);
220         amfree(header_buffer);
221         return FALSE;
222     }
223
224     dumpfile_free_data(header_pointer);
225     parse_file_header(header_buffer, header_pointer, DISK_BLOCK_BYTES);
226     amfree(header_buffer);
227     
228     if (!(header_pointer->type == F_DUMPFILE ||
229           header_pointer->type == F_CONT_DUMPFILE)) {
230         *errmsg = newvstrallocf(*errmsg,
231                 "Got strange header from file %s",
232                 filename);
233         aclose(fd);
234         return FALSE;
235     }
236     
237     *fd_pointer = fd;
238     return TRUE;
239 }
240
241 /* Copy fd and header information from first chunk fields to current
242    chunk. Returns FALSE if an error occurs (unlikely). */
243 static gboolean copy_chunk_data(int * from_fd, int* to_fd,
244                                 dumpfile_t * from_header,
245                                 dumpfile_t * to_header,
246                                 char **errmsg) {
247     g_return_val_if_fail(from_fd != NULL, FALSE);
248     g_return_val_if_fail(to_fd != NULL, FALSE);
249     g_return_val_if_fail(from_header != NULL, FALSE);
250     g_return_val_if_fail(to_header != NULL, FALSE);
251     g_return_val_if_fail(*to_fd < 0, FALSE);
252     
253     *to_fd = dup(*from_fd);
254     if (*to_fd < 0) {
255         *errmsg = newvstrallocf(*errmsg, "dup(%d) failed!", *from_fd);
256         return FALSE;
257     }
258
259     dumpfile_free_data(to_header);
260     dumpfile_copy_in_place(to_header, from_header);
261
262     return TRUE;
263 }
264
265
266 static gboolean first_time_setup(TaperFileSource * self) {
267     TaperSource * pself = (TaperSource*)self;
268
269     if (selfp->part_start_chunk_fd >= 0) {
270         return TRUE;
271     }
272
273     g_return_val_if_fail(self->holding_disk_file != NULL, FALSE);
274
275     if (!open_holding_file(self->holding_disk_file, 
276                            &(selfp->part_start_chunk_fd),
277                            &(selfp->part_start_chunk_header),
278                            &(pself->errmsg))) {
279         return FALSE;
280     }
281
282     /* We are all set; just copy the "start chunk" datums into the
283        "current chunk" fields. */
284     if (!copy_chunk_data(&(selfp->part_start_chunk_fd),
285                          &(selfp->current_chunk_fd),
286                          &(selfp->part_start_chunk_header),
287                          &(selfp->current_chunk_header),
288                          &(pself->errmsg))) {
289         aclose(selfp->part_start_chunk_fd);
290         return FALSE;
291     }
292
293     dumpfile_free(pself->first_header);
294     pself->first_header = dumpfile_copy(&(selfp->part_start_chunk_header));
295
296     /* Should not be necessary. You never know! */
297     selfp->current_part_pos = selfp->part_start_chunk_offset =
298         selfp->current_chunk_position = 0;
299
300     return TRUE;
301 }
302
303 static int retry_read(int fd, void * buf, size_t count) {
304     for (;;) {
305         int read_result = read(fd, buf, count);
306         if (read_result < 0 && (0
307 #ifdef EAGAIN
308                                 || errno == EAGAIN
309 #endif
310 #ifdef EWOULDBLOCK
311                                 || errno == EWOULDBLOCK
312 #endif
313 #ifdef EINTR
314                                 || errno == EINTR
315 #endif
316                   )) {
317             /* Try again. */
318             continue;
319         } else {
320             if (read_result < 0) {
321                 g_fprintf(stderr, "Error reading holding disk: %s\n",
322                         strerror(errno));
323             }
324             return read_result;
325         }
326     }
327 }
328
329 /* If another chunk is available, load it. Returns TRUE if there are
330    no more chunks or the next chunk is loaded, or FALSE if an error
331    occurs. */
332 static gboolean get_next_chunk(TaperFileSource * self) {
333     char * cont_filename = NULL;
334     TaperSource * pself = (TaperSource*)self;
335
336     if (selfp->current_chunk_header.cont_filename[0] != '\0') {
337         cont_filename =
338             g_strdup(selfp->current_chunk_header.cont_filename);
339     } else {
340         /* No more data. */
341         aclose(selfp->current_chunk_fd);
342         dumpfile_free_data(&(selfp->current_chunk_header));
343         bzero(&(selfp->current_chunk_header),
344               sizeof(selfp->current_chunk_header));
345         return TRUE;
346     }
347
348     /* More data. */
349
350     aclose(selfp->current_chunk_fd);
351
352     if (!open_holding_file(cont_filename,
353                            &(selfp->current_chunk_fd),
354                            &(selfp->current_chunk_header),
355                            &(pself->errmsg))) {
356         amfree(cont_filename);
357         dumpfile_free_data(&(selfp->current_chunk_header));
358         bzero(&(selfp->current_chunk_header),
359               sizeof(selfp->current_chunk_header));
360         aclose(selfp->current_chunk_fd);
361         return FALSE;
362     }
363
364     amfree(cont_filename);
365     selfp->current_chunk_position = 0;
366
367     return TRUE;
368 }
369
370 static ssize_t 
371 taper_file_source_read (TaperSource * pself, void * buf, size_t count) {
372     TaperFileSource * self = (TaperFileSource*) pself;
373     int read_result;
374
375     g_return_val_if_fail (self != NULL, -1);
376     g_return_val_if_fail (TAPER_IS_FILE_SOURCE (self), -1);
377     g_return_val_if_fail (buf != NULL, -1);
378     g_return_val_if_fail (count > 0, -1);
379     
380     if (!first_time_setup(self))
381         return -1;
382
383     if (pself->max_part_size > 0) {
384         count = MIN(count, pself->max_part_size - selfp->current_part_pos);
385     }
386     if (count <= 0) {
387         /* Was positive before. Thus we are at EOP. */
388         pself->end_of_part = TRUE;
389         return 0;
390     }
391
392     /* We don't use full_read, because we would rather return a partial
393      * read ASAP. */
394     read_result = retry_read(selfp->current_chunk_fd, buf, count);
395     if (read_result < 0) {
396         /* Nothing we can do. */
397         pself->errmsg = newvstrallocf(pself->errmsg,
398                 "Error reading holding disk '%s': %s'",
399                  self->holding_disk_file, strerror(errno));
400         return read_result;
401     } else if (read_result == 0) {
402         if (!get_next_chunk(self)) {
403             return -1; 
404         }
405
406         if (selfp->current_chunk_fd >= 0) {
407             /* Try again with the next chunk. */
408             return taper_file_source_read(pself, buf, count);
409         } else {
410             pself->end_of_data = TRUE;
411             return 0;
412         }
413     } else {
414         /* Success. */
415         selfp->current_part_pos += read_result;
416         selfp->current_chunk_position += read_result;
417         return read_result;
418     }
419 }
420
421 static gboolean taper_file_source_seek_to_part_start (TaperSource * pself) {
422     TaperFileSource * self = (TaperFileSource*)pself;
423     off_t lseek_result;
424
425     g_return_val_if_fail (self != NULL, FALSE);
426     g_return_val_if_fail (TAPER_IS_FILE_SOURCE (self), FALSE);
427
428     aclose(selfp->current_chunk_fd);
429     if (!copy_chunk_data(&(selfp->part_start_chunk_fd),
430                          &(selfp->current_chunk_fd),
431                          &(selfp->part_start_chunk_header),
432                          &(selfp->current_chunk_header),
433                          &(pself->errmsg))) {
434         return FALSE;
435     }
436
437     selfp->current_chunk_position = selfp->part_start_chunk_offset;
438
439     lseek_result = lseek(selfp->current_chunk_fd,
440                          DISK_BLOCK_BYTES + selfp->current_chunk_position,
441                          SEEK_SET);
442     if (lseek_result < 0) {
443         pself->errmsg = newvstrallocf(pself->errmsg,
444                 "Could not seek holding disk file: %s\n",
445                 strerror(errno));
446         return FALSE;
447     }
448
449     selfp->current_part_pos = 0;
450
451     if (parent_class->seek_to_part_start)
452         return parent_class->seek_to_part_start(pself);
453     else
454         return TRUE;
455 }
456
457 static void taper_file_source_start_new_part (TaperSource * pself) {
458     TaperFileSource * self = (TaperFileSource*)pself;
459     g_return_if_fail (self != NULL);
460     g_return_if_fail (TAPER_IS_FILE_SOURCE (self));
461
462     aclose(selfp->part_start_chunk_fd);
463     if (!copy_chunk_data(&(selfp->current_chunk_fd),
464                          &(selfp->part_start_chunk_fd),
465                          &(selfp->current_chunk_header),
466                          &(selfp->part_start_chunk_header),
467                          &(pself->errmsg))) {
468         /* We can't return FALSE. :-( Instead, we set things up so
469            they will fail on the next read(). */
470         aclose(selfp->current_chunk_fd);
471         aclose(selfp->part_start_chunk_fd);
472         return;
473     }
474
475     selfp->part_start_chunk_offset = selfp->current_chunk_position;
476     selfp->current_part_pos = 0;
477
478     if (parent_class->start_new_part)
479         parent_class->start_new_part(pself);
480 }
481