source: trunk/src/gmake/kmkbuiltin/install.c@ 371

Last change on this file since 371 was 371, checked in by bird, 20 years ago

Fixed linux bustage and ported install to linux.

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