]> git.wh0rd.org - dump.git/blob - restore/main.c
Added -F script option to restore.
[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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #ifndef lint
43 static const char rcsid[] =
44 "$Id: main.c,v 1.25 2001/07/18 12:54:06 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #include <compatlfs.h>
49 #include <sys/param.h>
50 #include <sys/stat.h>
51 #include <errno.h>
52
53 #ifdef __linux__
54 #include <sys/time.h>
55 #include <time.h>
56 #ifdef HAVE_EXT2FS_EXT2_FS_H
57 #include <ext2fs/ext2_fs.h>
58 #else
59 #include <linux/ext2_fs.h>
60 #endif
61 #include <bsdcompat.h>
62 #include <signal.h>
63 #include <string.h>
64 #else /* __linux__ */
65 #include <ufs/ufs/dinode.h>
66 #endif /* __linux__ */
67 #include <protocols/dumprestore.h>
68
69 #include <compaterr.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <unistd.h>
73
74 #ifdef __linux__
75 #include <ext2fs/ext2fs.h>
76 #include <getopt.h>
77 #endif
78
79 #include "pathnames.h"
80 #include "restore.h"
81 #include "extern.h"
82
83 int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
84 int hflag = 1, mflag = 1, Nflag = 0, zflag = 0;
85 int uflag = 0;
86 int dokerberos = 0;
87 char command = '\0';
88 long dumpnum = 1;
89 long volno = 0;
90 long ntrec;
91 char *dumpmap;
92 char *usedinomap;
93 dump_ino_t maxino;
94 time_t dumptime;
95 time_t dumpdate;
96 FILE *terminal;
97 char *tmpdir;
98 int compare_ignore_not_found;
99 int compare_errors;
100 char filesys[NAMELEN];
101 static const char *stdin_opt = NULL;
102 char *bot_script = NULL;
103
104 #ifdef __linux__
105 char *__progname;
106 #endif
107
108 static void obsolete __P((int *, char **[]));
109 static void usage __P((void));
110 static void use_stdin __P((const char *));
111
112 int
113 main(int argc, char *argv[])
114 {
115 int ch;
116 dump_ino_t ino;
117 char *inputdev = _PATH_DEFTAPE;
118 char *symtbl = "./restoresymtable";
119 char *p, name[MAXPATHLEN];
120 FILE *filelist = NULL;
121 char fname[MAXPATHLEN];
122 #ifdef USE_QFA
123 tapeposflag = 0;
124 #endif
125 #ifdef USE_QFADEBUG
126 time_t tistart, tiend, titaken;
127 #endif
128
129 /* Temp files should *not* be readable. We set permissions later. */
130 (void) umask(077);
131 filesys[0] = '\0';
132 #ifdef __linux__
133 __progname = argv[0];
134 #endif
135
136 if (argc < 2)
137 usage();
138
139 if ((inputdev = getenv("TAPE")) == NULL)
140 inputdev = _PATH_DEFTAPE;
141 if ((tmpdir = getenv("TMPDIR")) == NULL)
142 tmpdir = _PATH_TMP;
143 if ((tmpdir = strdup(tmpdir)) == NULL)
144 err(1, "malloc tmpdir");
145 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
146 ;
147 obsolete(&argc, &argv);
148 while ((ch = getopt(argc, argv,
149 "b:CcdD:f:F:hi"
150 #ifdef KERBEROS
151 "k"
152 #endif
153 "mMN"
154 #ifdef USE_QFA
155 "Q:"
156 #endif
157 "Rrs:tT:uvxX:y")) != -1)
158 switch(ch) {
159 case 'b':
160 /* Change default tape blocksize. */
161 bflag = 1;
162 ntrec = strtol(optarg, &p, 10);
163 if (*p)
164 errx(1, "illegal blocksize -- %s", optarg);
165 if (ntrec <= 0)
166 errx(1, "block size must be greater than 0");
167 break;
168 case 'c':
169 cvtflag = 1;
170 break;
171 case 'D':
172 strncpy(filesys, optarg, NAMELEN);
173 filesys[NAMELEN - 1] = '\0';
174 break;
175 case 'T':
176 tmpdir = optarg;
177 break;
178 case 'd':
179 dflag = 1;
180 break;
181 case 'f':
182 if( !strcmp(optarg,"-") )
183 use_stdin("-f");
184 inputdev = optarg;
185 break;
186 case 'F':
187 bot_script = optarg;
188 break;
189 case 'h':
190 hflag = 0;
191 break;
192 #ifdef KERBEROS
193 case 'k':
194 dokerberos = 1;
195 break;
196 #endif
197 case 'C':
198 case 'i':
199 case 'R':
200 case 'r':
201 case 't':
202 case 'x':
203 if (command != '\0')
204 errx(1,
205 "%c and %c options are mutually exclusive",
206 ch, command);
207 command = ch;
208 break;
209 case 'm':
210 mflag = 0;
211 break;
212 case 'M':
213 Mflag = 1;
214 break;
215 case 'N':
216 Nflag = 1;
217 break;
218 #ifdef USE_QFA
219 case 'Q':
220 gTapeposfile = optarg;
221 tapeposflag = 1;
222 break;
223 #endif
224 case 's':
225 /* Dumpnum (skip to) for multifile dump tapes. */
226 dumpnum = strtol(optarg, &p, 10);
227 if (*p)
228 errx(1, "illegal dump number -- %s", optarg);
229 if (dumpnum <= 0)
230 errx(1, "dump number must be greater than 0");
231 break;
232 case 'u':
233 uflag = 1;
234 break;
235 case 'v':
236 vflag = 1;
237 break;
238 case 'X':
239 if( !strcmp(optarg,"-") ) {
240 use_stdin("-X");
241 filelist = stdin;
242 }
243 else
244 if ( !(filelist = fopen(optarg,"r")) )
245 errx(1, "can't open file for reading -- %s", optarg);
246 break;
247 case 'y':
248 yflag = 1;
249 break;
250 default:
251 usage();
252 }
253 argc -= optind;
254 argv += optind;
255
256 if (command == '\0')
257 errx(1, "none of C, i, R, r, t or x options specified");
258
259 #ifdef USE_QFA
260 if (!mflag && tapeposflag)
261 errx(1, "m and Q options are mutually exclusive");
262 #endif
263
264 if (signal(SIGINT, onintr) == SIG_IGN)
265 (void) signal(SIGINT, SIG_IGN);
266 if (signal(SIGTERM, onintr) == SIG_IGN)
267 (void) signal(SIGTERM, SIG_IGN);
268 setlinebuf(stderr);
269
270 atexit(cleanup);
271
272 setinput(inputdev);
273
274 if (argc == 0 && !filelist) {
275 argc = 1;
276 *--argv = ".";
277 }
278
279 #ifdef USE_QFA
280 if (tapeposflag) {
281 msg("reading QFA positions from %s\n", gTapeposfile);
282 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
283 errx(1, "can't open file for reading -- %s",
284 gTapeposfile);
285 /* start reading header info */
286 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
287 errx(1, "not requested format of -- %s", gTapeposfile);
288 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
289 if (strcmp(gTps, QFA_MAGIC) != 0)
290 errx(1, "not requested format of -- %s", gTapeposfile);
291 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
292 errx(1, "not requested format of -- %s", gTapeposfile);
293 gTps[strlen(gTps) - 1] = 0;
294 if (strcmp(gTps, QFA_VERSION) != 0)
295 errx(1, "not requested format of -- %s", gTapeposfile);
296 /* read dumpdate */
297 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
298 errx(1, "not requested format of -- %s", gTapeposfile);
299 gTps[strlen(gTps) - 1] = 0;
300 /* TODO: check dumpdate from QFA file with current dump file's
301 * dump date */
302 /* if not equal either output warning and continue without QFA
303 * or abort */
304 /* read empty line */
305 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
306 errx(1, "not requested format of -- %s", gTapeposfile);
307 gTps[strlen(gTps) - 1] = 0;
308 /* read table header line */
309 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
310 errx(1, "not requested format of -- %s", gTapeposfile);
311 gTps[strlen(gTps) - 1] = 0;
312 /* end reading header info */
313 /* tape position table starts here */
314 gSeekstart = ftell(gTapeposfp); /* remember for later use */
315 }
316 #endif /* USE_QFA */
317
318 switch (command) {
319 /*
320 * Compare contents of tape.
321 */
322 case 'C': {
323 struct STAT stbuf;
324
325 Vprintf(stdout, "Begin compare restore\n");
326 compare_ignore_not_found = 0;
327 compare_errors = 0;
328 setup();
329 printf("filesys = %s\n", filesys);
330 if (STAT(filesys, &stbuf) < 0)
331 err(1, "cannot stat directory %s", filesys);
332 if (chdir(filesys) < 0)
333 err(1, "cannot cd to %s", filesys);
334 compare_ignore_not_found = dumptime > 0;
335 initsymtable((char *)0);
336 extractdirs(0);
337 treescan(".", ROOTINO, nodeupdates);
338 compareleaves();
339 checkrestore();
340 if (compare_errors) {
341 printf("Some files were modified!\n");
342 exit(2);
343 }
344 break;
345 }
346
347 /*
348 * Interactive mode.
349 */
350 case 'i':
351 setup();
352 extractdirs(1);
353 initsymtable(NULL);
354 runcmdshell();
355 break;
356 /*
357 * Incremental restoration of a file system.
358 */
359 case 'r':
360 setup();
361 if (dumptime > 0) {
362 /*
363 * This is an incremental dump tape.
364 */
365 Vprintf(stdout, "Begin incremental restore\n");
366 initsymtable(symtbl);
367 extractdirs(1);
368 removeoldleaves();
369 Vprintf(stdout, "Calculate node updates.\n");
370 treescan(".", ROOTINO, nodeupdates);
371 findunreflinks();
372 removeoldnodes();
373 } else {
374 /*
375 * This is a level zero dump tape.
376 */
377 Vprintf(stdout, "Begin level 0 restore\n");
378 initsymtable((char *)0);
379 extractdirs(1);
380 Vprintf(stdout, "Calculate extraction list.\n");
381 treescan(".", ROOTINO, nodeupdates);
382 }
383 createleaves(symtbl);
384 createlinks();
385 setdirmodes(FORCE);
386 checkrestore();
387 if (dflag) {
388 Vprintf(stdout, "Verify the directory structure\n");
389 treescan(".", ROOTINO, verifyfile);
390 }
391 dumpsymtable(symtbl, (long)1);
392 break;
393 /*
394 * Resume an incremental file system restoration.
395 */
396 case 'R':
397 initsymtable(symtbl);
398 skipmaps();
399 skipdirs();
400 createleaves(symtbl);
401 createlinks();
402 setdirmodes(FORCE);
403 checkrestore();
404 dumpsymtable(symtbl, (long)1);
405 break;
406
407 /* handle file names from either text file (-X) or the command line */
408 #define NEXTFILE(p) \
409 p = NULL; \
410 if (argc) { \
411 --argc; \
412 p = *argv++; \
413 } \
414 else if (filelist) { \
415 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
416 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
417 *(p + strlen(p) - 1) = '\0'; \
418 if ( !*p ) /* skip empty lines */ \
419 continue; \
420 } \
421 }
422
423 /*
424 * List contents of tape.
425 */
426 case 't':
427 setup();
428 extractdirs(0);
429 initsymtable((char *)0);
430 for (;;) {
431 NEXTFILE(p);
432 if (!p)
433 break;
434 canon(p, name, sizeof(name));
435 ino = dirlookup(name);
436 if (ino == 0)
437 continue;
438 treescan(name, ino, listfile);
439 }
440 break;
441 /*
442 * Batch extraction of tape contents.
443 */
444 case 'x':
445 #ifdef USE_QFADEBUG
446 tistart = time(NULL);
447 #endif
448 setup();
449 extractdirs(1);
450 initsymtable((char *)0);
451 for (;;) {
452 NEXTFILE(p);
453 if (!p)
454 break;
455 canon(p, name, sizeof(name));
456 ino = dirlookup(name);
457 if (ino == 0)
458 continue;
459 if (mflag)
460 pathcheck(name);
461 treescan(name, ino, addfile);
462 }
463 createfiles();
464 createlinks();
465 setdirmodes(0);
466 if (dflag)
467 checkrestore();
468 #ifdef USE_QFADEBUG
469 tiend = time(NULL);
470 titaken = tiend - tistart;
471 msg("restore took %d:%02d:%02d\n", titaken / 3600,
472 (titaken % 3600) / 60, titaken % 60);
473 #endif /* USE_QFADEBUG */
474 break;
475 }
476 exit(0);
477 /* NOTREACHED */
478 return 0; /* gcc shut up */
479 }
480
481 static void
482 usage(void)
483 {
484 #ifdef __linux__
485 const char *ext2ver, *ext2date;
486
487 ext2fs_get_library_version(&ext2ver, &ext2date);
488 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
489 __progname, _DUMP_VERSION, ext2ver, ext2date);
490 #else
491 (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
492 #endif
493
494 #ifdef KERBEROS
495 #define kerbflag "k"
496 #else
497 #define kerbflag
498 #endif
499
500 #ifdef USE_QFA
501 #define qfaflag "[-Q file] "
502 #else
503 #define qfaflag
504 #endif
505
506 (void)fprintf(stderr,
507 "usage:\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n\t%s%s\n",
508 __progname, " -C [-c" kerbflag "Mvy] [-b blocksize] [-D filesystem] [-f file] [-F script ] [-s fileno]",
509 __progname, " -i [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-F script] " qfaflag "[-s fileno]",
510 __progname, " -r [-c" kerbflag "Muvy] [-b blocksize] [-f file] [-F script] [-s fileno] [-T directory]",
511 __progname, " -R [-c" kerbflag "Muvy] [-b blocksize] [-f file] [-F script] [-s fileno] [-T directory]",
512 __progname, " -t [-ch" kerbflag "Muvy] [-b blocksize] [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]",
513 __progname, " -x [-ch" kerbflag "mMuvy] [-b blocksize] [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]");
514 exit(1);
515 }
516
517 /*
518 * obsolete --
519 * Change set of key letters and ordered arguments into something
520 * getopt(3) will like.
521 */
522 static void
523 obsolete(int *argcp, char **argvp[])
524 {
525 int argc, flags;
526 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
527
528 /* Setup. */
529 argv = *argvp;
530 argc = *argcp;
531
532 /* Return if no arguments or first argument has leading dash. */
533 ap = argv[1];
534 if (argc == 1 || *ap == '-')
535 return;
536
537 /* Allocate space for new arguments. */
538 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
539 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
540 err(1, "malloc args");
541
542 *nargv++ = *argv;
543 argv += 2, argc -= 2;
544
545 for (flags = 0; *ap; ++ap) {
546 switch (*ap) {
547 case 'b':
548 case 'D':
549 case 'f':
550 case 'F':
551 case 'Q':
552 case 's':
553 case 'T':
554 case 'X':
555 if (*argv == NULL) {
556 warnx("option requires an argument -- %c", *ap);
557 usage();
558 }
559 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
560 err(1, "malloc arg");
561 nargv[0][0] = '-';
562 nargv[0][1] = *ap;
563 (void)strcpy(&nargv[0][2], *argv);
564 ++argv;
565 ++nargv;
566 break;
567 default:
568 if (!flags) {
569 *p++ = '-';
570 flags = 1;
571 }
572 *p++ = *ap;
573 break;
574 }
575 }
576
577 /* Terminate flags. */
578 if (flags) {
579 *p = '\0';
580 *nargv++ = flagsp;
581 }
582
583 /* Copy remaining arguments. */
584 while ((*nargv++ = *argv++));
585
586 /* Update argument count. */
587 *argcp = nargv - *argvp - 1;
588 }
589
590 /*
591 * use_stdin --
592 * reserve stdin for opt (avoid conflicts)
593 */
594 void
595 use_stdin(const char *opt)
596 {
597 if (stdin_opt)
598 errx(1, "can't handle standard input for both %s and %s",
599 stdin_opt, opt);
600 stdin_opt = opt;
601 }