- if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
- if (dir) {
- return 0; /* For user, bail out right now */
- }
- if (dir_find_next_appendable_volume(jcr)) {
- slot = jcr->VolCatInfo.Slot;
- }
- }
- Dmsg1(100, "Want changer slot=%d\n", slot);
-
- if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
- uint32_t timeout = jcr->device->max_changer_wait;
- POOLMEM *changer, *results;
- int status, loaded;
-
- results = get_pool_memory(PM_MESSAGE);
- changer = get_pool_memory(PM_FNAME);
-
- /* Find out what is loaded, zero means device is unloaded */
- changer = edit_device_codes(jcr, changer, jcr->device->changer_command,
- "loaded");
- status = run_program(changer, timeout, results);
- Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
- if (status == 0) {
- loaded = atoi(results);
- } else {
- loaded = -1; /* force unload */
- }
- Dmsg1(100, "loaded=%s\n", results);
-
- /* If bad status or tape we want is not loaded, load it. */
- if (status != 0 || loaded != slot) {
- if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
- offline_dev(dev);
- }
- /* We are going to load a new tape, so close the device */
- force_close_dev(dev);
- if (loaded != 0) { /* must unload drive */
- Dmsg0(100, "Doing changer unload.\n");
- if (dir) {
- bnet_fsend(dir, _("3902 Issuing autochanger \"unload\" command.\n"));
- } else {
- Jmsg(jcr, M_INFO, 0, _("Issuing autochanger \"unload\" command.\n"));
- }
- changer = edit_device_codes(jcr, changer,
- jcr->device->changer_command, "unload");
- status = run_program(changer, timeout, NULL);
- Dmsg1(100, "unload status=%d\n", status);
- }
- /*
- * Load the desired cassette
- */
- Dmsg1(100, "Doing changer load slot %d\n", slot);
- if (dir) {
- bnet_fsend(dir, _("3903 Issuing autochanger \"load slot %d\" command.\n"),
- slot);
- } else {
- Jmsg(jcr, M_INFO, 0, _("Issuing autochanger \"load slot %d\" command.\n"),
- slot);
- }
- changer = edit_device_codes(jcr, changer,
- jcr->device->changer_command, "load");
- status = run_program(changer, timeout, NULL);
- if (status == 0) {
- Jmsg(jcr, M_INFO, 0, _("Autochanger \"load slot\" status is OK.\n"));
- } else {
- Jmsg(jcr, M_INFO, 0, _("Bad autochanger \"load slot\" status = %d.\n"),
- status);
- }
- Dmsg2(100, "load slot %d status=%d\n", slot, status);
- }
- free_pool_memory(changer);
- free_pool_memory(results);
- Dmsg1(100, "After changer, status=%d\n", status);
- if (status == 0) { /* did we succeed? */
- rtn_stat = 1; /* tape loaded by changer */
- }
+ dev->block_num = dev->file = 0;
+ dev->EndBlock = dev->EndFile = 0;
+ memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
+ memset(&dcr->VolCatInfo, 0, sizeof(dcr->VolCatInfo));
+ memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
+ /* Force re-read of label */
+ dev->state &= ~(ST_LABEL|ST_READ|ST_APPEND);
+ dev->label_type = B_BACULA_LABEL;
+ dcr->VolumeName[0] = 0;
+
+ if (dev->is_open() && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
+ offline_or_rewind_dev(dev);
+ close_dev(dev);