]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/match_bsr.c
Use rentrant mysql lib, eliminate race in sql_list, Win32 streams, misc see kes-1.31
[bacula/bacula] / bacula / src / stored / match_bsr.c
index 21d5a19420415b4ec4f6f1041a893b728c0626df..da72874d597e6ed28b4b619d78842790f4220a6f 100755 (executable)
 
 #include "bacula.h"
 #include "stored.h"
+#include <fnmatch.h>
 
 /* Forward references */
-static int match_sesstime(BSR_SESSTIME *sesstime, DEV_RECORD *rec);
-static int match_sessid(BSR_SESSID *sessid, DEV_RECORD *rec);
-static int match_client(BSR_CLIENT *client, SESSION_LABEL *sessrec);
-static int match_job(BSR_JOB *job, SESSION_LABEL *sessrec);
-static int match_job_type(BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec);
-static int match_job_level(BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec);
-static int match_jobid(BSR_JOBID *jobid, SESSION_LABEL *sessrec);
-static int match_findex(BSR_FINDEX *findex, DEV_RECORD *rec);
-static int match_volfile(BSR_VOLFILE *volfile, DEV_RECORD *rec);
-static int match_one_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec);
+static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, int done);
+static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, int done);
+static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec, int done);
+static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, int done);
+static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, int done);
+static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, int done);
+static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, int done);
+static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, int done);
+static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, int done);
+static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, int done);
+static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, int done);
+static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec, int done);
 
 /*********************************************************************
  *
  *     Match Bootstrap records
- *
+ *       returns  1 on match
+ *       returns  0 no match
+ *      returns -1 no additional matches possible
  */
 int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
 {
-   if (!bsr) {
-      return 0;
-   }
-   if (match_one_bsr(bsr, rec, volrec, sessrec)) {
-      return 1;
+   int stat;
+
+   if (bsr) {
+      stat = match_all(bsr, rec, volrec, sessrec, 1);
+   } else {
+      stat = 0;
    }
-   return match_bsr(bsr->next, rec, volrec, sessrec);
+// Dmsg1(000, "BSR returning %d\n", stat);
+   return stat;
 }
 
-static int match_one_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
+/* 
+ * Match all the components of current record
+ *   returns  1 on match
+ *   returns  0 no match
+ *   returns -1 no additional matches possible
+ */
+static int match_all(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, 
+                    SESSION_LABEL *sessrec, int done)
 {
-   if (strcmp(bsr->VolumeName, volrec->VolName) != 0) {
-      return 0;
+   if (bsr->done) {
+      goto no_match;
+   }
+   if (bsr->count && bsr->count <= bsr->found) {
+      bsr->done = 1;
+      goto no_match;
+   }
+   if (!match_volume(bsr, bsr->volume, volrec, 1)) {
+      goto no_match;
    }
-   if (!match_volfile(bsr->volfile, rec)) {
-      return 0;
+   if (!match_volfile(bsr, bsr->volfile, rec, 1)) {
+      goto no_match;
    }
-   if (!match_sesstime(bsr->sesstime, rec)) {
-      return 0;
+   if (!match_sesstime(bsr, bsr->sesstime, rec, 1)) {
+      goto no_match;
    }
-   if (!match_sessid(bsr->sessid, rec)) {
-      return 0;
+
+   /* NOTE!! This test MUST come after the sesstime test */
+   if (!match_sessid(bsr, bsr->sessid, rec, 1)) {
+      goto no_match;
+   }
+
+   /* NOTE!! This test MUST come after sesstime and sessid tests */
+   if (!match_findex(bsr, bsr->FileIndex, rec, 1)) {
+      goto no_match;
    }
-   if (!match_jobid(bsr->JobId, sessrec)) {
-      return 0;
+   if (!match_jobid(bsr, bsr->JobId, sessrec, 1)) {
+      goto no_match;
    }
-   if (!match_job(bsr->job, sessrec)) {
-      return 0;
+   if (!match_job(bsr, bsr->job, sessrec, 1)) {
+      goto no_match;
    }
-   if (!match_client(bsr->client, sessrec)) {
-      return 0;
+   if (!match_client(bsr, bsr->client, sessrec, 1)) {
+      goto no_match;
    }
-   if (!match_findex(bsr->FileIndex, rec)) {
-      return 0;
+   if (!match_job_type(bsr, bsr->JobType, sessrec, 1)) {
+      goto no_match;
    }
-   if (!match_job_type(bsr->JobType, sessrec)) {
-      return 0;
+   if (!match_job_level(bsr, bsr->JobLevel, sessrec, 1)) {
+      goto no_match;
    }
-   if (!match_job_level(bsr->JobLevel, sessrec)) {
-      return 0;
+   if (!match_stream(bsr, bsr->stream, rec, 1)) {
+      goto no_match;
    }
+   bsr->found++;
    return 1;
+
+no_match:
+   if (bsr->next) {
+      return match_all(bsr->next, rec, volrec, sessrec, bsr->done && done);
+   }
+   if (bsr->done && done) {
+      return -1;
+   }
+   return 0;
 }
 
-static int match_client(BSR_CLIENT *client, SESSION_LABEL *sessrec)
+static int match_volume(BSR *bsr, BSR_VOLUME *volume, VOLUME_LABEL *volrec, int done) 
+{
+   if (!volume) {
+      return 0;                      /* Volume must match */
+   }
+   if (strcmp(volume->VolumeName, volrec->VolName) == 0) {
+      return 1;
+   }
+   if (volume->next) {
+      return match_volume(bsr, volume->next, volrec, 1);
+   }
+   return 0;
+}
+
+static int match_client(BSR *bsr, BSR_CLIENT *client, SESSION_LABEL *sessrec, int done)
 {
    if (!client) {
       return 1;                      /* no specification matches all */
    }
-   if (strcmp(client->ClientName, sessrec->ClientName) == 0) {
+   if (fnmatch(client->ClientName, sessrec->ClientName, 0) == 0) {
       return 1;
    }
    if (client->next) {
-      return match_client(client->next, sessrec);
+      return match_client(bsr, client->next, sessrec, 1);
    }
    return 0;
 }
 
-static int match_job(BSR_JOB *job, SESSION_LABEL *sessrec)
+static int match_job(BSR *bsr, BSR_JOB *job, SESSION_LABEL *sessrec, int done)
 {
    if (!job) {
       return 1;                      /* no specification matches all */
    }
-   if (strcmp(job->Job, sessrec->Job) == 0) {
-      job->found++;
+   if (fnmatch(job->Job, sessrec->Job, 0) == 0) {
       return 1;
    }
    if (job->next) {
-      return match_job(job->next, sessrec);
+      return match_job(bsr, job->next, sessrec, 1);
    }
    return 0;
 }
 
-
-static int match_job_type(BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec)
+static int match_job_type(BSR *bsr, BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec, int done)
 {
    if (!job_type) {
       return 1;                      /* no specification matches all */
@@ -133,12 +183,12 @@ static int match_job_type(BSR_JOBTYPE *job_type, SESSION_LABEL *sessrec)
       return 1;
    }
    if (job_type->next) {
-      return match_job_type(job_type->next, sessrec);
+      return match_job_type(bsr, job_type->next, sessrec, 1);
    }
    return 0;
 }
 
-static int match_job_level(BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec)
+static int match_job_level(BSR *bsr, BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec, int done)
 {
    if (!job_level) {
       return 1;                      /* no specification matches all */
@@ -147,85 +197,124 @@ static int match_job_level(BSR_JOBLEVEL *job_level, SESSION_LABEL *sessrec)
       return 1;
    }
    if (job_level->next) {
-      return match_job_level(job_level->next, sessrec);
+      return match_job_level(bsr, job_level->next, sessrec, 1);
    }
    return 0;
 }
 
-static int match_jobid(BSR_JOBID *jobid, SESSION_LABEL *sessrec)
+static int match_jobid(BSR *bsr, BSR_JOBID *jobid, SESSION_LABEL *sessrec, int done)
 {
    if (!jobid) {
       return 1;                      /* no specification matches all */
    }
    if (jobid->JobId <= sessrec->JobId && jobid->JobId2 >= sessrec->JobId) {
-      jobid->found++;
       return 1;
    }
    if (jobid->next) {
-      return match_jobid(jobid->next, sessrec);
+      return match_jobid(bsr, jobid->next, sessrec, 1);
    }
    return 0;
 }
 
-
-static int match_volfile(BSR_VOLFILE *volfile, DEV_RECORD *rec)
+static int match_volfile(BSR *bsr, BSR_VOLFILE *volfile, DEV_RECORD *rec, int done)
 {
    if (!volfile) {
       return 1;                      /* no specification matches all */
    }
+   /* For the moment, these tests work only with tapes. */
+   if (!(rec->state & REC_ISTAPE)) {
+      return 1;                      /* All File records OK for this match */
+   }
+// Dmsg3(000, "match_volfile: sfile=%d efile=%d recfile=%d\n",
+//            volfile->sfile, volfile->efile, rec->File);
    if (volfile->sfile <= rec->File && volfile->efile >= rec->File) {
-      volfile->found++;
       return 1;
    }
+   /* Once we get past last efile, we are done */
+   if (rec->File > volfile->efile) {
+      volfile->done = 1;             /* set local done */
+   }
    if (volfile->next) {
-      return match_volfile(volfile->next, rec);
+      return match_volfile(bsr, volfile->next, rec, volfile->done && done);
+   }
+
+   /* If we are done and all prior matches are done, this bsr is finished */
+   if (volfile->done && done) {
+      bsr->done = 1;
    }
    return 0;
 }
 
-
-static int match_findex(BSR_FINDEX *findex, DEV_RECORD *rec)
+static int match_stream(BSR *bsr, BSR_STREAM *stream, DEV_RECORD *rec, int done)
 {
-   if (!findex) {
+   if (!stream) {
       return 1;                      /* no specification matches all */
    }
-   if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
-      findex->found++;
+   if (stream->stream == rec->Stream) {
       return 1;
    }
-   if (findex->next) {
-      return match_findex(findex->next, rec);
+   if (stream->next) {
+      return match_stream(bsr, stream->next, rec, 1);
    }
    return 0;
 }
 
+static int match_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_RECORD *rec, int done)
+{
+   if (!sesstime) {
+      return 1;                      /* no specification matches all */
+   }
+   if (sesstime->sesstime == rec->VolSessionTime) {
+      return 1;
+   }
+   if (rec->VolSessionTime > sesstime->sesstime) {
+      sesstime->done = 1;
+   }
+   if (sesstime->next) {
+      return match_sesstime(bsr, sesstime->next, rec, sesstime->done && done);
+   }
+   if (sesstime->done && done) {
+      bsr->done = 1;
+   }
+   return 0;
+}
 
-static int match_sessid(BSR_SESSID *sessid, DEV_RECORD *rec)
+static int match_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_RECORD *rec, int done)
 {
    if (!sessid) {
       return 1;                      /* no specification matches all */
    }
    if (sessid->sessid <= rec->VolSessionId && sessid->sessid2 >= rec->VolSessionId) {
-      sessid->found++;
       return 1;
    }
+   if (rec->VolSessionId > sessid->sessid2) {
+      sessid->done = 1;
+   }
    if (sessid->next) {
-      return match_sessid(sessid->next, rec);
+      return match_sessid(bsr, sessid->next, rec, sessid->done && done);
+   }
+   if (sessid->done && done) {
+      bsr->done = 1;
    }
    return 0;
 }
 
-static int match_sesstime(BSR_SESSTIME *sesstime, DEV_RECORD *rec)
+static int match_findex(BSR *bsr, BSR_FINDEX *findex, DEV_RECORD *rec, int done)
 {
-   if (!sesstime) {
+   if (!findex) {
       return 1;                      /* no specification matches all */
    }
-   if (sesstime->sesstime == rec->VolSessionTime) {
-      sesstime->found++;
+   if (findex->findex <= rec->FileIndex && findex->findex2 >= rec->FileIndex) {
       return 1;
    }
-   if (sesstime->next) {
-      return match_sesstime(sesstime->next, rec);
+   if (rec->FileIndex > findex->findex2) {
+      findex->done = 1;
+   }
+   if (findex->next) {
+      return match_findex(bsr, findex->next, rec, findex->done && done);
+   }
+   if (findex->done && done) {
+      bsr->done = 1;
    }
    return 0;
 }