]>
git.wh0rd.org - dump.git/blob - restore/tape.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 <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 static const char rcsid
[] =
49 "$Id: tape.c,v 1.24 2001/02/21 16:13:05 stelian Exp $";
53 #include <sys/param.h>
60 #include <linux/ext2_fs.h>
61 #include <bsdcompat.h>
63 #include <ufs/ufs/dinode.h>
64 #endif /* __linux__ */
65 #include <protocols/dumprestore.h>
68 #include <compaterr.h>
77 #endif /* HAVE_ZLIB */
80 #include <ext2fs/ext2fs.h>
85 #include "pathnames.h"
87 static long fssize
= MAXBSIZE
;
89 static int pipein
= 0;
90 static char magtape
[MAXPATHLEN
];
91 static char magtapeprefix
[MAXPATHLEN
];
95 static char *tbufptr
= NULL
; /* active tape buffer */
97 static char *comprbuf
; /* uncompress work buf */
98 static size_t comprlen
;
100 static union u_spcl endoftapemark
;
101 static long blksread
; /* blocks read since last header */
102 static long tpblksread
= 0; /* TP_BSIZE blocks read */
103 static long tapesread
;
104 static sigjmp_buf restart
;
105 static int gettingfile
= 0; /* restart has a valid frame */
106 static char *host
= NULL
;
110 static char lnkbuf
[MAXPATHLEN
+ 1];
113 int oldinofmt
; /* old inode format conversion required */
114 int Bcvt
; /* Swap Bytes (for CCI or sun) */
115 static int Qcvt
; /* Swap quads (for sun) */
117 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
119 static void accthdr
__P((struct s_spcl
*));
120 static int checksum
__P((int *));
121 static void findinode
__P((struct s_spcl
*));
122 static void findtapeblksize
__P((void));
123 static int gethead
__P((struct s_spcl
*));
124 static void readtape
__P((char *));
125 static void setdumpnum
__P((void));
126 static u_int swabi
__P((u_int
));
128 static u_long swabl
__P((u_long
));
130 static u_char
*swab64
__P((u_char
*, int));
131 static u_char
*swab32
__P((u_char
*, int));
132 static u_char
*swab16
__P((u_char
*, int));
133 static void terminateinput
__P((void));
134 static void xtrfile
__P((char *, size_t));
135 static void xtrlnkfile
__P((char *, size_t));
136 static void xtrlnkskip
__P((char *, size_t));
137 static void xtrmap
__P((char *, size_t));
138 static void xtrmapskip
__P((char *, size_t));
139 static void xtrskip
__P((char *, size_t));
141 #define COMPARE_ONTHEFLY 1
144 static int ifile
; /* input file for compare */
145 static int cmperror
; /* compare error */
146 static void xtrcmpfile
__P((char *, size_t));
147 static void xtrcmpskip
__P((char *, size_t));
150 static int readmapflag
;
153 * Set up an input source
156 setinput(char *source
)
162 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
166 if (strchr(source
, ':')) {
168 source
= strchr(host
, ':');
170 if (rmthost(host
) == 0)
174 if (strcmp(source
, "-") == 0) {
176 * Since input is coming from a pipe we must establish
177 * our own connection to the terminal.
179 terminal
= fopen(_PATH_TTY
, "r");
180 if (terminal
== NULL
) {
181 warn("cannot open %s", _PATH_TTY
);
182 terminal
= fopen(_PATH_DEVNULL
, "r");
183 if (terminal
== NULL
)
184 err(1, "cannot open %s", _PATH_DEVNULL
);
188 setuid(getuid()); /* no longer need or want root privileges */
190 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
191 magtapeprefix
[MAXPATHLEN
-1] = '\0';
192 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
195 strncpy(magtape
, source
, MAXPATHLEN
);
196 magtape
[MAXPATHLEN
- 1] = '\0';
200 newtapebuf(long size
)
202 static int tapebufsize
= -1;
205 if (size
<= tapebufsize
)
209 tapebuf
= malloc(size
* TP_BSIZE
);
211 errx(1, "Cannot allocate space for tape buffer");
214 if (comprbuf
!= NULL
)
216 comprlen
= size
* TP_BSIZE
;
217 comprbuf
= malloc(comprlen
);
218 if (comprbuf
== NULL
)
219 errx(1, "Cannot allocate space for uncompress buffer");
220 #endif /* HAVE_ZLIB */
224 * Verify that the tape drive can be accessed and
225 * that it actually is a dump tape.
233 Vprintf(stdout
, "Verify tape and initialize maps\n");
236 mt
= rmtopen(magtape
, 0);
242 mt
= open(magtape
, O_RDONLY
, 0);
244 err(1, "%s", magtape
);
248 if (!pipein
&& !bflag
)
250 if (gethead(&spcl
) == FAIL
) {
251 blkcnt
--; /* push back this block */
255 if (gethead(&spcl
) == FAIL
)
256 errx(1, "Tape is not a dump tape");
257 fprintf(stderr
, "Converting to new file system format.\n");
260 if (spcl
.c_flags
& DR_COMPRESSED
) {
261 fprintf(stderr
, "Dump tape is compressed.\n");
265 errx(1,"This restore version doesn't support decompression");
266 #endif /* HAVE_ZLIB */
269 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
270 endoftapemark
.s_spcl
.c_type
= TS_END
;
271 ip
= (int *)&endoftapemark
;
272 j
= sizeof(union u_spcl
) / sizeof(int);
277 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
279 if (vflag
|| command
== 't' || command
== 'C')
281 if (filesys
[0] == '\0') {
283 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
284 filesys
[NAMELEN
- 1] = '\0';
285 dirptr
= strstr(filesys
, " (dir");
289 dumptime
= spcl
.c_ddate
;
290 dumpdate
= spcl
.c_date
;
291 if (stat(".", &stbuf
) < 0)
292 err(1, "cannot stat .");
293 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
295 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
296 fssize
= stbuf
.st_blksize
;
297 if (((fssize
- 1) & fssize
) != 0)
298 errx(1, "bad block size %ld", fssize
);
299 if (spcl
.c_volume
!= 1)
300 errx(1, "Tape is not volume 1 of the dump");
301 if (gethead(&spcl
) == FAIL
) {
302 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
303 panic("no header after volume mark!\n");
306 if (spcl
.c_type
!= TS_CLRI
)
307 errx(1, "Cannot find file removal list");
308 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
309 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
310 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
312 errx(1, "no memory for active inode map");
314 curfile
.action
= USING
;
315 getfile(xtrmap
, xtrmapskip
);
316 if (spcl
.c_type
!= TS_BITS
)
317 errx(1, "Cannot find file dump list");
318 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
319 if (map
== (char *)NULL
)
320 errx(1, "no memory for file dump list");
322 curfile
.action
= USING
;
323 getfile(xtrmap
, xtrmapskip
);
325 * If there may be whiteout entries on the tape, pretend that the
326 * whiteout inode exists, so that the whiteout entries can be
330 SETINO(WINO
, dumpmap
);
334 * Prompt user to load a new dump volume.
335 * "Nextvol" is the next suggested volume to use.
336 * This suggested volume is enforced when doing full
337 * or incremental restores, but can be overridden by
338 * the user when only extracting a subset of the files.
343 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
344 union u_spcl tmpspcl
;
345 # define tmpbuf tmpspcl.s_spcl
355 panic("Changing volumes on pipe input?\n");
363 exit(1); /* pipes do not get a second chance */
364 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
371 while (newvol
<= 0) {
372 if (tapesread
== 0) {
373 fprintf(stderr
, "%s%s%s%s%s",
374 "You have not read any tapes yet.\n",
375 "Unless you know which volume your",
376 " file(s) are on you should start\n",
377 "with the last volume and work",
378 " towards the first.\n");
380 fprintf(stderr
, "You have read volumes");
382 for (i
= 1; i
< 32; i
++)
383 if (tapesread
& (1 << i
)) {
384 fprintf(stderr
, "%s%ld", buf
, (long)i
);
387 fprintf(stderr
, "\n");
390 fprintf(stderr
, "Specify next volume #: ");
391 (void) fflush(stderr
);
392 (void) fgets(buf
, TP_BSIZE
, terminal
);
393 } while (!feof(terminal
) && buf
[0] == '\n');
399 "Volume numbers are positive numerics\n");
402 if (newvol
== volno
) {
403 tapesread
|= 1 << volno
;
408 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
409 magtape
[MAXPATHLEN
- 1] = '\0';
411 if (!Mflag
|| haderror
) {
413 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
414 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
415 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
416 (void) fflush(stderr
);
417 (void) fgets(buf
, TP_BSIZE
, terminal
);
420 if (!strcmp(buf
, "none\n")) {
424 if (buf
[0] != '\n') {
425 (void) strcpy(magtape
, buf
);
426 magtape
[strlen(magtape
) - 1] = '\0';
431 mt
= rmtopen(magtape
, 0);
434 mt
= open(magtape
, O_RDONLY
, 0);
437 fprintf(stderr
, "Cannot open %s\n", magtape
);
446 if (gethead(&tmpbuf
) == FAIL
) {
447 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
448 fprintf(stderr
, "tape is not dump tape\n");
453 if (tmpbuf
.c_volume
!= volno
) {
454 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
459 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
461 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
462 ctime4(&tmpbuf
.c_date
));
463 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
465 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
466 ctime(&tmpbuf
.c_date
));
467 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
473 tapesread
|= 1 << volno
;
476 * If continuing from the previous volume, skip over any
477 * blocks read already at the end of the previous volume.
479 * If coming to this volume at random, skip to the beginning
480 * of the next record.
482 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
483 tpblksread
, (long)tmpbuf
.c_firstrec
);
484 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
486 tpblksread
= tmpbuf
.c_firstrec
;
487 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
489 } else if (tmpbuf
.c_firstrec
> 0 &&
490 tmpbuf
.c_firstrec
< tpblksread
- 1) {
492 * -1 since we've read the volume header
494 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
495 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
496 (long)i
, i
> 1 ? "s" : "");
501 if (curfile
.action
== USING
) {
503 panic("active file into volume 1\n");
507 * Skip up to the beginning of the next record
509 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
510 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
512 (void) gethead(&spcl
);
516 siglongjmp(restart
, 1);
521 * Handle unexpected EOF.
527 if (gettingfile
&& curfile
.action
== USING
) {
528 printf("Warning: %s %s\n",
529 "End-of-input encountered while extracting", curfile
.name
);
531 curfile
.name
= "<name unknown>";
532 curfile
.action
= UNKNOWN
;
534 curfile
.ino
= maxino
;
537 siglongjmp(restart
, 1);
542 * handle multiple dumps per tape by skipping forward to the
550 if (dumpnum
== 1 || volno
!= 1)
553 errx(1, "Cannot have multiple dumps on pipe input");
555 tcom
.mt_count
= dumpnum
- 1;
558 rmtioctl(MTFSF
, dumpnum
- 1);
561 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
569 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
570 fprintf(stdout
, "Dumped from: %s",
571 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
573 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
574 fprintf(stdout
, "Dumped from: %s",
575 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
577 if (spcl
.c_host
[0] == '\0')
579 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
580 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
581 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
585 extractfile(char *name
)
589 struct timeval timep
[2];
593 curfile
.action
= USING
;
595 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
596 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
597 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
598 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
599 #else /* __linux__ */
600 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
601 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
602 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
603 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
604 #endif /* __linux__ */
605 mode
= curfile
.dip
->di_mode
;
606 flags
= curfile
.dip
->di_flags
;
607 switch (mode
& IFMT
) {
610 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
615 Vprintf(stdout
, "skipped socket %s\n", name
);
621 ep
= lookupname(name
);
622 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
623 panic("unextracted directory %s\n", name
);
627 Vprintf(stdout
, "extract file %s\n", name
);
628 return (genliteraldir(name
, curfile
.ino
));
633 uid_t luid
= curfile
.dip
->di_uid
;
634 gid_t lgid
= curfile
.dip
->di_gid
;
638 getfile(xtrlnkfile
, xtrlnkskip
);
641 "%s: zero length symbolic link (ignored)\n", name
);
644 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
647 (void) lchown(name
, luid
, lgid
);
653 Vprintf(stdout
, "extract fifo %s\n", name
);
660 if (mkfifo(name
, mode
) < 0) {
661 warn("%s: cannot create fifo", name
);
665 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
666 (void) chmod(name
, mode
);
669 (void) fsetflags(name
, flags
);
671 (void) chflags(name
, flags
);
679 Vprintf(stdout
, "extract special file %s\n", name
);
686 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
687 warn("%s: cannot create special file", name
);
691 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
692 (void) chmod(name
, mode
);
696 warn("%s: fsetflags called on a special file", name
);
697 (void) fsetflags(name
, flags
);
700 (void) chflags(name
, flags
);
707 Vprintf(stdout
, "extract file %s\n", name
);
714 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
716 warn("%s: cannot create file", name
);
720 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
721 (void) fchmod(ofile
, mode
);
724 (void) setflags(ofile
, flags
);
726 (void) fchflags(ofile
, flags
);
728 getfile(xtrfile
, xtrskip
);
737 * skip over bit maps on the tape
743 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
748 * skip over a file on the tape
754 curfile
.action
= SKIP
;
755 getfile(xtrnull
, xtrnull
);
759 * Extract a file from the tape.
760 * When an allocated block is found it is passed to the fill function;
761 * when an unallocated block (hole) is found, a zeroed buffer is passed
762 * to the skip function.
765 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
768 volatile int curblk
= 0;
769 volatile quad_t size
= spcl
.c_dinode
.di_size
;
770 volatile int last_write_was_hole
= 0;
771 quad_t origsize
= size
;
772 static char clearedbuf
[MAXBSIZE
];
773 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
776 if (spcl
.c_type
== TS_END
)
777 panic("ran off end of tape\n");
778 if (spcl
.c_magic
!= NFS_MAGIC
)
779 panic("not at beginning of a file\n");
780 if (!gettingfile
&& setjmp(restart
) != 0)
784 for (i
= 0; i
< spcl
.c_count
; i
++) {
785 if (readmapflag
|| spcl
.c_addr
[i
]) {
786 readtape(&buf
[curblk
++][0]);
787 if (curblk
== fssize
/ TP_BSIZE
) {
788 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
789 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
791 last_write_was_hole
= 0;
795 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
797 (curblk
- 1) * TP_BSIZE
+ size
));
800 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
802 last_write_was_hole
= 1;
804 if ((size
-= TP_BSIZE
) <= 0) {
805 for (i
++; i
< spcl
.c_count
; i
++)
806 if (readmapflag
|| spcl
.c_addr
[i
])
811 if (gethead(&spcl
) == GOOD
&& size
> 0) {
812 if (spcl
.c_type
== TS_ADDR
)
815 "Missing address (header) block for %s at %ld blocks\n",
816 curfile
.name
, (long)blksread
);
819 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
820 last_write_was_hole
= 0;
822 if (last_write_was_hole
) {
823 ftruncate(ofile
, origsize
);
830 * Write out the next block of a file.
833 xtrfile(char *buf
, size_t size
)
838 if (write(ofile
, buf
, (int) size
) == -1)
839 err(1, "write error extracting inode %lu, name %s\nwrite",
840 (unsigned long)curfile
.ino
, curfile
.name
);
844 * Skip over a hole in a file.
848 xtrskip(char *buf
, size_t size
)
851 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
852 err(1, "seek error extracting inode %lu, name %s\nlseek",
853 (unsigned long)curfile
.ino
, curfile
.name
);
857 * Collect the next block of a symbolic link.
860 xtrlnkfile(char *buf
, size_t size
)
864 if (pathlen
> MAXPATHLEN
)
865 errx(1, "symbolic link name: %s->%s%s; too long %d",
866 curfile
.name
, lnkbuf
, buf
, pathlen
);
867 (void) strcat(lnkbuf
, buf
);
871 * Skip over a hole in a symbolic link (should never happen).
875 xtrlnkskip(char *buf
, size_t size
)
878 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
882 * Collect the next block of a bit map.
885 xtrmap(char *buf
, size_t size
)
888 memmove(map
, buf
, size
);
893 * Skip over a hole in a bit map (should never happen).
897 xtrmapskip(char *buf
, size_t size
)
900 panic("hole in map\n");
905 * Noop, when an extraction function is not needed.
909 xtrnull(char *buf
, size_t size
)
917 * Compare the next block of a file.
920 xtrcmpfile(char *buf
, size_t size
)
922 static char cmpbuf
[MAXBSIZE
];
927 if (read(ifile
, cmpbuf
, size
) != size
) {
928 fprintf(stderr
, "%s: size has changed.\n",
934 if (memcmp(buf
, cmpbuf
, size
) != 0) {
935 fprintf(stderr
, "%s: tape and disk copies are different\n",
943 * Skip over a hole in a file.
946 xtrcmpskip(char *buf
, size_t size
)
948 static char cmpbuf
[MAXBSIZE
];
954 if (read(ifile
, cmpbuf
, size
) != size
) {
955 fprintf(stderr
, "%s: size has changed.\n",
961 for (i
= 0; i
< size
; ++i
)
962 if (cmpbuf
[i
] != '\0') {
963 fprintf(stderr
, "%s: tape and disk copies are different\n",
969 #endif /* COMPARE_ONTHEFLY */
971 #if !COMPARE_ONTHEFLY
973 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
975 static char buf_tape
[BUFSIZ
];
976 static char buf_disk
[BUFSIZ
];
981 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
982 close(fd_tape
), close(fd_disk
);
983 panic("do_cmpfiles: unexpected EOF[1]");
985 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
986 close(fd_tape
), close(fd_disk
);
987 panic("do_cmpfiles: unexpected EOF[2]");
989 if (n_tape
!= n_disk
) {
990 close(fd_tape
), close(fd_disk
);
991 panic("do_cmpfiles: sizes different!");
993 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
999 /* for debugging compare problems */
1000 #undef COMPARE_FAIL_KEEP_FILE
1003 #ifdef COMPARE_FAIL_KEEP_FILE
1004 /* return true if tapefile should be unlinked after compare */
1009 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
1011 struct stat sbuf_tape
;
1012 int fd_tape
, fd_disk
;
1014 if (stat(tapefile
, &sbuf_tape
) != 0) {
1015 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1020 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1022 "%s: size changed from %ld to %ld.\n",
1023 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1025 #ifdef COMPARE_FAIL_KEEP_FILE
1032 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
1033 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1036 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
1038 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1042 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1043 fprintf(stderr
, "%s: tape and disk copies are different\n",
1048 #ifdef COMPARE_FAIL_KEEP_FILE
1049 /* rename the file to live in /tmp */
1050 /* rename `tapefile' to /tmp/<basename of diskfile> */
1052 char *p
= strrchr(diskfile
, '/');
1053 char newname
[MAXPATHLEN
];
1055 panic("can't find / in %s\n", diskfile
);
1057 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1058 if (rename(tapefile
, newname
)) {
1059 panic("rename from %s to %s failed: %s\n",
1063 fprintf(stderr
, "*** %s saved to %s\n",
1068 /* don't unlink the file (it's not there anymore */
1077 #ifdef COMPARE_FAIL_KEEP_FILE
1081 #endif /* !COMPARE_ONTHEFLY */
1083 #if !COMPARE_ONTHEFLY
1084 static char tmpfilename
[MAXPATHLEN
];
1088 comparefile(char *name
)
1093 #if !COMPARE_ONTHEFLY
1094 static char *tmpfile
= NULL
;
1098 if ((r
= lstat(name
, &sb
)) != 0) {
1099 warn("%s: does not exist (%d)", name
, r
);
1105 curfile
.name
= name
;
1106 curfile
.action
= USING
;
1107 mode
= curfile
.dip
->di_mode
;
1109 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1110 (long)sb
.st_size
, mode
);
1112 if (sb
.st_mode
!= mode
) {
1113 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1114 name
, mode
& 07777, sb
.st_mode
& 07777);
1117 switch (mode
& IFMT
) {
1131 char lbuf
[MAXPATHLEN
+ 1];
1134 if (!(sb
.st_mode
& S_IFLNK
)) {
1135 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1142 getfile(xtrlnkfile
, xtrlnkskip
);
1145 "%s: zero length symbolic link (ignored)\n",
1150 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1151 panic("readlink of %s failed: %s", name
,
1156 if (strcmp(lbuf
, lnkbuf
) != 0) {
1158 "%s: symbolic link changed from %s to %s.\n",
1159 name
, lnkbuf
, lbuf
);
1168 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1169 fprintf(stderr
, "%s: no longer a special file\n",
1176 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1178 "%s: device changed from %d,%d to %d,%d.\n",
1180 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1181 (int)curfile
.dip
->di_rdev
& 0xff,
1182 ((int)sb
.st_rdev
>> 8) & 0xff,
1183 (int)sb
.st_rdev
& 0xff);
1190 #if COMPARE_ONTHEFLY
1191 if ((ifile
= open(name
, O_RDONLY
)) < 0) {
1192 panic("Can't open %s: %s\n", name
, strerror(errno
));
1198 getfile(xtrcmpfile
, xtrcmpskip
);
1201 if (read(ifile
, &c
, 1) != 0) {
1202 fprintf(stderr
, "%s: size has changed.\n",
1212 if (tmpfile
== NULL
) {
1213 /* argument to mktemp() must not be in RO space: */
1214 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1215 tmpfile
= mktemp(&tmpfilename
[0]);
1217 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1218 panic("cannot delete tmp file %s: %s\n",
1219 tmpfile
, strerror(errno
));
1221 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1222 panic("cannot create file temp file %s: %s\n",
1223 name
, strerror(errno
));
1225 getfile(xtrfile
, xtrskip
);
1226 (void) close(ofile
);
1227 #ifdef COMPARE_FAIL_KEEP_FILE
1228 if (cmpfiles(tmpfile
, name
, &sb
))
1231 cmpfiles(tmpfile
, name
, &sb
);
1234 #endif /* COMPARE_ONTHEFLY */
1241 * Read TP_BSIZE blocks from the input.
1242 * Handle read errors, and end of media.
1247 ssize_t rd
, newvol
, i
;
1248 int cnt
, seek_failed
;
1251 struct tapebuf
* tpb
;
1252 unsigned long worklen
;
1255 if (blkcnt
< numtrec
) {
1256 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1262 for (i
= 0; i
< ntrec
; i
++)
1263 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1266 cnt
= ntrec
* TP_BSIZE
;
1271 i
= rmtread(&tapebuf
[rd
], cnt
);
1274 i
= read(mt
, &tapebuf
[rd
], cnt
);
1279 if (i
== 0 && !pipein
)
1282 if (zflag
&& i
!= ntrec
* TP_BSIZE
) {
1283 tpb
= (struct tapebuf
*) tapebuf
;
1284 if (i
!= tpb
->clen
+ sizeof(struct tapebuf
))
1285 errx(1,"tape is not a compressed dump tape");
1287 cresult
= uncompress(comprbuf
, &worklen
, tpb
->buf
, tpb
->clen
);
1288 if (cresult
!= Z_OK
)
1289 errx(1,"tape is not a compressed dump tape");
1290 if (worklen
!= tpb
->unclen
)
1291 errx(1,"decompression error, length mismatch");
1295 #endif /* HAVE_ZLIB */
1298 * Check for mid-tape short read error.
1299 * If found, skip rest of buffer and start with the next.
1301 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1302 Dprintf(stdout
, "mid-media short read error.\n");
1306 * Handle partial block read.
1308 if (pipein
&& i
== 0 && rd
> 0)
1310 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1319 * Short read. Process the blocks read.
1321 if (i
% TP_BSIZE
!= 0)
1323 "partial block read: %ld should be %ld\n",
1324 (long)i
, ntrec
* TP_BSIZE
);
1325 numtrec
= i
/ TP_BSIZE
;
1329 * Handle read error.
1335 fprintf(stderr
, "Tape read error while ");
1336 switch (curfile
.action
) {
1338 fprintf(stderr
, "trying to set up tape\n");
1341 fprintf(stderr
, "trying to resynchronize\n");
1344 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1347 fprintf(stderr
, "skipping over inode %lu\n",
1348 (unsigned long)curfile
.ino
);
1351 if (!yflag
&& !reply("continue"))
1353 i
= ntrec
* TP_BSIZE
;
1354 memset(tapebuf
, 0, (size_t)i
);
1357 seek_failed
= (rmtseek(i
, 1) < 0);
1360 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1363 warn("continuation failed");
1364 if (!yflag
&& !reply("assume end-of-tape and continue"))
1370 * Handle end of tape.
1376 Vprintf(stdout
, "End-of-tape encountered\n");
1385 if (rd
% TP_BSIZE
!= 0)
1386 panic("partial block read: %d should be %d\n",
1387 rd
, ntrec
* TP_BSIZE
);
1389 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1392 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1398 findtapeblksize(void)
1402 for (i
= 0; i
< ntrec
; i
++)
1403 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1407 i
= rmtread(tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1410 i
= read(mt
, tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1413 err(1, "tape read error");
1414 if (i
% TP_BSIZE
!= 0)
1415 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1417 ntrec
= i
/ TP_BSIZE
;
1420 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1438 * Read the next block from the tape.
1439 * Check to see if it is one of several vintage headers.
1440 * If it is an old style header, convert it to a new style header.
1441 * If it is not any valid header, return an error.
1444 gethead(struct s_spcl
*buf
)
1452 char dummy
[TP_BSIZE
];
1459 u_int16_t c_inumber
;
1464 u_int16_t odi_nlink
;
1480 readtape((char *)buf
);
1481 if (buf
->c_magic
!= NFS_MAGIC
) {
1482 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1485 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1489 if (checksum((int *)buf
) == FAIL
)
1492 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1495 readtape((char *)(&u_ospcl
.s_ospcl
));
1496 memset((char *)buf
, 0, (long)TP_BSIZE
);
1497 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1498 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1499 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1500 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1501 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1502 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1503 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1504 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1505 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1506 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1507 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1508 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1509 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1510 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1512 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1513 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1514 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1515 #else /* __linux__ */
1516 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1517 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1518 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1519 #endif /* __linux__ */
1520 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1521 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1522 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1523 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1525 buf
->c_magic
= NFS_MAGIC
;
1528 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1529 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1530 qcvt
.qval
= buf
->c_dinode
.di_size
;
1531 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1532 printf("Note: Doing Quad swapping\n");
1537 qcvt
.qval
= buf
->c_dinode
.di_size
;
1539 qcvt
.val
[1] = qcvt
.val
[0];
1541 buf
->c_dinode
.di_size
= qcvt
.qval
;
1545 switch (buf
->c_type
) {
1550 * Have to patch up missing information in bit map headers
1553 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1554 if (buf
->c_count
> TP_NINDIR
)
1557 for (i
= 0; i
< buf
->c_count
; i
++)
1562 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1574 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1578 * If we are restoring a filesystem with old format inodes,
1579 * copy the uid/gid to the new location.
1582 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1583 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1591 * Check that a header is where it belongs and predict the next header
1594 accthdr(struct s_spcl
*header
)
1596 static ino_t previno
= 0x7fffffff;
1597 static int prevtype
;
1598 static long predict
;
1601 if (header
->c_type
== TS_TAPE
) {
1602 fprintf(stderr
, "Volume header (%s inode format) ",
1603 oldinofmt
? "old" : "new");
1604 if (header
->c_firstrec
)
1605 fprintf(stderr
, "begins with record %d",
1606 header
->c_firstrec
);
1607 fprintf(stderr
, "\n");
1608 previno
= 0x7fffffff;
1611 if (previno
== 0x7fffffff)
1615 fprintf(stderr
, "Dumped inodes map header");
1618 fprintf(stderr
, "Used inodes map header");
1621 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
1624 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
1627 fprintf(stderr
, "End of tape header");
1630 if (predict
!= blksread
- 1)
1631 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1632 predict
, blksread
- 1);
1633 fprintf(stderr
, "\n");
1636 if (header
->c_type
!= TS_END
)
1637 for (i
= 0; i
< header
->c_count
; i
++)
1638 if (readmapflag
|| header
->c_addr
[i
] != 0)
1642 prevtype
= header
->c_type
;
1643 previno
= header
->c_inumber
;
1647 * Find an inode header.
1648 * Complain if had to skip, and complain is set.
1651 findinode(struct s_spcl
*header
)
1653 static long skipcnt
= 0;
1657 curfile
.name
= "<name unknown>";
1658 curfile
.action
= UNKNOWN
;
1662 if (header
->c_magic
!= NFS_MAGIC
) {
1664 while (gethead(header
) == FAIL
||
1665 header
->c_date
!= dumpdate
)
1668 switch (header
->c_type
) {
1672 * Skip up to the beginning of the next record
1674 for (i
= 0; i
< header
->c_count
; i
++)
1675 if (header
->c_addr
[i
])
1677 while (gethead(header
) == FAIL
||
1678 header
->c_date
!= dumpdate
)
1683 curfile
.dip
= &header
->c_dinode
;
1684 curfile
.ino
= header
->c_inumber
;
1688 curfile
.ino
= maxino
;
1692 curfile
.name
= "<file removal list>";
1696 curfile
.name
= "<file dump list>";
1700 panic("unexpected tape header\n");
1704 panic("unknown tape header type %d\n", spcl
.c_type
);
1708 } while (header
->c_type
== TS_ADDR
);
1710 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1720 j
= sizeof(union u_spcl
) / sizeof(int);
1727 /* What happens if we want to read restore tapes
1728 for a 16bit int machine??? */
1734 if (i
!= CHECKSUM
) {
1735 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
1736 (unsigned long)curfile
.ino
, curfile
.name
);
1746 #include <varargs.h>
1751 msg(const char *fmt
, ...)
1764 (void)vfprintf(stderr
, fmt
, ap
);
1767 #endif /* RRESTORE */
1770 swab16(u_char
*sp
, int n
)
1775 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1782 swab32(u_char
*sp
, int n
)
1787 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1788 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
1795 swab64(u_char
*sp
, int n
)
1800 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
1801 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
1802 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
1803 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
1810 swabst(u_char
*cp
, u_char
*sp
)
1816 case '0': case '1': case '2': case '3': case '4':
1817 case '5': case '6': case '7': case '8': case '9':
1818 n
= (n
* 10) + (*cp
++ - '0');
1821 case 's': case 'w': case 'h':
1839 default: /* Any other character, like 'b' counts as byte. */
1853 swabst((u_char
*)"i", (u_char
*)&x
);
1861 swabst((u_char
*)"l", (u_char
*)&x
);