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 int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec);
45 static int dump_label = FALSE;
46 static int list_blocks = FALSE;
47 static int list_jobs = FALSE;
48 static DEV_RECORD *rec;
49 static DEV_BLOCK *block;
51 static SESSION_LABEL sessrec;
52 static uint32_t num_files = 0;
55 #define CONFIG_FILE "bacula-sd.conf"
57 bool forge_on = false;
62 static BSR *bsr = NULL;
67 "\nVersion: " VERSION " (" BDATE ")\n\n"
68 "Usage: bls [-d debug_level] <physical-device-name>\n"
69 " -b <file> specify a bootstrap file\n"
70 " -c <file> specify a config file\n"
71 " -d <level> specify debug level\n"
72 " -e <file> exclude list\n"
73 " -i <file> include list\n"
76 " (no j or k option) list saved files\n"
78 " -p proceed inspite of errors\n"
80 " -V specify Volume names (separated by |)\n"
81 " -? print this message\n\n");
86 int main (int argc, char *argv[])
91 char *VolumeName= NULL;
93 bool ignore_label_errors = false;
95 working_directory = "/tmp";
96 my_name_is(argc, argv, "bls");
97 init_msg(NULL, NULL); /* initialize message handler */
99 memset(&ff, 0, sizeof(ff));
100 init_include_exclude_files(&ff);
102 while ((ch = getopt(argc, argv, "b:c:d:e:i:jkLpvV:?")) != -1) {
108 case 'c': /* specify config file */
109 if (configfile != NULL) {
112 configfile = bstrdup(optarg);
115 case 'd': /* debug level */
116 debug_level = atoi(optarg);
117 if (debug_level <= 0)
121 case 'e': /* exclude list */
122 if ((fd = fopen(optarg, "r")) == NULL) {
123 Pmsg2(0, _("Could not open exclude file: %s, ERR=%s\n"),
124 optarg, strerror(errno));
127 while (fgets(line, sizeof(line), fd) != NULL) {
128 strip_trailing_junk(line);
129 Dmsg1(100, "add_exclude %s\n", line);
130 add_fname_to_exclude_list(&ff, line);
135 case 'i': /* include list */
136 if ((fd = fopen(optarg, "r")) == NULL) {
137 Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
138 optarg, strerror(errno));
141 while (fgets(line, sizeof(line), fd) != NULL) {
142 strip_trailing_junk(line);
143 Dmsg1(100, "add_include %s\n", line);
144 add_fname_to_include_list(&ff, 0, line);
162 ignore_label_errors = true;
170 case 'V': /* Volume name */
184 Pmsg0(0, _("No archive name specified\n"));
188 if (configfile == NULL) {
189 configfile = bstrdup(CONFIG_FILE);
192 parse_config(configfile);
194 if (ff.included_files_list == NULL) {
195 add_fname_to_include_list(&ff, 0, "/");
198 for (i=0; i < argc; i++) {
200 bsr = parse_bsr(NULL, bsrName);
202 jcr = setup_jcr("bls", argv[i], bsr, VolumeName);
203 jcr->ignore_label_errors = ignore_label_errors;
204 dev = setup_to_access_device(jcr, 1); /* acquire for read */
209 block = new_block(dev);
212 * Assume that we have already read the volume label.
213 * If on second or subsequent volume, adjust buffer pointer
215 if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
217 Warning, this Volume is a continuation of Volume %s\n",
218 dev->VolHdr.PrevVolName);
223 } else if (list_jobs) {
237 static void do_close(JCR *jcr)
248 /* List just block information */
249 static void do_blocks(char *infname)
252 dump_volume_label(dev);
256 if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
257 Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
258 if (dev->state & ST_EOT) {
259 if (!mount_next_read_volume(jcr, dev, block)) {
260 Jmsg(jcr, M_INFO, 0, _("Got EOM at file %u on device %s, Volume \"%s\"\n"),
261 dev->file, dev_name(dev), jcr->VolumeName);
264 /* Read and discard Volume label */
266 record = new_record();
267 read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK);
268 read_record_from_block(block, record);
269 get_session_record(dev, record, &sessrec);
271 Jmsg(jcr, M_INFO, 0, _("Mounted Volume \"%s\".\n"), jcr->VolumeName);
273 } else if (dev->state & ST_EOF) {
274 Jmsg(jcr, M_INFO, 0, _("Got EOF at file %u on device %s, Volume \"%s\"\n"),
275 dev->file, dev_name(dev), jcr->VolumeName);
276 Dmsg0(20, "read_record got eof. try again\n");
278 } else if (dev->state & ST_SHORT) {
279 Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
283 display_tape_error_status(jcr, dev);
287 if (!match_bsr_block(bsr, block)) {
288 Dmsg5(100, "reject Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
289 block->BlockNumber, block->block_len, block->BlockVer,
290 block->VolSessionId, block->VolSessionTime);
293 Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
294 block->BlockNumber, block->block_len, block->BlockVer,
295 block->VolSessionId, block->VolSessionTime);
297 read_record_from_block(block, rec);
298 Pmsg7(-1, "Block: %u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
299 block->BlockNumber, block->block_len,
300 FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
301 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
303 } else if (verbose > 1) {
304 dump_block(block, "");
306 printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
314 * We are only looking for labels or in particular Job Session records
316 static int jobs_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
318 if (rec->FileIndex < 0) {
319 dump_label_record(dev, rec, verbose);
325 /* Do list job records */
326 static void do_jobs(char *infname)
328 read_records(jcr, dev, jobs_cb, mount_next_read_volume);
331 /* Do an ls type listing of an archive */
332 static void do_ls(char *infname)
335 dump_volume_label(dev);
338 read_records(jcr, dev, record_cb, mount_next_read_volume);
339 printf("%u files found.\n", num_files);
343 * Called here for each record from read_records()
345 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
347 if (rec->FileIndex < 0) {
348 get_session_record(dev, rec, &sessrec);
351 /* File Attributes stream */
352 if (rec->Stream == STREAM_UNIX_ATTRIBUTES ||
353 rec->Stream == STREAM_UNIX_ATTRIBUTES_EX) {
355 if (!unpack_attributes_record(jcr, rec->Stream, rec->data, attr)) {
356 Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
359 if (attr->file_index != rec->FileIndex) {
360 Emsg2(M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
361 rec->FileIndex, attr->file_index);
364 attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
365 build_attr_output_fnames(jcr, attr);
367 if (file_is_included(&ff, attr->fname) && !file_is_excluded(&ff, attr->fname)) {
368 print_ls_output(jcr, attr);
376 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
379 memset(sessrec, 0, sizeof(sessrec));
380 switch (rec->FileIndex) {
382 rtype = "Fresh Volume Label";
385 rtype = "Volume Label";
386 unser_volume_label(dev, rec);
389 rtype = "Begin Session";
390 unser_session_label(sessrec, rec);
393 rtype = "End Session";
396 rtype = "End of Medium";
402 Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
403 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
407 /* Dummies to replace askdir.c */
408 int dir_get_volume_info(JCR *jcr, enum get_vol_info_rw writing) { return 1;}
409 int dir_find_next_appendable_volume(JCR *jcr) { return 1;}
410 int dir_update_volume_info(JCR *jcr, DEVICE *dev, int relabel) { return 1; }
411 int dir_create_jobmedia_record(JCR *jcr) { return 1; }
412 int dir_ask_sysop_to_create_appendable_volume(JCR *jcr, DEVICE *dev) { return 1; }
413 int dir_update_file_attributes(JCR *jcr, DEV_RECORD *rec) { return 1;}
414 int dir_send_job_status(JCR *jcr) {return 1;}
417 int dir_ask_sysop_to_mount_volume(JCR *jcr, DEVICE *dev)
419 fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
420 jcr->VolumeName, dev_name(dev));