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