]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/bls.c
Update config + write own fgets() + ...
[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 void 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 static int non_support_data = 0;
56
57 #define CONFIG_FILE "bacula-sd.conf"
58 char *configfile;
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 "    (none of above)    list saved files\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
93    working_directory = "/tmp";
94    my_name_is(argc, argv, "bls");
95    init_msg(NULL, NULL);              /* initialize message handler */
96
97    memset(&ff, 0, sizeof(ff));
98    init_include_exclude_files(&ff);
99
100    while ((ch = getopt(argc, argv, "b:c:d:e:i:jkLtvV:?")) != -1) {
101       switch (ch) {
102       case 'b':
103          bsrName = optarg;
104          break;
105
106       case 'c':                    /* specify config file */
107          if (configfile != NULL) {
108             free(configfile);
109          }
110          configfile = bstrdup(optarg);
111          break;
112
113       case 'd':                    /* debug level */
114          debug_level = atoi(optarg);
115          if (debug_level <= 0)
116             debug_level = 1; 
117          break;
118
119       case 'e':                    /* exclude list */
120          if ((fd = fopen(optarg, "r")) == NULL) {
121             Pmsg2(0, _("Could not open exclude file: %s, ERR=%s\n"),
122                optarg, strerror(errno));
123             exit(1);
124          }
125          while (fgets(line, sizeof(line), fd) != NULL) {
126             strip_trailing_junk(line);
127             Dmsg1(100, "add_exclude %s\n", line);
128             add_fname_to_exclude_list(&ff, line);
129          }
130          fclose(fd);
131          break;
132
133       case 'i':                    /* include list */
134          if ((fd = fopen(optarg, "r")) == NULL) {
135             Pmsg2(0, "Could not open include file: %s, ERR=%s\n",
136                optarg, strerror(errno));
137             exit(1);
138          }
139          while (fgets(line, sizeof(line), fd) != NULL) {
140             strip_trailing_junk(line);
141             Dmsg1(100, "add_include %s\n", line);
142             add_fname_to_include_list(&ff, 0, line);
143          }
144          fclose(fd);
145          break;
146
147       case 'j':
148          list_jobs = TRUE;
149          break;
150
151       case 'k':
152          list_blocks = TRUE;
153          break;
154
155       case 'L':
156          dump_label = TRUE;
157          break;
158
159       case 'v':
160          verbose++;
161          break;
162
163       case 'V':                    /* Volume name */
164          VolumeName = optarg;
165          break;
166
167       case '?':
168       default:
169          usage();
170
171       } /* end switch */
172    } /* end while */
173    argc -= optind;
174    argv += optind;
175
176    if (!argc) {
177       Pmsg0(0, _("No archive name specified\n"));
178       usage();
179    }
180
181    if (configfile == NULL) {
182       configfile = bstrdup(CONFIG_FILE);
183    }
184
185    parse_config(configfile);
186
187    if (ff.included_files_list == NULL) {
188       add_fname_to_include_list(&ff, 0, "/");
189    }
190
191    for (i=0; i < argc; i++) {
192       if (bsrName) {
193          bsr = parse_bsr(NULL, bsrName);
194       }
195       jcr = setup_jcr("bls", argv[i], bsr, VolumeName);
196       dev = setup_to_access_device(jcr, 1);   /* acquire for read */
197       if (!dev) {
198          exit(1);
199       }
200       rec = new_record();
201       block = new_block(dev);
202       attr = new_attr();
203       /*
204        * Assume that we have already read the volume label.
205        * If on second or subsequent volume, adjust buffer pointer 
206        */
207       if (dev->VolHdr.PrevVolName[0] != 0) { /* second volume */
208          Pmsg1(0, "\n\
209 Warning, this Volume is a continuation of Volume %s\n",
210                 dev->VolHdr.PrevVolName);
211       }
212
213       if (list_blocks) {
214          do_blocks(argv[i]);
215       } else if (list_jobs) {
216          do_jobs(argv[i]);
217       } else {
218          do_ls(argv[i]);
219       }
220       do_close(jcr);
221    }
222    if (bsr) {
223       free_bsr(bsr);
224    }
225    return 0;
226 }
227
228
229 static void do_close(JCR *jcr)
230 {
231    release_device(jcr, dev);
232    free_attr(attr);
233    term_dev(dev);
234    free_record(rec);
235    free_block(block);
236    free_jcr(jcr);
237 }
238
239
240 /* List just block information */
241 static void do_blocks(char *infname)
242 {
243    if (verbose) {
244       dump_volume_label(dev);
245       rec = new_record();
246    }
247    for ( ;; ) {
248       if (!read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) {
249          Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
250          if (dev->state & ST_EOT) {
251             if (!mount_next_read_volume(jcr, dev, block)) {
252                Jmsg(jcr, M_INFO, 0, _("Got EOM at file %u on device %s, Volume \"%s\"\n"), 
253                   dev->file, dev_name(dev), jcr->VolumeName);
254                break;
255             }
256             /* Read and discard Volume label */
257             DEV_RECORD *record;
258             record = new_record();
259             read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK);
260             read_record_from_block(block, record);
261             get_session_record(dev, record, &sessrec);
262             free_record(record);
263             Jmsg(jcr, M_INFO, 0, _("Mounted Volume \"%s\".\n"), jcr->VolumeName);
264             
265          } else if (dev->state & ST_EOF) {
266             Jmsg(jcr, M_INFO, 0, _("Got EOF at file %u on device %s, Volume \"%s\"\n"), 
267                dev->file, dev_name(dev), jcr->VolumeName);
268             Dmsg0(20, "read_record got eof. try again\n");
269             continue;
270          } else if (dev->state & ST_SHORT) {
271             Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
272             continue;
273          } else {
274             /* I/O error */
275             display_tape_error_status(jcr, dev);
276             break;
277          }
278       }
279       if (!match_bsr_block(bsr, block)) {
280          Dmsg5(100, "reject Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
281             block->BlockNumber, block->block_len, block->BlockVer,
282             block->VolSessionId, block->VolSessionTime);
283          continue;
284       }
285       Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
286          block->BlockNumber, block->block_len, block->BlockVer,
287          block->VolSessionId, block->VolSessionTime);
288       if (verbose == 1) {
289          read_record_from_block(block, rec);
290          Pmsg7(-1, "Block: %u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
291               block->BlockNumber, block->block_len,
292               FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
293               stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
294          rec->remainder = 0;
295       } else if (verbose > 1) {
296          dump_block(block, "");
297       } else {
298          printf("Block: %d size=%d\n", block->BlockNumber, block->block_len);
299       }
300
301    }
302    return;
303 }
304
305 /*
306  * We are only looking for labels or in particula Job Session records
307  */
308 static void jobs_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
309 {
310    if (rec->FileIndex < 0) {
311       dump_label_record(dev, rec, verbose);
312    }
313    rec->remainder = 0;
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 void 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;
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       if (!is_stream_supported(attr->data_stream)) {
357          if (!non_support_data++) {
358             Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
359                stream_to_ascii(attr->data_stream));
360          }
361          return;
362       }
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;
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, VOLUME_CAT_INFO *vol, int relabel) { return 1; }
409 int     dir_create_jobmedia_record(JCR *jcr) { return 1; }
410 int     dir_ask_sysop_to_mount_next_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 }