xref: /freebsd/sbin/restore/main.c (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
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 <paths.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, Nflag = 0;
67 int	uflag = 0;
68 int	dokerberos = 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 	if ((inputdev = getenv("TAPE")) == NULL)
99 		inputdev = _PATH_DEFTAPE;
100 	obsolete(&argc, &argv);
101 #ifdef KERBEROS
102 #define	optlist "b:df:hikmNRrs:tuvxy"
103 #else
104 #define	optlist "b:df:himNRrs:tuvxy"
105 #endif
106 	while ((ch = getopt(argc, argv, optlist)) != -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 'd':
118 			dflag = 1;
119 			break;
120 		case 'f':
121 			inputdev = optarg;
122 			break;
123 		case 'h':
124 			hflag = 0;
125 			break;
126 #ifdef KERBEROS
127 		case 'k':
128 			dokerberos = 1;
129 			break;
130 #endif
131 		case 'i':
132 		case 'R':
133 		case 'r':
134 		case 't':
135 		case 'x':
136 			if (command != '\0')
137 				errx(1,
138 				    "%c and %c options are mutually exclusive",
139 				    ch, command);
140 			command = ch;
141 			break;
142 		case 'm':
143 			mflag = 0;
144 			break;
145 		case 'N':
146 			Nflag = 1;
147 			break;
148 		case 's':
149 			/* Dumpnum (skip to) for multifile dump tapes. */
150 			dumpnum = strtol(optarg, &p, 10);
151 			if (*p)
152 				errx(1, "illegal dump number -- %s", optarg);
153 			if (dumpnum <= 0)
154 				errx(1, "dump number must be greater than 0");
155 			break;
156 		case 'u':
157 			uflag = 1;
158 			break;
159 		case 'v':
160 			vflag = 1;
161 			break;
162 		case 'y':
163 			yflag = 1;
164 			break;
165 		default:
166 			usage();
167 		}
168 	argc -= optind;
169 	argv += optind;
170 
171 	if (command == '\0')
172 		errx(1, "none of i, R, r, t or x options specified");
173 
174 	if (signal(SIGINT, onintr) == SIG_IGN)
175 		(void) signal(SIGINT, SIG_IGN);
176 	if (signal(SIGTERM, onintr) == SIG_IGN)
177 		(void) signal(SIGTERM, SIG_IGN);
178 	setlinebuf(stderr);
179 
180 	setinput(inputdev);
181 
182 	if (argc == 0) {
183 		argc = 1;
184 		*--argv = ".";
185 	}
186 
187 	switch (command) {
188 	/*
189 	 * Interactive mode.
190 	 */
191 	case 'i':
192 		setup();
193 		extractdirs(1);
194 		initsymtable(NULL);
195 		runcmdshell();
196 		break;
197 	/*
198 	 * Incremental restoration of a file system.
199 	 */
200 	case 'r':
201 		setup();
202 		if (dumptime > 0) {
203 			/*
204 			 * This is an incremental dump tape.
205 			 */
206 			vprintf(stdout, "Begin incremental restore\n");
207 			initsymtable(symtbl);
208 			extractdirs(1);
209 			removeoldleaves();
210 			vprintf(stdout, "Calculate node updates.\n");
211 			treescan(".", ROOTINO, nodeupdates);
212 			findunreflinks();
213 			removeoldnodes();
214 		} else {
215 			/*
216 			 * This is a level zero dump tape.
217 			 */
218 			vprintf(stdout, "Begin level 0 restore\n");
219 			initsymtable((char *)0);
220 			extractdirs(1);
221 			vprintf(stdout, "Calculate extraction list.\n");
222 			treescan(".", ROOTINO, nodeupdates);
223 		}
224 		createleaves(symtbl);
225 		createlinks();
226 		setdirmodes(FORCE);
227 		checkrestore();
228 		if (dflag) {
229 			vprintf(stdout, "Verify the directory structure\n");
230 			treescan(".", ROOTINO, verifyfile);
231 		}
232 		dumpsymtable(symtbl, (long)1);
233 		break;
234 	/*
235 	 * Resume an incremental file system restoration.
236 	 */
237 	case 'R':
238 		initsymtable(symtbl);
239 		skipmaps();
240 		skipdirs();
241 		createleaves(symtbl);
242 		createlinks();
243 		setdirmodes(FORCE);
244 		checkrestore();
245 		dumpsymtable(symtbl, (long)1);
246 		break;
247 	/*
248 	 * List contents of tape.
249 	 */
250 	case 't':
251 		setup();
252 		extractdirs(0);
253 		initsymtable((char *)0);
254 		while (argc--) {
255 			canon(*argv++, name, sizeof(name));
256 			ino = dirlookup(name);
257 			if (ino == 0)
258 				continue;
259 			treescan(name, ino, listfile);
260 		}
261 		break;
262 	/*
263 	 * Batch extraction of tape contents.
264 	 */
265 	case 'x':
266 		setup();
267 		extractdirs(1);
268 		initsymtable((char *)0);
269 		while (argc--) {
270 			canon(*argv++, name, sizeof(name));
271 			ino = dirlookup(name);
272 			if (ino == 0)
273 				continue;
274 			if (mflag)
275 				pathcheck(name);
276 			treescan(name, ino, addfile);
277 		}
278 		createfiles();
279 		createlinks();
280 		setdirmodes(0);
281 		if (dflag)
282 			checkrestore();
283 		break;
284 	}
285 	done(0);
286 	/* NOTREACHED */
287 }
288 
289 static void
290 usage()
291 {
292 	(void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n",
293 	  "restore -i [-cdhkmNuvy] [-b blocksize] [-f file] [-s fileno]",
294 	  "restore -r [-cdkNuvy] [-b blocksize] [-f file] [-s fileno]",
295 	  "restore -R [-cdkNuvy] [-b blocksize] [-f file] [-s fileno]",
296 	  "restore -x [-cdhkmNuvy] [-b blocksize] [-f file] [-s fileno] [file ...]",
297 	  "restore -t [-cdhkNuvy] [-b blocksize] [-f file] [-s fileno] [file ...]");
298 	done(1);
299 }
300 
301 /*
302  * obsolete --
303  *	Change set of key letters and ordered arguments into something
304  *	getopt(3) will like.
305  */
306 static void
307 obsolete(int *argcp, char **argvp[])
308 {
309 	int argc, flags;
310 	char *ap, **argv, *flagsp, **nargv, *p;
311 
312 	/* Setup. */
313 	argv = *argvp;
314 	argc = *argcp;
315 
316 	/* Return if no arguments or first argument has leading dash. */
317 	ap = argv[1];
318 	if (argc == 1 || *ap == '-')
319 		return;
320 
321 	/* Allocate space for new arguments. */
322 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
323 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
324 		err(1, NULL);
325 
326 	*nargv++ = *argv;
327 	argv += 2, argc -= 2;
328 
329 	for (flags = 0; *ap; ++ap) {
330 		switch (*ap) {
331 		case 'b':
332 		case 'f':
333 		case 's':
334 			if (*argv == NULL) {
335 				warnx("option requires an argument -- %c", *ap);
336 				usage();
337 			}
338 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
339 				err(1, NULL);
340 			nargv[0][0] = '-';
341 			nargv[0][1] = *ap;
342 			(void)strcpy(&nargv[0][2], *argv);
343 			++argv;
344 			++nargv;
345 			break;
346 		default:
347 			if (!flags) {
348 				*p++ = '-';
349 				flags = 1;
350 			}
351 			*p++ = *ap;
352 			break;
353 		}
354 	}
355 
356 	/* Terminate flags. */
357 	if (flags) {
358 		*p = '\0';
359 		*nargv++ = flagsp;
360 	}
361 
362 	/* Copy remaining arguments. */
363 	while ((*nargv++ = *argv++));
364 
365 	/* Update argument count. */
366 	*argcp = nargv - *argvp - 1;
367 }
368