xref: /freebsd/usr.bin/xinstall/xinstall.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
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
35 static 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 #ifndef lint
41 #if 0
42 static char sccsid[] = "From: @(#)xinstall.c	8.1 (Berkeley) 7/21/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 /*-
49  * Todo:
50  * o for -C, compare original files except in -s case.
51  * o for -C, don't change anything if nothing needs be changed.  In
52  *   particular, don't toggle the immutable flags just to allow null
53  *   attribute changes and don't clear the dump flag.  (I think inode
54  *   ctimes are not updated for null attribute changes, but this is a
55  *   bug.)
56  * o independent of -C, if a copy must be made, then copy to a tmpfile,
57  *   set all attributes except the immutable flags, then rename, then
58  *   set the immutable flags.  It's annoying that the immutable flags
59  *   defeat the atomicicity of rename - it seems that there must be
60  *   a window where the target is not immutable.
61  */
62 
63 #include <sys/param.h>
64 #include <sys/wait.h>
65 #include <sys/mman.h>
66 #include <sys/stat.h>
67 #include <sys/mount.h>
68 
69 #include <ctype.h>
70 #include <err.h>
71 #include <errno.h>
72 #include <fcntl.h>
73 #include <grp.h>
74 #include <paths.h>
75 #include <pwd.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <unistd.h>
80 #include <sysexits.h>
81 #include <utime.h>
82 
83 #include "pathnames.h"
84 
85 /* Bootstrap aid - this doesn't exist in most older releases */
86 #ifndef MAP_FAILED
87 #define MAP_FAILED ((void *)-1)	/* from <sys/mman.h> */
88 #endif
89 
90 int debug, docompare, docopy, dodir, dopreserve, dostrip, nommap, verbose;
91 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
92 char *group, *owner, pathbuf[MAXPATHLEN];
93 char pathbuf2[MAXPATHLEN];
94 
95 #define	DIRECTORY	0x01		/* Tell install it's a directory. */
96 #define	SETFLAGS	0x02		/* Tell install to set flags. */
97 #define	NOCHANGEBITS	(UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
98 
99 void	copy __P((int, char *, int, char *, off_t));
100 int	compare __P((int, const char *, int, const char *,
101 		     const struct stat *, const struct stat *));
102 void	install __P((char *, char *, u_long, u_int));
103 void	install_dir __P((char *));
104 u_long	string_to_flags __P((char **, u_long *, u_long *));
105 void	strip __P((char *));
106 void	usage __P((void));
107 int	trymmap __P((int));
108 
109 #define ALLOW_NUMERIC_IDS 1
110 #ifdef ALLOW_NUMERIC_IDS
111 
112 uid_t   uid = -1;
113 gid_t   gid = -1;
114 
115 uid_t	resolve_uid __P((char *));
116 gid_t	resolve_gid __P((char *));
117 u_long	numeric_id __P((char *, char *));
118 
119 #else
120 
121 struct passwd *pp;
122 struct group *gp;
123 
124 #endif /* ALLOW_NUMERIC_IDS */
125 
126 int
127 main(argc, argv)
128 	int argc;
129 	char *argv[];
130 {
131 	struct stat from_sb, to_sb;
132 	mode_t *set;
133 	u_long fset;
134 	u_int iflags;
135 	int ch, no_target;
136 	char *flags, *to_name;
137 
138 	iflags = 0;
139 	while ((ch = getopt(argc, argv, "CcdDf:g:m:Mo:psv")) != -1)
140 		switch((char)ch) {
141 		case 'C':
142 			docompare = docopy = 1;
143 			break;
144 		case 'c':
145 			docopy = 1;
146 			break;
147 		case 'D':
148 			debug++;
149 			break;
150 		case 'd':
151 			dodir = 1;
152 			break;
153 		case 'f':
154 			flags = optarg;
155 			if (string_to_flags(&flags, &fset, NULL))
156 				errx(EX_USAGE, "%s: invalid flag", flags);
157 			iflags |= SETFLAGS;
158 			break;
159 		case 'g':
160 			group = optarg;
161 			break;
162 		case 'm':
163 			if (!(set = setmode(optarg)))
164 				errx(EX_USAGE, "invalid file mode: %s",
165 				     optarg);
166 			mode = getmode(set, 0);
167 			free(set);
168 			break;
169 		case 'M':
170 			nommap = 1;
171 			break;
172 		case 'o':
173 			owner = optarg;
174 			break;
175 		case 'p':
176 			docompare = docopy = dopreserve = 1;
177 			break;
178 		case 's':
179 			dostrip = 1;
180 			break;
181 		case 'v':
182 			verbose = 1;
183 			break;
184 		case '?':
185 		default:
186 			usage();
187 		}
188 	argc -= optind;
189 	argv += optind;
190 
191 	/* some options make no sense when creating directories */
192 	if (dostrip && dodir)
193 		usage();
194 
195 	/* must have at least two arguments, except when creating directories */
196 	if (argc < 2 && !dodir)
197 		usage();
198 
199 #ifdef ALLOW_NUMERIC_IDS
200 
201 	if (owner)
202 		uid = resolve_uid(owner);
203 	if (group)
204 		gid = resolve_gid(group);
205 
206 #else
207 
208 	/* get group and owner id's */
209 	if (owner && !(pp = getpwnam(owner)))
210 		errx(EX_NOUSER, "unknown user %s", owner);
211 	if (group && !(gp = getgrnam(group)))
212 		errx(EX_NOUSER, "unknown group %s", group);
213 
214 #endif /* ALLOW_NUMERIC_IDS */
215 
216 	if (dodir) {
217 		for (; *argv != NULL; ++argv)
218 			install_dir(*argv);
219 		exit(EX_OK);
220 		/* NOTREACHED */
221 	}
222 
223 	no_target = stat(to_name = argv[argc - 1], &to_sb);
224 	if (!no_target && (to_sb.st_mode & S_IFMT) == S_IFDIR) {
225 		for (; *argv != to_name; ++argv)
226 			install(*argv, to_name, fset, iflags | DIRECTORY);
227 		exit(EX_OK);
228 		/* NOTREACHED */
229 	}
230 
231 	/* can't do file1 file2 directory/file */
232 	if (argc != 2)
233 		usage();
234 
235 	if (!no_target) {
236 		if (stat(*argv, &from_sb))
237 			err(EX_OSERR, "%s", *argv);
238 		if (!S_ISREG(to_sb.st_mode)) {
239 			errno = EFTYPE;
240 			err(EX_OSERR, "%s", to_name);
241 		}
242 		if (to_sb.st_dev == from_sb.st_dev &&
243 		    to_sb.st_ino == from_sb.st_ino)
244 			errx(EX_USAGE,
245 			    "%s and %s are the same file", *argv, to_name);
246 /*
247  * XXX - It's not at all clear why this code was here, since it completely
248  * duplicates code install().  The version in install() handles the -C flag
249  * correctly, so we'll just disable this for now.
250  */
251 #if 0
252 		/*
253 		 * Unlink now... avoid ETXTBSY errors later.  Try and turn
254 		 * off the append/immutable bits -- if we fail, go ahead,
255 		 * it might work.
256 		 */
257 		if (to_sb.st_flags & NOCHANGEBITS)
258 			(void)chflags(to_name,
259 			    to_sb.st_flags & ~(NOCHANGEBITS));
260 		(void)unlink(to_name);
261 #endif
262 	}
263 	install(*argv, to_name, fset, iflags);
264 	exit(EX_OK);
265 	/* NOTREACHED */
266 }
267 
268 #ifdef ALLOW_NUMERIC_IDS
269 
270 uid_t
271 resolve_uid(s)
272 	char *s;
273 {
274 	struct passwd *pw;
275 
276 	return ((pw = getpwnam(s)) == NULL) ?
277 		(uid_t) numeric_id(s, "user") : pw->pw_uid;
278 }
279 
280 gid_t
281 resolve_gid(s)
282 	char *s;
283 {
284 	struct group *gr;
285 
286 	return ((gr = getgrnam(s)) == NULL) ?
287 		(gid_t) numeric_id(s, "group") : gr->gr_gid;
288 }
289 
290 u_long
291 numeric_id(name, type)
292 	char *name, *type;
293 {
294 	u_long val;
295 	char *ep;
296 
297 	/*
298 	 * XXX
299 	 * We know that uid_t's and gid_t's are unsigned longs.
300 	 */
301 	errno = 0;
302 	val = strtoul(name, &ep, 10);
303 	if (errno)
304 		err(EX_NOUSER, "%s", name);
305 	if (*ep != '\0')
306 		errx(EX_NOUSER, "unknown %s %s", type, name);
307 	return (val);
308 }
309 
310 #endif /* ALLOW_NUMERIC_IDS */
311 
312 /*
313  * install --
314  *	build a path name and install the file
315  */
316 void
317 install(from_name, to_name, fset, flags)
318 	char *from_name, *to_name;
319 	u_long fset;
320 	u_int flags;
321 {
322 	struct stat from_sb, to_sb;
323 	int devnull, from_fd, to_fd, serrno;
324 	char *p, *old_to_name = 0;
325 
326 	if (debug >= 2 && !docompare)
327 		fprintf(stderr, "install: invoked without -C for %s to %s\n",
328 			from_name, to_name);
329 
330 	/* If try to install NULL file to a directory, fails. */
331 	if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
332 		if (stat(from_name, &from_sb))
333 			err(EX_OSERR, "%s", from_name);
334 		if (!S_ISREG(from_sb.st_mode)) {
335 			errno = EFTYPE;
336 			err(EX_OSERR, "%s", from_name);
337 		}
338 		/* Build the target path. */
339 		if (flags & DIRECTORY) {
340 			(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
341 			    to_name,
342 			    (p = strrchr(from_name, '/')) ? ++p : from_name);
343 			to_name = pathbuf;
344 		}
345 		devnull = 0;
346 	} else {
347 		from_sb.st_flags = 0;	/* XXX */
348 		devnull = 1;
349 	}
350 
351 	if (docompare) {
352 		old_to_name = to_name;
353 		/*
354 		 * Make a new temporary file in the same file system
355 		 * (actually, in in the same directory) as the target so
356 		 * that the temporary file can be renamed to the target.
357 		 */
358 		snprintf(pathbuf2, sizeof pathbuf2, "%s", to_name);
359 		p = strrchr(pathbuf2, '/');
360 		p = (p == NULL ? pathbuf2 : p + 1);
361 		snprintf(p, &pathbuf2[sizeof pathbuf2] - p, "INS@XXXX");
362 		to_fd = mkstemp(pathbuf2);
363 		if (to_fd < 0)
364 			/* XXX should fall back to not comparing. */
365 			err(EX_OSERR, "mkstemp: %s for %s", pathbuf2, to_name);
366 		to_name = pathbuf2;
367 	} else {
368 		/*
369 		 * Unlink now... avoid errors later.  Try to turn off the
370 		 * append/immutable bits -- if we fail, go ahead, it might
371 		 * work.
372 		 */
373 		if (stat(to_name, &to_sb) == 0 && to_sb.st_flags & NOCHANGEBITS)
374 			(void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
375 		unlink(to_name);
376 
377 		/* Create target. */
378 		to_fd = open(to_name,
379 			     O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
380 		if (to_fd < 0)
381 			err(EX_OSERR, "%s", to_name);
382 	}
383 
384 	if (!devnull) {
385 		if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
386 			serrno = errno;
387 			(void)unlink(to_name);
388 			errno = serrno;
389 			err(EX_OSERR, "%s", from_name);
390 		}
391 		copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
392 		(void)close(from_fd);
393 	}
394 
395 	if (dostrip) {
396 		(void)close(to_fd);
397 
398 		strip(to_name);
399 
400 		/* Reopen target. */
401 		to_fd = open(to_name, O_RDWR, 0);
402 		if (to_fd < 0)
403 			err(EX_OSERR, "%s", to_name);
404 	}
405 
406 	/*
407 	 * Unfortunately, because we strip the installed file and not the
408 	 * original one, it is impossible to do the comparison without
409 	 * first laboriously copying things over and then comparing.
410 	 * It may be possible to better optimize the !dostrip case, however.
411 	 * For further study.
412 	 */
413 	if (docompare) {
414 		struct stat old_sb, new_sb, timestamp_sb;
415 		int old_fd;
416 		struct utimbuf utb;
417 
418 		old_fd = open(old_to_name, O_RDONLY, 0);
419 		if (old_fd < 0 && errno == ENOENT)
420 			goto different;
421 		if (old_fd < 0)
422 			err(EX_OSERR, "%s", old_to_name);
423 		fstat(old_fd, &old_sb);
424 		if (old_sb.st_flags & NOCHANGEBITS)
425 			(void)fchflags(old_fd, old_sb.st_flags & ~NOCHANGEBITS);
426 		fstat(to_fd, &new_sb);
427 		if (compare(old_fd, old_to_name, to_fd, to_name, &old_sb,
428 			    &new_sb)) {
429 different:
430 			if (debug != 0)
431 				fprintf(stderr,
432 					"install: renaming for %s: %s to %s\n",
433 					from_name, to_name, old_to_name);
434 			if (verbose != 0)
435 				printf("install: %s -> %s\n",
436 					from_name, old_to_name);
437 			if (dopreserve && stat(from_name, &timestamp_sb) == 0) {
438 				utb.actime = from_sb.st_atime;
439 				utb.modtime = from_sb.st_mtime;
440 				(void)utime(to_name, &utb);
441 			}
442 moveit:
443 			if (rename(to_name, old_to_name) < 0) {
444 				serrno = errno;
445 				unlink(to_name);
446 				unlink(old_to_name);
447 				errno = serrno;
448 				err(EX_OSERR, "rename: %s to %s", to_name,
449 				    old_to_name);
450 			}
451 			close(old_fd);
452 		} else {
453 			if (old_sb.st_nlink != 1) {
454 				/*
455 				 * Replace the target, although it hasn't
456 				 * changed, to snap the extra links.  But
457 				 * preserve the target file times.
458 				 */
459 				if (fstat(old_fd, &timestamp_sb) == 0) {
460 					utb.actime = timestamp_sb.st_atime;
461 					utb.modtime = timestamp_sb.st_mtime;
462 					(void)utime(to_name, &utb);
463 				}
464 				goto moveit;
465 			}
466 			if (unlink(to_name) < 0)
467 				err(EX_OSERR, "unlink: %s", to_name);
468 			close(to_fd);
469 			to_fd = old_fd;
470 		}
471 		to_name = old_to_name;
472 	}
473 
474 	/*
475 	 * Set owner, group, mode for target; do the chown first,
476 	 * chown may lose the setuid bits.
477 	 */
478 	if ((group || owner) &&
479 #ifdef ALLOW_NUMERIC_IDS
480 	    fchown(to_fd, owner ? uid : -1, group ? gid : -1)) {
481 #else
482 	    fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1)) {
483 #endif
484 		serrno = errno;
485 		(void)unlink(to_name);
486 		errno = serrno;
487 		err(EX_OSERR,"%s: chown/chgrp", to_name);
488 	}
489 	if (fchmod(to_fd, mode)) {
490 		serrno = errno;
491 		(void)unlink(to_name);
492 		errno = serrno;
493 		err(EX_OSERR, "%s: chmod", to_name);
494 	}
495 
496 	/*
497 	 * If provided a set of flags, set them, otherwise, preserve the
498 	 * flags, except for the dump flag.
499 	 * NFS does not support flags.  Ignore EOPNOTSUPP flags if we're just
500 	 * trying to turn off UF_NODUMP.  If we're trying to set real flags,
501 	 * then warn if the the fs doesn't support it, otherwise fail.
502 	 */
503 	if (fchflags(to_fd,
504 	    flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
505 		if (flags & SETFLAGS) {
506 			if (errno == EOPNOTSUPP)
507 				warn("%s: chflags", to_name);
508 			else {
509 				serrno = errno;
510 				(void)unlink(to_name);
511 				errno = serrno;
512 				err(EX_OSERR, "%s: chflags", to_name);
513 			}
514 		}
515 	}
516 
517 	(void)close(to_fd);
518 	if (!docopy && !devnull && unlink(from_name))
519 		err(EX_OSERR, "%s", from_name);
520 }
521 
522 /*
523  * compare --
524  *	compare two files; non-zero means files differ
525  */
526 int
527 compare(int from_fd, const char *from_name, int to_fd, const char *to_name,
528 	const struct stat *from_sb, const struct stat *to_sb)
529 {
530 	char *p, *q;
531 	int rv;
532 	size_t tsize;
533 	int done_compare;
534 
535 	rv = 0;
536 	if (from_sb->st_size != to_sb->st_size)
537 		return 1;
538 
539 	tsize = (size_t)from_sb->st_size;
540 
541 	if (tsize <= 8 * 1024 * 1024) {
542 		done_compare = 0;
543 		if (trymmap(from_fd) && trymmap(to_fd)) {
544 			p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
545 			if (p == (char *)MAP_FAILED)
546 				goto out;
547 			q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
548 			if (q == (char *)MAP_FAILED) {
549 				munmap(p, tsize);
550 				goto out;
551 			}
552 
553 			rv = memcmp(p, q, tsize);
554 			munmap(p, tsize);
555 			munmap(q, tsize);
556 			done_compare = 1;
557 		}
558 	out:
559 		if (!done_compare) {
560 			char buf1[MAXBSIZE];
561 			char buf2[MAXBSIZE];
562 			int n1, n2;
563 
564 			rv = 0;
565 			lseek(from_fd, 0, SEEK_SET);
566 			lseek(to_fd, 0, SEEK_SET);
567 			while (rv == 0) {
568 				n1 = read(from_fd, buf1, sizeof(buf1));
569 				if (n1 == 0)
570 					break;		/* EOF */
571 				else if (n1 > 0) {
572 					n2 = read(to_fd, buf2, n1);
573 					if (n2 == n1)
574 						rv = memcmp(buf1, buf2, n1);
575 					else
576 						rv = 1;	/* out of sync */
577 				} else
578 					rv = 1;		/* read failure */
579 			}
580 			lseek(from_fd, 0, SEEK_SET);
581 			lseek(to_fd, 0, SEEK_SET);
582 		}
583 	} else
584 		rv = 1;	/* don't bother in this case */
585 
586 	return rv;
587 }
588 
589 /*
590  * copy --
591  *	copy from one file to another
592  */
593 void
594 copy(from_fd, from_name, to_fd, to_name, size)
595 	register int from_fd, to_fd;
596 	char *from_name, *to_name;
597 	off_t size;
598 {
599 	register int nr, nw;
600 	int serrno;
601 	char *p, buf[MAXBSIZE];
602 	int done_copy;
603 
604 	/*
605 	 * Mmap and write if less than 8M (the limit is so we don't totally
606 	 * trash memory on big files.  This is really a minor hack, but it
607 	 * wins some CPU back.
608 	 */
609 	done_copy = 0;
610 	if (size <= 8 * 1048576 && trymmap(from_fd)) {
611 		if ((p = mmap(NULL, (size_t)size, PROT_READ,
612 				MAP_SHARED, from_fd, (off_t)0)) == (char *)MAP_FAILED)
613 			goto out;
614 		if ((nw = write(to_fd, p, size)) != size) {
615 			serrno = errno;
616 			(void)unlink(to_name);
617 			errno = nw > 0 ? EIO : serrno;
618 			err(EX_OSERR, "%s", to_name);
619 		}
620 		done_copy = 1;
621 	out:
622 	}
623 	if (!done_copy) {
624 		while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
625 			if ((nw = write(to_fd, buf, nr)) != nr) {
626 				serrno = errno;
627 				(void)unlink(to_name);
628 				errno = nw > 0 ? EIO : serrno;
629 				err(EX_OSERR, "%s", to_name);
630 			}
631 		if (nr != 0) {
632 			serrno = errno;
633 			(void)unlink(to_name);
634 			errno = serrno;
635 			err(EX_OSERR, "%s", from_name);
636 		}
637 	}
638 }
639 
640 /*
641  * strip --
642  *	use strip(1) to strip the target file
643  */
644 void
645 strip(to_name)
646 	char *to_name;
647 {
648 	int serrno, status;
649 
650 	switch (fork()) {
651 	case -1:
652 		serrno = errno;
653 		(void)unlink(to_name);
654 		errno = serrno;
655 		err(EX_TEMPFAIL, "fork");
656 	case 0:
657 		execlp("strip", "strip", to_name, NULL);
658 		err(EX_OSERR, "exec(strip)");
659 	default:
660 		if (wait(&status) == -1 || status) {
661 			(void)unlink(to_name);
662 			exit(EX_SOFTWARE);
663 			/* NOTREACHED */
664 		}
665 	}
666 }
667 
668 /*
669  * install_dir --
670  *	build directory heirarchy
671  */
672 void
673 install_dir(path)
674         char *path;
675 {
676 	register char *p;
677 	struct stat sb;
678 	int ch;
679 
680 	for (p = path;; ++p)
681 		if (!*p || (p != path && *p  == '/')) {
682 			ch = *p;
683 			*p = '\0';
684 			if (stat(path, &sb)) {
685 				if (errno != ENOENT || mkdir(path, 0755) < 0) {
686 					err(EX_OSERR, "mkdir %s", path);
687 					/* NOTREACHED */
688 				}
689 			} else if (!S_ISDIR(sb.st_mode))
690 				errx(EX_OSERR, "%s exists but is not a directory", path);
691 			if (!(*p = ch))
692 				break;
693  		}
694 
695 	if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
696 		warn("chown %u:%u %s", uid, gid, path);
697 	if (chmod(path, mode))
698 		warn("chmod %o %s", mode, path);
699 }
700 
701 /*
702  * usage --
703  *	print a usage message and die
704  */
705 void
706 usage()
707 {
708 	(void)fprintf(stderr,"\
709 usage: install [-CcDpsv] [-f flags] [-g group] [-m mode] [-o owner] file1 file2\n\
710        install [-CcDpsv] [-f flags] [-g group] [-m mode] [-o owner] file1 ...\n\
711              fileN directory\n\
712        install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
713 	exit(EX_USAGE);
714 	/* NOTREACHED */
715 }
716 
717 /*
718  * trymmap --
719  *	return true (1) if mmap should be tried, false (0) if not.
720  */
721 int
722 trymmap(fd)
723 	int fd;
724 {
725 /*
726  * The ifdef is for bootstrapping - f_fstypename doesn't exist in
727  * pre-Lite2-merge systems.
728  */
729 #ifdef MFSNAMELEN
730 	struct statfs stfs;
731 
732 	if (nommap || fstatfs(fd, &stfs) != 0)
733 		return (0);
734 	if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
735 	    strcmp(stfs.f_fstypename, "cd9660") == 0)
736 		return (1);
737 #endif
738 	return (0);
739 }
740