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