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