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