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, ...
12 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
34 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec);
35 static char *rec_state_to_str(DEV_RECORD *rec);
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))
44 uint32_t record, num_files = 0;
48 SESSION_LABEL sessrec;
50 block = new_block(dev);
52 for ( ;ok && !done; ) {
53 if (job_cancelled(jcr)) {
57 if (!read_block_from_device(dev, block)) {
58 Dmsg0(20, "!read_record()\n");
59 if (dev->state & ST_EOT) {
60 DEV_RECORD *trec = new_record();
62 Dmsg3(100, "EOT. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
63 block->BlockNumber, rec->remainder);
64 if (!mount_cb(jcr, dev, block)) {
65 Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
66 block->BlockNumber, rec->remainder);
69 * Create EOT Label so that Media record may
70 * be properly updated because this is the last
73 trec->FileIndex = EOT_LABEL;
74 trec->File = dev->file;
75 trec->Block = rec->Block; /* return block last read */
76 record_cb(jcr, dev, block, trec);
80 Dmsg3(100, "After mount next vol. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
81 block->BlockNumber, rec->remainder);
83 * We just have a new tape up, now read the label (first record)
84 * and pass it off to the callback routine, then continue
85 * most likely reading the previous record.
87 read_block_from_device(dev, block);
88 read_record_from_block(block, trec);
89 get_session_record(dev, trec, &sessrec);
90 record_cb(jcr, dev, block, trec);
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");
100 if (dev->state & ST_SHORT) {
101 Emsg0(M_INFO, 0, dev->errmsg);
104 // display_error_status();
109 Dmsg2(10, "Block: %d blen=%d\n", block->BlockNumber, block->block_len);
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);
120 Dmsg3(10, "read-OK. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
121 block->BlockNumber, rec->remainder);
123 * At this point, we have at least a record header.
124 * Now decide if we want this record or not, but remember
125 * before accessing the record, we may need to read again to
130 Dmsg6(30, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
131 rec_state_to_str(rec), block->BlockNumber,
132 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
134 if (debug_level >= 30) {
135 Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
136 FI_to_ascii(rec->FileIndex),
137 stream_to_ascii(rec->Stream, rec->FileIndex),
141 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
142 Dmsg0(40, "Get EOM LABEL\n");
144 break; /* yes, get out */
147 /* Some sort of label? */
148 if (rec->FileIndex < 0) {
149 get_session_record(dev, rec, &sessrec);
150 record_cb(jcr, dev, block, rec);
152 } /* end if label record */
158 int stat = match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec);
159 if (stat == -1) { /* no more possible matches */
160 done = TRUE; /* all items found, stop */
162 } else if (stat == 0) { /* no match */
164 Dmsg5(10, "BSR no match rec=%d block=%d SessId=%d SessTime=%d FI=%d\n",
165 record, block->BlockNumber, rec->VolSessionId, rec->VolSessionTime,
169 continue; /* we don't want record, read next one */
172 if (is_partial_record(rec)) {
173 Dmsg6(10, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
174 rec_state_to_str(rec), block->BlockNumber,
175 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
176 break; /* read second part of record */
178 record_cb(jcr, dev, block, rec);
182 printf("%u files found.\n", num_files);
190 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
193 memset(sessrec, 0, sizeof(sessrec));
194 switch (rec->FileIndex) {
196 rtype = "Fresh Volume Label";
199 rtype = "Volume Label";
200 unser_volume_label(dev, rec);
203 rtype = "Begin Session";
204 unser_session_label(sessrec, rec);
207 rtype = "End Session";
210 rtype = "End of Media";
216 Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
217 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
220 static char *rec_state_to_str(DEV_RECORD *rec)
222 static char buf[200];
224 if (rec->state & REC_NO_HEADER) {
225 strcat(buf, "Nohdr,");
227 if (is_partial_record(rec)) {
228 strcat(buf, "partial,");
230 if (rec->state & REC_BLOCK_EMPTY) {
231 strcat(buf, "empty,");
233 if (rec->state & REC_NO_MATCH) {
234 strcat(buf, "Nomatch,");
236 if (rec->state & REC_CONTINUATION) {
237 strcat(buf, "cont,");
240 buf[strlen(buf)-1] = 0;