+static void reread_last_block(DCR *dcr)
+{
+#define CHECK_LAST_BLOCK
+#ifdef CHECK_LAST_BLOCK
+ bool ok = true;
+ DEVICE *dev = dcr->dev;
+ JCR *jcr = dcr->jcr;
+ DEV_BLOCK *block = dcr->block;
+ /*
+ * If the device is a tape and it supports backspace record,
+ * we backspace over one or two eof marks depending on
+ * how many we just wrote, then over the last record,
+ * then re-read it and verify that the block number is
+ * correct.
+ */
+ if (dev->is_tape() && dev_cap(dev, CAP_BSR)) {
+ /* Now back up over what we wrote and read the last block */
+ if (!dev->bsf(1)) {
+ berrno be;
+ ok = false;
+ Jmsg(jcr, M_ERROR, 0, _("Backspace file at EOT failed. ERR=%s\n"),
+ be.strerror(dev->dev_errno));
+ }
+ if (ok && dev->has_cap(CAP_TWOEOF) && !dev->bsf(1)) {
+ berrno be;
+ ok = false;
+ Jmsg(jcr, M_ERROR, 0, _("Backspace file at EOT failed. ERR=%s\n"),
+ be.strerror(dev->dev_errno));
+ }
+ /* Backspace over record */
+ if (ok && !dev->bsr(1)) {
+ berrno be;
+ ok = false;
+ Jmsg(jcr, M_ERROR, 0, _("Backspace record at EOT failed. ERR=%s\n"),
+ be.strerror(dev->dev_errno));
+ /*
+ * On FreeBSD systems, if the user got here, it is likely that his/her
+ * tape drive is "frozen". The correct thing to do is a
+ * rewind(), but if we do that, higher levels in cleaning up, will
+ * most likely write the EOS record over the beginning of the
+ * tape. The rewind *is* done later in mount.c when another
+ * tape is requested. Note, the clrerror() call in bsr()
+ * calls ioctl(MTCERRSTAT), which *should* fix the problem.
+ */
+ }
+ if (ok) {
+ DEV_BLOCK *lblock = new_block(dev);
+ /* Note, this can destroy dev->errmsg */
+ dcr->block = lblock;
+ if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
+ Jmsg(jcr, M_ERROR, 0, _("Re-read last block at EOT failed. ERR=%s"),
+ dev->errmsg);
+ } else {
+ /*
+ * If we wrote block and the block numbers don't agree
+ * we have a possible problem.
+ */
+ if (lblock->VolSessionId == block->VolSessionId &&
+ lblock->BlockNumber+1 != block->BlockNumber) {
+ Jmsg(jcr, M_ERROR, 0, _(
+"Re-read of last block OK, but block numbers differ. Last block=%u Current block=%u.\n"),
+ lblock->BlockNumber, block->BlockNumber);
+ } else {
+ Jmsg(jcr, M_INFO, 0, _("Re-read of last block succeeded.\n"));
+ }
+ }
+ free_block(lblock);
+ dcr->block = block;
+ }
+ }
+#endif
+}
+