]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bextract.c
More DCR implementation + debug code for zeroed block headers
[bacula/bacula] / bacula / src / stored / bextract.c
1 /*
2  *
3  *  Dumb program to extract files from a Bacula backup.
4  *
5  *   Kern E. Sibbald, 2000
6  *
7  *   Version $Id$
8  *
9  */
10 /*
11    Copyright (C) 2000-2004 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "stored.h"
32 #include "findlib/find.h"
33
34 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
35 int win32_client = 1;
36 #else
37 int win32_client = 0;
38 #endif
39
40 static void do_extract(char *fname);
41 static bool record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
42
43 static DEVICE *dev = NULL;
44 static DCR *dcr;
45 static BFILE bfd;
46 static JCR *jcr;
47 static FF_PKT my_ff;
48 static FF_PKT *ff = &my_ff;
49 static BSR *bsr = NULL;
50 static bool extract = false;
51 static int non_support_data = 0;
52 static long total = 0;
53 static ATTR *attr;
54 static char *where;
55 static uint32_t num_files = 0;
56 static uint32_t compress_buf_size = 70000;
57 static POOLMEM *compress_buf;
58 static int prog_name_msg = 0;
59 static int win32_data_msg = 0;
60 static char *VolumeName = NULL;
61
62 static char *wbuf;                    /* write buffer address */
63 static uint32_t wsize;                /* write size */
64 static uint64_t fileAddr = 0;         /* file write address */
65
66 #define CONFIG_FILE "bacula-sd.conf"
67 char *configfile;
68 bool forge_on = false;
69
70 static void usage()
71 {
72    fprintf(stderr,
73 "\nVersion: " VERSION " (" BDATE ")\n\n"
74 "Usage: bextract <options> <bacula-archive-device-name> <directory-to-store-files>\n"
75 "       -b <file>       specify a bootstrap file\n"
76 "       -c <file>       specify a configuration file\n"
77 "       -d <nn>         set debug level to nn\n"
78 "       -e <file>       exclude list\n"
79 "       -i <file>       include list\n"
80 "       -p              proceed inspite of I/O errors\n"
81 "       -V <volumes>    specify Volume names (separated by |)\n"
82 "       -?              print this message\n\n");
83    exit(1);
84 }
85
86
87 int main (int argc, char *argv[])
88 {
89    int ch;   
90    FILE *fd;
91    char line[1000];
92    int got_inc = FALSE;
93
94    working_directory = "/tmp";
95    my_name_is(argc, argv, "bextract");
96    init_msg(NULL, NULL);              /* setup message handler */
97
98    memset(ff, 0, sizeof(FF_PKT));
99    init_include_exclude_files(ff);
100    binit(&bfd);
101
102    while ((ch = getopt(argc, argv, "b:c:d:e:i:pV:?")) != -1) {
103       switch (ch) {
104       case 'b':                    /* bootstrap file */
105          bsr = parse_bsr(NULL, optarg);
106 //       dump_bsr(bsr, true);
107          break;
108
109       case 'c':                    /* specify config file */
110          if (configfile != NULL) {
111             free(configfile);
112          }
113          configfile = bstrdup(optarg);
114          break;
115
116       case 'd':                    /* debug level */
117          debug_level = atoi(optarg);
118          if (debug_level <= 0)
119             debug_level = 1; 
120          break;
121
122       case 'e':                    /* exclude list */
123          if ((fd = fopen(optarg, "r")) == NULL) {
124             Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
125                optarg, strerror(errno));
126             exit(1);
127          }
128          while (fgets(line, sizeof(line), fd) != NULL) {
129             strip_trailing_junk(line);
130             Dmsg1(900, "add_exclude %s\n", line);
131             add_fname_to_exclude_list(ff, line);
132          }
133          fclose(fd);
134          break;
135
136       case 'i':                    /* include list */
137          if ((fd = fopen(optarg, "r")) == NULL) {
138             Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
139                optarg, strerror(errno));
140             exit(1);
141          }
142          while (fgets(line, sizeof(line), fd) != NULL) {
143             strip_trailing_junk(line);
144             Dmsg1(900, "add_include %s\n", line);
145             add_fname_to_include_list(ff, 0, line);
146          }
147          fclose(fd);
148          got_inc = TRUE;
149          break;
150
151       case 'p':
152          forge_on = true;
153          break;
154
155       case 'V':                    /* Volume name */
156          VolumeName = optarg;
157          break;
158
159       case '?':
160       default:
161          usage();
162
163       } /* end switch */
164    } /* end while */
165    argc -= optind;
166    argv += optind;
167
168    if (argc != 2) {
169       Pmsg0(0, "Wrong number of arguments: \n");
170       usage();
171    }
172
173    if (configfile == NULL) {
174       configfile = bstrdup(CONFIG_FILE);
175    }
176
177    parse_config(configfile);
178
179    if (!got_inc) {                            /* If no include file, */
180       add_fname_to_include_list(ff, 0, "/");  /*   include everything */
181    }
182
183    where = argv[1];
184    do_extract(argv[0]);
185
186    if (bsr) {
187       free_bsr(bsr);
188    }
189    if (prog_name_msg) {
190       Pmsg1(000, "%d Program Name and/or Program Data Stream records ignored.\n",
191          prog_name_msg);
192    }
193    if (win32_data_msg) {
194       Pmsg1(000, "%d Win32 data or Win32 gzip data stream records. Ignored.\n",
195          win32_data_msg);
196    }
197    return 0;
198 }
199
200 static void do_extract(char *devname)
201 {
202    struct stat statp;
203    jcr = setup_jcr("bextract", devname, bsr, VolumeName);
204    dev = setup_to_access_device(jcr, 1);    /* acquire for read */
205    if (!dev) {
206       exit(1);
207    }
208    dcr = jcr->dcr;
209
210    /* Make sure where directory exists and that it is a directory */
211    if (stat(where, &statp) < 0) {
212       Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
213          where, strerror(errno));
214    }
215    if (!S_ISDIR(statp.st_mode)) {
216       Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
217    }
218
219    free(jcr->where);
220    jcr->where = bstrdup(where);
221    attr = new_attr();
222
223    compress_buf = get_memory(compress_buf_size);
224
225    read_records(dcr, record_cb, mount_next_read_volume);
226    /* If output file is still open, it was the last one in the
227     * archive since we just hit an end of file, so close the file. 
228     */
229    if (is_bopen(&bfd)) {
230       set_attributes(jcr, attr, &bfd);
231    }
232    release_device(jcr);
233
234    free_attr(attr);
235    term_dev(dev);
236    free_jcr(jcr);
237    printf("%u files restored.\n", num_files);
238    return;
239 }
240
241 /*
242  * Called here for each record from read_records()
243  */
244 static bool record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
245 {
246    int stat;
247
248    if (rec->FileIndex < 0) {
249       return true;                    /* we don't want labels */
250    }
251
252    /* File Attributes stream */
253
254    switch (rec->Stream) {
255    case STREAM_UNIX_ATTRIBUTES:
256    case STREAM_UNIX_ATTRIBUTES_EX:  
257
258       /* If extracting, it was from previous stream, so
259        * close the output file.
260        */
261       if (extract) {
262          if (!is_bopen(&bfd)) {
263             Emsg0(M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
264          }
265          set_attributes(jcr, attr, &bfd);
266          extract = false;
267       }
268
269       if (!unpack_attributes_record(jcr, rec->Stream, rec->data, attr)) {
270          Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
271       }
272
273       if (attr->file_index != rec->FileIndex) {
274          Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
275             rec->FileIndex, attr->file_index);
276       }
277          
278       if (file_is_included(ff, attr->fname) && !file_is_excluded(ff, attr->fname)) {
279
280          attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
281          if (!is_stream_supported(attr->data_stream)) {
282             if (!non_support_data++) {
283                Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
284                   stream_to_ascii(attr->data_stream));
285             }
286             extract = false;
287             return true;
288          }
289
290
291          build_attr_output_fnames(jcr, attr);
292
293          extract = false;
294          stat = create_file(jcr, attr, &bfd, REPLACE_ALWAYS);   
295          switch (stat) {
296          case CF_ERROR:
297          case CF_SKIP:
298             break;
299          case CF_EXTRACT:
300             extract = true;
301             print_ls_output(jcr, attr);
302             num_files++;
303             fileAddr = 0;
304             break;
305          case CF_CREATED:
306             set_attributes(jcr, attr, &bfd);
307             print_ls_output(jcr, attr);
308             num_files++;
309             fileAddr = 0;
310             break;
311          }  
312       }
313       break;
314
315    /* Data stream and extracting */
316    case STREAM_FILE_DATA:
317    case STREAM_SPARSE_DATA:
318    case STREAM_WIN32_DATA:  
319
320       if (extract) {
321          if (rec->Stream == STREAM_SPARSE_DATA) {
322             ser_declare;
323             uint64_t faddr;
324             wbuf = rec->data + SPARSE_FADDR_SIZE;
325             wsize = rec->data_len - SPARSE_FADDR_SIZE;
326             ser_begin(rec->data, SPARSE_FADDR_SIZE);
327             unser_uint64(faddr);
328             if (fileAddr != faddr) {
329                fileAddr = faddr;
330                if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
331                   Emsg2(M_ERROR_TERM, 0, _("Seek error on %s: %s\n"), 
332                      attr->ofname, strerror(errno));
333                }
334             }
335          } else {
336             wbuf = rec->data;
337             wsize = rec->data_len;
338          }
339          total += wsize;
340          Dmsg2(8, "Write %u bytes, total=%u\n", wsize, total);
341          if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
342             Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"), 
343                attr->ofname, strerror(errno));
344          }
345          fileAddr += wsize;
346       }
347       break;
348
349    /* GZIP data stream */
350    case STREAM_GZIP_DATA:
351    case STREAM_SPARSE_GZIP_DATA: 
352    case STREAM_WIN32_GZIP_DATA:  
353 #ifdef HAVE_LIBZ
354       if (extract) {
355          uLong compress_len;
356          int stat;
357
358          if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
359             ser_declare;
360             uint64_t faddr;
361             char ec1[50];
362             wbuf = rec->data + SPARSE_FADDR_SIZE;
363             wsize = rec->data_len - SPARSE_FADDR_SIZE;
364             ser_begin(rec->data, SPARSE_FADDR_SIZE);
365             unser_uint64(faddr);
366             if (fileAddr != faddr) {
367                fileAddr = faddr;
368                if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
369                   Emsg3(M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"), 
370                      edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
371                   extract = false;
372                   return true;
373                }
374             }
375          } else {
376             wbuf = rec->data;
377             wsize = rec->data_len;
378          }
379          compress_len = compress_buf_size;
380          if ((stat=uncompress((Bytef *)compress_buf, &compress_len, 
381                (const Bytef *)wbuf, (uLong)wsize) != Z_OK)) {
382             Emsg1(M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
383             extract = false;
384             return true;
385          }
386
387          Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
388          if ((uLongf)bwrite(&bfd, compress_buf, (size_t)compress_len) != compress_len) {
389             Pmsg0(0, "===Write error===\n");
390             Emsg2(M_ERROR, 0, _("Write error on %s: %s\n"), 
391                attr->ofname, strerror(errno));
392             extract = false;
393             return true;
394          }
395          total += compress_len;
396          fileAddr += compress_len;
397          Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
398             compress_len);
399       }
400 #else
401       if (extract) {
402          Emsg0(M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
403          extract = false;
404          return true;
405       }
406 #endif
407       break;
408
409    case STREAM_MD5_SIGNATURE:
410    case STREAM_SHA1_SIGNATURE:
411       break;
412
413    case STREAM_PROGRAM_NAMES:
414    case STREAM_PROGRAM_DATA:
415       if (!prog_name_msg) {
416          Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
417          prog_name_msg++;
418       }
419       break;
420
421    default:
422       /* If extracting, wierd stream (not 1 or 2), close output file anyway */
423       if (extract) {
424          if (!is_bopen(&bfd)) {
425             Emsg0(M_ERROR, 0, "Logic error output file should be open but is not.\n");
426          }
427          set_attributes(jcr, attr, &bfd);
428          extract = false;
429       }
430       Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), 
431          rec->Stream);
432       break;
433       
434    } /* end switch */
435    return true;
436 }
437
438 /* Dummies to replace askdir.c */
439 bool    dir_get_volume_info(JCR *jcr, enum get_vol_info_rw  writing) { return 1;}
440 bool    dir_find_next_appendable_volume(JCR *jcr) { return 1;}
441 bool    dir_update_volume_info(JCR *jcr, bool relabel) { return 1; }
442 bool    dir_create_jobmedia_record(JCR *jcr) { return 1; }
443 bool    dir_ask_sysop_to_create_appendable_volume(JCR *jcr) { return 1; }
444 bool    dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
445 bool    dir_send_job_status(JCR *jcr) {return 1;}
446
447
448 bool dir_ask_sysop_to_mount_volume(JCR *jcr)
449 {
450    DEVICE *dev = jcr->dcr->dev;
451    fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
452       jcr->VolumeName, dev_name(dev));
453    getchar();   
454    return true;
455 }