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

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

o Ported all kmk builtins to win32.
o Fixed serveral bugs in kmk builtins.
o Probably broke both linux, bsd and OS/2. :-)

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