]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read_record.c
a3c8858b5d4eba1ecd407a102d743250607deb7d
[bacula/bacula] / bacula / src / stored / read_record.c
1 /*
2  *
3  *  This routine provides a routine that will handle all
4  *    the gory little details of reading a record from a Bacula
5  *    archive. It uses a callback to pass you each record in turn,
6  *    as well as a callback for mounting the next tape.  It takes
7  *    care of reading blocks, applying the bsr, ...
8  *
9  *    Kern E. Sibbald, August MMII
10  *
11  *   Version $Id$
12  */
13 /*
14    Copyright (C) 2000-2003 Kern Sibbald and John Walker
15
16    This program is free software; you can redistribute it and/or
17    modify it under the terms of the GNU General Public License as
18    published by the Free Software Foundation; either version 2 of
19    the License, or (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public
27    License along with this program; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29    MA 02111-1307, USA.
30
31  */
32
33 #include "bacula.h"
34 #include "stored.h"
35
36 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
37 #ifdef DEBUG
38 static char *rec_state_to_str(DEV_RECORD *rec);
39 #endif
40
41 int read_records(JCR *jcr,  DEVICE *dev, 
42        void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec),
43        int mount_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block))
44 {
45    DEV_BLOCK *block;
46    DEV_RECORD *rec;
47    uint32_t record, num_files = 0;
48    int ok = TRUE;
49    int done = FALSE;
50    SESSION_LABEL sessrec;
51
52    block = new_block(dev);
53    rec = new_record();
54    for ( ; ok && !done; ) {
55       if (job_canceled(jcr)) {
56          ok = FALSE;
57          break;
58       }
59       if (!read_block_from_device(jcr, dev, block, CHECK_BLOCK_NUMBERS)) {
60          Dmsg0(20, "!read_record()\n");
61          if (dev->state & ST_EOT) {
62             DEV_RECORD *trec = new_record();
63
64             Dmsg3(100, "EOT. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
65                   block->BlockNumber, rec->remainder);
66             Jmsg(jcr, M_INFO, 0, "Got EOM at file %u  on device %s, Volume \"%s\"\n", 
67                  dev->file, dev_name(dev), jcr->VolumeName);
68             if (!mount_cb(jcr, dev, block)) {
69                Jmsg(jcr, M_INFO, 0, "End of all volumes.\n");
70                Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
71                   block->BlockNumber, rec->remainder);
72                ok = FALSE;
73                /*
74                 * Create EOT Label so that Media record may
75                 *  be properly updated because this is the last
76                 *  tape.
77                 */
78                trec->FileIndex = EOT_LABEL;
79                trec->File = dev->file;
80                trec->Block = rec->Block; /* return block last read */
81                record_cb(jcr, dev, block, trec);
82                free_record(trec);
83                break;
84             }
85             Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
86                   block->BlockNumber, rec->remainder);
87             /*
88              * We just have a new tape up, now read the label (first record)
89              *  and pass it off to the callback routine, then continue
90              *  most likely reading the previous record.
91              */
92             read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK);
93             read_record_from_block(block, trec);
94             get_session_record(dev, trec, &sessrec);
95             record_cb(jcr, dev, block, trec);
96             free_record(trec);
97             goto next_record;         /* go read new tape */
98
99          } else if (dev->state & ST_EOF) {
100             Jmsg(jcr, M_INFO, 0, "Got EOF at file %u  on device %s, Volume \"%s\"\n", 
101                   dev->file, dev_name(dev), jcr->VolumeName);
102             Dmsg0(20, "read_record got eof. try again\n");
103             continue;
104          } else if (dev->state & ST_SHORT) {
105             Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
106             continue;
107          } else {
108             /* I/O error or strange end of tape */
109             display_tape_error_status(jcr, dev);
110             ok = FALSE;
111             break;
112          }
113       }
114       if (verbose) {
115          Dmsg4(000, "Block: %d VI=%u VT=%u blen=%d\n", block->BlockNumber, 
116             block->VolSessionId, block->VolSessionTime, block->block_len);
117       }
118
119 next_record:
120       record = 0;
121       for (rec->state=0; !is_block_empty(rec); ) {
122          if (!read_record_from_block(block, rec)) {
123             Dmsg3(10, "!read-break. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
124                   block->BlockNumber, rec->remainder);
125             break;
126          }
127          Dmsg3(10, "read-OK. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
128                   block->BlockNumber, rec->remainder);
129          /*
130           * At this point, we have at least a record header.
131           *  Now decide if we want this record or not, but remember
132           *  before accessing the record, we may need to read again to
133           *  get all the data.
134           */
135          record++;
136          if (verbose) {
137             Dmsg6(30, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
138                rec_state_to_str(rec), block->BlockNumber,
139                rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
140          }
141          if (debug_level >= 30) {
142             Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
143                   FI_to_ascii(rec->FileIndex), 
144                   stream_to_ascii(rec->Stream, rec->FileIndex), 
145                   rec->data_len);
146          }
147
148          if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
149             Dmsg0(40, "Get EOM LABEL\n");
150             rec->remainder = 0;
151             break;                         /* yes, get out */
152          }
153
154          /* Some sort of label? */ 
155          if (rec->FileIndex < 0) {
156             get_session_record(dev, rec, &sessrec);
157             record_cb(jcr, dev, block, rec);
158             continue;
159          } /* end if label record */
160
161          /* 
162           * Apply BSR filter
163           */
164          if (jcr->bsr) {
165             int stat = match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec);
166             if (stat == -1) { /* no more possible matches */
167                done = TRUE;   /* all items found, stop */
168                break;
169             } else if (stat == 0) {  /* no match */
170                if (verbose) {
171                   Dmsg5(10, "BSR no match rec=%d block=%d SessId=%d SessTime=%d FI=%d\n",
172                      record, block->BlockNumber, rec->VolSessionId, rec->VolSessionTime, 
173                      rec->FileIndex);
174                }
175                rec->remainder = 0;
176                continue;              /* we don't want record, read next one */
177             }
178          }
179          if (is_partial_record(rec)) {
180             Dmsg6(10, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
181                rec_state_to_str(rec), block->BlockNumber,
182                rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
183             break;                    /* read second part of record */
184          }
185          record_cb(jcr, dev, block, rec);
186       }
187    }
188    if (verbose) {
189       printf("%u files found.\n", num_files);
190    }
191    free_record(rec);
192    free_block(block);
193    return ok;
194 }
195
196
197 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
198 {
199    char *rtype;
200    memset(sessrec, 0, sizeof(sessrec));
201    switch (rec->FileIndex) {
202       case PRE_LABEL:
203          rtype = "Fresh Volume Label";   
204          break;
205       case VOL_LABEL:
206          rtype = "Volume Label";
207          unser_volume_label(dev, rec);
208          break;
209       case SOS_LABEL:
210          rtype = "Begin Session";
211          unser_session_label(sessrec, rec);
212          break;
213       case EOS_LABEL:
214          rtype = "End Session";
215          break;
216       case EOM_LABEL:
217          rtype = "End of Media";
218          break;
219       default:
220          rtype = "Unknown";
221          break;
222    }
223    Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
224          rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
225 }
226
227 #ifdef DEBUG
228 static char *rec_state_to_str(DEV_RECORD *rec)
229 {
230    static char buf[200]; 
231    buf[0] = 0;
232    if (rec->state & REC_NO_HEADER) {
233       strcat(buf, "Nohdr,");
234    }
235    if (is_partial_record(rec)) {
236       strcat(buf, "partial,");
237    }
238    if (rec->state & REC_BLOCK_EMPTY) {
239       strcat(buf, "empty,");
240    }
241    if (rec->state & REC_NO_MATCH) {
242       strcat(buf, "Nomatch,");
243    }
244    if (rec->state & REC_CONTINUATION) {
245       strcat(buf, "cont,");
246    }
247    if (buf[0]) {
248       buf[strlen(buf)-1] = 0;
249    }
250    return buf;
251 }
252 #endif