]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bextract.c
Add address and socket for MySQL
[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 int ofd = -1;
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 long record_file_index;
52 static long total = 0;
53 static POOLMEM *fname;                    /* original file name */
54 static POOLMEM *ofile;                    /* output name with prefix */
55 static POOLMEM *lname;                    /* link name */
56 static POOLMEM *attribsEx;                /* extended attributes (Win32) */
57 static char *where;
58 static int wherelen;                      /* prefix length */
59 static uint32_t num_files = 0;
60 static struct stat statp;
61 static uint32_t compress_buf_size = 70000;
62 static POOLMEM *compress_buf;
63 static int type;
64 static int stream;
65 static int prog_name_msg = 0;
66 static char *VolumeName = NULL;
67
68 static char *wbuf;                    /* write buffer address */
69 static uint32_t wsize;                /* write size */
70 static uint64_t fileAddr = 0;         /* file write address */
71
72 #define CONFIG_FILE "bacula-sd.conf"
73 char *configfile;
74
75
76 static void usage()
77 {
78    fprintf(stderr,
79 "\nVersion: " VERSION " (" BDATE ")\n\n"
80 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
81 "       -b <file>       specify a bootstrap file\n"
82 "       -c <file>       specify a configuration file\n"
83 "       -dnn            set debug level to nn\n"
84 "       -e <file>       exclude list\n"
85 "       -i <file>       include list\n"
86 "       -V              specify Volume names (separated by |)\n"
87 "       -?              print this message\n\n");
88    exit(1);
89 }
90
91
92 int main (int argc, char *argv[])
93 {
94    int ch;   
95    FILE *fd;
96    char line[1000];
97    int got_inc = FALSE;
98
99    working_directory = "/tmp";
100    my_name_is(argc, argv, "bextract");
101    init_msg(NULL, NULL);              /* setup message handler */
102
103    memset(ff, 0, sizeof(FF_PKT));
104    init_include_exclude_files(ff);
105
106    while ((ch = getopt(argc, argv, "b:c:d:e:i:?")) != -1) {
107       switch (ch) {
108          case 'b':                    /* bootstrap file */
109             bsr = parse_bsr(NULL, optarg);
110 //          dump_bsr(bsr);
111             break;
112
113          case 'c':                    /* specify config file */
114             if (configfile != NULL) {
115                free(configfile);
116             }
117             configfile = bstrdup(optarg);
118             break;
119
120          case 'd':                    /* debug level */
121             debug_level = atoi(optarg);
122             if (debug_level <= 0)
123                debug_level = 1; 
124             break;
125
126          case 'e':                    /* exclude list */
127             if ((fd = fopen(optarg, "r")) == NULL) {
128                Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
129                   optarg, strerror(errno));
130                exit(1);
131             }
132             while (fgets(line, sizeof(line), fd) != NULL) {
133                strip_trailing_junk(line);
134                Dmsg1(900, "add_exclude %s\n", line);
135                add_fname_to_exclude_list(ff, line);
136             }
137             fclose(fd);
138             break;
139
140          case 'i':                    /* include list */
141             if ((fd = fopen(optarg, "r")) == NULL) {
142                Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
143                   optarg, strerror(errno));
144                exit(1);
145             }
146             while (fgets(line, sizeof(line), fd) != NULL) {
147                strip_trailing_junk(line);
148                Dmsg1(900, "add_include %s\n", line);
149                add_fname_to_include_list(ff, 0, line);
150             }
151             fclose(fd);
152             got_inc = TRUE;
153             break;
154
155          case 'V':                    /* Volume name */
156             VolumeName = optarg;
157             break;
158
159          case '?':
160          default:
161             usage();
162
163       }  
164    }
165    argc -= optind;
166    argv += optind;
167
168    if (argc != 2) {
169       Pmsg0(0, "Wrong number of arguments: \n");
170       usage();
171    }
172
173    if (configfile == NULL) {
174       configfile = bstrdup(CONFIG_FILE);
175    }
176
177    parse_config(configfile);
178
179    if (!got_inc) {                            /* If no include file, */
180       add_fname_to_include_list(ff, 0, "/");  /*   include everything */
181    }
182
183    where = argv[1];
184    do_extract(argv[0]);
185
186    if (bsr) {
187       free_bsr(bsr);
188    }
189    return 0;
190 }
191
192 static void do_extract(char *devname)
193 {
194
195    jcr = setup_jcr("bextract", devname, bsr, VolumeName);
196    dev = setup_to_access_device(jcr, 1);    /* acquire for read */
197    if (!dev) {
198       exit(1);
199    }
200
201    /* Make sure where directory exists and that it is a directory */
202    if (stat(where, &statp) < 0) {
203       Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
204          where, strerror(errno));
205    }
206    if (!S_ISDIR(statp.st_mode)) {
207       Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
208    }
209
210    wherelen = strlen(where);
211    fname = get_pool_memory(PM_FNAME);
212    ofile = get_pool_memory(PM_FNAME);
213    lname = get_pool_memory(PM_FNAME);
214    attribsEx = get_pool_memory(PM_FNAME);
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 (ofd >= 0) {
223       set_attributes(jcr, fname, ofile, lname, type, stream, &statp,
224                      attribsEx, &ofd);
225    }
226    release_device(jcr, dev);
227
228    free_pool_memory(fname);
229    free_pool_memory(ofile);
230    free_pool_memory(lname);
231    free_pool_memory(compress_buf);
232    term_dev(dev);
233    free_jcr(jcr);
234    printf("%u files restored.\n", num_files);
235    return;
236 }
237
238 /*
239  * Called here for each record from read_records()
240  */
241 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
242 {
243    int stat;
244
245    if (rec->FileIndex < 0) {
246       return;                         /* we don't want labels */
247    }
248
249    /* File Attributes stream */
250    if (rec->Stream == STREAM_UNIX_ATTRIBUTES || rec->Stream == STREAM_WIN32_ATTRIBUTES) {
251       char *ap, *lp, *fp, *apex;
252
253       stream = rec->Stream;
254
255       /* If extracting, it was from previous stream, so
256        * close the output file.
257        */
258       if (extract) {
259          if (ofd < 0) {
260             Emsg0(M_ERROR, 0, "Logic error output file should be open\n");
261          }
262          extract = FALSE;
263          set_attributes(jcr, fname, ofile, lname, type, stream, &statp,
264                         attribsEx, &ofd);
265       }
266
267       if (sizeof_pool_memory(fname) < rec->data_len) {
268          fname = realloc_pool_memory(fname, rec->data_len + 1);
269       }
270       if (sizeof_pool_memory(ofile) < rec->data_len + wherelen + 1) {
271          ofile = realloc_pool_memory(ofile, rec->data_len + wherelen + 1);
272       }
273       if (sizeof_pool_memory(lname) < rec->data_len) {
274          lname = realloc_pool_memory(lname, rec->data_len + wherelen + 1);
275       }
276       *fname = 0;
277       *lname = 0;
278
279       /*              
280        * An Attributes record consists of:
281        *    File_index
282        *    Type   (FT_types)
283        *    Filename
284        *    Attributes
285        *    Link name (if file linked i.e. FT_LNK)
286        *    Extended Attributes (Win32) 
287        *
288        */
289       sscanf(rec->data, "%ld %d", &record_file_index, &type);
290       if (record_file_index != rec->FileIndex)
291          Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
292             rec->FileIndex, record_file_index);
293       ap = rec->data;
294       while (*ap++ != ' ')         /* skip record file index */
295          ;
296       while (*ap++ != ' ')         /* skip type */
297          ;
298       /* Save filename and position to attributes */
299       fp = fname;
300       while (*ap != 0) {
301          *fp++  = *ap++;
302       }
303       *fp = *ap++;                 /* terminate filename & point to attribs */
304
305       /* Skip to Link name */
306       if (type == FT_LNK || type == FT_LNKSAVED) {
307          lp = ap;
308          while (*lp++ != 0) {
309             ;
310          }
311       } else {
312          lp = "";
313       }
314
315       if (rec->Stream == STREAM_WIN32_ATTRIBUTES) {
316          apex = ap;                   /* start at attributes */
317          while (*apex++ != 0) {       /* skip attributes */
318             ;
319          }
320          while (*apex++ != 0) {      /* skip link name */
321             ;
322          }
323          pm_strcpy(&attribsEx, apex);  /* make a copy of Extended attributes */
324       } else {
325          *attribsEx = 0;              /* no extended attributes */
326       }
327
328          
329       if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
330          uint32_t LinkFI;
331
332          decode_stat(ap, &statp, &LinkFI);
333          /*
334           * Prepend the where directory so that the
335           * files are put where the user wants.
336           *
337           * We do a little jig here to handle Win32 files with
338           *   a drive letter -- we simply strip the drive: from
339           *   every filename if a prefix is supplied.
340           */
341          if (where[0] == 0) {
342             strcpy(ofile, fname);
343             strcpy(lname, lp);
344          } else {
345             char *fn;
346             strcpy(ofile, where);
347             if (win32_client && fname[1] == ':') {
348                fn = fname+2;          /* skip over drive: */
349             } else {
350                fn = fname;            /* take whole name */
351             }
352             /* Ensure where is terminated with a slash */
353             if (where[wherelen-1] != '/' && fn[0] != '/') {
354                strcat(ofile, "/");
355             }
356             strcat(ofile, fn);        /* copy rest of name */
357             /* Fixup link name */
358             if (type == FT_LNK || type == FT_LNKSAVED) {
359                if (lp[0] == '/') {      /* if absolute path */
360                   strcpy(lname, where);
361                }       
362                if (win32_client && lp[1] == ':') {
363                   strcat(lname, lp+2); /* copy rest of name */
364                } else {
365                   strcat(lname, lp);   /* On Unix systems we take everything */
366                }
367             }
368          }
369
370          /*          Pmsg1(000, "Restoring: %s\n", ofile); */
371
372          extract = FALSE;
373          stat = create_file(jcr, fname, ofile, lname, type, stream,
374                             &statp, attribsEx, &ofd, REPLACE_ALWAYS);
375          switch (stat) {
376          case CF_ERROR:
377          case CF_SKIP:
378             break;
379          case CF_EXTRACT:
380             extract = TRUE;
381             /* Fall-through wanted */
382          case CF_CREATED:
383             print_ls_output(ofile, lname, type, &statp);   
384             num_files++;
385             fileAddr = 0;
386             break;
387          }  
388       }
389
390    /* Data stream and extracting */
391    } else if (rec->Stream == STREAM_FILE_DATA || rec->Stream == STREAM_SPARSE_DATA) {
392       if (extract) {
393          if (rec->Stream == STREAM_SPARSE_DATA) {
394             ser_declare;
395             uint64_t faddr;
396             wbuf = rec->data + SPARSE_FADDR_SIZE;
397             wsize = rec->data_len - SPARSE_FADDR_SIZE;
398             ser_begin(rec->data, SPARSE_FADDR_SIZE);
399             unser_uint64(faddr);
400             if (fileAddr != faddr) {
401                fileAddr = faddr;
402                if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
403                   Emsg2(M_ERROR_TERM, 0, _("Seek error on %s: %s\n"), ofile, strerror(errno));
404                }
405             }
406          } else {
407             wbuf = rec->data;
408             wsize = rec->data_len;
409          }
410          total += wsize;
411          Dmsg2(8, "Write %u bytes, total=%u\n", wsize, total);
412          if ((uint32_t)write(ofd, wbuf, wsize) != wsize) {
413             Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
414          }
415          fileAddr += wsize;
416       }
417
418    } else if (rec->Stream == STREAM_GZIP_DATA || rec->Stream == STREAM_SPARSE_GZIP_DATA) {
419 #ifdef HAVE_LIBZ
420       if (extract) {
421          uLongf compress_len;
422          int stat;
423
424          if (rec->Stream == STREAM_SPARSE_GZIP_DATA) {
425             ser_declare;
426             uint64_t faddr;
427             wbuf = rec->data + SPARSE_FADDR_SIZE;
428             wsize = rec->data_len - SPARSE_FADDR_SIZE;
429             ser_begin(rec->data, SPARSE_FADDR_SIZE);
430             unser_uint64(faddr);
431             if (fileAddr != faddr) {
432                fileAddr = faddr;
433                if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
434                   Emsg2(M_ERROR, 0, _("Seek error on %s: %s\n"), ofile, strerror(errno));
435                }
436             }
437          } else {
438             wbuf = rec->data;
439             wsize = rec->data_len;
440          }
441          compress_len = compress_buf_size;
442          if ((stat=uncompress((Bytef *)compress_buf, &compress_len, 
443                (const Bytef *)wbuf, (uLong)wsize) != Z_OK)) {
444             Emsg1(M_ERROR_TERM, 0, _("Uncompression error. ERR=%d\n"), stat);
445          }
446
447          Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
448          if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
449             Pmsg0(0, "===Write error===\n");
450             Emsg2(M_ERROR_TERM, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
451          }
452          total += compress_len;
453          fileAddr += compress_len;
454          Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
455             compress_len);
456       }
457 #else
458       if (extract) {
459          Emsg0(M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
460       }
461 #endif
462
463
464    /* If extracting, wierd stream (not 1 or 2), close output file anyway */
465    } else if (extract) {
466       if (ofd < 0) {
467          Emsg0(M_ERROR, 0, "Logic error output file should be open\n");
468       }
469       extract = FALSE;
470       set_attributes(jcr, fname, ofile, lname, type, stream, &statp,
471                      attribsEx, &ofd);
472    } else if (rec->Stream == STREAM_PROGRAM_NAMES || rec->Stream == STREAM_PROGRAM_DATA) {
473       if (!prog_name_msg) {
474          Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
475          prog_name_msg = 1;
476       }
477    } else if (!(rec->Stream == STREAM_MD5_SIGNATURE ||
478                 rec->Stream == STREAM_SHA1_SIGNATURE)) {
479       Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec->Stream, rec->data);
480    }
481 }
482
483
484
485
486 /* Dummies to replace askdir.c */
487 int     dir_get_volume_info(JCR *jcr, int writing) { return 1;}
488 int     dir_find_next_appendable_volume(JCR *jcr) { return 1;}
489 int     dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
490 int     dir_create_jobmedia_record(JCR *jcr) { return 1; }
491 int     dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
492 int     dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
493 int     dir_send_job_status(JCR *jcr) {return 1;}
494
495
496 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
497 {
498    fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
499       jcr->VolumeName, dev_name(dev));
500    getchar();   
501    return 1;
502 }