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