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 * 3. 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 timespec *,
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;
154 group = owner = NULL;
155 while ((ch = getopt(argc, argv, "B:bCcD:df:g:h:l:M:m:N:o:pSsT:Uv")) !=
168 /* For backwards compatibility. */
188 for (p = optarg; *p != '\0'; p++)
191 dolink &= ~(LN_HARD|LN_MIXED);
192 dolink |= LN_SYMBOLIC;
195 dolink &= ~(LN_SYMBOLIC|LN_MIXED);
199 dolink &= ~(LN_SYMBOLIC|LN_HARD);
203 dolink &= ~LN_RELATIVE;
204 dolink |= LN_ABSOLUTE;
207 dolink &= ~LN_ABSOLUTE;
208 dolink |= LN_RELATIVE;
211 errx(1, "%c: invalid link type", *p);
220 if (!(set = setmode(optarg)))
221 errx(EX_USAGE, "invalid file mode: %s",
223 mode = getmode(set, 0);
227 if (!setup_getid(optarg))
228 err(EX_OSERR, "Unable to use user and group "
229 "databases in `%s'", optarg);
236 docompare = dopreserve = 1;
260 /* some options make no sense when creating directories */
261 if (dostrip && dodir) {
262 warnx("-d and -s may not be specified together");
266 if (getenv("DONTSTRIP") != NULL) {
267 warnx("DONTSTRIP set - will not strip installed binaries");
271 /* must have at least two arguments, except when creating directories */
272 if (argc == 0 || (argc == 1 && !dodir))
275 if (digest != NULL) {
276 if (strcmp(digest, "none") == 0) {
277 digesttype = DIGEST_NONE;
278 } else if (strcmp(digest, "md5") == 0) {
279 digesttype = DIGEST_MD5;
280 } else if (strcmp(digest, "rmd160") == 0) {
281 digesttype = DIGEST_RIPEMD160;
282 } else if (strcmp(digest, "sha1") == 0) {
283 digesttype = DIGEST_SHA1;
284 } else if (strcmp(digest, "sha256") == 0) {
285 digesttype = DIGEST_SHA256;
286 } else if (strcmp(digest, "sha512") == 0) {
287 digesttype = DIGEST_SHA512;
289 warnx("unknown digest `%s'", digest);
294 /* need to make a temp copy so we can compare stripped version */
295 if (docompare && dostrip)
298 /* get group and owner id's */
299 if (group != NULL && !dounpriv) {
300 if (gid_from_group(group, &gid) == -1) {
302 if (!parseid(group, &id))
303 errx(1, "unknown group %s", group);
309 if (owner != NULL && !dounpriv) {
310 if (uid_from_user(owner, &uid) == -1) {
312 if (!parseid(owner, &id))
313 errx(1, "unknown user %s", owner);
319 if (fflags != NULL && !dounpriv) {
320 if (strtofflags(&fflags, &fset, NULL))
321 errx(EX_USAGE, "%s: invalid flag", fflags);
325 if (metafile != NULL) {
326 if ((metafp = fopen(metafile, "a")) == NULL)
327 warn("open %s", metafile);
329 digesttype = DIGEST_NONE;
332 for (; *argv != NULL; ++argv)
338 to_name = argv[argc - 1];
339 no_target = stat(to_name, &to_sb);
340 if (!no_target && S_ISDIR(to_sb.st_mode)) {
341 if (dolink & LN_SYMBOLIC) {
342 if (lstat(to_name, &to_sb) != 0)
343 err(EX_OSERR, "%s vanished", to_name);
344 if (S_ISLNK(to_sb.st_mode)) {
347 err(EX_USAGE, "%s", to_name);
349 install(*argv, to_name, fset, iflags);
353 for (; *argv != to_name; ++argv)
354 install(*argv, to_name, fset, iflags | DIRECTORY);
359 /* can't do file1 file2 directory/file */
362 warnx("target directory `%s' does not exist",
365 warnx("target `%s' is not a directory",
370 if (!no_target && !dolink) {
371 if (stat(*argv, &from_sb))
372 err(EX_OSERR, "%s", *argv);
373 if (!S_ISREG(to_sb.st_mode)) {
375 err(EX_OSERR, "%s", to_name);
377 if (to_sb.st_dev == from_sb.st_dev &&
378 to_sb.st_ino == from_sb.st_ino)
380 "%s and %s are the same file", *argv, to_name);
382 install(*argv, to_name, fset, iflags);
388 digest_file(const char *name)
391 switch (digesttype) {
393 return (MD5File(name, NULL));
394 case DIGEST_RIPEMD160:
395 return (RIPEMD160_File(name, NULL));
397 return (SHA1_File(name, NULL));
399 return (SHA256_File(name, NULL));
401 return (SHA512_File(name, NULL));
408 digest_init(DIGEST_CTX *c)
411 switch (digesttype) {
417 case DIGEST_RIPEMD160:
418 RIPEMD160_Init(&(c->RIPEMD160));
421 SHA1_Init(&(c->SHA1));
424 SHA256_Init(&(c->SHA256));
427 SHA512_Init(&(c->SHA512));
433 digest_update(DIGEST_CTX *c, const char *data, size_t len)
436 switch (digesttype) {
440 MD5Update(&(c->MD5), data, len);
442 case DIGEST_RIPEMD160:
443 RIPEMD160_Update(&(c->RIPEMD160), data, len);
446 SHA1_Update(&(c->SHA1), data, len);
449 SHA256_Update(&(c->SHA256), data, len);
452 SHA512_Update(&(c->SHA512), data, len);
458 digest_end(DIGEST_CTX *c, char *buf)
461 switch (digesttype) {
463 return (MD5End(&(c->MD5), buf));
464 case DIGEST_RIPEMD160:
465 return (RIPEMD160_End(&(c->RIPEMD160), buf));
467 return (SHA1_End(&(c->SHA1), buf));
469 return (SHA256_End(&(c->SHA256), buf));
471 return (SHA512_End(&(c->SHA512), buf));
479 * parse uid or gid from arg into id, returning non-zero if successful
482 parseid(const char *name, id_t *id)
486 *id = (id_t)strtoul(name, &ep, 10);
487 if (errno || *ep != '\0')
494 * mktemp implementation used mkstemp to avoid mktemp warnings. We
495 * really do need mktemp semantics here as we will be creating a link.
498 quiet_mktemp(char *template)
502 if ((fd = mkstemp(template)) == -1)
505 if (unlink(template) == -1)
506 err(EX_OSERR, "unlink %s", template);
512 * make a hard link, obeying dorename if set
513 * return -1 on failure
516 do_link(const char *from_name, const char *to_name,
517 const struct stat *target_sb)
519 char tmpl[MAXPATHLEN];
522 if (safecopy && target_sb != NULL) {
523 (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
524 /* This usage is safe. */
525 if (quiet_mktemp(tmpl) == NULL)
526 err(EX_OSERR, "%s: mktemp", tmpl);
527 ret = link(from_name, tmpl);
529 if (target_sb->st_mode & S_IFDIR && rmdir(to_name) ==
532 err(EX_OSERR, "%s", to_name);
534 if (target_sb->st_flags & NOCHANGEBITS)
535 (void)chflags(to_name, target_sb->st_flags &
538 printf("install: link %s -> %s\n",
540 ret = rename(tmpl, to_name);
542 * If rename has posix semantics, then the temporary
543 * file may still exist when from_name and to_name point
544 * to the same file, so unlink it unconditionally.
551 printf("install: link %s -> %s\n",
553 return (link(from_name, to_name));
559 * Make a symbolic link, obeying dorename if set. Exit on failure.
562 do_symlink(const char *from_name, const char *to_name,
563 const struct stat *target_sb)
565 char tmpl[MAXPATHLEN];
567 if (safecopy && target_sb != NULL) {
568 (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
569 /* This usage is safe. */
570 if (quiet_mktemp(tmpl) == NULL)
571 err(EX_OSERR, "%s: mktemp", tmpl);
573 if (symlink(from_name, tmpl) == -1)
574 err(EX_OSERR, "symlink %s -> %s", from_name, tmpl);
576 if (target_sb->st_mode & S_IFDIR && rmdir(to_name) == -1) {
578 err(EX_OSERR, "%s", to_name);
580 if (target_sb->st_flags & NOCHANGEBITS)
581 (void)chflags(to_name, target_sb->st_flags &
584 printf("install: symlink %s -> %s\n",
586 if (rename(tmpl, to_name) == -1) {
587 /* Remove temporary link before exiting. */
589 err(EX_OSERR, "%s: rename", to_name);
593 printf("install: symlink %s -> %s\n",
595 if (symlink(from_name, to_name) == -1)
596 err(EX_OSERR, "symlink %s -> %s", from_name, to_name);
602 * make a link from source to destination
605 makelink(const char *from_name, const char *to_name,
606 const struct stat *target_sb)
608 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
611 /* Try hard links first. */
612 if (dolink & (LN_HARD|LN_MIXED)) {
613 if (do_link(from_name, to_name, target_sb) == -1) {
614 if ((dolink & LN_HARD) || errno != EXDEV)
615 err(EX_OSERR, "link %s -> %s", from_name, to_name);
617 if (stat(to_name, &to_sb))
618 err(EX_OSERR, "%s: stat", to_name);
619 if (S_ISREG(to_sb.st_mode)) {
621 * XXX: hard links to anything other than
622 * plain files are not metalogged
625 const char *oowner, *ogroup;
630 * XXX: use underlying perms, unless
631 * overridden on command line.
635 mode = (to_sb.st_mode & 0777);
645 dres = digest_file(from_name);
646 metadata_log(to_name, "file", NULL, NULL,
647 dres, to_sb.st_size);
658 /* Symbolic links. */
659 if (dolink & LN_ABSOLUTE) {
660 /* Convert source path to absolute. */
661 if (realpath(from_name, src) == NULL)
662 err(EX_OSERR, "%s: realpath", from_name);
663 do_symlink(src, to_name, target_sb);
664 /* XXX: src may point outside of destdir */
665 metadata_log(to_name, "link", NULL, src, NULL, 0);
669 if (dolink & LN_RELATIVE) {
670 char *to_name_copy, *cp, *d, *ld, *ls, *s;
672 if (*from_name != '/') {
673 /* this is already a relative link */
674 do_symlink(from_name, to_name, target_sb);
675 /* XXX: from_name may point outside of destdir. */
676 metadata_log(to_name, "link", NULL, from_name, NULL, 0);
680 /* Resolve pathnames. */
681 if (realpath(from_name, src) == NULL)
682 err(EX_OSERR, "%s: realpath", from_name);
685 * The last component of to_name may be a symlink,
686 * so use realpath to resolve only the directory.
688 to_name_copy = strdup(to_name);
689 if (to_name_copy == NULL)
690 err(EX_OSERR, "%s: strdup", to_name);
691 cp = dirname(to_name_copy);
692 if (realpath(cp, dst) == NULL)
693 err(EX_OSERR, "%s: realpath", cp);
694 /* .. and add the last component. */
695 if (strcmp(dst, "/") != 0) {
696 if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
697 errx(1, "resolved pathname too long");
699 strcpy(to_name_copy, to_name);
700 cp = basename(to_name_copy);
701 if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
702 errx(1, "resolved pathname too long");
705 /* Trim common path components. */
707 for (s = src, d = dst; *s == *d; ls = s, ld = d, s++, d++)
710 * If we didn't end after a directory separator, then we've
711 * falsely matched the last component. For example, if one
712 * invoked install -lrs /lib/foo.so /libexec/ then the source
713 * would terminate just after the separator while the
714 * destination would terminate in the middle of 'libexec',
715 * leading to a full directory getting falsely eaten.
717 if ((ls != NULL && *ls != '/') || (ld != NULL && *ld != '/'))
722 /* Count the number of directories we need to backtrack. */
723 for (++d, lnk[0] = '\0'; *d; d++)
725 (void)strlcat(lnk, "../", sizeof(lnk));
727 (void)strlcat(lnk, ++s, sizeof(lnk));
729 do_symlink(lnk, to_name, target_sb);
730 /* XXX: Link may point outside of destdir. */
731 metadata_log(to_name, "link", NULL, lnk, NULL, 0);
736 * If absolute or relative was not specified, try the names the
739 do_symlink(from_name, to_name, target_sb);
740 /* XXX: from_name may point outside of destdir. */
741 metadata_log(to_name, "link", NULL, from_name, NULL, 0);
746 * build a path name and install the file
749 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
751 struct stat from_sb, temp_sb, to_sb;
752 struct timespec tsb[2];
753 int devnull, files_match, from_fd, serrno, target;
754 int tempcopy, temp_fd, to_fd;
755 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
762 /* If try to install NULL file to a directory, fails. */
763 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
765 if (stat(from_name, &from_sb))
766 err(EX_OSERR, "%s", from_name);
767 if (!S_ISREG(from_sb.st_mode)) {
769 err(EX_OSERR, "%s", from_name);
772 /* Build the target path. */
773 if (flags & DIRECTORY) {
774 (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s%s",
776 to_name[strlen(to_name) - 1] == '/' ? "" : "/",
777 (p = strrchr(from_name, '/')) ? ++p : from_name);
785 target = (lstat(to_name, &to_sb) == 0);
788 if (target && !safecopy) {
789 if (to_sb.st_mode & S_IFDIR && rmdir(to_name) == -1)
790 err(EX_OSERR, "%s", to_name);
791 if (to_sb.st_flags & NOCHANGEBITS)
792 (void)chflags(to_name,
793 to_sb.st_flags & ~NOCHANGEBITS);
796 makelink(from_name, to_name, target ? &to_sb : NULL);
800 if (target && !S_ISREG(to_sb.st_mode) && !S_ISLNK(to_sb.st_mode)) {
806 /* Only copy safe if the target exists. */
807 tempcopy = safecopy && target;
809 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
810 err(EX_OSERR, "%s", from_name);
812 /* If we don't strip, we can compare first. */
813 if (docompare && !dostrip && target && S_ISREG(to_sb.st_mode)) {
814 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
815 err(EX_OSERR, "%s", to_name);
817 files_match = to_sb.st_size == 0;
819 files_match = !(compare(from_fd, from_name,
820 (size_t)from_sb.st_size, to_fd,
821 to_name, (size_t)to_sb.st_size, &digestresult));
823 /* Close "to" file unless we match. */
830 to_fd = create_tempfile(to_name, tempfile,
833 err(EX_OSERR, "%s", tempfile);
835 if ((to_fd = create_newfile(to_name, target,
837 err(EX_OSERR, "%s", to_name);
839 (void)printf("install: %s -> %s\n",
843 digestresult = copy(from_fd, from_name, to_fd,
844 tempcopy ? tempfile : to_name, from_sb.st_size);
850 strip(tempcopy ? tempfile : to_name);
853 * Re-open our fd on the target, in case we used a strip
854 * that does not work in-place -- like GNU binutils strip.
857 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
859 err(EX_OSERR, "stripping %s", to_name);
863 * Compare the stripped temp file with the target.
865 if (docompare && dostrip && target && S_ISREG(to_sb.st_mode)) {
868 /* Re-open to_fd using the real target name. */
869 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
870 err(EX_OSERR, "%s", to_name);
872 if (fstat(temp_fd, &temp_sb)) {
874 (void)unlink(tempfile);
876 err(EX_OSERR, "%s", tempfile);
879 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
880 to_name, (size_t)to_sb.st_size, &digestresult)
883 * If target has more than one link we need to
884 * replace it in order to snap the extra links.
885 * Need to preserve target file times, though.
887 if (to_sb.st_nlink != 1) {
888 tsb[0] = to_sb.st_atim;
889 tsb[1] = to_sb.st_mtim;
890 (void)utimensat(AT_FDCWD, tempfile, tsb, 0);
893 (void)unlink(tempfile);
895 (void) close(temp_fd);
898 digestresult = digest_file(tempfile);
901 * Move the new file into place if doing a safe copy
902 * and the files are different (or just not compared).
904 if (tempcopy && !files_match) {
905 /* Try to turn off the immutable bits. */
906 if (to_sb.st_flags & NOCHANGEBITS)
907 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
909 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
910 suffix) != strlen(to_name) + strlen(suffix)) {
912 errx(EX_OSERR, "%s: backup filename too long",
916 (void)printf("install: %s -> %s\n", to_name, backup);
917 if (unlink(backup) < 0 && errno != ENOENT) {
919 if (to_sb.st_flags & NOCHANGEBITS)
920 (void)chflags(to_name, to_sb.st_flags);
923 err(EX_OSERR, "unlink: %s", backup);
925 if (link(to_name, backup) < 0) {
928 if (to_sb.st_flags & NOCHANGEBITS)
929 (void)chflags(to_name, to_sb.st_flags);
931 err(EX_OSERR, "link: %s to %s", to_name,
936 (void)printf("install: %s -> %s\n", from_name, to_name);
937 if (rename(tempfile, to_name) < 0) {
941 err(EX_OSERR, "rename: %s to %s",
945 /* Re-open to_fd so we aren't hosed by the rename(2). */
947 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
948 err(EX_OSERR, "%s", to_name);
952 * Preserve the timestamp of the source file if necessary.
954 if (dopreserve && !files_match && !devnull) {
955 tsb[0] = from_sb.st_atim;
956 tsb[1] = from_sb.st_mtim;
957 (void)utimensat(AT_FDCWD, to_name, tsb, 0);
960 if (fstat(to_fd, &to_sb) == -1) {
962 (void)unlink(to_name);
964 err(EX_OSERR, "%s", to_name);
968 * Set owner, group, mode for target; do the chown first,
969 * chown may lose the setuid bits.
971 if (!dounpriv && ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
972 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
973 (mode != (to_sb.st_mode & ALLPERMS)))) {
974 /* Try to turn off the immutable bits. */
975 if (to_sb.st_flags & NOCHANGEBITS)
976 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
980 (gid != (gid_t)-1 && gid != to_sb.st_gid) ||
981 (uid != (uid_t)-1 && uid != to_sb.st_uid))
982 if (fchown(to_fd, uid, gid) == -1) {
984 (void)unlink(to_name);
986 err(EX_OSERR,"%s: chown/chgrp", to_name);
989 if (mode != (to_sb.st_mode & ALLPERMS)) {
991 dounpriv ? mode & (S_IRWXU|S_IRWXG|S_IRWXO) : mode)) {
993 (void)unlink(to_name);
995 err(EX_OSERR, "%s: chmod", to_name);
1000 * If provided a set of flags, set them, otherwise, preserve the
1001 * flags, except for the dump flag.
1002 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
1003 * trying to turn off UF_NODUMP. If we're trying to set real flags,
1004 * then warn if the fs doesn't support it, otherwise fail.
1006 if (!dounpriv & !devnull && (flags & SETFLAGS ||
1007 (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
1009 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
1010 if (flags & SETFLAGS) {
1011 if (errno == EOPNOTSUPP)
1012 warn("%s: chflags", to_name);
1015 (void)unlink(to_name);
1017 err(EX_OSERR, "%s: chflags", to_name);
1024 (void)close(from_fd);
1026 metadata_log(to_name, "file", tsb, NULL, digestresult, to_sb.st_size);
1032 * compare two files; non-zero means files differ
1035 compare(int from_fd, const char *from_name __unused, size_t from_len,
1036 int to_fd, const char *to_name __unused, size_t to_len,
1045 if (from_len != to_len)
1048 if (from_len <= MAX_CMP_SIZE) {
1052 if (trymmap(from_fd) && trymmap(to_fd)) {
1053 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
1055 if (p == MAP_FAILED)
1057 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
1059 if (q == MAP_FAILED) {
1060 munmap(p, from_len);
1064 rv = memcmp(p, q, from_len);
1066 digest_update(&ctx, p, from_len);
1067 munmap(p, from_len);
1068 munmap(q, from_len);
1072 if (!done_compare) {
1073 char buf1[MAXBSIZE];
1074 char buf2[MAXBSIZE];
1078 lseek(from_fd, 0, SEEK_SET);
1079 lseek(to_fd, 0, SEEK_SET);
1081 n1 = read(from_fd, buf1, sizeof(buf1));
1085 n2 = read(to_fd, buf2, n1);
1087 rv = memcmp(buf1, buf2, n1);
1089 rv = 1; /* out of sync */
1091 rv = 1; /* read failure */
1092 digest_update(&ctx, buf1, n1);
1094 lseek(from_fd, 0, SEEK_SET);
1095 lseek(to_fd, 0, SEEK_SET);
1098 rv = 1; /* don't bother in this case */
1100 if (dresp != NULL) {
1102 *dresp = digest_end(&ctx, NULL);
1104 (void)digest_end(&ctx, NULL);
1111 * create_tempfile --
1112 * create a temporary file based on path and open it
1115 create_tempfile(const char *path, char *temp, size_t tsize)
1119 (void)strncpy(temp, path, tsize);
1120 temp[tsize - 1] = '\0';
1121 if ((p = strrchr(temp, '/')) != NULL)
1125 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
1126 temp[tsize - 1] = '\0';
1127 return (mkstemp(temp));
1132 * create a new file, overwriting an existing one if necessary
1135 create_newfile(const char *path, int target, struct stat *sbp)
1137 char backup[MAXPATHLEN];
1138 int saved_errno = 0;
1143 * Unlink now... avoid ETXTBSY errors later. Try to turn
1144 * off the append/immutable bits -- if we fail, go ahead,
1147 if (sbp->st_flags & NOCHANGEBITS)
1148 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
1151 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
1152 path, suffix) != strlen(path) + strlen(suffix)) {
1153 saved_errno = errno;
1154 if (sbp->st_flags & NOCHANGEBITS)
1155 (void)chflags(path, sbp->st_flags);
1156 errno = saved_errno;
1157 errx(EX_OSERR, "%s: backup filename too long",
1160 (void)snprintf(backup, MAXPATHLEN, "%s%s",
1163 (void)printf("install: %s -> %s\n",
1165 if (rename(path, backup) < 0) {
1166 saved_errno = errno;
1167 if (sbp->st_flags & NOCHANGEBITS)
1168 (void)chflags(path, sbp->st_flags);
1169 errno = saved_errno;
1170 err(EX_OSERR, "rename: %s to %s", path, backup);
1173 if (unlink(path) < 0)
1174 saved_errno = errno;
1177 newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
1178 if (newfd < 0 && saved_errno != 0)
1179 errno = saved_errno;
1185 * copy from one file to another
1188 copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
1198 /* Rewind file descriptors. */
1199 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1200 err(EX_OSERR, "lseek: %s", from_name);
1201 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1202 err(EX_OSERR, "lseek: %s", to_name);
1207 * Mmap and write if less than 8M (the limit is so we don't totally
1208 * trash memory on big files. This is really a minor hack, but it
1209 * wins some CPU back.
1212 if (size <= 8 * 1048576 && trymmap(from_fd) &&
1213 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
1214 from_fd, (off_t)0)) != MAP_FAILED) {
1215 nw = write(to_fd, p, size);
1218 (void)unlink(to_name);
1221 "short write to %s: %jd bytes written, %jd bytes asked to write",
1222 to_name, (uintmax_t)nw, (uintmax_t)size);
1225 err(EX_OSERR, "%s", to_name);
1228 digest_update(&ctx, p, size);
1229 (void)munmap(p, size);
1233 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
1234 if ((nw = write(to_fd, buf, nr)) != nr) {
1236 (void)unlink(to_name);
1239 "short write to %s: %jd bytes written, %jd bytes asked to write",
1240 to_name, (uintmax_t)nw,
1244 err(EX_OSERR, "%s", to_name);
1247 digest_update(&ctx, buf, nr);
1251 (void)unlink(to_name);
1253 err(EX_OSERR, "%s", from_name);
1256 return (digest_end(&ctx, NULL));
1261 * use strip(1) to strip the target file
1264 strip(const char *to_name)
1266 const char *stripbin;
1267 const char *args[3];
1271 stripbin = getenv("STRIPBIN");
1272 if (stripbin == NULL)
1277 error = posix_spawnp(&pid, stripbin, NULL, NULL,
1278 __DECONST(char **, args), environ);
1280 (void)unlink(to_name);
1281 errc(error == EAGAIN || error == EPROCLIM || error == ENOMEM ?
1282 EX_TEMPFAIL : EX_OSERR, error, "spawn %s", stripbin);
1284 if (waitpid(pid, &status, 0) == -1) {
1286 (void)unlink(to_name);
1287 errc(EX_SOFTWARE, error, "wait");
1291 (void)unlink(to_name);
1292 errx(EX_SOFTWARE, "strip command %s failed on %s",
1299 * build directory hierarchy
1302 install_dir(char *path)
1306 int ch, tried_mkdir;
1308 for (p = path;; ++p)
1309 if (!*p || (p != path && *p == '/')) {
1314 if (stat(path, &sb) < 0) {
1315 if (errno != ENOENT || tried_mkdir)
1316 err(EX_OSERR, "stat %s", path);
1317 if (mkdir(path, 0755) < 0) {
1319 if (errno == EEXIST)
1321 err(EX_OSERR, "mkdir %s", path);
1324 (void)printf("install: mkdir %s\n",
1326 } else if (!S_ISDIR(sb.st_mode))
1327 errx(EX_OSERR, "%s exists but is not a directory", path);
1333 if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
1334 chown(path, uid, gid))
1335 warn("chown %u:%u %s", uid, gid, path);
1336 /* XXXBED: should we do the chmod in the dounpriv case? */
1337 if (chmod(path, mode))
1338 warn("chmod %o %s", mode, path);
1340 metadata_log(path, "dir", NULL, NULL, NULL, 0);
1345 * if metafp is not NULL, output mtree(8) full path name and settings to
1346 * metafp, to allow permissions to be set correctly by other tools,
1347 * or to allow integrity checks to be performed.
1350 metadata_log(const char *path, const char *type, struct timespec *ts,
1351 const char *slink, const char *digestresult, off_t size)
1353 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
1357 struct flock metalog_lock;
1361 /* Buffer for strsvis(3). */
1362 buf = (char *)malloc(4 * strlen(path) + 1);
1364 warnx("%s", strerror(ENOMEM));
1368 /* Lock log file. */
1369 metalog_lock.l_start = 0;
1370 metalog_lock.l_len = 0;
1371 metalog_lock.l_whence = SEEK_SET;
1372 metalog_lock.l_type = F_WRLCK;
1373 if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
1374 warn("can't lock %s", metafile);
1379 /* Remove destdir. */
1382 destlen = strlen(destdir);
1383 if (strncmp(p, destdir, destlen) == 0 &&
1384 (p[destlen] == '/' || p[destlen] == '\0'))
1387 while (*p && *p == '/')
1389 strsvis(buf, p, VIS_OCTAL, extra);
1391 /* Print details. */
1392 fprintf(metafp, ".%s%s type=%s", *p ? "/" : "", p, type);
1394 fprintf(metafp, " uname=%s", owner);
1396 fprintf(metafp, " gname=%s", group);
1397 fprintf(metafp, " mode=%#o", mode);
1399 strsvis(buf, slink, VIS_CSTYLE, extra); /* encode link */
1400 fprintf(metafp, " link=%s", buf);
1402 if (*type == 'f') /* type=file */
1403 fprintf(metafp, " size=%lld", (long long)size);
1404 if (ts != NULL && dopreserve)
1405 fprintf(metafp, " time=%lld.%09ld",
1406 (long long)ts[1].tv_sec, ts[1].tv_nsec);
1407 if (digestresult && digest)
1408 fprintf(metafp, " %s=%s", digest, digestresult);
1410 fprintf(metafp, " flags=%s", fflags);
1412 fprintf(metafp, " tags=%s", tags);
1413 fputc('\n', metafp);
1417 /* Unlock log file. */
1418 metalog_lock.l_type = F_UNLCK;
1419 if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1)
1420 warn("can't unlock %s", metafile);
1426 * print a usage message and die
1431 (void)fprintf(stderr,
1432 "usage: install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
1433 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1434 " [-B suffix] [-l linkflags] [-N dbdir]\n"
1436 " install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
1437 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1438 " [-B suffix] [-l linkflags] [-N dbdir]\n"
1439 " file1 ... fileN directory\n"
1440 " install -dU [-vU] [-g group] [-m mode] [-N dbdir] [-o owner]\n"
1441 " [-M log] [-D dest] [-h hash] [-T tags]\n"
1442 " directory ...\n");
1449 * return true (1) if mmap should be tried, false (0) if not.
1455 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
1456 * pre-Lite2-merge systems.
1461 if (fstatfs(fd, &stfs) != 0)
1463 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
1464 strcmp(stfs.f_fstypename, "cd9660") == 0)