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