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