install(1): Add -U option from FreeBSD
[dragonfly.git] / usr.bin / xinstall / xinstall.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
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
27 * SUCH DAMAGE.
1de703da
MD
28 *
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 $
984263bc
MD
32 */
33
984263bc
MD
34#include <sys/param.h>
35#include <sys/mman.h>
36#include <sys/mount.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39
40#include <ctype.h>
41#include <err.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <grp.h>
45#include <paths.h>
46#include <pwd.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <sysexits.h>
51#include <unistd.h>
52#include <utime.h>
53
984263bc
MD
54/* Bootstrap aid - this doesn't exist in most older releases */
55#ifndef MAP_FAILED
56#define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
57#endif
349c725b
MD
58#ifndef UF_NOHISTORY
59#define UF_NOHISTORY 0
60#endif
984263bc
MD
61
62#define MAX_CMP_SIZE (16 * 1024 * 1024)
63
64#define DIRECTORY 0x01 /* Tell install it's a directory. */
65#define SETFLAGS 0x02 /* Tell install to set flags. */
66#define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
67#define BACKUP_SUFFIX ".old"
68
f18767d2 69static struct passwd *pp;
70static struct group *gp;
71static gid_t gid;
72static uid_t uid;
60af0858 73static int dobackup, docompare, dodir, dopreserve, dostrip, dounpriv, nommap,
74 safecopy, verbose;
f18767d2 75static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
76static const char *suffix = BACKUP_SUFFIX;
23d5265f 77static char *destdir, *fflags;
984263bc 78
2d3086b4
SW
79static int file_getgroup(const char *etcdir, const char *group, gid_t *gidret);
80static int file_getowner(const char *etcdir, const char *owner, uid_t *uidret);
4e72d2de 81
a88cc817
JM
82static int compare(int, const char *, size_t, int, const char *, size_t);
83static void copy(int, const char *, int, const char *, off_t);
84static int create_newfile(const char *, int, struct stat *);
85static int create_tempfile(const char *, char *, size_t);
86static void install(const char *, const char *, u_long, u_long, u_int);
87static void install_dir(char *);
984263bc 88u_long numeric_id(const char *, const char *);
a88cc817
JM
89static void strip(const char *);
90static int trymmap(int);
91static void usage(void);
984263bc
MD
92
93int
a88cc817 94main(int argc, char *argv[])
984263bc
MD
95{
96 struct stat from_sb, to_sb;
97 mode_t *set;
98 u_long fset;
c7a88692 99 u_long fclr;
984263bc 100 int ch, no_target;
4e72d2de 101 int trysys;
984263bc 102 u_int iflags;
984263bc 103 const char *group, *owner, *to_name;
4e72d2de 104 const char *etcdir;
984263bc
MD
105
106 iflags = 0;
4e72d2de
MD
107 trysys = 0;
108 group = NULL;
109 owner = NULL;
110 etcdir = NULL;
111
60af0858 112 while ((ch = getopt(argc, argv, "L:B:bCcD:df:g:lMm:o:pSsUv")) != -1)
984263bc
MD
113 switch((char)ch) {
114 case 'B':
115 suffix = optarg;
116 /* FALLTHROUGH */
117 case 'b':
118 dobackup = 1;
119 break;
120 case 'C':
121 docompare = 1;
122 break;
123 case 'c':
124 /* For backwards compatibility. */
125 break;
a2bc4e56
FT
126 case 'D':
127 destdir = optarg;
128 break;
a88cc817
JM
129 case 'd':
130 dodir = 1;
131 break;
984263bc 132 case 'f':
23d5265f 133 fflags = optarg;
984263bc
MD
134 break;
135 case 'g':
136 group = optarg;
137 break;
a88cc817
JM
138 case 'L':
139 etcdir = optarg;
140 break;
4e72d2de
MD
141 case 'l':
142 trysys = 1;
143 break;
984263bc
MD
144 case 'M':
145 nommap = 1;
146 break;
147 case 'm':
148 if (!(set = setmode(optarg)))
149 errx(EX_USAGE, "invalid file mode: %s",
150 optarg);
151 mode = getmode(set, 0);
152 free(set);
153 break;
154 case 'o':
155 owner = optarg;
156 break;
157 case 'p':
158 docompare = dopreserve = 1;
159 break;
160 case 'S':
161 safecopy = 1;
162 break;
163 case 's':
164 dostrip = 1;
165 break;
60af0858 166 case 'U':
167 dounpriv = 1;
168 break;
984263bc
MD
169 case 'v':
170 verbose = 1;
171 break;
172 case '?':
173 default:
174 usage();
175 }
176 argc -= optind;
177 argv += optind;
178
179 /* some options make no sense when creating directories */
a88cc817
JM
180 if (dostrip && dodir) {
181 warnx("-d and -s may not be specified together");
984263bc 182 usage();
a88cc817 183 }
984263bc
MD
184
185 /* must have at least two arguments, except when creating directories */
186 if (argc < 2 && !dodir)
187 usage();
188
189 /* need to make a temp copy so we can compare stripped version */
190 if (docompare && dostrip)
191 safecopy = 1;
192
4e72d2de
MD
193 /* no etcdir specified, always try the system */
194 if (etcdir == NULL)
195 trysys = 1;
196 uid = (uid_t)-1;
197 gid = (gid_t)-1;
198
984263bc 199 /* get group and owner id's */
60af0858 200 if (group != NULL && !dounpriv) {
4e72d2de
MD
201 if (etcdir && file_getgroup(etcdir, group, &gid)) {
202 ;
203 } else if (trysys && (gp = getgrnam(group)) != NULL) {
984263bc 204 gid = gp->gr_gid;
4e72d2de 205 } else {
984263bc 206 gid = (gid_t)numeric_id(group, "group");
4e72d2de
MD
207 }
208 }
984263bc 209
60af0858 210 if (owner != NULL && !dounpriv) {
4e72d2de
MD
211 if (etcdir && file_getowner(etcdir, owner, &uid)) {
212 ;
213 } else if (trysys && (pp = getpwnam(owner)) != NULL) {
984263bc 214 uid = pp->pw_uid;
4e72d2de 215 } else {
984263bc 216 uid = (uid_t)numeric_id(owner, "user");
4e72d2de
MD
217 }
218 }
984263bc 219
60af0858 220 if (fflags != NULL && !dounpriv) {
23d5265f 221 if (strtofflags(&fflags, &fset, &fclr))
222 errx(EX_USAGE, "%s: invalid flag", fflags);
223 iflags |= SETFLAGS;
224 }
225
984263bc
MD
226 if (dodir) {
227 for (; *argv != NULL; ++argv)
228 install_dir(*argv);
229 exit(EX_OK);
230 /* NOTREACHED */
231 }
232
f18767d2 233 to_name = argv[argc - 1];
234 no_target = stat(to_name, &to_sb);
984263bc
MD
235 if (!no_target && S_ISDIR(to_sb.st_mode)) {
236 for (; *argv != to_name; ++argv)
c7a88692 237 install(*argv, to_name, fset, fclr, iflags | DIRECTORY);
984263bc
MD
238 exit(EX_OK);
239 /* NOTREACHED */
240 }
241
242 /* can't do file1 file2 directory/file */
a88cc817
JM
243 if (argc != 2) {
244 if (no_target)
245 warnx("target directory `%s' does not exist",
246 argv[argc - 1]);
247 else
248 warnx("target `%s' is not a directory",
249 argv[argc - 1]);
984263bc 250 usage();
a88cc817 251 }
984263bc
MD
252
253 if (!no_target) {
254 if (stat(*argv, &from_sb))
255 err(EX_OSERR, "%s", *argv);
256 if (!S_ISREG(to_sb.st_mode)) {
257 errno = EFTYPE;
258 err(EX_OSERR, "%s", to_name);
259 }
260 if (to_sb.st_dev == from_sb.st_dev &&
261 to_sb.st_ino == from_sb.st_ino)
262 errx(EX_USAGE,
263 "%s and %s are the same file", *argv, to_name);
264 }
c7a88692 265 install(*argv, to_name, fset, fclr, iflags);
984263bc
MD
266 exit(EX_OK);
267 /* NOTREACHED */
268}
269
270u_long
1d1731fa 271numeric_id(const char *name, const char *type)
984263bc
MD
272{
273 u_long val;
274 char *ep;
275
276 /*
277 * XXX
278 * We know that uid_t's and gid_t's are unsigned longs.
279 */
280 errno = 0;
281 val = strtoul(name, &ep, 10);
282 if (errno)
283 err(EX_NOUSER, "%s", name);
284 if (*ep != '\0')
285 errx(EX_NOUSER, "unknown %s %s", type, name);
286 return (val);
287}
288
4e72d2de
MD
289static
290int
2d3086b4 291file_getgroup(const char *etcdir, const char *group, gid_t *gidret)
4e72d2de
MD
292{
293 FILE *fp;
294 size_t len;
295 size_t grlen;
296 char *path;
297 char *ptr;
298 char *scan;
299
300 grlen = strlen(group);
301
302 if (asprintf(&path, "%s/group", etcdir) < 0)
303 errx(EX_OSERR, "asprintf()");
304 if ((fp = fopen(path, "r")) != NULL) {
305 while ((ptr = fgetln(fp, &len)) != NULL && len) {
306 ptr[len - 1] = 0;
307 if ((scan = strchr(ptr, ':')) == NULL)
308 continue;
2d3086b4 309 if ((size_t)(scan - ptr) != grlen)
4e72d2de
MD
310 continue;
311 if (strncmp(ptr, group, grlen) != 0)
312 continue;
313 if ((scan = strchr(scan + 1, ':')) == NULL)
314 continue;
2d3086b4 315 *gidret = strtoul(scan + 1, NULL, 10);
4e72d2de
MD
316 break;
317 }
318 fclose(fp);
319 }
320 free(path);
2d3086b4 321 return((*gidret == (gid_t)-1) ? 0 : 1);
4e72d2de
MD
322}
323
324static
325int
2d3086b4 326file_getowner(const char *etcdir, const char *owner, uid_t *uidret)
4e72d2de
MD
327{
328 FILE *fp;
329 size_t len;
330 size_t owner_len;
331 char *path;
332 char *ptr;
333 char *scan;
334
335 owner_len = strlen(owner);
336
337 if (asprintf(&path, "%s/master.passwd", etcdir) < 0)
338 errx(EX_OSERR, "asprintf()");
339 if ((fp = fopen(path, "r")) != NULL) {
340 while ((ptr = fgetln(fp, &len)) != NULL && len) {
341 ptr[len - 1] = 0;
342 if ((scan = strchr(ptr, ':')) == NULL)
343 continue;
2d3086b4 344 if ((size_t)(scan - ptr) != owner_len)
4e72d2de
MD
345 continue;
346 if (strncmp(ptr, owner, owner_len) != 0)
347 continue;
348 if ((scan = strchr(scan + 1, ':')) == NULL)
349 continue;
2d3086b4 350 *uidret = strtoul(scan + 1, NULL, 10);
4e72d2de
MD
351 break;
352 }
353 fclose(fp);
354 }
355 free(path);
2d3086b4 356 return((*uidret == (uid_t)-1) ? 0 : 1);
4e72d2de
MD
357}
358
984263bc
MD
359/*
360 * install --
361 * build a path name and install the file
362 */
a88cc817 363static void
c7a88692
MD
364install(const char *from_name, const char *to_name, u_long fset, u_long fclr,
365 u_int flags)
984263bc
MD
366{
367 struct stat from_sb, temp_sb, to_sb;
368 struct utimbuf utb;
369 int devnull, files_match, from_fd, serrno, target;
370 int tempcopy, temp_fd, to_fd;
c7a88692 371 u_long nfset;
984263bc
MD
372 char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
373
374 files_match = 0;
2d3086b4 375 from_fd = -1;
984263bc
MD
376
377 /* If try to install NULL file to a directory, fails. */
378 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
379 if (stat(from_name, &from_sb))
380 err(EX_OSERR, "%s", from_name);
381 if (!S_ISREG(from_sb.st_mode)) {
382 errno = EFTYPE;
383 err(EX_OSERR, "%s", from_name);
384 }
385 /* Build the target path. */
386 if (flags & DIRECTORY) {
a88cc817 387 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
984263bc
MD
388 to_name,
389 (p = strrchr(from_name, '/')) ? ++p : from_name);
390 to_name = pathbuf;
391 }
392 devnull = 0;
393 } else {
394 devnull = 1;
395 }
396
397 target = stat(to_name, &to_sb) == 0;
398
399 /* Only install to regular files. */
400 if (target && !S_ISREG(to_sb.st_mode)) {
401 errno = EFTYPE;
402 warn("%s", to_name);
403 return;
404 }
405
406 /* Only copy safe if the target exists. */
407 tempcopy = safecopy && target;
408
409 if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
410 err(EX_OSERR, "%s", from_name);
411
412 /* If we don't strip, we can compare first. */
413 if (docompare && !dostrip && target) {
414 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
415 err(EX_OSERR, "%s", to_name);
416 if (devnull)
417 files_match = to_sb.st_size == 0;
418 else
419 files_match = !(compare(from_fd, from_name,
420 (size_t)from_sb.st_size, to_fd,
421 to_name, (size_t)to_sb.st_size));
422
423 /* Close "to" file unless we match. */
424 if (!files_match)
a88cc817 425 (void)close(to_fd);
984263bc
MD
426 }
427
428 if (!files_match) {
429 if (tempcopy) {
430 to_fd = create_tempfile(to_name, tempfile,
431 sizeof(tempfile));
432 if (to_fd < 0)
433 err(EX_OSERR, "%s", tempfile);
434 } else {
435 if ((to_fd = create_newfile(to_name, target,
436 &to_sb)) < 0)
437 err(EX_OSERR, "%s", to_name);
438 if (verbose)
a88cc817 439 (void)printf("install: %s -> %s\n",
984263bc
MD
440 from_name, to_name);
441 }
442 if (!devnull)
443 copy(from_fd, from_name, to_fd,
444 tempcopy ? tempfile : to_name, from_sb.st_size);
445 }
446
447 if (dostrip) {
448 strip(tempcopy ? tempfile : to_name);
449
450 /*
451 * Re-open our fd on the target, in case we used a strip
452 * that does not work in-place -- like GNU binutils strip.
453 */
454 close(to_fd);
455 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
456 if (to_fd < 0)
457 err(EX_OSERR, "stripping %s", to_name);
458 }
459
460 /*
461 * Compare the stripped temp file with the target.
462 */
463 if (docompare && dostrip && target) {
464 temp_fd = to_fd;
465
466 /* Re-open to_fd using the real target name. */
467 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
468 err(EX_OSERR, "%s", to_name);
469
470 if (fstat(temp_fd, &temp_sb)) {
471 serrno = errno;
a88cc817 472 (void)unlink(tempfile);
984263bc
MD
473 errno = serrno;
474 err(EX_OSERR, "%s", tempfile);
475 }
476
477 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
478 to_name, (size_t)to_sb.st_size) == 0) {
479 /*
480 * If target has more than one link we need to
481 * replace it in order to snap the extra links.
482 * Need to preserve target file times, though.
483 */
484 if (to_sb.st_nlink != 1) {
485 utb.actime = to_sb.st_atime;
486 utb.modtime = to_sb.st_mtime;
739246af 487 utime(tempfile, &utb);
984263bc
MD
488 } else {
489 files_match = 1;
a88cc817 490 (void)unlink(tempfile);
984263bc 491 }
a88cc817 492 (void) close(temp_fd);
984263bc
MD
493 }
494 }
495
496 /*
497 * Move the new file into place if doing a safe copy
498 * and the files are different (or just not compared).
499 */
500 if (tempcopy && !files_match) {
501 /* Try to turn off the immutable bits. */
502 if (to_sb.st_flags & NOCHANGEBITS)
a88cc817 503 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
984263bc
MD
504 if (dobackup) {
505 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
506 suffix) != strlen(to_name) + strlen(suffix)) {
507 unlink(tempfile);
508 errx(EX_OSERR, "%s: backup filename too long",
509 to_name);
510 }
511 if (verbose)
a88cc817 512 (void)printf("install: %s -> %s\n", to_name, backup);
984263bc
MD
513 if (rename(to_name, backup) < 0) {
514 serrno = errno;
515 unlink(tempfile);
516 errno = serrno;
517 err(EX_OSERR, "rename: %s to %s", to_name,
518 backup);
519 }
520 }
521 if (verbose)
a88cc817 522 (void)printf("install: %s -> %s\n", from_name, to_name);
984263bc
MD
523 if (rename(tempfile, to_name) < 0) {
524 serrno = errno;
525 unlink(tempfile);
526 errno = serrno;
527 err(EX_OSERR, "rename: %s to %s",
528 tempfile, to_name);
529 }
530
531 /* Re-open to_fd so we aren't hosed by the rename(2). */
a88cc817 532 (void) close(to_fd);
984263bc
MD
533 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
534 err(EX_OSERR, "%s", to_name);
535 }
536
537 /*
538 * Preserve the timestamp of the source file if necessary.
539 */
540 if (dopreserve && !files_match && !devnull) {
541 utb.actime = from_sb.st_atime;
542 utb.modtime = from_sb.st_mtime;
739246af 543 utime(to_name, &utb);
984263bc
MD
544 }
545
546 if (fstat(to_fd, &to_sb) == -1) {
547 serrno = errno;
a88cc817 548 (void)unlink(to_name);
984263bc
MD
549 errno = serrno;
550 err(EX_OSERR, "%s", to_name);
551 }
552
553 /*
554 * Set owner, group, mode for target; do the chown first,
555 * chown may lose the setuid bits.
556 */
60af0858 557 if (!dounpriv && ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
984263bc 558 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
60af0858 559 (mode != to_sb.st_mode))) {
984263bc
MD
560 /* Try to turn off the immutable bits. */
561 if (to_sb.st_flags & NOCHANGEBITS)
a88cc817 562 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
984263bc
MD
563 }
564
60af0858 565 if (!dounpriv && (
566 (gid != (gid_t)-1 && gid != to_sb.st_gid) ||
567 (uid != (uid_t)-1 && uid != to_sb.st_uid)))
984263bc
MD
568 if (fchown(to_fd, uid, gid) == -1) {
569 serrno = errno;
a88cc817 570 (void)unlink(to_name);
984263bc
MD
571 errno = serrno;
572 err(EX_OSERR,"%s: chown/chgrp", to_name);
573 }
574
60af0858 575 if (mode != to_sb.st_mode) {
576 if (fchmod(to_fd,
577 dounpriv ? mode & (S_IRWXU|S_IRWXG|S_IRWXO) : mode)) {
984263bc 578 serrno = errno;
a88cc817 579 (void)unlink(to_name);
984263bc
MD
580 errno = serrno;
581 err(EX_OSERR, "%s: chmod", to_name);
582 }
60af0858 583 }
984263bc
MD
584
585 /*
586 * If provided a set of flags, set them, otherwise, preserve the
c7a88692
MD
587 * flags, except for the dump and history flags. The dump flag
588 * is left clear on the target while the history flag from when
589 * the target was created (which is inherited from the target's
590 * parent directory) is retained.
591 */
592 if (flags & SETFLAGS) {
593 nfset = (to_sb.st_flags | fset) & ~fclr;
594 } else {
2d3086b4 595 nfset = (from_sb.st_flags & ~(UF_NODUMP | UF_NOHISTORY)) |
c7a88692
MD
596 (to_sb.st_flags & UF_NOHISTORY);
597 }
598
599 /*
984263bc
MD
600 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
601 * trying to turn off UF_NODUMP. If we're trying to set real flags,
a88cc817 602 * then warn if the fs doesn't support it, otherwise fail.
984263bc 603 */
60af0858 604 if (!dounpriv && !devnull && fchflags(to_fd, nfset)) {
984263bc
MD
605 if (flags & SETFLAGS) {
606 if (errno == EOPNOTSUPP)
607 warn("%s: chflags", to_name);
608 else {
609 serrno = errno;
a88cc817 610 (void)unlink(to_name);
984263bc
MD
611 errno = serrno;
612 err(EX_OSERR, "%s: chflags", to_name);
613 }
614 }
615 }
616
a88cc817 617 (void)close(to_fd);
984263bc 618 if (!devnull)
a88cc817 619 (void)close(from_fd);
984263bc
MD
620}
621
622/*
623 * compare --
624 * compare two files; non-zero means files differ
625 */
a88cc817 626static int
984263bc
MD
627compare(int from_fd, const char *from_name __unused, size_t from_len,
628 int to_fd, const char *to_name __unused, size_t to_len)
629{
630 char *p, *q;
631 int rv;
632 int done_compare;
633
634 rv = 0;
635 if (from_len != to_len)
636 return 1;
637
638 if (from_len <= MAX_CMP_SIZE) {
639 done_compare = 0;
640 if (trymmap(from_fd) && trymmap(to_fd)) {
641 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
642 if (p == (char *)MAP_FAILED)
643 goto out;
644 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
645 if (q == (char *)MAP_FAILED) {
646 munmap(p, from_len);
647 goto out;
648 }
649
650 rv = memcmp(p, q, from_len);
651 munmap(p, from_len);
652 munmap(q, from_len);
653 done_compare = 1;
654 }
655 out:
656 if (!done_compare) {
657 char buf1[MAXBSIZE];
658 char buf2[MAXBSIZE];
659 int n1, n2;
660
661 rv = 0;
662 lseek(from_fd, 0, SEEK_SET);
663 lseek(to_fd, 0, SEEK_SET);
664 while (rv == 0) {
665 n1 = read(from_fd, buf1, sizeof(buf1));
666 if (n1 == 0)
667 break; /* EOF */
668 else if (n1 > 0) {
669 n2 = read(to_fd, buf2, n1);
670 if (n2 == n1)
671 rv = memcmp(buf1, buf2, n1);
672 else
673 rv = 1; /* out of sync */
674 } else
675 rv = 1; /* read failure */
676 }
677 lseek(from_fd, 0, SEEK_SET);
678 lseek(to_fd, 0, SEEK_SET);
679 }
680 } else
681 rv = 1; /* don't bother in this case */
682
683 return rv;
684}
685
686/*
687 * create_tempfile --
688 * create a temporary file based on path and open it
689 */
a88cc817 690static int
1d1731fa 691create_tempfile(const char *path, char *temp, size_t tsize)
984263bc
MD
692{
693 char *p;
694
a88cc817 695 (void)strncpy(temp, path, tsize);
984263bc
MD
696 temp[tsize - 1] = '\0';
697 if ((p = strrchr(temp, '/')) != NULL)
698 p++;
699 else
700 p = temp;
a88cc817 701 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
984263bc
MD
702 temp[tsize - 1] = '\0';
703 return (mkstemp(temp));
704}
705
706/*
707 * create_newfile --
708 * create a new file, overwriting an existing one if necessary
709 */
a88cc817 710static int
1d1731fa 711create_newfile(const char *path, int target, struct stat *sbp)
984263bc
MD
712{
713 char backup[MAXPATHLEN];
714
715 if (target) {
716 /*
717 * Unlink now... avoid ETXTBSY errors later. Try to turn
718 * off the append/immutable bits -- if we fail, go ahead,
719 * it might work.
720 */
721 if (sbp->st_flags & NOCHANGEBITS)
a88cc817 722 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
984263bc
MD
723
724 if (dobackup) {
725 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
726 path, suffix) != strlen(path) + strlen(suffix))
727 errx(EX_OSERR, "%s: backup filename too long",
728 path);
a88cc817 729 (void)snprintf(backup, MAXPATHLEN, "%s%s",
984263bc
MD
730 path, suffix);
731 if (verbose)
a88cc817 732 (void)printf("install: %s -> %s\n",
984263bc
MD
733 path, backup);
734 if (rename(path, backup) < 0)
735 err(EX_OSERR, "rename: %s to %s", path, backup);
736 } else
739246af 737 unlink(path);
984263bc
MD
738 }
739
740 return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
741}
742
743/*
744 * copy --
745 * copy from one file to another
746 */
a88cc817 747static void
739246af 748copy(int from_fd, const char *from_name, int to_fd,
1d1731fa 749 const char *to_name, off_t size)
984263bc 750{
739246af 751 int nr, nw;
984263bc 752 int serrno;
f18767d2 753 char *p;
754 char buf[MAXBSIZE];
984263bc
MD
755 int done_copy;
756
757 /* Rewind file descriptors. */
758 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
759 err(EX_OSERR, "lseek: %s", from_name);
760 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
761 err(EX_OSERR, "lseek: %s", to_name);
762
763 /*
764 * Mmap and write if less than 8M (the limit is so we don't totally
765 * trash memory on big files. This is really a minor hack, but it
766 * wins some CPU back.
767 */
768 done_copy = 0;
769 if (size <= 8 * 1048576 && trymmap(from_fd) &&
770 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
771 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
772 if ((nw = write(to_fd, p, size)) != size) {
773 serrno = errno;
a88cc817 774 (void)unlink(to_name);
984263bc
MD
775 errno = nw > 0 ? EIO : serrno;
776 err(EX_OSERR, "%s", to_name);
777 }
778 done_copy = 1;
779 }
780 if (!done_copy) {
f18767d2 781 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
984263bc
MD
782 if ((nw = write(to_fd, buf, nr)) != nr) {
783 serrno = errno;
a88cc817 784 (void)unlink(to_name);
984263bc
MD
785 errno = nw > 0 ? EIO : serrno;
786 err(EX_OSERR, "%s", to_name);
787 }
f18767d2 788 }
984263bc
MD
789 if (nr != 0) {
790 serrno = errno;
a88cc817 791 (void)unlink(to_name);
984263bc
MD
792 errno = serrno;
793 err(EX_OSERR, "%s", from_name);
794 }
795 }
796}
797
798/*
799 * strip --
800 * use strip(1) to strip the target file
801 */
a88cc817 802static void
1d1731fa 803strip(const char *to_name)
984263bc
MD
804{
805 const char *stripbin;
806 int serrno, status;
807
808 switch (fork()) {
809 case -1:
810 serrno = errno;
a88cc817 811 (void)unlink(to_name);
984263bc
MD
812 errno = serrno;
813 err(EX_TEMPFAIL, "fork");
814 case 0:
815 stripbin = getenv("STRIPBIN");
816 if (stripbin == NULL)
817 stripbin = "strip";
79ac1d4a 818 execlp(stripbin, stripbin, to_name, NULL);
984263bc
MD
819 err(EX_OSERR, "exec(%s)", stripbin);
820 default:
821 if (wait(&status) == -1 || status) {
822 serrno = errno;
a88cc817 823 (void)unlink(to_name);
984263bc
MD
824 errc(EX_SOFTWARE, serrno, "wait");
825 /* NOTREACHED */
826 }
827 }
828}
829
0e6b9ee8
MD
830/*
831 * When doing a concurrent make -j N multiple install's can race the mkdir.
832 */
833static
834int
835mkdir_race(const char *path, int nmode)
836{
837 int res;
838 struct stat sb;
839
840 res = mkdir(path, nmode);
841 if (res < 0 && errno == EEXIST) {
842 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode))
843 return(0);
e019ff8c 844 res = mkdir(path, nmode);
0e6b9ee8
MD
845 }
846 return (res);
847}
848
984263bc
MD
849/*
850 * install_dir --
739246af 851 * build directory hierarchy
984263bc 852 */
a88cc817 853static void
1d1731fa 854install_dir(char *path)
984263bc 855{
739246af 856 char *p;
984263bc
MD
857 struct stat sb;
858 int ch;
859
860 for (p = path;; ++p)
861 if (!*p || (p != path && *p == '/')) {
862 ch = *p;
863 *p = '\0';
864 if (stat(path, &sb)) {
0e6b9ee8
MD
865 if (errno != ENOENT ||
866 mkdir_race(path, 0755) < 0) {
984263bc
MD
867 err(EX_OSERR, "mkdir %s", path);
868 /* NOTREACHED */
869 } else if (verbose)
a88cc817 870 (void)printf("install: mkdir %s\n",
984263bc
MD
871 path);
872 } else if (!S_ISDIR(sb.st_mode))
873 errx(EX_OSERR, "%s exists but is not a directory", path);
874 if (!(*p = ch))
875 break;
f18767d2 876 }
984263bc 877
60af0858 878 if (!dounpriv) {
879 if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
880 chown(path, uid, gid))
881 warn("chown %u:%u %s", uid, gid, path);
882 /* XXXBED: should we do the chmod in the dounpriv case? */
883 if (chmod(path, mode))
884 warn("chmod %o %s", mode, path);
885 }
984263bc
MD
886}
887
888/*
889 * usage --
890 * print a usage message and die
891 */
a88cc817 892static void
1d1731fa 893usage(void)
984263bc 894{
60af0858 895 fprintf(stderr,
896"usage: install [-bCcpSsUv] [-B suffix] [-D dest] [-f flags] [-g group]\n"
897" [-m mode] [-o owner] file1 file2\n"
898" install [-bCcpSsUv] [-B suffix] [-D dest] [-f flags] [-g group]\n"
899" [-m mode] [-o owner] file1 ... fileN directory\n"
900" install -d [-lUv] [-D dest] [-g group] [-m mode] [-o owner]\n"
901" directory ...\n");
984263bc
MD
902 exit(EX_USAGE);
903 /* NOTREACHED */
904}
905
906/*
907 * trymmap --
908 * return true (1) if mmap should be tried, false (0) if not.
909 */
a88cc817 910static int
1d1731fa 911trymmap(int fd)
984263bc
MD
912{
913/*
914 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
915 * pre-Lite2-merge systems.
916 */
917#ifdef MFSNAMELEN
918 struct statfs stfs;
919
920 if (nommap || fstatfs(fd, &stfs) != 0)
921 return (0);
922 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
923 strcmp(stfs.f_fstypename, "cd9660") == 0)
924 return (1);
925#endif
926 return (0);
927}