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