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. 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
33 #include <sys/param.h>
35 #include <sys/mount.h>
57 /* Bootstrap aid - this doesn't exist in most older releases */
59 #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
62 #define UF_NOHISTORY 0
65 #define MAX_CMP_SIZE (16 * 1024 * 1024)
67 #define LN_ABSOLUTE 0x01
68 #define LN_RELATIVE 0x02
70 #define LN_SYMBOLIC 0x08
73 #define DIRECTORY 0x01 /* Tell install it's a directory. */
74 #define SETFLAGS 0x02 /* Tell install to set flags. */
75 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
76 #define BACKUP_SUFFIX ".old"
80 static int dobackup
, docompare
, dodir
, dolink
, dopreserve
, dostrip
, dounpriv
,
81 nommap
, safecopy
, verbose
;
82 static int haveopt_f
, haveopt_g
, haveopt_m
, haveopt_o
;
83 static mode_t mode
= S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
;
84 static const char *group
, *owner
;
85 static const char *suffix
= BACKUP_SUFFIX
;
86 static char *destdir
, *fflags
;
88 static int compare(int, const char *, size_t, int, const char *, size_t);
89 static void copy(int, const char *, int, const char *, off_t
);
90 static int create_newfile(const char *, int, struct stat
*);
91 static int create_tempfile(const char *, char *, size_t);
92 static int do_link(const char *, const char *, const struct stat
*);
93 static void do_symlink(const char *, const char *, const struct stat
*);
94 static void makelink(const char *, const char *, const struct stat
*);
95 static void install(const char *, const char *, u_long
, u_long
, u_int
);
96 static void install_dir(char *);
97 static int parseid(const char *, id_t
*);
98 static void strip(const char *);
99 static int trymmap(int);
100 static void usage(void);
103 main(int argc
, char *argv
[])
105 struct stat from_sb
, to_sb
;
120 /* NOTE: please also update 'tools/install.sh' */
121 while ((ch
= getopt(argc
, argv
, "B:bCcD:df:g:L:l:M:m:N:o:pSsUv")) != -1)
133 /* For backwards compatibility. */
142 #ifdef _ST_FLAGS_PRESENT_
152 for (p
= optarg
; *p
; p
++)
155 dolink
&= ~(LN_HARD
|LN_MIXED
);
156 dolink
|= LN_SYMBOLIC
;
159 dolink
&= ~(LN_SYMBOLIC
|LN_MIXED
);
163 dolink
&= ~(LN_SYMBOLIC
|LN_HARD
);
167 dolink
&= ~LN_RELATIVE
;
168 dolink
|= LN_ABSOLUTE
;
171 dolink
&= ~LN_ABSOLUTE
;
172 dolink
|= LN_RELATIVE
;
175 errx(EXIT_FAILURE
, "%c: invalid link type", *p
);
184 if (!(set
= setmode(optarg
)))
185 errx(EX_USAGE
, "invalid file mode: %s",
187 mode
= getmode(set
, 0);
191 /* -L kept for compatibility with pre-5.4 DragonFly */
192 warnx("Option -L is deprecated, use -N instead");
195 if (!setup_getid(optarg
))
196 err(EX_OSERR
, "Unable to use user and group "
197 "databases in `%s'", optarg
);
204 docompare
= dopreserve
= 1;
225 /* some options make no sense when creating directories */
226 if (dostrip
&& dodir
) {
227 warnx("-d and -s may not be specified together");
231 if (getenv("DONTSTRIP") != NULL
) {
232 warnx("DONTSTRIP set - will not strip installed binaries");
236 /* must have at least two arguments, except when creating directories */
237 if (argc
== 0 || (argc
== 1 && !dodir
))
240 /* need to make a temp copy so we can compare stripped version */
241 if (docompare
&& dostrip
)
244 /* get group and owner id's */
245 if (group
!= NULL
&& !dounpriv
) {
246 if (gid_from_group(group
, &gid
) == -1) {
248 if (!parseid(group
, &id
))
249 errx(1, "unknown group %s", group
);
255 if (owner
!= NULL
&& !dounpriv
) {
256 if (uid_from_user(owner
, &uid
) == -1) {
258 if (!parseid(owner
, &id
))
259 errx(1, "unknown user %s", owner
);
265 #ifdef _ST_FLAGS_PRESENT_
266 if (fflags
!= NULL
&& !dounpriv
) {
267 if (strtofflags(&fflags
, &fset
, &fclr
))
268 errx(EX_USAGE
, "%s: invalid flag", fflags
);
274 for (; *argv
!= NULL
; ++argv
)
280 to_name
= argv
[argc
- 1];
281 no_target
= stat(to_name
, &to_sb
);
282 if (!no_target
&& S_ISDIR(to_sb
.st_mode
)) {
283 if (dolink
& LN_SYMBOLIC
) {
284 if (lstat(to_name
, &to_sb
) != 0)
285 err(EX_OSERR
, "%s vanished", to_name
);
286 if (S_ISLNK(to_sb
.st_mode
)) {
289 err(EX_USAGE
, "%s", to_name
);
291 install(*argv
, to_name
, fset
, fclr
, iflags
);
295 for (; *argv
!= to_name
; ++argv
)
296 install(*argv
, to_name
, fset
, fclr
, iflags
| DIRECTORY
);
301 /* can't do file1 file2 directory/file */
304 warnx("target directory `%s' does not exist",
307 warnx("target `%s' is not a directory",
312 if (!no_target
&& !dolink
) {
313 if (stat(*argv
, &from_sb
))
314 err(EX_OSERR
, "%s", *argv
);
315 if (!S_ISREG(to_sb
.st_mode
)) {
317 err(EX_OSERR
, "%s", to_name
);
319 if (to_sb
.st_dev
== from_sb
.st_dev
&&
320 to_sb
.st_ino
== from_sb
.st_ino
)
322 "%s and %s are the same file", *argv
, to_name
);
324 install(*argv
, to_name
, fset
, fclr
, iflags
);
331 * parse uid or gid from arg into id, returning non-zero if successful
334 parseid(const char *name
, id_t
*id
)
338 *id
= (id_t
)strtoul(name
, &ep
, 10);
339 if (errno
|| *ep
!= '\0')
346 * mktemp implementation used mkstemp to avoid mktemp warnings. We
347 * really do need mktemp semantics here as we will be creating a link.
350 quiet_mktemp(char *template)
354 if ((fd
= mkstemp(template)) == -1)
357 if (unlink(template) == -1)
358 err(EX_OSERR
, "unlink %s", template);
364 * make a hard link, obeying dorename if set
365 * return -1 on failure
368 do_link(const char *from_name
, const char *to_name
,
369 const struct stat
*target_sb
)
371 char tmpl
[MAXPATHLEN
];
374 if (safecopy
&& target_sb
!= NULL
) {
375 (void)snprintf(tmpl
, sizeof(tmpl
), "%s.inst.XXXXXX", to_name
);
376 /* This usage is safe. */
377 if (quiet_mktemp(tmpl
) == NULL
)
378 err(EX_OSERR
, "%s: mktemp", tmpl
);
379 ret
= link(from_name
, tmpl
);
381 if (target_sb
->st_mode
& S_IFDIR
&& rmdir(to_name
) ==
384 err(EX_OSERR
, "%s", to_name
);
386 #ifdef _ST_FLAGS_PRESENT_
387 if (target_sb
->st_flags
& NOCHANGEBITS
)
388 (void)chflags(to_name
, target_sb
->st_flags
&
392 printf("install: link %s -> %s\n",
394 ret
= rename(tmpl
, to_name
);
396 * If rename has posix semantics, then the temporary
397 * file may still exist when from_name and to_name point
398 * to the same file, so unlink it unconditionally.
405 printf("install: link %s -> %s\n",
407 return (link(from_name
, to_name
));
413 * Make a symbolic link, obeying dorename if set. Exit on failure.
416 do_symlink(const char *from_name
, const char *to_name
,
417 const struct stat
*target_sb
)
419 char tmpl
[MAXPATHLEN
];
421 if (safecopy
&& target_sb
!= NULL
) {
422 (void)snprintf(tmpl
, sizeof(tmpl
), "%s.inst.XXXXXX", to_name
);
423 /* This usage is safe. */
424 if (quiet_mktemp(tmpl
) == NULL
)
425 err(EX_OSERR
, "%s: mktemp", tmpl
);
427 if (symlink(from_name
, tmpl
) == -1)
428 err(EX_OSERR
, "symlink %s -> %s", from_name
, tmpl
);
430 if (target_sb
->st_mode
& S_IFDIR
&& rmdir(to_name
) == -1) {
432 err(EX_OSERR
, "%s", to_name
);
434 #ifdef _ST_FLAGS_PRESENT_
435 if (target_sb
->st_flags
& NOCHANGEBITS
)
436 (void)chflags(to_name
, target_sb
->st_flags
&
440 printf("install: symlink %s -> %s\n",
442 if (rename(tmpl
, to_name
) == -1) {
443 /* Remove temporary link before exiting. */
445 err(EX_OSERR
, "%s: rename", to_name
);
449 printf("install: symlink %s -> %s\n",
451 if (symlink(from_name
, to_name
) == -1)
452 err(EX_OSERR
, "symlink %s -> %s", from_name
, to_name
);
458 * make a link from source to destination
461 makelink(const char *from_name
, const char *to_name
,
462 const struct stat
*target_sb
)
464 char src
[MAXPATHLEN
], dst
[MAXPATHLEN
], lnk
[MAXPATHLEN
];
467 /* Try hard links first. */
468 if (dolink
& (LN_HARD
|LN_MIXED
)) {
469 if (do_link(from_name
, to_name
, target_sb
) == -1) {
470 if ((dolink
& LN_HARD
) || errno
!= EXDEV
)
471 err(EX_OSERR
, "link %s -> %s", from_name
, to_name
);
473 if (stat(to_name
, &to_sb
))
474 err(EX_OSERR
, "%s: stat", to_name
);
475 if (S_ISREG(to_sb
.st_mode
)) {
477 * XXX: hard links to anything other than
478 * plain files are not metalogged
481 const char *oowner
, *ogroup
;
485 * XXX: use underlying perms, unless
486 * overridden on command line.
490 mode
= (to_sb
.st_mode
& 0777);
509 /* Symbolic links. */
510 if (dolink
& LN_ABSOLUTE
) {
511 /* Convert source path to absolute. */
512 if (realpath(from_name
, src
) == NULL
)
513 err(EX_OSERR
, "%s: realpath", from_name
);
514 do_symlink(src
, to_name
, target_sb
);
515 /* XXX: src may point outside of destdir */
519 if (dolink
& LN_RELATIVE
) {
520 char *to_name_copy
, *cp
, *d
, *s
;
522 if (*from_name
!= '/') {
523 /* this is already a relative link */
524 do_symlink(from_name
, to_name
, target_sb
);
525 /* XXX: from_name may point outside of destdir. */
529 /* Resolve pathnames. */
530 if (realpath(from_name
, src
) == NULL
)
531 err(EX_OSERR
, "%s: realpath", from_name
);
534 * The last component of to_name may be a symlink,
535 * so use realpath to resolve only the directory.
537 to_name_copy
= strdup(to_name
);
538 if (to_name_copy
== NULL
)
539 err(EX_OSERR
, "%s: strdup", to_name
);
540 cp
= dirname(to_name_copy
);
541 if (realpath(cp
, dst
) == NULL
)
542 err(EX_OSERR
, "%s: realpath", cp
);
543 /* .. and add the last component. */
544 if (strcmp(dst
, "/") != 0) {
545 if (strlcat(dst
, "/", sizeof(dst
)) > sizeof(dst
))
546 errx(1, "resolved pathname too long");
548 strcpy(to_name_copy
, to_name
);
549 cp
= basename(to_name_copy
);
550 if (strlcat(dst
, cp
, sizeof(dst
)) > sizeof(dst
))
551 errx(1, "resolved pathname too long");
554 /* Trim common path components. */
555 for (s
= src
, d
= dst
; *s
== *d
; s
++, d
++)
560 /* Count the number of directories we need to backtrack. */
561 for (++d
, lnk
[0] = '\0'; *d
; d
++)
563 (void)strlcat(lnk
, "../", sizeof(lnk
));
565 (void)strlcat(lnk
, ++s
, sizeof(lnk
));
567 do_symlink(lnk
, to_name
, target_sb
);
568 /* XXX: Link may point outside of destdir. */
573 * If absolute or relative was not specified, try the names the
576 do_symlink(from_name
, to_name
, target_sb
);
577 /* XXX: from_name may point outside of destdir. */
582 * build a path name and install the file
585 install(const char *from_name
, const char *to_name
, u_long fset
, u_long fclr
,
588 struct stat from_sb
, temp_sb
, to_sb
;
590 int devnull
, files_match
, from_fd
, serrno
, target
;
591 int tempcopy
, temp_fd
, to_fd
;
592 #ifdef _ST_FLAGS_PRESENT_
595 char backup
[MAXPATHLEN
], *p
, pathbuf
[MAXPATHLEN
], tempfile
[MAXPATHLEN
];
601 /* If try to install NULL file to a directory, fails. */
602 if (flags
& DIRECTORY
|| strcmp(from_name
, _PATH_DEVNULL
)) {
604 if (stat(from_name
, &from_sb
))
605 err(EX_OSERR
, "%s", from_name
);
606 if (!S_ISREG(from_sb
.st_mode
)) {
608 err(EX_OSERR
, "%s", from_name
);
611 /* Build the target path. */
612 if (flags
& DIRECTORY
) {
613 (void)snprintf(pathbuf
, sizeof(pathbuf
), "%s%s%s",
615 to_name
[strlen(to_name
) - 1] == '/' ? "" : "/",
616 (p
= strrchr(from_name
, '/')) ? ++p
: from_name
);
624 target
= (lstat(to_name
, &to_sb
) == 0);
627 if (target
&& !safecopy
) {
628 if (to_sb
.st_mode
& S_IFDIR
&& rmdir(to_name
) == -1)
629 err(EX_OSERR
, "%s", to_name
);
630 #ifdef _ST_FLAGS_PRESENT_
631 if (to_sb
.st_flags
& NOCHANGEBITS
)
632 (void)chflags(to_name
,
633 to_sb
.st_flags
& ~NOCHANGEBITS
);
637 makelink(from_name
, to_name
, target
? &to_sb
: NULL
);
641 if (target
&& !S_ISREG(to_sb
.st_mode
) && !S_ISLNK(to_sb
.st_mode
)) {
647 /* Only copy safe if the target exists. */
648 tempcopy
= safecopy
&& target
;
650 if (!devnull
&& (from_fd
= open(from_name
, O_RDONLY
, 0)) < 0)
651 err(EX_OSERR
, "%s", from_name
);
653 /* If we don't strip, we can compare first. */
654 if (docompare
&& !dostrip
&& target
&& S_ISREG(to_sb
.st_mode
)) {
655 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
656 err(EX_OSERR
, "%s", to_name
);
658 files_match
= to_sb
.st_size
== 0;
660 files_match
= !(compare(from_fd
, from_name
,
661 (size_t)from_sb
.st_size
, to_fd
,
662 to_name
, (size_t)to_sb
.st_size
));
664 /* Close "to" file unless we match. */
671 to_fd
= create_tempfile(to_name
, tempfile
,
674 err(EX_OSERR
, "%s", tempfile
);
676 if ((to_fd
= create_newfile(to_name
, target
,
678 err(EX_OSERR
, "%s", to_name
);
680 (void)printf("install: %s -> %s\n",
684 copy(from_fd
, from_name
, to_fd
,
685 tempcopy
? tempfile
: to_name
, from_sb
.st_size
);
689 strip(tempcopy
? tempfile
: to_name
);
692 * Re-open our fd on the target, in case we used a strip
693 * that does not work in-place -- like GNU binutils strip.
696 to_fd
= open(tempcopy
? tempfile
: to_name
, O_RDONLY
, 0);
698 err(EX_OSERR
, "stripping %s", to_name
);
702 * Compare the stripped temp file with the target.
704 if (docompare
&& dostrip
&& target
&& S_ISREG(to_sb
.st_mode
)) {
707 /* Re-open to_fd using the real target name. */
708 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
709 err(EX_OSERR
, "%s", to_name
);
711 if (fstat(temp_fd
, &temp_sb
)) {
713 (void)unlink(tempfile
);
715 err(EX_OSERR
, "%s", tempfile
);
718 if (compare(temp_fd
, tempfile
, (size_t)temp_sb
.st_size
, to_fd
,
719 to_name
, (size_t)to_sb
.st_size
) == 0) {
721 * If target has more than one link we need to
722 * replace it in order to snap the extra links.
723 * Need to preserve target file times, though.
725 if (to_sb
.st_nlink
!= 1) {
726 utb
.actime
= to_sb
.st_atime
;
727 utb
.modtime
= to_sb
.st_mtime
;
728 utime(tempfile
, &utb
);
731 (void)unlink(tempfile
);
733 (void) close(temp_fd
);
738 * Move the new file into place if doing a safe copy
739 * and the files are different (or just not compared).
741 if (tempcopy
&& !files_match
) {
742 #ifdef _ST_FLAGS_PRESENT_
743 /* Try to turn off the immutable bits. */
744 if (to_sb
.st_flags
& NOCHANGEBITS
)
745 (void)chflags(to_name
, to_sb
.st_flags
& ~NOCHANGEBITS
);
748 if ((size_t)snprintf(backup
, MAXPATHLEN
, "%s%s", to_name
,
749 suffix
) != strlen(to_name
) + strlen(suffix
)) {
751 errx(EX_OSERR
, "%s: backup filename too long",
755 (void)printf("install: %s -> %s\n", to_name
, backup
);
756 if (rename(to_name
, backup
) < 0) {
760 err(EX_OSERR
, "rename: %s to %s", to_name
,
765 (void)printf("install: %s -> %s\n", from_name
, to_name
);
766 if (rename(tempfile
, to_name
) < 0) {
770 err(EX_OSERR
, "rename: %s to %s",
774 /* Re-open to_fd so we aren't hosed by the rename(2). */
776 if ((to_fd
= open(to_name
, O_RDONLY
, 0)) < 0)
777 err(EX_OSERR
, "%s", to_name
);
781 * Preserve the timestamp of the source file if necessary.
783 if (dopreserve
&& !files_match
&& !devnull
) {
784 utb
.actime
= from_sb
.st_atime
;
785 utb
.modtime
= from_sb
.st_mtime
;
786 utime(to_name
, &utb
);
789 if (fstat(to_fd
, &to_sb
) == -1) {
791 (void)unlink(to_name
);
793 err(EX_OSERR
, "%s", to_name
);
797 * Set owner, group, mode for target; do the chown first,
798 * chown may lose the setuid bits.
800 #ifdef _ST_FLAGS_PRESENT_
801 if (!dounpriv
&& ((gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
802 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
) ||
803 (mode
!= to_sb
.st_mode
))) {
804 /* Try to turn off the immutable bits. */
805 if (to_sb
.st_flags
& NOCHANGEBITS
)
806 (void)fchflags(to_fd
, to_sb
.st_flags
& ~NOCHANGEBITS
);
811 (gid
!= (gid_t
)-1 && gid
!= to_sb
.st_gid
) ||
812 (uid
!= (uid_t
)-1 && uid
!= to_sb
.st_uid
)))
813 if (fchown(to_fd
, uid
, gid
) == -1) {
815 (void)unlink(to_name
);
817 err(EX_OSERR
,"%s: chown/chgrp", to_name
);
820 if (mode
!= to_sb
.st_mode
) {
822 dounpriv
? mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
) : mode
)) {
824 (void)unlink(to_name
);
826 err(EX_OSERR
, "%s: chmod", to_name
);
831 * If provided a set of flags, set them, otherwise, preserve the
832 * flags, except for the dump and history flags. The dump flag
833 * is left clear on the target while the history flag from when
834 * the target was created (which is inherited from the target's
835 * parent directory) is retained.
837 #ifdef _ST_FLAGS_PRESENT_
838 if (flags
& SETFLAGS
) {
839 nfset
= (to_sb
.st_flags
| fset
) & ~fclr
;
841 nfset
= (from_sb
.st_flags
& ~(UF_NODUMP
| UF_NOHISTORY
)) |
842 (to_sb
.st_flags
& UF_NOHISTORY
);
846 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
847 * trying to turn off UF_NODUMP. If we're trying to set real flags,
848 * then warn if the fs doesn't support it, otherwise fail.
850 if (!dounpriv
&& !devnull
&& fchflags(to_fd
, nfset
)) {
851 if (flags
& SETFLAGS
) {
852 if (errno
== EOPNOTSUPP
)
853 warn("%s: chflags", to_name
);
856 (void)unlink(to_name
);
858 err(EX_OSERR
, "%s: chflags", to_name
);
866 (void)close(from_fd
);
871 * compare two files; non-zero means files differ
874 compare(int from_fd
, const char *from_name __unused
, size_t from_len
,
875 int to_fd
, const char *to_name __unused
, size_t to_len
)
882 if (from_len
!= to_len
)
885 if (from_len
<= MAX_CMP_SIZE
) {
887 if (trymmap(from_fd
) && trymmap(to_fd
)) {
888 p
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
,
890 if (p
== (char *)MAP_FAILED
)
892 q
= mmap(NULL
, from_len
, PROT_READ
, MAP_SHARED
,
894 if (q
== (char *)MAP_FAILED
) {
899 rv
= memcmp(p
, q
, from_len
);
911 lseek(from_fd
, 0, SEEK_SET
);
912 lseek(to_fd
, 0, SEEK_SET
);
914 n1
= read(from_fd
, buf1
, sizeof(buf1
));
918 n2
= read(to_fd
, buf2
, n1
);
920 rv
= memcmp(buf1
, buf2
, n1
);
922 rv
= 1; /* out of sync */
924 rv
= 1; /* read failure */
926 lseek(from_fd
, 0, SEEK_SET
);
927 lseek(to_fd
, 0, SEEK_SET
);
930 rv
= 1; /* don't bother in this case */
937 * create a temporary file based on path and open it
940 create_tempfile(const char *path
, char *temp
, size_t tsize
)
944 (void)strncpy(temp
, path
, tsize
);
945 temp
[tsize
- 1] = '\0';
946 if ((p
= strrchr(temp
, '/')) != NULL
)
950 (void)strncpy(p
, "INS@XXXXXX", &temp
[tsize
- 1] - p
);
951 temp
[tsize
- 1] = '\0';
952 return (mkstemp(temp
));
957 * create a new file, overwriting an existing one if necessary
960 create_newfile(const char *path
, int target
, struct stat
*sbp
)
962 char backup
[MAXPATHLEN
];
966 * Unlink now... avoid ETXTBSY errors later. Try to turn
967 * off the append/immutable bits -- if we fail, go ahead,
970 #ifdef _ST_FLAGS_PRESENT_
971 if (sbp
->st_flags
& NOCHANGEBITS
)
972 (void)chflags(path
, sbp
->st_flags
& ~NOCHANGEBITS
);
976 if ((size_t)snprintf(backup
, MAXPATHLEN
, "%s%s",
977 path
, suffix
) != strlen(path
) + strlen(suffix
))
978 errx(EX_OSERR
, "%s: backup filename too long",
980 (void)snprintf(backup
, MAXPATHLEN
, "%s%s",
983 (void)printf("install: %s -> %s\n",
985 if (rename(path
, backup
) < 0)
986 err(EX_OSERR
, "rename: %s to %s", path
, backup
);
991 return (open(path
, O_CREAT
| O_RDWR
| O_TRUNC
, S_IRUSR
| S_IWUSR
));
996 * copy from one file to another
999 copy(int from_fd
, const char *from_name
, int to_fd
,
1000 const char *to_name
, off_t size
)
1008 /* Rewind file descriptors. */
1009 if (lseek(from_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
1010 err(EX_OSERR
, "lseek: %s", from_name
);
1011 if (lseek(to_fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1)
1012 err(EX_OSERR
, "lseek: %s", to_name
);
1015 * Mmap and write if less than 8M (the limit is so we don't totally
1016 * trash memory on big files. This is really a minor hack, but it
1017 * wins some CPU back.
1020 if (size
<= 8 * 1048576 && trymmap(from_fd
) &&
1021 (p
= mmap(NULL
, (size_t)size
, PROT_READ
, MAP_SHARED
,
1022 from_fd
, (off_t
)0)) != (char *)MAP_FAILED
) {
1023 nw
= write(to_fd
, p
, size
);
1026 (void)unlink(to_name
);
1027 errno
= nw
> 0 ? EIO
: serrno
;
1028 err(EX_OSERR
, "%s", to_name
);
1033 while ((nr
= read(from_fd
, buf
, sizeof(buf
))) > 0) {
1034 if ((nw
= write(to_fd
, buf
, nr
)) != nr
) {
1036 (void)unlink(to_name
);
1037 errno
= nw
> 0 ? EIO
: serrno
;
1038 err(EX_OSERR
, "%s", to_name
);
1043 (void)unlink(to_name
);
1045 err(EX_OSERR
, "%s", from_name
);
1052 * use strip(1) to strip the target file
1055 strip(const char *to_name
)
1057 const char *stripbin
;
1063 (void)unlink(to_name
);
1065 err(EX_TEMPFAIL
, "fork");
1067 stripbin
= getenv("STRIPBIN");
1068 if (stripbin
== NULL
)
1070 execlp(stripbin
, stripbin
, to_name
, NULL
);
1071 err(EX_OSERR
, "exec(%s)", stripbin
);
1073 if (wait(&status
) == -1 || status
) {
1075 (void)unlink(to_name
);
1076 errc(EX_SOFTWARE
, serrno
, "wait");
1083 * When doing a concurrent make -j N multiple install's can race the mkdir.
1087 mkdir_race(const char *path
, int nmode
)
1092 res
= mkdir(path
, nmode
);
1093 if (res
< 0 && errno
== EEXIST
) {
1094 if (stat(path
, &sb
) == 0 && S_ISDIR(sb
.st_mode
))
1096 res
= mkdir(path
, nmode
);
1103 * build directory hierarchy
1106 install_dir(char *path
)
1112 for (p
= path
;; ++p
)
1113 if (!*p
|| (p
!= path
&& *p
== '/')) {
1116 if (stat(path
, &sb
)) {
1117 if (errno
!= ENOENT
||
1118 mkdir_race(path
, 0755) < 0) {
1119 err(EX_OSERR
, "mkdir %s", path
);
1122 (void)printf("install: mkdir %s\n",
1124 } else if (!S_ISDIR(sb
.st_mode
))
1125 errx(EX_OSERR
, "%s exists but is not a directory", path
);
1131 if ((gid
!= (gid_t
)-1 || uid
!= (uid_t
)-1) &&
1132 chown(path
, uid
, gid
))
1133 warn("chown %u:%u %s", uid
, gid
, path
);
1134 /* XXXBED: should we do the chmod in the dounpriv case? */
1135 if (chmod(path
, mode
))
1136 warn("chmod %o %s", mode
, path
);
1142 * print a usage message and die
1148 "usage: install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
1149 " [-D dest] [-h hash]\n"
1150 " [-B suffix] [-l linkflags] [-N dbdir]\n"
1152 " install [-bCcpSsUv] [-B suffix] [-D dest] [-f flags] [-g group]\n"
1153 " [-N dbdir] [-m mode] [-o owner] file1 ... fileN directory\n"
1154 " install -d [-lUv] [-D dest] [-g group] [-m mode] [-N dbdir] [-o owner]\n"
1155 " directory ...\n");
1162 * return true (1) if mmap should be tried, false (0) if not.
1168 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
1169 * pre-Lite2-merge systems.
1174 if (nommap
|| fstatfs(fd
, &stfs
) != 0)
1176 if (strcmp(stfs
.f_fstypename
, "ufs") == 0 ||
1177 strcmp(stfs
.f_fstypename
, "cd9660") == 0)