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";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.66 2005/01/25 14:34:57 ssouhlal Exp $");
50 # include <sys/param.h>
52 # include <sys/mman.h>
54 # include <sys/mount.h>
55 # include <sys/wait.h>
56 # include <sys/time.h>
57 #endif /* !_MSC_VER */
72 #if defined(__EMX__) || defined(_MSC_VER)
77 # include "solfakes.h"
80 # include "mscfakes.h"
82 #include "kmkbuiltin.h"
85 extern void * bsd_setmode(const char *p
);
86 extern mode_t
bsd_getmode(const void *bbox
, mode_t omode
);
93 # define MAXBSIZE 0x20000
96 /* Bootstrap aid - this doesn't exist in most older releases */
98 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
101 #define MAX_CMP_SIZE (16 * 1024 * 1024)
103 #define DIRECTORY 0x01 /* Tell install it's a directory. */
104 #define SETFLAGS 0x02 /* Tell install to set flags. */
105 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
106 #define BACKUP_SUFFIX ".old"
113 # define EFTYPE EINVAL
116 #if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
117 # define IS_SLASH(ch) ((ch) == '/' || (ch) == '\\')
119 # define IS_SLASH(ch) ((ch) == '/')
124 static int dobackup
, docompare
, dodir
, dopreserve
, dostrip
, nommap
, safecopy
, verbose
;
125 static mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
126 static const char *suffix
= BACKUP_SUFFIX
;
128 static struct option long_options
[] =
130 { "help", no_argument
, 0, 261 },
131 { "version", no_argument
, 0, 262 },
136 static int copy(int, const char *, int, const char *, off_t
);
137 static int compare(int, const char *, size_t, int, const char *, size_t);
138 static int create_newfile(const char *, int, struct stat
*);
139 static int create_tempfile(const char *, char *, size_t);
140 static int install(const char *, const char *, u_long
, u_int
);
141 static int install_dir(char *);
142 static u_long
numeric_id(const char *, const char *);
143 static int strip(const char *);
145 static int trymmap(int);
147 static int usage(FILE *);
148 static char *last_slash(const char *);
151 kmk_builtin_install(int argc
, char *argv
[], char **envp
)
153 struct stat from_sb
, to_sb
;
159 const char *group
, *owner
, *to_name
;
161 /* reinitialize globals */
162 mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
163 suffix
= BACKUP_SUFFIX
;
166 dobackup
= docompare
= dodir
= dopreserve
= dostrip
= nommap
= safecopy
= verbose
= 0;
168 /* reset getopt and set progname. */
169 g_progname
= argv
[0];
173 optind
= 0; /* init */
176 group
= owner
= NULL
;
177 while ((ch
= getopt_long(argc
, argv
, "B:bCcdf:g:Mm:o:pSsv", long_options
, NULL
)) != -1)
189 /* For backwards compatibility. */
197 if (strtofflags(&flags
, &fset
, NULL
))
198 return errx(EX_USAGE
, "%s: invalid flag", flags
);
211 if (!(set
= bsd_setmode(optarg
)))
212 return errx(EX_USAGE
, "invalid file mode: %s",
214 mode
= bsd_getmode(set
, 0);
221 docompare
= dopreserve
= 1;
236 return kbuild_version(argv
[0]);
239 return usage(stderr
);
244 /* some options make no sense when creating directories */
245 if (dostrip
&& dodir
) {
246 warnx("-d and -s may not be specified together");
247 return usage(stderr
);
250 /* must have at least two arguments, except when creating directories */
251 if (argc
== 0 || (argc
== 1 && !dodir
))
252 return usage(stderr
);
254 /* need to make a temp copy so we can compare stripped version */
255 if (docompare
&& dostrip
)
258 /* get group and owner id's */
262 if ((gp
= getgrnam(group
)) != NULL
)
267 gid
= (gid_t
)numeric_id(group
, "group");
268 if (gid
== (gid_t
)-1)
277 if ((pp
= getpwnam(owner
)) != NULL
)
282 uid
= (uid_t
)numeric_id(owner
, "user");
283 if (uid
== (uid_t
)-1)
290 for (; *argv
!= NULL
; ++argv
) {
291 int rc
= install_dir(*argv
);
299 no_target
= stat(to_name
= argv
[argc
- 1], &to_sb
);
300 if (!no_target
&& S_ISDIR(to_sb
.st_mode
)) {
301 for (; *argv
!= to_name
; ++argv
) {
302 int rc
= install(*argv
, to_name
, fset
, iflags
| DIRECTORY
);
309 /* can't do file1 file2 directory/file */
311 warnx("wrong number or types of arguments");
312 return usage(stderr
);
316 if (stat(*argv
, &from_sb
))
317 return err(EX_OSERR
, "%s", *argv
);
318 if (!S_ISREG(to_sb
.st_mode
)) {
320 return err(EX_OSERR
, "%s", to_name
);
322 if (to_sb
.st_dev
== from_sb
.st_dev
&&
324 to_sb
.st_ino
== from_sb
.st_ino
&&
326 return errx(EX_USAGE
,
327 "%s and %s are the same file", *argv
, to_name
);
329 return install(*argv
, to_name
, fset
, iflags
);
333 numeric_id(const char *name
, const char *type
)
340 * We know that uid_t's and gid_t's are unsigned longs.
343 val
= strtoul(name
, &ep
, 10);
345 return err(-1, "%s", name
);
347 return errx(-1, "unknown %s %s", type
, name
);
353 * build a path name and install the file
356 install(const char *from_name
, const char *to_name
, u_long fset
, u_int flags
)
358 struct stat from_sb
, temp_sb
, to_sb
;
359 struct timeval tvb
[2];
360 int devnull
, files_match
, from_fd
, serrno
, target
;
361 int tempcopy
, temp_fd
, to_fd
;
362 char backup
[MAXPATHLEN
], *p
, pathbuf
[MAXPATHLEN
], tempfile
[MAXPATHLEN
];
370 /* If try to install NULL file to a directory, fails. */
371 if (flags
& DIRECTORY
372 #if defined(__EMX__) || defined(_MSC_VER)
373 || ( stricmp(from_name
, _PATH_DEVNULL
)
374 && stricmp(from_name
, "nul")
376 && stricmp(from_name
, "/dev/nul")
380 || strcmp(from_name
, _PATH_DEVNULL
)
383 if (stat(from_name
, &from_sb
))
384 return err(EX_OSERR
, "%s", from_name
);
385 if (!S_ISREG(from_sb
.st_mode
)) {
387 return err(EX_OSERR
, "%s", from_name
);
389 /* Build the target path. */
390 if (flags
& DIRECTORY
) {
391 (void)snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
393 (p
= last_slash(from_name
)) ? ++p
: from_name
);
401 target
= stat(to_name
, &to_sb
) == 0;
403 /* Only install to regular files. */
404 if (target
&& !S_ISREG(to_sb
.st_mode
)) {
410 /* Only copy safe if the target exists. */
411 tempcopy
= safecopy
&& target
;
413 if (!devnull
&& (from_fd
= open(from_name
, O_RDONLY
| O_BINARY
, 0)) < 0)
414 return err(EX_OSERR
, "%s", from_name
);
416 /* If we don't strip, we can compare first. */
417 if (docompare
&& !dostrip
&& target
) {
418 if ((to_fd
= open(to_name
, O_RDONLY
| O_BINARY
, 0)) < 0) {
419 rc
= err(EX_OSERR
, "%s", to_name
);
423 files_match
= to_sb
.st_size
== 0;
425 files_match
= !(compare(from_fd
, from_name
,
426 (size_t)from_sb
.st_size
, to_fd
,
427 to_name
, (size_t)to_sb
.st_size
));
429 /* Close "to" file unless we match. */
438 to_fd
= create_tempfile(to_name
, tempfile
,
441 rc
= err(EX_OSERR
, "%s", tempfile
);
445 if ((to_fd
= create_newfile(to_name
, target
,
447 rc
= err(EX_OSERR
, "%s", to_name
);
451 (void)printf("install: %s -> %s\n",
455 rc
= copy(from_fd
, from_name
, to_fd
,
456 tempcopy
? tempfile
: to_name
, from_sb
.st_size
);
463 #if defined(__EMX__) || defined(_MSC_VER)
464 /* close before we strip. */
468 rc
= strip(tempcopy
? tempfile
: to_name
);
473 * Re-open our fd on the target, in case we used a strip
474 * that does not work in-place -- like GNU binutils strip.
476 #if !defined(__EMX__) && !defined(_MSC_VER)
479 to_fd
= open(tempcopy
? tempfile
: to_name
, O_RDONLY
| O_BINARY
, 0);
481 rc
= err(EX_OSERR
, "stripping %s", to_name
);
487 * Compare the stripped temp file with the target.
489 if (docompare
&& dostrip
&& target
) {
492 /* Re-open to_fd using the real target name. */
493 if ((to_fd
= open(to_name
, O_RDONLY
| O_BINARY
, 0)) < 0) {
494 rc
= err(EX_OSERR
, "%s", to_name
);
498 if (fstat(temp_fd
, &temp_sb
)) {
500 (void)unlink(tempfile
);
502 rc
= err(EX_OSERR
, "%s", tempfile
);
506 if (compare(temp_fd
, tempfile
, (size_t)temp_sb
.st_size
, to_fd
,
507 to_name
, (size_t)to_sb
.st_size
) == 0) {
509 * If target has more than one link we need to
510 * replace it in order to snap the extra links.
511 * Need to preserve target file times, though.
513 #if !defined(_MSC_VER) && !defined(__EMX__)
514 if (to_sb
.st_nlink
!= 1) {
515 tvb
[0].tv_sec
= to_sb
.st_atime
;
517 tvb
[1].tv_sec
= to_sb
.st_mtime
;
519 (void)utimes(tempfile
, tvb
);
525 (void)unlink(tempfile
);
527 (void) close(temp_fd
);
533 * Move the new file into place if doing a safe copy
534 * and the files are different (or just not compared).
536 if (tempcopy
&& !files_match
) {
538 /* Try to turn off the immutable bits. */
539 if (to_sb
.st_flags
& NOCHANGEBITS
)
540 (void)chflags(to_name
, to_sb
.st_flags
& ~NOCHANGEBITS
);
543 if ((size_t)snprintf(backup
, MAXPATHLEN
, "%s%s", to_name
,
544 suffix
) != strlen(to_name
) + strlen(suffix
)) {
546 rc
= errx(EX_OSERR
, "%s: backup filename too long",
551 (void)printf("install: %s -> %s\n", to_name
, backup
);
552 if (rename(to_name
, backup
) < 0) {
556 rc
= err(EX_OSERR
, "rename: %s to %s", to_name
,
562 (void)printf("install: %s -> %s\n", from_name
, to_name
);
563 if (rename(tempfile
, to_name
) < 0) {
567 rc
= err(EX_OSERR
, "rename: %s to %s",
572 /* Re-open to_fd so we aren't hosed by the rename(2). */
574 if ((to_fd
= open(to_name
, O_RDONLY
| O_BINARY
, 0)) < 0) {
575 rc
= err(EX_OSERR
, "%s", to_name
);
581 * Preserve the timestamp of the source file if necessary.
583 if (dopreserve
&& !files_match
&& !devnull
) {
584 tvb
[0].tv_sec
= from_sb
.st_atime
;
586 tvb
[1].tv_sec
= from_sb
.st_mtime
;
588 (void)utimes(to_name
, tvb
);
591 if (fstat(to_fd
, &to_sb
) == -1) {
593 (void)unlink(to_name
);
595 rc
= err(EX_OSERR
, "%s", to_name
);
600 * Set owner, group, mode for target; do the chown first,
601 * chown may lose the setuid bits.
604 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
605 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
) ||
606 (mode
!= (to_sb
.st_mode
& ALLPERMS
))) {
607 /* Try to turn off the immutable bits. */
608 if (to_sb
.st_flags
& NOCHANGEBITS
)
609 (void)fchflags(to_fd
, to_sb
.st_flags
& ~NOCHANGEBITS
);
613 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
614 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
))
615 if (fchown(to_fd
, uid
, gid
) == -1) {
617 (void)unlink(to_name
);
619 rc
= err(EX_OSERR
,"%s: chown/chgrp", to_name
);
623 if (mode
!= (to_sb
.st_mode
& ALLPERMS
))
624 if (fchmod(to_fd
, mode
)) {
626 (void)unlink(to_name
);
628 rc
= err(EX_OSERR
, "%s: chmod", to_name
);
633 * If provided a set of flags, set them, otherwise, preserve the
634 * flags, except for the dump flag.
635 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
636 * trying to turn off UF_NODUMP. If we're trying to set real flags,
637 * then warn if the the fs doesn't support it, otherwise fail.
640 if (!devnull
&& (flags
& SETFLAGS
||
641 (from_sb
.st_flags
& ~UF_NODUMP
) != to_sb
.st_flags
) &&
643 flags
& SETFLAGS
? fset
: from_sb
.st_flags
& ~UF_NODUMP
)) {
644 if (flags
& SETFLAGS
) {
645 if (errno
== EOPNOTSUPP
)
646 warn("%s: chflags", to_name
);
649 (void)unlink(to_name
);
651 rc
= err(EX_OSERR
, "%s: chflags", to_name
);
662 (void)close(temp_fd
);
664 (void)close(from_fd
);
670 * compare two files; non-zero means files differ
673 compare(int from_fd
, const char *from_name __unused
, size_t from_len
,
674 int to_fd
, const char *to_name __unused
, size_t to_len
)
681 if (from_len
!= to_len
)
684 if (from_len
<= MAX_CMP_SIZE
) {
687 if (trymmap(from_fd
) && trymmap(to_fd
)) {
688 p
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, from_fd
, (off_t
)0);
689 if (p
== (char *)MAP_FAILED
)
691 q
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, to_fd
, (off_t
)0);
692 if (q
== (char *)MAP_FAILED
) {
697 rv
= memcmp(p
, q
, from_len
);
713 lseek(from_fd
, 0, SEEK_SET
);
714 lseek(to_fd
, 0, SEEK_SET
);
716 n1
= read(from_fd
, buf1
, sizeof(buf1
));
720 n2
= read(to_fd
, buf2
, n1
);
722 rv
= memcmp(buf1
, buf2
, n1
);
724 rv
= 1; /* out of sync */
726 rv
= 1; /* read failure */
728 lseek(from_fd
, 0, SEEK_SET
);
729 lseek(to_fd
, 0, SEEK_SET
);
732 rv
= 1; /* don't bother in this case */
739 * create a temporary file based on path and open it
742 create_tempfile(const char *path
, char *temp
, size_t tsize
)
746 (void)strncpy(temp
, path
, tsize
);
747 temp
[tsize
- 1] = '\0';
748 if ((p
= last_slash(temp
)) != NULL
)
752 (void)strncpy(p
, "INS@XXXX", &temp
[tsize
- 1] - p
);
753 temp
[tsize
- 1] = '\0';
754 return (mkstemp(temp
));
759 * create a new file, overwriting an existing one if necessary
762 create_newfile(const char *path
, int target
, struct stat
*sbp
)
764 char backup
[MAXPATHLEN
];
770 * Unlink now... avoid ETXTBSY errors later. Try to turn
771 * off the append/immutable bits -- if we fail, go ahead,
775 if (sbp
->st_flags
& NOCHANGEBITS
)
776 (void)chflags(path
, sbp
->st_flags
& ~NOCHANGEBITS
);
780 if ((size_t)snprintf(backup
, MAXPATHLEN
, "%s%s",
781 path
, suffix
) != strlen(path
) + strlen(suffix
)) {
782 errx(EX_OSERR
, "%s: backup filename too long",
784 errno
= ENAMETOOLONG
;
787 (void)snprintf(backup
, MAXPATHLEN
, "%s%s",
790 (void)printf("install: %s -> %s\n",
792 if (rename(path
, backup
) < 0) {
793 err(EX_OSERR
, "rename: %s to %s", path
, backup
);
797 if (unlink(path
) < 0)
801 newfd
= open(path
, O_CREAT
| O_RDWR
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
802 if (newfd
< 0 && saved_errno
!= 0)
809 * copy from one file to another
812 copy(int from_fd
, const char *from_name
, int to_fd
, const char *to_name
,
817 char *p
, buf
[MAXBSIZE
];
820 /* Rewind file descriptors. */
821 if (lseek(from_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
822 return err(EX_OSERR
, "lseek: %s", from_name
);
823 if (lseek(to_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
824 return err(EX_OSERR
, "lseek: %s", to_name
);
827 * Mmap and write if less than 8M (the limit is so we don't totally
828 * trash memory on big files. This is really a minor hack, but it
829 * wins some CPU back.
833 if (size
<= 8 * 1048576 && trymmap(from_fd
) &&
834 (p
= mmap(NULL
, (size_t)size
, PROT_READ
, MAP_SHARED
,
835 from_fd
, (off_t
)0)) != (char *)MAP_FAILED
) {
836 if ((nw
= write(to_fd
, p
, size
)) != size
) {
838 (void)unlink(to_name
);
839 errno
= nw
> 0 ? EIO
: serrno
;
840 err(EX_OSERR
, "%s", to_name
);
848 while ((nr
= read(from_fd
, buf
, sizeof(buf
))) > 0)
849 if ((nw
= write(to_fd
, buf
, nr
)) != nr
) {
851 (void)unlink(to_name
);
852 errno
= nw
> 0 ? EIO
: serrno
;
853 return err(EX_OSERR
, "%s", to_name
);
857 (void)unlink(to_name
);
859 return err(EX_OSERR
, "%s", from_name
);
867 * use strip(1) to strip the target file
870 strip(const char *to_name
)
872 #if defined(__EMX__) || defined(_MSC_VER)
873 const char *stripbin
= getenv("STRIPBIN");
874 if (stripbin
== NULL
)
876 return spawnlp(P_WAIT
, stripbin
, stripbin
, to_name
, NULL
);
878 const char *stripbin
;
886 (void)unlink(to_name
);
888 return err(EX_TEMPFAIL
, "fork");
890 stripbin
= getenv("STRIPBIN");
891 if (stripbin
== NULL
)
893 execlp(stripbin
, stripbin
, to_name
, (char *)NULL
);
894 err(EX_OSERR
, "exec(%s)", stripbin
);
897 if (waitpid(pid
, &status
, 0) == -1 || status
) {
899 (void)unlink(to_name
);
901 return err(EX_SOFTWARE
, "waitpid");
911 * build directory heirarchy
914 install_dir(char *path
)
921 if (!*p
|| (p
!= path
&& IS_SLASH(*p
))) {
924 if (stat(path
, &sb
)) {
925 if (errno
!= ENOENT
|| mkdir(path
, 0755) < 0) {
926 return err(EX_OSERR
, "mkdir %s", path
);
929 (void)printf("install: mkdir %s\n",
931 } else if (!S_ISDIR(sb
.st_mode
))
932 return errx(EX_OSERR
, "%s exists but is not a directory", path
);
937 if ((gid
!= (gid_t
)-1 || uid
!= (uid_t
)-1) && chown(path
, uid
, gid
))
938 warn("chown %u:%u %s", uid
, gid
, path
);
939 if (chmod(path
, mode
))
940 warn("chmod %o %s", mode
, path
);
946 * print a usage message and die
952 "usage: %s [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
953 " [-o owner] file1 file2\n"
954 " or: %s [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
955 " [-o owner] file1 ... fileN directory\n"
956 " or: %s -d [-v] [-g group] [-m mode] [-o owner] directory ...\n"
958 " or: %s --version\n",
959 g_progname
, g_progname
, g_progname
, g_progname
, g_progname
);
966 * return true (1) if mmap should be tried, false (0) if not.
972 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
973 * pre-Lite2-merge systems.
978 if (nommap
|| fstatfs(fd
, &stfs
) != 0)
980 if (strcmp(stfs
.f_fstypename
, "ufs") == 0 ||
981 strcmp(stfs
.f_fstypename
, "cd9660") == 0)
988 /* figures out where the last slash or colon is. */
990 last_slash(const char *path
)
992 #if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
993 char *p
= (char *)strrchr(path
, '/');
996 char *p2
= strrchr(p
, '\\');
1002 p
= (char *)strrchr(path
, '\\');
1003 if (!p
&& isalpha(path
[0]) && path
[1] == ':')
1004 p
= (char *)&path
[1];
1008 return strrchr(path
, '/');