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