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>
71 #endif /* __APPLE__ */
73 #include "pathnames.h"
75 /* Bootstrap aid - this doesn't exist in most older releases */
77 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
80 #define DIRECTORY 0x01 /* Tell install it's a directory. */
81 #define SETFLAGS 0x02 /* Tell install to set flags. */
82 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
83 #define BACKUP_SUFFIX ".old"
89 int dobackup
, docompare
, dodir
, dopreserve
, dostrip
, nommap
, safecopy
, verbose
;
90 mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
91 char *suffix
= BACKUP_SUFFIX
;
93 void copy
__P((int, char *, int, char *, off_t
));
94 int compare
__P((int, const char *, size_t, int, const char *, size_t));
95 int create_newfile
__P((char *, int, struct stat
*));
96 int create_tempfile
__P((char *, char *, size_t));
97 void install
__P((char *, char *, u_long
, u_int
));
98 void install_dir
__P((char *));
99 u_long numeric_id
__P((char *, char *));
100 void strip
__P((char *));
101 int trymmap
__P((int));
102 void usage
__P((void));
109 struct stat from_sb
, to_sb
;
114 char *flags
, *group
, *owner
, *to_name
;
117 group
= owner
= NULL
;
118 while ((ch
= getopt(argc
, argv
, "B:bCcdf:g:Mm:o:pSsv")) != -1)
130 /* For backwards compatibility. */
137 if (strtofflags(&flags
, &fset
, NULL
))
138 errx(EX_USAGE
, "%s: invalid flag", flags
);
148 if (!(set
= setmode(optarg
)))
149 errx(EX_USAGE
, "invalid file mode: %s",
151 mode
= getmode(set
, 0);
158 docompare
= dopreserve
= 1;
176 /* some options make no sense when creating directories */
177 if ((safecopy
|| dostrip
) && dodir
)
181 * Older versions allowed -d -C combo. Issue a warning
182 * for now, but turn this into an error before 4.5-RELEASE.
184 if (docompare
&& dodir
)
185 warnx("the -d and -C options may not be specified together");
187 /* must have at least two arguments, except when creating directories */
188 if (argc
< 2 && !dodir
)
191 /* need to make a temp copy so we can compare stripped version */
192 if (docompare
&& dostrip
)
195 /* get group and owner id's */
197 if ((gp
= getgrnam(group
)) != NULL
)
200 gid
= (uid_t
)numeric_id(group
, "group");
205 if ((pp
= getpwnam(owner
)) != NULL
)
208 uid
= (uid_t
)numeric_id(owner
, "user");
213 for (; *argv
!= NULL
; ++argv
)
219 no_target
= stat(to_name
= argv
[argc
- 1], &to_sb
);
220 if (!no_target
&& S_ISDIR(to_sb
.st_mode
)) {
221 for (; *argv
!= to_name
; ++argv
)
222 install(*argv
, to_name
, fset
, iflags
| DIRECTORY
);
227 /* can't do file1 file2 directory/file */
232 if (stat(*argv
, &from_sb
))
233 err(EX_OSERR
, "%s", *argv
);
234 if (!S_ISREG(to_sb
.st_mode
)) {
236 err(EX_OSERR
, "%s", to_name
);
238 if (to_sb
.st_dev
== from_sb
.st_dev
&&
239 to_sb
.st_ino
== from_sb
.st_ino
)
241 "%s and %s are the same file", *argv
, to_name
);
243 install(*argv
, to_name
, fset
, iflags
);
249 numeric_id(name
, type
)
257 * We know that uid_t's and gid_t's are unsigned longs.
260 val
= strtoul(name
, &ep
, 10);
262 err(EX_NOUSER
, "%s", name
);
264 errx(EX_NOUSER
, "unknown %s %s", type
, name
);
270 * build a path name and install the file
273 install(from_name
, to_name
, fset
, flags
)
274 char *from_name
, *to_name
;
278 struct stat from_sb
, temp_sb
, to_sb
;
280 int devnull
, files_match
, from_fd
=0, serrno
, target
;
281 int tempcopy
, temp_fd
, to_fd
=0;
282 char backup
[MAXPATHLEN
], *p
, pathbuf
[MAXPATHLEN
], tempfile
[MAXPATHLEN
];
286 /* If try to install NULL file to a directory, fails. */
287 if (flags
& DIRECTORY
|| strcmp(from_name
, _PATH_DEVNULL
)) {
288 if (stat(from_name
, &from_sb
))
289 err(EX_OSERR
, "%s", from_name
);
290 if (!S_ISREG(from_sb
.st_mode
)) {
292 err(EX_OSERR
, "%s", from_name
);
294 /* Build the target path. */
295 if (flags
& DIRECTORY
) {
296 (void)snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
298 (p
= strrchr(from_name
, '/')) ?
++p
: from_name
);
306 target
= stat(to_name
, &to_sb
) == 0;
308 /* Only install to regular files. */
309 if (target
&& !S_ISREG(to_sb
.st_mode
)) {
315 /* Only copy safe if the target exists. */
316 tempcopy
= safecopy
&& target
;
318 if (!devnull
&& (from_fd
= open(from_name
, O_RDONLY
, 0)) < 0)
319 err(EX_OSERR
, "%s", from_name
);
321 /* If we don't strip, we can compare first. */
322 if (docompare
&& !dostrip
&& target
) {
323 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
324 err(EX_OSERR
, "%s", to_name
);
326 files_match
= to_sb
.st_size
== 0;
328 files_match
= !(compare(from_fd
, from_name
,
329 (size_t)from_sb
.st_size
, to_fd
,
330 to_name
, (size_t)to_sb
.st_size
));
332 /* Close "to" file unless we match. */
339 to_fd
= create_tempfile(to_name
, tempfile
,
342 err(EX_OSERR
, "%s", tempfile
);
344 if ((to_fd
= create_newfile(to_name
, target
,
346 err(EX_OSERR
, "%s", to_name
);
348 (void)printf("install: %s -> %s\n",
352 copy(from_fd
, from_name
, to_fd
,
353 tempcopy ? tempfile
: to_name
, from_sb
.st_size
);
357 strip(tempcopy ? tempfile
: to_name
);
360 * Re-open our fd on the target, in case we used a strip
361 * that does not work in-place -- like GNU binutils strip.
364 to_fd
= open(tempcopy ? tempfile
: to_name
, O_RDONLY
, 0);
366 err(EX_OSERR
, "stripping %s", to_name
);
370 * Compare the stripped temp file with the target.
372 if (docompare
&& dostrip
&& target
) {
375 /* Re-open to_fd using the real target name. */
376 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
377 err(EX_OSERR
, "%s", to_name
);
379 if (fstat(temp_fd
, &temp_sb
)) {
381 (void)unlink(tempfile
);
383 err(EX_OSERR
, "%s", tempfile
);
386 if (compare(temp_fd
, tempfile
, (size_t)temp_sb
.st_size
, to_fd
,
387 to_name
, (size_t)to_sb
.st_size
) == 0) {
389 * If target has more than one link we need to
390 * replace it in order to snap the extra links.
391 * Need to preserve target file times, though.
393 if (to_sb
.st_nlink
!= 1) {
394 utb
.actime
= to_sb
.st_atime
;
395 utb
.modtime
= to_sb
.st_mtime
;
396 (void)utime(tempfile
, &utb
);
399 (void)unlink(tempfile
);
401 (void) close(temp_fd
);
406 * Move the new file into place if doing a safe copy
407 * and the files are different (or just not compared).
409 if (tempcopy
&& !files_match
) {
410 /* Try to turn off the immutable bits. */
411 if (to_sb
.st_flags
& NOCHANGEBITS
)
412 (void)chflags(to_name
, to_sb
.st_flags
& ~NOCHANGEBITS
);
414 if (snprintf(backup
, MAXPATHLEN
, "%s%s", to_name
,
415 suffix
) != strlen(to_name
) + strlen(suffix
)) {
417 errx(EX_OSERR
, "%s: backup filename too long",
421 (void)printf("install: %s -> %s\n", to_name
, backup
);
422 if (rename(to_name
, backup
) < 0) {
426 err(EX_OSERR
, "rename: %s to %s", to_name
,
431 (void)printf("install: %s -> %s\n", from_name
, to_name
);
432 if (rename(tempfile
, to_name
) < 0) {
436 err(EX_OSERR
, "rename: %s to %s",
440 /* Re-open to_fd so we aren't hosed by the rename(2). */
442 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
443 err(EX_OSERR
, "%s", to_name
);
447 /* in case mtime is modified */
448 if (!devnull
&& (S_ISLNK(from_sb
.st_mode
) || S_ISREG(from_sb
.st_mode
)) &&
449 fcopyfile(from_fd
, to_fd
, NULL
, COPYFILE_XATTR
) < 0) {
450 warn("%s: unable to copy extended attributes from %s", to_name
, from_name
);
452 #endif /* __APPLE__ */
454 * Preserve the timestamp of the source file if necessary.
456 if (dopreserve
&& !files_match
&& !devnull
) {
457 utb
.actime
= from_sb
.st_atime
;
458 utb
.modtime
= from_sb
.st_mtime
;
459 (void)utime(to_name
, &utb
);
462 if (fstat(to_fd
, &to_sb
) == -1) {
464 (void)unlink(to_name
);
466 err(EX_OSERR
, "%s", to_name
);
470 * Set owner, group, mode for target; do the chown first,
471 * chown may lose the setuid bits.
473 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
474 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
) ||
475 (mode
!= to_sb
.st_mode
)) {
476 /* Try to turn off the immutable bits. */
477 if (to_sb
.st_flags
& NOCHANGEBITS
)
478 (void)fchflags(to_fd
, to_sb
.st_flags
& ~NOCHANGEBITS
);
481 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
482 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
))
483 if (fchown(to_fd
, uid
, gid
) == -1) {
485 (void)unlink(to_name
);
487 err(EX_OSERR
,"%s: chown/chgrp", to_name
);
490 if (mode
!= to_sb
.st_mode
)
491 if (fchmod(to_fd
, mode
)) {
493 (void)unlink(to_name
);
495 err(EX_OSERR
, "%s: chmod", to_name
);
499 * If provided a set of flags, set them, otherwise, preserve the
500 * flags, except for the dump flag.
501 * NFS does not support flags. Ignore ENOTSUP flags if we're just
502 * trying to turn off UF_NODUMP. If we're trying to set real flags,
503 * then warn if the the fs doesn't support it, otherwise fail.
505 if (!devnull
&& fchflags(to_fd
,
506 flags
& SETFLAGS ? fset
: from_sb
.st_flags
& ~UF_NODUMP
)) {
507 if (flags
& SETFLAGS
) {
508 if (errno
== ENOTSUP
)
509 warn("%s: chflags", to_name
);
512 (void)unlink(to_name
);
514 err(EX_OSERR
, "%s: chflags", to_name
);
519 /* the ACL could prevent credential/permission system calls later on... */
520 if (!devnull
&& (S_ISLNK(from_sb
.st_mode
) || S_ISREG(from_sb
.st_mode
)) &&
521 (fcopyfile(from_fd
, to_fd
, NULL
, COPYFILE_ACL
) < 0)) {
522 warn("%s: unable to copy ACL from %s", to_name
, from_name
);
524 #endif /* __APPLE__ */
528 (void)close(from_fd
);
530 #if TARGET_OS_EMBEDDED
531 #define BUFFER_SIZE 128*1024
532 #else /* !TARGET_OS_EMBEDDED */
533 #define BUFFER_SIZE MAXBSIZE
534 #endif /* TARGET_OS_EMBEDDED */
537 * compare two files; non-zero means files differ
540 compare(int from_fd
, const char *from_name
, size_t from_len
,
541 int to_fd
, const char *to_name
, size_t to_len
)
548 if (from_len
!= to_len
)
551 if (from_len
<= 8 * 1024 * 1024) {
553 if (trymmap(from_fd
) && trymmap(to_fd
)) {
554 p
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, from_fd
, (off_t
)0);
555 if (p
== (char *)MAP_FAILED
)
557 q
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, to_fd
, (off_t
)0);
558 if (q
== (char *)MAP_FAILED
) {
563 rv
= memcmp(p
, q
, from_len
);
570 char buf1
[BUFFER_SIZE
];
571 char buf2
[BUFFER_SIZE
];
575 lseek(from_fd
, 0, SEEK_SET
);
576 lseek(to_fd
, 0, SEEK_SET
);
578 n1
= read(from_fd
, buf1
, sizeof(buf1
));
582 n2
= read(to_fd
, buf2
, n1
);
584 rv
= memcmp(buf1
, buf2
, n1
);
586 rv
= 1; /* out of sync */
588 rv
= 1; /* read failure */
590 lseek(from_fd
, 0, SEEK_SET
);
591 lseek(to_fd
, 0, SEEK_SET
);
594 rv
= 1; /* don't bother in this case */
601 * create a temporary file based on path and open it
604 create_tempfile(path
, temp
, tsize
)
611 (void)strncpy(temp
, path
, tsize
);
612 temp
[tsize
- 1] = '\0';
613 if ((p
= strrchr(temp
, '/')) != NULL
)
617 (void)strncpy(p
, "INS@XXXX", &temp
[tsize
- 1] - p
);
618 temp
[tsize
- 1] = '\0';
619 return (mkstemp(temp
));
624 * create a new file, overwriting an existing one if necessary
627 create_newfile(path
, target
, sbp
)
632 char backup
[MAXPATHLEN
];
636 * Unlink now... avoid ETXTBSY errors later. Try to turn
637 * off the append/immutable bits -- if we fail, go ahead,
640 if (sbp
->st_flags
& NOCHANGEBITS
)
641 (void)chflags(path
, sbp
->st_flags
& ~NOCHANGEBITS
);
644 if (snprintf(backup
, MAXPATHLEN
, "%s%s",
645 path
, suffix
) != strlen(path
) + strlen(suffix
))
646 errx(EX_OSERR
, "%s: backup filename too long",
648 (void)snprintf(backup
, MAXPATHLEN
, "%s%s",
651 (void)printf("install: %s -> %s\n",
653 if (rename(path
, backup
) < 0)
654 err(EX_OSERR
, "rename: %s to %s", path
, backup
);
659 return (open(path
, O_CREAT
| O_RDWR
| O_TRUNC
, S_IRUSR
| S_IWUSR
));
664 * copy from one file to another
667 copy(from_fd
, from_name
, to_fd
, to_name
, size
)
668 register int from_fd
, to_fd
;
669 char *from_name
, *to_name
;
674 char *p
, buf
[BUFFER_SIZE
];
677 /* Rewind file descriptors. */
678 if (lseek(from_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
679 err(EX_OSERR
, "lseek: %s", from_name
);
680 if (lseek(to_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
681 err(EX_OSERR
, "lseek: %s", to_name
);
684 * Mmap and write if less than 8M (the limit is so we don't totally
685 * trash memory on big files. This is really a minor hack, but it
686 * wins some CPU back.
689 if (size
<= 8 * 1048576 && trymmap(from_fd
) &&
690 (p
= mmap(NULL
, (size_t)size
, PROT_READ
, MAP_SHARED
,
691 from_fd
, (off_t
)0)) != (char *)MAP_FAILED
) {
692 if ((nw
= write(to_fd
, p
, size
)) != size
) {
694 (void)unlink(to_name
);
695 errno
= nw
> 0 ? EIO
: serrno
;
696 err(EX_OSERR
, "%s", to_name
);
701 while ((nr
= read(from_fd
, buf
, sizeof(buf
))) > 0)
702 if ((nw
= write(to_fd
, buf
, nr
)) != nr
) {
704 (void)unlink(to_name
);
705 errno
= nw
> 0 ? EIO
: serrno
;
706 err(EX_OSERR
, "%s", to_name
);
710 (void)unlink(to_name
);
712 err(EX_OSERR
, "%s", from_name
);
719 * use strip(1) to strip the target file
727 extern char** environ
;
728 char *const argv
[] = { "xcrun", "strip", "-", to_name
, NULL
};
730 if (0 == (error
= posix_spawnp(&pid
, "xcrun", NULL
, NULL
, argv
, environ
))) {
732 pid_t child
= waitpid(pid
, &status
, 0);
733 if ((child
== -1) || status
) {
735 errx(EX_SOFTWARE
, "child process failed: xcrun strip - %s", to_name
);
739 err(EX_OSERR
, "xcrun strip - %s", to_name
);
745 * build directory heirarchy
756 if (!*p
|| (p
!= path
&& *p
== '/')) {
759 if (stat(path
, &sb
)) {
760 if (errno
!= ENOENT
|| mkdir(path
, 0755) < 0) {
761 err(EX_OSERR
, "mkdir %s", path
);
764 (void)printf("install: mkdir %s\n",
766 } else if (!S_ISDIR(sb
.st_mode
))
767 errx(EX_OSERR
, "%s exists but is not a directory", path
);
772 if ((gid
!= (gid_t
)-1 || uid
!= (uid_t
)-1) && chown(path
, uid
, gid
))
773 warn("chown %u:%u %s", uid
, gid
, path
);
774 if (chmod(path
, mode
))
775 warn("chmod %o %s", mode
, path
);
780 * print a usage message and die
785 (void)fprintf(stderr
, "\
786 usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
787 [-o owner] file1 file2\n\
788 install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
789 [-o owner] file1 ... fileN directory\n\
790 install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
797 * return true (1) if mmap should be tried, false (0) if not.
804 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
805 * pre-Lite2-merge systems.
810 if (nommap
|| fstatfs(fd
, &stfs
) != 0)
812 if (strcmp(stfs
.f_fstypename
, "ufs") == 0 ||
813 strcmp(stfs
.f_fstypename
, "cd9660") == 0)