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