xinstall.c revision 96439
1/*
2 * Copyright (c) 1987, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1987, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)xinstall.c	8.1 (Berkeley) 7/21/93";
43#endif /* not lint */
44#endif
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/usr.bin/xinstall/xinstall.c 96439 2002-05-12 04:02:57Z bde $");
48
49#include <sys/param.h>
50#include <sys/mman.h>
51#include <sys/mount.h>
52#include <sys/stat.h>
53#include <sys/wait.h>
54
55#include <ctype.h>
56#include <err.h>
57#include <errno.h>
58#include <fcntl.h>
59#include <grp.h>
60#include <paths.h>
61#include <pwd.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <sysexits.h>
66#include <unistd.h>
67#include <utime.h>
68
69#include "pathnames.h"
70
71/* Bootstrap aid - this doesn't exist in most older releases */
72#ifndef MAP_FAILED
73#define MAP_FAILED ((void *)-1)	/* from <sys/mman.h> */
74#endif
75
76#define MAX_CMP_SIZE	(16 * 1024 * 1024)
77
78#define	DIRECTORY	0x01		/* Tell install it's a directory. */
79#define	SETFLAGS	0x02		/* Tell install to set flags. */
80#define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
81#define	BACKUP_SUFFIX	".old"
82
83struct passwd *pp;
84struct group *gp;
85gid_t gid;
86uid_t uid;
87int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
88mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
89const char *suffix = BACKUP_SUFFIX;
90
91void	copy(int, const char *, int, const char *, off_t);
92int	compare(int, const char *, size_t, int, const char *, size_t);
93int	create_newfile(const char *, int, struct stat *);
94int	create_tempfile(const char *, char *, size_t);
95void	install(const char *, const char *, u_long, u_int);
96void	install_dir(char *);
97u_long	numeric_id(const char *, const char *);
98void	strip(const char *);
99int	trymmap(int);
100void	usage(void);
101
102int
103main(argc, argv)
104	int argc;
105	char *argv[];
106{
107	struct stat from_sb, to_sb;
108	mode_t *set;
109	u_long fset;
110	int ch, no_target;
111	u_int iflags;
112	char *flags;
113	const char *group, *owner, *to_name;
114
115	iflags = 0;
116	group = owner = NULL;
117	while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:o:pSsv")) != -1)
118		switch((char)ch) {
119		case 'B':
120			suffix = optarg;
121			/* FALLTHROUGH */
122		case 'b':
123			dobackup = 1;
124			break;
125		case 'C':
126			docompare = 1;
127			break;
128		case 'c':
129			/* For backwards compatibility. */
130			break;
131		case 'd':
132			dodir = 1;
133			break;
134		case 'f':
135			flags = optarg;
136			if (strtofflags(&flags, &fset, NULL))
137				errx(EX_USAGE, "%s: invalid flag", flags);
138			iflags |= SETFLAGS;
139			break;
140		case 'g':
141			group = optarg;
142			break;
143		case 'M':
144			nommap = 1;
145			break;
146		case 'm':
147			if (!(set = setmode(optarg)))
148				errx(EX_USAGE, "invalid file mode: %s",
149				     optarg);
150			mode = getmode(set, 0);
151			free(set);
152			break;
153		case 'o':
154			owner = optarg;
155			break;
156		case 'p':
157			docompare = dopreserve = 1;
158			break;
159		case 'S':
160			safecopy = 1;
161			break;
162		case 's':
163			dostrip = 1;
164			break;
165		case 'v':
166			verbose = 1;
167			break;
168		case '?':
169		default:
170			usage();
171		}
172	argc -= optind;
173	argv += optind;
174
175	/* some options make no sense when creating directories */
176	if ((safecopy || dostrip) && dodir)
177		usage();
178
179	/*
180	 * Older versions allowed -d -C combo.  Issue a warning
181	 * for now, but turn this into an error before 4.5-RELEASE.
182	 */
183	if (docompare && dodir)
184		warnx("the -d and -C options may not be specified together");
185
186	/* must have at least two arguments, except when creating directories */
187	if (argc < 2 && !dodir)
188		usage();
189
190	/* need to make a temp copy so we can compare stripped version */
191	if (docompare && dostrip)
192		safecopy = 1;
193
194	/* get group and owner id's */
195	if (group != NULL) {
196		if ((gp = getgrnam(group)) != NULL)
197			gid = gp->gr_gid;
198		else
199			gid = (gid_t)numeric_id(group, "group");
200	} else
201		gid = (gid_t)-1;
202
203	if (owner != NULL) {
204		if ((pp = getpwnam(owner)) != NULL)
205			uid = pp->pw_uid;
206		else
207			uid = (uid_t)numeric_id(owner, "user");
208	} else
209		uid = (uid_t)-1;
210
211	if (dodir) {
212		for (; *argv != NULL; ++argv)
213			install_dir(*argv);
214		exit(EX_OK);
215		/* NOTREACHED */
216	}
217
218	no_target = stat(to_name = argv[argc - 1], &to_sb);
219	if (!no_target && S_ISDIR(to_sb.st_mode)) {
220		for (; *argv != to_name; ++argv)
221			install(*argv, to_name, fset, iflags | DIRECTORY);
222		exit(EX_OK);
223		/* NOTREACHED */
224	}
225
226	/* can't do file1 file2 directory/file */
227	if (argc != 2)
228		usage();
229
230	if (!no_target) {
231		if (stat(*argv, &from_sb))
232			err(EX_OSERR, "%s", *argv);
233		if (!S_ISREG(to_sb.st_mode)) {
234			errno = EFTYPE;
235			err(EX_OSERR, "%s", to_name);
236		}
237		if (to_sb.st_dev == from_sb.st_dev &&
238		    to_sb.st_ino == from_sb.st_ino)
239			errx(EX_USAGE,
240			    "%s and %s are the same file", *argv, to_name);
241	}
242	install(*argv, to_name, fset, iflags);
243	exit(EX_OK);
244	/* NOTREACHED */
245}
246
247u_long
248numeric_id(name, type)
249	const char *name, *type;
250{
251	u_long val;
252	char *ep;
253
254	/*
255	 * XXX
256	 * We know that uid_t's and gid_t's are unsigned longs.
257	 */
258	errno = 0;
259	val = strtoul(name, &ep, 10);
260	if (errno)
261		err(EX_NOUSER, "%s", name);
262	if (*ep != '\0')
263		errx(EX_NOUSER, "unknown %s %s", type, name);
264	return (val);
265}
266
267/*
268 * install --
269 *	build a path name and install the file
270 */
271void
272install(from_name, to_name, fset, flags)
273	const char *from_name, *to_name;
274	u_long fset;
275	u_int flags;
276{
277	struct stat from_sb, temp_sb, to_sb;
278	struct utimbuf utb;
279	int devnull, files_match, from_fd, serrno, target;
280	int tempcopy, temp_fd, to_fd;
281	char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
282
283	files_match = 0;
284
285	/* If try to install NULL file to a directory, fails. */
286	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
287		if (stat(from_name, &from_sb))
288			err(EX_OSERR, "%s", from_name);
289		if (!S_ISREG(from_sb.st_mode)) {
290			errno = EFTYPE;
291			err(EX_OSERR, "%s", from_name);
292		}
293		/* Build the target path. */
294		if (flags & DIRECTORY) {
295			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
296			    to_name,
297			    (p = strrchr(from_name, '/')) ? ++p : from_name);
298			to_name = pathbuf;
299		}
300		devnull = 0;
301	} else {
302		devnull = 1;
303	}
304
305	target = stat(to_name, &to_sb) == 0;
306
307	/* Only install to regular files. */
308	if (target && !S_ISREG(to_sb.st_mode)) {
309		errno = EFTYPE;
310		warn("%s", to_name);
311		return;
312	}
313
314	/* Only copy safe if the target exists. */
315	tempcopy = safecopy && target;
316
317	if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
318		err(EX_OSERR, "%s", from_name);
319
320	/* If we don't strip, we can compare first. */
321	if (docompare && !dostrip && target) {
322		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
323			err(EX_OSERR, "%s", to_name);
324		if (devnull)
325			files_match = to_sb.st_size == 0;
326		else
327			files_match = !(compare(from_fd, from_name,
328			    (size_t)from_sb.st_size, to_fd,
329			    to_name, (size_t)to_sb.st_size));
330
331		/* Close "to" file unless we match. */
332		if (!files_match)
333			(void)close(to_fd);
334	}
335
336	if (!files_match) {
337		if (tempcopy) {
338			to_fd = create_tempfile(to_name, tempfile,
339			    sizeof(tempfile));
340			if (to_fd < 0)
341				err(EX_OSERR, "%s", tempfile);
342		} else {
343			if ((to_fd = create_newfile(to_name, target,
344			    &to_sb)) < 0)
345				err(EX_OSERR, "%s", to_name);
346			if (verbose)
347				(void)printf("install: %s -> %s\n",
348				    from_name, to_name);
349		}
350		if (!devnull)
351			copy(from_fd, from_name, to_fd,
352			     tempcopy ? tempfile : to_name, from_sb.st_size);
353	}
354
355	if (dostrip) {
356		strip(tempcopy ? tempfile : to_name);
357
358		/*
359		 * Re-open our fd on the target, in case we used a strip
360		 * that does not work in-place -- like GNU binutils strip.
361		 */
362		close(to_fd);
363		to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
364		if (to_fd < 0)
365			err(EX_OSERR, "stripping %s", to_name);
366	}
367
368	/*
369	 * Compare the stripped temp file with the target.
370	 */
371	if (docompare && dostrip && target) {
372		temp_fd = to_fd;
373
374		/* Re-open to_fd using the real target name. */
375		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
376			err(EX_OSERR, "%s", to_name);
377
378		if (fstat(temp_fd, &temp_sb)) {
379			serrno = errno;
380			(void)unlink(tempfile);
381			errno = serrno;
382			err(EX_OSERR, "%s", tempfile);
383		}
384
385		if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
386			    to_name, (size_t)to_sb.st_size) == 0) {
387			/*
388			 * If target has more than one link we need to
389			 * replace it in order to snap the extra links.
390			 * Need to preserve target file times, though.
391			 */
392			if (to_sb.st_nlink != 1) {
393				utb.actime = to_sb.st_atime;
394				utb.modtime = to_sb.st_mtime;
395				(void)utime(tempfile, &utb);
396			} else {
397				files_match = 1;
398				(void)unlink(tempfile);
399			}
400			(void) close(temp_fd);
401		}
402	}
403
404	/*
405	 * Move the new file into place if doing a safe copy
406	 * and the files are different (or just not compared).
407	 */
408	if (tempcopy && !files_match) {
409		/* Try to turn off the immutable bits. */
410		if (to_sb.st_flags & NOCHANGEBITS)
411			(void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
412		if (dobackup) {
413			if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
414			    suffix) != strlen(to_name) + strlen(suffix)) {
415				unlink(tempfile);
416				errx(EX_OSERR, "%s: backup filename too long",
417				    to_name);
418			}
419			if (verbose)
420				(void)printf("install: %s -> %s\n", to_name, backup);
421			if (rename(to_name, backup) < 0) {
422				serrno = errno;
423				unlink(tempfile);
424				errno = serrno;
425				err(EX_OSERR, "rename: %s to %s", to_name,
426				     backup);
427			}
428		}
429		if (verbose)
430			(void)printf("install: %s -> %s\n", from_name, to_name);
431		if (rename(tempfile, to_name) < 0) {
432			serrno = errno;
433			unlink(tempfile);
434			errno = serrno;
435			err(EX_OSERR, "rename: %s to %s",
436			    tempfile, to_name);
437		}
438
439		/* Re-open to_fd so we aren't hosed by the rename(2). */
440		(void) close(to_fd);
441		if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
442			err(EX_OSERR, "%s", to_name);
443	}
444
445	/*
446	 * Preserve the timestamp of the source file if necessary.
447	 */
448	if (dopreserve && !files_match && !devnull) {
449		utb.actime = from_sb.st_atime;
450		utb.modtime = from_sb.st_mtime;
451		(void)utime(to_name, &utb);
452	}
453
454	if (fstat(to_fd, &to_sb) == -1) {
455		serrno = errno;
456		(void)unlink(to_name);
457		errno = serrno;
458		err(EX_OSERR, "%s", to_name);
459	}
460
461	/*
462	 * Set owner, group, mode for target; do the chown first,
463	 * chown may lose the setuid bits.
464	 */
465	if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
466	    (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
467	    (mode != to_sb.st_mode)) {
468		/* Try to turn off the immutable bits. */
469		if (to_sb.st_flags & NOCHANGEBITS)
470			(void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
471	}
472
473	if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
474	    (uid != (uid_t)-1 && uid != to_sb.st_uid))
475		if (fchown(to_fd, uid, gid) == -1) {
476			serrno = errno;
477			(void)unlink(to_name);
478			errno = serrno;
479			err(EX_OSERR,"%s: chown/chgrp", to_name);
480		}
481
482	if (mode != to_sb.st_mode)
483		if (fchmod(to_fd, mode)) {
484			serrno = errno;
485			(void)unlink(to_name);
486			errno = serrno;
487			err(EX_OSERR, "%s: chmod", to_name);
488		}
489
490	/*
491	 * If provided a set of flags, set them, otherwise, preserve the
492	 * flags, except for the dump flag.
493	 * NFS does not support flags.  Ignore EOPNOTSUPP flags if we're just
494	 * trying to turn off UF_NODUMP.  If we're trying to set real flags,
495	 * then warn if the the fs doesn't support it, otherwise fail.
496	 */
497	if (!devnull && fchflags(to_fd,
498	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
499		if (flags & SETFLAGS) {
500			if (errno == EOPNOTSUPP)
501				warn("%s: chflags", to_name);
502			else {
503				serrno = errno;
504				(void)unlink(to_name);
505				errno = serrno;
506				err(EX_OSERR, "%s: chflags", to_name);
507			}
508		}
509	}
510
511	(void)close(to_fd);
512	if (!devnull)
513		(void)close(from_fd);
514}
515
516/*
517 * compare --
518 *	compare two files; non-zero means files differ
519 */
520int
521compare(int from_fd, const char *from_name __unused, size_t from_len,
522	int to_fd, const char *to_name __unused, size_t to_len)
523{
524	char *p, *q;
525	int rv;
526	int done_compare;
527
528	rv = 0;
529	if (from_len != to_len)
530		return 1;
531
532	if (from_len <= MAX_CMP_SIZE) {
533		done_compare = 0;
534		if (trymmap(from_fd) && trymmap(to_fd)) {
535			p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
536			if (p == (char *)MAP_FAILED)
537				goto out;
538			q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
539			if (q == (char *)MAP_FAILED) {
540				munmap(p, from_len);
541				goto out;
542			}
543
544			rv = memcmp(p, q, from_len);
545			munmap(p, from_len);
546			munmap(q, from_len);
547			done_compare = 1;
548		}
549	out:
550		if (!done_compare) {
551			char buf1[MAXBSIZE];
552			char buf2[MAXBSIZE];
553			int n1, n2;
554
555			rv = 0;
556			lseek(from_fd, 0, SEEK_SET);
557			lseek(to_fd, 0, SEEK_SET);
558			while (rv == 0) {
559				n1 = read(from_fd, buf1, sizeof(buf1));
560				if (n1 == 0)
561					break;		/* EOF */
562				else if (n1 > 0) {
563					n2 = read(to_fd, buf2, n1);
564					if (n2 == n1)
565						rv = memcmp(buf1, buf2, n1);
566					else
567						rv = 1;	/* out of sync */
568				} else
569					rv = 1;		/* read failure */
570			}
571			lseek(from_fd, 0, SEEK_SET);
572			lseek(to_fd, 0, SEEK_SET);
573		}
574	} else
575		rv = 1;	/* don't bother in this case */
576
577	return rv;
578}
579
580/*
581 * create_tempfile --
582 *	create a temporary file based on path and open it
583 */
584int
585create_tempfile(path, temp, tsize)
586	const char *path;
587	char *temp;
588	size_t tsize;
589{
590	char *p;
591
592	(void)strncpy(temp, path, tsize);
593	temp[tsize - 1] = '\0';
594	if ((p = strrchr(temp, '/')) != NULL)
595		p++;
596	else
597		p = temp;
598	(void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
599	temp[tsize - 1] = '\0';
600	return (mkstemp(temp));
601}
602
603/*
604 * create_newfile --
605 *	create a new file, overwriting an existing one if necessary
606 */
607int
608create_newfile(path, target, sbp)
609	const char *path;
610	int target;
611	struct stat *sbp;
612{
613	char backup[MAXPATHLEN];
614
615	if (target) {
616		/*
617		 * Unlink now... avoid ETXTBSY errors later.  Try to turn
618		 * off the append/immutable bits -- if we fail, go ahead,
619		 * it might work.
620		 */
621		if (sbp->st_flags & NOCHANGEBITS)
622			(void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
623
624		if (dobackup) {
625			if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
626			    path, suffix) != strlen(path) + strlen(suffix))
627				errx(EX_OSERR, "%s: backup filename too long",
628				    path);
629			(void)snprintf(backup, MAXPATHLEN, "%s%s",
630			    path, suffix);
631			if (verbose)
632				(void)printf("install: %s -> %s\n",
633				    path, backup);
634			if (rename(path, backup) < 0)
635				err(EX_OSERR, "rename: %s to %s", path, backup);
636		} else
637			(void)unlink(path);
638	}
639
640	return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
641}
642
643/*
644 * copy --
645 *	copy from one file to another
646 */
647void
648copy(from_fd, from_name, to_fd, to_name, size)
649	register int from_fd, to_fd;
650	const char *from_name, *to_name;
651	off_t size;
652{
653	register int nr, nw;
654	int serrno;
655	char *p, buf[MAXBSIZE];
656	int done_copy;
657
658	/* Rewind file descriptors. */
659	if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
660		err(EX_OSERR, "lseek: %s", from_name);
661	if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
662		err(EX_OSERR, "lseek: %s", to_name);
663
664	/*
665	 * Mmap and write if less than 8M (the limit is so we don't totally
666	 * trash memory on big files.  This is really a minor hack, but it
667	 * wins some CPU back.
668	 */
669	done_copy = 0;
670	if (size <= 8 * 1048576 && trymmap(from_fd) &&
671	    (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
672		    from_fd, (off_t)0)) != (char *)MAP_FAILED) {
673		if ((nw = write(to_fd, p, size)) != size) {
674			serrno = errno;
675			(void)unlink(to_name);
676			errno = nw > 0 ? EIO : serrno;
677			err(EX_OSERR, "%s", to_name);
678		}
679		done_copy = 1;
680	}
681	if (!done_copy) {
682		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
683			if ((nw = write(to_fd, buf, nr)) != nr) {
684				serrno = errno;
685				(void)unlink(to_name);
686				errno = nw > 0 ? EIO : serrno;
687				err(EX_OSERR, "%s", to_name);
688			}
689		if (nr != 0) {
690			serrno = errno;
691			(void)unlink(to_name);
692			errno = serrno;
693			err(EX_OSERR, "%s", from_name);
694		}
695	}
696}
697
698/*
699 * strip --
700 *	use strip(1) to strip the target file
701 */
702void
703strip(to_name)
704	const char *to_name;
705{
706	const char *stripbin;
707	int serrno, status;
708
709	switch (fork()) {
710	case -1:
711		serrno = errno;
712		(void)unlink(to_name);
713		errno = serrno;
714		err(EX_TEMPFAIL, "fork");
715	case 0:
716		stripbin = getenv("STRIPBIN");
717		if (stripbin == NULL)
718			stripbin = "strip";
719		execlp(stripbin, stripbin, to_name, (char *)NULL);
720		err(EX_OSERR, "exec(%s)", stripbin);
721	default:
722		if (wait(&status) == -1 || status) {
723			(void)unlink(to_name);
724			exit(EX_SOFTWARE);
725			/* NOTREACHED */
726		}
727	}
728}
729
730/*
731 * install_dir --
732 *	build directory heirarchy
733 */
734void
735install_dir(path)
736	char *path;
737{
738	register char *p;
739	struct stat sb;
740	int ch;
741
742	for (p = path;; ++p)
743		if (!*p || (p != path && *p  == '/')) {
744			ch = *p;
745			*p = '\0';
746			if (stat(path, &sb)) {
747				if (errno != ENOENT || mkdir(path, 0755) < 0) {
748					err(EX_OSERR, "mkdir %s", path);
749					/* NOTREACHED */
750				} else if (verbose)
751					(void)printf("install: mkdir %s\n",
752						     path);
753			} else if (!S_ISDIR(sb.st_mode))
754				errx(EX_OSERR, "%s exists but is not a directory", path);
755			if (!(*p = ch))
756				break;
757 		}
758
759	if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
760		warn("chown %u:%u %s", uid, gid, path);
761	if (chmod(path, mode))
762		warn("chmod %o %s", mode, path);
763}
764
765/*
766 * usage --
767 *	print a usage message and die
768 */
769void
770usage()
771{
772	(void)fprintf(stderr, "\
773usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
774               [-o owner] file1 file2\n\
775       install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
776               [-o owner] file1 ... fileN directory\n\
777       install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
778	exit(EX_USAGE);
779	/* NOTREACHED */
780}
781
782/*
783 * trymmap --
784 *	return true (1) if mmap should be tried, false (0) if not.
785 */
786int
787trymmap(fd)
788	int fd;
789{
790/*
791 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
792 * pre-Lite2-merge systems.
793 */
794#ifdef MFSNAMELEN
795	struct statfs stfs;
796
797	if (nommap || fstatfs(fd, &stfs) != 0)
798		return (0);
799	if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
800	    strcmp(stfs.f_fstypename, "cd9660") == 0)
801		return (1);
802#endif
803	return (0);
804}
805