2 * Match Bootstrap Records (used for restores) against
5 * Kern Sibbald, June MMII
10 Copyright (C) 2002-2006 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
14 version 2 as amended with additional clauses defined in the
15 file LICENSE in the main source directory.
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
20 the file LICENSE for additional details.
30 #include "lib/fnmatch.h"
33 /* Forward references */
34 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done);
35 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done);
36 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec);
37 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done);
38 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done);
39 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done);
40 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done);
41 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done);
42 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done);
43 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done);
44 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done);
45 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec, bool done);
46 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block);
47 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block);
48 static BSR *find_smallest_volfile(BSR *fbsr, BSR *bsr);
51 /*********************************************************************
53 * If possible, position the archive device (tape) to read the
56 void position_bsr_block(BSR *bsr, DEV_BLOCK *block)
58 /* To be implemented */
61 /*********************************************************************
63 * Do fast block rejection based on bootstrap records.
64 * use_fast_rejection will be set if we have VolSessionId and VolSessTime
65 * in each record. When BlockVer is >= 2, we have those in the block header
66 * so can do fast rejection.
68 * returns: 1 if block may contain valid records
69 * 0 if block may be skipped (i.e. it contains no records of
70 * that can match the bsr).
73 int match_bsr_block(BSR *bsr, DEV_BLOCK *block)
75 if (!bsr || !bsr->use_fast_rejection || (block->BlockVer < 2)) {
76 return 1; /* cannot fast reject */
79 for ( ; bsr; bsr=bsr->next) {
80 if (!match_block_sesstime(bsr, bsr->sesstime, block)) {
83 if (!match_block_sessid(bsr, bsr->sessid, block)) {
91 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block)
94 return 1; /* no specification matches all */
96 if (sesstime->sesstime == block->VolSessionTime) {
100 return match_block_sesstime(bsr, sesstime->next, block);
105 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block)
108 return 1; /* no specification matches all */
110 if (sessid->sessid <= block->VolSessionId && sessid->sessid2 >= block->VolSessionId) {
114 return match_block_sessid(bsr, sessid->next, block);
120 /*********************************************************************
122 * Match Bootstrap records
124 * returns 0 no match and reposition is set if we should
125 * reposition the tape
126 * returns -1 no additional matches possible
128 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
133 * The bsr->reposition flag is set any time a bsr is done.
134 * In this case, we can probably reposition the
135 * tape to the next available bsr position.
138 bsr->reposition = false;
139 stat = match_all(bsr, rec, volrec, sessrec, true);
141 * Note, bsr->reposition is set by match_all when
142 * a bsr is done. We turn it off if a match was
143 * found or if we cannot use positioning
145 if (stat != 0 || !bsr->use_positioning) {
146 bsr->reposition = false;
149 stat = 1; /* no bsr => match all */
155 * Find the next bsr that applies to the current tape.
156 * It is the one with the smallest VolFile position.
158 BSR *find_next_bsr(BSR *root_bsr, DEVICE *dev)
161 BSR *found_bsr = NULL;
162 bool no_file_seek = !dev->is_tape();
164 no_file_seek = false;
167 if (!root_bsr || !root_bsr->use_positioning ||
168 !root_bsr->reposition || no_file_seek) {
169 Dmsg2(300, "No nxt_bsr use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
172 Dmsg2(300, "use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
173 root_bsr->mount_next_volume = false;
174 for (bsr=root_bsr; bsr; bsr=bsr->next) {
175 if (bsr->done || !match_volume(bsr, bsr->volume, &dev->VolHdr, 1)) {
178 if (found_bsr == NULL) {
181 found_bsr = find_smallest_volfile(found_bsr, bsr);
185 * If we get to this point and found no bsr, it means
186 * that any additional bsr's must apply to the next
187 * tape, so set a flag.
189 if (found_bsr == NULL) {
190 root_bsr->mount_next_volume = true;
195 static BSR *find_smallest_volfile(BSR *found_bsr, BSR *bsr)
197 BSR *return_bsr = found_bsr;
200 uint32_t found_bsr_sfile, bsr_sfile;
201 uint32_t found_bsr_sblock, bsr_sblock;
203 vf = found_bsr->volfile;
204 found_bsr_sfile = vf->sfile;
205 while ( (vf=vf->next) ) {
206 if (vf->sfile < found_bsr_sfile) {
207 found_bsr_sfile = vf->sfile;
211 bsr_sfile = vf->sfile;
212 while ( (vf=vf->next) ) {
213 if (vf->sfile < bsr_sfile) {
214 bsr_sfile = vf->sfile;
217 if (found_bsr_sfile > bsr_sfile) {
219 } else if (found_bsr_sfile == bsr_sfile) {
220 /* Must check block */
221 vb = found_bsr->volblock;
222 found_bsr_sblock = vb->sblock;
223 while ( (vb=vb->next) ) {
224 if (vb->sblock < found_bsr_sblock) {
225 found_bsr_sblock = vb->sblock;
229 bsr_sblock = vb->sblock;
230 while ( (vb=vb->next) ) {
231 if (vb->sblock < bsr_sblock) {
232 bsr_sblock = vb->sblock;
235 if (found_bsr_sblock > bsr_sblock) {
244 * Called to tell the matcher that the end of
245 * the current file has been reached.
246 * The bsr argument is not used, but is included
247 * for consistency with the other match calls.
249 * Returns: true if we should reposition
252 bool match_set_eof(BSR *bsr, DEV_RECORD *rec)
254 BSR *rbsr = rec->bsr;
255 Dmsg1(300, "match_set %d\n", rbsr != NULL);
261 if (rbsr->count && rbsr->found >= rbsr->count) {
263 rbsr->root->reposition = true;
264 Dmsg2(500, "match_set_eof reposition count=%d found=%d\n",
265 rbsr->count, rbsr->found);
272 * Match all the components of current record
275 * returns -1 no additional matches possible
277 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec,
278 SESSION_LABEL *sessrec, bool done)
281 Dmsg0(300, "bsr->done set\n");
284 if (!match_volume(bsr, bsr->volume, volrec, 1)) {
285 Dmsg2(300, "bsr vol=%s != rec vol=%s\n", bsr->volume, volrec);
288 if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
289 Dmsg2(300, "Fail on file. bsr=%d rec=%d\n", bsr->volfile->efile,
293 if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
294 Dmsg2(300, "Fail on sesstime. bsr=%d rec=%d\n",
295 bsr->sesstime->sesstime, rec->VolSessionTime);
299 /* NOTE!! This test MUST come after the sesstime test */
300 if (!match_sessid(bsr, bsr->sessid, rec)) {
301 Dmsg2(300, "Fail on sessid. bsr=%d rec=%d\n",
302 bsr->sessid->sessid, rec->VolSessionId);
306 /* NOTE!! This test MUST come after sesstime and sessid tests */
307 if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
308 Dmsg2(300, "Fail on findex. bsr=%d rec=%d\n",
309 bsr->FileIndex->findex2, rec->FileIndex);
313 * If a count was specified and we have a FileIndex, assume
314 * it is a Bacula created bsr (or the equivalent). We
315 * then save the bsr where the match occurred so that
316 * after processing the record or records, we can update
317 * the found count. I.e. rec->bsr points to the bsr that
318 * satisfied the match.
320 if (bsr->count && bsr->FileIndex) {
322 return 1; /* this is a complete match */
326 * The selections below are not used by Bacula's
327 * restore command, and don't work because of
328 * the rec->bsr = bsr optimization above.
330 if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
331 Dmsg0(300, "fail on JobId\n");
335 if (!match_job(bsr, bsr->job, sessrec, 1)) {
336 Dmsg0(300, "fail on Job\n");
339 if (!match_client(bsr, bsr->client, sessrec, 1)) {
340 Dmsg0(300, "fail on Client\n");
343 if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
344 Dmsg0(300, "fail on Job type\n");
347 if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
348 Dmsg0(300, "fail on Job level\n");
351 if (!match_stream(bsr, bsr->stream, rec, 1)) {
352 Dmsg0(300, "fail on stream\n");
359 return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
361 if (bsr->done && done) {
367 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done)
370 return 0; /* Volume must match */
372 if (strcmp(volume->VolumeName, volrec->VolumeName) == 0) {
376 return match_volume(bsr, volume->next, volrec, 1);
381 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
384 return 1; /* no specification matches all */
386 if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
390 return match_client(bsr, client->next, sessrec, 1);
395 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
398 return 1; /* no specification matches all */
400 if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
404 return match_job(bsr, job->next, sessrec, 1);
409 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
412 return 1; /* no specification matches all */
414 if (job_type->JobType == sessrec->JobType) {
417 if (job_type->next) {
418 return match_job_type(bsr, job_type->next, sessrec, 1);
423 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
426 return 1; /* no specification matches all */
428 if (job_level->JobLevel == sessrec->JobLevel) {
431 if (job_level->next) {
432 return match_job_level(bsr, job_level->next, sessrec, 1);
437 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
440 return 1; /* no specification matches all */
442 if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
446 return match_jobid(bsr, jobid->next, sessrec, 1);
451 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
454 return 1; /* no specification matches all */
456 /* For the moment, these tests work only with tapes. */
457 if (!(rec->state & REC_ISTAPE)) {
458 return 1; /* All File records OK for this match */
460 // Dmsg3(300, "match_volfile: sfile=%d efile=%d recfile=%d\n",
461 // volfile->sfile, volfile->efile, rec->File);
462 if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
465 /* Once we get past last efile, we are done */
466 if (rec->File > volfile->efile) {
467 volfile->done = true; /* set local done */
470 return match_volfile(bsr, volfile->next, rec, volfile->done && done);
473 /* If we are done and all prior matches are done, this bsr is finished */
474 if (volfile->done && done) {
476 bsr->root->reposition = true;
477 Dmsg2(300, "bsr done from volfile rec=%d volefile=%d\n",
478 rec->File, volfile->efile);
483 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
486 return 1; /* no specification matches all */
488 if (stream->stream == rec->Stream) {
492 return match_stream(bsr, stream->next, rec, 1);
497 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
500 return 1; /* no specification matches all */
502 if (sesstime->sesstime == rec->VolSessionTime) {
505 if (rec->VolSessionTime > sesstime->sesstime) {
506 sesstime->done = true;
508 if (sesstime->next) {
509 return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
511 if (sesstime->done && done) {
513 bsr->root->reposition = true;
514 Dmsg0(300, "bsr done from sesstime\n");
519 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
522 return 1; /* no specification matches all */
524 if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
528 return match_sessid(bsr, sessid->next, rec);
534 * When reading the Volume, the Volume Findex (rec->FileIndex) always
535 * are found in sequential order. Thus we can make optimizations.
537 * ***FIXME*** optimizations
538 * We could optimize a lot here by removing the recursion, and
539 * stopping the search earlier -- say when rec->FileIndex > findex->findex2
540 * and findex->next == NULL. Also, the current entry tests could be skipped
541 * if findex->done is set.
543 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
546 return 1; /* no specification matches all */
548 if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
551 if (rec->FileIndex > findex->findex2) {
555 return match_findex(bsr, findex->next, rec, findex->done && done);
557 if (findex->done && done) {
559 bsr->root->reposition = true;
560 Dmsg1(300, "bsr done from findex %d\n", rec->FileIndex);