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