]>
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.35 2002/01/16 10:29:26 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;
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)
171 /* Change default tape blocksize. */
173 ntrec
= strtol(optarg
, &p
, 10);
175 errx(1, "illegal blocksize -- %s", optarg
);
177 errx(1, "block size must be greater than 0");
183 strncpy(filesys
, optarg
, NAMELEN
);
184 filesys
[NAMELEN
- 1] = '\0';
193 if( !strcmp(optarg
,"-") )
216 "%c and %c options are mutually exclusive",
224 Lflag
= strtol(optarg
, &p
, 10);
226 errx(1, "illegal limit -- %s", optarg
);
228 errx(1, "limit must be greater than 0");
241 gTapeposfile
= optarg
;
246 /* Dumpnum (skip to) for multifile dump tapes. */
247 dumpnum
= strtol(optarg
, &p
, 10);
249 errx(1, "illegal dump number -- %s", optarg
);
251 errx(1, "dump number must be greater than 0");
263 if( !strcmp(optarg
,"-") ) {
268 if ( !(filelist
= fopen(optarg
,"r")) )
269 errx(1, "can't open file for reading -- %s", optarg
);
281 errx(1, "none of C, i, R, r, t or x options specified");
284 if (!mflag
&& tapeposflag
)
285 errx(1, "m and Q options are mutually exclusive");
288 if (signal(SIGINT
, onintr
) == SIG_IGN
)
289 (void) signal(SIGINT
, SIG_IGN
);
290 if (signal(SIGTERM
, onintr
) == SIG_IGN
)
291 (void) signal(SIGTERM
, SIG_IGN
);
296 if (command
== 'C' && inputdev
[0] != '/' && strcmp(inputdev
, "-")
298 && !strchr(inputdev
, ':')
301 /* since we chdir into the directory we are comparing
302 * to, we must retain the full tape path */
303 char wd
[MAXPATHLEN
], fullpathinput
[MAXPATHLEN
];
304 if (!getcwd(wd
, MAXPATHLEN
))
305 err(1, "can't get current directory");
306 snprintf(fullpathinput
, MAXPATHLEN
, "%s/%s", wd
, inputdev
);
307 fullpathinput
[MAXPATHLEN
- 1] = '\0';
308 setinput(fullpathinput
);
313 if (argc
== 0 && !filelist
) {
320 msg("reading QFA positions from %s\n", gTapeposfile
);
321 if ((gTapeposfp
= fopen(gTapeposfile
, "r")) == NULL
)
322 errx(1, "can't open file for reading -- %s",
324 /* start reading header info */
325 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
326 errx(1, "not requested format of -- %s", gTapeposfile
);
327 gTps
[strlen(gTps
) - 1] = 0; /* delete end of line */
328 if (strcmp(gTps
, QFA_MAGIC
) != 0)
329 errx(1, "not requested format of -- %s", gTapeposfile
);
330 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
331 errx(1, "not requested format of -- %s", gTapeposfile
);
332 gTps
[strlen(gTps
) - 1] = 0;
333 if (strcmp(gTps
, QFA_VERSION
) != 0)
334 errx(1, "not requested format of -- %s", gTapeposfile
);
336 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
337 errx(1, "not requested format of -- %s", gTapeposfile
);
338 gTps
[strlen(gTps
) - 1] = 0;
339 /* TODO: check dumpdate from QFA file with current dump file's
341 /* if not equal either output warning and continue without QFA
343 /* read empty line */
344 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
345 errx(1, "not requested format of -- %s", gTapeposfile
);
346 gTps
[strlen(gTps
) - 1] = 0;
347 /* read table header line */
348 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
349 errx(1, "not requested format of -- %s", gTapeposfile
);
350 gTps
[strlen(gTps
) - 1] = 0;
351 /* end reading header info */
352 /* tape position table starts here */
353 gSeekstart
= ftell(gTapeposfp
); /* remember for later use */
359 * Compare contents of tape.
364 Vprintf(stdout
, "Begin compare restore\n");
365 compare_ignore_not_found
= 0;
368 printf("filesys = %s\n", filesys
);
369 if (STAT(filesys
, &stbuf
) < 0)
370 err(1, "cannot stat directory %s", filesys
);
371 if (chdir(filesys
) < 0)
372 err(1, "cannot cd to %s", filesys
);
373 compare_ignore_not_found
= dumptime
> 0;
374 initsymtable((char *)0);
376 treescan(".", ROOTINO
, nodeupdates
);
379 if (compare_errors
) {
380 printf("Some files were modified!\n");
396 * Incremental restoration of a file system.
399 aflag
= 1; /* in -r or -R mode, -a is default */
403 * This is an incremental dump tape.
405 Vprintf(stdout
, "Begin incremental restore\n");
406 initsymtable(symtbl
);
409 Vprintf(stdout
, "Calculate node updates.\n");
410 treescan(".", ROOTINO
, nodeupdates
);
415 * This is a level zero dump tape.
417 Vprintf(stdout
, "Begin level 0 restore\n");
418 initsymtable((char *)0);
420 Vprintf(stdout
, "Calculate extraction list.\n");
421 treescan(".", ROOTINO
, nodeupdates
);
423 createleaves(symtbl
);
428 Vprintf(stdout
, "Verify the directory structure\n");
429 treescan(".", ROOTINO
, verifyfile
);
431 dumpsymtable(symtbl
, (long)1);
434 * Resume an incremental file system restoration.
437 aflag
= 1; /* in -r or -R mode, -a is default */
438 initsymtable(symtbl
);
441 createleaves(symtbl
);
445 dumpsymtable(symtbl
, (long)1);
448 /* handle file names from either text file (-X) or the command line */
449 #define NEXTFILE(p) \
455 else if (filelist) { \
456 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
457 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
458 *(p + strlen(p) - 1) = '\0'; \
459 if ( !*p ) /* skip empty lines */ \
465 * List contents of tape.
470 initsymtable((char *)0);
475 canon(p
, name
, sizeof(name
));
476 ino
= dirlookup(name
);
479 treescan(name
, ino
, listfile
);
483 * Batch extraction of tape contents.
487 tistart
= time(NULL
);
491 initsymtable((char *)0);
496 canon(p
, name
, sizeof(name
));
497 ino
= dirlookup(name
);
502 treescan(name
, ino
, addfile
);
511 titaken
= tiend
- tistart
;
512 msg("restore took %d:%02d:%02d\n", titaken
/ 3600,
513 (titaken
% 3600) / 60, titaken
% 60);
514 #endif /* USE_QFADEBUG */
519 return 0; /* gcc shut up */
526 const char *ext2ver
, *ext2date
;
528 ext2fs_get_library_version(&ext2ver
, &ext2date
);
529 (void)fprintf(stderr
, "%s %s (using libext2fs %s of %s)\n",
530 __progname
, _DUMP_VERSION
, ext2ver
, ext2date
);
532 (void)fprintf(stderr
, "%s %s\n", __progname
, _DUMP_VERSION
);
542 #define qfaflag "[-Q file] "
547 (void)fprintf(stderr
,
548 "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",
549 __progname
, " -C [-c" kerbflag
"lMvVy] [-b blocksize] [-D filesystem] [-f file] [-F script] [-L limit] [-s fileno]",
550 __progname
, " -i [-ach" kerbflag
"lmMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag
"[-s fileno]",
551 __progname
, " -r [-c" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script] [-s fileno] [-T directory]",
552 __progname
, " -R [-c" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script] [-s fileno] [-T directory]",
553 __progname
, " -t [-ch" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]",
554 __progname
, " -x [-ach" kerbflag
"lmMuvVy] [-b blocksize] [-f file] [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]");
560 * Change set of key letters and ordered arguments into something
561 * getopt(3) will like.
564 obsolete(int *argcp
, char **argvp
[])
567 char *ap
, **argv
, *flagsp
= NULL
, **nargv
, *p
= NULL
;
573 /* Return if no arguments or first argument has leading dash. */
575 if (argc
== 1 || *ap
== '-')
578 /* Allocate space for new arguments. */
579 if ((*argvp
= nargv
= malloc((argc
+ 1) * sizeof(char *))) == NULL
||
580 (p
= flagsp
= malloc(strlen(ap
) + 2)) == NULL
)
581 err(1, "malloc args");
584 argv
+= 2, argc
-= 2;
586 for (flags
= 0; *ap
; ++ap
) {
598 warnx("option requires an argument -- %c", *ap
);
601 if ((nargv
[0] = malloc(strlen(*argv
) + 2 + 1)) == NULL
)
602 err(1, "malloc arg");
605 (void)strcpy(&nargv
[0][2], *argv
);
619 /* Terminate flags. */
625 /* Copy remaining arguments. */
626 while ((*nargv
++ = *argv
++));
628 /* Update argument count. */
629 *argcp
= nargv
- *argvp
- 1;
634 * reserve stdin for opt (avoid conflicts)
637 use_stdin(const char *opt
)
640 errx(1, "can't handle standard input for both %s and %s",