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