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
33 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)xinstall.c 8.1 (Berkeley) 7/21/93
35 * $FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.38.2.8 2002/08/07 16:29:48 ru Exp $
36 * $DragonFly: src/usr.bin/xinstall/xinstall.c,v 1.3 2003/10/04 20:36:55 hmp Exp $
39 #include <sys/param.h>
41 #include <sys/mount.h>
59 #include "pathnames.h"
61 /* Bootstrap aid - this doesn't exist in most older releases */
63 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
66 #define MAX_CMP_SIZE (16 * 1024 * 1024)
68 #define DIRECTORY 0x01 /* Tell install it's a directory. */
69 #define SETFLAGS 0x02 /* Tell install to set flags. */
70 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
71 #define BACKUP_SUFFIX ".old"
77 int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
78 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
79 const char *suffix = BACKUP_SUFFIX;
81 void copy(int, const char *, int, const char *, off_t);
82 int compare(int, const char *, size_t, int, const char *, size_t);
83 int create_newfile(const char *, int, struct stat *);
84 int create_tempfile(const char *, char *, size_t);
85 void install(const char *, const char *, u_long, u_int);
86 void install_dir(char *);
87 u_long numeric_id(const char *, const char *);
88 void strip(const char *);
93 main(int argc, char **argv)
95 struct stat from_sb, to_sb;
101 const char *group, *owner, *to_name;
104 group = owner = NULL;
105 while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:o:pSsv")) != -1)
117 /* For backwards compatibility. */
124 if (strtofflags(&flags, &fset, NULL))
125 errx(EX_USAGE, "%s: invalid flag", flags);
135 if (!(set = setmode(optarg)))
136 errx(EX_USAGE, "invalid file mode: %s",
138 mode = getmode(set, 0);
145 docompare = dopreserve = 1;
163 /* some options make no sense when creating directories */
164 if (dostrip && dodir)
167 /* must have at least two arguments, except when creating directories */
168 if (argc < 2 && !dodir)
171 /* need to make a temp copy so we can compare stripped version */
172 if (docompare && dostrip)
175 /* get group and owner id's */
177 if ((gp = getgrnam(group)) != NULL)
180 gid = (gid_t)numeric_id(group, "group");
185 if ((pp = getpwnam(owner)) != NULL)
188 uid = (uid_t)numeric_id(owner, "user");
193 for (; *argv != NULL; ++argv)
199 no_target = stat(to_name = argv[argc - 1], &to_sb);
200 if (!no_target && S_ISDIR(to_sb.st_mode)) {
201 for (; *argv != to_name; ++argv)
202 install(*argv, to_name, fset, iflags | DIRECTORY);
207 /* can't do file1 file2 directory/file */
212 if (stat(*argv, &from_sb))
213 err(EX_OSERR, "%s", *argv);
214 if (!S_ISREG(to_sb.st_mode)) {
216 err(EX_OSERR, "%s", to_name);
218 if (to_sb.st_dev == from_sb.st_dev &&
219 to_sb.st_ino == from_sb.st_ino)
221 "%s and %s are the same file", *argv, to_name);
223 install(*argv, to_name, fset, iflags);
229 numeric_id(const char *name, const char *type)
236 * We know that uid_t's and gid_t's are unsigned longs.
239 val = strtoul(name, &ep, 10);
241 err(EX_NOUSER, "%s", name);
243 errx(EX_NOUSER, "unknown %s %s", type, name);
249 * build a path name and install the file
252 install(const char *from_name, const char *to_name, u_long fset, u_int flags)
254 struct stat from_sb, temp_sb, to_sb;
256 int devnull, files_match, from_fd, serrno, target;
257 int tempcopy, temp_fd, to_fd;
258 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
262 /* If try to install NULL file to a directory, fails. */
263 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
264 if (stat(from_name, &from_sb))
265 err(EX_OSERR, "%s", from_name);
266 if (!S_ISREG(from_sb.st_mode)) {
268 err(EX_OSERR, "%s", from_name);
270 /* Build the target path. */
271 if (flags & DIRECTORY) {
272 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
274 (p = strrchr(from_name, '/')) ? ++p : from_name);
282 target = stat(to_name, &to_sb) == 0;
284 /* Only install to regular files. */
285 if (target && !S_ISREG(to_sb.st_mode)) {
291 /* Only copy safe if the target exists. */
292 tempcopy = safecopy && target;
294 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
295 err(EX_OSERR, "%s", from_name);
297 /* If we don't strip, we can compare first. */
298 if (docompare && !dostrip && target) {
299 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
300 err(EX_OSERR, "%s", to_name);
302 files_match = to_sb.st_size == 0;
304 files_match = !(compare(from_fd, from_name,
305 (size_t)from_sb.st_size, to_fd,
306 to_name, (size_t)to_sb.st_size));
308 /* Close "to" file unless we match. */
315 to_fd = create_tempfile(to_name, tempfile,
318 err(EX_OSERR, "%s", tempfile);
320 if ((to_fd = create_newfile(to_name, target,
322 err(EX_OSERR, "%s", to_name);
324 (void)printf("install: %s -> %s\n",
328 copy(from_fd, from_name, to_fd,
329 tempcopy ? tempfile : to_name, from_sb.st_size);
333 strip(tempcopy ? tempfile : to_name);
336 * Re-open our fd on the target, in case we used a strip
337 * that does not work in-place -- like GNU binutils strip.
340 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
342 err(EX_OSERR, "stripping %s", to_name);
346 * Compare the stripped temp file with the target.
348 if (docompare && dostrip && target) {
351 /* Re-open to_fd using the real target name. */
352 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
353 err(EX_OSERR, "%s", to_name);
355 if (fstat(temp_fd, &temp_sb)) {
357 (void)unlink(tempfile);
359 err(EX_OSERR, "%s", tempfile);
362 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
363 to_name, (size_t)to_sb.st_size) == 0) {
365 * If target has more than one link we need to
366 * replace it in order to snap the extra links.
367 * Need to preserve target file times, though.
369 if (to_sb.st_nlink != 1) {
370 utb.actime = to_sb.st_atime;
371 utb.modtime = to_sb.st_mtime;
372 (void)utime(tempfile, &utb);
375 (void)unlink(tempfile);
377 (void) close(temp_fd);
382 * Move the new file into place if doing a safe copy
383 * and the files are different (or just not compared).
385 if (tempcopy && !files_match) {
386 /* Try to turn off the immutable bits. */
387 if (to_sb.st_flags & NOCHANGEBITS)
388 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
390 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
391 suffix) != strlen(to_name) + strlen(suffix)) {
393 errx(EX_OSERR, "%s: backup filename too long",
397 (void)printf("install: %s -> %s\n", to_name, backup);
398 if (rename(to_name, backup) < 0) {
402 err(EX_OSERR, "rename: %s to %s", to_name,
407 (void)printf("install: %s -> %s\n", from_name, to_name);
408 if (rename(tempfile, to_name) < 0) {
412 err(EX_OSERR, "rename: %s to %s",
416 /* Re-open to_fd so we aren't hosed by the rename(2). */
418 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
419 err(EX_OSERR, "%s", to_name);
423 * Preserve the timestamp of the source file if necessary.
425 if (dopreserve && !files_match && !devnull) {
426 utb.actime = from_sb.st_atime;
427 utb.modtime = from_sb.st_mtime;
428 (void)utime(to_name, &utb);
431 if (fstat(to_fd, &to_sb) == -1) {
433 (void)unlink(to_name);
435 err(EX_OSERR, "%s", to_name);
439 * Set owner, group, mode for target; do the chown first,
440 * chown may lose the setuid bits.
442 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
443 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
444 (mode != to_sb.st_mode)) {
445 /* Try to turn off the immutable bits. */
446 if (to_sb.st_flags & NOCHANGEBITS)
447 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
450 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
451 (uid != (uid_t)-1 && uid != to_sb.st_uid))
452 if (fchown(to_fd, uid, gid) == -1) {
454 (void)unlink(to_name);
456 err(EX_OSERR,"%s: chown/chgrp", to_name);
459 if (mode != to_sb.st_mode)
460 if (fchmod(to_fd, mode)) {
462 (void)unlink(to_name);
464 err(EX_OSERR, "%s: chmod", to_name);
468 * If provided a set of flags, set them, otherwise, preserve the
469 * flags, except for the dump flag.
470 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
471 * trying to turn off UF_NODUMP. If we're trying to set real flags,
472 * then warn if the the fs doesn't support it, otherwise fail.
474 if (!devnull && fchflags(to_fd,
475 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
476 if (flags & SETFLAGS) {
477 if (errno == EOPNOTSUPP)
478 warn("%s: chflags", to_name);
481 (void)unlink(to_name);
483 err(EX_OSERR, "%s: chflags", to_name);
490 (void)close(from_fd);
495 * compare two files; non-zero means files differ
498 compare(int from_fd, const char *from_name __unused, size_t from_len,
499 int to_fd, const char *to_name __unused, size_t to_len)
506 if (from_len != to_len)
509 if (from_len <= MAX_CMP_SIZE) {
511 if (trymmap(from_fd) && trymmap(to_fd)) {
512 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
513 if (p == (char *)MAP_FAILED)
515 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
516 if (q == (char *)MAP_FAILED) {
521 rv = memcmp(p, q, from_len);
533 lseek(from_fd, 0, SEEK_SET);
534 lseek(to_fd, 0, SEEK_SET);
536 n1 = read(from_fd, buf1, sizeof(buf1));
540 n2 = read(to_fd, buf2, n1);
542 rv = memcmp(buf1, buf2, n1);
544 rv = 1; /* out of sync */
546 rv = 1; /* read failure */
548 lseek(from_fd, 0, SEEK_SET);
549 lseek(to_fd, 0, SEEK_SET);
552 rv = 1; /* don't bother in this case */
559 * create a temporary file based on path and open it
562 create_tempfile(const char *path, char *temp, size_t tsize)
566 (void)strncpy(temp, path, tsize);
567 temp[tsize - 1] = '\0';
568 if ((p = strrchr(temp, '/')) != NULL)
572 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
573 temp[tsize - 1] = '\0';
574 return (mkstemp(temp));
579 * create a new file, overwriting an existing one if necessary
582 create_newfile(const char *path, int target, struct stat *sbp)
584 char backup[MAXPATHLEN];
588 * Unlink now... avoid ETXTBSY errors later. Try to turn
589 * off the append/immutable bits -- if we fail, go ahead,
592 if (sbp->st_flags & NOCHANGEBITS)
593 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
596 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
597 path, suffix) != strlen(path) + strlen(suffix))
598 errx(EX_OSERR, "%s: backup filename too long",
600 (void)snprintf(backup, MAXPATHLEN, "%s%s",
603 (void)printf("install: %s -> %s\n",
605 if (rename(path, backup) < 0)
606 err(EX_OSERR, "rename: %s to %s", path, backup);
611 return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
616 * copy from one file to another
619 copy(register int from_fd, const char *from_name, register int to_fd,
620 const char *to_name, off_t size)
624 char *p, buf[MAXBSIZE];
627 /* Rewind file descriptors. */
628 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
629 err(EX_OSERR, "lseek: %s", from_name);
630 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
631 err(EX_OSERR, "lseek: %s", to_name);
634 * Mmap and write if less than 8M (the limit is so we don't totally
635 * trash memory on big files. This is really a minor hack, but it
636 * wins some CPU back.
639 if (size <= 8 * 1048576 && trymmap(from_fd) &&
640 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
641 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
642 if ((nw = write(to_fd, p, size)) != size) {
644 (void)unlink(to_name);
645 errno = nw > 0 ? EIO : serrno;
646 err(EX_OSERR, "%s", to_name);
651 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
652 if ((nw = write(to_fd, buf, nr)) != nr) {
654 (void)unlink(to_name);
655 errno = nw > 0 ? EIO : serrno;
656 err(EX_OSERR, "%s", to_name);
660 (void)unlink(to_name);
662 err(EX_OSERR, "%s", from_name);
669 * use strip(1) to strip the target file
672 strip(const char *to_name)
674 const char *stripbin;
680 (void)unlink(to_name);
682 err(EX_TEMPFAIL, "fork");
684 stripbin = getenv("STRIPBIN");
685 if (stripbin == NULL)
687 execlp(stripbin, stripbin, to_name, (char *)NULL);
688 err(EX_OSERR, "exec(%s)", stripbin);
690 if (wait(&status) == -1 || status) {
692 (void)unlink(to_name);
693 errc(EX_SOFTWARE, serrno, "wait");
701 * build directory heirarchy
704 install_dir(char *path)
711 if (!*p || (p != path && *p == '/')) {
714 if (stat(path, &sb)) {
715 if (errno != ENOENT || mkdir(path, 0755) < 0) {
716 err(EX_OSERR, "mkdir %s", path);
719 (void)printf("install: mkdir %s\n",
721 } else if (!S_ISDIR(sb.st_mode))
722 errx(EX_OSERR, "%s exists but is not a directory", path);
727 if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
728 warn("chown %u:%u %s", uid, gid, path);
729 if (chmod(path, mode))
730 warn("chmod %o %s", mode, path);
735 * print a usage message and die
740 (void)fprintf(stderr, "\
741 usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
742 [-o owner] file1 file2\n\
743 install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
744 [-o owner] file1 ... fileN directory\n\
745 install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
752 * return true (1) if mmap should be tried, false (0) if not.
758 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
759 * pre-Lite2-merge systems.
764 if (nommap || fstatfs(fd, &stfs) != 0)
766 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
767 strcmp(stfs.f_fstypename, "cd9660") == 0)