3 * record.c -- tape record handling functions
5 * Kern Sibbald, April MMI
6 * added BB02 format October MMII
12 Copyright (C) 2000-2004 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,
35 extern int debug_level;
38 * Convert a FileIndex into a printable
39 * ASCII string. Not reentrant.
40 * If the FileIndex is negative, it flags the
41 * record as a Label, otherwise it is simply
42 * the FileIndex of the current file.
44 const char *FI_to_ascii(int fi)
48 sprintf(buf, "%d", fi);
66 sprintf(buf, "unknown: %d", fi);
73 * Convert a Stream ID into a printable
74 * ASCII string. Not reentrant.
76 * A negative stream number represents
77 * stream data that is continued from a
78 * record in the previous block.
79 * If the FileIndex is negative, we are
80 * dealing with a Label, hence the
81 * stream is the JobId.
83 const char *stream_to_ascii(int stream, int fi)
87 sprintf(buf, "%d", stream);
91 case STREAM_UNIX_ATTRIBUTES:
93 case STREAM_FILE_DATA:
95 case STREAM_WIN32_DATA:
97 case STREAM_WIN32_GZIP_DATA:
99 case STREAM_MD5_SIGNATURE:
101 case STREAM_SHA1_SIGNATURE:
103 case STREAM_GZIP_DATA:
105 case STREAM_UNIX_ATTRIBUTES_EX:
106 return "UNIX-ATTR-EX";
107 case STREAM_SPARSE_DATA:
108 return "SPARSE-DATA";
109 case STREAM_SPARSE_GZIP_DATA:
110 return "SPARSE-GZIP";
111 case STREAM_PROGRAM_NAMES:
113 case STREAM_PROGRAM_DATA:
115 case -STREAM_UNIX_ATTRIBUTES:
117 case -STREAM_FILE_DATA:
119 case -STREAM_WIN32_DATA:
120 return "contWIN32-DATA";
121 case -STREAM_WIN32_GZIP_DATA:
122 return "contWIN32-GZIP";
123 case -STREAM_MD5_SIGNATURE:
125 case -STREAM_SHA1_SIGNATURE:
127 case -STREAM_GZIP_DATA:
129 case -STREAM_UNIX_ATTRIBUTES_EX:
130 return "contUNIX-ATTR-EX";
131 case -STREAM_SPARSE_DATA:
132 return "contSPARSE-DATA";
133 case -STREAM_SPARSE_GZIP_DATA:
134 return "contSPARSE-GZIP";
135 case -STREAM_PROGRAM_NAMES:
136 return "contPROG-NAMES";
137 case -STREAM_PROGRAM_DATA:
138 return "contPROG-DATA";
140 sprintf(buf, "%d", stream);
146 * Return a new record entity
148 DEV_RECORD *new_record(void)
152 rec = (DEV_RECORD *)get_memory(sizeof(DEV_RECORD));
153 memset(rec, 0, sizeof(DEV_RECORD));
154 rec->data = get_pool_memory(PM_MESSAGE);
158 void empty_record(DEV_RECORD *rec)
160 rec->File = rec->Block = 0;
161 rec->VolSessionId = rec->VolSessionTime = 0;
162 rec->FileIndex = rec->Stream = 0;
163 rec->data_len = rec->remainder = 0;
164 rec->state &= ~(REC_PARTIAL_RECORD|REC_BLOCK_EMPTY|REC_NO_MATCH|REC_CONTINUATION);
168 * Free the record entity
171 void free_record(DEV_RECORD *rec)
173 Dmsg0(350, "Enter free_record.\n");
175 free_pool_memory(rec->data);
177 Dmsg0(350, "Data buf is freed.\n");
178 free_pool_memory((POOLMEM *)rec);
179 Dmsg0(350, "Leave free_record.\n");
184 * Write a Record to the block
186 * Returns: false on failure (none or partially written)
187 * true on success (all bytes written)
189 * and remainder returned in packet.
191 * We require enough room for the header, and we deal with
192 * two special cases. 1. Only part of the record may have
193 * been transferred the last time (when remainder is
194 * non-zero), and 2. The remaining bytes to write may not
195 * all fit into the block.
197 bool write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
202 remlen = block->buf_len - block->binbuf;
204 ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
205 ASSERT(block->buf_len >= block->binbuf);
207 Dmsg6(490, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
208 rem=%d remainder=%d\n",
209 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
210 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
211 remlen, rec->remainder);
214 * If rec->remainder is non-zero, we have been called a
215 * second (or subsequent) time to finish writing a record
216 * that did not previously fit into the block.
218 if (rec->remainder == 0) {
219 /* Require enough room to write a full header */
220 if (remlen >= WRITE_RECHDR_LENGTH) {
221 ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
222 if (BLOCK_VER == 1) {
223 ser_uint32(rec->VolSessionId);
224 ser_uint32(rec->VolSessionTime);
226 block->VolSessionId = rec->VolSessionId;
227 block->VolSessionTime = rec->VolSessionTime;
229 ser_int32(rec->FileIndex);
230 ser_int32(rec->Stream);
231 ser_uint32(rec->data_len);
233 block->bufp += WRITE_RECHDR_LENGTH;
234 block->binbuf += WRITE_RECHDR_LENGTH;
235 remlen -= WRITE_RECHDR_LENGTH;
236 rec->remainder = rec->data_len;
237 if (rec->FileIndex > 0) {
238 /* If data record, update what we have in this block */
239 if (block->FirstIndex == 0) {
240 block->FirstIndex = rec->FileIndex;
242 block->LastIndex = rec->FileIndex;
245 rec->remainder = rec->data_len + WRITE_RECHDR_LENGTH;
250 * We are here to write unwritten bytes from a previous
251 * time. Presumably we have a new buffer (possibly
252 * containing a volume label), so the new header
253 * should be able to fit in the block -- otherwise we have
254 * an error. Note, we have to continue splitting the
255 * data record if it is longer than the block.
257 * First, write the header, then write as much as
258 * possible of the data record.
260 * Every time we write a header and it is a continuation
261 * of a previous partially written record, we store the
262 * Stream as -Stream in the record header.
264 ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
265 if (BLOCK_VER == 1) {
266 ser_uint32(rec->VolSessionId);
267 ser_uint32(rec->VolSessionTime);
269 block->VolSessionId = rec->VolSessionId;
270 block->VolSessionTime = rec->VolSessionTime;
272 ser_int32(rec->FileIndex);
273 if (rec->remainder > rec->data_len) {
274 ser_int32(rec->Stream); /* normal full header */
275 ser_uint32(rec->data_len);
276 rec->remainder = rec->data_len; /* must still do data record */
278 ser_int32(-rec->Stream); /* mark this as a continuation record */
279 ser_uint32(rec->remainder); /* bytes to do */
282 /* Require enough room to write a full header */
283 ASSERT(remlen >= WRITE_RECHDR_LENGTH);
285 block->bufp += WRITE_RECHDR_LENGTH;
286 block->binbuf += WRITE_RECHDR_LENGTH;
287 remlen -= WRITE_RECHDR_LENGTH;
288 if (rec->FileIndex > 0) {
289 /* If data record, update what we have in this block */
290 if (block->FirstIndex == 0) {
291 block->FirstIndex = rec->FileIndex;
293 block->LastIndex = rec->FileIndex;
297 return false; /* partial transfer */
301 * Now deal with data record.
302 * Part of it may have already been transferred, and we
303 * may not have enough room to transfer the whole this time.
305 if (rec->remainder > 0) {
306 /* Write as much of data as possible */
307 if (remlen >= rec->remainder) {
308 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
310 block->bufp += rec->remainder;
311 block->binbuf += rec->remainder;
313 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
316 if (!sm_check_rtn(__FILE__, __LINE__, False)) {
317 /* We damaged a buffer */
318 Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
319 rem=%d remainder=%d\n",
320 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
321 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
322 remlen, rec->remainder);
323 Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
324 block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
326 Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
327 block->buf, block->bufp-block->buf);
329 Emsg0(M_ABORT, 0, "Damaged buffer\n");
333 block->bufp += remlen;
334 block->binbuf += remlen;
335 rec->remainder -= remlen;
336 return false; /* did partial transfer */
339 rec->remainder = 0; /* did whole transfer */
345 * Test if we can write whole record to the block
347 * Returns: false on failure
348 * true on success (all bytes can be written)
350 bool can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
354 remlen = block->buf_len - block->binbuf;
355 if (rec->remainder == 0) {
356 if (remlen >= WRITE_RECHDR_LENGTH) {
357 remlen -= WRITE_RECHDR_LENGTH;
358 rec->remainder = rec->data_len;
365 if (rec->remainder > 0 && remlen < rec->remainder) {
373 * Read a Record from the block
374 * Returns: false if nothing read or if the continuation record does not match.
375 * In both of these cases, a block read must be done.
376 * true if at least the record header was read, this
377 * routine may have to be called again with a new
378 * block if the entire record was not read.
380 bool read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
384 uint32_t VolSessionId;
385 uint32_t VolSessionTime;
391 remlen = block->binbuf;
392 rec->Block = block->BlockNumber;
393 rec->File = ((DEVICE *)block->dev)->file;
395 /* Clear state flags */
397 if (((DEVICE *)block->dev)->state & ST_TAPE) {
398 rec->state |= REC_ISTAPE;
403 * Get the header. There is always a full header,
404 * otherwise we find it in the next block.
406 Dmsg3(450, "Block=%d Ver=%d size=%u\n", block->BlockNumber, block->BlockVer,
408 if (block->BlockVer == 1) {
409 rhl = RECHDR1_LENGTH;
411 rhl = RECHDR2_LENGTH;
414 Dmsg4(450, "Enter read_record_block: remlen=%d data_len=%d rem=%d blkver=%d\n",
415 remlen, rec->data_len, rec->remainder, block->BlockVer);
417 unser_begin(block->bufp, WRITE_RECHDR_LENGTH);
418 if (block->BlockVer == 1) {
419 unser_uint32(VolSessionId);
420 unser_uint32(VolSessionTime);
422 VolSessionId = block->VolSessionId;
423 VolSessionTime = block->VolSessionTime;
425 unser_int32(FileIndex);
427 unser_uint32(data_bytes);
430 block->binbuf -= rhl;
433 /* If we are looking for more (remainder!=0), we reject anything
434 * where the VolSessionId and VolSessionTime don't agree
436 if (rec->remainder && (rec->VolSessionId != VolSessionId ||
437 rec->VolSessionTime != VolSessionTime)) {
438 rec->state |= REC_NO_MATCH;
439 Dmsg0(450, "remainder and VolSession doesn't match\n");
440 return false; /* This is from some other Session */
443 /* if Stream is negative, it means that this is a continuation
444 * of a previous partially written record.
446 if (Stream < 0) { /* continuation record? */
447 Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n",
449 rec->state |= REC_CONTINUATION;
450 if (!rec->remainder) { /* if we didn't read previously */
451 rec->data_len = 0; /* return data as if no continuation */
452 } else if (rec->Stream != -Stream) {
453 rec->state |= REC_NO_MATCH;
454 return false; /* This is from some other Session */
456 rec->Stream = -Stream; /* set correct Stream */
457 } else { /* Regular record */
458 rec->Stream = Stream;
459 rec->data_len = 0; /* transfer to beginning of data */
461 rec->VolSessionId = VolSessionId;
462 rec->VolSessionTime = VolSessionTime;
463 rec->FileIndex = FileIndex;
465 if (block->FirstIndex == 0) {
466 block->FirstIndex = FileIndex;
468 block->LastIndex = FileIndex;
471 Dmsg6(450, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%u\n"
472 "remlen=%d data_len=%d\n",
473 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
474 stream_to_ascii(rec->Stream, rec->FileIndex), data_bytes, remlen,
478 * No more records in this block because the number
479 * of remaining bytes are less than a record header
480 * length, so return empty handed, but indicate that
481 * he must read again. By returning, we allow the
482 * higher level routine to fetch the next block and
485 Dmsg0(450, "read_record_block: nothing\n");
487 if (!rec->remainder) {
488 rec->remainder = 1; /* set to expect continuation */
489 rec->data_len = 0; /* no data transferred */
492 rec->state |= (REC_NO_HEADER | REC_BLOCK_EMPTY);
493 empty_block(block); /* mark block empty */
497 ASSERT(data_bytes < MAX_BLOCK_LENGTH); /* temp sanity check */
499 rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
502 * At this point, we have read the header, now we
503 * must transfer as much of the data record as
504 * possible taking into account: 1. A partial
505 * data record may have previously been transferred,
506 * 2. The current block may not contain the whole data
509 if (remlen >= data_bytes) {
510 /* Got whole record */
511 memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
512 block->bufp += data_bytes;
513 block->binbuf -= data_bytes;
514 rec->data_len += data_bytes;
517 memcpy(rec->data+rec->data_len, block->bufp, remlen);
518 block->bufp += remlen;
519 block->binbuf -= remlen;
520 rec->data_len += remlen;
521 rec->remainder = 1; /* partial record transferred */
522 Dmsg1(450, "read_record_block: partial xfered=%d\n", rec->data_len);
523 rec->state |= (REC_PARTIAL_RECORD | REC_BLOCK_EMPTY);
527 Dmsg4(450, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
528 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
529 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
530 return true; /* transferred full record */