VirtualBox

source: kBuild/trunk/src/gmakenew/kmkbuiltin/install.c@ 902

Last change on this file since 902 was 809, checked in by bird, 18 years ago

Solaris + cleanup.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette