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