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