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