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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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>
|
|
|
|
#include <sys/wait.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
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>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#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>
|
1995-10-09 07:21:00 +00:00
|
|
|
#include <utime.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
1997-05-24 05:39:19 +00:00
|
|
|
/* Bootstrap aid - this doesn't exist in most older releases */
|
|
|
|
#ifndef MAP_FAILED
|
1998-01-13 02:12:43 +00:00
|
|
|
#define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
|
1997-05-24 05:39:19 +00:00
|
|
|
#endif
|
|
|
|
|
2002-03-18 23:26:13 +00:00
|
|
|
#define MAX_CMP_SIZE (16 * 1024 * 1024)
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
struct passwd *pp;
|
|
|
|
struct group *gp;
|
|
|
|
gid_t gid;
|
|
|
|
uid_t uid;
|
|
|
|
int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
|
|
|
|
mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
2001-12-12 08:49:51 +00:00
|
|
|
const char *suffix = BACKUP_SUFFIX;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-03-22 01:42:45 +00:00
|
|
|
void copy(int, const char *, int, const char *, off_t);
|
|
|
|
int compare(int, const char *, size_t, int, const char *, size_t);
|
|
|
|
int create_newfile(const char *, int, struct stat *);
|
|
|
|
int create_tempfile(const char *, char *, size_t);
|
|
|
|
void install(const char *, const char *, u_long, u_int);
|
|
|
|
void install_dir(char *);
|
|
|
|
u_long numeric_id(const char *, const char *);
|
|
|
|
void strip(const char *);
|
|
|
|
int trymmap(int);
|
|
|
|
void usage(void);
|
1996-04-06 01:50:40 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
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;
|
2001-12-12 08:49:51 +00:00
|
|
|
char *flags;
|
|
|
|
const char *group, *owner, *to_name;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
iflags = 0;
|
2001-05-28 16:41:53 +00:00
|
|
|
group = owner = NULL;
|
|
|
|
while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:o:pSsv")) != -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;
|
1996-09-29 06:29:54 +00:00
|
|
|
case 'd':
|
|
|
|
dodir = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'f':
|
|
|
|
flags = optarg;
|
2000-06-17 14:19:33 +00:00
|
|
|
if (strtofflags(&flags, &fset, NULL))
|
1995-10-09 07:21:00 +00:00
|
|
|
errx(EX_USAGE, "%s: invalid flag", flags);
|
1994-05-27 12:33:43 +00:00
|
|
|
iflags |= SETFLAGS;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
group = optarg;
|
|
|
|
break;
|
2001-05-28 16:41:53 +00:00
|
|
|
case 'M':
|
|
|
|
nommap = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'm':
|
|
|
|
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;
|
|
|
|
case 'o':
|
|
|
|
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;
|
1996-08-12 17:03:30 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1996-09-29 06:29:54 +00:00
|
|
|
|
|
|
|
/* some options make no sense when creating directories */
|
2001-05-30 07:08:49 +00:00
|
|
|
if ((safecopy || dostrip) && dodir)
|
1996-09-29 06:29:54 +00:00
|
|
|
usage();
|
|
|
|
|
2001-05-30 07:08:49 +00:00
|
|
|
/*
|
|
|
|
* Older versions allowed -d -C combo. Issue a warning
|
|
|
|
* for now, but turn this into an error before 4.5-RELEASE.
|
|
|
|
*/
|
|
|
|
if (docompare && dodir)
|
|
|
|
warnx("the -d and -C options may not be specified together");
|
|
|
|
|
1996-09-29 06:29:54 +00:00
|
|
|
/* must have at least two arguments, except when creating directories */
|
|
|
|
if (argc < 2 && !dodir)
|
1994-05-27 12:33:43 +00:00
|
|
|
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 */
|
2001-05-28 16:41:53 +00:00
|
|
|
if (group != NULL) {
|
|
|
|
if ((gp = getgrnam(group)) != NULL)
|
|
|
|
gid = gp->gr_gid;
|
|
|
|
else
|
2001-12-11 22:46:42 +00:00
|
|
|
gid = (gid_t)numeric_id(group, "group");
|
2001-05-28 16:41:53 +00:00
|
|
|
} else
|
|
|
|
gid = (gid_t)-1;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
if (owner != NULL) {
|
|
|
|
if ((pp = getpwnam(owner)) != NULL)
|
|
|
|
uid = pp->pw_uid;
|
|
|
|
else
|
|
|
|
uid = (uid_t)numeric_id(owner, "user");
|
|
|
|
} else
|
|
|
|
uid = (uid_t)-1;
|
1996-04-06 01:50:40 +00:00
|
|
|
|
1996-09-29 06:29:54 +00:00
|
|
|
if (dodir) {
|
|
|
|
for (; *argv != NULL; ++argv)
|
|
|
|
install_dir(*argv);
|
|
|
|
exit(EX_OK);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
no_target = stat(to_name = argv[argc - 1], &to_sb);
|
2001-05-28 16:41:53 +00:00
|
|
|
if (!no_target && S_ISDIR(to_sb.st_mode)) {
|
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 */
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (!no_target) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1996-04-06 01:50:40 +00:00
|
|
|
u_long
|
|
|
|
numeric_id(name, type)
|
2001-12-12 08:49:51 +00:00
|
|
|
const char *name, *type;
|
1996-04-06 01:50:40 +00:00
|
|
|
{
|
|
|
|
u_long val;
|
|
|
|
char *ep;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* We know that uid_t's and gid_t's are unsigned longs.
|
|
|
|
*/
|
|
|
|
errno = 0;
|
|
|
|
val = strtoul(name, &ep, 10);
|
|
|
|
if (errno)
|
|
|
|
err(EX_NOUSER, "%s", name);
|
|
|
|
if (*ep != '\0')
|
|
|
|
errx(EX_NOUSER, "unknown %s %s", type, name);
|
|
|
|
return (val);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* install --
|
|
|
|
* build a path name and install the file
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
install(from_name, to_name, fset, flags)
|
2001-12-12 08:49:51 +00:00
|
|
|
const char *from_name, *to_name;
|
1994-05-27 12:33:43 +00:00
|
|
|
u_long fset;
|
|
|
|
u_int flags;
|
|
|
|
{
|
2001-05-28 16:41:53 +00:00
|
|
|
struct stat from_sb, temp_sb, to_sb;
|
|
|
|
struct utimbuf utb;
|
|
|
|
int devnull, files_match, from_fd, serrno, target;
|
|
|
|
int tempcopy, temp_fd, to_fd;
|
|
|
|
char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
|
1995-10-09 07:21:00 +00:00
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
files_match = 0;
|
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)) {
|
|
|
|
if (stat(from_name, &from_sb))
|
1995-10-09 07:21:00 +00:00
|
|
|
err(EX_OSERR, "%s", from_name);
|
|
|
|
if (!S_ISREG(from_sb.st_mode)) {
|
|
|
|
errno = EFTYPE;
|
|
|
|
err(EX_OSERR, "%s", from_name);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
target = stat(to_name, &to_sb) == 0;
|
1995-10-09 07:21:00 +00:00
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
/* Only install to regular files. */
|
|
|
|
if (target && !S_ISREG(to_sb.st_mode)) {
|
|
|
|
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. */
|
|
|
|
if (docompare && !dostrip && target) {
|
|
|
|
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,
|
|
|
|
to_name, (size_t)to_sb.st_size));
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
copy(from_fd, from_name, to_fd,
|
|
|
|
tempcopy ? tempfile : to_name, from_sb.st_size);
|
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.
|
|
|
|
*/
|
|
|
|
if (docompare && dostrip && target) {
|
|
|
|
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,
|
|
|
|
to_name, (size_t)to_sb.st_size) == 0) {
|
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
utb.actime = to_sb.st_atime;
|
|
|
|
utb.modtime = to_sb.st_mtime;
|
|
|
|
(void)utime(tempfile, &utb);
|
|
|
|
} else {
|
|
|
|
files_match = 1;
|
|
|
|
(void)unlink(tempfile);
|
|
|
|
}
|
|
|
|
(void) close(temp_fd);
|
|
|
|
}
|
1997-09-14 08:21:44 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
utb.actime = from_sb.st_atime;
|
|
|
|
utb.modtime = from_sb.st_mtime;
|
|
|
|
(void)utime(to_name, &utb);
|
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.
|
|
|
|
*/
|
|
|
|
if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
|
|
|
(uid != (uid_t)-1 && uid != to_sb.st_uid) ||
|
|
|
|
(mode != to_sb.st_mode)) {
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2001-05-28 16:41:53 +00:00
|
|
|
if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
|
|
|
(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode != to_sb.st_mode)
|
|
|
|
if (fchmod(to_fd, mode)) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errno = serrno;
|
|
|
|
err(EX_OSERR, "%s: chmod", to_name);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
* then warn if the the fs doesn't support it, otherwise fail.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2001-05-28 16:41:53 +00:00
|
|
|
if (!devnull && 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);
|
1995-10-09 07:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compare --
|
|
|
|
* compare two files; non-zero means files differ
|
|
|
|
*/
|
|
|
|
int
|
2001-12-11 22:46:42 +00:00
|
|
|
compare(int from_fd, const char *from_name __unused, size_t from_len,
|
|
|
|
int to_fd, const char *to_name __unused, size_t to_len)
|
1995-10-09 07:21:00 +00:00
|
|
|
{
|
|
|
|
char *p, *q;
|
|
|
|
int rv;
|
1996-09-05 07:54:08 +00:00
|
|
|
int done_compare;
|
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) {
|
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)) {
|
2001-05-28 16:41:53 +00:00
|
|
|
p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
|
1997-01-16 21:58:40 +00:00
|
|
|
if (p == (char *)MAP_FAILED)
|
1996-09-05 07:54:08 +00:00
|
|
|
goto out;
|
2001-05-28 16:41:53 +00:00
|
|
|
q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
|
1997-01-16 21:58:40 +00:00
|
|
|
if (q == (char *)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);
|
|
|
|
munmap(p, from_len);
|
|
|
|
munmap(q, from_len);
|
1996-09-05 07:54:08 +00:00
|
|
|
done_compare = 1;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (!done_compare) {
|
|
|
|
char buf1[MAXBSIZE];
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
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 */
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
create_tempfile(path, temp, tsize)
|
2001-12-12 08:49:51 +00:00
|
|
|
const char *path;
|
2001-05-28 16:41:53 +00:00
|
|
|
char *temp;
|
|
|
|
size_t tsize;
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
create_newfile(path, target, sbp)
|
2001-12-12 08:49:51 +00:00
|
|
|
const char *path;
|
2001-05-28 16:41:53 +00:00
|
|
|
int target;
|
|
|
|
struct stat *sbp;
|
|
|
|
{
|
|
|
|
char backup[MAXPATHLEN];
|
|
|
|
|
|
|
|
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
|
|
|
|
(void)unlink(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* copy --
|
|
|
|
* copy from one file to another
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
copy(from_fd, from_name, to_fd, to_name, size)
|
|
|
|
register int from_fd, to_fd;
|
2001-12-12 08:49:51 +00:00
|
|
|
const char *from_name, *to_name;
|
1994-05-27 12:33:43 +00:00
|
|
|
off_t size;
|
|
|
|
{
|
|
|
|
register int nr, nw;
|
|
|
|
int serrno;
|
|
|
|
char *p, buf[MAXBSIZE];
|
1996-09-05 07:54:08 +00:00
|
|
|
int done_copy;
|
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);
|
|
|
|
|
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,
|
|
|
|
from_fd, (off_t)0)) != (char *)MAP_FAILED) {
|
1996-09-05 07:54:08 +00:00
|
|
|
if ((nw = write(to_fd, p, size)) != size) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errno = nw > 0 ? EIO : serrno;
|
1995-10-09 07:21:00 +00:00
|
|
|
err(EX_OSERR, "%s", to_name);
|
1996-09-05 07:54:08 +00:00
|
|
|
}
|
|
|
|
done_copy = 1;
|
|
|
|
}
|
|
|
|
if (!done_copy) {
|
1994-05-27 12:33:43 +00:00
|
|
|
while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
|
|
|
|
if ((nw = write(to_fd, buf, nr)) != nr) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
1995-10-09 07:21:00 +00:00
|
|
|
errno = nw > 0 ? EIO : serrno;
|
|
|
|
err(EX_OSERR, "%s", to_name);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* strip --
|
|
|
|
* use strip(1) to strip the target file
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
strip(to_name)
|
2001-12-12 08:49:51 +00:00
|
|
|
const char *to_name;
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-11 19:15:15 +00:00
|
|
|
const char *stripbin;
|
1994-05-27 12:33:43 +00:00
|
|
|
int serrno, status;
|
|
|
|
|
1998-10-13 14:52:33 +00:00
|
|
|
switch (fork()) {
|
1994-05-27 12:33:43 +00:00
|
|
|
case -1:
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
1995-10-09 07:21:00 +00:00
|
|
|
errno = serrno;
|
|
|
|
err(EX_TEMPFAIL, "fork");
|
1994-05-27 12:33:43 +00:00
|
|
|
case 0:
|
2002-05-11 19:15:15 +00:00
|
|
|
stripbin = getenv("STRIPBIN");
|
|
|
|
if (stripbin == NULL)
|
|
|
|
stripbin = "strip";
|
|
|
|
execlp(stripbin, stripbin, to_name, (char *)NULL);
|
2002-05-12 03:47:23 +00:00
|
|
|
err(EX_OSERR, "exec(%s)", stripbin);
|
1994-05-27 12:33:43 +00:00
|
|
|
default:
|
1996-06-23 12:59:18 +00:00
|
|
|
if (wait(&status) == -1 || status) {
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)unlink(to_name);
|
1996-06-23 12:59:18 +00:00
|
|
|
exit(EX_SOFTWARE);
|
1996-09-28 02:53:45 +00:00
|
|
|
/* NOTREACHED */
|
1996-06-23 12:59:18 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-09-29 06:29:54 +00:00
|
|
|
/*
|
|
|
|
* install_dir --
|
|
|
|
* build directory heirarchy
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
install_dir(path)
|
2001-05-28 16:41:53 +00:00
|
|
|
char *path;
|
1996-09-29 06:29:54 +00:00
|
|
|
{
|
|
|
|
register char *p;
|
|
|
|
struct stat sb;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
for (p = path;; ++p)
|
|
|
|
if (!*p || (p != path && *p == '/')) {
|
|
|
|
ch = *p;
|
|
|
|
*p = '\0';
|
|
|
|
if (stat(path, &sb)) {
|
1997-10-28 14:20:10 +00:00
|
|
|
if (errno != ENOENT || mkdir(path, 0755) < 0) {
|
1997-10-27 22:53:33 +00:00
|
|
|
err(EX_OSERR, "mkdir %s", path);
|
1996-09-29 06:29:54 +00:00
|
|
|
/* NOTREACHED */
|
2001-05-28 16:41:53 +00:00
|
|
|
} else if (verbose)
|
|
|
|
(void)printf("install: mkdir %s\n",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1997-10-27 22:53:33 +00:00
|
|
|
if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
|
|
|
|
warn("chown %u:%u %s", uid, gid, path);
|
1997-10-28 14:20:10 +00:00
|
|
|
if (chmod(path, mode))
|
1997-10-27 22:53:33 +00:00
|
|
|
warn("chmod %o %s", mode, path);
|
1996-09-29 06:29:54 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* usage --
|
|
|
|
* print a usage message and die
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2001-05-28 16:41:53 +00:00
|
|
|
(void)fprintf(stderr, "\
|
|
|
|
usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
|
|
|
|
[-o owner] file1 file2\n\
|
|
|
|
install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n\
|
|
|
|
[-o owner] file1 ... fileN directory\n\
|
1999-12-06 05:02:32 +00:00
|
|
|
install -d [-v] [-g group] [-m mode] [-o owner] 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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
trymmap(fd)
|
|
|
|
int fd;
|
|
|
|
{
|
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;
|
|
|
|
|
1998-01-20 13:52:32 +00:00
|
|
|
if (nommap || fstatfs(fd, &stfs) != 0)
|
|
|
|
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
|
|
|
}
|