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