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.49 2005/01/14 13:04:56 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;
102 char *dumpmap = NULL;
103 char *usedinomap = NULL;
109 int compare_ignore_not_found;
111 char filesys[NAMELEN];
112 static const char *stdin_opt = NULL;
113 char *bot_script = NULL;
114 dump_ino_t volinfo[TP_NINOS];
124 int createtapeposflag;
125 unsigned long qfadumpdate;
126 long long curtapepos;
131 #if defined(__linux__) || defined(sunos)
135 static void obsolete __P((int *, char **[]));
136 static void usage __P((void));
137 static void use_stdin __P((const char *));
139 #define FORCED_UMASK (077)
142 main(int argc, char *argv[])
146 char *inputdev = _PATH_DEFTAPE;
147 char *symtbl = "./restoresymtable";
148 char *p, name[MAXPATHLEN];
149 FILE *filelist = NULL;
150 char fname[MAXPATHLEN];
153 time_t tistart, tiend, titaken;
157 createtapeposflag = 0;
160 /* Temp files should *not* be readable. We set permissions later. */
161 orig_umask = umask(FORCED_UMASK);
163 #if defined(__linux__) || defined(sunos)
164 __progname = argv[0];
170 if ((inputdev = getenv("TAPE")) == NULL)
171 inputdev = _PATH_DEFTAPE;
172 if ((tmpdir = getenv("TMPDIR")) == NULL)
174 if ((tmpdir = strdup(tmpdir)) == NULL)
175 err(1, "malloc tmpdir");
176 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
178 obsolete(&argc, &argv);
179 while ((ch = getopt(argc, argv,
188 "Rrs:tT:uvVxX:y")) != -1)
198 /* Change default tape blocksize. */
200 ntrec = strtol(optarg, &p, 10);
202 errx(1, "illegal blocksize -- %s", optarg);
204 errx(1, "block size must be greater than 0");
210 strncpy(filesys, optarg, NAMELEN);
211 filesys[NAMELEN - 1] = '\0';
220 if( !strcmp(optarg,"-") )
246 "%c and %c options are mutually exclusive",
251 gTapeposfile = optarg;
252 createtapeposflag = 1;
261 Lflag = strtol(optarg, &p, 10);
263 errx(1, "illegal limit -- %s", optarg);
265 errx(1, "limit must be greater than 0");
281 gTapeposfile = optarg;
287 /* Dumpnum (skip to) for multifile dump tapes. */
288 dumpnum = strtol(optarg, &p, 10);
290 errx(1, "illegal dump number -- %s", optarg);
292 errx(1, "dump number must be greater than 0");
304 if( !strcmp(optarg,"-") ) {
309 if ( !(filelist = fopen(optarg,"r")) )
310 errx(1, "can't open file for reading -- %s", optarg);
322 errx(1, "none of C, i, R, r, t or x options specified");
325 if (!mflag && tapeposflag)
326 errx(1, "m and Q options are mutually exclusive");
328 if (tapeposflag && command != 'i' && command != 'x' && command != 't')
329 errx(1, "Q option is not valid for %c command", command);
332 if (Afile && command != 'i' && command != 'x' && command != 't')
333 errx(1, "A option is not valid for %c command", command);
335 if (signal(SIGINT, onintr) == SIG_IGN)
336 (void) signal(SIGINT, SIG_IGN);
337 if (signal(SIGTERM, onintr) == SIG_IGN)
338 (void) signal(SIGTERM, SIG_IGN);
343 if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
345 && !strchr(inputdev, ':')
348 /* since we chdir into the directory we are comparing
349 * to, we must retain the full tape path */
350 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
351 if (!getcwd(wd, MAXPATHLEN))
352 err(1, "can't get current directory");
353 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
354 fullpathinput[MAXPATHLEN - 1] = '\0';
355 setinput(fullpathinput);
360 wdfd = open(".", O_RDONLY);
362 err(1, "can't get current directory");
364 if (argc == 0 && !filelist) {
371 msg("reading QFA positions from %s\n", gTapeposfile);
372 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
373 errx(1, "can't open file for reading -- %s",
375 /* start reading header info */
376 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
377 errx(1, "not requested format of -- %s", gTapeposfile);
378 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
379 if (strcmp(gTps, QFA_MAGIC) != 0)
380 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 if (strcmp(gTps, QFA_VERSION) != 0)
385 errx(1, "not requested format of -- %s", gTapeposfile);
387 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
388 errx(1, "not requested format of -- %s", gTapeposfile);
389 gTps[strlen(gTps) - 1] = 0;
390 qfadumpdate = atol(gTps);
391 /* read empty line */
392 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
393 errx(1, "not requested format of -- %s", gTapeposfile);
394 gTps[strlen(gTps) - 1] = 0;
395 /* read table header line */
396 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
397 errx(1, "not requested format of -- %s", gTapeposfile);
398 gTps[strlen(gTps) - 1] = 0;
399 /* end reading header info */
400 /* tape position table starts here */
401 gSeekstart = ftell(gTapeposfp); /* remember for later use */
403 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
404 errx(1, "can't get SCSI-ID for %s\n", inputdev);
407 errx(1, "can't get SCSI-ID for %s\n", inputdev);
409 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
410 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
411 errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
419 * Compare contents of tape.
424 Vprintf(stdout, "Begin compare restore\n");
425 compare_ignore_not_found = 0;
429 printf("filesys = %s\n", filesys);
430 if (STAT(filesys, &stbuf) < 0)
431 err(1, "cannot stat directory %s", filesys);
432 if (chdir(filesys) < 0)
433 err(1, "cannot cd to %s", filesys);
434 compare_ignore_not_found = dumptime > 0;
435 initsymtable((char *)0);
437 treescan(".", ROOTINO, nodeupdates);
441 if (compare_errors) {
442 printf("Some files were modified!\n");
458 * Incremental restoration of a file system.
461 aflag = 1; /* in -r or -R mode, -a is default */
465 * This is an incremental dump tape.
467 Vprintf(stdout, "Begin incremental restore\n");
468 initsymtable(symtbl);
471 Vprintf(stdout, "Calculate node updates.\n");
472 treescan(".", ROOTINO, nodeupdates);
477 * This is a level zero dump tape.
479 Vprintf(stdout, "Begin level 0 restore\n");
480 initsymtable((char *)0);
482 Vprintf(stdout, "Calculate extraction list.\n");
483 treescan(".", ROOTINO, nodeupdates);
485 createleaves(symtbl);
490 Vprintf(stdout, "Verify the directory structure\n");
491 treescan(".", ROOTINO, verifyfile);
493 dumpsymtable(symtbl, (long)1);
496 * Resume an incremental file system restoration.
499 aflag = 1; /* in -r or -R mode, -a is default */
500 initsymtable(symtbl);
503 createleaves(symtbl);
507 dumpsymtable(symtbl, (long)1);
510 /* handle file names from either text file (-X) or the command line */
511 #define NEXTFILE(p) \
517 else if (filelist) { \
518 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
519 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
520 *(p + strlen(p) - 1) = '\0'; \
521 if ( !*p ) /* skip empty lines */ \
527 * List contents of tape.
532 initsymtable((char *)0);
538 canon(p, name, sizeof(name));
539 ino = dirlookup(name);
542 treescan(name, ino, listfile);
546 * Batch extraction of tape contents.
550 tistart = time(NULL);
554 initsymtable((char *)0);
559 canon(p, name, sizeof(name));
560 ino = dirlookup(name);
565 treescan(name, ino, addfile);
569 setdirmodes(oflag ? FORCE : 0);
579 titaken = tiend - tistart;
580 msg("restore took %d:%02d:%02d\n", titaken / 3600,
581 (titaken % 3600) / 60, titaken % 60);
582 #endif /* DEBUG_QFA */
587 tistart = time(NULL);
590 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
591 errx(1, "can't get SCSI-ID for %s\n", inputdev);
594 errx(1, "can't get SCSI-ID for %s\n", inputdev);
596 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
597 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
598 errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
602 msg("writing QFA positions to %s\n", gTapeposfile);
603 (void) umask(orig_umask);
604 if ((gTapeposfd = open(gTapeposfile, O_WRONLY|O_CREAT|O_TRUNC,
605 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
606 |S_IROTH|S_IWOTH)) < 0)
607 errx(1, "can't create tapeposfile\n");
608 (void) umask(FORCED_UMASK);
609 /* print QFA-file header */
610 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
611 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
612 errx(1, "can't write tapeposfile\n");
613 sprintf(gTps, "ino\ttapeno\ttapepos\n");
614 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
615 errx(1, "can't write tapeposfile\n");
618 initsymtable((char *)0);
623 canon(p, name, sizeof(name));
624 ino = dirlookup(name);
629 treescan(name, ino, addfile);
639 titaken = tiend - tistart;
640 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600,
641 (titaken % 3600) / 60, titaken % 60);
642 #endif /* DEBUG_QFA */
648 return 0; /* gcc shut up */
654 char white[MAXPATHLEN];
655 const char *ext2ver, *ext2date;
657 memset(white, ' ', MAXPATHLEN);
658 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
661 ext2fs_get_library_version(&ext2ver, &ext2date);
662 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
663 __progname, _DUMP_VERSION, ext2ver, ext2date);
665 (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
675 #define qfaflag "[-Q file] "
682 "\t%s -C [-cd" kerbflag "lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
683 "\t%s [-F script] [-L limit] [-s fileno]\n"
684 "\t%s -i [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
685 "\t%s [-F script] " qfaflag "[-s fileno]\n"
687 "\t%s -P file [-acdh" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
688 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
690 "\t%s -r [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
691 "\t%s [-s fileno] [-T directory]\n"
692 "\t%s -R [-cd" kerbflag "lMuvVy] [-b blocksize] [-f file] [-F script]\n"
693 "\t%s [-s fileno] [-T directory]\n"
694 "\t%s -t [-cdh" kerbflag "lMuvVy] [-A file] [-b blocksize] [-f file]\n"
695 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
696 "\t%s -x [-acdh" kerbflag "lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
697 "\t%s [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
712 * Change set of key letters and ordered arguments into something
713 * getopt(3) will like.
716 obsolete(int *argcp, char **argvp[])
719 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
725 /* Return if no arguments or first argument has leading dash. */
727 if (argc == 1 || *ap == '-')
730 /* Allocate space for new arguments. */
731 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
732 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
733 err(1, "malloc args");
736 argv += 2, argc -= 2;
738 for (flags = 0; *ap; ++ap) {
751 warnx("option requires an argument -- %c", *ap);
754 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
755 err(1, "malloc arg");
758 (void)strcpy(&nargv[0][2], *argv);
772 /* Terminate flags. */
778 /* Copy remaining arguments. */
779 while ((*nargv++ = *argv++));
781 /* Update argument count. */
782 *argcp = nargv - *argvp - 1;
787 * reserve stdin for opt (avoid conflicts)
790 use_stdin(const char *opt)
793 errx(1, "can't handle standard input for both %s and %s",