]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/read_record.c
kes Merge patch from Sergey Svishchev <svs@ropnet.ru> that preserves
[bacula/bacula] / bacula / src / stored / read_record.c
index 0203b612ead604b0aa077d55a8d76d15ba021775..cb5d026cf4a9aa561c0fb6dbb4e9f6927027c318 100644 (file)
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000-2005 Kern Sibbald
+   Bacula® - The Network Backup Solution
 
-   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.
+   Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   The main author of Bacula is Kern Sibbald, with contributions from
+   many others, a complete list can be found in the file AUTHORS.
+   This program is Free Software; you can redistribute it and/or
+   modify it under the terms of version two of the GNU General Public
+   License as published by the Free Software Foundation plus additions
+   that are listed in the file LICENSE.
+
+   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.
 
-   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.
+   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., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
- */
+   Bacula® is a registered trademark of John Walker.
+   The licensor of Bacula is the Free Software Foundation Europe
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+   Switzerland, email:ftf@fsfeurope.org.
+*/
 
 #include "bacula.h"
 #include "stored.h"
 
 /* Forward referenced functions */
 static void handle_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
-static BSR *position_to_first_file(JCR *jcr, DEVICE *dev);
-static bool try_repositioning(JCR *jcr, DEV_RECORD *rec, DEVICE *dev);
+static BSR *position_to_first_file(JCR *jcr, DCR *dcr);
+static bool try_repositioning(JCR *jcr, DEV_RECORD *rec, DCR *dcr);
 #ifdef DEBUG
 static char *rec_state_to_str(DEV_RECORD *rec);
 #endif
 
+static const int dbglvl = 1000;
+
 bool read_records(DCR *dcr,
        bool record_cb(DCR *dcr, DEV_RECORD *rec),
        bool mount_cb(DCR *dcr))
