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