]>
git.saurik.com Git - apple/file_cmds.git/blob - install/xinstall.c
31e542dcbc88077949aa32d9d03c8f34ab07a62c
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>
72 #include "pathnames.h"
74 /* Bootstrap aid - this doesn't exist in most older releases */
76 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
79 #define DIRECTORY 0x01 /* Tell install it's a directory. */
80 #define SETFLAGS 0x02 /* Tell install to set flags. */
81 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
82 #define BACKUP_SUFFIX ".old"
88 int dobackup
, docompare
, dodir
, dopreserve
, dostrip
, nommap
, safecopy
, verbose
;
89 mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
90 char *suffix
= BACKUP_SUFFIX
;
92 void copy
__P((int, char *, int, char *, off_t
));
93 int compare
__P((int, const char *, size_t, int, const char *, size_t));
94 int create_newfile
__P((char *, int, struct stat
*));
95 int create_tempfile
__P((char *, char *, size_t));
96 void install
__P((char *, char *, u_long
, u_int
));
97 void install_dir
__P((char *));
98 u_long numeric_id
__P((char *, char *));
99 void strip
__P((char *));
100 int trymmap
__P((int));
101 void usage
__P((void));
108 struct stat from_sb
, to_sb
;
113 char *flags
, *group
, *owner
, *to_name
;
116 group
= owner
= NULL
;
117 while ((ch
= getopt(argc
, argv
, "B:bCcdf:g:Mm:o:pSsv")) != -1)
129 /* For backwards compatibility. */
136 if (strtofflags(&flags
, &fset
, NULL
))
137 errx(EX_USAGE
, "%s: invalid flag", flags
);
147 if (!(set
= setmode(optarg
)))
148 errx(EX_USAGE
, "invalid file mode: %s",
150 mode
= getmode(set
, 0);
157 docompare
= dopreserve
= 1;
175 /* some options make no sense when creating directories */
176 if ((safecopy
|| dostrip
) && dodir
)
180 * Older versions allowed -d -C combo. Issue a warning
181 * for now, but turn this into an error before 4.5-RELEASE.
183 if (docompare
&& dodir
)
184 warnx("the -d and -C options may not be specified together");
186 /* must have at least two arguments, except when creating directories */
187 if (argc
< 2 && !dodir
)
190 /* need to make a temp copy so we can compare stripped version */
191 if (docompare
&& dostrip
)
194 /* get group and owner id's */
196 if ((gp
= getgrnam(group
)) != NULL
)
199 gid
= (uid_t
)numeric_id(group
, "group");
204 if ((pp
= getpwnam(owner
)) != NULL
)
207 uid
= (uid_t
)numeric_id(owner
, "user");
212 for (; *argv
!= NULL
; ++argv
)
218 no_target
= stat(to_name
= argv
[argc
- 1], &to_sb
);
219 if (!no_target
&& S_ISDIR(to_sb
.st_mode
)) {
220 for (; *argv
!= to_name
; ++argv
)
221 install(*argv
, to_name
, fset
, iflags
| DIRECTORY
);
226 /* can't do file1 file2 directory/file */
231 if (stat(*argv
, &from_sb
))
232 err(EX_OSERR
, "%s", *argv
);
233 if (!S_ISREG(to_sb
.st_mode
)) {
235 err(EX_OSERR
, "%s", to_name
);
237 if (to_sb
.st_dev
== from_sb
.st_dev
&&
238 to_sb
.st_ino
== from_sb
.st_ino
)
240 "%s and %s are the same file", *argv
, to_name
);
242 install(*argv
, to_name
, fset
, iflags
);
248 numeric_id(name
, type
)
256 * We know that uid_t's and gid_t's are unsigned longs.
259 val
= strtoul(name
, &ep
, 10);
261 err(EX_NOUSER
, "%s", name
);
263 errx(EX_NOUSER
, "unknown %s %s", type
, name
);
269 * build a path name and install the file
272 install(from_name
, to_name
, fset
, flags
)
273 char *from_name
, *to_name
;
277 struct stat from_sb
, temp_sb
, to_sb
;
279 int devnull
, files_match
, from_fd
=0, serrno
, target
;
280 int tempcopy
, temp_fd
, to_fd
=0;
281 char backup
[MAXPATHLEN
], *p
, pathbuf
[MAXPATHLEN
], tempfile
[MAXPATHLEN
];
285 /* If try to install NULL file to a directory, fails. */
286 if (flags
& DIRECTORY
|| strcmp(from_name
, _PATH_DEVNULL
)) {
287 if (stat(from_name
, &from_sb
))
288 err(EX_OSERR
, "%s", from_name
);
289 if (!S_ISREG(from_sb
.st_mode
)) {
291 err(EX_OSERR
, "%s", from_name
);
293 /* Build the target path. */
294 if (flags
& DIRECTORY
) {
295 (void)snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
297 (p
= strrchr(from_name
, '/')) ? ++p
: from_name
);
305 target
= stat(to_name
, &to_sb
) == 0;
307 /* Only install to regular files. */
308 if (target
&& !S_ISREG(to_sb
.st_mode
)) {
314 /* Only copy safe if the target exists. */
315 tempcopy
= safecopy
&& target
;
317 if (!devnull
&& (from_fd
= open(from_name
, O_RDONLY
, 0)) < 0)
318 err(EX_OSERR
, "%s", from_name
);
320 /* If we don't strip, we can compare first. */
321 if (docompare
&& !dostrip
&& target
) {
322 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
323 err(EX_OSERR
, "%s", to_name
);
325 files_match
= to_sb
.st_size
== 0;
327 files_match
= !(compare(from_fd
, from_name
,
328 (size_t)from_sb
.st_size
, to_fd
,
329 to_name
, (size_t)to_sb
.st_size
));
331 /* Close "to" file unless we match. */
338 to_fd
= create_tempfile(to_name
, tempfile
,
341 err(EX_OSERR
, "%s", tempfile
);
343 if ((to_fd
= create_newfile(to_name
, target
,
345 err(EX_OSERR
, "%s", to_name
);
347 (void)printf("install: %s -> %s\n",
351 copy(from_fd
, from_name
, to_fd
,
352 tempcopy
? tempfile
: to_name
, from_sb
.st_size
);
356 strip(tempcopy
? tempfile
: to_name
);
359 * Re-open our fd on the target, in case we used a strip
360 * that does not work in-place -- like GNU binutils strip.
363 to_fd
= open(tempcopy
? tempfile
: to_name
, O_RDONLY
, 0);
365 err(EX_OSERR
, "stripping %s", to_name
);
369 * Compare the stripped temp file with the target.
371 if (docompare
&& dostrip
&& target
) {
374 /* Re-open to_fd using the real target name. */
375 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
376 err(EX_OSERR
, "%s", to_name
);
378 if (fstat(temp_fd
, &temp_sb
)) {
380 (void)unlink(tempfile
);
382 err(EX_OSERR
, "%s", tempfile
);
385 if (compare(temp_fd
, tempfile
, (size_t)temp_sb
.st_size
, to_fd
,
386 to_name
, (size_t)to_sb
.st_size
) == 0) {
388 * If target has more than one link we need to
389 * replace it in order to snap the extra links.
390 * Need to preserve target file times, though.
392 if (to_sb
.st_nlink
!= 1) {
393 utb
.actime
= to_sb
.st_atime
;
394 utb
.modtime
= to_sb
.st_mtime
;
395 (void)utime(tempfile
, &utb
);
398 (void)unlink(tempfile
);
400 (void) close(temp_fd
);
405 * Move the new file into place if doing a safe copy
406 * and the files are different (or just not compared).
408 if (tempcopy
&& !files_match
) {
409 /* Try to turn off the immutable bits. */
410 if (to_sb
.st_flags
& NOCHANGEBITS
)
411 (void)chflags(to_name
, to_sb
.st_flags
& ~NOCHANGEBITS
);
413 if (snprintf(backup
, MAXPATHLEN
, "%s%s", to_name
,
414 suffix
) != strlen(to_name
) + strlen(suffix
)) {
416 errx(EX_OSERR
, "%s: backup filename too long",
420 (void)printf("install: %s -> %s\n", to_name
, backup
);
421 if (rename(to_name
, backup
) < 0) {
425 err(EX_OSERR
, "rename: %s to %s", to_name
,
430 (void)printf("install: %s -> %s\n", from_name
, to_name
);
431 if (rename(tempfile
, to_name
) < 0) {
435 err(EX_OSERR
, "rename: %s to %s",
439 /* Re-open to_fd so we aren't hosed by the rename(2). */
441 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
442 err(EX_OSERR
, "%s", to_name
);
446 * Preserve the timestamp of the source file if necessary.
448 if (dopreserve
&& !files_match
&& !devnull
) {
449 utb
.actime
= from_sb
.st_atime
;
450 utb
.modtime
= from_sb
.st_mtime
;
451 (void)utime(to_name
, &utb
);
454 if (fstat(to_fd
, &to_sb
) == -1) {
456 (void)unlink(to_name
);
458 err(EX_OSERR
, "%s", to_name
);
462 * Set owner, group, mode for target; do the chown first,
463 * chown may lose the setuid bits.
465 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
466 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
) ||
467 (mode
!= to_sb
.st_mode
)) {
468 /* Try to turn off the immutable bits. */
469 if (to_sb
.st_flags
& NOCHANGEBITS
)
470 (void)fchflags(to_fd
, to_sb
.st_flags
& ~NOCHANGEBITS
);
473 if ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
474 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
))
475 if (fchown(to_fd
, uid
, gid
) == -1) {
477 (void)unlink(to_name
);
479 err(EX_OSERR
,"%s: chown/chgrp", to_name
);
482 if (mode
!= to_sb
.st_mode
)
483 if (fchmod(to_fd
, mode
)) {
485 (void)unlink(to_name
);
487 err(EX_OSERR
, "%s: chmod", to_name
);
491 * If provided a set of flags, set them, otherwise, preserve the
492 * flags, except for the dump flag.
493 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
494 * trying to turn off UF_NODUMP. If we're trying to set real flags,
495 * then warn if the the fs doesn't support it, otherwise fail.
497 if (!devnull
&& fchflags(to_fd
,
498 flags
& SETFLAGS
? fset
: from_sb
.st_flags
& ~UF_NODUMP
)) {
499 if (flags
& SETFLAGS
) {
500 if (errno
== EOPNOTSUPP
)
501 warn("%s: chflags", to_name
);
504 (void)unlink(to_name
);
506 err(EX_OSERR
, "%s: chflags", to_name
);
513 if (copyfile(from_name
, to_name
, NULL
, COPYFILE_ACL
| COPYFILE_XATTR
) < 0)
515 warn("%s: copyfile", to_name
);
522 (void)close(from_fd
);
527 * compare two files; non-zero means files differ
530 compare(int from_fd
, const char *from_name
, size_t from_len
,
531 int to_fd
, const char *to_name
, size_t to_len
)
538 if (from_len
!= to_len
)
541 if (from_len
<= 8 * 1024 * 1024) {
543 if (trymmap(from_fd
) && trymmap(to_fd
)) {
544 p
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, from_fd
, (off_t
)0);
545 if (p
== (char *)MAP_FAILED
)
547 q
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
, to_fd
, (off_t
)0);
548 if (q
== (char *)MAP_FAILED
) {
553 rv
= memcmp(p
, q
, from_len
);
565 lseek(from_fd
, 0, SEEK_SET
);
566 lseek(to_fd
, 0, SEEK_SET
);
568 n1
= read(from_fd
, buf1
, sizeof(buf1
));
572 n2
= read(to_fd
, buf2
, n1
);
574 rv
= memcmp(buf1
, buf2
, n1
);
576 rv
= 1; /* out of sync */
578 rv
= 1; /* read failure */
580 lseek(from_fd
, 0, SEEK_SET
);
581 lseek(to_fd
, 0, SEEK_SET
);
584 rv
= 1; /* don't bother in this case */
591 * create a temporary file based on path and open it
594 create_tempfile(path
, temp
, tsize
)
601 (void)strncpy(temp
, path
, tsize
);
602 temp
[tsize
- 1] = '\0';
603 if ((p
= strrchr(temp
, '/')) != NULL
)
607 (void)strncpy(p
, "INS@XXXX", &temp
[tsize
- 1] - p
);
608 temp
[tsize
- 1] = '\0';
609 return (mkstemp(temp
));
614 * create a new file, overwriting an existing one if necessary
617 create_newfile(path
, target
, sbp
)
622 char backup
[MAXPATHLEN
];
626 * Unlink now... avoid ETXTBSY errors later. Try to turn
627 * off the append/immutable bits -- if we fail, go ahead,
630 if (sbp
->st_flags
& NOCHANGEBITS
)
631 (void)chflags(path
, sbp
->st_flags
& ~NOCHANGEBITS
);
634 if (snprintf(backup
, MAXPATHLEN
, "%s%s",
635 path
, suffix
) != strlen(path
) + strlen(suffix
))
636 errx(EX_OSERR
, "%s: backup filename too long",
638 (void)snprintf(backup
, MAXPATHLEN
, "%s%s",
641 (void)printf("install: %s -> %s\n",
643 if (rename(path
, backup
) < 0)
644 err(EX_OSERR
, "rename: %s to %s", path
, backup
);
649 return (open(path
, O_CREAT
| O_RDWR
| O_TRUNC
, S_IRUSR
| S_IWUSR
));
654 * copy from one file to another
657 copy(from_fd
, from_name
, to_fd
, to_name
, size
)
658 register int from_fd
, to_fd
;
659 char *from_name
, *to_name
;
664 char *p
, buf
[MAXBSIZE
];
667 /* Rewind file descriptors. */
668 if (lseek(from_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
669 err(EX_OSERR
, "lseek: %s", from_name
);
670 if (lseek(to_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
671 err(EX_OSERR
, "lseek: %s", to_name
);
674 * Mmap and write if less than 8M (the limit is so we don't totally
675 * trash memory on big files. This is really a minor hack, but it
676 * wins some CPU back.
679 if (size
<= 8 * 1048576 && trymmap(from_fd
) &&
680 (p
= mmap(NULL
, (size_t)size
, PROT_READ
, MAP_SHARED
,
681 from_fd
, (off_t
)0)) != (char *)MAP_FAILED
) {
682 if ((nw
= write(to_fd
, p
, size
)) != size
) {
684 (void)unlink(to_name
);
685 errno
= nw
> 0 ? EIO
: serrno
;
686 err(EX_OSERR
, "%s", to_name
);
691 while ((nr
= read(from_fd
, buf
, sizeof(buf
))) > 0)
692 if ((nw
= write(to_fd
, buf
, nr
)) != nr
) {
694 (void)unlink(to_name
);
695 errno
= nw
> 0 ? EIO
: serrno
;
696 err(EX_OSERR
, "%s", to_name
);
700 (void)unlink(to_name
);
702 err(EX_OSERR
, "%s", from_name
);
709 * use strip(1) to strip the target file
720 (void)unlink(to_name
);
722 err(EX_TEMPFAIL
, "fork");
724 execlp("strip", "strip", to_name
, NULL
);
725 err(EX_OSERR
, "exec(strip)");
727 if (wait(&status
) == -1 || status
) {
728 (void)unlink(to_name
);
737 * build directory heirarchy
748 if (!*p
|| (p
!= path
&& *p
== '/')) {
751 if (stat(path
, &sb
)) {
752 if (errno
!= ENOENT
|| mkdir(path
, 0755) < 0) {
753 err(EX_OSERR
, "mkdir %s", path
);
756 (void)printf("install: mkdir %s\n",
758 } else if (!S_ISDIR(sb
.st_mode
))
759 errx(EX_OSERR
, "%s exists but is not a directory", path
);
764 if ((gid
!= (gid_t
)-1 || uid
!= (uid_t
)-1) && chown(path
, uid
, gid
))
765 warn("chown %u:%u %s", uid
, gid
, path
);
766 if (chmod(path
, mode
))
767 warn("chmod %o %s", mode
, path
);
772 * print a usage message and die
777 (void)fprintf(stderr
, "\
778 usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
779 [-o owner] file1 file2\n\
780 install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
781 [-o owner] file1 ... fileN directory\n\
782 install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
789 * return true (1) if mmap should be tried, false (0) if not.
796 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
797 * pre-Lite2-merge systems.
802 if (nommap
|| fstatfs(fd
, &stfs
) != 0)
804 if (strcmp(stfs
.f_fstypename
, "ufs") == 0 ||
805 strcmp(stfs
.f_fstypename
, "cd9660") == 0)