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