VirtualBox

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

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

fixed a few warnings the broken --help and --version for install.

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