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