VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/install.c@ 1193

Last change on this file since 1193 was 1193, checked in by bird, 17 years ago

16KB -> 256KB copy buffer for windows.

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