VirtualBox

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

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

Always use the GNU make getopt stuff.

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