]>
git.wh0rd.org - dump.git/blob - restore/main.c
0f43cb7e1de0f64d4f7637d1dce2032708aa8054
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.48 2005/01/13 15:41:06 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;
428 printf("filesys = %s\n", filesys
);
429 if (STAT(filesys
, &stbuf
) < 0)
430 err(1, "cannot stat directory %s", filesys
);
431 if (chdir(filesys
) < 0)
432 err(1, "cannot cd to %s", filesys
);
433 compare_ignore_not_found
= dumptime
> 0;
434 initsymtable((char *)0);
436 treescan(".", ROOTINO
, nodeupdates
);
440 if (compare_errors
) {
441 printf("Some files were modified!\n");
457 * Incremental restoration of a file system.
460 aflag
= 1; /* in -r or -R mode, -a is default */
464 * This is an incremental dump tape.
466 Vprintf(stdout
, "Begin incremental restore\n");
467 initsymtable(symtbl
);
470 Vprintf(stdout
, "Calculate node updates.\n");
471 treescan(".", ROOTINO
, nodeupdates
);
476 * This is a level zero dump tape.
478 Vprintf(stdout
, "Begin level 0 restore\n");
479 initsymtable((char *)0);
481 Vprintf(stdout
, "Calculate extraction list.\n");
482 treescan(".", ROOTINO
, nodeupdates
);
484 createleaves(symtbl
);
489 Vprintf(stdout
, "Verify the directory structure\n");
490 treescan(".", ROOTINO
, verifyfile
);
492 dumpsymtable(symtbl
, (long)1);
495 * Resume an incremental file system restoration.
498 aflag
= 1; /* in -r or -R mode, -a is default */
499 initsymtable(symtbl
);
502 createleaves(symtbl
);
506 dumpsymtable(symtbl
, (long)1);
509 /* handle file names from either text file (-X) or the command line */
510 #define NEXTFILE(p) \
516 else if (filelist) { \
517 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
518 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
519 *(p + strlen(p) - 1) = '\0'; \
520 if ( !*p ) /* skip empty lines */ \
526 * List contents of tape.
531 initsymtable((char *)0);
537 canon(p
, name
, sizeof(name
));
538 ino
= dirlookup(name
);
541 treescan(name
, ino
, listfile
);
545 * Batch extraction of tape contents.
549 tistart
= time(NULL
);
553 initsymtable((char *)0);
558 canon(p
, name
, sizeof(name
));
559 ino
= dirlookup(name
);
564 treescan(name
, ino
, addfile
);
568 setdirmodes(oflag
? FORCE
: 0);
578 titaken
= tiend
- tistart
;
579 msg("restore took %d:%02d:%02d\n", titaken
/ 3600,
580 (titaken
% 3600) / 60, titaken
% 60);
581 #endif /* DEBUG_QFA */
586 tistart
= time(NULL
);
589 if (GetSCSIIDFromPath(inputdev
, &scsiid
)) {
590 errx(1, "can't get SCSI-ID for %s\n", inputdev
);
593 errx(1, "can't get SCSI-ID for %s\n", inputdev
);
595 sprintf(smtcpath
, "/dev/rsmtc%ld,0", scsiid
);
596 if ((fdsmtc
= open(smtcpath
, O_RDWR
)) == -1) {
597 errx(1, "can't open smtc device: %s, %d\n", smtcpath
, errno
);
601 msg("writing QFA positions to %s\n", gTapeposfile
);
602 (void) umask(orig_umask
);
603 if ((gTapeposfd
= open(gTapeposfile
, O_WRONLY
|O_CREAT
|O_TRUNC
,
604 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
605 |S_IROTH
|S_IWOTH
)) < 0)
606 errx(1, "can't create tapeposfile\n");
607 (void) umask(FORCED_UMASK
);
608 /* print QFA-file header */
609 sprintf(gTps
, "%s\n%s\n%ld\n\n", QFA_MAGIC
, QFA_VERSION
, (unsigned long)spcl
.c_date
);
610 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
))
611 errx(1, "can't write tapeposfile\n");
612 sprintf(gTps
, "ino\ttapeno\ttapepos\n");
613 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
))
614 errx(1, "can't write tapeposfile\n");
617 initsymtable((char *)0);
622 canon(p
, name
, sizeof(name
));
623 ino
= dirlookup(name
);
628 treescan(name
, ino
, addfile
);
638 titaken
= tiend
- tistart
;
639 msg("writing QFA positions took %d:%02d:%02d\n", titaken
/ 3600,
640 (titaken
% 3600) / 60, titaken
% 60);
641 #endif /* DEBUG_QFA */
647 return 0; /* gcc shut up */
653 char white
[MAXPATHLEN
];
654 const char *ext2ver
, *ext2date
;
656 memset(white
, ' ', MAXPATHLEN
);
657 white
[MIN(strlen(__progname
), MAXPATHLEN
- 1)] = '\0';
660 ext2fs_get_library_version(&ext2ver
, &ext2date
);
661 (void)fprintf(stderr
, "%s %s (using libext2fs %s of %s)\n",
662 __progname
, _DUMP_VERSION
, ext2ver
, ext2date
);
664 (void)fprintf(stderr
, "%s %s\n", __progname
, _DUMP_VERSION
);
674 #define qfaflag "[-Q file] "
681 "\t%s -C [-cd" kerbflag
"lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
682 "\t%s [-F script] [-L limit] [-s fileno]\n"
683 "\t%s -i [-acdh" kerbflag
"lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
684 "\t%s [-F script] " qfaflag
"[-s fileno]\n"
686 "\t%s -P file [-acdh" kerbflag
"lmMuvVy] [-A file] [-b blocksize]\n"
687 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
689 "\t%s -r [-cd" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script]\n"
690 "\t%s [-s fileno] [-T directory]\n"
691 "\t%s -R [-cd" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script]\n"
692 "\t%s [-s fileno] [-T directory]\n"
693 "\t%s -t [-cdh" kerbflag
"lMuvVy] [-A file] [-b blocksize] [-f file]\n"
694 "\t%s [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]\n"
695 "\t%s -x [-acdh" kerbflag
"lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
696 "\t%s [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]\n",
711 * Change set of key letters and ordered arguments into something
712 * getopt(3) will like.
715 obsolete(int *argcp
, char **argvp
[])
718 char *ap
, **argv
, *flagsp
= NULL
, **nargv
, *p
= NULL
;
724 /* Return if no arguments or first argument has leading dash. */
726 if (argc
== 1 || *ap
== '-')
729 /* Allocate space for new arguments. */
730 if ((*argvp
= nargv
= malloc((argc
+ 1) * sizeof(char *))) == NULL
||
731 (p
= flagsp
= malloc(strlen(ap
) + 2)) == NULL
)
732 err(1, "malloc args");
735 argv
+= 2, argc
-= 2;
737 for (flags
= 0; *ap
; ++ap
) {
750 warnx("option requires an argument -- %c", *ap
);
753 if ((nargv
[0] = malloc(strlen(*argv
) + 2 + 1)) == NULL
)
754 err(1, "malloc arg");
757 (void)strcpy(&nargv
[0][2], *argv
);
771 /* Terminate flags. */
777 /* Copy remaining arguments. */
778 while ((*nargv
++ = *argv
++));
780 /* Update argument count. */
781 *argcp
= nargv
- *argvp
- 1;
786 * reserve stdin for opt (avoid conflicts)
789 use_stdin(const char *opt
)
792 errx(1, "can't handle standard input for both %s and %s",