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