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
[] = "From: @(#)xinstall.c 8.1 (Berkeley) 7/21/93";
44 static const char rcsid
[] =
45 "$FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.43 2001/05/30 07:08:49 ru Exp $";
48 #include <sys/param.h>
52 #include <sys/mount.h>
68 #include "pathnames.h"
70 /* Bootstrap aid - this doesn't exist in most older releases */
72 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
75 #define DIRECTORY 0x01 /* Tell install it's a directory. */
76 #define SETFLAGS 0x02 /* Tell install to set flags. */
77 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
78 #define BACKUP_SUFFIX ".old"
84 int dobackup
, docompare
, dodir
, dopreserve
, dostrip
, nommap
, safecopy
, verbose
;
85 mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
86 char *suffix
= BACKUP_SUFFIX
;
88 void copy
__P((int, char *, int, char *, off_t
));
89 int compare
__P((int, const char *, size_t, int, const char *, size_t));
90 int create_newfile
__P((char *, int, struct stat
*));
91 int create_tempfile
__P((char *, char *, size_t));
92 void install
__P((char *, char *, u_long
, u_int
));
93 void install_dir
__P((char *));
94 u_long numeric_id
__P((char *, char *));
95 void strip
__P((char *));
96 int trymmap
__P((int));
97 void usage
__P((void));
104 struct stat from_sb
, to_sb
;
109 char *flags
, *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 ((safecopy
|| dostrip
) && dodir
)
176 * Older versions allowed -d -C combo. Issue a warning
177 * for now, but turn this into an error before 4.5-RELEASE.
179 if (docompare
&& dodir
)
180 warnx("the -d and -C options may not be specified together");
182 /* must have at least two arguments, except when creating directories */
183 if (argc
< 2 && !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
= (uid_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 */
227 if (stat(*argv
, &from_sb
))
228 err(EX_OSERR
, "%s", *argv
);
229 if (!S_ISREG(to_sb
.st_mode
)) {
231 err(EX_OSERR
, "%s", to_name
);
233 if (to_sb
.st_dev
== from_sb
.st_dev
&&
234 to_sb
.st_ino
== from_sb
.st_ino
)
236 "%s and %s are the same file", *argv
, to_name
);
238 install(*argv
, to_name
, fset
, iflags
);
244 numeric_id(name
, type
)
252 * We know that uid_t's and gid_t's are unsigned longs.
255 val
= strtoul(name
, &ep
, 10);
257 err(EX_NOUSER
, "%s", name
);
259 errx(EX_NOUSER
, "unknown %s %s", type
, name
);
265 * build a path name and install the file
268 install(from_name
, to_name
, fset
, flags
)
269 char *from_name
, *to_name
;
273 struct stat from_sb
, temp_sb
, to_sb
;
275 int devnull
, files_match
, from_fd
=0, serrno
, target
;
276 int tempcopy
, temp_fd
, to_fd
=0;
277 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 utb
.actime
= to_sb
.st_atime
;
390 utb
.modtime
= to_sb
.st_mtime
;
391 (void)utime(tempfile
, &utb
);
394 (void)unlink(tempfile
);
396 (void) close(temp_fd
);
401 * Move the new file into place if doing a safe copy
402 * and the files are different (or just not compared).
404 if (tempcopy
&& !files_match
) {
405 /* Try to turn off the immutable bits. */
406 if (to_sb
.st_flags
& NOCHANGEBITS
)
407 (void)chflags(to_name
, to_sb
.st_flags
& ~NOCHANGEBITS
);
409 if (snprintf(backup
, MAXPATHLEN
, "%s%s", to_name
,
410 suffix
) != strlen(to_name
) + strlen(suffix
)) {
412 errx(EX_OSERR
, "%s: backup filename too long",
416 (void)printf("install: %s -> %s\n", to_name
, backup
);
417 if (rename(to_name
, backup
) < 0) {
421 err(EX_OSERR
, "rename: %s to %s", to_name
,
426 (void)printf("install: %s -> %s\n", from_name
, to_name
);
427 if (rename(tempfile
, to_name
) < 0) {
431 err(EX_OSERR
, "rename: %s to %s",
435 /* Re-open to_fd so we aren't hosed by the rename(2). */
437 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
438 err(EX_OSERR
, "%s", to_name
);
442 * Preserve the timestamp of the source file if necessary.
444 if (dopreserve
&& !files_match
&& !devnull
) {
445 utb
.actime
= from_sb
.st_atime
;
446 utb
.modtime
= from_sb
.st_mtime
;
447 (void)utime(to_name
, &utb
);
450 if (fstat(to_fd
, &to_sb
) == -1) {
452 (void)unlink(to_name
);
454 err(EX_OSERR
, "%s", to_name
);
458 * Set owner, group, mode for target; do the chown first,
459 * chown may lose the setuid bits.
461 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
462 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
) ||
463 (mode
!= to_sb
.st_mode
)) {
464 /* Try to turn off the immutable bits. */
465 if (to_sb
.st_flags
& NOCHANGEBITS
)
466 (void)fchflags(to_fd
, to_sb
.st_flags
& ~NOCHANGEBITS
);
469 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
470 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
))
471 if (fchown(to_fd
, uid
, gid
) == -1) {
473 (void)unlink(to_name
);
475 err(EX_OSERR
,"%s: chown/chgrp", to_name
);
478 if (mode
!= to_sb
.st_mode
)
479 if (fchmod(to_fd
, mode
)) {
481 (void)unlink(to_name
);
483 err(EX_OSERR
, "%s: chmod", to_name
);
487 * If provided a set of flags, set them, otherwise, preserve the
488 * flags, except for the dump flag.
489 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
490 * trying to turn off UF_NODUMP. If we're trying to set real flags,
491 * then warn if the the fs doesn't support it, otherwise fail.
493 if (!devnull
&& fchflags(to_fd
,
494 flags
& SETFLAGS ? fset
: from_sb
.st_flags
& ~UF_NODUMP
)) {
495 if (flags
& SETFLAGS
) {
496 if (errno
== EOPNOTSUPP
)
497 warn("%s: chflags", to_name
);
500 (void)unlink(to_name
);
502 err(EX_OSERR
, "%s: chflags", to_name
);
509 (void)close(from_fd
);
514 * compare two files; non-zero means files differ
517 compare(int from_fd
, const char *from_name
, size_t from_len
,
518 int to_fd
, const char *to_name
, size_t to_len
)
525 if (from_len
!= to_len
)
528 if (from_len
<= 8 * 1024 * 1024) {
530 if (trymmap(from_fd
) && trymmap(to_fd
)) {
531 p
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, from_fd
, (off_t
)0);
532 if (p
== (char *)MAP_FAILED
)
534 q
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, to_fd
, (off_t
)0);
535 if (q
== (char *)MAP_FAILED
) {
540 rv
= memcmp(p
, q
, from_len
);
552 lseek(from_fd
, 0, SEEK_SET
);
553 lseek(to_fd
, 0, SEEK_SET
);
555 n1
= read(from_fd
, buf1
, sizeof(buf1
));
559 n2
= read(to_fd
, buf2
, n1
);
561 rv
= memcmp(buf1
, buf2
, n1
);
563 rv
= 1; /* out of sync */
565 rv
= 1; /* read failure */
567 lseek(from_fd
, 0, SEEK_SET
);
568 lseek(to_fd
, 0, SEEK_SET
);
571 rv
= 1; /* don't bother in this case */
578 * create a temporary file based on path and open it
581 create_tempfile(path
, temp
, tsize
)
588 (void)strncpy(temp
, path
, tsize
);
589 temp
[tsize
- 1] = '\0';
590 if ((p
= strrchr(temp
, '/')) != NULL
)
594 (void)strncpy(p
, "INS@XXXX", &temp
[tsize
- 1] - p
);
595 temp
[tsize
- 1] = '\0';
596 return (mkstemp(temp
));
601 * create a new file, overwriting an existing one if necessary
604 create_newfile(path
, target
, sbp
)
609 char backup
[MAXPATHLEN
];
613 * Unlink now... avoid ETXTBSY errors later. Try to turn
614 * off the append/immutable bits -- if we fail, go ahead,
617 if (sbp
->st_flags
& NOCHANGEBITS
)
618 (void)chflags(path
, sbp
->st_flags
& ~NOCHANGEBITS
);
621 if (snprintf(backup
, MAXPATHLEN
, "%s%s",
622 path
, suffix
) != strlen(path
) + strlen(suffix
))
623 errx(EX_OSERR
, "%s: backup filename too long",
625 (void)snprintf(backup
, MAXPATHLEN
, "%s%s",
628 (void)printf("install: %s -> %s\n",
630 if (rename(path
, backup
) < 0)
631 err(EX_OSERR
, "rename: %s to %s", path
, backup
);
636 return (open(path
, O_CREAT
| O_RDWR
| O_TRUNC
, S_IRUSR
| S_IWUSR
));
641 * copy from one file to another
644 copy(from_fd
, from_name
, to_fd
, to_name
, size
)
645 register int from_fd
, to_fd
;
646 char *from_name
, *to_name
;
651 char *p
, buf
[MAXBSIZE
];
654 /* Rewind file descriptors. */
655 if (lseek(from_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
656 err(EX_OSERR
, "lseek: %s", from_name
);
657 if (lseek(to_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
658 err(EX_OSERR
, "lseek: %s", to_name
);
661 * Mmap and write if less than 8M (the limit is so we don't totally
662 * trash memory on big files. This is really a minor hack, but it
663 * wins some CPU back.
666 if (size
<= 8 * 1048576 && trymmap(from_fd
) &&
667 (p
= mmap(NULL
, (size_t)size
, PROT_READ
, MAP_SHARED
,
668 from_fd
, (off_t
)0)) != (char *)MAP_FAILED
) {
669 if ((nw
= write(to_fd
, p
, size
)) != size
) {
671 (void)unlink(to_name
);
672 errno
= nw
> 0 ? EIO
: serrno
;
673 err(EX_OSERR
, "%s", to_name
);
678 while ((nr
= read(from_fd
, buf
, sizeof(buf
))) > 0)
679 if ((nw
= write(to_fd
, buf
, nr
)) != nr
) {
681 (void)unlink(to_name
);
682 errno
= nw
> 0 ? EIO
: serrno
;
683 err(EX_OSERR
, "%s", to_name
);
687 (void)unlink(to_name
);
689 err(EX_OSERR
, "%s", from_name
);
696 * use strip(1) to strip the target file
707 (void)unlink(to_name
);
709 err(EX_TEMPFAIL
, "fork");
711 execlp("strip", "strip", to_name
, NULL
);
712 err(EX_OSERR
, "exec(strip)");
714 if (wait(&status
) == -1 || status
) {
715 (void)unlink(to_name
);
724 * build directory heirarchy
735 if (!*p
|| (p
!= path
&& *p
== '/')) {
738 if (stat(path
, &sb
)) {
739 if (errno
!= ENOENT
|| mkdir(path
, 0755) < 0) {
740 err(EX_OSERR
, "mkdir %s", path
);
743 (void)printf("install: mkdir %s\n",
745 } else if (!S_ISDIR(sb
.st_mode
))
746 errx(EX_OSERR
, "%s exists but is not a directory", path
);
751 if ((gid
!= (gid_t
)-1 || uid
!= (uid_t
)-1) && chown(path
, uid
, gid
))
752 warn("chown %u:%u %s", uid
, gid
, path
);
753 if (chmod(path
, mode
))
754 warn("chmod %o %s", mode
, path
);
759 * print a usage message and die
764 (void)fprintf(stderr
, "\
765 usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
766 [-o owner] file1 file2\n\
767 install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
768 [-o owner] file1 ... fileN directory\n\
769 install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
776 * return true (1) if mmap should be tried, false (0) if not.
783 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
784 * pre-Lite2-merge systems.
789 if (nommap
|| fstatfs(fd
, &stfs
) != 0)
791 if (strcmp(stfs
.f_fstypename
, "ufs") == 0 ||
792 strcmp(stfs
.f_fstypename
, "cd9660") == 0)