- rec = new_record();
- free_pool_memory(rec->data);
- rec->data = ds->msg; /* use socket message buffer */
- hdr = get_pool_memory(PM_MESSAGE);
-
- /*
- * Read records, apply BSR filtering, and return any that are
- * matched.
- */
- for ( ; ok && !done; ) {
- if (job_canceled(jcr)) {
- ok = FALSE;
- break;
- }
- /* Read Record */
- Dmsg1(500, "Main read_record. rem=%d\n", rec->remainder);
-
- if (block_is_empty(block) && !read_block_from_device(jcr, dev, block, CHECK_BLOCK_NUMBERS)) {
- Dmsg1(500, "Main read record failed. rem=%d\n", rec->remainder);
- if (dev->state & ST_EOT) {
- DEV_RECORD *record;
- if (!mount_next_read_volume(jcr, dev, block)) {
- break;
- }
- /* Read and discard Volume Label */
- record = new_record();
- read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK);
- read_record_from_block(block, record);
- get_session_record(dev, record, &sessrec);
- free_record(record);
- /* Now, continue to read the records on this volume */
-
- } else if (dev->state & ST_EOF) {
- Dmsg0(90, "Got End of File. Trying again ...\n");
- continue; /* End of File */
- } else if (dev->state & ST_SHORT) {
- continue;
- } else {
- /* I/O error -- or non-standard end of tape */
- Dmsg0(000, "I/O error, bailing out.\n");
- ok = FALSE;
- break;
- }
- }
-
- for (rec->state=0; !is_block_empty(rec); ) {
-
- if (!read_record_from_block(block, rec)) {
- break;
- }
- /*
- * At this point, we have at least a record header.
- * Now decide if we want this record or not, but remember
- * before accessing the record, we may need to read again to
- * get all the data.
- */
-
- if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
- Dmsg0(40, "Get EOM LABEL\n");
- rec->remainder = 0;
- break; /* yes, get out */
- }
-
- /* Some sort of label? */
- if (rec->FileIndex < 0) {
- get_session_record(dev, rec, &sessrec);
- continue;
- } /* end if label record */
-
-
- /* Match BSR against current record */
- if (jcr->bsr) {
- int stat = match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec);
- if (stat == -1) { /* no more possible matches */
- done = TRUE;
- break;
- } else if (stat == 0) { /* no match */
- Dmsg0(50, "BSR rejected record\n");
- rec->remainder = 0;
- continue;
- }
- } else {
- /* Old way, deprecated */
- if (rec->VolSessionId != jcr->read_VolSessionId ||
- rec->VolSessionTime != jcr->read_VolSessionTime) {
- Dmsg0(50, "Ignore record ids not equal\n");
- rec->remainder = 0;
- continue; /* ignore */
- }
- }
-
- if (block->block_read) {
- if (++BlockNumber != block->BlockNumber) {
- Jmsg(jcr, M_ERROR, 0, _("Invalid block number. Expected %u, got %u\n"),
- BlockNumber, block->BlockNumber);
- }
- BlockNumber = block->BlockNumber;
- block->block_read = false;
- }
-
- if (is_partial_record(rec)) {
- break; /* Go read full record */
- }
-
- /*
- * We "finally" have a full record here. Now
- * generate Header parameters and send to File daemon
- * Note, we build header in hdr buffer to avoid wiping
- * out the data record
- */
- ds->msg = hdr;
- Dmsg5(400, "Send to FD: SessId=%u SessTim=%u FI=%d Strm=%d, len=%d\n",
- rec->VolSessionId, rec->VolSessionTime, rec->FileIndex, rec->Stream,
- rec->data_len);
- if (!bnet_fsend(ds, rec_header, rec->VolSessionId, rec->VolSessionTime,
- rec->FileIndex, rec->Stream, rec->data_len)) {
- Dmsg1(30, ">filed: Error Hdr=%s\n", ds->msg);
- hdr = ds->msg;
- ds->msg = rec->data;
- Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
- bnet_strerror(ds));
- ok = FALSE;
- break;
- } else {
- Dmsg1(30, ">filed: Hdr=%s\n", ds->msg);
- }
-
- hdr = ds->msg; /* restore hdr buffer */
- ds->msg = rec->data; /* restore data record address */