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 #define MAX_CMP_SIZE (16 * 1024 * 1024)
77 #define LN_ABSOLUTE 0x01
78 #define LN_RELATIVE 0x02
80 #define LN_SYMBOLIC 0x08
83 #define DIRECTORY 0x01 /* Tell install it's a directory. */
84 #define SETFLAGS 0x02 /* Tell install to set flags. */
85 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
86 #define BACKUP_SUFFIX ".old"
90 RIPEMD160_CTX RIPEMD160;
103 } digesttype = DIGEST_NONE;
107 static int dobackup, docompare, dodir, dolink, dopreserve, dostrip, dounpriv,
109 static int haveopt_f, haveopt_g, haveopt_m, haveopt_o;
110 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
112 static const char *group, *owner;
113 static const char *suffix = BACKUP_SUFFIX;
114 static char *destdir, *digest, *fflags, *metafile, *tags;
116 static int compare(int, const char *, size_t, int, const char *, size_t,
118 static char *copy(int, const char *, int, const char *, off_t);
119 static int create_newfile(const char *, int, struct stat *);
120 static int create_tempfile(const char *, char *, size_t);
121 static char *quiet_mktemp(char *template);
122 static char *digest_file(const char *);
123 static void digest_init(DIGEST_CTX *);
124 static void digest_update(DIGEST_CTX *, const char *, size_t);
125 static char *digest_end(DIGEST_CTX *, char *);
126 static int do_link(const char *, const char *, const struct stat *);
127 static void do_symlink(const char *, const char *, const struct stat *);
128 static void makelink(const char *, const char *, const struct stat *);
129 static void install(const char *, const char *, u_long, u_int);
130 static void install_dir(char *);
131 static void metadata_log(const char *, const char *, struct timeval *,
132 const char *, const char *, off_t);
133 static int parseid(const char *, id_t *);
134 static void strip(const char *);
135 static int trymmap(int);
136 static void usage(void);
139 main(int argc, char *argv[])
141 struct stat from_sb, to_sb;
150 group = owner = NULL;
151 while ((ch = getopt(argc, argv, "B:bCcD:df:g:h:l:M:m:N:o:pSsT:Uv")) !=
164 /* For backwards compatibility. */
184 for (p = optarg; *p != '\0'; p++)
187 dolink &= ~(LN_HARD|LN_MIXED);
188 dolink |= LN_SYMBOLIC;
191 dolink &= ~(LN_SYMBOLIC|LN_MIXED);
195 dolink &= ~(LN_SYMBOLIC|LN_HARD);
199 dolink &= ~LN_RELATIVE;
200 dolink |= LN_ABSOLUTE;
203 dolink &= ~LN_ABSOLUTE;
204 dolink |= LN_RELATIVE;
207 errx(1, "%c: invalid link type", *p);
216 if (!(set = setmode(optarg)))
217 errx(EX_USAGE, "invalid file mode: %s",
219 mode = getmode(set, 0);
223 if (!setup_getid(optarg))
224 err(EX_OSERR, "Unable to use user and group "
225 "databases in `%s'", optarg);
232 docompare = dopreserve = 1;
256 /* some options make no sense when creating directories */
257 if (dostrip && dodir) {
258 warnx("-d and -s may not be specified together");
262 if (getenv("DONTSTRIP") != NULL) {
263 warnx("DONTSTRIP set - will not strip installed binaries");
267 /* must have at least two arguments, except when creating directories */
268 if (argc == 0 || (argc == 1 && !dodir))
271 if (digest != NULL) {
272 if (strcmp(digest, "none") == 0) {
273 digesttype = DIGEST_NONE;
274 } else if (strcmp(digest, "md5") == 0) {
275 digesttype = DIGEST_MD5;
276 } else if (strcmp(digest, "rmd160") == 0) {
277 digesttype = DIGEST_RIPEMD160;
278 } else if (strcmp(digest, "sha1") == 0) {
279 digesttype = DIGEST_SHA1;
280 } else if (strcmp(digest, "sha256") == 0) {
281 digesttype = DIGEST_SHA256;
282 } else if (strcmp(digest, "sha512") == 0) {
283 digesttype = DIGEST_SHA512;
285 warnx("unknown digest `%s'", digest);
290 /* need to make a temp copy so we can compare stripped version */
291 if (docompare && dostrip)
294 /* get group and owner id's */
295 if (group != NULL && !dounpriv) {
296 if (gid_from_group(group, &gid) == -1) {
298 if (!parseid(group, &id))
299 errx(1, "unknown group %s", group);
305 if (owner != NULL && !dounpriv) {
306 if (uid_from_user(owner, &uid) == -1) {
308 if (!parseid(owner, &id))
309 errx(1, "unknown user %s", owner);
315 if (fflags != NULL && !dounpriv) {
316 if (strtofflags(&fflags, &fset, NULL))
317 errx(EX_USAGE, "%s: invalid flag", fflags);
321 if (metafile != NULL) {
322 if ((metafp = fopen(metafile, "a")) == NULL)
323 warn("open %s", metafile);
325 digesttype = DIGEST_NONE;
328 for (; *argv != NULL; ++argv)
334 to_name = argv[argc - 1];
335 no_target = stat(to_name, &to_sb);
336 if (!no_target && S_ISDIR(to_sb.st_mode)) {
337 if (dolink & LN_SYMBOLIC) {
338 if (lstat(to_name, &to_sb) != 0)
339 err(EX_OSERR, "%s vanished", to_name);
340 if (S_ISLNK(to_sb.st_mode)) {
343 err(EX_USAGE, "%s", to_name);
345 install(*argv, to_name, fset, iflags);
349 for (; *argv != to_name; ++argv)
350 install(*argv, to_name, fset, iflags | DIRECTORY);
355 /* can't do file1 file2 directory/file */
358 warnx("target directory `%s' does not exist",
361 warnx("target `%s' is not a directory",
366 if (!no_target && !dolink) {
367 if (stat(*argv, &from_sb))
368 err(EX_OSERR, "%s", *argv);
369 if (!S_ISREG(to_sb.st_mode)) {
371 err(EX_OSERR, "%s", to_name);
373 if (to_sb.st_dev == from_sb.st_dev &&
374 to_sb.st_ino == from_sb.st_ino)
376 "%s and %s are the same file", *argv, to_name);
378 install(*argv, to_name, fset, iflags);
384 digest_file(const char *name)
387 switch (digesttype) {
389 return (MD5File(name, NULL));
390 case DIGEST_RIPEMD160:
391 return (RIPEMD160_File(name, NULL));
393 return (SHA1_File(name, NULL));
395 return (SHA256_File(name, NULL));
397 return (SHA512_File(name, NULL));
404 digest_init(DIGEST_CTX *c)
407 switch (digesttype) {
413 case DIGEST_RIPEMD160:
414 RIPEMD160_Init(&(c->RIPEMD160));
417 SHA1_Init(&(c->SHA1));
420 SHA256_Init(&(c->SHA256));
423 SHA512_Init(&(c->SHA512));
429 digest_update(DIGEST_CTX *c, const char *data, size_t len)
432 switch (digesttype) {
436 MD5Update(&(c->MD5), data, len);
438 case DIGEST_RIPEMD160:
439 RIPEMD160_Update(&(c->RIPEMD160), data, len);
442 SHA1_Update(&(c->SHA1), data, len);
445 SHA256_Update(&(c->SHA256), data, len);
448 SHA512_Update(&(c->SHA512), data, len);
454 digest_end(DIGEST_CTX *c, char *buf)
457 switch (digesttype) {
459 return (MD5End(&(c->MD5), buf));
460 case DIGEST_RIPEMD160:
461 return (RIPEMD160_End(&(c->RIPEMD160), buf));
463 return (SHA1_End(&(c->SHA1), buf));
465 return (SHA256_End(&(c->SHA256), buf));
467 return (SHA512_End(&(c->SHA512), buf));
475 * parse uid or gid from arg into id, returning non-zero if successful
478 parseid(const char *name, id_t *id)
482 *id = (id_t)strtoul(name, &ep, 10);
483 if (errno || *ep != '\0')
490 * mktemp implementation used mkstemp to avoid mktemp warnings. We
491 * really do need mktemp semantics here as we will be creating a link.
494 quiet_mktemp(char *template)
498 if ((fd = mkstemp(template)) == -1)
501 if (unlink(template) == -1)
502 err(EX_OSERR, "unlink %s", template);
508 * make a hard link, obeying dorename if set
509 * return -1 on failure
512 do_link(const char *from_name, const char *to_name,
513 const struct stat *target_sb)
515 char tmpl[MAXPATHLEN];
518 if (safecopy && target_sb != NULL) {
519 (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
520 /* This usage is safe. */
521 if (quiet_mktemp(tmpl) == NULL)
522 err(EX_OSERR, "%s: mktemp", tmpl);
523 ret = link(from_name, tmpl);
525 if (target_sb->st_mode & S_IFDIR && rmdir(to_name) ==
528 err(EX_OSERR, "%s", to_name);
530 if (target_sb->st_flags & NOCHANGEBITS)
531 (void)chflags(to_name, target_sb->st_flags &
534 ret = rename(tmpl, to_name);
536 * If rename has posix semantics, then the temporary
537 * file may still exist when from_name and to_name point
538 * to the same file, so unlink it unconditionally.
544 return (link(from_name, to_name));
549 * Make a symbolic link, obeying dorename if set. Exit on failure.
552 do_symlink(const char *from_name, const char *to_name,
553 const struct stat *target_sb)
555 char tmpl[MAXPATHLEN];
557 if (safecopy && target_sb != NULL) {
558 (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
559 /* This usage is safe. */
560 if (quiet_mktemp(tmpl) == NULL)
561 err(EX_OSERR, "%s: mktemp", tmpl);
563 if (symlink(from_name, tmpl) == -1)
564 err(EX_OSERR, "symlink %s -> %s", from_name, tmpl);
566 if (target_sb->st_mode & S_IFDIR && rmdir(to_name) == -1) {
568 err(EX_OSERR, "%s", to_name);
570 if (target_sb->st_flags & NOCHANGEBITS)
571 (void)chflags(to_name, target_sb->st_flags &
575 if (rename(tmpl, to_name) == -1) {
576 /* Remove temporary link before exiting. */
578 err(EX_OSERR, "%s: rename", to_name);
581 if (symlink(from_name, to_name) == -1)
582 err(EX_OSERR, "symlink %s -> %s", from_name, to_name);
588 * make a link from source to destination
591 makelink(const char *from_name, const char *to_name,
592 const struct stat *target_sb)
594 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
597 /* Try hard links first. */
598 if (dolink & (LN_HARD|LN_MIXED)) {
599 if (do_link(from_name, to_name, target_sb) == -1) {
600 if ((dolink & LN_HARD) || errno != EXDEV)
601 err(EX_OSERR, "link %s -> %s", from_name, to_name);
603 if (stat(to_name, &to_sb))
604 err(EX_OSERR, "%s: stat", to_name);
605 if (S_ISREG(to_sb.st_mode)) {
607 * XXX: hard links to anything other than
608 * plain files are not metalogged
611 const char *oowner, *ogroup;
616 * XXX: use underlying perms, unless
617 * overridden on command line.
621 mode = (to_sb.st_mode & 0777);
631 dres = digest_file(from_name);
632 metadata_log(to_name, "file", NULL, NULL,
633 dres, to_sb.st_size);
644 /* Symbolic links. */
645 if (dolink & LN_ABSOLUTE) {
646 /* Convert source path to absolute. */
647 if (realpath(from_name, src) == NULL)
648 err(EX_OSERR, "%s: realpath", from_name);
649 do_symlink(src, to_name, target_sb);
650 /* XXX: src may point outside of destdir */
651 metadata_log(to_name, "link", NULL, src, NULL, 0);
655 if (dolink & LN_RELATIVE) {
658 /* Resolve pathnames. */
659 if (realpath(from_name, src) == NULL)
660 err(EX_OSERR, "%s: realpath", from_name);
663 * The last component of to_name may be a symlink,
664 * so use realpath to resolve only the directory.
666 cp = dirname(to_name);
667 if (realpath(cp, dst) == NULL)
668 err(EX_OSERR, "%s: realpath", cp);
669 /* .. and add the last component. */
670 if (strcmp(dst, "/") != 0) {
671 if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
672 errx(1, "resolved pathname too long");
674 cp = basename(to_name);
675 if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
676 errx(1, "resolved pathname too long");
678 /* Trim common path components. */
679 for (s = src, d = dst; *s == *d; s++, d++)
684 /* Count the number of directories we need to backtrack. */
685 for (++d, lnk[0] = '\0'; *d; d++)
687 (void)strlcat(lnk, "../", sizeof(lnk));
689 (void)strlcat(lnk, ++s, sizeof(lnk));
691 do_symlink(lnk, to_name, target_sb);
692 /* XXX: Link may point outside of destdir. */
693 metadata_log(to_name, "link", NULL, lnk, NULL, 0);
698 * If absolute or relative was not specified, try the names the
701 do_symlink(from_name, to_name, target_sb);
702 /* XXX: from_name may point outside of destdir. */
703 metadata_log(to_name, "link", NULL, from_name, NULL, 0);
708 * build a path name and install the file
711 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
713 struct stat from_sb, temp_sb, to_sb;
714 struct timeval tvb[2];
715 int devnull, files_match, from_fd, serrno, target;
716 int tempcopy, temp_fd, to_fd;
717 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
724 /* If try to install NULL file to a directory, fails. */
725 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
727 if (stat(from_name, &from_sb))
728 err(EX_OSERR, "%s", from_name);
729 if (!S_ISREG(from_sb.st_mode)) {
731 err(EX_OSERR, "%s", from_name);
734 /* Build the target path. */
735 if (flags & DIRECTORY) {
736 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
738 (p = strrchr(from_name, '/')) ? ++p : from_name);
747 target = (stat(to_name, &to_sb) == 0);
749 target = (lstat(to_name, &to_sb) == 0);
752 if (target && !safecopy) {
753 if (to_sb.st_mode & S_IFDIR && rmdir(to_name) == -1)
754 err(EX_OSERR, "%s", to_name);
755 if (to_sb.st_flags & NOCHANGEBITS)
756 (void)chflags(to_name,
757 to_sb.st_flags & ~NOCHANGEBITS);
760 makelink(from_name, to_name, target ? &to_sb : NULL);
764 /* Only install to regular files. */
765 if (target && !S_ISREG(to_sb.st_mode)) {
771 /* Only copy safe if the target exists. */
772 tempcopy = safecopy && target;
774 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
775 err(EX_OSERR, "%s", from_name);
777 /* If we don't strip, we can compare first. */
778 if (docompare && !dostrip && target) {
779 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
780 err(EX_OSERR, "%s", to_name);
782 files_match = to_sb.st_size == 0;
784 files_match = !(compare(from_fd, from_name,
785 (size_t)from_sb.st_size, to_fd,
786 to_name, (size_t)to_sb.st_size, &digestresult));
788 /* Close "to" file unless we match. */
795 to_fd = create_tempfile(to_name, tempfile,
798 err(EX_OSERR, "%s", tempfile);
800 if ((to_fd = create_newfile(to_name, target,
802 err(EX_OSERR, "%s", to_name);
804 (void)printf("install: %s -> %s\n",
808 digestresult = copy(from_fd, from_name, to_fd,
809 tempcopy ? tempfile : to_name, from_sb.st_size);
815 strip(tempcopy ? tempfile : to_name);
818 * Re-open our fd on the target, in case we used a strip
819 * that does not work in-place -- like GNU binutils strip.
822 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
824 err(EX_OSERR, "stripping %s", to_name);
828 * Compare the stripped temp file with the target.
830 if (docompare && dostrip && target) {
833 /* Re-open to_fd using the real target name. */
834 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
835 err(EX_OSERR, "%s", to_name);
837 if (fstat(temp_fd, &temp_sb)) {
839 (void)unlink(tempfile);
841 err(EX_OSERR, "%s", tempfile);
844 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
845 to_name, (size_t)to_sb.st_size, &digestresult)
848 * If target has more than one link we need to
849 * replace it in order to snap the extra links.
850 * Need to preserve target file times, though.
852 if (to_sb.st_nlink != 1) {
853 tvb[0].tv_sec = to_sb.st_atime;
855 tvb[1].tv_sec = to_sb.st_mtime;
857 (void)utimes(tempfile, tvb);
860 (void)unlink(tempfile);
862 (void) close(temp_fd);
866 if (dostrip && (!docompare || !target))
867 digestresult = digest_file(tempfile);
870 * Move the new file into place if doing a safe copy
871 * and the files are different (or just not compared).
873 if (tempcopy && !files_match) {
874 /* Try to turn off the immutable bits. */
875 if (to_sb.st_flags & NOCHANGEBITS)
876 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
878 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
879 suffix) != strlen(to_name) + strlen(suffix)) {
881 errx(EX_OSERR, "%s: backup filename too long",
885 (void)printf("install: %s -> %s\n", to_name, backup);
886 if (rename(to_name, backup) < 0) {
890 err(EX_OSERR, "rename: %s to %s", to_name,
895 (void)printf("install: %s -> %s\n", from_name, to_name);
896 if (rename(tempfile, to_name) < 0) {
900 err(EX_OSERR, "rename: %s to %s",
904 /* Re-open to_fd so we aren't hosed by the rename(2). */
906 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
907 err(EX_OSERR, "%s", to_name);
911 * Preserve the timestamp of the source file if necessary.
913 if (dopreserve && !files_match && !devnull) {
914 tvb[0].tv_sec = from_sb.st_atime;
916 tvb[1].tv_sec = from_sb.st_mtime;
918 (void)utimes(to_name, tvb);
921 if (fstat(to_fd, &to_sb) == -1) {
923 (void)unlink(to_name);
925 err(EX_OSERR, "%s", to_name);
929 * Set owner, group, mode for target; do the chown first,
930 * chown may lose the setuid bits.
932 if (!dounpriv && ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
933 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
934 (mode != (to_sb.st_mode & ALLPERMS)))) {
935 /* Try to turn off the immutable bits. */
936 if (to_sb.st_flags & NOCHANGEBITS)
937 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
941 (gid != (gid_t)-1 && gid != to_sb.st_gid) ||
942 (uid != (uid_t)-1 && uid != to_sb.st_uid))
943 if (fchown(to_fd, uid, gid) == -1) {
945 (void)unlink(to_name);
947 err(EX_OSERR,"%s: chown/chgrp", to_name);
950 if (mode != (to_sb.st_mode & ALLPERMS)) {
952 dounpriv ? mode & (S_IRWXU|S_IRWXG|S_IRWXO) : mode)) {
954 (void)unlink(to_name);
956 err(EX_OSERR, "%s: chmod", to_name);
961 * If provided a set of flags, set them, otherwise, preserve the
962 * flags, except for the dump flag.
963 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
964 * trying to turn off UF_NODUMP. If we're trying to set real flags,
965 * then warn if the fs doesn't support it, otherwise fail.
967 if (!dounpriv & !devnull && (flags & SETFLAGS ||
968 (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
970 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
971 if (flags & SETFLAGS) {
972 if (errno == EOPNOTSUPP)
973 warn("%s: chflags", to_name);
976 (void)unlink(to_name);
978 err(EX_OSERR, "%s: chflags", to_name);
985 (void)close(from_fd);
987 metadata_log(to_name, "file", tvb, NULL, digestresult, to_sb.st_size);
993 * compare two files; non-zero means files differ
996 compare(int from_fd, const char *from_name __unused, size_t from_len,
997 int to_fd, const char *to_name __unused, size_t to_len,
1006 if (from_len != to_len)
1009 if (from_len <= MAX_CMP_SIZE) {
1013 if (trymmap(from_fd) && trymmap(to_fd)) {
1014 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
1016 if (p == MAP_FAILED)
1018 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
1020 if (q == MAP_FAILED) {
1021 munmap(p, from_len);
1025 rv = memcmp(p, q, from_len);
1027 digest_update(&ctx, p, from_len);
1028 munmap(p, from_len);
1029 munmap(q, from_len);
1033 if (!done_compare) {
1034 char buf1[MAXBSIZE];
1035 char buf2[MAXBSIZE];
1039 lseek(from_fd, 0, SEEK_SET);
1040 lseek(to_fd, 0, SEEK_SET);
1042 n1 = read(from_fd, buf1, sizeof(buf1));
1046 n2 = read(to_fd, buf2, n1);
1048 rv = memcmp(buf1, buf2, n1);
1050 rv = 1; /* out of sync */
1052 rv = 1; /* read failure */
1053 digest_update(&ctx, buf1, n1);
1055 lseek(from_fd, 0, SEEK_SET);
1056 lseek(to_fd, 0, SEEK_SET);
1059 rv = 1; /* don't bother in this case */
1061 if (dresp != NULL) {
1063 *dresp = digest_end(&ctx, NULL);
1065 (void)digest_end(&ctx, NULL);
1072 * create_tempfile --
1073 * create a temporary file based on path and open it
1076 create_tempfile(const char *path, char *temp, size_t tsize)
1080 (void)strncpy(temp, path, tsize);
1081 temp[tsize - 1] = '\0';
1082 if ((p = strrchr(temp, '/')) != NULL)
1086 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
1087 temp[tsize - 1] = '\0';
1088 return (mkstemp(temp));
1093 * create a new file, overwriting an existing one if necessary
1096 create_newfile(const char *path, int target, struct stat *sbp)
1098 char backup[MAXPATHLEN];
1099 int saved_errno = 0;
1104 * Unlink now... avoid ETXTBSY errors later. Try to turn
1105 * off the append/immutable bits -- if we fail, go ahead,
1108 if (sbp->st_flags & NOCHANGEBITS)
1109 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
1112 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
1113 path, suffix) != strlen(path) + strlen(suffix))
1114 errx(EX_OSERR, "%s: backup filename too long",
1116 (void)snprintf(backup, MAXPATHLEN, "%s%s",
1119 (void)printf("install: %s -> %s\n",
1121 if (rename(path, backup) < 0)
1122 err(EX_OSERR, "rename: %s to %s", path, backup);
1124 if (unlink(path) < 0)
1125 saved_errno = errno;
1128 newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1129 if (newfd < 0 && saved_errno != 0)
1130 errno = saved_errno;
1136 * copy from one file to another
1139 copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
1149 /* Rewind file descriptors. */
1150 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1151 err(EX_OSERR, "lseek: %s", from_name);
1152 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1153 err(EX_OSERR, "lseek: %s", to_name);
1158 * Mmap and write if less than 8M (the limit is so we don't totally
1159 * trash memory on big files. This is really a minor hack, but it
1160 * wins some CPU back.
1163 if (size <= 8 * 1048576 && trymmap(from_fd) &&
1164 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
1165 from_fd, (off_t)0)) != MAP_FAILED) {
1166 nw = write(to_fd, p, size);
1169 (void)unlink(to_name);
1172 "short write to %s: %jd bytes written, %jd bytes asked to write",
1173 to_name, (uintmax_t)nw, (uintmax_t)size);
1176 err(EX_OSERR, "%s", to_name);
1179 digest_update(&ctx, p, size);
1180 (void)munmap(p, size);
1184 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
1185 if ((nw = write(to_fd, buf, nr)) != nr) {
1187 (void)unlink(to_name);
1190 "short write to %s: %jd bytes written, %jd bytes asked to write",
1191 to_name, (uintmax_t)nw,
1195 err(EX_OSERR, "%s", to_name);
1198 digest_update(&ctx, buf, nr);
1202 (void)unlink(to_name);
1204 err(EX_OSERR, "%s", from_name);
1207 return (digest_end(&ctx, NULL));
1212 * use strip(1) to strip the target file
1215 strip(const char *to_name)
1217 const char *stripbin;
1223 (void)unlink(to_name);
1225 err(EX_TEMPFAIL, "fork");
1227 stripbin = getenv("STRIPBIN");
1228 if (stripbin == NULL)
1230 execlp(stripbin, stripbin, to_name, (char *)NULL);
1231 err(EX_OSERR, "exec(%s)", stripbin);
1233 if (wait(&status) == -1 || status) {
1235 (void)unlink(to_name);
1236 errc(EX_SOFTWARE, serrno, "wait");
1244 * build directory hierarchy
1247 install_dir(char *path)
1253 for (p = path;; ++p)
1254 if (!*p || (p != path && *p == '/')) {
1257 if (stat(path, &sb)) {
1258 if (errno != ENOENT || mkdir(path, 0755) < 0) {
1259 err(EX_OSERR, "mkdir %s", path);
1262 (void)printf("install: mkdir %s\n",
1264 } else if (!S_ISDIR(sb.st_mode))
1265 errx(EX_OSERR, "%s exists but is not a directory", path);
1271 if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
1272 chown(path, uid, gid))
1273 warn("chown %u:%u %s", uid, gid, path);
1274 /* XXXBED: should we do the chmod in the dounpriv case? */
1275 if (chmod(path, mode))
1276 warn("chmod %o %s", mode, path);
1278 metadata_log(path, "dir", NULL, NULL, NULL, 0);
1283 * if metafp is not NULL, output mtree(8) full path name and settings to
1284 * metafp, to allow permissions to be set correctly by other tools,
1285 * or to allow integrity checks to be performed.
1288 metadata_log(const char *path, const char *type, struct timeval *tv,
1289 const char *slink, const char *digestresult, off_t size)
1291 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
1295 struct flock metalog_lock;
1299 /* Buffer for strsvis(3). */
1300 buf = (char *)malloc(4 * strlen(path) + 1);
1302 warnx("%s", strerror(ENOMEM));
1306 /* Lock log file. */
1307 metalog_lock.l_start = 0;
1308 metalog_lock.l_len = 0;
1309 metalog_lock.l_whence = SEEK_SET;
1310 metalog_lock.l_type = F_WRLCK;
1311 if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
1312 warn("can't lock %s", metafile);
1317 /* Remove destdir. */
1320 destlen = strlen(destdir);
1321 if (strncmp(p, destdir, destlen) == 0 &&
1322 (p[destlen] == '/' || p[destlen] == '\0'))
1325 while (*p && *p == '/')
1327 strsvis(buf, p, VIS_OCTAL, extra);
1329 /* Print details. */
1330 fprintf(metafp, ".%s%s type=%s", *p ? "/" : "", p, type);
1332 fprintf(metafp, " uname=%s", owner);
1334 fprintf(metafp, " gname=%s", group);
1335 fprintf(metafp, " mode=%#o", mode);
1337 strsvis(buf, slink, VIS_CSTYLE, extra); /* encode link */
1338 fprintf(metafp, " link=%s", buf);
1340 if (*type == 'f') /* type=file */
1341 fprintf(metafp, " size=%lld", (long long)size);
1342 if (tv != NULL && dopreserve)
1343 fprintf(metafp, " time=%lld.%ld",
1344 (long long)tv[1].tv_sec, (long)tv[1].tv_usec);
1345 if (digestresult && digest)
1346 fprintf(metafp, " %s=%s", digest, digestresult);
1348 fprintf(metafp, " flags=%s", fflags);
1350 fprintf(metafp, " tags=%s", tags);
1351 fputc('\n', metafp);
1355 /* Unlock log file. */
1356 metalog_lock.l_type = F_UNLCK;
1357 if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1)
1358 warn("can't unlock %s", metafile);
1364 * print a usage message and die
1369 (void)fprintf(stderr,
1370 "usage: install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
1371 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1372 " [-B suffix] [-l linkflags] [-N dbdir]\n"
1374 " 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"
1377 " file1 ... fileN directory\n"
1378 " install -dU [-vU] [-g group] [-m mode] [-N dbdir] [-o owner]\n"
1379 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1380 " directory ...\n");
1387 * return true (1) if mmap should be tried, false (0) if not.
1393 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
1394 * pre-Lite2-merge systems.
1399 if (fstatfs(fd, &stfs) != 0)
1401 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
1402 strcmp(stfs.f_fstypename, "cd9660") == 0)