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