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