VirtualBox

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

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

kmk_install: Added --hard-link-files-when-possible for use with the staging.

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

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