main.c revision 1.13 1 /* $NetBSD: main.c,v 1.13 1997/07/01 05:37:51 lukem 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 #ifndef lint
37 static char copyright[] =
38 "@(#) 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.3 (Berkeley) 9/13/94";
45 #else
46 static char rcsid[] = "$NetBSD: main.c,v 1.13 1997/07/01 05:37:51 lukem Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/time.h>
52
53 #include <ufs/ffs/fs.h>
54 #include <ufs/ufs/dinode.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
65 #include "restore.h"
66 #include "extern.h"
67
68 int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
69 int hflag = 1, mflag = 1, Nflag = 0;
70 char command = '\0';
71 long dumpnum = 1;
72 long volno = 0;
73 long ntrec;
74 char *dumpmap;
75 char *usedinomap;
76 ino_t maxino;
77 time_t dumptime;
78 time_t dumpdate;
79 FILE *terminal;
80 char *tmpdir;
81 uid_t uid; /* real uid */
82 uid_t euid; /* effective uid */
83
84 static void obsolete __P((int *, char **[]));
85 static void usage __P((void));
86
87 int
88 main(argc, argv)
89 int argc;
90 char *argv[];
91 {
92 int ch;
93 ino_t ino;
94 char *inputdev;
95 char *symtbl = "./restoresymtable";
96 char *p, name[MAXPATHLEN];
97
98 uid = getuid();
99 euid = geteuid();
100 (void) seteuid(uid);
101
102 if (argc < 2)
103 usage();
104
105 if ((inputdev = getenv("TAPE")) == NULL)
106 inputdev = _PATH_DEFTAPE;
107 if ((tmpdir = getenv("TMPDIR")) == NULL)
108 tmpdir = _PATH_TMP;
109 obsolete(&argc, &argv);
110 while ((ch = getopt(argc, argv, "b:cdf:himNRrs:tvxy")) != EOF)
111 switch(ch) {
112 case 'b':
113 /* Change default tape blocksize. */
114 bflag = 1;
115 ntrec = strtol(optarg, &p, 10);
116 if (*p)
117 errx(1, "illegal blocksize -- %s", optarg);
118 if (ntrec <= 0)
119 errx(1, "block size must be greater than 0");
120 break;
121 case 'c':
122 cvtflag = 1;
123 break;
124 case 'd':
125 dflag = 1;
126 break;
127 case 'f':
128 inputdev = optarg;
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 '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 (signal(SIGINT, onintr) == SIG_IGN)
174 (void) signal(SIGINT, SIG_IGN);
175 if (signal(SIGTERM, onintr) == SIG_IGN)
176 (void) signal(SIGTERM, SIG_IGN);
177 setlinebuf(stderr);
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
294 (void)fprintf(stderr, "usage: restore -i [-chmvy] [-b blocksize] [-f file] [-s fileno]\n");
295 (void)fprintf(stderr, " restore -R [-cvy] [-b blocksize] [-f file] [-s fileno]\n");
296 (void)fprintf(stderr, " restore -r [-cvy] [-b blocksize] [-f file] [-s fileno]\n");
297 (void)fprintf(stderr, " restore -t [-chvy] [-b blocksize] [-f file] [-s fileno] [file ...]\n");
298 (void)fprintf(stderr, " restore -x [-chmvy] [-b blocksize] [-f file] [-s fileno] [file ...]\n");
299 exit(1);
300 }
301
302 /*
303 * obsolete --
304 * Change set of key letters and ordered arguments into something
305 * getopt(3) will like.
306 */
307 static void
308 obsolete(argcp, argvp)
309 int *argcp;
310 char **argvp[];
311 {
312 int argc, flags;
313 char *ap, **argv, *flagsp, **nargv, *p;
314
315 /* Setup. */
316 argv = *argvp;
317 argc = *argcp;
318
319 /* Return if no arguments or first argument has leading dash. */
320 ap = argv[1];
321 if (argc == 1 || *ap == '-')
322 return;
323
324 /* Allocate space for new arguments. */
325 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
326 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
327 err(1, NULL);
328
329 *nargv++ = *argv;
330 argv += 2;
331
332 for (flags = 0; *ap; ++ap) {
333 switch (*ap) {
334 case 'b':
335 case 'f':
336 case 's':
337 if (*argv == NULL) {
338 warnx("option requires an argument -- %c", *ap);
339 usage();
340 }
341 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
342 err(1, NULL);
343 nargv[0][0] = '-';
344 nargv[0][1] = *ap;
345 (void)strcpy(&nargv[0][2], *argv);
346 ++argv;
347 ++nargv;
348 break;
349 default:
350 if (!flags) {
351 *p++ = '-';
352 flags = 1;
353 }
354 *p++ = *ap;
355 break;
356 }
357 }
358
359 /* Terminate flags. */
360 if (flags) {
361 *p = '\0';
362 *nargv++ = flagsp;
363 }
364
365 /* Copy remaining arguments. */
366 while (*nargv++ = *argv++);
367
368 /* Update argument count. */
369 *argcp = nargv - *argvp - 1;
370 }
371