Fix flags handling for the install program. Properly set and clear flags
[dragonfly.git] / usr.bin / xinstall / xinstall.c
blob97828a98cf11a8c1e46c66b7d815d8daebfa0976
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. 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.
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.
33 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)xinstall.c 8.1 (Berkeley) 7/21/93
35 * $FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.38.2.8 2002/08/07 16:29:48 ru Exp $
36 * $DragonFly: src/usr.bin/xinstall/xinstall.c,v 1.6 2008/11/11 01:51:24 dillon Exp $
39 #include <sys/param.h>
40 #include <sys/mman.h>
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43 #include <sys/wait.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <grp.h>
50 #include <paths.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sysexits.h>
56 #include <unistd.h>
57 #include <utime.h>
59 #include "pathnames.h"
61 /* Bootstrap aid - this doesn't exist in most older releases */
62 #ifndef MAP_FAILED
63 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
64 #endif
66 #define MAX_CMP_SIZE (16 * 1024 * 1024)
68 #define DIRECTORY 0x01 /* Tell install it's a directory. */
69 #define SETFLAGS 0x02 /* Tell install to set flags. */
70 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
71 #define BACKUP_SUFFIX ".old"
73 struct passwd *pp;
74 struct group *gp;
75 gid_t gid;
76 uid_t uid;
77 int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
78 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
79 const char *suffix = BACKUP_SUFFIX;
81 static int file_getgroup(const char *etcdir, const char *group, gid_t *gid);
82 static int file_getowner(const char *etcdir, const char *owner, uid_t *uid);
84 void copy(int, const char *, int, const char *, off_t);
85 int compare(int, const char *, size_t, int, const char *, size_t);
86 int create_newfile(const char *, int, struct stat *);
87 int create_tempfile(const char *, char *, size_t);
88 void install(const char *, const char *, u_long, u_long, u_int);
89 void install_dir(char *);
90 u_long numeric_id(const char *, const char *);
91 void strip(const char *);
92 int trymmap(int);
93 void usage(void);
95 int
96 main(int argc, char **argv)
98 struct stat from_sb, to_sb;
99 mode_t *set;
100 u_long fset;
101 u_long fclr;
102 int ch, no_target;
103 int trysys;
104 u_int iflags;
105 char *flags;
106 const char *group, *owner, *to_name;
107 const char *etcdir;
109 iflags = 0;
110 trysys = 0;
111 group = NULL;
112 owner = NULL;
113 etcdir = NULL;
115 while ((ch = getopt(argc, argv, "L:B:bCcdf:g:lMm:o:pSsv")) != -1)
116 switch((char)ch) {
117 case 'L':
118 etcdir = optarg;
119 break;
120 case 'B':
121 suffix = optarg;
122 /* FALLTHROUGH */
123 case 'b':
124 dobackup = 1;
125 break;
126 case 'C':
127 docompare = 1;
128 break;
129 case 'c':
130 /* For backwards compatibility. */
131 break;
132 case 'd':
133 dodir = 1;
134 break;
135 case 'f':
136 flags = optarg;
137 if (strtofflags(&flags, &fset, &fclr))
138 errx(EX_USAGE, "%s: invalid flag", flags);
139 iflags |= SETFLAGS;
140 break;
141 case 'g':
142 group = optarg;
143 break;
144 case 'l':
145 trysys = 1;
146 break;
147 case 'M':
148 nommap = 1;
149 break;
150 case 'm':
151 if (!(set = setmode(optarg)))
152 errx(EX_USAGE, "invalid file mode: %s",
153 optarg);
154 mode = getmode(set, 0);
155 free(set);
156 break;
157 case 'o':
158 owner = optarg;
159 break;
160 case 'p':
161 docompare = dopreserve = 1;
162 break;
163 case 'S':
164 safecopy = 1;
165 break;
166 case 's':
167 dostrip = 1;
168 break;
169 case 'v':
170 verbose = 1;
171 break;
172 case '?':
173 default:
174 usage();
176 argc -= optind;
177 argv += optind;
179 /* some options make no sense when creating directories */
180 if (dostrip && dodir)
181 usage();
183 /* must have at least two arguments, except when creating directories */
184 if (argc < 2 && !dodir)
185 usage();
187 /* need to make a temp copy so we can compare stripped version */
188 if (docompare && dostrip)
189 safecopy = 1;
191 /* no etcdir specified, always try the system */
192 if (etcdir == NULL)
193 trysys = 1;
194 uid = (uid_t)-1;
195 gid = (gid_t)-1;
197 /* get group and owner id's */
198 if (group != NULL) {
199 if (etcdir && file_getgroup(etcdir, group, &gid)) {
201 } else if (trysys && (gp = getgrnam(group)) != NULL) {
202 gid = gp->gr_gid;
203 } else {
204 gid = (gid_t)numeric_id(group, "group");
208 if (owner != NULL) {
209 if (etcdir && file_getowner(etcdir, owner, &uid)) {
211 } else if (trysys && (pp = getpwnam(owner)) != NULL) {
212 uid = pp->pw_uid;
213 } else {
214 uid = (uid_t)numeric_id(owner, "user");
218 if (dodir) {
219 for (; *argv != NULL; ++argv)
220 install_dir(*argv);
221 exit(EX_OK);
222 /* NOTREACHED */
225 no_target = stat(to_name = argv[argc - 1], &to_sb);
226 if (!no_target && S_ISDIR(to_sb.st_mode)) {
227 for (; *argv != to_name; ++argv)
228 install(*argv, to_name, fset, fclr, iflags | DIRECTORY);
229 exit(EX_OK);
230 /* NOTREACHED */
233 /* can't do file1 file2 directory/file */
234 if (argc != 2)
235 usage();
237 if (!no_target) {
238 if (stat(*argv, &from_sb))
239 err(EX_OSERR, "%s", *argv);
240 if (!S_ISREG(to_sb.st_mode)) {
241 errno = EFTYPE;
242 err(EX_OSERR, "%s", to_name);
244 if (to_sb.st_dev == from_sb.st_dev &&
245 to_sb.st_ino == from_sb.st_ino)
246 errx(EX_USAGE,
247 "%s and %s are the same file", *argv, to_name);
249 install(*argv, to_name, fset, fclr, iflags);
250 exit(EX_OK);
251 /* NOTREACHED */
254 u_long
255 numeric_id(const char *name, const char *type)
257 u_long val;
258 char *ep;
261 * XXX
262 * We know that uid_t's and gid_t's are unsigned longs.
264 errno = 0;
265 val = strtoul(name, &ep, 10);
266 if (errno)
267 err(EX_NOUSER, "%s", name);
268 if (*ep != '\0')
269 errx(EX_NOUSER, "unknown %s %s", type, name);
270 return (val);
273 static
275 file_getgroup(const char *etcdir, const char *group, gid_t *gid)
277 FILE *fp;
278 size_t len;
279 size_t grlen;
280 char *path;
281 char *ptr;
282 char *scan;
284 grlen = strlen(group);
286 if (asprintf(&path, "%s/group", etcdir) < 0)
287 errx(EX_OSERR, "asprintf()");
288 if ((fp = fopen(path, "r")) != NULL) {
289 while ((ptr = fgetln(fp, &len)) != NULL && len) {
290 ptr[len - 1] = 0;
291 if ((scan = strchr(ptr, ':')) == NULL)
292 continue;
293 if (scan - ptr != grlen)
294 continue;
295 if (strncmp(ptr, group, grlen) != 0)
296 continue;
297 if ((scan = strchr(scan + 1, ':')) == NULL)
298 continue;
299 *gid = strtoul(scan + 1, NULL, 10);
300 break;
302 fclose(fp);
304 free(path);
305 return((*gid == (gid_t)-1) ? 0 : 1);
308 static
310 file_getowner(const char *etcdir, const char *owner, uid_t *uid)
312 FILE *fp;
313 size_t len;
314 size_t owner_len;
315 char *path;
316 char *ptr;
317 char *scan;
319 owner_len = strlen(owner);
321 if (asprintf(&path, "%s/master.passwd", etcdir) < 0)
322 errx(EX_OSERR, "asprintf()");
323 if ((fp = fopen(path, "r")) != NULL) {
324 while ((ptr = fgetln(fp, &len)) != NULL && len) {
325 ptr[len - 1] = 0;
326 if ((scan = strchr(ptr, ':')) == NULL)
327 continue;
328 if (scan - ptr != owner_len)
329 continue;
330 if (strncmp(ptr, owner, owner_len) != 0)
331 continue;
332 if ((scan = strchr(scan + 1, ':')) == NULL)
333 continue;
334 *uid = strtoul(scan + 1, NULL, 10);
335 break;
337 fclose(fp);
339 free(path);
340 return((*uid == (uid_t)-1) ? 0 : 1);
344 * install --
345 * build a path name and install the file
347 void
348 install(const char *from_name, const char *to_name, u_long fset, u_long fclr,
349 u_int flags)
351 struct stat from_sb, temp_sb, to_sb;
352 struct utimbuf utb;
353 int devnull, files_match, from_fd, serrno, target;
354 int tempcopy, temp_fd, to_fd;
355 u_long nfset;
356 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
358 files_match = 0;
360 /* If try to install NULL file to a directory, fails. */
361 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
362 if (stat(from_name, &from_sb))
363 err(EX_OSERR, "%s", from_name);
364 if (!S_ISREG(from_sb.st_mode)) {
365 errno = EFTYPE;
366 err(EX_OSERR, "%s", from_name);
368 /* Build the target path. */
369 if (flags & DIRECTORY) {
370 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
371 to_name,
372 (p = strrchr(from_name, '/')) ? ++p : from_name);
373 to_name = pathbuf;
375 devnull = 0;
376 } else {
377 devnull = 1;
380 target = stat(to_name, &to_sb) == 0;
382 /* Only install to regular files. */
383 if (target && !S_ISREG(to_sb.st_mode)) {
384 errno = EFTYPE;
385 warn("%s", to_name);
386 return;
389 /* Only copy safe if the target exists. */
390 tempcopy = safecopy && target;
392 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
393 err(EX_OSERR, "%s", from_name);
395 /* If we don't strip, we can compare first. */
396 if (docompare && !dostrip && target) {
397 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
398 err(EX_OSERR, "%s", to_name);
399 if (devnull)
400 files_match = to_sb.st_size == 0;
401 else
402 files_match = !(compare(from_fd, from_name,
403 (size_t)from_sb.st_size, to_fd,
404 to_name, (size_t)to_sb.st_size));
406 /* Close "to" file unless we match. */
407 if (!files_match)
408 close(to_fd);
411 if (!files_match) {
412 if (tempcopy) {
413 to_fd = create_tempfile(to_name, tempfile,
414 sizeof(tempfile));
415 if (to_fd < 0)
416 err(EX_OSERR, "%s", tempfile);
417 } else {
418 if ((to_fd = create_newfile(to_name, target,
419 &to_sb)) < 0)
420 err(EX_OSERR, "%s", to_name);
421 if (verbose)
422 printf("install: %s -> %s\n",
423 from_name, to_name);
425 if (!devnull)
426 copy(from_fd, from_name, to_fd,
427 tempcopy ? tempfile : to_name, from_sb.st_size);
430 if (dostrip) {
431 strip(tempcopy ? tempfile : to_name);
434 * Re-open our fd on the target, in case we used a strip
435 * that does not work in-place -- like GNU binutils strip.
437 close(to_fd);
438 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
439 if (to_fd < 0)
440 err(EX_OSERR, "stripping %s", to_name);
444 * Compare the stripped temp file with the target.
446 if (docompare && dostrip && target) {
447 temp_fd = to_fd;
449 /* Re-open to_fd using the real target name. */
450 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
451 err(EX_OSERR, "%s", to_name);
453 if (fstat(temp_fd, &temp_sb)) {
454 serrno = errno;
455 unlink(tempfile);
456 errno = serrno;
457 err(EX_OSERR, "%s", tempfile);
460 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
461 to_name, (size_t)to_sb.st_size) == 0) {
463 * If target has more than one link we need to
464 * replace it in order to snap the extra links.
465 * Need to preserve target file times, though.
467 if (to_sb.st_nlink != 1) {
468 utb.actime = to_sb.st_atime;
469 utb.modtime = to_sb.st_mtime;
470 utime(tempfile, &utb);
471 } else {
472 files_match = 1;
473 unlink(tempfile);
475 close(temp_fd);
480 * Move the new file into place if doing a safe copy
481 * and the files are different (or just not compared).
483 if (tempcopy && !files_match) {
484 /* Try to turn off the immutable bits. */
485 if (to_sb.st_flags & NOCHANGEBITS)
486 chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
487 if (dobackup) {
488 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
489 suffix) != strlen(to_name) + strlen(suffix)) {
490 unlink(tempfile);
491 errx(EX_OSERR, "%s: backup filename too long",
492 to_name);
494 if (verbose)
495 printf("install: %s -> %s\n", to_name, backup);
496 if (rename(to_name, backup) < 0) {
497 serrno = errno;
498 unlink(tempfile);
499 errno = serrno;
500 err(EX_OSERR, "rename: %s to %s", to_name,
501 backup);
504 if (verbose)
505 printf("install: %s -> %s\n", from_name, to_name);
506 if (rename(tempfile, to_name) < 0) {
507 serrno = errno;
508 unlink(tempfile);
509 errno = serrno;
510 err(EX_OSERR, "rename: %s to %s",
511 tempfile, to_name);
514 /* Re-open to_fd so we aren't hosed by the rename(2). */
515 close(to_fd);
516 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
517 err(EX_OSERR, "%s", to_name);
521 * Preserve the timestamp of the source file if necessary.
523 if (dopreserve && !files_match && !devnull) {
524 utb.actime = from_sb.st_atime;
525 utb.modtime = from_sb.st_mtime;
526 utime(to_name, &utb);
529 if (fstat(to_fd, &to_sb) == -1) {
530 serrno = errno;
531 unlink(to_name);
532 errno = serrno;
533 err(EX_OSERR, "%s", to_name);
537 * Set owner, group, mode for target; do the chown first,
538 * chown may lose the setuid bits.
540 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
541 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
542 (mode != to_sb.st_mode)) {
543 /* Try to turn off the immutable bits. */
544 if (to_sb.st_flags & NOCHANGEBITS)
545 fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
548 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
549 (uid != (uid_t)-1 && uid != to_sb.st_uid))
550 if (fchown(to_fd, uid, gid) == -1) {
551 serrno = errno;
552 unlink(to_name);
553 errno = serrno;
554 err(EX_OSERR,"%s: chown/chgrp", to_name);
557 if (mode != to_sb.st_mode)
558 if (fchmod(to_fd, mode)) {
559 serrno = errno;
560 unlink(to_name);
561 errno = serrno;
562 err(EX_OSERR, "%s: chmod", to_name);
566 * If provided a set of flags, set them, otherwise, preserve the
567 * flags, except for the dump and history flags. The dump flag
568 * is left clear on the target while the history flag from when
569 * the target was created (which is inherited from the target's
570 * parent directory) is retained.
572 if (flags & SETFLAGS) {
573 nfset = (to_sb.st_flags | fset) & ~fclr;
574 } else {
575 nfset = from_sb.st_flags & ~(UF_NODUMP | UF_NOHISTORY) |
576 (to_sb.st_flags & UF_NOHISTORY);
580 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
581 * trying to turn off UF_NODUMP. If we're trying to set real flags,
582 * then warn if the the fs doesn't support it, otherwise fail.
584 if (!devnull && fchflags(to_fd, nfset)) {
585 if (flags & SETFLAGS) {
586 if (errno == EOPNOTSUPP)
587 warn("%s: chflags", to_name);
588 else {
589 serrno = errno;
590 unlink(to_name);
591 errno = serrno;
592 err(EX_OSERR, "%s: chflags", to_name);
597 close(to_fd);
598 if (!devnull)
599 close(from_fd);
603 * compare --
604 * compare two files; non-zero means files differ
607 compare(int from_fd, const char *from_name __unused, size_t from_len,
608 int to_fd, const char *to_name __unused, size_t to_len)
610 char *p, *q;
611 int rv;
612 int done_compare;
614 rv = 0;
615 if (from_len != to_len)
616 return 1;
618 if (from_len <= MAX_CMP_SIZE) {
619 done_compare = 0;
620 if (trymmap(from_fd) && trymmap(to_fd)) {
621 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
622 if (p == (char *)MAP_FAILED)
623 goto out;
624 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
625 if (q == (char *)MAP_FAILED) {
626 munmap(p, from_len);
627 goto out;
630 rv = memcmp(p, q, from_len);
631 munmap(p, from_len);
632 munmap(q, from_len);
633 done_compare = 1;
635 out:
636 if (!done_compare) {
637 char buf1[MAXBSIZE];
638 char buf2[MAXBSIZE];
639 int n1, n2;
641 rv = 0;
642 lseek(from_fd, 0, SEEK_SET);
643 lseek(to_fd, 0, SEEK_SET);
644 while (rv == 0) {
645 n1 = read(from_fd, buf1, sizeof(buf1));
646 if (n1 == 0)
647 break; /* EOF */
648 else if (n1 > 0) {
649 n2 = read(to_fd, buf2, n1);
650 if (n2 == n1)
651 rv = memcmp(buf1, buf2, n1);
652 else
653 rv = 1; /* out of sync */
654 } else
655 rv = 1; /* read failure */
657 lseek(from_fd, 0, SEEK_SET);
658 lseek(to_fd, 0, SEEK_SET);
660 } else
661 rv = 1; /* don't bother in this case */
663 return rv;
667 * create_tempfile --
668 * create a temporary file based on path and open it
671 create_tempfile(const char *path, char *temp, size_t tsize)
673 char *p;
675 strncpy(temp, path, tsize);
676 temp[tsize - 1] = '\0';
677 if ((p = strrchr(temp, '/')) != NULL)
678 p++;
679 else
680 p = temp;
681 strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
682 temp[tsize - 1] = '\0';
683 return (mkstemp(temp));
687 * create_newfile --
688 * create a new file, overwriting an existing one if necessary
691 create_newfile(const char *path, int target, struct stat *sbp)
693 char backup[MAXPATHLEN];
695 if (target) {
697 * Unlink now... avoid ETXTBSY errors later. Try to turn
698 * off the append/immutable bits -- if we fail, go ahead,
699 * it might work.
701 if (sbp->st_flags & NOCHANGEBITS)
702 chflags(path, sbp->st_flags & ~NOCHANGEBITS);
704 if (dobackup) {
705 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
706 path, suffix) != strlen(path) + strlen(suffix))
707 errx(EX_OSERR, "%s: backup filename too long",
708 path);
709 snprintf(backup, MAXPATHLEN, "%s%s",
710 path, suffix);
711 if (verbose)
712 printf("install: %s -> %s\n",
713 path, backup);
714 if (rename(path, backup) < 0)
715 err(EX_OSERR, "rename: %s to %s", path, backup);
716 } else
717 unlink(path);
720 return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
724 * copy --
725 * copy from one file to another
727 void
728 copy(int from_fd, const char *from_name, int to_fd,
729 const char *to_name, off_t size)
731 int nr, nw;
732 int serrno;
733 char *p, buf[MAXBSIZE];
734 int done_copy;
736 /* Rewind file descriptors. */
737 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
738 err(EX_OSERR, "lseek: %s", from_name);
739 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
740 err(EX_OSERR, "lseek: %s", to_name);
743 * Mmap and write if less than 8M (the limit is so we don't totally
744 * trash memory on big files. This is really a minor hack, but it
745 * wins some CPU back.
747 done_copy = 0;
748 if (size <= 8 * 1048576 && trymmap(from_fd) &&
749 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
750 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
751 if ((nw = write(to_fd, p, size)) != size) {
752 serrno = errno;
753 unlink(to_name);
754 errno = nw > 0 ? EIO : serrno;
755 err(EX_OSERR, "%s", to_name);
757 done_copy = 1;
759 if (!done_copy) {
760 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
761 if ((nw = write(to_fd, buf, nr)) != nr) {
762 serrno = errno;
763 unlink(to_name);
764 errno = nw > 0 ? EIO : serrno;
765 err(EX_OSERR, "%s", to_name);
767 if (nr != 0) {
768 serrno = errno;
769 unlink(to_name);
770 errno = serrno;
771 err(EX_OSERR, "%s", from_name);
777 * strip --
778 * use strip(1) to strip the target file
780 void
781 strip(const char *to_name)
783 const char *stripbin;
784 int serrno, status;
786 switch (fork()) {
787 case -1:
788 serrno = errno;
789 unlink(to_name);
790 errno = serrno;
791 err(EX_TEMPFAIL, "fork");
792 case 0:
793 stripbin = getenv("STRIPBIN");
794 if (stripbin == NULL)
795 stripbin = "strip";
796 execlp(stripbin, stripbin, to_name, (char *)NULL);
797 err(EX_OSERR, "exec(%s)", stripbin);
798 default:
799 if (wait(&status) == -1 || status) {
800 serrno = errno;
801 unlink(to_name);
802 errc(EX_SOFTWARE, serrno, "wait");
803 /* NOTREACHED */
809 * install_dir --
810 * build directory hierarchy
812 void
813 install_dir(char *path)
815 char *p;
816 struct stat sb;
817 int ch;
819 for (p = path;; ++p)
820 if (!*p || (p != path && *p == '/')) {
821 ch = *p;
822 *p = '\0';
823 if (stat(path, &sb)) {
824 if (errno != ENOENT || mkdir(path, 0755) < 0) {
825 err(EX_OSERR, "mkdir %s", path);
826 /* NOTREACHED */
827 } else if (verbose)
828 printf("install: mkdir %s\n",
829 path);
830 } else if (!S_ISDIR(sb.st_mode))
831 errx(EX_OSERR, "%s exists but is not a directory", path);
832 if (!(*p = ch))
833 break;
836 if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
837 warn("chown %u:%u %s", uid, gid, path);
838 if (chmod(path, mode))
839 warn("chmod %o %s", mode, path);
843 * usage --
844 * print a usage message and die
846 void
847 usage(void)
849 fprintf(stderr, "\
850 usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
851 [-o owner] file1 file2\n\
852 install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
853 [-o owner] file1 ... fileN directory\n\
854 install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
855 exit(EX_USAGE);
856 /* NOTREACHED */
860 * trymmap --
861 * return true (1) if mmap should be tried, false (0) if not.
864 trymmap(int fd)
867 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
868 * pre-Lite2-merge systems.
870 #ifdef MFSNAMELEN
871 struct statfs stfs;
873 if (nommap || fstatfs(fd, &stfs) != 0)
874 return (0);
875 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
876 strcmp(stfs.f_fstypename, "cd9660") == 0)
877 return (1);
878 #endif
879 return (0);