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
29 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)xinstall.c 8.1 (Berkeley) 7/21/93
31 * $FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.38.2.8 2002/08/07 16:29:48 ru Exp $
34 #include <sys/param.h>
36 #include <sys/mount.h>
54 #include "pathnames.h"
56 /* Bootstrap aid - this doesn't exist in most older releases */
58 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
61 #define UF_NOHISTORY 0
64 #define MAX_CMP_SIZE (16 * 1024 * 1024)
66 #define DIRECTORY 0x01 /* Tell install it's a directory. */
67 #define SETFLAGS 0x02 /* Tell install to set flags. */
68 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
69 #define BACKUP_SUFFIX ".old"
75 int dobackup
, docompare
, dodir
, dopreserve
, dostrip
, nommap
, safecopy
, verbose
;
76 mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
77 const char *suffix
= BACKUP_SUFFIX
;
80 static int file_getgroup(const char *etcdir
, const char *group
, gid_t
*gidret
);
81 static int file_getowner(const char *etcdir
, const char *owner
, uid_t
*uidret
);
83 void copy(int, const char *, int, const char *, off_t
);
84 int compare(int, const char *, size_t, int, const char *, size_t);
85 int create_newfile(const char *, int, struct stat
*);
86 int create_tempfile(const char *, char *, size_t);
87 void install(const char *, const char *, u_long
, u_long
, u_int
);
88 void install_dir(char *);
89 u_long
numeric_id(const char *, const char *);
90 void strip(const char *);
95 main(int argc
, char **argv
)
97 struct stat from_sb
, to_sb
;
105 const char *group
, *owner
, *to_name
;
114 while ((ch
= getopt(argc
, argv
, "L:B:bCcdf:g:lMm:o:pSsv:D:")) != -1)
129 /* For backwards compatibility. */
139 if (strtofflags(&flags
, &fset
, &fclr
))
140 errx(EX_USAGE
, "%s: invalid flag", flags
);
153 if (!(set
= setmode(optarg
)))
154 errx(EX_USAGE
, "invalid file mode: %s",
156 mode
= getmode(set
, 0);
163 docompare
= dopreserve
= 1;
181 /* some options make no sense when creating directories */
182 if (dostrip
&& dodir
)
185 /* must have at least two arguments, except when creating directories */
186 if (argc
< 2 && !dodir
)
189 /* need to make a temp copy so we can compare stripped version */
190 if (docompare
&& dostrip
)
193 /* no etcdir specified, always try the system */
199 /* get group and owner id's */
201 if (etcdir
&& file_getgroup(etcdir
, group
, &gid
)) {
203 } else if (trysys
&& (gp
= getgrnam(group
)) != NULL
) {
206 gid
= (gid_t
)numeric_id(group
, "group");
211 if (etcdir
&& file_getowner(etcdir
, owner
, &uid
)) {
213 } else if (trysys
&& (pp
= getpwnam(owner
)) != NULL
) {
216 uid
= (uid_t
)numeric_id(owner
, "user");
221 for (; *argv
!= NULL
; ++argv
)
227 no_target
= stat(to_name
= argv
[argc
- 1], &to_sb
);
228 if (!no_target
&& S_ISDIR(to_sb
.st_mode
)) {
229 for (; *argv
!= to_name
; ++argv
)
230 install(*argv
, to_name
, fset
, fclr
, iflags
| DIRECTORY
);
235 /* can't do file1 file2 directory/file */
240 if (stat(*argv
, &from_sb
))
241 err(EX_OSERR
, "%s", *argv
);
242 if (!S_ISREG(to_sb
.st_mode
)) {
244 err(EX_OSERR
, "%s", to_name
);
246 if (to_sb
.st_dev
== from_sb
.st_dev
&&
247 to_sb
.st_ino
== from_sb
.st_ino
)
249 "%s and %s are the same file", *argv
, to_name
);
251 install(*argv
, to_name
, fset
, fclr
, iflags
);
257 numeric_id(const char *name
, const char *type
)
264 * We know that uid_t's and gid_t's are unsigned longs.
267 val
= strtoul(name
, &ep
, 10);
269 err(EX_NOUSER
, "%s", name
);
271 errx(EX_NOUSER
, "unknown %s %s", type
, name
);
277 file_getgroup(const char *etcdir
, const char *group
, gid_t
*gidret
)
286 grlen
= strlen(group
);
288 if (asprintf(&path
, "%s/group", etcdir
) < 0)
289 errx(EX_OSERR
, "asprintf()");
290 if ((fp
= fopen(path
, "r")) != NULL
) {
291 while ((ptr
= fgetln(fp
, &len
)) != NULL
&& len
) {
293 if ((scan
= strchr(ptr
, ':')) == NULL
)
295 if ((size_t)(scan
- ptr
) != grlen
)
297 if (strncmp(ptr
, group
, grlen
) != 0)
299 if ((scan
= strchr(scan
+ 1, ':')) == NULL
)
301 *gidret
= strtoul(scan
+ 1, NULL
, 10);
307 return((*gidret
== (gid_t
)-1) ? 0 : 1);
312 file_getowner(const char *etcdir
, const char *owner
, uid_t
*uidret
)
321 owner_len
= strlen(owner
);
323 if (asprintf(&path
, "%s/master.passwd", etcdir
) < 0)
324 errx(EX_OSERR
, "asprintf()");
325 if ((fp
= fopen(path
, "r")) != NULL
) {
326 while ((ptr
= fgetln(fp
, &len
)) != NULL
&& len
) {
328 if ((scan
= strchr(ptr
, ':')) == NULL
)
330 if ((size_t)(scan
- ptr
) != owner_len
)
332 if (strncmp(ptr
, owner
, owner_len
) != 0)
334 if ((scan
= strchr(scan
+ 1, ':')) == NULL
)
336 *uidret
= strtoul(scan
+ 1, NULL
, 10);
342 return((*uidret
== (uid_t
)-1) ? 0 : 1);
347 * build a path name and install the file
350 install(const char *from_name
, const char *to_name
, u_long fset
, u_long fclr
,
353 struct stat from_sb
, temp_sb
, to_sb
;
355 int devnull
, files_match
, from_fd
, serrno
, target
;
356 int tempcopy
, temp_fd
, to_fd
;
358 char backup
[MAXPATHLEN
], *p
, pathbuf
[MAXPATHLEN
], tempfile
[MAXPATHLEN
];
363 /* If try to install NULL file to a directory, fails. */
364 if (flags
& DIRECTORY
|| strcmp(from_name
, _PATH_DEVNULL
)) {
365 if (stat(from_name
, &from_sb
))
366 err(EX_OSERR
, "%s", from_name
);
367 if (!S_ISREG(from_sb
.st_mode
)) {
369 err(EX_OSERR
, "%s", from_name
);
371 /* Build the target path. */
372 if (flags
& DIRECTORY
) {
373 snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
375 (p
= strrchr(from_name
, '/')) ? ++p
: from_name
);
383 target
= stat(to_name
, &to_sb
) == 0;
385 /* Only install to regular files. */
386 if (target
&& !S_ISREG(to_sb
.st_mode
)) {
392 /* Only copy safe if the target exists. */
393 tempcopy
= safecopy
&& target
;
395 if (!devnull
&& (from_fd
= open(from_name
, O_RDONLY
, 0)) < 0)
396 err(EX_OSERR
, "%s", from_name
);
398 /* If we don't strip, we can compare first. */
399 if (docompare
&& !dostrip
&& target
) {
400 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
401 err(EX_OSERR
, "%s", to_name
);
403 files_match
= to_sb
.st_size
== 0;
405 files_match
= !(compare(from_fd
, from_name
,
406 (size_t)from_sb
.st_size
, to_fd
,
407 to_name
, (size_t)to_sb
.st_size
));
409 /* Close "to" file unless we match. */
416 to_fd
= create_tempfile(to_name
, tempfile
,
419 err(EX_OSERR
, "%s", tempfile
);
421 if ((to_fd
= create_newfile(to_name
, target
,
423 err(EX_OSERR
, "%s", to_name
);
425 printf("install: %s -> %s\n",
429 copy(from_fd
, from_name
, to_fd
,
430 tempcopy
? tempfile
: to_name
, from_sb
.st_size
);
434 strip(tempcopy
? tempfile
: to_name
);
437 * Re-open our fd on the target, in case we used a strip
438 * that does not work in-place -- like GNU binutils strip.
441 to_fd
= open(tempcopy
? tempfile
: to_name
, O_RDONLY
, 0);
443 err(EX_OSERR
, "stripping %s", to_name
);
447 * Compare the stripped temp file with the target.
449 if (docompare
&& dostrip
&& target
) {
452 /* Re-open to_fd using the real target name. */
453 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
454 err(EX_OSERR
, "%s", to_name
);
456 if (fstat(temp_fd
, &temp_sb
)) {
460 err(EX_OSERR
, "%s", tempfile
);
463 if (compare(temp_fd
, tempfile
, (size_t)temp_sb
.st_size
, to_fd
,
464 to_name
, (size_t)to_sb
.st_size
) == 0) {
466 * If target has more than one link we need to
467 * replace it in order to snap the extra links.
468 * Need to preserve target file times, though.
470 if (to_sb
.st_nlink
!= 1) {
471 utb
.actime
= to_sb
.st_atime
;
472 utb
.modtime
= to_sb
.st_mtime
;
473 utime(tempfile
, &utb
);
483 * Move the new file into place if doing a safe copy
484 * and the files are different (or just not compared).
486 if (tempcopy
&& !files_match
) {
487 /* Try to turn off the immutable bits. */
488 if (to_sb
.st_flags
& NOCHANGEBITS
)
489 chflags(to_name
, to_sb
.st_flags
& ~NOCHANGEBITS
);
491 if ((size_t)snprintf(backup
, MAXPATHLEN
, "%s%s", to_name
,
492 suffix
) != strlen(to_name
) + strlen(suffix
)) {
494 errx(EX_OSERR
, "%s: backup filename too long",
498 printf("install: %s -> %s\n", to_name
, backup
);
499 if (rename(to_name
, backup
) < 0) {
503 err(EX_OSERR
, "rename: %s to %s", to_name
,
508 printf("install: %s -> %s\n", from_name
, to_name
);
509 if (rename(tempfile
, to_name
) < 0) {
513 err(EX_OSERR
, "rename: %s to %s",
517 /* Re-open to_fd so we aren't hosed by the rename(2). */
519 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
520 err(EX_OSERR
, "%s", to_name
);
524 * Preserve the timestamp of the source file if necessary.
526 if (dopreserve
&& !files_match
&& !devnull
) {
527 utb
.actime
= from_sb
.st_atime
;
528 utb
.modtime
= from_sb
.st_mtime
;
529 utime(to_name
, &utb
);
532 if (fstat(to_fd
, &to_sb
) == -1) {
536 err(EX_OSERR
, "%s", to_name
);
540 * Set owner, group, mode for target; do the chown first,
541 * chown may lose the setuid bits.
543 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
544 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
) ||
545 (mode
!= to_sb
.st_mode
)) {
546 /* Try to turn off the immutable bits. */
547 if (to_sb
.st_flags
& NOCHANGEBITS
)
548 fchflags(to_fd
, to_sb
.st_flags
& ~NOCHANGEBITS
);
551 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
552 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
))
553 if (fchown(to_fd
, uid
, gid
) == -1) {
557 err(EX_OSERR
,"%s: chown/chgrp", to_name
);
560 if (mode
!= to_sb
.st_mode
)
561 if (fchmod(to_fd
, mode
)) {
565 err(EX_OSERR
, "%s: chmod", to_name
);
569 * If provided a set of flags, set them, otherwise, preserve the
570 * flags, except for the dump and history flags. The dump flag
571 * is left clear on the target while the history flag from when
572 * the target was created (which is inherited from the target's
573 * parent directory) is retained.
575 if (flags
& SETFLAGS
) {
576 nfset
= (to_sb
.st_flags
| fset
) & ~fclr
;
578 nfset
= (from_sb
.st_flags
& ~(UF_NODUMP
| UF_NOHISTORY
)) |
579 (to_sb
.st_flags
& UF_NOHISTORY
);
583 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
584 * trying to turn off UF_NODUMP. If we're trying to set real flags,
585 * then warn if the the fs doesn't support it, otherwise fail.
587 if (!devnull
&& fchflags(to_fd
, nfset
)) {
588 if (flags
& SETFLAGS
) {
589 if (errno
== EOPNOTSUPP
)
590 warn("%s: chflags", to_name
);
595 err(EX_OSERR
, "%s: chflags", to_name
);
607 * compare two files; non-zero means files differ
610 compare(int from_fd
, const char *from_name __unused
, size_t from_len
,
611 int to_fd
, const char *to_name __unused
, size_t to_len
)
618 if (from_len
!= to_len
)
621 if (from_len
<= MAX_CMP_SIZE
) {
623 if (trymmap(from_fd
) && trymmap(to_fd
)) {
624 p
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, from_fd
, (off_t
)0);
625 if (p
== (char *)MAP_FAILED
)
627 q
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, to_fd
, (off_t
)0);
628 if (q
== (char *)MAP_FAILED
) {
633 rv
= memcmp(p
, q
, from_len
);
645 lseek(from_fd
, 0, SEEK_SET
);
646 lseek(to_fd
, 0, SEEK_SET
);
648 n1
= read(from_fd
, buf1
, sizeof(buf1
));
652 n2
= read(to_fd
, buf2
, n1
);
654 rv
= memcmp(buf1
, buf2
, n1
);
656 rv
= 1; /* out of sync */
658 rv
= 1; /* read failure */
660 lseek(from_fd
, 0, SEEK_SET
);
661 lseek(to_fd
, 0, SEEK_SET
);
664 rv
= 1; /* don't bother in this case */
671 * create a temporary file based on path and open it
674 create_tempfile(const char *path
, char *temp
, size_t tsize
)
678 strncpy(temp
, path
, tsize
);
679 temp
[tsize
- 1] = '\0';
680 if ((p
= strrchr(temp
, '/')) != NULL
)
684 strncpy(p
, "INS@XXXX", &temp
[tsize
- 1] - p
);
685 temp
[tsize
- 1] = '\0';
686 return (mkstemp(temp
));
691 * create a new file, overwriting an existing one if necessary
694 create_newfile(const char *path
, int target
, struct stat
*sbp
)
696 char backup
[MAXPATHLEN
];
700 * Unlink now... avoid ETXTBSY errors later. Try to turn
701 * off the append/immutable bits -- if we fail, go ahead,
704 if (sbp
->st_flags
& NOCHANGEBITS
)
705 chflags(path
, sbp
->st_flags
& ~NOCHANGEBITS
);
708 if ((size_t)snprintf(backup
, MAXPATHLEN
, "%s%s",
709 path
, suffix
) != strlen(path
) + strlen(suffix
))
710 errx(EX_OSERR
, "%s: backup filename too long",
712 snprintf(backup
, MAXPATHLEN
, "%s%s",
715 printf("install: %s -> %s\n",
717 if (rename(path
, backup
) < 0)
718 err(EX_OSERR
, "rename: %s to %s", path
, backup
);
723 return (open(path
, O_CREAT
| O_RDWR
| O_TRUNC
, S_IRUSR
| S_IWUSR
));
728 * copy from one file to another
731 copy(int from_fd
, const char *from_name
, int to_fd
,
732 const char *to_name
, off_t size
)
736 char *p
, buf
[MAXBSIZE
];
739 /* Rewind file descriptors. */
740 if (lseek(from_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
741 err(EX_OSERR
, "lseek: %s", from_name
);
742 if (lseek(to_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
743 err(EX_OSERR
, "lseek: %s", to_name
);
746 * Mmap and write if less than 8M (the limit is so we don't totally
747 * trash memory on big files. This is really a minor hack, but it
748 * wins some CPU back.
751 if (size
<= 8 * 1048576 && trymmap(from_fd
) &&
752 (p
= mmap(NULL
, (size_t)size
, PROT_READ
, MAP_SHARED
,
753 from_fd
, (off_t
)0)) != (char *)MAP_FAILED
) {
754 if ((nw
= write(to_fd
, p
, size
)) != size
) {
757 errno
= nw
> 0 ? EIO
: serrno
;
758 err(EX_OSERR
, "%s", to_name
);
763 while ((nr
= read(from_fd
, buf
, sizeof(buf
))) > 0)
764 if ((nw
= write(to_fd
, buf
, nr
)) != nr
) {
767 errno
= nw
> 0 ? EIO
: serrno
;
768 err(EX_OSERR
, "%s", to_name
);
774 err(EX_OSERR
, "%s", from_name
);
781 * use strip(1) to strip the target file
784 strip(const char *to_name
)
786 const char *stripbin
;
794 err(EX_TEMPFAIL
, "fork");
796 stripbin
= getenv("STRIPBIN");
797 if (stripbin
== NULL
)
799 execlp(stripbin
, stripbin
, to_name
, NULL
);
800 err(EX_OSERR
, "exec(%s)", stripbin
);
802 if (wait(&status
) == -1 || status
) {
805 errc(EX_SOFTWARE
, serrno
, "wait");
812 * When doing a concurrent make -j N multiple install's can race the mkdir.
816 mkdir_race(const char *path
, int nmode
)
821 res
= mkdir(path
, nmode
);
822 if (res
< 0 && errno
== EEXIST
) {
823 if (stat(path
, &sb
) == 0 && S_ISDIR(sb
.st_mode
))
825 res
= mkdir(path
, nmode
);
832 * build directory hierarchy
835 install_dir(char *path
)
842 if (!*p
|| (p
!= path
&& *p
== '/')) {
845 if (stat(path
, &sb
)) {
846 if (errno
!= ENOENT
||
847 mkdir_race(path
, 0755) < 0) {
848 err(EX_OSERR
, "mkdir %s", path
);
851 printf("install: mkdir %s\n",
853 } else if (!S_ISDIR(sb
.st_mode
))
854 errx(EX_OSERR
, "%s exists but is not a directory", path
);
859 if ((gid
!= (gid_t
)-1 || uid
!= (uid_t
)-1) && chown(path
, uid
, gid
))
860 warn("chown %u:%u %s", uid
, gid
, path
);
861 if (chmod(path
, mode
))
862 warn("chmod %o %s", mode
, path
);
867 * print a usage message and die
873 usage: install [-bCcpSsv] [-B suffix] [-D dest] [-f flags] [-g group] [-m mode]\n\
874 [-o owner] file1 file2\n\
875 install [-bCcpSsv] [-B suffix] [-D dest] [-f flags] [-g group] [-m mode]\n\
876 [-o owner] file1 ... fileN directory\n\
877 install -d [-v] [-D dest] [-g group] [-m mode] [-o owner] directory ...\n");
884 * return true (1) if mmap should be tried, false (0) if not.
890 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
891 * pre-Lite2-merge systems.
896 if (nommap
|| fstatfs(fd
, &stfs
) != 0)
898 if (strcmp(stfs
.f_fstypename
, "ufs") == 0 ||
899 strcmp(stfs
.f_fstypename
, "cd9660") == 0)