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.53 2009/06/18 09:42:12 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];
116 int dirhash_size = 1;
125 int createtapeposflag;
126 unsigned long qfadumpdate;
127 long long curtapepos;
130 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
131 int transselinuxflag = 0;
132 char *transselinuxarg = NULL;
137 #if defined(__linux__) || defined(sunos)
141 static void obsolete __P((int *, char **[]));
142 static void usage __P((void));
143 static void use_stdin __P((const char *));
145 #define FORCED_UMASK (077)
148 main(int argc, char *argv[])
152 char *inputdev = _PATH_DEFTAPE;
153 char *symtbl = "./restoresymtable";
154 char *p, name[MAXPATHLEN];
155 FILE *filelist = NULL;
156 char fname[MAXPATHLEN];
159 time_t tistart, tiend, titaken;
163 createtapeposflag = 0;
165 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
166 char transselinuxopt;
169 /* Temp files should *not* be readable. We set permissions later. */
170 orig_umask = umask(FORCED_UMASK);
172 #if defined(__linux__) || defined(sunos)
173 __progname = argv[0];
179 if ((inputdev = getenv("TAPE")) == NULL)
180 inputdev = _PATH_DEFTAPE;
181 if ((tmpdir = getenv("TMPDIR")) == NULL)
183 if ((tmpdir = strdup(tmpdir)) == NULL)
184 err(1, "malloc tmpdir");
185 for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
187 obsolete(&argc, &argv);
188 while ((ch = getopt(argc, argv,
190 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
201 "Rrs:tT:uvVxX:y")) != -1)
211 /* Change default tape blocksize. */
213 ntrec = strtol(optarg, &p, 10);
215 errx(1, "illegal blocksize -- %s", optarg);
217 errx(1, "block size must be greater than 0");
223 strncpy(filesys, optarg, NAMELEN);
224 filesys[NAMELEN - 1] = '\0';
226 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
228 transselinuxflag = 1;
229 transselinuxopt = ch;
232 transselinuxflag = 1;
233 transselinuxarg = optarg;
234 transselinuxopt = ch;
244 if( !strcmp(optarg,"-") )
255 dirhash_size = strtol(optarg, &p, 10);
257 errx(1, "illegal hash size -- %s", optarg);
258 if (dirhash_size < 1)
259 errx(1, "hash size must be greater than 0");
277 "%c and %c options are mutually exclusive",
282 gTapeposfile = optarg;
283 createtapeposflag = 1;
292 Lflag = strtol(optarg, &p, 10);
294 errx(1, "illegal limit -- %s", optarg);
296 errx(1, "limit must be greater than 0");
312 gTapeposfile = optarg;
318 /* Dumpnum (skip to) for multifile dump tapes. */
319 dumpnum = strtol(optarg, &p, 10);
321 errx(1, "illegal dump number -- %s", optarg);
323 errx(1, "dump number must be greater than 0");
335 if( !strcmp(optarg,"-") ) {
340 if ( !(filelist = fopen(optarg,"r")) )
341 errx(1, "can't open file for reading -- %s", optarg);
353 errx(1, "none of C, i, R, r, t or x options specified");
356 if (!mflag && tapeposflag)
357 errx(1, "m and Q options are mutually exclusive");
359 if (tapeposflag && command != 'i' && command != 'x' && command != 't')
360 errx(1, "Q option is not valid for %c command", command);
363 if (Afile && command != 'i' && command != 'x' && command != 't')
364 errx(1, "A option is not valid for %c command", command);
366 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
367 if (transselinuxflag && !strchr("CirRx", command))
368 errx(1, "%c option is not valid for %c command", transselinuxopt, command);
371 if (signal(SIGINT, onintr) == SIG_IGN)
372 (void) signal(SIGINT, SIG_IGN);
373 if (signal(SIGTERM, onintr) == SIG_IGN)
374 (void) signal(SIGTERM, SIG_IGN);
379 if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
381 && !strchr(inputdev, ':')
384 /* since we chdir into the directory we are comparing
385 * to, we must retain the full tape path */
386 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
387 if (!getcwd(wd, MAXPATHLEN))
388 err(1, "can't get current directory");
389 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
390 fullpathinput[MAXPATHLEN - 1] = '\0';
391 setinput(fullpathinput);
396 wdfd = open(".", O_RDONLY);
398 err(1, "can't get current directory");
400 if (argc == 0 && !filelist) {
407 msg("reading QFA positions from %s\n", gTapeposfile);
408 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
409 errx(1, "can't open file for reading -- %s",
411 /* start reading header info */
412 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
413 errx(1, "not requested format of -- %s", gTapeposfile);
414 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
415 if (strcmp(gTps, QFA_MAGIC) != 0)
416 errx(1, "not requested format of -- %s", gTapeposfile);
417 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
418 errx(1, "not requested format of -- %s", gTapeposfile);
419 gTps[strlen(gTps) - 1] = 0;
420 if (strcmp(gTps, QFA_VERSION) != 0)
421 errx(1, "not requested format of -- %s", gTapeposfile);
423 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
424 errx(1, "not requested format of -- %s", gTapeposfile);
425 gTps[strlen(gTps) - 1] = 0;
426 qfadumpdate = atol(gTps);
427 /* read empty line */
428 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
429 errx(1, "not requested format of -- %s", gTapeposfile);
430 gTps[strlen(gTps) - 1] = 0;
431 /* read table header line */
432 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
433 errx(1, "not requested format of -- %s", gTapeposfile);
434 gTps[strlen(gTps) - 1] = 0;
435 /* end reading header info */
436 /* tape position table starts here */
437 gSeekstart = ftell(gTapeposfp); /* remember for later use */
439 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
440 errx(1, "can't get SCSI-ID for %s\n", inputdev);
443 errx(1, "can't get SCSI-ID for %s\n", inputdev);
445 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
446 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
447 errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
455 * Compare contents of tape.
460 Vprintf(stdout, "Begin compare restore\n");
461 compare_ignore_not_found = 0;
466 printf("filesys = %s\n", filesys);
467 if (STAT(filesys, &stbuf) < 0)
468 err(1, "cannot stat directory %s", filesys);
469 if (chdir(filesys) < 0)
470 err(1, "cannot cd to %s", filesys);
471 compare_ignore_not_found = dumptime > 0;
472 initsymtable((char *)0);
474 treescan(".", ROOTINO, nodeupdates);
478 if (compare_errors) {
479 printf("Some files were modified! %d compare errors\n", compare_errors);
495 * Incremental restoration of a file system.
498 aflag = 1; /* in -r or -R mode, -a is default */
502 * This is an incremental dump tape.
504 Vprintf(stdout, "Begin incremental restore\n");
505 initsymtable(symtbl);
508 Vprintf(stdout, "Calculate node updates.\n");
509 treescan(".", ROOTINO, nodeupdates);
514 * This is a level zero dump tape.
516 Vprintf(stdout, "Begin level 0 restore\n");
517 initsymtable((char *)0);
519 Vprintf(stdout, "Calculate extraction list.\n");
520 treescan(".", ROOTINO, nodeupdates);
522 createleaves(symtbl);
527 Vprintf(stdout, "Verify the directory structure\n");
528 treescan(".", ROOTINO, verifyfile);
530 dumpsymtable(symtbl, (long)1);
533 * Resume an incremental file system restoration.
536 aflag = 1; /* in -r or -R mode, -a is default */
537 initsymtable(symtbl);
540 createleaves(symtbl);
544 dumpsymtable(symtbl, (long)1);
547 /* handle file names from either text file (-X) or the command line */
548 #define NEXTFILE(p) \
554 else if (filelist) { \
555 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
556 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
557 *(p + strlen(p) - 1) = '\0'; \
558 if ( !*p ) /* skip empty lines */ \
564 * List contents of tape.
569 initsymtable((char *)0);
575 canon(p, name, sizeof(name));
576 ino = dirlookup(name);
579 treescan(name, ino, listfile);
583 * Batch extraction of tape contents.
587 tistart = time(NULL);
591 initsymtable((char *)0);
596 canon(p, name, sizeof(name));
597 ino = dirlookup(name);
602 treescan(name, ino, addfile);
606 setdirmodes(oflag ? FORCE : 0);
616 titaken = tiend - tistart;
617 msg("restore took %d:%02d:%02d\n", titaken / 3600,
618 (titaken % 3600) / 60, titaken % 60);
619 #endif /* DEBUG_QFA */
624 tistart = time(NULL);
627 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
628 errx(1, "can't get SCSI-ID for %s\n", inputdev);
631 errx(1, "can't get SCSI-ID for %s\n", inputdev);
633 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
634 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
635 errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
639 msg("writing QFA positions to %s\n", gTapeposfile);
640 (void) umask(orig_umask);
641 if ((gTapeposfd = open(gTapeposfile, O_WRONLY|O_CREAT|O_TRUNC,
642 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
643 |S_IROTH|S_IWOTH)) < 0)
644 errx(1, "can't create tapeposfile\n");
645 (void) umask(FORCED_UMASK);
646 /* print QFA-file header */
647 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
648 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
649 errx(1, "can't write tapeposfile\n");
650 sprintf(gTps, "ino\ttapeno\ttapepos\n");
651 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
652 errx(1, "can't write tapeposfile\n");
655 initsymtable((char *)0);
660 canon(p, name, sizeof(name));
661 ino = dirlookup(name);
666 treescan(name, ino, addfile);
676 titaken = tiend - tistart;
677 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600,
678 (titaken % 3600) / 60, titaken % 60);
679 #endif /* DEBUG_QFA */
685 return 0; /* gcc shut up */
691 char white[MAXPATHLEN];
692 const char *ext2ver, *ext2date;
694 memset(white, ' ', MAXPATHLEN);
695 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
698 ext2fs_get_library_version(&ext2ver, &ext2date);
699 (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
700 __progname, _DUMP_VERSION, ext2ver, ext2date);
702 (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
712 #define qfaflag "[-Q file] "
717 #ifdef TRANSSELINUX /*GAN6May06 SELinux MLS */
719 # define tsEflag "[-E mls] "
726 "\t%s -C [-cd" tseflag "H" kerbflag "lMvVy] [-b blocksize] [-D filesystem] " tsEflag"\n"
727 "\t%s [-f file] [-F script] [-L limit] [-s fileno]\n"
728 "\t%s -i [-acd" tseflag "hH" kerbflag "lmMouvVy] [-A file] [-b blocksize] " tsEflag"\n"
729 "\t%s [-f file] [-F script] " qfaflag "[-s fileno]\n"
731 "\t%s -P file [-acdhH" kerbflag "lmMuvVy] [-b blocksize]\n"
732 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
734 "\t%s -r [-cd" tseflag "H" kerbflag "lMuvVy] [-b blocksize] " tsEflag"\n"
735 "\t%s [-f file] [-F script] [-s fileno] [-T directory]\n"
736 "\t%s -R [-cd" tseflag "H" kerbflag "lMuvVy] [-b blocksize] " tsEflag"\n"
737 "\t%s [-f file] [-F script] [-s fileno] [-T directory]\n"
738 "\t%s -t [-cdhH" kerbflag "lMuvVy] [-A file] [-b blocksize]\n"
739 "\t%s [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
740 "\t%s -x [-acd" tseflag "hH" kerbflag "lmMouvVy] [-A file] [-b blocksize] " tsEflag"\n"
741 "\t%s [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
756 * Change set of key letters and ordered arguments into something
757 * getopt(3) will like.
760 obsolete(int *argcp, char **argvp[])
763 char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
769 /* Return if no arguments or first argument has leading dash. */
771 if (argc == 1 || *ap == '-')
774 /* Allocate space for new arguments. */
775 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
776 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
777 err(1, "malloc args");
780 argv += 2, argc -= 2;
782 for (flags = 0; *ap; ++ap) {
796 warnx("option requires an argument -- %c", *ap);
799 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
800 err(1, "malloc arg");
803 (void)strcpy(&nargv[0][2], *argv);
817 /* Terminate flags. */
823 /* Copy remaining arguments. */
824 while ((*nargv++ = *argv++));
826 /* Update argument count. */
827 *argcp = nargv - *argvp - 1;
832 * reserve stdin for opt (avoid conflicts)
835 use_stdin(const char *opt)
838 errx(1, "can't handle standard input for both %s and %s",