2 * Copyright (c) 2012, 2013 SRI International
3 * Copyright (c) 1987, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1987, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
39 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
48 #include <sys/mount.h>
75 /* Bootstrap aid - this doesn't exist in most older releases */
77 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
80 #define MAX_CMP_SIZE (16 * 1024 * 1024)
82 #define LN_ABSOLUTE 0x01
83 #define LN_RELATIVE 0x02
85 #define LN_SYMBOLIC 0x08
88 #define DIRECTORY 0x01 /* Tell install it's a directory. */
89 #define SETFLAGS 0x02 /* Tell install to set flags. */
90 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
91 #define BACKUP_SUFFIX ".old"
95 RIPEMD160_CTX RIPEMD160;
108 } digesttype = DIGEST_NONE;
112 static int dobackup, docompare, dodir, dolink, dopreserve, dostrip, dounpriv,
114 static int haveopt_f, haveopt_g, haveopt_m, haveopt_o;
115 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
117 static const char *group, *owner;
118 static const char *suffix = BACKUP_SUFFIX;
119 static char *destdir, *digest, *fflags, *metafile, *tags;
121 static int compare(int, const char *, size_t, int, const char *, size_t,
123 static char *copy(int, const char *, int, const char *, off_t);
124 static int create_newfile(const char *, int, struct stat *);
125 static int create_tempfile(const char *, char *, size_t);
126 static char *quiet_mktemp(char *template);
127 static char *digest_file(const char *);
128 static void digest_init(DIGEST_CTX *);
129 static void digest_update(DIGEST_CTX *, const unsigned char *, size_t);
130 static char *digest_end(DIGEST_CTX *, char *);
131 static int do_link(const char *, const char *, const struct stat *);
132 static void do_symlink(const char *, const char *, const struct stat *);
133 static void makelink(const char *, const char *, const struct stat *);
134 static void install(const char *, const char *, u_long, u_int);
135 static void install_dir(char *);
136 static void metadata_log(const char *, const char *, struct timeval *,
137 const char *, const char *, off_t);
138 static int parseid(const char *, id_t *);
139 static void strip(const char *);
140 static int trymmap(int);
141 static void usage(void);
144 main(int argc, char *argv[])
146 struct stat from_sb, to_sb;
155 group = owner = NULL;
156 while ((ch = getopt(argc, argv, "B:bCcD:df:g:h:l:M:m:N:o:pSsT:Uv")) !=
169 /* For backwards compatibility. */
189 for (p = optarg; *p != '\0'; p++)
192 dolink &= ~(LN_HARD|LN_MIXED);
193 dolink |= LN_SYMBOLIC;
196 dolink &= ~(LN_SYMBOLIC|LN_MIXED);
200 dolink &= ~(LN_SYMBOLIC|LN_HARD);
204 dolink &= ~LN_RELATIVE;
205 dolink |= LN_ABSOLUTE;
208 dolink &= ~LN_ABSOLUTE;
209 dolink |= LN_RELATIVE;
212 errx(1, "%c: invalid link type", *p);
221 if (!(set = setmode(optarg)))
222 errx(EX_USAGE, "invalid file mode: %s",
224 mode = getmode(set, 0);
228 if (!setup_getid(optarg))
229 err(EX_OSERR, "Unable to use user and group "
230 "databases in `%s'", optarg);
237 docompare = dopreserve = 1;
261 /* some options make no sense when creating directories */
262 if (dostrip && dodir) {
263 warnx("-d and -s may not be specified together");
267 if (getenv("DONTSTRIP") != NULL) {
268 warnx("DONTSTRIP set - will not strip installed binaries");
272 /* must have at least two arguments, except when creating directories */
273 if (argc == 0 || (argc == 1 && !dodir))
276 if (digest != NULL) {
277 if (strcmp(digest, "none") == 0) {
278 digesttype = DIGEST_NONE;
279 } else if (strcmp(digest, "md5") == 0) {
280 digesttype = DIGEST_MD5;
281 } else if (strcmp(digest, "rmd160") == 0) {
282 digesttype = DIGEST_RIPEMD160;
283 } else if (strcmp(digest, "sha1") == 0) {
284 digesttype = DIGEST_SHA1;
285 } else if (strcmp(digest, "sha256") == 0) {
286 digesttype = DIGEST_SHA256;
287 } else if (strcmp(digest, "sha512") == 0) {
288 digesttype = DIGEST_SHA512;
290 warnx("unknown digest `%s'", digest);
295 /* need to make a temp copy so we can compare stripped version */
296 if (docompare && dostrip)
299 /* get group and owner id's */
300 if (group != NULL && !dounpriv) {
301 if (gid_from_group(group, &gid) == -1) {
303 if (!parseid(group, &id))
304 errx(1, "unknown group %s", group);
310 if (owner != NULL && !dounpriv) {
311 if (uid_from_user(owner, &uid) == -1) {
313 if (!parseid(owner, &id))
314 errx(1, "unknown user %s", owner);
320 if (fflags != NULL && !dounpriv) {
321 if (strtofflags(&fflags, &fset, NULL))
322 errx(EX_USAGE, "%s: invalid flag", fflags);
326 if (metafile != NULL) {
327 if ((metafp = fopen(metafile, "a")) == NULL)
328 warn("open %s", metafile);
330 digesttype = DIGEST_NONE;
333 for (; *argv != NULL; ++argv)
339 to_name = argv[argc - 1];
340 no_target = stat(to_name, &to_sb);
341 if (!no_target && S_ISDIR(to_sb.st_mode)) {
342 if (dolink & LN_SYMBOLIC) {
343 if (lstat(to_name, &to_sb) != 0)
344 err(EX_OSERR, "%s vanished", to_name);
345 if (S_ISLNK(to_sb.st_mode)) {
348 err(EX_USAGE, "%s", to_name);
350 install(*argv, to_name, fset, iflags);
354 for (; *argv != to_name; ++argv)
355 install(*argv, to_name, fset, iflags | DIRECTORY);
360 /* can't do file1 file2 directory/file */
363 warnx("target directory `%s' does not exist",
366 warnx("target `%s' is not a directory",
371 if (!no_target && !dolink) {
372 if (stat(*argv, &from_sb))
373 err(EX_OSERR, "%s", *argv);
374 if (!S_ISREG(to_sb.st_mode)) {
376 err(EX_OSERR, "%s", to_name);
378 if (to_sb.st_dev == from_sb.st_dev &&
379 to_sb.st_ino == from_sb.st_ino)
381 "%s and %s are the same file", *argv, to_name);
383 install(*argv, to_name, fset, iflags);
389 digest_file(const char *name)
392 switch (digesttype) {
394 return (MD5File(name, NULL));
395 case DIGEST_RIPEMD160:
396 return (RIPEMD160_File(name, NULL));
398 return (SHA1_File(name, NULL));
400 return (SHA256_File(name, NULL));
402 return (SHA512_File(name, NULL));
409 digest_init(DIGEST_CTX *c)
412 switch (digesttype) {
418 case DIGEST_RIPEMD160:
419 RIPEMD160_Init(&(c->RIPEMD160));
422 SHA1_Init(&(c->SHA1));
425 SHA256_Init(&(c->SHA256));
428 SHA512_Init(&(c->SHA512));
434 digest_update(DIGEST_CTX *c, const unsigned char *data, size_t len)
437 switch (digesttype) {
441 MD5Update(&(c->MD5), data, len);
443 case DIGEST_RIPEMD160:
444 RIPEMD160_Update(&(c->RIPEMD160), data, len);
447 SHA1_Update(&(c->SHA1), data, len);
450 SHA256_Update(&(c->SHA256), data, len);
453 SHA512_Update(&(c->SHA512), data, len);
459 digest_end(DIGEST_CTX *c, char *buf)
462 switch (digesttype) {
464 return (MD5End(&(c->MD5), buf));
465 case DIGEST_RIPEMD160:
466 return (RIPEMD160_End(&(c->RIPEMD160), buf));
468 return (SHA1_End(&(c->SHA1), buf));
470 return (SHA256_End(&(c->SHA256), buf));
472 return (SHA512_End(&(c->SHA512), buf));
480 * parse uid or gid from arg into id, returning non-zero if successful
483 parseid(const char *name, id_t *id)
487 *id = (id_t)strtoul(name, &ep, 10);
488 if (errno || *ep != '\0')
495 * mktemp implementation used mkstemp to avoid mktemp warnings. We
496 * really do need mktemp semantics here as we will be creating a link.
499 quiet_mktemp(char *template)
503 if ((fd = mkstemp(template)) == -1)
506 if (unlink(template) == -1)
507 err(EX_OSERR, "unlink %s", template);
513 * make a hard link, obeying dorename if set
514 * return -1 on failure
517 do_link(const char *from_name, const char *to_name,
518 const struct stat *target_sb)
520 char tmpl[MAXPATHLEN];
523 if (safecopy && target_sb != NULL) {
524 (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
525 /* This usage is safe. */
526 if (quiet_mktemp(tmpl) == NULL)
527 err(EX_OSERR, "%s: mktemp", tmpl);
528 ret = link(from_name, tmpl);
530 if (target_sb->st_mode & S_IFDIR && rmdir(to_name) ==
533 err(EX_OSERR, "%s", to_name);
535 if (target_sb->st_flags & NOCHANGEBITS)
536 (void)chflags(to_name, target_sb->st_flags &
539 ret = rename(tmpl, to_name);
541 * If rename has posix semantics, then the temporary
542 * file may still exist when from_name and to_name point
543 * to the same file, so unlink it unconditionally.
549 return (link(from_name, to_name));
554 * Make a symbolic link, obeying dorename if set. Exit on failure.
557 do_symlink(const char *from_name, const char *to_name,
558 const struct stat *target_sb)
560 char tmpl[MAXPATHLEN];
562 if (safecopy && target_sb != NULL) {
563 (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
564 /* This usage is safe. */
565 if (quiet_mktemp(tmpl) == NULL)
566 err(EX_OSERR, "%s: mktemp", tmpl);
568 if (symlink(from_name, tmpl) == -1)
569 err(EX_OSERR, "symlink %s -> %s", from_name, tmpl);
571 if (target_sb->st_mode & S_IFDIR && rmdir(to_name) == -1) {
573 err(EX_OSERR, "%s", to_name);
575 if (target_sb->st_flags & NOCHANGEBITS)
576 (void)chflags(to_name, target_sb->st_flags &
580 if (rename(tmpl, to_name) == -1) {
581 /* Remove temporary link before exiting. */
583 err(EX_OSERR, "%s: rename", to_name);
586 if (symlink(from_name, to_name) == -1)
587 err(EX_OSERR, "symlink %s -> %s", from_name, to_name);
593 * make a link from source to destination
596 makelink(const char *from_name, const char *to_name,
597 const struct stat *target_sb)
599 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
602 /* Try hard links first. */
603 if (dolink & (LN_HARD|LN_MIXED)) {
604 if (do_link(from_name, to_name, target_sb) == -1) {
605 if ((dolink & LN_HARD) || errno != EXDEV)
606 err(EX_OSERR, "link %s -> %s", from_name, to_name);
608 if (stat(to_name, &to_sb))
609 err(EX_OSERR, "%s: stat", to_name);
610 if (S_ISREG(to_sb.st_mode)) {
612 * XXX: hard links to anything other than
613 * plain files are not metalogged
616 const char *oowner, *ogroup;
621 * XXX: use underlying perms, unless
622 * overridden on command line.
626 mode = (to_sb.st_mode & 0777);
636 dres = digest_file(from_name);
637 metadata_log(to_name, "file", NULL, NULL,
638 dres, to_sb.st_size);
649 /* Symbolic links. */
650 if (dolink & LN_ABSOLUTE) {
651 /* Convert source path to absolute. */
652 if (realpath(from_name, src) == NULL)
653 err(EX_OSERR, "%s: realpath", from_name);
654 do_symlink(src, to_name, target_sb);
655 /* XXX: src may point outside of destdir */
656 metadata_log(to_name, "link", NULL, src, NULL, 0);
660 if (dolink & LN_RELATIVE) {
663 /* Resolve pathnames. */
664 if (realpath(from_name, src) == NULL)
665 err(EX_OSERR, "%s: realpath", from_name);
668 * The last component of to_name may be a symlink,
669 * so use realpath to resolve only the directory.
671 cp = dirname(to_name);
672 if (realpath(cp, dst) == NULL)
673 err(EX_OSERR, "%s: realpath", cp);
674 /* .. and add the last component. */
675 if (strcmp(dst, "/") != 0) {
676 if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
677 errx(1, "resolved pathname too long");
679 cp = basename(to_name);
680 if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
681 errx(1, "resolved pathname too long");
683 /* Trim common path components. */
684 for (s = src, d = dst; *s == *d; s++, d++)
689 /* Count the number of directories we need to backtrack. */
690 for (++d, lnk[0] = '\0'; *d; d++)
692 (void)strlcat(lnk, "../", sizeof(lnk));
694 (void)strlcat(lnk, ++s, sizeof(lnk));
696 do_symlink(lnk, to_name, target_sb);
697 /* XXX: Link may point outside of destdir. */
698 metadata_log(to_name, "link", NULL, lnk, NULL, 0);
703 * If absolute or relative was not specified, try the names the
706 do_symlink(from_name, to_name, target_sb);
707 /* XXX: from_name may point outside of destdir. */
708 metadata_log(to_name, "link", NULL, from_name, NULL, 0);
713 * build a path name and install the file
716 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
718 struct stat from_sb, temp_sb, to_sb;
719 struct timeval tvb[2];
720 int devnull, files_match, from_fd, serrno, target;
721 int tempcopy, temp_fd, to_fd;
722 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
729 /* If try to install NULL file to a directory, fails. */
730 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
732 if (stat(from_name, &from_sb))
733 err(EX_OSERR, "%s", from_name);
734 if (!S_ISREG(from_sb.st_mode)) {
736 err(EX_OSERR, "%s", from_name);
739 /* Build the target path. */
740 if (flags & DIRECTORY) {
741 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
743 (p = strrchr(from_name, '/')) ? ++p : from_name);
752 target = (stat(to_name, &to_sb) == 0);
754 target = (lstat(to_name, &to_sb) == 0);
757 if (target && !safecopy) {
758 if (to_sb.st_mode & S_IFDIR && rmdir(to_name) == -1)
759 err(EX_OSERR, "%s", to_name);
760 if (to_sb.st_flags & NOCHANGEBITS)
761 (void)chflags(to_name,
762 to_sb.st_flags & ~NOCHANGEBITS);
765 makelink(from_name, to_name, target ? &to_sb : NULL);
769 /* Only install to regular files. */
770 if (target && !S_ISREG(to_sb.st_mode)) {
776 /* Only copy safe if the target exists. */
777 tempcopy = safecopy && target;
779 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
780 err(EX_OSERR, "%s", from_name);
782 /* If we don't strip, we can compare first. */
783 if (docompare && !dostrip && target) {
784 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
785 err(EX_OSERR, "%s", to_name);
787 files_match = to_sb.st_size == 0;
789 files_match = !(compare(from_fd, from_name,
790 (size_t)from_sb.st_size, to_fd,
791 to_name, (size_t)to_sb.st_size, &digestresult));
793 /* Close "to" file unless we match. */
800 to_fd = create_tempfile(to_name, tempfile,
803 err(EX_OSERR, "%s", tempfile);
805 if ((to_fd = create_newfile(to_name, target,
807 err(EX_OSERR, "%s", to_name);
809 (void)printf("install: %s -> %s\n",
813 digestresult = copy(from_fd, from_name, to_fd,
814 tempcopy ? tempfile : to_name, from_sb.st_size);
820 strip(tempcopy ? tempfile : to_name);
823 * Re-open our fd on the target, in case we used a strip
824 * that does not work in-place -- like GNU binutils strip.
827 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
829 err(EX_OSERR, "stripping %s", to_name);
833 * Compare the stripped temp file with the target.
835 if (docompare && dostrip && target) {
838 /* Re-open to_fd using the real target name. */
839 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
840 err(EX_OSERR, "%s", to_name);
842 if (fstat(temp_fd, &temp_sb)) {
844 (void)unlink(tempfile);
846 err(EX_OSERR, "%s", tempfile);
849 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
850 to_name, (size_t)to_sb.st_size, &digestresult)
853 * If target has more than one link we need to
854 * replace it in order to snap the extra links.
855 * Need to preserve target file times, though.
857 if (to_sb.st_nlink != 1) {
858 tvb[0].tv_sec = to_sb.st_atime;
860 tvb[1].tv_sec = to_sb.st_mtime;
862 (void)utimes(tempfile, tvb);
865 (void)unlink(tempfile);
867 (void) close(temp_fd);
871 if (dostrip && (!docompare || !target))
872 digestresult = digest_file(tempfile);
875 * Move the new file into place if doing a safe copy
876 * and the files are different (or just not compared).
878 if (tempcopy && !files_match) {
879 /* Try to turn off the immutable bits. */
880 if (to_sb.st_flags & NOCHANGEBITS)
881 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
883 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
884 suffix) != strlen(to_name) + strlen(suffix)) {
886 errx(EX_OSERR, "%s: backup filename too long",
890 (void)printf("install: %s -> %s\n", to_name, backup);
891 if (rename(to_name, backup) < 0) {
895 err(EX_OSERR, "rename: %s to %s", to_name,
900 (void)printf("install: %s -> %s\n", from_name, to_name);
901 if (rename(tempfile, to_name) < 0) {
905 err(EX_OSERR, "rename: %s to %s",
909 /* Re-open to_fd so we aren't hosed by the rename(2). */
911 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
912 err(EX_OSERR, "%s", to_name);
916 * Preserve the timestamp of the source file if necessary.
918 if (dopreserve && !files_match && !devnull) {
919 tvb[0].tv_sec = from_sb.st_atime;
921 tvb[1].tv_sec = from_sb.st_mtime;
923 (void)utimes(to_name, tvb);
926 if (fstat(to_fd, &to_sb) == -1) {
928 (void)unlink(to_name);
930 err(EX_OSERR, "%s", to_name);
934 * Set owner, group, mode for target; do the chown first,
935 * chown may lose the setuid bits.
937 if (!dounpriv && ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
938 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
939 (mode != (to_sb.st_mode & ALLPERMS)))) {
940 /* Try to turn off the immutable bits. */
941 if (to_sb.st_flags & NOCHANGEBITS)
942 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
946 (gid != (gid_t)-1 && gid != to_sb.st_gid) ||
947 (uid != (uid_t)-1 && uid != to_sb.st_uid))
948 if (fchown(to_fd, uid, gid) == -1) {
950 (void)unlink(to_name);
952 err(EX_OSERR,"%s: chown/chgrp", to_name);
955 if (mode != (to_sb.st_mode & ALLPERMS)) {
957 dounpriv ? mode & (S_IRWXU|S_IRWXG|S_IRWXO) : mode)) {
959 (void)unlink(to_name);
961 err(EX_OSERR, "%s: chmod", to_name);
966 * If provided a set of flags, set them, otherwise, preserve the
967 * flags, except for the dump flag.
968 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
969 * trying to turn off UF_NODUMP. If we're trying to set real flags,
970 * then warn if the fs doesn't support it, otherwise fail.
972 if (!dounpriv & !devnull && (flags & SETFLAGS ||
973 (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
975 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
976 if (flags & SETFLAGS) {
977 if (errno == EOPNOTSUPP)
978 warn("%s: chflags", to_name);
981 (void)unlink(to_name);
983 err(EX_OSERR, "%s: chflags", to_name);
990 (void)close(from_fd);
992 metadata_log(to_name, "file", tvb, NULL, digestresult, to_sb.st_size);
998 * compare two files; non-zero means files differ
1001 compare(int from_fd, const char *from_name __unused, size_t from_len,
1002 int to_fd, const char *to_name __unused, size_t to_len,
1011 if (from_len != to_len)
1014 if (from_len <= MAX_CMP_SIZE) {
1018 if (trymmap(from_fd) && trymmap(to_fd)) {
1019 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
1021 if (p == (char *)MAP_FAILED)
1023 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
1025 if (q == (char *)MAP_FAILED) {
1026 munmap(p, from_len);
1030 rv = memcmp(p, q, from_len);
1032 digest_update(&ctx, p, from_len);
1033 munmap(p, from_len);
1034 munmap(q, from_len);
1038 if (!done_compare) {
1039 char buf1[MAXBSIZE];
1040 char buf2[MAXBSIZE];
1044 lseek(from_fd, 0, SEEK_SET);
1045 lseek(to_fd, 0, SEEK_SET);
1047 n1 = read(from_fd, buf1, sizeof(buf1));
1051 n2 = read(to_fd, buf2, n1);
1053 rv = memcmp(buf1, buf2, n1);
1055 rv = 1; /* out of sync */
1057 rv = 1; /* read failure */
1058 digest_update(&ctx, buf1, n1);
1060 lseek(from_fd, 0, SEEK_SET);
1061 lseek(to_fd, 0, SEEK_SET);
1064 rv = 1; /* don't bother in this case */
1066 if (dresp != NULL) {
1068 *dresp = digest_end(&ctx, NULL);
1070 (void)digest_end(&ctx, NULL);
1077 * create_tempfile --
1078 * create a temporary file based on path and open it
1081 create_tempfile(const char *path, char *temp, size_t tsize)
1085 (void)strncpy(temp, path, tsize);
1086 temp[tsize - 1] = '\0';
1087 if ((p = strrchr(temp, '/')) != NULL)
1091 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
1092 temp[tsize - 1] = '\0';
1093 return (mkstemp(temp));
1098 * create a new file, overwriting an existing one if necessary
1101 create_newfile(const char *path, int target, struct stat *sbp)
1103 char backup[MAXPATHLEN];
1104 int saved_errno = 0;
1109 * Unlink now... avoid ETXTBSY errors later. Try to turn
1110 * off the append/immutable bits -- if we fail, go ahead,
1113 if (sbp->st_flags & NOCHANGEBITS)
1114 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
1117 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
1118 path, suffix) != strlen(path) + strlen(suffix))
1119 errx(EX_OSERR, "%s: backup filename too long",
1121 (void)snprintf(backup, MAXPATHLEN, "%s%s",
1124 (void)printf("install: %s -> %s\n",
1126 if (rename(path, backup) < 0)
1127 err(EX_OSERR, "rename: %s to %s", path, backup);
1129 if (unlink(path) < 0)
1130 saved_errno = errno;
1133 newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1134 if (newfd < 0 && saved_errno != 0)
1135 errno = saved_errno;
1141 * copy from one file to another
1144 copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
1149 char *p, buf[MAXBSIZE];
1153 /* Rewind file descriptors. */
1154 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1155 err(EX_OSERR, "lseek: %s", from_name);
1156 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1157 err(EX_OSERR, "lseek: %s", to_name);
1162 * Mmap and write if less than 8M (the limit is so we don't totally
1163 * trash memory on big files. This is really a minor hack, but it
1164 * wins some CPU back.
1167 if (size <= 8 * 1048576 && trymmap(from_fd) &&
1168 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
1169 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
1170 nw = write(to_fd, p, size);
1173 (void)unlink(to_name);
1176 "short write to %s: %jd bytes written, %jd bytes asked to write",
1177 to_name, (uintmax_t)nw, (uintmax_t)size);
1180 err(EX_OSERR, "%s", to_name);
1183 digest_update(&ctx, p, size);
1184 (void)munmap(p, size);
1188 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
1189 if ((nw = write(to_fd, buf, nr)) != nr) {
1191 (void)unlink(to_name);
1194 "short write to %s: %jd bytes written, %jd bytes asked to write",
1195 to_name, (uintmax_t)nw,
1199 err(EX_OSERR, "%s", to_name);
1202 digest_update(&ctx, buf, nr);
1206 (void)unlink(to_name);
1208 err(EX_OSERR, "%s", from_name);
1211 return (digest_end(&ctx, NULL));
1216 * use strip(1) to strip the target file
1219 strip(const char *to_name)
1221 const char *stripbin;
1227 (void)unlink(to_name);
1229 err(EX_TEMPFAIL, "fork");
1231 stripbin = getenv("STRIPBIN");
1232 if (stripbin == NULL)
1234 execlp(stripbin, stripbin, to_name, (char *)NULL);
1235 err(EX_OSERR, "exec(%s)", stripbin);
1237 if (wait(&status) == -1 || status) {
1239 (void)unlink(to_name);
1240 errc(EX_SOFTWARE, serrno, "wait");
1248 * build directory hierarchy
1251 install_dir(char *path)
1257 for (p = path;; ++p)
1258 if (!*p || (p != path && *p == '/')) {
1261 if (stat(path, &sb)) {
1262 if (errno != ENOENT || mkdir(path, 0755) < 0) {
1263 err(EX_OSERR, "mkdir %s", path);
1266 (void)printf("install: mkdir %s\n",
1268 } else if (!S_ISDIR(sb.st_mode))
1269 errx(EX_OSERR, "%s exists but is not a directory", path);
1275 if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
1276 chown(path, uid, gid))
1277 warn("chown %u:%u %s", uid, gid, path);
1278 /* XXXBED: should we do the chmod in the dounpriv case? */
1279 if (chmod(path, mode))
1280 warn("chmod %o %s", mode, path);
1282 metadata_log(path, "dir", NULL, NULL, NULL, 0);
1287 * if metafp is not NULL, output mtree(8) full path name and settings to
1288 * metafp, to allow permissions to be set correctly by other tools,
1289 * or to allow integrity checks to be performed.
1292 metadata_log(const char *path, const char *type, struct timeval *tv,
1293 const char *slink, const char *digestresult, off_t size)
1295 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
1299 struct flock metalog_lock;
1303 /* Buffer for strsvis(3). */
1304 buf = (char *)malloc(4 * strlen(path) + 1);
1306 warnx("%s", strerror(ENOMEM));
1310 /* Lock log file. */
1311 metalog_lock.l_start = 0;
1312 metalog_lock.l_len = 0;
1313 metalog_lock.l_whence = SEEK_SET;
1314 metalog_lock.l_type = F_WRLCK;
1315 if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
1316 warn("can't lock %s", metafile);
1321 /* Remove destdir. */
1324 destlen = strlen(destdir);
1325 if (strncmp(p, destdir, destlen) == 0 &&
1326 (p[destlen] == '/' || p[destlen] == '\0'))
1329 while (*p && *p == '/')
1331 strsvis(buf, p, VIS_OCTAL, extra);
1333 /* Print details. */
1334 fprintf(metafp, ".%s%s type=%s", *p ? "/" : "", p, type);
1336 fprintf(metafp, " uname=%s", owner);
1338 fprintf(metafp, " gname=%s", group);
1339 fprintf(metafp, " mode=%#o", mode);
1341 strsvis(buf, slink, VIS_CSTYLE, extra); /* encode link */
1342 fprintf(metafp, " link=%s", buf);
1344 if (*type == 'f') /* type=file */
1345 fprintf(metafp, " size=%lld", (long long)size);
1346 if (tv != NULL && dopreserve)
1347 fprintf(metafp, " time=%lld.%ld",
1348 (long long)tv[1].tv_sec, (long)tv[1].tv_usec);
1349 if (digestresult && digest)
1350 fprintf(metafp, " %s=%s", digest, digestresult);
1352 fprintf(metafp, " flags=%s", fflags);
1354 fprintf(metafp, " tags=%s", tags);
1355 fputc('\n', metafp);
1359 /* Unlock log file. */
1360 metalog_lock.l_type = F_UNLCK;
1361 if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1)
1362 warn("can't unlock %s", metafile);
1368 * print a usage message and die
1373 (void)fprintf(stderr,
1374 "usage: install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
1375 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1376 " [-B suffix] [-l linkflags] [-N dbdir]\n"
1378 " install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
1379 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1380 " [-B suffix] [-l linkflags] [-N dbdir]\n"
1381 " file1 ... fileN directory\n"
1382 " install -dU [-vU] [-g group] [-m mode] [-N dbdir] [-o owner]\n"
1383 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1384 " directory ...\n");
1391 * return true (1) if mmap should be tried, false (0) if not.
1397 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
1398 * pre-Lite2-merge systems.
1403 if (fstatfs(fd, &stfs) != 0)
1405 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
1406 strcmp(stfs.f_fstypename, "cd9660") == 0)