3 * record.c -- tape record handling functions
5 * Kern Sibbald, April MMI
6 * added BB02 format October MMII
12 Copyright (C) 2000-2003 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 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 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);
159 * Free the record entity
162 void free_record(DEV_RECORD *rec)
164 Dmsg0(150, "Enter free_record.\n");
166 free_pool_memory(rec->data);
168 Dmsg0(150, "Data buf is freed.\n");
169 free_pool_memory((POOLMEM *)rec);
170 Dmsg0(150, "Leave free_record.\n");
175 * Write a Record to the block
177 * Returns: 0 on failure (none or partially written)
178 * 1 on success (all bytes written)
180 * and remainder returned in packet.
182 * We require enough room for the header, and we deal with
183 * two special cases. 1. Only part of the record may have
184 * been transferred the last time (when remainder is
185 * non-zero), and 2. The remaining bytes to write may not
186 * all fit into the block.
188 int write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
193 remlen = block->buf_len - block->binbuf;
195 ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
198 Dmsg6(190, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
199 rem=%d remainder=%d\n",
200 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
201 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
202 remlen, rec->remainder);
205 * If rec->remainder is non-zero, we have been called a
206 * second (or subsequent) time to finish writing a record
207 * that did not previously fit into the block.
209 if (rec->remainder == 0) {
210 /* Require enough room to write a full header */
211 if (remlen >= WRITE_RECHDR_LENGTH) {
212 ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
213 if (BLOCK_VER == 1) {
214 ser_uint32(rec->VolSessionId);
215 ser_uint32(rec->VolSessionTime);
217 block->VolSessionId = rec->VolSessionId;
218 block->VolSessionTime = rec->VolSessionTime;
220 ser_int32(rec->FileIndex);
221 ser_int32(rec->Stream);
222 ser_uint32(rec->data_len);
224 block->bufp += WRITE_RECHDR_LENGTH;
225 block->binbuf += WRITE_RECHDR_LENGTH;
226 remlen -= WRITE_RECHDR_LENGTH;
227 rec->remainder = rec->data_len;
228 if (rec->FileIndex > 0) {
229 /* If data record, update what we have in this block */
230 if (block->FirstIndex == 0) {
231 block->FirstIndex = rec->FileIndex;
233 block->LastIndex = rec->FileIndex;
236 rec->remainder = rec->data_len + WRITE_RECHDR_LENGTH;
241 * We are here to write unwritten bytes from a previous
242 * time. Presumably we have a new buffer (possibly
243 * containing a volume label), so the new header
244 * should be able to fit in the block -- otherwise we have
245 * an error. Note, we have to continue splitting the
246 * data record if it is longer than the block.
248 * First, write the header, then write as much as
249 * possible of the data record.
251 * Every time we write a header and it is a continuation
252 * of a previous partially written record, we store the
253 * Stream as -Stream in the record header.
255 ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
256 if (BLOCK_VER == 1) {
257 ser_uint32(rec->VolSessionId);
258 ser_uint32(rec->VolSessionTime);
260 block->VolSessionId = rec->VolSessionId;
261 block->VolSessionTime = rec->VolSessionTime;
263 ser_int32(rec->FileIndex);
264 if (rec->remainder > rec->data_len) {
265 ser_int32(rec->Stream); /* normal full header */
266 ser_uint32(rec->data_len);
267 rec->remainder = rec->data_len; /* must still do data record */
269 ser_int32(-rec->Stream); /* mark this as a continuation record */
270 ser_uint32(rec->remainder); /* bytes to do */
273 /* Require enough room to write a full header */
274 ASSERT(remlen >= WRITE_RECHDR_LENGTH);
276 block->bufp += WRITE_RECHDR_LENGTH;
277 block->binbuf += WRITE_RECHDR_LENGTH;
278 remlen -= WRITE_RECHDR_LENGTH;
279 if (rec->FileIndex > 0) {
280 /* If data record, update what we have in this block */
281 if (block->FirstIndex == 0) {
282 block->FirstIndex = rec->FileIndex;
284 block->LastIndex = rec->FileIndex;
288 return 0; /* partial transfer */
292 * Now deal with data record.
293 * Part of it may have already been transferred, and we
294 * may not have enough room to transfer the whole this time.
296 if (rec->remainder > 0) {
297 /* Write as much of data as possible */
298 if (remlen >= rec->remainder) {
299 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
301 block->bufp += rec->remainder;
302 block->binbuf += rec->remainder;
304 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
307 if (!sm_check_rtn(__FILE__, __LINE__, False)) {
308 /* We damaged a buffer */
309 Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
310 rem=%d remainder=%d\n",
311 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
312 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
313 remlen, rec->remainder);
314 Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
315 block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
317 Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
318 block->buf, block->bufp-block->buf);
320 Emsg0(M_ABORT, 0, "Damaged buffer\n");
324 block->bufp += remlen;
325 block->binbuf += remlen;
326 rec->remainder -= remlen;
327 return 0; /* did partial transfer */
330 rec->remainder = 0; /* did whole transfer */
336 * Test if we can write whole record to the block
338 * Returns: 0 on failure
339 * 1 on success (all bytes can be written)
341 int can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
345 remlen = block->buf_len - block->binbuf;
346 if (rec->remainder == 0) {
347 if (remlen >= WRITE_RECHDR_LENGTH) {
348 remlen -= WRITE_RECHDR_LENGTH;
349 rec->remainder = rec->data_len;
356 if (rec->remainder > 0 && remlen < rec->remainder) {
364 * Read a Record from the block
365 * Returns: 0 if nothing read or if the continuation record does not match.
366 * In both of these cases, a block read must be done.
367 * 1 if at least the record header was read, this
368 * routine may have to be called again with a new
369 * block if the entire record was not read.
371 int read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
375 uint32_t VolSessionId;
376 uint32_t VolSessionTime;
382 remlen = block->binbuf;
383 rec->Block = block->BlockNumber;
384 rec->File = ((DEVICE *)block->dev)->file;
386 /* Clear state flags */
388 if (((DEVICE *)block->dev)->state & ST_TAPE) {
389 rec->state |= REC_ISTAPE;
394 * Get the header. There is always a full header,
395 * otherwise we find it in the next block.
397 Dmsg3(100, "Block=%d Ver=%d size=%u\n", block->BlockNumber, block->BlockVer,
399 if (block->BlockVer == 1) {
400 rhl = RECHDR1_LENGTH;
402 rhl = RECHDR2_LENGTH;
405 Dmsg4(90, "Enter read_record_block: remlen=%d data_len=%d rem=%d blkver=%d\n",
406 remlen, rec->data_len, rec->remainder, block->BlockVer);
408 unser_begin(block->bufp, WRITE_RECHDR_LENGTH);
409 if (block->BlockVer == 1) {
410 unser_uint32(VolSessionId);
411 unser_uint32(VolSessionTime);
413 VolSessionId = block->VolSessionId;
414 VolSessionTime = block->VolSessionTime;
416 unser_int32(FileIndex);
418 unser_uint32(data_bytes);
421 block->binbuf -= rhl;
424 /* If we are looking for more (remainder!=0), we reject anything
425 * where the VolSessionId and VolSessionTime don't agree
427 if (rec->remainder && (rec->VolSessionId != VolSessionId ||
428 rec->VolSessionTime != VolSessionTime)) {
429 rec->state |= REC_NO_MATCH;
430 return 0; /* This is from some other Session */
433 /* if Stream is negative, it means that this is a continuation
434 * of a previous partially written record.
436 if (Stream < 0) { /* continuation record? */
437 Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n",
439 rec->state |= REC_CONTINUATION;
440 if (!rec->remainder) { /* if we didn't read previously */
441 rec->data_len = 0; /* return data as if no continuation */
442 } else if (rec->Stream != -Stream) {
443 rec->state |= REC_NO_MATCH;
444 return 0; /* This is from some other Session */
446 rec->Stream = -Stream; /* set correct Stream */
447 } else { /* Regular record */
448 rec->Stream = Stream;
449 rec->data_len = 0; /* transfer to beginning of data */
451 rec->VolSessionId = VolSessionId;
452 rec->VolSessionTime = VolSessionTime;
453 rec->FileIndex = FileIndex;
455 if (block->FirstIndex == 0) {
456 block->FirstIndex = FileIndex;
458 block->LastIndex = FileIndex;
461 Dmsg6(100, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%u\n"
462 "remlen=%d data_len=%d\n",
463 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
464 stream_to_ascii(rec->Stream, rec->FileIndex), data_bytes, remlen,
468 * No more records in this block because the number
469 * of remaining bytes are less than a record header
470 * length, so return empty handed, but indicate that
471 * he must read again. By returning, we allow the
472 * higher level routine to fetch the next block and
475 Dmsg0(90, "read_record_block: nothing\n");
477 if (!rec->remainder) {
478 rec->remainder = 1; /* set to expect continuation */
479 rec->data_len = 0; /* no data transferred */
482 rec->state |= (REC_NO_HEADER | REC_BLOCK_EMPTY);
483 empty_block(block); /* mark block empty */
487 ASSERT(data_bytes < MAX_BLOCK_LENGTH); /* temp sanity check */
489 rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
492 * At this point, we have read the header, now we
493 * must transfer as much of the data record as
494 * possible taking into account: 1. A partial
495 * data record may have previously been transferred,
496 * 2. The current block may not contain the whole data
499 if (remlen >= data_bytes) {
500 /* Got whole record */
501 memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
502 block->bufp += data_bytes;
503 block->binbuf -= data_bytes;
504 rec->data_len += data_bytes;
507 memcpy(rec->data+rec->data_len, block->bufp, remlen);
508 block->bufp += remlen;
509 block->binbuf -= remlen;
510 rec->data_len += remlen;
511 rec->remainder = 1; /* partial record transferred */
512 Dmsg1(90, "read_record_block: partial xfered=%d\n", rec->data_len);
513 rec->state |= (REC_PARTIAL_RECORD | REC_BLOCK_EMPTY);
517 Dmsg4(90, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
518 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
519 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
520 return 1; /* transferred full record */