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