main.c revision 1.22 1 /* $NetBSD: main.c,v 1.22 2002/11/18 04:28:03 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.22 2002/11/18 04:28:03 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 int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
70 int hflag = 1, mflag = 1, Nflag = 0;
71 char command = '\0';
72 int32_t dumpnum = 1;
73 int32_t volno = 0;
74 int32_t ntrec;
75 char *dumpmap;
76 char *usedinomap;
77 ino_t maxino;
78 time_t dumptime;
79 time_t dumpdate;
80 size_t pagesize;
81 FILE *terminal;
82 char *tmpdir;
83
84 int main __P((int, char *[]));
85 static void obsolete __P((int *, char **[]));
86 static void usage __P((void));
87
88 int
89 main(argc, argv)
90 int argc;
91 char *argv[];
92 {
93 int ch;
94 ino_t ino;
95 char *inputdev;
96 char *symtbl = "./restoresymtable";
97 char *p, name[MAXPATHLEN];
98
99 if (argc < 2)
100 usage();
101
102 if ((inputdev = getenv("TAPE")) == NULL)
103 inputdev = _PATH_DEFTAPE;
104 if ((tmpdir = getenv("TMPDIR")) == NULL)
105 tmpdir = _PATH_TMP;
106 obsolete(&argc, &argv);
107 while ((ch = getopt(argc, argv, "b:cdf:himNRrs:tuvxy")) != -1)
108 switch(ch) {
109 case 'b':
110 /* Change default tape blocksize. */
111 bflag = 1;
112 ntrec = strtol(optarg, &p, 10);
113 if (*p)
114 errx(1, "illegal blocksize -- %s", optarg);
115 if (ntrec <= 0)
116 errx(1, "block size must be greater than 0");
117 break;
118 case 'c':
119 cvtflag = 1;
120 break;
121 case 'd':
122 dflag = 1;
123 break;
124 case 'f':
125 inputdev = optarg;
126 break;
127 case 'h':
128 hflag = 0;
129 break;
130 case 'i':
131 case 'R':
132 case 'r':
133 case 't':
134 case 'x':
135 if (command != '\0')
136 errx(1,
137 "%c and %c options are mutually exclusive",
138 ch, command);
139 command = ch;
140 break;
141 case 'm':
142 mflag = 0;
143 break;
144 case 'N':
145 Nflag = 1;
146 break;
147 case 's':
148 /* Dumpnum (skip to) for multifile dump tapes. */
149 dumpnum = strtol(optarg, &p, 10);
150 if (*p)
151 errx(1, "illegal dump number -- %s", optarg);
152 if (dumpnum <= 0)
153 errx(1, "dump number must be greater than 0");
154 break;
155 case 'u':
156 uflag = 1;
157 break;
158 case 'v':
159 vflag = 1;
160 break;
161 case 'y':
162 yflag = 1;
163 break;
164 default:
165 usage();
166 }
167 argc -= optind;
168 argv += optind;
169
170 if (command == '\0')
171 errx(1, "none of i, R, r, t or x options specified");
172
173 if (Nflag || command == 't')
174 uflag = 0;
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 pagesize = sysconf(_SC_PAGESIZE);
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 const char *progname = getprogname();
298
299 (void)fprintf(stderr,
300 "usage: %s -i [-cdhmvyN] [-b blocksize] [-f file] [-s fileno]\n",
301 progname);
302 (void)fprintf(stderr,
303 "\t%s -R [-cdvyN] [-b blocksize] [-f file] [-s fileno]\n",
304 progname);
305 (void)fprintf(stderr,
306 "\t%s -r [-cdvyN] [-b blocksize] [-f file] [-s fileno]\n",
307 progname);
308 (void)fprintf(stderr,
309 "\t%s -t [-cdhvy] [-b blocksize] [-f file] [-s fileno] [file ...]\n",
310 progname);
311 (void)fprintf(stderr,
312 "\t%s -x [-cdhmvyN] [-b blocksize] [-f file] [-s fileno] [file ...]\n",
313 progname);
314 exit(1);
315 }
316
317 /*
318 * obsolete --
319 * Change set of key letters and ordered arguments into something
320 * getopt(3) will like.
321 */
322 static void
323 obsolete(argcp, argvp)
324 int *argcp;
325 char **argvp[];
326 {
327 int argc, flags;
328 char *ap, **argv, *flagsp, **nargv, *p;
329
330 /* Setup. */
331 argv = *argvp;
332 argc = *argcp;
333
334 /* Return if no arguments or first argument has leading dash. */
335 ap = argv[1];
336 if (argc == 1 || *ap == '-')
337 return;
338
339 /* Allocate space for new arguments. */
340 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
341 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
342 err(1, NULL);
343
344 *nargv++ = *argv;
345 argv += 2;
346
347 for (flags = 0; *ap; ++ap) {
348 switch (*ap) {
349 case 'b':
350 case 'f':
351 case 's':
352 if (*argv == NULL) {
353 warnx("option requires an argument -- %c", *ap);
354 usage();
355 }
356 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
357 err(1, NULL);
358 nargv[0][0] = '-';
359 nargv[0][1] = *ap;
360 (void)strcpy(&nargv[0][2], *argv);
361 ++argv;
362 ++nargv;
363 break;
364 default:
365 if (!flags) {
366 *p++ = '-';
367 flags = 1;
368 }
369 *p++ = *ap;
370 break;
371 }
372 }
373
374 /* Terminate flags. */
375 if (flags) {
376 *p = '\0';
377 *nargv++ = flagsp;
378 }
379
380 /* Copy remaining arguments. */
381 while ((*nargv++ = *argv++) != NULL)
382 ;
383
384 /* Update argument count. */
385 *argcp = nargv - *argvp - 1;
386 }
387