Home | History | Annotate | Line # | Download | only in xinstall
xinstall.c revision 1.70
      1  1.70     lukem /*	$NetBSD: xinstall.c,v 1.70 2002/04/10 06:02:52 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.70     lukem __RCSID("$NetBSD: xinstall.c,v 1.70 2002/04/10 06:02:52 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.70     lukem 	if (stat(from_name, &from_sb))
    531  1.70     lukem 		err(1, "%s: stat", from_name);
    532  1.70     lukem #ifdef BSD4_4
    533  1.70     lukem 	TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
    534  1.70     lukem 	TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
    535  1.70     lukem #else
    536  1.70     lukem 	tv[0].tv_sec = from_sb.st_atime;
    537  1.70     lukem 	tv[0].tv_usec = 0;
    538  1.70     lukem 	tv[1].tv_sec = from_sb.st_mtime;
    539  1.70     lukem 	tv[1].tv_usec = 0;
    540  1.70     lukem #endif
    541  1.70     lukem 
    542   1.5       jtc 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
    543  1.56     perry 		if (!dolink) {
    544  1.56     perry 			if (!S_ISREG(from_sb.st_mode))
    545  1.56     perry 				errx(1, "%s: not a regular file", from_name);
    546  1.56     perry 		}
    547   1.5       jtc 		/* Build the target path. */
    548   1.5       jtc 		if (flags & DIRECTORY) {
    549   1.5       jtc 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
    550   1.5       jtc 			    to_name,
    551  1.21     lukem 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
    552   1.1       cgd 			to_name = pathbuf;
    553   1.1       cgd 		}
    554   1.1       cgd 		devnull = 0;
    555   1.5       jtc 	} else {
    556  1.65        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    557   1.5       jtc 		from_sb.st_flags = 0;	/* XXX */
    558  1.65        tv #endif
    559   1.1       cgd 		devnull = 1;
    560   1.5       jtc 	}
    561   1.1       cgd 
    562   1.5       jtc 	/*
    563   1.5       jtc 	 * Unlink now... avoid ETXTBSY errors later.  Try and turn
    564   1.5       jtc 	 * off the append/immutable bits -- if we fail, go ahead,
    565   1.5       jtc 	 * it might work.
    566   1.5       jtc 	 */
    567  1.65        tv #if !HAVE_CONFIG_H
    568   1.5       jtc 	if (stat(to_name, &to_sb) == 0 &&
    569   1.5       jtc 	    to_sb.st_flags & (NOCHANGEBITS))
    570   1.5       jtc 		(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
    571  1.65        tv #endif
    572  1.33  christos 	if (dorename) {
    573  1.64   msaitoh 		(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
    574  1.61    simonb 		    xdirname(to_name));
    575  1.33  christos 		oto_name = to_name;
    576  1.33  christos 		to_name = tmpl;
    577  1.33  christos 	} else {
    578  1.37  christos 		oto_name = NULL;	/* pacify gcc */
    579  1.33  christos 		if (dobackup)
    580  1.33  christos 			backup(to_name);
    581  1.33  christos 		else
    582  1.33  christos 			(void)unlink(to_name);
    583  1.33  christos 	}
    584  1.16  christos 
    585  1.16  christos 	if (dolink) {
    586  1.61    simonb 		makelink(from_name, dorename ? oto_name : to_name);
    587  1.16  christos 		return;
    588  1.16  christos 	}
    589   1.1       cgd 
    590   1.5       jtc 	/* Create target. */
    591  1.33  christos 	if (dorename) {
    592  1.33  christos 		if ((to_fd = mkstemp(to_name)) == -1)
    593  1.66        tv 			err(1, "%s: mkstemp", to_name);
    594  1.33  christos 	} else {
    595  1.33  christos 		if ((to_fd = open(to_name,
    596  1.33  christos 		    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
    597  1.66        tv 			err(1, "%s: open", to_name);
    598  1.33  christos 	}
    599   1.1       cgd 	if (!devnull) {
    600   1.1       cgd 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
    601   1.1       cgd 			(void)unlink(to_name);
    602  1.66        tv 			err(1, "%s: open", from_name);
    603   1.1       cgd 		}
    604   1.5       jtc 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
    605   1.1       cgd 		(void)close(from_fd);
    606   1.1       cgd 	}
    607   1.9  jonathan 
    608   1.9  jonathan 	if (dostrip) {
    609   1.1       cgd 		strip(to_name);
    610   1.9  jonathan 
    611   1.9  jonathan 		/*
    612   1.9  jonathan 		 * Re-open our fd on the target, in case we used a strip
    613   1.9  jonathan 		 *  that does not work in-place -- like gnu binutils strip.
    614   1.9  jonathan 		 */
    615   1.9  jonathan 		close(to_fd);
    616   1.9  jonathan 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    617  1.69     lukem 			err(1, "stripping %s", to_name);
    618  1.69     lukem 	}
    619  1.69     lukem 
    620  1.69     lukem 	if (afterinstallcmd != NULL) {
    621  1.69     lukem 		afterinstall(afterinstallcmd, to_name, 1);
    622  1.69     lukem 
    623  1.69     lukem 		/*
    624  1.69     lukem 		 * Re-open our fd on the target, in case we used an
    625  1.69     lukem 		 * after-install command that does not work in-place
    626  1.69     lukem 		 */
    627  1.69     lukem 		close(to_fd);
    628  1.69     lukem 		if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
    629  1.69     lukem 			err(1, "running after install command on %s", to_name);
    630   1.9  jonathan 	}
    631   1.9  jonathan 
    632  1.41       cgd 	/*
    633  1.41       cgd 	 * Set owner, group, mode for target; do the chown first,
    634  1.41       cgd 	 * chown may lose the setuid bits.
    635  1.41       cgd 	 */
    636  1.50     lukem 	if (!dounpriv &&
    637  1.50     lukem 	    (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
    638  1.41       cgd 		serrno = errno;
    639  1.41       cgd 		(void)unlink(to_name);
    640  1.41       cgd 		errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
    641   1.1       cgd 	}
    642  1.60     dillo 	tmpmode = mode;
    643  1.58        tv 	if (dounpriv)
    644  1.60     dillo 		tmpmode &= S_IRWXU|S_IRWXG|S_IRWXO;
    645  1.60     dillo 	if (fchmod(to_fd, tmpmode) == -1) {
    646   1.5       jtc 		serrno = errno;
    647   1.5       jtc 		(void)unlink(to_name);
    648   1.5       jtc 		errx(1, "%s: chmod: %s", to_name, strerror(serrno));
    649   1.5       jtc 	}
    650   1.5       jtc 
    651   1.5       jtc 	/*
    652  1.26  christos 	 * Preserve the date of the source file.
    653  1.26  christos 	 */
    654  1.26  christos 	if (dopreserve) {
    655  1.65        tv #if HAVE_FUTIMES
    656  1.65        tv 		if (futimes(to_fd, tv) == -1)
    657  1.65        tv 			warn("%s: futimes", to_name);
    658  1.65        tv #else
    659  1.65        tv 		if (utimes(to_name, tv) == -1)
    660  1.66        tv 			warn("%s: utimes", to_name);
    661  1.65        tv #endif
    662   1.5       jtc 	}
    663   1.5       jtc 
    664   1.1       cgd 	(void)close(to_fd);
    665  1.33  christos 
    666  1.50     lukem 	if (dorename) {
    667  1.33  christos 		if (rename(to_name, oto_name) == -1)
    668  1.33  christos 			err(1, "%s: rename", to_name);
    669  1.50     lukem 		to_name = oto_name;
    670  1.50     lukem 	}
    671  1.33  christos 
    672   1.5       jtc 	if (!docopy && !devnull && unlink(from_name))
    673  1.66        tv 		err(1, "%s: unlink", from_name);
    674  1.50     lukem 
    675  1.50     lukem 	/*
    676  1.50     lukem 	 * If provided a set of flags, set them, otherwise, preserve the
    677  1.50     lukem 	 * flags, except for the dump flag.
    678  1.50     lukem 	 */
    679  1.65        tv #if !HAVE_CONFIG_H
    680  1.50     lukem 	if (!dounpriv && chflags(to_name,
    681  1.51     lukem 	    flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
    682  1.51     lukem 	{
    683  1.50     lukem 		if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
    684  1.50     lukem 			warn("%s: chflags", to_name);
    685  1.50     lukem 	}
    686  1.65        tv #endif
    687  1.50     lukem 
    688  1.59     perry 	metadata_log(to_name, "file", tv, NULL);
    689   1.1       cgd }
    690   1.1       cgd 
    691   1.1       cgd /*
    692   1.1       cgd  * copy --
    693   1.1       cgd  *	copy from one file to another
    694   1.1       cgd  */
    695   1.5       jtc void
    696  1.48    simonb copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
    697   1.1       cgd {
    698  1.53     lukem 	ssize_t	nr, nw;
    699  1.53     lukem 	int	serrno;
    700  1.53     lukem 	char	*p;
    701  1.53     lukem 	char	buf[MAXBSIZE];
    702   1.1       cgd 
    703   1.5       jtc 	/*
    704  1.28  wsanchez 	 * There's no reason to do anything other than close the file
    705  1.28  wsanchez 	 * now if it's empty, so let's not bother.
    706   1.5       jtc 	 */
    707  1.28  wsanchez 	if (size > 0) {
    708  1.45       chs 
    709  1.28  wsanchez 		/*
    710  1.45       chs 		 * Mmap and write if less than 8M (the limit is so we
    711  1.45       chs 		 * don't totally trash memory on big files).  This is
    712  1.45       chs 		 * really a minor hack, but it wins some CPU back.
    713  1.28  wsanchez 		 */
    714  1.45       chs 
    715  1.28  wsanchez 		if (size <= 8 * 1048576) {
    716  1.28  wsanchez 			if ((p = mmap(NULL, (size_t)size, PROT_READ,
    717  1.45       chs 			    MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
    718  1.45       chs 			    == MAP_FAILED) {
    719  1.45       chs 				goto mmap_failed;
    720  1.36   thorpej 			}
    721  1.44       cgd #ifdef MADV_SEQUENTIAL
    722  1.39  christos 			if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
    723  1.39  christos 			    && errno != EOPNOTSUPP)
    724  1.38  sommerfe 				warnx("madvise: %s", strerror(errno));
    725  1.44       cgd #endif
    726  1.38  sommerfe 
    727  1.36   thorpej 			if (write(to_fd, p, size) != size) {
    728  1.36   thorpej 				serrno = errno;
    729  1.36   thorpej 				(void)unlink(to_name);
    730  1.66        tv 				errx(1, "%s: write: %s",
    731  1.36   thorpej 				    to_name, strerror(serrno));
    732  1.36   thorpej 			}
    733  1.28  wsanchez 		} else {
    734  1.45       chs mmap_failed:
    735  1.36   thorpej 			while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
    736  1.28  wsanchez 				if ((nw = write(to_fd, buf, nr)) != nr) {
    737  1.28  wsanchez 					serrno = errno;
    738  1.28  wsanchez 					(void)unlink(to_name);
    739  1.66        tv 					errx(1, "%s: write: %s", to_name,
    740  1.45       chs 					    strerror(nw > 0 ? EIO : serrno));
    741  1.28  wsanchez 				}
    742  1.36   thorpej 			}
    743  1.28  wsanchez 			if (nr != 0) {
    744   1.5       jtc 				serrno = errno;
    745   1.5       jtc 				(void)unlink(to_name);
    746  1.66        tv 				errx(1, "%s: read: %s", from_name, strerror(serrno));
    747   1.5       jtc 			}
    748   1.1       cgd 		}
    749   1.1       cgd 	}
    750   1.1       cgd }
    751   1.1       cgd 
    752   1.1       cgd /*
    753   1.1       cgd  * strip --
    754   1.1       cgd  *	use strip(1) to strip the target file
    755   1.1       cgd  */
    756   1.5       jtc void
    757  1.48    simonb strip(char *to_name)
    758   1.1       cgd {
    759  1.53     lukem 	int	serrno, status;
    760  1.53     lukem 	char	*stripprog;
    761   1.1       cgd 
    762   1.1       cgd 	switch (vfork()) {
    763   1.1       cgd 	case -1:
    764   1.5       jtc 		serrno = errno;
    765   1.5       jtc 		(void)unlink(to_name);
    766  1.22   thorpej 		errx(1, "vfork: %s", strerror(serrno));
    767  1.61    simonb 		/*NOTREACHED*/
    768   1.1       cgd 	case 0:
    769  1.18   thorpej 		stripprog = getenv("STRIP");
    770  1.18   thorpej 		if (stripprog == NULL)
    771  1.18   thorpej 			stripprog = _PATH_STRIP;
    772  1.28  wsanchez 
    773  1.28  wsanchez 		if (stripArgs) {
    774  1.50     lukem 			/*
    775  1.50     lukem 			 * build up a command line and let /bin/sh
    776  1.50     lukem 			 * parse the arguments
    777  1.50     lukem 			 */
    778  1.28  wsanchez 			char* cmd = (char*)malloc(sizeof(char)*
    779  1.28  wsanchez 						  (3+strlen(stripprog)+
    780  1.28  wsanchez 						     strlen(stripArgs)+
    781  1.28  wsanchez 						     strlen(to_name)));
    782  1.49    simonb 
    783  1.49    simonb 			if (cmd == NULL)
    784  1.49    simonb 				errx(1, "%s", strerror(ENOMEM));
    785  1.28  wsanchez 
    786  1.28  wsanchez 			sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
    787  1.28  wsanchez 
    788  1.28  wsanchez 			execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    789  1.28  wsanchez 		} else
    790  1.40       cgd 			execlp(stripprog, "strip", to_name, NULL);
    791  1.28  wsanchez 
    792  1.66        tv 		warn("%s: exec of strip", stripprog);
    793  1.22   thorpej 		_exit(1);
    794  1.61    simonb 		/*NOTREACHED*/
    795   1.1       cgd 	default:
    796   1.1       cgd 		if (wait(&status) == -1 || status)
    797   1.5       jtc 			(void)unlink(to_name);
    798   1.1       cgd 	}
    799  1.32   hubertf }
    800  1.32   hubertf 
    801  1.32   hubertf /*
    802  1.69     lukem  * afterinstall --
    803  1.69     lukem  *	run provided command on the target file or directory after it's been
    804  1.69     lukem  *	installed and stripped, but before permissions are set or it's renamed
    805  1.69     lukem  */
    806  1.69     lukem void
    807  1.69     lukem afterinstall(const char *command, const char *to_name, int errunlink)
    808  1.69     lukem {
    809  1.69     lukem 	int	serrno, status;
    810  1.69     lukem 	char	*cmd;
    811  1.69     lukem 
    812  1.69     lukem 	switch (vfork()) {
    813  1.69     lukem 	case -1:
    814  1.69     lukem 		serrno = errno;
    815  1.69     lukem 		if (errunlink)
    816  1.69     lukem 			(void)unlink(to_name);
    817  1.69     lukem 		errx(1, "vfork: %s", strerror(serrno));
    818  1.69     lukem 		/*NOTREACHED*/
    819  1.69     lukem 	case 0:
    820  1.69     lukem 		/*
    821  1.69     lukem 		 * build up a command line and let /bin/sh
    822  1.69     lukem 		 * parse the arguments
    823  1.69     lukem 		 */
    824  1.69     lukem 		cmd = (char*)malloc(sizeof(char)*
    825  1.69     lukem 					  (2+strlen(command)+
    826  1.69     lukem 					     strlen(to_name)));
    827  1.69     lukem 
    828  1.69     lukem 		if (cmd == NULL)
    829  1.69     lukem 			errx(1, "%s", strerror(ENOMEM));
    830  1.69     lukem 
    831  1.69     lukem 		sprintf(cmd, "%s %s", command, to_name);
    832  1.69     lukem 
    833  1.69     lukem 		execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
    834  1.69     lukem 
    835  1.69     lukem 		warn("%s: exec of after install command", command);
    836  1.69     lukem 		_exit(1);
    837  1.69     lukem 		/*NOTREACHED*/
    838  1.69     lukem 	default:
    839  1.69     lukem 		if ((wait(&status) == -1 || status) && errunlink)
    840  1.69     lukem 			(void)unlink(to_name);
    841  1.69     lukem 	}
    842  1.69     lukem }
    843  1.69     lukem 
    844  1.69     lukem /*
    845  1.61    simonb  * backup --
    846  1.61    simonb  *	backup file "to_name" to to_name.suffix
    847  1.61    simonb  *	if suffix contains a "%", it's taken as a printf(3) pattern
    848  1.61    simonb  *	used for a numbered backup.
    849  1.32   hubertf  */
    850  1.32   hubertf void
    851  1.48    simonb backup(const char *to_name)
    852  1.32   hubertf {
    853  1.61    simonb 	char	bname[FILENAME_MAX];
    854  1.32   hubertf 
    855  1.35   hubertf 	if (numberedbackup) {
    856  1.32   hubertf 		/* Do numbered backup */
    857  1.32   hubertf 		int cnt;
    858  1.32   hubertf 		char suffix_expanded[FILENAME_MAX];
    859  1.32   hubertf 
    860  1.32   hubertf 		cnt=0;
    861  1.32   hubertf 		do {
    862  1.50     lukem 			(void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
    863  1.50     lukem 			    cnt);
    864  1.61    simonb 			(void)snprintf(bname, FILENAME_MAX, "%s%s", to_name,
    865  1.61    simonb 			    suffix_expanded);
    866  1.32   hubertf 			cnt++;
    867  1.61    simonb 		} while (access(bname, F_OK) == 0);
    868  1.32   hubertf 	} else {
    869  1.32   hubertf 		/* Do simple backup */
    870  1.61    simonb 		(void)snprintf(bname, FILENAME_MAX, "%s%s", to_name, suffix);
    871  1.32   hubertf 	}
    872  1.32   hubertf 
    873  1.61    simonb 	(void)rename(to_name, bname);
    874   1.1       cgd }
    875   1.1       cgd 
    876   1.1       cgd /*
    877   1.5       jtc  * install_dir --
    878  1.47       wiz  *	build directory hierarchy
    879   1.2       jtc  */
    880   1.7       jtc void
    881  1.50     lukem install_dir(char *path, u_int flags)
    882   1.2       jtc {
    883  1.53     lukem         char		*p;
    884  1.53     lukem         struct stat	sb;
    885  1.53     lukem         int		ch;
    886   1.2       jtc 
    887   1.4       cgd         for (p = path;; ++p)
    888   1.4       cgd                 if (!*p || (p != path && *p  == '/')) {
    889   1.2       jtc                         ch = *p;
    890   1.2       jtc                         *p = '\0';
    891   1.2       jtc                         if (stat(path, &sb)) {
    892   1.2       jtc                                 if (errno != ENOENT || mkdir(path, 0777) < 0) {
    893  1.66        tv 					err(1, "%s: mkdir", path);
    894   1.2       jtc                                 }
    895   1.2       jtc                         }
    896   1.2       jtc                         if (!(*p = ch))
    897   1.4       cgd 				break;
    898   1.2       jtc                 }
    899   1.2       jtc 
    900  1.69     lukem 	if (afterinstallcmd != NULL)
    901  1.69     lukem 		afterinstall(afterinstallcmd, path, 0);
    902  1.69     lukem 
    903  1.50     lukem 	if (!dounpriv && (
    904  1.50     lukem 	    ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
    905  1.50     lukem 	    || chmod(path, mode) == -1 )) {
    906  1.66        tv                 warn("%s: chown/chmod", path);
    907  1.41       cgd 	}
    908  1.59     perry 	metadata_log(path, "dir", NULL, NULL);
    909   1.2       jtc }
    910   1.2       jtc 
    911   1.2       jtc /*
    912  1.50     lukem  * metadata_log --
    913  1.50     lukem  *	if metafp is not NULL, output mtree(8) full path name and settings to
    914  1.50     lukem  *	metafp, to allow permissions to be set correctly by other tools.
    915  1.50     lukem  */
    916  1.50     lukem void
    917  1.59     perry metadata_log(const char *path, const char *type, struct timeval *tv,
    918  1.57     lukem 	const char *link)
    919  1.50     lukem {
    920  1.50     lukem 	const char	extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
    921  1.53     lukem 	char		*buf;
    922  1.50     lukem 
    923  1.50     lukem 	if (!metafp)
    924  1.50     lukem 		return;
    925  1.50     lukem 	buf = (char *)malloc(4 * strlen(path) + 1);	/* buf for strsvis(3) */
    926  1.50     lukem 	if (buf == NULL) {
    927  1.50     lukem 		warnx("%s", strerror(ENOMEM));
    928  1.50     lukem 		return;
    929  1.50     lukem 	}
    930  1.50     lukem 	if (flock(fileno(metafp), LOCK_EX) == -1) {	/* lock log file */
    931  1.50     lukem 		warn("can't lock %s", metafile);
    932  1.50     lukem 		return;
    933  1.50     lukem 	}
    934  1.50     lukem 
    935  1.50     lukem 	strsvis(buf, path, VIS_CSTYLE, extra);		/* encode name */
    936  1.50     lukem 	fprintf(metafp, ".%s%s type=%s mode=%#o",	/* print details */
    937  1.59     perry 	    buf[0] == '/' ? "" : "/", buf, type, mode);
    938  1.57     lukem 	if (link)
    939  1.57     lukem 		fprintf(metafp, " link=%s", link);
    940  1.52        tv 	if (owner)
    941  1.52        tv 		fprintf(metafp, " uname=%s", owner);
    942  1.52        tv 	if (group)
    943  1.52        tv 		fprintf(metafp, " gname=%s", group);
    944  1.52        tv 	if (fflags)
    945  1.52        tv 		fprintf(metafp, " flags=%s", fflags);
    946  1.54     lukem 	if (tags)
    947  1.54     lukem 		fprintf(metafp, " tags=%s", tags);
    948  1.57     lukem 	if (tv != NULL && dopreserve)
    949  1.50     lukem 		fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
    950  1.50     lukem 	fputc('\n', metafp);
    951  1.50     lukem 	fflush(metafp);					/* flush output */
    952  1.50     lukem 	if (flock(fileno(metafp), LOCK_UN) == -1) {	/* unlock log file */
    953  1.50     lukem 		warn("can't unlock %s", metafile);
    954  1.50     lukem 	}
    955  1.50     lukem 	free(buf);
    956  1.50     lukem }
    957  1.50     lukem 
    958  1.61    simonb /*
    959  1.61    simonb  * xbasename --
    960  1.61    simonb  *	libc basename(3) that returns a pointer to a static buffer
    961  1.61    simonb  *	instead of overwriting that passed-in string.
    962  1.61    simonb  */
    963  1.61    simonb char *
    964  1.61    simonb xbasename(char *path)
    965  1.61    simonb {
    966  1.61    simonb 	static char tmp[MAXPATHLEN];
    967  1.61    simonb 
    968  1.61    simonb 	(void)strlcpy(tmp, path, sizeof(tmp));
    969  1.61    simonb 	return (basename(tmp));
    970  1.61    simonb }
    971  1.61    simonb 
    972  1.61    simonb /*
    973  1.61    simonb  * xdirname --
    974  1.61    simonb  *	libc dirname(3) that returns a pointer to a static buffer
    975  1.61    simonb  *	instead of overwriting that passed-in string.
    976  1.61    simonb  */
    977  1.61    simonb char *
    978  1.61    simonb xdirname(char *path)
    979  1.61    simonb {
    980  1.61    simonb 	static char tmp[MAXPATHLEN];
    981  1.50     lukem 
    982  1.61    simonb 	(void)strlcpy(tmp, path, sizeof(tmp));
    983  1.61    simonb 	return (dirname(tmp));
    984  1.61    simonb }
    985  1.50     lukem 
    986  1.50     lukem /*
    987   1.1       cgd  * usage --
    988   1.1       cgd  *	print a usage message and die
    989   1.1       cgd  */
    990   1.5       jtc void
    991  1.48    simonb usage(void)
    992   1.1       cgd {
    993  1.69     lukem 	const char *prog;
    994  1.69     lukem 
    995  1.69     lukem 	prog = getprogname();
    996  1.53     lukem 
    997  1.69     lukem 	(void)fprintf(stderr,
    998  1.69     lukem "usage: %s [-Ubcprs] [-M log] [-T tags] [-B suffix] [-a afterinstallcmd]\n"
    999  1.69     lukem "           [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags]\n"
   1000  1.69     lukem "           [-S stripflags] file1 file2\n"
   1001  1.69     lukem "       %s [-Ubcprs] [-M log] [-T tags] [-B suffix] [-a afterinstallcmd]\n"
   1002  1.69     lukem "           [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags]\n"
   1003  1.69     lukem "           [-S stripflags] file1 ... fileN directory\n"
   1004  1.69     lukem "       %s -d [-Up] [-M log] [-T tags] [-a afterinstallcmd] [-m mode]\n"
   1005  1.69     lukem "           [-o owner] [-g group] directory ...\n",
   1006  1.69     lukem 	    prog, prog, prog);
   1007   1.6       jtc 	exit(1);
   1008   1.1       cgd }
   1009