+ return 0;
+}
+
+static int match_block_sesstime(BSR *bsr, BSR_SESSTIME *sesstime, DEV_BLOCK *block)
+{
+ if (!sesstime) {
+ return 1; /* no specification matches all */
+ }
+ if (sesstime->sesstime == block->VolSessionTime) {
+ return 1;
+ }
+ if (sesstime->next) {
+ return match_block_sesstime(bsr, sesstime->next, block);
+ }
+ return 0;
+}
+
+static int match_block_sessid(BSR *bsr, BSR_SESSID *sessid, DEV_BLOCK *block)
+{
+ if (!sessid) {
+ return 1; /* no specification matches all */
+ }
+ if (sessid->sessid <= block->VolSessionId && sessid->sessid2 >= block->VolSessionId) {
+ return 1;
+ }
+ if (sessid->next) {
+ return match_block_sessid(bsr, sessid->next, block);
+ }
+ return 0;
+}
+
+
+/*********************************************************************
+ *
+ * Match Bootstrap records
+ * returns 1 on match
+ * returns 0 no match and reposition is set if we should
+ * reposition the tape
+ * returns -1 no additional matches possible
+ */
+int match_bsr(BSR *bsr, DEV_RECORD *rec, VOLUME_LABEL *volrec, SESSION_LABEL *sessrec)
+{
+ int stat;
+
+ /*
+ * The bsr->reposition flag is set any time a bsr is done.
+ * In this case, we can probably reposition the
+ * tape to the next available bsr position.
+ */
+ if (bsr) {
+ bsr->reposition = false;
+ stat = match_all(bsr, rec, volrec, sessrec, true);
+ /*
+ * Note, bsr->reposition is set by match_all when
+ * a bsr is done. We turn it off if a match was
+ * found or if we cannot use positioning
+ */
+ if (stat != 0 || !bsr->use_positioning) {
+ bsr->reposition = false;
+ }
+ } else {
+ stat = 1; /* no bsr => match all */
+ }
+ return stat;
+}
+
+/*
+ * Find the next bsr that applies to the current tape.
+ * It is the one with the smallest VolFile position.
+ */
+BSR *find_next_bsr(BSR *root_bsr, DEVICE *dev)
+{
+ BSR *bsr;
+ BSR *found_bsr = NULL;
+
+ /* Do tape/disk seeking only if CAP_POSITIONBLOCKS is on */
+ if (!root_bsr) {
+ Dmsg0(dbglevel, "NULL root bsr pointer passed to find_next_bsr.\n");
+ return NULL;
+ }
+ if (!root_bsr->use_positioning ||
+ !root_bsr->reposition || !dev->has_cap(CAP_POSITIONBLOCKS)) {
+ Dmsg2(dbglevel, "No nxt_bsr use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
+ return NULL;
+ }
+ Dmsg2(dbglevel, "use_pos=%d repos=%d\n", root_bsr->use_positioning, root_bsr->reposition);
+ root_bsr->mount_next_volume = false;
+ /* Walk through all bsrs to find the next one to use => smallest file,block */
+ for (bsr=root_bsr; bsr; bsr=bsr->next) {
+ if (bsr->done || !match_volume(bsr, bsr->volume, &dev->VolHdr, 1)) {
+ continue;
+ }
+ if (found_bsr == NULL) {
+ found_bsr = bsr;
+ } else {
+ found_bsr = find_smallest_volfile(found_bsr, bsr);
+ }
+ }
+ /*
+ * If we get to this point and found no bsr, it means
+ * that any additional bsr's must apply to the next
+ * tape, so set a flag.
+ */
+ if (found_bsr == NULL) {
+ root_bsr->mount_next_volume = true;
+ }
+ return found_bsr;
+}
+
+/*
+ * ***FIXME***
+ * This routine needs to be fixed to only look at items that
+ * are not marked as done. Otherwise, it can find a bsr
+ * that has already been consumed, and this will cause the
+ * bsr to be used, thus we may seek back and re-read the
+ * same records, causing an error. This deficiency must
+ * be fixed. For the moment, it has been kludged in
+ * read_record.c to avoid seeking back if find_next_bsr
+ * returns a bsr pointing to a smaller address (file/block).
+ */
+static BSR *find_smallest_volfile(BSR *found_bsr, BSR *bsr)
+{
+ BSR *return_bsr = found_bsr;
+ BSR_VOLFILE *vf;
+ BSR_VOLBLOCK *vb;
+ uint32_t found_bsr_sfile, bsr_sfile;
+ uint32_t found_bsr_sblock, bsr_sblock;
+
+ /* Find the smallest file in the found_bsr */
+ vf = found_bsr->volfile;
+ found_bsr_sfile = vf->sfile;
+ while ( (vf=vf->next) ) {
+ if (vf->sfile < found_bsr_sfile) {
+ found_bsr_sfile = vf->sfile;
+ }
+ }
+
+ /* Find the smallest file in the bsr */
+ vf = bsr->volfile;
+ bsr_sfile = vf->sfile;
+ while ( (vf=vf->next) ) {
+ if (vf->sfile < bsr_sfile) {
+ bsr_sfile = vf->sfile;
+ }
+ }
+
+ /* if the bsr file is less than the found_bsr file, return bsr */
+ if (found_bsr_sfile > bsr_sfile) {
+ return_bsr = bsr;
+ } else if (found_bsr_sfile == bsr_sfile) {
+ /* Files are equal */
+ /* find smallest block in found_bsr */
+ vb = found_bsr->volblock;
+ found_bsr_sblock = vb->sblock;
+ while ( (vb=vb->next) ) {
+ if (vb->sblock < found_bsr_sblock) {
+ found_bsr_sblock = vb->sblock;
+ }
+ }
+ /* Find smallest block in bsr */
+ vb = bsr->volblock;
+ bsr_sblock = vb->sblock;
+ while ( (vb=vb->next) ) {
+ if (vb->sblock < bsr_sblock) {
+ bsr_sblock = vb->sblock;
+ }
+ }
+ /* Compare and return the smallest */
+ if (found_bsr_sblock > bsr_sblock) {
+ return_bsr = bsr;
+ }
+ }
+ return return_bsr;