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");
77 int main (int argc, char *argv[])
83 my_name_is(argc, argv, "bls");
84 init_msg(NULL, NULL); /* initialize message handler */
86 memset(&ff, 0, sizeof(ff));
87 init_include_exclude_files(&ff);
89 while ((ch = getopt(argc, argv, "b:d:e:i:jkLtv?")) != -1) {
92 bsr = parse_bsr(NULL, optarg);
95 case 'd': /* debug level */
96 debug_level = atoi(optarg);
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));
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);
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));
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);
158 if (!argc && !default_tape) {
159 Pmsg0(0, "No archive name specified\n");
163 if (ff.included_files_list == NULL) {
164 add_fname_to_include_list(&ff, 0, "/");
167 /* Try default device */
169 do_ls(DEFAULT_TAPE_DRIVE);
173 for (i=0; i < argc; i++) {
177 } else if (list_jobs) {
190 static void my_free_jcr(JCR *jcr)
196 * Setup device, jcr, and prepare to read
198 static void do_setup(char *infname)
200 jcr = new_jcr(sizeof(JCR), my_free_jcr);
203 if (strncmp(infname, "/dev/", 5) != 0) {
204 /* Try stripping file part */
205 p = infname + strlen(infname);
206 while (p >= infname && *p != '/')
209 strcpy(VolName, p+1);
213 Dmsg2(10, "Device=%s, Vol=%s.\n", infname, VolName);
214 dev = init_dev(NULL, infname);
216 Emsg1(M_FATAL, 0, "Cannot open %s\n", infname);
219 /* ***FIXME**** init capabilities */
220 if (!open_device(dev)) {
221 Emsg1(M_FATAL, 0, "Cannot open %s\n", infname);
224 Dmsg0(90, "Device opened for read.\n");
227 block = new_block(dev);
231 for (p = VolName; p && *p; ) {
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);
247 static void do_close()
255 static int mount_next_volume(char *infname)
257 if (rec->remainder) {
258 Dmsg0(20, "Not end of record. Next volume has more data for current record.\n");
260 Dmsg2(20, "NumVolumes=%d CurVolume=%d\n", NumVolumes, CurVolume);
261 if (NumVolumes > 1 && CurVolume < NumVolumes) {
266 Dmsg1(20, "There is another volume %s.\n", p);
268 jcr->VolumeName = check_pool_memory_size(jcr->VolumeName,
270 strcpy(jcr->VolumeName, VolName);
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);
278 return 1; /* Next volume mounted */
280 printf("End of Device reached.\n");
285 * Device got an error, attempt to analyse it
287 static void display_error_status()
291 Emsg0(M_ERROR, 0, dev->errmsg);
292 status_dev(dev, &status);
293 Dmsg1(20, "Device status: %x\n", status);
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");
305 Emsg2(M_ERROR_TERM, 0, "Read error on Record Header %s: %s\n", dev_name(dev), strerror(errno));
309 /* List just block information */
310 static void do_blocks(char *infname)
313 dump_volume_label(dev);
315 /* Assume that we have already read the volume label.
316 * If on second or subsequent volume, adjust buffer pointer
318 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
320 Warning, this Volume is a continuation of Volume %s\n",
321 dev->VolHdr.PrevVolName);
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)) {
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");
341 if (dev->state & ST_SHORT) {
342 Emsg0(M_INFO, 0, dev->errmsg);
345 display_error_status();
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);
357 printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
364 /* Do list job records */
365 static void do_jobs(char *infname)
368 /* Assume that we have already read the volume label.
369 * If on second or subsequent volume, adjust buffer pointer
371 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
373 Warning, this Volume is a continuation of Volume %s\n",
374 dev->VolHdr.PrevVolName);
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)) {
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");
391 if (dev->state & ST_SHORT) {
392 Pmsg0(000, "Got short block.\n");
393 Emsg0(M_INFO, 0, dev->errmsg);
396 display_error_status();
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),
408 * Check for End of File record (all zeros)
409 * NOTE: this no longer exists
411 if (rec->VolSessionId == 0 && rec->VolSessionTime == 0) {
412 Emsg0(M_ERROR_TERM, 0, "Zero VolSessionId and VolSessionTime. This shouldn't happen\n");
416 * Check for Start or End of Session Record
419 if (rec->FileIndex < 0) {
420 dump_label_record(dev, rec, verbose);
429 /* Do an ls type listing of an archive */
430 static void do_ls(char *infname)
435 long record_file_index;
438 dump_volume_label(dev);
442 /* Assume that we have already read the volume label.
443 * If on second or subsequent volume, adjust buffer pointer
445 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
447 Warning, this Volume is a continuation of Volume %s\n",
448 dev->VolHdr.PrevVolName);
452 SESSION_LABEL sessrec;
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)) {
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");
467 if (dev->state & ST_SHORT) {
468 Emsg0(M_INFO, 0, dev->errmsg);
471 display_error_status();
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),
483 * Check for End of File record (all zeros)
484 * NOTE: this no longer exists
486 if (rec->VolSessionId == 0 && rec->VolSessionTime == 0) {
487 Emsg0(M_ERROR_TERM, 0, "Zero VolSessionId and VolSessionTime. This shouldn't happen\n");
491 * Check for Start or End of Session Record
494 if (rec->FileIndex < 0) {
496 memset(&sessrec, 0, sizeof(sessrec));
497 switch (rec->FileIndex) {
499 rtype = "Fresh Volume Label";
502 rtype = "Volume Label";
503 unser_volume_label(dev, rec);
506 rtype = "Begin Session";
507 unser_session_label(&sessrec, rec);
510 rtype = "End Session";
513 rtype = "End of Media";
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);
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 */
529 continue; /* ignore other labels */
530 } /* end if label record */
535 if (bsr && !match_bsr(bsr, rec, &dev->VolHdr, &sessrec)) {
539 /* File Attributes stream */
540 if (rec->Stream == STREAM_UNIX_ATTRIBUTES) {
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);
549 while (*ap++ != ' ') /* skip record file index */
551 while (*ap++ != ' ') /* skip type */
553 /* Save filename and position to attributes */
558 *fp = *ap++; /* terminate filename & point to attribs */
560 decode_stat(ap, &statp);
561 /* Skip to link name */
564 print_ls_output(fname, ap, type, &statp);
570 extern char *getuser(uid_t uid);
571 extern char *getgroup(gid_t gid);
573 static void print_ls_output(char *fname, char *link, int type, struct stat *statp)
580 if (!file_is_included(&ff, fname) || file_is_excluded(&ff, fname)) {
583 p = encode_mode(statp->st_mode, buf);
584 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
586 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
588 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
590 p = encode_time(statp->st_ctime, p);
594 for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
596 if (type == FT_LNK) {
602 for (f=link; *f && (p-buf) < (int)sizeof(buf); )
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;}
620 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
622 fprintf(stderr, "Mount Volume %s on device %s and press return when ready: ",
623 jcr->VolumeName, dev_name(dev));