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