]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read_record.c
Implement Pmsg() and block number checking
[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             if (!mount_cb(jcr, dev, block)) {
67                Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
68                   block->BlockNumber, rec->remainder);
69                ok = FALSE;
70                /*
71                 * Create EOT Label so that Media record may
72                 *  be properly updated because this is the last
73                 *  tape.
74                 */
75                trec->FileIndex = EOT_LABEL;
76                trec->File = dev->file;
77                trec->Block = rec->Block; /* return block last read */
78                record_cb(jcr, dev, block, trec);
79                free_record(trec);
80                break;
81             }
82             Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
83                   block->BlockNumber, rec->remainder);
84             /*
85              * We just have a new tape up, now read the label (first record)
86              *  and pass it off to the callback routine, then continue
87              *  most likely reading the previous record.
88              */
89             read_block_from_device(jcr, dev, block, NO_BLOCK_NUMBER_CHECK);
90             read_record_from_block(block, trec);
91             get_session_record(dev, trec, &sessrec);
92             record_cb(jcr, dev, block, trec);
93             free_record(trec);
94             goto next_record;
95          }
96          if (dev->state & ST_EOF) {
97             Emsg2(M_INFO, 0, "Got EOF on device %s, Volume \"%s\"\n", 
98                   dev_name(dev), jcr->VolumeName);
99             Dmsg0(20, "read_record got eof. try again\n");
100             continue;
101          }
102          if (dev->state & ST_SHORT) {
103             Emsg0(M_INFO, 0, dev->errmsg);
104             continue;
105          }
106 //       display_error_status();
107          ok = FALSE;
108          break;
109       }
110       if (verbose) {
111          Dmsg2(10, "Block: %d blen=%d\n", block->BlockNumber, block->block_len);
112       }
113
114 next_record:
115       record = 0;
116       for (rec->state=0; !is_block_empty(rec); ) {
117          if (!read_record_from_block(block, rec)) {
118             Dmsg3(10, "!read-break. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
119                   block->BlockNumber, rec->remainder);
120             break;
121          }
122          Dmsg3(10, "read-OK. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec), 
123                   block->BlockNumber, rec->remainder);
124          /*
125           * At this point, we have at least a record header.
126           *  Now decide if we want this record or not, but remember
127           *  before accessing the record, we may need to read again to
128           *  get all the data.
129           */
130          record++;
131          if (verbose) {
132             Dmsg6(30, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
133                rec_state_to_str(rec), block->BlockNumber,
134                rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
135          }
136          if (debug_level >= 30) {
137             Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
138                   FI_to_ascii(rec->FileIndex), 
139                   stream_to_ascii(rec->Stream, rec->FileIndex), 
140                   rec->data_len);
141          }
142
143          if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
144             Dmsg0(40, "Get EOM LABEL\n");
145             rec->remainder = 0;
146             break;                         /* yes, get out */
147          }
148
149          /* Some sort of label? */ 
150          if (rec->FileIndex < 0) {
151             get_session_record(dev, rec, &sessrec);
152             record_cb(jcr, dev, block, rec);
153             continue;
154          } /* end if label record */
155
156          /* 
157           * Apply BSR filter
158           */
159          if (jcr->bsr) {
160             int stat = match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec);
161             if (stat == -1) { /* no more possible matches */
162                done = TRUE;   /* all items found, stop */
163                break;
164             } else if (stat == 0) {  /* no match */
165                if (verbose) {
166                   Dmsg5(10, "BSR no match rec=%d block=%d SessId=%d SessTime=%d FI=%d\n",
167                      record, block->BlockNumber, rec->VolSessionId, rec->VolSessionTime, 
168                      rec->FileIndex);
169                }
170                rec->remainder = 0;
171                continue;              /* we don't want record, read next one */
172             }
173          }
174          if (is_partial_record(rec)) {
175             Dmsg6(10, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
176                rec_state_to_str(rec), block->BlockNumber,
177                rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
178             break;                    /* read second part of record */
179          }
180          record_cb(jcr, dev, block, rec);
181       }
182    }
183    if (verbose) {
184       printf("%u files found.\n", num_files);
185    }
186    free_record(rec);
187    free_block(block);
188    return ok;
189 }
190
191
192 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
193 {
194    char *rtype;
195    memset(sessrec, 0, sizeof(sessrec));
196    switch (rec->FileIndex) {
197       case PRE_LABEL:
198          rtype = "Fresh Volume Label";   
199          break;
200       case VOL_LABEL:
201          rtype = "Volume Label";
202          unser_volume_label(dev, rec);
203          break;
204       case SOS_LABEL:
205          rtype = "Begin Session";
206          unser_session_label(sessrec, rec);
207          break;
208       case EOS_LABEL:
209          rtype = "End Session";
210          break;
211       case EOM_LABEL:
212          rtype = "End of Media";
213          break;
214       default:
215          rtype = "Unknown";
216          break;
217    }
218    Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
219          rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
220 }
221
222 #ifdef DEBUG
223 static char *rec_state_to_str(DEV_RECORD *rec)
224 {
225    static char buf[200]; 
226    buf[0] = 0;
227    if (rec->state & REC_NO_HEADER) {
228       strcat(buf, "Nohdr,");
229    }
230    if (is_partial_record(rec)) {
231       strcat(buf, "partial,");
232    }
233    if (rec->state & REC_BLOCK_EMPTY) {
234       strcat(buf, "empty,");
235    }
236    if (rec->state & REC_NO_MATCH) {
237       strcat(buf, "Nomatch,");
238    }
239    if (rec->state & REC_CONTINUATION) {
240       strcat(buf, "cont,");
241    }
242    if (buf[0]) {
243       buf[strlen(buf)-1] = 0;
244    }
245    return buf;
246 }
247 #endif