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