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