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