]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bls.c
3e405c6fd32090a8a223ac0229d9a8d434d9f175
[bacula/bacula] / bacula / src / stored / bls.c
1 /*
2  *
3  *  Dumb program to do an "ls" of a Bacula 1.0 mortal file.
4  *
5  *   Version $Id$
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29 #include "findlib/find.h"
30
31 static void do_blocks(char *infname);
32 static void do_jobs(char *infname);
33 static void do_ls(char *fname);
34 static void print_ls_output(char *fname, char *link, int type, struct stat *statp);
35 static void do_setup(char *infname);
36 static void do_close();
37
38 static DEVICE *dev;
39 static int default_tape = FALSE;
40 static int dump_label = FALSE;
41 static int list_blocks = FALSE;
42 static int list_jobs = FALSE;
43 static int verbose = 0;
44 static char Vol[2000];
45 static char *VolName;
46 static char *p;
47 static DEV_RECORD *rec;
48 static DEV_BLOCK *block;
49 static int NumVolumes, CurVolume;
50 static JCR *jcr;
51
52
53 extern char BaculaId[];
54
55 static FF_PKT ff;
56
57 static BSR *bsr = NULL;
58
59 static void usage()
60 {
61    fprintf(stderr,
62 "Usage: bls [-d debug_level] <physical-device-name>\n"
63 "       -b <file>       specify a bootstrap file\n"
64 "       -e <file>       exclude list\n"
65 "       -i <file>       include list\n"
66 "       -j              list jobs\n"
67 "       -k              list blocks\n"
68 "       -L              list tape label\n"
69 "    (none of above)    list saved files\n"
70 "       -t              use default tape device\n"
71 "       -v              be verbose\n"
72 "       -?              print this message\n\n");
73    exit(1);
74 }
75
76
77 int main (int argc, char *argv[])
78 {
79    int i, ch;
80    FILE *fd;
81    char line[1000];
82
83    my_name_is(argc, argv, "bls");
84    init_msg(NULL, NULL);              /* initialize message handler */
85
86    memset(&ff, 0, sizeof(ff));
87    init_include_exclude_files(&ff);
88
89    while ((ch = getopt(argc, argv, "b:d:e:i:jkLtv?")) != -1) {
90       switch (ch) {
91          case 'b':
92             bsr = parse_bsr(NULL, optarg);
93             break;
94
95          case 'd':                    /* debug level */
96             debug_level = atoi(optarg);
97             if (debug_level <= 0)
98                debug_level = 1; 
99             break;
100
101          case 'e':                    /* exclude list */
102             if ((fd = fopen(optarg, "r")) == NULL) {
103                Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
104                   optarg, strerror(errno));
105                exit(1);
106             }
107             while (fgets(line, sizeof(line), fd) != NULL) {
108                strip_trailing_junk(line);
109                Dmsg1(000, "add_exclude %s\n", line);
110                add_fname_to_exclude_list(&ff, line);
111             }
112             fclose(fd);
113             break;
114
115          case 'i':                    /* include list */
116             if ((fd = fopen(optarg, "r")) == NULL) {
117                Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
118                   optarg, strerror(errno));
119                exit(1);
120             }
121             while (fgets(line, sizeof(line), fd) != NULL) {
122                strip_trailing_junk(line);
123                Dmsg1(000, "add_include %s\n", line);
124                add_fname_to_include_list(&ff, 0, line);
125             }
126             fclose(fd);
127             break;
128
129          case 'j':
130             list_jobs = TRUE;
131             break;
132
133          case 'k':
134             list_blocks = TRUE;
135             break;
136
137          case 'L':
138             dump_label = TRUE;
139             break;
140
141          case 't':
142             default_tape = TRUE;
143             break;
144
145          case 'v':
146             verbose++;
147             break;
148
149          case '?':
150          default:
151             usage();
152
153       }  
154    }
155    argc -= optind;
156    argv += optind;
157
158    if (!argc && !default_tape) {
159       Pmsg0(0, "No archive name specified\n");
160       usage();
161    }
162
163    if (ff.included_files_list == NULL) {
164       add_fname_to_include_list(&ff, 0, "/");
165    }
166
167    /* Try default device */
168    if (default_tape) {
169       do_ls(DEFAULT_TAPE_DRIVE);
170       return 0;
171    }
172
173    for (i=0; i < argc; i++) {
174       do_setup(argv[i]);
175       if (list_blocks) {
176          do_blocks(argv[i]);
177       } else if (list_jobs) {
178          do_jobs(argv[i]);
179       } else {
180          do_ls(argv[i]);
181       }
182       do_close();
183    }
184    if (bsr) {
185       free_bsr(bsr);
186    }
187    return 0;
188 }
189
190 static void my_free_jcr(JCR *jcr)
191 {
192    return;
193 }
194
195 /*
196  * Setup device, jcr, and prepare to read
197  */
198 static void do_setup(char *infname) 
199 {
200    jcr = new_jcr(sizeof(JCR), my_free_jcr);
201    VolName = Vol;
202    VolName[0] = 0;
203    if (strncmp(infname, "/dev/", 5) != 0) {
204       /* Try stripping file part */
205       p = infname + strlen(infname);
206       while (p >= infname && *p != '/')
207          p--;
208       if (*p == '/') {
209          strcpy(VolName, p+1);
210          *p = 0;
211       }
212    }
213    Dmsg2(10, "Device=%s, Vol=%s.\n", infname, VolName);
214    dev = init_dev(NULL, infname);
215    if (!dev) {
216       Emsg1(M_FATAL, 0, "Cannot open %s\n", infname);
217       exit(1);
218    }
219    /* ***FIXME**** init capabilities */
220    if (!open_device(dev)) {
221       Emsg1(M_FATAL, 0, "Cannot open %s\n", infname);
222       exit(1);
223    }
224    Dmsg0(90, "Device opened for read.\n");
225
226    rec = new_record();
227    block = new_block(dev);
228
229    NumVolumes = 0;
230    CurVolume = 1;
231    for (p = VolName; p && *p; ) {
232       p = strchr(p, '^');
233       if (p) {
234          *p++ = 0;
235       }
236       NumVolumes++;
237    }
238
239    jcr->VolumeName = (char *)check_pool_memory_size(jcr->VolumeName, strlen(VolName)+1);
240    strcpy(jcr->VolumeName, VolName);
241    if (!acquire_device_for_read(jcr, dev, block)) {
242       Emsg0(M_ERROR, 0, dev->errmsg);
243       exit(1);
244    }
245 }
246
247 static void do_close()
248 {
249    term_dev(dev);
250    free_record(rec);
251    free_block(block);
252    free_jcr(jcr);
253 }
254
255 static int mount_next_volume(char *infname)
256 {
257    if (rec->remainder) {
258       Dmsg0(20, "Not end of record. Next volume has more data for current record.\n");
259    }
260    Dmsg2(20, "NumVolumes=%d CurVolume=%d\n", NumVolumes, CurVolume);
261    if (NumVolumes > 1 && CurVolume < NumVolumes) {
262       p = VolName;
263       while (*p++)  
264          { }
265       CurVolume++;
266       Dmsg1(20, "There is another volume %s.\n", p);
267       VolName = p;
268       jcr->VolumeName = check_pool_memory_size(jcr->VolumeName, 
269                           strlen(VolName)+1);
270       strcpy(jcr->VolumeName, VolName);
271
272       close_dev(dev);
273       dev->state &= ~ST_READ; 
274       if (!acquire_device_for_read(jcr, dev, block)) {
275          Emsg2(M_FATAL, 0, "Cannot open Dev=%s, Vol=%s\n", infname, VolName);
276          exit(1);
277       }
278       return 1;              /* Next volume mounted */
279    }
280    printf("End of Device reached.\n");
281    return 0;                 /* EOT */
282 }
283
284 /*
285  * Device got an error, attempt to analyse it
286  */
287 static void display_error_status()
288 {
289    uint32_t status;
290
291    Emsg0(M_ERROR, 0, dev->errmsg);
292    status_dev(dev, &status);
293    Dmsg1(20, "Device status: %x\n", status);
294    if (status & MT_EOD)
295       Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
296    else if (status & MT_EOT)
297       Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
298    else if (status & MT_EOF)
299       Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
300    else if (status & MT_DR_OPEN)
301       Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
302    else if (!(status & MT_ONLINE))
303       Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
304    else
305       Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
306 }
307
308
309 /* List just block information */
310 static void do_blocks(char *infname)
311 {
312
313    dump_volume_label(dev);
314
315    /* Assume that we have already read the volume label.
316     * If on second or subsequent volume, adjust buffer pointer 
317     */
318    if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
319       Pmsg1(0, "\n\
320 Warning, this Volume is a continuation of Volume %s\n",
321                 dev->VolHdr.PrevVolName);
322    }
323  
324    if (verbose) {
325       rec = new_record();
326    }
327    for ( ;; ) {
328       if (!read_block_from_device(dev, block)) {
329          Dmsg0(20, "!read_block()\n");
330          if (dev->state & ST_EOT) {
331             if (!mount_next_volume(infname)) {
332                break;
333             }
334             continue;
335          }
336          if (dev->state & ST_EOF) {
337             Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
338             Dmsg0(20, "read_record got eof. try again\n");
339             continue;
340          }
341          if (dev->state & ST_SHORT) {
342             Emsg0(M_INFO, 0, dev->errmsg);
343             continue;
344          }
345          display_error_status();
346          break;
347       }
348
349       if (verbose) {
350          read_record_from_block(block, rec);
351          Pmsg6(-1, "Block: %d blen=%d First rec FI=%s SessId=%d Strm=%s rlen=%d\n",
352               block->BlockNumber, block->block_len,
353               FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
354               stream_to_ascii(rec->Stream), rec->data_len);
355          rec->remainder = 0;
356       } else {
357          printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
358       }
359
360    }
361    return;
362 }
363
364 /* Do list job records */
365 static void do_jobs(char *infname)
366 {
367
368    /* Assume that we have already read the volume label.
369     * If on second or subsequent volume, adjust buffer pointer 
370     */
371    if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
372       Pmsg1(0, "\n\
373 Warning, this Volume is a continuation of Volume %s\n",
374                 dev->VolHdr.PrevVolName);
375    }
376  
377    for ( ;; ) {
378       if (!read_block_from_device(dev, block)) {
379          Dmsg0(20, "!read_block()\n");
380          if (dev->state & ST_EOT) {
381             if (!mount_next_volume(infname)) {
382                break;
383             }
384             continue;
385          }
386          if (dev->state & ST_EOF) {
387             Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
388             Dmsg0(20, "read_record got eof. try again\n");
389             continue;
390          }
391          if (dev->state & ST_SHORT) {
392             Pmsg0(000, "Got short block.\n");
393             Emsg0(M_INFO, 0, dev->errmsg);
394             continue;
395          }
396          display_error_status();
397          break;
398       }
399       while (read_record_from_block(block, rec)) {
400          if (debug_level >= 30) {
401             Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
402                   FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream), 
403                   rec->data_len);
404          }
405
406
407          /*  
408           * Check for End of File record (all zeros)
409           *    NOTE: this no longer exists
410           */
411          if (rec->VolSessionId == 0 && rec->VolSessionTime == 0) {
412             Emsg0(M_ERROR_TERM, 0, "Zero VolSessionId and VolSessionTime. This shouldn't happen\n");
413          }
414
415          /* 
416           * Check for Start or End of Session Record 
417           *
418           */
419          if (rec->FileIndex < 0) {
420             dump_label_record(dev, rec, verbose);
421             continue;
422          }
423       }
424       rec->remainder = 0;
425    }
426    return;
427 }
428
429 /* Do an ls type listing of an archive */
430 static void do_ls(char *infname)
431 {
432    char fname[2000];
433    struct stat statp;
434    int type;
435    long record_file_index;
436
437    if (dump_label) {
438       dump_volume_label(dev);
439       return;
440    }
441
442    /* Assume that we have already read the volume label.
443     * If on second or subsequent volume, adjust buffer pointer 
444     */
445    if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
446       Pmsg1(0, "\n\
447 Warning, this Volume is a continuation of Volume %s\n",
448                 dev->VolHdr.PrevVolName);
449    }
450  
451    for ( ;; ) {
452       SESSION_LABEL sessrec;
453
454       if (!read_record(dev, block, rec)) {
455          Dmsg0(20, "!read_record()\n");
456          if (dev->state & ST_EOT) {
457             if (!mount_next_volume(infname)) {
458                break;
459             }
460             continue;
461          }
462          if (dev->state & ST_EOF) {
463             Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
464             Dmsg0(20, "read_record got eof. try again\n");
465             continue;
466          }
467          if (dev->state & ST_SHORT) {
468             Emsg0(M_INFO, 0, dev->errmsg);
469             continue;
470          }
471          display_error_status();
472          break;
473       }
474
475       if (debug_level >= 30) {
476          Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
477                FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream), 
478                rec->data_len);
479       }
480
481
482       /*  
483        * Check for End of File record (all zeros)
484        *    NOTE: this no longer exists
485        */
486       if (rec->VolSessionId == 0 && rec->VolSessionTime == 0) {
487          Emsg0(M_ERROR_TERM, 0, "Zero VolSessionId and VolSessionTime. This shouldn't happen\n");
488       }
489
490       /* 
491        * Check for Start or End of Session Record 
492        *
493        */
494       if (rec->FileIndex < 0) {
495          char *rtype;
496          memset(&sessrec, 0, sizeof(sessrec));
497          switch (rec->FileIndex) {
498             case PRE_LABEL:
499                rtype = "Fresh Volume Label";   
500                break;
501             case VOL_LABEL:
502                rtype = "Volume Label";
503                unser_volume_label(dev, rec);
504                break;
505             case SOS_LABEL:
506                rtype = "Begin Session";
507                unser_session_label(&sessrec, rec);
508                break;
509             case EOS_LABEL:
510                rtype = "End Session";
511                break;
512             case EOM_LABEL:
513                rtype = "End of Media";
514                break;
515             default:
516                rtype = "Unknown";
517                break;
518          }
519          if (debug_level > 0) {
520             printf("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
521                rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
522          }
523
524          Dmsg1(40, "Got label = %d\n", rec->FileIndex);
525          if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
526             Dmsg0(40, "Get EOM LABEL\n");
527             break;                         /* yes, get out */
528          }
529          continue;                         /* ignore other labels */
530       } /* end if label record */
531
532       /* 
533        * Apply BSR filter
534        */
535       if (bsr && !match_bsr(bsr, rec, &dev->VolHdr, &sessrec)) {
536          continue;
537       }
538
539       /* File Attributes stream */
540       if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
541          char *ap, *fp;
542          sscanf(rec->data, "%ld %d", &record_file_index, &type);
543          if (record_file_index != rec->FileIndex) {
544             Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
545                rec->FileIndex, record_file_index);
546          }
547          ap = rec->data;
548
549          while (*ap++ != ' ')         /* skip record file index */
550             ;
551          while (*ap++ != ' ')         /* skip type */
552             ;
553          /* Save filename and position to attributes */
554          fp = fname;
555          while (*ap != 0) {
556             *fp++  = *ap++;
557          }
558          *fp = *ap++;                 /* terminate filename & point to attribs */
559
560          decode_stat(ap, &statp);
561          /* Skip to link name */  
562          while (*ap++ != 0)
563             ;
564          print_ls_output(fname, ap, type, &statp);
565       }
566    }
567    return;
568 }
569
570 extern char *getuser(uid_t uid);
571 extern char *getgroup(gid_t gid);
572
573 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
574 {
575    char buf[1000]; 
576    char ec1[30];
577    char *p, *f;
578    int n;
579
580    if (!file_is_included(&ff, fname) || file_is_excluded(&ff, fname)) {
581       return;
582    }
583    p = encode_mode(statp->st_mode, buf);
584    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
585    p += n;
586    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
587    p += n;
588    n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
589    p += n;
590    p = encode_time(statp->st_ctime, p);
591    *p++ = ' ';
592    *p++ = ' ';
593    /* Copy file name */
594    for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
595       *p++ = *f++;
596    if (type == FT_LNK) {
597       *p++ = ' ';
598       *p++ = '-';
599       *p++ = '>';
600       *p++ = ' ';
601       /* Copy link name */
602       for (f=link; *f && (p-buf) < (int)sizeof(buf); )
603          *p++ = *f++;
604    }
605    *p++ = '\n';
606    *p = 0;
607    fputs(buf, stdout);
608 }
609
610 /* Dummies to replace askdir.c */
611 int     dir_get_volume_info(JCR *jcr) { return 1;}
612 int     dir_find_next_appendable_volume(JCR *jcr) { return 1;}
613 int     dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
614 int     dir_create_jobmedia_record(JCR *jcr) { return 1; }
615 int     dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
616 int     dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
617 int     dir_send_job_status(JCR *jcr) {return 1;}
618
619
620 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
621 {
622    fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
623       jcr->VolumeName, dev_name(dev));
624    getchar();   
625    return 1;
626 }