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