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.39 2002/06/05 13:29:15 stelian Exp $";
48 #include <compatlfs.h>
50 #include <sys/param.h>
57 #ifdef HAVE_EXT2FS_EXT2_FS_H
58 #include <ext2fs/ext2_fs.h>
60 #include <linux/ext2_fs.h>
62 #include <bsdcompat.h>
66 #include <ufs/ufs/dinode.h>
67 #endif /* __linux__ */
68 #include <protocols/dumprestore.h>
70 #include <compaterr.h>
76 #include <ext2fs/ext2fs.h>
80 #include "pathnames.h"
84 int aflag = 0, bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
85 int hflag = 1, mflag = 1, Mflag = 0, Nflag = 0, Vflag = 0, zflag = 0;
86 int uflag = 0, lflag = 0, Lflag = 0;
100 int compare_ignore_not_found;
102 char filesys[NAMELEN];
103 static const char *stdin_opt = NULL;
104 char *bot_script = NULL;
105 dump_ino_t volinfo[TP_NINOS];
114 int createtapeposflag;
115 unsigned long qfadumpdate;
116 long long curtapepos;
119 #if defined(__linux__) || defined(sunos)
123 static void obsolete __P((int *, char **[]));
124 static void usage __P((void));
125 static void use_stdin __P((const char *));
128 main(int argc, char *argv[])
132 char *inputdev = _PATH_DEFTAPE;
133 char *symtbl = "./restoresymtable";
134 char *p, name[MAXPATHLEN];
135 FILE *filelist = NULL;
136 char fname[MAXPATHLEN];
138 time_t tistart, tiend, titaken;
142 createtapeposflag = 0;
145 /* Temp files should *not* be readable. We set permissions later. */
148 #if defined(__linux__) || defined(sunos)
149 __progname = argv[0];
155 if ((inputdev = getenv("TAPE")) == NULL)
156 inputdev = _PATH_DEFTAPE;
157 if ((tmpdir = getenv("TMPDIR")) == NULL)
159 if ((tmpdir = strdup(tmpdir)) == NULL)
160 err(1, "malloc tmpdir");
161 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
163 obsolete(&argc, &argv);
164 while ((ch = getopt(argc, argv,
173 "Rrs:tT:uvVxX:y")) != -1)
183 /* Change default tape blocksize. */
185 ntrec = strtol(optarg, &p, 10);
187 errx(1, "illegal blocksize -- %s", optarg);
189 errx(1, "block size must be greater than 0");
195 strncpy(filesys, optarg, NAMELEN);
196 filesys[NAMELEN - 1] = '\0';
205 if( !strcmp(optarg,"-") )
231 "%c and %c options are mutually exclusive",
236 gTapeposfile = optarg;
237 createtapeposflag = 1;
246 Lflag = strtol(optarg, &p, 10);
248 errx(1, "illegal limit -- %s", optarg);
250 errx(1, "limit must be greater than 0");
263 gTapeposfile = optarg;
269 /* Dumpnum (skip to) for multifile dump tapes. */
270 dumpnum = strtol(optarg, &p, 10);
272 errx(1, "illegal dump number -- %s", optarg);
274 errx(1, "dump number must be greater than 0");
286 if( !strcmp(optarg,"-") ) {
291 if ( !(filelist = fopen(optarg,"r")) )
292 errx(1, "can't open file for reading -- %s", optarg);
304 errx(1, "none of C, i, R, r, t or x options specified");
307 if (!mflag && tapeposflag)
308 errx(1, "m and Q options are mutually exclusive");
310 if (tapeposflag && command != 'i' && command != 'x' && command != 't')
311 errx(1, "Q option is not valid for %c command", command);
314 if (Afile && command != 'i' && command != 'x' && command != 't')
315 errx(1, "A option is not valid for %c command", command);
317 if (signal(SIGINT, onintr) == SIG_IGN)
318 (void) signal(SIGINT, SIG_IGN);
319 if (signal(SIGTERM, onintr) == SIG_IGN)
320 (void) signal(SIGTERM, SIG_IGN);
325 if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
327 && !strchr(inputdev, ':')
330 /* since we chdir into the directory we are comparing
331 * to, we must retain the full tape path */
332 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
333 if (!getcwd(wd, MAXPATHLEN))
334 err(1, "can't get current directory");
335 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
336 fullpathinput[MAXPATHLEN - 1] = '\0';
337 setinput(fullpathinput);
342 if (argc == 0 && !filelist) {
349 msg("reading QFA positions from %s\n", gTapeposfile);
350 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
351 errx(1, "can't open file for reading -- %s",
353 /* start reading header info */
354 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
355 errx(1, "not requested format of -- %s", gTapeposfile);
356 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
357 if (strcmp(gTps, QFA_MAGIC) != 0)
358 errx(1, "not requested format of -- %s", gTapeposfile);
359 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
360 errx(1, "not requested format of -- %s", gTapeposfile);
361 gTps[strlen(gTps) - 1] = 0;
362 if (strcmp(gTps, QFA_VERSION) != 0)
363 errx(1, "not requested format of -- %s", gTapeposfile);
365 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
366 errx(1, "not requested format of -- %s", gTapeposfile);
367 gTps[strlen(gTps) - 1] = 0;
368 qfadumpdate = atol(gTps);
369 /* read empty line */
370 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
371 errx(1, "not requested format of -- %s", gTapeposfile);
372 gTps[strlen(gTps) - 1] = 0;
373 /* read table header line */
374 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
375 errx(1, "not requested format of -- %s", gTapeposfile);
376 gTps[strlen(gTps) - 1] = 0;
377 /* end reading header info */
378 /* tape position table starts here */
379 gSeekstart = ftell(gTapeposfp); /* remember for later use */
385 * Compare contents of tape.
390 Vprintf(stdout, "Begin compare restore\n");
391 compare_ignore_not_found = 0;
394 printf("filesys = %s\n", filesys);
395 if (STAT(filesys, &stbuf) < 0)
396 err(1, "cannot stat directory %s", filesys);
397 if (chdir(filesys) < 0)
398 err(1, "cannot cd to %s", filesys);
399 compare_ignore_not_found = dumptime > 0;
400 initsymtable((char *)0);
402 treescan(".", ROOTINO, nodeupdates);
405 if (compare_errors) {
406 printf("Some files were modified!\n");
422 * Incremental restoration of a file system.
425 aflag = 1; /* in -r or -R mode, -a is default */
429 * This is an incremental dump tape.
431 Vprintf(stdout, "Begin incremental restore\n");
432 initsymtable(symtbl);
435 Vprintf(stdout, "Calculate node updates.\n");
436 treescan(".", ROOTINO, nodeupdates);
441 * This is a level zero dump tape.
443 Vprintf(stdout, "Begin level 0 restore\n");
444 initsymtable((char *)0);
446 Vprintf(stdout, "Calculate extraction list.\n");
447 treescan(".", ROOTINO, nodeupdates);
449 createleaves(symtbl);
454 Vprintf(stdout, "Verify the directory structure\n");
455 treescan(".", ROOTINO, verifyfile);
457 dumpsymtable(symtbl, (long)1);
460 * Resume an incremental file system restoration.
463 aflag = 1; /* in -r or -R mode, -a is default */
464 initsymtable(symtbl);
467 createleaves(symtbl);
471 dumpsymtable(symtbl, (long)1);
474 /* handle file names from either text file (-X) or the command line */
475 #define NEXTFILE(p) \
481 else if (filelist) { \
482 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
483 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
484 *(p + strlen(p) - 1) = '\0'; \
485 if ( !*p ) /* skip empty lines */ \
491 * List contents of tape.
496 initsymtable((char *)0);
502 canon(p, name, sizeof(name));
503 ino = dirlookup(name);
506 treescan(name, ino, listfile);
510 * Batch extraction of tape contents.
514 tistart = time(NULL);
518 initsymtable((char *)0);
523 canon(p, name, sizeof(name));
524 ino = dirlookup(name);
529 treescan(name, ino, addfile);
538 titaken = tiend - tistart;
539 msg("restore took %d:%02d:%02d\n", titaken / 3600,
540 (titaken % 3600) / 60, titaken % 60);
541 #endif /* DEBUG_QFA */
546 tistart = time(NULL);
549 msg("writing QFA positions to %s\n", gTapeposfile);
550 if ((gTapeposfd = open(gTapeposfile, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0)
551 errx(1, "can't create tapeposfile\n");
552 /* print QFA-file header */
553 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION,(unsigned long)spcl.c_date);
554 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps))
555 errx(1, "can't write tapeposfile\n");
556 sprintf(gTps, "ino\ttapeno\ttapepos\n");
557 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps))
558 errx(1, "can't write tapeposfile\n");
561 initsymtable((char *)0);
566 canon(p, name, sizeof(name));
567 ino = dirlookup(name);
572 treescan(name, ino, addfile);
577 titaken = tiend - tistart;
578 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600,
579 (titaken % 3600) / 60, titaken % 60);
580 #endif /* DEBUG_QFA */
586 return 0; /* gcc shut up */
592 char white[MAXPATHLEN];
593 const char *ext2ver, *ext2date;
595 memset(white, ' ', MAXPATHLEN);
596 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
599 ext2fs_get_library_version(&ext2ver, &ext2date);
600 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
601 __progname, _DUMP_VERSION, ext2ver, ext2date);
603 (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
613 #define qfaflag "[-Q file] "
620 "\t%s -C [-cd" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
621 "\t%s [-F script] [-L limit] [-s fileno]\n"
622 "\t%s -i [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize] [-f file]\n"
623 "\t%s [-F script] " qfaflag "[-s fileno]\n"
625 "\t%s -P file [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
626 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
628 "\t%s -r [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
629 "\t%s [-s fileno] [-T directory]\n"
630 "\t%s -R [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
631 "\t%s [-s fileno] [-T directory]\n"
632 "\t%s -t [-cdh" kerbflag "lMuvVy] [-A file] [-b blocksize] [-f file]\n"
633 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
634 "\t%s -x [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize] [-f file]\n"
635 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
650 * Change set of key letters and ordered arguments into something
651 * getopt(3) will like.
654 obsolete(int *argcp, char **argvp[])
657 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
663 /* Return if no arguments or first argument has leading dash. */
665 if (argc == 1 || *ap == '-')
668 /* Allocate space for new arguments. */
669 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
670 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
671 err(1, "malloc args");
674 argv += 2, argc -= 2;
676 for (flags = 0; *ap; ++ap) {
689 warnx("option requires an argument -- %c", *ap);
692 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
693 err(1, "malloc arg");
696 (void)strcpy(&nargv[0][2], *argv);
710 /* Terminate flags. */
716 /* Copy remaining arguments. */
717 while ((*nargv++ = *argv++));
719 /* Update argument count. */
720 *argcp = nargv - *argvp - 1;
725 * reserve stdin for opt (avoid conflicts)
728 use_stdin(const char *opt)
731 errx(1, "can't handle standard input for both %s and %s",