]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bls.c
Remove debug EndFile debug code
[bacula/bacula] / bacula / src / stored / bls.c
1 /*
2  *
3  *  Dumb program to do an "ls" of a Bacula 1.0 mortal file.
4  *
5  *   Version $Id$
6  */
7 /*
8    Copyright (C) 2000-2004 Kern Sibbald and John Walker
9
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.
14
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.
19
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,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29 #include "findlib/find.h"
30
31 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
32 int win32_client = 1;
33 #else
34 int win32_client = 0;
35 #endif
36
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(DCR *dcr, DEV_RECORD *rec);
43
44 static DEVICE *dev;
45 static DCR *dcr;
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;
51 static JCR *jcr;
52 static SESSION_LABEL sessrec;
53 static uint32_t num_files = 0;
54 static ATTR *attr;
55
56 #define CONFIG_FILE "bacula-sd.conf"
57 char *configfile;
58 bool forge_on = false;
59
60
61 static FF_PKT ff;
62
63 static BSR *bsr = NULL;
64
65 static void usage()
66 {
67    fprintf(stderr,
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"
76 "       -j              list jobs\n"
77 "       -k              list blocks\n"
78 "    (no j or k option) list saved files\n"
79 "       -L              dump label\n"
80 "       -p              proceed inspite of errors\n"
81 "       -v              be verbose\n"
82 "       -V              specify Volume names (separated by |)\n"
83 "       -?              print this message\n\n");
84    exit(1);
85 }
86
87
88 int main (int argc, char *argv[])
89 {
90    int i, ch;
91    FILE *fd;
92    char line[1000];
93    char *VolumeName= NULL;
94    char *bsrName = NULL;
95    bool ignore_label_errors = false;
96
97    working_directory = "/tmp";
98    my_name_is(argc, argv, "bls");
99    init_msg(NULL, NULL);              /* initialize message handler */
100
101    memset(&ff, 0, sizeof(ff));
102    init_include_exclude_files(&ff);
103
104    while ((ch = getopt(argc, argv, "b:c:d:e:i:jkLpvV:?")) != -1) {
105       switch (ch) {
106       case 'b':
107          bsrName = optarg;
108          break;
109
110       case 'c':                    /* specify config file */
111          if (configfile != NULL) {
112             free(configfile);
113          }
114          configfile = bstrdup(optarg);
115          break;
116
117       case 'd':                    /* debug level */
118          debug_level = atoi(optarg);
119          if (debug_level <= 0)
120             debug_level = 1; 
121          break;
122
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));
127             exit(1);
128          }
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);
133          }
134          fclose(fd);
135          break;
136
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));
141             exit(1);
142          }
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);
147          }
148          fclose(fd);
149          break;
150
151       case 'j':
152          list_jobs = true;
153          break;
154
155       case 'k':
156          list_blocks = true;
157          break;
158
159       case 'L':
160          dump_label = true;
161          break;
162
163       case 'p':
164          ignore_label_errors = true;
165          forge_on = true;
166          break;
167
168       case 'v':
169          verbose++;
170          break;
171
172       case 'V':                    /* Volume name */
173          VolumeName = optarg;
174          break;
175
176       case '?':
177       default:
178          usage();
179
180       } /* end switch */
181    } /* end while */
182    argc -= optind;
183    argv += optind;
184
185    if (!argc) {
186       Pmsg0(0, _("No archive name specified\n"));
187       usage();
188    }
189
190    if (configfile == NULL) {
191       configfile = bstrdup(CONFIG_FILE);
192    }
193
194    parse_config(configfile);
195
196    if (ff.included_files_list == NULL) {
197       add_fname_to_include_list(&ff, 0, "/");
198    }
199
200    for (i=0; i < argc; i++) {
201       if (bsrName) {
202          bsr = parse_bsr(NULL, bsrName);
203       }
204       jcr = setup_jcr("bls", argv[i], bsr, VolumeName, 1); /* acquire for read */ 
205       if (!jcr) {
206          exit(1);
207       }
208       jcr->ignore_label_errors = ignore_label_errors;
209       dev = jcr->dcr->dev;
210       if (!dev) {
211          exit(1);
212       }
213       dcr = jcr->dcr;
214       rec = new_record();
215       block = new_block(dev);
216       attr = new_attr();
217       /*
218        * Assume that we have already read the volume label.
219        * If on second or subsequent volume, adjust buffer pointer 
220        */
221       if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
222          Pmsg1(0, "\n\
223 Warning, this Volume is a continuation of Volume %s\n",
224                 dev->VolHdr.PrevVolName);
225       }
226
227       if (list_blocks) {
228          do_blocks(argv[i]);
229       } else if (list_jobs) {
230          do_jobs(argv[i]);
231       } else {
232          do_ls(argv[i]);
233       }
234       do_close(jcr);
235    }
236    if (bsr) {
237       free_bsr(bsr);
238    }
239    return 0;
240 }
241
242
243 static void do_close(JCR *jcr)
244 {
245    release_device(jcr);
246    free_attr(attr);
247    free_record(rec);
248    free_block(block);
249    free_jcr(jcr);
250    term_dev(dev);
251 }
252
253
254 /* List just block information */
255 static void do_blocks(char *infname)
256 {
257    if (verbose) {
258       dump_volume_label(dev);
259       rec = new_record();
260    }
261    for ( ;; ) {
262       if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
263          Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
264          if (dev->state & ST_EOT) {
265             if (!mount_next_read_volume(dcr)) {
266                Jmsg(jcr, M_INFO, 0, _("Got EOM at file %u on device %s, Volume \"%s\"\n"), 
267                   dev->file, dev_name(dev), dcr->VolumeName);
268                break;
269             }
270             /* Read and discard Volume label */
271             DEV_RECORD *record;
272             record = new_record();
273             read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK);
274             read_record_from_block(block, record);
275             get_session_record(dev, record, &sessrec);
276             free_record(record);
277             Jmsg(jcr, M_INFO, 0, _("Mounted Volume \"%s\".\n"), dcr->VolumeName);
278             
279          } else if (dev->state & ST_EOF) {
280             Jmsg(jcr, M_INFO, 0, _("Got EOF at file %u on device %s, Volume \"%s\"\n"), 
281                dev->file, dev_name(dev), dcr->VolumeName);
282             Dmsg0(20, "read_record got eof. try again\n");
283             continue;
284          } else if (dev->state & ST_SHORT) {
285             Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
286             continue;
287          } else {
288             /* I/O error */
289             display_tape_error_status(jcr, dev);
290             break;
291          }
292       }
293       if (!match_bsr_block(bsr, block)) {
294          Dmsg5(100, "reject Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
295             block->BlockNumber, block->block_len, block->BlockVer,
296             block->VolSessionId, block->VolSessionTime);
297          continue;
298       }
299       Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
300         block->BlockNumber, block->block_len, block->BlockVer,
301         block->VolSessionId, block->VolSessionTime);
302       if (verbose == 1) {
303          read_record_from_block(block, rec);
304          Pmsg9(-1, "File:blk=%u:%u blk_num=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
305               dev->file, dev->block_num,
306               block->BlockNumber, block->block_len,
307               FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
308               stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
309          rec->remainder = 0;
310       } else if (verbose > 1) {
311          dump_block(block, "");
312       } else {
313          printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
314       }
315
316    }
317    return;
318 }
319
320 /*
321  * We are only looking for labels or in particular Job Session records
322  */
323 static bool jobs_cb(DCR *dcr, DEV_RECORD *rec)
324 {
325    if (rec->FileIndex < 0) {
326       dump_label_record(dcr->dev, rec, verbose);
327    }
328    rec->remainder = 0;
329    return true;
330 }
331
332 /* Do list job records */
333 static void do_jobs(char *infname)
334 {
335    read_records(dcr, jobs_cb, mount_next_read_volume);
336 }
337
338 /* Do an ls type listing of an archive */
339 static void do_ls(char *infname)
340 {
341    if (dump_label) {
342       dump_volume_label(dev);
343       return;
344    }
345    read_records(dcr, record_cb, mount_next_read_volume);
346    printf("%u files found.\n", num_files);
347 }
348
349 /*
350  * Called here for each record from read_records()
351  */
352 static bool record_cb(DCR *dcr, DEV_RECORD *rec)
353 {
354    if (rec->FileIndex < 0) {
355       get_session_record(dev, rec, &sessrec);
356       return true;
357    }
358    /* File Attributes stream */
359    if (rec->Stream == STREAM_UNIX_ATTRIBUTES || 
360        rec->Stream == STREAM_UNIX_ATTRIBUTES_EX) {
361
362       if (verbose > 1) {
363          const char *rtype = "Attributes";
364          Pmsg5(-1, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
365                rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
366       }
367       if (!unpack_attributes_record(jcr, rec->Stream, rec->data, attr)) {
368          if (!forge_on) {
369             Emsg0(M_ERROR_TERM, 0, _("Cannot continue.\n"));
370          }
371          num_files++;
372          return true;
373       }
374
375       if (attr->file_index != rec->FileIndex) {
376          Emsg2(forge_on?M_WARNING:M_ERROR_TERM, 0, _("Record header file index %ld not equal record index %ld\n"),
377                rec->FileIndex, attr->file_index);
378       }
379
380       attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
381       build_attr_output_fnames(jcr, attr);
382
383       if (file_is_included(&ff, attr->fname) && !file_is_excluded(&ff, attr->fname)) {
384          print_ls_output(jcr, attr);
385          num_files++;
386       }
387    }
388    return true;
389 }
390
391
392 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
393 {
394    const char *rtype;
395    memset(sessrec, 0, sizeof(sessrec));
396    switch (rec->FileIndex) {
397    case PRE_LABEL:
398       rtype = "Fresh Volume Label";   
399       break;
400    case VOL_LABEL:
401       rtype = "Volume Label";
402       unser_volume_label(dev, rec);
403       break;
404    case SOS_LABEL:
405       rtype = "Begin Job Session";
406       unser_session_label(sessrec, rec);
407       break;
408    case EOS_LABEL:
409       rtype = "End Job Session";
410       break;
411    case EOM_LABEL:
412       rtype = "End of Medium";
413       break;
414    default:
415       rtype = "Unknown";
416       break;
417    }
418    Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
419          rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
420    if (verbose) {
421       Pmsg5(-1, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
422             rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
423    }
424 }
425
426
427 /* Dummies to replace askdir.c */
428 bool    dir_get_volume_info(DCR *dcr, enum get_vol_info_rw  writing) { return 1;}
429 bool    dir_find_next_appendable_volume(DCR *dcr) { return 1;}
430 bool    dir_update_volume_info(DCR *dcr, bool relabel) { return 1; }
431 bool    dir_create_jobmedia_record(DCR *dcr) { return 1; }
432 bool    dir_ask_sysop_to_create_appendable_volume(DCR *dcr) { return 1; }
433 bool    dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
434 bool    dir_send_job_status(JCR *jcr) {return 1;}
435
436
437 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
438 {
439    DEVICE *dev = dcr->dev;
440    fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
441       dcr->VolumeName, dev_name(dev));
442    getchar();   
443    return true;
444 }