bacula-reposition.patch, bacula-tape-reopen.patch, and
     bacula-eod.patch. This corrects tape positioning on older devices,
     and also a problem with btape.
kes  Apply fix from bug report #696 where writing to a FIFO does not
     work due to a dumb typo.
kes  Unlock the tape door before the file descriptor is released. This
     should fix lots of somewhat subtle failures when using an autochanger.
git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@3551 
91ce42f0-d328-0410-95d8-
f526ca767f89
 
  *
  */
 /*
-   Copyright (C) 2000, 2001 Kern Sibbald and John Walker
+   Copyright (C) 2001-2006 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
-   as published by the Free Software Foundation; either version 2
-   of the License, or (at your option) any later version.
+   version 2 as amended with additional clauses defined in the
+   file LICENSE in the main source directory.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+   the file LICENSE for additional details.
 
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
 #include "bacula.h"
 
  *     Version $Id$
  */
 /*
-   Copyright (C) 2002-2005 Kern Sibbald
+   Copyright (C) 2002-2006 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
  */
 static int check_resources()
 {
-   int xOK = true;
+   bool ok = true;
    DIRRES *director;
 
    LockRes();
             director->tls_enable = true;
          } else {
             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
-            xOK = false;
+            ok = false;
             continue;
          }
       }
                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
                              " At least one CA certificate store is required.\n"),
                              director->hdr.name, configfile);
-         xOK = false;
+         ok = false;
       }
    }
    
    if (numdir == 0) {
       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
                           "Without that I don't how to speak to the Director :-(\n"), configfile);
-      xOK = false;
+      ok = false;
    }
 
    CONRES *cons;
             cons->tls_enable = true;
          } else {
             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
-            xOK = false;
+            ok = false;
             continue;
          }
       }
          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
                              " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
                              cons->hdr.name, configfile);
-         xOK = false;
+         ok = false;
       }
    }
 
    UnlockRes();
 
-   return xOK;
+   return ok;
 }
 
 
 
    DEV_RECORD *rec = dcr->rec;
    int i;
 
-   open_the_device();
+   if (!dev->is_open()) {
+      open_the_device();
+   }
    sm_check(__FILE__, __LINE__, false);
    empty_block(block);
    if (verbose > 1) {
 
       } else if (S_ISCHR(statp.st_mode)) {
          device->dev_type = B_TAPE_DEV;
       } else if (S_ISFIFO(statp.st_mode)) {
-         device->dev_type = B_FILE_DEV;
+         device->dev_type = B_FIFO_DEV;
       } else if (!(device->cap_bits & CAP_REQMOUNT)) {
          Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory\n"
                " or have RequiresMount=yes for DVD. st_mode=%x\n"),
          /*
           * Avoid infinite loop by ensuring we advance.
           */
-         if (file_num == (int)file) {
+         if (!at_eot() && file_num == (int)file) {
             struct mtget mt_stat;
             Dmsg1(100, "fsf did not advance from file %d\n", file_num);
             set_ateof();
             if (dev_get_os_pos(this, &mt_stat)) {
-               Dmsg2(100, "Adjust file from %d to %d\n", file , mt_stat.mt_fileno);
+               Dmsg2(100, "Adjust file from %d to %d\n", file_num, mt_stat.mt_fileno);
                file = mt_stat.mt_fileno;
             }       
             break;
    }
 
    /* After this point, we are tape only */
-   Dmsg4(100, "reposition from %u:%u to %u:%u\n",
-      file, block_num, rfile, rblock);
+   Dmsg4(100, "reposition from %u:%u to %u:%u\n", file, block_num, rfile, rblock);
    if (rfile < file) {
       Dmsg0(100, "Rewind\n");
       if (!rewind(NULL)) {
       /* Ignore errors as Bacula can read to the correct block */
       Dmsg1(100, "fsr %d\n", rblock-block_num);
       return fsr(rblock-block_num);
+   } else {
+      while (rblock > block_num) {
+         if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
+            berrno be;
+            dev_errno = errno;
+            Dmsg2(30, "Failed to find requested block on %s: ERR=%s",
+               print_name(), be.strerror());
+            return false;
+         }
+         Dmsg2(300, "moving forward wanted_blk=%d at_blk=%d\n", rblock, block_num);
+      }
    }
    return true;
 }
 
    switch (dev_type) {
    case B_TAPE_DEV:
-      tape_close(fd);
       unlock_door(); 
+      tape_close(fd);
       break;
    default:
       ::close(fd);
 
               Technical notes on version 1.39  
 
 General:
+12Oct06
+kes  Apply the three patches from Richard Mortimer. 
+     bacula-reposition.patch, bacula-tape-reopen.patch, and
+     bacula-eod.patch. This corrects tape positioning on older devices,
+     and also a problem with btape.
+kes  Apply fix from bug report #696 where writing to a FIFO does not
+     work due to a dumb typo.
+kes  Unlock the tape door before the file descriptor is released. This
+     should fix lots of somewhat subtle failures when using an autochanger.
 11Oct06
 kes  Correct mtx-changer bug that I introduced in a recent commit, and
      pointed out by Robert Nelson -- thanks.