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