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