2 * Parse a Bootstrap Records (used for restores)
4 * Kern Sibbald, June MMII
10 Copyright (C) 2002 Kern Sibbald and John Walker
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_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);
55 ITEM_HANDLER *handler;
59 * List of all keywords permitted in bsr files and their handlers
61 struct kw_items items[] = {
62 {"volume", store_vol},
63 {"client", store_client},
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},
87 BSR *bsr = (BSR *)malloc(sizeof(BSR));
88 memset(bsr, 0, sizeof(BSR));
93 * Format a scanner error message
95 static void s_err(char *file, int line, LEX *lc, char *msg, ...)
97 JCR *jcr = (JCR *)(lc->caller_ctx);
101 va_start(arg_ptr, msg);
102 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
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);
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);
117 /*********************************************************************
119 * Parse Bootstrap file
122 BSR *parse_bsr(JCR *jcr, char *fname)
126 BSR *root_bsr = new_bsr();
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) {
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);
146 Dmsg1(200, "calling handler for %s\n", items[i].name);
147 /* Call item handler */
148 bsr = items[i].handler(lc, bsr);
154 Dmsg1(200, "Keyword = %s\n", lc->str);
155 scan_err1(lc, "Keyword %s not found", lc->str);
163 lc = lex_close_file(lc);
164 Dmsg0(200, "Leave parse_bsf()\n");
170 root_bsr->use_fast_rejection = is_fast_rejection_ok(root_bsr);
171 root_bsr->use_positioning = is_positioning_ok(root_bsr);
173 for (bsr=root_bsr; bsr; bsr=bsr->next) {
174 bsr->root = root_bsr;
179 static bool is_fast_rejection_ok(BSR *bsr)
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.
186 if (!(bsr->sesstime && bsr->sessid)) {
190 return is_fast_rejection_ok(bsr->next);
195 static bool is_positioning_ok(BSR *bsr)
198 * Every bsr should have a volfile entry and a volblock entry
199 * if we are going to use positioning
201 if (!bsr->volfile || !bsr->volblock) {
205 return is_positioning_ok(bsr->next);
211 static BSR *store_vol(LEX *lc, BSR *bsr)
217 token = lex_get_token(lc, T_STRING);
218 if (token == T_ERROR) {
222 bsr->next = new_bsr();
225 /* This may actually be more than one volume separated by a |
226 * If so, separate them.
228 for (p=lc->str; p && *p; ) {
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 */
238 bsr->volume = volume;
240 BSR_VOLUME *bc = bsr->volume;
241 for ( ;bc->next; bc=bc->next)
250 static BSR *store_client(LEX *lc, BSR *bsr)
256 token = lex_get_token(lc, T_NAME);
257 if (token == T_ERROR) {
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 */
265 bsr->client = client;
267 BSR_CLIENT *bc = bsr->client;
268 for ( ;bc->next; bc=bc->next)
272 token = lex_get_token(lc, T_ALL);
273 if (token != T_COMMA) {
280 static BSR *store_job(LEX *lc, BSR *bsr)
286 token = lex_get_token(lc, T_NAME);
287 if (token == T_ERROR) {
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 */
297 /* Add to end of chain */
298 BSR_JOB *bc = bsr->job;
299 for ( ;bc->next; bc=bc->next)
303 token = lex_get_token(lc, T_ALL);
304 if (token != T_COMMA) {
311 static BSR *store_findex(LEX *lc, BSR *bsr)
317 token = lex_get_token(lc, T_PINT32_RANGE);
318 if (token == T_ERROR) {
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;
329 /* Add to end of chain */
330 BSR_FINDEX *bs = bsr->FileIndex;
331 for ( ;bs->next; bs=bs->next)
335 token = lex_get_token(lc, T_ALL);
336 if (token != T_COMMA) {
344 static BSR *store_jobid(LEX *lc, BSR *bsr)
350 token = lex_get_token(lc, T_PINT32_RANGE);
351 if (token == T_ERROR) {
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 */
362 /* Add to end of chain */
363 BSR_JOBID *bs = bsr->JobId;
364 for ( ;bs->next; bs=bs->next)
368 token = lex_get_token(lc, T_ALL);
369 if (token != T_COMMA) {
377 static BSR *store_count(LEX *lc, BSR *bsr)
381 token = lex_get_token(lc, T_PINT32);
382 if (token == T_ERROR) {
385 bsr->count = lc->pint32_val;
391 static BSR *store_jobtype(LEX *lc, BSR *bsr)
393 /* *****FIXME****** */
394 Dmsg0(-1, "JobType not yet implemented\n");
399 static BSR *store_joblevel(LEX *lc, BSR *bsr)
401 /* *****FIXME****** */
402 Dmsg0(-1, "JobLevel not yet implemented\n");
410 * Routine to handle Volume start/end file
412 static BSR *store_volfile(LEX *lc, BSR *bsr)
415 BSR_VOLFILE *volfile;
418 token = lex_get_token(lc, T_PINT32_RANGE);
419 if (token == T_ERROR) {
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 */
428 bsr->volfile = volfile;
430 /* Add to end of chain */
431 BSR_VOLFILE *bs = bsr->volfile;
432 for ( ;bs->next; bs=bs->next)
436 token = lex_get_token(lc, T_ALL);
437 if (token != T_COMMA) {
446 * Routine to handle Volume start/end Block
448 static BSR *store_volblock(LEX *lc, BSR *bsr)
451 BSR_VOLBLOCK *volblock;
454 token = lex_get_token(lc, T_PINT32_RANGE);
455 if (token == T_ERROR) {
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;
466 /* Add to end of chain */
467 BSR_VOLBLOCK *bs = bsr->volblock;
468 for ( ;bs->next; bs=bs->next)
472 token = lex_get_token(lc, T_ALL);
473 if (token != T_COMMA) {
481 static BSR *store_sessid(LEX *lc, BSR *bsr)
487 token = lex_get_token(lc, T_PINT32_RANGE);
488 if (token == T_ERROR) {
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 */
499 /* Add to end of chain */
500 BSR_SESSID *bs = bsr->sessid;
501 for ( ;bs->next; bs=bs->next)
505 token = lex_get_token(lc, T_ALL);
506 if (token != T_COMMA) {
513 static BSR *store_sesstime(LEX *lc, BSR *bsr)
519 token = lex_get_token(lc, T_PINT32);
520 if (token == T_ERROR) {
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;
530 /* Add to end of chain */
531 BSR_SESSTIME *bs = bsr->sesstime;
532 for ( ;bs->next; bs=bs->next)
536 token = lex_get_token(lc, T_ALL);
537 if (token != T_COMMA) {
545 static BSR *store_stream(LEX *lc, BSR *bsr)
551 token = lex_get_token(lc, T_INT32);
552 if (token == T_ERROR) {
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 */
560 bsr->stream = stream;
562 /* Add to end of chain */
563 BSR_STREAM *bs = bsr->stream;
564 for ( ;bs->next; bs=bs->next)
568 token = lex_get_token(lc, T_ALL);
569 if (token != T_COMMA) {
576 static BSR *store_slot(LEX *lc, BSR *bsr)
580 token = lex_get_token(lc, T_PINT32);
581 if (token == T_ERROR) {
584 bsr->Slot = lc->pint32_val;
589 static BSR *store_include(LEX *lc, BSR *bsr)
595 static BSR *store_exclude(LEX *lc, BSR *bsr)
601 void dump_volfile(BSR_VOLFILE *volfile)
604 Dmsg2(-1, "VolFile : %u-%u\n", volfile->sfile, volfile->efile);
605 dump_volfile(volfile->next);
609 void dump_volblock(BSR_VOLBLOCK *volblock)
612 Dmsg2(-1, "VolBlock : %u-%u\n", volblock->sblock, volblock->eblock);
613 dump_volblock(volblock->next);
618 void dump_findex(BSR_FINDEX *FileIndex)
621 if (FileIndex->findex == FileIndex->findex2) {
622 Dmsg1(-1, "FileIndex : %u\n", FileIndex->findex);
624 Dmsg2(-1, "FileIndex : %u-%u\n", FileIndex->findex, FileIndex->findex2);
626 dump_findex(FileIndex->next);
630 void dump_jobid(BSR_JOBID *jobid)
633 if (jobid->JobId == jobid->JobId2) {
634 Dmsg1(-1, "JobId : %u\n", jobid->JobId);
636 Dmsg2(-1, "JobId : %u-%u\n", jobid->JobId, jobid->JobId2);
638 dump_jobid(jobid->next);
642 void dump_sessid(BSR_SESSID *sessid)
645 if (sessid->sessid == sessid->sessid2) {
646 Dmsg1(-1, "SessId : %u\n", sessid->sessid);
648 Dmsg2(-1, "SessId : %u-%u\n", sessid->sessid, sessid->sessid2);
650 dump_sessid(sessid->next);
654 void dump_volume(BSR_VOLUME *volume)
657 Dmsg1(-1, "VolumeName : %s\n", volume->VolumeName);
658 dump_volume(volume->next);
663 void dump_client(BSR_CLIENT *client)
666 Dmsg1(-1, "Client : %s\n", client->ClientName);
667 dump_client(client->next);
671 void dump_job(BSR_JOB *job)
674 Dmsg1(-1, "Job : %s\n", job->Job);
679 void dump_sesstime(BSR_SESSTIME *sesstime)
682 Dmsg1(-1, "SessTime : %u\n", sesstime->sesstime);
683 dump_sesstime(sesstime->next);
691 void dump_bsr(BSR *bsr, bool recurse)
693 int save_debug = debug_level;
696 Dmsg0(-1, "BSR is NULL\n");
697 debug_level = save_debug;
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);
710 dump_findex(bsr->FileIndex);
712 Dmsg1(-1, "Slot : %u\n", bsr->Slot);
715 Dmsg1(-1, "count : %u\n", bsr->count);
716 Dmsg1(-1, "found : %u\n", bsr->found);
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) {
724 dump_bsr(bsr->next, true);
726 debug_level = save_debug;
731 /*********************************************************************
736 static void free_bsr_item(BSR *bsr)
739 free_bsr_item(bsr->next);
744 void free_bsr(BSR *bsr)
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);
764 /*****************************************************************
765 * Routines for handling volumes
770 vol = (VOL_LIST *)malloc(sizeof(VOL_LIST));
771 memset(vol, 0, sizeof(VOL_LIST));
776 * Add current volume to end of list, only if the Volume
777 * is not already in the list.
779 * returns: 1 if volume added
780 * 0 if volume already in list
782 int add_vol(JCR *jcr, VOL_LIST *vol)
784 VOL_LIST *next = jcr->VolList;
786 if (!next) { /* list empty ? */
787 jcr->VolList = vol; /* yes, add volume */
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;
794 return 0; /* already in list */
797 if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
798 if (vol->start_file < next->start_file) {
799 next->start_file = vol->start_file;
801 return 0; /* already in list */
803 next->next = vol; /* add volume */
808 void free_vol_list(JCR *jcr)
810 VOL_LIST *next = jcr->VolList;
822 * Create a list of Volumes (and Slots and Start positions) to be
823 * used in the current restore job.
825 void create_vol_list(JCR *jcr)
831 * Build a list of volumes to be processed
837 if (!bsr->volume || !bsr->volume->VolumeName) {
840 for ( ; bsr; bsr=bsr->next) {
842 BSR_VOLFILE *volfile;
843 uint32_t sfile = UINT32_MAX;
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;
851 /* Now add volumes for this bsr */
852 for (bsrvol = bsr->volume; bsrvol; bsrvol=bsrvol->next) {
854 bstrncpy(vol->VolumeName, bsrvol->VolumeName, sizeof(vol->VolumeName));
855 vol->start_file = sfile;
856 if (add_vol(jcr, vol)) {
858 Dmsg1(400, "Added volume %s\n", vol->VolumeName);
860 Dmsg1(400, "Duplicate volume %s\n", vol->VolumeName);
863 sfile = 0; /* start at beginning of second volume */
867 /* This is the old way -- deprecated */
868 for (p = jcr->VolumeName; p && *p; ) {
869 n = strchr(p, '|'); /* volume name separator */
871 *n++ = 0; /* Terminate name */
874 bstrncpy(vol->VolumeName, p, sizeof(vol->VolumeName));
875 if (add_vol(jcr, vol)) {