Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.50
      1  1.50     lukem /*	$NetBSD: xinstall.c,v 1.50 2001/10/11 02:06:32 lukem Exp $	*/
      2   1.5       jtc 
      3   1.1       cgd /*
      4   1.5       jtc  * Copyright (c) 1987, 1993
      5   1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.20       mrg #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.20       mrg __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
     39  1.20       mrg 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.5       jtc #if 0
     44   1.5       jtc static char sccsid[] = "@(#)xinstall.c	8.1 (Berkeley) 7/21/93";
     45  1.20       mrg #else
     46  1.50     lukem __RCSID("$NetBSD: xinstall.c,v 1.50 2001/10/11 02:06:32 lukem Exp $");
     47   1.5       jtc #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd #include <sys/param.h>
     51   1.5       jtc #include <sys/wait.h>
     52   1.5       jtc #include <sys/mman.h>
     53   1.1       cgd #include <sys/stat.h>
     54   1.5       jtc 
     55   1.5       jtc #include <ctype.h>
     56  1.50     lukem #include <err.h>
     57   1.5       jtc #include <errno.h>
     58   1.5       jtc #include <fcntl.h>
     59   1.1       cgd #include <grp.h>
     60   1.5       jtc #include <paths.h>
     61   1.1       cgd #include <pwd.h>
     62   1.1       cgd #include <stdio.h>
     63   1.5       jtc #include <stdlib.h>
     64   1.5       jtc #include <string.h>
     65   1.5       jtc #include <unistd.h>
     66  1.50     lukem #include <vis.h>
     67   1.5       jtc 
     68   1.1       cgd #include "pathnames.h"
     69  1.29       mrg #include "stat_flags.h"
     70   1.1       cgd 
     71  1.28  wsanchez #define STRIP_ARGS_MAX 32
     72  1.31   hubertf #define BACKUP_SUFFIX ".old"
     73  1.28  wsanchez 
     74  1.50     lukem int	dobackup, docopy, dodir, dostrip, dolink, dopreserve, dorename,
     75  1.50     lukem 	    dounpriv;
     76  1.50     lukem int	numberedbackup;
     77  1.50     lukem int	mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
     78  1.50     lukem char	pathbuf[MAXPATHLEN];
     79  1.50     lukem uid_t	uid;
     80  1.50     lukem gid_t	gid;
     81  1.50     lukem FILE	*metafp;
     82  1.50     lukem char	*metafile;
     83  1.50     lukem u_long	fileflags;
     84  1.50     lukem char	*stripArgs;
     85  1.50     lukem char	*suffix = BACKUP_SUFFIX;
     86   1.5       jtc 
     87  1.16  christos #define LN_ABSOLUTE	0x01
     88  1.16  christos #define LN_RELATIVE	0x02
     89  1.16  christos #define LN_HARD		0x04
     90  1.16  christos #define LN_SYMBOLIC	0x08
     91  1.16  christos #define LN_MIXED	0x10
     92  1.16  christos 
     93   1.5       jtc #define	DIRECTORY	0x01		/* Tell install it's a directory. */
     94   1.5       jtc #define	SETFLAGS	0x02		/* Tell install to set flags. */
     95  1.50     lukem #define	HASUID		0x04		/* Tell install the uid was given */
     96  1.50     lukem #define	HASGID		0x08		/* Tell install the gid was given */
     97   1.5       jtc 
     98  1.50     lukem void	backup(const char *);
     99  1.48    simonb void	copy(int, char *, int, char *, off_t);
    100  1.50     lukem void	install(char *, char *, u_int);
    101  1.50     lukem void	install_dir(char *, u_int);
    102  1.50     lukem int	main(int, char *[]);
    103  1.48    simonb void	makelink(char *, char *);
    104  1.50     lukem int	parseid(char *, id_t *);
    105  1.48    simonb void	strip(char *);
    106  1.50     lukem void	metadata_log(const char *, mode_t, u_int, struct timeval *);
    107  1.48    simonb void	usage(void);
    108   1.1       cgd 
    109   1.5       jtc int
    110  1.48    simonb main(int argc, char *argv[])
    111   1.1       cgd {
    112   1.1       cgd 	struct stat from_sb, to_sb;
    113  1.42     enami 	void *set;
    114   1.5       jtc 	u_int iflags;
    115   1.1       cgd 	int ch, no_target;
    116  1.16  christos 	char *p;
    117  1.16  christos 	char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
    118  1.43       cgd 
    119  1.43       cgd 	setprogname(argv[0]);
    120   1.1       cgd 
    121   1.5       jtc 	iflags = 0;
    122  1.50     lukem 	while ((ch = getopt(argc, argv, "cbB:df:g:l:m:M:o:prsS:U")) != -1)
    123   1.1       cgd 		switch((char)ch) {
    124  1.31   hubertf 		case 'B':
    125  1.31   hubertf 			suffix = optarg;
    126  1.35   hubertf 			numberedbackup = 0;
    127  1.35   hubertf 			{
    128  1.35   hubertf 				/* Check if given suffix really generates
    129  1.35   hubertf 				   different suffixes - catch e.g. ".%" */
    130  1.35   hubertf 				char suffix_expanded0[FILENAME_MAX],
    131  1.35   hubertf 				     suffix_expanded1[FILENAME_MAX];
    132  1.35   hubertf 				(void)snprintf(suffix_expanded0, FILENAME_MAX,
    133  1.35   hubertf 					       suffix, 0);
    134  1.35   hubertf 				(void)snprintf(suffix_expanded1, FILENAME_MAX,
    135  1.35   hubertf 					       suffix, 1);
    136  1.35   hubertf 				if (strcmp(suffix_expanded0, suffix_expanded1)
    137  1.35   hubertf 				    != 0)
    138  1.35   hubertf 					numberedbackup = 1;
    139  1.35   hubertf 			}
    140  1.31   hubertf 			/* fall through; -B implies -b */
    141  1.31   hubertf 		case 'b':
    142  1.31   hubertf 			dobackup = 1;
    143  1.31   hubertf 			break;
    144   1.1       cgd 		case 'c':
    145   1.1       cgd 			docopy = 1;
    146   1.1       cgd 			break;
    147  1.26  christos 		case 'd':
    148  1.26  christos 			dodir = 1;
    149  1.26  christos 			break;
    150   1.5       jtc 		case 'f':
    151   1.5       jtc 			flags = optarg;
    152  1.50     lukem 			if (string_to_flags(&flags, &fileflags, NULL))
    153   1.5       jtc 				errx(1, "%s: invalid flag", flags);
    154   1.5       jtc 			iflags |= SETFLAGS;
    155   1.5       jtc 			break;
    156   1.1       cgd 		case 'g':
    157   1.1       cgd 			group = optarg;
    158   1.1       cgd 			break;
    159  1.16  christos 		case 'l':
    160  1.16  christos 			for (p = optarg; *p; p++)
    161  1.16  christos 				switch (*p) {
    162  1.16  christos 				case 's':
    163  1.16  christos 					dolink &= ~(LN_HARD|LN_MIXED);
    164  1.16  christos 					dolink |= LN_SYMBOLIC;
    165  1.16  christos 					break;
    166  1.16  christos 				case 'h':
    167  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_MIXED);
    168  1.16  christos 					dolink |= LN_HARD;
    169  1.16  christos 					break;
    170  1.16  christos 				case 'm':
    171  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_HARD);
    172  1.16  christos 					dolink |= LN_MIXED;
    173  1.16  christos 					break;
    174  1.16  christos 				case 'a':
    175  1.16  christos 					dolink &= ~LN_RELATIVE;
    176  1.16  christos 					dolink |= LN_ABSOLUTE;
    177  1.16  christos 					break;
    178  1.16  christos 				case 'r':
    179  1.16  christos 					dolink &= ~LN_ABSOLUTE;
    180  1.16  christos 					dolink |= LN_RELATIVE;
    181  1.16  christos 					break;
    182  1.16  christos 				default:
    183  1.16  christos 					errx(1, "%c: invalid link type", *p);
    184  1.16  christos 					break;
    185  1.16  christos 				}
    186  1.16  christos 			break;
    187  1.26  christos 		case 'm':
    188  1.26  christos 			if (!(set = setmode(optarg)))
    189  1.26  christos 				errx(1, "%s: invalid file mode", optarg);
    190  1.26  christos 			mode = getmode(set, 0);
    191  1.42     enami 			free(set);
    192  1.26  christos 			break;
    193  1.50     lukem 		case 'M':
    194  1.50     lukem 			metafile = optarg;
    195  1.50     lukem 			break;
    196  1.26  christos 		case 'o':
    197  1.26  christos 			owner = optarg;
    198  1.26  christos 			break;
    199  1.26  christos 		case 'p':
    200  1.26  christos 			dopreserve = 1;
    201  1.26  christos 			break;
    202  1.33  christos 		case 'r':
    203  1.33  christos 			dorename = 1;
    204  1.33  christos 			break;
    205  1.28  wsanchez 		case 'S':
    206  1.49    simonb 			stripArgs = strdup(optarg);
    207  1.49    simonb 			if (stripArgs == NULL)
    208  1.49    simonb 				errx(1, "%s", strerror(ENOMEM));
    209  1.28  wsanchez 			/* fall through; -S implies -s */
    210  1.26  christos 		case 's':
    211  1.26  christos 			dostrip = 1;
    212  1.26  christos 			break;
    213  1.38  sommerfe 		case 'U':
    214  1.50     lukem 			dounpriv = 1;
    215  1.38  sommerfe 			break;
    216   1.1       cgd 		case '?':
    217   1.1       cgd 		default:
    218   1.1       cgd 			usage();
    219   1.1       cgd 		}
    220   1.1       cgd 	argc -= optind;
    221   1.1       cgd 	argv += optind;
    222   1.2       jtc 
    223  1.23        tv 	/* strip and link options make no sense when creating directories */
    224  1.23        tv 	if ((dostrip || dolink) && dodir)
    225  1.16  christos 		usage();
    226  1.16  christos 
    227  1.16  christos 	/* strip and flags make no sense with links */
    228  1.16  christos 	if ((dostrip || flags) && dolink)
    229   1.2       jtc 		usage();
    230   1.2       jtc 
    231   1.2       jtc 	/* must have at least two arguments, except when creating directories */
    232   1.2       jtc 	if (argc < 2 && !dodir)
    233   1.1       cgd 		usage();
    234   1.1       cgd 
    235   1.1       cgd 	/* get group and owner id's */
    236  1.50     lukem 	if (group) {
    237  1.50     lukem 		struct group *gp;
    238  1.50     lukem 
    239  1.50     lukem 		if ((gp = getgrnam(group)) != NULL)
    240  1.50     lukem 			gid = gp->gr_gid;
    241  1.50     lukem 		else if (! parseid(group, &gid))
    242  1.41       cgd 			errx(1, "unknown group %s", group);
    243  1.50     lukem 		iflags |= HASGID;
    244  1.50     lukem 	}
    245  1.50     lukem 	if (owner) {
    246  1.50     lukem 		struct passwd *pp;
    247  1.50     lukem 
    248  1.50     lukem 		if ((pp = getpwnam(owner)) != NULL)
    249  1.50     lukem 			uid = pp->pw_uid;
    250  1.50     lukem 		else if (! parseid(owner, &uid))
    251  1.41       cgd 			errx(1, "unknown user %s", owner);
    252  1.50     lukem 		iflags |= HASUID;
    253  1.50     lukem 	}
    254  1.50     lukem 
    255  1.50     lukem 	if (metafile) {
    256  1.50     lukem 		if ((metafp = fopen(metafile, "a")) == NULL)
    257  1.50     lukem 			warn("open %s", metafile);
    258  1.41       cgd 	}
    259   1.1       cgd 
    260   1.2       jtc 	if (dodir) {
    261   1.2       jtc 		for (; *argv != NULL; ++argv)
    262  1.50     lukem 			install_dir(*argv, iflags);
    263   1.2       jtc 		exit (0);
    264   1.5       jtc 	}
    265   1.2       jtc 
    266   1.1       cgd 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    267   1.2       jtc 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    268   1.1       cgd 		for (; *argv != to_name; ++argv)
    269  1.50     lukem 			install(*argv, to_name, iflags | DIRECTORY);
    270   1.1       cgd 		exit(0);
    271   1.1       cgd 	}
    272   1.1       cgd 
    273   1.1       cgd 	/* can't do file1 file2 directory/file */
    274   1.1       cgd 	if (argc != 2)
    275   1.1       cgd 		usage();
    276   1.1       cgd 
    277   1.1       cgd 	if (!no_target) {
    278   1.5       jtc 		if (stat(*argv, &from_sb))
    279   1.5       jtc 			err(1, "%s", *argv);
    280   1.5       jtc 		if (!S_ISREG(to_sb.st_mode))
    281  1.41       cgd 			errx(1, "%s: not a regular file", to_name);
    282  1.16  christos 		if (!dolink && to_sb.st_dev == from_sb.st_dev &&
    283   1.5       jtc 		    to_sb.st_ino == from_sb.st_ino)
    284   1.5       jtc 			errx(1, "%s and %s are the same file", *argv, to_name);
    285   1.5       jtc 		/*
    286   1.5       jtc 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    287   1.5       jtc 		 * off the append/immutable bits -- if we fail, go ahead,
    288   1.5       jtc 		 * it might work.
    289   1.5       jtc 		 */
    290   1.5       jtc #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
    291   1.5       jtc 		if (to_sb.st_flags & NOCHANGEBITS)
    292   1.5       jtc 			(void)chflags(to_name,
    293   1.5       jtc 			    to_sb.st_flags & ~(NOCHANGEBITS));
    294  1.32   hubertf 		if (dobackup)
    295  1.32   hubertf 			backup(to_name);
    296  1.34  christos 		else if (!dorename)
    297  1.31   hubertf 			(void)unlink(to_name);
    298   1.1       cgd 	}
    299  1.50     lukem 	install(*argv, to_name, iflags);
    300   1.1       cgd 	exit(0);
    301   1.1       cgd }
    302   1.1       cgd 
    303   1.1       cgd /*
    304  1.50     lukem  * parseid --
    305  1.50     lukem  *	parse uid or gid from arg into id, returning non-zero if successful
    306  1.50     lukem  */
    307  1.50     lukem int
    308  1.50     lukem parseid(char *name, id_t *id)
    309  1.50     lukem {
    310  1.50     lukem 	char	*ep;
    311  1.50     lukem 
    312  1.50     lukem 	errno = 0;
    313  1.50     lukem 	*id = (id_t)strtoul(name, &ep, 10);
    314  1.50     lukem 	if (errno || *ep != '\0')
    315  1.50     lukem 		return (0);
    316  1.50     lukem 	return (1);
    317  1.50     lukem }
    318  1.50     lukem 
    319  1.50     lukem /*
    320  1.16  christos  * makelink --
    321  1.16  christos  *	make a link from source to destination
    322  1.16  christos  */
    323  1.16  christos void
    324  1.48    simonb makelink(char *from_name, char *to_name)
    325  1.16  christos {
    326  1.16  christos 	char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
    327  1.16  christos 
    328  1.16  christos 	/* Try hard links first */
    329  1.16  christos 	if (dolink & (LN_HARD|LN_MIXED)) {
    330  1.16  christos 		if (link(from_name, to_name) == -1) {
    331  1.16  christos 			if ((dolink & LN_HARD) || errno != EXDEV)
    332  1.16  christos 				err(1, "link %s -> %s", from_name, to_name);
    333  1.16  christos 		}
    334  1.16  christos 		else
    335  1.16  christos 			return;
    336  1.16  christos 	}
    337  1.16  christos 
    338  1.16  christos 	/* Symbolic links */
    339  1.16  christos 	if (dolink & LN_ABSOLUTE) {
    340  1.16  christos 		/* Convert source path to absolute */
    341  1.16  christos 		if (realpath(from_name, src) == NULL)
    342  1.16  christos 			err(1, "%s", src);
    343  1.16  christos 		if (symlink(src, to_name) == -1)
    344  1.16  christos 			err(1, "symlink %s -> %s", src, to_name);
    345  1.16  christos 		return;
    346  1.16  christos 	}
    347  1.16  christos 
    348  1.16  christos 	if (dolink & LN_RELATIVE) {
    349  1.16  christos 		char *s, *d;
    350  1.16  christos 
    351  1.16  christos 		/* Resolve pathnames */
    352  1.16  christos 		if (realpath(from_name, src) == NULL)
    353  1.16  christos 			err(1, "%s", src);
    354  1.16  christos 		if (realpath(to_name, dst) == NULL)
    355  1.16  christos 			err(1, "%s", dst);
    356  1.16  christos 
    357  1.16  christos 		/* trim common path components */
    358  1.16  christos 		for (s = src, d = dst; *s == *d; s++, d++)
    359  1.16  christos 			continue;
    360  1.16  christos 		while (*s != '/')
    361  1.16  christos 			s--, d--;
    362  1.16  christos 
    363  1.16  christos 		/* count the number of directories we need to backtrack */
    364  1.16  christos 		for (++d, lnk[0] = '\0'; *d; d++)
    365  1.16  christos 			if (*d == '/')
    366  1.16  christos 				(void) strcat(lnk, "../");
    367  1.16  christos 
    368  1.16  christos 		(void) strcat(lnk, ++s);
    369  1.16  christos 
    370  1.16  christos 		if (symlink(lnk, dst) == -1)
    371  1.16  christos 			err(1, "symlink %s -> %s", lnk, dst);
    372  1.16  christos 		return;
    373  1.16  christos 	}
    374  1.16  christos 
    375  1.16  christos 	/*
    376  1.16  christos 	 * If absolute or relative was not specified,
    377  1.16  christos 	 * try the names the user provided
    378  1.16  christos 	 */
    379  1.16  christos 	if (symlink(from_name, to_name) == -1)
    380  1.17  christos 		err(1, "symlink %s -> %s", from_name, to_name);
    381  1.16  christos 
    382  1.16  christos }
    383  1.16  christos 
    384  1.16  christos /*
    385   1.1       cgd  * install --
    386   1.1       cgd  *	build a path name and install the file
    387   1.1       cgd  */
    388   1.5       jtc void
    389  1.50     lukem install(char *from_name, char *to_name, u_int flags)
    390   1.1       cgd {
    391   1.5       jtc 	struct stat from_sb, to_sb;
    392  1.50     lukem 	struct timeval tv[2];
    393   1.5       jtc 	int devnull, from_fd, to_fd, serrno;
    394  1.33  christos 	char *p, tmpl[MAXPATHLEN], *oto_name;
    395   1.5       jtc 
    396   1.5       jtc 	/* If try to install NULL file to a directory, fails. */
    397   1.5       jtc 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    398   1.5       jtc 		if (stat(from_name, &from_sb))
    399   1.5       jtc 			err(1, "%s", from_name);
    400   1.5       jtc 		if (!S_ISREG(from_sb.st_mode))
    401  1.41       cgd 			errx(1, "%s: not a regular file", to_name);
    402   1.5       jtc 		/* Build the target path. */
    403   1.5       jtc 		if (flags & DIRECTORY) {
    404   1.5       jtc 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    405   1.5       jtc 			    to_name,
    406  1.21     lukem 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
    407   1.1       cgd 			to_name = pathbuf;
    408   1.1       cgd 		}
    409   1.1       cgd 		devnull = 0;
    410   1.5       jtc 	} else {
    411   1.5       jtc 		from_sb.st_flags = 0;	/* XXX */
    412   1.1       cgd 		devnull = 1;
    413   1.5       jtc 	}
    414   1.1       cgd 
    415   1.5       jtc 	/*
    416   1.5       jtc 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    417   1.5       jtc 	 * off the append/immutable bits -- if we fail, go ahead,
    418   1.5       jtc 	 * it might work.
    419   1.5       jtc 	 */
    420   1.5       jtc 	if (stat(to_name, &to_sb) == 0 &&
    421   1.5       jtc 	    to_sb.st_flags & (NOCHANGEBITS))
    422   1.5       jtc 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    423  1.33  christos 	if (dorename) {
    424  1.33  christos 		char *ptr, c, *dir;
    425  1.33  christos 
    426  1.33  christos 		if ((ptr = strrchr(to_name, '/')) != NULL) {
    427  1.33  christos 			c = *++ptr;
    428  1.33  christos 			*ptr = '\0';
    429  1.33  christos 			dir = to_name;
    430  1.33  christos 		} else {
    431  1.37  christos 			c = '\0';	/* pacify gcc */
    432  1.33  christos 			dir = tmpl;
    433  1.33  christos 			*dir = '\0';
    434  1.33  christos 		}
    435  1.33  christos 		(void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
    436  1.33  christos 		if (ptr)
    437  1.33  christos 			*ptr = c;
    438  1.33  christos 		oto_name = to_name;
    439  1.33  christos 		to_name = tmpl;
    440  1.33  christos 
    441  1.33  christos 	} else {
    442  1.37  christos 		oto_name = NULL;	/* pacify gcc */
    443  1.33  christos 		if (dobackup)
    444  1.33  christos 			backup(to_name);
    445  1.33  christos 		else
    446  1.33  christos 			(void)unlink(to_name);
    447  1.33  christos 	}
    448  1.16  christos 
    449  1.16  christos 	if (dolink) {
    450  1.16  christos 		makelink(from_name, to_name);
    451  1.16  christos 		return;
    452  1.16  christos 	}
    453   1.1       cgd 
    454   1.5       jtc 	/* Create target. */
    455  1.33  christos 	if (dorename) {
    456  1.33  christos 		if ((to_fd = mkstemp(to_name)) == -1)
    457  1.33  christos 			err(1, "%s", to_name);
    458  1.33  christos 	} else {
    459  1.33  christos 		if ((to_fd = open(to_name,
    460  1.33  christos 		    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    461  1.33  christos 			err(1, "%s", to_name);
    462  1.33  christos 	}
    463   1.1       cgd 	if (!devnull) {
    464   1.1       cgd 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    465   1.1       cgd 			(void)unlink(to_name);
    466   1.5       jtc 			err(1, "%s", from_name);
    467   1.1       cgd 		}
    468   1.5       jtc 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    469   1.1       cgd 		(void)close(from_fd);
    470   1.1       cgd 	}
    471   1.9  jonathan 
    472   1.9  jonathan 	if (dostrip) {
    473   1.1       cgd 		strip(to_name);
    474   1.9  jonathan 
    475   1.9  jonathan 		/*
    476   1.9  jonathan 		 * Re-open our fd on the target, in case we used a strip
    477   1.9  jonathan 		 *  that does not work in-place -- like gnu binutils strip.
    478   1.9  jonathan 		 */
    479   1.9  jonathan 		close(to_fd);
    480   1.9  jonathan 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    481   1.9  jonathan 		  err(1, "stripping %s", to_name);
    482   1.9  jonathan 	}
    483   1.9  jonathan 
    484  1.41       cgd 	/*
    485  1.41       cgd 	 * Set owner, group, mode for target; do the chown first,
    486  1.41       cgd 	 * chown may lose the setuid bits.
    487  1.41       cgd 	 */
    488  1.50     lukem 	if (!dounpriv &&
    489  1.50     lukem 	    (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
    490  1.41       cgd 		serrno = errno;
    491  1.41       cgd 		(void)unlink(to_name);
    492  1.41       cgd 		errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    493   1.1       cgd 	}
    494  1.50     lukem 	if (!dounpriv && fchmod(to_fd, mode) == -1) {
    495   1.5       jtc 		serrno = errno;
    496   1.5       jtc 		(void)unlink(to_name);
    497   1.5       jtc 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    498   1.5       jtc 	}
    499   1.5       jtc 
    500   1.5       jtc 	/*
    501  1.26  christos 	 * Preserve the date of the source file.
    502  1.26  christos 	 */
    503  1.26  christos 	if (dopreserve) {
    504  1.37  christos #ifdef BSD4_4
    505  1.26  christos 		TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
    506  1.26  christos 		TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
    507  1.37  christos #else
    508  1.37  christos 		tv[0].tv_sec = from_sb.st_atime;
    509  1.37  christos 		tv[0].tv_usec = 0;
    510  1.37  christos 		tv[1].tv_sec = from_sb.st_mtime;
    511  1.37  christos 		tv[1].tv_usec = 0;
    512  1.37  christos #endif
    513  1.50     lukem 		if (!dounpriv && futimes(to_fd, tv) == -1)
    514  1.26  christos 			warn("%s: futimes", to_name);
    515   1.5       jtc 	}
    516   1.5       jtc 
    517   1.1       cgd 	(void)close(to_fd);
    518  1.33  christos 
    519  1.50     lukem 	if (dorename) {
    520  1.33  christos 		if (rename(to_name, oto_name) == -1)
    521  1.33  christos 			err(1, "%s: rename", to_name);
    522  1.50     lukem 		to_name = oto_name;
    523  1.50     lukem 	}
    524  1.33  christos 
    525   1.5       jtc 	if (!docopy && !devnull && unlink(from_name))
    526   1.5       jtc 		err(1, "%s", from_name);
    527  1.50     lukem 
    528  1.50     lukem 	/*
    529  1.50     lukem 	 * If provided a set of flags, set them, otherwise, preserve the
    530  1.50     lukem 	 * flags, except for the dump flag.
    531  1.50     lukem 	 */
    532  1.50     lukem 	if (!dounpriv && chflags(to_name,
    533  1.50     lukem 	    flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) != -1) {
    534  1.50     lukem 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    535  1.50     lukem 			warn("%s: chflags", to_name);
    536  1.50     lukem 	}
    537  1.50     lukem 
    538  1.50     lukem 	metadata_log(to_name, S_IFREG, flags, dopreserve ? tv : NULL);
    539   1.1       cgd }
    540   1.1       cgd 
    541   1.1       cgd /*
    542   1.1       cgd  * copy --
    543   1.1       cgd  *	copy from one file to another
    544   1.1       cgd  */
    545   1.5       jtc void
    546  1.48    simonb copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
    547   1.1       cgd {
    548  1.36   thorpej 	ssize_t nr, nw;
    549   1.5       jtc 	int serrno;
    550  1.14   thorpej 	char *p;
    551  1.14   thorpej 	char buf[MAXBSIZE];
    552   1.1       cgd 
    553   1.5       jtc 	/*
    554  1.28  wsanchez 	 * There's no reason to do anything other than close the file
    555  1.28  wsanchez 	 * now if it's empty, so let's not bother.
    556   1.5       jtc 	 */
    557  1.28  wsanchez 	if (size > 0) {
    558  1.45       chs 
    559  1.28  wsanchez 		/*
    560  1.45       chs 		 * Mmap and write if less than 8M (the limit is so we
    561  1.45       chs 		 * don't totally trash memory on big files).  This is
    562  1.45       chs 		 * really a minor hack, but it wins some CPU back.
    563  1.28  wsanchez 		 */
    564  1.45       chs 
    565  1.28  wsanchez 		if (size <= 8 * 1048576) {
    566  1.28  wsanchez 			if ((p = mmap(NULL, (size_t)size, PROT_READ,
    567  1.45       chs 			    MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
    568  1.45       chs 			    == MAP_FAILED) {
    569  1.45       chs 				goto mmap_failed;
    570  1.36   thorpej 			}
    571  1.44       cgd #ifdef MADV_SEQUENTIAL
    572  1.39  christos 			if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
    573  1.39  christos 			    && errno != EOPNOTSUPP)
    574  1.38  sommerfe 				warnx("madvise: %s", strerror(errno));
    575  1.44       cgd #endif
    576  1.38  sommerfe 
    577  1.36   thorpej 			if (write(to_fd, p, size) != size) {
    578  1.36   thorpej 				serrno = errno;
    579  1.36   thorpej 				(void)unlink(to_name);
    580  1.36   thorpej 				errx(1, "%s: %s",
    581  1.36   thorpej 				    to_name, strerror(serrno));
    582  1.36   thorpej 			}
    583  1.28  wsanchez 		} else {
    584  1.45       chs mmap_failed:
    585  1.36   thorpej 			while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
    586  1.28  wsanchez 				if ((nw = write(to_fd, buf, nr)) != nr) {
    587  1.28  wsanchez 					serrno = errno;
    588  1.28  wsanchez 					(void)unlink(to_name);
    589  1.45       chs 					errx(1, "%s: %s", to_name,
    590  1.45       chs 					    strerror(nw > 0 ? EIO : serrno));
    591  1.28  wsanchez 				}
    592  1.36   thorpej 			}
    593  1.28  wsanchez 			if (nr != 0) {
    594   1.5       jtc 				serrno = errno;
    595   1.5       jtc 				(void)unlink(to_name);
    596  1.28  wsanchez 				errx(1, "%s: %s", from_name, strerror(serrno));
    597   1.5       jtc 			}
    598   1.1       cgd 		}
    599   1.1       cgd 	}
    600   1.1       cgd }
    601   1.1       cgd 
    602   1.1       cgd /*
    603   1.1       cgd  * strip --
    604   1.1       cgd  *	use strip(1) to strip the target file
    605   1.1       cgd  */
    606   1.5       jtc void
    607  1.48    simonb strip(char *to_name)
    608   1.1       cgd {
    609   1.5       jtc 	int serrno, status;
    610  1.18   thorpej 	char *stripprog;
    611   1.1       cgd 
    612   1.1       cgd 	switch (vfork()) {
    613   1.1       cgd 	case -1:
    614   1.5       jtc 		serrno = errno;
    615   1.5       jtc 		(void)unlink(to_name);
    616  1.22   thorpej 		errx(1, "vfork: %s", strerror(serrno));
    617   1.1       cgd 	case 0:
    618  1.18   thorpej 		stripprog = getenv("STRIP");
    619  1.18   thorpej 		if (stripprog == NULL)
    620  1.18   thorpej 			stripprog = _PATH_STRIP;
    621  1.28  wsanchez 
    622  1.28  wsanchez 		if (stripArgs) {
    623  1.50     lukem 			/*
    624  1.50     lukem 			 * build up a command line and let /bin/sh
    625  1.50     lukem 			 * parse the arguments
    626  1.50     lukem 			 */
    627  1.28  wsanchez 			char* cmd = (char*)malloc(sizeof(char)*
    628  1.28  wsanchez 						  (3+strlen(stripprog)+
    629  1.28  wsanchez 						     strlen(stripArgs)+
    630  1.28  wsanchez 						     strlen(to_name)));
    631  1.49    simonb 
    632  1.49    simonb 			if (cmd == NULL)
    633  1.49    simonb 				errx(1, "%s", strerror(ENOMEM));
    634  1.28  wsanchez 
    635  1.28  wsanchez 			sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
    636  1.28  wsanchez 
    637  1.28  wsanchez 			execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    638  1.28  wsanchez 		} else
    639  1.40       cgd 			execlp(stripprog, "strip", to_name, NULL);
    640  1.28  wsanchez 
    641  1.22   thorpej 		warn("%s", stripprog);
    642  1.22   thorpej 		_exit(1);
    643   1.1       cgd 	default:
    644   1.1       cgd 		if (wait(&status) == -1 || status)
    645   1.5       jtc 			(void)unlink(to_name);
    646   1.1       cgd 	}
    647  1.32   hubertf }
    648  1.32   hubertf 
    649  1.32   hubertf /*
    650  1.32   hubertf  * backup file "to_name" to to_name.suffix
    651  1.32   hubertf  * if suffix contains a "%", it's taken as a printf(3) pattern
    652  1.32   hubertf  * used for a numbered backup.
    653  1.32   hubertf  */
    654  1.32   hubertf void
    655  1.48    simonb backup(const char *to_name)
    656  1.32   hubertf {
    657  1.32   hubertf 	char backup[FILENAME_MAX];
    658  1.32   hubertf 
    659  1.35   hubertf 	if (numberedbackup) {
    660  1.32   hubertf 		/* Do numbered backup */
    661  1.32   hubertf 		int cnt;
    662  1.32   hubertf 		char suffix_expanded[FILENAME_MAX];
    663  1.32   hubertf 
    664  1.32   hubertf 		cnt=0;
    665  1.32   hubertf 		do {
    666  1.50     lukem 			(void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
    667  1.50     lukem 			    cnt);
    668  1.32   hubertf 			(void)snprintf(backup, FILENAME_MAX, "%s%s",
    669  1.32   hubertf 				       to_name, suffix_expanded);
    670  1.32   hubertf 			cnt++;
    671  1.32   hubertf 		} while (access(backup, F_OK)==0);
    672  1.32   hubertf 	} else {
    673  1.32   hubertf 		/* Do simple backup */
    674  1.32   hubertf 		(void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
    675  1.32   hubertf 	}
    676  1.32   hubertf 
    677  1.32   hubertf 	(void)rename(to_name, backup);
    678   1.1       cgd }
    679   1.1       cgd 
    680   1.1       cgd /*
    681   1.5       jtc  * install_dir --
    682  1.47       wiz  *	build directory hierarchy
    683   1.2       jtc  */
    684   1.7       jtc void
    685  1.50     lukem install_dir(char *path, u_int flags)
    686   1.2       jtc {
    687  1.20       mrg         char *p;
    688   1.2       jtc         struct stat sb;
    689   1.4       cgd         int ch;
    690   1.2       jtc 
    691   1.4       cgd         for (p = path;; ++p)
    692   1.4       cgd                 if (!*p || (p != path && *p  == '/')) {
    693   1.2       jtc                         ch = *p;
    694   1.2       jtc                         *p = '\0';
    695   1.2       jtc                         if (stat(path, &sb)) {
    696   1.2       jtc                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    697   1.7       jtc 					err(1, "%s", path);
    698   1.7       jtc 					/* NOTREACHED */
    699   1.2       jtc                                 }
    700   1.2       jtc                         }
    701   1.2       jtc                         if (!(*p = ch))
    702   1.4       cgd 				break;
    703   1.2       jtc                 }
    704   1.2       jtc 
    705  1.50     lukem 	if (!dounpriv && (
    706  1.50     lukem 	    ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
    707  1.50     lukem 	    || chmod(path, mode) == -1 )) {
    708   1.5       jtc                 warn("%s", path);
    709  1.41       cgd 	}
    710  1.50     lukem 	metadata_log(path, S_IFDIR, flags & ~SETFLAGS, NULL);
    711   1.2       jtc }
    712   1.2       jtc 
    713   1.2       jtc /*
    714  1.50     lukem  * metadata_log --
    715  1.50     lukem  *	if metafp is not NULL, output mtree(8) full path name and settings to
    716  1.50     lukem  *	metafp, to allow permissions to be set correctly by other tools.
    717  1.50     lukem  */
    718  1.50     lukem void
    719  1.50     lukem metadata_log(const char *path, mode_t type, u_int flags, struct timeval *tv)
    720  1.50     lukem {
    721  1.50     lukem 	const char	extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
    722  1.50     lukem 	char	*buf;
    723  1.50     lukem 
    724  1.50     lukem 	if (!metafp)
    725  1.50     lukem 		return;
    726  1.50     lukem 	buf = (char *)malloc(4 * strlen(path) + 1);	/* buf for strsvis(3) */
    727  1.50     lukem 	if (buf == NULL) {
    728  1.50     lukem 		warnx("%s", strerror(ENOMEM));
    729  1.50     lukem 		return;
    730  1.50     lukem 	}
    731  1.50     lukem 	if (flock(fileno(metafp), LOCK_EX) == -1) {	/* lock log file */
    732  1.50     lukem 		warn("can't lock %s", metafile);
    733  1.50     lukem 		return;
    734  1.50     lukem 	}
    735  1.50     lukem 
    736  1.50     lukem 	strsvis(buf, path, VIS_CSTYLE, extra);		/* encode name */
    737  1.50     lukem 	fprintf(metafp, ".%s%s type=%s mode=%#o",	/* print details */
    738  1.50     lukem 	    buf[0] == '/' ? "" : "/", buf,
    739  1.50     lukem 	    S_ISDIR(type) ? "dir" : "file", mode);
    740  1.50     lukem 	if (flags & HASUID)
    741  1.50     lukem 		fprintf(metafp, " uid=%d", uid);
    742  1.50     lukem 	if (flags & HASGID)
    743  1.50     lukem 		fprintf(metafp, " gid=%d", gid);
    744  1.50     lukem 	if (flags & SETFLAGS)
    745  1.50     lukem 		fprintf(metafp, " flags=%s",
    746  1.50     lukem 		    flags_to_string(fileflags, "none"));
    747  1.50     lukem 	if (tv != NULL)
    748  1.50     lukem 		fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
    749  1.50     lukem 	fputc('\n', metafp);
    750  1.50     lukem 	fflush(metafp);					/* flush output */
    751  1.50     lukem 	if (flock(fileno(metafp), LOCK_UN) == -1) {	/* unlock log file */
    752  1.50     lukem 		warn("can't unlock %s", metafile);
    753  1.50     lukem 	}
    754  1.50     lukem 	free(buf);
    755  1.50     lukem }
    756  1.50     lukem 
    757  1.50     lukem 
    758  1.50     lukem 
    759  1.50     lukem /*
    760   1.1       cgd  * usage --
    761   1.1       cgd  *	print a usage message and die
    762   1.1       cgd  */
    763   1.5       jtc void
    764  1.48    simonb usage(void)
    765   1.1       cgd {
    766   1.2       jtc 	(void)fprintf(stderr, "\
    767  1.50     lukem usage: install [-Ubcprs] [-M log] [-B suffix] [-f flags] [-m mode]\n\
    768  1.50     lukem 	    [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
    769  1.50     lukem        install [-Ubcprs] [-M log] [-B suffix] [-f flags] [-m mode]\n\
    770  1.50     lukem 	    [-o owner] [-g group] [-l linkflags] [-S stripflags] \n\
    771  1.50     lukem 	    file1 ... fileN directory\n\
    772  1.50     lukem        install [-Up] [-M log] -d [-m mode] [-o owner] [-g group] directory ...\n");
    773   1.6       jtc 	exit(1);
    774   1.1       cgd }
    775