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