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

Last change on this file since 2489 was 2486, checked in by bird, 14 years ago

kmk_install: Windows fixes.

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