]>
git.wh0rd.org - dump.git/blob - restore/main.c
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
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
43 static const char rcsid
[] =
44 "$Id: main.c,v 1.37 2002/01/25 14:59:53 stelian Exp $";
48 #include <compatlfs.h>
49 #include <sys/param.h>
56 #ifdef HAVE_EXT2FS_EXT2_FS_H
57 #include <ext2fs/ext2_fs.h>
59 #include <linux/ext2_fs.h>
61 #include <bsdcompat.h>
65 #include <ufs/ufs/dinode.h>
66 #endif /* __linux__ */
67 #include <protocols/dumprestore.h>
69 #include <compaterr.h>
75 #include <ext2fs/ext2fs.h>
79 #include "pathnames.h"
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;
99 int compare_ignore_not_found
;
101 char filesys
[NAMELEN
];
102 static const char *stdin_opt
= NULL
;
103 char *bot_script
= NULL
;
104 dump_ino_t volinfo
[TP_NINOS
];
118 static void obsolete
__P((int *, char **[]));
119 static void usage
__P((void));
120 static void use_stdin
__P((const char *));
123 main(int argc
, char *argv
[])
127 char *inputdev
= _PATH_DEFTAPE
;
128 char *symtbl
= "./restoresymtable";
129 char *p
, name
[MAXPATHLEN
];
130 FILE *filelist
= NULL
;
131 char fname
[MAXPATHLEN
];
136 time_t tistart
, tiend
, titaken
;
139 /* Temp files should *not* be readable. We set permissions later. */
143 __progname
= argv
[0];
149 if ((inputdev
= getenv("TAPE")) == NULL
)
150 inputdev
= _PATH_DEFTAPE
;
151 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
153 if ((tmpdir
= strdup(tmpdir
)) == NULL
)
154 err(1, "malloc tmpdir");
155 for (p
= tmpdir
+ strlen(tmpdir
) - 1; p
>= tmpdir
&& *p
== '/'; p
--)
157 obsolete(&argc
, &argv
);
158 while ((ch
= getopt(argc
, argv
,
167 "Rrs:tT:uvVxX:y")) != -1)
177 /* Change default tape blocksize. */
179 ntrec
= strtol(optarg
, &p
, 10);
181 errx(1, "illegal blocksize -- %s", optarg
);
183 errx(1, "block size must be greater than 0");
189 strncpy(filesys
, optarg
, NAMELEN
);
190 filesys
[NAMELEN
- 1] = '\0';
199 if( !strcmp(optarg
,"-") )
222 "%c and %c options are mutually exclusive",
230 Lflag
= strtol(optarg
, &p
, 10);
232 errx(1, "illegal limit -- %s", optarg
);
234 errx(1, "limit must be greater than 0");
247 gTapeposfile
= optarg
;
253 /* Dumpnum (skip to) for multifile dump tapes. */
254 dumpnum
= strtol(optarg
, &p
, 10);
256 errx(1, "illegal dump number -- %s", optarg
);
258 errx(1, "dump number must be greater than 0");
270 if( !strcmp(optarg
,"-") ) {
275 if ( !(filelist
= fopen(optarg
,"r")) )
276 errx(1, "can't open file for reading -- %s", optarg
);
288 errx(1, "none of C, i, R, r, t or x options specified");
291 if (!mflag
&& tapeposflag
)
292 errx(1, "m and Q options are mutually exclusive");
294 if (tapeposflag
&& command
!= 'i' && command
!= 'x' && command
!= 't')
295 errx(1, "Q option is not valid for %c command", command
);
298 if (Afile
&& command
!= 'i' && command
!= 'x' && command
!= 't')
299 errx(1, "A option is not valid for %c command", command
);
301 if (signal(SIGINT
, onintr
) == SIG_IGN
)
302 (void) signal(SIGINT
, SIG_IGN
);
303 if (signal(SIGTERM
, onintr
) == SIG_IGN
)
304 (void) signal(SIGTERM
, SIG_IGN
);
309 if (command
== 'C' && inputdev
[0] != '/' && strcmp(inputdev
, "-")
311 && !strchr(inputdev
, ':')
314 /* since we chdir into the directory we are comparing
315 * to, we must retain the full tape path */
316 char wd
[MAXPATHLEN
], fullpathinput
[MAXPATHLEN
];
317 if (!getcwd(wd
, MAXPATHLEN
))
318 err(1, "can't get current directory");
319 snprintf(fullpathinput
, MAXPATHLEN
, "%s/%s", wd
, inputdev
);
320 fullpathinput
[MAXPATHLEN
- 1] = '\0';
321 setinput(fullpathinput
);
326 if (argc
== 0 && !filelist
) {
333 msg("reading QFA positions from %s\n", gTapeposfile
);
334 if ((gTapeposfp
= fopen(gTapeposfile
, "r")) == NULL
)
335 errx(1, "can't open file for reading -- %s",
337 /* start reading header info */
338 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
339 errx(1, "not requested format of -- %s", gTapeposfile
);
340 gTps
[strlen(gTps
) - 1] = 0; /* delete end of line */
341 if (strcmp(gTps
, QFA_MAGIC
) != 0)
342 errx(1, "not requested format of -- %s", gTapeposfile
);
343 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
344 errx(1, "not requested format of -- %s", gTapeposfile
);
345 gTps
[strlen(gTps
) - 1] = 0;
346 if (strcmp(gTps
, QFA_VERSION
) != 0)
347 errx(1, "not requested format of -- %s", gTapeposfile
);
349 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
350 errx(1, "not requested format of -- %s", gTapeposfile
);
351 gTps
[strlen(gTps
) - 1] = 0;
352 /* TODO: check dumpdate from QFA file with current dump file's
354 /* if not equal either output warning and continue without QFA
356 /* read empty line */
357 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
358 errx(1, "not requested format of -- %s", gTapeposfile
);
359 gTps
[strlen(gTps
) - 1] = 0;
360 /* read table header line */
361 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
362 errx(1, "not requested format of -- %s", gTapeposfile
);
363 gTps
[strlen(gTps
) - 1] = 0;
364 /* end reading header info */
365 /* tape position table starts here */
366 gSeekstart
= ftell(gTapeposfp
); /* remember for later use */
372 * Compare contents of tape.
377 Vprintf(stdout
, "Begin compare restore\n");
378 compare_ignore_not_found
= 0;
381 printf("filesys = %s\n", filesys
);
382 if (STAT(filesys
, &stbuf
) < 0)
383 err(1, "cannot stat directory %s", filesys
);
384 if (chdir(filesys
) < 0)
385 err(1, "cannot cd to %s", filesys
);
386 compare_ignore_not_found
= dumptime
> 0;
387 initsymtable((char *)0);
389 treescan(".", ROOTINO
, nodeupdates
);
392 if (compare_errors
) {
393 printf("Some files were modified!\n");
409 * Incremental restoration of a file system.
412 aflag
= 1; /* in -r or -R mode, -a is default */
416 * This is an incremental dump tape.
418 Vprintf(stdout
, "Begin incremental restore\n");
419 initsymtable(symtbl
);
422 Vprintf(stdout
, "Calculate node updates.\n");
423 treescan(".", ROOTINO
, nodeupdates
);
428 * This is a level zero dump tape.
430 Vprintf(stdout
, "Begin level 0 restore\n");
431 initsymtable((char *)0);
433 Vprintf(stdout
, "Calculate extraction list.\n");
434 treescan(".", ROOTINO
, nodeupdates
);
436 createleaves(symtbl
);
441 Vprintf(stdout
, "Verify the directory structure\n");
442 treescan(".", ROOTINO
, verifyfile
);
444 dumpsymtable(symtbl
, (long)1);
447 * Resume an incremental file system restoration.
450 aflag
= 1; /* in -r or -R mode, -a is default */
451 initsymtable(symtbl
);
454 createleaves(symtbl
);
458 dumpsymtable(symtbl
, (long)1);
461 /* handle file names from either text file (-X) or the command line */
462 #define NEXTFILE(p) \
468 else if (filelist) { \
469 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
470 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
471 *(p + strlen(p) - 1) = '\0'; \
472 if ( !*p ) /* skip empty lines */ \
478 * List contents of tape.
483 initsymtable((char *)0);
489 canon(p
, name
, sizeof(name
));
490 ino
= dirlookup(name
);
493 treescan(name
, ino
, listfile
);
497 * Batch extraction of tape contents.
501 tistart
= time(NULL
);
505 initsymtable((char *)0);
510 canon(p
, name
, sizeof(name
));
511 ino
= dirlookup(name
);
516 treescan(name
, ino
, addfile
);
525 titaken
= tiend
- tistart
;
526 msg("restore took %d:%02d:%02d\n", titaken
/ 3600,
527 (titaken
% 3600) / 60, titaken
% 60);
528 #endif /* USE_QFADEBUG */
533 return 0; /* gcc shut up */
539 char white
[MAXPATHLEN
];
540 const char *ext2ver
, *ext2date
;
542 memset(white
, ' ', MAXPATHLEN
);
543 white
[MIN(strlen(__progname
), MAXPATHLEN
- 1)] = '\0';
546 ext2fs_get_library_version(&ext2ver
, &ext2date
);
547 (void)fprintf(stderr
, "%s %s (using libext2fs %s of %s)\n",
548 __progname
, _DUMP_VERSION
, ext2ver
, ext2date
);
550 (void)fprintf(stderr
, "%s %s\n", __progname
, _DUMP_VERSION
);
560 #define qfaflag "[-Q file] "
567 "\t%s -C [-c" kerbflag
"lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
568 "\t%s [-F script] [-L limit] [-s fileno]\n"
569 "\t%s -i [-ach" kerbflag
"lmMuvVy] [-A file] [-b blocksize] [-f file]\n"
570 "\t%s [-F script] " qfaflag
"[-s fileno]\n"
571 "\t%s -r [-c" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script]\n"
572 "\t%s [-s fileno] [-T directory]\n"
573 "\t%s -R [-c" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script]\n"
574 "\t%s [-s fileno] [-T directory]\n"
575 "\t%s -t [-ch" kerbflag
"lMuvVy] [-A file] [-b blocksize] [-f file]\n"
576 "\t%s [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]\n"
577 "\t%s -x [-ach" kerbflag
"lmMuvVy] [-A file] [-b blocksize] [-f file]\n"
578 "\t%s [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]\n",
590 * Change set of key letters and ordered arguments into something
591 * getopt(3) will like.
594 obsolete(int *argcp
, char **argvp
[])
597 char *ap
, **argv
, *flagsp
= NULL
, **nargv
, *p
= NULL
;
603 /* Return if no arguments or first argument has leading dash. */
605 if (argc
== 1 || *ap
== '-')
608 /* Allocate space for new arguments. */
609 if ((*argvp
= nargv
= malloc((argc
+ 1) * sizeof(char *))) == NULL
||
610 (p
= flagsp
= malloc(strlen(ap
) + 2)) == NULL
)
611 err(1, "malloc args");
614 argv
+= 2, argc
-= 2;
616 for (flags
= 0; *ap
; ++ap
) {
629 warnx("option requires an argument -- %c", *ap
);
632 if ((nargv
[0] = malloc(strlen(*argv
) + 2 + 1)) == NULL
)
633 err(1, "malloc arg");
636 (void)strcpy(&nargv
[0][2], *argv
);
650 /* Terminate flags. */
656 /* Copy remaining arguments. */
657 while ((*nargv
++ = *argv
++));
659 /* Update argument count. */
660 *argcp
= nargv
- *argvp
- 1;
665 * reserve stdin for opt (avoid conflicts)
668 use_stdin(const char *opt
)
671 errx(1, "can't handle standard input for both %s and %s",