+/*
+ * 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;
+}
+
+/*
+ * Called after the signature record so that
+ * we can see if the current bsr has been
+ * fully processed (i.e. is done).
+ * The bsr argument is not used, but is included
+ * for consistency with the other match calls.
+ *
+ * Returns: true if we should reposition
+ * : false otherwise.
+ */
+bool is_this_bsr_done(BSR *bsr, DEV_RECORD *rec)
+{
+ BSR *rbsr = rec->bsr;
+ Dmsg1(dbglevel, "match_set %d\n", rbsr != NULL);
+ if (!rbsr) {
+ return false;
+ }
+ rec->bsr = NULL;
+ rbsr->found++;
+ if (rbsr->count && rbsr->found >= rbsr->count) {
+ rbsr->done = true;
+ rbsr->root->reposition = true;
+ Dmsg2(dbglevel, "is_end_this_bsr set reposition=1 count=%d found=%d\n",
+ rbsr->count, rbsr->found);
+ return true;
+ }
+ return false;
+}
+
+/*