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