Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.73
      1  1.73     lukem /*	$NetBSD: xinstall.c,v 1.73 2002/06/09 04:16:39 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.65        tv #if HAVE_CONFIG_H
     37  1.65        tv #include "config.h"
     38  1.65        tv #else
     39  1.65        tv #define HAVE_FUTIMES 1
     40  1.65        tv #define HAVE_STRUCT_STAT_ST_FLAGS 1
     41  1.65        tv #endif
     42  1.65        tv 
     43  1.20       mrg #include <sys/cdefs.h>
     44  1.67        tv #if defined(__COPYRIGHT) && !defined(lint)
     45  1.20       mrg __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
     46  1.20       mrg 	The Regents of the University of California.  All rights reserved.\n");
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49  1.67        tv #if defined(__RCSID) && !defined(lint)
     50   1.5       jtc #if 0
     51   1.5       jtc static char sccsid[] = "@(#)xinstall.c	8.1 (Berkeley) 7/21/93";
     52  1.20       mrg #else
     53  1.73     lukem __RCSID("$NetBSD: xinstall.c,v 1.73 2002/06/09 04:16:39 lukem Exp $");
     54   1.5       jtc #endif
     55   1.1       cgd #endif /* not lint */
     56   1.1       cgd 
     57   1.1       cgd #include <sys/param.h>
     58   1.5       jtc #include <sys/mman.h>
     59   1.1       cgd #include <sys/stat.h>
     60  1.61    simonb #include <sys/wait.h>
     61   1.5       jtc 
     62   1.5       jtc #include <ctype.h>
     63  1.67        tv #include <err.h>
     64   1.5       jtc #include <errno.h>
     65   1.5       jtc #include <fcntl.h>
     66   1.1       cgd #include <grp.h>
     67  1.67        tv #include <libgen.h>
     68   1.5       jtc #include <paths.h>
     69   1.1       cgd #include <pwd.h>
     70   1.1       cgd #include <stdio.h>
     71   1.5       jtc #include <stdlib.h>
     72   1.5       jtc #include <string.h>
     73   1.5       jtc #include <unistd.h>
     74  1.50     lukem #include <vis.h>
     75   1.5       jtc 
     76   1.1       cgd #include "pathnames.h"
     77  1.29       mrg #include "stat_flags.h"
     78   1.1       cgd 
     79  1.28  wsanchez #define STRIP_ARGS_MAX 32
     80  1.31   hubertf #define BACKUP_SUFFIX ".old"
     81  1.28  wsanchez 
     82  1.50     lukem int	dobackup, docopy, dodir, dostrip, dolink, dopreserve, dorename,
     83  1.50     lukem 	    dounpriv;
     84  1.50     lukem int	numberedbackup;
     85  1.50     lukem int	mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
     86  1.50     lukem char	pathbuf[MAXPATHLEN];
     87  1.65        tv id_t	uid, gid;
     88  1.54     lukem char	*group, *owner, *fflags, *tags;
     89  1.50     lukem FILE	*metafp;
     90  1.50     lukem char	*metafile;
     91  1.50     lukem u_long	fileflags;
     92  1.50     lukem char	*stripArgs;
     93  1.69     lukem char	*afterinstallcmd;
     94  1.50     lukem char	*suffix = BACKUP_SUFFIX;
     95   1.5       jtc 
     96  1.16  christos #define LN_ABSOLUTE	0x01
     97  1.16  christos #define LN_RELATIVE	0x02
     98  1.16  christos #define LN_HARD		0x04
     99  1.16  christos #define LN_SYMBOLIC	0x08
    100  1.16  christos #define LN_MIXED	0x10
    101  1.16  christos 
    102   1.5       jtc #define	DIRECTORY	0x01		/* Tell install it's a directory. */
    103   1.5       jtc #define	SETFLAGS	0x02		/* Tell install to set flags. */
    104  1.50     lukem #define	HASUID		0x04		/* Tell install the uid was given */
    105  1.50     lukem #define	HASGID		0x08		/* Tell install the gid was given */
    106   1.5       jtc 
    107  1.69     lukem void	afterinstall(const char *, const char *, int);
    108  1.50     lukem void	backup(const char *);
    109  1.48    simonb void	copy(int, char *, int, char *, off_t);
    110  1.61    simonb int	do_link(char *, char *);
    111  1.61    simonb void	do_symlink(char *, char *);
    112  1.50     lukem void	install(char *, char *, u_int);
    113  1.50     lukem void	install_dir(char *, u_int);
    114  1.50     lukem int	main(int, char *[]);
    115  1.48    simonb void	makelink(char *, char *);
    116  1.61    simonb void	metadata_log(const char *, const char *, struct timeval *, const char *);
    117  1.50     lukem int	parseid(char *, id_t *);
    118  1.48    simonb void	strip(char *);
    119  1.48    simonb void	usage(void);
    120  1.61    simonb char   *xbasename(char *);
    121  1.61    simonb char   *xdirname(char *);
    122   1.1       cgd 
    123   1.5       jtc int
    124  1.48    simonb main(int argc, char *argv[])
    125   1.1       cgd {
    126  1.53     lukem 	struct stat	from_sb, to_sb;
    127  1.53     lukem 	void		*set;
    128  1.53     lukem 	u_int		iflags;
    129  1.53     lukem 	int		ch, no_target;
    130  1.53     lukem 	char		*p, *to_name;
    131  1.43       cgd 
    132  1.43       cgd 	setprogname(argv[0]);
    133   1.1       cgd 
    134   1.5       jtc 	iflags = 0;
    135  1.69     lukem 	while ((ch = getopt(argc, argv, "a:cbB:df:g:l:m:M:o:prsS:T:U")) != -1)
    136   1.1       cgd 		switch((char)ch) {
    137  1.69     lukem 		case 'a':
    138  1.69     lukem 			afterinstallcmd = strdup(optarg);
    139  1.69     lukem 			if (afterinstallcmd == NULL)
    140  1.69     lukem 				errx(1, "%s", strerror(ENOMEM));
    141  1.69     lukem 			break;
    142  1.31   hubertf 		case 'B':
    143  1.31   hubertf 			suffix = optarg;
    144  1.35   hubertf 			numberedbackup = 0;
    145  1.35   hubertf 			{
    146  1.35   hubertf 				/* Check if given suffix really generates
    147  1.35   hubertf 				   different suffixes - catch e.g. ".%" */
    148  1.35   hubertf 				char suffix_expanded0[FILENAME_MAX],
    149  1.35   hubertf 				     suffix_expanded1[FILENAME_MAX];
    150  1.35   hubertf 				(void)snprintf(suffix_expanded0, FILENAME_MAX,
    151  1.35   hubertf 					       suffix, 0);
    152  1.35   hubertf 				(void)snprintf(suffix_expanded1, FILENAME_MAX,
    153  1.35   hubertf 					       suffix, 1);
    154  1.35   hubertf 				if (strcmp(suffix_expanded0, suffix_expanded1)
    155  1.35   hubertf 				    != 0)
    156  1.35   hubertf 					numberedbackup = 1;
    157  1.35   hubertf 			}
    158  1.31   hubertf 			/* fall through; -B implies -b */
    159  1.61    simonb 			/*FALLTHROUGH*/
    160  1.31   hubertf 		case 'b':
    161  1.31   hubertf 			dobackup = 1;
    162  1.31   hubertf 			break;
    163   1.1       cgd 		case 'c':
    164   1.1       cgd 			docopy = 1;
    165   1.1       cgd 			break;
    166  1.26  christos 		case 'd':
    167  1.26  christos 			dodir = 1;
    168  1.26  christos 			break;
    169  1.65        tv #if !HAVE_CONFIG_H
    170   1.5       jtc 		case 'f':
    171  1.52        tv 			fflags = optarg;
    172   1.5       jtc 			break;
    173  1.65        tv #endif
    174   1.1       cgd 		case 'g':
    175   1.1       cgd 			group = optarg;
    176   1.1       cgd 			break;
    177  1.16  christos 		case 'l':
    178  1.16  christos 			for (p = optarg; *p; p++)
    179  1.16  christos 				switch (*p) {
    180  1.16  christos 				case 's':
    181  1.16  christos 					dolink &= ~(LN_HARD|LN_MIXED);
    182  1.16  christos 					dolink |= LN_SYMBOLIC;
    183  1.16  christos 					break;
    184  1.16  christos 				case 'h':
    185  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_MIXED);
    186  1.16  christos 					dolink |= LN_HARD;
    187  1.16  christos 					break;
    188  1.16  christos 				case 'm':
    189  1.16  christos 					dolink &= ~(LN_SYMBOLIC|LN_HARD);
    190  1.16  christos 					dolink |= LN_MIXED;
    191  1.16  christos 					break;
    192  1.16  christos 				case 'a':
    193  1.16  christos 					dolink &= ~LN_RELATIVE;
    194  1.16  christos 					dolink |= LN_ABSOLUTE;
    195  1.16  christos 					break;
    196  1.16  christos 				case 'r':
    197  1.16  christos 					dolink &= ~LN_ABSOLUTE;
    198  1.16  christos 					dolink |= LN_RELATIVE;
    199  1.16  christos 					break;
    200  1.16  christos 				default:
    201  1.16  christos 					errx(1, "%c: invalid link type", *p);
    202  1.61    simonb 					/* NOTREACHED */
    203  1.16  christos 				}
    204  1.16  christos 			break;
    205  1.26  christos 		case 'm':
    206  1.26  christos 			if (!(set = setmode(optarg)))
    207  1.26  christos 				errx(1, "%s: invalid file mode", optarg);
    208  1.26  christos 			mode = getmode(set, 0);
    209  1.42     enami 			free(set);
    210  1.26  christos 			break;
    211  1.50     lukem 		case 'M':
    212  1.50     lukem 			metafile = optarg;
    213  1.50     lukem 			break;
    214  1.26  christos 		case 'o':
    215  1.26  christos 			owner = optarg;
    216  1.26  christos 			break;
    217  1.26  christos 		case 'p':
    218  1.26  christos 			dopreserve = 1;
    219  1.26  christos 			break;
    220  1.33  christos 		case 'r':
    221  1.33  christos 			dorename = 1;
    222  1.33  christos 			break;
    223  1.28  wsanchez 		case 'S':
    224  1.49    simonb 			stripArgs = strdup(optarg);
    225  1.49    simonb 			if (stripArgs == NULL)
    226  1.49    simonb 				errx(1, "%s", strerror(ENOMEM));
    227  1.28  wsanchez 			/* fall through; -S implies -s */
    228  1.61    simonb 			/*FALLTHROUGH*/
    229  1.26  christos 		case 's':
    230  1.26  christos 			dostrip = 1;
    231  1.26  christos 			break;
    232  1.54     lukem 		case 'T':
    233  1.54     lukem 			tags = optarg;
    234  1.54     lukem 			break;
    235  1.38  sommerfe 		case 'U':
    236  1.50     lukem 			dounpriv = 1;
    237  1.38  sommerfe 			break;
    238   1.1       cgd 		case '?':
    239   1.1       cgd 		default:
    240   1.1       cgd 			usage();
    241   1.1       cgd 		}
    242   1.1       cgd 	argc -= optind;
    243   1.1       cgd 	argv += optind;
    244   1.2       jtc 
    245  1.23        tv 	/* strip and link options make no sense when creating directories */
    246  1.23        tv 	if ((dostrip || dolink) && dodir)
    247  1.16  christos 		usage();
    248  1.16  christos 
    249  1.16  christos 	/* strip and flags make no sense with links */
    250  1.52        tv 	if ((dostrip || fflags) && dolink)
    251   1.2       jtc 		usage();
    252   1.2       jtc 
    253   1.2       jtc 	/* must have at least two arguments, except when creating directories */
    254   1.2       jtc 	if (argc < 2 && !dodir)
    255   1.1       cgd 		usage();
    256   1.1       cgd 
    257   1.1       cgd 	/* get group and owner id's */
    258  1.52        tv 	if (group && !dounpriv) {
    259  1.50     lukem 		struct group *gp;
    260  1.50     lukem 
    261  1.50     lukem 		if ((gp = getgrnam(group)) != NULL)
    262  1.50     lukem 			gid = gp->gr_gid;
    263  1.50     lukem 		else if (! parseid(group, &gid))
    264  1.41       cgd 			errx(1, "unknown group %s", group);
    265  1.50     lukem 		iflags |= HASGID;
    266  1.50     lukem 	}
    267  1.52        tv 	if (owner && !dounpriv) {
    268  1.50     lukem 		struct passwd *pp;
    269  1.50     lukem 
    270  1.50     lukem 		if ((pp = getpwnam(owner)) != NULL)
    271  1.50     lukem 			uid = pp->pw_uid;
    272  1.50     lukem 		else if (! parseid(owner, &uid))
    273  1.41       cgd 			errx(1, "unknown user %s", owner);
    274  1.50     lukem 		iflags |= HASUID;
    275  1.50     lukem 	}
    276  1.50     lukem 
    277  1.65        tv #if !HAVE_CONFIG_H
    278  1.52        tv 	if (fflags && !dounpriv) {
    279  1.52        tv 		if (string_to_flags(&fflags, &fileflags, NULL))
    280  1.52        tv 			errx(1, "%s: invalid flag", fflags);
    281  1.52        tv 		iflags |= SETFLAGS;
    282  1.52        tv 	}
    283  1.65        tv #endif
    284  1.52        tv 
    285  1.50     lukem 	if (metafile) {
    286  1.50     lukem 		if ((metafp = fopen(metafile, "a")) == NULL)
    287  1.50     lukem 			warn("open %s", metafile);
    288  1.41       cgd 	}
    289   1.1       cgd 
    290   1.2       jtc 	if (dodir) {
    291   1.2       jtc 		for (; *argv != NULL; ++argv)
    292  1.50     lukem 			install_dir(*argv, iflags);
    293   1.2       jtc 		exit (0);
    294   1.5       jtc 	}
    295   1.2       jtc 
    296   1.1       cgd 	no_target = stat(to_name = argv[argc - 1], &to_sb);
    297   1.2       jtc 	if (!no_target && S_ISDIR(to_sb.st_mode)) {
    298   1.1       cgd 		for (; *argv != to_name; ++argv)
    299  1.50     lukem 			install(*argv, to_name, iflags | DIRECTORY);
    300   1.1       cgd 		exit(0);
    301   1.1       cgd 	}
    302   1.1       cgd 
    303   1.1       cgd 	/* can't do file1 file2 directory/file */
    304   1.1       cgd 	if (argc != 2)
    305   1.1       cgd 		usage();
    306   1.1       cgd 
    307   1.1       cgd 	if (!no_target) {
    308  1.61    simonb 		/* makelink() handles checks for links */
    309  1.61    simonb 		if (!dolink) {
    310  1.61    simonb 			if (stat(*argv, &from_sb))
    311  1.66        tv 				err(1, "%s: stat", *argv);
    312  1.61    simonb 			if (!S_ISREG(to_sb.st_mode))
    313  1.61    simonb 				errx(1, "%s: not a regular file", to_name);
    314  1.61    simonb 			if (to_sb.st_dev == from_sb.st_dev &&
    315  1.61    simonb 			    to_sb.st_ino == from_sb.st_ino)
    316  1.61    simonb 				errx(1, "%s and %s are the same file", *argv,
    317  1.61    simonb 				    to_name);
    318  1.61    simonb 		}
    319   1.5       jtc 		/*
    320   1.5       jtc 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    321   1.5       jtc 		 * off the append/immutable bits -- if we fail, go ahead,
    322   1.5       jtc 		 * it might work.
    323   1.5       jtc 		 */
    324  1.65        tv #if !HAVE_CONFIG_H
    325   1.5       jtc #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
    326   1.5       jtc 		if (to_sb.st_flags & NOCHANGEBITS)
    327   1.5       jtc 			(void)chflags(to_name,
    328   1.5       jtc 			    to_sb.st_flags & ~(NOCHANGEBITS));
    329  1.65        tv #endif
    330  1.32   hubertf 		if (dobackup)
    331  1.32   hubertf 			backup(to_name);
    332  1.34  christos 		else if (!dorename)
    333  1.31   hubertf 			(void)unlink(to_name);
    334   1.1       cgd 	}
    335  1.50     lukem 	install(*argv, to_name, iflags);
    336   1.1       cgd 	exit(0);
    337   1.1       cgd }
    338   1.1       cgd 
    339   1.1       cgd /*
    340  1.50     lukem  * parseid --
    341  1.50     lukem  *	parse uid or gid from arg into id, returning non-zero if successful
    342  1.50     lukem  */
    343  1.50     lukem int
    344  1.50     lukem parseid(char *name, id_t *id)
    345  1.50     lukem {
    346  1.50     lukem 	char	*ep;
    347  1.50     lukem 
    348  1.50     lukem 	errno = 0;
    349  1.50     lukem 	*id = (id_t)strtoul(name, &ep, 10);
    350  1.50     lukem 	if (errno || *ep != '\0')
    351  1.50     lukem 		return (0);
    352  1.50     lukem 	return (1);
    353  1.50     lukem }
    354  1.50     lukem 
    355  1.50     lukem /*
    356  1.61    simonb  * do_link --
    357  1.61    simonb  *	make a hard link, obeying dorename if set
    358  1.61    simonb  *	return -1 on failure
    359  1.61    simonb  */
    360  1.61    simonb int
    361  1.61    simonb do_link(char *from_name, char *to_name)
    362  1.61    simonb {
    363  1.61    simonb 	char tmpl[MAXPATHLEN];
    364  1.61    simonb 	int ret;
    365  1.61    simonb 
    366  1.61    simonb 	if (dorename) {
    367  1.61    simonb 		(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
    368  1.61    simonb 		    xdirname(to_name));
    369  1.62     perry 		/* This usage is safe. The linker will bitch anyway. */
    370  1.61    simonb 		if (mktemp(tmpl) == NULL)
    371  1.66        tv 			err(1, "%s: mktemp", tmpl);
    372  1.61    simonb 		ret = link(from_name, tmpl);
    373  1.61    simonb 		if (ret == 0) {
    374  1.61    simonb 			ret = rename(tmpl, to_name);
    375  1.61    simonb 			if (ret < 0)
    376  1.61    simonb 				/* remove temporary link before exiting */
    377  1.61    simonb 				(void)unlink(tmpl);
    378  1.61    simonb 		}
    379  1.61    simonb 		return (ret);
    380  1.61    simonb 	} else
    381  1.61    simonb 		return (link(from_name, to_name));
    382  1.61    simonb }
    383  1.61    simonb 
    384  1.61    simonb /*
    385  1.61    simonb  * do_symlink --
    386  1.61    simonb  *	make a symbolic link, obeying dorename if set
    387  1.61    simonb  *	exit on failure
    388  1.61    simonb  */
    389  1.61    simonb void
    390  1.61    simonb do_symlink(char *from_name, char *to_name)
    391  1.61    simonb {
    392  1.61    simonb 	char tmpl[MAXPATHLEN];
    393  1.61    simonb 
    394  1.61    simonb 	if (dorename) {
    395  1.61    simonb 		(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
    396  1.61    simonb 		    xdirname(to_name));
    397  1.62     perry 		/* This usage is safe. The linker will bitch anyway. */
    398  1.61    simonb 		if (mktemp(tmpl) == NULL)
    399  1.66        tv 			err(1, "%s: mktemp", tmpl);
    400  1.61    simonb 
    401  1.61    simonb 		if (symlink(from_name, tmpl) == -1)
    402  1.61    simonb 			err(1, "symlink %s -> %s", from_name, tmpl);
    403  1.61    simonb 		if (rename(tmpl, to_name) == -1) {
    404  1.61    simonb 			/* remove temporary link before exiting */
    405  1.61    simonb 			(void)unlink(tmpl);
    406  1.61    simonb 			err(1, "%s: rename", to_name);
    407  1.61    simonb 		}
    408  1.61    simonb 	} else {
    409  1.61    simonb 		if (symlink(from_name, to_name) == -1)
    410  1.61    simonb 			err(1, "symlink %s -> %s", from_name, to_name);
    411  1.61    simonb 	}
    412  1.61    simonb }
    413  1.61    simonb 
    414  1.61    simonb /*
    415  1.16  christos  * makelink --
    416  1.16  christos  *	make a link from source to destination
    417  1.16  christos  */
    418  1.16  christos void
    419  1.48    simonb makelink(char *from_name, char *to_name)
    420  1.16  christos {
    421  1.53     lukem 	char	src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
    422  1.68     lukem 	struct stat	to_sb;
    423  1.16  christos 
    424  1.16  christos 	/* Try hard links first */
    425  1.16  christos 	if (dolink & (LN_HARD|LN_MIXED)) {
    426  1.61    simonb 		if (do_link(from_name, to_name) == -1) {
    427  1.16  christos 			if ((dolink & LN_HARD) || errno != EXDEV)
    428  1.16  christos 				err(1, "link %s -> %s", from_name, to_name);
    429  1.68     lukem 		} else {
    430  1.68     lukem 			if (stat(to_name, &to_sb))
    431  1.68     lukem 				err(1, "%s: stat", to_name);
    432  1.69     lukem 			if (S_ISREG(to_sb.st_mode)) {
    433  1.69     lukem 					/* XXX: only metalog hardlinked files */
    434  1.69     lukem 				int omode;
    435  1.69     lukem 				char *oowner, *ogroup, *offlags;
    436  1.69     lukem 
    437  1.69     lukem 					/* XXX: use underlying perms */
    438  1.69     lukem 				omode = mode;
    439  1.69     lukem 				mode = (to_sb.st_mode & 0777);
    440  1.69     lukem 				oowner = owner;
    441  1.69     lukem 				owner = NULL;
    442  1.69     lukem 				ogroup = group;
    443  1.69     lukem 				group = NULL;
    444  1.69     lukem 				offlags = fflags;
    445  1.69     lukem 				fflags = NULL;
    446  1.68     lukem 				metadata_log(to_name, "file", NULL, NULL);
    447  1.69     lukem 				mode = omode;
    448  1.69     lukem 				owner = oowner;
    449  1.69     lukem 				group = ogroup;
    450  1.69     lukem 				fflags = offlags;
    451  1.69     lukem 			}
    452  1.16  christos 			return;
    453  1.57     lukem 		}
    454  1.16  christos 	}
    455  1.16  christos 
    456  1.16  christos 	/* Symbolic links */
    457  1.16  christos 	if (dolink & LN_ABSOLUTE) {
    458  1.16  christos 		/* Convert source path to absolute */
    459  1.16  christos 		if (realpath(from_name, src) == NULL)
    460  1.66        tv 			err(1, "%s: realpath", from_name);
    461  1.61    simonb 		do_symlink(src, to_name);
    462  1.59     perry 		metadata_log(to_name, "link", NULL, src);
    463  1.16  christos 		return;
    464  1.16  christos 	}
    465  1.16  christos 
    466  1.16  christos 	if (dolink & LN_RELATIVE) {
    467  1.61    simonb 		char *cp, *d, *s;
    468  1.16  christos 
    469  1.16  christos 		/* Resolve pathnames */
    470  1.16  christos 		if (realpath(from_name, src) == NULL)
    471  1.66        tv 			err(1, "%s: realpath", from_name);
    472  1.61    simonb 
    473  1.61    simonb 		/*
    474  1.61    simonb 		 * The last component of to_name may be a symlink,
    475  1.61    simonb 		 * so use realpath to resolve only the directory.
    476  1.61    simonb 		 */
    477  1.63     perry 		cp = xdirname(to_name);
    478  1.61    simonb 		if (realpath(cp, dst) == NULL)
    479  1.66        tv 			err(1, "%s: realpath", cp);
    480  1.61    simonb 		/* .. and add the last component */
    481  1.61    simonb 		if (strcmp(dst, "/") != 0) {
    482  1.61    simonb 			if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
    483  1.61    simonb 				errx(1, "resolved pathname too long");
    484  1.61    simonb 		}
    485  1.61    simonb 		cp = xbasename(to_name);
    486  1.61    simonb 		if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
    487  1.61    simonb 			errx(1, "resolved pathname too long");
    488  1.16  christos 
    489  1.16  christos 		/* trim common path components */
    490  1.16  christos 		for (s = src, d = dst; *s == *d; s++, d++)
    491  1.16  christos 			continue;
    492  1.16  christos 		while (*s != '/')
    493  1.16  christos 			s--, d--;
    494  1.16  christos 
    495  1.16  christos 		/* count the number of directories we need to backtrack */
    496  1.16  christos 		for (++d, lnk[0] = '\0'; *d; d++)
    497  1.16  christos 			if (*d == '/')
    498  1.61    simonb 				(void)strcat(lnk, "../");
    499  1.16  christos 
    500  1.61    simonb 		(void)strcat(lnk, ++s);
    501  1.16  christos 
    502  1.61    simonb 		do_symlink(lnk, dst);
    503  1.59     perry 		metadata_log(dst, "link", NULL, lnk);
    504  1.16  christos 		return;
    505  1.16  christos 	}
    506  1.16  christos 
    507  1.16  christos 	/*
    508  1.16  christos 	 * If absolute or relative was not specified,
    509  1.16  christos 	 * try the names the user provided
    510  1.16  christos 	 */
    511  1.61    simonb 	do_symlink(from_name, to_name);
    512  1.59     perry 	metadata_log(to_name, "link", NULL, from_name);
    513  1.16  christos }
    514  1.16  christos 
    515  1.16  christos /*
    516   1.1       cgd  * install --
    517   1.1       cgd  *	build a path name and install the file
    518   1.1       cgd  */
    519   1.5       jtc void
    520  1.50     lukem install(char *from_name, char *to_name, u_int flags)
    521   1.1       cgd {
    522  1.65        tv 	struct stat	from_sb;
    523  1.65        tv #if !HAVE_CONFIG_H
    524  1.65        tv 	struct stat	to_sb;
    525  1.65        tv #endif
    526  1.53     lukem 	struct timeval	tv[2];
    527  1.60     dillo 	int		devnull, from_fd, to_fd, serrno, tmpmode;
    528  1.53     lukem 	char		*p, tmpl[MAXPATHLEN], *oto_name;
    529   1.5       jtc 
    530  1.71     lukem 	if (!dolink) {
    531  1.71     lukem 			/* ensure that from_sb & tv are sane if !dolink */
    532  1.71     lukem 		if (stat(from_name, &from_sb))
    533  1.71     lukem 			err(1, "%s: stat", from_name);
    534  1.70     lukem #ifdef BSD4_4
    535  1.71     lukem 		TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
    536  1.71     lukem 		TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
    537  1.70     lukem #else
    538  1.71     lukem 		tv[0].tv_sec = from_sb.st_atime;
    539  1.71     lukem 		tv[0].tv_usec = 0;
    540  1.71     lukem 		tv[1].tv_sec = from_sb.st_mtime;
    541  1.71     lukem 		tv[1].tv_usec = 0;
    542  1.70     lukem #endif
    543  1.71     lukem 	}
    544  1.70     lukem 
    545   1.5       jtc 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    546  1.56     perry 		if (!dolink) {
    547  1.56     perry 			if (!S_ISREG(from_sb.st_mode))
    548  1.56     perry 				errx(1, "%s: not a regular file", from_name);
    549  1.56     perry 		}
    550   1.5       jtc 		/* Build the target path. */
    551   1.5       jtc 		if (flags & DIRECTORY) {
    552   1.5       jtc 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    553   1.5       jtc 			    to_name,
    554  1.21     lukem 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
    555   1.1       cgd 			to_name = pathbuf;
    556   1.1       cgd 		}
    557   1.1       cgd 		devnull = 0;
    558   1.5       jtc 	} else {
    559  1.65        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    560   1.5       jtc 		from_sb.st_flags = 0;	/* XXX */
    561  1.65        tv #endif
    562   1.1       cgd 		devnull = 1;
    563   1.5       jtc 	}
    564   1.1       cgd 
    565   1.5       jtc 	/*
    566   1.5       jtc 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    567   1.5       jtc 	 * off the append/immutable bits -- if we fail, go ahead,
    568   1.5       jtc 	 * it might work.
    569   1.5       jtc 	 */
    570  1.65        tv #if !HAVE_CONFIG_H
    571   1.5       jtc 	if (stat(to_name, &to_sb) == 0 &&
    572   1.5       jtc 	    to_sb.st_flags & (NOCHANGEBITS))
    573   1.5       jtc 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    574  1.65        tv #endif
    575  1.33  christos 	if (dorename) {
    576  1.64   msaitoh 		(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
    577  1.61    simonb 		    xdirname(to_name));
    578  1.33  christos 		oto_name = to_name;
    579  1.33  christos 		to_name = tmpl;
    580  1.33  christos 	} else {
    581  1.37  christos 		oto_name = NULL;	/* pacify gcc */
    582  1.33  christos 		if (dobackup)
    583  1.33  christos 			backup(to_name);
    584  1.33  christos 		else
    585  1.33  christos 			(void)unlink(to_name);
    586  1.33  christos 	}
    587  1.16  christos 
    588  1.16  christos 	if (dolink) {
    589  1.61    simonb 		makelink(from_name, dorename ? oto_name : to_name);
    590  1.16  christos 		return;
    591  1.16  christos 	}
    592   1.1       cgd 
    593   1.5       jtc 	/* Create target. */
    594  1.33  christos 	if (dorename) {
    595  1.33  christos 		if ((to_fd = mkstemp(to_name)) == -1)
    596  1.66        tv 			err(1, "%s: mkstemp", to_name);
    597  1.33  christos 	} else {
    598  1.33  christos 		if ((to_fd = open(to_name,
    599  1.33  christos 		    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    600  1.66        tv 			err(1, "%s: open", to_name);
    601  1.33  christos 	}
    602   1.1       cgd 	if (!devnull) {
    603   1.1       cgd 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    604   1.1       cgd 			(void)unlink(to_name);
    605  1.66        tv 			err(1, "%s: open", from_name);
    606   1.1       cgd 		}
    607   1.5       jtc 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    608   1.1       cgd 		(void)close(from_fd);
    609   1.1       cgd 	}
    610   1.9  jonathan 
    611   1.9  jonathan 	if (dostrip) {
    612   1.1       cgd 		strip(to_name);
    613   1.9  jonathan 
    614   1.9  jonathan 		/*
    615   1.9  jonathan 		 * Re-open our fd on the target, in case we used a strip
    616   1.9  jonathan 		 *  that does not work in-place -- like gnu binutils strip.
    617   1.9  jonathan 		 */
    618   1.9  jonathan 		close(to_fd);
    619   1.9  jonathan 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    620  1.69     lukem 			err(1, "stripping %s", to_name);
    621  1.69     lukem 	}
    622  1.69     lukem 
    623  1.69     lukem 	if (afterinstallcmd != NULL) {
    624  1.69     lukem 		afterinstall(afterinstallcmd, to_name, 1);
    625  1.69     lukem 
    626  1.69     lukem 		/*
    627  1.69     lukem 		 * Re-open our fd on the target, in case we used an
    628  1.69     lukem 		 * after-install command that does not work in-place
    629  1.69     lukem 		 */
    630  1.69     lukem 		close(to_fd);
    631  1.69     lukem 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    632  1.69     lukem 			err(1, "running after install command on %s", to_name);
    633   1.9  jonathan 	}
    634   1.9  jonathan 
    635  1.41       cgd 	/*
    636  1.41       cgd 	 * Set owner, group, mode for target; do the chown first,
    637  1.41       cgd 	 * chown may lose the setuid bits.
    638  1.41       cgd 	 */
    639  1.50     lukem 	if (!dounpriv &&
    640  1.50     lukem 	    (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
    641  1.41       cgd 		serrno = errno;
    642  1.41       cgd 		(void)unlink(to_name);
    643  1.41       cgd 		errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    644   1.1       cgd 	}
    645  1.60     dillo 	tmpmode = mode;
    646  1.58        tv 	if (dounpriv)
    647  1.60     dillo 		tmpmode &= S_IRWXU|S_IRWXG|S_IRWXO;
    648  1.60     dillo 	if (fchmod(to_fd, tmpmode) == -1) {
    649   1.5       jtc 		serrno = errno;
    650   1.5       jtc 		(void)unlink(to_name);
    651   1.5       jtc 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    652   1.5       jtc 	}
    653   1.5       jtc 
    654   1.5       jtc 	/*
    655  1.26  christos 	 * Preserve the date of the source file.
    656  1.26  christos 	 */
    657  1.26  christos 	if (dopreserve) {
    658  1.65        tv #if HAVE_FUTIMES
    659  1.65        tv 		if (futimes(to_fd, tv) == -1)
    660  1.65        tv 			warn("%s: futimes", to_name);
    661  1.65        tv #else
    662  1.65        tv 		if (utimes(to_name, tv) == -1)
    663  1.66        tv 			warn("%s: utimes", to_name);
    664  1.65        tv #endif
    665   1.5       jtc 	}
    666   1.5       jtc 
    667   1.1       cgd 	(void)close(to_fd);
    668  1.33  christos 
    669  1.50     lukem 	if (dorename) {
    670  1.33  christos 		if (rename(to_name, oto_name) == -1)
    671  1.33  christos 			err(1, "%s: rename", to_name);
    672  1.50     lukem 		to_name = oto_name;
    673  1.50     lukem 	}
    674  1.33  christos 
    675   1.5       jtc 	if (!docopy && !devnull && unlink(from_name))
    676  1.66        tv 		err(1, "%s: unlink", from_name);
    677  1.50     lukem 
    678  1.50     lukem 	/*
    679  1.50     lukem 	 * If provided a set of flags, set them, otherwise, preserve the
    680  1.50     lukem 	 * flags, except for the dump flag.
    681  1.50     lukem 	 */
    682  1.65        tv #if !HAVE_CONFIG_H
    683  1.50     lukem 	if (!dounpriv && chflags(to_name,
    684  1.51     lukem 	    flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
    685  1.51     lukem 	{
    686  1.50     lukem 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    687  1.50     lukem 			warn("%s: chflags", to_name);
    688  1.50     lukem 	}
    689  1.65        tv #endif
    690  1.50     lukem 
    691  1.59     perry 	metadata_log(to_name, "file", tv, NULL);
    692   1.1       cgd }
    693   1.1       cgd 
    694   1.1       cgd /*
    695   1.1       cgd  * copy --
    696   1.1       cgd  *	copy from one file to another
    697   1.1       cgd  */
    698   1.5       jtc void
    699  1.48    simonb copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
    700   1.1       cgd {
    701  1.53     lukem 	ssize_t	nr, nw;
    702  1.53     lukem 	int	serrno;
    703  1.53     lukem 	char	*p;
    704  1.53     lukem 	char	buf[MAXBSIZE];
    705   1.1       cgd 
    706   1.5       jtc 	/*
    707  1.28  wsanchez 	 * There's no reason to do anything other than close the file
    708  1.28  wsanchez 	 * now if it's empty, so let's not bother.
    709   1.5       jtc 	 */
    710  1.28  wsanchez 	if (size > 0) {
    711  1.45       chs 
    712  1.28  wsanchez 		/*
    713  1.45       chs 		 * Mmap and write if less than 8M (the limit is so we
    714  1.45       chs 		 * don't totally trash memory on big files).  This is
    715  1.45       chs 		 * really a minor hack, but it wins some CPU back.
    716  1.28  wsanchez 		 */
    717  1.45       chs 
    718  1.28  wsanchez 		if (size <= 8 * 1048576) {
    719  1.28  wsanchez 			if ((p = mmap(NULL, (size_t)size, PROT_READ,
    720  1.45       chs 			    MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
    721  1.45       chs 			    == MAP_FAILED) {
    722  1.45       chs 				goto mmap_failed;
    723  1.36   thorpej 			}
    724  1.44       cgd #ifdef MADV_SEQUENTIAL
    725  1.39  christos 			if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
    726  1.39  christos 			    && errno != EOPNOTSUPP)
    727  1.38  sommerfe 				warnx("madvise: %s", strerror(errno));
    728  1.44       cgd #endif
    729  1.38  sommerfe 
    730  1.36   thorpej 			if (write(to_fd, p, size) != size) {
    731  1.36   thorpej 				serrno = errno;
    732  1.36   thorpej 				(void)unlink(to_name);
    733  1.66        tv 				errx(1, "%s: write: %s",
    734  1.36   thorpej 				    to_name, strerror(serrno));
    735  1.36   thorpej 			}
    736  1.28  wsanchez 		} else {
    737  1.45       chs mmap_failed:
    738  1.36   thorpej 			while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
    739  1.28  wsanchez 				if ((nw = write(to_fd, buf, nr)) != nr) {
    740  1.28  wsanchez 					serrno = errno;
    741  1.28  wsanchez 					(void)unlink(to_name);
    742  1.66        tv 					errx(1, "%s: write: %s", to_name,
    743  1.45       chs 					    strerror(nw > 0 ? EIO : serrno));
    744  1.28  wsanchez 				}
    745  1.36   thorpej 			}
    746  1.28  wsanchez 			if (nr != 0) {
    747   1.5       jtc 				serrno = errno;
    748   1.5       jtc 				(void)unlink(to_name);
    749  1.66        tv 				errx(1, "%s: read: %s", from_name, strerror(serrno));
    750   1.5       jtc 			}
    751   1.1       cgd 		}
    752   1.1       cgd 	}
    753   1.1       cgd }
    754   1.1       cgd 
    755   1.1       cgd /*
    756   1.1       cgd  * strip --
    757   1.1       cgd  *	use strip(1) to strip the target file
    758   1.1       cgd  */
    759   1.5       jtc void
    760  1.48    simonb strip(char *to_name)
    761   1.1       cgd {
    762  1.53     lukem 	int	serrno, status;
    763  1.53     lukem 	char	*stripprog;
    764   1.1       cgd 
    765   1.1       cgd 	switch (vfork()) {
    766   1.1       cgd 	case -1:
    767   1.5       jtc 		serrno = errno;
    768   1.5       jtc 		(void)unlink(to_name);
    769  1.22   thorpej 		errx(1, "vfork: %s", strerror(serrno));
    770  1.61    simonb 		/*NOTREACHED*/
    771   1.1       cgd 	case 0:
    772  1.18   thorpej 		stripprog = getenv("STRIP");
    773  1.18   thorpej 		if (stripprog == NULL)
    774  1.18   thorpej 			stripprog = _PATH_STRIP;
    775  1.28  wsanchez 
    776  1.28  wsanchez 		if (stripArgs) {
    777  1.50     lukem 			/*
    778  1.50     lukem 			 * build up a command line and let /bin/sh
    779  1.50     lukem 			 * parse the arguments
    780  1.50     lukem 			 */
    781  1.28  wsanchez 			char* cmd = (char*)malloc(sizeof(char)*
    782  1.28  wsanchez 						  (3+strlen(stripprog)+
    783  1.28  wsanchez 						     strlen(stripArgs)+
    784  1.28  wsanchez 						     strlen(to_name)));
    785  1.49    simonb 
    786  1.49    simonb 			if (cmd == NULL)
    787  1.49    simonb 				errx(1, "%s", strerror(ENOMEM));
    788  1.28  wsanchez 
    789  1.28  wsanchez 			sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
    790  1.28  wsanchez 
    791  1.28  wsanchez 			execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    792  1.28  wsanchez 		} else
    793  1.40       cgd 			execlp(stripprog, "strip", to_name, NULL);
    794  1.28  wsanchez 
    795  1.66        tv 		warn("%s: exec of strip", stripprog);
    796  1.22   thorpej 		_exit(1);
    797  1.61    simonb 		/*NOTREACHED*/
    798   1.1       cgd 	default:
    799   1.1       cgd 		if (wait(&status) == -1 || status)
    800   1.5       jtc 			(void)unlink(to_name);
    801   1.1       cgd 	}
    802  1.32   hubertf }
    803  1.32   hubertf 
    804  1.32   hubertf /*
    805  1.69     lukem  * afterinstall --
    806  1.69     lukem  *	run provided command on the target file or directory after it's been
    807  1.69     lukem  *	installed and stripped, but before permissions are set or it's renamed
    808  1.69     lukem  */
    809  1.69     lukem void
    810  1.69     lukem afterinstall(const char *command, const char *to_name, int errunlink)
    811  1.69     lukem {
    812  1.69     lukem 	int	serrno, status;
    813  1.69     lukem 	char	*cmd;
    814  1.69     lukem 
    815  1.69     lukem 	switch (vfork()) {
    816  1.69     lukem 	case -1:
    817  1.69     lukem 		serrno = errno;
    818  1.69     lukem 		if (errunlink)
    819  1.69     lukem 			(void)unlink(to_name);
    820  1.69     lukem 		errx(1, "vfork: %s", strerror(serrno));
    821  1.69     lukem 		/*NOTREACHED*/
    822  1.69     lukem 	case 0:
    823  1.69     lukem 		/*
    824  1.69     lukem 		 * build up a command line and let /bin/sh
    825  1.69     lukem 		 * parse the arguments
    826  1.69     lukem 		 */
    827  1.69     lukem 		cmd = (char*)malloc(sizeof(char)*
    828  1.69     lukem 					  (2+strlen(command)+
    829  1.69     lukem 					     strlen(to_name)));
    830  1.69     lukem 
    831  1.69     lukem 		if (cmd == NULL)
    832  1.69     lukem 			errx(1, "%s", strerror(ENOMEM));
    833  1.69     lukem 
    834  1.69     lukem 		sprintf(cmd, "%s %s", command, to_name);
    835  1.69     lukem 
    836  1.69     lukem 		execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    837  1.69     lukem 
    838  1.69     lukem 		warn("%s: exec of after install command", command);
    839  1.69     lukem 		_exit(1);
    840  1.69     lukem 		/*NOTREACHED*/
    841  1.69     lukem 	default:
    842  1.69     lukem 		if ((wait(&status) == -1 || status) && errunlink)
    843  1.69     lukem 			(void)unlink(to_name);
    844  1.69     lukem 	}
    845  1.69     lukem }
    846  1.69     lukem 
    847  1.69     lukem /*
    848  1.61    simonb  * backup --
    849  1.61    simonb  *	backup file "to_name" to to_name.suffix
    850  1.61    simonb  *	if suffix contains a "%", it's taken as a printf(3) pattern
    851  1.61    simonb  *	used for a numbered backup.
    852  1.32   hubertf  */
    853  1.32   hubertf void
    854  1.48    simonb backup(const char *to_name)
    855  1.32   hubertf {
    856  1.61    simonb 	char	bname[FILENAME_MAX];
    857  1.32   hubertf 
    858  1.35   hubertf 	if (numberedbackup) {
    859  1.32   hubertf 		/* Do numbered backup */
    860  1.32   hubertf 		int cnt;
    861  1.32   hubertf 		char suffix_expanded[FILENAME_MAX];
    862  1.32   hubertf 
    863  1.32   hubertf 		cnt=0;
    864  1.32   hubertf 		do {
    865  1.50     lukem 			(void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
    866  1.50     lukem 			    cnt);
    867  1.61    simonb 			(void)snprintf(bname, FILENAME_MAX, "%s%s", to_name,
    868  1.61    simonb 			    suffix_expanded);
    869  1.32   hubertf 			cnt++;
    870  1.61    simonb 		} while (access(bname, F_OK) == 0);
    871  1.32   hubertf 	} else {
    872  1.32   hubertf 		/* Do simple backup */
    873  1.61    simonb 		(void)snprintf(bname, FILENAME_MAX, "%s%s", to_name, suffix);
    874  1.32   hubertf 	}
    875  1.32   hubertf 
    876  1.61    simonb 	(void)rename(to_name, bname);
    877   1.1       cgd }
    878   1.1       cgd 
    879   1.1       cgd /*
    880   1.5       jtc  * install_dir --
    881  1.47       wiz  *	build directory hierarchy
    882   1.2       jtc  */
    883   1.7       jtc void
    884  1.50     lukem install_dir(char *path, u_int flags)
    885   1.2       jtc {
    886  1.53     lukem         char		*p;
    887  1.53     lukem         struct stat	sb;
    888  1.53     lukem         int		ch;
    889   1.2       jtc 
    890   1.4       cgd         for (p = path;; ++p)
    891   1.4       cgd                 if (!*p || (p != path && *p  == '/')) {
    892   1.2       jtc                         ch = *p;
    893   1.2       jtc                         *p = '\0';
    894   1.2       jtc                         if (stat(path, &sb)) {
    895   1.2       jtc                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    896  1.66        tv 					err(1, "%s: mkdir", path);
    897   1.2       jtc                                 }
    898   1.2       jtc                         }
    899   1.2       jtc                         if (!(*p = ch))
    900   1.4       cgd 				break;
    901   1.2       jtc                 }
    902   1.2       jtc 
    903  1.69     lukem 	if (afterinstallcmd != NULL)
    904  1.69     lukem 		afterinstall(afterinstallcmd, path, 0);
    905  1.69     lukem 
    906  1.50     lukem 	if (!dounpriv && (
    907  1.50     lukem 	    ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
    908  1.50     lukem 	    || chmod(path, mode) == -1 )) {
    909  1.66        tv                 warn("%s: chown/chmod", path);
    910  1.41       cgd 	}
    911  1.59     perry 	metadata_log(path, "dir", NULL, NULL);
    912   1.2       jtc }
    913   1.2       jtc 
    914   1.2       jtc /*
    915  1.50     lukem  * metadata_log --
    916  1.50     lukem  *	if metafp is not NULL, output mtree(8) full path name and settings to
    917  1.50     lukem  *	metafp, to allow permissions to be set correctly by other tools.
    918  1.50     lukem  */
    919  1.50     lukem void
    920  1.59     perry metadata_log(const char *path, const char *type, struct timeval *tv,
    921  1.57     lukem 	const char *link)
    922  1.50     lukem {
    923  1.72      yamt 	static const char	extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
    924  1.53     lukem 	char		*buf;
    925  1.73     lukem 	struct flock	metalog_lock;
    926  1.50     lukem 
    927  1.50     lukem 	if (!metafp)
    928  1.50     lukem 		return;
    929  1.50     lukem 	buf = (char *)malloc(4 * strlen(path) + 1);	/* buf for strsvis(3) */
    930  1.50     lukem 	if (buf == NULL) {
    931  1.50     lukem 		warnx("%s", strerror(ENOMEM));
    932  1.50     lukem 		return;
    933  1.50     lukem 	}
    934  1.73     lukem 							/* lock log file */
    935  1.73     lukem 	metalog_lock.l_start = 0;
    936  1.73     lukem 	metalog_lock.l_len = 0;
    937  1.73     lukem 	metalog_lock.l_whence = SEEK_SET;
    938  1.73     lukem 	metalog_lock.l_type = F_WRLCK;
    939  1.73     lukem 	if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
    940  1.50     lukem 		warn("can't lock %s", metafile);
    941  1.50     lukem 		return;
    942  1.50     lukem 	}
    943  1.50     lukem 
    944  1.50     lukem 	strsvis(buf, path, VIS_CSTYLE, extra);		/* encode name */
    945  1.50     lukem 	fprintf(metafp, ".%s%s type=%s mode=%#o",	/* print details */
    946  1.59     perry 	    buf[0] == '/' ? "" : "/", buf, type, mode);
    947  1.57     lukem 	if (link)
    948  1.57     lukem 		fprintf(metafp, " link=%s", link);
    949  1.52        tv 	if (owner)
    950  1.52        tv 		fprintf(metafp, " uname=%s", owner);
    951  1.52        tv 	if (group)
    952  1.52        tv 		fprintf(metafp, " gname=%s", group);
    953  1.52        tv 	if (fflags)
    954  1.52        tv 		fprintf(metafp, " flags=%s", fflags);
    955  1.54     lukem 	if (tags)
    956  1.54     lukem 		fprintf(metafp, " tags=%s", tags);
    957  1.57     lukem 	if (tv != NULL && dopreserve)
    958  1.50     lukem 		fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
    959  1.50     lukem 	fputc('\n', metafp);
    960  1.50     lukem 	fflush(metafp);					/* flush output */
    961  1.73     lukem 							/* unlock log file */
    962  1.73     lukem 	metalog_lock.l_type = F_UNLCK;
    963  1.73     lukem 	if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
    964  1.50     lukem 		warn("can't unlock %s", metafile);
    965  1.50     lukem 	}
    966  1.50     lukem 	free(buf);
    967  1.50     lukem }
    968  1.50     lukem 
    969  1.61    simonb /*
    970  1.61    simonb  * xbasename --
    971  1.61    simonb  *	libc basename(3) that returns a pointer to a static buffer
    972  1.61    simonb  *	instead of overwriting that passed-in string.
    973  1.61    simonb  */
    974  1.61    simonb char *
    975  1.61    simonb xbasename(char *path)
    976  1.61    simonb {
    977  1.61    simonb 	static char tmp[MAXPATHLEN];
    978  1.61    simonb 
    979  1.61    simonb 	(void)strlcpy(tmp, path, sizeof(tmp));
    980  1.61    simonb 	return (basename(tmp));
    981  1.61    simonb }
    982  1.61    simonb 
    983  1.61    simonb /*
    984  1.61    simonb  * xdirname --
    985  1.61    simonb  *	libc dirname(3) that returns a pointer to a static buffer
    986  1.61    simonb  *	instead of overwriting that passed-in string.
    987  1.61    simonb  */
    988  1.61    simonb char *
    989  1.61    simonb xdirname(char *path)
    990  1.61    simonb {
    991  1.61    simonb 	static char tmp[MAXPATHLEN];
    992  1.50     lukem 
    993  1.61    simonb 	(void)strlcpy(tmp, path, sizeof(tmp));
    994  1.61    simonb 	return (dirname(tmp));
    995  1.61    simonb }
    996  1.50     lukem 
    997  1.50     lukem /*
    998   1.1       cgd  * usage --
    999   1.1       cgd  *	print a usage message and die
   1000   1.1       cgd  */
   1001   1.5       jtc void
   1002  1.48    simonb usage(void)
   1003   1.1       cgd {
   1004  1.69     lukem 	const char *prog;
   1005  1.69     lukem 
   1006  1.69     lukem 	prog = getprogname();
   1007  1.53     lukem 
   1008  1.69     lukem 	(void)fprintf(stderr,
   1009  1.69     lukem "usage: %s [-Ubcprs] [-M log] [-T tags] [-B suffix] [-a afterinstallcmd]\n"
   1010  1.69     lukem "           [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags]\n"
   1011  1.69     lukem "           [-S stripflags] file1 file2\n"
   1012  1.69     lukem "       %s [-Ubcprs] [-M log] [-T tags] [-B suffix] [-a afterinstallcmd]\n"
   1013  1.69     lukem "           [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags]\n"
   1014  1.69     lukem "           [-S stripflags] file1 ... fileN directory\n"
   1015  1.69     lukem "       %s -d [-Up] [-M log] [-T tags] [-a afterinstallcmd] [-m mode]\n"
   1016  1.69     lukem "           [-o owner] [-g group] directory ...\n",
   1017  1.69     lukem 	    prog, prog, prog);
   1018   1.6       jtc 	exit(1);
   1019   1.1       cgd }
   1020