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.41 2002/07/19 14:57:39 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, oflag = 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");
266 gTapeposfile = optarg;
272 /* Dumpnum (skip to) for multifile dump tapes. */
273 dumpnum = strtol(optarg, &p, 10);
275 errx(1, "illegal dump number -- %s", optarg);
277 errx(1, "dump number must be greater than 0");
289 if( !strcmp(optarg,"-") ) {
294 if ( !(filelist = fopen(optarg,"r")) )
295 errx(1, "can't open file for reading -- %s", optarg);
307 errx(1, "none of C, i, R, r, t or x options specified");
310 if (!mflag && tapeposflag)
311 errx(1, "m and Q options are mutually exclusive");
313 if (tapeposflag && command != 'i' && command != 'x' && command != 't')
314 errx(1, "Q option is not valid for %c command", command);
317 if (Afile && command != 'i' && command != 'x' && command != 't')
318 errx(1, "A option is not valid for %c command", command);
320 if (signal(SIGINT, onintr) == SIG_IGN)
321 (void) signal(SIGINT, SIG_IGN);
322 if (signal(SIGTERM, onintr) == SIG_IGN)
323 (void) signal(SIGTERM, SIG_IGN);
328 if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
330 && !strchr(inputdev, ':')
333 /* since we chdir into the directory we are comparing
334 * to, we must retain the full tape path */
335 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
336 if (!getcwd(wd, MAXPATHLEN))
337 err(1, "can't get current directory");
338 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
339 fullpathinput[MAXPATHLEN - 1] = '\0';
340 setinput(fullpathinput);
345 if (argc == 0 && !filelist) {
352 msg("reading QFA positions from %s\n", gTapeposfile);
353 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
354 errx(1, "can't open file for reading -- %s",
356 /* start reading header info */
357 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
358 errx(1, "not requested format of -- %s", gTapeposfile);
359 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
360 if (strcmp(gTps, QFA_MAGIC) != 0)
361 errx(1, "not requested format of -- %s", gTapeposfile);
362 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
363 errx(1, "not requested format of -- %s", gTapeposfile);
364 gTps[strlen(gTps) - 1] = 0;
365 if (strcmp(gTps, QFA_VERSION) != 0)
366 errx(1, "not requested format of -- %s", gTapeposfile);
368 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
369 errx(1, "not requested format of -- %s", gTapeposfile);
370 gTps[strlen(gTps) - 1] = 0;
371 qfadumpdate = atol(gTps);
372 /* read empty line */
373 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
374 errx(1, "not requested format of -- %s", gTapeposfile);
375 gTps[strlen(gTps) - 1] = 0;
376 /* read table header line */
377 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
378 errx(1, "not requested format of -- %s", gTapeposfile);
379 gTps[strlen(gTps) - 1] = 0;
380 /* end reading header info */
381 /* tape position table starts here */
382 gSeekstart = ftell(gTapeposfp); /* remember for later use */
388 * Compare contents of tape.
393 Vprintf(stdout, "Begin compare restore\n");
394 compare_ignore_not_found = 0;
397 printf("filesys = %s\n", filesys);
398 if (STAT(filesys, &stbuf) < 0)
399 err(1, "cannot stat directory %s", filesys);
400 if (chdir(filesys) < 0)
401 err(1, "cannot cd to %s", filesys);
402 compare_ignore_not_found = dumptime > 0;
403 initsymtable((char *)0);
405 treescan(".", ROOTINO, nodeupdates);
408 if (compare_errors) {
409 printf("Some files were modified!\n");
425 * Incremental restoration of a file system.
428 aflag = 1; /* in -r or -R mode, -a is default */
432 * This is an incremental dump tape.
434 Vprintf(stdout, "Begin incremental restore\n");
435 initsymtable(symtbl);
438 Vprintf(stdout, "Calculate node updates.\n");
439 treescan(".", ROOTINO, nodeupdates);
444 * This is a level zero dump tape.
446 Vprintf(stdout, "Begin level 0 restore\n");
447 initsymtable((char *)0);
449 Vprintf(stdout, "Calculate extraction list.\n");
450 treescan(".", ROOTINO, nodeupdates);
452 createleaves(symtbl);
457 Vprintf(stdout, "Verify the directory structure\n");
458 treescan(".", ROOTINO, verifyfile);
460 dumpsymtable(symtbl, (long)1);
463 * Resume an incremental file system restoration.
466 aflag = 1; /* in -r or -R mode, -a is default */
467 initsymtable(symtbl);
470 createleaves(symtbl);
474 dumpsymtable(symtbl, (long)1);
477 /* handle file names from either text file (-X) or the command line */
478 #define NEXTFILE(p) \
484 else if (filelist) { \
485 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
486 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
487 *(p + strlen(p) - 1) = '\0'; \
488 if ( !*p ) /* skip empty lines */ \
494 * List contents of tape.
499 initsymtable((char *)0);
505 canon(p, name, sizeof(name));
506 ino = dirlookup(name);
509 treescan(name, ino, listfile);
513 * Batch extraction of tape contents.
517 tistart = time(NULL);
521 initsymtable((char *)0);
526 canon(p, name, sizeof(name));
527 ino = dirlookup(name);
532 treescan(name, ino, addfile);
536 setdirmodes(oflag ? FORCE : 0);
541 titaken = tiend - tistart;
542 msg("restore took %d:%02d:%02d\n", titaken / 3600,
543 (titaken % 3600) / 60, titaken % 60);
544 #endif /* DEBUG_QFA */
549 tistart = time(NULL);
552 msg("writing QFA positions to %s\n", gTapeposfile);
553 if ((gTapeposfd = open(gTapeposfile, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0)
554 errx(1, "can't create tapeposfile\n");
555 /* print QFA-file header */
556 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION,(unsigned long)spcl.c_date);
557 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
558 errx(1, "can't write tapeposfile\n");
559 sprintf(gTps, "ino\ttapeno\ttapepos\n");
560 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
561 errx(1, "can't write tapeposfile\n");
564 initsymtable((char *)0);
569 canon(p, name, sizeof(name));
570 ino = dirlookup(name);
575 treescan(name, ino, addfile);
580 titaken = tiend - tistart;
581 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600,
582 (titaken % 3600) / 60, titaken % 60);
583 #endif /* DEBUG_QFA */
589 return 0; /* gcc shut up */
595 char white[MAXPATHLEN];
596 const char *ext2ver, *ext2date;
598 memset(white, ' ', MAXPATHLEN);
599 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
602 ext2fs_get_library_version(&ext2ver, &ext2date);
603 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
604 __progname, _DUMP_VERSION, ext2ver, ext2date);
606 (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
616 #define qfaflag "[-Q file] "
623 "\t%s -C [-cd" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
624 "\t%s [-F script] [-L limit] [-s fileno]\n"
625 "\t%s -i [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
626 "\t%s [-F script] " qfaflag "[-s fileno]\n"
628 "\t%s -P file [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
629 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
631 "\t%s -r [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
632 "\t%s [-s fileno] [-T directory]\n"
633 "\t%s -R [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
634 "\t%s [-s fileno] [-T directory]\n"
635 "\t%s -t [-cdh" kerbflag "lMuvVy] [-A file] [-b blocksize] [-f file]\n"
636 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
637 "\t%s -x [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
638 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
653 * Change set of key letters and ordered arguments into something
654 * getopt(3) will like.
657 obsolete(int *argcp, char **argvp[])
660 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
666 /* Return if no arguments or first argument has leading dash. */
668 if (argc == 1 || *ap == '-')
671 /* Allocate space for new arguments. */
672 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
673 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
674 err(1, "malloc args");
677 argv += 2, argc -= 2;
679 for (flags = 0; *ap; ++ap) {
692 warnx("option requires an argument -- %c", *ap);
695 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
696 err(1, "malloc arg");
699 (void)strcpy(&nargv[0][2], *argv);
713 /* Terminate flags. */
719 /* Copy remaining arguments. */
720 while ((*nargv++ = *argv++));
722 /* Update argument count. */
723 *argcp = nargv - *argvp - 1;
728 * reserve stdin for opt (avoid conflicts)
731 use_stdin(const char *opt)
734 errx(1, "can't handle standard input for both %s and %s",