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 do_close();
35 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
38 static int default_tape = FALSE;
39 static int dump_label = FALSE;
40 static int list_blocks = FALSE;
41 static int list_jobs = FALSE;
42 static int verbose = 0;
43 static DEV_RECORD *rec;
44 static DEV_BLOCK *block;
46 static SESSION_LABEL sessrec;
49 extern char BaculaId[];
53 static BSR *bsr = NULL;
58 "\nVersion: " VERSION " (" DATE ")\n\n"
59 "Usage: bls [-d debug_level] <physical-device-name>\n"
60 " -b <file> specify a bootstrap file\n"
61 " -e <file> exclude list\n"
62 " -i <file> include list\n"
65 " -L list tape label\n"
66 " (none of above) list saved files\n"
67 " -t use default tape device\n"
69 " -? print this message\n\n");
73 static char *rec_state_to_str(DEV_RECORD *rec)
77 if (rec->state & REC_NO_HEADER) {
78 strcat(buf, "Nohdr,");
80 if (is_partial_record(rec)) {
81 strcat(buf, "partial,");
83 if (rec->state & REC_BLOCK_EMPTY) {
84 strcat(buf, "empty,");
86 if (rec->state & REC_NO_MATCH) {
87 strcat(buf, "Nomatch,");
89 if (rec->state & REC_CONTINUATION) {
93 buf[strlen(buf)-1] = 0;
100 int main (int argc, char *argv[])
106 my_name_is(argc, argv, "bls");
107 init_msg(NULL, NULL); /* initialize message handler */
109 memset(&ff, 0, sizeof(ff));
110 init_include_exclude_files(&ff);
112 while ((ch = getopt(argc, argv, "b:d:e:i:jkLtv?")) != -1) {
115 bsr = parse_bsr(NULL, optarg);
118 case 'd': /* debug level */
119 debug_level = atoi(optarg);
120 if (debug_level <= 0)
124 case 'e': /* exclude list */
125 if ((fd = fopen(optarg, "r")) == NULL) {
126 Pmsg2(0, "Could not open exclude file: %s, ERR=%s\n",
127 optarg, strerror(errno));
130 while (fgets(line, sizeof(line), fd) != NULL) {
131 strip_trailing_junk(line);
132 Dmsg1(100, "add_exclude %s\n", line);
133 add_fname_to_exclude_list(&ff, line);
138 case 'i': /* include list */
139 if ((fd = fopen(optarg, "r")) == NULL) {
140 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
141 optarg, strerror(errno));
144 while (fgets(line, sizeof(line), fd) != NULL) {
145 strip_trailing_junk(line);
146 Dmsg1(100, "add_include %s\n", line);
147 add_fname_to_include_list(&ff, 0, line);
181 if (!argc && !default_tape) {
182 Pmsg0(0, "No archive name specified\n");
186 if (ff.included_files_list == NULL) {
187 add_fname_to_include_list(&ff, 0, "/");
190 /* Try default device */
192 do_ls(DEFAULT_TAPE_DRIVE);
196 for (i=0; i < argc; i++) {
197 jcr = setup_jcr("bls", argv[i], bsr);
198 dev = setup_to_read_device(jcr);
203 block = new_block(dev);
206 } else if (list_jobs) {
220 static void do_close()
230 * Device got an error, attempt to analyse it
232 static void display_error_status()
236 Emsg0(M_ERROR, 0, dev->errmsg);
237 status_dev(dev, &status);
238 Dmsg1(20, "Device status: %x\n", status);
240 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Data\n");
241 else if (status & MT_EOT)
242 Emsg0(M_ERROR_TERM, 0, "Unexpected End of Tape\n");
243 else if (status & MT_EOF)
244 Emsg0(M_ERROR_TERM, 0, "Unexpected End of File\n");
245 else if (status & MT_DR_OPEN)
246 Emsg0(M_ERROR_TERM, 0, "Tape Door is Open\n");
247 else if (!(status & MT_ONLINE))
248 Emsg0(M_ERROR_TERM, 0, "Unexpected Tape is Off-line\n");
250 Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
254 /* List just block information */
255 static void do_blocks(char *infname)
258 dump_volume_label(dev);
260 /* Assume that we have already read the volume label.
261 * If on second or subsequent volume, adjust buffer pointer
263 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
265 Warning, this Volume is a continuation of Volume %s\n",
266 dev->VolHdr.PrevVolName);
273 if (!read_block_from_device(dev, block)) {
274 Dmsg0(20, "!read_block()\n");
275 if (dev->state & ST_EOT) {
276 if (!mount_next_read_volume(jcr, dev, block)) {
277 printf("End of File on device\n");
281 record = new_record();
282 read_block_from_device(dev, block);
283 read_record_from_block(block, record);
284 get_session_record(dev, record, &sessrec);
286 printf("Volume %s mounted.\n", jcr->VolumeName);
289 if (dev->state & ST_EOF) {
290 Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
291 Dmsg0(20, "read_record got eof. try again\n");
294 if (dev->state & ST_SHORT) {
295 Emsg0(M_INFO, 0, dev->errmsg);
298 display_error_status();
303 read_record_from_block(block, rec);
304 Pmsg6(-1, "Block: %d blen=%d First rec FI=%s SessId=%d Strm=%s rlen=%d\n",
305 block->BlockNumber, block->block_len,
306 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
307 stream_to_ascii(rec->Stream), rec->data_len);
310 printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
317 /* Do list job records */
318 static void do_jobs(char *infname)
321 /* Assume that we have already read the volume label.
322 * If on second or subsequent volume, adjust buffer pointer
324 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
326 Warning, this Volume is a continuation of Volume %s\n",
327 dev->VolHdr.PrevVolName);
331 if (!read_block_from_device(dev, block)) {
332 Dmsg0(20, "!read_block()\n");
333 if (dev->state & ST_EOT) {
335 if (!mount_next_read_volume(jcr, dev, block)) {
336 printf("Got EOF on device %s\n", dev_name(dev));
339 record = new_record();
340 read_block_from_device(dev, block);
341 read_record_from_block(block, record);
342 get_session_record(dev, record, &sessrec);
344 printf("Volume %s mounted.\n", jcr->VolumeName);
347 if (dev->state & ST_EOF) {
348 Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
349 Dmsg0(20, "read_record got eof. try again\n");
352 if (dev->state & ST_SHORT) {
353 Pmsg0(000, "Got short block.\n");
354 Emsg0(M_INFO, 0, dev->errmsg);
357 display_error_status();
360 while (read_record_from_block(block, rec)) {
361 if (debug_level >= 30) {
362 Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
363 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream),
369 * Check for End of File record (all zeros)
370 * NOTE: this no longer exists
372 if (rec->VolSessionId == 0 && rec->VolSessionTime == 0) {
373 Emsg0(M_ERROR_TERM, 0, "Zero VolSessionId and VolSessionTime. This shouldn't happen\n");
377 * Check for Start or End of Session Record
380 if (rec->FileIndex < 0) {
381 dump_label_record(dev, rec, verbose);
390 /* Do an ls type listing of an archive */
391 static void do_ls(char *infname)
396 long record_file_index;
397 uint32_t num_files = 0;
401 dump_volume_label(dev);
405 /* Assume that we have already read the volume label.
406 * If on second or subsequent volume, adjust buffer pointer
408 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
410 Warning, this Volume is a continuation of Volume %s\n",
411 dev->VolHdr.PrevVolName);
416 if (!read_block_from_device(dev, block)) {
417 Dmsg0(20, "!read_record()\n");
418 if (dev->state & ST_EOT) {
420 Dmsg3(100, "EOT. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
421 block->BlockNumber, rec->remainder);
422 if (!mount_next_read_volume(jcr, dev, block)) {
423 Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
424 block->BlockNumber, rec->remainder);
427 Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
428 block->BlockNumber, rec->remainder);
429 record = new_record();
430 read_block_from_device(dev, block);
431 read_record_from_block(block, record);
432 get_session_record(dev, record, &sessrec);
436 if (dev->state & ST_EOF) {
437 Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
438 Dmsg0(20, "read_record got eof. try again\n");
441 if (dev->state & ST_SHORT) {
442 Emsg0(M_INFO, 0, dev->errmsg);
445 display_error_status();
449 Dmsg2(10, "Block: %d blen=%d\n", block->BlockNumber, block->block_len);
454 for (rec->state=0; !is_block_empty(rec); ) {
455 if (!read_record_from_block(block, rec)) {
456 Dmsg3(10, "!read-break. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
457 block->BlockNumber, rec->remainder);
460 Dmsg3(10, "read-OK. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
461 block->BlockNumber, rec->remainder);
463 * At this point, we have at least a record header.
464 * Now decide if we want this record or not, but remember
465 * before accessing the record, we may need to read again to
470 Dmsg6(30, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
471 rec_state_to_str(rec), block->BlockNumber,
472 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
474 if (debug_level >= 30) {
475 Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
476 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream),
480 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
481 Dmsg0(40, "Get EOM LABEL\n");
483 break; /* yes, get out */
486 /* Some sort of label? */
487 if (rec->FileIndex < 0) {
488 get_session_record(dev, rec, &sessrec);
490 } /* end if label record */
495 if (bsr && !match_bsr(bsr, rec, &dev->VolHdr, &sessrec)) {
497 Dmsg5(10, "BSR no match rec=%d block=%d SessId=%d SessTime=%d FI=%d\n",
498 record, block->BlockNumber, rec->VolSessionId, rec->VolSessionTime,
502 continue; /* we don't want record, read next one */
504 if (is_partial_record(rec)) {
505 Dmsg6(10, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
506 rec_state_to_str(rec), block->BlockNumber,
507 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
508 break; /* read second part of record */
511 /* File Attributes stream */
512 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
514 sscanf(rec->data, "%ld %d", &record_file_index, &type);
515 if (record_file_index != rec->FileIndex) {
516 Emsg2(M_ERROR_TERM, 0, "Record header file index %ld not equal record index %ld\n",
517 rec->FileIndex, record_file_index);
521 while (*ap++ != ' ') /* skip record file index */
523 while (*ap++ != ' ') /* skip type */
525 /* Save filename and position to attributes */
530 *fp = *ap++; /* terminate filename & point to attribs */
532 decode_stat(ap, &statp);
533 /* Skip to link name */
536 if (file_is_included(&ff, fname) && !file_is_excluded(&ff, fname)) {
537 print_ls_output(fname, ap, type, &statp);
544 printf("%u files found.\n", num_files);
550 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
553 memset(sessrec, 0, sizeof(sessrec));
554 switch (rec->FileIndex) {
556 rtype = "Fresh Volume Label";
559 rtype = "Volume Label";
560 unser_volume_label(dev, rec);
563 rtype = "Begin Session";
564 unser_session_label(sessrec, rec);
567 rtype = "End Session";
570 rtype = "End of Media";
576 Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
577 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
581 /* Dummies to replace askdir.c */
582 int dir_get_volume_info(JCR *jcr) { return 1;}
583 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
584 int dir_update_volume_info(JCR *jcr, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
585 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
586 int dir_ask_sysop_to_mount_next_volume(JCR *jcr, DEVICE *dev) { return 1; }
587 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
588 int dir_send_job_status(JCR *jcr) {return 1;}
591 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
593 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
594 jcr->VolumeName, dev_name(dev));