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