3 * record.c -- tape record handling functions
5 * Kern Sibbald, April MMI
6 * added BB02 format October MMII
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,
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_MD5_SIGNATURE:
97 case STREAM_GZIP_DATA:
99 case STREAM_WIN32_ATTRIBUTES:
101 case STREAM_SPARSE_DATA:
102 return "SPARSE-DATA";
103 case STREAM_SPARSE_GZIP_DATA:
104 return "SPARSE-GZIP";
105 case STREAM_PROGRAM_NAMES:
107 case STREAM_PROGRAM_DATA:
109 case -STREAM_UNIX_ATTRIBUTES:
111 case -STREAM_FILE_DATA:
113 case -STREAM_MD5_SIGNATURE:
115 case -STREAM_GZIP_DATA:
117 case -STREAM_WIN32_ATTRIBUTES:
118 return "contWIN32-ATTR";
119 case -STREAM_SPARSE_DATA:
120 return "contSPARSE-DATA";
121 case -STREAM_SPARSE_GZIP_DATA:
122 return "contSPARSE-GZIP";
123 case -STREAM_PROGRAM_NAMES:
124 return "contPROG-NAMES";
125 case -STREAM_PROGRAM_DATA:
126 return "contPROG-DATA";
128 sprintf(buf, "%d", stream);
134 * Return a new record entity
136 DEV_RECORD *new_record(void)
140 rec = (DEV_RECORD *) get_memory(sizeof(DEV_RECORD));
141 memset(rec, 0, sizeof(DEV_RECORD));
142 rec->data = get_pool_memory(PM_MESSAGE);
147 * Free the record entity
150 void free_record(DEV_RECORD *rec)
152 Dmsg0(150, "Enter free_record.\n");
154 free_pool_memory(rec->data);
156 Dmsg0(150, "Data buf is freed.\n");
157 free_pool_memory((POOLMEM *)rec);
158 Dmsg0(150, "Leave free_record.\n");
163 * Write a Record to the block
165 * Returns: 0 on failure (none or partially written)
166 * 1 on success (all bytes written)
168 * and remainder returned in packet.
170 * We require enough room for the header, and we deal with
171 * two special cases. 1. Only part of the record may have
172 * been transferred the last time (when remainder is
173 * non-zero), and 2. The remaining bytes to write may not
174 * all fit into the block.
176 int write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
181 remlen = block->buf_len - block->binbuf;
183 ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
186 Dmsg6(190, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
187 rem=%d remainder=%d\n",
188 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
189 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
190 remlen, rec->remainder);
193 * If rec->remainder is non-zero, we have been called a
194 * second (or subsequent) time to finish writing a record
195 * that did not previously fit into the block.
197 if (rec->remainder == 0) {
198 /* Require enough room to write a full header */
199 if (remlen >= WRITE_RECHDR_LENGTH) {
200 ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
201 if (BLOCK_VER == 1) {
202 ser_uint32(rec->VolSessionId);
203 ser_uint32(rec->VolSessionTime);
205 block->VolSessionId = rec->VolSessionId;
206 block->VolSessionTime = rec->VolSessionTime;
208 ser_int32(rec->FileIndex);
209 ser_int32(rec->Stream);
210 ser_uint32(rec->data_len);
212 block->bufp += WRITE_RECHDR_LENGTH;
213 block->binbuf += WRITE_RECHDR_LENGTH;
214 remlen -= WRITE_RECHDR_LENGTH;
215 rec->remainder = rec->data_len;
217 rec->remainder = rec->data_len + WRITE_RECHDR_LENGTH;
222 * We are here to write unwritten bytes from a previous
223 * time. Presumably we have a new buffer (possibly
224 * containing a volume label), so the new header
225 * should be able to fit in the block -- otherwise we have
226 * an error. Note, we have to continue splitting the
227 * data record if it is longer than the block.
229 * First, write the header, then write as much as
230 * possible of the data record.
232 * Every time we write a header and it is a continuation
233 * of a previous partially written record, we store the
234 * Stream as -Stream in the record header.
236 ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
237 if (BLOCK_VER == 1) {
238 ser_uint32(rec->VolSessionId);
239 ser_uint32(rec->VolSessionTime);
241 block->VolSessionId = rec->VolSessionId;
242 block->VolSessionTime = rec->VolSessionTime;
244 ser_int32(rec->FileIndex);
245 if (rec->remainder > rec->data_len) {
246 ser_int32(rec->Stream); /* normal full header */
247 ser_uint32(rec->data_len);
248 rec->remainder = rec->data_len; /* must still do data record */
250 ser_int32(-rec->Stream); /* mark this as a continuation record */
251 ser_uint32(rec->remainder); /* bytes to do */
254 /* Require enough room to write a full header */
255 ASSERT(remlen >= WRITE_RECHDR_LENGTH);
257 block->bufp += WRITE_RECHDR_LENGTH;
258 block->binbuf += WRITE_RECHDR_LENGTH;
259 remlen -= WRITE_RECHDR_LENGTH;
262 return 0; /* partial transfer */
266 * Now deal with data record.
267 * Part of it may have already been transferred, and we
268 * may not have enough room to transfer the whole this time.
270 if (rec->remainder > 0) {
271 /* Write as much of data as possible */
272 if (remlen >= rec->remainder) {
273 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
275 block->bufp += rec->remainder;
276 block->binbuf += rec->remainder;
278 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
281 if (!sm_check_rtn(__FILE__, __LINE__, False)) {
282 /* We damaged a buffer */
283 Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
284 rem=%d remainder=%d\n",
285 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
286 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
287 remlen, rec->remainder);
288 Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
289 block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
291 Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
292 block->buf, block->bufp-block->buf);
294 Emsg0(M_ABORT, 0, "Damaged buffer\n");
298 block->bufp += remlen;
299 block->binbuf += remlen;
300 rec->remainder -= remlen;
301 return 0; /* did partial transfer */
304 rec->remainder = 0; /* did whole transfer */
310 * Test if we can write whole record to the block
312 * Returns: 0 on failure
313 * 1 on success (all bytes can be written)
315 int can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
319 remlen = block->buf_len - block->binbuf;
320 if (rec->remainder == 0) {
321 if (remlen >= WRITE_RECHDR_LENGTH) {
322 remlen -= WRITE_RECHDR_LENGTH;
323 rec->remainder = rec->data_len;
330 if (rec->remainder > 0 && remlen < rec->remainder) {
338 * Read a Record from the block
339 * Returns: 0 if nothing read or if the continuation record does not match.
340 * In both of these cases, a block read must be done.
341 * 1 if at least the record header was read, this
342 * routine may have to be called again with a new
343 * block if the entire record was not read.
345 int read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
349 uint32_t VolSessionId;
350 uint32_t VolSessionTime;
356 remlen = block->binbuf;
357 rec->Block = block->BlockNumber;
358 rec->File = ((DEVICE *)block->dev)->file;
360 /* Clear state flags */
362 if (((DEVICE *)block->dev)->state & ST_TAPE) {
363 rec->state |= REC_ISTAPE;
368 * Get the header. There is always a full header,
369 * otherwise we find it in the next block.
371 Dmsg3(100, "Block=%d Ver=%d size=%u\n", block->BlockNumber, block->BlockVer,
373 if (block->BlockVer == 1) {
374 rhl = RECHDR1_LENGTH;
376 rhl = RECHDR2_LENGTH;
379 Dmsg4(90, "Enter read_record_block: remlen=%d data_len=%d rem=%d blkver=%d\n",
380 remlen, rec->data_len, rec->remainder, block->BlockVer);
382 unser_begin(block->bufp, WRITE_RECHDR_LENGTH);
383 if (block->BlockVer == 1) {
384 unser_uint32(VolSessionId);
385 unser_uint32(VolSessionTime);
387 VolSessionId = block->VolSessionId;
388 VolSessionTime = block->VolSessionTime;
390 unser_int32(FileIndex);
392 unser_uint32(data_bytes);
395 block->binbuf -= rhl;
398 /* If we are looking for more (remainder!=0), we reject anything
399 * where the VolSessionId and VolSessionTime don't agree
401 if (rec->remainder && (rec->VolSessionId != VolSessionId ||
402 rec->VolSessionTime != VolSessionTime)) {
403 rec->state |= REC_NO_MATCH;
404 return 0; /* This is from some other Session */
407 /* if Stream is negative, it means that this is a continuation
408 * of a previous partially written record.
410 if (Stream < 0) { /* continuation record? */
411 Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n",
413 rec->state |= REC_CONTINUATION;
414 if (!rec->remainder) { /* if we didn't read previously */
415 rec->data_len = 0; /* return data as if no continuation */
416 } else if (rec->Stream != -Stream) {
417 rec->state |= REC_NO_MATCH;
418 return 0; /* This is from some other Session */
420 rec->Stream = -Stream; /* set correct Stream */
421 } else { /* Regular record */
422 rec->Stream = Stream;
423 rec->data_len = 0; /* transfer to beginning of data */
425 rec->VolSessionId = VolSessionId;
426 rec->VolSessionTime = VolSessionTime;
427 rec->FileIndex = FileIndex;
429 Dmsg6(100, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%u\n\
430 remlen=%d data_len=%d\n",
431 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
432 stream_to_ascii(rec->Stream, rec->FileIndex), data_bytes, remlen,
436 * No more records in this block because the number
437 * of remaining bytes are less than a record header
438 * length, so return empty handed, but indicate that
439 * he must read again. By returning, we allow the
440 * higher level routine to fetch the next block and
443 Dmsg0(90, "read_record_block: nothing\n");
445 if (!rec->remainder) {
446 rec->remainder = 1; /* set to expect continuation */
447 rec->data_len = 0; /* no data transferred */
450 rec->state |= (REC_NO_HEADER | REC_BLOCK_EMPTY);
451 empty_block(block); /* mark block empty */
455 ASSERT(data_bytes < MAX_BLOCK_LENGTH); /* temp sanity check */
457 rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
460 * At this point, we have read the header, now we
461 * must transfer as much of the data record as
462 * possible taking into account: 1. A partial
463 * data record may have previously been transferred,
464 * 2. The current block may not contain the whole data
467 if (remlen >= data_bytes) {
468 /* Got whole record */
469 memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
470 block->bufp += data_bytes;
471 block->binbuf -= data_bytes;
472 rec->data_len += data_bytes;
475 memcpy(rec->data+rec->data_len, block->bufp, remlen);
476 block->bufp += remlen;
477 block->binbuf -= remlen;
478 rec->data_len += remlen;
479 rec->remainder = 1; /* partial record transferred */
480 Dmsg1(90, "read_record_block: partial xfered=%d\n", rec->data_len);
481 rec->state |= (REC_PARTIAL_RECORD | REC_BLOCK_EMPTY);
485 Dmsg4(90, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
486 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
487 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
488 return 1; /* transferred full record */