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