@@ -59,7 +69,7 @@ bool read_records(DCR *dcr,
    dlist *recs;                         /* linked list of rec packets open */
 
    recs = New(dlist(rec, &rec->link));
-   position_to_first_file(jcr, dev);
+   position_to_first_file(jcr, dcr);
    jcr->mount_next_volume = false;
 
    for ( ; ok && !done; ) {
@@ -70,10 +80,10 @@ bool read_records(DCR *dcr,
       if (!read_block_from_device(dcr, CHECK_BLOCK_NUMBERS)) {
          if (dev->at_eot()) {
             DEV_RECORD *trec = new_record();
-            Jmsg(jcr, M_INFO, 0, "End of Volume at file %u on device %s, Volume \"%s\"\n",
+            Jmsg(jcr, M_INFO, 0, _("End of Volume at file %u on device %s, Volume \"%s\"\n"),
                  dev->file, dev->print_name(), dcr->VolumeName);
             if (!mount_cb(dcr)) {
-               Jmsg(jcr, M_INFO, 0, "End of all volumes.\n");
+               Jmsg(jcr, M_INFO, 0, _("End of all volumes.\n"));
                ok = false;            /* Stop everything */
                /*
                 * Create EOT Label so that Media record may
@@ -91,48 +101,62 @@ bool read_records(DCR *dcr,
                break;
             }
             jcr->mount_next_volume = false;
+            /*  
+             * The Device can change at the end of a tape, so refresh it
+             *   from the dcr.
+             */
+            dev = dcr->dev;
             /*
              * We just have a new tape up, now read the label (first record)
              *  and pass it off to the callback routine, then continue
              *  most likely reading the previous record.
              */
             read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK);
-            read_record_from_block(block, trec);
+            read_record_from_block(dcr, block, trec);
             handle_session_record(dev, trec, &sessrec);
             ok = record_cb(dcr, trec);
             free_record(trec);
-            position_to_first_file(jcr, dev);
+            position_to_first_file(jcr, dcr);
             /* After reading label, we must read first data block */
             continue;
 
          } else if (dev->at_eof()) {
             if (verbose) {
-               Jmsg(jcr, M_INFO, 0, "Got EOF at file %u  on device %s, Volume \"%s\"\n",
-                  dev->file, dev->print_name(), dcr->VolumeName);
+               char *fp;
+               uint32_t fp_num;
+               if (dev->is_dvd()) {
+                  fp = _("part");
+                  fp_num = dev->part;
+               } else {
+                  fp = _("file");
+                  fp_num = dev->file;
+               }
+               Jmsg(jcr, M_INFO, 0, _("End of %s %u on device %s, Volume \"%s\"\n"),
+                  fp, fp_num, dev->print_name(), dcr->VolumeName);
             }
-            Dmsg3(200, "Got EOF at file %u  on device %s, Volume \"%s\"\n",
+            Dmsg3(200, "End of file %u  on device %s, Volume \"%s\"\n",
                   dev->file, dev->print_name(), dcr->VolumeName);
             continue;
-         } else if (dev_state(dev, ST_SHORT)) {
+         } else if (dev->is_short_block()) {
             Jmsg1(jcr, M_ERROR, 0, "%s", dev->errmsg);
             continue;
          } else {
             /* I/O error or strange end of tape */
             display_tape_error_status(jcr, dev);
             if (forge_on || jcr->ignore_label_errors) {
-               fsr_dev(dev, 1);       /* try skipping bad record */
-               Pmsg0(000, "Did fsr\n");
+               dev->fsr(1);       /* try skipping bad record */
+               Pmsg0(000, _("Did fsr in attemp to skip bad record.\n"));
                continue;              /* try to continue */
             }
             ok = false;               /* stop everything */
             break;
          }
       }
-      Dmsg2(300, "New block at position=(file:block) %u:%u\n", dev->file, dev->block_num);
+      Dmsg2(dbglvl, "Read new block at pos=%u:%u\n", dev->file, dev->block_num);
 #ifdef if_and_when_FAST_BLOCK_REJECTION_is_working
       /* this does not stop when file/block are too big */
       if (!match_bsr_block(jcr->bsr, block)) {
-         if (try_repositioning(jcr, rec, dev)) {
+         if (try_repositioning(jcr, rec, dcr)) {
             break;                    /* get next volume */
          }
          continue;                    /* skip this record */
@@ -154,21 +178,22 @@ bool read_records(DCR *dcr,
       if (!found) {
          rec = new_record();
          recs->prepend(rec);
-         Dmsg2(300, "New record for SI=%d ST=%d\n",
+         Dmsg3(dbglvl, "New record for state=%s SI=%d ST=%d\n",
+             rec_state_to_str(rec),
              block->VolSessionId, block->VolSessionTime);
       }
-      Dmsg3(300, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
+      Dmsg3(dbglvl, "Before read rec loop. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
             block->BlockNumber, rec->remainder);
       record = 0;
       rec->state = 0;
-      Dmsg1(300, "Block empty %d\n", is_block_empty(rec));
-      for (rec->state=0; !is_block_empty(rec); ) {
-         if (!read_record_from_block(block, rec)) {
+      Dmsg1(dbglvl, "Block %s empty\n", is_block_empty(rec)?"is":"NOT");
+      for (rec->state=0; ok && !is_block_empty(rec); ) {
+         if (!read_record_from_block(dcr, block, rec)) {
             Dmsg3(400, "!read-break. state=%s blk=%d rem=%d\n", rec_state_to_str(rec),
                   block->BlockNumber, rec->remainder);
             break;
          }
-         Dmsg5(300, "read-OK. state=%s blk=%d rem=%d file:block=%u:%u\n",
+         Dmsg5(dbglvl, "read-OK. state=%s blk=%d rem=%d file:block=%u:%u\n",
                  rec_state_to_str(rec), block->BlockNumber, rec->remainder,
                  dev->file, dev->block_num);
          /*
@@ -178,7 +203,7 @@ bool read_records(DCR *dcr,
           *  get all the data.
           */
          record++;
-         Dmsg6(300, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
+         Dmsg6(dbglvl, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
             rec_state_to_str(rec), block->BlockNumber,
             rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
 
@@ -190,9 +215,25 @@ bool read_records(DCR *dcr,
          /* Some sort of label? */
          if (rec->FileIndex < 0) {
             handle_session_record(dev, rec, &sessrec);
+            if (jcr->bsr) {
+               /* We just check block FI and FT not FileIndex */
+               rec->match_stat = match_bsr_block(jcr->bsr, block);
+            } else {
+               rec->match_stat = 0;
+            }
+            /*
+             * Note, we pass *all* labels to the callback routine. If
+             *  he wants to know if they matched the bsr, then he must
+             *  check the match_stat in the record */
             ok = record_cb(dcr, rec);
+            /*
+             * If this is the end of the Session (EOS) for this record
+             *  we can remove the record.  Note, there is a separate
+             *  record to read each session. If a new session is seen
+             *  a new record will be created at approx line 157 above.
+             */
             if (rec->FileIndex == EOS_LABEL) {
-               Dmsg2(300, "Remove rec. SI=%d ST=%d\n", rec->VolSessionId,
+               Dmsg2(dbglvl, "Remove EOS rec. SI=%d ST=%d\n", rec->VolSessionId,
                   rec->VolSessionTime);
                recs->remove(rec);
                free_record(rec);
@@ -204,43 +245,52 @@ bool read_records(DCR *dcr,
           * Apply BSR filter
           */
          if (jcr->bsr) {
-            int stat = match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec);
-            if (stat == -1) { /* no more possible matches */
+            rec->match_stat = match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec);
+            if (rec->match_stat == -1) { /* no more possible matches */
                done = true;   /* all items found, stop */
-               Dmsg2(300, "All done=(file:block) %u:%u\n", dev->file, dev->block_num);
+               Dmsg2(dbglvl, "All done=(file:block) %u:%u\n", dev->file, dev->block_num);
                break;
-            } else if (stat == 0) {  /* no match */
-               Dmsg4(300, "Clear rem=%d FI=%d before set_eof pos %u:%u\n",
+            } else if (rec->match_stat == 0) {  /* no match */
+               Dmsg4(dbglvl, "BSR no match: clear rem=%d FI=%d before set_eof pos %u:%u\n",
                   rec->remainder, rec->FileIndex, dev->file, dev->block_num);
                rec->remainder = 0;
                rec->state &= ~REC_PARTIAL_RECORD;
-               if (try_repositioning(jcr, rec, dev)) {
+               if (try_repositioning(jcr, rec, dcr)) {
                   break;
                }
                continue;              /* we don't want record, read next one */
             }
          }
+         dcr->VolLastIndex = rec->FileIndex;  /* let caller know where we are */
          if (is_partial_record(rec)) {
-            Dmsg6(300, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
+            Dmsg6(dbglvl, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
                rec_state_to_str(rec), block->BlockNumber,
                rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
             break;                    /* read second part of record */
          }
+         Dmsg6(dbglvl, "OK callback. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
+               rec_state_to_str(rec), block->BlockNumber,
+               rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
          ok = record_cb(dcr, rec);
-         if (rec->Stream == STREAM_MD5_SIGNATURE || rec->Stream == STREAM_SHA1_SIGNATURE) {
-            Dmsg3(300, "Done FI=%u before set_eof pos %u:%u\n", rec->FileIndex,
+         /*
+          * If we have a digest stream, we check to see if we have 
+          *  finished the current bsr, and if so, repositioning will
+          *  be turned on.
+          */
+         if (crypto_digest_stream_type(rec->Stream) != CRYPTO_DIGEST_NONE) {
+            Dmsg3(dbglvl, "Have digest FI=%u before bsr check pos %u:%u\n", rec->FileIndex,
                   dev->file, dev->block_num);
-            if (match_set_eof(jcr->bsr, rec) && try_repositioning(jcr, rec, dev)) {
-               Dmsg2(300, "Break after match_set_eof pos %u:%u\n",
+            if (is_this_bsr_done(jcr->bsr, rec) && try_repositioning(jcr, rec, dcr)) {
+               Dmsg2(dbglvl, "This bsr done, break pos %u:%u\n",
                      dev->file, dev->block_num);
                break;
             }
-            Dmsg2(300, "After set_eof pos %u:%u\n", dev->file, dev->block_num);
+            Dmsg2(900, "After is_bsr_done pos %u:%u\n", dev->file, dev->block_num);
          }
       } /* end for loop over records */
-      Dmsg2(300, "After end records position=(file:block) %u:%u\n", dev->file, dev->block_num);
+      Dmsg2(dbglvl, "After end recs in block. pos=%u:%u\n", dev->file, dev->block_num);
    } /* end for loop over blocks */
-// Dmsg2(300, "Position=(file:block) %u:%u\n", dev->file, dev->block_num);
+// Dmsg2(dbglvl, "Position=(file:block) %u:%u\n", dev->file, dev->block_num);
 
    /* Walk down list and free all remaining allocated recs */
    while (!recs->empty()) {
@@ -258,33 +308,44 @@ bool read_records(DCR *dcr,
  *   Returns:  true  if at end of volume
  *             false otherwise
  */
-static bool try_repositioning(JCR *jcr, DEV_RECORD *rec, DEVICE *dev)
+static bool try_repositioning(JCR *jcr, DEV_RECORD *rec, DCR *dcr)
 {
    BSR *bsr;
+   DEVICE *dev = dcr->dev;
+
    bsr = find_next_bsr(jcr->bsr, dev);
    if (bsr == NULL && jcr->bsr->mount_next_volume) {
-      Dmsg0(300, "Would mount next volume here\n");
-      Dmsg2(300, "Current postion (file:block) %u:%u\n",
+      Dmsg0(dbglvl, "Would mount next volume here\n");
+      Dmsg2(dbglvl, "Current postion (file:block) %u:%u\n",
          dev->file, dev->block_num);
       jcr->bsr->mount_next_volume = false;
       if (!dev->at_eot()) {
          /* Set EOT flag to force mount of next Volume */
          jcr->mount_next_volume = true;
-         dev->state |= ST_EOT;
+         dev->set_eot();
       }
       rec->Block = 0;
       return true;
    }
    if (bsr) {
+      /*
+       * ***FIXME*** gross kludge to make disk seeking work.  Remove
+       *   when find_next_bsr() is fixed not to return a bsr already
+       *   completed.
+       */
+      if (dev->file > bsr->volfile->sfile ||             
+         (dev->file == bsr->volfile->sfile && dev->block_num > bsr->volblock->sblock)) {
+         return false;
+      }
       if (verbose) {
-         Jmsg(jcr, M_INFO, 0, "Reposition from (file:block) %u:%u to %u:%u\n",
+         Jmsg(jcr, M_INFO, 0, _("Reposition from (file:block) %u:%u to %u:%u\n"),
             dev->file, dev->block_num, bsr->volfile->sfile,
             bsr->volblock->sblock);
       }
-      Dmsg4(300, "Try_Reposition from (file:block) %u:%u to %u:%u\n",
+      Dmsg4(10, "Try_Reposition from (file:block) %u:%u to %u:%u\n",
             dev->file, dev->block_num, bsr->volfile->sfile,
             bsr->volblock->sblock);
-      reposition_dev(dev, bsr->volfile->sfile, bsr->volblock->sblock);
+      dev->reposition(dcr, bsr->volfile->sfile, bsr->volblock->sblock);
       rec->Block = 0;
    }
    return false;
@@ -293,9 +354,10 @@ static bool try_repositioning(JCR *jcr, DEV_RECORD *rec, DEVICE *dev)
 /*
  * Position to the first file on this volume
  */
-static BSR *position_to_first_file(JCR *jcr, DEVICE *dev)
+static BSR *position_to_first_file(JCR *jcr, DCR *dcr)
 {
    BSR *bsr = NULL;
+   DEVICE *dev = dcr->dev;
    /*
     * Now find and position to first file and block
     *   on this tape.
@@ -304,11 +366,10 @@ static BSR *position_to_first_file(JCR *jcr, DEVICE *dev)
       jcr->bsr->reposition = true;    /* force repositioning */
       bsr = find_next_bsr(jcr->bsr, dev);
       if (bsr && (bsr->volfile->sfile != 0 || bsr->volblock->sblock != 0)) {
-         Jmsg(jcr, M_INFO, 0, _("Forward spacing to file:block %u:%u.\n"),
-            bsr->volfile->sfile, bsr->volblock->sblock);
-         Dmsg2(300, "Forward spacing to file:block %u:%u.\n",
+         Jmsg(jcr, M_INFO, 0, _("Forward spacing Volume \"%s\" to file:block %u:%u.\n"),
+            dev->VolHdr.VolumeName,
             bsr->volfile->sfile, bsr->volblock->sblock);
-         reposition_dev(dev, bsr->volfile->sfile, bsr->volblock->sblock);
+         dev->reposition(dcr, bsr->volfile->sfile, bsr->volblock->sblock);
       }
    }
    return bsr;
@@ -323,28 +384,28 @@ static void handle_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *s
    memset(sessrec, 0, sizeof(sessrec));
    switch (rec->FileIndex) {
    case PRE_LABEL:
-      rtype = "Fresh Volume Label";
+      rtype = _("Fresh Volume Label");
       break;
    case VOL_LABEL:
-      rtype = "Volume Label";
+      rtype = _("Volume Label");
       unser_volume_label(dev, rec);
       break;
    case SOS_LABEL:
-      rtype = "Begin Session";
+      rtype = _("Begin Session");
       unser_session_label(sessrec, rec);
       break;
    case EOS_LABEL:
-      rtype = "End Session";
+      rtype = _("End Session");
       break;
    case EOM_LABEL:
-      rtype = "End of Media";
+      rtype = _("End of Media");
       break;
    default:
-      bsnprintf(buf, sizeof(buf), "Unknown code %d\n", rec->FileIndex);
+      bsnprintf(buf, sizeof(buf), _("Unknown code %d\n"), rec->FileIndex);
       rtype = buf;
       break;
    }
-   Dmsg5(300, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
+   Dmsg5(dbglvl, _("%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n"),
          rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
 }