3 * record.c -- tape record handling functions
5 * Kern Sibbald, April MMI
11 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 extern int debug_level;
37 * Convert a FileIndex into a printable
38 * ASCII string. Not reentrant.
39 * If the FileIndex is negative, it flags the
40 * record as a Label, otherwise it is simply
41 * the FileIndex of the current file.
43 char *FI_to_ascii(int fi)
47 sprintf(buf, "%d", fi);
62 sprintf(buf, "unknown: %d", fi);
69 * Convert a Stream ID into a printable
70 * ASCII string. Not reentrant.
72 char *stream_to_ascii(int stream)
76 case STREAM_UNIX_ATTRIBUTES:
78 case STREAM_FILE_DATA:
80 case STREAM_MD5_SIGNATURE:
82 case STREAM_GZIP_DATA:
85 sprintf(buf, "%d", stream);
91 * Return a new record entity
93 DEV_RECORD *new_record(void)
97 rec = (DEV_RECORD *) get_memory(sizeof(DEV_RECORD));
98 memset(rec, 0, sizeof(DEV_RECORD));
99 rec->data = get_pool_memory(PM_MESSAGE);
104 * Free the record entity
107 void free_record(DEV_RECORD *rec)
109 Dmsg0(150, "Enter free_record.\n");
111 free_pool_memory(rec->data);
113 Dmsg0(150, "Data buf is freed.\n");
114 free_pool_memory((POOLMEM *)rec);
115 Dmsg0(150, "Leave free_record.\n");
120 * Write a Record to the block
122 * Returns: 0 on failure (none or partially written)
123 * 1 on success (all bytes written)
125 * and remainder returned in packet.
127 * We require enough room for the header, and we deal with
128 * two special cases. 1. Only part of the record may have
129 * been transferred the last time (when remainder is
130 * non-zero), and 2. The remaining bytes to write may not
131 * all fit into the block.
133 int write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
138 sm_check(__FILE__, __LINE__, False);
139 remlen = block->buf_len - block->binbuf;
141 ASSERT(block->binbuf == (uint32_t) (block->bufp - block->buf));
144 Dmsg6(190, "write_record_to_block() FI=%s SessId=%d Strm=%s len=%d\n\
145 rem=%d remainder=%d\n",
146 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
147 stream_to_ascii(rec->Stream), rec->data_len,
148 remlen, rec->remainder);
151 * If rec->remainder is non-zero, we have been called a
152 * second (or subsequent) time to finish writing a record
153 * that did not previously fit into the block.
155 if (rec->remainder == 0) {
156 /* Require enough room to write a full header */
157 if (remlen >= RECHDR_LENGTH) {
158 ser_begin(block->bufp, RECHDR_LENGTH);
159 ser_uint32(rec->VolSessionId);
160 ser_uint32(rec->VolSessionTime);
161 ser_int32(rec->FileIndex);
162 ser_int32(rec->Stream);
163 ser_uint32(rec->data_len);
164 ASSERT(ser_length(block->bufp) == RECHDR_LENGTH);
166 block->bufp += RECHDR_LENGTH;
167 block->binbuf += RECHDR_LENGTH;
168 remlen -= RECHDR_LENGTH;
169 rec->remainder = rec->data_len;
171 rec->remainder = rec->data_len + RECHDR_LENGTH;
172 sm_check(__FILE__, __LINE__, False);
177 * We are here to write unwritten bytes from a previous
178 * time. Presumably we have a new buffer (possibly
179 * containing a volume label), so the new header
180 * should be able to fit in the block -- otherwise we have
181 * an error. Note, we may have to continue splitting the
182 * data record though.
184 * First, write the header, then write as much as
185 * possible of the data record.
187 * Every time we write a header and it is a continuation
188 * of a previous partially written record, we store the
189 * Stream as -Stream in the record header.
191 ser_begin(block->bufp, RECHDR_LENGTH);
192 ser_uint32(rec->VolSessionId);
193 ser_uint32(rec->VolSessionTime);
194 ser_int32(rec->FileIndex);
195 if (rec->remainder > rec->data_len) {
196 ser_int32(rec->Stream); /* normal full header */
197 ser_uint32(rec->data_len);
198 rec->remainder = rec->data_len; /* must still do data record */
200 ser_int32(-rec->Stream); /* mark this as a continuation record */
201 ser_uint32(rec->remainder); /* bytes to do */
203 ASSERT(ser_length(block->bufp) == RECHDR_LENGTH);
205 /* Require enough room to write a full header */
206 ASSERT(remlen >= RECHDR_LENGTH);
208 block->bufp += RECHDR_LENGTH;
209 block->binbuf += RECHDR_LENGTH;
210 remlen -= RECHDR_LENGTH;
213 sm_check(__FILE__, __LINE__, False);
214 return 0; /* partial transfer */
218 * Now deal with data record.
219 * Part of it may have already been transferred, and we
220 * may not have enough room to transfer the whole this time.
222 if (rec->remainder > 0) {
223 /* Write as much of data as possible */
224 if (remlen >= rec->remainder) {
225 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
227 block->bufp += rec->remainder;
228 block->binbuf += rec->remainder;
230 memcpy(block->bufp, rec->data+rec->data_len-rec->remainder,
232 if (!sm_check_rtn(__FILE__, __LINE__, False)) {
233 /* We damaged a buffer */
234 Dmsg6(0, "Damaged block FI=%s SessId=%d Strm=%s len=%d\n\
235 rem=%d remainder=%d\n",
236 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
237 stream_to_ascii(rec->Stream), rec->data_len,
238 remlen, rec->remainder);
239 Dmsg5(0, "Damaged block: bufp=%x binbuf=%d buf_len=%d rem=%d moved=%d\n",
240 block->bufp, block->binbuf, block->buf_len, block->buf_len-block->binbuf,
242 Dmsg2(0, "Damaged block: buf=%x binbuffrombuf=%d \n",
243 block->buf, block->bufp-block->buf);
245 Emsg0(M_ABORT, 0, "Damaged buffer\n");
248 block->bufp += remlen;
249 block->binbuf += remlen;
250 rec->remainder -= remlen;
251 return 0; /* did partial transfer */
254 rec->remainder = 0; /* did whole transfer */
255 sm_check(__FILE__, __LINE__, False);
261 * Test if we can write whole record to the block
263 * Returns: 0 on failure
264 * 1 on success (all bytes can be written)
266 int can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
270 remlen = block->buf_len - block->binbuf;
271 if (rec->remainder == 0) {
272 if (remlen >= RECHDR_LENGTH) {
273 remlen -= RECHDR_LENGTH;
274 rec->remainder = rec->data_len;
281 if (rec->remainder > 0 && remlen < rec->remainder) {
289 * Read a Record from the block
290 * Returns: 0 if nothing read or if the continuation record does not match.
291 * In both of these cases, a block read must be done.
292 * 1 if at least the record header was read, this
293 * routine may have to be called again with a new
294 * block if the entire record was not read.
296 int read_record_from_block(DEV_BLOCK *block, DEV_RECORD *rec)
300 uint32_t VolSessionId;
301 uint32_t VolSessionTime;
306 remlen = block->binbuf;
308 /* Clear state flags */
312 * Get the header. There is always a full header,
313 * otherwise we find it in the next block.
315 if (remlen >= RECHDR_LENGTH) {
316 Dmsg3(90, "read_record_block: remlen=%d data_len=%d rem=%d\n",
317 remlen, rec->data_len, rec->remainder);
319 unser_begin(block->bufp, RECHDR_LENGTH);
320 unser_uint32(VolSessionId);
321 unser_uint32(VolSessionTime);
322 unser_int32(FileIndex);
324 unser_uint32(data_bytes);
326 ASSERT(unser_length(block->bufp) == RECHDR_LENGTH);
327 block->bufp += RECHDR_LENGTH;
328 block->binbuf -= RECHDR_LENGTH;
329 remlen -= RECHDR_LENGTH;
332 * if Stream is negative, it means that this is a continuation
333 * of a previous partially written record.
335 if (Stream < 0) { /* continuation record? */
336 Dmsg1(500, "Got negative Stream => continuation. remainder=%d\n",
338 rec->state |= REC_CONTINUATION;
339 if (!rec->remainder) { /* if we didn't read previously */
340 rec->data_len = 0; /* return data as if no continuation */
341 } else if (rec->VolSessionId != VolSessionId ||
342 rec->VolSessionTime != VolSessionTime ||
343 rec->Stream != -Stream) {
344 rec->state |= REC_NO_MATCH;
345 return 0; /* This is from some other Session */
347 rec->Stream = -Stream; /* set correct Stream */
348 } else { /* Regular record */
349 rec->Stream = Stream;
350 rec->data_len = 0; /* transfer to beginning of data */
352 rec->VolSessionId = VolSessionId;
353 rec->VolSessionTime = VolSessionTime;
354 rec->FileIndex = FileIndex;
356 Dmsg6(90, "rd_rec_blk() got FI=%s SessId=%d Strm=%s len=%d\n\
357 remlen=%d data_len=%d\n",
358 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
359 stream_to_ascii(rec->Stream), data_bytes, remlen, rec->data_len);
362 * No more records in this block because the number
363 * of remaining bytes are less than a record header
364 * length, so return empty handed, but indicate that
365 * he must read again. By returning, we allow the
366 * higher level routine to fetch the next block and
369 Dmsg0(90, "read_record_block: nothing\n");
370 if (!rec->remainder) {
371 rec->remainder = 1; /* set to expect continuation */
372 rec->data_len = 0; /* no data transferred */
374 rec->state |= (REC_NO_HEADER | REC_BLOCK_EMPTY);
378 ASSERT(data_bytes < MAX_BLOCK_LENGTH); /* temp sanity check */
380 rec->data = check_pool_memory_size(rec->data, rec->data_len+data_bytes);
383 * At this point, we have read the header, now we
384 * must transfer as much of the data record as
385 * possible taking into account: 1. A partial
386 * data record may have previously been transferred,
387 * 2. The current block may not contain the whole data
390 if (remlen >= data_bytes) {
391 /* Got whole record */
392 memcpy(rec->data+rec->data_len, block->bufp, data_bytes);
393 block->bufp += data_bytes;
394 block->binbuf -= data_bytes;
395 rec->data_len += data_bytes;
398 memcpy(rec->data+rec->data_len, block->bufp, remlen);
399 block->bufp += remlen;
400 block->binbuf -= remlen;
401 rec->data_len += remlen;
402 rec->remainder = 1; /* partial record transferred */
403 Dmsg1(90, "read_record_block: partial xfered=%d\n", rec->data_len);
404 rec->state |= (REC_PARTIAL_RECORD | REC_BLOCK_EMPTY);
408 Dmsg4(90, "Rtn full rd_rec_blk FI=%s SessId=%d Strm=%s len=%d\n",
409 FI_to_ascii(rec->FileIndex), rec->VolSessionId,
410 stream_to_ascii(rec->Stream), rec->data_len);
411 return 1; /* transferred full record */