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