]> git.wh0rd.org - dump.git/blob - restore/main.c
Added '-F script' option to dump.
[dump.git] / restore / main.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999-2000
6 */
7
8 /*
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifndef lint
42 static const char rcsid[] =
43 "$Id: main.c,v 1.9 2000/03/02 11:34:51 stelian Exp $";
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <errno.h>
49
50 #ifdef __linux__
51 #include <linux/ext2_fs.h>
52 #include <bsdcompat.h>
53 #include <signal.h>
54 #include <string.h>
55 #else /* __linux__ */
56 #include <ufs/ufs/dinode.h>
57 #endif /* __linux__ */
58 #include <protocols/dumprestore.h>
59
60 #include <compaterr.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <unistd.h>
64
65 #ifdef __linux__
66 #include <ext2fs/ext2fs.h>
67 #include <getopt.h>
68 #endif
69
70 #include "pathnames.h"
71 #include "restore.h"
72 #include "extern.h"
73
74 int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
75 int hflag = 1, mflag = 1, Nflag = 0;
76 int uflag = 0;
77 int dokerberos = 0;
78 char command = '\0';
79 long dumpnum = 1;
80 long volno = 0;
81 long ntrec;
82 char *dumpmap;
83 char *usedinomap;
84 ino_t maxino;
85 time_t dumptime;
86 time_t dumpdate;
87 FILE *terminal;
88 char *tmpdir;
89 int compare_ignore_not_found;
90 char filesys[NAMELEN];
91
92 #ifdef __linux__
93 char *__progname;
94 #endif
95
96 static void obsolete __P((int *, char **[]));
97 static void usage __P((void));
98
99 int
100 main(int argc, char *argv[])
101 {
102 int ch;
103 ino_t ino;
104 char *inputdev = _PATH_DEFTAPE;
105 char *symtbl = "./restoresymtable";
106 char *p, name[MAXPATHLEN];
107
108 /* Temp files should *not* be readable. We set permissions later. */
109 (void) umask(077);
110 filesys[0] = '\0';
111 #ifdef __linux__
112 __progname = argv[0];
113 #endif
114
115 if (argc < 2)
116 usage();
117
118 if ((inputdev = getenv("TAPE")) == NULL)
119 inputdev = _PATH_DEFTAPE;
120 if ((tmpdir = getenv("TMPDIR")) == NULL)
121 tmpdir = _PATH_TMP;
122 if ((tmpdir = strdup(tmpdir)) == NULL)
123 err(1, "malloc tmpdir");
124 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
125 ;
126 obsolete(&argc, &argv);
127 #ifdef KERBEROS
128 #define optlist "b:CcdD:f:hikmMNRrs:tT:uvxy"
129 #else
130 #define optlist "b:CcdD:f:himMNRrs:tT:uvxy"
131 #endif
132 while ((ch = getopt(argc, argv, optlist)) != -1)
133 switch(ch) {
134 case 'b':
135 /* Change default tape blocksize. */
136 bflag = 1;
137 ntrec = strtol(optarg, &p, 10);
138 if (*p)
139 errx(1, "illegal blocksize -- %s", optarg);
140 if (ntrec <= 0)
141 errx(1, "block size must be greater than 0");
142 break;
143 case 'c':
144 cvtflag = 1;
145 break;
146 case 'D':
147 strncpy(filesys, optarg, NAMELEN);
148 filesys[NAMELEN - 1] = '\0';
149 break;
150 case 'T':
151 tmpdir = optarg;
152 break;
153 case 'd':
154 dflag = 1;
155 break;
156 case 'f':
157 inputdev = optarg;
158 break;
159 case 'h':
160 hflag = 0;
161 break;
162 #ifdef KERBEROS
163 case 'k':
164 dokerberos = 1;
165 break;
166 #endif
167 case 'C':
168 case 'i':
169 case 'R':
170 case 'r':
171 case 't':
172 case 'x':
173 if (command != '\0')
174 errx(1,
175 "%c and %c options are mutually exclusive",
176 ch, command);
177 command = ch;
178 break;
179 case 'm':
180 mflag = 0;
181 break;
182 case 'M':
183 Mflag = 1;
184 break;
185 case 'N':
186 Nflag = 1;
187 break;
188 case 's':
189 /* Dumpnum (skip to) for multifile dump tapes. */
190 dumpnum = strtol(optarg, &p, 10);
191 if (*p)
192 errx(1, "illegal dump number -- %s", optarg);
193 if (dumpnum <= 0)
194 errx(1, "dump number must be greater than 0");
195 break;
196 case 'u':
197 uflag = 1;
198 break;
199 case 'v':
200 vflag = 1;
201 break;
202 case 'y':
203 yflag = 1;
204 break;
205 default:
206 usage();
207 }
208 argc -= optind;
209 argv += optind;
210
211 if (command == '\0')
212 errx(1, "none of C, i, R, r, t or x options specified");
213
214 if (signal(SIGINT, onintr) == SIG_IGN)
215 (void) signal(SIGINT, SIG_IGN);
216 if (signal(SIGTERM, onintr) == SIG_IGN)
217 (void) signal(SIGTERM, SIG_IGN);
218 setlinebuf(stderr);
219
220 atexit(cleanup);
221
222 setinput(inputdev);
223
224 if (argc == 0) {
225 argc = 1;
226 *--argv = ".";
227 }
228
229 switch (command) {
230 /*
231 * Compare contents of tape.
232 */
233 case 'C': {
234 struct stat stbuf;
235
236 Vprintf(stdout, "Begin compare restore\n");
237 compare_ignore_not_found = 0;
238 setup();
239 printf("filesys = %s\n", filesys);
240 if (stat(filesys, &stbuf) < 0)
241 err(1, "cannot stat directory %s", filesys);
242 if (chdir(filesys) < 0)
243 err(1, "cannot cd to %s", filesys);
244 compare_ignore_not_found = dumptime > 0;
245 initsymtable((char *)0);
246 extractdirs(0);
247 treescan(".", ROOTINO, nodeupdates);
248 compareleaves();
249 checkrestore();
250 break;
251 }
252
253 /*
254 * Interactive mode.
255 */
256 case 'i':
257 setup();
258 extractdirs(1);
259 initsymtable(NULL);
260 runcmdshell();
261 break;
262 /*
263 * Incremental restoration of a file system.
264 */
265 case 'r':
266 setup();
267 if (dumptime > 0) {
268 /*
269 * This is an incremental dump tape.
270 */
271 Vprintf(stdout, "Begin incremental restore\n");
272 initsymtable(symtbl);
273 extractdirs(1);
274 removeoldleaves();
275 Vprintf(stdout, "Calculate node updates.\n");
276 treescan(".", ROOTINO, nodeupdates);
277 findunreflinks();
278 removeoldnodes();
279 } else {
280 /*
281 * This is a level zero dump tape.
282 */
283 Vprintf(stdout, "Begin level 0 restore\n");
284 initsymtable((char *)0);
285 extractdirs(1);
286 Vprintf(stdout, "Calculate extraction list.\n");
287 treescan(".", ROOTINO, nodeupdates);
288 }
289 createleaves(symtbl);
290 createlinks();
291 setdirmodes(FORCE);
292 checkrestore();
293 if (dflag) {
294 Vprintf(stdout, "Verify the directory structure\n");
295 treescan(".", ROOTINO, verifyfile);
296 }
297 dumpsymtable(symtbl, (long)1);
298 break;
299 /*
300 * Resume an incremental file system restoration.
301 */
302 case 'R':
303 initsymtable(symtbl);
304 skipmaps();
305 skipdirs();
306 createleaves(symtbl);
307 createlinks();
308 setdirmodes(FORCE);
309 checkrestore();
310 dumpsymtable(symtbl, (long)1);
311 break;
312 /*
313 * List contents of tape.
314 */
315 case 't':
316 setup();
317 extractdirs(0);
318 initsymtable((char *)0);
319 while (argc--) {
320 canon(*argv++, name, sizeof(name));
321 ino = dirlookup(name);
322 if (ino == 0)
323 continue;
324 treescan(name, ino, listfile);
325 }
326 break;
327 /*
328 * Batch extraction of tape contents.
329 */
330 case 'x':
331 setup();
332 extractdirs(1);
333 initsymtable((char *)0);
334 while (argc--) {
335 canon(*argv++, name, sizeof(name));
336 ino = dirlookup(name);
337 if (ino == 0)
338 continue;
339 if (mflag)
340 pathcheck(name);
341 treescan(name, ino, addfile);
342 }
343 createfiles();
344 createlinks();
345 setdirmodes(0);
346 if (dflag)
347 checkrestore();
348 break;
349 }
350 exit(0);
351 /* NOTREACHED */
352 return 0; /* gcc shut up */
353 }
354
355 static void
356 usage(void)
357 {
358 #ifdef KERBEROS
359 #define kerbflag "k"
360 #else
361 #define kerbflag
362 #endif
363 (void)fprintf(stderr,
364 "%s %s\n", __progname, _DUMP_VERSION);
365 (void)fprintf(stderr,
366 "usage:\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n",
367 __progname, " -i [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-s fileno]",
368 __progname, " -r [-c" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno]",
369 __progname, " -R [-c" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno]",
370 __progname, " -x [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-s fileno] [file ...]",
371 __progname, " -t [-ch" kerbflag "Muvy] [-b blocksize] [-f file] [-s fileno] [file ...]");
372 exit(1);
373 }
374
375 /*
376 * obsolete --
377 * Change set of key letters and ordered arguments into something
378 * getopt(3) will like.
379 */
380 static void
381 obsolete(int *argcp, char **argvp[])
382 {
383 int argc, flags;
384 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
385
386 /* Setup. */
387 argv = *argvp;
388 argc = *argcp;
389
390 /* Return if no arguments or first argument has leading dash. */
391 ap = argv[1];
392 if (argc == 1 || *ap == '-')
393 return;
394
395 /* Allocate space for new arguments. */
396 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
397 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
398 err(1, "malloc args");
399
400 *nargv++ = *argv;
401 argv += 2, argc -= 2;
402
403 for (flags = 0; *ap; ++ap) {
404 switch (*ap) {
405 case 'b':
406 case 'D':
407 case 'f':
408 case 's':
409 case 'T':
410 if (*argv == NULL) {
411 warnx("option requires an argument -- %c", *ap);
412 usage();
413 }
414 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
415 err(1, "malloc arg");
416 nargv[0][0] = '-';
417 nargv[0][1] = *ap;
418 (void)strcpy(&nargv[0][2], *argv);
419 ++argv;
420 ++nargv;
421 break;
422 default:
423 if (!flags) {
424 *p++ = '-';
425 flags = 1;
426 }
427 *p++ = *ap;
428 break;
429 }
430 }
431
432 /* Terminate flags. */
433 if (flags) {
434 *p = '\0';
435 *nargv++ = flagsp;
436 }
437
438 /* Copy remaining arguments. */
439 while ((*nargv++ = *argv++));
440
441 /* Update argument count. */
442 *argcp = nargv - *argvp - 1;
443 }