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