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