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