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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char rcsid[] =
40 "$Id: main.c,v 1.45 2003/10/26 16:05:48 stelian Exp $";
44 #include <compatlfs.h>
45 #include <sys/types.h>
47 #include <sys/param.h>
54 #ifdef HAVE_EXT2FS_EXT2_FS_H
55 #include <ext2fs/ext2_fs.h>
57 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
66 #include <sys/fcntl.h>
67 #include <bsdcompat.h>
70 #include <ufs/ufs/dinode.h>
72 #endif /* __linux__ */
73 #include <protocols/dumprestore.h>
75 #include <compaterr.h>
81 #include <ext2fs/ext2fs.h>
85 #include "pathnames.h"
89 int abortifconnerr = 1; /* set to 1 if lib dumprmt.o should exit on connection errors
90 otherwise just print a message using msg */
92 int aflag = 0, bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
93 int hflag = 1, mflag = 1, Mflag = 0, Nflag = 0, Vflag = 0, zflag = 0;
94 int uflag = 0, lflag = 0, Lflag = 0, oflag = 0;
101 char *dumpmap = NULL;
102 char *usedinomap = NULL;
108 int compare_ignore_not_found;
110 char filesys[NAMELEN];
111 static const char *stdin_opt = NULL;
112 char *bot_script = NULL;
113 dump_ino_t volinfo[TP_NINOS];
122 int createtapeposflag;
123 unsigned long qfadumpdate;
124 long long curtapepos;
129 #if defined(__linux__) || defined(sunos)
133 static void obsolete __P((int *, char **[]));
134 static void usage __P((void));
135 static void use_stdin __P((const char *));
137 #define FORCED_UMASK (077)
140 main(int argc, char *argv[])
144 char *inputdev = _PATH_DEFTAPE;
145 char *symtbl = "./restoresymtable";
146 char *p, name[MAXPATHLEN];
147 FILE *filelist = NULL;
148 char fname[MAXPATHLEN];
151 time_t tistart, tiend, titaken;
155 createtapeposflag = 0;
158 /* Temp files should *not* be readable. We set permissions later. */
159 orig_umask = umask(FORCED_UMASK);
161 #if defined(__linux__) || defined(sunos)
162 __progname = argv[0];
168 if ((inputdev = getenv("TAPE")) == NULL)
169 inputdev = _PATH_DEFTAPE;
170 if ((tmpdir = getenv("TMPDIR")) == NULL)
172 if ((tmpdir = strdup(tmpdir)) == NULL)
173 err(1, "malloc tmpdir");
174 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
176 obsolete(&argc, &argv);
177 while ((ch = getopt(argc, argv,
186 "Rrs:tT:uvVxX:y")) != -1)
196 /* Change default tape blocksize. */
198 ntrec = strtol(optarg, &p, 10);
200 errx(1, "illegal blocksize -- %s", optarg);
202 errx(1, "block size must be greater than 0");
208 strncpy(filesys, optarg, NAMELEN);
209 filesys[NAMELEN - 1] = '\0';
218 if( !strcmp(optarg,"-") )
244 "%c and %c options are mutually exclusive",
249 gTapeposfile = optarg;
250 createtapeposflag = 1;
259 Lflag = strtol(optarg, &p, 10);
261 errx(1, "illegal limit -- %s", optarg);
263 errx(1, "limit must be greater than 0");
279 gTapeposfile = optarg;
285 /* Dumpnum (skip to) for multifile dump tapes. */
286 dumpnum = strtol(optarg, &p, 10);
288 errx(1, "illegal dump number -- %s", optarg);
290 errx(1, "dump number must be greater than 0");
302 if( !strcmp(optarg,"-") ) {
307 if ( !(filelist = fopen(optarg,"r")) )
308 errx(1, "can't open file for reading -- %s", optarg);
320 errx(1, "none of C, i, R, r, t or x options specified");
323 if (!mflag && tapeposflag)
324 errx(1, "m and Q options are mutually exclusive");
326 if (tapeposflag && command != 'i' && command != 'x' && command != 't')
327 errx(1, "Q option is not valid for %c command", command);
330 if (Afile && command != 'i' && command != 'x' && command != 't')
331 errx(1, "A option is not valid for %c command", command);
333 if (signal(SIGINT, onintr) == SIG_IGN)
334 (void) signal(SIGINT, SIG_IGN);
335 if (signal(SIGTERM, onintr) == SIG_IGN)
336 (void) signal(SIGTERM, SIG_IGN);
341 if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
343 && !strchr(inputdev, ':')
346 /* since we chdir into the directory we are comparing
347 * to, we must retain the full tape path */
348 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
349 if (!getcwd(wd, MAXPATHLEN))
350 err(1, "can't get current directory");
351 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
352 fullpathinput[MAXPATHLEN - 1] = '\0';
353 setinput(fullpathinput);
358 if (argc == 0 && !filelist) {
365 msg("reading QFA positions from %s\n", gTapeposfile);
366 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
367 errx(1, "can't open file for reading -- %s",
369 /* start reading header info */
370 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
371 errx(1, "not requested format of -- %s", gTapeposfile);
372 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
373 if (strcmp(gTps, QFA_MAGIC) != 0)
374 errx(1, "not requested format of -- %s", gTapeposfile);
375 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
376 errx(1, "not requested format of -- %s", gTapeposfile);
377 gTps[strlen(gTps) - 1] = 0;
378 if (strcmp(gTps, QFA_VERSION) != 0)
379 errx(1, "not requested format of -- %s", gTapeposfile);
381 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
382 errx(1, "not requested format of -- %s", gTapeposfile);
383 gTps[strlen(gTps) - 1] = 0;
384 qfadumpdate = atol(gTps);
385 /* read empty line */
386 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
387 errx(1, "not requested format of -- %s", gTapeposfile);
388 gTps[strlen(gTps) - 1] = 0;
389 /* read table header line */
390 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
391 errx(1, "not requested format of -- %s", gTapeposfile);
392 gTps[strlen(gTps) - 1] = 0;
393 /* end reading header info */
394 /* tape position table starts here */
395 gSeekstart = ftell(gTapeposfp); /* remember for later use */
397 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
398 errx(1, "can't get SCSI-ID for %s\n", inputdev);
401 errx(1, "can't get SCSI-ID for %s\n", inputdev);
403 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
404 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
405 errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
413 * Compare contents of tape.
418 Vprintf(stdout, "Begin compare restore\n");
419 compare_ignore_not_found = 0;
422 printf("filesys = %s\n", filesys);
423 if (STAT(filesys, &stbuf) < 0)
424 err(1, "cannot stat directory %s", filesys);
425 if (chdir(filesys) < 0)
426 err(1, "cannot cd to %s", filesys);
427 compare_ignore_not_found = dumptime > 0;
428 initsymtable((char *)0);
430 treescan(".", ROOTINO, nodeupdates);
433 if (compare_errors) {
434 printf("Some files were modified!\n");
450 * Incremental restoration of a file system.
453 aflag = 1; /* in -r or -R mode, -a is default */
457 * This is an incremental dump tape.
459 Vprintf(stdout, "Begin incremental restore\n");
460 initsymtable(symtbl);
463 Vprintf(stdout, "Calculate node updates.\n");
464 treescan(".", ROOTINO, nodeupdates);
469 * This is a level zero dump tape.
471 Vprintf(stdout, "Begin level 0 restore\n");
472 initsymtable((char *)0);
474 Vprintf(stdout, "Calculate extraction list.\n");
475 treescan(".", ROOTINO, nodeupdates);
477 createleaves(symtbl);
482 Vprintf(stdout, "Verify the directory structure\n");
483 treescan(".", ROOTINO, verifyfile);
485 dumpsymtable(symtbl, (long)1);
488 * Resume an incremental file system restoration.
491 aflag = 1; /* in -r or -R mode, -a is default */
492 initsymtable(symtbl);
495 createleaves(symtbl);
499 dumpsymtable(symtbl, (long)1);
502 /* handle file names from either text file (-X) or the command line */
503 #define NEXTFILE(p) \
509 else if (filelist) { \
510 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
511 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
512 *(p + strlen(p) - 1) = '\0'; \
513 if ( !*p ) /* skip empty lines */ \
519 * List contents of tape.
524 initsymtable((char *)0);
530 canon(p, name, sizeof(name));
531 ino = dirlookup(name);
534 treescan(name, ino, listfile);
538 * Batch extraction of tape contents.
542 tistart = time(NULL);
546 initsymtable((char *)0);
551 canon(p, name, sizeof(name));
552 ino = dirlookup(name);
557 treescan(name, ino, addfile);
561 setdirmodes(oflag ? FORCE : 0);
571 titaken = tiend - tistart;
572 msg("restore took %d:%02d:%02d\n", titaken / 3600,
573 (titaken % 3600) / 60, titaken % 60);
574 #endif /* DEBUG_QFA */
579 tistart = time(NULL);
582 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
583 errx(1, "can't get SCSI-ID for %s\n", inputdev);
586 errx(1, "can't get SCSI-ID for %s\n", inputdev);
588 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
589 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
590 errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
594 msg("writing QFA positions to %s\n", gTapeposfile);
595 (void) umask(orig_umask);
596 if ((gTapeposfd = open(gTapeposfile, O_WRONLY|O_CREAT|O_TRUNC,
597 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
598 |S_IROTH|S_IWOTH)) < 0)
599 errx(1, "can't create tapeposfile\n");
600 (void) umask(FORCED_UMASK);
601 /* print QFA-file header */
602 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
603 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
604 errx(1, "can't write tapeposfile\n");
605 sprintf(gTps, "ino\ttapeno\ttapepos\n");
606 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
607 errx(1, "can't write tapeposfile\n");
610 initsymtable((char *)0);
615 canon(p, name, sizeof(name));
616 ino = dirlookup(name);
621 treescan(name, ino, addfile);
631 titaken = tiend - tistart;
632 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600,
633 (titaken % 3600) / 60, titaken % 60);
634 #endif /* DEBUG_QFA */
640 return 0; /* gcc shut up */
646 char white[MAXPATHLEN];
647 const char *ext2ver, *ext2date;
649 memset(white, ' ', MAXPATHLEN);
650 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
653 ext2fs_get_library_version(&ext2ver, &ext2date);
654 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
655 __progname, _DUMP_VERSION, ext2ver, ext2date);
657 (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
667 #define qfaflag "[-Q file] "
674 "\t%s -C [-cd" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
675 "\t%s [-F script] [-L limit] [-s fileno]\n"
676 "\t%s -i [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
677 "\t%s [-F script] " qfaflag "[-s fileno]\n"
679 "\t%s -P file [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
680 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
682 "\t%s -r [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
683 "\t%s [-s fileno] [-T directory]\n"
684 "\t%s -R [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
685 "\t%s [-s fileno] [-T directory]\n"
686 "\t%s -t [-cdh" kerbflag "lMuvVy] [-A file] [-b blocksize] [-f file]\n"
687 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
688 "\t%s -x [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
689 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
704 * Change set of key letters and ordered arguments into something
705 * getopt(3) will like.
708 obsolete(int *argcp, char **argvp[])
711 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
717 /* Return if no arguments or first argument has leading dash. */
719 if (argc == 1 || *ap == '-')
722 /* Allocate space for new arguments. */
723 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
724 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
725 err(1, "malloc args");
728 argv += 2, argc -= 2;
730 for (flags = 0; *ap; ++ap) {
743 warnx("option requires an argument -- %c", *ap);
746 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
747 err(1, "malloc arg");
750 (void)strcpy(&nargv[0][2], *argv);
764 /* Terminate flags. */
770 /* Copy remaining arguments. */
771 while ((*nargv++ = *argv++));
773 /* Update argument count. */
774 *argcp = nargv - *argvp - 1;
779 * reserve stdin for opt (avoid conflicts)
782 use_stdin(const char *opt)
785 errx(1, "can't handle standard input for both %s and %s",