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