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