xref: /original-bsd/bin/pax/buf_subs.c (revision b8aab004)
1 /*-
2  * Copyright (c) 1992 Keith Muller.
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Keith Muller of the University of California, San Diego.
8  *
9  * %sccs.include.redist.c%
10  */
11 
12 #ifndef lint
13 static char sccsid[] = "@(#)buf_subs.c	8.1 (Berkeley) 05/31/93";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/stat.h>
19 #include <sys/param.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "pax.h"
27 #include "extern.h"
28 
29 /*
30  * routines which implement archive and file buffering
31  */
32 
33 #define MAXFBSZ		4096		/* max block size for hole detection */
34 #define MINFBSZ		512		/* min block size for hole detection */
35 #define MAXFLT          10              /* default media read error limit */
36 
37 static char bufmem[MAXBLK+BLKMULT];	/* i/o buffer + pushback id space */
38 static char *buf;			/* normal start of i/o buffer */
39 static char *bufend;			/* end or last char in i/o buffer */
40 static char *bufpt;			/* read/write point in i/o buffer */
41 int blksz = MAXBLK;                    	/* block input/output size in bytes */
42 int wrblksz;                      	/* user spec output size in bytes */
43 int maxflt = MAXFLT;			/* MAX consecutive media errors */
44 int rdblksz;				/* first read blksize (tapes only) */
45 off_t wrlimit;				/* # of bytes written per archive vol */
46 off_t wrcnt;				/* # of bytes written on current vol */
47 off_t rdcnt;				/* # of bytes read on current vol */
48 
49 /*
50  * wr_start()
51  *	set up the buffering system to operate in a write mode
52  * Return:
53  *	0 if ok, -1 if the user specified write block size violates pax spec
54  */
55 
56 #if __STDC__
57 int
58 wr_start(void)
59 #else
60 int
61 wr_start()
62 #endif
63 {
64 	buf = &(bufmem[BLKMULT]);
65 	/*
66 	 * Check to make sure the write block size meets pax specs. If the user
67 	 * does not specify a blocksize, we use the format default blocksize.
68 	 * We must be picky on writes, so we do not allow the user to create an
69 	 * archive that might be hard to read elsewhere. If all ok, we then
70 	 * open the first archive volume
71 	 */
72 	if (!wrblksz)
73 		wrblksz = frmt->bsz;
74 	if (wrblksz > MAXBLK) {
75 		warn(1, "Write block size of %d too large, maximium is: %d",
76 			wrblksz, MAXBLK);
77 		return(-1);
78 	}
79 	if (wrblksz % BLKMULT) {
80 		warn(1, "Write block size of %d is not a %d byte multiple",
81 		    wrblksz, BLKMULT);
82 		return(-1);
83 	}
84 
85 	/*
86 	 * we only allow wrblksz to be used with all archive operations
87 	 */
88 	blksz = rdblksz = wrblksz;
89 	if ((ar_open(arcname) < 0) && (ar_next() < 0))
90 		return(-1);
91 	wrcnt = 0;
92 	bufend = buf + wrblksz;
93 	bufpt = buf;
94 	return(0);
95 }
96 
97 /*
98  * rd_start()
99  *	set up buffering system to read an archive
100  * Return:
101  *	0 if ok, -1 otherwise
102  */
103 
104 #if __STDC__
105 int
106 rd_start(void)
107 #else
108 int
109 rd_start()
110 #endif
111 {
112 	/*
113 	 * leave space for the header pushback (see get_arc()). If we are
114 	 * going to append and user specified a write block size, check it
115 	 * right away
116 	 */
117 	buf = &(bufmem[BLKMULT]);
118 	if ((act == APPND) && wrblksz) {
119 		if (wrblksz > MAXBLK) {
120 			warn(1,"Write block size %d too large, maximium is: %d",
121 				wrblksz, MAXBLK);
122 			return(-1);
123 		}
124 		if (wrblksz % BLKMULT) {
125 			warn(1, "Write block size %d is not a %d byte multiple",
126 		    	wrblksz, BLKMULT);
127 			return(-1);
128 		}
129 	}
130 
131 	/*
132 	 * open the archive
133 	 */
134 	if ((ar_open(arcname) < 0) && (ar_next() < 0))
135 		return(-1);
136 	bufend = buf + rdblksz;
137 	bufpt = bufend;
138 	rdcnt = 0;
139 	return(0);
140 }
141 
142 /*
143  * cp_start()
144  *	set up buffer system for copying within the file system
145  */
146 
147 #if __STDC__
148 void
149 cp_start(void)
150 #else
151 void
152 cp_start()
153 #endif
154 {
155 	buf = &(bufmem[BLKMULT]);
156 	rdblksz = blksz = MAXBLK;
157 }
158 
159 /*
160  * appnd_start()
161  *	Set up the buffering system to append new members to an archive that
162  *	was just read. The last block(s) of an archive may contain a format
163  *	specific trailer. To append a new member, this trailer has to be
164  *	removed from the archive. The first byte of the trailer is replaced by
165  *	the start of the header of the first file added to the archive. The
166  *	format specific end read function tells us how many bytes to move
167  *	backwards in the archive to be positioned BEFORE the trailer. Two
168  *	different postions have to be adjusted, the O.S. file offset (e.g. the
169  *	position of the tape head) and the write point within the data we have
170  *	stored in the read (soon to become write) buffer. We may have to move
171  *	back several records (the number depends on the size of the archive
172  *	record and the size of the format trailer) to read up the record where
173  *	the first byte of the trailer is recorded. Trailers may span (and
174  *	overlap) record boundries.
175  *	We first calculate which record has the first byte of the trailer. We
176  *	move the OS file offset back to the start of this record and read it
177  *	up. We set the buffer write pointer to be at this byte (the byte where
178  *	the trailer starts). We then move the OS file pointer back to the
179  *	start of this record so a flush of this buffer will replace the record
180  *	in the archive.
181  *	A major problem is rewriting this last record. For archives stored
182  *	on disk files, this is trival. However, many devices are really picky
183  *	about the conditions under which they will allow a write to occur.
184  *	Often devices restrict the conditions where writes can be made writes,
185  *	so it may not be feasable to append archives stored on all types of
186  *	devices.
187  * Return:
188  *	0 for success, -1 for failure
189  */
190 
191 #if __STDC__
192 int
193 appnd_start(off_t skcnt)
194 #else
195 int
196 appnd_start(skcnt)
197 	off_t skcnt;
198 #endif
199 {
200 	register int res;
201 	off_t cnt;
202 
203 	if (exit_val != 0) {
204 		warn(0, "Cannot append to an archive that may have flaws.");
205 		return(-1);
206 	}
207 	/*
208 	 * if the user did not specify a write blocksize, inherit the size used
209 	 * in the last archive volume read. (If a is set we still use rdblksz
210 	 * until next volume, cannot shift sizes within a single volume).
211 	 */
212 	if (!wrblksz)
213 		wrblksz = blksz = rdblksz;
214 	else
215 		blksz = rdblksz;
216 
217 	/*
218 	 * make sure that this volume allows appends
219 	 */
220 	if (ar_app_ok() < 0)
221 		return(-1);
222 
223 	/*
224 	 * Calculate bytes to move back and move in front of record where we
225 	 * need to start writing from. Remember we have to add in any padding
226 	 * that might be in the buffer after the trailer in the last block. We
227 	 * travel skcnt + padding ROUNDED UP to blksize.
228 	 */
229 	skcnt += bufend - bufpt;
230 	if ((cnt = (skcnt/blksz) * blksz) < skcnt)
231 		cnt += blksz;
232 	if (ar_rev((off_t)cnt) < 0)
233 		goto out;
234 
235 	/*
236 	 * We may have gone too far if there is valid data in the block we are
237 	 * now in front of, read up the block and position the pointer after
238 	 * the valid data.
239 	 */
240 	if ((cnt -= skcnt) > 0) {
241 		/*
242 		 * watch out for stupid tape drives. ar_rev() will set rdblksz
243 		 * to be real physical blocksize so we must loop until we get
244 		 * the old rdblksz (now in blksz). If ar_rev() fouls up the
245 		 * determination of the physical block size, we will fail.
246 		 */
247 		bufpt = buf;
248 		bufend = buf + blksz;
249 		while (bufpt < bufend) {
250 			if ((res = ar_read(bufpt, rdblksz)) <= 0)
251 				goto out;
252 			bufpt += res;
253 		}
254 		if (ar_rev((off_t)(bufpt - buf)) < 0)
255 			goto out;
256 		bufpt = buf + cnt;
257 		bufend = buf + blksz;
258 	} else {
259 		/*
260 		 * buffer is empty
261 		 */
262 		bufend = buf + blksz;
263 		bufpt = buf;
264 	}
265 	rdblksz = blksz;
266 	rdcnt -= skcnt;
267 	wrcnt = 0;
268 
269 	/*
270 	 * At this point we are ready to write. If the device requires special
271 	 * handling to write at a point were previously recorded data resides,
272 	 * that is handled in ar_set_wr(). From now on we operate under normal
273 	 * ARCHIVE mode (write) conditions
274 	 */
275 	if (ar_set_wr() < 0)
276 		return(-1);
277 	act = ARCHIVE;
278 	return(0);
279 
280     out:
281 	warn(1, "Unable to rewrite archive trailer, cannot append.");
282 	return(-1);
283 }
284 
285 /*
286  * rd_sync()
287  *	A read error occurred on this archive volume. Resync the buffer and
288  *	try to reset the device (if possible) so we can continue to read. Keep
289  *	trying to do this until we get a valid read, or we reach the limit on
290  *	consecutive read faults (at which point we give up). The user can
291  *	adjust the read error limit through a command line option.
292  * Returns:
293  *	0 on success, and -1 on failure
294  */
295 
296 #if __STDC__
297 int
298 rd_sync(void)
299 #else
300 int
301 rd_sync()
302 #endif
303 {
304 	register int errcnt = 0;
305 	register int res;
306 
307 	/*
308 	 * if the user says bail out on first fault, we are out of here...
309 	 */
310 	if (maxflt == 0)
311 		return(-1);
312 	if (act == APPND) {
313 		warn(1, "Unable to append when there are archive read errors.");
314 		return(-1);
315 	}
316 
317 	/*
318 	 * poke at device and try to get past media error
319 	 */
320 	if (ar_rdsync() < 0) {
321 		if (ar_next() < 0)
322 			return(-1);
323 		else
324 			rdcnt = 0;
325 	}
326 
327 	for (;;) {
328 		if ((res = ar_read(buf, blksz)) > 0) {
329 			/*
330 			 * All right! got some data, fill that buffer
331 			 */
332 			bufpt = buf;
333 			bufend = buf + res;
334 			rdcnt += res;
335 			return(0);
336 		}
337 
338 		/*
339 		 * Oh well, yet another failed read...
340 		 * if error limit reached, ditch. o.w. poke device to move past
341 		 * bad media and try again. if media is badly damaged, we ask
342 		 * the poor (and upset user at this point) for the next archive
343 		 * volume. remember the goal on reads is to get the most we
344 		 * can extract out of the archive.
345 		 */
346 		if ((maxflt > 0) && (++errcnt > maxflt))
347 			warn(0,"Archive read error limit (%d) reached",maxflt);
348 		else if (ar_rdsync() == 0)
349 			continue;
350 		if (ar_next() < 0)
351 			break;
352 		rdcnt = 0;
353 		errcnt = 0;
354 	}
355 	return(-1);
356 }
357 
358 /*
359  * pback()
360  *	push the data used during the archive id phase back into the I/O
361  *	buffer. This is required as we cannot be sure that the header does NOT
362  *	overlap a block boundry (as in the case we are trying to recover a
363  *	flawed archived). This was not designed to be used for any other
364  *	purpose. (What software engineering, HA!)
365  *	WARNING: do not even THINK of pback greater than BLKMULT, unless the
366  *	pback space is increased.
367  */
368 
369 #if __STDC__
370 void
371 pback(char *pt, int cnt)
372 #else
373 void
374 pback(pt, cnt)
375 	char *pt;
376 	int cnt;
377 #endif
378 {
379 	bufpt -= cnt;
380 	bcopy(pt, bufpt, cnt);
381 	return;
382 }
383 
384 /*
385  * rd_skip()
386  *	skip foward in the archive during a archive read. Used to get quickly
387  *	past file data and padding for files the user did NOT select.
388  * Return:
389  *	0 if ok, -1 failure, and 1 when EOF on the archive volume was detected.
390  */
391 
392 #if __STDC__
393 int
394 rd_skip(off_t skcnt)
395 #else
396 int
397 rd_skip(skcnt)
398 	off_t skcnt;
399 #endif
400 {
401 	off_t res;
402 	off_t cnt;
403 	off_t skipped = 0;
404 
405 	/*
406 	 * consume what data we have in the buffer. If we have to move foward
407 	 * whole records, we call the low level skip function to see if we can
408 	 * move within the archive without doing the expensive reads on data we
409 	 * do not want.
410 	 */
411 	if (skcnt == 0)
412 		return(0);
413 	res = MIN((bufend - bufpt), skcnt);
414 	bufpt += res;
415 	skcnt -= res;
416 
417 	/*
418 	 * if skcnt is now 0, then no additional i/o is needed
419 	 */
420 	if (skcnt == 0)
421 		return(0);
422 
423 	/*
424 	 * We have to read more, calculate complete and partial record reads
425 	 * based on rdblksz. we skip over "cnt" complete records
426 	 */
427 	res = skcnt%rdblksz;
428 	cnt = (skcnt/rdblksz) * rdblksz;
429 
430 	/*
431 	 * if the skip fails, we will have to resync. ar_fow will tell us
432 	 * how much it can skip over. We will have to read the rest.
433 	 */
434 	if (ar_fow(cnt, &skipped) < 0)
435 		return(-1);
436 	res += cnt - skipped;
437 	rdcnt += skipped;
438 
439 	/*
440 	 * what is left we have to read (which may be the whole thing if
441 	 * ar_fow() told us the device can only read to skip records);
442 	 */
443 	while (res > 0L) {
444 		cnt = bufend - bufpt;
445 		/*
446 		 * if the read fails, we will have to resync
447 		 */
448 		if ((cnt <= 0) && ((cnt = buf_fill()) < 0))
449 			return(-1);
450 		if (cnt == 0)
451 			return(1);
452 		cnt = MIN(cnt, res);
453 		bufpt += cnt;
454 		res -= cnt;
455 	}
456 	return(0);
457 }
458 
459 /*
460  * wr_fin()
461  *	flush out any data (and pad if required) the last block. We always pad
462  *	with zero (even though we do not have to). Padding with 0 makes it a
463  *	lot easier to recover if the archive is damaged. zero paddding SHOULD
464  *	BE a requirement....
465  */
466 
467 #if __STDC__
468 void
469 wr_fin(void)
470 #else
471 void
472 wr_fin()
473 #endif
474 {
475 	if (bufpt > buf) {
476 		bzero(bufpt, bufend - bufpt);
477 		bufpt = bufend;
478 		(void)buf_flush(blksz);
479 	}
480 }
481 
482 /*
483  * wr_rdbuf()
484  *	fill the write buffer from data passed to it in a buffer (usually used
485  *	by format specific write routines to pass a file header). On failure we
486  *	punt. We do not allow the user to continue to write flawed archives.
487  *	We assume these headers are not very large (the memory copy we use is
488  *	a bit expensive).
489  * Return:
490  *	0 if buffer was filled ok, -1 o.w. (buffer flush failure)
491  */
492 
493 #if __STDC__
494 int
495 wr_rdbuf(register char *out, register int outcnt)
496 #else
497 int
498 wr_rdbuf(out, outcnt)
499 	register char *out;
500 	register int outcnt;
501 #endif
502 {
503 	register int cnt;
504 
505 	/*
506 	 * while there is data to copy copy into the write buffer. when the
507 	 * write buffer fills, flush it to the archive and continue
508 	 */
509 	while (outcnt > 0) {
510 		cnt = bufend - bufpt;
511 		if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))
512 			return(-1);
513 		/*
514 		 * only move what we have space for
515 		 */
516 		cnt = MIN(cnt, outcnt);
517 		bcopy(out, bufpt, cnt);
518 		bufpt += cnt;
519 		out += cnt;
520 		outcnt -= cnt;
521 	}
522 	return(0);
523 }
524 
525 /*
526  * rd_wrbuf()
527  *	copy from the read buffer into a supplied buffer a specified number of
528  *	bytes. If the read buffer is empty fill it and continue to copy.
529  *	usually used to obtain a file header for processing by a format
530  *	specific read routine.
531  * Return
532  *	number of bytes copied to the buffer, 0 indicates EOF on archive volume,
533  *	-1 is a read error
534  */
535 
536 #if __STDC__
537 int
538 rd_wrbuf(register char *in, register int cpcnt)
539 #else
540 int
541 rd_wrbuf(in, cpcnt)
542 	register char *in;
543 	register int cpcnt;
544 #endif
545 {
546 	register int res;
547 	register int cnt;
548 	register int incnt = cpcnt;
549 
550 	/*
551 	 * loop until we fill the buffer with the requested number of bytes
552 	 */
553 	while (incnt > 0) {
554 		cnt = bufend - bufpt;
555 		if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) {
556 			/*
557 			 * read error, return what we got (or the error if
558 			 * no data was copied). The caller must know that an
559 			 * error occured and has the best knowledge what to
560 			 * do with it
561 			 */
562 			if ((res = cpcnt - incnt) > 0)
563 				return(res);
564 			return(cnt);
565 		}
566 
567 		/*
568 		 * calculate how much data to copy based on whats left and
569 		 * state of buffer
570 		 */
571 		cnt = MIN(cnt, incnt);
572 		bcopy(bufpt, in, cnt);
573 		bufpt += cnt;
574 		incnt -= cnt;
575 		in += cnt;
576 	}
577 	return(cpcnt);
578 }
579 
580 /*
581  * wr_skip()
582  *	skip foward during a write. In other words add padding to the file.
583  *	we add zero filled padding as it makes flawed archives much easier to
584  *	recover from. the caller tells us how many bytes of padding to add
585  *	This routine was not designed to add HUGE amount of padding, just small
586  *	amounts (a few 512 byte blocks at most)
587  * Return:
588  *	0 if ok, -1 if there was a buf_flush failure
589  */
590 
591 #if __STDC__
592 int
593 wr_skip(off_t skcnt)
594 #else
595 int
596 wr_skip(skcnt)
597 	off_t skcnt;
598 #endif
599 {
600 	register int cnt;
601 
602 	/*
603 	 * loop while there is more padding to add
604 	 */
605 	while (skcnt > 0L) {
606 		cnt = bufend - bufpt;
607 		if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))
608 			return(-1);
609 		cnt = MIN(cnt, skcnt);
610 		bzero(bufpt, cnt);
611 		bufpt += cnt;
612 		skcnt -= cnt;
613 	}
614 	return(0);
615 }
616 
617 /*
618  * wr_rdfile()
619  *	fill write buffer with the contents of a file. We are passed an	open
620  *	file descriptor to the file an the archive structure that describes the
621  *	file we are storing. The variable "left" is modified to contain the
622  *	number of bytes of the file we were NOT able to write to the archive.
623  *	it is important that we always write EXACTLY the number of bytes that
624  *	the format specific write routine told us to. The file can also get
625  *	bigger, so reading to the end of file would create an improper archive,
626  *	we just detect this case and warn the user. We never create a bad
627  *	archive if we can avoid it. Of course trying to archive files that are
628  *	active is asking for trouble. It we fail, we pass back how much we
629  *	could NOT copy and let the caller deal with it.
630  * Return:
631  *	0 ok, -1 if archive write failure. a short read of the file returns a
632  *	0, but "left" is set to be greater than zero.
633  */
634 
635 #if __STDC__
636 int
637 wr_rdfile(ARCHD *arcn, int ifd, off_t *left)
638 #else
639 int
640 wr_rdfile(arcn, ifd, left)
641 	ARCHD *arcn;
642 	int ifd;
643 	off_t *left;
644 #endif
645 {
646 	register int cnt;
647 	register int res = 0;
648 	register off_t size = arcn->sb.st_size;
649 	struct stat sb;
650 
651 	/*
652 	 * while there are more bytes to write
653 	 */
654 	while (size > 0L) {
655 		cnt = bufend - bufpt;
656 		if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) {
657 			*left = size;
658 			return(-1);
659 		}
660 		cnt = MIN(cnt, size);
661 		if ((res = read(ifd, bufpt, cnt)) <= 0)
662 			break;
663 		size -= res;
664 		bufpt += res;
665 	}
666 
667 	/*
668 	 * better check the file did not change during this operation
669 	 * or the file read failed.
670 	 */
671 	if (res < 0)
672 		syswarn(1, errno, "Read fault on %s", arcn->org_name);
673 	else if (size != 0L)
674 		warn(1, "File changed size during read %s", arcn->org_name);
675 	else if (fstat(ifd, &sb) < 0)
676 		syswarn(1, errno, "Failed stat on %s", arcn->org_name);
677 	else if (arcn->sb.st_mtime != sb.st_mtime)
678 		warn(1, "File %s was modified during copy to archive",
679 			arcn->org_name);
680 	*left = size;
681 	return(0);
682 }
683 
684 /*
685  * rd_wrfile()
686  *	extract the contents of a file from the archive. If we are unable to
687  *	extract the entire file (due to failure to write the file) we return
688  *	the numbers of bytes we did NOT process. This way the caller knows how
689  *	many bytes to skip past to find the next archive header. If the failure
690  *	was due to an archive read, we will catch that when we try to skip. If
691  *	the format supplies a file data crc value, we calculate the actual crc
692  *	so that it can be compared to the value stored in the header
693  * NOTE:
694  *	We call a special function to write the file. This function attempts to
695  *	restore file holes (blocks of zeros) into the file. When files are
696  *	sparse this saves space, and is a LOT faster. For non sparse files
697  *	the performance hit is small. As of this writing, no archive supports
698  *	information on where the file holes are.
699  * Return:
700  *	0 ok, -1 if archive read failure. if we cannot write the entire file,
701  *	we return a 0 but "left" is set to be the amount unwritten
702  */
703 
704 #if __STDC__
705 int
706 rd_wrfile(ARCHD *arcn, int ofd, off_t *left)
707 #else
708 int
709 rd_wrfile(arcn, ofd, left)
710 	ARCHD *arcn;
711 	int ofd;
712 	off_t *left;
713 #endif
714 {
715 	register int cnt = 0;
716 	register off_t size = arcn->sb.st_size;
717 	register int res = 0;
718 	register char *fnm = arcn->name;
719 	int isem = 1;
720 	int rem;
721 	int sz = MINFBSZ;
722  	struct stat sb;
723 	u_long crc = 0L;
724 
725 	/*
726 	 * pass the blocksize of the file being written to the write routine,
727 	 * if an odd size, use the default MINFBSZ
728 	 */
729         if (fstat(ofd, &sb) == 0) {
730 		if ((sb.st_blksize > 0) && (sb.st_blksize <= MAXFBSZ))
731 			sz = (int)sb.st_blksize;
732         } else
733                 syswarn(0,errno,"Unable to obtain block size for file %s",fnm);
734 	rem = sz;
735 	*left = 0L;
736 
737 	/*
738 	 * Copy the archive to the file the number of bytes specified. We have
739 	 * to assume that we want to recover file holes as none of the archive
740 	 * formats can record the location of file holes.
741 	 */
742 	while (size > 0L) {
743 		cnt = bufend - bufpt;
744 		/*
745 		 * if we get a read error, we do not want to skip, as we may
746 		 * miss a header, so we do not set left, but if we get a write
747 		 * error, we do want to skip over the unprocessed data.
748 		 */
749 		if ((cnt <= 0) && ((cnt = buf_fill()) <= 0))
750 			break;
751 		cnt = MIN(cnt, size);
752 		if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) {
753 			*left = size;
754 			break;
755 		}
756 
757 		if (docrc) {
758 			/*
759 			 * update the actual crc value
760 			 */
761 			cnt = res;
762 			while (--cnt >= 0)
763 				crc += *bufpt++ & 0xff;
764 		} else
765 			bufpt += res;
766 		size -= res;
767 	}
768 
769 	/*
770 	 * if the last block has a file hole (all zero), we must make sure this
771 	 * gets updated in the file. We force the last block of zeros to be
772 	 * written. just closing with the file offset moved foward may not put
773 	 * a hole at the end of the file.
774 	 */
775 	if (isem && (arcn->sb.st_size > 0L))
776 		file_flush(ofd, fnm, isem);
777 
778 	/*
779 	 * if we failed from archive read, we do not want to skip
780 	 */
781 	if ((size > 0L) && (*left == 0L))
782 		return(-1);
783 
784 	/*
785 	 * some formats record a crc on file data. If so, then we compare the
786 	 * calculated crc to the crc stored in the archive
787 	 */
788 	if (docrc && (size == 0L) && (arcn->crc != crc))
789 		warn(1,"Actual crc does not match expected crc %s",arcn->name);
790 	return(0);
791 }
792 
793 /*
794  * cp_file()
795  *	copy the contents of one file to another. used during -rw phase of pax
796  *	just as in rd_wrfile() we use a special write function to write the
797  *	destination file so we can properly copy files with holes.
798  */
799 
800 #if __STDC__
801 void
802 cp_file(ARCHD *arcn, int fd1, int fd2)
803 #else
804 void
805 cp_file(arcn, fd1, fd2)
806 	ARCHD *arcn;
807 	int fd1;
808 	int fd2;
809 #endif
810 {
811 	register int cnt;
812 	register off_t cpcnt = 0L;
813 	register int res = 0;
814 	register char *fnm = arcn->name;
815 	register int no_hole = 0;
816 	int isem = 1;
817 	int rem;
818 	int sz = MINFBSZ;
819 	struct stat sb;
820 
821 	/*
822 	 * check for holes in the source file. If none, we will use regular
823 	 * write instead of file write.
824 	 */
825 	 if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size)
826 		++no_hole;
827 
828 	/*
829 	 * pass the blocksize of the file being written to the write routine,
830 	 * if an odd size, use the default MINFBSZ
831 	 */
832         if (fstat(fd2, &sb) == 0) {
833 		if ((sb.st_blksize > 0) && (sb.st_blksize <= MAXFBSZ))
834 			sz = sb.st_blksize;
835         } else
836                 syswarn(0,errno,"Unable to obtain block size for file %s",fnm);
837 	rem = sz;
838 
839 	/*
840 	 * read the source file and copy to destination file until EOF
841 	 */
842 	for(;;) {
843 		if ((cnt = read(fd1, buf, blksz)) <= 0)
844 			break;
845 		if (no_hole)
846 			res = write(fd2, buf, cnt);
847 		else
848 			res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm);
849 		if (res != cnt)
850 			break;
851 		cpcnt += cnt;
852 	}
853 
854 	/*
855 	 * check to make sure the copy is valid.
856 	 */
857 	if (res < 0)
858 		syswarn(1, errno, "Failed write during copy of %s to %s",
859 			arcn->org_name, arcn->name);
860 	else if (cpcnt != arcn->sb.st_size)
861 		warn(1, "File %s changed size during copy to %s",
862 			arcn->org_name, arcn->name);
863 	else if (fstat(fd1, &sb) < 0)
864 		syswarn(1, errno, "Failed stat of %s", arcn->org_name);
865 	else if (arcn->sb.st_mtime != sb.st_mtime)
866 		warn(1, "File %s was modified during copy to %s",
867 			arcn->org_name, arcn->name);
868 
869 	/*
870 	 * if the last block has a file hole (all zero), we must make sure this
871 	 * gets updated in the file. We force the last block of zeros to be
872 	 * written. just closing with the file offset moved foward may not put
873 	 * a hole at the end of the file.
874 	 */
875 	if (!no_hole && isem && (arcn->sb.st_size > 0L))
876 		file_flush(fd2, fnm, isem);
877 	return;
878 }
879 
880 /*
881  * buf_fill()
882  *	fill the read buffer with the next record (or what we can get) from
883  *	the archive volume.
884  * Return:
885  *	Number of bytes of data in the read buffer, -1 for read error, and
886  *	0 when finished (user specified termination in ar_next()).
887  */
888 
889 #if __STDC__
890 int
891 buf_fill(void)
892 #else
893 int
894 buf_fill()
895 #endif
896 {
897 	register int cnt;
898 	static int fini = 0;
899 
900 	if (fini)
901 		return(0);
902 
903 	for(;;) {
904 		/*
905 		 * try to fill the buffer. on error the next archive volume is
906 		 * opened and we try again.
907 		 */
908 		if ((cnt = ar_read(buf, blksz)) > 0) {
909 			bufpt = buf;
910 			bufend = buf + cnt;
911 			rdcnt += cnt;
912 			return(cnt);
913 		}
914 
915 		/*
916 		 * errors require resync, EOF goes to next archive
917 		 */
918 		if (cnt < 0)
919 			break;
920 		if (ar_next() < 0) {
921 			fini = 1;
922 			return(0);
923 		}
924 		rdcnt = 0;
925 	}
926 	exit_val = 1;
927 	return(-1);
928 }
929 
930 /*
931  * buf_flush()
932  *	force the write buffer to the archive. We are passed the number of
933  *	bytes in the buffer at the point of the flush. When we change archives
934  *	the record size might change. (either larger or smaller).
935  * Return:
936  *	0 if all is ok, -1 when a write error occurs.
937  */
938 
939 #if __STDC__
940 int
941 buf_flush(register int bufcnt)
942 #else
943 int
944 buf_flush(bufcnt)
945 	register int bufcnt;
946 #endif
947 {
948 	register int cnt;
949 	register int push = 0;
950 	register int totcnt = 0;
951 
952 	/*
953 	 * if we have reached the user specified byte count for each archive
954 	 * volume, prompt for the next volume. (The non-standrad -R flag).
955 	 * NOTE: If the wrlimit is smaller than wrcnt, we will always write
956 	 * at least one record. We always round limit UP to next blocksize.
957 	 */
958 	if ((wrlimit > 0) && (wrcnt > wrlimit)) {
959 		warn(0, "User specified archive volume byte limit reached.");
960 		if (ar_next() < 0) {
961 			wrcnt = 0;
962 			exit_val = 1;
963 			return(-1);
964 		}
965 		wrcnt = 0;
966 
967 		/*
968 		 * The new archive volume might have changed the size of the
969 		 * write blocksize. if so we figure out if we need to write
970 		 * (one or more times), or if there is now free space left in
971 		 * the buffer (it is no longer full). bufcnt has the number of
972 		 * bytes in the buffer, (the blocksize, at the point we were
973 		 * CALLED). Push has the amount of "extra" data in the buffer
974 		 * if the block size has shrunk from a volume change.
975 		 */
976 		bufend = buf + blksz;
977 		if (blksz > bufcnt)
978 			return(0);
979 		if (blksz < bufcnt)
980 			push = bufcnt - blksz;
981 	}
982 
983 	/*
984 	 * We have enough data to write at least one archive block
985 	 */
986 	for (;;) {
987 		/*
988 		 * write a block and check if it all went out ok
989 		 */
990 		cnt = ar_write(buf, blksz);
991 		if (cnt == blksz) {
992 			/*
993 			 * the write went ok
994 			 */
995 			wrcnt += cnt;
996 			totcnt += cnt;
997 			if (push > 0) {
998 				/* we have extra data to push to the front.
999 				 * check for more than 1 block of push, and if
1000 				 * so we loop back to write again
1001 				 */
1002 				bcopy(bufend, buf, push);
1003 				bufpt = buf + push;
1004 				if (push >= blksz) {
1005 					push -= blksz;
1006 					continue;
1007 				}
1008 			} else
1009 				bufpt = buf;
1010 			return(totcnt);
1011 		} else if (cnt > 0) {
1012 			/*
1013 			 * Oh drat we got a partial write!
1014 			 * if format doesnt care about alignment let it go,
1015 			 * we warned the user in ar_write().... but this means
1016 			 * the last record on this volume violates pax spec....
1017 			 */
1018 			totcnt += cnt;
1019 			wrcnt += cnt;
1020 			bufpt = buf + cnt;
1021 			cnt = bufcnt - cnt;
1022 			bcopy(bufpt, buf, cnt);
1023 			bufpt = buf + cnt;
1024 			if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0))
1025 				return(totcnt);
1026 			break;
1027 		}
1028 
1029 		/*
1030 		 * All done, go to next archive
1031 		 */
1032 		wrcnt = 0;
1033 		if (ar_next() < 0)
1034 			break;
1035 
1036 		/*
1037 		 * The new archive volume might also have changed the block
1038 		 * size. if so, figure out if we have too much or too little
1039 		 * data for using the new block size
1040 		 */
1041 		bufend = buf + blksz;
1042 		if (blksz > bufcnt)
1043 			return(0);
1044 		if (blksz < bufcnt)
1045 			push = bufcnt - blksz;
1046 	}
1047 
1048 	/*
1049 	 * write failed, stop pax. we must not create a bad archive!
1050 	 */
1051 	exit_val = 1;
1052 	return(-1);
1053 }
1054