1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2013-01-18 20:57:50 +00:00
|
|
|
* Copyright (c) 2012, 2013 SRI International
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1987, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2015-09-25 20:25:18 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-10-09 07:21:00 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1987, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
2002-05-12 04:02:57 +00:00
|
|
|
#endif /* not lint */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-05-12 04:02:57 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2002-05-12 04:02:57 +00:00
|
|
|
static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
|
|
|
|
#endif /* not lint */
|
1997-08-27 06:29:23 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-05-12 04:02:57 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mman.h>
|
1996-09-05 07:27:43 +00:00
|
|
|
#include <sys/mount.h>
|
2001-12-12 08:49:51 +00:00
|
|
|
#include <sys/stat.h>
|
2002-11-30 18:59:31 +00:00
|
|
|
#include <sys/time.h>
|
2001-12-12 08:49:51 +00:00
|
|
|
#include <sys/wait.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1995-10-09 07:21:00 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
2013-01-18 20:57:50 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <md5.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
2013-01-18 20:57:50 +00:00
|
|
|
#include <ripemd.h>
|
|
|
|
#include <sha.h>
|
|
|
|
#include <sha256.h>
|
|
|
|
#include <sha512.h>
|
2014-03-08 23:05:28 +00:00
|
|
|
#include <spawn.h>
|
2012-07-02 09:53:57 +00:00
|
|
|
#include <stdint.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1996-09-28 02:53:45 +00:00
|
|
|
#include <sysexits.h>
|
2001-12-11 22:46:42 +00:00
|
|
|
#include <unistd.h>
|
2013-01-18 20:57:50 +00:00
|
|
|
#include <vis.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2013-01-11 20:53:28 +00:00
|
|
|
#include "mtree.h"
|
|
|
|
|
2002-03-18 23:26:13 +00:00
|
|
|
#define MAX_CMP_SIZE (16 * 1024 * 1024)
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
#define LN_ABSOLUTE 0x01
|
|
|
|
#define LN_RELATIVE 0x02
|
|
|
|
#define LN_HARD 0x04
|
|
|
|
#define LN_SYMBOLIC 0x08
|
|
|
|
#define LN_MIXED 0x10
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#define DIRECTORY 0x01 /* Tell install it's a directory. */
|
|
|
|
#define SETFLAGS 0x02 /* Tell install to set flags. */
|
1995-10-09 07:21:00 +00:00
|
|
|
#define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
|
2001-05-28 16:41:53 +00:00
|
|
|
#define BACKUP_SUFFIX ".old"
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
typedef union {
|
|
|
|
MD5_CTX MD5;
|
|
|
|
RIPEMD160_CTX RIPEMD160;
|
|
|
|
SHA1_CTX SHA1;
|
|
|
|
SHA256_CTX SHA256;
|
|
|
|
SHA512_CTX SHA512;
|
|
|
|
} DIGEST_CTX;
|
|
|
|
|
|
|
|
static enum {
|
|
|
|
DIGEST_NONE = 0,
|
|
|
|
DIGEST_MD5,
|
|
|
|
DIGEST_RIPEMD160,
|
|
|
|
DIGEST_SHA1,
|
|
|
|
DIGEST_SHA256,
|
|
|
|
DIGEST_SHA512,
|
|
|
|
} digesttype = DIGEST_NONE;
|
|
|
|
|
2014-03-08 23:05:28 +00:00
|
|
|
extern char **environ;
|
|
|
|
|
2011-11-06 08:19:07 +00:00
|
|
|
static gid_t gid;
|
|
|
|
static uid_t uid;
|
2013-01-18 20:57:50 +00:00
|
|
|
static int dobackup, docompare, dodir, dolink, dopreserve, dostrip, dounpriv,
|
|
|
|
safecopy, verbose;
|
|
|
|
static int haveopt_f, haveopt_g, haveopt_m, haveopt_o;
|
2011-11-06 08:19:07 +00:00
|
|
|
static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
2013-01-18 20:57:50 +00:00
|
|
|
static FILE *metafp;
|
|
|
|
static const char *group, *owner;
|
2011-11-06 08:19:07 +00:00
|
|
|
static const char *suffix = BACKUP_SUFFIX;
|
2013-01-18 20:57:50 +00:00
|
|
|
static char *destdir, *digest, *fflags, *metafile, *tags;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
static int compare(int, const char *, size_t, int, const char *, size_t,
|
|
|
|
char **);
|
|
|
|
static char *copy(int, const char *, int, const char *, off_t);
|
2009-12-13 03:34:19 +00:00
|
|
|
static int create_newfile(const char *, int, struct stat *);
|
|
|
|
static int create_tempfile(const char *, char *, size_t);
|
2013-01-18 20:57:50 +00:00
|
|
|
static char *quiet_mktemp(char *template);
|
|
|
|
static char *digest_file(const char *);
|
|
|
|
static void digest_init(DIGEST_CTX *);
|
2013-10-30 18:40:55 +00:00
|
|
|
static void digest_update(DIGEST_CTX *, const char *, size_t);
|
2013-01-18 20:57:50 +00:00
|
|
|
static char *digest_end(DIGEST_CTX *, char *);
|
|
|
|
static int do_link(const char *, const char *, const struct stat *);
|
|
|
|
static void do_symlink(const char *, const char *, const struct stat *);
|
|
|
|
static void makelink(const char *, const char *, const struct stat *);
|
2009-12-13 03:34:19 +00:00
|
|
|
static void install(const char *, const char *, u_long, u_int);
|
|
|
|
static void install_dir(char *);
|
2013-01-18 20:57:50 +00:00
|
|
|
static void metadata_log(const char *, const char *, struct timeval *,
|
|
|
|
const char *, const char *, off_t);
|
2013-01-11 20:53:28 +00:00
|
|
|
static int parseid(const char *, id_t *);
|
2009-12-13 03:34:19 +00:00
|
|
|
static void strip(const char *);
|
|
|
|
static int trymmap(int);
|
|
|
|
static void usage(void);
|
1996-04-06 01:50:40 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
int
|
2002-09-04 23:29:10 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct stat from_sb, to_sb;
|
|
|
|
mode_t *set;
|
|
|
|
u_long fset;
|
|
|
|
int ch, no_target;
|
2001-05-28 16:41:53 +00:00
|
|
|
u_int iflags;
|
2013-01-18 20:57:50 +00:00
|
|
|
char *p;
|
|
|
|
const char *to_name;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
iflags = 0;
|
2001-05-28 16:41:53 +00:00
|
|
|
group = owner = NULL;
|
2013-01-18 20:57:50 +00:00
|
|
|
while ((ch = getopt(argc, argv, "B:bCcD:df:g:h:l:M:m:N:o:pSsT:Uv")) !=
|
|
|
|
-1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch((char)ch) {
|
2001-05-28 16:41:53 +00:00
|
|
|
case 'B':
|
|
|
|
suffix = optarg;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'b':
|
|
|
|
dobackup = 1;
|
|
|
|
break;
|
1995-10-09 07:21:00 +00:00
|
|
|
case 'C':
|
2001-05-28 16:41:53 +00:00
|
|
|
docompare = 1;
|
1995-10-09 07:21:00 +00:00
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'c':
|
2001-05-28 16:41:53 +00:00
|
|
|
/* For backwards compatibility. */
|
1995-10-09 07:21:00 +00:00
|
|
|
break;
|
2013-01-18 20:57:50 +00:00
|
|
|
case 'D':
|
|
|
|
destdir = optarg;
|
|
|
|
break;
|
1996-09-29 06:29:54 +00:00
|
|
|
case 'd':
|
|
|
|
dodir = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'f':
|
2013-01-18 20:57:50 +00:00
|
|
|
haveopt_f = 1;
|
|
|
|
fflags = optarg;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'g':
|
2013-01-18 20:57:50 +00:00
|
|
|
haveopt_g = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
group = optarg;
|
|
|
|
break;
|
2013-01-18 20:57:50 +00:00
|
|
|
case 'h':
|
|
|
|
digest = optarg;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
for (p = optarg; *p != '\0'; p++)
|
|
|
|
switch (*p) {
|
|
|
|
case 's':
|
|
|
|
dolink &= ~(LN_HARD|LN_MIXED);
|
|
|
|
dolink |= LN_SYMBOLIC;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
dolink &= ~(LN_SYMBOLIC|LN_MIXED);
|
|
|
|
dolink |= LN_HARD;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
dolink &= ~(LN_SYMBOLIC|LN_HARD);
|
|
|
|
dolink |= LN_MIXED;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
dolink &= ~LN_RELATIVE;
|
|
|
|
dolink |= LN_ABSOLUTE;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
dolink &= ~LN_ABSOLUTE;
|
|
|
|
dolink |= LN_RELATIVE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "%c: invalid link type", *p);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
break;
|
2001-05-28 16:41:53 +00:00
|
|
|
case 'M':
|
2013-01-18 20:57:50 +00:00
|
|
|
metafile = optarg;
|
2001-05-28 16:41:53 +00:00
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'm':
|
2013-01-18 20:57:50 +00:00
|
|
|
haveopt_m = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!(set = setmode(optarg)))
|
1995-10-09 07:21:00 +00:00
|
|
|
errx(EX_USAGE, "invalid file mode: %s",
|
|
|
|
optarg);
|
1994-05-27 12:33:43 +00:00
|
|
|
mode = getmode(set, 0);
|
1998-12-16 04:51:27 +00:00
|
|
|
free(set);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2013-01-11 20:53:28 +00:00
|
|
|
case 'N':
|
|
|
|
if (!setup_getid(optarg))
|
2013-01-18 20:57:50 +00:00
|
|
|
err(EX_OSERR, "Unable to use user and group "
|
2013-01-11 20:53:28 +00:00
|
|
|
"databases in `%s'", optarg);
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'o':
|
2013-01-18 20:57:50 +00:00
|
|
|
haveopt_o = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
owner = optarg;
|
|
|
|
break;
|
1995-10-09 07:21:00 +00:00
|
|
|
case 'p':
|
2001-05-28 16:41:53 +00:00
|
|
|
docompare = dopreserve = 1;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
safecopy = 1;
|
1995-10-09 07:21:00 +00:00
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 's':
|
|
|
|
dostrip = 1;
|
|
|
|
break;
|
2013-01-18 20:57:50 +00:00
|
|
|
case 'T':
|
|
|
|
tags = optarg;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
dounpriv = 1;
|
|
|
|
break;
|
1996-08-12 17:03:30 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
2004-02-07 07:22:11 +00:00
|
|
|
usage();
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1996-09-29 06:29:54 +00:00
|
|
|
|
|
|
|
/* some options make no sense when creating directories */
|
2004-03-17 11:06:40 +00:00
|
|
|
if (dostrip && dodir) {
|
|
|
|
warnx("-d and -s may not be specified together");
|
2004-02-07 07:22:11 +00:00
|
|
|
usage();
|
2004-03-17 11:06:40 +00:00
|
|
|
}
|
1996-09-29 06:29:54 +00:00
|
|
|
|
2006-03-06 21:52:59 +00:00
|
|
|
if (getenv("DONTSTRIP") != NULL) {
|
|
|
|
warnx("DONTSTRIP set - will not strip installed binaries");
|
|
|
|
dostrip = 0;
|
|
|
|
}
|
|
|
|
|
1996-09-29 06:29:54 +00:00
|
|
|
/* must have at least two arguments, except when creating directories */
|
2004-02-07 07:22:11 +00:00
|
|
|
if (argc == 0 || (argc == 1 && !dodir))
|
|
|
|
usage();
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (digest != NULL) {
|
|
|
|
if (strcmp(digest, "none") == 0) {
|
|
|
|
digesttype = DIGEST_NONE;
|
|
|
|
} else if (strcmp(digest, "md5") == 0) {
|
|
|
|
digesttype = DIGEST_MD5;
|
|
|
|
} else if (strcmp(digest, "rmd160") == 0) {
|
|
|
|
digesttype = DIGEST_RIPEMD160;
|
|
|
|
} else if (strcmp(digest, "sha1") == 0) {
|
|
|
|
digesttype = DIGEST_SHA1;
|
|
|
|
} else if (strcmp(digest, "sha256") == 0) {
|
|
|
|
digesttype = DIGEST_SHA256;
|
|
|
|
} else if (strcmp(digest, "sha512") == 0) {
|
|
|
|
digesttype = DIGEST_SHA512;
|
|
|
|
} else {
|
|
|
|
warnx("unknown digest `%s'", digest);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
/* need to make a temp copy so we can compare stripped version */
|
|
|
|
if (docompare && dostrip)
|
|
|
|
safecopy = 1;
|
1996-04-06 01:50:40 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/* get group and owner id's */
|
2013-01-18 20:57:50 +00:00
|
|
|
if (group != NULL && !dounpriv) {
|
2013-01-11 20:53:28 +00:00
|
|
|
if (gid_from_group(group, &gid) == -1) {
|
2013-01-18 20:57:50 +00:00
|
|
|
id_t id;
|
2013-01-11 20:53:28 +00:00
|
|
|
if (!parseid(group, &id))
|
|
|
|
errx(1, "unknown group %s", group);
|
|
|
|
gid = id;
|
|
|
|
}
|
2001-05-28 16:41:53 +00:00
|
|
|
} else
|
|
|
|
gid = (gid_t)-1;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (owner != NULL && !dounpriv) {
|
2013-01-11 20:53:28 +00:00
|
|
|
if (uid_from_user(owner, &uid) == -1) {
|
|
|
|
id_t id;
|
|
|
|
if (!parseid(owner, &id))
|
|
|
|
errx(1, "unknown user %s", owner);
|
|
|
|
uid = id;
|
|
|
|
}
|
2001-05-28 16:41:53 +00:00
|
|
|
} else
|
|
|
|
uid = (uid_t)-1;
|
1996-04-06 01:50:40 +00:00
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (fflags != NULL && !dounpriv) {
|
|
|
|
if (strtofflags(&fflags, &fset, NULL))
|
|
|
|
errx(EX_USAGE, "%s: invalid flag", fflags);
|
|
|
|
iflags |= SETFLAGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (metafile != NULL) {
|
|
|
|
if ((metafp = fopen(metafile, "a")) == NULL)
|
|
|
|
warn("open %s", metafile);
|
|
|
|
} else
|
|
|
|
digesttype = DIGEST_NONE;
|
|
|
|
|
1996-09-29 06:29:54 +00:00
|
|
|
if (dodir) {
|
|
|
|
for (; *argv != NULL; ++argv)
|
|
|
|
install_dir(*argv);
|
|
|
|
exit(EX_OK);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2013-01-22 16:23:08 +00:00
|
|
|
to_name = argv[argc - 1];
|
|
|
|
no_target = stat(to_name, &to_sb);
|
2001-05-28 16:41:53 +00:00
|
|
|
if (!no_target && S_ISDIR(to_sb.st_mode)) {
|
2013-01-22 16:23:08 +00:00
|
|
|
if (dolink & LN_SYMBOLIC) {
|
|
|
|
if (lstat(to_name, &to_sb) != 0)
|
|
|
|
err(EX_OSERR, "%s vanished", to_name);
|
|
|
|
if (S_ISLNK(to_sb.st_mode)) {
|
|
|
|
if (argc != 2) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
err(EX_USAGE, "%s", to_name);
|
|
|
|
}
|
|
|
|
install(*argv, to_name, fset, iflags);
|
|
|
|
exit(EX_OK);
|
|
|
|
}
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
for (; *argv != to_name; ++argv)
|
|
|
|
install(*argv, to_name, fset, iflags | DIRECTORY);
|
1996-09-28 02:53:45 +00:00
|
|
|
exit(EX_OK);
|
|
|
|
/* NOTREACHED */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* can't do file1 file2 directory/file */
|
2004-03-17 11:06:40 +00:00
|
|
|
if (argc != 2) {
|
2007-12-14 08:46:57 +00:00
|
|
|
if (no_target)
|
|
|
|
warnx("target directory `%s' does not exist",
|
|
|
|
argv[argc - 1]);
|
|
|
|
else
|
|
|
|
warnx("target `%s' is not a directory",
|
|
|
|
argv[argc - 1]);
|
2004-02-07 07:22:11 +00:00
|
|
|
usage();
|
2004-03-17 11:06:40 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (!no_target && !dolink) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (stat(*argv, &from_sb))
|
1995-10-09 07:21:00 +00:00
|
|
|
err(EX_OSERR, "%s", *argv);
|
|
|
|
if (!S_ISREG(to_sb.st_mode)) {
|
|
|
|
errno = EFTYPE;
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
if (to_sb.st_dev == from_sb.st_dev &&
|
|
|
|
to_sb.st_ino == from_sb.st_ino)
|
1995-10-09 07:21:00 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"%s and %s are the same file", *argv, to_name);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
install(*argv, to_name, fset, iflags);
|
1996-09-28 02:53:45 +00:00
|
|
|
exit(EX_OK);
|
|
|
|
/* NOTREACHED */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
static char *
|
|
|
|
digest_file(const char *name)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (digesttype) {
|
|
|
|
case DIGEST_MD5:
|
|
|
|
return (MD5File(name, NULL));
|
|
|
|
case DIGEST_RIPEMD160:
|
|
|
|
return (RIPEMD160_File(name, NULL));
|
|
|
|
case DIGEST_SHA1:
|
|
|
|
return (SHA1_File(name, NULL));
|
|
|
|
case DIGEST_SHA256:
|
|
|
|
return (SHA256_File(name, NULL));
|
|
|
|
case DIGEST_SHA512:
|
|
|
|
return (SHA512_File(name, NULL));
|
|
|
|
default:
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
digest_init(DIGEST_CTX *c)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (digesttype) {
|
|
|
|
case DIGEST_NONE:
|
|
|
|
break;
|
|
|
|
case DIGEST_MD5:
|
|
|
|
MD5Init(&(c->MD5));
|
|
|
|
break;
|
|
|
|
case DIGEST_RIPEMD160:
|
|
|
|
RIPEMD160_Init(&(c->RIPEMD160));
|
|
|
|
break;
|
|
|
|
case DIGEST_SHA1:
|
|
|
|
SHA1_Init(&(c->SHA1));
|
|
|
|
break;
|
|
|
|
case DIGEST_SHA256:
|
|
|
|
SHA256_Init(&(c->SHA256));
|
|
|
|
break;
|
|
|
|
case DIGEST_SHA512:
|
|
|
|
SHA512_Init(&(c->SHA512));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-10-30 18:40:55 +00:00
|
|
|
digest_update(DIGEST_CTX *c, const char *data, size_t len)
|
2013-01-18 20:57:50 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
switch (digesttype) {
|
|
|
|
case DIGEST_NONE:
|
|
|
|
break;
|
|
|
|
case DIGEST_MD5:
|
|
|
|
MD5Update(&(c->MD5), data, len);
|
|
|
|
break;
|
|
|
|
case DIGEST_RIPEMD160:
|
|
|
|
RIPEMD160_Update(&(c->RIPEMD160), data, len);
|
|
|
|
break;
|
|
|
|
case DIGEST_SHA1:
|
|
|
|
SHA1_Update(&(c->SHA1), data, len);
|
|
|
|
break;
|
|
|
|
case DIGEST_SHA256:
|
|
|
|
SHA256_Update(&(c->SHA256), data, len);
|
|
|
|
break;
|
|
|
|
case DIGEST_SHA512:
|
|
|
|
SHA512_Update(&(c->SHA512), data, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
digest_end(DIGEST_CTX *c, char *buf)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (digesttype) {
|
|
|
|
case DIGEST_MD5:
|
|
|
|
return (MD5End(&(c->MD5), buf));
|
|
|
|
case DIGEST_RIPEMD160:
|
|
|
|
return (RIPEMD160_End(&(c->RIPEMD160), buf));
|
|
|
|
case DIGEST_SHA1:
|
|
|
|
return (SHA1_End(&(c->SHA1), buf));
|
|
|
|
case DIGEST_SHA256:
|
|
|
|
return (SHA256_End(&(c->SHA256), buf));
|
|
|
|
case DIGEST_SHA512:
|
|
|
|
return (SHA512_End(&(c->SHA512), buf));
|
|
|
|
default:
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-11 20:53:28 +00:00
|
|
|
/*
|
|
|
|
* parseid --
|
|
|
|
* parse uid or gid from arg into id, returning non-zero if successful
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parseid(const char *name, id_t *id)
|
1996-04-06 01:50:40 +00:00
|
|
|
{
|
2013-01-11 20:53:28 +00:00
|
|
|
char *ep;
|
1996-04-06 01:50:40 +00:00
|
|
|
errno = 0;
|
2013-01-11 20:53:28 +00:00
|
|
|
*id = (id_t)strtoul(name, &ep, 10);
|
|
|
|
if (errno || *ep != '\0')
|
|
|
|
return (0);
|
|
|
|
return (1);
|
1996-04-06 01:50:40 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
/*
|
|
|
|
* quiet_mktemp --
|
|
|
|
* mktemp implementation used mkstemp to avoid mktemp warnings. We
|
|
|
|
* really do need mktemp semantics here as we will be creating a link.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
quiet_mktemp(char *template)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = mkstemp(template)) == -1)
|
|
|
|
return (NULL);
|
|
|
|
close (fd);
|
|
|
|
if (unlink(template) == -1)
|
|
|
|
err(EX_OSERR, "unlink %s", template);
|
|
|
|
return (template);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_link --
|
|
|
|
* make a hard link, obeying dorename if set
|
|
|
|
* return -1 on failure
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
do_link(const char *from_name, const char *to_name,
|
|
|
|
const struct stat *target_sb)
|
|
|
|
{
|
|
|
|
char tmpl[MAXPATHLEN];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (safecopy && target_sb != NULL) {
|
|
|
|
(void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
|
|
|
|
/* This usage is safe. */
|
|
|
|
if (quiet_mktemp(tmpl) == NULL)
|
|
|
|
err(EX_OSERR, "%s: mktemp", tmpl);
|
|
|
|
ret = link(from_name, tmpl);
|
|
|
|
if (ret == 0) {
|
|
|
|
if (target_sb->st_mode & S_IFDIR && rmdir(to_name) ==
|
|
|
|
-1) {
|
|
|
|
unlink(tmpl);
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
|
|
|
}
|
|
|
|
if (target_sb->st_flags & NOCHANGEBITS)
|
|
|
|
(void)chflags(to_name, target_sb->st_flags &
|
|
|
|
~NOCHANGEBITS);
|
|
|
|
unlink(to_name);
|
|
|
|
ret = rename(tmpl, to_name);
|
|
|
|
/*
|
|
|
|
* If rename has posix semantics, then the temporary
|
|
|
|
* file may still exist when from_name and to_name point
|
|
|
|
* to the same file, so unlink it unconditionally.
|
|
|
|
*/
|
|
|
|
(void)unlink(tmpl);
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
} else
|
|
|
|
return (link(from_name, to_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_symlink --
|
|
|
|
* Make a symbolic link, obeying dorename if set. Exit on failure.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_symlink(const char *from_name, const char *to_name,
|
|
|
|
const struct stat *target_sb)
|
|
|
|
{
|
|
|
|
char tmpl[MAXPATHLEN];
|
|
|
|
|
|
|
|
if (safecopy && target_sb != NULL) {
|
|
|
|
(void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
|
|
|
|
/* This usage is safe. */
|
|
|
|
if (quiet_mktemp(tmpl) == NULL)
|
|
|
|
err(EX_OSERR, "%s: mktemp", tmpl);
|
|
|
|
|
|
|
|
if (symlink(from_name, tmpl) == -1)
|
|
|
|
err(EX_OSERR, "symlink %s -> %s", from_name, tmpl);
|
|
|
|
|
|
|
|
if (target_sb->st_mode & S_IFDIR && rmdir(to_name) == -1) {
|
|
|
|
(void)unlink(tmpl);
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
|
|
|
}
|
|
|
|
if (target_sb->st_flags & NOCHANGEBITS)
|
|
|
|
(void)chflags(to_name, target_sb->st_flags &
|
|
|
|
~NOCHANGEBITS);
|
|
|
|
unlink(to_name);
|
|
|
|
|
|
|
|
if (rename(tmpl, to_name) == -1) {
|
|
|
|
/* Remove temporary link before exiting. */
|
|
|
|
(void)unlink(tmpl);
|
|
|
|
err(EX_OSERR, "%s: rename", to_name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (symlink(from_name, to_name) == -1)
|
|
|
|
err(EX_OSERR, "symlink %s -> %s", from_name, to_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* makelink --
|
|
|
|
* make a link from source to destination
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
makelink(const char *from_name, const char *to_name,
|
|
|
|
const struct stat *target_sb)
|
|
|
|
{
|
|
|
|
char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
|
|
|
|
struct stat to_sb;
|
|
|
|
|
|
|
|
/* Try hard links first. */
|
|
|
|
if (dolink & (LN_HARD|LN_MIXED)) {
|
|
|
|
if (do_link(from_name, to_name, target_sb) == -1) {
|
|
|
|
if ((dolink & LN_HARD) || errno != EXDEV)
|
|
|
|
err(EX_OSERR, "link %s -> %s", from_name, to_name);
|
|
|
|
} else {
|
|
|
|
if (stat(to_name, &to_sb))
|
|
|
|
err(EX_OSERR, "%s: stat", to_name);
|
|
|
|
if (S_ISREG(to_sb.st_mode)) {
|
|
|
|
/*
|
|
|
|
* XXX: hard links to anything other than
|
|
|
|
* plain files are not metalogged
|
|
|
|
*/
|
|
|
|
int omode;
|
|
|
|
const char *oowner, *ogroup;
|
|
|
|
char *offlags;
|
|
|
|
char *dres;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: use underlying perms, unless
|
|
|
|
* overridden on command line.
|
|
|
|
*/
|
|
|
|
omode = mode;
|
|
|
|
if (!haveopt_m)
|
|
|
|
mode = (to_sb.st_mode & 0777);
|
|
|
|
oowner = owner;
|
|
|
|
if (!haveopt_o)
|
|
|
|
owner = NULL;
|
|
|
|
ogroup = group;
|
|
|
|
if (!haveopt_g)
|
|
|
|
group = NULL;
|
|
|
|
offlags = fflags;
|
|
|
|
if (!haveopt_f)
|
|
|
|
fflags = NULL;
|
|
|
|
dres = digest_file(from_name);
|
|
|
|
metadata_log(to_name, "file", NULL, NULL,
|
|
|
|
dres, to_sb.st_size);
|
|
|
|
free(dres);
|
|
|
|
mode = omode;
|
|
|
|
owner = oowner;
|
|
|
|
group = ogroup;
|
|
|
|
fflags = offlags;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Symbolic links. */
|
|
|
|
if (dolink & LN_ABSOLUTE) {
|
|
|
|
/* Convert source path to absolute. */
|
|
|
|
if (realpath(from_name, src) == NULL)
|
|
|
|
err(EX_OSERR, "%s: realpath", from_name);
|
|
|
|
do_symlink(src, to_name, target_sb);
|
|
|
|
/* XXX: src may point outside of destdir */
|
|
|
|
metadata_log(to_name, "link", NULL, src, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dolink & LN_RELATIVE) {
|
|
|
|
char *cp, *d, *s;
|
|
|
|
|
2015-06-26 23:55:02 +00:00
|
|
|
if (*from_name != '/') {
|
|
|
|
/* this is already a relative link */
|
|
|
|
do_symlink(from_name, to_name, target_sb);
|
|
|
|
/* XXX: from_name may point outside of destdir. */
|
|
|
|
metadata_log(to_name, "link", NULL, from_name, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
/* Resolve pathnames. */
|
|
|
|
if (realpath(from_name, src) == NULL)
|
|
|
|
err(EX_OSERR, "%s: realpath", from_name);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The last component of to_name may be a symlink,
|
|
|
|
* so use realpath to resolve only the directory.
|
|
|
|
*/
|
|
|
|
cp = dirname(to_name);
|
|
|
|
if (realpath(cp, dst) == NULL)
|
|
|
|
err(EX_OSERR, "%s: realpath", cp);
|
|
|
|
/* .. and add the last component. */
|
|
|
|
if (strcmp(dst, "/") != 0) {
|
|
|
|
if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
|
|
|
|
errx(1, "resolved pathname too long");
|
|
|
|
}
|
|
|
|
cp = basename(to_name);
|
|
|
|
if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
|
|
|
|
errx(1, "resolved pathname too long");
|
|
|
|
|
|
|
|
/* Trim common path components. */
|
|
|
|
for (s = src, d = dst; *s == *d; s++, d++)
|
|
|
|
continue;
|
|
|
|
while (*s != '/')
|
|
|
|
s--, d--;
|
|
|
|
|
|
|
|
/* Count the number of directories we need to backtrack. */
|
|
|
|
for (++d, lnk[0] = '\0'; *d; d++)
|
|
|
|
if (*d == '/')
|
|
|
|
(void)strlcat(lnk, "../", sizeof(lnk));
|
|
|
|
|
|
|
|
(void)strlcat(lnk, ++s, sizeof(lnk));
|
|
|
|
|
|
|
|
do_symlink(lnk, to_name, target_sb);
|
|
|
|
/* XXX: Link may point outside of destdir. */
|
|
|
|
metadata_log(to_name, "link", NULL, lnk, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If absolute or relative was not specified, try the names the
|
|
|
|
* user provided.
|
|
|
|
*/
|
|
|
|
do_symlink(from_name, to_name, target_sb);
|
|
|
|
/* XXX: from_name may point outside of destdir. */
|
|
|
|
metadata_log(to_name, "link", NULL, from_name, NULL, 0);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* install --
|
|
|
|
* build a path name and install the file
|
|
|
|
*/
|
2009-12-14 16:54:39 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
install(const char *from_name, const char *to_name, u_long fset, u_int flags)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-28 16:41:53 +00:00
|
|
|
struct stat from_sb, temp_sb, to_sb;
|
2002-11-15 22:43:56 +00:00
|
|
|
struct timeval tvb[2];
|
2001-05-28 16:41:53 +00:00
|
|
|
int devnull, files_match, from_fd, serrno, target;
|
|
|
|
int tempcopy, temp_fd, to_fd;
|
|
|
|
char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
|
2013-01-18 20:57:50 +00:00
|
|
|
char *digestresult;
|
1995-10-09 07:21:00 +00:00
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
files_match = 0;
|
2005-01-25 14:34:57 +00:00
|
|
|
from_fd = -1;
|
|
|
|
to_fd = -1;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* If try to install NULL file to a directory, fails. */
|
|
|
|
if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
|
2013-01-18 20:57:50 +00:00
|
|
|
if (!dolink) {
|
|
|
|
if (stat(from_name, &from_sb))
|
|
|
|
err(EX_OSERR, "%s", from_name);
|
|
|
|
if (!S_ISREG(from_sb.st_mode)) {
|
|
|
|
errno = EFTYPE;
|
|
|
|
err(EX_OSERR, "%s", from_name);
|
|
|
|
}
|
1995-10-09 07:21:00 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
/* Build the target path. */
|
|
|
|
if (flags & DIRECTORY) {
|
|
|
|
(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
|
|
|
|
to_name,
|
1995-10-09 07:21:00 +00:00
|
|
|
(p = strrchr(from_name, '/')) ? ++p : from_name);
|
1994-05-27 12:33:43 +00:00
|
|
|
to_name = pathbuf;
|
|
|
|
}
|
|
|
|
devnull = 0;
|
|
|
|
} else {
|
|
|
|
devnull = 1;
|
|
|
|
}
|
|
|
|
|
2015-11-20 08:45:59 +00:00
|
|
|
target = (lstat(to_name, &to_sb) == 0);
|
2013-01-18 20:57:50 +00:00
|
|
|
|
|
|
|
if (dolink) {
|
|
|
|
if (target && !safecopy) {
|
|
|
|
if (to_sb.st_mode & S_IFDIR && rmdir(to_name) == -1)
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
|
|
|
if (to_sb.st_flags & NOCHANGEBITS)
|
|
|
|
(void)chflags(to_name,
|
|
|
|
to_sb.st_flags & ~NOCHANGEBITS);
|
|
|
|
unlink(to_name);
|
|
|
|
}
|
|
|
|
makelink(from_name, to_name, target ? &to_sb : NULL);
|
|
|
|
return;
|
|
|
|
}
|
1995-10-09 07:21:00 +00:00
|
|
|
|
2015-11-20 08:45:59 +00:00
|
|
|
if (target && !S_ISREG(to_sb.st_mode) && !S_ISLNK(to_sb.st_mode)) {
|
2001-05-28 16:41:53 +00:00
|
|
|
errno = EFTYPE;
|
|
|
|
warn("%s", to_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only copy safe if the target exists. */
|
|
|
|
tempcopy = safecopy && target;
|
|
|
|
|
|
|
|
if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
|
|
|
|
err(EX_OSERR, "%s", from_name);
|
|
|
|
|
|
|
|
/* If we don't strip, we can compare first. */
|
2015-11-20 08:45:59 +00:00
|
|
|
if (docompare && !dostrip && target && S_ISREG(to_sb.st_mode)) {
|
2001-05-28 16:41:53 +00:00
|
|
|
if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
|
1995-10-09 07:21:00 +00:00
|
|
|
err(EX_OSERR, "%s", to_name);
|
2001-05-28 16:41:53 +00:00
|
|
|
if (devnull)
|
|
|
|
files_match = to_sb.st_size == 0;
|
|
|
|
else
|
|
|
|
files_match = !(compare(from_fd, from_name,
|
|
|
|
(size_t)from_sb.st_size, to_fd,
|
2013-01-18 20:57:50 +00:00
|
|
|
to_name, (size_t)to_sb.st_size, &digestresult));
|
2001-05-28 16:41:53 +00:00
|
|
|
|
|
|
|
/* Close "to" file unless we match. */
|
|
|
|
if (!files_match)
|
|
|
|
(void)close(to_fd);
|
1995-10-09 07:21:00 +00:00
|
|
|
}
|
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
if (!files_match) {
|
|
|
|
if (tempcopy) {
|
|
|
|
to_fd = create_tempfile(to_name, tempfile,
|
|
|
|
sizeof(tempfile));
|
|
|
|
if (to_fd < 0)
|
|
|
|
err(EX_OSERR, "%s", tempfile);
|
|
|
|
} else {
|
|
|
|
if ((to_fd = create_newfile(to_name, target,
|
|
|
|
&to_sb)) < 0)
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
|
|
|
if (verbose)
|
|
|
|
(void)printf("install: %s -> %s\n",
|
|
|
|
from_name, to_name);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-28 16:41:53 +00:00
|
|
|
if (!devnull)
|
2013-01-18 20:57:50 +00:00
|
|
|
digestresult = copy(from_fd, from_name, to_fd,
|
2001-05-28 16:41:53 +00:00
|
|
|
tempcopy ? tempfile : to_name, from_sb.st_size);
|
2013-01-18 20:57:50 +00:00
|
|
|
else
|
|
|
|
digestresult = NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1995-10-09 07:21:00 +00:00
|
|
|
|
1997-09-14 08:21:44 +00:00
|
|
|
if (dostrip) {
|
2001-05-28 16:41:53 +00:00
|
|
|
strip(tempcopy ? tempfile : to_name);
|
1997-09-14 08:21:44 +00:00
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
/*
|
|
|
|
* Re-open our fd on the target, in case we used a strip
|
|
|
|
* that does not work in-place -- like GNU binutils strip.
|
|
|
|
*/
|
|
|
|
close(to_fd);
|
|
|
|
to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
|
1997-09-14 08:21:44 +00:00
|
|
|
if (to_fd < 0)
|
2001-05-28 16:41:53 +00:00
|
|
|
err(EX_OSERR, "stripping %s", to_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare the stripped temp file with the target.
|
|
|
|
*/
|
2015-11-20 08:45:59 +00:00
|
|
|
if (docompare && dostrip && target && S_ISREG(to_sb.st_mode)) {
|
2001-05-28 16:41:53 +00:00
|
|
|
temp_fd = to_fd;
|
|
|
|
|
|
|
|
/* Re-open to_fd using the real target name. */
|
|
|
|
if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
|
1997-09-14 08:21:44 +00:00
|
|
|
err(EX_OSERR, "%s", to_name);
|
2001-05-28 16:41:53 +00:00
|
|
|
|
|
|
|
if (fstat(temp_fd, &temp_sb)) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(tempfile);
|
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "%s", tempfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
|
2013-01-18 20:57:50 +00:00
|
|
|
to_name, (size_t)to_sb.st_size, &digestresult)
|
|
|
|
== 0) {
|
2001-05-28 16:41:53 +00:00
|
|
|
/*
|
|
|
|
* If target has more than one link we need to
|
|
|
|
* replace it in order to snap the extra links.
|
|
|
|
* Need to preserve target file times, though.
|
|
|
|
*/
|
|
|
|
if (to_sb.st_nlink != 1) {
|
2002-11-15 22:43:56 +00:00
|
|
|
tvb[0].tv_sec = to_sb.st_atime;
|
|
|
|
tvb[0].tv_usec = 0;
|
|
|
|
tvb[1].tv_sec = to_sb.st_mtime;
|
|
|
|
tvb[1].tv_usec = 0;
|
|
|
|
(void)utimes(tempfile, tvb);
|
2001-05-28 16:41:53 +00:00
|
|
|
} else {
|
|
|
|
files_match = 1;
|
|
|
|
(void)unlink(tempfile);
|
|
|
|
}
|
|
|
|
(void) close(temp_fd);
|
|
|
|
}
|
2015-11-20 08:45:59 +00:00
|
|
|
} else if (dostrip)
|
2013-01-18 20:57:50 +00:00
|
|
|
digestresult = digest_file(tempfile);
|
|
|
|
|
1995-10-09 07:21:00 +00:00
|
|
|
/*
|
2001-05-28 16:41:53 +00:00
|
|
|
* Move the new file into place if doing a safe copy
|
|
|
|
* and the files are different (or just not compared).
|
1995-10-09 07:21:00 +00:00
|
|
|
*/
|
2001-05-28 16:41:53 +00:00
|
|
|
if (tempcopy && !files_match) {
|
|
|
|
/* Try to turn off the immutable bits. */
|
|
|
|
if (to_sb.st_flags & NOCHANGEBITS)
|
|
|
|
(void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
|
|
|
|
if (dobackup) {
|
2001-12-12 08:49:51 +00:00
|
|
|
if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
|
|
|
|
suffix) != strlen(to_name) + strlen(suffix)) {
|
2001-05-28 16:41:53 +00:00
|
|
|
unlink(tempfile);
|
|
|
|
errx(EX_OSERR, "%s: backup filename too long",
|
|
|
|
to_name);
|
1995-10-09 07:21:00 +00:00
|
|
|
}
|
2001-05-28 16:41:53 +00:00
|
|
|
if (verbose)
|
|
|
|
(void)printf("install: %s -> %s\n", to_name, backup);
|
|
|
|
if (rename(to_name, backup) < 0) {
|
1995-10-09 07:21:00 +00:00
|
|
|
serrno = errno;
|
2001-05-28 16:41:53 +00:00
|
|
|
unlink(tempfile);
|
1995-10-09 07:21:00 +00:00
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "rename: %s to %s", to_name,
|
2001-05-28 16:41:53 +00:00
|
|
|
backup);
|
1995-10-09 07:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
2001-05-28 16:41:53 +00:00
|
|
|
if (verbose)
|
|
|
|
(void)printf("install: %s -> %s\n", from_name, to_name);
|
|
|
|
if (rename(tempfile, to_name) < 0) {
|
|
|
|
serrno = errno;
|
|
|
|
unlink(tempfile);
|
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "rename: %s to %s",
|
|
|
|
tempfile, to_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-open to_fd so we aren't hosed by the rename(2). */
|
|
|
|
(void) close(to_fd);
|
|
|
|
if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
1995-10-09 07:21:00 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2001-05-28 16:41:53 +00:00
|
|
|
* Preserve the timestamp of the source file if necessary.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2001-05-28 16:41:53 +00:00
|
|
|
if (dopreserve && !files_match && !devnull) {
|
2002-11-15 22:43:56 +00:00
|
|
|
tvb[0].tv_sec = from_sb.st_atime;
|
|
|
|
tvb[0].tv_usec = 0;
|
|
|
|
tvb[1].tv_sec = from_sb.st_mtime;
|
|
|
|
tvb[1].tv_usec = 0;
|
|
|
|
(void)utimes(to_name, tvb);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-05-28 16:41:53 +00:00
|
|
|
|
|
|
|
if (fstat(to_fd, &to_sb) == -1) {
|
1994-05-27 12:33:43 +00:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
1995-10-09 07:21:00 +00:00
|
|
|
errno = serrno;
|
2001-05-28 16:41:53 +00:00
|
|
|
err(EX_OSERR, "%s", to_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set owner, group, mode for target; do the chown first,
|
|
|
|
* chown may lose the setuid bits.
|
|
|
|
*/
|
2013-01-18 20:57:50 +00:00
|
|
|
if (!dounpriv && ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
2001-05-28 16:41:53 +00:00
|
|
|
(uid != (uid_t)-1 && uid != to_sb.st_uid) ||
|
2013-01-18 20:57:50 +00:00
|
|
|
(mode != (to_sb.st_mode & ALLPERMS)))) {
|
2001-05-28 16:41:53 +00:00
|
|
|
/* Try to turn off the immutable bits. */
|
|
|
|
if (to_sb.st_flags & NOCHANGEBITS)
|
|
|
|
(void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (!dounpriv &
|
|
|
|
(gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
2001-05-28 16:41:53 +00:00
|
|
|
(uid != (uid_t)-1 && uid != to_sb.st_uid))
|
|
|
|
if (fchown(to_fd, uid, gid) == -1) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR,"%s: chown/chgrp", to_name);
|
|
|
|
}
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (mode != (to_sb.st_mode & ALLPERMS)) {
|
2013-01-31 16:04:40 +00:00
|
|
|
if (fchmod(to_fd,
|
|
|
|
dounpriv ? mode & (S_IRWXU|S_IRWXG|S_IRWXO) : mode)) {
|
2001-05-28 16:41:53 +00:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "%s: chmod", to_name);
|
|
|
|
}
|
2013-01-18 20:57:50 +00:00
|
|
|
}
|
2001-05-28 16:41:53 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* If provided a set of flags, set them, otherwise, preserve the
|
|
|
|
* flags, except for the dump flag.
|
1998-06-02 12:00:08 +00:00
|
|
|
* NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
|
|
|
|
* trying to turn off UF_NODUMP. If we're trying to set real flags,
|
2011-02-21 09:01:34 +00:00
|
|
|
* then warn if the fs doesn't support it, otherwise fail.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2013-01-18 20:57:50 +00:00
|
|
|
if (!dounpriv & !devnull && (flags & SETFLAGS ||
|
2002-10-31 10:41:51 +00:00
|
|
|
(from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
|
|
|
|
fchflags(to_fd,
|
1994-05-27 12:33:43 +00:00
|
|
|
flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
|
1998-06-02 12:00:08 +00:00
|
|
|
if (flags & SETFLAGS) {
|
|
|
|
if (errno == EOPNOTSUPP)
|
|
|
|
warn("%s: chflags", to_name);
|
|
|
|
else {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "%s: chflags", to_name);
|
|
|
|
}
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)close(to_fd);
|
2001-05-28 16:41:53 +00:00
|
|
|
if (!devnull)
|
|
|
|
(void)close(from_fd);
|
2013-01-18 20:57:50 +00:00
|
|
|
|
|
|
|
metadata_log(to_name, "file", tvb, NULL, digestresult, to_sb.st_size);
|
|
|
|
free(digestresult);
|
1995-10-09 07:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compare --
|
|
|
|
* compare two files; non-zero means files differ
|
|
|
|
*/
|
2009-12-14 16:54:39 +00:00
|
|
|
static int
|
2001-12-11 22:46:42 +00:00
|
|
|
compare(int from_fd, const char *from_name __unused, size_t from_len,
|
2013-01-18 20:57:50 +00:00
|
|
|
int to_fd, const char *to_name __unused, size_t to_len,
|
|
|
|
char **dresp)
|
1995-10-09 07:21:00 +00:00
|
|
|
{
|
2013-10-30 18:40:55 +00:00
|
|
|
char *p, *q;
|
1995-10-09 07:21:00 +00:00
|
|
|
int rv;
|
1996-09-05 07:54:08 +00:00
|
|
|
int done_compare;
|
2013-01-18 20:57:50 +00:00
|
|
|
DIGEST_CTX ctx;
|
1995-10-09 07:21:00 +00:00
|
|
|
|
1999-09-27 00:36:03 +00:00
|
|
|
rv = 0;
|
2001-05-28 16:41:53 +00:00
|
|
|
if (from_len != to_len)
|
1995-10-09 07:21:00 +00:00
|
|
|
return 1;
|
|
|
|
|
2002-03-18 23:26:13 +00:00
|
|
|
if (from_len <= MAX_CMP_SIZE) {
|
2013-01-18 20:57:50 +00:00
|
|
|
if (dresp != NULL)
|
|
|
|
digest_init(&ctx);
|
1996-09-05 07:54:08 +00:00
|
|
|
done_compare = 0;
|
1996-09-05 07:33:24 +00:00
|
|
|
if (trymmap(from_fd) && trymmap(to_fd)) {
|
2013-01-18 20:57:50 +00:00
|
|
|
p = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
|
|
|
|
from_fd, (off_t)0);
|
2013-10-30 18:40:55 +00:00
|
|
|
if (p == MAP_FAILED)
|
1996-09-05 07:54:08 +00:00
|
|
|
goto out;
|
2013-01-18 20:57:50 +00:00
|
|
|
q = mmap(NULL, from_len, PROT_READ, MAP_SHARED,
|
|
|
|
to_fd, (off_t)0);
|
2013-10-30 18:40:55 +00:00
|
|
|
if (q == MAP_FAILED) {
|
2001-05-28 16:41:53 +00:00
|
|
|
munmap(p, from_len);
|
1996-09-05 07:54:08 +00:00
|
|
|
goto out;
|
|
|
|
}
|
1996-09-05 07:33:24 +00:00
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
rv = memcmp(p, q, from_len);
|
2013-01-18 20:57:50 +00:00
|
|
|
if (dresp != NULL)
|
|
|
|
digest_update(&ctx, p, from_len);
|
2001-05-28 16:41:53 +00:00
|
|
|
munmap(p, from_len);
|
|
|
|
munmap(q, from_len);
|
1996-09-05 07:54:08 +00:00
|
|
|
done_compare = 1;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (!done_compare) {
|
2013-10-30 18:40:55 +00:00
|
|
|
char buf1[MAXBSIZE];
|
1996-09-05 07:54:08 +00:00
|
|
|
char buf2[MAXBSIZE];
|
1996-09-05 07:33:24 +00:00
|
|
|
int n1, n2;
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
lseek(from_fd, 0, SEEK_SET);
|
|
|
|
lseek(to_fd, 0, SEEK_SET);
|
|
|
|
while (rv == 0) {
|
|
|
|
n1 = read(from_fd, buf1, sizeof(buf1));
|
|
|
|
if (n1 == 0)
|
|
|
|
break; /* EOF */
|
|
|
|
else if (n1 > 0) {
|
|
|
|
n2 = read(to_fd, buf2, n1);
|
|
|
|
if (n2 == n1)
|
|
|
|
rv = memcmp(buf1, buf2, n1);
|
|
|
|
else
|
|
|
|
rv = 1; /* out of sync */
|
|
|
|
} else
|
|
|
|
rv = 1; /* read failure */
|
2013-01-18 20:57:50 +00:00
|
|
|
digest_update(&ctx, buf1, n1);
|
1996-09-05 07:33:24 +00:00
|
|
|
}
|
|
|
|
lseek(from_fd, 0, SEEK_SET);
|
|
|
|
lseek(to_fd, 0, SEEK_SET);
|
1996-09-05 07:27:43 +00:00
|
|
|
}
|
1996-09-05 07:33:24 +00:00
|
|
|
} else
|
|
|
|
rv = 1; /* don't bother in this case */
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (dresp != NULL) {
|
|
|
|
if (rv == 0)
|
|
|
|
*dresp = digest_end(&ctx, NULL);
|
|
|
|
else
|
|
|
|
(void)digest_end(&ctx, NULL);
|
|
|
|
}
|
|
|
|
|
1995-10-09 07:21:00 +00:00
|
|
|
return rv;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
/*
|
|
|
|
* create_tempfile --
|
|
|
|
* create a temporary file based on path and open it
|
|
|
|
*/
|
2009-12-14 16:54:39 +00:00
|
|
|
static int
|
2002-09-04 23:29:10 +00:00
|
|
|
create_tempfile(const char *path, char *temp, size_t tsize)
|
2001-05-28 16:41:53 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
(void)strncpy(temp, path, tsize);
|
|
|
|
temp[tsize - 1] = '\0';
|
|
|
|
if ((p = strrchr(temp, '/')) != NULL)
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
p = temp;
|
|
|
|
(void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
|
|
|
|
temp[tsize - 1] = '\0';
|
|
|
|
return (mkstemp(temp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create_newfile --
|
|
|
|
* create a new file, overwriting an existing one if necessary
|
|
|
|
*/
|
2009-12-14 16:54:39 +00:00
|
|
|
static int
|
2002-09-04 23:29:10 +00:00
|
|
|
create_newfile(const char *path, int target, struct stat *sbp)
|
2001-05-28 16:41:53 +00:00
|
|
|
{
|
|
|
|
char backup[MAXPATHLEN];
|
2002-11-30 23:12:59 +00:00
|
|
|
int saved_errno = 0;
|
|
|
|
int newfd;
|
2001-05-28 16:41:53 +00:00
|
|
|
|
|
|
|
if (target) {
|
|
|
|
/*
|
|
|
|
* Unlink now... avoid ETXTBSY errors later. Try to turn
|
|
|
|
* off the append/immutable bits -- if we fail, go ahead,
|
|
|
|
* it might work.
|
|
|
|
*/
|
|
|
|
if (sbp->st_flags & NOCHANGEBITS)
|
|
|
|
(void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
|
|
|
|
|
|
|
|
if (dobackup) {
|
2001-12-12 08:49:51 +00:00
|
|
|
if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
|
|
|
|
path, suffix) != strlen(path) + strlen(suffix))
|
2001-05-28 16:41:53 +00:00
|
|
|
errx(EX_OSERR, "%s: backup filename too long",
|
|
|
|
path);
|
|
|
|
(void)snprintf(backup, MAXPATHLEN, "%s%s",
|
|
|
|
path, suffix);
|
|
|
|
if (verbose)
|
|
|
|
(void)printf("install: %s -> %s\n",
|
|
|
|
path, backup);
|
|
|
|
if (rename(path, backup) < 0)
|
|
|
|
err(EX_OSERR, "rename: %s to %s", path, backup);
|
|
|
|
} else
|
2002-11-30 23:12:59 +00:00
|
|
|
if (unlink(path) < 0)
|
|
|
|
saved_errno = errno;
|
2001-05-28 16:41:53 +00:00
|
|
|
}
|
|
|
|
|
2002-11-30 23:12:59 +00:00
|
|
|
newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
|
|
|
|
if (newfd < 0 && saved_errno != 0)
|
|
|
|
errno = saved_errno;
|
|
|
|
return newfd;
|
2001-05-28 16:41:53 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* copy --
|
|
|
|
* copy from one file to another
|
|
|
|
*/
|
2013-01-18 20:57:50 +00:00
|
|
|
static char *
|
2002-09-04 23:29:10 +00:00
|
|
|
copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
|
|
|
|
off_t size)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-09-04 23:29:10 +00:00
|
|
|
int nr, nw;
|
1994-05-27 12:33:43 +00:00
|
|
|
int serrno;
|
2013-10-30 18:40:55 +00:00
|
|
|
char *p;
|
|
|
|
char buf[MAXBSIZE];
|
1996-09-05 07:54:08 +00:00
|
|
|
int done_copy;
|
2013-01-18 20:57:50 +00:00
|
|
|
DIGEST_CTX ctx;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
/* Rewind file descriptors. */
|
|
|
|
if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
|
|
|
err(EX_OSERR, "lseek: %s", from_name);
|
|
|
|
if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
|
|
|
err(EX_OSERR, "lseek: %s", to_name);
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
digest_init(&ctx);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Mmap and write if less than 8M (the limit is so we don't totally
|
|
|
|
* trash memory on big files. This is really a minor hack, but it
|
|
|
|
* wins some CPU back.
|
|
|
|
*/
|
1996-09-05 07:54:08 +00:00
|
|
|
done_copy = 0;
|
2001-05-28 16:41:53 +00:00
|
|
|
if (size <= 8 * 1048576 && trymmap(from_fd) &&
|
|
|
|
(p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
|
2013-10-30 18:40:55 +00:00
|
|
|
from_fd, (off_t)0)) != MAP_FAILED) {
|
2012-07-02 09:53:57 +00:00
|
|
|
nw = write(to_fd, p, size);
|
|
|
|
if (nw != size) {
|
1996-09-05 07:54:08 +00:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
2012-07-02 09:53:57 +00:00
|
|
|
if (nw >= 0) {
|
|
|
|
errx(EX_OSERR,
|
|
|
|
"short write to %s: %jd bytes written, %jd bytes asked to write",
|
|
|
|
to_name, (uintmax_t)nw, (uintmax_t)size);
|
|
|
|
} else {
|
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
|
|
|
}
|
1996-09-05 07:54:08 +00:00
|
|
|
}
|
2013-01-18 20:57:50 +00:00
|
|
|
digest_update(&ctx, p, size);
|
|
|
|
(void)munmap(p, size);
|
1996-09-05 07:54:08 +00:00
|
|
|
done_copy = 1;
|
|
|
|
}
|
|
|
|
if (!done_copy) {
|
2013-01-18 20:57:50 +00:00
|
|
|
while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((nw = write(to_fd, buf, nr)) != nr) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
2012-07-02 09:53:57 +00:00
|
|
|
if (nw >= 0) {
|
|
|
|
errx(EX_OSERR,
|
|
|
|
"short write to %s: %jd bytes written, %jd bytes asked to write",
|
|
|
|
to_name, (uintmax_t)nw,
|
|
|
|
(uintmax_t)size);
|
|
|
|
} else {
|
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2013-01-18 20:57:50 +00:00
|
|
|
digest_update(&ctx, buf, nr);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
if (nr != 0) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
1995-10-09 07:21:00 +00:00
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "%s", from_name);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-18 20:57:50 +00:00
|
|
|
return (digest_end(&ctx, NULL));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* strip --
|
|
|
|
* use strip(1) to strip the target file
|
|
|
|
*/
|
2009-12-14 16:54:39 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
strip(const char *to_name)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-11 19:15:15 +00:00
|
|
|
const char *stripbin;
|
2014-03-08 23:05:28 +00:00
|
|
|
const char *args[3];
|
|
|
|
pid_t pid;
|
|
|
|
int error, status;
|
|
|
|
|
|
|
|
stripbin = getenv("STRIPBIN");
|
|
|
|
if (stripbin == NULL)
|
|
|
|
stripbin = "strip";
|
|
|
|
args[0] = stripbin;
|
|
|
|
args[1] = to_name;
|
|
|
|
args[2] = NULL;
|
|
|
|
error = posix_spawnp(&pid, stripbin, NULL, NULL,
|
|
|
|
__DECONST(char **, args), environ);
|
|
|
|
if (error != 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)unlink(to_name);
|
2014-03-08 23:05:28 +00:00
|
|
|
errc(error == EAGAIN || error == EPROCLIM || error == ENOMEM ?
|
|
|
|
EX_TEMPFAIL : EX_OSERR, error, "spawn %s", stripbin);
|
|
|
|
}
|
|
|
|
if (waitpid(pid, &status, 0) == -1) {
|
|
|
|
error = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errc(EX_SOFTWARE, error, "wait");
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
if (status != 0) {
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(EX_SOFTWARE, "strip command %s failed on %s",
|
|
|
|
stripbin, to_name);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-09-29 06:29:54 +00:00
|
|
|
/*
|
|
|
|
* install_dir --
|
2010-02-20 10:19:19 +00:00
|
|
|
* build directory hierarchy
|
1996-09-29 06:29:54 +00:00
|
|
|
*/
|
2009-12-14 16:54:39 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
install_dir(char *path)
|
1996-09-29 06:29:54 +00:00
|
|
|
{
|
2002-09-04 23:29:10 +00:00
|
|
|
char *p;
|
1996-09-29 06:29:54 +00:00
|
|
|
struct stat sb;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
for (p = path;; ++p)
|
|
|
|
if (!*p || (p != path && *p == '/')) {
|
|
|
|
ch = *p;
|
|
|
|
*p = '\0';
|
2014-09-23 11:41:09 +00:00
|
|
|
again:
|
|
|
|
if (stat(path, &sb) < 0) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
err(EX_OSERR, "stat %s", path);
|
|
|
|
if (mkdir(path, 0755) < 0) {
|
|
|
|
if (errno == EEXIST)
|
|
|
|
goto again;
|
1997-10-27 22:53:33 +00:00
|
|
|
err(EX_OSERR, "mkdir %s", path);
|
2014-09-23 11:41:09 +00:00
|
|
|
}
|
|
|
|
if (verbose)
|
2001-05-28 16:41:53 +00:00
|
|
|
(void)printf("install: mkdir %s\n",
|
2014-09-23 11:41:09 +00:00
|
|
|
path);
|
1997-10-28 14:20:10 +00:00
|
|
|
} else if (!S_ISDIR(sb.st_mode))
|
|
|
|
errx(EX_OSERR, "%s exists but is not a directory", path);
|
1996-09-29 06:29:54 +00:00
|
|
|
if (!(*p = ch))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (!dounpriv) {
|
|
|
|
if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
|
|
|
|
chown(path, uid, gid))
|
|
|
|
warn("chown %u:%u %s", uid, gid, path);
|
|
|
|
/* XXXBED: should we do the chmod in the dounpriv case? */
|
|
|
|
if (chmod(path, mode))
|
|
|
|
warn("chmod %o %s", mode, path);
|
|
|
|
}
|
|
|
|
metadata_log(path, "dir", NULL, NULL, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* metadata_log --
|
|
|
|
* if metafp is not NULL, output mtree(8) full path name and settings to
|
|
|
|
* metafp, to allow permissions to be set correctly by other tools,
|
|
|
|
* or to allow integrity checks to be performed.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
metadata_log(const char *path, const char *type, struct timeval *tv,
|
|
|
|
const char *slink, const char *digestresult, off_t size)
|
|
|
|
{
|
|
|
|
static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
|
|
|
|
const char *p;
|
|
|
|
char *buf;
|
|
|
|
size_t destlen;
|
|
|
|
struct flock metalog_lock;
|
|
|
|
|
|
|
|
if (!metafp)
|
|
|
|
return;
|
|
|
|
/* Buffer for strsvis(3). */
|
|
|
|
buf = (char *)malloc(4 * strlen(path) + 1);
|
|
|
|
if (buf == NULL) {
|
|
|
|
warnx("%s", strerror(ENOMEM));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock log file. */
|
|
|
|
metalog_lock.l_start = 0;
|
|
|
|
metalog_lock.l_len = 0;
|
|
|
|
metalog_lock.l_whence = SEEK_SET;
|
|
|
|
metalog_lock.l_type = F_WRLCK;
|
|
|
|
if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
|
|
|
|
warn("can't lock %s", metafile);
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove destdir. */
|
|
|
|
p = path;
|
|
|
|
if (destdir) {
|
|
|
|
destlen = strlen(destdir);
|
|
|
|
if (strncmp(p, destdir, destlen) == 0 &&
|
|
|
|
(p[destlen] == '/' || p[destlen] == '\0'))
|
|
|
|
p += destlen;
|
|
|
|
}
|
|
|
|
while (*p && *p == '/')
|
|
|
|
p++;
|
|
|
|
strsvis(buf, p, VIS_OCTAL, extra);
|
|
|
|
p = buf;
|
|
|
|
/* Print details. */
|
|
|
|
fprintf(metafp, ".%s%s type=%s", *p ? "/" : "", p, type);
|
|
|
|
if (owner)
|
|
|
|
fprintf(metafp, " uname=%s", owner);
|
|
|
|
if (group)
|
|
|
|
fprintf(metafp, " gname=%s", group);
|
|
|
|
fprintf(metafp, " mode=%#o", mode);
|
|
|
|
if (slink) {
|
|
|
|
strsvis(buf, slink, VIS_CSTYLE, extra); /* encode link */
|
|
|
|
fprintf(metafp, " link=%s", buf);
|
|
|
|
}
|
|
|
|
if (*type == 'f') /* type=file */
|
|
|
|
fprintf(metafp, " size=%lld", (long long)size);
|
|
|
|
if (tv != NULL && dopreserve)
|
|
|
|
fprintf(metafp, " time=%lld.%ld",
|
|
|
|
(long long)tv[1].tv_sec, (long)tv[1].tv_usec);
|
|
|
|
if (digestresult && digest)
|
|
|
|
fprintf(metafp, " %s=%s", digest, digestresult);
|
|
|
|
if (fflags)
|
|
|
|
fprintf(metafp, " flags=%s", fflags);
|
|
|
|
if (tags)
|
|
|
|
fprintf(metafp, " tags=%s", tags);
|
|
|
|
fputc('\n', metafp);
|
|
|
|
/* Flush line. */
|
|
|
|
fflush(metafp);
|
|
|
|
|
|
|
|
/* Unlock log file. */
|
|
|
|
metalog_lock.l_type = F_UNLCK;
|
|
|
|
if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1)
|
|
|
|
warn("can't unlock %s", metafile);
|
|
|
|
free(buf);
|
1996-09-29 06:29:54 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* usage --
|
|
|
|
* print a usage message and die
|
|
|
|
*/
|
2009-12-14 16:54:39 +00:00
|
|
|
static void
|
2009-12-13 03:34:19 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-02-07 07:22:11 +00:00
|
|
|
(void)fprintf(stderr,
|
2013-01-18 20:57:50 +00:00
|
|
|
"usage: install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
|
|
|
|
" [-M log] [-D dest] [-h hash] [-T tags]\n"
|
|
|
|
" [-B suffix] [-l linkflags] [-N dbdir]\n"
|
|
|
|
" file1 file2\n"
|
|
|
|
" install [-bCcpSsUv] [-f flags] [-g group] [-m mode] [-o owner]\n"
|
|
|
|
" [-M log] [-D dest] [-h hash] [-T tags]\n"
|
|
|
|
" [-B suffix] [-l linkflags] [-N dbdir]\n"
|
|
|
|
" file1 ... fileN directory\n"
|
|
|
|
" install -dU [-vU] [-g group] [-m mode] [-N dbdir] [-o owner]\n"
|
|
|
|
" [-M log] [-D dest] [-h hash] [-T tags]\n"
|
2013-01-11 20:53:28 +00:00
|
|
|
" directory ...\n");
|
1996-09-28 02:53:45 +00:00
|
|
|
exit(EX_USAGE);
|
|
|
|
/* NOTREACHED */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1996-09-05 07:27:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* trymmap --
|
|
|
|
* return true (1) if mmap should be tried, false (0) if not.
|
|
|
|
*/
|
2009-12-14 16:54:39 +00:00
|
|
|
static int
|
2002-09-04 23:29:10 +00:00
|
|
|
trymmap(int fd)
|
1996-09-05 07:27:43 +00:00
|
|
|
{
|
1998-01-20 13:52:32 +00:00
|
|
|
/*
|
|
|
|
* The ifdef is for bootstrapping - f_fstypename doesn't exist in
|
|
|
|
* pre-Lite2-merge systems.
|
|
|
|
*/
|
|
|
|
#ifdef MFSNAMELEN
|
1996-09-05 07:27:43 +00:00
|
|
|
struct statfs stfs;
|
|
|
|
|
2013-01-18 20:57:50 +00:00
|
|
|
if (fstatfs(fd, &stfs) != 0)
|
1998-01-20 13:52:32 +00:00
|
|
|
return (0);
|
|
|
|
if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
|
|
|
|
strcmp(stfs.f_fstypename, "cd9660") == 0)
|
|
|
|
return (1);
|
1998-01-09 06:05:13 +00:00
|
|
|
#endif
|
1998-01-20 13:52:32 +00:00
|
|
|
return (0);
|
1996-09-05 07:27:43 +00:00
|
|
|
}
|