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