]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bextract.c
First cut AutoPrune
[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 static void do_extract(char *fname, char *prefix);
35 static void print_ls_output(char *fname, struct stat *statp);
36
37
38 static DEVICE *dev = NULL;
39 static int ofd = -1;
40
41 static JCR *jcr;
42 static FF_PKT my_ff;
43 static FF_PKT *ff = &my_ff;
44
45 static void usage()
46 {
47    fprintf(stderr,
48 "Usage: bextract [-d debug_level] <bacula-archive> <directory-to-store-files>\n"
49 "       -dnn            set debug level to nn\n"
50 "       -e <file>       exclude list\n"
51 "       -i <file>       include list\n"
52 "       -?              print this message\n\n");
53    exit(1);
54 }
55
56 static void my_free_jcr(JCR *jcr)
57 {
58    return;
59 }
60
61 int main (int argc, char *argv[])
62 {
63    int ch;   
64    FILE *fd;
65    char line[1000];
66
67    my_name_is(argc, argv, "bextract");
68    init_msg(NULL, NULL);              /* setup message handler */
69
70    memset(ff, 0, sizeof(FF_PKT));
71    init_include_exclude_files(ff);
72
73    while ((ch = getopt(argc, argv, "d:e:i:?")) != -1) {
74       switch (ch) {
75          case 'd':                    /* debug level */
76             debug_level = atoi(optarg);
77             if (debug_level <= 0)
78                debug_level = 1; 
79             break;
80
81          case 'e':                    /* exclude list */
82             if ((fd = fopen(optarg, "r")) == NULL) {
83                Dmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
84                   optarg, strerror(errno));
85                exit(1);
86             }
87             while (fgets(line, sizeof(line), fd) != NULL) {
88                strip_trailing_junk(line);
89                Dmsg1(900, "add_exclude %s\n", line);
90                add_fname_to_exclude_list(ff, line);
91             }
92             fclose(fd);
93             break;
94
95          case 'i':                    /* include list */
96             if ((fd = fopen(optarg, "r")) == NULL) {
97                Dmsg2(0, "Could not open include file: %s, ERR=%s\n",
98                   optarg, strerror(errno));
99                exit(1);
100             }
101             while (fgets(line, sizeof(line), fd) != NULL) {
102                strip_trailing_junk(line);
103                Dmsg1(900, "add_include %s\n", line);
104                add_fname_to_include_list(ff, 0, line);
105             }
106             fclose(fd);
107             break;
108
109          case '?':
110          default:
111             usage();
112
113       }  
114    }
115    argc -= optind;
116    argv += optind;
117
118    if (argc != 2) {
119       Dmsg0(0, "Wrong number of arguments: \n");
120       usage();
121    }
122
123    jcr = new_jcr(sizeof(JCR), my_free_jcr);
124    jcr->VolSessionId = 1;
125    jcr->VolSessionTime = (uint32_t)time(NULL);
126    jcr->NumVolumes = 1;
127
128    do_extract(argv[0], argv[1]);
129
130    free_jcr(jcr);
131    return 0;
132 }
133   
134
135 static void do_extract(char *devname, char *where)
136 {
137    int n;     
138    char VolName[100];
139    char *p;
140    struct stat statp;
141    int extract = FALSE;
142    int type;
143    long record_file_index;
144    long total = 0;
145    DEV_RECORD rec;
146    DEV_BLOCK *block;
147    char *fname;                       /* original file name */
148    char *ofile;                       /* output name with prefix */
149    char *lname;                       /* link name */
150    int wherelen;                      /* prefix length */
151
152    if (strncmp(devname, "/dev/", 5) != 0) {
153       /* Try stripping file part */
154       p = devname + strlen(devname);
155       while (p >= devname && *p != '/') {
156          p--;
157       }
158       if (*p == '/') {
159          strcpy(VolName, p+1);
160          *p = 0;
161       }
162    }
163
164    dev = init_dev(NULL, devname);
165    if (!dev || !open_device(dev)) {
166       Emsg1(M_ABORT, 0, "Cannot open %s\n", devname);
167    }
168    Dmsg0(90, "Device opened for read.\n");
169
170    if (stat(where, &statp) < 0) {
171       Emsg2(M_ABORT, 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_ABORT, 0, "%s must be a directory.\n", where);
176    }
177
178    wherelen = strlen(where);
179    fname = (char *)get_pool_memory(PM_FNAME);
180    ofile = (char *)get_pool_memory(PM_FNAME);
181    lname = (char *)get_pool_memory(PM_FNAME);
182
183    block = new_block(dev);
184
185    strcpy(jcr->VolumeName, VolName);
186
187    if (!acquire_device_for_read(jcr, dev, block)) {
188       Emsg1(M_ABORT, 0, "Cannot open %s\n", devname);
189    }
190
191    memset(&rec, 0, sizeof(rec));
192    rec.data = (char *) get_memory(70000);
193
194    for ( ;; ) {
195
196       if (!read_record(dev, block, &rec)) {
197          uint32_t status;
198          if (dev->state & ST_EOT) {
199             break;
200          }
201          if (dev->state & ST_EOF) {
202             continue;                 /* try again */
203          }
204          Dmsg0(0, "Read Record got a bad record\n");
205          status_dev(dev, &status);
206          Dmsg1(20, "Device status: %x\n", status);
207          if (status & MT_EOD)
208             Emsg0(M_ABORT, 0, "Unexpected End of Data\n");
209          else if (status & MT_EOT)
210             Emsg0(M_ABORT, 0, "Unexpected End of Tape\n");
211          else if (status & MT_EOF)
212             Emsg0(M_ABORT, 0, "Unexpected End of File\n");
213          else if (status & MT_DR_OPEN)
214             Emsg0(M_ABORT, 0, "Tape Door is Open\n");
215          else if (!(status & MT_ONLINE))
216             Emsg0(M_ABORT, 0, "Unexpected Tape is Off-line\n");
217          else
218             Emsg3(M_ABORT, 0, "Read error %d on Record Header %s: %s\n", n, dev_name(dev), strerror(errno));
219       }
220
221
222       /* This is no longer used */
223       if (rec.VolSessionId == 0 && rec.VolSessionTime == 0) {
224          Emsg0(M_ERROR, 0, "Zero header record. This shouldn't happen.\n");
225          break;                       /* END OF FILE */
226       }
227
228       /* 
229        * Check for Start or End of Session Record 
230        *
231        */
232       if (rec.FileIndex < 0) {
233          char *rtype;
234          switch (rec.FileIndex) {
235             case PRE_LABEL:
236                rtype = "Fresh Volume Label";   
237                break;
238             case VOL_LABEL:
239                rtype = "Volume Label";
240                break;
241             case SOS_LABEL:
242                rtype = "Begin Session";
243                break;
244             case EOS_LABEL:
245                rtype = "End Session";
246                break;
247             case EOM_LABEL:
248                rtype = "End of Media";
249                break;
250             default:
251                rtype = "Unknown";
252                break;
253          }
254          if (debug_level > 0) {
255             printf("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
256                rtype, rec.VolSessionId, rec.VolSessionTime, rec.Stream, rec.data_len);
257          }
258          continue;
259       }
260
261       /* File Attributes stream */
262       if (rec.Stream == STREAM_UNIX_ATTRIBUTES) {
263          char *ap, *lp;
264
265          /* If extracting, it was from previous stream, so
266           * close the output file.
267           */
268          if (extract) {
269             if (ofd < 0) {
270                Emsg0(M_ABORT, 0, "Logic error output file should be open\n");
271             }
272             close(ofd);
273             ofd = -1;
274             extract = FALSE;
275             set_statp(jcr, fname, ofile, lname, type, &statp);
276          }
277
278          if (sizeof_pool_memory(fname) < rec.data_len) {
279             fname = (char *) realloc_pool_memory(fname, rec.data_len + 1);
280          }
281          if (sizeof_pool_memory(ofile) < sizeof_pool_memory(fname) + wherelen + 1) {
282             ofile = (char *)realloc_pool_memory(ofile, sizeof_pool_memory(fname) + wherelen + 1);
283          }
284          if (sizeof_pool_memory(lname) < rec.data_len) {
285             ofile = (char *)realloc_pool_memory(ofile, rec.data_len + 1);
286          }
287          *fname = 0;
288          *lname = 0;
289
290          /*              
291           * An Attributes record consists of:
292           *    File_index
293           *    Type   (FT_types)
294           *    Filename
295           *    Attributes
296           *    Link name (if file linked i.e. FT_LNK)
297           *
298           */
299          sscanf(rec.data, "%ld %d %s", &record_file_index, &type, fname);
300          if (record_file_index != rec.FileIndex)
301             Emsg2(M_ABORT, 0, "Record header file index %ld not equal record index %ld\n",
302                rec.FileIndex, record_file_index);
303          ap = rec.data;
304          /* Skip to attributes */
305          while (*ap++ != 0)
306             ;
307          /* Skip to Link name */
308          if (type == FT_LNK) {
309             lp = ap;
310             while (*lp++ != 0) {
311                ;
312             }
313             strcat(lname, lp);        /* "save" link name */
314          } else {
315             *lname = 0;
316          }
317
318          /* Is this the file we want? */
319          if (file_is_included(ff, fname) && !file_is_excluded(ff, fname)) {
320
321             decode_stat(ap, &statp);
322             /*
323              * Prepend the where directory so that the
324              * files are put where the user wants.
325              *
326              * We do a little jig here to handle Win32 files with
327              * a drive letter.  
328              *   If where is null and we are running on a win32 client,
329              *      change nothing.
330              *   Otherwise, if the second character of the filename is a
331              *   colon (:), change it into a slash (/) -- this creates
332              *   a reasonable pathname on most systems.
333              */
334             strcpy(ofile, where);
335             if (fname[1] == ':') {
336                fname[1] = '/';
337                strcat(ofile, fname);
338                fname[1] = ':';
339             } else {
340                strcat(ofile, fname);
341             }
342 /*          Dmsg1(000, "Restoring: %s\n", ofile); */
343
344             extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
345
346             if (extract) {
347                 print_ls_output(ofile, &statp);   
348             }
349          }
350
351       /* Data stream and extracting */
352       } else if (rec.Stream == STREAM_FILE_DATA) {
353          if (extract) {
354             total += rec.data_len;
355             Dmsg2(8, "Write %ld bytes, total=%ld\n", rec.data_len, total);
356             if ((uint32_t)write(ofd, rec.data, rec.data_len) != rec.data_len) {
357                Emsg1(M_ABORT, 0, "Write error: %s\n", strerror(errno));
358             }
359          }
360
361       /* If extracting, wierd stream (not 1 or 2), close output file anyway */
362       } else if (extract) {
363          if (ofd < 0) {
364             Emsg0(M_ABORT, 0, "Logic error output file should be open\n");
365          }
366          close(ofd);
367          ofd = -1;
368          extract = FALSE;
369          set_statp(jcr, fname, ofile, lname, type, &statp);
370       } else if (rec.Stream != STREAM_MD5_SIGNATURE) {
371          Dmsg2(0, "None of above!!! stream=%d data=%s\n", rec.Stream, rec.data);
372       }
373    }
374
375    /* If output file is still open, it was the last one in the
376     * archive since we just hit an end of file, so close the file. 
377     */
378    if (ofd >= 0) {
379       close(ofd);
380       set_statp(jcr, fname, ofile, lname, type, &statp);
381    }
382    release_device(jcr, dev, block);
383
384    free_pool_memory(fname);
385    free_pool_memory(ofile);
386    free_pool_memory(lname);
387    term_dev(dev);
388    free_block(block);
389    return;
390 }
391
392 extern char *getuser(uid_t uid);
393 extern char *getgroup(gid_t gid);
394
395 static void print_ls_output(char *fname, struct stat *statp)
396 {
397    char buf[1000]; 
398    char *p, *f;
399    int n;
400
401    p = encode_mode(statp->st_mode, buf);
402    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
403    p += n;
404    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
405    p += n;
406    n = sprintf(p, "%8lld  ", (uint64_t)statp->st_size);
407    p += n;
408    p = encode_time(statp->st_ctime, p);
409    *p++ = ' ';
410    *p++ = ' ';
411    for (f=fname; *f; )
412       *p++ = *f++;
413    *p++ = '\n';
414    *p = 0;
415    fputs(buf, stdout);
416 }