Home | History | Annotate | Line # | Download | only in restore
main.c revision 1.11
      1 /*	$NetBSD: main.c,v 1.11 1996/03/15 22:39:39 scottr 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.11 1996/03/15 22:39:39 scottr 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 uid_t	uid;		/* real uid */
     81 uid_t	euid;		/* effective uid */
     82 
     83 static void obsolete __P((int *, char **[]));
     84 static void usage __P((void));
     85 
     86 int
     87 main(argc, argv)
     88 	int argc;
     89 	char *argv[];
     90 {
     91 	int ch;
     92 	ino_t ino;
     93 	char *inputdev;
     94 	char *symtbl = "./restoresymtable";
     95 	char *p, name[MAXPATHLEN];
     96 
     97 	uid = getuid();
     98 	euid = geteuid();
     99 	(void) seteuid(uid);
    100 
    101 	if (argc < 2)
    102 		usage();
    103 
    104 	if ((inputdev = getenv("TAPE")) == NULL)
    105 		inputdev = _PATH_DEFTAPE;
    106 	obsolete(&argc, &argv);
    107 	while ((ch = getopt(argc, argv, "b:cdf:himNRrs:tvxy")) != EOF)
    108 		switch(ch) {
    109 		case 'b':
    110 			/* Change default tape blocksize. */
    111 			bflag = 1;
    112 			ntrec = strtol(optarg, &p, 10);
    113 			if (*p)
    114 				errx(1, "illegal blocksize -- %s", optarg);
    115 			if (ntrec <= 0)
    116 				errx(1, "block size must be greater than 0");
    117 			break;
    118 		case 'c':
    119 			cvtflag = 1;
    120 			break;
    121 		case 'd':
    122 			dflag = 1;
    123 			break;
    124 		case 'f':
    125 			inputdev = optarg;
    126 			break;
    127 		case 'h':
    128 			hflag = 0;
    129 			break;
    130 		case 'i':
    131 		case 'R':
    132 		case 'r':
    133 		case 't':
    134 		case 'x':
    135 			if (command != '\0')
    136 				errx(1,
    137 				    "%c and %c options are mutually exclusive",
    138 				    ch, command);
    139 			command = ch;
    140 			break;
    141 		case 'm':
    142 			mflag = 0;
    143 			break;
    144 		case 'N':
    145 			Nflag = 1;
    146 			break;
    147 		case 's':
    148 			/* Dumpnum (skip to) for multifile dump tapes. */
    149 			dumpnum = strtol(optarg, &p, 10);
    150 			if (*p)
    151 				errx(1, "illegal dump number -- %s", optarg);
    152 			if (dumpnum <= 0)
    153 				errx(1, "dump number must be greater than 0");
    154 			break;
    155 		case 'v':
    156 			vflag = 1;
    157 			break;
    158 		case 'y':
    159 			yflag = 1;
    160 			break;
    161 		default:
    162 			usage();
    163 		}
    164 	argc -= optind;
    165 	argv += optind;
    166 
    167 	if (command == '\0')
    168 		errx(1, "none of i, R, r, t or x options specified");
    169 
    170 	if (signal(SIGINT, onintr) == SIG_IGN)
    171 		(void) signal(SIGINT, SIG_IGN);
    172 	if (signal(SIGTERM, onintr) == SIG_IGN)
    173 		(void) signal(SIGTERM, SIG_IGN);
    174 	setlinebuf(stderr);
    175 
    176 	atexit(cleanup);
    177 
    178 	setinput(inputdev);
    179 
    180 	if (argc == 0) {
    181 		argc = 1;
    182 		*--argv = ".";
    183 	}
    184 
    185 	switch (command) {
    186 	/*
    187 	 * Interactive mode.
    188 	 */
    189 	case 'i':
    190 		setup();
    191 		extractdirs(1);
    192 		initsymtable(NULL);
    193 		runcmdshell();
    194 		break;
    195 	/*
    196 	 * Incremental restoration of a file system.
    197 	 */
    198 	case 'r':
    199 		setup();
    200 		if (dumptime > 0) {
    201 			/*
    202 			 * This is an incremental dump tape.
    203 			 */
    204 			vprintf(stdout, "Begin incremental restore\n");
    205 			initsymtable(symtbl);
    206 			extractdirs(1);
    207 			removeoldleaves();
    208 			vprintf(stdout, "Calculate node updates.\n");
    209 			treescan(".", ROOTINO, nodeupdates);
    210 			findunreflinks();
    211 			removeoldnodes();
    212 		} else {
    213 			/*
    214 			 * This is a level zero dump tape.
    215 			 */
    216 			vprintf(stdout, "Begin level 0 restore\n");
    217 			initsymtable((char *)0);
    218 			extractdirs(1);
    219 			vprintf(stdout, "Calculate extraction list.\n");
    220 			treescan(".", ROOTINO, nodeupdates);
    221 		}
    222 		createleaves(symtbl);
    223 		createlinks();
    224 		setdirmodes(FORCE);
    225 		checkrestore();
    226 		if (dflag) {
    227 			vprintf(stdout, "Verify the directory structure\n");
    228 			treescan(".", ROOTINO, verifyfile);
    229 		}
    230 		dumpsymtable(symtbl, (long)1);
    231 		break;
    232 	/*
    233 	 * Resume an incremental file system restoration.
    234 	 */
    235 	case 'R':
    236 		initsymtable(symtbl);
    237 		skipmaps();
    238 		skipdirs();
    239 		createleaves(symtbl);
    240 		createlinks();
    241 		setdirmodes(FORCE);
    242 		checkrestore();
    243 		dumpsymtable(symtbl, (long)1);
    244 		break;
    245 	/*
    246 	 * List contents of tape.
    247 	 */
    248 	case 't':
    249 		setup();
    250 		extractdirs(0);
    251 		initsymtable((char *)0);
    252 		while (argc--) {
    253 			canon(*argv++, name);
    254 			ino = dirlookup(name);
    255 			if (ino == 0)
    256 				continue;
    257 			treescan(name, ino, listfile);
    258 		}
    259 		break;
    260 	/*
    261 	 * Batch extraction of tape contents.
    262 	 */
    263 	case 'x':
    264 		setup();
    265 		extractdirs(1);
    266 		initsymtable((char *)0);
    267 		while (argc--) {
    268 			canon(*argv++, name);
    269 			ino = dirlookup(name);
    270 			if (ino == 0)
    271 				continue;
    272 			if (mflag)
    273 				pathcheck(name);
    274 			treescan(name, ino, addfile);
    275 		}
    276 		createfiles();
    277 		createlinks();
    278 		setdirmodes(0);
    279 		if (dflag)
    280 			checkrestore();
    281 		break;
    282 	}
    283 	exit(0);
    284 	/* NOTREACHED */
    285 }
    286 
    287 static void
    288 usage()
    289 {
    290 
    291 	(void)fprintf(stderr, "usage: restore -i [-chmvy] [-b blocksize] [-f file] [-s fileno]\n");
    292 	(void)fprintf(stderr, "       restore -R [-cvy] [-b blocksize] [-f file] [-s fileno]\n");
    293 	(void)fprintf(stderr, "       restore -r [-cvy] [-b blocksize] [-f file] [-s fileno]\n");
    294 	(void)fprintf(stderr, "       restore -t [-chvy] [-b blocksize] [-f file] [-s fileno] [file ...]\n");
    295 	(void)fprintf(stderr, "       restore -x [-chmvy] [-b blocksize] [-f file] [-s fileno] [file ...]\n");
    296 	exit(1);
    297 }
    298 
    299 /*
    300  * obsolete --
    301  *	Change set of key letters and ordered arguments into something
    302  *	getopt(3) will like.
    303  */
    304 static void
    305 obsolete(argcp, argvp)
    306 	int *argcp;
    307 	char **argvp[];
    308 {
    309 	int argc, flags;
    310 	char *ap, **argv, *flagsp, **nargv, *p;
    311 
    312 	/* Setup. */
    313 	argv = *argvp;
    314 	argc = *argcp;
    315 
    316 	/* Return if no arguments or first argument has leading dash. */
    317 	ap = argv[1];
    318 	if (argc == 1 || *ap == '-')
    319 		return;
    320 
    321 	/* Allocate space for new arguments. */
    322 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
    323 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
    324 		err(1, NULL);
    325 
    326 	*nargv++ = *argv;
    327 	argv += 2;
    328 
    329 	for (flags = 0; *ap; ++ap) {
    330 		switch (*ap) {
    331 		case 'b':
    332 		case 'f':
    333 		case 's':
    334 			if (*argv == NULL) {
    335 				warnx("option requires an argument -- %c", *ap);
    336 				usage();
    337 			}
    338 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
    339 				err(1, NULL);
    340 			nargv[0][0] = '-';
    341 			nargv[0][1] = *ap;
    342 			(void)strcpy(&nargv[0][2], *argv);
    343 			++argv;
    344 			++nargv;
    345 			break;
    346 		default:
    347 			if (!flags) {
    348 				*p++ = '-';
    349 				flags = 1;
    350 			}
    351 			*p++ = *ap;
    352 			break;
    353 		}
    354 	}
    355 
    356 	/* Terminate flags. */
    357 	if (flags) {
    358 		*p = '\0';
    359 		*nargv++ = flagsp;
    360 	}
    361 
    362 	/* Copy remaining arguments. */
    363 	while (*nargv++ = *argv++);
    364 
    365 	/* Update argument count. */
    366 	*argcp = nargv - *argvp - 1;
    367 }
    368