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