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