source: trunk/src/kmk/kmkbuiltin/install.c@ 2544

Last change on this file since 2544 was 2543, checked in by bird, 14 years ago

install.c: Don't try hardlink symlink and other non-regular files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.1 KB
Line 
1/*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1987, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
43#endif /* not lint */
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.66 2005/01/25 14:34:57 ssouhlal Exp $");
47#endif
48
49#include "config.h"
50#ifndef _MSC_VER
51# include <sys/param.h>
52# ifdef USE_MMAP
53# include <sys/mman.h>
54# endif
55# include <sys/mount.h>
56# include <sys/wait.h>
57# include <sys/time.h>
58#endif /* !_MSC_VER */
59#include <sys/stat.h>
60
61#include <ctype.h>
62#include "err.h"
63#include <errno.h>
64#include <fcntl.h>
65#include <grp.h>
66#include <paths.h>
67#include <pwd.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <sysexits.h>
72#include <unistd.h>
73#if defined(__EMX__) || defined(_MSC_VER)
74# include <process.h>
75#endif
76#include "getopt.h"
77#ifdef __sun__
78# include "solfakes.h"
79#endif
80#ifdef _MSC_VER
81# include "mscfakes.h"
82#endif
83#include "kmkbuiltin.h"
84
85
86extern void * bsd_setmode(const char *p);
87extern mode_t bsd_getmode(const void *bbox, mode_t omode);
88
89#ifndef __unused
90# define __unused
91#endif
92
93#ifndef MAXBSIZE
94# define MAXBSIZE 0x20000
95#endif
96
97/* Bootstrap aid - this doesn't exist in most older releases */
98#ifndef MAP_FAILED
99#define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
100#endif
101
102#define MAX_CMP_SIZE (16 * 1024 * 1024)
103
104#define DIRECTORY 0x01 /* Tell install it's a directory. */
105#define SETFLAGS 0x02 /* Tell install to set flags. */
106#define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
107#define BACKUP_SUFFIX ".old"
108
109#ifndef O_BINARY
110# define O_BINARY 0
111#endif
112
113#ifndef EFTYPE
114# define EFTYPE EINVAL
115#endif
116
117#if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
118# define IS_SLASH(ch) ((ch) == '/' || (ch) == '\\')
119#else
120# define IS_SLASH(ch) ((ch) == '/')
121#endif
122
123static gid_t gid;
124static uid_t uid;
125static int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose, 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#ifdef KBUILD_OS_OS2
441 const char *why_not = "not supported on OS/2";
442#else
443 const char *why_not = NULL;
444 if (devnull) {
445 why_not = "/dev/null";
446 } else if (dostrip) {
447 why_not = "strip (-s)";
448 } else if (docompare) {
449 why_not = "compare (-C)";
450 } else if (dobackup) {
451 why_not = "backup (-b/-B)";
452 } else if (safecopy) {
453 why_not = "safe copy (-S)";
454 } else if (lstat(from_name, &temp_sb)) {
455 why_not = "lstat on source failed";
456 } else if (S_ISLNK(temp_sb.st_mode)) {
457 why_not = "symlink";
458 } else if (!S_ISREG(temp_sb.st_mode)) {
459 why_not = "not regular file";
460# if defined(KBUILD_OS_WINDOWS) || defined(KBUILD_OS_OS2)
461 } else if ((mode & S_IWUSR) != (from_sb.st_mode & S_IWUSR)) {
462# else
463 } else if (mode != (from_sb.st_mode & ALLPERMS)) {
464# endif
465 printf("install: warning: Not hard linking, mode differs: 0%03o, desires 0%03o\n"
466 "install: src path '%s'\n"
467 "install: dst path '%s'\n",
468 (from_sb.st_mode & ALLPERMS), mode, from_name, to_name);
469 why_not = NULL;
470 } else if (uid != (uid_t)-1 && gid != from_sb.st_uid) {
471 why_not = "uid mismatch";
472 } else if (gid != (gid_t)-1 && gid != from_sb.st_gid) {
473 why_not = "gid mismatch";
474 } else {
475 int rcLink = link(from_name, to_name);
476 if (rcLink != 0 && errno == EEXIST) {
477 unlink(to_name);
478 rcLink = link(from_name, to_name);
479 }
480 if (rcLink == 0) {
481 if (verbose)
482 printf("install: %s -> %s (hardlinked)\n", from_name, to_name);
483 goto l_done;
484 }
485 if (verbose)
486 printf("install: hard linking '%s' to '%s' failed: %s\n",
487 to_name, from_name, strerror(errno));
488 why_not = NULL;
489 }
490#endif
491 if (verbose && why_not)
492 printf("install: not hard linking '%s' to '%s' because: %s\n",
493 to_name, from_name, why_not);
494
495 /* Can't hard link or we failed, continue as nothing happend. */
496 }
497
498 if (!devnull && (from_fd = open(from_name, O_RDONLY | O_BINARY, 0)) < 0)
499 return err(EX_OSERR, "%s", from_name);
500
501 /* If we don't strip, we can compare first. */
502 if (docompare && !dostrip && target) {
503 if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
504 rc = err(EX_OSERR, "%s", to_name);
505 goto l_done;
506 }
507 if (devnull)
508 files_match = to_sb.st_size == 0;
509 else
510 files_match = !(compare(from_fd, from_name,
511 (size_t)from_sb.st_size, to_fd,
512 to_name, (size_t)to_sb.st_size));
513
514 /* Close "to" file unless we match. */
515 if (!files_match) {
516 (void)close(to_fd);
517 to_fd = -1;
518 }
519 }
520
521 if (!files_match) {
522 if (tempcopy) {
523 to_fd = create_tempfile(to_name, tempfile,
524 sizeof(tempfile));
525 if (to_fd < 0) {
526 rc = err(EX_OSERR, "%s", tempfile);
527 goto l_done;
528 }
529 } else {
530 if ((to_fd = create_newfile(to_name, target,
531 &to_sb)) < 0) {
532 rc = err(EX_OSERR, "%s", to_name);
533 goto l_done;
534 }
535 if (verbose)
536 (void)printf("install: %s -> %s\n",
537 from_name, to_name);
538 }
539 if (!devnull) {
540 rc = copy(from_fd, from_name, to_fd,
541 tempcopy ? tempfile : to_name, from_sb.st_size);
542 if (rc)
543 goto l_done;
544 }
545 }
546
547 if (dostrip) {
548#if defined(__EMX__) || defined(_MSC_VER)
549 /* close before we strip. */
550 close(to_fd);
551 to_fd = -1;
552#endif
553 rc = strip(tempcopy ? tempfile : to_name);
554 if (rc)
555 goto l_done;
556
557 /*
558 * Re-open our fd on the target, in case we used a strip
559 * that does not work in-place -- like GNU binutils strip.
560 */
561#if !defined(__EMX__) && !defined(_MSC_VER)
562 close(to_fd);
563#endif
564 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY | O_BINARY, 0);
565 if (to_fd < 0) {
566 rc = err(EX_OSERR, "stripping %s", to_name);
567 goto l_done;
568 }
569 }
570
571 /*
572 * Compare the stripped temp file with the target.
573 */
574 if (docompare && dostrip && target) {
575 temp_fd = to_fd;
576
577 /* Re-open to_fd using the real target name. */
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 if (fstat(temp_fd, &temp_sb)) {
584 serrno = errno;
585 (void)unlink(tempfile);
586 errno = serrno;
587 rc = err(EX_OSERR, "%s", tempfile);
588 goto l_done;
589 }
590
591 if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
592 to_name, (size_t)to_sb.st_size) == 0) {
593 /*
594 * If target has more than one link we need to
595 * replace it in order to snap the extra links.
596 * Need to preserve target file times, though.
597 */
598#if !defined(_MSC_VER) && !defined(__EMX__)
599 if (to_sb.st_nlink != 1) {
600 tvb[0].tv_sec = to_sb.st_atime;
601 tvb[0].tv_usec = 0;
602 tvb[1].tv_sec = to_sb.st_mtime;
603 tvb[1].tv_usec = 0;
604 (void)utimes(tempfile, tvb);
605 } else
606#endif
607 {
608
609 files_match = 1;
610 (void)unlink(tempfile);
611 }
612 (void) close(temp_fd);
613 temp_fd = -1;
614 }
615 }
616
617 /*
618 * Move the new file into place if doing a safe copy
619 * and the files are different (or just not compared).
620 */
621 if (tempcopy && !files_match) {
622#ifdef UF_IMMUTABLE
623 /* Try to turn off the immutable bits. */
624 if (to_sb.st_flags & NOCHANGEBITS)
625 (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
626#endif
627 if (dobackup) {
628 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
629 suffix) != strlen(to_name) + strlen(suffix)) {
630 unlink(tempfile);
631 rc = errx(EX_OSERR, "%s: backup filename too long",
632 to_name);
633 goto l_done;
634 }
635 if (verbose)
636 (void)printf("install: %s -> %s\n", to_name, backup);
637 if (rename(to_name, backup) < 0) {
638 serrno = errno;
639 unlink(tempfile);
640 errno = serrno;
641 rc = err(EX_OSERR, "rename: %s to %s", to_name,
642 backup);
643 goto l_done;
644 }
645 }
646 if (verbose)
647 (void)printf("install: %s -> %s\n", from_name, to_name);
648 if (rename(tempfile, to_name) < 0) {
649 serrno = errno;
650 unlink(tempfile);
651 errno = serrno;
652 rc = err(EX_OSERR, "rename: %s to %s",
653 tempfile, to_name);
654 goto l_done;
655 }
656
657 /* Re-open to_fd so we aren't hosed by the rename(2). */
658 (void) close(to_fd);
659 if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
660 rc = err(EX_OSERR, "%s", to_name);
661 goto l_done;
662 }
663 }
664
665 /*
666 * Preserve the timestamp of the source file if necessary.
667 */
668 if (dopreserve && !files_match && !devnull) {
669 tvb[0].tv_sec = from_sb.st_atime;
670 tvb[0].tv_usec = 0;
671 tvb[1].tv_sec = from_sb.st_mtime;
672 tvb[1].tv_usec = 0;
673 (void)utimes(to_name, tvb);
674 }
675
676 if (fstat(to_fd, &to_sb) == -1) {
677 serrno = errno;
678 (void)unlink(to_name);
679 errno = serrno;
680 rc = err(EX_OSERR, "%s", to_name);
681 goto l_done;
682 }
683
684 /*
685 * Set owner, group, mode for target; do the chown first,
686 * chown may lose the setuid bits.
687 */
688#ifdef UF_IMMUTABLE
689 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
690 (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
691 (mode != (to_sb.st_mode & ALLPERMS))) {
692 /* Try to turn off the immutable bits. */
693 if (to_sb.st_flags & NOCHANGEBITS)
694 (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
695 }
696#endif
697
698 if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
699 (uid != (uid_t)-1 && uid != to_sb.st_uid))
700 if (fchown(to_fd, uid, gid) == -1) {
701 if (errno == EPERM && ignore_perm_errors) {
702 warn("%s: ignoring chown uid=%d gid=%d failure", to_name, (int)uid, (int)gid);
703 } else {
704 serrno = errno;
705 (void)unlink(to_name);
706 errno = serrno;
707 rc = err(EX_OSERR,"%s: chown/chgrp", to_name);
708 goto l_done;
709 }
710 }
711
712 if (mode != (to_sb.st_mode & ALLPERMS))
713 if (fchmod(to_fd, mode)) {
714 serrno = errno;
715 if (serrno == EPERM && ignore_perm_errors) {
716 fchmod(to_fd, mode & (ALLPERMS & ~0007000));
717 errno = errno;
718 warn("%s: ignoring chmod 0%o failure", to_name, (int)(mode & ALLPERMS));
719 } else {
720 serrno = errno;
721 (void)unlink(to_name);
722 errno = serrno;
723 rc = err(EX_OSERR, "%s: chmod", to_name);
724 goto l_done;
725 }
726 }
727
728 /*
729 * If provided a set of flags, set them, otherwise, preserve the
730 * flags, except for the dump flag.
731 * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
732 * trying to turn off UF_NODUMP. If we're trying to set real flags,
733 * then warn if the the fs doesn't support it, otherwise fail.
734 */
735#ifdef UF_IMMUTABLE
736 if (!devnull && (flags & SETFLAGS ||
737 (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
738 fchflags(to_fd,
739 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
740 if (flags & SETFLAGS) {
741 if (errno == EOPNOTSUPP)
742 warn("%s: chflags", to_name);
743 else {
744 serrno = errno;
745 (void)unlink(to_name);
746 errno = serrno;
747 rc = err(EX_OSERR, "%s: chflags", to_name);
748 goto l_done;
749 }
750 }
751 }
752#endif
753
754l_done:
755 if (to_fd >= 0)
756 (void)close(to_fd);
757 if (temp_fd >= 0)
758 (void)close(temp_fd);
759 if (from_fd >= 0 && !devnull)
760 (void)close(from_fd);
761 return rc;
762}
763
764/*
765 * compare --
766 * compare two files; non-zero means files differ
767 */
768static int
769compare(int from_fd, const char *from_name __unused, size_t from_len,
770 int to_fd, const char *to_name __unused, size_t to_len)
771{
772 char *p, *q;
773 int rv;
774 int done_compare;
775
776 rv = 0;
777 if (from_len != to_len)
778 return 1;
779
780 if (from_len <= MAX_CMP_SIZE) {
781#ifdef USE_MMAP
782 done_compare = 0;
783 if (trymmap(from_fd) && trymmap(to_fd)) {
784 p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
785 if (p == (char *)MAP_FAILED)
786 goto out;
787 q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
788 if (q == (char *)MAP_FAILED) {
789 munmap(p, from_len);
790 goto out;
791 }
792
793 rv = memcmp(p, q, from_len);
794 munmap(p, from_len);
795 munmap(q, from_len);
796 done_compare = 1;
797 }
798 out:
799#else
800 (void)p; (void)q;
801 done_compare = 0;
802#endif
803 if (!done_compare) {
804 char buf1[MAXBSIZE];
805 char buf2[MAXBSIZE];
806 int n1, n2;
807
808 rv = 0;
809 lseek(from_fd, 0, SEEK_SET);
810 lseek(to_fd, 0, SEEK_SET);
811 while (rv == 0) {
812 n1 = read(from_fd, buf1, sizeof(buf1));
813 if (n1 == 0)
814 break; /* EOF */
815 else if (n1 > 0) {
816 n2 = read(to_fd, buf2, n1);
817 if (n2 == n1)
818 rv = memcmp(buf1, buf2, n1);
819 else
820 rv = 1; /* out of sync */
821 } else
822 rv = 1; /* read failure */
823 }
824 lseek(from_fd, 0, SEEK_SET);
825 lseek(to_fd, 0, SEEK_SET);
826 }
827 } else
828 rv = 1; /* don't bother in this case */
829
830 return rv;
831}
832
833/*
834 * create_tempfile --
835 * create a temporary file based on path and open it
836 */
837int
838create_tempfile(const char *path, char *temp, size_t tsize)
839{
840 char *p;
841
842 (void)strncpy(temp, path, tsize);
843 temp[tsize - 1] = '\0';
844 if ((p = last_slash(temp)) != NULL)
845 p++;
846 else
847 p = temp;
848 (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
849 temp[tsize - 1] = '\0';
850 return (mkstemp(temp));
851}
852
853/*
854 * create_newfile --
855 * create a new file, overwriting an existing one if necessary
856 */
857int
858create_newfile(const char *path, int target, struct stat *sbp)
859{
860 char backup[MAXPATHLEN];
861 int saved_errno = 0;
862 int newfd;
863
864 if (target) {
865 /*
866 * Unlink now... avoid ETXTBSY errors later. Try to turn
867 * off the append/immutable bits -- if we fail, go ahead,
868 * it might work.
869 */
870#ifdef UF_IMMUTABLE
871 if (sbp->st_flags & NOCHANGEBITS)
872 (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
873#endif
874
875 if (dobackup) {
876 if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
877 path, suffix) != strlen(path) + strlen(suffix)) {
878 errx(EX_OSERR, "%s: backup filename too long",
879 path);
880 errno = ENAMETOOLONG;
881 return -1;
882 }
883 (void)snprintf(backup, MAXPATHLEN, "%s%s",
884 path, suffix);
885 if (verbose)
886 (void)printf("install: %s -> %s\n",
887 path, backup);
888 if (rename(path, backup) < 0) {
889 err(EX_OSERR, "rename: %s to %s", path, backup);
890 return -1;
891 }
892 } else
893 if (unlink(path) < 0)
894 saved_errno = errno;
895 }
896
897 newfd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
898 if (newfd < 0 && saved_errno != 0)
899 errno = saved_errno;
900 return newfd;
901}
902
903/*
904 * copy --
905 * copy from one file to another
906 */
907static int
908copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
909 off_t size)
910{
911 int nr, nw;
912 int serrno;
913 char *p, buf[MAXBSIZE];
914 int done_copy;
915
916 /* Rewind file descriptors. */
917 if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
918 return err(EX_OSERR, "lseek: %s", from_name);
919 if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
920 return err(EX_OSERR, "lseek: %s", to_name);
921
922 /*
923 * Mmap and write if less than 8M (the limit is so we don't totally
924 * trash memory on big files. This is really a minor hack, but it
925 * wins some CPU back.
926 */
927 done_copy = 0;
928#ifdef USE_MMAP
929 if (size <= 8 * 1048576 && trymmap(from_fd) &&
930 (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
931 from_fd, (off_t)0)) != (char *)MAP_FAILED) {
932 if ((nw = write(to_fd, p, size)) != size) {
933 serrno = errno;
934 (void)unlink(to_name);
935 errno = nw > 0 ? EIO : serrno;
936 err(EX_OSERR, "%s", to_name);
937 }
938 done_copy = 1;
939 }
940#else
941 (void)p; (void)size;
942#endif
943 if (!done_copy) {
944 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
945 if ((nw = write(to_fd, buf, nr)) != nr) {
946 serrno = errno;
947 (void)unlink(to_name);
948 errno = nw > 0 ? EIO : serrno;
949 return err(EX_OSERR, "%s", to_name);
950 }
951 if (nr != 0) {
952 serrno = errno;
953 (void)unlink(to_name);
954 errno = serrno;
955 return err(EX_OSERR, "%s", from_name);
956 }
957 }
958 return EX_OK;
959}
960
961/*
962 * strip --
963 * use strip(1) to strip the target file
964 */
965static int
966strip(const char *to_name)
967{
968#if defined(__EMX__) || defined(_MSC_VER)
969 const char *stripbin = getenv("STRIPBIN");
970 if (stripbin == NULL)
971 stripbin = "strip";
972 return spawnlp(P_WAIT, stripbin, stripbin, to_name, NULL);
973#else
974 const char *stripbin;
975 int serrno, status;
976 pid_t pid;
977
978 pid = fork();
979 switch (pid) {
980 case -1:
981 serrno = errno;
982 (void)unlink(to_name);
983 errno = serrno;
984 return err(EX_TEMPFAIL, "fork");
985 case 0:
986 stripbin = getenv("STRIPBIN");
987 if (stripbin == NULL)
988 stripbin = "strip";
989 execlp(stripbin, stripbin, to_name, (char *)NULL);
990 err(EX_OSERR, "exec(%s)", stripbin);
991 exit(EX_OSERR);
992 default:
993 if (waitpid(pid, &status, 0) == -1 || status) {
994 serrno = errno;
995 (void)unlink(to_name);
996 errno = serrno;
997 return err(EX_SOFTWARE, "waitpid");
998 /* NOTREACHED */
999 }
1000 }
1001 return 0;
1002#endif
1003}
1004
1005/*
1006 * install_dir --
1007 * build directory heirarchy
1008 */
1009static int
1010install_dir(char *path)
1011{
1012 char *p;
1013 struct stat sb;
1014 int ch;
1015
1016 for (p = path;; ++p)
1017 if (!*p || (p != path && IS_SLASH(*p))) {
1018 ch = *p;
1019 *p = '\0';
1020 if (stat(path, &sb)) {
1021 if (errno != ENOENT || mkdir(path, 0755) < 0) {
1022 return err(EX_OSERR, "mkdir %s", path);
1023 /* NOTREACHED */
1024 } else if (verbose)
1025 (void)printf("install: mkdir %s\n",
1026 path);
1027 } else if (!S_ISDIR(sb.st_mode))
1028 return errx(EX_OSERR, "%s exists but is not a directory", path);
1029 if (!(*p = ch))
1030 break;
1031 }
1032
1033 if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
1034 warn("chown %u:%u %s", uid, gid, path);
1035 if (chmod(path, mode))
1036 warn("chmod %o %s", mode, path);
1037 return EX_OK;
1038}
1039
1040/*
1041 * usage --
1042 * print a usage message and die
1043 */
1044static int
1045usage(FILE *pf)
1046{
1047 fprintf(pf,
1048"usage: %s [-bCcpSsv] [--[no-]hard-link-files-when-possible]\n"
1049" [--[no-]ignore-perm-errors] [-B suffix] [-f flags]\n"
1050" [-g group] [-m mode] [-o owner] file1 file2\n"
1051" or: %s [-bCcpSsv] [--[no-]ignore-perm-errors] [-B suffix] [-f flags]\n"
1052" [-g group] [-m mode] [-o owner] file1 ... fileN directory\n"
1053" or: %s -d [-v] [-g group] [-m mode] [-o owner] directory ...\n"
1054" or: %s --help\n"
1055" or: %s --version\n",
1056 g_progname, g_progname, g_progname, g_progname, g_progname);
1057 return EX_USAGE;
1058}
1059
1060#ifdef USE_MMAP
1061/*
1062 * trymmap --
1063 * return true (1) if mmap should be tried, false (0) if not.
1064 */
1065static int
1066trymmap(int fd)
1067{
1068/*
1069 * The ifdef is for bootstrapping - f_fstypename doesn't exist in
1070 * pre-Lite2-merge systems.
1071 */
1072#ifdef MFSNAMELEN
1073 struct statfs stfs;
1074
1075 if (nommap || fstatfs(fd, &stfs) != 0)
1076 return (0);
1077 if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
1078 strcmp(stfs.f_fstypename, "cd9660") == 0)
1079 return (1);
1080#endif
1081 return (0);
1082}
1083#endif
1084
1085/* figures out where the last slash or colon is. */
1086static char *
1087last_slash(const char *path)
1088{
1089#if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
1090 char *p = (char *)strrchr(path, '/');
1091 if (p)
1092 {
1093 char *p2 = strrchr(p, '\\');
1094 if (p2)
1095 p = p2;
1096 }
1097 else
1098 {
1099 p = (char *)strrchr(path, '\\');
1100 if (!p && isalpha(path[0]) && path[1] == ':')
1101 p = (char *)&path[1];
1102 }
1103 return p;
1104#else
1105 return strrchr(path, '/');
1106#endif
1107}
1108
Note: See TracBrowser for help on using the repository browser.