VirtualBox

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

Last change on this file since 3101 was 3101, checked in by bird, 8 years ago

rm.c,install.c: GNU/kFreeBSD build fixes (no fflags conversion functions).

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

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