]> git.wh0rd.org - dump.git/blob - restore/main.c
Added -a flag to restore (automatic walk through volumes)
[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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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.35 2002/01/16 10:29:26 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 aflag = 0, bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
84 int hflag = 1, mflag = 1, Mflag = 0, Nflag = 0, Vflag = 0, zflag = 0;
85 int uflag = 0, lflag = 0, Lflag = 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 USE_QFA
105 FILE *gTapeposfp;
106 char *gTapeposfile;
107 char gTps[255];
108 long gSeekstart;
109 int tapeposflag;
110 #endif /* USE_QFA */
111
112 #ifdef __linux__
113 char *__progname;
114 #endif
115
116 static void obsolete __P((int *, char **[]));
117 static void usage __P((void));
118 static void use_stdin __P((const char *));
119
120 int
121 main(int argc, char *argv[])
122 {
123 int ch;
124 dump_ino_t ino;
125 char *inputdev = _PATH_DEFTAPE;
126 char *symtbl = "./restoresymtable";
127 char *p, name[MAXPATHLEN];
128 FILE *filelist = NULL;
129 char fname[MAXPATHLEN];
130 #ifdef USE_QFA
131 tapeposflag = 0;
132 #endif
133 #ifdef USE_QFADEBUG
134 time_t tistart, tiend, titaken;
135 #endif
136
137 /* Temp files should *not* be readable. We set permissions later. */
138 (void) umask(077);
139 filesys[0] = '\0';
140 #ifdef __linux__
141 __progname = argv[0];
142 #endif
143
144 if (argc < 2)
145 usage();
146
147 if ((inputdev = getenv("TAPE")) == NULL)
148 inputdev = _PATH_DEFTAPE;
149 if ((tmpdir = getenv("TMPDIR")) == NULL)
150 tmpdir = _PATH_TMP;
151 if ((tmpdir = strdup(tmpdir)) == NULL)
152 err(1, "malloc tmpdir");
153 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
154 ;
155 obsolete(&argc, &argv);
156 while ((ch = getopt(argc, argv,
157 "ab:CcdD:f:F:hi"
158 #ifdef KERBEROS
159 "k"
160 #endif
161 "lL:mMN"
162 #ifdef USE_QFA
163 "Q:"
164 #endif
165 "Rrs:tT:uvVxX:y")) != -1)
166 switch(ch) {
167 case 'a':
168 aflag = 1;
169 break;
170 case 'b':
171 /* Change default tape blocksize. */
172 bflag = 1;
173 ntrec = strtol(optarg, &p, 10);
174 if (*p)
175 errx(1, "illegal blocksize -- %s", optarg);
176 if (ntrec <= 0)
177 errx(1, "block size must be greater than 0");
178 break;
179 case 'c':
180 cvtflag = 1;
181 break;
182 case 'D':
183 strncpy(filesys, optarg, NAMELEN);
184 filesys[NAMELEN - 1] = '\0';
185 break;
186 case 'T':
187 tmpdir = optarg;
188 break;
189 case 'd':
190 dflag = 1;
191 break;
192 case 'f':
193 if( !strcmp(optarg,"-") )
194 use_stdin("-f");
195 inputdev = optarg;
196 break;
197 case 'F':
198 bot_script = optarg;
199 break;
200 case 'h':
201 hflag = 0;
202 break;
203 #ifdef KERBEROS
204 case 'k':
205 dokerberos = 1;
206 break;
207 #endif
208 case 'C':
209 case 'i':
210 case 'R':
211 case 'r':
212 case 't':
213 case 'x':
214 if (command != '\0')
215 errx(1,
216 "%c and %c options are mutually exclusive",
217 ch, command);
218 command = ch;
219 break;
220 case 'l':
221 lflag = 1;
222 break;
223 case 'L':
224 Lflag = strtol(optarg, &p, 10);
225 if (*p)
226 errx(1, "illegal limit -- %s", optarg);
227 if (Lflag < 0)
228 errx(1, "limit must be greater than 0");
229 break;
230 case 'm':
231 mflag = 0;
232 break;
233 case 'M':
234 Mflag = 1;
235 break;
236 case 'N':
237 Nflag = 1;
238 break;
239 #ifdef USE_QFA
240 case 'Q':
241 gTapeposfile = optarg;
242 tapeposflag = 1;
243 break;
244 #endif
245 case 's':
246 /* Dumpnum (skip to) for multifile dump tapes. */
247 dumpnum = strtol(optarg, &p, 10);
248 if (*p)
249 errx(1, "illegal dump number -- %s", optarg);
250 if (dumpnum <= 0)
251 errx(1, "dump number must be greater than 0");
252 break;
253 case 'u':
254 uflag = 1;
255 break;
256 case 'v':
257 vflag = 1;
258 break;
259 case 'V':
260 Vflag = 1;
261 break;
262 case 'X':
263 if( !strcmp(optarg,"-") ) {
264 use_stdin("-X");
265 filelist = stdin;
266 }
267 else
268 if ( !(filelist = fopen(optarg,"r")) )
269 errx(1, "can't open file for reading -- %s", optarg);
270 break;
271 case 'y':
272 yflag = 1;
273 break;
274 default:
275 usage();
276 }
277 argc -= optind;
278 argv += optind;
279
280 if (command == '\0')
281 errx(1, "none of C, i, R, r, t or x options specified");
282
283 #ifdef USE_QFA
284 if (!mflag && tapeposflag)
285 errx(1, "m and Q options are mutually exclusive");
286 #endif
287
288 if (signal(SIGINT, onintr) == SIG_IGN)
289 (void) signal(SIGINT, SIG_IGN);
290 if (signal(SIGTERM, onintr) == SIG_IGN)
291 (void) signal(SIGTERM, SIG_IGN);
292 setlinebuf(stderr);
293
294 atexit(cleanup);
295
296 if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
297 #ifdef RRESTORE
298 && !strchr(inputdev, ':')
299 #endif
300 ) {
301 /* since we chdir into the directory we are comparing
302 * to, we must retain the full tape path */
303 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
304 if (!getcwd(wd, MAXPATHLEN))
305 err(1, "can't get current directory");
306 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
307 fullpathinput[MAXPATHLEN - 1] = '\0';
308 setinput(fullpathinput);
309 }
310 else
311 setinput(inputdev);
312
313 if (argc == 0 && !filelist) {
314 argc = 1;
315 *--argv = ".";
316 }
317
318 #ifdef USE_QFA
319 if (tapeposflag) {
320 msg("reading QFA positions from %s\n", gTapeposfile);
321 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
322 errx(1, "can't open file for reading -- %s",
323 gTapeposfile);
324 /* start reading header info */
325 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
326 errx(1, "not requested format of -- %s", gTapeposfile);
327 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
328 if (strcmp(gTps, QFA_MAGIC) != 0)
329 errx(1, "not requested format of -- %s", gTapeposfile);
330 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
331 errx(1, "not requested format of -- %s", gTapeposfile);
332 gTps[strlen(gTps) - 1] = 0;
333 if (strcmp(gTps, QFA_VERSION) != 0)
334 errx(1, "not requested format of -- %s", gTapeposfile);
335 /* read dumpdate */
336 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
337 errx(1, "not requested format of -- %s", gTapeposfile);
338 gTps[strlen(gTps) - 1] = 0;
339 /* TODO: check dumpdate from QFA file with current dump file's
340 * dump date */
341 /* if not equal either output warning and continue without QFA
342 * or abort */
343 /* read empty line */
344 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
345 errx(1, "not requested format of -- %s", gTapeposfile);
346 gTps[strlen(gTps) - 1] = 0;
347 /* read table header line */
348 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
349 errx(1, "not requested format of -- %s", gTapeposfile);
350 gTps[strlen(gTps) - 1] = 0;
351 /* end reading header info */
352 /* tape position table starts here */
353 gSeekstart = ftell(gTapeposfp); /* remember for later use */
354 }
355 #endif /* USE_QFA */
356
357 switch (command) {
358 /*
359 * Compare contents of tape.
360 */
361 case 'C': {
362 struct STAT stbuf;
363
364 Vprintf(stdout, "Begin compare restore\n");
365 compare_ignore_not_found = 0;
366 compare_errors = 0;
367 setup();
368 printf("filesys = %s\n", filesys);
369 if (STAT(filesys, &stbuf) < 0)
370 err(1, "cannot stat directory %s", filesys);
371 if (chdir(filesys) < 0)
372 err(1, "cannot cd to %s", filesys);
373 compare_ignore_not_found = dumptime > 0;
374 initsymtable((char *)0);
375 extractdirs(0);
376 treescan(".", ROOTINO, nodeupdates);
377 compareleaves();
378 checkrestore();
379 if (compare_errors) {
380 printf("Some files were modified!\n");
381 exit(2);
382 }
383 break;
384 }
385
386 /*
387 * Interactive mode.
388 */
389 case 'i':
390 setup();
391 extractdirs(1);
392 initsymtable(NULL);
393 runcmdshell();
394 break;
395 /*
396 * Incremental restoration of a file system.
397 */
398 case 'r':
399 aflag = 1; /* in -r or -R mode, -a is default */
400 setup();
401 if (dumptime > 0) {
402 /*
403 * This is an incremental dump tape.
404 */
405 Vprintf(stdout, "Begin incremental restore\n");
406 initsymtable(symtbl);
407 extractdirs(1);
408 removeoldleaves();
409 Vprintf(stdout, "Calculate node updates.\n");
410 treescan(".", ROOTINO, nodeupdates);
411 findunreflinks();
412 removeoldnodes();
413 } else {
414 /*
415 * This is a level zero dump tape.
416 */
417 Vprintf(stdout, "Begin level 0 restore\n");
418 initsymtable((char *)0);
419 extractdirs(1);
420 Vprintf(stdout, "Calculate extraction list.\n");
421 treescan(".", ROOTINO, nodeupdates);
422 }
423 createleaves(symtbl);
424 createlinks();
425 setdirmodes(FORCE);
426 checkrestore();
427 if (dflag) {
428 Vprintf(stdout, "Verify the directory structure\n");
429 treescan(".", ROOTINO, verifyfile);
430 }
431 dumpsymtable(symtbl, (long)1);
432 break;
433 /*
434 * Resume an incremental file system restoration.
435 */
436 case 'R':
437 aflag = 1; /* in -r or -R mode, -a is default */
438 initsymtable(symtbl);
439 skipmaps();
440 skipdirs();
441 createleaves(symtbl);
442 createlinks();
443 setdirmodes(FORCE);
444 checkrestore();
445 dumpsymtable(symtbl, (long)1);
446 break;
447
448 /* handle file names from either text file (-X) or the command line */
449 #define NEXTFILE(p) \
450 p = NULL; \
451 if (argc) { \
452 --argc; \
453 p = *argv++; \
454 } \
455 else if (filelist) { \
456 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
457 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
458 *(p + strlen(p) - 1) = '\0'; \
459 if ( !*p ) /* skip empty lines */ \
460 continue; \
461 } \
462 }
463
464 /*
465 * List contents of tape.
466 */
467 case 't':
468 setup();
469 extractdirs(0);
470 initsymtable((char *)0);
471 for (;;) {
472 NEXTFILE(p);
473 if (!p)
474 break;
475 canon(p, name, sizeof(name));
476 ino = dirlookup(name);
477 if (ino == 0)
478 continue;
479 treescan(name, ino, listfile);
480 }
481 break;
482 /*
483 * Batch extraction of tape contents.
484 */
485 case 'x':
486 #ifdef USE_QFADEBUG
487 tistart = time(NULL);
488 #endif
489 setup();
490 extractdirs(1);
491 initsymtable((char *)0);
492 for (;;) {
493 NEXTFILE(p);
494 if (!p)
495 break;
496 canon(p, name, sizeof(name));
497 ino = dirlookup(name);
498 if (ino == 0)
499 continue;
500 if (mflag)
501 pathcheck(name);
502 treescan(name, ino, addfile);
503 }
504 createfiles();
505 createlinks();
506 setdirmodes(0);
507 if (dflag)
508 checkrestore();
509 #ifdef USE_QFADEBUG
510 tiend = time(NULL);
511 titaken = tiend - tistart;
512 msg("restore took %d:%02d:%02d\n", titaken / 3600,
513 (titaken % 3600) / 60, titaken % 60);
514 #endif /* USE_QFADEBUG */
515 break;
516 }
517 exit(0);
518 /* NOTREACHED */
519 return 0; /* gcc shut up */
520 }
521
522 static void
523 usage(void)
524 {
525 #ifdef __linux__
526 const char *ext2ver, *ext2date;
527
528 ext2fs_get_library_version(&ext2ver, &ext2date);
529 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
530 __progname, _DUMP_VERSION, ext2ver, ext2date);
531 #else
532 (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
533 #endif
534
535 #ifdef KERBEROS
536 #define kerbflag "k"
537 #else
538 #define kerbflag
539 #endif
540
541 #ifdef USE_QFA
542 #define qfaflag "[-Q file] "
543 #else
544 #define qfaflag
545 #endif
546
547 (void)fprintf(stderr,
548 "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",
549 __progname, " -C [-c" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file] [-F script] [-L limit] [-s fileno]",
550 __progname, " -i [-ach" kerbflag "lmMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag "[-s fileno]",
551 __progname, " -r [-c" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script] [-s fileno] [-T directory]",
552 __progname, " -R [-c" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script] [-s fileno] [-T directory]",
553 __progname, " -t [-ch" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]",
554 __progname, " -x [-ach" kerbflag "lmMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]");
555 exit(1);
556 }
557
558 /*
559 * obsolete --
560 * Change set of key letters and ordered arguments into something
561 * getopt(3) will like.
562 */
563 static void
564 obsolete(int *argcp, char **argvp[])
565 {
566 int argc, flags;
567 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
568
569 /* Setup. */
570 argv = *argvp;
571 argc = *argcp;
572
573 /* Return if no arguments or first argument has leading dash. */
574 ap = argv[1];
575 if (argc == 1 || *ap == '-')
576 return;
577
578 /* Allocate space for new arguments. */
579 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
580 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
581 err(1, "malloc args");
582
583 *nargv++ = *argv;
584 argv += 2, argc -= 2;
585
586 for (flags = 0; *ap; ++ap) {
587 switch (*ap) {
588 case 'b':
589 case 'D':
590 case 'f':
591 case 'F':
592 case 'L':
593 case 'Q':
594 case 's':
595 case 'T':
596 case 'X':
597 if (*argv == NULL) {
598 warnx("option requires an argument -- %c", *ap);
599 usage();
600 }
601 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
602 err(1, "malloc arg");
603 nargv[0][0] = '-';
604 nargv[0][1] = *ap;
605 (void)strcpy(&nargv[0][2], *argv);
606 ++argv;
607 ++nargv;
608 break;
609 default:
610 if (!flags) {
611 *p++ = '-';
612 flags = 1;
613 }
614 *p++ = *ap;
615 break;
616 }
617 }
618
619 /* Terminate flags. */
620 if (flags) {
621 *p = '\0';
622 *nargv++ = flagsp;
623 }
624
625 /* Copy remaining arguments. */
626 while ((*nargv++ = *argv++));
627
628 /* Update argument count. */
629 *argcp = nargv - *argvp - 1;
630 }
631
632 /*
633 * use_stdin --
634 * reserve stdin for opt (avoid conflicts)
635 */
636 void
637 use_stdin(const char *opt)
638 {
639 if (stdin_opt)
640 errx(1, "can't handle standard input for both %s and %s",
641 stdin_opt, opt);
642 stdin_opt = opt;
643 }