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