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