VirtualBox

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

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

Applied modified patches for Haiku support from Mike Smith.

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