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