3 * Dumb program to do an "ls" of a Bacula 1.0 mortal file.
8 Copyright (C) 2000-2004 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 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
37 static void do_blocks(char *infname);
38 static void do_jobs(char *infname);
39 static void do_ls(char *fname);
40 static void do_close(JCR *jcr);
41 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
42 static bool record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
46 static bool dump_label = false;
47 static bool list_blocks = false;
48 static bool list_jobs = false;
49 static DEV_RECORD *rec;
50 static DEV_BLOCK *block;
52 static SESSION_LABEL sessrec;
53 static uint32_t num_files = 0;
56 #define CONFIG_FILE "bacula-sd.conf"
58 bool forge_on = false;
63 static BSR *bsr = NULL;
68 "Copyright (C) 2000-2004 Kern Sibbald and John Walker.\n"
69 "\nVersion: " VERSION " (" BDATE ")\n\n"
70 "Usage: bls [-d debug_level] <physical-device-name>\n"
71 " -b <file> specify a bootstrap file\n"
72 " -c <file> specify a config file\n"
73 " -d <level> specify debug level\n"
74 " -e <file> exclude list\n"
75 " -i <file> include list\n"
78 " (no j or k option) list saved files\n"
80 " -p proceed inspite of errors\n"
82 " -V specify Volume names (separated by |)\n"
83 " -? print this message\n\n");
88 int main (int argc, char *argv[])
93 char *VolumeName= NULL;
95 bool ignore_label_errors = false;
97 working_directory = "/tmp";
98 my_name_is(argc, argv, "bls");
99 init_msg(NULL, NULL); /* initialize message handler */
101 memset(&ff, 0, sizeof(ff));
102 init_include_exclude_files(&ff);
104 while ((ch = getopt(argc, argv, "b:c:d:e:i:jkLpvV:?")) != -1) {
110 case 'c': /* specify config file */
111 if (configfile != NULL) {
114 configfile = bstrdup(optarg);
117 case 'd': /* debug level */
118 debug_level = atoi(optarg);
119 if (debug_level <= 0)
123 case 'e': /* exclude list */
124 if ((fd = fopen(optarg, "r")) == NULL) {
125 Pmsg2(0, _("Could not open exclude file: %s, ERR=%s\n"),
126 optarg, strerror(errno));
129 while (fgets(line, sizeof(line), fd) != NULL) {
130 strip_trailing_junk(line);
131 Dmsg1(100, "add_exclude %s\n", line);
132 add_fname_to_exclude_list(&ff, line);
137 case 'i': /* include list */
138 if ((fd = fopen(optarg, "r")) == NULL) {
139 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
140 optarg, strerror(errno));
143 while (fgets(line, sizeof(line), fd) != NULL) {
144 strip_trailing_junk(line);
145 Dmsg1(100, "add_include %s\n", line);
146 add_fname_to_include_list(&ff, 0, line);
164 ignore_label_errors = true;
172 case 'V': /* Volume name */
186 Pmsg0(0, _("No archive name specified\n"));
190 if (configfile == NULL) {
191 configfile = bstrdup(CONFIG_FILE);
194 parse_config(configfile);
196 if (ff.included_files_list == NULL) {
197 add_fname_to_include_list(&ff, 0, "/");
200 for (i=0; i < argc; i++) {
202 bsr = parse_bsr(NULL, bsrName);
204 jcr = setup_jcr("bls", argv[i], bsr, VolumeName);
205 jcr->ignore_label_errors = ignore_label_errors;
206 dev = setup_to_access_device(jcr, 1); /* acquire for read */
212 block = new_block(dev);
215 * Assume that we have already read the volume label.
216 * If on second or subsequent volume, adjust buffer pointer
218 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
220 Warning, this Volume is a continuation of Volume %s\n",
221 dev->VolHdr.PrevVolName);
226 } else if (list_jobs) {
240 static void do_close(JCR *jcr)
251 /* List just block information */
252 static void do_blocks(char *infname)
255 dump_volume_label(dev);
259 if (!read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK)) {
260 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
261 if (dev->state & ST_EOT) {
262 if (!mount_next_read_volume(jcr, dev, block)) {
263 Jmsg(jcr, M_INFO, 0, _("Got EOM at file %u on device %s, Volume \"%s\"\n"),
264 dev->file, dev_name(dev), jcr->VolumeName);
267 /* Read and discard Volume label */
269 record = new_record();
270 read_block_from_device(dcr, block, NO_BLOCK_NUMBER_CHECK);
271 read_record_from_block(block, record);
272 get_session_record(dev, record, &sessrec);
274 Jmsg(jcr, M_INFO, 0, _("Mounted Volume \"%s\".\n"), jcr->VolumeName);
276 } else if (dev->state & ST_EOF) {
277 Jmsg(jcr, M_INFO, 0, _("Got EOF at file %u on device %s, Volume \"%s\"\n"),
278 dev->file, dev_name(dev), jcr->VolumeName);
279 Dmsg0(20, "read_record got eof. try again\n");
281 } else if (dev->state & ST_SHORT) {
282 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
286 display_tape_error_status(jcr, dev);
290 if (!match_bsr_block(bsr, block)) {
291 Dmsg5(100, "reject Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
292 block->BlockNumber, block->block_len, block->BlockVer,
293 block->VolSessionId, block->VolSessionTime);
296 Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
297 block->BlockNumber, block->block_len, block->BlockVer,
298 block->VolSessionId, block->VolSessionTime);
300 read_record_from_block(block, rec);
301 Pmsg9(-1, "File:blk=%u:%u blk_num=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
302 dev->file, dev->block_num,
303 block->BlockNumber, block->block_len,
304 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
305 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
307 } else if (verbose > 1) {
308 dump_block(block, "");
310 printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
318 * We are only looking for labels or in particular Job Session records
320 static bool jobs_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
322 if (rec->FileIndex < 0) {
323 dump_label_record(dev, rec, verbose);
329 /* Do list job records */
330 static void do_jobs(char *infname)
332 read_records(dcr, jobs_cb, mount_next_read_volume);
335 /* Do an ls type listing of an archive */
336 static void do_ls(char *infname)
339 dump_volume_label(dev);
342 read_records(dcr, record_cb, mount_next_read_volume);
343 printf("%u files found.\n", num_files);
347 * Called here for each record from read_records()
349 static bool record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
351 if (rec->FileIndex < 0) {
352 get_session_record(dev, rec, &sessrec);
355 /* File Attributes stream */
356 if (rec->Stream == STREAM_UNIX_ATTRIBUTES ||
357 rec->Stream == STREAM_UNIX_ATTRIBUTES_EX) {
360 const char *rtype = "Attributes";
361 Pmsg5(-1, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
362 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
364 if (!unpack_attributes_record(jcr, rec->Stream, rec->data, attr)) {
366 Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
372 if (attr->file_index != rec->FileIndex) {
373 Emsg2(forge_on?M_WARNING:M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
374 rec->FileIndex, attr->file_index);
377 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
378 build_attr_output_fnames(jcr, attr);
380 if (file_is_included(&ff, attr->fname) && !file_is_excluded(&ff, attr->fname)) {
381 print_ls_output(jcr, attr);
389 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
392 memset(sessrec, 0, sizeof(sessrec));
393 switch (rec->FileIndex) {
395 rtype = "Fresh Volume Label";
398 rtype = "Volume Label";
399 unser_volume_label(dev, rec);
402 rtype = "Begin Job Session";
403 unser_session_label(sessrec, rec);
406 rtype = "End Job Session";
409 rtype = "End of Medium";
415 Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
416 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
418 Pmsg5(-1, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
419 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
424 /* Dummies to replace askdir.c */
425 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw writing) { return 1;}
426 bool dir_find_next_appendable_volume(DCR *dcr) { return 1;}
427 bool dir_update_volume_info(DCR *dcr, bool relabel) { return 1; }
428 bool dir_create_jobmedia_record(DCR *dcr) { return 1; }
429 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr) { return 1; }
430 bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
431 bool dir_send_job_status(JCR *jcr) {return 1;}
434 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
437 DEVICE *dev = dcr->dev;
438 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
439 jcr->VolumeName, dev_name(dev));