Home | History | Annotate | Line # | Download | only in pax
file_subs.c revision 1.5
      1  1.5  tls /*	$NetBSD: file_subs.c,v 1.5 1997/01/11 02:06:38 tls Exp $	*/
      2  1.4  cgd 
      3  1.1  jtc /*-
      4  1.1  jtc  * Copyright (c) 1992 Keith Muller.
      5  1.1  jtc  * Copyright (c) 1992, 1993
      6  1.1  jtc  *	The Regents of the University of California.  All rights reserved.
      7  1.1  jtc  *
      8  1.1  jtc  * This code is derived from software contributed to Berkeley by
      9  1.1  jtc  * Keith Muller of the University of California, San Diego.
     10  1.1  jtc  *
     11  1.1  jtc  * Redistribution and use in source and binary forms, with or without
     12  1.1  jtc  * modification, are permitted provided that the following conditions
     13  1.1  jtc  * are met:
     14  1.1  jtc  * 1. Redistributions of source code must retain the above copyright
     15  1.1  jtc  *    notice, this list of conditions and the following disclaimer.
     16  1.1  jtc  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  jtc  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  jtc  *    documentation and/or other materials provided with the distribution.
     19  1.1  jtc  * 3. All advertising materials mentioning features or use of this software
     20  1.1  jtc  *    must display the following acknowledgement:
     21  1.1  jtc  *	This product includes software developed by the University of
     22  1.1  jtc  *	California, Berkeley and its contributors.
     23  1.1  jtc  * 4. Neither the name of the University nor the names of its contributors
     24  1.1  jtc  *    may be used to endorse or promote products derived from this software
     25  1.1  jtc  *    without specific prior written permission.
     26  1.1  jtc  *
     27  1.1  jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1  jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1  jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1  jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1  jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1  jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1  jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1  jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1  jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1  jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1  jtc  * SUCH DAMAGE.
     38  1.1  jtc  */
     39  1.1  jtc 
     40  1.1  jtc #ifndef lint
     41  1.4  cgd #if 0
     42  1.4  cgd static char sccsid[] = "@(#)file_subs.c	8.1 (Berkeley) 5/31/93";
     43  1.4  cgd #else
     44  1.5  tls static char rcsid[] = "$NetBSD: file_subs.c,v 1.5 1997/01/11 02:06:38 tls Exp $";
     45  1.4  cgd #endif
     46  1.1  jtc #endif /* not lint */
     47  1.1  jtc 
     48  1.1  jtc #include <sys/types.h>
     49  1.1  jtc #include <sys/time.h>
     50  1.1  jtc #include <sys/stat.h>
     51  1.1  jtc #include <unistd.h>
     52  1.1  jtc #include <sys/param.h>
     53  1.1  jtc #include <fcntl.h>
     54  1.1  jtc #include <string.h>
     55  1.1  jtc #include <stdio.h>
     56  1.1  jtc #include <ctype.h>
     57  1.1  jtc #include <errno.h>
     58  1.1  jtc #include <sys/uio.h>
     59  1.1  jtc #include <stdlib.h>
     60  1.1  jtc #include "pax.h"
     61  1.1  jtc #include "extern.h"
     62  1.1  jtc 
     63  1.1  jtc static int
     64  1.5  tls mk_link __P((char *,struct stat *,char *, int));
     65  1.1  jtc 
     66  1.1  jtc /*
     67  1.1  jtc  * routines that deal with file operations such as: creating, removing;
     68  1.1  jtc  * and setting access modes, uid/gid and times of files
     69  1.1  jtc  */
     70  1.1  jtc 
     71  1.1  jtc #define FILEBITS		(S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
     72  1.1  jtc #define SETBITS			(S_ISUID | S_ISGID)
     73  1.1  jtc #define ABITS			(FILEBITS | SETBITS)
     74  1.1  jtc 
     75  1.1  jtc /*
     76  1.1  jtc  * file_creat()
     77  1.1  jtc  *	Create and open a file.
     78  1.1  jtc  * Return:
     79  1.1  jtc  *	file descriptor or -1 for failure
     80  1.1  jtc  */
     81  1.1  jtc 
     82  1.1  jtc #if __STDC__
     83  1.1  jtc int
     84  1.5  tls file_creat(ARCHD *arcn)
     85  1.1  jtc #else
     86  1.1  jtc int
     87  1.1  jtc file_creat(arcn)
     88  1.5  tls 	ARCHD *arcn;
     89  1.1  jtc #endif
     90  1.1  jtc {
     91  1.1  jtc 	int fd = -1;
     92  1.1  jtc 	mode_t file_mode;
     93  1.1  jtc 	int oerrno;
     94  1.1  jtc 
     95  1.1  jtc 	/*
     96  1.1  jtc 	 * assume file doesn't exist, so just try to create it, most times this
     97  1.1  jtc 	 * works. We have to take special handling when the file does exist. To
     98  1.1  jtc 	 * detect this, we use O_EXCL. For example when trying to create a
     99  1.1  jtc 	 * file and a character device or fifo exists with the same name, we
    100  1.1  jtc 	 * can accidently open the device by mistake (or block waiting to open)
    101  1.1  jtc 	 * If we find that the open has failed, then figure spend the effore to
    102  1.1  jtc 	 * figure out why. This strategy was found to have better average
    103  1.1  jtc 	 * performance in common use than checking the file (and the path)
    104  1.1  jtc 	 * first with lstat.
    105  1.1  jtc 	 */
    106  1.1  jtc 	file_mode = arcn->sb.st_mode & FILEBITS;
    107  1.1  jtc 	if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
    108  1.1  jtc 	    file_mode)) >= 0)
    109  1.1  jtc 		return(fd);
    110  1.1  jtc 
    111  1.1  jtc 	/*
    112  1.1  jtc 	 * the file seems to exist. First we try to get rid of it (found to be
    113  1.1  jtc 	 * the second most common failure when traced). If this fails, only
    114  1.1  jtc 	 * then we go to the expense to check and create the path to the file
    115  1.1  jtc 	 */
    116  1.1  jtc 	if (unlnk_exist(arcn->name, arcn->type) != 0)
    117  1.1  jtc 		return(-1);
    118  1.1  jtc 
    119  1.1  jtc 	for (;;) {
    120  1.1  jtc 		/*
    121  1.1  jtc 		 * try to open it again, if this fails, check all the nodes in
    122  1.1  jtc 		 * the path and give it a final try. if chk_path() finds that
    123  1.1  jtc 		 * it cannot fix anything, we will skip the last attempt
    124  1.1  jtc 		 */
    125  1.1  jtc 		if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC,
    126  1.1  jtc 		    file_mode)) >= 0)
    127  1.1  jtc 			break;
    128  1.1  jtc 		oerrno = errno;
    129  1.1  jtc 		if (chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
    130  1.1  jtc 			syswarn(1, oerrno, "Unable to create %s", arcn->name);
    131  1.1  jtc 			return(-1);
    132  1.1  jtc 		}
    133  1.1  jtc 	}
    134  1.1  jtc 	return(fd);
    135  1.1  jtc }
    136  1.1  jtc 
    137  1.1  jtc /*
    138  1.1  jtc  * file_close()
    139  1.1  jtc  *	Close file descriptor to a file just created by pax. Sets modes,
    140  1.1  jtc  *	ownership and times as required.
    141  1.1  jtc  * Return:
    142  1.1  jtc  *	0 for success, -1 for failure
    143  1.1  jtc  */
    144  1.1  jtc 
    145  1.1  jtc #if __STDC__
    146  1.1  jtc void
    147  1.5  tls file_close(ARCHD *arcn, int fd)
    148  1.1  jtc #else
    149  1.1  jtc void
    150  1.1  jtc file_close(arcn, fd)
    151  1.5  tls 	ARCHD *arcn;
    152  1.1  jtc 	int fd;
    153  1.1  jtc #endif
    154  1.1  jtc {
    155  1.1  jtc 	int res = 0;
    156  1.1  jtc 
    157  1.1  jtc 	if (fd < 0)
    158  1.1  jtc 		return;
    159  1.1  jtc 	if (close(fd) < 0)
    160  1.1  jtc 		syswarn(0, errno, "Unable to close file descriptor on %s",
    161  1.1  jtc 		    arcn->name);
    162  1.1  jtc 
    163  1.1  jtc 	/*
    164  1.1  jtc 	 * set owner/groups first as this may strip off mode bits we want
    165  1.1  jtc 	 * then set file permission modes. Then set file access and
    166  1.1  jtc 	 * modification times.
    167  1.1  jtc 	 */
    168  1.1  jtc 	if (pids)
    169  1.1  jtc 		res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid);
    170  1.1  jtc 
    171  1.1  jtc 	/*
    172  1.1  jtc 	 * IMPORTANT SECURITY NOTE:
    173  1.1  jtc 	 * if not preserving mode or we cannot set uid/gid, then PROHIBIT
    174  1.1  jtc 	 * set uid/gid bits
    175  1.1  jtc 	 */
    176  1.1  jtc 	if (!pmode || res)
    177  1.1  jtc 		arcn->sb.st_mode &= ~(SETBITS);
    178  1.1  jtc 	if (pmode)
    179  1.1  jtc 		set_pmode(arcn->name, arcn->sb.st_mode);
    180  1.1  jtc 	if (patime || pmtime)
    181  1.1  jtc 		set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
    182  1.1  jtc }
    183  1.1  jtc 
    184  1.1  jtc /*
    185  1.1  jtc  * lnk_creat()
    186  1.1  jtc  *	Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
    187  1.1  jtc  *	must exist;
    188  1.1  jtc  * Return:
    189  1.1  jtc  *	0 if ok, -1 otherwise
    190  1.1  jtc  */
    191  1.1  jtc 
    192  1.1  jtc #if __STDC__
    193  1.1  jtc int
    194  1.5  tls lnk_creat(ARCHD *arcn)
    195  1.1  jtc #else
    196  1.1  jtc int
    197  1.1  jtc lnk_creat(arcn)
    198  1.5  tls 	ARCHD *arcn;
    199  1.1  jtc #endif
    200  1.1  jtc {
    201  1.1  jtc 	struct stat sb;
    202  1.1  jtc 
    203  1.1  jtc 	/*
    204  1.1  jtc 	 * we may be running as root, so we have to be sure that link target
    205  1.1  jtc 	 * is not a directory, so we lstat and check
    206  1.1  jtc 	 */
    207  1.1  jtc 	if (lstat(arcn->ln_name, &sb) < 0) {
    208  1.1  jtc 		syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name,
    209  1.1  jtc 		    arcn->name);
    210  1.1  jtc 		return(-1);
    211  1.1  jtc 	}
    212  1.1  jtc 
    213  1.1  jtc 	if (S_ISDIR(sb.st_mode)) {
    214  1.1  jtc 		warn(1, "A hard link to the directory %s is not allowed",
    215  1.1  jtc 		    arcn->ln_name);
    216  1.1  jtc 		return(-1);
    217  1.1  jtc 	}
    218  1.1  jtc 
    219  1.1  jtc 	return(mk_link(arcn->ln_name, &sb, arcn->name, 0));
    220  1.1  jtc }
    221  1.1  jtc 
    222  1.1  jtc /*
    223  1.1  jtc  * cross_lnk()
    224  1.1  jtc  *	Create a hard link to arcn->org_name from arcn->name. Only used in copy
    225  1.1  jtc  *	with the -l flag. No warning or error if this does not succeed (we will
    226  1.1  jtc  *	then just create the file)
    227  1.1  jtc  * Return:
    228  1.1  jtc  *	1 if copy() should try to create this file node
    229  1.1  jtc  *	0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
    230  1.1  jtc  */
    231  1.1  jtc 
    232  1.1  jtc #if __STDC__
    233  1.1  jtc int
    234  1.5  tls cross_lnk(ARCHD *arcn)
    235  1.1  jtc #else
    236  1.1  jtc int
    237  1.1  jtc cross_lnk(arcn)
    238  1.5  tls 	ARCHD *arcn;
    239  1.1  jtc #endif
    240  1.1  jtc {
    241  1.1  jtc 	/*
    242  1.1  jtc 	 * try to make a link to orginal file (-l flag in copy mode). make sure
    243  1.1  jtc 	 * we do not try to link to directories in case we are running as root
    244  1.1  jtc 	 * (and it might succeed).
    245  1.1  jtc 	 */
    246  1.1  jtc 	if (arcn->type == PAX_DIR)
    247  1.1  jtc 		return(1);
    248  1.1  jtc 	return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1));
    249  1.1  jtc }
    250  1.1  jtc 
    251  1.1  jtc /*
    252  1.1  jtc  * chk_same()
    253  1.1  jtc  *	In copy mode if we are not trying to make hard links between the src
    254  1.1  jtc  *	and destinations, make sure we are not going to overwrite ourselves by
    255  1.1  jtc  *	accident. This slows things down a little, but we have to protect all
    256  1.1  jtc  *	those people who make typing errors.
    257  1.1  jtc  * Return:
    258  1.1  jtc  *	1 the target does not exist, go ahead and copy
    259  1.1  jtc  *	0 skip it file exists (-k) or may be the same as source file
    260  1.1  jtc  */
    261  1.1  jtc 
    262  1.1  jtc #if __STDC__
    263  1.1  jtc int
    264  1.5  tls chk_same(ARCHD *arcn)
    265  1.1  jtc #else
    266  1.1  jtc int
    267  1.1  jtc chk_same(arcn)
    268  1.5  tls 	ARCHD *arcn;
    269  1.1  jtc #endif
    270  1.1  jtc {
    271  1.1  jtc 	struct stat sb;
    272  1.1  jtc 
    273  1.1  jtc 	/*
    274  1.1  jtc 	 * if file does not exist, return. if file exists and -k, skip it
    275  1.1  jtc 	 * quietly
    276  1.1  jtc 	 */
    277  1.1  jtc 	if (lstat(arcn->name, &sb) < 0)
    278  1.1  jtc 		return(1);
    279  1.1  jtc 	if (kflag)
    280  1.1  jtc 		return(0);
    281  1.1  jtc 
    282  1.1  jtc 	/*
    283  1.1  jtc 	 * better make sure the user does not have src == dest by mistake
    284  1.1  jtc 	 */
    285  1.1  jtc 	if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) {
    286  1.1  jtc 		warn(1, "Unable to copy %s, file would overwrite itself",
    287  1.1  jtc 		    arcn->name);
    288  1.1  jtc 		return(0);
    289  1.1  jtc 	}
    290  1.1  jtc 	return(1);
    291  1.1  jtc }
    292  1.1  jtc 
    293  1.1  jtc /*
    294  1.1  jtc  * mk_link()
    295  1.1  jtc  *	try to make a hard link between two files. if ign set, we do not
    296  1.1  jtc  *	complain.
    297  1.1  jtc  * Return:
    298  1.1  jtc  *	0 if successful (or we are done with this file but no error, such as
    299  1.1  jtc  *	finding the from file exists and the user has set -k).
    300  1.1  jtc  *	1 when ign was set to indicates we could not make the link but we
    301  1.1  jtc  *	should try to copy/extract the file as that might work (and is an
    302  1.1  jtc  *	allowed option). -1 an error occurred.
    303  1.1  jtc  */
    304  1.1  jtc 
    305  1.1  jtc #if __STDC__
    306  1.1  jtc static int
    307  1.5  tls mk_link(char *to, struct stat *to_sb, char *from,
    308  1.1  jtc 	int ign)
    309  1.1  jtc #else
    310  1.1  jtc static int
    311  1.1  jtc mk_link(to, to_sb, from, ign)
    312  1.5  tls 	char *to;
    313  1.5  tls 	struct stat *to_sb;
    314  1.5  tls 	char *from;
    315  1.1  jtc 	int ign;
    316  1.1  jtc #endif
    317  1.1  jtc {
    318  1.1  jtc 	struct stat sb;
    319  1.1  jtc 	int oerrno;
    320  1.1  jtc 
    321  1.1  jtc 	/*
    322  1.1  jtc 	 * if from file exists, it has to be unlinked to make the link. If the
    323  1.1  jtc 	 * file exists and -k is set, skip it quietly
    324  1.1  jtc 	 */
    325  1.1  jtc 	if (lstat(from, &sb) == 0) {
    326  1.1  jtc 		if (kflag)
    327  1.1  jtc 			return(0);
    328  1.1  jtc 
    329  1.1  jtc 		/*
    330  1.1  jtc 		 * make sure it is not the same file, protect the user
    331  1.1  jtc 		 */
    332  1.1  jtc 		if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) {
    333  1.1  jtc 			warn(1, "Unable to link file %s to itself", to);
    334  1.1  jtc 			return(-1);;
    335  1.1  jtc 		}
    336  1.1  jtc 
    337  1.1  jtc 		/*
    338  1.1  jtc 		 * try to get rid of the file, based on the type
    339  1.1  jtc 		 */
    340  1.1  jtc 		if (S_ISDIR(sb.st_mode)) {
    341  1.1  jtc 			if (rmdir(from) < 0) {
    342  1.1  jtc 				syswarn(1, errno, "Unable to remove %s", from);
    343  1.1  jtc 				return(-1);
    344  1.1  jtc 			}
    345  1.1  jtc 		} else if (unlink(from) < 0) {
    346  1.1  jtc 			if (!ign) {
    347  1.1  jtc 				syswarn(1, errno, "Unable to remove %s", from);
    348  1.1  jtc 				return(-1);
    349  1.1  jtc 			}
    350  1.1  jtc 			return(1);
    351  1.1  jtc 		}
    352  1.1  jtc 	}
    353  1.1  jtc 
    354  1.1  jtc 	/*
    355  1.1  jtc 	 * from file is gone (or did not exist), try to make the hard link.
    356  1.1  jtc 	 * if it fails, check the path and try it again (if chk_path() says to
    357  1.1  jtc 	 * try again)
    358  1.1  jtc 	 */
    359  1.1  jtc 	for (;;) {
    360  1.1  jtc 		if (link(to, from) == 0)
    361  1.1  jtc 			break;
    362  1.1  jtc 		oerrno = errno;
    363  1.1  jtc 		if (chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0)
    364  1.1  jtc 			continue;
    365  1.1  jtc 		if (!ign) {
    366  1.1  jtc 			syswarn(1, oerrno, "Could not link to %s from %s", to,
    367  1.1  jtc 			    from);
    368  1.1  jtc 			return(-1);
    369  1.1  jtc 		}
    370  1.1  jtc 		return(1);
    371  1.1  jtc 	}
    372  1.1  jtc 
    373  1.1  jtc 	/*
    374  1.1  jtc 	 * all right the link was made
    375  1.1  jtc 	 */
    376  1.1  jtc 	return(0);
    377  1.1  jtc }
    378  1.1  jtc 
    379  1.1  jtc /*
    380  1.1  jtc  * node_creat()
    381  1.1  jtc  *	create an entry in the file system (other than a file or hard link).
    382  1.1  jtc  *	If successful, sets uid/gid modes and times as required.
    383  1.1  jtc  * Return:
    384  1.1  jtc  *	0 if ok, -1 otherwise
    385  1.1  jtc  */
    386  1.1  jtc 
    387  1.1  jtc #if __STDC__
    388  1.1  jtc int
    389  1.5  tls node_creat(ARCHD *arcn)
    390  1.1  jtc #else
    391  1.1  jtc int
    392  1.1  jtc node_creat(arcn)
    393  1.5  tls 	ARCHD *arcn;
    394  1.1  jtc #endif
    395  1.1  jtc {
    396  1.5  tls 	int res;
    397  1.5  tls 	int ign = 0;
    398  1.5  tls 	int oerrno;
    399  1.5  tls 	int pass = 0;
    400  1.1  jtc 	mode_t file_mode;
    401  1.1  jtc 	struct stat sb;
    402  1.1  jtc 
    403  1.1  jtc 	/*
    404  1.1  jtc 	 * create node based on type, if that fails try to unlink the node and
    405  1.1  jtc 	 * try again. finally check the path and try again. As noted in the
    406  1.1  jtc 	 * file and link creation routines, this method seems to exhibit the
    407  1.1  jtc 	 * best performance in general use workloads.
    408  1.1  jtc 	 */
    409  1.1  jtc 	file_mode = arcn->sb.st_mode & FILEBITS;
    410  1.1  jtc 
    411  1.1  jtc 	for (;;) {
    412  1.1  jtc 		switch(arcn->type) {
    413  1.1  jtc 		case PAX_DIR:
    414  1.1  jtc 			res = mkdir(arcn->name, file_mode);
    415  1.1  jtc 			if (ign)
    416  1.1  jtc 				res = 0;
    417  1.1  jtc 			break;
    418  1.1  jtc 		case PAX_CHR:
    419  1.1  jtc 			file_mode |= S_IFCHR;
    420  1.1  jtc 			res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
    421  1.1  jtc 			break;
    422  1.1  jtc 		case PAX_BLK:
    423  1.1  jtc 			file_mode |= S_IFBLK;
    424  1.1  jtc 			res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
    425  1.1  jtc 			break;
    426  1.1  jtc 		case PAX_FIF:
    427  1.1  jtc 			res = mkfifo(arcn->name, file_mode);
    428  1.1  jtc 			break;
    429  1.1  jtc 		case PAX_SCK:
    430  1.1  jtc 			/*
    431  1.1  jtc 			 * Skip sockets, operation has no meaning under BSD
    432  1.1  jtc 			 */
    433  1.1  jtc 			warn(0,
    434  1.1  jtc 			    "%s skipped. Sockets cannot be copied or extracted",
    435  1.1  jtc 			    arcn->name);
    436  1.1  jtc 			return(-1);
    437  1.1  jtc 		case PAX_SLK:
    438  1.1  jtc 			if ((res = symlink(arcn->ln_name, arcn->name)) == 0)
    439  1.1  jtc 				return(0);
    440  1.1  jtc 			break;
    441  1.1  jtc 		case PAX_CTG:
    442  1.1  jtc 		case PAX_HLK:
    443  1.1  jtc 		case PAX_HRG:
    444  1.1  jtc 		case PAX_REG:
    445  1.1  jtc 		default:
    446  1.1  jtc 			/*
    447  1.1  jtc 			 * we should never get here
    448  1.1  jtc 			 */
    449  1.1  jtc 			warn(0, "%s has an unknown file type, skipping",
    450  1.1  jtc 				arcn->name);
    451  1.1  jtc 			return(-1);
    452  1.1  jtc 		}
    453  1.1  jtc 
    454  1.1  jtc 		/*
    455  1.1  jtc 		 * if we were able to create the node break out of the loop,
    456  1.1  jtc 		 * otherwise try to unlink the node and try again. if that
    457  1.1  jtc 		 * fails check the full path and try a final time.
    458  1.1  jtc 		 */
    459  1.1  jtc 		if (res == 0)
    460  1.1  jtc 			break;
    461  1.1  jtc 
    462  1.1  jtc 		/*
    463  1.1  jtc 		 * we failed to make the node
    464  1.1  jtc 		 */
    465  1.1  jtc 		oerrno = errno;
    466  1.1  jtc 		if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0)
    467  1.1  jtc 			return(-1);
    468  1.1  jtc 
    469  1.1  jtc 		if (++pass <= 1)
    470  1.1  jtc 			continue;
    471  1.1  jtc 
    472  1.1  jtc 		if (chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
    473  1.1  jtc 			syswarn(1, oerrno, "Could not create: %s", arcn->name);
    474  1.1  jtc 			return(-1);
    475  1.1  jtc 		}
    476  1.1  jtc 	}
    477  1.1  jtc 
    478  1.1  jtc 	/*
    479  1.1  jtc 	 * we were able to create the node. set uid/gid, modes and times
    480  1.1  jtc 	 */
    481  1.1  jtc 	if (pids)
    482  1.1  jtc 		res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid);
    483  1.1  jtc 	else
    484  1.1  jtc 		res = 0;
    485  1.1  jtc 
    486  1.1  jtc 	/*
    487  1.1  jtc 	 * IMPORTANT SECURITY NOTE:
    488  1.1  jtc 	 * if not preserving mode or we cannot set uid/gid, then PROHIBIT any
    489  1.1  jtc 	 * set uid/gid bits
    490  1.1  jtc 	 */
    491  1.1  jtc 	if (!pmode || res)
    492  1.1  jtc 		arcn->sb.st_mode &= ~(SETBITS);
    493  1.1  jtc 	if (pmode)
    494  1.1  jtc 		set_pmode(arcn->name, arcn->sb.st_mode);
    495  1.1  jtc 
    496  1.1  jtc 	if (arcn->type == PAX_DIR) {
    497  1.1  jtc 		/*
    498  1.1  jtc 		 * Dirs must be processed again at end of extract to set times
    499  1.1  jtc 		 * and modes to agree with those stored in the archive. However
    500  1.1  jtc 		 * to allow extract to continue, we may have to also set owner
    501  1.1  jtc 		 * rights. This allows nodes in the archive that are children
    502  1.1  jtc 		 * of this directory to be extracted without failure. Both time
    503  1.1  jtc 		 * and modes will be fixed after the entire archive is read and
    504  1.1  jtc 		 * before pax exits.
    505  1.1  jtc 		 */
    506  1.1  jtc 		if (access(arcn->name, R_OK | W_OK | X_OK) < 0) {
    507  1.1  jtc 			if (lstat(arcn->name, &sb) < 0) {
    508  1.1  jtc 				syswarn(0, errno,"Could not access %s (stat)",
    509  1.1  jtc 				    arcn->name);
    510  1.1  jtc 				set_pmode(arcn->name,file_mode | S_IRWXU);
    511  1.1  jtc 			} else {
    512  1.1  jtc 				/*
    513  1.1  jtc 				 * We have to add rights to the dir, so we make
    514  1.1  jtc 				 * sure to restore the mode. The mode must be
    515  1.1  jtc 				 * restored AS CREATED and not as stored if
    516  1.1  jtc 				 * pmode is not set.
    517  1.1  jtc 				 */
    518  1.1  jtc 				set_pmode(arcn->name,
    519  1.1  jtc 				    ((sb.st_mode & FILEBITS) | S_IRWXU));
    520  1.1  jtc 				if (!pmode)
    521  1.1  jtc 					arcn->sb.st_mode = sb.st_mode;
    522  1.1  jtc 			}
    523  1.1  jtc 
    524  1.1  jtc 			/*
    525  1.1  jtc 			 * we have to force the mode to what was set here,
    526  1.1  jtc 			 * since we changed it from the default as created.
    527  1.1  jtc 			 */
    528  1.1  jtc 			add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1);
    529  1.1  jtc 		} else if (pmode || patime || pmtime)
    530  1.1  jtc 			add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0);
    531  1.1  jtc 	}
    532  1.1  jtc 
    533  1.1  jtc 	if (patime || pmtime)
    534  1.1  jtc 		set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
    535  1.1  jtc 	return(0);
    536  1.1  jtc }
    537  1.1  jtc 
    538  1.1  jtc /*
    539  1.1  jtc  * unlnk_exist()
    540  1.1  jtc  *	Remove node from file system with the specified name. We pass the type
    541  1.1  jtc  *	of the node that is going to replace it. When we try to create a
    542  1.1  jtc  *	directory and find that it already exists, we allow processing to
    543  1.1  jtc  *	continue as proper modes etc will always be set for it later on.
    544  1.1  jtc  * Return:
    545  1.1  jtc  *	0 is ok to proceed, no file with the specified name exists
    546  1.1  jtc  *	-1 we were unable to remove the node, or we should not remove it (-k)
    547  1.1  jtc  *	1 we found a directory and we were going to create a directory.
    548  1.1  jtc  */
    549  1.1  jtc 
    550  1.1  jtc #if __STDC__
    551  1.1  jtc int
    552  1.5  tls unlnk_exist(char *name, int type)
    553  1.1  jtc #else
    554  1.1  jtc int
    555  1.1  jtc unlnk_exist(name, type)
    556  1.5  tls 	char *name;
    557  1.5  tls 	int type;
    558  1.1  jtc #endif
    559  1.1  jtc {
    560  1.1  jtc 	struct stat sb;
    561  1.1  jtc 
    562  1.1  jtc 	/*
    563  1.1  jtc 	 * the file does not exist, or -k we are done
    564  1.1  jtc 	 */
    565  1.1  jtc 	if (lstat(name, &sb) < 0)
    566  1.1  jtc 		return(0);
    567  1.1  jtc 	if (kflag)
    568  1.1  jtc 		return(-1);
    569  1.1  jtc 
    570  1.1  jtc 	if (S_ISDIR(sb.st_mode)) {
    571  1.1  jtc 		/*
    572  1.1  jtc 		 * try to remove a directory, if it fails and we were going to
    573  1.1  jtc 		 * create a directory anyway, tell the caller (return a 1)
    574  1.1  jtc 		 */
    575  1.1  jtc 		if (rmdir(name) < 0) {
    576  1.1  jtc 			if (type == PAX_DIR)
    577  1.1  jtc 				return(1);
    578  1.1  jtc 			syswarn(1,errno,"Unable to remove directory %s", name);
    579  1.1  jtc 			return(-1);
    580  1.1  jtc 		}
    581  1.1  jtc 		return(0);
    582  1.1  jtc 	}
    583  1.1  jtc 
    584  1.1  jtc 	/*
    585  1.1  jtc 	 * try to get rid of all non-directory type nodes
    586  1.1  jtc 	 */
    587  1.1  jtc 	if (unlink(name) < 0) {
    588  1.1  jtc 		syswarn(1, errno, "Could not unlink %s", name);
    589  1.1  jtc 		return(-1);
    590  1.1  jtc 	}
    591  1.1  jtc 	return(0);
    592  1.1  jtc }
    593  1.1  jtc 
    594  1.1  jtc /*
    595  1.1  jtc  * chk_path()
    596  1.1  jtc  *	We were trying to create some kind of node in the file system and it
    597  1.1  jtc  *	failed. chk_path() makes sure the path up to the node exists and is
    598  1.1  jtc  *	writeable. When we have to create a directory that is missing along the
    599  1.1  jtc  *	path somewhere, the directory we create will be set to the same
    600  1.1  jtc  *	uid/gid as the file has (when uid and gid are being preserved).
    601  1.1  jtc  *	NOTE: this routine is a real performance loss. It is only used as a
    602  1.1  jtc  *	last resort when trying to create entries in the file system.
    603  1.1  jtc  * Return:
    604  1.1  jtc  *	-1 when it could find nothing it is allowed to fix.
    605  1.1  jtc  *	0 otherwise
    606  1.1  jtc  */
    607  1.1  jtc 
    608  1.1  jtc #if __STDC__
    609  1.1  jtc int
    610  1.5  tls chk_path( char *name, uid_t st_uid, gid_t st_gid)
    611  1.1  jtc #else
    612  1.1  jtc int
    613  1.1  jtc chk_path(name, st_uid, st_gid)
    614  1.5  tls 	char *name;
    615  1.1  jtc 	uid_t st_uid;
    616  1.1  jtc 	gid_t st_gid;
    617  1.1  jtc #endif
    618  1.1  jtc {
    619  1.5  tls 	char *spt = name;
    620  1.1  jtc 	struct stat sb;
    621  1.1  jtc 	int retval = -1;
    622  1.1  jtc 
    623  1.1  jtc 	/*
    624  1.1  jtc 	 * watch out for paths with nodes stored directly in / (e.g. /bozo)
    625  1.1  jtc 	 */
    626  1.1  jtc 	if (*spt == '/')
    627  1.1  jtc 		++spt;
    628  1.1  jtc 
    629  1.1  jtc 	for(;;) {
    630  1.1  jtc 		/*
    631  1.1  jtc 		 * work foward from the first / and check each part of the path
    632  1.1  jtc 		 */
    633  1.1  jtc 		spt = strchr(spt, '/');
    634  1.1  jtc 		if (spt == NULL)
    635  1.1  jtc 			break;
    636  1.1  jtc 		*spt = '\0';
    637  1.1  jtc 
    638  1.1  jtc 		/*
    639  1.1  jtc 		 * if it exists we assume it is a directory, it is not within
    640  1.1  jtc 		 * the spec (at least it seems to read that way) to alter the
    641  1.1  jtc 		 * file system for nodes NOT EXPLICITLY stored on the archive.
    642  1.1  jtc 		 * If that assumption is changed, you would test the node here
    643  1.1  jtc 		 * and figure out how to get rid of it (probably like some
    644  1.1  jtc 		 * recursive unlink()) or fix up the directory permissions if
    645  1.1  jtc 		 * required (do an access()).
    646  1.1  jtc 		 */
    647  1.1  jtc 		if (lstat(name, &sb) == 0) {
    648  1.1  jtc 			*(spt++) = '/';
    649  1.1  jtc 			continue;
    650  1.1  jtc 		}
    651  1.1  jtc 
    652  1.1  jtc 		/*
    653  1.1  jtc 		 * the path fails at this point, see if we can create the
    654  1.1  jtc 		 * needed directory and continue on
    655  1.1  jtc 		 */
    656  1.1  jtc 		if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
    657  1.1  jtc 			*spt = '/';
    658  1.1  jtc 			retval = -1;
    659  1.1  jtc 			break;
    660  1.1  jtc 		}
    661  1.1  jtc 
    662  1.1  jtc 		/*
    663  1.1  jtc 		 * we were able to create the directory. We will tell the
    664  1.1  jtc 		 * caller that we found something to fix, and it is ok to try
    665  1.1  jtc 		 * and create the node again.
    666  1.1  jtc 		 */
    667  1.1  jtc 		retval = 0;
    668  1.1  jtc 		if (pids)
    669  1.1  jtc 			(void)set_ids(name, st_uid, st_gid);
    670  1.1  jtc 
    671  1.1  jtc 		/*
    672  1.1  jtc 		 * make sure the user doen't have some strange umask that
    673  1.1  jtc 		 * causes this newly created directory to be unusable. We fix
    674  1.1  jtc 		 * the modes and restore them back to the creation default at
    675  1.1  jtc 		 * the end of pax
    676  1.1  jtc 		 */
    677  1.1  jtc 		if ((access(name, R_OK | W_OK | X_OK) < 0) &&
    678  1.1  jtc 		    (lstat(name, &sb) == 0)) {
    679  1.1  jtc 			set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU));
    680  1.1  jtc 			add_dir(name, spt - name, &sb, 1);
    681  1.1  jtc 		}
    682  1.1  jtc 		*(spt++) = '/';
    683  1.1  jtc 		continue;
    684  1.1  jtc 	}
    685  1.1  jtc 	return(retval);
    686  1.1  jtc }
    687  1.1  jtc 
    688  1.1  jtc /*
    689  1.1  jtc  * set_ftime()
    690  1.1  jtc  *	Set the access time and modification time for a named file. If frc is
    691  1.1  jtc  *	non-zero we force these times to be set even if the the user did not
    692  1.1  jtc  *	request access and/or modification time preservation (this is also
    693  1.1  jtc  *	used by -t to reset access times).
    694  1.1  jtc  *	When ign is zero, only those times the user has asked for are set, the
    695  1.1  jtc  *	other ones are left alone. We do not assume the un-documented feature
    696  1.1  jtc  *	of many utimes() implementations that consider a 0 time value as a do
    697  1.1  jtc  *	not set request.
    698  1.1  jtc  */
    699  1.1  jtc 
    700  1.1  jtc #if __STDC__
    701  1.1  jtc void
    702  1.1  jtc set_ftime(char *fnm, time_t mtime, time_t atime, int frc)
    703  1.1  jtc #else
    704  1.1  jtc void
    705  1.1  jtc set_ftime(fnm, mtime, atime, frc)
    706  1.1  jtc 	char *fnm;
    707  1.1  jtc 	time_t mtime;
    708  1.1  jtc 	time_t atime;
    709  1.1  jtc 	int frc;
    710  1.1  jtc #endif
    711  1.1  jtc {
    712  1.1  jtc 	static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}};
    713  1.1  jtc 	struct stat sb;
    714  1.1  jtc 
    715  1.1  jtc 	tv[0].tv_sec = (long)atime;
    716  1.1  jtc 	tv[1].tv_sec = (long)mtime;
    717  1.1  jtc 	if (!frc && (!patime || !pmtime)) {
    718  1.1  jtc 		/*
    719  1.1  jtc 		 * if we are not forcing, only set those times the user wants
    720  1.1  jtc 		 * set. We get the current values of the times if we need them.
    721  1.1  jtc 		 */
    722  1.1  jtc 		if (lstat(fnm, &sb) == 0) {
    723  1.1  jtc 			if (!patime)
    724  1.1  jtc 				tv[0].tv_sec = (long)sb.st_atime;
    725  1.1  jtc 			if (!pmtime)
    726  1.1  jtc 				tv[1].tv_sec = (long)sb.st_mtime;
    727  1.1  jtc 		} else
    728  1.1  jtc 			syswarn(0,errno,"Unable to obtain file stats %s", fnm);
    729  1.1  jtc 	}
    730  1.1  jtc 
    731  1.1  jtc 	/*
    732  1.1  jtc 	 * set the times
    733  1.1  jtc 	 */
    734  1.1  jtc 	if (utimes(fnm, tv) < 0)
    735  1.1  jtc 		syswarn(1, errno, "Access/modification time set failed on: %s",
    736  1.1  jtc 		    fnm);
    737  1.1  jtc 	return;
    738  1.1  jtc }
    739  1.1  jtc 
    740  1.1  jtc /*
    741  1.1  jtc  * set_ids()
    742  1.1  jtc  *	set the uid and gid of a file system node
    743  1.1  jtc  * Return:
    744  1.1  jtc  *	0 when set, -1 on failure
    745  1.1  jtc  */
    746  1.1  jtc 
    747  1.1  jtc #if __STDC__
    748  1.1  jtc int
    749  1.1  jtc set_ids(char *fnm, uid_t uid, gid_t gid)
    750  1.1  jtc #else
    751  1.1  jtc int
    752  1.1  jtc set_ids(fnm, uid, gid)
    753  1.1  jtc 	char *fnm;
    754  1.1  jtc 	uid_t uid;
    755  1.1  jtc 	gid_t gid;
    756  1.1  jtc #endif
    757  1.1  jtc {
    758  1.1  jtc 	if (chown(fnm, uid, gid) < 0) {
    759  1.1  jtc 		syswarn(1, errno, "Unable to set file uid/gid of %s", fnm);
    760  1.1  jtc 		return(-1);
    761  1.1  jtc 	}
    762  1.1  jtc 	return(0);
    763  1.1  jtc }
    764  1.1  jtc 
    765  1.1  jtc /*
    766  1.1  jtc  * set_pmode()
    767  1.1  jtc  *	Set file access mode
    768  1.1  jtc  */
    769  1.1  jtc 
    770  1.1  jtc #if __STDC__
    771  1.1  jtc void
    772  1.1  jtc set_pmode(char *fnm, mode_t mode)
    773  1.1  jtc #else
    774  1.1  jtc void
    775  1.1  jtc set_pmode(fnm, mode)
    776  1.1  jtc 	char *fnm;
    777  1.1  jtc 	mode_t mode;
    778  1.1  jtc #endif
    779  1.1  jtc {
    780  1.1  jtc 	mode &= ABITS;
    781  1.1  jtc 	if (chmod(fnm, mode) < 0)
    782  1.1  jtc 		syswarn(1, errno, "Could not set permissions on %s", fnm);
    783  1.1  jtc 	return;
    784  1.1  jtc }
    785  1.1  jtc 
    786  1.1  jtc /*
    787  1.1  jtc  * file_write()
    788  1.1  jtc  *	Write/copy a file (during copy or archive extract). This routine knows
    789  1.1  jtc  *	how to copy files with lseek holes in it. (Which are read as file
    790  1.1  jtc  *	blocks containing all 0's but do not have any file blocks associated
    791  1.1  jtc  *	with the data). Typical examples of these are files created by dbm
    792  1.1  jtc  *	variants (.pag files). While the file size of these files are huge, the
    793  1.1  jtc  *	actual storage is quite small (the files are sparse). The problem is
    794  1.1  jtc  *	the holes read as all zeros so are probably stored on the archive that
    795  1.1  jtc  *	way (there is no way to determine if the file block is really a hole,
    796  1.1  jtc  *	we only know that a file block of all zero's can be a hole).
    797  1.1  jtc  *	At this writing, no major archive format knows how to archive files
    798  1.1  jtc  *	with holes. However, on extraction (or during copy, -rw) we have to
    799  1.1  jtc  *	deal with these files. Without detecting the holes, the files can
    800  1.1  jtc  *	consume a lot of file space if just written to disk. This replacement
    801  1.1  jtc  *	for write when passed the basic allocation size of a file system block,
    802  1.1  jtc  *	uses lseek whenever it detects the input data is all 0 within that
    803  1.1  jtc  *	file block. In more detail, the strategy is as follows:
    804  1.1  jtc  *	While the input is all zero keep doing an lseek. Keep track of when we
    805  1.1  jtc  *	pass over file block boundries. Only write when we hit a non zero
    806  1.1  jtc  *	input. once we have written a file block, we continue to write it to
    807  1.1  jtc  *	the end (we stop looking at the input). When we reach the start of the
    808  1.1  jtc  *	next file block, start checking for zero blocks again. Working on file
    809  1.1  jtc  *	block boundries significantly reduces the overhead when copying files
    810  1.1  jtc  *	that are NOT very sparse. This overhead (when compared to a write) is
    811  1.1  jtc  *	almost below the measurement resolution on many systems. Without it,
    812  1.1  jtc  *	files with holes cannot be safely copied. It does has a side effect as
    813  1.1  jtc  *	it can put holes into files that did not have them before, but that is
    814  1.1  jtc  *	not a problem since the file contents are unchanged (in fact it saves
    815  1.1  jtc  *	file space). (Except on paging files for diskless clients. But since we
    816  1.1  jtc  *	cannot determine one of those file from here, we ignore them). If this
    817  1.1  jtc  *	ever ends up on a system where CTG files are supported and the holes
    818  1.1  jtc  *	are not desired, just do a conditional test in those routines that
    819  1.1  jtc  *	call file_write() and have it call write() instead. BEFORE CLOSING THE
    820  1.1  jtc  *	FILE, make sure to call file_flush() when the last write finishes with
    821  1.1  jtc  *	an empty block. A lot of file systems will not create an lseek hole at
    822  1.1  jtc  *	the end. In this case we drop a single 0 at the end to force the
    823  1.1  jtc  *	trailing 0's in the file.
    824  1.1  jtc  *	---Parameters---
    825  1.1  jtc  *	rem: how many bytes left in this file system block
    826  1.1  jtc  *	isempt: have we written to the file block yet (is it empty)
    827  1.1  jtc  *	sz: basic file block allocation size
    828  1.1  jtc  *	cnt: number of bytes on this write
    829  1.1  jtc  *	str: buffer to write
    830  1.1  jtc  * Return:
    831  1.1  jtc  *	number of bytes written, -1 on write (or lseek) error.
    832  1.1  jtc  */
    833  1.1  jtc 
    834  1.1  jtc #if __STDC__
    835  1.1  jtc int
    836  1.5  tls file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz,
    837  1.1  jtc 	char *name)
    838  1.1  jtc #else
    839  1.1  jtc int
    840  1.1  jtc file_write(fd, str, cnt, rem, isempt, sz, name)
    841  1.1  jtc 	int fd;
    842  1.1  jtc 	char *str;
    843  1.5  tls 	int cnt;
    844  1.1  jtc 	int *rem;
    845  1.1  jtc 	int *isempt;
    846  1.1  jtc 	int sz;
    847  1.1  jtc 	char *name;
    848  1.1  jtc #endif
    849  1.1  jtc {
    850  1.5  tls 	char *pt;
    851  1.5  tls 	char *end;
    852  1.5  tls 	int wcnt;
    853  1.5  tls 	char *st = str;
    854  1.1  jtc 
    855  1.1  jtc 	/*
    856  1.1  jtc 	 * while we have data to process
    857  1.1  jtc 	 */
    858  1.1  jtc 	while (cnt) {
    859  1.1  jtc 		if (!*rem) {
    860  1.1  jtc 			/*
    861  1.1  jtc 			 * We are now at the start of file system block again
    862  1.1  jtc 			 * (or what we think one is...). start looking for
    863  1.1  jtc 			 * empty blocks again
    864  1.1  jtc 			 */
    865  1.1  jtc 			*isempt = 1;
    866  1.1  jtc 			*rem = sz;
    867  1.1  jtc 		}
    868  1.1  jtc 
    869  1.1  jtc 		/*
    870  1.1  jtc 		 * only examine up to the end of the current file block or
    871  1.1  jtc 		 * remaining characters to write, whatever is smaller
    872  1.1  jtc 		 */
    873  1.1  jtc 		wcnt = MIN(cnt, *rem);
    874  1.1  jtc 		cnt -= wcnt;
    875  1.1  jtc 		*rem -= wcnt;
    876  1.1  jtc 		if (*isempt) {
    877  1.1  jtc 			/*
    878  1.1  jtc 			 * have not written to this block yet, so we keep
    879  1.1  jtc 			 * looking for zero's
    880  1.1  jtc 			 */
    881  1.1  jtc 			pt = st;
    882  1.1  jtc 			end = st + wcnt;
    883  1.1  jtc 
    884  1.1  jtc 			/*
    885  1.1  jtc 			 * look for a zero filled buffer
    886  1.1  jtc 			 */
    887  1.1  jtc 			while ((pt < end) && (*pt == '\0'))
    888  1.1  jtc 				++pt;
    889  1.1  jtc 
    890  1.1  jtc 			if (pt == end) {
    891  1.1  jtc 				/*
    892  1.1  jtc 				 * skip, buf is empty so far
    893  1.1  jtc 				 */
    894  1.1  jtc 				if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
    895  1.1  jtc 					syswarn(1,errno,"File seek on %s",
    896  1.1  jtc 					    name);
    897  1.1  jtc 					return(-1);
    898  1.1  jtc 				}
    899  1.1  jtc 				st = pt;
    900  1.1  jtc 				continue;
    901  1.1  jtc 			}
    902  1.1  jtc 			/*
    903  1.1  jtc 			 * drat, the buf is not zero filled
    904  1.1  jtc 			 */
    905  1.1  jtc 			*isempt = 0;
    906  1.1  jtc 		}
    907  1.1  jtc 
    908  1.1  jtc 		/*
    909  1.1  jtc 		 * have non-zero data in this file system block, have to write
    910  1.1  jtc 		 */
    911  1.1  jtc 		if (write(fd, st, wcnt) != wcnt) {
    912  1.1  jtc 			syswarn(1, errno, "Failed write to file %s", name);
    913  1.1  jtc 			return(-1);
    914  1.1  jtc 		}
    915  1.1  jtc 		st += wcnt;
    916  1.1  jtc 	}
    917  1.1  jtc 	return(st - str);
    918  1.1  jtc }
    919  1.1  jtc 
    920  1.1  jtc /*
    921  1.1  jtc  * file_flush()
    922  1.1  jtc  *	when the last file block in a file is zero, many file systems will not
    923  1.1  jtc  *	let us create a hole at the end. To get the last block with zeros, we
    924  1.1  jtc  *	write the last BYTE with a zero (back up one byte and write a zero).
    925  1.1  jtc  */
    926  1.1  jtc 
    927  1.1  jtc #if __STDC__
    928  1.1  jtc void
    929  1.1  jtc file_flush(int fd, char *fname, int isempt)
    930  1.1  jtc #else
    931  1.1  jtc void
    932  1.1  jtc file_flush(fd, fname, isempt)
    933  1.1  jtc 	int fd;
    934  1.1  jtc 	char *fname;
    935  1.1  jtc 	int isempt;
    936  1.1  jtc #endif
    937  1.1  jtc {
    938  1.1  jtc 	static char blnk[] = "\0";
    939  1.1  jtc 
    940  1.1  jtc 	/*
    941  1.1  jtc 	 * silly test, but make sure we are only called when the last block is
    942  1.1  jtc 	 * filled with all zeros.
    943  1.1  jtc 	 */
    944  1.1  jtc 	if (!isempt)
    945  1.1  jtc 		return;
    946  1.1  jtc 
    947  1.1  jtc 	/*
    948  1.1  jtc 	 * move back one byte and write a zero
    949  1.1  jtc 	 */
    950  1.1  jtc 	if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
    951  1.1  jtc 		syswarn(1, errno, "Failed seek on file %s", fname);
    952  1.1  jtc 		return;
    953  1.1  jtc 	}
    954  1.1  jtc 
    955  1.1  jtc 	if (write(fd, blnk, 1) < 0)
    956  1.1  jtc 		syswarn(1, errno, "Failed write to file %s", fname);
    957  1.1  jtc 	return;
    958  1.1  jtc }
    959  1.1  jtc 
    960  1.1  jtc /*
    961  1.1  jtc  * rdfile_close()
    962  1.1  jtc  *	close a file we have beed reading (to copy or archive). If we have to
    963  1.1  jtc  *	reset access time (tflag) do so (the times are stored in arcn).
    964  1.1  jtc  */
    965  1.1  jtc 
    966  1.1  jtc #if __STDC__
    967  1.1  jtc void
    968  1.5  tls rdfile_close(ARCHD *arcn, int *fd)
    969  1.1  jtc #else
    970  1.1  jtc void
    971  1.1  jtc rdfile_close(arcn, fd)
    972  1.5  tls 	ARCHD *arcn;
    973  1.5  tls 	int *fd;
    974  1.1  jtc #endif
    975  1.1  jtc {
    976  1.1  jtc 	/*
    977  1.1  jtc 	 * make sure the file is open
    978  1.1  jtc 	 */
    979  1.1  jtc 	if (*fd < 0)
    980  1.1  jtc 		return;
    981  1.1  jtc 
    982  1.1  jtc 	(void)close(*fd);
    983  1.1  jtc 	*fd = -1;
    984  1.1  jtc 	if (!tflag)
    985  1.1  jtc 		return;
    986  1.1  jtc 
    987  1.1  jtc 	/*
    988  1.1  jtc 	 * user wants last access time reset
    989  1.1  jtc 	 */
    990  1.1  jtc 	set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1);
    991  1.1  jtc 	return;
    992  1.1  jtc }
    993  1.1  jtc 
    994  1.1  jtc /*
    995  1.1  jtc  * set_crc()
    996  1.1  jtc  *	read a file to calculate its crc. This is a real drag. Archive formats
    997  1.1  jtc  *	that have this, end up reading the file twice (we have to write the
    998  1.1  jtc  *	header WITH the crc before writing the file contents. Oh well...
    999  1.1  jtc  * Return:
   1000  1.1  jtc  *	0 if was able to calculate the crc, -1 otherwise
   1001  1.1  jtc  */
   1002  1.1  jtc 
   1003  1.1  jtc #if __STDC__
   1004  1.1  jtc int
   1005  1.5  tls set_crc(ARCHD *arcn, int fd)
   1006  1.1  jtc #else
   1007  1.1  jtc int
   1008  1.1  jtc set_crc(arcn, fd)
   1009  1.5  tls 	ARCHD *arcn;
   1010  1.5  tls 	int fd;
   1011  1.1  jtc #endif
   1012  1.1  jtc {
   1013  1.5  tls 	int i;
   1014  1.5  tls 	int res;
   1015  1.1  jtc 	off_t cpcnt = 0L;
   1016  1.1  jtc 	u_long size;
   1017  1.1  jtc 	unsigned long crc = 0L;
   1018  1.1  jtc 	char tbuf[FILEBLK];
   1019  1.1  jtc 	struct stat sb;
   1020  1.1  jtc 
   1021  1.1  jtc 	if (fd < 0) {
   1022  1.1  jtc 		/*
   1023  1.1  jtc 		 * hmm, no fd, should never happen. well no crc then.
   1024  1.1  jtc 		 */
   1025  1.1  jtc 		arcn->crc = 0L;
   1026  1.1  jtc 		return(0);
   1027  1.1  jtc 	}
   1028  1.1  jtc 
   1029  1.1  jtc 	if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf))
   1030  1.1  jtc 		size = (u_long)sizeof(tbuf);
   1031  1.1  jtc 
   1032  1.1  jtc 	/*
   1033  1.1  jtc 	 * read all the bytes we think that there are in the file. If the user
   1034  1.1  jtc 	 * is trying to archive an active file, forget this file.
   1035  1.1  jtc 	 */
   1036  1.1  jtc 	for(;;) {
   1037  1.1  jtc 		if ((res = read(fd, tbuf, size)) <= 0)
   1038  1.1  jtc 			break;
   1039  1.1  jtc 		cpcnt += res;
   1040  1.1  jtc 		for (i = 0; i < res; ++i)
   1041  1.1  jtc 			crc += (tbuf[i] & 0xff);
   1042  1.1  jtc 	}
   1043  1.1  jtc 
   1044  1.1  jtc 	/*
   1045  1.1  jtc 	 * safety check. we want to avoid archiving files that are active as
   1046  1.1  jtc 	 * they can create inconsistant archive copies.
   1047  1.1  jtc 	 */
   1048  1.1  jtc 	if (cpcnt != arcn->sb.st_size)
   1049  1.1  jtc 		warn(1, "File changed size %s", arcn->org_name);
   1050  1.1  jtc 	else if (fstat(fd, &sb) < 0)
   1051  1.1  jtc 		syswarn(1, errno, "Failed stat on %s", arcn->org_name);
   1052  1.1  jtc 	else if (arcn->sb.st_mtime != sb.st_mtime)
   1053  1.1  jtc 		warn(1, "File %s was modified during read", arcn->org_name);
   1054  1.1  jtc 	else if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
   1055  1.1  jtc 		syswarn(1, errno, "File rewind failed on: %s", arcn->org_name);
   1056  1.1  jtc 	else {
   1057  1.1  jtc 		arcn->crc = crc;
   1058  1.1  jtc 		return(0);
   1059  1.1  jtc 	}
   1060  1.1  jtc 	return(-1);
   1061  1.1  jtc }
   1062