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);
54 ITEM_HANDLER *handler;
58 * List of all keywords permitted in bsr files and their handlers
60 struct kw_items items[] = {
61 {"volume", store_vol},
62 {"client", store_client},
64 {"jobid", store_jobid},
65 {"count", store_count},
66 {"fileindex", store_findex},
67 {"jobtype", store_jobtype},
68 {"joblevel", store_joblevel},
69 {"volsessionid", store_sessid},
70 {"volsessiontime", store_sesstime},
71 {"include", store_include},
72 {"exclude", store_exclude},
73 {"volfile", store_volfile},
74 {"volblock", store_volblock},
75 {"stream", store_stream},
86 BSR *bsr = (BSR *)malloc(sizeof(BSR));
87 memset(bsr, 0, sizeof(BSR));
92 * Format a scanner error message
94 static void s_err(char *file, int line, LEX *lc, char *msg, ...)
96 JCR *jcr = (JCR *)(lc->caller_ctx);
100 va_start(arg_ptr, msg);
101 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
105 Jmsg(jcr, M_FATAL, 0, _("Bootstrap file error: %s\n\
106 : Line %d, col %d of file %s\n%s\n"),
107 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
109 e_msg(file, line, M_FATAL, 0, _("Bootstrap file error: %s\n\
110 : Line %d, col %d of file %s\n%s\n"),
111 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
116 /*********************************************************************
118 * Parse Bootstrap file
121 BSR *parse_bsr(JCR *jcr, char *cf)
125 BSR *root_bsr = new_bsr();
128 Dmsg1(200, "Enter parse_bsf %s\n", cf);
129 lc = lex_open_file(lc, cf, s_err);
130 lc->caller_ctx = (void *)jcr;
131 while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
132 Dmsg1(200, "parse got token=%s\n", lex_tok_to_str(token));
133 if (token == T_EOL) {
136 for (i=0; items[i].name; i++) {
137 if (strcasecmp(items[i].name, lc->str) == 0) {
138 token = lex_get_token(lc, T_ALL);
139 Dmsg1 (200, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
140 if (token != T_EQUALS) {
141 scan_err1(lc, "expected an equals, got: %s", lc->str);
145 Dmsg1(200, "calling handler for %s\n", items[i].name);
146 /* Call item handler */
147 bsr = items[i].handler(lc, bsr);
153 Dmsg1(200, "Keyword = %s\n", lc->str);
154 scan_err1(lc, "Keyword %s not found", lc->str);
162 lc = lex_close_file(lc);
163 Dmsg0(200, "Leave parse_bsf()\n");
169 root_bsr->use_fast_rejection = is_fast_rejection_ok(root_bsr);
174 static bool is_fast_rejection_ok(BSR *bsr)
177 * Although, this can be optimized, for the moment, require
178 * all bsrs to have both sesstime and sessid set before
179 * we do fast rejection.
181 if (!(bsr->sesstime && bsr->sessid)) {
185 return is_fast_rejection_ok(bsr->next);
190 static BSR *store_vol(LEX *lc, BSR *bsr)
196 token = lex_get_token(lc, T_STRING);
197 if (token == T_ERROR) {
201 bsr->next = new_bsr();
204 /* This may actually be more than one volume separated by a |
205 * If so, separate them.
207 for (p=lc->str; p && *p; ) {
212 volume = (BSR_VOLUME *)malloc(sizeof(BSR_VOLUME));
213 memset(volume, 0, sizeof(BSR_VOLUME));
214 bstrncpy(volume->VolumeName, p, sizeof(volume->VolumeName));
215 /* Add it to the end of the volume chain */
217 bsr->volume = volume;
219 BSR_VOLUME *bc = bsr->volume;
220 for ( ;bc->next; bc=bc->next)
229 static BSR *store_client(LEX *lc, BSR *bsr)
235 token = lex_get_token(lc, T_NAME);
236 if (token == T_ERROR) {
239 client = (BSR_CLIENT *)malloc(sizeof(BSR_CLIENT));
240 memset(client, 0, sizeof(BSR_CLIENT));
241 bstrncpy(client->ClientName, lc->str, sizeof(client->ClientName));
242 /* Add it to the end of the client chain */
244 bsr->client = client;
246 BSR_CLIENT *bc = bsr->client;
247 for ( ;bc->next; bc=bc->next)
251 token = lex_get_token(lc, T_ALL);
252 if (token != T_COMMA) {
259 static BSR *store_job(LEX *lc, BSR *bsr)
265 token = lex_get_token(lc, T_NAME);
266 if (token == T_ERROR) {
269 job = (BSR_JOB *)malloc(sizeof(BSR_JOB));
270 memset(job, 0, sizeof(BSR_JOB));
271 bstrncpy(job->Job, lc->str, sizeof(job->Job));
272 /* Add it to the end of the client chain */
276 /* Add to end of chain */
277 BSR_JOB *bc = bsr->job;
278 for ( ;bc->next; bc=bc->next)
282 token = lex_get_token(lc, T_ALL);
283 if (token != T_COMMA) {
290 static BSR *store_findex(LEX *lc, BSR *bsr)
296 token = lex_get_token(lc, T_PINT32_RANGE);
297 if (token == T_ERROR) {
300 findex = (BSR_FINDEX *)malloc(sizeof(BSR_FINDEX));
301 memset(findex, 0, sizeof(BSR_FINDEX));
302 findex->findex = lc->pint32_val;
303 findex->findex2 = lc->pint32_val2;
304 /* Add it to the end of the chain */
305 if (!bsr->FileIndex) {
306 bsr->FileIndex = findex;
308 /* Add to end of chain */
309 BSR_FINDEX *bs = bsr->FileIndex;
310 for ( ;bs->next; bs=bs->next)
314 token = lex_get_token(lc, T_ALL);
315 if (token != T_COMMA) {
323 static BSR *store_jobid(LEX *lc, BSR *bsr)
329 token = lex_get_token(lc, T_PINT32_RANGE);
330 if (token == T_ERROR) {
333 jobid = (BSR_JOBID *)malloc(sizeof(BSR_JOBID));
334 memset(jobid, 0, sizeof(BSR_JOBID));
335 jobid->JobId = lc->pint32_val;
336 jobid->JobId2 = lc->pint32_val2;
337 /* Add it to the end of the chain */
341 /* Add to end of chain */
342 BSR_JOBID *bs = bsr->JobId;
343 for ( ;bs->next; bs=bs->next)
347 token = lex_get_token(lc, T_ALL);
348 if (token != T_COMMA) {
356 static BSR *store_count(LEX *lc, BSR *bsr)
360 token = lex_get_token(lc, T_PINT32);
361 if (token == T_ERROR) {
364 bsr->count = lc->pint32_val;
370 static BSR *store_jobtype(LEX *lc, BSR *bsr)
372 /* *****FIXME****** */
373 Dmsg0(-1, "JobType not yet implemented\n");
378 static BSR *store_joblevel(LEX *lc, BSR *bsr)
380 /* *****FIXME****** */
381 Dmsg0(-1, "JobLevel not yet implemented\n");
389 * Routine to handle Volume start/end file
391 static BSR *store_volfile(LEX *lc, BSR *bsr)
394 BSR_VOLFILE *volfile;
397 token = lex_get_token(lc, T_PINT32_RANGE);
398 if (token == T_ERROR) {
401 volfile = (BSR_VOLFILE *)malloc(sizeof(BSR_VOLFILE));
402 memset(volfile, 0, sizeof(BSR_VOLFILE));
403 volfile->sfile = lc->pint32_val;
404 volfile->efile = lc->pint32_val2;
405 /* Add it to the end of the chain */
407 bsr->volfile = volfile;
409 /* Add to end of chain */
410 BSR_VOLFILE *bs = bsr->volfile;
411 for ( ;bs->next; bs=bs->next)
415 token = lex_get_token(lc, T_ALL);
416 if (token != T_COMMA) {
425 * Routine to handle Volume start/end Block
427 static BSR *store_volblock(LEX *lc, BSR *bsr)
430 BSR_VOLBLOCK *volblock;
433 token = lex_get_token(lc, T_PINT32_RANGE);
434 if (token == T_ERROR) {
437 volblock = (BSR_VOLBLOCK *)malloc(sizeof(BSR_VOLBLOCK));
438 memset(volblock, 0, sizeof(BSR_VOLBLOCK));
439 volblock->sblock = lc->pint32_val;
440 volblock->eblock = lc->pint32_val2;
441 /* Add it to the end of the chain */
442 if (!bsr->volblock) {
443 bsr->volblock = volblock;
445 /* Add to end of chain */
446 BSR_VOLBLOCK *bs = bsr->volblock;
447 for ( ;bs->next; bs=bs->next)
451 token = lex_get_token(lc, T_ALL);
452 if (token != T_COMMA) {
460 static BSR *store_sessid(LEX *lc, BSR *bsr)
466 token = lex_get_token(lc, T_PINT32_RANGE);
467 if (token == T_ERROR) {
470 sid = (BSR_SESSID *)malloc(sizeof(BSR_SESSID));
471 memset(sid, 0, sizeof(BSR_SESSID));
472 sid->sessid = lc->pint32_val;
473 sid->sessid2 = lc->pint32_val2;
474 /* Add it to the end of the chain */
478 /* Add to end of chain */
479 BSR_SESSID *bs = bsr->sessid;
480 for ( ;bs->next; bs=bs->next)
484 token = lex_get_token(lc, T_ALL);
485 if (token != T_COMMA) {
492 static BSR *store_sesstime(LEX *lc, BSR *bsr)
498 token = lex_get_token(lc, T_PINT32);
499 if (token == T_ERROR) {
502 stime = (BSR_SESSTIME *)malloc(sizeof(BSR_SESSTIME));
503 memset(stime, 0, sizeof(BSR_SESSTIME));
504 stime->sesstime = lc->pint32_val;
505 /* Add it to the end of the chain */
506 if (!bsr->sesstime) {
507 bsr->sesstime = stime;
509 /* Add to end of chain */
510 BSR_SESSTIME *bs = bsr->sesstime;
511 for ( ;bs->next; bs=bs->next)
515 token = lex_get_token(lc, T_ALL);
516 if (token != T_COMMA) {
524 static BSR *store_stream(LEX *lc, BSR *bsr)
530 token = lex_get_token(lc, T_INT32);
531 if (token == T_ERROR) {
534 stream = (BSR_STREAM *)malloc(sizeof(BSR_STREAM));
535 memset(stream, 0, sizeof(BSR_STREAM));
536 stream->stream = lc->int32_val;
537 /* Add it to the end of the chain */
539 bsr->stream = stream;
541 /* Add to end of chain */
542 BSR_STREAM *bs = bsr->stream;
543 for ( ;bs->next; bs=bs->next)
547 token = lex_get_token(lc, T_ALL);
548 if (token != T_COMMA) {
555 static BSR *store_slot(LEX *lc, BSR *bsr)
559 token = lex_get_token(lc, T_PINT32);
560 if (token == T_ERROR) {
563 bsr->Slot = lc->pint32_val;
568 static BSR *store_include(LEX *lc, BSR *bsr)
574 static BSR *store_exclude(LEX *lc, BSR *bsr)
580 void dump_volfile(BSR_VOLFILE *volfile)
583 Dmsg2(-1, "VolFile : %u-%u\n", volfile->sfile, volfile->efile);
584 dump_volfile(volfile->next);
588 void dump_volblock(BSR_VOLBLOCK *volblock)
591 Dmsg2(-1, "VolBlock : %u-%u\n", volblock->sblock, volblock->eblock);
592 dump_volblock(volblock->next);
597 void dump_findex(BSR_FINDEX *FileIndex)
600 if (FileIndex->findex == FileIndex->findex2) {
601 Dmsg1(-1, "FileIndex : %u\n", FileIndex->findex);
603 Dmsg2(-1, "FileIndex : %u-%u\n", FileIndex->findex, FileIndex->findex2);
605 dump_findex(FileIndex->next);
609 void dump_jobid(BSR_JOBID *jobid)
612 if (jobid->JobId == jobid->JobId2) {
613 Dmsg1(-1, "JobId : %u\n", jobid->JobId);
615 Dmsg2(-1, "JobId : %u-%u\n", jobid->JobId, jobid->JobId2);
617 dump_jobid(jobid->next);
621 void dump_sessid(BSR_SESSID *sessid)
624 if (sessid->sessid == sessid->sessid2) {
625 Dmsg1(-1, "SessId : %u\n", sessid->sessid);
627 Dmsg2(-1, "SessId : %u-%u\n", sessid->sessid, sessid->sessid2);
629 dump_sessid(sessid->next);
633 void dump_volume(BSR_VOLUME *volume)
636 Dmsg1(-1, "VolumeName : %s\n", volume->VolumeName);
637 dump_volume(volume->next);
642 void dump_client(BSR_CLIENT *client)
645 Dmsg1(-1, "Client : %s\n", client->ClientName);
646 dump_client(client->next);
650 void dump_job(BSR_JOB *job)
653 Dmsg1(-1, "Job : %s\n", job->Job);
658 void dump_sesstime(BSR_SESSTIME *sesstime)
661 Dmsg1(-1, "SessTime : %u\n", sesstime->sesstime);
662 dump_sesstime(sesstime->next);
670 void dump_bsr(BSR *bsr)
673 Dmsg0(-1, "BSR is NULL\n");
677 "Next : 0x%x\n", bsr->next);
678 dump_volume(bsr->volume);
679 dump_sessid(bsr->sessid);
680 dump_sesstime(bsr->sesstime);
681 dump_volfile(bsr->volfile);
682 dump_volblock(bsr->volblock);
683 dump_client(bsr->client);
684 dump_jobid(bsr->JobId);
686 dump_findex(bsr->FileIndex);
688 Dmsg1(-1, "Slot : %u\n", bsr->Slot);
691 Dmsg1(-1, "count : %u\n", bsr->count);
701 /*********************************************************************
706 static void free_bsr_item(BSR *bsr)
709 free_bsr_item(bsr->next);
714 void free_bsr(BSR *bsr)
719 free_bsr_item((BSR *)bsr->volume);
720 free_bsr_item((BSR *)bsr->client);
721 free_bsr_item((BSR *)bsr->sessid);
722 free_bsr_item((BSR *)bsr->sesstime);
723 free_bsr_item((BSR *)bsr->volfile);
724 free_bsr_item((BSR *)bsr->volblock);
725 free_bsr_item((BSR *)bsr->JobId);
726 free_bsr_item((BSR *)bsr->job);
727 free_bsr_item((BSR *)bsr->FileIndex);
728 free_bsr_item((BSR *)bsr->JobType);
729 free_bsr_item((BSR *)bsr->JobLevel);
734 /*****************************************************************
735 * Routines for handling volumes
740 vol = (VOL_LIST *)malloc(sizeof(VOL_LIST));
741 memset(vol, 0, sizeof(VOL_LIST));
746 * Add current volume to end of list, only if the Volume
747 * is not already in the list.
749 * returns: 1 if volume added
750 * 0 if volume already in list
752 int add_vol(JCR *jcr, VOL_LIST *vol)
754 VOL_LIST *next = jcr->VolList;
756 if (!next) { /* list empty ? */
757 jcr->VolList = vol; /* yes, add volume */
759 for ( ; next->next; next=next->next) {
760 if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
761 if (vol->start_file < next->start_file) {
762 next->start_file = vol->start_file;
764 return 0; /* already in list */
767 if (strcmp(vol->VolumeName, next->VolumeName) == 0) {
768 if (vol->start_file < next->start_file) {
769 next->start_file = vol->start_file;
771 return 0; /* already in list */
773 next->next = vol; /* add volume */
778 void free_vol_list(JCR *jcr)
780 VOL_LIST *next = jcr->VolList;
792 * Create a list of Volumes (and Slots and Start positions) to be
793 * used in the current restore job.
795 void create_vol_list(JCR *jcr)
801 * Build a list of volumes to be processed
807 if (!bsr->volume || !bsr->volume->VolumeName) {
810 for ( ; bsr; bsr=bsr->next) {
812 BSR_VOLFILE *volfile;
813 uint32_t sfile = UINT32_MAX;
815 /* Find minimum start file so that we can forward space to it */
816 for (volfile = bsr->volfile; volfile; volfile=volfile->next) {
817 if (volfile->sfile < sfile) {
818 sfile = volfile->sfile;
821 /* Now add volumes for this bsr */
822 for (bsrvol = bsr->volume; bsrvol; bsrvol=bsrvol->next) {
824 bstrncpy(vol->VolumeName, bsrvol->VolumeName, sizeof(vol->VolumeName));
825 vol->start_file = sfile;
826 if (add_vol(jcr, vol)) {
828 Dmsg1(400, "Added volume %s\n", vol->VolumeName);
830 Dmsg1(400, "Duplicate volume %s\n", vol->VolumeName);
833 sfile = 0; /* start at beginning of second volume */
837 /* This is the old way -- deprecated */
838 for (p = jcr->VolumeName; p && *p; ) {
839 n = strchr(p, '|'); /* volume name separator */
841 *n++ = 0; /* Terminate name */
844 bstrncpy(vol->VolumeName, p, sizeof(vol->VolumeName));
845 if (add_vol(jcr, vol)) {