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