]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/match_bsr.c
aa564d0c347688d07955b81a8b114f8c1f17029a
[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    /* Not yet working */
89 // if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
90 //    goto no_match;
91 // }
92    if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
93       goto no_match;
94    }
95
96    /* NOTE!! This test MUST come after the sesstime test */
97    if (!match_sessid(bsr, bsr->sessid, rec, 1)) {
98       goto no_match;
99    }
100
101    /* NOTE!! This test MUST come after sesstime and sessid tests */
102    if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
103       goto no_match;
104    }
105    if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
106       goto no_match;
107    }
108    if (!match_job(bsr, bsr->job, sessrec, 1)) {
109       goto no_match;
110    }
111    if (!match_client(bsr, bsr->client, sessrec, 1)) {
112       goto no_match;
113    }
114    if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
115       goto no_match;
116    }
117    if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
118       goto no_match;
119    }
120    if (!match_stream(bsr, bsr->stream, rec, 1)) {
121       goto no_match;
122    }
123    bsr->found++;
124    return 1;
125
126 no_match:
127    if (bsr->next) {
128       return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
129    }
130    if (bsr->done && done) {
131       return -1;
132    }
133    return 0;
134 }
135
136 static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, int done) 
137 {
138    if (!volume) {
139       return 0;                       /* Volume must match */
140    }
141    if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
142       return 1;
143    }
144    if (volume->next) {
145       return match_volume(bsr, volume->next, volrec, 1);
146    }
147    return 0;
148 }
149
150 static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, int done)
151 {
152    if (!client) {
153       return 1;                       /* no specification matches all */
154    }
155    if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
156       return 1;
157    }
158    if (client->next) {
159       return match_client(bsr, client->next, sessrec, 1);
160    }
161    return 0;
162 }
163
164 static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, int done)
165 {
166    if (!job) {
167       return 1;                       /* no specification matches all */
168    }
169    if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
170       return 1;
171    }
172    if (job->next) {
173       return match_job(bsr, job->next, sessrec, 1);
174    }
175    return 0;
176 }
177
178 static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, int done)
179 {
180    if (!job_type) {
181       return 1;                       /* no specification matches all */
182    }
183    if (job_type->JobType == sessrec->JobType) {
184       return 1;
185    }
186    if (job_type->next) {
187       return match_job_type(bsr, job_type->next, sessrec, 1);
188    }
189    return 0;
190 }
191
192 static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, int done)
193 {
194    if (!job_level) {
195       return 1;                       /* no specification matches all */
196    }
197    if (job_level->JobLevel == sessrec->JobLevel) {
198       return 1;
199    }
200    if (job_level->next) {
201       return match_job_level(bsr, job_level->next, sessrec, 1);
202    }
203    return 0;
204 }
205
206 static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, int done)
207 {
208    if (!jobid) {
209       return 1;                       /* no specification matches all */
210    }
211    if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
212       return 1;
213    }
214    if (jobid->next) {
215       return match_jobid(bsr, jobid->next, sessrec, 1);
216    }
217    return 0;
218 }
219
220 static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, int done)
221 {
222    if (!volfile) {
223       return 1;                       /* no specification matches all */
224    }
225    if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
226       return 1;
227    }
228    /* Once we get past last efile, we are done */
229    if (rec->File > volfile->efile) {
230       volfile->done = 1;              /* set local done */
231    }
232    if (volfile->next) {
233       return match_volfile(bsr, volfile->next, rec, volfile->done && done);
234    }
235
236    /* If we are done and all prior matches are done, this bsr is finished */
237    if (volfile->done && done) {
238       bsr->done = 1;
239    }
240    return 0;
241 }
242
243 static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, int done)
244 {
245    if (!stream) {
246       return 1;                       /* no specification matches all */
247    }
248    if (stream->stream == rec->Stream) {
249       return 1;
250    }
251    if (stream->next) {
252       return match_stream(bsr, stream->next, rec, 1);
253    }
254    return 0;
255 }
256
257 static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, int done)
258 {
259    if (!sesstime) {
260       return 1;                       /* no specification matches all */
261    }
262    if (sesstime->sesstime == rec->VolSessionTime) {
263       return 1;
264    }
265    if (rec->VolSessionTime > sesstime->sesstime) {
266       sesstime->done = 1;
267    }
268    if (sesstime->next) {
269       return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
270    }
271    if (sesstime->done && done) {
272       bsr->done = 1;
273    }
274    return 0;
275 }
276
277 static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec, int done)
278 {
279    if (!sessid) {
280       return 1;                       /* no specification matches all */
281    }
282    if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
283       return 1;
284    }
285    if (rec->VolSessionId > sessid->sessid2) {
286       sessid->done = 1;
287    }
288    if (sessid->next) {
289       return match_sessid(bsr, sessid->next, rec, sessid->done && done);
290    }
291    if (sessid->done && done) {
292       bsr->done = 1;
293    }
294    return 0;
295 }
296
297 static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, int done)
298 {
299    if (!findex) {
300       return 1;                       /* no specification matches all */
301    }
302    if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
303       return 1;
304    }
305    if (rec->FileIndex > findex->findex2) {
306       findex->done = 1;
307    }
308    if (findex->next) {
309       return match_findex(bsr, findex->next, rec, findex->done && done);
310    }
311    if (findex->done && done) {
312       bsr->done = 1;
313    }
314    return 0;
315 }