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