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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1987, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
47 #include <sys/mount.h>
64 /* Bootstrap aid - this doesn't exist in most older releases */
66 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
69 #define MAX_CMP_SIZE (16 * 1024 * 1024)
71 #define DIRECTORY 0x01 /* Tell install it's a directory. */
72 #define SETFLAGS 0x02 /* Tell install to set flags. */
73 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
74 #define BACKUP_SUFFIX ".old"
80 int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
81 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
82 const char *suffix = BACKUP_SUFFIX;
84 static int compare(int, const char *, size_t, int, const char *, size_t);
85 static void copy(int, const char *, int, const char *, off_t);
86 static int create_newfile(const char *, int, struct stat *);
87 static int create_tempfile(const char *, char *, size_t);
88 static void install(const char *, const char *, u_long, u_int);
89 static void install_dir(char *);
90 static u_long numeric_id(const char *, const char *);
91 static void strip(const char *);
92 static int trymmap(int);
93 static void usage(void);
96 main(int argc, char *argv[])
98 struct stat from_sb, to_sb;
104 const char *group, *owner, *to_name;
107 group = owner = NULL;
108 while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:o:pSsv")) != -1)
120 /* For backwards compatibility. */
127 if (strtofflags(&flags, &fset, NULL))
128 errx(EX_USAGE, "%s: invalid flag", flags);
138 if (!(set = setmode(optarg)))
139 errx(EX_USAGE, "invalid file mode: %s",
141 mode = getmode(set, 0);
148 docompare = dopreserve = 1;
166 /* some options make no sense when creating directories */
167 if (dostrip && dodir) {
168 warnx("-d and -s may not be specified together");
172 if (getenv("DONTSTRIP") != NULL) {
173 warnx("DONTSTRIP set - will not strip installed binaries");
177 /* must have at least two arguments, except when creating directories */
178 if (argc == 0 || (argc == 1 && !dodir))
181 /* need to make a temp copy so we can compare stripped version */
182 if (docompare && dostrip)
185 /* get group and owner id's */
187 if ((gp = getgrnam(group)) != NULL)
190 gid = (gid_t)numeric_id(group, "group");
195 if ((pp = getpwnam(owner)) != NULL)
198 uid = (uid_t)numeric_id(owner, "user");
203 for (; *argv != NULL; ++argv)
209 no_target = stat(to_name = argv[argc - 1], &to_sb);
210 if (!no_target && S_ISDIR(to_sb.st_mode)) {
211 for (; *argv != to_name; ++argv)
212 install(*argv, to_name, fset, iflags | DIRECTORY);
217 /* can't do file1 file2 directory/file */
220 warnx("target directory `%s' does not exist",
223 warnx("target `%s' is not a directory",
229 if (stat(*argv, &from_sb))
230 err(EX_OSERR, "%s", *argv);
231 if (!S_ISREG(to_sb.st_mode)) {
233 err(EX_OSERR, "%s", to_name);
235 if (to_sb.st_dev == from_sb.st_dev &&
236 to_sb.st_ino == from_sb.st_ino)
238 "%s and %s are the same file", *argv, to_name);
240 install(*argv, to_name, fset, iflags);
246 numeric_id(const char *name, const char *type)
253 * We know that uid_t's and gid_t's are unsigned longs.
256 val = strtoul(name, &ep, 10);
258 err(EX_NOUSER, "%s", name);
260 errx(EX_NOUSER, "unknown %s %s", type, name);
266 * build a path name and install the file
269 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
271 struct stat from_sb, temp_sb, to_sb;
272 struct timeval tvb[2];
273 int devnull, files_match, from_fd, serrno, target;
274 int tempcopy, temp_fd, to_fd;
275 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
281 /* If try to install NULL file to a directory, fails. */
282 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
283 if (stat(from_name, &from_sb))
284 err(EX_OSERR, "%s", from_name);
285 if (!S_ISREG(from_sb.st_mode)) {
287 err(EX_OSERR, "%s", from_name);
289 /* Build the target path. */
290 if (flags & DIRECTORY) {
291 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
293 (p = strrchr(from_name, '/')) ? ++p : from_name);
301 target = stat(to_name, &to_sb) == 0;
303 /* Only install to regular files. */
304 if (target && !S_ISREG(to_sb.st_mode)) {
310 /* Only copy safe if the target exists. */
311 tempcopy = safecopy && target;
313 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
314 err(EX_OSERR, "%s", from_name);
316 /* If we don't strip, we can compare first. */
317 if (docompare && !dostrip && target) {
318 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
319 err(EX_OSERR, "%s", to_name);
321 files_match = to_sb.st_size == 0;
323 files_match = !(compare(from_fd, from_name,
324 (size_t)from_sb.st_size, to_fd,
325 to_name, (size_t)to_sb.st_size));
327 /* Close "to" file unless we match. */
334 to_fd = create_tempfile(to_name, tempfile,
337 err(EX_OSERR, "%s", tempfile);
339 if ((to_fd = create_newfile(to_name, target,
341 err(EX_OSERR, "%s", to_name);
343 (void)printf("install: %s -> %s\n",
347 copy(from_fd, from_name, to_fd,
348 tempcopy ? tempfile : to_name, from_sb.st_size);
352 strip(tempcopy ? tempfile : to_name);
355 * Re-open our fd on the target, in case we used a strip
356 * that does not work in-place -- like GNU binutils strip.
359 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
361 err(EX_OSERR, "stripping %s", to_name);
365 * Compare the stripped temp file with the target.
367 if (docompare && dostrip && target) {
370 /* Re-open to_fd using the real target name. */
371 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
372 err(EX_OSERR, "%s", to_name);
374 if (fstat(temp_fd, &temp_sb)) {
376 (void)unlink(tempfile);
378 err(EX_OSERR, "%s", tempfile);
381 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
382 to_name, (size_t)to_sb.st_size) == 0) {
384 * If target has more than one link we need to
385 * replace it in order to snap the extra links.
386 * Need to preserve target file times, though.
388 if (to_sb.st_nlink != 1) {
389 tvb[0].tv_sec = to_sb.st_atime;
391 tvb[1].tv_sec = to_sb.st_mtime;
393 (void)utimes(tempfile, tvb);
396 (void)unlink(tempfile);
398 (void) close(temp_fd);
403 * Move the new file into place if doing a safe copy
404 * and the files are different (or just not compared).
406 if (tempcopy && !files_match) {
407 /* Try to turn off the immutable bits. */
408 if (to_sb.st_flags & NOCHANGEBITS)
409 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
411 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
412 suffix) != strlen(to_name) + strlen(suffix)) {
414 errx(EX_OSERR, "%s: backup filename too long",
418 (void)printf("install: %s -> %s\n", to_name, backup);
419 if (rename(to_name, backup) < 0) {
423 err(EX_OSERR, "rename: %s to %s", to_name,
428 (void)printf("install: %s -> %s\n", from_name, to_name);
429 if (rename(tempfile, to_name) < 0) {
433 err(EX_OSERR, "rename: %s to %s",
437 /* Re-open to_fd so we aren't hosed by the rename(2). */
439 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
440 err(EX_OSERR, "%s", to_name);
444 * Preserve the timestamp of the source file if necessary.
446 if (dopreserve && !files_match && !devnull) {
447 tvb[0].tv_sec = from_sb.st_atime;
449 tvb[1].tv_sec = from_sb.st_mtime;
451 (void)utimes(to_name, tvb);
454 if (fstat(to_fd, &to_sb) == -1) {
456 (void)unlink(to_name);
458 err(EX_OSERR, "%s", to_name);
462 * Set owner, group, mode for target; do the chown first,
463 * chown may lose the setuid bits.
465 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
466 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
467 (mode != (to_sb.st_mode & ALLPERMS))) {
468 /* Try to turn off the immutable bits. */
469 if (to_sb.st_flags & NOCHANGEBITS)
470 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
473 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
474 (uid != (uid_t)-1 && uid != to_sb.st_uid))
475 if (fchown(to_fd, uid, gid) == -1) {
477 (void)unlink(to_name);
479 err(EX_OSERR,"%s: chown/chgrp", to_name);
482 if (mode != (to_sb.st_mode & ALLPERMS))
483 if (fchmod(to_fd, mode)) {
485 (void)unlink(to_name);
487 err(EX_OSERR, "%s: chmod", to_name);
491 * If provided a set of flags, set them, otherwise, preserve the
492 * flags, except for the dump flag.
493 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
494 * trying to turn off UF_NODUMP. If we're trying to set real flags,
495 * then warn if the fs doesn't support it, otherwise fail.
497 if (!devnull && (flags & SETFLAGS ||
498 (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
500 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
501 if (flags & SETFLAGS) {
502 if (errno == EOPNOTSUPP)
503 warn("%s: chflags", to_name);
506 (void)unlink(to_name);
508 err(EX_OSERR, "%s: chflags", to_name);
515 (void)close(from_fd);
520 * compare two files; non-zero means files differ
523 compare(int from_fd, const char *from_name __unused, size_t from_len,
524 int to_fd, const char *to_name __unused, size_t to_len)
531 if (from_len != to_len)
534 if (from_len <= MAX_CMP_SIZE) {
536 if (trymmap(from_fd) && trymmap(to_fd)) {
537 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
538 if (p == (char *)MAP_FAILED)
540 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
541 if (q == (char *)MAP_FAILED) {
546 rv = memcmp(p, q, from_len);
558 lseek(from_fd, 0, SEEK_SET);
559 lseek(to_fd, 0, SEEK_SET);
561 n1 = read(from_fd, buf1, sizeof(buf1));
565 n2 = read(to_fd, buf2, n1);
567 rv = memcmp(buf1, buf2, n1);
569 rv = 1; /* out of sync */
571 rv = 1; /* read failure */
573 lseek(from_fd, 0, SEEK_SET);
574 lseek(to_fd, 0, SEEK_SET);
577 rv = 1; /* don't bother in this case */
584 * create a temporary file based on path and open it
587 create_tempfile(const char *path, char *temp, size_t tsize)
591 (void)strncpy(temp, path, tsize);
592 temp[tsize - 1] = '\0';
593 if ((p = strrchr(temp, '/')) != NULL)
597 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
598 temp[tsize - 1] = '\0';
599 return (mkstemp(temp));
604 * create a new file, overwriting an existing one if necessary
607 create_newfile(const char *path, int target, struct stat *sbp)
609 char backup[MAXPATHLEN];
615 * Unlink now... avoid ETXTBSY errors later. Try to turn
616 * off the append/immutable bits -- if we fail, go ahead,
619 if (sbp->st_flags & NOCHANGEBITS)
620 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
623 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
624 path, suffix) != strlen(path) + strlen(suffix))
625 errx(EX_OSERR, "%s: backup filename too long",
627 (void)snprintf(backup, MAXPATHLEN, "%s%s",
630 (void)printf("install: %s -> %s\n",
632 if (rename(path, backup) < 0)
633 err(EX_OSERR, "rename: %s to %s", path, backup);
635 if (unlink(path) < 0)
639 newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
640 if (newfd < 0 && saved_errno != 0)
647 * copy from one file to another
650 copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
655 char *p, buf[MAXBSIZE];
658 /* Rewind file descriptors. */
659 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
660 err(EX_OSERR, "lseek: %s", from_name);
661 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
662 err(EX_OSERR, "lseek: %s", to_name);
665 * Mmap and write if less than 8M (the limit is so we don't totally
666 * trash memory on big files. This is really a minor hack, but it
667 * wins some CPU back.
670 if (size <= 8 * 1048576 && trymmap(from_fd) &&
671 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
672 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
673 if ((nw = write(to_fd, p, size)) != size) {
675 (void)unlink(to_name);
676 errno = nw > 0 ? EIO : serrno;
677 err(EX_OSERR, "%s", to_name);
682 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
683 if ((nw = write(to_fd, buf, nr)) != nr) {
685 (void)unlink(to_name);
686 errno = nw > 0 ? EIO : serrno;
687 err(EX_OSERR, "%s", to_name);
691 (void)unlink(to_name);
693 err(EX_OSERR, "%s", from_name);
700 * use strip(1) to strip the target file
703 strip(const char *to_name)
705 const char *stripbin;
711 (void)unlink(to_name);
713 err(EX_TEMPFAIL, "fork");
715 stripbin = getenv("STRIPBIN");
716 if (stripbin == NULL)
718 execlp(stripbin, stripbin, to_name, (char *)NULL);
719 err(EX_OSERR, "exec(%s)", stripbin);
721 if (wait(&status) == -1 || status) {
723 (void)unlink(to_name);
724 errc(EX_SOFTWARE, serrno, "wait");
732 * build directory hierarchy
735 install_dir(char *path)
742 if (!*p || (p != path && *p == '/')) {
745 if (stat(path, &sb)) {
746 if (errno != ENOENT || mkdir(path, 0755) < 0) {
747 err(EX_OSERR, "mkdir %s", path);
750 (void)printf("install: mkdir %s\n",
752 } else if (!S_ISDIR(sb.st_mode))
753 errx(EX_OSERR, "%s exists but is not a directory", path);
758 if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
759 warn("chown %u:%u %s", uid, gid, path);
760 if (chmod(path, mode))
761 warn("chmod %o %s", mode, path);
766 * print a usage message and die
771 (void)fprintf(stderr,
772 "usage: install [-bCcMpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
773 " [-o owner] file1 file2\n"
774 " install [-bCcMpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
775 " [-o owner] file1 ... fileN directory\n"
776 " install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
783 * return true (1) if mmap should be tried, false (0) if not.
789 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
790 * pre-Lite2-merge systems.
795 if (nommap || fstatfs(fd, &stfs) != 0)
797 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
798 strcmp(stfs.f_fstypename, "cd9660") == 0)