2 * Parse a Bootstrap Records (used for restores)
4 * Kern Sibbald, June MMII
10 Copyright (C) 2002-2005 Kern Sibbald
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.
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.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
32 typedef BSR * (ITEM_HANDLER)(LEX *lc, BSR *bsr);
34 static BSR *store_vol(LEX *lc, BSR *bsr);
35 static BSR *store_mediatype(LEX *lc, BSR *bsr);
36 static BSR *store_client(LEX *lc, BSR *bsr);
37 static BSR *store_job(LEX *lc, BSR *bsr);
38 static BSR *store_jobid(LEX *lc, BSR *bsr);
39 static BSR *store_count(LEX *lc, BSR *bsr);
40 static BSR *store_jobtype(LEX *lc, BSR *bsr);
41 static BSR *store_joblevel(LEX *lc, BSR *bsr);
42 static BSR *store_findex(LEX *lc, BSR *bsr);
43 static BSR *store_sessid(LEX *lc, BSR *bsr);
44 static BSR *store_volfile(LEX *lc, BSR *bsr);
45 static BSR *store_volblock(LEX *lc, BSR *bsr);
46 static BSR *store_sesstime(LEX *lc, BSR *bsr);
47 static BSR *store_include(LEX *lc, BSR *bsr);
48 static BSR *store_exclude(LEX *lc, BSR *bsr);
49 static BSR *store_stream(LEX *lc, BSR *bsr);
50 static BSR *store_slot(LEX *lc, BSR *bsr);
51 static bool is_fast_rejection_ok(BSR *bsr);
52 static bool is_positioning_ok(BSR *bsr);
56 ITEM_HANDLER *handler;
60 * List of all keywords permitted in bsr files and their handlers
62 struct kw_items items[] = {
63 {"volume", store_vol},
64 {"mediatype", store_mediatype},
65 {"client", store_client},
67 {"jobid", store_jobid},
68 {"count", store_count},
69 {"fileindex", store_findex},
70 {"jobtype", store_jobtype},
71 {"joblevel", store_joblevel},
72 {"volsessionid", store_sessid},
73 {"volsessiontime", store_sesstime},
74 {"include", store_include},
75 {"exclude", store_exclude},
76 {"volfile", store_volfile},
77 {"volblock", store_volblock},
78 {"stream", store_stream},
89 BSR *bsr = (BSR *)malloc(sizeof(BSR));
90 memset(bsr, 0, sizeof(BSR));
95 * Format a scanner error message
97 static void s_err(const char *file, int line, LEX *lc, const char *msg, ...)
99 JCR *jcr = (JCR *)(lc->caller_ctx);
103 va_start(arg_ptr, msg);
104 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
108 Jmsg(jcr, M_FATAL, 0, _("Bootstrap file error: %s\n"
109 " : Line %d, col %d of file %s\n%s\n"),
110 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
112 e_msg(file, line, M_FATAL, 0, _("Bootstrap file error: %s\n"
113 " : Line %d, col %d of file %s\n%s\n"),
114 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
119 /*********************************************************************
121 * Parse Bootstrap file
124 BSR *parse_bsr(JCR *jcr, char *fname)
128 BSR *root_bsr = new_bsr();
131 Dmsg1(200, "Enter parse_bsf %s\n", fname);
132 lc = lex_open_file(lc, fname, s_err);
133 lc->caller_ctx = (void *)jcr;
134 while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
135 Dmsg1(200, "parse got token=%s\n", lex_tok_to_str(token));
136 if (token == T_EOL) {
139 for (i=0; items[i].name; i++) {
140 if (strcasecmp(items[i].name, lc->str) == 0) {
141 token = lex_get_token(lc, T_ALL);
142 Dmsg1 (200, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
143 if (token != T_EQUALS) {
144 scan_err1(lc, "expected an equals, got: %s", lc->str);
148 Dmsg1(200, "calling handler for %s\n", items[i].name);
149 /* Call item handler */
150 bsr = items[i].handler(lc, bsr);
156 Dmsg1(200, "Keyword = %s\n", lc->str);
157 scan_err1(lc, "Keyword %s not found", lc->str);
165 lc = lex_close_file(lc);
166 Dmsg0(200, "Leave parse_bsf()\n");
172 root_bsr->use_fast_rejection = is_fast_rejection_ok(root_bsr);
173 root_bsr->use_positioning = is_positioning_ok(root_bsr);
175 for (bsr=root_bsr; bsr; bsr=bsr->next) {
176 bsr->root = root_bsr;
181 static bool is_fast_rejection_ok(BSR *bsr)
184 * Although, this can be optimized, for the moment, require
185 * all bsrs to have both sesstime and sessid set before
186 * we do fast rejection.
188 if (!(bsr->sesstime && bsr->sessid)) {
192 return is_fast_rejection_ok(bsr->next);
197 static bool is_positioning_ok(BSR *bsr)
200 * Every bsr should have a volfile entry and a volblock entry
201 * if we are going to use positioning
203 if (!bsr->volfile || !bsr->volblock) {
207 return is_positioning_ok(bsr->next);
213 static BSR *store_vol(LEX *lc, BSR *bsr)
219 token = lex_get_token(lc, T_STRING);
220 if (token == T_ERROR) {
224 bsr->next = new_bsr();
227 /* This may actually be more than one volume separated by a |
228 * If so, separate them.
230 for (p=lc->str; p && *p; ) {
235 volume = (BSR_VOLUME *)malloc(sizeof(BSR_VOLUME));
236 memset(volume, 0, sizeof(BSR_VOLUME));
237 bstrncpy(volume->VolumeName, p, sizeof(volume->VolumeName));
238 /* Add it to the end of the volume chain */
240 bsr->volume = volume;
242 BSR_VOLUME *bc = bsr->volume;
243 for ( ;bc->next; bc=bc->next)
252 /* Shove the MediaType in each Volume in the current bsr */
253 static BSR *store_mediatype(LEX *lc, BSR *bsr)
257 token = lex_get_token(lc, T_STRING);
258 if (token == T_ERROR) {
262 Emsg1(M_ERROR,0, _("MediaType %s in bsr at inappropriate place.\n"),
267 for (bv=bsr->volume; bv; bv=bv->next) {
268 bstrncpy(bv->MediaType, lc->str, sizeof(bv->MediaType));
274 static BSR *store_client(LEX *lc, BSR *bsr)
280 token = lex_get_token(lc, T_NAME);
281 if (token == T_ERROR) {
284 client = (BSR_CLIENT *)malloc(sizeof(BSR_CLIENT));
285 memset(client, 0, sizeof(BSR_CLIENT));
286 bstrncpy(client->ClientName, lc->str, sizeof(client->ClientName));
287 /* Add it to the end of the client chain */
289 bsr->client = client;
291 BSR_CLIENT *bc = bsr->client;
292 for ( ;bc->next; bc=bc->next)
296 token = lex_get_token(lc, T_ALL);
297 if (token != T_COMMA) {
304 static BSR *store_job(LEX *lc, BSR *bsr)
310 token = lex_get_token(lc, T_NAME);
311 if (token == T_ERROR) {
314 job = (BSR_JOB *)malloc(sizeof(BSR_JOB));
315 memset(job, 0, sizeof(BSR_JOB));
316 bstrncpy(job->Job, lc->str, sizeof(job->Job));
317 /* Add it to the end of the client chain */
321 /* Add to end of chain */
322 BSR_JOB *bc = bsr->job;
323 for ( ;bc->next; bc=bc->next)
327 token = lex_get_token(lc, T_ALL);
328 if (token != T_COMMA) {
335 static BSR *store_findex(LEX *lc, BSR *bsr)
341 token = lex_get_token(lc, T_PINT32_RANGE);
342 if (token == T_ERROR) {
345 findex = (BSR_FINDEX *)malloc(sizeof(BSR_FINDEX));
346 memset(findex, 0, sizeof(BSR_FINDEX));
347 findex->findex = lc->pint32_val;
348 findex->findex2 = lc->pint32_val2;
349 /* Add it to the end of the chain */
350 if (!bsr->FileIndex) {
351 bsr->FileIndex = findex;
353 /* Add to end of chain */
354 BSR_FINDEX *bs = bsr->FileIndex;
355 for ( ;bs->next; bs=bs->next)
359 token = lex_get_token(lc, T_ALL);
360 if (token != T_COMMA) {
368 static BSR *store_jobid(LEX *lc, BSR *bsr)
374 token = lex_get_token(lc, T_PINT32_RANGE);
375 if (token == T_ERROR) {
378 jobid = (BSR_JOBID *)malloc(sizeof(BSR_JOBID));
379 memset(jobid, 0, sizeof(BSR_JOBID));
380 jobid->JobId = lc->pint32_val;
381 jobid->JobId2 = lc->pint32_val2;
382 /* Add it to the end of the chain */
386 /* Add to end of chain */
387 BSR_JOBID *bs = bsr->JobId;
388 for ( ;bs->next; bs=bs->next)
392 token = lex_get_token(lc, T_ALL);
393 if (token != T_COMMA) {
401 static BSR *store_count(LEX *lc, BSR *bsr)
405 token = lex_get_token(lc, T_PINT32);
406 if (token == T_ERROR) {
409 bsr->count = lc->pint32_val;
415 static BSR *store_jobtype(LEX *lc, BSR *bsr)
417 /* *****FIXME****** */
418 Dmsg0(-1, "JobType not yet implemented\n");
423 static BSR *store_joblevel(LEX *lc, BSR *bsr)
425 /* *****FIXME****** */
426 Dmsg0(-1, "JobLevel not yet implemented\n");
434 * Routine to handle Volume start/end file
436 static BSR *store_volfile(LEX *lc, BSR *bsr)
439 BSR_VOLFILE *volfile;
442 token = lex_get_token(lc, T_PINT32_RANGE);
443 if (token == T_ERROR) {
446 volfile = (BSR_VOLFILE *)malloc(sizeof(BSR_VOLFILE));
447 memset(volfile, 0, sizeof(BSR_VOLFILE));
448 volfile->sfile = lc->pint32_val;
449 volfile->efile = lc->pint32_val2;
450 /* Add it to the end of the chain */
452 bsr->volfile = volfile;
454 /* Add to end of chain */
455 BSR_VOLFILE *bs = bsr->volfile;
456 for ( ;bs->next; bs=bs->next)
460 token = lex_get_token(lc, T_ALL);
461 if (token != T_COMMA) {
470 * Routine to handle Volume start/end Block
472 static BSR *store_volblock(LEX *lc, BSR *bsr)
475 BSR_VOLBLOCK *volblock;
478 token = lex_get_token(lc, T_PINT32_RANGE);
479 if (token == T_ERROR) {
482 volblock = (BSR_VOLBLOCK *)malloc(sizeof(BSR_VOLBLOCK));
483 memset(volblock, 0, sizeof(BSR_VOLBLOCK));
484 volblock->sblock = lc->pint32_val;
485 volblock->eblock = lc->pint32_val2;
486 /* Add it to the end of the chain */
487 if (!bsr->volblock) {
488 bsr->volblock = volblock;
490 /* Add to end of chain */
491 BSR_VOLBLOCK *bs = bsr->volblock;
492 for ( ;bs->next; bs=bs->next)
496 token = lex_get_token(lc, T_ALL);
497 if (token != T_COMMA) {
505 static BSR *store_sessid(LEX *lc, BSR *bsr)
511 token = lex_get_token(lc, T_PINT32_RANGE);
512 if (token == T_ERROR) {
515 sid = (BSR_SESSID *)malloc(sizeof(BSR_SESSID));
516 memset(sid, 0, sizeof(BSR_SESSID));
517 sid->sessid = lc->pint32_val;
518 sid->sessid2 = lc->pint32_val2;
519 /* Add it to the end of the chain */
523 /* Add to end of chain */
524 BSR_SESSID *bs = bsr->sessid;
525 for ( ;bs->next; bs=bs->next)
529 token = lex_get_token(lc, T_ALL);
530 if (token != T_COMMA) {
537 static BSR *store_sesstime(LEX *lc, BSR *bsr)
543 token = lex_get_token(lc, T_PINT32);
544 if (token == T_ERROR) {
547 stime = (BSR_SESSTIME *)malloc(sizeof(BSR_SESSTIME));
548 memset(stime, 0, sizeof(BSR_SESSTIME));
549 stime->sesstime = lc->pint32_val;
550 /* Add it to the end of the chain */
551 if (!bsr->sesstime) {
552 bsr->sesstime = stime;
554 /* Add to end of chain */
555 BSR_SESSTIME *bs = bsr->sesstime;
556 for ( ;bs->next; bs=bs->next)
560 token = lex_get_token(lc, T_ALL);
561 if (token != T_COMMA) {
569 static BSR *store_stream(LEX *lc, BSR *bsr)
575 token = lex_get_token(lc, T_INT32);
576 if (token == T_ERROR) {
579 stream = (BSR_STREAM *)malloc(sizeof(BSR_STREAM));
580 memset(stream, 0, sizeof(BSR_STREAM));
581 stream->stream = lc->int32_val;
582 /* Add it to the end of the chain */
584 bsr->stream = stream;
586 /* Add to end of chain */
587 BSR_STREAM *bs = bsr->stream;
588 for ( ;bs->next; bs=bs->next)
592 token = lex_get_token(lc, T_ALL);
593 if (token != T_COMMA) {
600 static BSR *store_slot(LEX *lc, BSR *bsr)
604 token = lex_get_token(lc, T_PINT32);
605 if (token == T_ERROR) {
608 bsr->Slot = lc->pint32_val;
613 static BSR *store_include(LEX *lc, BSR *bsr)
619 static BSR *store_exclude(LEX *lc, BSR *bsr)
625 void dump_volfile(BSR_VOLFILE *volfile)
628 Dmsg2(-1, "VolFile : %u-%u\n", volfile->sfile, volfile->efile);
629 dump_volfile(volfile->next);
633 void dump_volblock(BSR_VOLBLOCK *volblock)
636 Dmsg2(-1, "VolBlock : %u-%u\n", volblock->sblock, volblock->eblock);
637 dump_volblock(volblock->next);
642 void dump_findex(BSR_FINDEX *FileIndex)
645 if (FileIndex->findex == FileIndex->findex2) {
646 Dmsg1(-1, "FileIndex : %u\n", FileIndex->findex);
648 Dmsg2(-1, "FileIndex : %u-%u\n", FileIndex->findex, FileIndex->findex2);
650 dump_findex(FileIndex->next);
654 void dump_jobid(BSR_JOBID *jobid)
657 if (jobid->JobId == jobid->JobId2) {
658 Dmsg1(-1, "JobId : %u\n", jobid->JobId);
660 Dmsg2(-1, "JobId : %u-%u\n", jobid->JobId, jobid->JobId2);
662 dump_jobid(jobid->next);
666 void dump_sessid(BSR_SESSID *sessid)
669 if (sessid->sessid == sessid->sessid2) {
670 Dmsg1(-1, "SessId : %u\n", sessid->sessid);
672 Dmsg2(-1, "SessId : %u-%u\n", sessid->sessid, sessid->sessid2);
674 dump_sessid(sessid->next);
678 void dump_volume(BSR_VOLUME *volume)
681 Dmsg1(-1, "VolumeName : %s\n", volume->VolumeName);
682 dump_volume(volume->next);
687 void dump_client(BSR_CLIENT *client)
690 Dmsg1(-1, "Client : %s\n", client->ClientName);
691 dump_client(client->next);
695 void dump_job(BSR_JOB *job)
698 Dmsg1(-1, "Job : %s\n", job->Job);
703 void dump_sesstime(BSR_SESSTIME *sesstime)
706 Dmsg1(-1, "SessTime : %u\n", sesstime->sesstime);
707 dump_sesstime(sesstime->next);
715 void dump_bsr(BSR *bsr, bool recurse)
717 int save_debug = debug_level;
720 Dmsg0(-1, "BSR is NULL\n");
721 debug_level = save_debug;
724 Dmsg1(-1, "Next : 0x%x\n", bsr->next);
725 Dmsg1(-1, "Root bsr : 0x%x\n", bsr->root);
726 dump_volume(bsr->volume);
727 dump_sessid(bsr->sessid);
728 dump_sesstime(bsr->sesstime);
729 dump_volfile(bsr->volfile);
730 dump_volblock(bsr->volblock);
731 dump_client(bsr->client);
732 dump_jobid(bsr->JobId);
734 dump_findex(bsr->FileIndex);
736 Dmsg1(-1, "Slot : %u\n", bsr->Slot);
739 Dmsg1(-1, "count : %u\n", bsr->count);
740 Dmsg1(-1, "found : %u\n", bsr->found);
743 Dmsg1(-1, "done : %s\n", bsr->done?"yes":"no");
744 Dmsg1(-1, "positioning : %d\n", bsr->use_positioning);
745 Dmsg1(-1, "fast_reject : %d\n", bsr->use_fast_rejection);
746 if (recurse && bsr->next) {
748 dump_bsr(bsr->next, true);
750 debug_level = save_debug;
755 /*********************************************************************
760 static void free_bsr_item(BSR *bsr)
763 free_bsr_item(bsr->next);
768 void free_bsr(BSR *bsr)
773 free_bsr_item((BSR *)bsr->volume);
774 free_bsr_item((BSR *)bsr->client);
775 free_bsr_item((BSR *)bsr->sessid);
776 free_bsr_item((BSR *)bsr->sesstime);
777 free_bsr_item((BSR *)bsr->volfile);
778 free_bsr_item((BSR *)bsr->volblock);
779 free_bsr_item((BSR *)bsr->JobId);
780 free_bsr_item((BSR *)bsr->job);
781 free_bsr_item((BSR *)bsr->FileIndex);
782 free_bsr_item((BSR *)bsr->JobType);
783 free_bsr_item((BSR *)bsr->JobLevel);
788 /*****************************************************************
789 * Routines for handling volumes
794 vol = (VOL_LIST *)malloc(sizeof(VOL_LIST));
795 memset(vol, 0, sizeof(VOL_LIST));
800 * Add current volume to end of list, only if the Volume
801 * is not already in the list.
803 * returns: 1 if volume added
804 * 0 if volume already in list
806 int add_vol(JCR *jcr, VOL_LIST *vol)
808 VOL_LIST *next = jcr->VolList;
810 if (!next) { /* list empty ? */
811 jcr->VolList = vol; /* yes, add volume */
813 for ( ; next->next; next=next->next) {
814 if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
815 if (vol->start_file < next->start_file) {
816 next->start_file = vol->start_file;
818 return 0; /* already in list */
821 if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
822 if (vol->start_file < next->start_file) {
823 next->start_file = vol->start_file;
825 return 0; /* already in list */
827 next->next = vol; /* add volume */
832 void free_vol_list(JCR *jcr)
834 VOL_LIST *next = jcr->VolList;
846 * Create a list of Volumes (and Slots and Start positions) to be
847 * used in the current restore job.
849 void create_vol_list(JCR *jcr)
855 * Build a list of volumes to be processed
861 if (!bsr->volume || !bsr->volume->VolumeName) {
864 for ( ; bsr; bsr=bsr->next) {
866 BSR_VOLFILE *volfile;
867 uint32_t sfile = UINT32_MAX;
869 /* Find minimum start file so that we can forward space to it */
870 for (volfile = bsr->volfile; volfile; volfile=volfile->next) {
871 if (volfile->sfile < sfile) {
872 sfile = volfile->sfile;
875 /* Now add volumes for this bsr */
876 for (bsrvol = bsr->volume; bsrvol; bsrvol=bsrvol->next) {
878 bstrncpy(vol->VolumeName, bsrvol->VolumeName, sizeof(vol->VolumeName));
879 bstrncpy(vol->MediaType, bsrvol->MediaType, sizeof(vol->MediaType));
880 vol->start_file = sfile;
881 if (add_vol(jcr, vol)) {
883 Dmsg2(400, "Added volume=%s mediatype=%s\n", vol->VolumeName,
886 Dmsg1(400, "Duplicate volume %s\n", vol->VolumeName);
889 sfile = 0; /* start at beginning of second volume */
893 /* This is the old way -- deprecated */
894 for (p = jcr->dcr->VolumeName; p && *p; ) {
895 n = strchr(p, '|'); /* volume name separator */
897 *n++ = 0; /* Terminate name */
900 bstrncpy(vol->VolumeName, p, sizeof(vol->VolumeName));
901 bstrncpy(vol->MediaType, jcr->dcr->media_type, sizeof(vol->MediaType));
902 if (add_vol(jcr, vol)) {