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