+ /*
+ * If we are not dealing with a streaming device,
+ * write the block now to ensure we have write permission.
+ * It is better to find out now rather than later.
+ * We do not write the block now if this is an ANSI label. This
+ * avoids re-writing the ANSI label, which we do not want to do.
+ */
+ if (!dev->has_cap(CAP_STREAM)) {
+ if (!dev->rewind(dcr)) {
+ Jmsg2(jcr, M_FATAL, 0, _("Rewind error on device %s: ERR=%s\n"),
+ dev->print_name(), dev->print_errmsg());
+ return false;
+ }
+ if (recycle) {
+ Dmsg1(150, "Doing recycle. Vol=%s\n", dcr->VolumeName);
+// volume_unused(dcr); /* mark volume unused */
+ if (!dev->truncate(dcr)) {
+ Jmsg2(jcr, M_FATAL, 0, _("Truncate error on device %s: ERR=%s\n"),
+ dev->print_name(), dev->print_errmsg());
+ return false;
+ }
+ if (dev->open(dcr, OPEN_READ_WRITE) < 0) {
+ Jmsg2(jcr, M_FATAL, 0,
+ _("Failed to re-open DVD after truncate on device %s: ERR=%s\n"),
+ dev->print_name(), dev->print_errmsg());
+ return false;
+ }
+ }
+
+ /*
+ * If we have already detected an ANSI label, re-read it
+ * to skip past it. Otherwise, we write a new one if
+ * so requested.
+ */
+ if (dev->label_type != B_BACULA_LABEL) {
+ if (read_ansi_ibm_label(dcr) != VOL_OK) {
+ dev->rewind(dcr);
+ return false;
+ }
+ } else if (!write_ansi_ibm_labels(dcr, ANSI_VOL_LABEL, dev->VolHdr.VolumeName)) {
+ return false;
+ }
+
+ /* Attempt write to check write permission */
+ Dmsg1(200, "Attempt to write to device fd=%d.\n", dev->fd());
+ if (!write_block_to_dev(dcr)) {
+ Jmsg2(jcr, M_ERROR, 0, _("Unable to write device %s: ERR=%s\n"),
+ dev->print_name(), dev->print_errmsg());
+ Dmsg0(200, "===ERROR write block to dev\n");
+ return false;
+ }
+ }
+ dev->set_labeled();
+ /* Set or reset Volume statistics */
+ dev->VolCatInfo.VolCatJobs = 0;
+ dev->VolCatInfo.VolCatFiles = 0;
+ dev->VolCatInfo.VolCatErrors = 0;
+ dev->VolCatInfo.VolCatBlocks = 0;
+ dev->VolCatInfo.VolCatRBytes = 0;
+ if (recycle) {
+ dev->VolCatInfo.VolCatMounts++;
+ dev->VolCatInfo.VolCatRecycles++;
+ dir_create_jobmedia_record(dcr, true);
+ } else {
+ dev->VolCatInfo.VolCatMounts = 1;
+ dev->VolCatInfo.VolCatRecycles = 0;
+ dev->VolCatInfo.VolCatWrites = 1;
+ dev->VolCatInfo.VolCatReads = 1;
+ }
+ Dmsg1(150, "dir_update_vol_info. Set Append vol=%s\n", dcr->VolumeName);
+ bstrncpy(dev->VolCatInfo.VolCatStatus, "Append", sizeof(dev->VolCatInfo.VolCatStatus));
+ bstrncpy(dev->VolCatInfo.VolCatName, dcr->VolumeName, sizeof(dev->VolCatInfo.VolCatName));
+ if (!dir_update_volume_info(dcr, true, true)) { /* indicate doing relabel */