VirtualBox

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

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

kmk_install,kmk: install fixes.

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

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