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