]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/match_bsr.c
Make btape fill/unfill work
[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) 2002 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
31 #include "bacula.h"
32 #include "stored.h"
33 #include <fnmatch.h>
34
35 /* Forward references */
36 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, int done);
37 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, int done);
38 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec, int done);
39 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, int done);
40 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, int done);
41 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, int done);
42 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, int done);
43 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, int done);
44 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, int done);
45 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, int done);
46 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, int done);
47 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec, int done);
48
49 /*********************************************************************
50  *
51  *      Match Bootstrap records
52  *        returns  1 on match
53  *        returns  0 no match
54  *       returns -1 no additional matches possible
55  */
56 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
57 {
58    int stat;
59
60    if (bsr) {
61       stat = match_all(bsr, rec, volrec, sessrec, 1);
62    } else {
63       stat = 0;
64    }
65 // Dmsg1(000, "BSR returning %d\n", stat);
66    return stat;
67 }
68
69 /* 
70  * Match all the components of current record
71  *   returns  1 on match
72  *   returns  0 no match
73  *   returns -1 no additional matches possible
74  */
75 static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, 
76                      SESSION_LABEL *sessrec, int done)
77 {
78    if (bsr->done) {
79       goto no_match;
80    }
81    if (bsr->count && bsr->count <= bsr->found) {
82       bsr->done = 1;
83       goto no_match;
84    }
85    if (!match_volume(bsr, bsr->volume, volrec, 1)) {
86       goto no_match;
87    }
88    if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
89       goto no_match;
90    }
91    if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
92       goto no_match;
93    }
94
95    /* NOTE!! This test MUST come after the sesstime test */
96    if (!match_sessid(bsr, bsr->sessid, rec, 1)) {
97       goto no_match;
98    }
99
100    /* NOTE!! This test MUST come after sesstime and sessid tests */
101    if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
102       goto no_match;
103    }
104    if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
105       goto no_match;
106    }
107    if (!match_job(bsr, bsr->job, sessrec, 1)) {
108       goto no_match;
109    }
110    if (!match_client(bsr, bsr->client, sessrec, 1)) {
111       goto no_match;
112    }
113    if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
114       goto no_match;
115    }
116    if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
117       goto no_match;
118    }
119    if (!match_stream(bsr, bsr->stream, rec, 1)) {
120       goto no_match;
121    }
122    bsr->found++;
123    return 1;
124
125 no_match:
126    if (bsr->next) {
127       return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
128    }
129    if (bsr->done && done) {
130       return -1;
131    }
132    return 0;
133 }
134
135 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, int done) 
136 {
137    if (!volume) {
138       return 0;                       /* Volume must match */
139    }
140    if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
141       return 1;
142    }
143    if (volume->next) {
144       return match_volume(bsr, volume->next, volrec, 1);
145    }
146    return 0;
147 }
148
149 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, int done)
150 {
151    if (!client) {
152       return 1;                       /* no specification matches all */
153    }
154    if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
155       return 1;
156    }
157    if (client->next) {
158       return match_client(bsr, client->next, sessrec, 1);
159    }
160    return 0;
161 }
162
163 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, int done)
164 {
165    if (!job) {
166       return 1;                       /* no specification matches all */
167    }
168    if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
169       return 1;
170    }
171    if (job->next) {
172       return match_job(bsr, job->next, sessrec, 1);
173    }
174    return 0;
175 }
176
177 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, int done)
178 {
179    if (!job_type) {
180       return 1;                       /* no specification matches all */
181    }
182    if (job_type->JobType == sessrec->JobType) {
183       return 1;
184    }
185    if (job_type->next) {
186       return match_job_type(bsr, job_type->next, sessrec, 1);
187    }
188    return 0;
189 }
190
191 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, int done)
192 {
193    if (!job_level) {
194       return 1;                       /* no specification matches all */
195    }
196    if (job_level->JobLevel == sessrec->JobLevel) {
197       return 1;
198    }
199    if (job_level->next) {
200       return match_job_level(bsr, job_level->next, sessrec, 1);
201    }
202    return 0;
203 }
204
205 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, int done)
206 {
207    if (!jobid) {
208       return 1;                       /* no specification matches all */
209    }
210    if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
211       return 1;
212    }
213    if (jobid->next) {
214       return match_jobid(bsr, jobid->next, sessrec, 1);
215    }
216    return 0;
217 }
218
219 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, int done)
220 {
221    if (!volfile) {
222       return 1;                       /* no specification matches all */
223    }
224    /* For the moment, these tests work only with tapes. */
225    if (!(rec->state & REC_ISTAPE)) {
226       return 1;                       /* All File records OK for this match */
227    }
228 // Dmsg3(000, "match_volfile: sfile=%d efile=%d recfile=%d\n",
229 //             volfile->sfile, volfile->efile, rec->File);
230    if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
231       return 1;
232    }
233    /* Once we get past last efile, we are done */
234    if (rec->File > volfile->efile) {
235       volfile->done = 1;              /* set local done */
236    }
237    if (volfile->next) {
238       return match_volfile(bsr, volfile->next, rec, volfile->done && done);
239    }
240
241    /* If we are done and all prior matches are done, this bsr is finished */
242    if (volfile->done && done) {
243       bsr->done = 1;
244    }
245    return 0;
246 }
247
248 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, int done)
249 {
250    if (!stream) {
251       return 1;                       /* no specification matches all */
252    }
253    if (stream->stream == rec->Stream) {
254       return 1;
255    }
256    if (stream->next) {
257       return match_stream(bsr, stream->next, rec, 1);
258    }
259    return 0;
260 }
261
262 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, int done)
263 {
264    if (!sesstime) {
265       return 1;                       /* no specification matches all */
266    }
267    if (sesstime->sesstime == rec->VolSessionTime) {
268       return 1;
269    }
270    if (rec->VolSessionTime > sesstime->sesstime) {
271       sesstime->done = 1;
272    }
273    if (sesstime->next) {
274       return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
275    }
276    if (sesstime->done && done) {
277       bsr->done = 1;
278    }
279    return 0;
280 }
281
282 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec, int done)
283 {
284    if (!sessid) {
285       return 1;                       /* no specification matches all */
286    }
287    if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
288       return 1;
289    }
290    if (rec->VolSessionId > sessid->sessid2) {
291       sessid->done = 1;
292    }
293    if (sessid->next) {
294       return match_sessid(bsr, sessid->next, rec, sessid->done && done);
295    }
296    if (sessid->done && done) {
297       bsr->done = 1;
298    }
299    return 0;
300 }
301
302 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, int done)
303 {
304    if (!findex) {
305       return 1;                       /* no specification matches all */
306    }
307    if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
308       return 1;
309    }
310    if (rec->FileIndex > findex->findex2) {
311       findex->done = 1;
312    }
313    if (findex->next) {
314       return match_findex(bsr, findex->next, rec, findex->done && done);
315    }
316    if (findex->done && done) {
317       bsr->done = 1;
318    }
319    return 0;
320 }