3 * Dumb program to do an "ls" of a Bacula 1.0 mortal file.
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
29 #include "findlib/find.h"
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();
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];
47 static DEV_RECORD *rec;
48 static DEV_BLOCK *block;
49 static int NumVolumes, CurVolume;
53 extern char BaculaId[];
57 static BSR *bsr = NULL;
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"
68 " -L list tape label\n"
69 " (none of above) list saved files\n"
70 " -t use default tape device\n"
72 " -? print this message\n\n");
76 static char *rec_state_to_str(DEV_RECORD *rec)
80 if (rec->state & REC_NO_HEADER) {
81 strcat(buf, "Nohdr,");
83 if (is_partial_record(rec)) {
84 strcat(buf, "partial,");
86 if (rec->state & REC_BLOCK_EMPTY) {
87 strcat(buf, "empty,");
89 if (rec->state & REC_NO_MATCH) {
90 strcat(buf, "Nomatch,");
92 if (rec->state & REC_CONTINUATION) {
96 buf[strlen(buf)-1] = 0;
103 int main (int argc, char *argv[])
109 my_name_is(argc, argv, "bls");
110 init_msg(NULL, NULL); /* initialize message handler */
112 memset(&ff, 0, sizeof(ff));
113 init_include_exclude_files(&ff);
115 while ((ch = getopt(argc, argv, "b:d:e:i:jkLtv?")) != -1) {
118 bsr = parse_bsr(NULL, optarg);
121 case 'd': /* debug level */
122 debug_level = atoi(optarg);
123 if (debug_level <= 0)
127 case 'e': /* exclude list */
128 if ((fd = fopen(optarg, "r")) == NULL) {
129 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
130 optarg, strerror(errno));
133 while (fgets(line, sizeof(line), fd) != NULL) {
134 strip_trailing_junk(line);
135 Dmsg1(000, "add_exclude %s\n", line);
136 add_fname_to_exclude_list(&ff, line);
141 case 'i': /* include list */
142 if ((fd = fopen(optarg, "r")) == NULL) {
143 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
144 optarg, strerror(errno));
147 while (fgets(line, sizeof(line), fd) != NULL) {
148 strip_trailing_junk(line);
149 Dmsg1(000, "add_include %s\n", line);
150 add_fname_to_include_list(&ff, 0, line);
184 if (!argc && !default_tape) {
185 Pmsg0(0, "No archive name specified\n");
189 if (ff.included_files_list == NULL) {
190 add_fname_to_include_list(&ff, 0, "/");
193 /* Try default device */
195 do_ls(DEFAULT_TAPE_DRIVE);
199 for (i=0; i < argc; i++) {
203 } else if (list_jobs) {
216 static void my_free_jcr(JCR *jcr)
222 * Setup device, jcr, and prepare to read
224 static void do_setup(char *infname)
226 jcr = new_jcr(sizeof(JCR), my_free_jcr);
227 jcr->VolSessionId = 1;
228 jcr->VolSessionTime = (uint32_t)time(NULL);
230 strcpy(jcr->Job, "bls");
231 jcr->dev_name = get_pool_memory(PM_FNAME);
232 strcpy(jcr->dev_name, infname);
236 if (strncmp(infname, "/dev/", 5) != 0) {
237 /* Try stripping file part */
238 p = infname + strlen(infname);
239 while (p >= infname && *p != '/')
242 strcpy(VolName, p+1);
246 Dmsg2(10, "Device=%s, Vol=%s.\n", infname, VolName);
247 dev = init_dev(NULL, infname);
249 Emsg1(M_FATAL, 0, "Cannot open %s\n", infname);
252 /* ***FIXME**** init capabilities */
253 if (!open_device(dev)) {
254 Emsg1(M_FATAL, 0, "Cannot open %s\n", infname);
257 Dmsg0(90, "Device opened for read.\n");
260 block = new_block(dev);
264 for (p = VolName; p && *p; ) {
272 pm_strcpy(&jcr->VolumeName, VolName);
273 Dmsg1(100, "Volume=%s\n", jcr->VolumeName);
274 if (!acquire_device_for_read(jcr, dev, block)) {
275 Emsg0(M_ERROR, 0, dev->errmsg);
280 static void do_close()
288 static int mount_next_volume(char *infname)
290 if (rec->remainder) {
291 Dmsg0(20, "Not end of record. Next volume has more data for current record.\n");
293 Dmsg2(20, "NumVolumes=%d CurVolume=%d\n", NumVolumes, CurVolume);
294 if (NumVolumes > 1 && CurVolume < NumVolumes) {
299 Dmsg1(20, "There is another volume %s.\n", p);
301 pm_strcpy(&jcr->VolumeName, VolName);
304 dev->state &= ~ST_READ;
305 if (!acquire_device_for_read(jcr, dev, block)) {
306 Emsg2(M_FATAL, 0, "Cannot open Dev=%s, Vol=%s\n", infname, VolName);
309 return 1; /* Next volume mounted */
311 printf("End of Device reached.\n");
316 * Device got an error, attempt to analyse it
318 static void display_error_status()
322 Emsg0(M_ERROR, 0, dev->errmsg);
323 status_dev(dev, &status);
324 Dmsg1(20, "Device status: %x\n", status);
326 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
327 else if (status & MT_EOT)
328 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
329 else if (status & MT_EOF)
330 Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
331 else if (status & MT_DR_OPEN)
332 Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
333 else if (!(status & MT_ONLINE))
334 Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
336 Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
340 /* List just block information */
341 static void do_blocks(char *infname)
344 dump_volume_label(dev);
346 /* Assume that we have already read the volume label.
347 * If on second or subsequent volume, adjust buffer pointer
349 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
351 Warning, this Volume is a continuation of Volume %s\n",
352 dev->VolHdr.PrevVolName);
359 if (!read_block_from_device(dev, block)) {
360 Dmsg0(20, "!read_block()\n");
361 if (dev->state & ST_EOT) {
362 if (!mount_next_volume(infname)) {
367 if (dev->state & ST_EOF) {
368 Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
369 Dmsg0(20, "read_record got eof. try again\n");
372 if (dev->state & ST_SHORT) {
373 Emsg0(M_INFO, 0, dev->errmsg);
376 display_error_status();
381 read_record_from_block(block, rec);
382 Pmsg6(-1, "Block: %d blen=%d First rec FI=%s SessId=%d Strm=%s rlen=%d\n",
383 block->BlockNumber, block->block_len,
384 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
385 stream_to_ascii(rec->Stream), rec->data_len);
388 printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
395 /* Do list job records */
396 static void do_jobs(char *infname)
399 /* Assume that we have already read the volume label.
400 * If on second or subsequent volume, adjust buffer pointer
402 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
404 Warning, this Volume is a continuation of Volume %s\n",
405 dev->VolHdr.PrevVolName);
409 if (!read_block_from_device(dev, block)) {
410 Dmsg0(20, "!read_block()\n");
411 if (dev->state & ST_EOT) {
412 if (!mount_next_volume(infname)) {
417 if (dev->state & ST_EOF) {
418 Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
419 Dmsg0(20, "read_record got eof. try again\n");
422 if (dev->state & ST_SHORT) {
423 Pmsg0(000, "Got short block.\n");
424 Emsg0(M_INFO, 0, dev->errmsg);
427 display_error_status();
430 while (read_record_from_block(block, rec)) {
431 if (debug_level >= 30) {
432 Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
433 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream),
439 * Check for End of File record (all zeros)
440 * NOTE: this no longer exists
442 if (rec->VolSessionId == 0 && rec->VolSessionTime == 0) {
443 Emsg0(M_ERROR_TERM, 0, "Zero VolSessionId and VolSessionTime. This shouldn't happen\n");
447 * Check for Start or End of Session Record
450 if (rec->FileIndex < 0) {
451 dump_label_record(dev, rec, verbose);
460 /* Do an ls type listing of an archive */
461 static void do_ls(char *infname)
466 long record_file_index;
467 uint32_t num_files = 0;
471 dump_volume_label(dev);
475 /* Assume that we have already read the volume label.
476 * If on second or subsequent volume, adjust buffer pointer
478 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
480 Warning, this Volume is a continuation of Volume %s\n",
481 dev->VolHdr.PrevVolName);
485 SESSION_LABEL sessrec;
487 if (!read_block_from_device(dev, block)) {
488 Dmsg0(20, "!read_record()\n");
489 if (dev->state & ST_EOT) {
490 if (!mount_next_volume(infname)) {
495 if (dev->state & ST_EOF) {
496 Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
497 Dmsg0(20, "read_record got eof. try again\n");
500 if (dev->state & ST_SHORT) {
501 Emsg0(M_INFO, 0, dev->errmsg);
504 display_error_status();
508 Dmsg2(10, "Block: %d blen=%d\n", block->BlockNumber, block->block_len);
512 for (rec->state=0; !is_block_empty(rec); ) {
513 if (!read_record_from_block(block, rec)) {
514 Dmsg3(10, "!read-break. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
515 block->BlockNumber, rec->remainder);
518 Dmsg3(10, "read-OK. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
519 block->BlockNumber, rec->remainder);
521 * At this point, we have at least a record header.
522 * Now decide if we want this record or not, but remember
523 * before accessing the record, we may need to read again to
528 Dmsg6(30, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
529 rec_state_to_str(rec), block->BlockNumber,
530 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
532 if (debug_level >= 30) {
533 Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
534 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream),
539 * Check for End of File record (all zeros)
540 * NOTE: this no longer exists
542 if (rec->VolSessionId == 0 && rec->VolSessionTime == 0) {
543 Emsg0(M_ERROR_TERM, 0, "Zero VolSessionId and VolSessionTime. This shouldn't happen\n");
547 * Check for Start or End of Session Record
550 if (rec->FileIndex < 0) {
552 memset(&sessrec, 0, sizeof(sessrec));
553 switch (rec->FileIndex) {
555 rtype = "Fresh Volume Label";
558 rtype = "Volume Label";
559 unser_volume_label(dev, rec);
562 rtype = "Begin Session";
563 unser_session_label(&sessrec, rec);
566 rtype = "End Session";
569 rtype = "End of Media";
575 if (debug_level > 0) {
576 printf("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
577 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
580 Dmsg1(40, "Got label = %d\n", rec->FileIndex);
581 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
582 Dmsg0(100, "EOM LABEL break\n");
584 break; /* yes, get out */
587 continue; /* we don't want record, read next one */
588 } /* end if label record */
593 if (bsr && !match_bsr(bsr, rec, &dev->VolHdr, &sessrec)) {
595 Dmsg5(10, "BSR no match rec=%d block=%d SessId=%d SessTime=%d FI=%d\n",
596 record, block->BlockNumber, rec->VolSessionId, rec->VolSessionTime,
600 continue; /* we don't want record, read next one */
602 if (is_partial_record(rec)) {
603 Dmsg6(10, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
604 rec_state_to_str(rec), block->BlockNumber,
605 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
606 break; /* read second part of record */
609 /* File Attributes stream */
610 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
612 sscanf(rec->data, "%ld %d", &record_file_index, &type);
613 if (record_file_index != rec->FileIndex) {
614 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
615 rec->FileIndex, record_file_index);
619 while (*ap++ != ' ') /* skip record file index */
621 while (*ap++ != ' ') /* skip type */
623 /* Save filename and position to attributes */
628 *fp = *ap++; /* terminate filename & point to attribs */
630 decode_stat(ap, &statp);
631 /* Skip to link name */
634 print_ls_output(fname, ap, type, &statp);
640 printf("%u files found.\n", num_files);
645 extern char *getuser(uid_t uid);
646 extern char *getgroup(gid_t gid);
648 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
655 if (!file_is_included(&ff, fname) || file_is_excluded(&ff, fname)) {
658 p = encode_mode(statp->st_mode, buf);
659 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
661 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
663 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
665 p = encode_time(statp->st_ctime, p);
669 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
671 if (type == FT_LNK) {
677 for (f=link; *f && (p-buf) < (int)sizeof(buf); )
685 /* Dummies to replace askdir.c */
686 int dir_get_volume_info(JCR *jcr) { return 1;}
687 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
688 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
689 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
690 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
691 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
692 int dir_send_job_status(JCR *jcr) {return 1;}
695 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
697 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
698 jcr->VolumeName, dev_name(dev));