xref: /freebsd/bin/pax/file_subs.c (revision f2703795ca9c00c3cae16ecc06316c06aa8692aa)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
34b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
44b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
54b88c807SRodney W. Grimes  *
64b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
74b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
84b88c807SRodney W. Grimes  *
94b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
104b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
114b88c807SRodney W. Grimes  * are met:
124b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
144b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
164b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
344b88c807SRodney W. Grimes #ifndef lint
35c9a8d1f4SPhilippe Charnier #if 0
36c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)file_subs.c	8.1 (Berkeley) 5/31/93";
37c9a8d1f4SPhilippe Charnier #endif
384b88c807SRodney W. Grimes #endif /* not lint */
392749b141SDavid E. O'Brien #include <sys/cdefs.h>
402749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #include <sys/types.h>
434b88c807SRodney W. Grimes #include <sys/time.h>
444b88c807SRodney W. Grimes #include <sys/stat.h>
454b88c807SRodney W. Grimes #include <unistd.h>
464b88c807SRodney W. Grimes #include <fcntl.h>
474b88c807SRodney W. Grimes #include <string.h>
484b88c807SRodney W. Grimes #include <stdio.h>
494b88c807SRodney W. Grimes #include <errno.h>
504b88c807SRodney W. Grimes #include <sys/uio.h>
514b88c807SRodney W. Grimes #include <stdlib.h>
524b88c807SRodney W. Grimes #include "pax.h"
53b1787decSKris Kennaway #include "options.h"
544b88c807SRodney W. Grimes #include "extern.h"
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes static int
57f789b261SWarner Losh mk_link(char *,struct stat *,char *, int);
584b88c807SRodney W. Grimes 
594b88c807SRodney W. Grimes /*
604b88c807SRodney W. Grimes  * routines that deal with file operations such as: creating, removing;
614b88c807SRodney W. Grimes  * and setting access modes, uid/gid and times of files
624b88c807SRodney W. Grimes  */
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes #define FILEBITS		(S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
654b88c807SRodney W. Grimes #define SETBITS			(S_ISUID | S_ISGID)
664b88c807SRodney W. Grimes #define ABITS			(FILEBITS | SETBITS)
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes /*
694b88c807SRodney W. Grimes  * file_creat()
704b88c807SRodney W. Grimes  *	Create and open a file.
714b88c807SRodney W. Grimes  * Return:
724b88c807SRodney W. Grimes  *	file descriptor or -1 for failure
734b88c807SRodney W. Grimes  */
744b88c807SRodney W. Grimes 
754b88c807SRodney W. Grimes int
76f789b261SWarner Losh file_creat(ARCHD *arcn)
774b88c807SRodney W. Grimes {
784b88c807SRodney W. Grimes 	int fd = -1;
794b88c807SRodney W. Grimes 	mode_t file_mode;
804b88c807SRodney W. Grimes 	int oerrno;
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes 	/*
834b88c807SRodney W. Grimes 	 * assume file doesn't exist, so just try to create it, most times this
844b88c807SRodney W. Grimes 	 * works. We have to take special handling when the file does exist. To
854b88c807SRodney W. Grimes 	 * detect this, we use O_EXCL. For example when trying to create a
864b88c807SRodney W. Grimes 	 * file and a character device or fifo exists with the same name, we
874b88c807SRodney W. Grimes 	 * can accidently open the device by mistake (or block waiting to open)
8846be34b9SKris Kennaway 	 * If we find that the open has failed, then figure spend the effort to
894b88c807SRodney W. Grimes 	 * figure out why. This strategy was found to have better average
904b88c807SRodney W. Grimes 	 * performance in common use than checking the file (and the path)
914b88c807SRodney W. Grimes 	 * first with lstat.
924b88c807SRodney W. Grimes 	 */
934b88c807SRodney W. Grimes 	file_mode = arcn->sb.st_mode & FILEBITS;
944b88c807SRodney W. Grimes 	if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
954b88c807SRodney W. Grimes 	    file_mode)) >= 0)
964b88c807SRodney W. Grimes 		return(fd);
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes 	/*
994b88c807SRodney W. Grimes 	 * the file seems to exist. First we try to get rid of it (found to be
1004b88c807SRodney W. Grimes 	 * the second most common failure when traced). If this fails, only
1014b88c807SRodney W. Grimes 	 * then we go to the expense to check and create the path to the file
1024b88c807SRodney W. Grimes 	 */
1034b88c807SRodney W. Grimes 	if (unlnk_exist(arcn->name, arcn->type) != 0)
1044b88c807SRodney W. Grimes 		return(-1);
1054b88c807SRodney W. Grimes 
1064b88c807SRodney W. Grimes 	for (;;) {
1074b88c807SRodney W. Grimes 		/*
1084b88c807SRodney W. Grimes 		 * try to open it again, if this fails, check all the nodes in
1094b88c807SRodney W. Grimes 		 * the path and give it a final try. if chk_path() finds that
1104b88c807SRodney W. Grimes 		 * it cannot fix anything, we will skip the last attempt
1114b88c807SRodney W. Grimes 		 */
1124b88c807SRodney W. Grimes 		if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC,
1134b88c807SRodney W. Grimes 		    file_mode)) >= 0)
1144b88c807SRodney W. Grimes 			break;
1154b88c807SRodney W. Grimes 		oerrno = errno;
116b1787decSKris Kennaway 		if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
117778766feSKris Kennaway 			syswarn(1, oerrno, "Unable to create %s", arcn->name);
1184b88c807SRodney W. Grimes 			return(-1);
1194b88c807SRodney W. Grimes 		}
1204b88c807SRodney W. Grimes 	}
1214b88c807SRodney W. Grimes 	return(fd);
1224b88c807SRodney W. Grimes }
1234b88c807SRodney W. Grimes 
1244b88c807SRodney W. Grimes /*
1254b88c807SRodney W. Grimes  * file_close()
1264b88c807SRodney W. Grimes  *	Close file descriptor to a file just created by pax. Sets modes,
1274b88c807SRodney W. Grimes  *	ownership and times as required.
1284b88c807SRodney W. Grimes  * Return:
1294b88c807SRodney W. Grimes  *	0 for success, -1 for failure
1304b88c807SRodney W. Grimes  */
1314b88c807SRodney W. Grimes 
1324b88c807SRodney W. Grimes void
133f789b261SWarner Losh file_close(ARCHD *arcn, int fd)
1344b88c807SRodney W. Grimes {
1354b88c807SRodney W. Grimes 	int res = 0;
1364b88c807SRodney W. Grimes 
1374b88c807SRodney W. Grimes 	if (fd < 0)
1384b88c807SRodney W. Grimes 		return;
1394b88c807SRodney W. Grimes 	if (close(fd) < 0)
140778766feSKris Kennaway 		syswarn(0, errno, "Unable to close file descriptor on %s",
1414b88c807SRodney W. Grimes 		    arcn->name);
1424b88c807SRodney W. Grimes 
1434b88c807SRodney W. Grimes 	/*
1444b88c807SRodney W. Grimes 	 * set owner/groups first as this may strip off mode bits we want
1454b88c807SRodney W. Grimes 	 * then set file permission modes. Then set file access and
1464b88c807SRodney W. Grimes 	 * modification times.
1474b88c807SRodney W. Grimes 	 */
1484b88c807SRodney W. Grimes 	if (pids)
1494b88c807SRodney W. Grimes 		res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid);
1504b88c807SRodney W. Grimes 
1514b88c807SRodney W. Grimes 	/*
1524b88c807SRodney W. Grimes 	 * IMPORTANT SECURITY NOTE:
1534b88c807SRodney W. Grimes 	 * if not preserving mode or we cannot set uid/gid, then PROHIBIT
1544b88c807SRodney W. Grimes 	 * set uid/gid bits
1554b88c807SRodney W. Grimes 	 */
1564b88c807SRodney W. Grimes 	if (!pmode || res)
1574b88c807SRodney W. Grimes 		arcn->sb.st_mode &= ~(SETBITS);
1584b88c807SRodney W. Grimes 	if (pmode)
1594b88c807SRodney W. Grimes 		set_pmode(arcn->name, arcn->sb.st_mode);
1604b88c807SRodney W. Grimes 	if (patime || pmtime)
1614b88c807SRodney W. Grimes 		set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
1624b88c807SRodney W. Grimes }
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes /*
1654b88c807SRodney W. Grimes  * lnk_creat()
1664b88c807SRodney W. Grimes  *	Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
1674b88c807SRodney W. Grimes  *	must exist;
1684b88c807SRodney W. Grimes  * Return:
1694b88c807SRodney W. Grimes  *	0 if ok, -1 otherwise
1704b88c807SRodney W. Grimes  */
1714b88c807SRodney W. Grimes 
1724b88c807SRodney W. Grimes int
173f789b261SWarner Losh lnk_creat(ARCHD *arcn)
1744b88c807SRodney W. Grimes {
1754b88c807SRodney W. Grimes 	struct stat sb;
1764b88c807SRodney W. Grimes 
1774b88c807SRodney W. Grimes 	/*
1784b88c807SRodney W. Grimes 	 * we may be running as root, so we have to be sure that link target
1794b88c807SRodney W. Grimes 	 * is not a directory, so we lstat and check
1804b88c807SRodney W. Grimes 	 */
1814b88c807SRodney W. Grimes 	if (lstat(arcn->ln_name, &sb) < 0) {
182778766feSKris Kennaway 		syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name,
1834b88c807SRodney W. Grimes 		    arcn->name);
1844b88c807SRodney W. Grimes 		return(-1);
1854b88c807SRodney W. Grimes 	}
1864b88c807SRodney W. Grimes 
1874b88c807SRodney W. Grimes 	if (S_ISDIR(sb.st_mode)) {
188778766feSKris Kennaway 		paxwarn(1, "A hard link to the directory %s is not allowed",
1894b88c807SRodney W. Grimes 		    arcn->ln_name);
1904b88c807SRodney W. Grimes 		return(-1);
1914b88c807SRodney W. Grimes 	}
1924b88c807SRodney W. Grimes 
1934b88c807SRodney W. Grimes 	return(mk_link(arcn->ln_name, &sb, arcn->name, 0));
1944b88c807SRodney W. Grimes }
1954b88c807SRodney W. Grimes 
1964b88c807SRodney W. Grimes /*
1974b88c807SRodney W. Grimes  * cross_lnk()
1984b88c807SRodney W. Grimes  *	Create a hard link to arcn->org_name from arcn->name. Only used in copy
199778766feSKris Kennaway  *	with the -l flag. No warning or error if this does not succeed (we will
2004b88c807SRodney W. Grimes  *	then just create the file)
2014b88c807SRodney W. Grimes  * Return:
2024b88c807SRodney W. Grimes  *	1 if copy() should try to create this file node
2034b88c807SRodney W. Grimes  *	0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
2044b88c807SRodney W. Grimes  */
2054b88c807SRodney W. Grimes 
2064b88c807SRodney W. Grimes int
207f789b261SWarner Losh cross_lnk(ARCHD *arcn)
2084b88c807SRodney W. Grimes {
2094b88c807SRodney W. Grimes 	/*
21046be34b9SKris Kennaway 	 * try to make a link to original file (-l flag in copy mode). make sure
2114b88c807SRodney W. Grimes 	 * we do not try to link to directories in case we are running as root
2124b88c807SRodney W. Grimes 	 * (and it might succeed).
2134b88c807SRodney W. Grimes 	 */
2144b88c807SRodney W. Grimes 	if (arcn->type == PAX_DIR)
2154b88c807SRodney W. Grimes 		return(1);
2164b88c807SRodney W. Grimes 	return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1));
2174b88c807SRodney W. Grimes }
2184b88c807SRodney W. Grimes 
2194b88c807SRodney W. Grimes /*
2204b88c807SRodney W. Grimes  * chk_same()
2214b88c807SRodney W. Grimes  *	In copy mode if we are not trying to make hard links between the src
2224b88c807SRodney W. Grimes  *	and destinations, make sure we are not going to overwrite ourselves by
2234b88c807SRodney W. Grimes  *	accident. This slows things down a little, but we have to protect all
2244b88c807SRodney W. Grimes  *	those people who make typing errors.
2254b88c807SRodney W. Grimes  * Return:
2264b88c807SRodney W. Grimes  *	1 the target does not exist, go ahead and copy
2274b88c807SRodney W. Grimes  *	0 skip it file exists (-k) or may be the same as source file
2284b88c807SRodney W. Grimes  */
2294b88c807SRodney W. Grimes 
2304b88c807SRodney W. Grimes int
231f789b261SWarner Losh chk_same(ARCHD *arcn)
2324b88c807SRodney W. Grimes {
2334b88c807SRodney W. Grimes 	struct stat sb;
2344b88c807SRodney W. Grimes 
2354b88c807SRodney W. Grimes 	/*
2364b88c807SRodney W. Grimes 	 * if file does not exist, return. if file exists and -k, skip it
2374b88c807SRodney W. Grimes 	 * quietly
2384b88c807SRodney W. Grimes 	 */
2394b88c807SRodney W. Grimes 	if (lstat(arcn->name, &sb) < 0)
2404b88c807SRodney W. Grimes 		return(1);
2414b88c807SRodney W. Grimes 	if (kflag)
2424b88c807SRodney W. Grimes 		return(0);
2434b88c807SRodney W. Grimes 
2444b88c807SRodney W. Grimes 	/*
2454b88c807SRodney W. Grimes 	 * better make sure the user does not have src == dest by mistake
2464b88c807SRodney W. Grimes 	 */
2474b88c807SRodney W. Grimes 	if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) {
248778766feSKris Kennaway 		paxwarn(1, "Unable to copy %s, file would overwrite itself",
2494b88c807SRodney W. Grimes 		    arcn->name);
2504b88c807SRodney W. Grimes 		return(0);
2514b88c807SRodney W. Grimes 	}
2524b88c807SRodney W. Grimes 	return(1);
2534b88c807SRodney W. Grimes }
2544b88c807SRodney W. Grimes 
2554b88c807SRodney W. Grimes /*
2564b88c807SRodney W. Grimes  * mk_link()
2574b88c807SRodney W. Grimes  *	try to make a hard link between two files. if ign set, we do not
2584b88c807SRodney W. Grimes  *	complain.
2594b88c807SRodney W. Grimes  * Return:
2604b88c807SRodney W. Grimes  *	0 if successful (or we are done with this file but no error, such as
2614b88c807SRodney W. Grimes  *	finding the from file exists and the user has set -k).
2624b88c807SRodney W. Grimes  *	1 when ign was set to indicates we could not make the link but we
2634b88c807SRodney W. Grimes  *	should try to copy/extract the file as that might work (and is an
2644b88c807SRodney W. Grimes  *	allowed option). -1 an error occurred.
2654b88c807SRodney W. Grimes  */
2664b88c807SRodney W. Grimes 
2674b88c807SRodney W. Grimes static int
268f789b261SWarner Losh mk_link(char *to, struct stat *to_sb, char *from,
2694b88c807SRodney W. Grimes 	int ign)
2704b88c807SRodney W. Grimes {
2714b88c807SRodney W. Grimes 	struct stat sb;
2724b88c807SRodney W. Grimes 	int oerrno;
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes 	/*
2754b88c807SRodney W. Grimes 	 * if from file exists, it has to be unlinked to make the link. If the
2764b88c807SRodney W. Grimes 	 * file exists and -k is set, skip it quietly
2774b88c807SRodney W. Grimes 	 */
2784b88c807SRodney W. Grimes 	if (lstat(from, &sb) == 0) {
2794b88c807SRodney W. Grimes 		if (kflag)
2804b88c807SRodney W. Grimes 			return(0);
2814b88c807SRodney W. Grimes 
2824b88c807SRodney W. Grimes 		/*
2834b88c807SRodney W. Grimes 		 * make sure it is not the same file, protect the user
2844b88c807SRodney W. Grimes 		 */
2854b88c807SRodney W. Grimes 		if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) {
286778766feSKris Kennaway 			paxwarn(1, "Unable to link file %s to itself", to);
287f2703795SRalf S. Engelschall 			return(-1);
2884b88c807SRodney W. Grimes 		}
2894b88c807SRodney W. Grimes 
2904b88c807SRodney W. Grimes 		/*
2914b88c807SRodney W. Grimes 		 * try to get rid of the file, based on the type
2924b88c807SRodney W. Grimes 		 */
2934b88c807SRodney W. Grimes 		if (S_ISDIR(sb.st_mode)) {
2944b88c807SRodney W. Grimes 			if (rmdir(from) < 0) {
295778766feSKris Kennaway 				syswarn(1, errno, "Unable to remove %s", from);
2964b88c807SRodney W. Grimes 				return(-1);
2974b88c807SRodney W. Grimes 			}
2984b88c807SRodney W. Grimes 		} else if (unlink(from) < 0) {
2994b88c807SRodney W. Grimes 			if (!ign) {
300778766feSKris Kennaway 				syswarn(1, errno, "Unable to remove %s", from);
3014b88c807SRodney W. Grimes 				return(-1);
3024b88c807SRodney W. Grimes 			}
3034b88c807SRodney W. Grimes 			return(1);
3044b88c807SRodney W. Grimes 		}
3054b88c807SRodney W. Grimes 	}
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes 	/*
3084b88c807SRodney W. Grimes 	 * from file is gone (or did not exist), try to make the hard link.
3094b88c807SRodney W. Grimes 	 * if it fails, check the path and try it again (if chk_path() says to
3104b88c807SRodney W. Grimes 	 * try again)
3114b88c807SRodney W. Grimes 	 */
3124b88c807SRodney W. Grimes 	for (;;) {
3134b88c807SRodney W. Grimes 		if (link(to, from) == 0)
3144b88c807SRodney W. Grimes 			break;
3154b88c807SRodney W. Grimes 		oerrno = errno;
316b1787decSKris Kennaway 		if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0)
3174b88c807SRodney W. Grimes 			continue;
3184b88c807SRodney W. Grimes 		if (!ign) {
319778766feSKris Kennaway 			syswarn(1, oerrno, "Could not link to %s from %s", to,
3204b88c807SRodney W. Grimes 			    from);
3214b88c807SRodney W. Grimes 			return(-1);
3224b88c807SRodney W. Grimes 		}
3234b88c807SRodney W. Grimes 		return(1);
3244b88c807SRodney W. Grimes 	}
3254b88c807SRodney W. Grimes 
3264b88c807SRodney W. Grimes 	/*
3274b88c807SRodney W. Grimes 	 * all right the link was made
3284b88c807SRodney W. Grimes 	 */
3294b88c807SRodney W. Grimes 	return(0);
3304b88c807SRodney W. Grimes }
3314b88c807SRodney W. Grimes 
3324b88c807SRodney W. Grimes /*
3334b88c807SRodney W. Grimes  * node_creat()
3344b88c807SRodney W. Grimes  *	create an entry in the file system (other than a file or hard link).
3354b88c807SRodney W. Grimes  *	If successful, sets uid/gid modes and times as required.
3364b88c807SRodney W. Grimes  * Return:
3374b88c807SRodney W. Grimes  *	0 if ok, -1 otherwise
3384b88c807SRodney W. Grimes  */
3394b88c807SRodney W. Grimes 
3404b88c807SRodney W. Grimes int
341f789b261SWarner Losh node_creat(ARCHD *arcn)
3424b88c807SRodney W. Grimes {
343f789b261SWarner Losh 	int res;
344f789b261SWarner Losh 	int ign = 0;
345f789b261SWarner Losh 	int oerrno;
346f789b261SWarner Losh 	int pass = 0;
3474b88c807SRodney W. Grimes 	mode_t file_mode;
3484b88c807SRodney W. Grimes 	struct stat sb;
3494b88c807SRodney W. Grimes 
3504b88c807SRodney W. Grimes 	/*
3514b88c807SRodney W. Grimes 	 * create node based on type, if that fails try to unlink the node and
3524b88c807SRodney W. Grimes 	 * try again. finally check the path and try again. As noted in the
3534b88c807SRodney W. Grimes 	 * file and link creation routines, this method seems to exhibit the
3544b88c807SRodney W. Grimes 	 * best performance in general use workloads.
3554b88c807SRodney W. Grimes 	 */
3564b88c807SRodney W. Grimes 	file_mode = arcn->sb.st_mode & FILEBITS;
3574b88c807SRodney W. Grimes 
3584b88c807SRodney W. Grimes 	for (;;) {
3594b88c807SRodney W. Grimes 		switch(arcn->type) {
3604b88c807SRodney W. Grimes 		case PAX_DIR:
3614b88c807SRodney W. Grimes 			res = mkdir(arcn->name, file_mode);
3624b88c807SRodney W. Grimes 			if (ign)
3634b88c807SRodney W. Grimes 				res = 0;
3644b88c807SRodney W. Grimes 			break;
3654b88c807SRodney W. Grimes 		case PAX_CHR:
3664b88c807SRodney W. Grimes 			file_mode |= S_IFCHR;
3674b88c807SRodney W. Grimes 			res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
3684b88c807SRodney W. Grimes 			break;
3694b88c807SRodney W. Grimes 		case PAX_BLK:
3704b88c807SRodney W. Grimes 			file_mode |= S_IFBLK;
3714b88c807SRodney W. Grimes 			res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
3724b88c807SRodney W. Grimes 			break;
3734b88c807SRodney W. Grimes 		case PAX_FIF:
3744b88c807SRodney W. Grimes 			res = mkfifo(arcn->name, file_mode);
3754b88c807SRodney W. Grimes 			break;
3764b88c807SRodney W. Grimes 		case PAX_SCK:
3774b88c807SRodney W. Grimes 			/*
3784b88c807SRodney W. Grimes 			 * Skip sockets, operation has no meaning under BSD
3794b88c807SRodney W. Grimes 			 */
380778766feSKris Kennaway 			paxwarn(0,
3814b88c807SRodney W. Grimes 			    "%s skipped. Sockets cannot be copied or extracted",
3824b88c807SRodney W. Grimes 			    arcn->name);
3834b88c807SRodney W. Grimes 			return(-1);
3844b88c807SRodney W. Grimes 		case PAX_SLK:
385b1787decSKris Kennaway 			res = symlink(arcn->ln_name, arcn->name);
3864b88c807SRodney W. Grimes 			break;
3874b88c807SRodney W. Grimes 		case PAX_CTG:
3884b88c807SRodney W. Grimes 		case PAX_HLK:
3894b88c807SRodney W. Grimes 		case PAX_HRG:
3904b88c807SRodney W. Grimes 		case PAX_REG:
3914b88c807SRodney W. Grimes 		default:
3924b88c807SRodney W. Grimes 			/*
3934b88c807SRodney W. Grimes 			 * we should never get here
3944b88c807SRodney W. Grimes 			 */
395778766feSKris Kennaway 			paxwarn(0, "%s has an unknown file type, skipping",
3964b88c807SRodney W. Grimes 				arcn->name);
3974b88c807SRodney W. Grimes 			return(-1);
3984b88c807SRodney W. Grimes 		}
3994b88c807SRodney W. Grimes 
4004b88c807SRodney W. Grimes 		/*
4014b88c807SRodney W. Grimes 		 * if we were able to create the node break out of the loop,
4024b88c807SRodney W. Grimes 		 * otherwise try to unlink the node and try again. if that
4034b88c807SRodney W. Grimes 		 * fails check the full path and try a final time.
4044b88c807SRodney W. Grimes 		 */
4054b88c807SRodney W. Grimes 		if (res == 0)
4064b88c807SRodney W. Grimes 			break;
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes 		/*
4094b88c807SRodney W. Grimes 		 * we failed to make the node
4104b88c807SRodney W. Grimes 		 */
4114b88c807SRodney W. Grimes 		oerrno = errno;
4124b88c807SRodney W. Grimes 		if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0)
4134b88c807SRodney W. Grimes 			return(-1);
4144b88c807SRodney W. Grimes 
4154b88c807SRodney W. Grimes 		if (++pass <= 1)
4164b88c807SRodney W. Grimes 			continue;
4174b88c807SRodney W. Grimes 
418b1787decSKris Kennaway 		if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
419778766feSKris Kennaway 			syswarn(1, oerrno, "Could not create: %s", arcn->name);
4204b88c807SRodney W. Grimes 			return(-1);
4214b88c807SRodney W. Grimes 		}
4224b88c807SRodney W. Grimes 	}
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes 	/*
4254b88c807SRodney W. Grimes 	 * we were able to create the node. set uid/gid, modes and times
4264b88c807SRodney W. Grimes 	 */
4274b88c807SRodney W. Grimes 	if (pids)
428b1787decSKris Kennaway 		res = ((arcn->type == PAX_SLK) ?
429b1787decSKris Kennaway 		    set_lids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid) :
430b1787decSKris Kennaway 		    set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid));
4314b88c807SRodney W. Grimes 	else
4324b88c807SRodney W. Grimes 		res = 0;
4334b88c807SRodney W. Grimes 
4344b88c807SRodney W. Grimes 	/*
435b1787decSKris Kennaway 	 * symlinks are done now.
436b1787decSKris Kennaway 	 */
437b1787decSKris Kennaway 	if (arcn->type == PAX_SLK)
438b1787decSKris Kennaway 		return(0);
439b1787decSKris Kennaway 
440b1787decSKris Kennaway 	/*
4414b88c807SRodney W. Grimes 	 * IMPORTANT SECURITY NOTE:
4424b88c807SRodney W. Grimes 	 * if not preserving mode or we cannot set uid/gid, then PROHIBIT any
4434b88c807SRodney W. Grimes 	 * set uid/gid bits
4444b88c807SRodney W. Grimes 	 */
4454b88c807SRodney W. Grimes 	if (!pmode || res)
4464b88c807SRodney W. Grimes 		arcn->sb.st_mode &= ~(SETBITS);
4474b88c807SRodney W. Grimes 	if (pmode)
4484b88c807SRodney W. Grimes 		set_pmode(arcn->name, arcn->sb.st_mode);
4494b88c807SRodney W. Grimes 
450b1787decSKris Kennaway 	if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) {
4514b88c807SRodney W. Grimes 		/*
4524b88c807SRodney W. Grimes 		 * Dirs must be processed again at end of extract to set times
4534b88c807SRodney W. Grimes 		 * and modes to agree with those stored in the archive. However
4544b88c807SRodney W. Grimes 		 * to allow extract to continue, we may have to also set owner
4554b88c807SRodney W. Grimes 		 * rights. This allows nodes in the archive that are children
4564b88c807SRodney W. Grimes 		 * of this directory to be extracted without failure. Both time
4574b88c807SRodney W. Grimes 		 * and modes will be fixed after the entire archive is read and
4584b88c807SRodney W. Grimes 		 * before pax exits.
4594b88c807SRodney W. Grimes 		 */
4604b88c807SRodney W. Grimes 		if (access(arcn->name, R_OK | W_OK | X_OK) < 0) {
4614b88c807SRodney W. Grimes 			if (lstat(arcn->name, &sb) < 0) {
462778766feSKris Kennaway 				syswarn(0, errno,"Could not access %s (stat)",
4634b88c807SRodney W. Grimes 				    arcn->name);
4644b88c807SRodney W. Grimes 				set_pmode(arcn->name,file_mode | S_IRWXU);
4654b88c807SRodney W. Grimes 			} else {
4664b88c807SRodney W. Grimes 				/*
4674b88c807SRodney W. Grimes 				 * We have to add rights to the dir, so we make
4684b88c807SRodney W. Grimes 				 * sure to restore the mode. The mode must be
4694b88c807SRodney W. Grimes 				 * restored AS CREATED and not as stored if
4704b88c807SRodney W. Grimes 				 * pmode is not set.
4714b88c807SRodney W. Grimes 				 */
4724b88c807SRodney W. Grimes 				set_pmode(arcn->name,
4734b88c807SRodney W. Grimes 				    ((sb.st_mode & FILEBITS) | S_IRWXU));
4744b88c807SRodney W. Grimes 				if (!pmode)
4754b88c807SRodney W. Grimes 					arcn->sb.st_mode = sb.st_mode;
4764b88c807SRodney W. Grimes 			}
4774b88c807SRodney W. Grimes 
4784b88c807SRodney W. Grimes 			/*
4794b88c807SRodney W. Grimes 			 * we have to force the mode to what was set here,
4804b88c807SRodney W. Grimes 			 * since we changed it from the default as created.
4814b88c807SRodney W. Grimes 			 */
4824b88c807SRodney W. Grimes 			add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1);
4834b88c807SRodney W. Grimes 		} else if (pmode || patime || pmtime)
4844b88c807SRodney W. Grimes 			add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0);
4854b88c807SRodney W. Grimes 	}
4864b88c807SRodney W. Grimes 
4874b88c807SRodney W. Grimes 	if (patime || pmtime)
4884b88c807SRodney W. Grimes 		set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
4894b88c807SRodney W. Grimes 	return(0);
4904b88c807SRodney W. Grimes }
4914b88c807SRodney W. Grimes 
4924b88c807SRodney W. Grimes /*
4934b88c807SRodney W. Grimes  * unlnk_exist()
4944b88c807SRodney W. Grimes  *	Remove node from file system with the specified name. We pass the type
4954b88c807SRodney W. Grimes  *	of the node that is going to replace it. When we try to create a
4964b88c807SRodney W. Grimes  *	directory and find that it already exists, we allow processing to
4974b88c807SRodney W. Grimes  *	continue as proper modes etc will always be set for it later on.
4984b88c807SRodney W. Grimes  * Return:
4994b88c807SRodney W. Grimes  *	0 is ok to proceed, no file with the specified name exists
5004b88c807SRodney W. Grimes  *	-1 we were unable to remove the node, or we should not remove it (-k)
5014b88c807SRodney W. Grimes  *	1 we found a directory and we were going to create a directory.
5024b88c807SRodney W. Grimes  */
5034b88c807SRodney W. Grimes 
5044b88c807SRodney W. Grimes int
505f789b261SWarner Losh unlnk_exist(char *name, int type)
5064b88c807SRodney W. Grimes {
5074b88c807SRodney W. Grimes 	struct stat sb;
5084b88c807SRodney W. Grimes 
5094b88c807SRodney W. Grimes 	/*
5104b88c807SRodney W. Grimes 	 * the file does not exist, or -k we are done
5114b88c807SRodney W. Grimes 	 */
5124b88c807SRodney W. Grimes 	if (lstat(name, &sb) < 0)
5134b88c807SRodney W. Grimes 		return(0);
5144b88c807SRodney W. Grimes 	if (kflag)
5154b88c807SRodney W. Grimes 		return(-1);
5164b88c807SRodney W. Grimes 
5174b88c807SRodney W. Grimes 	if (S_ISDIR(sb.st_mode)) {
5184b88c807SRodney W. Grimes 		/*
5194b88c807SRodney W. Grimes 		 * try to remove a directory, if it fails and we were going to
5204b88c807SRodney W. Grimes 		 * create a directory anyway, tell the caller (return a 1)
5214b88c807SRodney W. Grimes 		 */
5224b88c807SRodney W. Grimes 		if (rmdir(name) < 0) {
5234b88c807SRodney W. Grimes 			if (type == PAX_DIR)
5244b88c807SRodney W. Grimes 				return(1);
525778766feSKris Kennaway 			syswarn(1,errno,"Unable to remove directory %s", name);
5264b88c807SRodney W. Grimes 			return(-1);
5274b88c807SRodney W. Grimes 		}
5284b88c807SRodney W. Grimes 		return(0);
5294b88c807SRodney W. Grimes 	}
5304b88c807SRodney W. Grimes 
5314b88c807SRodney W. Grimes 	/*
5324b88c807SRodney W. Grimes 	 * try to get rid of all non-directory type nodes
5334b88c807SRodney W. Grimes 	 */
5344b88c807SRodney W. Grimes 	if (unlink(name) < 0) {
535778766feSKris Kennaway 		syswarn(1, errno, "Could not unlink %s", name);
5364b88c807SRodney W. Grimes 		return(-1);
5374b88c807SRodney W. Grimes 	}
5384b88c807SRodney W. Grimes 	return(0);
5394b88c807SRodney W. Grimes }
5404b88c807SRodney W. Grimes 
5414b88c807SRodney W. Grimes /*
5424b88c807SRodney W. Grimes  * chk_path()
5434b88c807SRodney W. Grimes  *	We were trying to create some kind of node in the file system and it
5444b88c807SRodney W. Grimes  *	failed. chk_path() makes sure the path up to the node exists and is
5454b88c807SRodney W. Grimes  *	writeable. When we have to create a directory that is missing along the
5464b88c807SRodney W. Grimes  *	path somewhere, the directory we create will be set to the same
5474b88c807SRodney W. Grimes  *	uid/gid as the file has (when uid and gid are being preserved).
5484b88c807SRodney W. Grimes  *	NOTE: this routine is a real performance loss. It is only used as a
5494b88c807SRodney W. Grimes  *	last resort when trying to create entries in the file system.
5504b88c807SRodney W. Grimes  * Return:
5514b88c807SRodney W. Grimes  *	-1 when it could find nothing it is allowed to fix.
5524b88c807SRodney W. Grimes  *	0 otherwise
5534b88c807SRodney W. Grimes  */
5544b88c807SRodney W. Grimes 
5554b88c807SRodney W. Grimes int
556f789b261SWarner Losh chk_path( char *name, uid_t st_uid, gid_t st_gid)
5574b88c807SRodney W. Grimes {
558f789b261SWarner Losh 	char *spt = name;
5594b88c807SRodney W. Grimes 	struct stat sb;
5604b88c807SRodney W. Grimes 	int retval = -1;
5614b88c807SRodney W. Grimes 
5624b88c807SRodney W. Grimes 	/*
5634b88c807SRodney W. Grimes 	 * watch out for paths with nodes stored directly in / (e.g. /bozo)
5644b88c807SRodney W. Grimes 	 */
5654b88c807SRodney W. Grimes 	if (*spt == '/')
5664b88c807SRodney W. Grimes 		++spt;
5674b88c807SRodney W. Grimes 
5684b88c807SRodney W. Grimes 	for(;;) {
5694b88c807SRodney W. Grimes 		/*
5704b88c807SRodney W. Grimes 		 * work foward from the first / and check each part of the path
5714b88c807SRodney W. Grimes 		 */
5724b88c807SRodney W. Grimes 		spt = strchr(spt, '/');
5734b88c807SRodney W. Grimes 		if (spt == NULL)
5744b88c807SRodney W. Grimes 			break;
5754b88c807SRodney W. Grimes 		*spt = '\0';
5764b88c807SRodney W. Grimes 
5774b88c807SRodney W. Grimes 		/*
5784b88c807SRodney W. Grimes 		 * if it exists we assume it is a directory, it is not within
5794b88c807SRodney W. Grimes 		 * the spec (at least it seems to read that way) to alter the
5804b88c807SRodney W. Grimes 		 * file system for nodes NOT EXPLICITLY stored on the archive.
5814b88c807SRodney W. Grimes 		 * If that assumption is changed, you would test the node here
5824b88c807SRodney W. Grimes 		 * and figure out how to get rid of it (probably like some
5834b88c807SRodney W. Grimes 		 * recursive unlink()) or fix up the directory permissions if
5844b88c807SRodney W. Grimes 		 * required (do an access()).
5854b88c807SRodney W. Grimes 		 */
5864b88c807SRodney W. Grimes 		if (lstat(name, &sb) == 0) {
5874b88c807SRodney W. Grimes 			*(spt++) = '/';
5884b88c807SRodney W. Grimes 			continue;
5894b88c807SRodney W. Grimes 		}
5904b88c807SRodney W. Grimes 
5914b88c807SRodney W. Grimes 		/*
5924b88c807SRodney W. Grimes 		 * the path fails at this point, see if we can create the
5934b88c807SRodney W. Grimes 		 * needed directory and continue on
5944b88c807SRodney W. Grimes 		 */
5954b88c807SRodney W. Grimes 		if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
5964b88c807SRodney W. Grimes 			*spt = '/';
5974b88c807SRodney W. Grimes 			retval = -1;
5984b88c807SRodney W. Grimes 			break;
5994b88c807SRodney W. Grimes 		}
6004b88c807SRodney W. Grimes 
6014b88c807SRodney W. Grimes 		/*
6024b88c807SRodney W. Grimes 		 * we were able to create the directory. We will tell the
6034b88c807SRodney W. Grimes 		 * caller that we found something to fix, and it is ok to try
6044b88c807SRodney W. Grimes 		 * and create the node again.
6054b88c807SRodney W. Grimes 		 */
6064b88c807SRodney W. Grimes 		retval = 0;
6074b88c807SRodney W. Grimes 		if (pids)
6084b88c807SRodney W. Grimes 			(void)set_ids(name, st_uid, st_gid);
6094b88c807SRodney W. Grimes 
6104b88c807SRodney W. Grimes 		/*
6114b88c807SRodney W. Grimes 		 * make sure the user doen't have some strange umask that
6124b88c807SRodney W. Grimes 		 * causes this newly created directory to be unusable. We fix
6134b88c807SRodney W. Grimes 		 * the modes and restore them back to the creation default at
6144b88c807SRodney W. Grimes 		 * the end of pax
6154b88c807SRodney W. Grimes 		 */
6164b88c807SRodney W. Grimes 		if ((access(name, R_OK | W_OK | X_OK) < 0) &&
6174b88c807SRodney W. Grimes 		    (lstat(name, &sb) == 0)) {
6184b88c807SRodney W. Grimes 			set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU));
6194b88c807SRodney W. Grimes 			add_dir(name, spt - name, &sb, 1);
6204b88c807SRodney W. Grimes 		}
6214b88c807SRodney W. Grimes 		*(spt++) = '/';
6224b88c807SRodney W. Grimes 		continue;
6234b88c807SRodney W. Grimes 	}
6244b88c807SRodney W. Grimes 	return(retval);
6254b88c807SRodney W. Grimes }
6264b88c807SRodney W. Grimes 
6274b88c807SRodney W. Grimes /*
6284b88c807SRodney W. Grimes  * set_ftime()
6294b88c807SRodney W. Grimes  *	Set the access time and modification time for a named file. If frc is
630778766feSKris Kennaway  *	non-zero we force these times to be set even if the user did not
6314b88c807SRodney W. Grimes  *	request access and/or modification time preservation (this is also
6324b88c807SRodney W. Grimes  *	used by -t to reset access times).
6334b88c807SRodney W. Grimes  *	When ign is zero, only those times the user has asked for are set, the
6344b88c807SRodney W. Grimes  *	other ones are left alone. We do not assume the un-documented feature
6354b88c807SRodney W. Grimes  *	of many utimes() implementations that consider a 0 time value as a do
6364b88c807SRodney W. Grimes  *	not set request.
6374b88c807SRodney W. Grimes  */
6384b88c807SRodney W. Grimes 
6394b88c807SRodney W. Grimes void
6404b88c807SRodney W. Grimes set_ftime(char *fnm, time_t mtime, time_t atime, int frc)
6414b88c807SRodney W. Grimes {
6424b88c807SRodney W. Grimes 	static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}};
6434b88c807SRodney W. Grimes 	struct stat sb;
6444b88c807SRodney W. Grimes 
6456e63a104SMatthew Dillon 	tv[0].tv_sec = atime;
6466e63a104SMatthew Dillon 	tv[1].tv_sec = mtime;
6474b88c807SRodney W. Grimes 	if (!frc && (!patime || !pmtime)) {
6484b88c807SRodney W. Grimes 		/*
6494b88c807SRodney W. Grimes 		 * if we are not forcing, only set those times the user wants
6504b88c807SRodney W. Grimes 		 * set. We get the current values of the times if we need them.
6514b88c807SRodney W. Grimes 		 */
6524b88c807SRodney W. Grimes 		if (lstat(fnm, &sb) == 0) {
6534b88c807SRodney W. Grimes 			if (!patime)
6546e63a104SMatthew Dillon 				tv[0].tv_sec = sb.st_atime;
6554b88c807SRodney W. Grimes 			if (!pmtime)
6566e63a104SMatthew Dillon 				tv[1].tv_sec = sb.st_mtime;
6574b88c807SRodney W. Grimes 		} else
658778766feSKris Kennaway 			syswarn(0,errno,"Unable to obtain file stats %s", fnm);
6594b88c807SRodney W. Grimes 	}
6604b88c807SRodney W. Grimes 
6614b88c807SRodney W. Grimes 	/*
6624b88c807SRodney W. Grimes 	 * set the times
6634b88c807SRodney W. Grimes 	 */
6644b88c807SRodney W. Grimes 	if (utimes(fnm, tv) < 0)
665778766feSKris Kennaway 		syswarn(1, errno, "Access/modification time set failed on: %s",
6664b88c807SRodney W. Grimes 		    fnm);
6674b88c807SRodney W. Grimes 	return;
6684b88c807SRodney W. Grimes }
6694b88c807SRodney W. Grimes 
6704b88c807SRodney W. Grimes /*
6714b88c807SRodney W. Grimes  * set_ids()
6724b88c807SRodney W. Grimes  *	set the uid and gid of a file system node
6734b88c807SRodney W. Grimes  * Return:
6744b88c807SRodney W. Grimes  *	0 when set, -1 on failure
6754b88c807SRodney W. Grimes  */
6764b88c807SRodney W. Grimes 
6774b88c807SRodney W. Grimes int
6784b88c807SRodney W. Grimes set_ids(char *fnm, uid_t uid, gid_t gid)
6794b88c807SRodney W. Grimes {
6804b88c807SRodney W. Grimes 	if (chown(fnm, uid, gid) < 0) {
681b1787decSKris Kennaway 		/*
682b1787decSKris Kennaway 		 * ignore EPERM unless in verbose mode or being run by root.
683b1787decSKris Kennaway 		 * if running as pax, POSIX requires a warning.
684b1787decSKris Kennaway 		 */
685b1787decSKris Kennaway 		if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag ||
686b1787decSKris Kennaway 		    geteuid() == 0)
687b1787decSKris Kennaway 			syswarn(1, errno, "Unable to set file uid/gid of %s",
688b1787decSKris Kennaway 			    fnm);
689b1787decSKris Kennaway 		return(-1);
690b1787decSKris Kennaway 	}
691b1787decSKris Kennaway 	return(0);
692b1787decSKris Kennaway }
693b1787decSKris Kennaway 
694b1787decSKris Kennaway /*
695b1787decSKris Kennaway  * set_lids()
696b1787decSKris Kennaway  *	set the uid and gid of a file system node
697b1787decSKris Kennaway  * Return:
698b1787decSKris Kennaway  *	0 when set, -1 on failure
699b1787decSKris Kennaway  */
700b1787decSKris Kennaway 
701b1787decSKris Kennaway int
702b1787decSKris Kennaway set_lids(char *fnm, uid_t uid, gid_t gid)
703b1787decSKris Kennaway {
704b1787decSKris Kennaway 	if (lchown(fnm, uid, gid) < 0) {
705b1787decSKris Kennaway 		/*
706b1787decSKris Kennaway 		 * ignore EPERM unless in verbose mode or being run by root.
707b1787decSKris Kennaway 		 * if running as pax, POSIX requires a warning.
708b1787decSKris Kennaway 		 */
709b1787decSKris Kennaway 		if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag ||
710b1787decSKris Kennaway 		    geteuid() == 0)
711b1787decSKris Kennaway 			syswarn(1, errno, "Unable to set file uid/gid of %s",
712b1787decSKris Kennaway 			    fnm);
7134b88c807SRodney W. Grimes 		return(-1);
7144b88c807SRodney W. Grimes 	}
7154b88c807SRodney W. Grimes 	return(0);
7164b88c807SRodney W. Grimes }
7174b88c807SRodney W. Grimes 
7184b88c807SRodney W. Grimes /*
7194b88c807SRodney W. Grimes  * set_pmode()
7204b88c807SRodney W. Grimes  *	Set file access mode
7214b88c807SRodney W. Grimes  */
7224b88c807SRodney W. Grimes 
7234b88c807SRodney W. Grimes void
7244b88c807SRodney W. Grimes set_pmode(char *fnm, mode_t mode)
7254b88c807SRodney W. Grimes {
7264b88c807SRodney W. Grimes 	mode &= ABITS;
7274b88c807SRodney W. Grimes 	if (chmod(fnm, mode) < 0)
728778766feSKris Kennaway 		syswarn(1, errno, "Could not set permissions on %s", fnm);
7294b88c807SRodney W. Grimes 	return;
7304b88c807SRodney W. Grimes }
7314b88c807SRodney W. Grimes 
7324b88c807SRodney W. Grimes /*
7334b88c807SRodney W. Grimes  * file_write()
7344b88c807SRodney W. Grimes  *	Write/copy a file (during copy or archive extract). This routine knows
7354b88c807SRodney W. Grimes  *	how to copy files with lseek holes in it. (Which are read as file
7364b88c807SRodney W. Grimes  *	blocks containing all 0's but do not have any file blocks associated
7374b88c807SRodney W. Grimes  *	with the data). Typical examples of these are files created by dbm
7384b88c807SRodney W. Grimes  *	variants (.pag files). While the file size of these files are huge, the
7394b88c807SRodney W. Grimes  *	actual storage is quite small (the files are sparse). The problem is
7404b88c807SRodney W. Grimes  *	the holes read as all zeros so are probably stored on the archive that
7414b88c807SRodney W. Grimes  *	way (there is no way to determine if the file block is really a hole,
7424b88c807SRodney W. Grimes  *	we only know that a file block of all zero's can be a hole).
7434b88c807SRodney W. Grimes  *	At this writing, no major archive format knows how to archive files
7444b88c807SRodney W. Grimes  *	with holes. However, on extraction (or during copy, -rw) we have to
7454b88c807SRodney W. Grimes  *	deal with these files. Without detecting the holes, the files can
7464b88c807SRodney W. Grimes  *	consume a lot of file space if just written to disk. This replacement
7474b88c807SRodney W. Grimes  *	for write when passed the basic allocation size of a file system block,
7484b88c807SRodney W. Grimes  *	uses lseek whenever it detects the input data is all 0 within that
7494b88c807SRodney W. Grimes  *	file block. In more detail, the strategy is as follows:
7504b88c807SRodney W. Grimes  *	While the input is all zero keep doing an lseek. Keep track of when we
7514b88c807SRodney W. Grimes  *	pass over file block boundries. Only write when we hit a non zero
7524b88c807SRodney W. Grimes  *	input. once we have written a file block, we continue to write it to
7534b88c807SRodney W. Grimes  *	the end (we stop looking at the input). When we reach the start of the
7544b88c807SRodney W. Grimes  *	next file block, start checking for zero blocks again. Working on file
7554b88c807SRodney W. Grimes  *	block boundries significantly reduces the overhead when copying files
7564b88c807SRodney W. Grimes  *	that are NOT very sparse. This overhead (when compared to a write) is
7574b88c807SRodney W. Grimes  *	almost below the measurement resolution on many systems. Without it,
7584b88c807SRodney W. Grimes  *	files with holes cannot be safely copied. It does has a side effect as
7594b88c807SRodney W. Grimes  *	it can put holes into files that did not have them before, but that is
7604b88c807SRodney W. Grimes  *	not a problem since the file contents are unchanged (in fact it saves
7614b88c807SRodney W. Grimes  *	file space). (Except on paging files for diskless clients. But since we
7624b88c807SRodney W. Grimes  *	cannot determine one of those file from here, we ignore them). If this
7634b88c807SRodney W. Grimes  *	ever ends up on a system where CTG files are supported and the holes
7644b88c807SRodney W. Grimes  *	are not desired, just do a conditional test in those routines that
7654b88c807SRodney W. Grimes  *	call file_write() and have it call write() instead. BEFORE CLOSING THE
7664b88c807SRodney W. Grimes  *	FILE, make sure to call file_flush() when the last write finishes with
7674b88c807SRodney W. Grimes  *	an empty block. A lot of file systems will not create an lseek hole at
7684b88c807SRodney W. Grimes  *	the end. In this case we drop a single 0 at the end to force the
7694b88c807SRodney W. Grimes  *	trailing 0's in the file.
7704b88c807SRodney W. Grimes  *	---Parameters---
7714b88c807SRodney W. Grimes  *	rem: how many bytes left in this file system block
7724b88c807SRodney W. Grimes  *	isempt: have we written to the file block yet (is it empty)
7734b88c807SRodney W. Grimes  *	sz: basic file block allocation size
7744b88c807SRodney W. Grimes  *	cnt: number of bytes on this write
7754b88c807SRodney W. Grimes  *	str: buffer to write
7764b88c807SRodney W. Grimes  * Return:
7774b88c807SRodney W. Grimes  *	number of bytes written, -1 on write (or lseek) error.
7784b88c807SRodney W. Grimes  */
7794b88c807SRodney W. Grimes 
7804b88c807SRodney W. Grimes int
781f789b261SWarner Losh file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz,
7824b88c807SRodney W. Grimes 	char *name)
7834b88c807SRodney W. Grimes {
784f789b261SWarner Losh 	char *pt;
785f789b261SWarner Losh 	char *end;
786f789b261SWarner Losh 	int wcnt;
787f789b261SWarner Losh 	char *st = str;
7884b88c807SRodney W. Grimes 
7894b88c807SRodney W. Grimes 	/*
7904b88c807SRodney W. Grimes 	 * while we have data to process
7914b88c807SRodney W. Grimes 	 */
7924b88c807SRodney W. Grimes 	while (cnt) {
7934b88c807SRodney W. Grimes 		if (!*rem) {
7944b88c807SRodney W. Grimes 			/*
7954b88c807SRodney W. Grimes 			 * We are now at the start of file system block again
7964b88c807SRodney W. Grimes 			 * (or what we think one is...). start looking for
7974b88c807SRodney W. Grimes 			 * empty blocks again
7984b88c807SRodney W. Grimes 			 */
7994b88c807SRodney W. Grimes 			*isempt = 1;
8004b88c807SRodney W. Grimes 			*rem = sz;
8014b88c807SRodney W. Grimes 		}
8024b88c807SRodney W. Grimes 
8034b88c807SRodney W. Grimes 		/*
8044b88c807SRodney W. Grimes 		 * only examine up to the end of the current file block or
8054b88c807SRodney W. Grimes 		 * remaining characters to write, whatever is smaller
8064b88c807SRodney W. Grimes 		 */
8074b88c807SRodney W. Grimes 		wcnt = MIN(cnt, *rem);
8084b88c807SRodney W. Grimes 		cnt -= wcnt;
8094b88c807SRodney W. Grimes 		*rem -= wcnt;
8104b88c807SRodney W. Grimes 		if (*isempt) {
8114b88c807SRodney W. Grimes 			/*
8124b88c807SRodney W. Grimes 			 * have not written to this block yet, so we keep
8134b88c807SRodney W. Grimes 			 * looking for zero's
8144b88c807SRodney W. Grimes 			 */
8154b88c807SRodney W. Grimes 			pt = st;
8164b88c807SRodney W. Grimes 			end = st + wcnt;
8174b88c807SRodney W. Grimes 
8184b88c807SRodney W. Grimes 			/*
8194b88c807SRodney W. Grimes 			 * look for a zero filled buffer
8204b88c807SRodney W. Grimes 			 */
8214b88c807SRodney W. Grimes 			while ((pt < end) && (*pt == '\0'))
8224b88c807SRodney W. Grimes 				++pt;
8234b88c807SRodney W. Grimes 
8244b88c807SRodney W. Grimes 			if (pt == end) {
8254b88c807SRodney W. Grimes 				/*
8264b88c807SRodney W. Grimes 				 * skip, buf is empty so far
8274b88c807SRodney W. Grimes 				 */
8284b88c807SRodney W. Grimes 				if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
829778766feSKris Kennaway 					syswarn(1,errno,"File seek on %s",
8304b88c807SRodney W. Grimes 					    name);
8314b88c807SRodney W. Grimes 					return(-1);
8324b88c807SRodney W. Grimes 				}
8334b88c807SRodney W. Grimes 				st = pt;
8344b88c807SRodney W. Grimes 				continue;
8354b88c807SRodney W. Grimes 			}
8364b88c807SRodney W. Grimes 			/*
8374b88c807SRodney W. Grimes 			 * drat, the buf is not zero filled
8384b88c807SRodney W. Grimes 			 */
8394b88c807SRodney W. Grimes 			*isempt = 0;
8404b88c807SRodney W. Grimes 		}
8414b88c807SRodney W. Grimes 
8424b88c807SRodney W. Grimes 		/*
8434b88c807SRodney W. Grimes 		 * have non-zero data in this file system block, have to write
8444b88c807SRodney W. Grimes 		 */
8454b88c807SRodney W. Grimes 		if (write(fd, st, wcnt) != wcnt) {
846778766feSKris Kennaway 			syswarn(1, errno, "Failed write to file %s", name);
8474b88c807SRodney W. Grimes 			return(-1);
8484b88c807SRodney W. Grimes 		}
8494b88c807SRodney W. Grimes 		st += wcnt;
8504b88c807SRodney W. Grimes 	}
8514b88c807SRodney W. Grimes 	return(st - str);
8524b88c807SRodney W. Grimes }
8534b88c807SRodney W. Grimes 
8544b88c807SRodney W. Grimes /*
8554b88c807SRodney W. Grimes  * file_flush()
8564b88c807SRodney W. Grimes  *	when the last file block in a file is zero, many file systems will not
8574b88c807SRodney W. Grimes  *	let us create a hole at the end. To get the last block with zeros, we
8584b88c807SRodney W. Grimes  *	write the last BYTE with a zero (back up one byte and write a zero).
8594b88c807SRodney W. Grimes  */
8604b88c807SRodney W. Grimes 
8614b88c807SRodney W. Grimes void
8624b88c807SRodney W. Grimes file_flush(int fd, char *fname, int isempt)
8634b88c807SRodney W. Grimes {
8644b88c807SRodney W. Grimes 	static char blnk[] = "\0";
8654b88c807SRodney W. Grimes 
8664b88c807SRodney W. Grimes 	/*
8674b88c807SRodney W. Grimes 	 * silly test, but make sure we are only called when the last block is
8684b88c807SRodney W. Grimes 	 * filled with all zeros.
8694b88c807SRodney W. Grimes 	 */
8704b88c807SRodney W. Grimes 	if (!isempt)
8714b88c807SRodney W. Grimes 		return;
8724b88c807SRodney W. Grimes 
8734b88c807SRodney W. Grimes 	/*
8744b88c807SRodney W. Grimes 	 * move back one byte and write a zero
8754b88c807SRodney W. Grimes 	 */
8764b88c807SRodney W. Grimes 	if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
877778766feSKris Kennaway 		syswarn(1, errno, "Failed seek on file %s", fname);
8784b88c807SRodney W. Grimes 		return;
8794b88c807SRodney W. Grimes 	}
8804b88c807SRodney W. Grimes 
8814b88c807SRodney W. Grimes 	if (write(fd, blnk, 1) < 0)
882778766feSKris Kennaway 		syswarn(1, errno, "Failed write to file %s", fname);
8834b88c807SRodney W. Grimes 	return;
8844b88c807SRodney W. Grimes }
8854b88c807SRodney W. Grimes 
8864b88c807SRodney W. Grimes /*
8874b88c807SRodney W. Grimes  * rdfile_close()
8884b88c807SRodney W. Grimes  *	close a file we have beed reading (to copy or archive). If we have to
8894b88c807SRodney W. Grimes  *	reset access time (tflag) do so (the times are stored in arcn).
8904b88c807SRodney W. Grimes  */
8914b88c807SRodney W. Grimes 
8924b88c807SRodney W. Grimes void
893f789b261SWarner Losh rdfile_close(ARCHD *arcn, int *fd)
8944b88c807SRodney W. Grimes {
8954b88c807SRodney W. Grimes 	/*
8964b88c807SRodney W. Grimes 	 * make sure the file is open
8974b88c807SRodney W. Grimes 	 */
8984b88c807SRodney W. Grimes 	if (*fd < 0)
8994b88c807SRodney W. Grimes 		return;
9004b88c807SRodney W. Grimes 
9014b88c807SRodney W. Grimes 	(void)close(*fd);
9024b88c807SRodney W. Grimes 	*fd = -1;
9034b88c807SRodney W. Grimes 	if (!tflag)
9044b88c807SRodney W. Grimes 		return;
9054b88c807SRodney W. Grimes 
9064b88c807SRodney W. Grimes 	/*
9074b88c807SRodney W. Grimes 	 * user wants last access time reset
9084b88c807SRodney W. Grimes 	 */
9094b88c807SRodney W. Grimes 	set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1);
9104b88c807SRodney W. Grimes 	return;
9114b88c807SRodney W. Grimes }
9124b88c807SRodney W. Grimes 
9134b88c807SRodney W. Grimes /*
9144b88c807SRodney W. Grimes  * set_crc()
9154b88c807SRodney W. Grimes  *	read a file to calculate its crc. This is a real drag. Archive formats
9164b88c807SRodney W. Grimes  *	that have this, end up reading the file twice (we have to write the
9174b88c807SRodney W. Grimes  *	header WITH the crc before writing the file contents. Oh well...
9184b88c807SRodney W. Grimes  * Return:
9194b88c807SRodney W. Grimes  *	0 if was able to calculate the crc, -1 otherwise
9204b88c807SRodney W. Grimes  */
9214b88c807SRodney W. Grimes 
9224b88c807SRodney W. Grimes int
923f789b261SWarner Losh set_crc(ARCHD *arcn, int fd)
9244b88c807SRodney W. Grimes {
925f789b261SWarner Losh 	int i;
926f789b261SWarner Losh 	int res;
9274b88c807SRodney W. Grimes 	off_t cpcnt = 0L;
9284b88c807SRodney W. Grimes 	u_long size;
9294b88c807SRodney W. Grimes 	unsigned long crc = 0L;
9304b88c807SRodney W. Grimes 	char tbuf[FILEBLK];
9314b88c807SRodney W. Grimes 	struct stat sb;
9324b88c807SRodney W. Grimes 
9334b88c807SRodney W. Grimes 	if (fd < 0) {
9344b88c807SRodney W. Grimes 		/*
9354b88c807SRodney W. Grimes 		 * hmm, no fd, should never happen. well no crc then.
9364b88c807SRodney W. Grimes 		 */
9374b88c807SRodney W. Grimes 		arcn->crc = 0L;
9384b88c807SRodney W. Grimes 		return(0);
9394b88c807SRodney W. Grimes 	}
9404b88c807SRodney W. Grimes 
9414b88c807SRodney W. Grimes 	if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf))
9424b88c807SRodney W. Grimes 		size = (u_long)sizeof(tbuf);
9434b88c807SRodney W. Grimes 
9444b88c807SRodney W. Grimes 	/*
9454b88c807SRodney W. Grimes 	 * read all the bytes we think that there are in the file. If the user
9464b88c807SRodney W. Grimes 	 * is trying to archive an active file, forget this file.
9474b88c807SRodney W. Grimes 	 */
9484b88c807SRodney W. Grimes 	for(;;) {
9494b88c807SRodney W. Grimes 		if ((res = read(fd, tbuf, size)) <= 0)
9504b88c807SRodney W. Grimes 			break;
9514b88c807SRodney W. Grimes 		cpcnt += res;
9524b88c807SRodney W. Grimes 		for (i = 0; i < res; ++i)
9534b88c807SRodney W. Grimes 			crc += (tbuf[i] & 0xff);
9544b88c807SRodney W. Grimes 	}
9554b88c807SRodney W. Grimes 
9564b88c807SRodney W. Grimes 	/*
9574b88c807SRodney W. Grimes 	 * safety check. we want to avoid archiving files that are active as
9584b88c807SRodney W. Grimes 	 * they can create inconsistant archive copies.
9594b88c807SRodney W. Grimes 	 */
9604b88c807SRodney W. Grimes 	if (cpcnt != arcn->sb.st_size)
961778766feSKris Kennaway 		paxwarn(1, "File changed size %s", arcn->org_name);
9624b88c807SRodney W. Grimes 	else if (fstat(fd, &sb) < 0)
963778766feSKris Kennaway 		syswarn(1, errno, "Failed stat on %s", arcn->org_name);
9644b88c807SRodney W. Grimes 	else if (arcn->sb.st_mtime != sb.st_mtime)
965778766feSKris Kennaway 		paxwarn(1, "File %s was modified during read", arcn->org_name);
9664b88c807SRodney W. Grimes 	else if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
967778766feSKris Kennaway 		syswarn(1, errno, "File rewind failed on: %s", arcn->org_name);
9684b88c807SRodney W. Grimes 	else {
9694b88c807SRodney W. Grimes 		arcn->crc = crc;
9704b88c807SRodney W. Grimes 		return(0);
9714b88c807SRodney W. Grimes 	}
9724b88c807SRodney W. Grimes 	return(-1);
9734b88c807SRodney W. Grimes }
974