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

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

kmk_install: always warn if mode prevents hard linking.

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