]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/parse_bsr.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / stored / parse_bsr.c
1 /*     
2  *   Parse a Bootstrap Records (used for restores) 
3  *  
4  *     Kern Sibbald, June MMII
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2002 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    MA 02111-1307, USA.
25
26  */
27
28
29 #include "bacula.h"
30 #include "stored.h"
31
32 typedef BSR * (ITEM_HANDLER)(LEX *lc, BSR *bsr);
33
34 static BSR *store_vol(LEX *lc, BSR *bsr);
35 static BSR *store_client(LEX *lc, BSR *bsr);
36 static BSR *store_job(LEX *lc, BSR *bsr);
37 static BSR *store_jobid(LEX *lc, BSR *bsr);
38 static BSR *store_count(LEX *lc, BSR *bsr);
39 static BSR *store_jobtype(LEX *lc, BSR *bsr);
40 static BSR *store_joblevel(LEX *lc, BSR *bsr);
41 static BSR *store_findex(LEX *lc, BSR *bsr);
42 static BSR *store_sessid(LEX *lc, BSR *bsr);
43 static BSR *store_volfile(LEX *lc, BSR *bsr);
44 static BSR *store_volblock(LEX *lc, BSR *bsr);
45 static BSR *store_sesstime(LEX *lc, BSR *bsr);
46 static BSR *store_include(LEX *lc, BSR *bsr);
47 static BSR *store_exclude(LEX *lc, BSR *bsr);
48 static BSR *store_stream(LEX *lc, BSR *bsr);
49 static BSR *store_slot(LEX *lc, BSR *bsr);
50 static bool is_fast_rejection_ok(BSR *bsr);
51 static bool is_positioning_ok(BSR *bsr);
52
53 struct kw_items {
54    char *name;
55    ITEM_HANDLER *handler;
56 };
57
58 /*
59  * List of all keywords permitted in bsr files and their handlers
60  */
61 struct kw_items items[] = {
62    {"volume", store_vol},
63    {"client", store_client},
64    {"job", store_job},
65    {"jobid", store_jobid},
66    {"count", store_count},
67    {"fileindex", store_findex},
68    {"jobtype", store_jobtype},
69    {"joblevel", store_joblevel},
70    {"volsessionid", store_sessid},
71    {"volsessiontime", store_sesstime},
72    {"include", store_include},
73    {"exclude", store_exclude},
74    {"volfile", store_volfile},
75    {"volblock", store_volblock},
76    {"stream",  store_stream},
77    {"slot",    store_slot},
78    {NULL, NULL}
79
80 };
81
82 /* 
83  * Create a BSR record
84  */
85 static BSR *new_bsr()
86 {
87    BSR *bsr = (BSR *)malloc(sizeof(BSR));
88    memset(bsr, 0, sizeof(BSR));
89    return bsr;
90 }
91
92 /*
93  * Format a scanner error message 
94  */
95 static void s_err(char *file, int line, LEX *lc, char *msg, ...)
96 {
97    JCR *jcr = (JCR *)(lc->caller_ctx);
98    va_list arg_ptr;
99    char buf[MAXSTRING];
100
101    va_start(arg_ptr, msg);
102    bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
103    va_end(arg_ptr);
104      
105    if (jcr) {
106       Jmsg(jcr, M_FATAL, 0, _("Bootstrap file error: %s\n\
107             : Line %d, col %d of file %s\n%s\n"),
108          buf, lc->line_no, lc->col_no, lc->fname, lc->line);
109    } else {
110       e_msg(file, line, M_FATAL, 0, _("Bootstrap file error: %s\n\
111             : Line %d, col %d of file %s\n%s\n"),
112          buf, lc->line_no, lc->col_no, lc->fname, lc->line);
113    }
114 }
115
116
117 /*********************************************************************
118  *
119  *      Parse Bootstrap file
120  *
121  */
122 BSR *parse_bsr(JCR *jcr, char *fname)
123 {
124    LEX *lc = NULL;
125    int token, i;
126    BSR *root_bsr = new_bsr();
127    BSR *bsr = root_bsr;
128      
129    Dmsg1(200, "Enter parse_bsf %s\n", fname);
130    lc = lex_open_file(lc, fname, s_err);
131    lc->caller_ctx = (void *)jcr;
132    while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
133       Dmsg1(200, "parse got token=%s\n", lex_tok_to_str(token));
134       if (token == T_EOL) {
135          continue;
136       }
137       for (i=0; items[i].name; i++) {
138          if (strcasecmp(items[i].name, lc->str) == 0) {
139             token = lex_get_token(lc, T_ALL);
140             Dmsg1 (200, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
141             if (token != T_EQUALS) {
142                scan_err1(lc, "expected an equals, got: %s", lc->str);
143                bsr = NULL;
144                break;
145             }
146             Dmsg1(200, "calling handler for %s\n", items[i].name);
147             /* Call item handler */
148             bsr = items[i].handler(lc, bsr);
149             i = -1;
150             break;
151          }
152       }
153       if (i >= 0) {
154          Dmsg1(200, "Keyword = %s\n", lc->str);
155          scan_err1(lc, "Keyword %s not found", lc->str);
156          bsr = NULL;
157          break;
158       }
159       if (!bsr) {
160          break;
161       }
162    }
163    lc = lex_close_file(lc);
164    Dmsg0(200, "Leave parse_bsf()\n");
165    if (!bsr) {
166       free_bsr(root_bsr);
167       root_bsr = NULL;
168    }
169    if (root_bsr) {
170       root_bsr->use_fast_rejection = is_fast_rejection_ok(root_bsr);
171       root_bsr->use_positioning = is_positioning_ok(root_bsr);
172    }
173    for (bsr=root_bsr; bsr; bsr=bsr->next) {
174       bsr->root = root_bsr;
175    }
176    return root_bsr;
177 }
178
179 static bool is_fast_rejection_ok(BSR *bsr)
180 {
181    /*
182     * Although, this can be optimized, for the moment, require
183     *  all bsrs to have both sesstime and sessid set before
184     *  we do fast rejection.
185     */
186    if (!(bsr->sesstime && bsr->sessid)) {
187       return false;
188    }
189    if (bsr->next) {
190       return is_fast_rejection_ok(bsr->next);
191    }
192    return true;
193 }
194
195 static bool is_positioning_ok(BSR *bsr)
196 {
197    /*
198     * Every bsr should have a volfile entry and a volblock entry
199     *   if we are going to use positioning
200     */
201    if (!bsr->volfile || !bsr->volblock) {
202       return false;
203    }
204    if (bsr->next) {
205       return is_positioning_ok(bsr->next);
206    }
207    return true;
208 }
209
210
211 static BSR *store_vol(LEX *lc, BSR *bsr)
212 {
213    int token;
214    BSR_VOLUME *volume;
215    char *p, *n;
216     
217    token = lex_get_token(lc, T_STRING);
218    if (token == T_ERROR) {
219       return NULL;
220    }
221    if (bsr->volume) {
222       bsr->next = new_bsr();
223       bsr = bsr->next;
224    }
225    /* This may actually be more than one volume separated by a |  
226     * If so, separate them.
227     */
228    for (p=lc->str; p && *p; ) {
229       n = strchr(p, '|');
230       if (n) {
231          *n++ = 0;
232       }
233       volume = (BSR_VOLUME *)malloc(sizeof(BSR_VOLUME));
234       memset(volume, 0, sizeof(BSR_VOLUME));
235       bstrncpy(volume->VolumeName, p, sizeof(volume->VolumeName));
236       /* Add it to the end of the volume chain */
237       if (!bsr->volume) {
238          bsr->volume = volume;
239       } else {
240          BSR_VOLUME *bc = bsr->volume;
241          for ( ;bc->next; bc=bc->next)  
242             { }
243          bc->next = volume;
244       }
245       p = n;
246    }
247    return bsr;
248 }
249
250 static BSR *store_client(LEX *lc, BSR *bsr)
251 {
252    int token;
253    BSR_CLIENT *client;
254     
255    for (;;) {
256       token = lex_get_token(lc, T_NAME);
257       if (token == T_ERROR) {
258          return NULL;
259       }
260       client = (BSR_CLIENT *)malloc(sizeof(BSR_CLIENT));
261       memset(client, 0, sizeof(BSR_CLIENT));
262       bstrncpy(client->ClientName, lc->str, sizeof(client->ClientName));
263       /* Add it to the end of the client chain */
264       if (!bsr->client) {
265          bsr->client = client;
266       } else {
267          BSR_CLIENT *bc = bsr->client;
268          for ( ;bc->next; bc=bc->next)  
269             { }
270          bc->next = client;
271       }
272       token = lex_get_token(lc, T_ALL);
273       if (token != T_COMMA) {
274          break;
275       }
276    }
277    return bsr;
278 }
279
280 static BSR *store_job(LEX *lc, BSR *bsr)
281 {
282    int token;
283    BSR_JOB *job;
284     
285    for (;;) {
286       token = lex_get_token(lc, T_NAME);
287       if (token == T_ERROR) {
288          return NULL;
289       }
290       job = (BSR_JOB *)malloc(sizeof(BSR_JOB));
291       memset(job, 0, sizeof(BSR_JOB));
292       bstrncpy(job->Job, lc->str, sizeof(job->Job));
293       /* Add it to the end of the client chain */
294       if (!bsr->job) {
295          bsr->job = job;
296       } else {
297          /* Add to end of chain */
298          BSR_JOB *bc = bsr->job;
299          for ( ;bc->next; bc=bc->next)
300             { }
301          bc->next = job;
302       }
303       token = lex_get_token(lc, T_ALL);
304       if (token != T_COMMA) {
305          break;
306       }
307    }
308    return bsr;
309 }
310
311 static BSR *store_findex(LEX *lc, BSR *bsr)
312 {
313    int token;
314    BSR_FINDEX *findex;
315
316    for (;;) {
317       token = lex_get_token(lc, T_PINT32_RANGE);
318       if (token == T_ERROR) {
319          return NULL;
320       }
321       findex = (BSR_FINDEX *)malloc(sizeof(BSR_FINDEX));
322       memset(findex, 0, sizeof(BSR_FINDEX));
323       findex->findex = lc->pint32_val;
324       findex->findex2 = lc->pint32_val2;
325       /* Add it to the end of the chain */
326       if (!bsr->FileIndex) {
327          bsr->FileIndex = findex;
328       } else {
329          /* Add to end of chain */
330          BSR_FINDEX *bs = bsr->FileIndex;
331          for ( ;bs->next; bs=bs->next)
332             {  }
333          bs->next = findex;
334       }
335       token = lex_get_token(lc, T_ALL);
336       if (token != T_COMMA) {
337          break;
338       }
339    }
340    return bsr;
341 }
342
343
344 static BSR *store_jobid(LEX *lc, BSR *bsr)
345 {
346    int token;
347    BSR_JOBID *jobid;
348
349    for (;;) {
350       token = lex_get_token(lc, T_PINT32_RANGE);
351       if (token == T_ERROR) {
352          return NULL;
353       }
354       jobid = (BSR_JOBID *)malloc(sizeof(BSR_JOBID));
355       memset(jobid, 0, sizeof(BSR_JOBID));
356       jobid->JobId = lc->pint32_val;
357       jobid->JobId2 = lc->pint32_val2;
358       /* Add it to the end of the chain */
359       if (!bsr->JobId) {
360          bsr->JobId = jobid;
361       } else {
362          /* Add to end of chain */
363          BSR_JOBID *bs = bsr->JobId;
364          for ( ;bs->next; bs=bs->next)
365             {  }
366          bs->next = jobid;
367       }
368       token = lex_get_token(lc, T_ALL);
369       if (token != T_COMMA) {
370          break;
371       }
372    }
373    return bsr;
374 }
375
376
377 static BSR *store_count(LEX *lc, BSR *bsr)
378 {
379    int token;
380
381    token = lex_get_token(lc, T_PINT32);
382    if (token == T_ERROR) {
383       return NULL;
384    }
385    bsr->count = lc->pint32_val;
386    scan_to_eol(lc);
387    return bsr;
388 }
389
390
391 static BSR *store_jobtype(LEX *lc, BSR *bsr)
392 {
393    /* *****FIXME****** */
394    Dmsg0(-1, "JobType not yet implemented\n");
395    return bsr;
396 }
397
398
399 static BSR *store_joblevel(LEX *lc, BSR *bsr)
400 {
401    /* *****FIXME****** */
402    Dmsg0(-1, "JobLevel not yet implemented\n");
403    return bsr;
404 }
405
406
407
408
409 /*
410  * Routine to handle Volume start/end file   
411  */
412 static BSR *store_volfile(LEX *lc, BSR *bsr)
413 {
414    int token;
415    BSR_VOLFILE *volfile;
416
417    for (;;) {
418       token = lex_get_token(lc, T_PINT32_RANGE);
419       if (token == T_ERROR) {
420          return NULL;
421       }
422       volfile = (BSR_VOLFILE *)malloc(sizeof(BSR_VOLFILE));
423       memset(volfile, 0, sizeof(BSR_VOLFILE));
424       volfile->sfile = lc->pint32_val;
425       volfile->efile = lc->pint32_val2;
426       /* Add it to the end of the chain */
427       if (!bsr->volfile) {
428          bsr->volfile = volfile;
429       } else {
430          /* Add to end of chain */
431          BSR_VOLFILE *bs = bsr->volfile;
432          for ( ;bs->next; bs=bs->next)
433             {  }
434          bs->next = volfile;
435       }
436       token = lex_get_token(lc, T_ALL);
437       if (token != T_COMMA) {
438          break;
439       }
440    }
441    return bsr;
442 }
443
444
445 /*
446  * Routine to handle Volume start/end Block  
447  */
448 static BSR *store_volblock(LEX *lc, BSR *bsr)
449 {
450    int token;
451    BSR_VOLBLOCK *volblock;
452
453    for (;;) {
454       token = lex_get_token(lc, T_PINT32_RANGE);
455       if (token == T_ERROR) {
456          return NULL;
457       }
458       volblock = (BSR_VOLBLOCK *)malloc(sizeof(BSR_VOLBLOCK));
459       memset(volblock, 0, sizeof(BSR_VOLBLOCK));
460       volblock->sblock = lc->pint32_val;
461       volblock->eblock = lc->pint32_val2;
462       /* Add it to the end of the chain */
463       if (!bsr->volblock) {
464          bsr->volblock = volblock;
465       } else {
466          /* Add to end of chain */
467          BSR_VOLBLOCK *bs = bsr->volblock;
468          for ( ;bs->next; bs=bs->next)
469             {  }
470          bs->next = volblock;
471       }
472       token = lex_get_token(lc, T_ALL);
473       if (token != T_COMMA) {
474          break;
475       }
476    }
477    return bsr;
478 }
479
480
481 static BSR *store_sessid(LEX *lc, BSR *bsr)
482 {
483    int token;
484    BSR_SESSID *sid;
485
486    for (;;) {
487       token = lex_get_token(lc, T_PINT32_RANGE);
488       if (token == T_ERROR) {
489          return NULL;
490       }
491       sid = (BSR_SESSID *)malloc(sizeof(BSR_SESSID));
492       memset(sid, 0, sizeof(BSR_SESSID));
493       sid->sessid = lc->pint32_val;
494       sid->sessid2 = lc->pint32_val2;
495       /* Add it to the end of the chain */
496       if (!bsr->sessid) {
497          bsr->sessid = sid;
498       } else {
499          /* Add to end of chain */
500          BSR_SESSID *bs = bsr->sessid;
501          for ( ;bs->next; bs=bs->next)
502             {  }
503          bs->next = sid;
504       }
505       token = lex_get_token(lc, T_ALL);
506       if (token != T_COMMA) {
507          break;
508       }
509    }
510    return bsr;
511 }
512
513 static BSR *store_sesstime(LEX *lc, BSR *bsr)
514 {
515    int token;
516    BSR_SESSTIME *stime;
517
518    for (;;) {
519       token = lex_get_token(lc, T_PINT32);
520       if (token == T_ERROR) {
521          return NULL;
522       }
523       stime = (BSR_SESSTIME *)malloc(sizeof(BSR_SESSTIME));
524       memset(stime, 0, sizeof(BSR_SESSTIME));
525       stime->sesstime = lc->pint32_val;
526       /* Add it to the end of the chain */
527       if (!bsr->sesstime) {
528          bsr->sesstime = stime;
529       } else {
530          /* Add to end of chain */
531          BSR_SESSTIME *bs = bsr->sesstime;
532          for ( ;bs->next; bs=bs->next)
533             { }
534          bs->next = stime;
535       }
536       token = lex_get_token(lc, T_ALL);
537       if (token != T_COMMA) {
538          break;
539       }
540    }
541    return bsr;
542 }
543
544
545 static BSR *store_stream(LEX *lc, BSR *bsr)
546 {
547    int token;
548    BSR_STREAM *stream;
549
550    for (;;) {
551       token = lex_get_token(lc, T_INT32);
552       if (token == T_ERROR) {
553          return NULL;
554       }
555       stream = (BSR_STREAM *)malloc(sizeof(BSR_STREAM));
556       memset(stream, 0, sizeof(BSR_STREAM));
557       stream->stream = lc->int32_val;
558       /* Add it to the end of the chain */
559       if (!bsr->stream) {
560          bsr->stream = stream;
561       } else {
562          /* Add to end of chain */
563          BSR_STREAM *bs = bsr->stream;
564          for ( ;bs->next; bs=bs->next)
565             { }
566          bs->next = stream;
567       }
568       token = lex_get_token(lc, T_ALL);
569       if (token != T_COMMA) {
570          break;
571       }
572    }
573    return bsr;
574 }
575
576 static BSR *store_slot(LEX *lc, BSR *bsr)
577 {
578    int token;
579
580    token = lex_get_token(lc, T_PINT32);
581    if (token == T_ERROR) {
582       return NULL;
583    }
584    bsr->Slot = lc->pint32_val;
585    scan_to_eol(lc);
586    return bsr;
587 }
588
589 static BSR *store_include(LEX *lc, BSR *bsr)
590 {
591    scan_to_eol(lc);
592    return bsr;
593 }
594
595 static BSR *store_exclude(LEX *lc, BSR *bsr)
596 {
597    scan_to_eol(lc);
598    return bsr;
599 }
600
601 void dump_volfile(BSR_VOLFILE *volfile)
602 {
603    if (volfile) {
604       Dmsg2(-1, "VolFile     : %u-%u\n", volfile->sfile, volfile->efile);
605       dump_volfile(volfile->next);
606    }
607 }
608
609 void dump_volblock(BSR_VOLBLOCK *volblock)
610 {
611    if (volblock) {
612       Dmsg2(-1, "VolBlock    : %u-%u\n", volblock->sblock, volblock->eblock);
613       dump_volblock(volblock->next);
614    }
615 }
616
617
618 void dump_findex(BSR_FINDEX *FileIndex)
619 {
620    if (FileIndex) {
621       if (FileIndex->findex == FileIndex->findex2) {
622          Dmsg1(-1, "FileIndex   : %u\n", FileIndex->findex);
623       } else {
624          Dmsg2(-1, "FileIndex   : %u-%u\n", FileIndex->findex, FileIndex->findex2);
625       }
626       dump_findex(FileIndex->next);
627    }
628 }
629
630 void dump_jobid(BSR_JOBID *jobid)
631 {
632    if (jobid) {
633       if (jobid->JobId == jobid->JobId2) {
634          Dmsg1(-1, "JobId       : %u\n", jobid->JobId);
635       } else {
636          Dmsg2(-1, "JobId       : %u-%u\n", jobid->JobId, jobid->JobId2);
637       }
638       dump_jobid(jobid->next);
639    }
640 }
641
642 void dump_sessid(BSR_SESSID *sessid)
643 {
644    if (sessid) {
645       if (sessid->sessid == sessid->sessid2) {
646          Dmsg1(-1, "SessId      : %u\n", sessid->sessid);
647       } else {
648          Dmsg2(-1, "SessId      : %u-%u\n", sessid->sessid, sessid->sessid2);
649       }
650       dump_sessid(sessid->next);
651    }
652 }
653
654 void dump_volume(BSR_VOLUME *volume)
655 {
656    if (volume) {
657       Dmsg1(-1, "VolumeName  : %s\n", volume->VolumeName);
658       dump_volume(volume->next);
659    }
660 }
661
662
663 void dump_client(BSR_CLIENT *client)
664 {
665    if (client) {
666       Dmsg1(-1, "Client      : %s\n", client->ClientName);
667       dump_client(client->next);
668    }
669 }
670
671 void dump_job(BSR_JOB *job)
672 {
673    if (job) {
674       Dmsg1(-1, "Job          : %s\n", job->Job);
675       dump_job(job->next);
676    }
677 }
678
679 void dump_sesstime(BSR_SESSTIME *sesstime)
680 {
681    if (sesstime) {
682       Dmsg1(-1, "SessTime    : %u\n", sesstime->sesstime);
683       dump_sesstime(sesstime->next);
684    }
685 }
686
687
688
689
690
691 void dump_bsr(BSR *bsr, bool recurse)
692 {
693    int save_debug = debug_level;
694    debug_level = 1;
695    if (!bsr) {
696       Dmsg0(-1, "BSR is NULL\n");
697       debug_level = save_debug;
698       return;
699    }
700    Dmsg1(-1,    "Next        : 0x%x\n", bsr->next);
701    Dmsg1(-1,    "Root bsr    : 0x%x\n", bsr->root);
702    dump_volume(bsr->volume);
703    dump_sessid(bsr->sessid);
704    dump_sesstime(bsr->sesstime);
705    dump_volfile(bsr->volfile);
706    dump_volblock(bsr->volblock);
707    dump_client(bsr->client);
708    dump_jobid(bsr->JobId);
709    dump_job(bsr->job);
710    dump_findex(bsr->FileIndex);
711    if (bsr->Slot) {
712       Dmsg1(-1, "Slot        : %u\n", bsr->Slot);
713    }
714    if (bsr->count) {
715       Dmsg1(-1, "count       : %u\n", bsr->count);
716       Dmsg1(-1, "found       : %u\n", bsr->found);
717    }
718
719    Dmsg1(-1,    "done        : %s\n", bsr->done?"yes":"no");
720    Dmsg1(-1,    "positioning : %d\n", bsr->use_positioning);
721    Dmsg1(-1,    "fast_reject : %d\n", bsr->use_fast_rejection);
722    if (recurse && bsr->next) {
723       Dmsg0(-1, "\n");
724       dump_bsr(bsr->next, true);
725    }
726    debug_level = save_debug;
727 }
728
729
730
731 /*********************************************************************
732  *
733  *      Free bsr resources
734  */
735
736 static void free_bsr_item(BSR *bsr)
737 {
738    if (bsr) {
739       free_bsr_item(bsr->next);
740       free(bsr);
741    }
742 }
743
744 void free_bsr(BSR *bsr)
745 {
746    if (!bsr) {
747       return;
748    }
749    free_bsr_item((BSR *)bsr->volume);
750    free_bsr_item((BSR *)bsr->client);
751    free_bsr_item((BSR *)bsr->sessid);
752    free_bsr_item((BSR *)bsr->sesstime);
753    free_bsr_item((BSR *)bsr->volfile);
754    free_bsr_item((BSR *)bsr->volblock);
755    free_bsr_item((BSR *)bsr->JobId);
756    free_bsr_item((BSR *)bsr->job);
757    free_bsr_item((BSR *)bsr->FileIndex);
758    free_bsr_item((BSR *)bsr->JobType);
759    free_bsr_item((BSR *)bsr->JobLevel);
760    free_bsr(bsr->next);
761    free(bsr);
762 }
763
764 /*****************************************************************
765  * Routines for handling volumes     
766  */
767 VOL_LIST *new_vol()
768 {
769    VOL_LIST *vol;
770    vol = (VOL_LIST *)malloc(sizeof(VOL_LIST));
771    memset(vol, 0, sizeof(VOL_LIST));
772    return vol;
773 }
774
775 /* 
776  * Add current volume to end of list, only if the Volume
777  * is not already in the list.
778  *
779  *   returns: 1 if volume added
780  *            0 if volume already in list
781  */
782 int add_vol(JCR *jcr, VOL_LIST *vol)
783 {
784    VOL_LIST *next = jcr->VolList;
785
786    if (!next) {                       /* list empty ? */
787       jcr->VolList = vol;             /* yes, add volume */
788    } else {
789       for ( ; next->next; next=next->next) {
790          if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
791             if (vol->start_file < next->start_file) {
792                next->start_file = vol->start_file;
793             }
794             return 0;                 /* already in list */
795          }
796       }
797       if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
798          if (vol->start_file < next->start_file) {
799             next->start_file = vol->start_file;
800          }
801          return 0;                    /* already in list */
802       }
803       next->next = vol;               /* add volume */
804    }
805    return 1;
806 }
807
808 void free_vol_list(JCR *jcr)
809 {
810    VOL_LIST *next = jcr->VolList;
811    VOL_LIST *tmp;
812
813    for ( ; next; ) {
814       tmp = next->next;
815       free(next);
816       next = tmp;
817    }
818    jcr->VolList = NULL;
819 }
820
821 /*
822  * Create a list of Volumes (and Slots and Start positions) to be
823  *  used in the current restore job.
824  */
825 void create_vol_list(JCR *jcr)
826 {
827    char *p, *n;
828    VOL_LIST *vol;
829
830    /* 
831     * Build a list of volumes to be processed
832     */
833    jcr->NumVolumes = 0;
834    jcr->CurVolume = 0;
835    if (jcr->bsr) {
836       BSR *bsr = jcr->bsr;
837       if (!bsr->volume || !bsr->volume->VolumeName) {
838          return;
839       }
840       for ( ; bsr; bsr=bsr->next) {
841          BSR_VOLUME *bsrvol;
842          BSR_VOLFILE *volfile;
843          uint32_t sfile = UINT32_MAX;
844
845          /* Find minimum start file so that we can forward space to it */
846          for (volfile = bsr->volfile; volfile; volfile=volfile->next) {
847             if (volfile->sfile < sfile) {
848                sfile = volfile->sfile;
849             }
850          }
851          /* Now add volumes for this bsr */
852          for (bsrvol = bsr->volume; bsrvol; bsrvol=bsrvol->next) {
853             vol = new_vol();
854             bstrncpy(vol->VolumeName, bsrvol->VolumeName, sizeof(vol->VolumeName));
855             vol->start_file = sfile;
856             if (add_vol(jcr, vol)) {
857                jcr->NumVolumes++;
858                Dmsg1(400, "Added volume %s\n", vol->VolumeName);
859             } else {
860                Dmsg1(400, "Duplicate volume %s\n", vol->VolumeName);
861                free((char *)vol);
862             }
863             sfile = 0;                /* start at beginning of second volume */
864          }
865       }
866    } else {
867       /* This is the old way -- deprecated */ 
868       for (p = jcr->VolumeName; p && *p; ) {
869          n = strchr(p, '|');             /* volume name separator */
870          if (n) {
871             *n++ = 0;                    /* Terminate name */
872          }
873          vol = new_vol();
874          bstrncpy(vol->VolumeName, p, sizeof(vol->VolumeName));
875          if (add_vol(jcr, vol)) {
876             jcr->NumVolumes++;
877          } else {
878             free((char *)vol);
879          }
880          p = n;
881       }
882    }
883 }