VirtualBox

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

最後變更 在這個檔案從1710是 1710,由 bird 提交於 17 年 前

kmk: Made chmod build on windows. Some cleanup of the bsdisms.

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

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