2003-10-27 03:12:41 +03:00
|
|
|
/* $NetBSD: xinstall.c,v 1.80 2003/10/27 00:12:44 lukem Exp $ */
|
1994-11-14 07:57:16 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-11-14 07:57:16 +03:00
|
|
|
* Copyright (c) 1987, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03: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.
|
|
|
|
*/
|
|
|
|
|
2003-10-27 03:12:41 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
2002-01-21 23:00:02 +03:00
|
|
|
#else
|
|
|
|
#define HAVE_FUTIMES 1
|
|
|
|
#define HAVE_STRUCT_STAT_ST_FLAGS 1
|
|
|
|
#endif
|
|
|
|
|
1997-10-19 18:42:36 +04:00
|
|
|
#include <sys/cdefs.h>
|
2002-02-01 01:43:33 +03:00
|
|
|
#if defined(__COPYRIGHT) && !defined(lint)
|
1997-10-19 18:42:36 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-02-01 01:43:33 +03:00
|
|
|
#if defined(__RCSID) && !defined(lint)
|
1994-11-14 07:57:16 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
|
1997-10-19 18:42:36 +04:00
|
|
|
#else
|
2003-10-27 03:12:41 +03:00
|
|
|
__RCSID("$NetBSD: xinstall.c,v 1.80 2003/10/27 00:12:44 lukem Exp $");
|
1994-11-14 07:57:16 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
#include <sys/mman.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/stat.h>
|
2001-11-23 19:14:51 +03:00
|
|
|
#include <sys/wait.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <ctype.h>
|
2002-02-01 01:43:33 +03:00
|
|
|
#include <err.h>
|
1993-06-11 22:30:46 +04:00
|
|
|
#include <errno.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
2002-02-01 01:43:33 +03:00
|
|
|
#include <libgen.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <paths.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2001-10-11 06:06:32 +04:00
|
|
|
#include <vis.h>
|
1994-11-14 07:57:16 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "pathnames.h"
|
1998-10-10 11:38:22 +04:00
|
|
|
#include "stat_flags.h"
|
2002-12-19 11:30:39 +03:00
|
|
|
#include "mtree.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-10-08 06:12:51 +04:00
|
|
|
#define STRIP_ARGS_MAX 32
|
1999-01-25 04:42:57 +03:00
|
|
|
#define BACKUP_SUFFIX ".old"
|
1998-10-08 06:12:51 +04:00
|
|
|
|
2001-10-11 06:06:32 +04:00
|
|
|
int dobackup, docopy, dodir, dostrip, dolink, dopreserve, dorename,
|
|
|
|
dounpriv;
|
|
|
|
int numberedbackup;
|
|
|
|
int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
|
|
|
|
char pathbuf[MAXPATHLEN];
|
2002-01-21 23:00:02 +03:00
|
|
|
id_t uid, gid;
|
2001-10-25 19:37:06 +04:00
|
|
|
char *group, *owner, *fflags, *tags;
|
2001-10-11 06:06:32 +04:00
|
|
|
FILE *metafp;
|
|
|
|
char *metafile;
|
|
|
|
u_long fileflags;
|
|
|
|
char *stripArgs;
|
2002-03-19 17:17:04 +03:00
|
|
|
char *afterinstallcmd;
|
2001-10-11 06:06:32 +04:00
|
|
|
char *suffix = BACKUP_SUFFIX;
|
2002-12-22 13:15:17 +03:00
|
|
|
char *destdir;
|
1994-11-14 07:57:16 +03:00
|
|
|
|
1997-03-16 22:43:19 +03:00
|
|
|
#define LN_ABSOLUTE 0x01
|
|
|
|
#define LN_RELATIVE 0x02
|
|
|
|
#define LN_HARD 0x04
|
|
|
|
#define LN_SYMBOLIC 0x08
|
|
|
|
#define LN_MIXED 0x10
|
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
#define DIRECTORY 0x01 /* Tell install it's a directory. */
|
|
|
|
#define SETFLAGS 0x02 /* Tell install to set flags. */
|
2001-10-11 06:06:32 +04:00
|
|
|
#define HASUID 0x04 /* Tell install the uid was given */
|
|
|
|
#define HASGID 0x08 /* Tell install the gid was given */
|
1994-11-14 07:57:16 +03:00
|
|
|
|
2002-03-19 17:17:04 +03:00
|
|
|
void afterinstall(const char *, const char *, int);
|
2001-10-11 06:06:32 +04:00
|
|
|
void backup(const char *);
|
2001-09-15 18:55:38 +04:00
|
|
|
void copy(int, char *, int, char *, off_t);
|
2001-11-23 19:14:51 +03:00
|
|
|
int do_link(char *, char *);
|
|
|
|
void do_symlink(char *, char *);
|
2001-10-11 06:06:32 +04:00
|
|
|
void install(char *, char *, u_int);
|
|
|
|
void install_dir(char *, u_int);
|
|
|
|
int main(int, char *[]);
|
2001-09-15 18:55:38 +04:00
|
|
|
void makelink(char *, char *);
|
2001-11-23 19:14:51 +03:00
|
|
|
void metadata_log(const char *, const char *, struct timeval *, const char *);
|
2001-10-11 06:06:32 +04:00
|
|
|
int parseid(char *, id_t *);
|
2001-09-15 18:55:38 +04:00
|
|
|
void strip(char *);
|
|
|
|
void usage(void);
|
2001-11-23 19:14:51 +03:00
|
|
|
char *xbasename(char *);
|
|
|
|
char *xdirname(char *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
int
|
2001-09-15 18:55:38 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-10-25 19:06:12 +04:00
|
|
|
struct stat from_sb, to_sb;
|
|
|
|
void *set;
|
|
|
|
u_int iflags;
|
|
|
|
int ch, no_target;
|
|
|
|
char *p, *to_name;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-02-21 03:06:22 +03:00
|
|
|
setprogname(argv[0]);
|
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
iflags = 0;
|
2002-12-22 13:15:17 +03:00
|
|
|
while ((ch = getopt(argc, argv, "a:cbB:dD:f:g:l:m:M:N:o:prsS:T:U"))
|
|
|
|
!= -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch((char)ch) {
|
2002-03-19 17:17:04 +03:00
|
|
|
case 'a':
|
|
|
|
afterinstallcmd = strdup(optarg);
|
|
|
|
if (afterinstallcmd == NULL)
|
|
|
|
errx(1, "%s", strerror(ENOMEM));
|
|
|
|
break;
|
1999-01-25 04:42:57 +03:00
|
|
|
case 'B':
|
|
|
|
suffix = optarg;
|
1999-03-29 21:01:49 +04:00
|
|
|
numberedbackup = 0;
|
|
|
|
{
|
|
|
|
/* Check if given suffix really generates
|
|
|
|
different suffixes - catch e.g. ".%" */
|
|
|
|
char suffix_expanded0[FILENAME_MAX],
|
|
|
|
suffix_expanded1[FILENAME_MAX];
|
|
|
|
(void)snprintf(suffix_expanded0, FILENAME_MAX,
|
|
|
|
suffix, 0);
|
|
|
|
(void)snprintf(suffix_expanded1, FILENAME_MAX,
|
|
|
|
suffix, 1);
|
|
|
|
if (strcmp(suffix_expanded0, suffix_expanded1)
|
|
|
|
!= 0)
|
|
|
|
numberedbackup = 1;
|
|
|
|
}
|
1999-01-25 04:42:57 +03:00
|
|
|
/* fall through; -B implies -b */
|
2001-11-23 19:14:51 +03:00
|
|
|
/*FALLTHROUGH*/
|
1999-01-25 04:42:57 +03:00
|
|
|
case 'b':
|
|
|
|
dobackup = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'c':
|
|
|
|
docopy = 1;
|
|
|
|
break;
|
1998-09-28 12:16:15 +04:00
|
|
|
case 'd':
|
|
|
|
dodir = 1;
|
|
|
|
break;
|
2002-12-22 13:15:17 +03:00
|
|
|
case 'D':
|
|
|
|
destdir = optarg;
|
|
|
|
break;
|
2003-10-27 03:12:41 +03:00
|
|
|
#if ! HAVE_NBTOOL_CONFIG_H
|
1994-11-14 07:57:16 +03:00
|
|
|
case 'f':
|
2001-10-19 18:26:19 +04:00
|
|
|
fflags = optarg;
|
1994-11-14 07:57:16 +03:00
|
|
|
break;
|
2002-01-21 23:00:02 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'g':
|
|
|
|
group = optarg;
|
|
|
|
break;
|
1997-03-16 22:43:19 +03:00
|
|
|
case 'l':
|
|
|
|
for (p = optarg; *p; 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);
|
2001-11-23 19:14:51 +03:00
|
|
|
/* NOTREACHED */
|
1997-03-16 22:43:19 +03:00
|
|
|
}
|
|
|
|
break;
|
1998-09-28 12:16:15 +04:00
|
|
|
case 'm':
|
|
|
|
if (!(set = setmode(optarg)))
|
|
|
|
errx(1, "%s: invalid file mode", optarg);
|
|
|
|
mode = getmode(set, 0);
|
2000-10-10 18:30:40 +04:00
|
|
|
free(set);
|
1998-09-28 12:16:15 +04:00
|
|
|
break;
|
2001-10-11 06:06:32 +04:00
|
|
|
case 'M':
|
|
|
|
metafile = optarg;
|
|
|
|
break;
|
2002-12-19 11:30:39 +03:00
|
|
|
case 'N':
|
|
|
|
if (! setup_getid(optarg))
|
|
|
|
errx(1,
|
|
|
|
"Unable to use user and group databases in `%s'",
|
|
|
|
optarg);
|
|
|
|
break;
|
1998-09-28 12:16:15 +04:00
|
|
|
case 'o':
|
|
|
|
owner = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
dopreserve = 1;
|
|
|
|
break;
|
1999-02-04 14:56:48 +03:00
|
|
|
case 'r':
|
|
|
|
dorename = 1;
|
|
|
|
break;
|
1998-10-08 06:12:51 +04:00
|
|
|
case 'S':
|
2001-09-15 20:45:23 +04:00
|
|
|
stripArgs = strdup(optarg);
|
|
|
|
if (stripArgs == NULL)
|
|
|
|
errx(1, "%s", strerror(ENOMEM));
|
1998-10-08 06:12:51 +04:00
|
|
|
/* fall through; -S implies -s */
|
2001-11-23 19:14:51 +03:00
|
|
|
/*FALLTHROUGH*/
|
1998-09-28 12:16:15 +04:00
|
|
|
case 's':
|
|
|
|
dostrip = 1;
|
|
|
|
break;
|
2001-10-25 19:37:06 +04:00
|
|
|
case 'T':
|
|
|
|
tags = optarg;
|
|
|
|
break;
|
1999-08-01 09:02:06 +04:00
|
|
|
case 'U':
|
2001-10-11 06:06:32 +04:00
|
|
|
dounpriv = 1;
|
1999-08-01 09:02:06 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-06-11 22:30:46 +04:00
|
|
|
|
1997-12-01 01:20:00 +03:00
|
|
|
/* strip and link options make no sense when creating directories */
|
|
|
|
if ((dostrip || dolink) && dodir)
|
1997-03-16 22:43:19 +03:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* strip and flags make no sense with links */
|
2001-10-19 18:26:19 +04:00
|
|
|
if ((dostrip || fflags) && dolink)
|
1993-06-11 22:30:46 +04:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* must have at least two arguments, except when creating directories */
|
|
|
|
if (argc < 2 && !dodir)
|
1993-03-21 12:45:37 +03:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* get group and owner id's */
|
2001-10-19 18:26:19 +04:00
|
|
|
if (group && !dounpriv) {
|
2002-12-19 11:30:39 +03:00
|
|
|
if (gid_from_group(group, &gid) == -1 && ! parseid(group, &gid))
|
2000-07-27 07:57:50 +04:00
|
|
|
errx(1, "unknown group %s", group);
|
2001-10-11 06:06:32 +04:00
|
|
|
iflags |= HASGID;
|
|
|
|
}
|
2001-10-19 18:26:19 +04:00
|
|
|
if (owner && !dounpriv) {
|
2002-12-19 11:30:39 +03:00
|
|
|
if (uid_from_user(owner, &uid) == -1 && ! parseid(owner, &uid))
|
2000-07-27 07:57:50 +04:00
|
|
|
errx(1, "unknown user %s", owner);
|
2001-10-11 06:06:32 +04:00
|
|
|
iflags |= HASUID;
|
|
|
|
}
|
|
|
|
|
2003-10-27 03:12:41 +03:00
|
|
|
#if ! HAVE_NBTOOL_CONFIG_H
|
2001-10-19 18:26:19 +04:00
|
|
|
if (fflags && !dounpriv) {
|
|
|
|
if (string_to_flags(&fflags, &fileflags, NULL))
|
|
|
|
errx(1, "%s: invalid flag", fflags);
|
|
|
|
iflags |= SETFLAGS;
|
|
|
|
}
|
2002-01-21 23:00:02 +03:00
|
|
|
#endif
|
2001-10-19 18:26:19 +04:00
|
|
|
|
2001-10-11 06:06:32 +04:00
|
|
|
if (metafile) {
|
|
|
|
if ((metafp = fopen(metafile, "a")) == NULL)
|
|
|
|
warn("open %s", metafile);
|
2000-07-27 07:57:50 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-06-11 22:30:46 +04:00
|
|
|
if (dodir) {
|
|
|
|
for (; *argv != NULL; ++argv)
|
2001-10-11 06:06:32 +04:00
|
|
|
install_dir(*argv, iflags);
|
1993-06-11 22:30:46 +04:00
|
|
|
exit (0);
|
1994-11-14 07:57:16 +03:00
|
|
|
}
|
1993-06-11 22:30:46 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
no_target = stat(to_name = argv[argc - 1], &to_sb);
|
1993-06-11 22:30:46 +04:00
|
|
|
if (!no_target && S_ISDIR(to_sb.st_mode)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
for (; *argv != to_name; ++argv)
|
2001-10-11 06:06:32 +04:00
|
|
|
install(*argv, to_name, iflags | DIRECTORY);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can't do file1 file2 directory/file */
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (!no_target) {
|
2001-11-23 19:14:51 +03:00
|
|
|
/* makelink() handles checks for links */
|
|
|
|
if (!dolink) {
|
|
|
|
if (stat(*argv, &from_sb))
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: stat", *argv);
|
2001-11-23 19:14:51 +03:00
|
|
|
if (!S_ISREG(to_sb.st_mode))
|
|
|
|
errx(1, "%s: not a regular file", to_name);
|
|
|
|
if (to_sb.st_dev == from_sb.st_dev &&
|
|
|
|
to_sb.st_ino == from_sb.st_ino)
|
|
|
|
errx(1, "%s and %s are the same file", *argv,
|
|
|
|
to_name);
|
|
|
|
}
|
1994-11-14 07:57:16 +03:00
|
|
|
/*
|
|
|
|
* Unlink now... avoid ETXTBSY errors later. Try and turn
|
|
|
|
* off the append/immutable bits -- if we fail, go ahead,
|
|
|
|
* it might work.
|
|
|
|
*/
|
2003-10-27 03:12:41 +03:00
|
|
|
#if ! HAVE_NBTOOL_CONFIG_H
|
1994-11-14 07:57:16 +03:00
|
|
|
#define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
|
|
|
|
if (to_sb.st_flags & NOCHANGEBITS)
|
|
|
|
(void)chflags(to_name,
|
|
|
|
to_sb.st_flags & ~(NOCHANGEBITS));
|
2002-01-21 23:00:02 +03:00
|
|
|
#endif
|
1999-01-26 04:34:25 +03:00
|
|
|
if (dobackup)
|
|
|
|
backup(to_name);
|
1999-03-24 18:13:46 +03:00
|
|
|
else if (!dorename)
|
1999-01-25 04:42:57 +03:00
|
|
|
(void)unlink(to_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2001-10-11 06:06:32 +04:00
|
|
|
install(*argv, to_name, iflags);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2001-10-11 06:06:32 +04:00
|
|
|
/*
|
|
|
|
* parseid --
|
|
|
|
* parse uid or gid from arg into id, returning non-zero if successful
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
parseid(char *name, id_t *id)
|
|
|
|
{
|
|
|
|
char *ep;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
*id = (id_t)strtoul(name, &ep, 10);
|
|
|
|
if (errno || *ep != '\0')
|
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2001-11-23 19:14:51 +03:00
|
|
|
/*
|
|
|
|
* do_link --
|
|
|
|
* make a hard link, obeying dorename if set
|
|
|
|
* return -1 on failure
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
do_link(char *from_name, char *to_name)
|
|
|
|
{
|
|
|
|
char tmpl[MAXPATHLEN];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (dorename) {
|
|
|
|
(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
|
|
|
|
xdirname(to_name));
|
2001-11-23 21:14:51 +03:00
|
|
|
/* This usage is safe. The linker will bitch anyway. */
|
2001-11-23 19:14:51 +03:00
|
|
|
if (mktemp(tmpl) == NULL)
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: mktemp", tmpl);
|
2001-11-23 19:14:51 +03:00
|
|
|
ret = link(from_name, tmpl);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = rename(tmpl, to_name);
|
|
|
|
if (ret < 0)
|
|
|
|
/* remove temporary link before exiting */
|
|
|
|
(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
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
do_symlink(char *from_name, char *to_name)
|
|
|
|
{
|
|
|
|
char tmpl[MAXPATHLEN];
|
|
|
|
|
|
|
|
if (dorename) {
|
|
|
|
(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
|
|
|
|
xdirname(to_name));
|
2001-11-23 21:14:51 +03:00
|
|
|
/* This usage is safe. The linker will bitch anyway. */
|
2001-11-23 19:14:51 +03:00
|
|
|
if (mktemp(tmpl) == NULL)
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: mktemp", tmpl);
|
2001-11-23 19:14:51 +03:00
|
|
|
|
|
|
|
if (symlink(from_name, tmpl) == -1)
|
|
|
|
err(1, "symlink %s -> %s", from_name, tmpl);
|
|
|
|
if (rename(tmpl, to_name) == -1) {
|
|
|
|
/* remove temporary link before exiting */
|
|
|
|
(void)unlink(tmpl);
|
|
|
|
err(1, "%s: rename", to_name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (symlink(from_name, to_name) == -1)
|
|
|
|
err(1, "symlink %s -> %s", from_name, to_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-03-16 22:43:19 +03:00
|
|
|
/*
|
|
|
|
* makelink --
|
|
|
|
* make a link from source to destination
|
|
|
|
*/
|
|
|
|
void
|
2001-09-15 18:55:38 +04:00
|
|
|
makelink(char *from_name, char *to_name)
|
1997-03-16 22:43:19 +03:00
|
|
|
{
|
2001-10-25 19:06:12 +04:00
|
|
|
char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
|
2002-02-28 03:22:51 +03:00
|
|
|
struct stat to_sb;
|
1997-03-16 22:43:19 +03:00
|
|
|
|
|
|
|
/* Try hard links first */
|
|
|
|
if (dolink & (LN_HARD|LN_MIXED)) {
|
2001-11-23 19:14:51 +03:00
|
|
|
if (do_link(from_name, to_name) == -1) {
|
1997-03-16 22:43:19 +03:00
|
|
|
if ((dolink & LN_HARD) || errno != EXDEV)
|
|
|
|
err(1, "link %s -> %s", from_name, to_name);
|
2002-02-28 03:22:51 +03:00
|
|
|
} else {
|
|
|
|
if (stat(to_name, &to_sb))
|
|
|
|
err(1, "%s: stat", to_name);
|
2002-03-19 17:17:04 +03:00
|
|
|
if (S_ISREG(to_sb.st_mode)) {
|
2002-02-28 03:22:51 +03:00
|
|
|
/* XXX: only metalog hardlinked files */
|
2002-03-19 17:17:04 +03:00
|
|
|
int omode;
|
|
|
|
char *oowner, *ogroup, *offlags;
|
|
|
|
|
|
|
|
/* XXX: use underlying perms */
|
|
|
|
omode = mode;
|
|
|
|
mode = (to_sb.st_mode & 0777);
|
|
|
|
oowner = owner;
|
|
|
|
owner = NULL;
|
|
|
|
ogroup = group;
|
|
|
|
group = NULL;
|
|
|
|
offlags = fflags;
|
|
|
|
fflags = NULL;
|
|
|
|
metadata_log(to_name, "file", NULL, NULL);
|
|
|
|
mode = omode;
|
|
|
|
owner = oowner;
|
|
|
|
group = ogroup;
|
|
|
|
fflags = offlags;
|
|
|
|
}
|
1997-03-16 22:43:19 +03:00
|
|
|
return;
|
2001-10-29 05:57:21 +03:00
|
|
|
}
|
1997-03-16 22:43:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Symbolic links */
|
|
|
|
if (dolink & LN_ABSOLUTE) {
|
|
|
|
/* Convert source path to absolute */
|
|
|
|
if (realpath(from_name, src) == NULL)
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: realpath", from_name);
|
2001-11-23 19:14:51 +03:00
|
|
|
do_symlink(src, to_name);
|
2001-11-19 05:51:00 +03:00
|
|
|
metadata_log(to_name, "link", NULL, src);
|
1997-03-16 22:43:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dolink & LN_RELATIVE) {
|
2001-11-23 19:14:51 +03:00
|
|
|
char *cp, *d, *s;
|
1997-03-16 22:43:19 +03:00
|
|
|
|
|
|
|
/* Resolve pathnames */
|
|
|
|
if (realpath(from_name, src) == NULL)
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: realpath", from_name);
|
2001-11-23 19:14:51 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The last component of to_name may be a symlink,
|
|
|
|
* so use realpath to resolve only the directory.
|
|
|
|
*/
|
2001-11-25 00:58:42 +03:00
|
|
|
cp = xdirname(to_name);
|
2001-11-23 19:14:51 +03:00
|
|
|
if (realpath(cp, dst) == NULL)
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: realpath", cp);
|
2001-11-23 19:14:51 +03:00
|
|
|
/* .. and add the last component */
|
|
|
|
if (strcmp(dst, "/") != 0) {
|
|
|
|
if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
|
|
|
|
errx(1, "resolved pathname too long");
|
|
|
|
}
|
|
|
|
cp = xbasename(to_name);
|
|
|
|
if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
|
|
|
|
errx(1, "resolved pathname too long");
|
1997-03-16 22:43:19 +03:00
|
|
|
|
|
|
|
/* 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 == '/')
|
2003-07-12 18:30:53 +04:00
|
|
|
(void)strlcat(lnk, "../", sizeof(lnk));
|
1997-03-16 22:43:19 +03:00
|
|
|
|
2003-07-12 18:30:53 +04:00
|
|
|
(void)strlcat(lnk, ++s, sizeof(lnk));
|
1997-03-16 22:43:19 +03:00
|
|
|
|
2001-11-23 19:14:51 +03:00
|
|
|
do_symlink(lnk, dst);
|
2001-11-19 05:51:00 +03:00
|
|
|
metadata_log(dst, "link", NULL, lnk);
|
1997-03-16 22:43:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If absolute or relative was not specified,
|
|
|
|
* try the names the user provided
|
|
|
|
*/
|
2001-11-23 19:14:51 +03:00
|
|
|
do_symlink(from_name, to_name);
|
2001-11-19 05:51:00 +03:00
|
|
|
metadata_log(to_name, "link", NULL, from_name);
|
1997-03-16 22:43:19 +03:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* install --
|
|
|
|
* build a path name and install the file
|
|
|
|
*/
|
1994-11-14 07:57:16 +03:00
|
|
|
void
|
2001-10-11 06:06:32 +04:00
|
|
|
install(char *from_name, char *to_name, u_int flags)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-01-21 23:00:02 +03:00
|
|
|
struct stat from_sb;
|
2003-10-27 03:12:41 +03:00
|
|
|
#if ! HAVE_NBTOOL_CONFIG_H
|
2002-01-21 23:00:02 +03:00
|
|
|
struct stat to_sb;
|
|
|
|
#endif
|
2001-10-25 19:06:12 +04:00
|
|
|
struct timeval tv[2];
|
2001-11-23 02:27:38 +03:00
|
|
|
int devnull, from_fd, to_fd, serrno, tmpmode;
|
2001-10-25 19:06:12 +04:00
|
|
|
char *p, tmpl[MAXPATHLEN], *oto_name;
|
1994-11-14 07:57:16 +03:00
|
|
|
|
2002-04-10 10:12:04 +04:00
|
|
|
if (!dolink) {
|
|
|
|
/* ensure that from_sb & tv are sane if !dolink */
|
|
|
|
if (stat(from_name, &from_sb))
|
|
|
|
err(1, "%s: stat", from_name);
|
2002-04-10 10:02:52 +04:00
|
|
|
#ifdef BSD4_4
|
2002-04-10 10:12:04 +04:00
|
|
|
TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
|
|
|
|
TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
|
2002-04-10 10:02:52 +04:00
|
|
|
#else
|
2002-04-10 10:12:04 +04:00
|
|
|
tv[0].tv_sec = from_sb.st_atime;
|
|
|
|
tv[0].tv_usec = 0;
|
|
|
|
tv[1].tv_sec = from_sb.st_mtime;
|
|
|
|
tv[1].tv_usec = 0;
|
2002-04-10 10:02:52 +04:00
|
|
|
#endif
|
2002-04-10 10:12:04 +04:00
|
|
|
}
|
2002-04-10 10:02:52 +04:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
|
2001-10-29 03:25:44 +03:00
|
|
|
if (!dolink) {
|
|
|
|
if (!S_ISREG(from_sb.st_mode))
|
|
|
|
errx(1, "%s: not a regular file", from_name);
|
|
|
|
}
|
1994-11-14 07:57:16 +03:00
|
|
|
/* Build the target path. */
|
|
|
|
if (flags & DIRECTORY) {
|
|
|
|
(void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
|
|
|
|
to_name,
|
1997-10-20 07:32:25 +04:00
|
|
|
(p = strrchr(from_name, '/')) ? ++p : from_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
to_name = pathbuf;
|
|
|
|
}
|
|
|
|
devnull = 0;
|
1994-11-14 07:57:16 +03:00
|
|
|
} else {
|
2002-01-21 23:00:02 +03:00
|
|
|
#if HAVE_STRUCT_STAT_ST_FLAGS
|
1994-11-14 07:57:16 +03:00
|
|
|
from_sb.st_flags = 0; /* XXX */
|
2002-01-21 23:00:02 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
devnull = 1;
|
1994-11-14 07:57:16 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
/*
|
|
|
|
* Unlink now... avoid ETXTBSY errors later. Try and turn
|
|
|
|
* off the append/immutable bits -- if we fail, go ahead,
|
|
|
|
* it might work.
|
|
|
|
*/
|
2003-10-27 03:12:41 +03:00
|
|
|
#if ! HAVE_NBTOOL_CONFIG_H
|
1994-11-14 07:57:16 +03:00
|
|
|
if (stat(to_name, &to_sb) == 0 &&
|
|
|
|
to_sb.st_flags & (NOCHANGEBITS))
|
|
|
|
(void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
|
2002-01-21 23:00:02 +03:00
|
|
|
#endif
|
1999-02-04 14:56:48 +03:00
|
|
|
if (dorename) {
|
2001-12-10 11:54:38 +03:00
|
|
|
(void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
|
2001-11-23 19:14:51 +03:00
|
|
|
xdirname(to_name));
|
1999-02-04 14:56:48 +03:00
|
|
|
oto_name = to_name;
|
|
|
|
to_name = tmpl;
|
|
|
|
} else {
|
1999-07-06 18:45:31 +04:00
|
|
|
oto_name = NULL; /* pacify gcc */
|
1999-02-04 14:56:48 +03:00
|
|
|
if (dobackup)
|
|
|
|
backup(to_name);
|
|
|
|
else
|
|
|
|
(void)unlink(to_name);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-16 22:43:19 +03:00
|
|
|
if (dolink) {
|
2001-11-23 19:14:51 +03:00
|
|
|
makelink(from_name, dorename ? oto_name : to_name);
|
1997-03-16 22:43:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
/* Create target. */
|
1999-02-04 14:56:48 +03:00
|
|
|
if (dorename) {
|
|
|
|
if ((to_fd = mkstemp(to_name)) == -1)
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: mkstemp", to_name);
|
1999-02-04 14:56:48 +03:00
|
|
|
} else {
|
|
|
|
if ((to_fd = open(to_name,
|
|
|
|
O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: open", to_name);
|
1999-02-04 14:56:48 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!devnull) {
|
|
|
|
if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
|
|
|
|
(void)unlink(to_name);
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: open", from_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-11-14 07:57:16 +03:00
|
|
|
copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)close(from_fd);
|
|
|
|
}
|
1995-12-20 13:25:17 +03:00
|
|
|
|
|
|
|
if (dostrip) {
|
1993-03-21 12:45:37 +03:00
|
|
|
strip(to_name);
|
1995-12-20 13:25:17 +03: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);
|
|
|
|
if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
|
2002-03-19 17:17:04 +03:00
|
|
|
err(1, "stripping %s", to_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (afterinstallcmd != NULL) {
|
|
|
|
afterinstall(afterinstallcmd, to_name, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-open our fd on the target, in case we used an
|
|
|
|
* after-install command that does not work in-place
|
|
|
|
*/
|
|
|
|
close(to_fd);
|
|
|
|
if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
|
|
|
|
err(1, "running after install command on %s", to_name);
|
1995-12-20 13:25:17 +03:00
|
|
|
}
|
|
|
|
|
2000-07-27 07:57:50 +04:00
|
|
|
/*
|
|
|
|
* Set owner, group, mode for target; do the chown first,
|
|
|
|
* chown may lose the setuid bits.
|
|
|
|
*/
|
2001-10-11 06:06:32 +04:00
|
|
|
if (!dounpriv &&
|
|
|
|
(flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
|
2000-07-27 07:57:50 +04:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2001-11-23 02:27:38 +03:00
|
|
|
tmpmode = mode;
|
2001-11-12 22:08:31 +03:00
|
|
|
if (dounpriv)
|
2001-11-23 02:27:38 +03:00
|
|
|
tmpmode &= S_IRWXU|S_IRWXG|S_IRWXO;
|
|
|
|
if (fchmod(to_fd, tmpmode) == -1) {
|
1994-11-14 07:57:16 +03:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(1, "%s: chmod: %s", to_name, strerror(serrno));
|
|
|
|
}
|
|
|
|
|
1998-09-28 12:16:15 +04:00
|
|
|
/*
|
|
|
|
* Preserve the date of the source file.
|
|
|
|
*/
|
|
|
|
if (dopreserve) {
|
2002-01-21 23:00:02 +03:00
|
|
|
#if HAVE_FUTIMES
|
|
|
|
if (futimes(to_fd, tv) == -1)
|
|
|
|
warn("%s: futimes", to_name);
|
|
|
|
#else
|
|
|
|
if (utimes(to_name, tv) == -1)
|
2002-01-28 22:44:03 +03:00
|
|
|
warn("%s: utimes", to_name);
|
2002-01-21 23:00:02 +03:00
|
|
|
#endif
|
1998-09-28 12:16:15 +04:00
|
|
|
}
|
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
(void)close(to_fd);
|
1999-02-04 14:56:48 +03:00
|
|
|
|
2001-10-11 06:06:32 +04:00
|
|
|
if (dorename) {
|
1999-02-04 14:56:48 +03:00
|
|
|
if (rename(to_name, oto_name) == -1)
|
|
|
|
err(1, "%s: rename", to_name);
|
2001-10-11 06:06:32 +04:00
|
|
|
to_name = oto_name;
|
|
|
|
}
|
1999-02-04 14:56:48 +03:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
if (!docopy && !devnull && unlink(from_name))
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: unlink", from_name);
|
2001-10-11 06:06:32 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If provided a set of flags, set them, otherwise, preserve the
|
|
|
|
* flags, except for the dump flag.
|
|
|
|
*/
|
2003-10-27 03:12:41 +03:00
|
|
|
#if ! HAVE_NBTOOL_CONFIG_H
|
2001-10-11 06:06:32 +04:00
|
|
|
if (!dounpriv && chflags(to_name,
|
2001-10-11 08:27:30 +04:00
|
|
|
flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
|
|
|
|
{
|
2001-10-11 06:06:32 +04:00
|
|
|
if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
|
|
|
|
warn("%s: chflags", to_name);
|
|
|
|
}
|
2002-01-21 23:00:02 +03:00
|
|
|
#endif
|
2001-10-11 06:06:32 +04:00
|
|
|
|
2001-11-19 05:51:00 +03:00
|
|
|
metadata_log(to_name, "file", tv, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy --
|
|
|
|
* copy from one file to another
|
|
|
|
*/
|
1994-11-14 07:57:16 +03:00
|
|
|
void
|
2001-09-15 18:55:38 +04:00
|
|
|
copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-10-25 19:06:12 +04:00
|
|
|
ssize_t nr, nw;
|
|
|
|
int serrno;
|
|
|
|
char *p;
|
|
|
|
char buf[MAXBSIZE];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-11-14 07:57:16 +03:00
|
|
|
/*
|
1998-10-08 06:12:51 +04:00
|
|
|
* There's no reason to do anything other than close the file
|
|
|
|
* now if it's empty, so let's not bother.
|
1994-11-14 07:57:16 +03:00
|
|
|
*/
|
1998-10-08 06:12:51 +04:00
|
|
|
if (size > 0) {
|
2001-05-28 07:37:22 +04:00
|
|
|
|
1998-10-08 06:12:51 +04:00
|
|
|
/*
|
2001-05-28 07:37:22 +04: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.
|
1998-10-08 06:12:51 +04:00
|
|
|
*/
|
2001-05-28 07:37:22 +04:00
|
|
|
|
1998-10-08 06:12:51 +04:00
|
|
|
if (size <= 8 * 1048576) {
|
|
|
|
if ((p = mmap(NULL, (size_t)size, PROT_READ,
|
2001-05-28 07:37:22 +04:00
|
|
|
MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
|
|
|
|
== MAP_FAILED) {
|
|
|
|
goto mmap_failed;
|
1999-06-26 04:41:39 +04:00
|
|
|
}
|
2003-03-13 21:23:02 +03:00
|
|
|
#if defined(MADV_SEQUENTIAL) && !defined(__APPLE__)
|
2000-01-15 04:10:12 +03:00
|
|
|
if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
|
|
|
|
&& errno != EOPNOTSUPP)
|
1999-08-01 09:02:06 +04:00
|
|
|
warnx("madvise: %s", strerror(errno));
|
2001-03-22 02:16:33 +03:00
|
|
|
#endif
|
1999-08-01 09:02:06 +04:00
|
|
|
|
1999-06-26 04:41:39 +04:00
|
|
|
if (write(to_fd, p, size) != size) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
2002-01-28 22:44:03 +03:00
|
|
|
errx(1, "%s: write: %s",
|
1999-06-26 04:41:39 +04:00
|
|
|
to_name, strerror(serrno));
|
|
|
|
}
|
1998-10-08 06:12:51 +04:00
|
|
|
} else {
|
2001-05-28 07:37:22 +04:00
|
|
|
mmap_failed:
|
1999-06-26 04:41:39 +04:00
|
|
|
while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
|
1998-10-08 06:12:51 +04:00
|
|
|
if ((nw = write(to_fd, buf, nr)) != nr) {
|
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
2002-01-28 22:44:03 +03:00
|
|
|
errx(1, "%s: write: %s", to_name,
|
2001-05-28 07:37:22 +04:00
|
|
|
strerror(nw > 0 ? EIO : serrno));
|
1998-10-08 06:12:51 +04:00
|
|
|
}
|
1999-06-26 04:41:39 +04:00
|
|
|
}
|
1998-10-08 06:12:51 +04:00
|
|
|
if (nr != 0) {
|
1994-11-14 07:57:16 +03:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
2002-01-28 22:44:03 +03:00
|
|
|
errx(1, "%s: read: %s", from_name, strerror(serrno));
|
1994-11-14 07:57:16 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* strip --
|
|
|
|
* use strip(1) to strip the target file
|
|
|
|
*/
|
1994-11-14 07:57:16 +03:00
|
|
|
void
|
2001-09-15 18:55:38 +04:00
|
|
|
strip(char *to_name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-10-25 19:06:12 +04:00
|
|
|
int serrno, status;
|
|
|
|
char *stripprog;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
switch (vfork()) {
|
|
|
|
case -1:
|
1994-11-14 07:57:16 +03:00
|
|
|
serrno = errno;
|
|
|
|
(void)unlink(to_name);
|
1997-10-28 02:16:34 +03:00
|
|
|
errx(1, "vfork: %s", strerror(serrno));
|
2001-11-23 19:14:51 +03:00
|
|
|
/*NOTREACHED*/
|
1993-03-21 12:45:37 +03:00
|
|
|
case 0:
|
1997-04-17 11:56:55 +04:00
|
|
|
stripprog = getenv("STRIP");
|
|
|
|
if (stripprog == NULL)
|
|
|
|
stripprog = _PATH_STRIP;
|
1998-10-08 06:12:51 +04:00
|
|
|
|
|
|
|
if (stripArgs) {
|
2001-10-11 06:06:32 +04:00
|
|
|
/*
|
|
|
|
* build up a command line and let /bin/sh
|
|
|
|
* parse the arguments
|
|
|
|
*/
|
1998-10-08 06:12:51 +04:00
|
|
|
char* cmd = (char*)malloc(sizeof(char)*
|
|
|
|
(3+strlen(stripprog)+
|
|
|
|
strlen(stripArgs)+
|
|
|
|
strlen(to_name)));
|
|
|
|
|
2001-09-15 20:45:23 +04:00
|
|
|
if (cmd == NULL)
|
|
|
|
errx(1, "%s", strerror(ENOMEM));
|
|
|
|
|
1998-10-08 06:12:51 +04:00
|
|
|
sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
|
|
|
|
|
|
|
|
execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
|
|
|
|
} else
|
2000-07-27 07:45:02 +04:00
|
|
|
execlp(stripprog, "strip", to_name, NULL);
|
1998-10-08 06:12:51 +04:00
|
|
|
|
2002-01-28 22:44:03 +03:00
|
|
|
warn("%s: exec of strip", stripprog);
|
1997-10-28 02:16:34 +03:00
|
|
|
_exit(1);
|
2001-11-23 19:14:51 +03:00
|
|
|
/*NOTREACHED*/
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
|
|
|
if (wait(&status) == -1 || status)
|
1994-11-14 07:57:16 +03:00
|
|
|
(void)unlink(to_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-19 17:17:04 +03:00
|
|
|
/*
|
|
|
|
* afterinstall --
|
|
|
|
* run provided command on the target file or directory after it's been
|
|
|
|
* installed and stripped, but before permissions are set or it's renamed
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
afterinstall(const char *command, const char *to_name, int errunlink)
|
|
|
|
{
|
|
|
|
int serrno, status;
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
switch (vfork()) {
|
|
|
|
case -1:
|
|
|
|
serrno = errno;
|
|
|
|
if (errunlink)
|
|
|
|
(void)unlink(to_name);
|
|
|
|
errx(1, "vfork: %s", strerror(serrno));
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* build up a command line and let /bin/sh
|
|
|
|
* parse the arguments
|
|
|
|
*/
|
|
|
|
cmd = (char*)malloc(sizeof(char)*
|
|
|
|
(2+strlen(command)+
|
|
|
|
strlen(to_name)));
|
|
|
|
|
|
|
|
if (cmd == NULL)
|
|
|
|
errx(1, "%s", strerror(ENOMEM));
|
|
|
|
|
|
|
|
sprintf(cmd, "%s %s", command, to_name);
|
|
|
|
|
|
|
|
execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
|
|
|
|
|
|
|
|
warn("%s: exec of after install command", command);
|
|
|
|
_exit(1);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
default:
|
|
|
|
if ((wait(&status) == -1 || status) && errunlink)
|
|
|
|
(void)unlink(to_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-26 04:34:25 +03:00
|
|
|
/*
|
2001-11-23 19:14:51 +03:00
|
|
|
* backup --
|
|
|
|
* backup file "to_name" to to_name.suffix
|
|
|
|
* if suffix contains a "%", it's taken as a printf(3) pattern
|
|
|
|
* used for a numbered backup.
|
1999-01-26 04:34:25 +03:00
|
|
|
*/
|
|
|
|
void
|
2001-09-15 18:55:38 +04:00
|
|
|
backup(const char *to_name)
|
1999-01-26 04:34:25 +03:00
|
|
|
{
|
2001-11-23 19:14:51 +03:00
|
|
|
char bname[FILENAME_MAX];
|
1999-01-26 04:34:25 +03:00
|
|
|
|
1999-03-29 21:01:49 +04:00
|
|
|
if (numberedbackup) {
|
1999-01-26 04:34:25 +03:00
|
|
|
/* Do numbered backup */
|
|
|
|
int cnt;
|
|
|
|
char suffix_expanded[FILENAME_MAX];
|
|
|
|
|
|
|
|
cnt=0;
|
|
|
|
do {
|
2001-10-11 06:06:32 +04:00
|
|
|
(void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
|
|
|
|
cnt);
|
2001-11-23 19:14:51 +03:00
|
|
|
(void)snprintf(bname, FILENAME_MAX, "%s%s", to_name,
|
|
|
|
suffix_expanded);
|
1999-01-26 04:34:25 +03:00
|
|
|
cnt++;
|
2001-11-23 19:14:51 +03:00
|
|
|
} while (access(bname, F_OK) == 0);
|
1999-01-26 04:34:25 +03:00
|
|
|
} else {
|
|
|
|
/* Do simple backup */
|
2001-11-23 19:14:51 +03:00
|
|
|
(void)snprintf(bname, FILENAME_MAX, "%s%s", to_name, suffix);
|
1999-01-26 04:34:25 +03:00
|
|
|
}
|
|
|
|
|
2001-11-23 19:14:51 +03:00
|
|
|
(void)rename(to_name, bname);
|
1999-01-26 04:34:25 +03:00
|
|
|
}
|
|
|
|
|
1993-06-11 22:30:46 +04:00
|
|
|
/*
|
1994-11-14 07:57:16 +03:00
|
|
|
* install_dir --
|
2001-08-24 14:24:45 +04:00
|
|
|
* build directory hierarchy
|
1993-06-11 22:30:46 +04:00
|
|
|
*/
|
1994-12-20 04:24:38 +03:00
|
|
|
void
|
2001-10-11 06:06:32 +04:00
|
|
|
install_dir(char *path, u_int flags)
|
1993-06-11 22:30:46 +04:00
|
|
|
{
|
2001-10-25 19:06:12 +04:00
|
|
|
char *p;
|
|
|
|
struct stat sb;
|
|
|
|
int ch;
|
1993-06-11 22:30:46 +04:00
|
|
|
|
1994-10-03 00:32:31 +03:00
|
|
|
for (p = path;; ++p)
|
|
|
|
if (!*p || (p != path && *p == '/')) {
|
1993-06-11 22:30:46 +04:00
|
|
|
ch = *p;
|
|
|
|
*p = '\0';
|
|
|
|
if (stat(path, &sb)) {
|
|
|
|
if (errno != ENOENT || mkdir(path, 0777) < 0) {
|
2002-01-28 22:44:03 +03:00
|
|
|
err(1, "%s: mkdir", path);
|
1993-06-11 22:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(*p = ch))
|
1994-10-03 00:32:31 +03:00
|
|
|
break;
|
1993-06-11 22:30:46 +04:00
|
|
|
}
|
|
|
|
|
2002-03-19 17:17:04 +03:00
|
|
|
if (afterinstallcmd != NULL)
|
|
|
|
afterinstall(afterinstallcmd, path, 0);
|
|
|
|
|
2001-10-11 06:06:32 +04:00
|
|
|
if (!dounpriv && (
|
|
|
|
((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
|
|
|
|
|| chmod(path, mode) == -1 )) {
|
2002-01-28 22:44:03 +03:00
|
|
|
warn("%s: chown/chmod", path);
|
2000-07-27 07:57:50 +04:00
|
|
|
}
|
2001-11-19 05:51:00 +03:00
|
|
|
metadata_log(path, "dir", NULL, NULL);
|
1993-06-11 22:30:46 +04:00
|
|
|
}
|
|
|
|
|
2001-10-11 06:06:32 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
void
|
2001-11-19 05:51:00 +03:00
|
|
|
metadata_log(const char *path, const char *type, struct timeval *tv,
|
2001-10-29 05:57:21 +03:00
|
|
|
const char *link)
|
2001-10-11 06:06:32 +04:00
|
|
|
{
|
2002-06-08 19:02:27 +04:00
|
|
|
static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
|
2002-12-22 13:15:17 +03:00
|
|
|
char *buf, *p;
|
|
|
|
size_t destlen;
|
2002-06-09 08:16:39 +04:00
|
|
|
struct flock metalog_lock;
|
2001-10-11 06:06:32 +04:00
|
|
|
|
|
|
|
if (!metafp)
|
|
|
|
return;
|
|
|
|
buf = (char *)malloc(4 * strlen(path) + 1); /* buf for strsvis(3) */
|
|
|
|
if (buf == NULL) {
|
|
|
|
warnx("%s", strerror(ENOMEM));
|
|
|
|
return;
|
|
|
|
}
|
2002-06-09 08:16:39 +04:00
|
|
|
/* 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) {
|
2001-10-11 06:06:32 +04:00
|
|
|
warn("can't lock %s", metafile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strsvis(buf, path, VIS_CSTYLE, extra); /* encode name */
|
2002-12-22 13:15:17 +03:00
|
|
|
p = buf; /* remove destdir */
|
|
|
|
if (destdir) {
|
|
|
|
destlen = strlen(destdir);
|
2002-12-23 09:24:15 +03:00
|
|
|
if (strncmp(p, destdir, destlen) == 0 &&
|
|
|
|
(p[destlen] == '/' || p[destlen] == '\0'))
|
2002-12-22 13:15:17 +03:00
|
|
|
p += destlen;
|
|
|
|
}
|
|
|
|
while (*p && *p == '/') /* remove leading /s */
|
|
|
|
p++;
|
|
|
|
/* print details */
|
2002-12-23 09:24:15 +03:00
|
|
|
fprintf(metafp, ".%s%s type=%s mode=%#o", *p ? "/" : "", p, type, mode);
|
2001-10-29 05:57:21 +03:00
|
|
|
if (link)
|
|
|
|
fprintf(metafp, " link=%s", link);
|
2001-10-19 18:26:19 +04:00
|
|
|
if (owner)
|
|
|
|
fprintf(metafp, " uname=%s", owner);
|
|
|
|
if (group)
|
|
|
|
fprintf(metafp, " gname=%s", group);
|
|
|
|
if (fflags)
|
|
|
|
fprintf(metafp, " flags=%s", fflags);
|
2001-10-25 19:37:06 +04:00
|
|
|
if (tags)
|
|
|
|
fprintf(metafp, " tags=%s", tags);
|
2001-10-29 05:57:21 +03:00
|
|
|
if (tv != NULL && dopreserve)
|
2001-10-11 06:06:32 +04:00
|
|
|
fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
|
|
|
|
fputc('\n', metafp);
|
|
|
|
fflush(metafp); /* flush output */
|
2002-06-09 08:16:39 +04:00
|
|
|
/* unlock log file */
|
|
|
|
metalog_lock.l_type = F_UNLCK;
|
|
|
|
if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
|
2001-10-11 06:06:32 +04:00
|
|
|
warn("can't unlock %s", metafile);
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2001-11-23 19:14:51 +03:00
|
|
|
/*
|
|
|
|
* xbasename --
|
|
|
|
* libc basename(3) that returns a pointer to a static buffer
|
|
|
|
* instead of overwriting that passed-in string.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
xbasename(char *path)
|
|
|
|
{
|
|
|
|
static char tmp[MAXPATHLEN];
|
|
|
|
|
|
|
|
(void)strlcpy(tmp, path, sizeof(tmp));
|
|
|
|
return (basename(tmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xdirname --
|
|
|
|
* libc dirname(3) that returns a pointer to a static buffer
|
|
|
|
* instead of overwriting that passed-in string.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
xdirname(char *path)
|
|
|
|
{
|
|
|
|
static char tmp[MAXPATHLEN];
|
2001-10-11 06:06:32 +04:00
|
|
|
|
2001-11-23 19:14:51 +03:00
|
|
|
(void)strlcpy(tmp, path, sizeof(tmp));
|
|
|
|
return (dirname(tmp));
|
|
|
|
}
|
2001-10-11 06:06:32 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* usage --
|
|
|
|
* print a usage message and die
|
|
|
|
*/
|
1994-11-14 07:57:16 +03:00
|
|
|
void
|
2001-09-15 18:55:38 +04:00
|
|
|
usage(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-03-19 17:17:04 +03:00
|
|
|
const char *prog;
|
|
|
|
|
|
|
|
prog = getprogname();
|
|
|
|
|
|
|
|
(void)fprintf(stderr,
|
2002-12-22 13:15:17 +03:00
|
|
|
"usage: %s [-Ubcprs] [-M log] [-D dest] [-T tags] [-B suffix]\n"
|
|
|
|
" [-a aftercmd] [-f flags] [-m mode] [-N dbdir] [-o owner] [-g group] \n"
|
|
|
|
" [-l linkflags] [-S stripflags] file1 file2\n"
|
|
|
|
" %s [-Ubcprs] [-M log] [-D dest] [-T tags] [-B suffix]\n"
|
|
|
|
" [-a aftercmd] [-f flags] [-m mode] [-N dbdir] [-o owner] [-g group]\n"
|
|
|
|
" [-l linkflags] [-S stripflags] file1 ... fileN directory\n"
|
|
|
|
" %s -d [-Up] [-M log] [-D dest] [-T tags] [-a aftercmd] [-m mode]\n"
|
2002-12-19 11:30:39 +03:00
|
|
|
" [-N dbdir] [-o owner] [-g group] directory ...\n",
|
2002-03-19 17:17:04 +03:00
|
|
|
prog, prog, prog);
|
1994-12-19 01:05:40 +03:00
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|