2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1987, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
51 #include <sys/mount.h>
69 /* Bootstrap aid - this doesn't exist in most older releases */
71 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
74 #define MAX_CMP_SIZE (16 * 1024 * 1024)
76 #define DIRECTORY 0x01 /* Tell install it's a directory. */
77 #define SETFLAGS 0x02 /* Tell install to set flags. */
78 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
79 #define BACKUP_SUFFIX ".old"
85 int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
86 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
87 const char *suffix = BACKUP_SUFFIX;
89 void copy(int, const char *, int, const char *, off_t);
90 int compare(int, const char *, size_t, int, const char *, size_t);
91 int create_newfile(const char *, int, struct stat *);
92 int create_tempfile(const char *, char *, size_t);
93 void install(const char *, const char *, u_long, u_int);
94 void install_dir(char *);
95 u_long numeric_id(const char *, const char *);
96 void strip(const char *);
101 main(int argc, char *argv[])
103 struct stat from_sb, to_sb;
109 const char *group, *owner, *to_name;
112 group = owner = NULL;
113 while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:o:pSsv")) != -1)
125 /* For backwards compatibility. */
132 if (strtofflags(&flags, &fset, NULL))
133 errx(EX_USAGE, "%s: invalid flag", flags);
143 if (!(set = setmode(optarg)))
144 errx(EX_USAGE, "invalid file mode: %s",
146 mode = getmode(set, 0);
153 docompare = dopreserve = 1;
171 /* some options make no sense when creating directories */
172 if (dostrip && dodir) {
173 warnx("-d and -s may not be specified together");
177 if (getenv("DONTSTRIP") != NULL) {
178 warnx("DONTSTRIP set - will not strip installed binaries");
182 /* must have at least two arguments, except when creating directories */
183 if (argc == 0 || (argc == 1 && !dodir))
186 /* need to make a temp copy so we can compare stripped version */
187 if (docompare && dostrip)
190 /* get group and owner id's */
192 if ((gp = getgrnam(group)) != NULL)
195 gid = (gid_t)numeric_id(group, "group");
200 if ((pp = getpwnam(owner)) != NULL)
203 uid = (uid_t)numeric_id(owner, "user");
208 for (; *argv != NULL; ++argv)
214 no_target = stat(to_name = argv[argc - 1], &to_sb);
215 if (!no_target && S_ISDIR(to_sb.st_mode)) {
216 for (; *argv != to_name; ++argv)
217 install(*argv, to_name, fset, iflags | DIRECTORY);
222 /* can't do file1 file2 directory/file */
225 warnx("target directory `%s' does not exist",
228 warnx("target `%s' is not a directory",
234 if (stat(*argv, &from_sb))
235 err(EX_OSERR, "%s", *argv);
236 if (!S_ISREG(to_sb.st_mode)) {
238 err(EX_OSERR, "%s", to_name);
240 if (to_sb.st_dev == from_sb.st_dev &&
241 to_sb.st_ino == from_sb.st_ino)
243 "%s and %s are the same file", *argv, to_name);
245 install(*argv, to_name, fset, iflags);
251 numeric_id(const char *name, const char *type)
258 * We know that uid_t's and gid_t's are unsigned longs.
261 val = strtoul(name, &ep, 10);
263 err(EX_NOUSER, "%s", name);
265 errx(EX_NOUSER, "unknown %s %s", type, name);
271 * build a path name and install the file
274 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
276 struct stat from_sb, temp_sb, to_sb;
277 struct timeval tvb[2];
278 int devnull, files_match, from_fd, serrno, target;
279 int tempcopy, temp_fd, to_fd;
280 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
286 /* If try to install NULL file to a directory, fails. */
287 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
288 if (stat(from_name, &from_sb))
289 err(EX_OSERR, "%s", from_name);
290 if (!S_ISREG(from_sb.st_mode)) {
292 err(EX_OSERR, "%s", from_name);
294 /* Build the target path. */
295 if (flags & DIRECTORY) {
296 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
298 (p = strrchr(from_name, '/')) ? ++p : from_name);
306 target = stat(to_name, &to_sb) == 0;
308 /* Only install to regular files. */
309 if (target && !S_ISREG(to_sb.st_mode)) {
315 /* Only copy safe if the target exists. */
316 tempcopy = safecopy && target;
318 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
319 err(EX_OSERR, "%s", from_name);
321 /* If we don't strip, we can compare first. */
322 if (docompare && !dostrip && target) {
323 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
324 err(EX_OSERR, "%s", to_name);
326 files_match = to_sb.st_size == 0;
328 files_match = !(compare(from_fd, from_name,
329 (size_t)from_sb.st_size, to_fd,
330 to_name, (size_t)to_sb.st_size));
332 /* Close "to" file unless we match. */
339 to_fd = create_tempfile(to_name, tempfile,
342 err(EX_OSERR, "%s", tempfile);
344 if ((to_fd = create_newfile(to_name, target,
346 err(EX_OSERR, "%s", to_name);
348 (void)printf("install: %s -> %s\n",
352 copy(from_fd, from_name, to_fd,
353 tempcopy ? tempfile : to_name, from_sb.st_size);
357 strip(tempcopy ? tempfile : to_name);
360 * Re-open our fd on the target, in case we used a strip
361 * that does not work in-place -- like GNU binutils strip.
364 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
366 err(EX_OSERR, "stripping %s", to_name);
370 * Compare the stripped temp file with the target.
372 if (docompare && dostrip && target) {
375 /* Re-open to_fd using the real target name. */
376 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
377 err(EX_OSERR, "%s", to_name);
379 if (fstat(temp_fd, &temp_sb)) {
381 (void)unlink(tempfile);
383 err(EX_OSERR, "%s", tempfile);
386 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
387 to_name, (size_t)to_sb.st_size) == 0) {
389 * If target has more than one link we need to
390 * replace it in order to snap the extra links.
391 * Need to preserve target file times, though.
393 if (to_sb.st_nlink != 1) {
394 tvb[0].tv_sec = to_sb.st_atime;
396 tvb[1].tv_sec = to_sb.st_mtime;
398 (void)utimes(tempfile, tvb);
401 (void)unlink(tempfile);
403 (void) close(temp_fd);
408 * Move the new file into place if doing a safe copy
409 * and the files are different (or just not compared).
411 if (tempcopy && !files_match) {
412 /* Try to turn off the immutable bits. */
413 if (to_sb.st_flags & NOCHANGEBITS)
414 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
416 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
417 suffix) != strlen(to_name) + strlen(suffix)) {
419 errx(EX_OSERR, "%s: backup filename too long",
423 (void)printf("install: %s -> %s\n", to_name, backup);
424 if (rename(to_name, backup) < 0) {
428 err(EX_OSERR, "rename: %s to %s", to_name,
433 (void)printf("install: %s -> %s\n", from_name, to_name);
434 if (rename(tempfile, to_name) < 0) {
438 err(EX_OSERR, "rename: %s to %s",
442 /* Re-open to_fd so we aren't hosed by the rename(2). */
444 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
445 err(EX_OSERR, "%s", to_name);
449 * Preserve the timestamp of the source file if necessary.
451 if (dopreserve && !files_match && !devnull) {
452 tvb[0].tv_sec = from_sb.st_atime;
454 tvb[1].tv_sec = from_sb.st_mtime;
456 (void)utimes(to_name, tvb);
459 if (fstat(to_fd, &to_sb) == -1) {
461 (void)unlink(to_name);
463 err(EX_OSERR, "%s", to_name);
467 * Set owner, group, mode for target; do the chown first,
468 * chown may lose the setuid bits.
470 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
471 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
472 (mode != (to_sb.st_mode & ALLPERMS))) {
473 /* Try to turn off the immutable bits. */
474 if (to_sb.st_flags & NOCHANGEBITS)
475 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
478 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
479 (uid != (uid_t)-1 && uid != to_sb.st_uid))
480 if (fchown(to_fd, uid, gid) == -1) {
482 (void)unlink(to_name);
484 err(EX_OSERR,"%s: chown/chgrp", to_name);
487 if (mode != (to_sb.st_mode & ALLPERMS))
488 if (fchmod(to_fd, mode)) {
490 (void)unlink(to_name);
492 err(EX_OSERR, "%s: chmod", to_name);
496 * If provided a set of flags, set them, otherwise, preserve the
497 * flags, except for the dump flag.
498 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
499 * trying to turn off UF_NODUMP. If we're trying to set real flags,
500 * then warn if the the fs doesn't support it, otherwise fail.
502 if (!devnull && (flags & SETFLAGS ||
503 (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
505 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
506 if (flags & SETFLAGS) {
507 if (errno == EOPNOTSUPP)
508 warn("%s: chflags", to_name);
511 (void)unlink(to_name);
513 err(EX_OSERR, "%s: chflags", to_name);
520 (void)close(from_fd);
525 * compare two files; non-zero means files differ
528 compare(int from_fd, const char *from_name __unused, size_t from_len,
529 int to_fd, const char *to_name __unused, size_t to_len)
536 if (from_len != to_len)
539 if (from_len <= MAX_CMP_SIZE) {
541 if (trymmap(from_fd) && trymmap(to_fd)) {
542 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
543 if (p == (char *)MAP_FAILED)
545 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
546 if (q == (char *)MAP_FAILED) {
551 rv = memcmp(p, q, from_len);
563 lseek(from_fd, 0, SEEK_SET);
564 lseek(to_fd, 0, SEEK_SET);
566 n1 = read(from_fd, buf1, sizeof(buf1));
570 n2 = read(to_fd, buf2, n1);
572 rv = memcmp(buf1, buf2, n1);
574 rv = 1; /* out of sync */
576 rv = 1; /* read failure */
578 lseek(from_fd, 0, SEEK_SET);
579 lseek(to_fd, 0, SEEK_SET);
582 rv = 1; /* don't bother in this case */
589 * create a temporary file based on path and open it
592 create_tempfile(const char *path, char *temp, size_t tsize)
596 (void)strncpy(temp, path, tsize);
597 temp[tsize - 1] = '\0';
598 if ((p = strrchr(temp, '/')) != NULL)
602 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
603 temp[tsize - 1] = '\0';
604 return (mkstemp(temp));
609 * create a new file, overwriting an existing one if necessary
612 create_newfile(const char *path, int target, struct stat *sbp)
614 char backup[MAXPATHLEN];
620 * Unlink now... avoid ETXTBSY errors later. Try to turn
621 * off the append/immutable bits -- if we fail, go ahead,
624 if (sbp->st_flags & NOCHANGEBITS)
625 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
628 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
629 path, suffix) != strlen(path) + strlen(suffix))
630 errx(EX_OSERR, "%s: backup filename too long",
632 (void)snprintf(backup, MAXPATHLEN, "%s%s",
635 (void)printf("install: %s -> %s\n",
637 if (rename(path, backup) < 0)
638 err(EX_OSERR, "rename: %s to %s", path, backup);
640 if (unlink(path) < 0)
644 newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
645 if (newfd < 0 && saved_errno != 0)
652 * copy from one file to another
655 copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
660 char *p, buf[MAXBSIZE];
663 /* Rewind file descriptors. */
664 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
665 err(EX_OSERR, "lseek: %s", from_name);
666 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
667 err(EX_OSERR, "lseek: %s", to_name);
670 * Mmap and write if less than 8M (the limit is so we don't totally
671 * trash memory on big files. This is really a minor hack, but it
672 * wins some CPU back.
675 if (size <= 8 * 1048576 && trymmap(from_fd) &&
676 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
677 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
678 if ((nw = write(to_fd, p, size)) != size) {
680 (void)unlink(to_name);
681 errno = nw > 0 ? EIO : serrno;
682 err(EX_OSERR, "%s", to_name);
687 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
688 if ((nw = write(to_fd, buf, nr)) != nr) {
690 (void)unlink(to_name);
691 errno = nw > 0 ? EIO : serrno;
692 err(EX_OSERR, "%s", to_name);
696 (void)unlink(to_name);
698 err(EX_OSERR, "%s", from_name);
705 * use strip(1) to strip the target file
708 strip(const char *to_name)
710 const char *stripbin;
716 (void)unlink(to_name);
718 err(EX_TEMPFAIL, "fork");
720 stripbin = getenv("STRIPBIN");
721 if (stripbin == NULL)
723 execlp(stripbin, stripbin, to_name, (char *)NULL);
724 err(EX_OSERR, "exec(%s)", stripbin);
726 if (wait(&status) == -1 || status) {
728 (void)unlink(to_name);
729 errc(EX_SOFTWARE, serrno, "wait");
737 * build directory heirarchy
740 install_dir(char *path)
747 if (!*p || (p != path && *p == '/')) {
750 if (stat(path, &sb)) {
751 if (errno != ENOENT || mkdir(path, 0755) < 0) {
752 err(EX_OSERR, "mkdir %s", path);
755 (void)printf("install: mkdir %s\n",
757 } else if (!S_ISDIR(sb.st_mode))
758 errx(EX_OSERR, "%s exists but is not a directory", path);
763 if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
764 warn("chown %u:%u %s", uid, gid, path);
765 if (chmod(path, mode))
766 warn("chmod %o %s", mode, path);
771 * print a usage message and die
776 (void)fprintf(stderr,
777 "usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
778 " [-o owner] file1 file2\n"
779 " install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
780 " [-o owner] file1 ... fileN directory\n"
781 " install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
788 * return true (1) if mmap should be tried, false (0) if not.
794 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
795 * pre-Lite2-merge systems.
800 if (nommap || fstatfs(fd, &stfs) != 0)
802 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
803 strcmp(stfs.f_fstypename, "cd9660") == 0)