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