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>
76 #define MAX_CMP_SIZE (16 * 1024 * 1024)
78 #define LN_ABSOLUTE 0x01
79 #define LN_RELATIVE 0x02
81 #define LN_SYMBOLIC 0x08
84 #define DIRECTORY 0x01 /* Tell install it's a directory. */
85 #define SETFLAGS 0x02 /* Tell install to set flags. */
86 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
87 #define BACKUP_SUFFIX ".old"
91 RIPEMD160_CTX RIPEMD160;
104 } digesttype = DIGEST_NONE;
106 extern char **environ;
110 static int dobackup, docompare, dodir, dolink, dopreserve, dostrip, dounpriv,
112 static int haveopt_f, haveopt_g, haveopt_m, haveopt_o;
113 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
115 static const char *group, *owner;
116 static const char *suffix = BACKUP_SUFFIX;
117 static char *destdir, *digest, *fflags, *metafile, *tags;
119 static int compare(int, const char *, size_t, int, const char *, size_t,
121 static char *copy(int, const char *, int, const char *, off_t);
122 static int create_newfile(const char *, int, struct stat *);
123 static int create_tempfile(const char *, char *, size_t);
124 static char *quiet_mktemp(char *template);
125 static char *digest_file(const char *);
126 static void digest_init(DIGEST_CTX *);
127 static void digest_update(DIGEST_CTX *, const char *, size_t);
128 static char *digest_end(DIGEST_CTX *, char *);
129 static int do_link(const char *, const char *, const struct stat *);
130 static void do_symlink(const char *, const char *, const struct stat *);
131 static void makelink(const char *, const char *, const struct stat *);
132 static void install(const char *, const char *, u_long, u_int);
133 static void install_dir(char *);
134 static void metadata_log(const char *, const char *, struct timeval *,
135 const char *, const char *, off_t);
136 static int parseid(const char *, id_t *);
137 static void strip(const char *);
138 static int trymmap(int);
139 static void usage(void);
142 main(int argc, char *argv[])
144 struct stat from_sb, to_sb;
153 group = owner = NULL;
154 while ((ch = getopt(argc, argv, "B:bCcD:df:g:h:l:M:m:N:o:pSsT:Uv")) !=
167 /* For backwards compatibility. */
187 for (p = optarg; *p != '\0'; p++)
190 dolink &= ~(LN_HARD|LN_MIXED);
191 dolink |= LN_SYMBOLIC;
194 dolink &= ~(LN_SYMBOLIC|LN_MIXED);
198 dolink &= ~(LN_SYMBOLIC|LN_HARD);
202 dolink &= ~LN_RELATIVE;
203 dolink |= LN_ABSOLUTE;
206 dolink &= ~LN_ABSOLUTE;
207 dolink |= LN_RELATIVE;
210 errx(1, "%c: invalid link type", *p);
219 if (!(set = setmode(optarg)))
220 errx(EX_USAGE, "invalid file mode: %s",
222 mode = getmode(set, 0);
226 if (!setup_getid(optarg))
227 err(EX_OSERR, "Unable to use user and group "
228 "databases in `%s'", optarg);
235 docompare = dopreserve = 1;
259 /* some options make no sense when creating directories */
260 if (dostrip && dodir) {
261 warnx("-d and -s may not be specified together");
265 if (getenv("DONTSTRIP") != NULL) {
266 warnx("DONTSTRIP set - will not strip installed binaries");
270 /* must have at least two arguments, except when creating directories */
271 if (argc == 0 || (argc == 1 && !dodir))
274 if (digest != NULL) {
275 if (strcmp(digest, "none") == 0) {
276 digesttype = DIGEST_NONE;
277 } else if (strcmp(digest, "md5") == 0) {
278 digesttype = DIGEST_MD5;
279 } else if (strcmp(digest, "rmd160") == 0) {
280 digesttype = DIGEST_RIPEMD160;
281 } else if (strcmp(digest, "sha1") == 0) {
282 digesttype = DIGEST_SHA1;
283 } else if (strcmp(digest, "sha256") == 0) {
284 digesttype = DIGEST_SHA256;
285 } else if (strcmp(digest, "sha512") == 0) {
286 digesttype = DIGEST_SHA512;
288 warnx("unknown digest `%s'", digest);
293 /* need to make a temp copy so we can compare stripped version */
294 if (docompare && dostrip)
297 /* get group and owner id's */
298 if (group != NULL && !dounpriv) {
299 if (gid_from_group(group, &gid) == -1) {
301 if (!parseid(group, &id))
302 errx(1, "unknown group %s", group);
308 if (owner != NULL && !dounpriv) {
309 if (uid_from_user(owner, &uid) == -1) {
311 if (!parseid(owner, &id))
312 errx(1, "unknown user %s", owner);
318 if (fflags != NULL && !dounpriv) {
319 if (strtofflags(&fflags, &fset, NULL))
320 errx(EX_USAGE, "%s: invalid flag", fflags);
324 if (metafile != NULL) {
325 if ((metafp = fopen(metafile, "a")) == NULL)
326 warn("open %s", metafile);
328 digesttype = DIGEST_NONE;
331 for (; *argv != NULL; ++argv)
337 to_name = argv[argc - 1];
338 no_target = stat(to_name, &to_sb);
339 if (!no_target && S_ISDIR(to_sb.st_mode)) {
340 if (dolink & LN_SYMBOLIC) {
341 if (lstat(to_name, &to_sb) != 0)
342 err(EX_OSERR, "%s vanished", to_name);
343 if (S_ISLNK(to_sb.st_mode)) {
346 err(EX_USAGE, "%s", to_name);
348 install(*argv, to_name, fset, iflags);
352 for (; *argv != to_name; ++argv)
353 install(*argv, to_name, fset, iflags | DIRECTORY);
358 /* can't do file1 file2 directory/file */
361 warnx("target directory `%s' does not exist",
364 warnx("target `%s' is not a directory",
369 if (!no_target && !dolink) {
370 if (stat(*argv, &from_sb))
371 err(EX_OSERR, "%s", *argv);
372 if (!S_ISREG(to_sb.st_mode)) {
374 err(EX_OSERR, "%s", to_name);
376 if (to_sb.st_dev == from_sb.st_dev &&
377 to_sb.st_ino == from_sb.st_ino)
379 "%s and %s are the same file", *argv, to_name);
381 install(*argv, to_name, fset, iflags);
387 digest_file(const char *name)
390 switch (digesttype) {
392 return (MD5File(name, NULL));
393 case DIGEST_RIPEMD160:
394 return (RIPEMD160_File(name, NULL));
396 return (SHA1_File(name, NULL));
398 return (SHA256_File(name, NULL));
400 return (SHA512_File(name, NULL));
407 digest_init(DIGEST_CTX *c)
410 switch (digesttype) {
416 case DIGEST_RIPEMD160:
417 RIPEMD160_Init(&(c->RIPEMD160));
420 SHA1_Init(&(c->SHA1));
423 SHA256_Init(&(c->SHA256));
426 SHA512_Init(&(c->SHA512));
432 digest_update(DIGEST_CTX *c, const char *data, size_t len)
435 switch (digesttype) {
439 MD5Update(&(c->MD5), data, len);
441 case DIGEST_RIPEMD160:
442 RIPEMD160_Update(&(c->RIPEMD160), data, len);
445 SHA1_Update(&(c->SHA1), data, len);
448 SHA256_Update(&(c->SHA256), data, len);
451 SHA512_Update(&(c->SHA512), data, len);
457 digest_end(DIGEST_CTX *c, char *buf)
460 switch (digesttype) {
462 return (MD5End(&(c->MD5), buf));
463 case DIGEST_RIPEMD160:
464 return (RIPEMD160_End(&(c->RIPEMD160), buf));
466 return (SHA1_End(&(c->SHA1), buf));
468 return (SHA256_End(&(c->SHA256), buf));
470 return (SHA512_End(&(c->SHA512), buf));
478 * parse uid or gid from arg into id, returning non-zero if successful
481 parseid(const char *name, id_t *id)
485 *id = (id_t)strtoul(name, &ep, 10);
486 if (errno || *ep != '\0')
493 * mktemp implementation used mkstemp to avoid mktemp warnings. We
494 * really do need mktemp semantics here as we will be creating a link.
497 quiet_mktemp(char *template)
501 if ((fd = mkstemp(template)) == -1)
504 if (unlink(template) == -1)
505 err(EX_OSERR, "unlink %s", template);
511 * make a hard link, obeying dorename if set
512 * return -1 on failure
515 do_link(const char *from_name, const char *to_name,
516 const struct stat *target_sb)
518 char tmpl[MAXPATHLEN];
521 if (safecopy && target_sb != NULL) {
522 (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
523 /* This usage is safe. */
524 if (quiet_mktemp(tmpl) == NULL)
525 err(EX_OSERR, "%s: mktemp", tmpl);
526 ret = link(from_name, tmpl);
528 if (target_sb->st_mode & S_IFDIR && rmdir(to_name) ==
531 err(EX_OSERR, "%s", to_name);
533 if (target_sb->st_flags & NOCHANGEBITS)
534 (void)chflags(to_name, target_sb->st_flags &
537 ret = rename(tmpl, to_name);
539 * If rename has posix semantics, then the temporary
540 * file may still exist when from_name and to_name point
541 * to the same file, so unlink it unconditionally.
547 return (link(from_name, to_name));
552 * Make a symbolic link, obeying dorename if set. Exit on failure.
555 do_symlink(const char *from_name, const char *to_name,
556 const struct stat *target_sb)
558 char tmpl[MAXPATHLEN];
560 if (safecopy && target_sb != NULL) {
561 (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
562 /* This usage is safe. */
563 if (quiet_mktemp(tmpl) == NULL)
564 err(EX_OSERR, "%s: mktemp", tmpl);
566 if (symlink(from_name, tmpl) == -1)
567 err(EX_OSERR, "symlink %s -> %s", from_name, tmpl);
569 if (target_sb->st_mode & S_IFDIR && rmdir(to_name) == -1) {
571 err(EX_OSERR, "%s", to_name);
573 if (target_sb->st_flags & NOCHANGEBITS)
574 (void)chflags(to_name, target_sb->st_flags &
578 if (rename(tmpl, to_name) == -1) {
579 /* Remove temporary link before exiting. */
581 err(EX_OSERR, "%s: rename", to_name);
584 if (symlink(from_name, to_name) == -1)
585 err(EX_OSERR, "symlink %s -> %s", from_name, to_name);
591 * make a link from source to destination
594 makelink(const char *from_name, const char *to_name,
595 const struct stat *target_sb)
597 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
600 /* Try hard links first. */
601 if (dolink & (LN_HARD|LN_MIXED)) {
602 if (do_link(from_name, to_name, target_sb) == -1) {
603 if ((dolink & LN_HARD) || errno != EXDEV)
604 err(EX_OSERR, "link %s -> %s", from_name, to_name);
606 if (stat(to_name, &to_sb))
607 err(EX_OSERR, "%s: stat", to_name);
608 if (S_ISREG(to_sb.st_mode)) {
610 * XXX: hard links to anything other than
611 * plain files are not metalogged
614 const char *oowner, *ogroup;
619 * XXX: use underlying perms, unless
620 * overridden on command line.
624 mode = (to_sb.st_mode & 0777);
634 dres = digest_file(from_name);
635 metadata_log(to_name, "file", NULL, NULL,
636 dres, to_sb.st_size);
647 /* Symbolic links. */
648 if (dolink & LN_ABSOLUTE) {
649 /* Convert source path to absolute. */
650 if (realpath(from_name, src) == NULL)
651 err(EX_OSERR, "%s: realpath", from_name);
652 do_symlink(src, to_name, target_sb);
653 /* XXX: src may point outside of destdir */
654 metadata_log(to_name, "link", NULL, src, NULL, 0);
658 if (dolink & LN_RELATIVE) {
661 /* Resolve pathnames. */
662 if (realpath(from_name, src) == NULL)
663 err(EX_OSERR, "%s: realpath", from_name);
666 * The last component of to_name may be a symlink,
667 * so use realpath to resolve only the directory.
669 cp = dirname(to_name);
670 if (realpath(cp, dst) == NULL)
671 err(EX_OSERR, "%s: realpath", cp);
672 /* .. and add the last component. */
673 if (strcmp(dst, "/") != 0) {
674 if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
675 errx(1, "resolved pathname too long");
677 cp = basename(to_name);
678 if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
679 errx(1, "resolved pathname too long");
681 /* Trim common path components. */
682 for (s = src, d = dst; *s == *d; s++, d++)
687 /* Count the number of directories we need to backtrack. */
688 for (++d, lnk[0] = '\0'; *d; d++)
690 (void)strlcat(lnk, "../", sizeof(lnk));
692 (void)strlcat(lnk, ++s, sizeof(lnk));
694 do_symlink(lnk, to_name, target_sb);
695 /* XXX: Link may point outside of destdir. */
696 metadata_log(to_name, "link", NULL, lnk, NULL, 0);
701 * If absolute or relative was not specified, try the names the
704 do_symlink(from_name, to_name, target_sb);
705 /* XXX: from_name may point outside of destdir. */
706 metadata_log(to_name, "link", NULL, from_name, NULL, 0);
711 * build a path name and install the file
714 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
716 struct stat from_sb, temp_sb, to_sb;
717 struct timeval tvb[2];
718 int devnull, files_match, from_fd, serrno, target;
719 int tempcopy, temp_fd, to_fd;
720 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
727 /* If try to install NULL file to a directory, fails. */
728 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
730 if (stat(from_name, &from_sb))
731 err(EX_OSERR, "%s", from_name);
732 if (!S_ISREG(from_sb.st_mode)) {
734 err(EX_OSERR, "%s", from_name);
737 /* Build the target path. */
738 if (flags & DIRECTORY) {
739 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
741 (p = strrchr(from_name, '/')) ? ++p : from_name);
750 target = (stat(to_name, &to_sb) == 0);
752 target = (lstat(to_name, &to_sb) == 0);
755 if (target && !safecopy) {
756 if (to_sb.st_mode & S_IFDIR && rmdir(to_name) == -1)
757 err(EX_OSERR, "%s", to_name);
758 if (to_sb.st_flags & NOCHANGEBITS)
759 (void)chflags(to_name,
760 to_sb.st_flags & ~NOCHANGEBITS);
763 makelink(from_name, to_name, target ? &to_sb : NULL);
767 /* Only install to regular files. */
768 if (target && !S_ISREG(to_sb.st_mode)) {
774 /* Only copy safe if the target exists. */
775 tempcopy = safecopy && target;
777 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
778 err(EX_OSERR, "%s", from_name);
780 /* If we don't strip, we can compare first. */
781 if (docompare && !dostrip && target) {
782 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
783 err(EX_OSERR, "%s", to_name);
785 files_match = to_sb.st_size == 0;
787 files_match = !(compare(from_fd, from_name,
788 (size_t)from_sb.st_size, to_fd,
789 to_name, (size_t)to_sb.st_size, &digestresult));
791 /* Close "to" file unless we match. */
798 to_fd = create_tempfile(to_name, tempfile,
801 err(EX_OSERR, "%s", tempfile);
803 if ((to_fd = create_newfile(to_name, target,
805 err(EX_OSERR, "%s", to_name);
807 (void)printf("install: %s -> %s\n",
811 digestresult = copy(from_fd, from_name, to_fd,
812 tempcopy ? tempfile : to_name, from_sb.st_size);
818 strip(tempcopy ? tempfile : to_name);
821 * Re-open our fd on the target, in case we used a strip
822 * that does not work in-place -- like GNU binutils strip.
825 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
827 err(EX_OSERR, "stripping %s", to_name);
831 * Compare the stripped temp file with the target.
833 if (docompare && dostrip && target) {
836 /* Re-open to_fd using the real target name. */
837 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
838 err(EX_OSERR, "%s", to_name);
840 if (fstat(temp_fd, &temp_sb)) {
842 (void)unlink(tempfile);
844 err(EX_OSERR, "%s", tempfile);
847 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
848 to_name, (size_t)to_sb.st_size, &digestresult)
851 * If target has more than one link we need to
852 * replace it in order to snap the extra links.
853 * Need to preserve target file times, though.
855 if (to_sb.st_nlink != 1) {
856 tvb[0].tv_sec = to_sb.st_atime;
858 tvb[1].tv_sec = to_sb.st_mtime;
860 (void)utimes(tempfile, tvb);
863 (void)unlink(tempfile);
865 (void) close(temp_fd);
869 if (dostrip && (!docompare || !target))
870 digestresult = digest_file(tempfile);
873 * Move the new file into place if doing a safe copy
874 * and the files are different (or just not compared).
876 if (tempcopy && !files_match) {
877 /* Try to turn off the immutable bits. */
878 if (to_sb.st_flags & NOCHANGEBITS)
879 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
881 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
882 suffix) != strlen(to_name) + strlen(suffix)) {
884 errx(EX_OSERR, "%s: backup filename too long",
888 (void)printf("install: %s -> %s\n", to_name, backup);
889 if (rename(to_name, backup) < 0) {
893 err(EX_OSERR, "rename: %s to %s", to_name,
898 (void)printf("install: %s -> %s\n", from_name, to_name);
899 if (rename(tempfile, to_name) < 0) {
903 err(EX_OSERR, "rename: %s to %s",
907 /* Re-open to_fd so we aren't hosed by the rename(2). */
909 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
910 err(EX_OSERR, "%s", to_name);
914 * Preserve the timestamp of the source file if necessary.
916 if (dopreserve && !files_match && !devnull) {
917 tvb[0].tv_sec = from_sb.st_atime;
919 tvb[1].tv_sec = from_sb.st_mtime;
921 (void)utimes(to_name, tvb);
924 if (fstat(to_fd, &to_sb) == -1) {
926 (void)unlink(to_name);
928 err(EX_OSERR, "%s", to_name);
932 * Set owner, group, mode for target; do the chown first,
933 * chown may lose the setuid bits.
935 if (!dounpriv && ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
936 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
937 (mode != (to_sb.st_mode & ALLPERMS)))) {
938 /* Try to turn off the immutable bits. */
939 if (to_sb.st_flags & NOCHANGEBITS)
940 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
944 (gid != (gid_t)-1 && gid != to_sb.st_gid) ||
945 (uid != (uid_t)-1 && uid != to_sb.st_uid))
946 if (fchown(to_fd, uid, gid) == -1) {
948 (void)unlink(to_name);
950 err(EX_OSERR,"%s: chown/chgrp", to_name);
953 if (mode != (to_sb.st_mode & ALLPERMS)) {
955 dounpriv ? mode & (S_IRWXU|S_IRWXG|S_IRWXO) : mode)) {
957 (void)unlink(to_name);
959 err(EX_OSERR, "%s: chmod", to_name);
964 * If provided a set of flags, set them, otherwise, preserve the
965 * flags, except for the dump flag.
966 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
967 * trying to turn off UF_NODUMP. If we're trying to set real flags,
968 * then warn if the fs doesn't support it, otherwise fail.
970 if (!dounpriv & !devnull && (flags & SETFLAGS ||
971 (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
973 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
974 if (flags & SETFLAGS) {
975 if (errno == EOPNOTSUPP)
976 warn("%s: chflags", to_name);
979 (void)unlink(to_name);
981 err(EX_OSERR, "%s: chflags", to_name);
988 (void)close(from_fd);
990 metadata_log(to_name, "file", tvb, NULL, digestresult, to_sb.st_size);
996 * compare two files; non-zero means files differ
999 compare(int from_fd, const char *from_name __unused, size_t from_len,
1000 int to_fd, const char *to_name __unused, size_t to_len,
1009 if (from_len != to_len)
1012 if (from_len <= MAX_CMP_SIZE) {
1016 if (trymmap(from_fd) && trymmap(to_fd)) {
1017 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
1019 if (p == MAP_FAILED)
1021 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
1023 if (q == MAP_FAILED) {
1024 munmap(p, from_len);
1028 rv = memcmp(p, q, from_len);
1030 digest_update(&ctx, p, from_len);
1031 munmap(p, from_len);
1032 munmap(q, from_len);
1036 if (!done_compare) {
1037 char buf1[MAXBSIZE];
1038 char buf2[MAXBSIZE];
1042 lseek(from_fd, 0, SEEK_SET);
1043 lseek(to_fd, 0, SEEK_SET);
1045 n1 = read(from_fd, buf1, sizeof(buf1));
1049 n2 = read(to_fd, buf2, n1);
1051 rv = memcmp(buf1, buf2, n1);
1053 rv = 1; /* out of sync */
1055 rv = 1; /* read failure */
1056 digest_update(&ctx, buf1, n1);
1058 lseek(from_fd, 0, SEEK_SET);
1059 lseek(to_fd, 0, SEEK_SET);
1062 rv = 1; /* don't bother in this case */
1064 if (dresp != NULL) {
1066 *dresp = digest_end(&ctx, NULL);
1068 (void)digest_end(&ctx, NULL);
1075 * create_tempfile --
1076 * create a temporary file based on path and open it
1079 create_tempfile(const char *path, char *temp, size_t tsize)
1083 (void)strncpy(temp, path, tsize);
1084 temp[tsize - 1] = '\0';
1085 if ((p = strrchr(temp, '/')) != NULL)
1089 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
1090 temp[tsize - 1] = '\0';
1091 return (mkstemp(temp));
1096 * create a new file, overwriting an existing one if necessary
1099 create_newfile(const char *path, int target, struct stat *sbp)
1101 char backup[MAXPATHLEN];
1102 int saved_errno = 0;
1107 * Unlink now... avoid ETXTBSY errors later. Try to turn
1108 * off the append/immutable bits -- if we fail, go ahead,
1111 if (sbp->st_flags & NOCHANGEBITS)
1112 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
1115 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
1116 path, suffix) != strlen(path) + strlen(suffix))
1117 errx(EX_OSERR, "%s: backup filename too long",
1119 (void)snprintf(backup, MAXPATHLEN, "%s%s",
1122 (void)printf("install: %s -> %s\n",
1124 if (rename(path, backup) < 0)
1125 err(EX_OSERR, "rename: %s to %s", path, backup);
1127 if (unlink(path) < 0)
1128 saved_errno = errno;
1131 newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1132 if (newfd < 0 && saved_errno != 0)
1133 errno = saved_errno;
1139 * copy from one file to another
1142 copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
1152 /* Rewind file descriptors. */
1153 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1154 err(EX_OSERR, "lseek: %s", from_name);
1155 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1156 err(EX_OSERR, "lseek: %s", to_name);
1161 * Mmap and write if less than 8M (the limit is so we don't totally
1162 * trash memory on big files. This is really a minor hack, but it
1163 * wins some CPU back.
1166 if (size <= 8 * 1048576 && trymmap(from_fd) &&
1167 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
1168 from_fd, (off_t)0)) != MAP_FAILED) {
1169 nw = write(to_fd, p, size);
1172 (void)unlink(to_name);
1175 "short write to %s: %jd bytes written, %jd bytes asked to write",
1176 to_name, (uintmax_t)nw, (uintmax_t)size);
1179 err(EX_OSERR, "%s", to_name);
1182 digest_update(&ctx, p, size);
1183 (void)munmap(p, size);
1187 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
1188 if ((nw = write(to_fd, buf, nr)) != nr) {
1190 (void)unlink(to_name);
1193 "short write to %s: %jd bytes written, %jd bytes asked to write",
1194 to_name, (uintmax_t)nw,
1198 err(EX_OSERR, "%s", to_name);
1201 digest_update(&ctx, buf, nr);
1205 (void)unlink(to_name);
1207 err(EX_OSERR, "%s", from_name);
1210 return (digest_end(&ctx, NULL));
1215 * use strip(1) to strip the target file
1218 strip(const char *to_name)
1220 const char *stripbin;
1221 const char *args[3];
1225 stripbin = getenv("STRIPBIN");
1226 if (stripbin == NULL)
1231 error = posix_spawnp(&pid, stripbin, NULL, NULL,
1232 __DECONST(char **, args), environ);
1234 (void)unlink(to_name);
1235 errc(error == EAGAIN || error == EPROCLIM || error == ENOMEM ?
1236 EX_TEMPFAIL : EX_OSERR, error, "spawn %s", stripbin);
1238 if (waitpid(pid, &status, 0) == -1) {
1240 (void)unlink(to_name);
1241 errc(EX_SOFTWARE, error, "wait");
1245 (void)unlink(to_name);
1246 errx(EX_SOFTWARE, "strip command %s failed on %s",
1253 * build directory hierarchy
1256 install_dir(char *path)
1262 for (p = path;; ++p)
1263 if (!*p || (p != path && *p == '/')) {
1266 if (stat(path, &sb)) {
1267 if (errno != ENOENT || mkdir(path, 0755) < 0) {
1268 err(EX_OSERR, "mkdir %s", path);
1271 (void)printf("install: mkdir %s\n",
1273 } else if (!S_ISDIR(sb.st_mode))
1274 errx(EX_OSERR, "%s exists but is not a directory", path);
1280 if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
1281 chown(path, uid, gid))
1282 warn("chown %u:%u %s", uid, gid, path);
1283 /* XXXBED: should we do the chmod in the dounpriv case? */
1284 if (chmod(path, mode))
1285 warn("chmod %o %s", mode, path);
1287 metadata_log(path, "dir", NULL, NULL, NULL, 0);
1292 * if metafp is not NULL, output mtree(8) full path name and settings to
1293 * metafp, to allow permissions to be set correctly by other tools,
1294 * or to allow integrity checks to be performed.
1297 metadata_log(const char *path, const char *type, struct timeval *tv,
1298 const char *slink, const char *digestresult, off_t size)
1300 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
1304 struct flock metalog_lock;
1308 /* Buffer for strsvis(3). */
1309 buf = (char *)malloc(4 * strlen(path) + 1);
1311 warnx("%s", strerror(ENOMEM));
1315 /* Lock log file. */
1316 metalog_lock.l_start = 0;
1317 metalog_lock.l_len = 0;
1318 metalog_lock.l_whence = SEEK_SET;
1319 metalog_lock.l_type = F_WRLCK;
1320 if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
1321 warn("can't lock %s", metafile);
1326 /* Remove destdir. */
1329 destlen = strlen(destdir);
1330 if (strncmp(p, destdir, destlen) == 0 &&
1331 (p[destlen] == '/' || p[destlen] == '\0'))
1334 while (*p && *p == '/')
1336 strsvis(buf, p, VIS_OCTAL, extra);
1338 /* Print details. */
1339 fprintf(metafp, ".%s%s type=%s", *p ? "/" : "", p, type);
1341 fprintf(metafp, " uname=%s", owner);
1343 fprintf(metafp, " gname=%s", group);
1344 fprintf(metafp, " mode=%#o", mode);
1346 strsvis(buf, slink, VIS_CSTYLE, extra); /* encode link */
1347 fprintf(metafp, " link=%s", buf);
1349 if (*type == 'f') /* type=file */
1350 fprintf(metafp, " size=%lld", (long long)size);
1351 if (tv != NULL && dopreserve)
1352 fprintf(metafp, " time=%lld.%ld",
1353 (long long)tv[1].tv_sec, (long)tv[1].tv_usec);
1354 if (digestresult && digest)
1355 fprintf(metafp, " %s=%s", digest, digestresult);
1357 fprintf(metafp, " flags=%s", fflags);
1359 fprintf(metafp, " tags=%s", tags);
1360 fputc('\n', metafp);
1364 /* Unlock log file. */
1365 metalog_lock.l_type = F_UNLCK;
1366 if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1)
1367 warn("can't unlock %s", metafile);
1373 * print a usage message and die
1378 (void)fprintf(stderr,
1379 "usage: install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
1380 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1381 " [-B suffix] [-l linkflags] [-N dbdir]\n"
1383 " install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
1384 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1385 " [-B suffix] [-l linkflags] [-N dbdir]\n"
1386 " file1 ... fileN directory\n"
1387 " install -dU [-vU] [-g group] [-m mode] [-N dbdir] [-o owner]\n"
1388 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1389 " directory ...\n");
1396 * return true (1) if mmap should be tried, false (0) if not.
1402 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
1403 * pre-Lite2-merge systems.
1408 if (fstatfs(fd, &stfs) != 0)
1410 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
1411 strcmp(stfs.f_fstypename, "cd9660") == 0)