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