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 /* 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 */
219 warnx("wrong number or types of arguments");
224 if (stat(*argv, &from_sb))
225 err(EX_OSERR, "%s", *argv);
226 if (!S_ISREG(to_sb.st_mode)) {
228 err(EX_OSERR, "%s", to_name);
230 if (to_sb.st_dev == from_sb.st_dev &&
231 to_sb.st_ino == from_sb.st_ino)
233 "%s and %s are the same file", *argv, to_name);
235 install(*argv, to_name, fset, iflags);
241 numeric_id(const char *name, const char *type)
248 * We know that uid_t's and gid_t's are unsigned longs.
251 val = strtoul(name, &ep, 10);
253 err(EX_NOUSER, "%s", name);
255 errx(EX_NOUSER, "unknown %s %s", type, name);
261 * build a path name and install the file
264 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
266 struct stat from_sb, temp_sb, to_sb;
267 struct timeval tvb[2];
268 int devnull, files_match, from_fd, serrno, target;
269 int tempcopy, temp_fd, to_fd;
270 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
276 /* If try to install NULL file to a directory, fails. */
277 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
278 if (stat(from_name, &from_sb))
279 err(EX_OSERR, "%s", from_name);
280 if (!S_ISREG(from_sb.st_mode)) {
282 err(EX_OSERR, "%s", from_name);
284 /* Build the target path. */
285 if (flags & DIRECTORY) {
286 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
288 (p = strrchr(from_name, '/')) ? ++p : from_name);
296 target = stat(to_name, &to_sb) == 0;
298 /* Only install to regular files. */
299 if (target && !S_ISREG(to_sb.st_mode)) {
305 /* Only copy safe if the target exists. */
306 tempcopy = safecopy && target;
308 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
309 err(EX_OSERR, "%s", from_name);
311 /* If we don't strip, we can compare first. */
312 if (docompare && !dostrip && target) {
313 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
314 err(EX_OSERR, "%s", to_name);
316 files_match = to_sb.st_size == 0;
318 files_match = !(compare(from_fd, from_name,
319 (size_t)from_sb.st_size, to_fd,
320 to_name, (size_t)to_sb.st_size));
322 /* Close "to" file unless we match. */
329 to_fd = create_tempfile(to_name, tempfile,
332 err(EX_OSERR, "%s", tempfile);
334 if ((to_fd = create_newfile(to_name, target,
336 err(EX_OSERR, "%s", to_name);
338 (void)printf("install: %s -> %s\n",
342 copy(from_fd, from_name, to_fd,
343 tempcopy ? tempfile : to_name, from_sb.st_size);
347 strip(tempcopy ? tempfile : to_name);
350 * Re-open our fd on the target, in case we used a strip
351 * that does not work in-place -- like GNU binutils strip.
354 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
356 err(EX_OSERR, "stripping %s", to_name);
360 * Compare the stripped temp file with the target.
362 if (docompare && dostrip && target) {
365 /* Re-open to_fd using the real target name. */
366 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
367 err(EX_OSERR, "%s", to_name);
369 if (fstat(temp_fd, &temp_sb)) {
371 (void)unlink(tempfile);
373 err(EX_OSERR, "%s", tempfile);
376 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
377 to_name, (size_t)to_sb.st_size) == 0) {
379 * If target has more than one link we need to
380 * replace it in order to snap the extra links.
381 * Need to preserve target file times, though.
383 if (to_sb.st_nlink != 1) {
384 tvb[0].tv_sec = to_sb.st_atime;
386 tvb[1].tv_sec = to_sb.st_mtime;
388 (void)utimes(tempfile, tvb);
391 (void)unlink(tempfile);
393 (void) close(temp_fd);
398 * Move the new file into place if doing a safe copy
399 * and the files are different (or just not compared).
401 if (tempcopy && !files_match) {
402 /* Try to turn off the immutable bits. */
403 if (to_sb.st_flags & NOCHANGEBITS)
404 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
406 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
407 suffix) != strlen(to_name) + strlen(suffix)) {
409 errx(EX_OSERR, "%s: backup filename too long",
413 (void)printf("install: %s -> %s\n", to_name, backup);
414 if (rename(to_name, backup) < 0) {
418 err(EX_OSERR, "rename: %s to %s", to_name,
423 (void)printf("install: %s -> %s\n", from_name, to_name);
424 if (rename(tempfile, to_name) < 0) {
428 err(EX_OSERR, "rename: %s to %s",
432 /* Re-open to_fd so we aren't hosed by the rename(2). */
434 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
435 err(EX_OSERR, "%s", to_name);
439 * Preserve the timestamp of the source file if necessary.
441 if (dopreserve && !files_match && !devnull) {
442 tvb[0].tv_sec = from_sb.st_atime;
444 tvb[1].tv_sec = from_sb.st_mtime;
446 (void)utimes(to_name, tvb);
449 if (fstat(to_fd, &to_sb) == -1) {
451 (void)unlink(to_name);
453 err(EX_OSERR, "%s", to_name);
457 * Set owner, group, mode for target; do the chown first,
458 * chown may lose the setuid bits.
460 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
461 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
462 (mode != (to_sb.st_mode & ALLPERMS))) {
463 /* Try to turn off the immutable bits. */
464 if (to_sb.st_flags & NOCHANGEBITS)
465 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
468 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
469 (uid != (uid_t)-1 && uid != to_sb.st_uid))
470 if (fchown(to_fd, uid, gid) == -1) {
472 (void)unlink(to_name);
474 err(EX_OSERR,"%s: chown/chgrp", to_name);
477 if (mode != (to_sb.st_mode & ALLPERMS))
478 if (fchmod(to_fd, mode)) {
480 (void)unlink(to_name);
482 err(EX_OSERR, "%s: chmod", to_name);
486 * If provided a set of flags, set them, otherwise, preserve the
487 * flags, except for the dump flag.
488 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
489 * trying to turn off UF_NODUMP. If we're trying to set real flags,
490 * then warn if the the fs doesn't support it, otherwise fail.
492 if (!devnull && (flags & SETFLAGS ||
493 (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
495 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
496 if (flags & SETFLAGS) {
497 if (errno == EOPNOTSUPP)
498 warn("%s: chflags", to_name);
501 (void)unlink(to_name);
503 err(EX_OSERR, "%s: chflags", to_name);
510 (void)close(from_fd);
515 * compare two files; non-zero means files differ
518 compare(int from_fd, const char *from_name __unused, size_t from_len,
519 int to_fd, const char *to_name __unused, size_t to_len)
526 if (from_len != to_len)
529 if (from_len <= MAX_CMP_SIZE) {
531 if (trymmap(from_fd) && trymmap(to_fd)) {
532 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
533 if (p == (char *)MAP_FAILED)
535 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
536 if (q == (char *)MAP_FAILED) {
541 rv = memcmp(p, q, from_len);
553 lseek(from_fd, 0, SEEK_SET);
554 lseek(to_fd, 0, SEEK_SET);
556 n1 = read(from_fd, buf1, sizeof(buf1));
560 n2 = read(to_fd, buf2, n1);
562 rv = memcmp(buf1, buf2, n1);
564 rv = 1; /* out of sync */
566 rv = 1; /* read failure */
568 lseek(from_fd, 0, SEEK_SET);
569 lseek(to_fd, 0, SEEK_SET);
572 rv = 1; /* don't bother in this case */
579 * create a temporary file based on path and open it
582 create_tempfile(const char *path, char *temp, size_t tsize)
586 (void)strncpy(temp, path, tsize);
587 temp[tsize - 1] = '\0';
588 if ((p = strrchr(temp, '/')) != NULL)
592 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
593 temp[tsize - 1] = '\0';
594 return (mkstemp(temp));
599 * create a new file, overwriting an existing one if necessary
602 create_newfile(const char *path, int target, struct stat *sbp)
604 char backup[MAXPATHLEN];
610 * Unlink now... avoid ETXTBSY errors later. Try to turn
611 * off the append/immutable bits -- if we fail, go ahead,
614 if (sbp->st_flags & NOCHANGEBITS)
615 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
618 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
619 path, suffix) != strlen(path) + strlen(suffix))
620 errx(EX_OSERR, "%s: backup filename too long",
622 (void)snprintf(backup, MAXPATHLEN, "%s%s",
625 (void)printf("install: %s -> %s\n",
627 if (rename(path, backup) < 0)
628 err(EX_OSERR, "rename: %s to %s", path, backup);
630 if (unlink(path) < 0)
634 newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
635 if (newfd < 0 && saved_errno != 0)
642 * copy from one file to another
645 copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
650 char *p, buf[MAXBSIZE];
653 /* Rewind file descriptors. */
654 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
655 err(EX_OSERR, "lseek: %s", from_name);
656 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
657 err(EX_OSERR, "lseek: %s", to_name);
660 * Mmap and write if less than 8M (the limit is so we don't totally
661 * trash memory on big files. This is really a minor hack, but it
662 * wins some CPU back.
665 if (size <= 8 * 1048576 && trymmap(from_fd) &&
666 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
667 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
668 if ((nw = write(to_fd, p, size)) != size) {
670 (void)unlink(to_name);
671 errno = nw > 0 ? EIO : serrno;
672 err(EX_OSERR, "%s", to_name);
677 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
678 if ((nw = write(to_fd, buf, nr)) != nr) {
680 (void)unlink(to_name);
681 errno = nw > 0 ? EIO : serrno;
682 err(EX_OSERR, "%s", to_name);
686 (void)unlink(to_name);
688 err(EX_OSERR, "%s", from_name);
695 * use strip(1) to strip the target file
698 strip(const char *to_name)
700 const char *stripbin;
706 (void)unlink(to_name);
708 err(EX_TEMPFAIL, "fork");
710 stripbin = getenv("STRIPBIN");
711 if (stripbin == NULL)
713 execlp(stripbin, stripbin, to_name, (char *)NULL);
714 err(EX_OSERR, "exec(%s)", stripbin);
716 if (wait(&status) == -1 || status) {
718 (void)unlink(to_name);
719 errc(EX_SOFTWARE, serrno, "wait");
727 * build directory heirarchy
730 install_dir(char *path)
737 if (!*p || (p != path && *p == '/')) {
740 if (stat(path, &sb)) {
741 if (errno != ENOENT || mkdir(path, 0755) < 0) {
742 err(EX_OSERR, "mkdir %s", path);
745 (void)printf("install: mkdir %s\n",
747 } else if (!S_ISDIR(sb.st_mode))
748 errx(EX_OSERR, "%s exists but is not a directory", path);
753 if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
754 warn("chown %u:%u %s", uid, gid, path);
755 if (chmod(path, mode))
756 warn("chmod %o %s", mode, path);
761 * print a usage message and die
766 (void)fprintf(stderr,
767 "usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
768 " [-o owner] file1 file2\n"
769 " install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
770 " [-o owner] file1 ... fileN directory\n"
771 " install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
778 * return true (1) if mmap should be tried, false (0) if not.
784 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
785 * pre-Lite2-merge systems.
790 if (nommap || fstatfs(fd, &stfs) != 0)
792 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
793 strcmp(stfs.f_fstypename, "cd9660") == 0)