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