install(1): Add a -N option, improving NetBSD and FreeBSD compatibility
[dragonfly.git] / usr.bin / xinstall / xinstall.c
blobecc5172a90a9ecc10690932ee3fc62c64798f22b
1 /*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)xinstall.c 8.1 (Berkeley) 7/21/93
31 * $FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.38.2.8 2002/08/07 16:29:48 ru Exp $
34 #include <sys/param.h>
35 #include <sys/mman.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <grp.h>
45 #include <paths.h>
46 #include <pwd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sysexits.h>
51 #include <unistd.h>
52 #include <utime.h>
54 /* Bootstrap aid - this doesn't exist in most older releases */
55 #ifndef MAP_FAILED
56 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
57 #endif
58 #ifndef UF_NOHISTORY
59 #define UF_NOHISTORY 0
60 #endif
62 #define MAX_CMP_SIZE (16 * 1024 * 1024)
64 #define DIRECTORY 0x01 /* Tell install it's a directory. */
65 #define SETFLAGS 0x02 /* Tell install to set flags. */
66 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
67 #define BACKUP_SUFFIX ".old"
69 static struct passwd *pp;
70 static struct group *gp;
71 static gid_t gid;
72 static uid_t uid;
73 static int dobackup, docompare, dodir, dopreserve, dostrip, dounpriv, nommap,
74 safecopy, verbose;
75 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
76 static const char *suffix = BACKUP_SUFFIX;
77 static char *destdir, *fflags;
79 static int file_getgroup(const char *etcdir, const char *group, gid_t *gidret);
80 static int file_getowner(const char *etcdir, const char *owner, uid_t *uidret);
82 static int compare(int, const char *, size_t, int, const char *, size_t);
83 static void copy(int, const char *, int, const char *, off_t);
84 static int create_newfile(const char *, int, struct stat *);
85 static int create_tempfile(const char *, char *, size_t);
86 static void install(const char *, const char *, u_long, u_long, u_int);
87 static void install_dir(char *);
88 u_long numeric_id(const char *, const char *);
89 static void strip(const char *);
90 static int trymmap(int);
91 static void usage(void);
93 int
94 main(int argc, char *argv[])
96 struct stat from_sb, to_sb;
97 mode_t *set;
98 u_long fset;
99 u_long fclr;
100 int ch, no_target;
101 int trysys;
102 u_int iflags;
103 const char *group, *owner, *to_name;
104 const char *etcdir;
106 fclr = 0;
107 fset = 0;
108 iflags = 0;
109 trysys = 0;
110 group = NULL;
111 owner = NULL;
112 etcdir = NULL;
114 while ((ch = getopt(argc, argv, "L:B:bCcD:df:g:lMm:N:o:pSsUv")) != -1)
115 switch((char)ch) {
116 case 'B':
117 suffix = optarg;
118 /* FALLTHROUGH */
119 case 'b':
120 dobackup = 1;
121 break;
122 case 'C':
123 docompare = 1;
124 break;
125 case 'c':
126 /* For backwards compatibility. */
127 break;
128 case 'D':
129 destdir = optarg;
130 break;
131 case 'd':
132 dodir = 1;
133 break;
134 case 'f':
135 fflags = optarg;
136 break;
137 case 'g':
138 group = optarg;
139 break;
140 case 'l':
141 trysys = 1;
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 'L':
154 /* -L option kept for compatibility with pre-5.4 */
155 warnx("Option -L is deprecated, use -N instead");
156 /* FALLTHROUGH */
157 case 'N':
158 etcdir = optarg;
159 break;
160 case 'o':
161 owner = optarg;
162 break;
163 case 'p':
164 docompare = dopreserve = 1;
165 break;
166 case 'S':
167 safecopy = 1;
168 break;
169 case 's':
170 dostrip = 1;
171 break;
172 case 'U':
173 dounpriv = 1;
174 break;
175 case 'v':
176 verbose = 1;
177 break;
178 case '?':
179 default:
180 usage();
182 argc -= optind;
183 argv += optind;
185 /* some options make no sense when creating directories */
186 if (dostrip && dodir) {
187 warnx("-d and -s may not be specified together");
188 usage();
191 if (getenv("DONTSTRIP") != NULL) {
192 warnx("DONTSTRIP set - will not strip installed binaries");
193 dostrip = 0;
196 /* must have at least two arguments, except when creating directories */
197 if (argc < 2 && !dodir)
198 usage();
200 /* need to make a temp copy so we can compare stripped version */
201 if (docompare && dostrip)
202 safecopy = 1;
204 /* no etcdir specified, always try the system */
205 if (etcdir == NULL)
206 trysys = 1;
207 uid = (uid_t)-1;
208 gid = (gid_t)-1;
210 /* get group and owner id's */
211 if (group != NULL && !dounpriv) {
212 if (etcdir && file_getgroup(etcdir, group, &gid)) {
214 } else if (trysys && (gp = getgrnam(group)) != NULL) {
215 gid = gp->gr_gid;
216 } else {
217 gid = (gid_t)numeric_id(group, "group");
221 if (owner != NULL && !dounpriv) {
222 if (etcdir && file_getowner(etcdir, owner, &uid)) {
224 } else if (trysys && (pp = getpwnam(owner)) != NULL) {
225 uid = pp->pw_uid;
226 } else {
227 uid = (uid_t)numeric_id(owner, "user");
231 if (fflags != NULL && !dounpriv) {
232 if (strtofflags(&fflags, &fset, &fclr))
233 errx(EX_USAGE, "%s: invalid flag", fflags);
234 iflags |= SETFLAGS;
237 if (dodir) {
238 for (; *argv != NULL; ++argv)
239 install_dir(*argv);
240 exit(EX_OK);
241 /* NOTREACHED */
244 to_name = argv[argc - 1];
245 no_target = stat(to_name, &to_sb);
246 if (!no_target && S_ISDIR(to_sb.st_mode)) {
247 for (; *argv != to_name; ++argv)
248 install(*argv, to_name, fset, fclr, iflags | DIRECTORY);
249 exit(EX_OK);
250 /* NOTREACHED */
253 /* can't do file1 file2 directory/file */
254 if (argc != 2) {
255 if (no_target)
256 warnx("target directory `%s' does not exist",
257 argv[argc - 1]);
258 else
259 warnx("target `%s' is not a directory",
260 argv[argc - 1]);
261 usage();
264 if (!no_target) {
265 if (stat(*argv, &from_sb))
266 err(EX_OSERR, "%s", *argv);
267 if (!S_ISREG(to_sb.st_mode)) {
268 errno = EFTYPE;
269 err(EX_OSERR, "%s", to_name);
271 if (to_sb.st_dev == from_sb.st_dev &&
272 to_sb.st_ino == from_sb.st_ino)
273 errx(EX_USAGE,
274 "%s and %s are the same file", *argv, to_name);
276 install(*argv, to_name, fset, fclr, iflags);
277 exit(EX_OK);
278 /* NOTREACHED */
281 u_long
282 numeric_id(const char *name, const char *type)
284 u_long val;
285 char *ep;
288 * XXX
289 * We know that uid_t's and gid_t's are unsigned longs.
291 errno = 0;
292 val = strtoul(name, &ep, 10);
293 if (errno)
294 err(EX_NOUSER, "%s", name);
295 if (*ep != '\0')
296 errx(EX_NOUSER, "unknown %s %s", type, name);
297 return (val);
300 static
302 file_getgroup(const char *etcdir, const char *group, gid_t *gidret)
304 FILE *fp;
305 size_t len;
306 size_t grlen;
307 char *path;
308 char *ptr;
309 char *scan;
311 grlen = strlen(group);
313 if (asprintf(&path, "%s/group", etcdir) < 0)
314 errx(EX_OSERR, "asprintf()");
315 if ((fp = fopen(path, "r")) != NULL) {
316 while ((ptr = fgetln(fp, &len)) != NULL && len) {
317 ptr[len - 1] = 0;
318 if ((scan = strchr(ptr, ':')) == NULL)
319 continue;
320 if ((size_t)(scan - ptr) != grlen)
321 continue;
322 if (strncmp(ptr, group, grlen) != 0)
323 continue;
324 if ((scan = strchr(scan + 1, ':')) == NULL)
325 continue;
326 *gidret = strtoul(scan + 1, NULL, 10);
327 break;
329 fclose(fp);
331 free(path);
332 return((*gidret == (gid_t)-1) ? 0 : 1);
335 static
337 file_getowner(const char *etcdir, const char *owner, uid_t *uidret)
339 FILE *fp;
340 size_t len;
341 size_t owner_len;
342 char *path;
343 char *ptr;
344 char *scan;
346 owner_len = strlen(owner);
348 if (asprintf(&path, "%s/master.passwd", etcdir) < 0)
349 errx(EX_OSERR, "asprintf()");
350 if ((fp = fopen(path, "r")) != NULL) {
351 while ((ptr = fgetln(fp, &len)) != NULL && len) {
352 ptr[len - 1] = 0;
353 if ((scan = strchr(ptr, ':')) == NULL)
354 continue;
355 if ((size_t)(scan - ptr) != owner_len)
356 continue;
357 if (strncmp(ptr, owner, owner_len) != 0)
358 continue;
359 if ((scan = strchr(scan + 1, ':')) == NULL)
360 continue;
361 *uidret = strtoul(scan + 1, NULL, 10);
362 break;
364 fclose(fp);
366 free(path);
367 return((*uidret == (uid_t)-1) ? 0 : 1);
371 * install --
372 * build a path name and install the file
374 static void
375 install(const char *from_name, const char *to_name, u_long fset, u_long fclr,
376 u_int flags)
378 struct stat from_sb, temp_sb, to_sb;
379 struct utimbuf utb;
380 int devnull, files_match, from_fd, serrno, target;
381 int tempcopy, temp_fd, to_fd;
382 u_long nfset;
383 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
385 files_match = 0;
386 from_fd = -1;
388 /* If try to install NULL file to a directory, fails. */
389 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
390 if (stat(from_name, &from_sb))
391 err(EX_OSERR, "%s", from_name);
392 if (!S_ISREG(from_sb.st_mode)) {
393 errno = EFTYPE;
394 err(EX_OSERR, "%s", from_name);
396 /* Build the target path. */
397 if (flags & DIRECTORY) {
398 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
399 to_name,
400 (p = strrchr(from_name, '/')) ? ++p : from_name);
401 to_name = pathbuf;
403 devnull = 0;
404 } else {
405 devnull = 1;
408 target = stat(to_name, &to_sb) == 0;
410 /* Only install to regular files. */
411 if (target && !S_ISREG(to_sb.st_mode)) {
412 errno = EFTYPE;
413 warn("%s", to_name);
414 return;
417 /* Only copy safe if the target exists. */
418 tempcopy = safecopy && target;
420 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
421 err(EX_OSERR, "%s", from_name);
423 /* If we don't strip, we can compare first. */
424 if (docompare && !dostrip && target) {
425 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
426 err(EX_OSERR, "%s", to_name);
427 if (devnull)
428 files_match = to_sb.st_size == 0;
429 else
430 files_match = !(compare(from_fd, from_name,
431 (size_t)from_sb.st_size, to_fd,
432 to_name, (size_t)to_sb.st_size));
434 /* Close "to" file unless we match. */
435 if (!files_match)
436 (void)close(to_fd);
439 if (!files_match) {
440 if (tempcopy) {
441 to_fd = create_tempfile(to_name, tempfile,
442 sizeof(tempfile));
443 if (to_fd < 0)
444 err(EX_OSERR, "%s", tempfile);
445 } else {
446 if ((to_fd = create_newfile(to_name, target,
447 &to_sb)) < 0)
448 err(EX_OSERR, "%s", to_name);
449 if (verbose)
450 (void)printf("install: %s -> %s\n",
451 from_name, to_name);
453 if (!devnull)
454 copy(from_fd, from_name, to_fd,
455 tempcopy ? tempfile : to_name, from_sb.st_size);
458 if (dostrip) {
459 strip(tempcopy ? tempfile : to_name);
462 * Re-open our fd on the target, in case we used a strip
463 * that does not work in-place -- like GNU binutils strip.
465 close(to_fd);
466 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
467 if (to_fd < 0)
468 err(EX_OSERR, "stripping %s", to_name);
472 * Compare the stripped temp file with the target.
474 if (docompare && dostrip && target) {
475 temp_fd = to_fd;
477 /* Re-open to_fd using the real target name. */
478 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
479 err(EX_OSERR, "%s", to_name);
481 if (fstat(temp_fd, &temp_sb)) {
482 serrno = errno;
483 (void)unlink(tempfile);
484 errno = serrno;
485 err(EX_OSERR, "%s", tempfile);
488 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
489 to_name, (size_t)to_sb.st_size) == 0) {
491 * If target has more than one link we need to
492 * replace it in order to snap the extra links.
493 * Need to preserve target file times, though.
495 if (to_sb.st_nlink != 1) {
496 utb.actime = to_sb.st_atime;
497 utb.modtime = to_sb.st_mtime;
498 utime(tempfile, &utb);
499 } else {
500 files_match = 1;
501 (void)unlink(tempfile);
503 (void) close(temp_fd);
508 * Move the new file into place if doing a safe copy
509 * and the files are different (or just not compared).
511 if (tempcopy && !files_match) {
512 /* Try to turn off the immutable bits. */
513 if (to_sb.st_flags & NOCHANGEBITS)
514 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
515 if (dobackup) {
516 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
517 suffix) != strlen(to_name) + strlen(suffix)) {
518 unlink(tempfile);
519 errx(EX_OSERR, "%s: backup filename too long",
520 to_name);
522 if (verbose)
523 (void)printf("install: %s -> %s\n", to_name, backup);
524 if (rename(to_name, backup) < 0) {
525 serrno = errno;
526 unlink(tempfile);
527 errno = serrno;
528 err(EX_OSERR, "rename: %s to %s", to_name,
529 backup);
532 if (verbose)
533 (void)printf("install: %s -> %s\n", from_name, to_name);
534 if (rename(tempfile, to_name) < 0) {
535 serrno = errno;
536 unlink(tempfile);
537 errno = serrno;
538 err(EX_OSERR, "rename: %s to %s",
539 tempfile, to_name);
542 /* Re-open to_fd so we aren't hosed by the rename(2). */
543 (void) close(to_fd);
544 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
545 err(EX_OSERR, "%s", to_name);
549 * Preserve the timestamp of the source file if necessary.
551 if (dopreserve && !files_match && !devnull) {
552 utb.actime = from_sb.st_atime;
553 utb.modtime = from_sb.st_mtime;
554 utime(to_name, &utb);
557 if (fstat(to_fd, &to_sb) == -1) {
558 serrno = errno;
559 (void)unlink(to_name);
560 errno = serrno;
561 err(EX_OSERR, "%s", to_name);
565 * Set owner, group, mode for target; do the chown first,
566 * chown may lose the setuid bits.
568 if (!dounpriv && ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
569 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
570 (mode != to_sb.st_mode))) {
571 /* Try to turn off the immutable bits. */
572 if (to_sb.st_flags & NOCHANGEBITS)
573 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
576 if (!dounpriv && (
577 (gid != (gid_t)-1 && gid != to_sb.st_gid) ||
578 (uid != (uid_t)-1 && uid != to_sb.st_uid)))
579 if (fchown(to_fd, uid, gid) == -1) {
580 serrno = errno;
581 (void)unlink(to_name);
582 errno = serrno;
583 err(EX_OSERR,"%s: chown/chgrp", to_name);
586 if (mode != to_sb.st_mode) {
587 if (fchmod(to_fd,
588 dounpriv ? mode & (S_IRWXU|S_IRWXG|S_IRWXO) : mode)) {
589 serrno = errno;
590 (void)unlink(to_name);
591 errno = serrno;
592 err(EX_OSERR, "%s: chmod", to_name);
597 * If provided a set of flags, set them, otherwise, preserve the
598 * flags, except for the dump and history flags. The dump flag
599 * is left clear on the target while the history flag from when
600 * the target was created (which is inherited from the target's
601 * parent directory) is retained.
603 if (flags & SETFLAGS) {
604 nfset = (to_sb.st_flags | fset) & ~fclr;
605 } else {
606 nfset = (from_sb.st_flags & ~(UF_NODUMP | UF_NOHISTORY)) |
607 (to_sb.st_flags & UF_NOHISTORY);
611 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
612 * trying to turn off UF_NODUMP. If we're trying to set real flags,
613 * then warn if the fs doesn't support it, otherwise fail.
615 if (!dounpriv && !devnull && fchflags(to_fd, nfset)) {
616 if (flags & SETFLAGS) {
617 if (errno == EOPNOTSUPP)
618 warn("%s: chflags", to_name);
619 else {
620 serrno = errno;
621 (void)unlink(to_name);
622 errno = serrno;
623 err(EX_OSERR, "%s: chflags", to_name);
628 (void)close(to_fd);
629 if (!devnull)
630 (void)close(from_fd);
634 * compare --
635 * compare two files; non-zero means files differ
637 static int
638 compare(int from_fd, const char *from_name __unused, size_t from_len,
639 int to_fd, const char *to_name __unused, size_t to_len)
641 char *p, *q;
642 int rv;
643 int done_compare;
645 rv = 0;
646 if (from_len != to_len)
647 return 1;
649 if (from_len <= MAX_CMP_SIZE) {
650 done_compare = 0;
651 if (trymmap(from_fd) && trymmap(to_fd)) {
652 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
653 if (p == (char *)MAP_FAILED)
654 goto out;
655 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
656 if (q == (char *)MAP_FAILED) {
657 munmap(p, from_len);
658 goto out;
661 rv = memcmp(p, q, from_len);
662 munmap(p, from_len);
663 munmap(q, from_len);
664 done_compare = 1;
666 out:
667 if (!done_compare) {
668 char buf1[MAXBSIZE];
669 char buf2[MAXBSIZE];
670 int n1, n2;
672 rv = 0;
673 lseek(from_fd, 0, SEEK_SET);
674 lseek(to_fd, 0, SEEK_SET);
675 while (rv == 0) {
676 n1 = read(from_fd, buf1, sizeof(buf1));
677 if (n1 == 0)
678 break; /* EOF */
679 else if (n1 > 0) {
680 n2 = read(to_fd, buf2, n1);
681 if (n2 == n1)
682 rv = memcmp(buf1, buf2, n1);
683 else
684 rv = 1; /* out of sync */
685 } else
686 rv = 1; /* read failure */
688 lseek(from_fd, 0, SEEK_SET);
689 lseek(to_fd, 0, SEEK_SET);
691 } else
692 rv = 1; /* don't bother in this case */
694 return rv;
698 * create_tempfile --
699 * create a temporary file based on path and open it
701 static int
702 create_tempfile(const char *path, char *temp, size_t tsize)
704 char *p;
706 (void)strncpy(temp, path, tsize);
707 temp[tsize - 1] = '\0';
708 if ((p = strrchr(temp, '/')) != NULL)
709 p++;
710 else
711 p = temp;
712 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
713 temp[tsize - 1] = '\0';
714 return (mkstemp(temp));
718 * create_newfile --
719 * create a new file, overwriting an existing one if necessary
721 static int
722 create_newfile(const char *path, int target, struct stat *sbp)
724 char backup[MAXPATHLEN];
726 if (target) {
728 * Unlink now... avoid ETXTBSY errors later. Try to turn
729 * off the append/immutable bits -- if we fail, go ahead,
730 * it might work.
732 if (sbp->st_flags & NOCHANGEBITS)
733 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
735 if (dobackup) {
736 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
737 path, suffix) != strlen(path) + strlen(suffix))
738 errx(EX_OSERR, "%s: backup filename too long",
739 path);
740 (void)snprintf(backup, MAXPATHLEN, "%s%s",
741 path, suffix);
742 if (verbose)
743 (void)printf("install: %s -> %s\n",
744 path, backup);
745 if (rename(path, backup) < 0)
746 err(EX_OSERR, "rename: %s to %s", path, backup);
747 } else
748 unlink(path);
751 return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
755 * copy --
756 * copy from one file to another
758 static void
759 copy(int from_fd, const char *from_name, int to_fd,
760 const char *to_name, off_t size)
762 int nr, nw;
763 int serrno;
764 char *p;
765 char buf[MAXBSIZE];
766 int done_copy;
768 /* Rewind file descriptors. */
769 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
770 err(EX_OSERR, "lseek: %s", from_name);
771 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
772 err(EX_OSERR, "lseek: %s", to_name);
775 * Mmap and write if less than 8M (the limit is so we don't totally
776 * trash memory on big files. This is really a minor hack, but it
777 * wins some CPU back.
779 done_copy = 0;
780 if (size <= 8 * 1048576 && trymmap(from_fd) &&
781 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
782 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
783 if ((nw = write(to_fd, p, size)) != size) {
784 serrno = errno;
785 (void)unlink(to_name);
786 errno = nw > 0 ? EIO : serrno;
787 err(EX_OSERR, "%s", to_name);
789 done_copy = 1;
791 if (!done_copy) {
792 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
793 if ((nw = write(to_fd, buf, nr)) != nr) {
794 serrno = errno;
795 (void)unlink(to_name);
796 errno = nw > 0 ? EIO : serrno;
797 err(EX_OSERR, "%s", to_name);
800 if (nr != 0) {
801 serrno = errno;
802 (void)unlink(to_name);
803 errno = serrno;
804 err(EX_OSERR, "%s", from_name);
810 * strip --
811 * use strip(1) to strip the target file
813 static void
814 strip(const char *to_name)
816 const char *stripbin;
817 int serrno, status;
819 switch (fork()) {
820 case -1:
821 serrno = errno;
822 (void)unlink(to_name);
823 errno = serrno;
824 err(EX_TEMPFAIL, "fork");
825 case 0:
826 stripbin = getenv("STRIPBIN");
827 if (stripbin == NULL)
828 stripbin = "strip";
829 execlp(stripbin, stripbin, to_name, NULL);
830 err(EX_OSERR, "exec(%s)", stripbin);
831 default:
832 if (wait(&status) == -1 || status) {
833 serrno = errno;
834 (void)unlink(to_name);
835 errc(EX_SOFTWARE, serrno, "wait");
836 /* NOTREACHED */
842 * When doing a concurrent make -j N multiple install's can race the mkdir.
844 static
846 mkdir_race(const char *path, int nmode)
848 int res;
849 struct stat sb;
851 res = mkdir(path, nmode);
852 if (res < 0 && errno == EEXIST) {
853 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode))
854 return(0);
855 res = mkdir(path, nmode);
857 return (res);
861 * install_dir --
862 * build directory hierarchy
864 static void
865 install_dir(char *path)
867 char *p;
868 struct stat sb;
869 int ch;
871 for (p = path;; ++p)
872 if (!*p || (p != path && *p == '/')) {
873 ch = *p;
874 *p = '\0';
875 if (stat(path, &sb)) {
876 if (errno != ENOENT ||
877 mkdir_race(path, 0755) < 0) {
878 err(EX_OSERR, "mkdir %s", path);
879 /* NOTREACHED */
880 } else if (verbose)
881 (void)printf("install: mkdir %s\n",
882 path);
883 } else if (!S_ISDIR(sb.st_mode))
884 errx(EX_OSERR, "%s exists but is not a directory", path);
885 if (!(*p = ch))
886 break;
889 if (!dounpriv) {
890 if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
891 chown(path, uid, gid))
892 warn("chown %u:%u %s", uid, gid, path);
893 /* XXXBED: should we do the chmod in the dounpriv case? */
894 if (chmod(path, mode))
895 warn("chmod %o %s", mode, path);
900 * usage --
901 * print a usage message and die
903 static void
904 usage(void)
906 fprintf(stderr,
907 "usage: install [-bCcpSsUv] [-B suffix] [-D dest] [-f flags] [-g group]\n"
908 " [-N dbdir] [-m mode] [-o owner] file1 file2\n"
909 " install [-bCcpSsUv] [-B suffix] [-D dest] [-f flags] [-g group]\n"
910 " [-N dbdir] [-m mode] [-o owner] file1 ... fileN directory\n"
911 " install -d [-lUv] [-D dest] [-g group] [-m mode] [-N dbdir] [-o owner]\n"
912 " directory ...\n");
913 exit(EX_USAGE);
914 /* NOTREACHED */
918 * trymmap --
919 * return true (1) if mmap should be tried, false (0) if not.
921 static int
922 trymmap(int fd)
925 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
926 * pre-Lite2-merge systems.
928 #ifdef MFSNAMELEN
929 struct statfs stfs;
931 if (nommap || fstatfs(fd, &stfs) != 0)
932 return (0);
933 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
934 strcmp(stfs.f_fstypename, "cd9660") == 0)
935 return (1);
936 #endif
937 return (0);