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