]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bextract.c
3d7ffa0f481d4e5f66a368d4a2230206723dc315
[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 DEV_BLOCK *block;
51 static int extract = FALSE;
52 static long record_file_index;
53 static long total = 0;
54 static POOLMEM *fname;                    /* original file name */
55 static POOLMEM *ofile;                    /* output name with prefix */
56 static POOLMEM *lname;                    /* link name */
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
65 static void usage()
66 {
67    fprintf(stderr,
68 "\nVersion: " VERSION " (" DATE ")\n\n"
69 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
70 "       -b <file>       specify a bootstrap file\n"
71 "       -dnn            set debug level to nn\n"
72 "       -e <file>       exclude list\n"
73 "       -i <file>       include list\n"
74 "       -?              print this message\n\n");
75    exit(1);
76 }
77
78
79 int main (int argc, char *argv[])
80 {
81    int ch;   
82    FILE *fd;
83    char line[1000];
84    int got_inc = FALSE;
85
86    my_name_is(argc, argv, "bextract");
87    init_msg(NULL, NULL);              /* setup message handler */
88
89    memset(ff, 0, sizeof(FF_PKT));
90    init_include_exclude_files(ff);
91
92    while ((ch = getopt(argc, argv, "b:d:e:i:?")) != -1) {
93       switch (ch) {
94          case 'b':                    /* bootstrap file */
95             bsr = parse_bsr(NULL, optarg);
96 //          dump_bsr(bsr);
97             break;
98
99          case 'd':                    /* debug level */
100             debug_level = atoi(optarg);
101             if (debug_level <= 0)
102                debug_level = 1; 
103             break;
104
105          case 'e':                    /* exclude list */
106             if ((fd = fopen(optarg, "r")) == NULL) {
107                Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
108                   optarg, strerror(errno));
109                exit(1);
110             }
111             while (fgets(line, sizeof(line), fd) != NULL) {
112                strip_trailing_junk(line);
113                Dmsg1(900, "add_exclude %s\n", line);
114                add_fname_to_exclude_list(ff, line);
115             }
116             fclose(fd);
117             break;
118
119          case 'i':                    /* include list */
120             if ((fd = fopen(optarg, "r")) == NULL) {
121                Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
122                   optarg, strerror(errno));
123                exit(1);
124             }
125             while (fgets(line, sizeof(line), fd) != NULL) {
126                strip_trailing_junk(line);
127                Dmsg1(900, "add_include %s\n", line);
128                add_fname_to_include_list(ff, 0, line);
129             }
130             fclose(fd);
131             got_inc = TRUE;
132             break;
133
134          case '?':
135          default:
136             usage();
137
138       }  
139    }
140    argc -= optind;
141    argv += optind;
142
143    if (argc != 2) {
144       Pmsg0(0, "Wrong number of arguments: \n");
145       usage();
146    }
147    if (!got_inc) {                            /* If no include file, */
148       add_fname_to_include_list(ff, 0, "/");  /*   include everything */
149    }
150
151    where = argv[1];
152    do_extract(argv[0]);
153
154    if (bsr) {
155       free_bsr(bsr);
156    }
157    return 0;
158 }
159
160 static void do_extract(char *devname)
161 {
162
163    jcr = setup_jcr("bextract", devname, bsr);
164    dev = setup_to_read_device(jcr);
165    if (!dev) {
166       exit(1);
167    }
168
169    /* Make sure where directory exists and that it is a directory */
170    if (stat(where, &statp) < 0) {
171       Emsg2(M_ERROR_TERM, 0, "Cannot stat %s. It must exist. ERR=%s\n",
172          where, strerror(errno));
173    }
174    if (!S_ISDIR(statp.st_mode)) {
175       Emsg1(M_ERROR_TERM, 0, "%s must be a directory.\n", where);
176    }
177
178    wherelen = strlen(where);
179    fname = get_pool_memory(PM_FNAME);
180    ofile = get_pool_memory(PM_FNAME);
181    lname = get_pool_memory(PM_FNAME);
182
183
184
185
186    compress_buf = get_memory(compress_buf_size);
187
188    read_records(jcr, dev, record_cb, mount_next_read_volume);
189    /* If output file is still open, it was the last one in the
190     * archive since we just hit an end of file, so close the file. 
191     */
192    if (ofd >= 0) {
193       close(ofd);
194       set_statp(jcr, fname, ofile, lname, type, &statp);
195    }
196    release_device(jcr, dev);
197
198    free_pool_memory(fname);
199    free_pool_memory(ofile);
200    free_pool_memory(lname);
201    free_pool_memory(compress_buf);
202    term_dev(dev);
203    free_jcr(jcr);
204    printf("%u files restored.\n", num_files);
205    return;
206 }
207
208 /*
209  * Called here for each record from read_records()
210  */
211 static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
212 {
213    if (rec->FileIndex < 0) {
214       return;                         /* we don't want labels */
215    }
216
217    /* File Attributes stream */
218    if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
219       char *ap, *lp, *fp;
220
221       /* If extracting, it was from previous stream, so
222        * close the output file.
223        */
224       if (extract) {
225          if (ofd < 0) {
226             Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
227          }
228          close(ofd);
229          ofd = -1;
230          extract = FALSE;
231          set_statp(jcr, fname, ofile, lname, type, &statp);
232       }
233
234       if (sizeof_pool_memory(fname) < rec->data_len) {
235          fname = realloc_pool_memory(fname, rec->data_len + 1);
236       }
237       if (sizeof_pool_memory(ofile) < rec->data_len + wherelen + 1) {
238          ofile = realloc_pool_memory(ofile, rec->data_len + wherelen + 1);
239       }
240       if (sizeof_pool_memory(lname) < rec->data_len) {
241          lname = realloc_pool_memory(lname, rec->data_len + wherelen + 1);
242       }
243       *fname = 0;
244       *lname = 0;
245
246       /*              
247        * An Attributes record consists of:
248        *    File_index
249        *    Type   (FT_types)
250        *    Filename
251        *    Attributes
252        *    Link name (if file linked i.e. FT_LNK)
253        *
254        */
255       sscanf(rec->data, "%ld %d", &record_file_index, &type);
256       if (record_file_index != rec->FileIndex)
257          Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
258             rec->FileIndex, record_file_index);
259       ap = rec->data;
260       while (*ap++ != ' ')         /* skip record file index */
261          ;
262       while (*ap++ != ' ')         /* skip type */
263          ;
264       /* Save filename and position to attributes */
265       fp = fname;
266       while (*ap != 0) {
267          *fp++  = *ap++;
268       }
269       *fp = *ap++;                 /* terminate filename & point to attribs */
270
271       /* Skip to Link name */
272       if (type == FT_LNK || type == FT_LNKSAVED) {
273          lp = ap;
274          while (*lp++ != 0) {
275             ;
276          }
277       } else {
278          lp = "";
279       }
280
281          
282       if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
283
284          decode_stat(ap, &statp);
285          /*
286           * Prepend the where directory so that the
287           * files are put where the user wants.
288           *
289           * We do a little jig here to handle Win32 files with
290           * a drive letter.  
291           *   If where is null and we are running on a win32 client,
292           *      change nothing.
293           *   Otherwise, if the second character of the filename is a
294           *   colon (:), change it into a slash (/) -- this creates
295           *   a reasonable pathname on most systems.
296           */
297          if (where[0] == 0 && win32_client) {
298             strcpy(ofile, fname);
299             strcpy(lname, lp);
300          } else {
301             strcpy(ofile, where);
302             if (fname[1] == ':') {
303                fname[1] = '/';
304                strcat(ofile, fname);
305                fname[1] = ':';
306             } else {
307                strcat(ofile, fname);
308             }
309             /* Fixup link name */
310             if (type == FT_LNK || type == FT_LNKSAVED) {
311                if (lp[0] == '/') {      /* if absolute path */
312                   strcpy(lname, where);
313                }       
314                /* ***FIXME**** we shouldn't have links on Windoz */
315                if (lp[1] == ':') {
316                   lp[1] = '/';
317                   strcat(lname, lp);
318                   lp[1] = ':';
319                } else {
320                   strcat(lname, lp);
321                }
322             }
323          }
324
325            /*          Pmsg1(000, "Restoring: %s\n", ofile); */
326
327          extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
328          num_files++;
329
330          if (extract) {
331              print_ls_output(ofile, lname, type, &statp);   
332          }
333       }
334
335    /* Data stream and extracting */
336    } else if (rec->Stream == STREAM_FILE_DATA) {
337       if (extract) {
338          total += rec->data_len;
339          Dmsg2(8, "Write %ld bytes, total=%ld\n", rec->data_len, total);
340          if ((uint32_t)write(ofd, rec->data, rec->data_len) != rec->data_len) {
341             Emsg1(M_ERROR_TERM, 0, "Write error: %s\n", strerror(errno));
342          }
343       }
344
345    } else if (rec->Stream == STREAM_GZIP_DATA) {
346 #ifdef HAVE_LIBZ
347       if (extract) {
348          uLongf compress_len;
349
350          compress_len = compress_buf_size;
351          if (uncompress((Bytef *)compress_buf, &compress_len, 
352                (const Bytef *)rec->data, (uLong)rec->data_len) != Z_OK) {
353             Emsg0(M_ERROR_TERM, 0, _("Uncompression error.\n"));
354          }
355
356          Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
357          if ((uLongf)write(ofd, compress_buf, (size_t)compress_len) != compress_len) {
358             Pmsg0(0, "===Write error===\n");
359             Emsg2(M_ERROR_TERM, 0, "Write error on %s: %s\n", ofile, strerror(errno));
360          }
361          total += compress_len;
362          Dmsg2(100, "Compress len=%d uncompressed=%d\n", rec->data_len,
363             compress_len);
364       }
365 #else
366       if (extract) {
367          Emsg0(M_ERROR_TERM, 0, "GZIP data stream found, but GZIP not configured!\n");
368       }
369 #endif
370
371
372    /* If extracting, wierd stream (not 1 or 2), close output file anyway */
373    } else if (extract) {
374       if (ofd < 0) {
375          Emsg0(M_ERROR_TERM, 0, "Logic error output file should be open\n");
376       }
377       close(ofd);
378       ofd = -1;
379       extract = FALSE;
380       set_statp(jcr, fname, ofile, lname, type, &statp);
381    } else if (rec->Stream != STREAM_MD5_SIGNATURE) {
382       Pmsg2(0, "None of above!!! stream=%d data=%s\n", rec->Stream, rec->data);
383    }
384 }
385
386
387
388
389 /* Dummies to replace askdir.c */
390 int     dir_get_volume_info(JCR *jcr) { return 1;}
391 int     dir_find_next_appendable_volume(JCR *jcr) { return 1;}
392 int     dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
393 int     dir_create_jobmedia_record(JCR *jcr) { return 1; }
394 int     dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
395 int     dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
396 int     dir_send_job_status(JCR *jcr) {return 1;}
397
398
399 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
400 {
401    fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
402       jcr->VolumeName, dev_name(dev));
403    getchar();   
404    return 1;
405 }