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