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 if ((lc = lex_open_file(lc, fname, s_err)) == NULL) {
134 Emsg2(M_ERROR_TERM, 0, _("Cannot open bootstrap file %s: %s\n"),
135 fname, be.strerror());
137 lc->caller_ctx = (void *)jcr;
138 while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
139 Dmsg1(200, "parse got token=%s\n", lex_tok_to_str(token));
140 if (token == T_EOL) {
143 for (i=0; items[i].name; i++) {
144 if (strcasecmp(items[i].name, lc->str) == 0) {
145 token = lex_get_token(lc, T_ALL);
146 Dmsg1 (200, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
147 if (token != T_EQUALS) {
148 scan_err1(lc, "expected an equals, got: %s", lc->str);
152 Dmsg1(200, "calling handler for %s\n", items[i].name);
153 /* Call item handler */
154 bsr = items[i].handler(lc, bsr);
160 Dmsg1(200, "Keyword = %s\n", lc->str);
161 scan_err1(lc, "Keyword %s not found", lc->str);
169 lc = lex_close_file(lc);
170 Dmsg0(200, "Leave parse_bsf()\n");
176 root_bsr->use_fast_rejection = is_fast_rejection_ok(root_bsr);
177 root_bsr->use_positioning = is_positioning_ok(root_bsr);
179 for (bsr=root_bsr; bsr; bsr=bsr->next) {
180 bsr->root = root_bsr;
185 static bool is_fast_rejection_ok(BSR *bsr)
188 * Although, this can be optimized, for the moment, require
189 * all bsrs to have both sesstime and sessid set before
190 * we do fast rejection.
192 for ( ; bsr; bsr=bsr->next) {
193 if (!(bsr->sesstime && bsr->sessid)) {
200 static bool is_positioning_ok(BSR *bsr)
203 * Every bsr should have a volfile entry and a volblock entry
204 * if we are going to use positioning
206 for ( ; bsr; bsr=bsr->next) {
207 if (!bsr->volfile || !bsr->volblock) {
215 static BSR *store_vol(LEX *lc, BSR *bsr)
221 token = lex_get_token(lc, T_STRING);
222 if (token == T_ERROR) {
226 bsr->next = new_bsr();
229 /* This may actually be more than one volume separated by a |
230 * If so, separate them.
232 for (p=lc->str; p && *p; ) {
237 volume = (BSR_VOLUME *)malloc(sizeof(BSR_VOLUME));
238 memset(volume, 0, sizeof(BSR_VOLUME));
239 bstrncpy(volume->VolumeName, p, sizeof(volume->VolumeName));
240 /* Add it to the end of the volume chain */
242 bsr->volume = volume;
244 BSR_VOLUME *bc = bsr->volume;
245 for ( ;bc->next; bc=bc->next)
254 /* Shove the MediaType in each Volume in the current bsr */
255 static BSR *store_mediatype(LEX *lc, BSR *bsr)
259 token = lex_get_token(lc, T_STRING);
260 if (token == T_ERROR) {
264 Emsg1(M_ERROR,0, _("MediaType %s in bsr at inappropriate place.\n"),
269 for (bv=bsr->volume; bv; bv=bv->next) {
270 bstrncpy(bv->MediaType, lc->str, sizeof(bv->MediaType));
276 static BSR *store_client(LEX *lc, BSR *bsr)
282 token = lex_get_token(lc, T_NAME);
283 if (token == T_ERROR) {
286 client = (BSR_CLIENT *)malloc(sizeof(BSR_CLIENT));
287 memset(client, 0, sizeof(BSR_CLIENT));
288 bstrncpy(client->ClientName, lc->str, sizeof(client->ClientName));
289 /* Add it to the end of the client chain */
291 bsr->client = client;
293 BSR_CLIENT *bc = bsr->client;
294 for ( ;bc->next; bc=bc->next)
298 token = lex_get_token(lc, T_ALL);
299 if (token != T_COMMA) {
306 static BSR *store_job(LEX *lc, BSR *bsr)
312 token = lex_get_token(lc, T_NAME);
313 if (token == T_ERROR) {
316 job = (BSR_JOB *)malloc(sizeof(BSR_JOB));
317 memset(job, 0, sizeof(BSR_JOB));
318 bstrncpy(job->Job, lc->str, sizeof(job->Job));
319 /* Add it to the end of the client chain */
323 /* Add to end of chain */
324 BSR_JOB *bc = bsr->job;
325 for ( ;bc->next; bc=bc->next)
329 token = lex_get_token(lc, T_ALL);
330 if (token != T_COMMA) {
337 static BSR *store_findex(LEX *lc, BSR *bsr)
343 token = lex_get_token(lc, T_PINT32_RANGE);
344 if (token == T_ERROR) {
347 findex = (BSR_FINDEX *)malloc(sizeof(BSR_FINDEX));
348 memset(findex, 0, sizeof(BSR_FINDEX));
349 findex->findex = lc->pint32_val;
350 findex->findex2 = lc->pint32_val2;
351 /* Add it to the end of the chain */
352 if (!bsr->FileIndex) {
353 bsr->FileIndex = findex;
355 /* Add to end of chain */
356 BSR_FINDEX *bs = bsr->FileIndex;
357 for ( ;bs->next; bs=bs->next)
361 token = lex_get_token(lc, T_ALL);
362 if (token != T_COMMA) {
370 static BSR *store_jobid(LEX *lc, BSR *bsr)
376 token = lex_get_token(lc, T_PINT32_RANGE);
377 if (token == T_ERROR) {
380 jobid = (BSR_JOBID *)malloc(sizeof(BSR_JOBID));
381 memset(jobid, 0, sizeof(BSR_JOBID));
382 jobid->JobId = lc->pint32_val;
383 jobid->JobId2 = lc->pint32_val2;
384 /* Add it to the end of the chain */
388 /* Add to end of chain */
389 BSR_JOBID *bs = bsr->JobId;
390 for ( ;bs->next; bs=bs->next)
394 token = lex_get_token(lc, T_ALL);
395 if (token != T_COMMA) {
403 static BSR *store_count(LEX *lc, BSR *bsr)
407 token = lex_get_token(lc, T_PINT32);
408 if (token == T_ERROR) {
411 bsr->count = lc->pint32_val;
417 static BSR *store_jobtype(LEX *lc, BSR *bsr)
419 /* *****FIXME****** */
420 Dmsg0(-1, "JobType not yet implemented\n");
425 static BSR *store_joblevel(LEX *lc, BSR *bsr)
427 /* *****FIXME****** */
428 Dmsg0(-1, "JobLevel not yet implemented\n");
436 * Routine to handle Volume start/end file
438 static BSR *store_volfile(LEX *lc, BSR *bsr)
441 BSR_VOLFILE *volfile;
444 token = lex_get_token(lc, T_PINT32_RANGE);
445 if (token == T_ERROR) {
448 volfile = (BSR_VOLFILE *)malloc(sizeof(BSR_VOLFILE));
449 memset(volfile, 0, sizeof(BSR_VOLFILE));
450 volfile->sfile = lc->pint32_val;
451 volfile->efile = lc->pint32_val2;
452 /* Add it to the end of the chain */
454 bsr->volfile = volfile;
456 /* Add to end of chain */
457 BSR_VOLFILE *bs = bsr->volfile;
458 for ( ;bs->next; bs=bs->next)
462 token = lex_get_token(lc, T_ALL);
463 if (token != T_COMMA) {
472 * Routine to handle Volume start/end Block
474 static BSR *store_volblock(LEX *lc, BSR *bsr)
477 BSR_VOLBLOCK *volblock;
480 token = lex_get_token(lc, T_PINT32_RANGE);
481 if (token == T_ERROR) {
484 volblock = (BSR_VOLBLOCK *)malloc(sizeof(BSR_VOLBLOCK));
485 memset(volblock, 0, sizeof(BSR_VOLBLOCK));
486 volblock->sblock = lc->pint32_val;
487 volblock->eblock = lc->pint32_val2;
488 /* Add it to the end of the chain */
489 if (!bsr->volblock) {
490 bsr->volblock = volblock;
492 /* Add to end of chain */
493 BSR_VOLBLOCK *bs = bsr->volblock;
494 for ( ;bs->next; bs=bs->next)
498 token = lex_get_token(lc, T_ALL);
499 if (token != T_COMMA) {
507 static BSR *store_sessid(LEX *lc, BSR *bsr)
513 token = lex_get_token(lc, T_PINT32_RANGE);
514 if (token == T_ERROR) {
517 sid = (BSR_SESSID *)malloc(sizeof(BSR_SESSID));
518 memset(sid, 0, sizeof(BSR_SESSID));
519 sid->sessid = lc->pint32_val;
520 sid->sessid2 = lc->pint32_val2;
521 /* Add it to the end of the chain */
525 /* Add to end of chain */
526 BSR_SESSID *bs = bsr->sessid;
527 for ( ;bs->next; bs=bs->next)
531 token = lex_get_token(lc, T_ALL);
532 if (token != T_COMMA) {
539 static BSR *store_sesstime(LEX *lc, BSR *bsr)
545 token = lex_get_token(lc, T_PINT32);
546 if (token == T_ERROR) {
549 stime = (BSR_SESSTIME *)malloc(sizeof(BSR_SESSTIME));
550 memset(stime, 0, sizeof(BSR_SESSTIME));
551 stime->sesstime = lc->pint32_val;
552 /* Add it to the end of the chain */
553 if (!bsr->sesstime) {
554 bsr->sesstime = stime;
556 /* Add to end of chain */
557 BSR_SESSTIME *bs = bsr->sesstime;
558 for ( ;bs->next; bs=bs->next)
562 token = lex_get_token(lc, T_ALL);
563 if (token != T_COMMA) {
571 static BSR *store_stream(LEX *lc, BSR *bsr)
577 token = lex_get_token(lc, T_INT32);
578 if (token == T_ERROR) {
581 stream = (BSR_STREAM *)malloc(sizeof(BSR_STREAM));
582 memset(stream, 0, sizeof(BSR_STREAM));
583 stream->stream = lc->int32_val;
584 /* Add it to the end of the chain */
586 bsr->stream = stream;
588 /* Add to end of chain */
589 BSR_STREAM *bs = bsr->stream;
590 for ( ;bs->next; bs=bs->next)
594 token = lex_get_token(lc, T_ALL);
595 if (token != T_COMMA) {
602 static BSR *store_slot(LEX *lc, BSR *bsr)
606 token = lex_get_token(lc, T_PINT32);
607 if (token == T_ERROR) {
610 bsr->Slot = lc->pint32_val;
615 static BSR *store_include(LEX *lc, BSR *bsr)
621 static BSR *store_exclude(LEX *lc, BSR *bsr)
627 void dump_volfile(BSR_VOLFILE *volfile)
630 Dmsg2(-1, "VolFile : %u-%u\n", volfile->sfile, volfile->efile);
631 dump_volfile(volfile->next);
635 void dump_volblock(BSR_VOLBLOCK *volblock)
638 Dmsg2(-1, "VolBlock : %u-%u\n", volblock->sblock, volblock->eblock);
639 dump_volblock(volblock->next);
644 void dump_findex(BSR_FINDEX *FileIndex)
647 if (FileIndex->findex == FileIndex->findex2) {
648 Dmsg1(-1, "FileIndex : %u\n", FileIndex->findex);
650 Dmsg2(-1, "FileIndex : %u-%u\n", FileIndex->findex, FileIndex->findex2);
652 dump_findex(FileIndex->next);
656 void dump_jobid(BSR_JOBID *jobid)
659 if (jobid->JobId == jobid->JobId2) {
660 Dmsg1(-1, "JobId : %u\n", jobid->JobId);
662 Dmsg2(-1, "JobId : %u-%u\n", jobid->JobId, jobid->JobId2);
664 dump_jobid(jobid->next);
668 void dump_sessid(BSR_SESSID *sessid)
671 if (sessid->sessid == sessid->sessid2) {
672 Dmsg1(-1, "SessId : %u\n", sessid->sessid);
674 Dmsg2(-1, "SessId : %u-%u\n", sessid->sessid, sessid->sessid2);
676 dump_sessid(sessid->next);
680 void dump_volume(BSR_VOLUME *volume)
683 Dmsg1(-1, "VolumeName : %s\n", volume->VolumeName);
684 dump_volume(volume->next);
689 void dump_client(BSR_CLIENT *client)
692 Dmsg1(-1, "Client : %s\n", client->ClientName);
693 dump_client(client->next);
697 void dump_job(BSR_JOB *job)
700 Dmsg1(-1, "Job : %s\n", job->Job);
705 void dump_sesstime(BSR_SESSTIME *sesstime)
708 Dmsg1(-1, "SessTime : %u\n", sesstime->sesstime);
709 dump_sesstime(sesstime->next);
717 void dump_bsr(BSR *bsr, bool recurse)
719 int save_debug = debug_level;
722 Dmsg0(-1, "BSR is NULL\n");
723 debug_level = save_debug;
726 Dmsg1(-1, "Next : 0x%x\n", bsr->next);
727 Dmsg1(-1, "Root bsr : 0x%x\n", bsr->root);
728 dump_volume(bsr->volume);
729 dump_sessid(bsr->sessid);
730 dump_sesstime(bsr->sesstime);
731 dump_volfile(bsr->volfile);
732 dump_volblock(bsr->volblock);
733 dump_client(bsr->client);
734 dump_jobid(bsr->JobId);
736 dump_findex(bsr->FileIndex);
738 Dmsg1(-1, "Slot : %u\n", bsr->Slot);
741 Dmsg1(-1, "count : %u\n", bsr->count);
742 Dmsg1(-1, "found : %u\n", bsr->found);
745 Dmsg1(-1, "done : %s\n", bsr->done?"yes":"no");
746 Dmsg1(-1, "positioning : %d\n", bsr->use_positioning);
747 Dmsg1(-1, "fast_reject : %d\n", bsr->use_fast_rejection);
748 if (recurse && bsr->next) {
750 dump_bsr(bsr->next, true);
752 debug_level = save_debug;
757 /*********************************************************************
762 static void free_bsr_item(BSR *bsr)
765 free_bsr_item(bsr->next);
770 void free_bsr(BSR *bsr)
775 free_bsr_item((BSR *)bsr->volume);
776 free_bsr_item((BSR *)bsr->client);
777 free_bsr_item((BSR *)bsr->sessid);
778 free_bsr_item((BSR *)bsr->sesstime);
779 free_bsr_item((BSR *)bsr->volfile);
780 free_bsr_item((BSR *)bsr->volblock);
781 free_bsr_item((BSR *)bsr->JobId);
782 free_bsr_item((BSR *)bsr->job);
783 free_bsr_item((BSR *)bsr->FileIndex);
784 free_bsr_item((BSR *)bsr->JobType);
785 free_bsr_item((BSR *)bsr->JobLevel);
790 /*****************************************************************
791 * Routines for handling volumes
796 vol = (VOL_LIST *)malloc(sizeof(VOL_LIST));
797 memset(vol, 0, sizeof(VOL_LIST));
802 * Add current volume to end of list, only if the Volume
803 * is not already in the list.
805 * returns: 1 if volume added
806 * 0 if volume already in list
808 int add_vol(JCR *jcr, VOL_LIST *vol)
810 VOL_LIST *next = jcr->VolList;
812 if (!next) { /* list empty ? */
813 jcr->VolList = vol; /* yes, add volume */
815 for ( ; next->next; next=next->next) {
816 if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
817 if (vol->start_file < next->start_file) {
818 next->start_file = vol->start_file;
820 return 0; /* already in list */
823 if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
824 if (vol->start_file < next->start_file) {
825 next->start_file = vol->start_file;
827 return 0; /* already in list */
829 next->next = vol; /* add volume */
834 void free_vol_list(JCR *jcr)
836 VOL_LIST *next = jcr->VolList;
848 * Create a list of Volumes (and Slots and Start positions) to be
849 * used in the current restore job.
851 void create_vol_list(JCR *jcr)
857 * Build a list of volumes to be processed
863 if (!bsr->volume || !bsr->volume->VolumeName) {
866 for ( ; bsr; bsr=bsr->next) {
868 BSR_VOLFILE *volfile;
869 uint32_t sfile = UINT32_MAX;
871 /* Find minimum start file so that we can forward space to it */
872 for (volfile = bsr->volfile; volfile; volfile=volfile->next) {
873 if (volfile->sfile < sfile) {
874 sfile = volfile->sfile;
877 /* Now add volumes for this bsr */
878 for (bsrvol = bsr->volume; bsrvol; bsrvol=bsrvol->next) {
880 bstrncpy(vol->VolumeName, bsrvol->VolumeName, sizeof(vol->VolumeName));
881 bstrncpy(vol->MediaType, bsrvol->MediaType, sizeof(vol->MediaType));
882 vol->start_file = sfile;
883 if (add_vol(jcr, vol)) {
885 Dmsg2(400, "Added volume=%s mediatype=%s\n", vol->VolumeName,
888 Dmsg1(400, "Duplicate volume %s\n", vol->VolumeName);
891 sfile = 0; /* start at beginning of second volume */
895 /* This is the old way -- deprecated */
896 for (p = jcr->dcr->VolumeName; p && *p; ) {
897 n = strchr(p, '|'); /* volume name separator */
899 *n++ = 0; /* Terminate name */
902 bstrncpy(vol->VolumeName, p, sizeof(vol->VolumeName));
903 bstrncpy(vol->MediaType, jcr->dcr->media_type, sizeof(vol->MediaType));
904 if (add_vol(jcr, vol)) {