xref: /freebsd/sbin/restore/main.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/4/95";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 
48 #include <ufs/ufs/dinode.h>
49 #include <protocols/dumprestore.h>
50 
51 #include <err.h>
52 #include <limits.h>
53 #include <locale.h>
54 #include <paths.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include "restore.h"
61 #include "extern.h"
62 
63 int	bflag = 0, cvtflag = 0, dflag = 0, Dflag = 0, vflag = 0, yflag = 0;
64 int	hflag = 1, mflag = 1, Nflag = 0;
65 int	uflag = 0;
66 int	pipecmd = 0;
67 char	command = '\0';
68 long	dumpnum = 1;
69 long	volno = 0;
70 long	ntrec;
71 char	*dumpmap;
72 char	*usedinomap;
73 ino_t	maxino;
74 time_t	dumptime;
75 time_t	dumpdate;
76 FILE 	*terminal;
77 
78 static void obsolete(int *, char **[]);
79 static void usage(void) __dead2;
80 
81 int
82 main(int argc, 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 	(void)setlocale(LC_ALL, "");
97 
98 	inputdev = NULL;
99 	obsolete(&argc, &argv);
100 	while ((ch = getopt(argc, argv, "b:dDf:himNP:Rrs:tuvxy")) != -1)
101 		switch(ch) {
102 		case 'b':
103 			/* Change default tape blocksize. */
104 			bflag = 1;
105 			ntrec = strtol(optarg, &p, 10);
106 			if (*p)
107 				errx(1, "illegal blocksize -- %s", optarg);
108 			if (ntrec <= 0)
109 				errx(1, "block size must be greater than 0");
110 			break;
111 		case 'd':
112 			dflag = 1;
113 			break;
114 		case 'D':
115 			Dflag = 1;
116 			break;
117 		case 'f':
118 			if (pipecmd)
119 				errx(1,
120 				    "-P and -f options are mutually exclusive");
121 			inputdev = optarg;
122 			break;
123 		case 'P':
124 			if (!pipecmd && inputdev)
125 				errx(1,
126 				    "-P and -f options are mutually exclusive");
127 			inputdev = optarg;
128 			pipecmd = 1;
129 			break;
130 		case 'h':
131 			hflag = 0;
132 			break;
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 	if (inputdev == NULL && (inputdev = getenv("TAPE")) == NULL)
183 		inputdev = _PATH_DEFTAPE;
184 	setinput(inputdev, pipecmd);
185 
186 	if (argc == 0) {
187 		argc = 1;
188 		*--argv = ".";
189 	}
190 
191 	switch (command) {
192 	/*
193 	 * Interactive mode.
194 	 */
195 	case 'i':
196 		setup();
197 		extractdirs(1);
198 		initsymtable(NULL);
199 		runcmdshell();
200 		break;
201 	/*
202 	 * Incremental restoration of a file system.
203 	 */
204 	case 'r':
205 		setup();
206 		if (dumptime > 0) {
207 			/*
208 			 * This is an incremental dump tape.
209 			 */
210 			vprintf(stdout, "Begin incremental restore\n");
211 			initsymtable(symtbl);
212 			extractdirs(1);
213 			removeoldleaves();
214 			vprintf(stdout, "Calculate node updates.\n");
215 			treescan(".", UFS_ROOTINO, nodeupdates);
216 			findunreflinks();
217 			removeoldnodes();
218 		} else {
219 			/*
220 			 * This is a level zero dump tape.
221 			 */
222 			vprintf(stdout, "Begin level 0 restore\n");
223 			initsymtable((char *)0);
224 			extractdirs(1);
225 			vprintf(stdout, "Calculate extraction list.\n");
226 			treescan(".", UFS_ROOTINO, nodeupdates);
227 		}
228 		createleaves(symtbl);
229 		createlinks();
230 		setdirmodes(FORCE);
231 		checkrestore();
232 		if (dflag) {
233 			vprintf(stdout, "Verify the directory structure\n");
234 			treescan(".", UFS_ROOTINO, verifyfile);
235 		}
236 		dumpsymtable(symtbl, (long)1);
237 		break;
238 	/*
239 	 * Resume an incremental file system restoration.
240 	 */
241 	case 'R':
242 		initsymtable(symtbl);
243 		skipmaps();
244 		skipdirs();
245 		createleaves(symtbl);
246 		createlinks();
247 		setdirmodes(FORCE);
248 		checkrestore();
249 		dumpsymtable(symtbl, (long)1);
250 		break;
251 	/*
252 	 * List contents of tape.
253 	 */
254 	case 't':
255 		setup();
256 		extractdirs(0);
257 		initsymtable((char *)0);
258 		while (argc--) {
259 			canon(*argv++, name, sizeof(name));
260 			ino = dirlookup(name);
261 			if (ino == 0)
262 				continue;
263 			treescan(name, ino, listfile);
264 		}
265 		break;
266 	/*
267 	 * Batch extraction of tape contents.
268 	 */
269 	case 'x':
270 		setup();
271 		extractdirs(1);
272 		initsymtable((char *)0);
273 		while (argc--) {
274 			canon(*argv++, name, sizeof(name));
275 			ino = dirlookup(name);
276 			if (ino == 0)
277 				continue;
278 			if (mflag)
279 				pathcheck(name);
280 			treescan(name, ino, addfile);
281 		}
282 		createfiles();
283 		createlinks();
284 		setdirmodes(0);
285 		if (dflag)
286 			checkrestore();
287 		break;
288 	}
289 	done(0);
290 	/* NOTREACHED */
291 }
292 
293 static void
294 usage()
295 {
296 	const char *const common =
297 	    "[-b blocksize] [-f file | -P pipecommand] [-s fileno]";
298 	const char *const fileell = "[file ...]";
299 
300 	(void)fprintf(stderr, "usage:\t%s %s\n\t%s %s\n\t%s %s\n"
301 	    "\t%s %s %s\n\t%s %s %s\n",
302 	    "restore -i [-dhmNuvy]", common,
303 	    "restore -R [-dNuvy]", common,
304 	    "restore -r [-dNuvy]", common,
305 	    "restore -t [-dhNuvy]", common, fileell,
306 	    "restore -x [-dhmNuvy]", common, fileell);
307 	done(1);
308 }
309 
310 /*
311  * obsolete --
312  *	Change set of key letters and ordered arguments into something
313  *	getopt(3) will like.
314  */
315 static void
316 obsolete(int *argcp, char **argvp[])
317 {
318 	int argc, flags;
319 	char *ap, **argv, *flagsp, **nargv, *p;
320 
321 	/* Setup. */
322 	argv = *argvp;
323 	argc = *argcp;
324 
325 	/* Return if no arguments or first argument has leading dash. */
326 	ap = argv[1];
327 	if (argc == 1 || *ap == '-')
328 		return;
329 
330 	/* Allocate space for new arguments. */
331 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
332 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
333 		err(1, NULL);
334 
335 	*nargv++ = *argv;
336 	argv += 2, argc -= 2;
337 
338 	for (flags = 0; *ap; ++ap) {
339 		switch (*ap) {
340 		case 'b':
341 		case 'f':
342 		case 's':
343 			if (*argv == NULL) {
344 				warnx("option requires an argument -- %c", *ap);
345 				usage();
346 			}
347 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
348 				err(1, NULL);
349 			nargv[0][0] = '-';
350 			nargv[0][1] = *ap;
351 			(void)strcpy(&nargv[0][2], *argv);
352 			++argv;
353 			++nargv;
354 			break;
355 		default:
356 			if (!flags) {
357 				*p++ = '-';
358 				flags = 1;
359 			}
360 			*p++ = *ap;
361 			break;
362 		}
363 	}
364 
365 	/* Terminate flags. */
366 	if (flags) {
367 		*p = '\0';
368 		*nargv++ = flagsp;
369 	} else
370 		free(flagsp);
371 
372 	/* Copy remaining arguments. */
373 	while ((*nargv++ = *argv++));
374 
375 	/* Update argument count. */
376 	*argcp = nargv - *argvp - 1;
377 }
378