Home | History | Annotate | Line # | Download | only in restore
main.c revision 1.32.20.1
      1  1.32.20.1       mjf /*	$NetBSD: main.c,v 1.32.20.1 2008/09/28 11:17:14 mjf Exp $	*/
      2        1.9       cgd 
      3        1.1       cgd /*
      4        1.4   mycroft  * Copyright (c) 1983, 1993
      5        1.4   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.23       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  */
     31        1.1       cgd 
     32       1.15     lukem #include <sys/cdefs.h>
     33        1.1       cgd #ifndef lint
     34  1.32.20.1       mjf __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     35  1.32.20.1       mjf  The Regents of the University of California.  All rights reserved.");
     36        1.1       cgd #endif /* not lint */
     37        1.1       cgd 
     38        1.1       cgd #ifndef lint
     39        1.9       cgd #if 0
     40       1.16     lukem static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/4/95";
     41        1.9       cgd #else
     42  1.32.20.1       mjf __RCSID("$NetBSD: main.c,v 1.32.20.1 2008/09/28 11:17:14 mjf Exp $");
     43        1.9       cgd #endif
     44        1.1       cgd #endif /* not lint */
     45        1.1       cgd 
     46        1.3       cgd #include <sys/param.h>
     47        1.3       cgd #include <sys/time.h>
     48        1.1       cgd 
     49       1.16     lukem #include <ufs/ufs/dinode.h>
     50        1.4   mycroft #include <ufs/ffs/fs.h>
     51        1.1       cgd #include <protocols/dumprestore.h>
     52        1.3       cgd 
     53        1.4   mycroft #include <err.h>
     54        1.3       cgd #include <errno.h>
     55       1.12     lukem #include <paths.h>
     56        1.3       cgd #include <signal.h>
     57        1.3       cgd #include <stdio.h>
     58        1.3       cgd #include <stdlib.h>
     59        1.3       cgd #include <string.h>
     60       1.16     lukem #include <unistd.h>
     61        1.3       cgd 
     62        1.3       cgd #include "restore.h"
     63        1.3       cgd #include "extern.h"
     64        1.1       cgd 
     65        1.1       cgd int	bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
     66       1.24     enami int	hflag = 1, mflag = 1, Dflag = 0, Nflag = 0;
     67        1.1       cgd char	command = '\0';
     68       1.15     lukem int32_t	dumpnum = 1;
     69       1.15     lukem int32_t	volno = 0;
     70       1.15     lukem int32_t	ntrec;
     71        1.1       cgd char	*dumpmap;
     72        1.5   mycroft char	*usedinomap;
     73        1.1       cgd ino_t	maxino;
     74        1.1       cgd time_t	dumptime;
     75        1.1       cgd time_t	dumpdate;
     76       1.22     enami size_t	pagesize;
     77       1.25     enami FILE	*terminal;
     78       1.31  christos const char	*tmpdir;
     79       1.29     fredb int	dotflag = 0;
     80        1.1       cgd 
     81       1.27    bouyer FILE *Mtreefile = NULL;
     82       1.27    bouyer 
     83       1.30   xtraeme static	void obsolete(int *, char **[]);
     84       1.30   xtraeme static	void usage(void);
     85        1.3       cgd 
     86        1.3       cgd int
     87       1.30   xtraeme main(int argc, char *argv[])
     88        1.1       cgd {
     89        1.3       cgd 	int ch;
     90        1.1       cgd 	ino_t ino;
     91       1.31  christos 	const char *inputdev;
     92       1.31  christos 	const char *symtbl = "./restoresymtable";
     93        1.3       cgd 	char *p, name[MAXPATHLEN];
     94       1.31  christos 	static char dot[] = ".";
     95       1.11    scottr 
     96        1.3       cgd 	if (argc < 2)
     97        1.3       cgd 		usage();
     98        1.1       cgd 
     99       1.10       mrg 	if ((inputdev = getenv("TAPE")) == NULL)
    100       1.10       mrg 		inputdev = _PATH_DEFTAPE;
    101       1.13     lukem 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    102       1.13     lukem 		tmpdir = _PATH_TMP;
    103        1.3       cgd 	obsolete(&argc, &argv);
    104       1.27    bouyer 	while ((ch = getopt(argc, argv, "b:cD:df:himM:NRrs:tuvxy")) != -1)
    105       1.25     enami 		switch (ch) {
    106        1.3       cgd 		case 'b':
    107        1.3       cgd 			/* Change default tape blocksize. */
    108        1.3       cgd 			bflag = 1;
    109        1.3       cgd 			ntrec = strtol(optarg, &p, 10);
    110        1.3       cgd 			if (*p)
    111        1.4   mycroft 				errx(1, "illegal blocksize -- %s", optarg);
    112        1.3       cgd 			if (ntrec <= 0)
    113        1.4   mycroft 				errx(1, "block size must be greater than 0");
    114        1.1       cgd 			break;
    115        1.1       cgd 		case 'c':
    116        1.3       cgd 			cvtflag = 1;
    117        1.1       cgd 			break;
    118       1.24     enami 		case 'D':
    119       1.24     enami 			ddesc = digest_lookup(optarg);
    120       1.24     enami 			if (ddesc == NULL)
    121       1.24     enami 				err(1, "unknown digest algorithm: %s", optarg);
    122       1.24     enami 			Dflag = 1;
    123       1.24     enami 			break;
    124        1.1       cgd 		case 'd':
    125        1.3       cgd 			dflag = 1;
    126        1.3       cgd 			break;
    127        1.3       cgd 		case 'f':
    128        1.3       cgd 			inputdev = optarg;
    129        1.1       cgd 			break;
    130        1.1       cgd 		case 'h':
    131        1.1       cgd 			hflag = 0;
    132        1.1       cgd 			break;
    133        1.3       cgd 		case 'i':
    134        1.3       cgd 		case 'R':
    135        1.3       cgd 		case 'r':
    136        1.3       cgd 		case 't':
    137        1.3       cgd 		case 'x':
    138        1.3       cgd 			if (command != '\0')
    139        1.4   mycroft 				errx(1,
    140        1.4   mycroft 				    "%c and %c options are mutually exclusive",
    141        1.3       cgd 				    ch, command);
    142        1.3       cgd 			command = ch;
    143        1.3       cgd 			break;
    144        1.1       cgd 		case 'm':
    145        1.1       cgd 			mflag = 0;
    146        1.1       cgd 			break;
    147        1.1       cgd 		case 'N':
    148        1.3       cgd 			Nflag = 1;
    149        1.3       cgd 			break;
    150       1.27    bouyer 		case 'M':
    151       1.27    bouyer 			Mtreefile = fopen(optarg, "a");
    152       1.27    bouyer 			if (Mtreefile == NULL)
    153       1.27    bouyer 				err(1, "can't open %s", optarg);
    154       1.27    bouyer 			break;
    155        1.3       cgd 		case 's':
    156        1.3       cgd 			/* Dumpnum (skip to) for multifile dump tapes. */
    157        1.3       cgd 			dumpnum = strtol(optarg, &p, 10);
    158        1.3       cgd 			if (*p)
    159        1.4   mycroft 				errx(1, "illegal dump number -- %s", optarg);
    160        1.3       cgd 			if (dumpnum <= 0)
    161        1.4   mycroft 				errx(1, "dump number must be greater than 0");
    162       1.18  christos 			break;
    163       1.18  christos 		case 'u':
    164       1.18  christos 			uflag = 1;
    165        1.1       cgd 			break;
    166        1.1       cgd 		case 'v':
    167        1.3       cgd 			vflag = 1;
    168        1.1       cgd 			break;
    169        1.1       cgd 		case 'y':
    170        1.3       cgd 			yflag = 1;
    171        1.1       cgd 			break;
    172        1.1       cgd 		default:
    173        1.3       cgd 			usage();
    174        1.1       cgd 		}
    175        1.3       cgd 	argc -= optind;
    176        1.3       cgd 	argv += optind;
    177        1.3       cgd 
    178        1.3       cgd 	if (command == '\0')
    179        1.4   mycroft 		errx(1, "none of i, R, r, t or x options specified");
    180       1.20     enami 
    181       1.20     enami 	if (Nflag || command == 't')
    182       1.20     enami 		uflag = 0;
    183        1.3       cgd 
    184        1.3       cgd 	if (signal(SIGINT, onintr) == SIG_IGN)
    185        1.3       cgd 		(void) signal(SIGINT, SIG_IGN);
    186        1.3       cgd 	if (signal(SIGTERM, onintr) == SIG_IGN)
    187        1.3       cgd 		(void) signal(SIGTERM, SIG_IGN);
    188        1.3       cgd 	setlinebuf(stderr);
    189       1.22     enami 	pagesize = sysconf(_SC_PAGESIZE);
    190        1.3       cgd 
    191        1.7   mycroft 	atexit(cleanup);
    192        1.7   mycroft 
    193        1.1       cgd 	setinput(inputdev);
    194        1.3       cgd 
    195        1.1       cgd 	if (argc == 0) {
    196        1.1       cgd 		argc = 1;
    197       1.31  christos 		*--argv = dot;
    198        1.1       cgd 	}
    199        1.3       cgd 
    200        1.1       cgd 	switch (command) {
    201        1.1       cgd 	/*
    202        1.1       cgd 	 * Interactive mode.
    203        1.1       cgd 	 */
    204        1.1       cgd 	case 'i':
    205        1.1       cgd 		setup();
    206        1.1       cgd 		extractdirs(1);
    207        1.3       cgd 		initsymtable(NULL);
    208        1.1       cgd 		runcmdshell();
    209        1.3       cgd 		break;
    210        1.1       cgd 	/*
    211        1.1       cgd 	 * Incremental restoration of a file system.
    212        1.1       cgd 	 */
    213        1.1       cgd 	case 'r':
    214        1.1       cgd 		setup();
    215        1.1       cgd 		if (dumptime > 0) {
    216        1.1       cgd 			/*
    217        1.1       cgd 			 * This is an incremental dump tape.
    218        1.1       cgd 			 */
    219        1.1       cgd 			vprintf(stdout, "Begin incremental restore\n");
    220        1.1       cgd 			initsymtable(symtbl);
    221        1.1       cgd 			extractdirs(1);
    222        1.1       cgd 			removeoldleaves();
    223        1.1       cgd 			vprintf(stdout, "Calculate node updates.\n");
    224        1.1       cgd 			treescan(".", ROOTINO, nodeupdates);
    225        1.1       cgd 			findunreflinks();
    226        1.1       cgd 			removeoldnodes();
    227        1.1       cgd 		} else {
    228        1.1       cgd 			/*
    229        1.1       cgd 			 * This is a level zero dump tape.
    230        1.1       cgd 			 */
    231        1.1       cgd 			vprintf(stdout, "Begin level 0 restore\n");
    232        1.1       cgd 			initsymtable((char *)0);
    233        1.1       cgd 			extractdirs(1);
    234        1.1       cgd 			vprintf(stdout, "Calculate extraction list.\n");
    235        1.1       cgd 			treescan(".", ROOTINO, nodeupdates);
    236        1.1       cgd 		}
    237        1.1       cgd 		createleaves(symtbl);
    238        1.1       cgd 		createlinks();
    239        1.3       cgd 		setdirmodes(FORCE);
    240        1.1       cgd 		checkrestore();
    241        1.1       cgd 		if (dflag) {
    242        1.1       cgd 			vprintf(stdout, "Verify the directory structure\n");
    243        1.1       cgd 			treescan(".", ROOTINO, verifyfile);
    244        1.1       cgd 		}
    245        1.1       cgd 		dumpsymtable(symtbl, (long)1);
    246        1.3       cgd 		break;
    247        1.1       cgd 	/*
    248        1.1       cgd 	 * Resume an incremental file system restoration.
    249        1.1       cgd 	 */
    250        1.1       cgd 	case 'R':
    251        1.1       cgd 		initsymtable(symtbl);
    252        1.1       cgd 		skipmaps();
    253        1.1       cgd 		skipdirs();
    254        1.1       cgd 		createleaves(symtbl);
    255        1.1       cgd 		createlinks();
    256        1.3       cgd 		setdirmodes(FORCE);
    257        1.1       cgd 		checkrestore();
    258        1.1       cgd 		dumpsymtable(symtbl, (long)1);
    259        1.3       cgd 		break;
    260        1.1       cgd 	/*
    261        1.1       cgd 	 * List contents of tape.
    262        1.1       cgd 	 */
    263        1.1       cgd 	case 't':
    264        1.1       cgd 		setup();
    265        1.1       cgd 		extractdirs(0);
    266        1.1       cgd 		initsymtable((char *)0);
    267        1.1       cgd 		while (argc--) {
    268        1.1       cgd 			canon(*argv++, name);
    269        1.1       cgd 			ino = dirlookup(name);
    270        1.1       cgd 			if (ino == 0)
    271        1.1       cgd 				continue;
    272        1.1       cgd 			treescan(name, ino, listfile);
    273        1.1       cgd 		}
    274        1.3       cgd 		break;
    275        1.1       cgd 	/*
    276        1.1       cgd 	 * Batch extraction of tape contents.
    277        1.1       cgd 	 */
    278        1.1       cgd 	case 'x':
    279        1.1       cgd 		setup();
    280        1.1       cgd 		extractdirs(1);
    281        1.1       cgd 		initsymtable((char *)0);
    282        1.1       cgd 		while (argc--) {
    283        1.1       cgd 			canon(*argv++, name);
    284        1.1       cgd 			ino = dirlookup(name);
    285        1.1       cgd 			if (ino == 0)
    286        1.1       cgd 				continue;
    287       1.29     fredb 			if (ino == ROOTINO)
    288       1.29     fredb 				dotflag = 1;
    289        1.1       cgd 			if (mflag)
    290        1.1       cgd 				pathcheck(name);
    291        1.1       cgd 			treescan(name, ino, addfile);
    292        1.1       cgd 		}
    293        1.1       cgd 		createfiles();
    294        1.1       cgd 		createlinks();
    295        1.3       cgd 		setdirmodes(0);
    296        1.1       cgd 		if (dflag)
    297        1.1       cgd 			checkrestore();
    298        1.3       cgd 		break;
    299        1.1       cgd 	}
    300        1.7   mycroft 	exit(0);
    301        1.3       cgd 	/* NOTREACHED */
    302        1.1       cgd }
    303        1.3       cgd 
    304        1.4   mycroft static void
    305       1.30   xtraeme usage(void)
    306        1.3       cgd {
    307       1.21       cgd 	const char *progname = getprogname();
    308        1.8   mycroft 
    309       1.14     lukem 	(void)fprintf(stderr,
    310       1.26       wiz 	    "usage: %s -i [-cdhmvyN] [-b bsize] [-D algorithm] "
    311       1.28       wiz 	    "[-f file] [-M mtreefile] [-s fileno]\n", progname);
    312       1.14     lukem 	(void)fprintf(stderr,
    313       1.26       wiz 	    "       %s -R [-cdvyN] [-b bsize] [-D algorithm] [-f file] "
    314       1.28       wiz 	    "[-M mtreefile] [-s fileno]\n", progname);
    315       1.14     lukem 	(void)fprintf(stderr,
    316       1.26       wiz 	    "       %s -r [-cdvyN] [-b bsize] [-D algorithm] [-f file] "
    317       1.28       wiz 	    "[-M mtreefile] [-s fileno]\n", progname);
    318       1.14     lukem 	(void)fprintf(stderr,
    319       1.26       wiz 	    "       %s -t [-cdhvy] [-b bsize] [-D algorithm] [-f file]\n"
    320       1.26       wiz 	    "           [-s fileno] [file ...]\n", progname);
    321       1.14     lukem 	(void)fprintf(stderr,
    322       1.26       wiz 	    "       %s -x [-cdhmvyN] [-b bsize] [-D algorithm] [-f file]\n"
    323       1.28       wiz 	    "           [-M mtreefile] [-s fileno] [file ...]\n", progname);
    324        1.7   mycroft 	exit(1);
    325        1.3       cgd }
    326        1.3       cgd 
    327        1.3       cgd /*
    328        1.3       cgd  * obsolete --
    329        1.3       cgd  *	Change set of key letters and ordered arguments into something
    330        1.3       cgd  *	getopt(3) will like.
    331        1.3       cgd  */
    332        1.3       cgd static void
    333       1.30   xtraeme obsolete(int *argcp, char **argvp[])
    334        1.3       cgd {
    335        1.3       cgd 	int argc, flags;
    336        1.3       cgd 	char *ap, **argv, *flagsp, **nargv, *p;
    337        1.3       cgd 
    338        1.3       cgd 	/* Setup. */
    339        1.3       cgd 	argv = *argvp;
    340        1.3       cgd 	argc = *argcp;
    341        1.3       cgd 
    342        1.3       cgd 	/* Return if no arguments or first argument has leading dash. */
    343        1.3       cgd 	ap = argv[1];
    344        1.3       cgd 	if (argc == 1 || *ap == '-')
    345        1.3       cgd 		return;
    346        1.3       cgd 
    347        1.3       cgd 	/* Allocate space for new arguments. */
    348        1.3       cgd 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
    349        1.3       cgd 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
    350       1.19  drochner 		err(1, NULL);
    351        1.3       cgd 
    352        1.3       cgd 	*nargv++ = *argv;
    353        1.3       cgd 	argv += 2;
    354        1.3       cgd 
    355        1.3       cgd 	for (flags = 0; *ap; ++ap) {
    356        1.6   mycroft 		switch (*ap) {
    357        1.3       cgd 		case 'b':
    358        1.3       cgd 		case 'f':
    359        1.3       cgd 		case 's':
    360        1.6   mycroft 			if (*argv == NULL) {
    361        1.6   mycroft 				warnx("option requires an argument -- %c", *ap);
    362        1.6   mycroft 				usage();
    363        1.6   mycroft 			}
    364        1.3       cgd 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
    365       1.19  drochner 				err(1, NULL);
    366        1.3       cgd 			nargv[0][0] = '-';
    367        1.3       cgd 			nargv[0][1] = *ap;
    368        1.3       cgd 			(void)strcpy(&nargv[0][2], *argv);
    369        1.6   mycroft 			++argv;
    370        1.3       cgd 			++nargv;
    371        1.3       cgd 			break;
    372        1.3       cgd 		default:
    373        1.3       cgd 			if (!flags) {
    374        1.3       cgd 				*p++ = '-';
    375        1.3       cgd 				flags = 1;
    376        1.3       cgd 			}
    377        1.3       cgd 			*p++ = *ap;
    378        1.3       cgd 			break;
    379        1.3       cgd 		}
    380        1.3       cgd 	}
    381        1.3       cgd 
    382        1.3       cgd 	/* Terminate flags. */
    383        1.3       cgd 	if (flags) {
    384        1.3       cgd 		*p = '\0';
    385        1.3       cgd 		*nargv++ = flagsp;
    386       1.32  christos 	} else {
    387       1.32  christos 		free(flagsp);
    388        1.3       cgd 	}
    389        1.3       cgd 
    390        1.3       cgd 	/* Copy remaining arguments. */
    391       1.14     lukem 	while ((*nargv++ = *argv++) != NULL)
    392       1.14     lukem 		;
    393        1.6   mycroft 
    394        1.6   mycroft 	/* Update argument count. */
    395        1.6   mycroft 	*argcp = nargv - *argvp - 1;
    396        1.3       cgd }
    397