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;
47 SESSION_LABEL sessrec;
49 block = new_block(dev);
52 if (job_cancelled(jcr)) {
56 if (!read_block_from_device(dev, block)) {
57 Dmsg0(20, "!read_record()\n");
58 if (dev->state & ST_EOT) {
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);
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 record = new_record();
71 read_block_from_device(dev, block);
72 read_record_from_block(block, record);
73 get_session_record(dev, record, &sessrec);
74 record_cb(jcr, dev, block, record);
78 if (dev->state & ST_EOF) {
79 Emsg1(M_INFO, 0, "Got EOF on device %s\n", dev_name(dev));
80 Dmsg0(20, "read_record got eof. try again\n");
83 if (dev->state & ST_SHORT) {
84 Emsg0(M_INFO, 0, dev->errmsg);
87 // display_error_status();
92 Dmsg2(10, "Block: %d blen=%d\n", block->BlockNumber, block->block_len);
97 for (rec->state=0; !is_block_empty(rec); ) {
98 if (!read_record_from_block(block, rec)) {
99 Dmsg3(10, "!read-break. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
100 block->BlockNumber, rec->remainder);
103 Dmsg3(10, "read-OK. stat=%s blk=%d rem=%d\n", rec_state_to_str(rec),
104 block->BlockNumber, rec->remainder);
106 * At this point, we have at least a record header.
107 * Now decide if we want this record or not, but remember
108 * before accessing the record, we may need to read again to
113 Dmsg6(30, "recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
114 rec_state_to_str(rec), block->BlockNumber,
115 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
117 if (debug_level >= 30) {
118 Dmsg4(30, "VolSId=%ld FI=%s Strm=%s Size=%ld\n", rec->VolSessionId,
119 FI_to_ascii(rec->FileIndex), stream_to_ascii(rec->Stream),
123 if (rec->FileIndex == EOM_LABEL) { /* end of tape? */
124 Dmsg0(40, "Get EOM LABEL\n");
126 break; /* yes, get out */
129 /* Some sort of label? */
130 if (rec->FileIndex < 0) {
131 get_session_record(dev, rec, &sessrec);
132 record_cb(jcr, dev, block, rec);
134 } /* end if label record */
139 if (jcr->bsr && !match_bsr(jcr->bsr, rec, &dev->VolHdr, &sessrec)) {
141 Dmsg5(10, "BSR no match rec=%d block=%d SessId=%d SessTime=%d FI=%d\n",
142 record, block->BlockNumber, rec->VolSessionId, rec->VolSessionTime,
146 continue; /* we don't want record, read next one */
148 if (is_partial_record(rec)) {
149 Dmsg6(10, "Partial, break. recno=%d state=%s blk=%d SI=%d ST=%d FI=%d\n", record,
150 rec_state_to_str(rec), block->BlockNumber,
151 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex);
152 break; /* read second part of record */
154 record_cb(jcr, dev, block, rec);
159 printf("%u files found.\n", num_files);
167 static void get_session_record(DEVICE *dev, DEV_RECORD *rec, SESSION_LABEL *sessrec)
170 memset(sessrec, 0, sizeof(sessrec));
171 switch (rec->FileIndex) {
173 rtype = "Fresh Volume Label";
176 rtype = "Volume Label";
177 unser_volume_label(dev, rec);
180 rtype = "Begin Session";
181 unser_session_label(sessrec, rec);
184 rtype = "End Session";
187 rtype = "End of Media";
193 Dmsg5(10, "%s Record: VolSessionId=%d VolSessionTime=%d JobId=%d DataLen=%d\n",
194 rtype, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
197 static char *rec_state_to_str(DEV_RECORD *rec)
199 static char buf[200];
201 if (rec->state & REC_NO_HEADER) {
202 strcat(buf, "Nohdr,");
204 if (is_partial_record(rec)) {
205 strcat(buf, "partial,");
207 if (rec->state & REC_BLOCK_EMPTY) {
208 strcat(buf, "empty,");
210 if (rec->state & REC_NO_MATCH) {
211 strcat(buf, "Nomatch,");
213 if (rec->state & REC_CONTINUATION) {
214 strcat(buf, "cont,");
217 buf[strlen(buf)-1] = 0;