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