]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/match_bsr.c
Implement block rejection during read
[bacula/bacula] / bacula / src / stored / match_bsr.c
1 /*     
2  *   Match Bootstrap Records (used for restores) against
3  *     Volume Records
4  *  
5  *     Kern Sibbald, June MMII
6  *
7  *   Version $Id$
8  */
9
10 /*
11    Copyright (C) 2000-2003 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "stored.h"
32 #include <fnmatch.h>
33
34 /* Forward references */
35 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done);
36 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done);
37 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec);
38 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done);
39 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done);
40 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done);
41 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done);
42 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done);
43 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done);
44 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done);
45 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done);
46 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec, bool done);
47 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block);
48 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block);
49
50 /*********************************************************************
51  *
52  *  If possible, position the archive device (tape) to read the
53  *  next block.
54  */
55 void position_bsr_block(BSR *bsr, DEV_BLOCK *block)
56 {
57    /* To be implemented */
58 }
59
60 /*********************************************************************
61  *
62  *  Do fast block rejection based on bootstrap records. 
63  *    use_fast_rejection will be set if we have VolSessionId and VolSessTime
64  *    in each record. When BlockVer is >= 2, we have those in the block header
65  *    so can do fast rejection.
66  *
67  *   returns:  1 if block may contain valid records
68  *             0 if block may be skipped (i.e. it contains no records of
69  *                  that can match the bsr).
70  *
71  */
72 int match_bsr_block(BSR *bsr, DEV_BLOCK *block)
73 {
74    if (!bsr || !bsr->use_fast_rejection || (block->BlockVer < 2)) {
75       return 1;                       /* cannot fast reject */
76    }
77
78    if (match_block_sesstime(bsr, bsr->sesstime, block)) {
79       return 1;
80    }
81    return match_block_sessid(bsr, bsr->sessid, block);
82 }
83
84 static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block)
85 {
86    if (!sesstime) {
87       return 1;                       /* no specification matches all */
88    }
89    if (sesstime->sesstime == block->VolSessionTime) {
90       return 1;
91    }
92    if (sesstime->next) {
93       return match_block_sesstime(bsr, sesstime->next, block);
94    }
95    return 0;
96 }
97
98 static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block)
99 {
100    if (!sessid) {
101       return 1;                       /* no specification matches all */
102    }
103    if (sessid->sessid <= block->VolSessionId && sessid->sessid2 >= block->VolSessionId) {
104       return 1;
105    }
106    if (sessid->next) {
107       return match_block_sessid(bsr, sessid->next, block);
108    }
109    return 0;
110 }
111
112
113 /*********************************************************************
114  *
115  *      Match Bootstrap records
116  *        returns  1 on match
117  *        returns  0 no match
118  *       returns -1 no additional matches possible
119  */
120 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
121 {
122    int stat;
123
124    if (bsr) {
125       stat = match_all(bsr, rec, volrec, sessrec, true);
126    } else {
127       stat = 0;
128    }
129 // Dmsg1(000, "BSR returning %d\n", stat);
130    return stat;
131 }
132
133 /* 
134  * Match all the components of current record
135  *   returns  1 on match
136  *   returns  0 no match
137  *   returns -1 no additional matches possible
138  */
139 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, 
140                      SESSION_LABEL *sessrec, bool done)
141 {
142    if (bsr->done) {
143       goto no_match;
144    }
145    if (bsr->count && bsr->count <= bsr->found) {
146       bsr->done = true;
147       goto no_match;
148    }
149    if (!match_volume(bsr, bsr->volume, volrec, 1)) {
150       goto no_match;
151    }
152    if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
153       goto no_match;
154    }
155    if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
156       goto no_match;
157    }
158
159    /* NOTE!! This test MUST come after the sesstime test */
160    if (!match_sessid(bsr, bsr->sessid, rec)) {
161       goto no_match;
162    }
163
164    /* NOTE!! This test MUST come after sesstime and sessid tests */
165    if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
166       goto no_match;
167    }
168    if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
169       goto no_match;
170    }
171    if (!match_job(bsr, bsr->job, sessrec, 1)) {
172       goto no_match;
173    }
174    if (!match_client(bsr, bsr->client, sessrec, 1)) {
175       goto no_match;
176    }
177    if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
178       goto no_match;
179    }
180    if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
181       goto no_match;
182    }
183    if (!match_stream(bsr, bsr->stream, rec, 1)) {
184       goto no_match;
185    }
186    bsr->found++;
187    return 1;
188
189 no_match:
190    if (bsr->next) {
191       return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
192    }
193    if (bsr->done && done) {
194       return -1;
195    }
196    return 0;
197 }
198
199 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, bool done) 
200 {
201    if (!volume) {
202       return 0;                       /* Volume must match */
203    }
204    if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
205       return 1;
206    }
207    if (volume->next) {
208       return match_volume(bsr, volume->next, volrec, 1);
209    }
210    return 0;
211 }
212
213 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, bool done)
214 {
215    if (!client) {
216       return 1;                       /* no specification matches all */
217    }
218    if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
219       return 1;
220    }
221    if (client->next) {
222       return match_client(bsr, client->next, sessrec, 1);
223    }
224    return 0;
225 }
226
227 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, bool done)
228 {
229    if (!job) {
230       return 1;                       /* no specification matches all */
231    }
232    if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
233       return 1;
234    }
235    if (job->next) {
236       return match_job(bsr, job->next, sessrec, 1);
237    }
238    return 0;
239 }
240
241 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, bool done)
242 {
243    if (!job_type) {
244       return 1;                       /* no specification matches all */
245    }
246    if (job_type->JobType == sessrec->JobType) {
247       return 1;
248    }
249    if (job_type->next) {
250       return match_job_type(bsr, job_type->next, sessrec, 1);
251    }
252    return 0;
253 }
254
255 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, bool done)
256 {
257    if (!job_level) {
258       return 1;                       /* no specification matches all */
259    }
260    if (job_level->JobLevel == sessrec->JobLevel) {
261       return 1;
262    }
263    if (job_level->next) {
264       return match_job_level(bsr, job_level->next, sessrec, 1);
265    }
266    return 0;
267 }
268
269 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, bool done)
270 {
271    if (!jobid) {
272       return 1;                       /* no specification matches all */
273    }
274    if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
275       return 1;
276    }
277    if (jobid->next) {
278       return match_jobid(bsr, jobid->next, sessrec, 1);
279    }
280    return 0;
281 }
282
283 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, bool done)
284 {
285    if (!volfile) {
286       return 1;                       /* no specification matches all */
287    }
288    /* For the moment, these tests work only with tapes. */
289    if (!(rec->state & REC_ISTAPE)) {
290       return 1;                       /* All File records OK for this match */
291    }
292 // Dmsg3(000, "match_volfile: sfile=%d efile=%d recfile=%d\n",
293 //             volfile->sfile, volfile->efile, rec->File);
294    if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
295       return 1;
296    }
297    /* Once we get past last efile, we are done */
298    if (rec->File > volfile->efile) {
299       volfile->done = true;              /* set local done */
300    }
301    if (volfile->next) {
302       return match_volfile(bsr, volfile->next, rec, volfile->done && done);
303    }
304
305    /* If we are done and all prior matches are done, this bsr is finished */
306    if (volfile->done && done) {
307       bsr->done = true;
308    }
309    return 0;
310 }
311
312 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, bool done)
313 {
314    if (!stream) {
315       return 1;                       /* no specification matches all */
316    }
317    if (stream->stream == rec->Stream) {
318       return 1;
319    }
320    if (stream->next) {
321       return match_stream(bsr, stream->next, rec, 1);
322    }
323    return 0;
324 }
325
326 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, bool done)
327 {
328    if (!sesstime) {
329       return 1;                       /* no specification matches all */
330    }
331    if (sesstime->sesstime == rec->VolSessionTime) {
332       return 1;
333    }
334    if (rec->VolSessionTime > sesstime->sesstime) {
335       sesstime->done = true;
336    }
337    if (sesstime->next) {
338       return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
339    }
340    if (sesstime->done && done) {
341       bsr->done = true;
342    }
343    return 0;
344 }
345
346 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec)
347 {
348    if (!sessid) {
349       return 1;                       /* no specification matches all */
350    }
351    if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
352       return 1;
353    }
354    if (sessid->next) {
355       return match_sessid(bsr, sessid->next, rec);
356    }
357    return 0;
358 }
359
360 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, bool done)
361 {
362    if (!findex) {
363       return 1;                       /* no specification matches all */
364    }
365    if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
366       return 1;
367    }
368    if (rec->FileIndex > findex->findex2) {
369       findex->done = true;
370    }
371    if (findex->next) {
372       return match_findex(bsr, findex->next, rec, findex->done && done);
373    }
374    if (findex->done && done) {
375       bsr->done = true;
376    }
377    return 0;
378 }