VirtualBox

source: kBuild/branches/kBuild-0.1.5/src/kmk/kmkbuiltin/install.c@ 2321

Last change on this file since 2321 was 2113, checked in by bird, 16 years ago

kmkbuiltin: include config.h

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