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