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 sm_check(__FILE__, __LINE__, False);
182 remlen = block->buf_len - block->binbuf;
184 ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
187 Dmsg6(190, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
188 rem=%d remainder=%d\n",
189 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
190 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
191 remlen, rec->remainder);
194 * If rec->remainder is non-zero, we have been called a
195 * second (or subsequent) time to finish writing a record
196 * that did not previously fit into the block.
198 if (rec->remainder == 0) {
199 /* Require enough room to write a full header */
200 if (remlen >= WRITE_RECHDR_LENGTH) {
201 ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
202 if (BLOCK_VER == 1) {
203 ser_uint32(rec->VolSessionId);
204 ser_uint32(rec->VolSessionTime);
206 block->VolSessionId = rec->VolSessionId;
207 block->VolSessionTime = rec->VolSessionTime;
209 ser_int32(rec->FileIndex);
210 ser_int32(rec->Stream);
211 ser_uint32(rec->data_len);
213 block->bufp += WRITE_RECHDR_LENGTH;
214 block->binbuf += WRITE_RECHDR_LENGTH;
215 remlen -= WRITE_RECHDR_LENGTH;
216 rec->remainder = rec->data_len;
218 rec->remainder = rec->data_len + WRITE_RECHDR_LENGTH;
219 sm_check(__FILE__, __LINE__, False);
224 * We are here to write unwritten bytes from a previous
225 * time. Presumably we have a new buffer (possibly
226 * containing a volume label), so the new header
227 * should be able to fit in the block -- otherwise we have
228 * an error. Note, we have to continue splitting the
229 * data record if it is longer than the block.
231 * First, write the header, then write as much as
232 * possible of the data record.
234 * Every time we write a header and it is a continuation
235 * of a previous partially written record, we store the
236 * Stream as -Stream in the record header.
238 ser_begin(block->bufp, WRITE_RECHDR_LENGTH);
239 if (BLOCK_VER == 1) {
240 ser_uint32(rec->VolSessionId);
241 ser_uint32(rec->VolSessionTime);
243 block->VolSessionId = rec->VolSessionId;
244 block->VolSessionTime = rec->VolSessionTime;
246 ser_int32(rec->FileIndex);
247 if (rec->remainder > rec->data_len) {
248 ser_int32(rec->Stream); /* normal full header */
249 ser_uint32(rec->data_len);
250 rec->remainder = rec->data_len; /* must still do data record */
252 ser_int32(-rec->Stream); /* mark this as a continuation record */
253 ser_uint32(rec->remainder); /* bytes to do */
256 /* Require enough room to write a full header */
257 ASSERT(remlen >= WRITE_RECHDR_LENGTH);
259 block->bufp += WRITE_RECHDR_LENGTH;
260 block->binbuf += WRITE_RECHDR_LENGTH;
261 remlen -= WRITE_RECHDR_LENGTH;
264 sm_check(__FILE__, __LINE__, False);
265 return 0; /* partial transfer */
269 * Now deal with data record.
270 * Part of it may have already been transferred, and we
271 * may not have enough room to transfer the whole this time.
273 if (rec->remainder > 0) {
274 /* Write as much of data as possible */
275 if (remlen >= rec->remainder) {
276 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
278 block->bufp += rec->remainder;
279 block->binbuf += rec->remainder;
281 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
284 if (!sm_check_rtn(__FILE__, __LINE__, False)) {
285 /* We damaged a buffer */
286 Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
287 rem=%d remainder=%d\n",
288 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
289 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
290 remlen, rec->remainder);
291 Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
292 block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
294 Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
295 block->buf, block->bufp-block->buf);
297 Emsg0(M_ABORT, 0, "Damaged buffer\n");
301 block->bufp += remlen;
302 block->binbuf += remlen;
303 rec->remainder -= remlen;
304 return 0; /* did partial transfer */
307 rec->remainder = 0; /* did whole transfer */
308 sm_check(__FILE__, __LINE__, False);
314 * Test if we can write whole record to the block
316 * Returns: 0 on failure
317 * 1 on success (all bytes can be written)
319 int can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
323 remlen = block->buf_len - block->binbuf;
324 if (rec->remainder == 0) {
325 if (remlen >= WRITE_RECHDR_LENGTH) {
326 remlen -= WRITE_RECHDR_LENGTH;
327 rec->remainder = rec->data_len;
334 if (rec->remainder > 0 && remlen < rec->remainder) {
342 * Read a Record from the block
343 * Returns: 0 if nothing read or if the continuation record does not match.
344 * In both of these cases, a block read must be done.
345 * 1 if at least the record header was read, this
346 * routine may have to be called again with a new
347 * block if the entire record was not read.
349 int read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
353 uint32_t VolSessionId;
354 uint32_t VolSessionTime;
360 remlen = block->binbuf;
361 rec->Block = block->BlockNumber;
362 rec->File = ((DEVICE *)block->dev)->file;
364 /* Clear state flags */
366 if (((DEVICE *)block->dev)->state & ST_TAPE) {
367 rec->state |= REC_ISTAPE;
372 * Get the header. There is always a full header,
373 * otherwise we find it in the next block.
375 Dmsg3(100, "Block=%d Ver=%d size=%u\n", block->BlockNumber, block->BlockVer,
377 if (block->BlockVer == 1) {
378 rhl = RECHDR1_LENGTH;
380 rhl = RECHDR2_LENGTH;
383 Dmsg4(90, "Enter read_record_block: remlen=%d data_len=%d rem=%d blkver=%d\n",
384 remlen, rec->data_len, rec->remainder, block->BlockVer);
386 unser_begin(block->bufp, WRITE_RECHDR_LENGTH);
387 if (block->BlockVer == 1) {
388 unser_uint32(VolSessionId);
389 unser_uint32(VolSessionTime);
391 VolSessionId = block->VolSessionId;
392 VolSessionTime = block->VolSessionTime;
394 unser_int32(FileIndex);
396 unser_uint32(data_bytes);
399 block->binbuf -= rhl;
402 /* If we are looking for more (remainder!=0), we reject anything
403 * where the VolSessionId and VolSessionTime don't agree
405 if (rec->remainder && (rec->VolSessionId != VolSessionId ||
406 rec->VolSessionTime != VolSessionTime)) {
407 rec->state |= REC_NO_MATCH;
408 return 0; /* This is from some other Session */
411 /* if Stream is negative, it means that this is a continuation
412 * of a previous partially written record.
414 if (Stream < 0) { /* continuation record? */
415 Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n",
417 rec->state |= REC_CONTINUATION;
418 if (!rec->remainder) { /* if we didn't read previously */
419 rec->data_len = 0; /* return data as if no continuation */
420 } else if (rec->Stream != -Stream) {
421 rec->state |= REC_NO_MATCH;
422 return 0; /* This is from some other Session */
424 rec->Stream = -Stream; /* set correct Stream */
425 } else { /* Regular record */
426 rec->Stream = Stream;
427 rec->data_len = 0; /* transfer to beginning of data */
429 rec->VolSessionId = VolSessionId;
430 rec->VolSessionTime = VolSessionTime;
431 rec->FileIndex = FileIndex;
433 Dmsg6(100, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%u\n\
434 remlen=%d data_len=%d\n",
435 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
436 stream_to_ascii(rec->Stream, rec->FileIndex), data_bytes, remlen,
440 * No more records in this block because the number
441 * of remaining bytes are less than a record header
442 * length, so return empty handed, but indicate that
443 * he must read again. By returning, we allow the
444 * higher level routine to fetch the next block and
447 Dmsg0(90, "read_record_block: nothing\n");
449 if (!rec->remainder) {
450 rec->remainder = 1; /* set to expect continuation */
451 rec->data_len = 0; /* no data transferred */
454 rec->state |= (REC_NO_HEADER | REC_BLOCK_EMPTY);
458 ASSERT(data_bytes < MAX_BLOCK_LENGTH); /* temp sanity check */
460 rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
463 * At this point, we have read the header, now we
464 * must transfer as much of the data record as
465 * possible taking into account: 1. A partial
466 * data record may have previously been transferred,
467 * 2. The current block may not contain the whole data
470 if (remlen >= data_bytes) {
471 /* Got whole record */
472 memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
473 block->bufp += data_bytes;
474 block->binbuf -= data_bytes;
475 rec->data_len += data_bytes;
478 memcpy(rec->data+rec->data_len, block->bufp, remlen);
479 block->bufp += remlen;
480 block->binbuf -= remlen;
481 rec->data_len += remlen;
482 rec->remainder = 1; /* partial record transferred */
483 Dmsg1(90, "read_record_block: partial xfered=%d\n", rec->data_len);
484 rec->state |= (REC_PARTIAL_RECORD | REC_BLOCK_EMPTY);
488 Dmsg4(90, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
489 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
490 stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
491 return 1; /* transferred full record */