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