]>
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 <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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.32 2001/11/16 14:09:07 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 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;
98 int compare_ignore_not_found
;
100 char filesys
[NAMELEN
];
101 static const char *stdin_opt
= NULL
;
102 char *bot_script
= NULL
;
116 static void obsolete
__P((int *, char **[]));
117 static void usage
__P((void));
118 static void use_stdin
__P((const char *));
121 main(int argc
, char *argv
[])
125 char *inputdev
= _PATH_DEFTAPE
;
126 char *symtbl
= "./restoresymtable";
127 char *p
, name
[MAXPATHLEN
];
128 FILE *filelist
= NULL
;
129 char fname
[MAXPATHLEN
];
134 time_t tistart
, tiend
, titaken
;
137 /* Temp files should *not* be readable. We set permissions later. */
141 __progname
= argv
[0];
147 if ((inputdev
= getenv("TAPE")) == NULL
)
148 inputdev
= _PATH_DEFTAPE
;
149 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
151 if ((tmpdir
= strdup(tmpdir
)) == NULL
)
152 err(1, "malloc tmpdir");
153 for (p
= tmpdir
+ strlen(tmpdir
) - 1; p
>= tmpdir
&& *p
== '/'; p
--)
155 obsolete(&argc
, &argv
);
156 while ((ch
= getopt(argc
, argv
,
165 "Rrs:tT:uvVxX:y")) != -1)
168 /* Change default tape blocksize. */
170 ntrec
= strtol(optarg
, &p
, 10);
172 errx(1, "illegal blocksize -- %s", optarg
);
174 errx(1, "block size must be greater than 0");
180 strncpy(filesys
, optarg
, NAMELEN
);
181 filesys
[NAMELEN
- 1] = '\0';
190 if( !strcmp(optarg
,"-") )
213 "%c and %c options are mutually exclusive",
231 gTapeposfile
= optarg
;
236 /* Dumpnum (skip to) for multifile dump tapes. */
237 dumpnum
= strtol(optarg
, &p
, 10);
239 errx(1, "illegal dump number -- %s", optarg
);
241 errx(1, "dump number must be greater than 0");
253 if( !strcmp(optarg
,"-") ) {
258 if ( !(filelist
= fopen(optarg
,"r")) )
259 errx(1, "can't open file for reading -- %s", optarg
);
271 errx(1, "none of C, i, R, r, t or x options specified");
274 if (!mflag
&& tapeposflag
)
275 errx(1, "m and Q options are mutually exclusive");
278 if (signal(SIGINT
, onintr
) == SIG_IGN
)
279 (void) signal(SIGINT
, SIG_IGN
);
280 if (signal(SIGTERM
, onintr
) == SIG_IGN
)
281 (void) signal(SIGTERM
, SIG_IGN
);
286 if (command
== 'C' && inputdev
[0] != '/' && strcmp(inputdev
, "-")
288 && !strchr(inputdev
, ':')
291 /* since we chdir into the directory we are comparing
292 * to, we must retain the full tape path */
293 char wd
[MAXPATHLEN
], fullpathinput
[MAXPATHLEN
];
294 if (!getcwd(wd
, MAXPATHLEN
))
295 err(1, "can't get current directory");
296 snprintf(fullpathinput
, MAXPATHLEN
, "%s/%s", wd
, inputdev
);
297 fullpathinput
[MAXPATHLEN
- 1] = '\0';
298 setinput(fullpathinput
);
303 if (argc
== 0 && !filelist
) {
310 msg("reading QFA positions from %s\n", gTapeposfile
);
311 if ((gTapeposfp
= fopen(gTapeposfile
, "r")) == NULL
)
312 errx(1, "can't open file for reading -- %s",
314 /* start reading header info */
315 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
316 errx(1, "not requested format of -- %s", gTapeposfile
);
317 gTps
[strlen(gTps
) - 1] = 0; /* delete end of line */
318 if (strcmp(gTps
, QFA_MAGIC
) != 0)
319 errx(1, "not requested format of -- %s", gTapeposfile
);
320 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
321 errx(1, "not requested format of -- %s", gTapeposfile
);
322 gTps
[strlen(gTps
) - 1] = 0;
323 if (strcmp(gTps
, QFA_VERSION
) != 0)
324 errx(1, "not requested format of -- %s", gTapeposfile
);
326 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
327 errx(1, "not requested format of -- %s", gTapeposfile
);
328 gTps
[strlen(gTps
) - 1] = 0;
329 /* TODO: check dumpdate from QFA file with current dump file's
331 /* if not equal either output warning and continue without QFA
333 /* read empty line */
334 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
335 errx(1, "not requested format of -- %s", gTapeposfile
);
336 gTps
[strlen(gTps
) - 1] = 0;
337 /* read table header line */
338 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
339 errx(1, "not requested format of -- %s", gTapeposfile
);
340 gTps
[strlen(gTps
) - 1] = 0;
341 /* end reading header info */
342 /* tape position table starts here */
343 gSeekstart
= ftell(gTapeposfp
); /* remember for later use */
349 * Compare contents of tape.
354 Vprintf(stdout
, "Begin compare restore\n");
355 compare_ignore_not_found
= 0;
358 printf("filesys = %s\n", filesys
);
359 if (STAT(filesys
, &stbuf
) < 0)
360 err(1, "cannot stat directory %s", filesys
);
361 if (chdir(filesys
) < 0)
362 err(1, "cannot cd to %s", filesys
);
363 compare_ignore_not_found
= dumptime
> 0;
364 initsymtable((char *)0);
366 treescan(".", ROOTINO
, nodeupdates
);
369 if (compare_errors
) {
370 printf("Some files were modified!\n");
386 * Incremental restoration of a file system.
392 * This is an incremental dump tape.
394 Vprintf(stdout
, "Begin incremental restore\n");
395 initsymtable(symtbl
);
398 Vprintf(stdout
, "Calculate node updates.\n");
399 treescan(".", ROOTINO
, nodeupdates
);
404 * This is a level zero dump tape.
406 Vprintf(stdout
, "Begin level 0 restore\n");
407 initsymtable((char *)0);
409 Vprintf(stdout
, "Calculate extraction list.\n");
410 treescan(".", ROOTINO
, nodeupdates
);
412 createleaves(symtbl
);
417 Vprintf(stdout
, "Verify the directory structure\n");
418 treescan(".", ROOTINO
, verifyfile
);
420 dumpsymtable(symtbl
, (long)1);
423 * Resume an incremental file system restoration.
426 initsymtable(symtbl
);
429 createleaves(symtbl
);
433 dumpsymtable(symtbl
, (long)1);
436 /* handle file names from either text file (-X) or the command line */
437 #define NEXTFILE(p) \
443 else if (filelist) { \
444 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
445 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
446 *(p + strlen(p) - 1) = '\0'; \
447 if ( !*p ) /* skip empty lines */ \
453 * List contents of tape.
458 initsymtable((char *)0);
463 canon(p
, name
, sizeof(name
));
464 ino
= dirlookup(name
);
467 treescan(name
, ino
, listfile
);
471 * Batch extraction of tape contents.
475 tistart
= time(NULL
);
479 initsymtable((char *)0);
484 canon(p
, name
, sizeof(name
));
485 ino
= dirlookup(name
);
490 treescan(name
, ino
, addfile
);
499 titaken
= tiend
- tistart
;
500 msg("restore took %d:%02d:%02d\n", titaken
/ 3600,
501 (titaken
% 3600) / 60, titaken
% 60);
502 #endif /* USE_QFADEBUG */
507 return 0; /* gcc shut up */
514 const char *ext2ver
, *ext2date
;
516 ext2fs_get_library_version(&ext2ver
, &ext2date
);
517 (void)fprintf(stderr
, "%s %s (using libext2fs %s of %s)\n",
518 __progname
, _DUMP_VERSION
, ext2ver
, ext2date
);
520 (void)fprintf(stderr
, "%s %s\n", __progname
, _DUMP_VERSION
);
530 #define qfaflag "[-Q file] "
535 (void)fprintf(stderr
,
536 "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",
537 __progname
, " -C [-c" kerbflag
"lMvVy] [-b blocksize] [-D filesystem] [-f file] [-F script] [-s fileno]",
538 __progname
, " -i [-ch" kerbflag
"lmMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag
"[-s fileno]",
539 __progname
, " -r [-c" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script] [-s fileno] [-T directory]",
540 __progname
, " -R [-c" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script] [-s fileno] [-T directory]",
541 __progname
, " -t [-ch" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]",
542 __progname
, " -x [-ch" kerbflag
"lmMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]");
548 * Change set of key letters and ordered arguments into something
549 * getopt(3) will like.
552 obsolete(int *argcp
, char **argvp
[])
555 char *ap
, **argv
, *flagsp
= NULL
, **nargv
, *p
= NULL
;
561 /* Return if no arguments or first argument has leading dash. */
563 if (argc
== 1 || *ap
== '-')
566 /* Allocate space for new arguments. */
567 if ((*argvp
= nargv
= malloc((argc
+ 1) * sizeof(char *))) == NULL
||
568 (p
= flagsp
= malloc(strlen(ap
) + 2)) == NULL
)
569 err(1, "malloc args");
572 argv
+= 2, argc
-= 2;
574 for (flags
= 0; *ap
; ++ap
) {
585 warnx("option requires an argument -- %c", *ap
);
588 if ((nargv
[0] = malloc(strlen(*argv
) + 2 + 1)) == NULL
)
589 err(1, "malloc arg");
592 (void)strcpy(&nargv
[0][2], *argv
);
606 /* Terminate flags. */
612 /* Copy remaining arguments. */
613 while ((*nargv
++ = *argv
++));
615 /* Update argument count. */
616 *argcp
= nargv
- *argvp
- 1;
621 * reserve stdin for opt (avoid conflicts)
624 use_stdin(const char *opt
)
627 errx(1, "can't handle standard input for both %s and %s",