]>
git.wh0rd.org - dump.git/blob - restore/main.c
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.47 2004/12/15 11:00:01 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
];
123 int createtapeposflag
;
124 unsigned long qfadumpdate
;
125 long long curtapepos
;
130 #if defined(__linux__) || defined(sunos)
134 static void obsolete
__P((int *, char **[]));
135 static void usage
__P((void));
136 static void use_stdin
__P((const char *));
138 #define FORCED_UMASK (077)
141 main(int argc
, char *argv
[])
145 char *inputdev
= _PATH_DEFTAPE
;
146 char *symtbl
= "./restoresymtable";
147 char *p
, name
[MAXPATHLEN
];
148 FILE *filelist
= NULL
;
149 char fname
[MAXPATHLEN
];
152 time_t tistart
, tiend
, titaken
;
156 createtapeposflag
= 0;
159 /* Temp files should *not* be readable. We set permissions later. */
160 orig_umask
= umask(FORCED_UMASK
);
162 #if defined(__linux__) || defined(sunos)
163 __progname
= argv
[0];
169 if ((inputdev
= getenv("TAPE")) == NULL
)
170 inputdev
= _PATH_DEFTAPE
;
171 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
173 if ((tmpdir
= strdup(tmpdir
)) == NULL
)
174 err(1, "malloc tmpdir");
175 for (p
= tmpdir
+ strlen(tmpdir
) - 1; p
>= tmpdir
&& *p
== '/'; p
--)
177 obsolete(&argc
, &argv
);
178 while ((ch
= getopt(argc
, argv
,
187 "Rrs:tT:uvVxX:y")) != -1)
197 /* Change default tape blocksize. */
199 ntrec
= strtol(optarg
, &p
, 10);
201 errx(1, "illegal blocksize -- %s", optarg
);
203 errx(1, "block size must be greater than 0");
209 strncpy(filesys
, optarg
, NAMELEN
);
210 filesys
[NAMELEN
- 1] = '\0';
219 if( !strcmp(optarg
,"-") )
245 "%c and %c options are mutually exclusive",
250 gTapeposfile
= optarg
;
251 createtapeposflag
= 1;
260 Lflag
= strtol(optarg
, &p
, 10);
262 errx(1, "illegal limit -- %s", optarg
);
264 errx(1, "limit must be greater than 0");
280 gTapeposfile
= optarg
;
286 /* Dumpnum (skip to) for multifile dump tapes. */
287 dumpnum
= strtol(optarg
, &p
, 10);
289 errx(1, "illegal dump number -- %s", optarg
);
291 errx(1, "dump number must be greater than 0");
303 if( !strcmp(optarg
,"-") ) {
308 if ( !(filelist
= fopen(optarg
,"r")) )
309 errx(1, "can't open file for reading -- %s", optarg
);
321 errx(1, "none of C, i, R, r, t or x options specified");
324 if (!mflag
&& tapeposflag
)
325 errx(1, "m and Q options are mutually exclusive");
327 if (tapeposflag
&& command
!= 'i' && command
!= 'x' && command
!= 't')
328 errx(1, "Q option is not valid for %c command", command
);
331 if (Afile
&& command
!= 'i' && command
!= 'x' && command
!= 't')
332 errx(1, "A option is not valid for %c command", command
);
334 if (signal(SIGINT
, onintr
) == SIG_IGN
)
335 (void) signal(SIGINT
, SIG_IGN
);
336 if (signal(SIGTERM
, onintr
) == SIG_IGN
)
337 (void) signal(SIGTERM
, SIG_IGN
);
342 if (command
== 'C' && inputdev
[0] != '/' && strcmp(inputdev
, "-")
344 && !strchr(inputdev
, ':')
347 /* since we chdir into the directory we are comparing
348 * to, we must retain the full tape path */
349 char wd
[MAXPATHLEN
], fullpathinput
[MAXPATHLEN
];
350 if (!getcwd(wd
, MAXPATHLEN
))
351 err(1, "can't get current directory");
352 snprintf(fullpathinput
, MAXPATHLEN
, "%s/%s", wd
, inputdev
);
353 fullpathinput
[MAXPATHLEN
- 1] = '\0';
354 setinput(fullpathinput
);
359 if (argc
== 0 && !filelist
) {
366 msg("reading QFA positions from %s\n", gTapeposfile
);
367 if ((gTapeposfp
= fopen(gTapeposfile
, "r")) == NULL
)
368 errx(1, "can't open file for reading -- %s",
370 /* start reading header info */
371 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
372 errx(1, "not requested format of -- %s", gTapeposfile
);
373 gTps
[strlen(gTps
) - 1] = 0; /* delete end of line */
374 if (strcmp(gTps
, QFA_MAGIC
) != 0)
375 errx(1, "not requested format of -- %s", gTapeposfile
);
376 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
377 errx(1, "not requested format of -- %s", gTapeposfile
);
378 gTps
[strlen(gTps
) - 1] = 0;
379 if (strcmp(gTps
, QFA_VERSION
) != 0)
380 errx(1, "not requested format of -- %s", gTapeposfile
);
382 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
383 errx(1, "not requested format of -- %s", gTapeposfile
);
384 gTps
[strlen(gTps
) - 1] = 0;
385 qfadumpdate
= atol(gTps
);
386 /* read empty line */
387 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
388 errx(1, "not requested format of -- %s", gTapeposfile
);
389 gTps
[strlen(gTps
) - 1] = 0;
390 /* read table header line */
391 if (fgets(gTps
, sizeof(gTps
), gTapeposfp
) == NULL
)
392 errx(1, "not requested format of -- %s", gTapeposfile
);
393 gTps
[strlen(gTps
) - 1] = 0;
394 /* end reading header info */
395 /* tape position table starts here */
396 gSeekstart
= ftell(gTapeposfp
); /* remember for later use */
398 if (GetSCSIIDFromPath(inputdev
, &scsiid
)) {
399 errx(1, "can't get SCSI-ID for %s\n", inputdev
);
402 errx(1, "can't get SCSI-ID for %s\n", inputdev
);
404 sprintf(smtcpath
, "/dev/rsmtc%ld,0", scsiid
);
405 if ((fdsmtc
= open(smtcpath
, O_RDWR
)) == -1) {
406 errx(1, "can't open smtc device: %s, %d\n", smtcpath
, errno
);
414 * Compare contents of tape.
419 Vprintf(stdout
, "Begin compare restore\n");
420 compare_ignore_not_found
= 0;
423 printf("filesys = %s\n", filesys
);
424 if (STAT(filesys
, &stbuf
) < 0)
425 err(1, "cannot stat directory %s", filesys
);
426 if (chdir(filesys
) < 0)
427 err(1, "cannot cd to %s", filesys
);
428 compare_ignore_not_found
= dumptime
> 0;
429 initsymtable((char *)0);
431 treescan(".", ROOTINO
, nodeupdates
);
435 if (compare_errors
) {
436 printf("Some files were modified!\n");
452 * Incremental restoration of a file system.
455 aflag
= 1; /* in -r or -R mode, -a is default */
459 * This is an incremental dump tape.
461 Vprintf(stdout
, "Begin incremental restore\n");
462 initsymtable(symtbl
);
465 Vprintf(stdout
, "Calculate node updates.\n");
466 treescan(".", ROOTINO
, nodeupdates
);
471 * This is a level zero dump tape.
473 Vprintf(stdout
, "Begin level 0 restore\n");
474 initsymtable((char *)0);
476 Vprintf(stdout
, "Calculate extraction list.\n");
477 treescan(".", ROOTINO
, nodeupdates
);
479 createleaves(symtbl
);
484 Vprintf(stdout
, "Verify the directory structure\n");
485 treescan(".", ROOTINO
, verifyfile
);
487 dumpsymtable(symtbl
, (long)1);
490 * Resume an incremental file system restoration.
493 aflag
= 1; /* in -r or -R mode, -a is default */
494 initsymtable(symtbl
);
497 createleaves(symtbl
);
501 dumpsymtable(symtbl
, (long)1);
504 /* handle file names from either text file (-X) or the command line */
505 #define NEXTFILE(p) \
511 else if (filelist) { \
512 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
513 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
514 *(p + strlen(p) - 1) = '\0'; \
515 if ( !*p ) /* skip empty lines */ \
521 * List contents of tape.
526 initsymtable((char *)0);
532 canon(p
, name
, sizeof(name
));
533 ino
= dirlookup(name
);
536 treescan(name
, ino
, listfile
);
540 * Batch extraction of tape contents.
544 tistart
= time(NULL
);
548 initsymtable((char *)0);
553 canon(p
, name
, sizeof(name
));
554 ino
= dirlookup(name
);
559 treescan(name
, ino
, addfile
);
563 setdirmodes(oflag
? FORCE
: 0);
573 titaken
= tiend
- tistart
;
574 msg("restore took %d:%02d:%02d\n", titaken
/ 3600,
575 (titaken
% 3600) / 60, titaken
% 60);
576 #endif /* DEBUG_QFA */
581 tistart
= time(NULL
);
584 if (GetSCSIIDFromPath(inputdev
, &scsiid
)) {
585 errx(1, "can't get SCSI-ID for %s\n", inputdev
);
588 errx(1, "can't get SCSI-ID for %s\n", inputdev
);
590 sprintf(smtcpath
, "/dev/rsmtc%ld,0", scsiid
);
591 if ((fdsmtc
= open(smtcpath
, O_RDWR
)) == -1) {
592 errx(1, "can't open smtc device: %s, %d\n", smtcpath
, errno
);
596 msg("writing QFA positions to %s\n", gTapeposfile
);
597 (void) umask(orig_umask
);
598 if ((gTapeposfd
= open(gTapeposfile
, O_WRONLY
|O_CREAT
|O_TRUNC
,
599 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
600 |S_IROTH
|S_IWOTH
)) < 0)
601 errx(1, "can't create tapeposfile\n");
602 (void) umask(FORCED_UMASK
);
603 /* print QFA-file header */
604 sprintf(gTps
, "%s\n%s\n%ld\n\n", QFA_MAGIC
, QFA_VERSION
, (unsigned long)spcl
.c_date
);
605 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
))
606 errx(1, "can't write tapeposfile\n");
607 sprintf(gTps
, "ino\ttapeno\ttapepos\n");
608 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
))
609 errx(1, "can't write tapeposfile\n");
612 initsymtable((char *)0);
617 canon(p
, name
, sizeof(name
));
618 ino
= dirlookup(name
);
623 treescan(name
, ino
, addfile
);
633 titaken
= tiend
- tistart
;
634 msg("writing QFA positions took %d:%02d:%02d\n", titaken
/ 3600,
635 (titaken
% 3600) / 60, titaken
% 60);
636 #endif /* DEBUG_QFA */
642 return 0; /* gcc shut up */
648 char white
[MAXPATHLEN
];
649 const char *ext2ver
, *ext2date
;
651 memset(white
, ' ', MAXPATHLEN
);
652 white
[MIN(strlen(__progname
), MAXPATHLEN
- 1)] = '\0';
655 ext2fs_get_library_version(&ext2ver
, &ext2date
);
656 (void)fprintf(stderr
, "%s %s (using libext2fs %s of %s)\n",
657 __progname
, _DUMP_VERSION
, ext2ver
, ext2date
);
659 (void)fprintf(stderr
, "%s %s\n", __progname
, _DUMP_VERSION
);
669 #define qfaflag "[-Q file] "
676 "\t%s -C [-cd" kerbflag
"lMvVy] [-b blocksize] [-D filesystem] [-f file]\n"
677 "\t%s [-F script] [-L limit] [-s fileno]\n"
678 "\t%s -i [-acdh" kerbflag
"lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
679 "\t%s [-F script] " qfaflag
"[-s fileno]\n"
681 "\t%s -P file [-acdh" kerbflag
"lmMuvVy] [-A file] [-b blocksize]\n"
682 "\t%s [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\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 -R [-cd" kerbflag
"lMuvVy] [-b blocksize] [-f file] [-F script]\n"
687 "\t%s [-s fileno] [-T directory]\n"
688 "\t%s -t [-cdh" kerbflag
"lMuvVy] [-A file] [-b blocksize] [-f file]\n"
689 "\t%s [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]\n"
690 "\t%s -x [-acdh" kerbflag
"lmMouvVy] [-A file] [-b blocksize] [-f file]\n"
691 "\t%s [-F script] " qfaflag
"[-s fileno] [-X filelist] [file ...]\n",
706 * Change set of key letters and ordered arguments into something
707 * getopt(3) will like.
710 obsolete(int *argcp
, char **argvp
[])
713 char *ap
, **argv
, *flagsp
= NULL
, **nargv
, *p
= NULL
;
719 /* Return if no arguments or first argument has leading dash. */
721 if (argc
== 1 || *ap
== '-')
724 /* Allocate space for new arguments. */
725 if ((*argvp
= nargv
= malloc((argc
+ 1) * sizeof(char *))) == NULL
||
726 (p
= flagsp
= malloc(strlen(ap
) + 2)) == NULL
)
727 err(1, "malloc args");
730 argv
+= 2, argc
-= 2;
732 for (flags
= 0; *ap
; ++ap
) {
745 warnx("option requires an argument -- %c", *ap
);
748 if ((nargv
[0] = malloc(strlen(*argv
) + 2 + 1)) == NULL
)
749 err(1, "malloc arg");
752 (void)strcpy(&nargv
[0][2], *argv
);
766 /* Terminate flags. */
772 /* Copy remaining arguments. */
773 while ((*nargv
++ = *argv
++));
775 /* Update argument count. */
776 *argcp
= nargv
- *argvp
- 1;
781 * reserve stdin for opt (avoid conflicts)
784 use_stdin(const char *opt
)
787 errx(1, "can't handle standard input for both %s and %s",