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