]>
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.25 2001/02/22 10:57:40 stelian Exp $";
53 #include <sys/param.h>
61 #include <linux/ext2_fs.h>
62 #include <bsdcompat.h>
64 #include <ufs/ufs/dinode.h>
65 #endif /* __linux__ */
66 #include <protocols/dumprestore.h>
69 #include <compaterr.h>
78 #endif /* HAVE_ZLIB */
81 #include <ext2fs/ext2fs.h>
86 #include "pathnames.h"
88 static long fssize
= MAXBSIZE
;
90 static int pipein
= 0;
91 static char magtape
[MAXPATHLEN
];
92 static char magtapeprefix
[MAXPATHLEN
];
96 static char *tbufptr
= NULL
; /* active tape buffer */
98 static char *comprbuf
; /* uncompress work buf */
99 static size_t comprlen
;
101 static union u_spcl endoftapemark
;
102 static long blksread
; /* blocks read since last header */
103 static long tpblksread
= 0; /* TP_BSIZE blocks read */
104 static long tapesread
;
105 static sigjmp_buf restart
;
106 static int gettingfile
= 0; /* restart has a valid frame */
107 static char *host
= NULL
;
111 static char lnkbuf
[MAXPATHLEN
+ 1];
114 int oldinofmt
; /* old inode format conversion required */
115 int Bcvt
; /* Swap Bytes (for CCI or sun) */
116 static int Qcvt
; /* Swap quads (for sun) */
118 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
120 static void accthdr
__P((struct s_spcl
*));
121 static int checksum
__P((int *));
122 static void findinode
__P((struct s_spcl
*));
123 static void findtapeblksize
__P((void));
124 static int gethead
__P((struct s_spcl
*));
125 static void readtape
__P((char *));
126 static void setdumpnum
__P((void));
127 static u_int swabi
__P((u_int
));
129 static u_long swabl
__P((u_long
));
131 static u_char
*swab64
__P((u_char
*, int));
132 static u_char
*swab32
__P((u_char
*, int));
133 static u_char
*swab16
__P((u_char
*, int));
134 static void terminateinput
__P((void));
135 static void xtrfile
__P((char *, size_t));
136 static void xtrlnkfile
__P((char *, size_t));
137 static void xtrlnkskip
__P((char *, size_t));
138 static void xtrmap
__P((char *, size_t));
139 static void xtrmapskip
__P((char *, size_t));
140 static void xtrskip
__P((char *, size_t));
142 #define COMPARE_ONTHEFLY 1
145 static int ifile
; /* input file for compare */
146 static int cmperror
; /* compare error */
147 static void xtrcmpfile
__P((char *, size_t));
148 static void xtrcmpskip
__P((char *, size_t));
151 static int readmapflag
;
154 * Set up an input source
157 setinput(char *source
)
163 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
167 if (strchr(source
, ':')) {
169 source
= strchr(host
, ':');
171 if (rmthost(host
) == 0)
175 if (strcmp(source
, "-") == 0) {
177 * Since input is coming from a pipe we must establish
178 * our own connection to the terminal.
180 terminal
= fopen(_PATH_TTY
, "r");
181 if (terminal
== NULL
) {
182 warn("cannot open %s", _PATH_TTY
);
183 terminal
= fopen(_PATH_DEVNULL
, "r");
184 if (terminal
== NULL
)
185 err(1, "cannot open %s", _PATH_DEVNULL
);
189 setuid(getuid()); /* no longer need or want root privileges */
191 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
192 magtapeprefix
[MAXPATHLEN
-1] = '\0';
193 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
196 strncpy(magtape
, source
, MAXPATHLEN
);
197 magtape
[MAXPATHLEN
- 1] = '\0';
201 newtapebuf(long size
)
203 static int tapebufsize
= -1;
206 if (size
<= tapebufsize
)
210 tapebuf
= malloc(size
* TP_BSIZE
);
212 errx(1, "Cannot allocate space for tape buffer");
215 if (comprbuf
!= NULL
)
217 comprlen
= size
* TP_BSIZE
;
218 comprbuf
= malloc(comprlen
);
219 if (comprbuf
== NULL
)
220 errx(1, "Cannot allocate space for uncompress buffer");
221 #endif /* HAVE_ZLIB */
225 * Verify that the tape drive can be accessed and
226 * that it actually is a dump tape.
234 Vprintf(stdout
, "Verify tape and initialize maps\n");
237 mt
= rmtopen(magtape
, 0);
243 mt
= open(magtape
, O_RDONLY
, 0);
245 err(1, "%s", magtape
);
249 if (!pipein
&& !bflag
)
251 if (gethead(&spcl
) == FAIL
) {
252 blkcnt
--; /* push back this block */
256 if (gethead(&spcl
) == FAIL
)
257 errx(1, "Tape is not a dump tape");
258 fprintf(stderr
, "Converting to new file system format.\n");
261 if (spcl
.c_flags
& DR_COMPRESSED
) {
262 fprintf(stderr
, "Dump tape is compressed.\n");
266 errx(1,"This restore version doesn't support decompression");
267 #endif /* HAVE_ZLIB */
270 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
271 endoftapemark
.s_spcl
.c_type
= TS_END
;
272 ip
= (int *)&endoftapemark
;
273 j
= sizeof(union u_spcl
) / sizeof(int);
278 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
280 if (vflag
|| command
== 't' || command
== 'C')
282 if (filesys
[0] == '\0') {
284 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
285 filesys
[NAMELEN
- 1] = '\0';
286 dirptr
= strstr(filesys
, " (dir");
290 dumptime
= spcl
.c_ddate
;
291 dumpdate
= spcl
.c_date
;
292 if (stat(".", &stbuf
) < 0)
293 err(1, "cannot stat .");
294 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
296 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
297 fssize
= stbuf
.st_blksize
;
298 if (((fssize
- 1) & fssize
) != 0)
299 errx(1, "bad block size %ld", fssize
);
300 if (spcl
.c_volume
!= 1)
301 errx(1, "Tape is not volume 1 of the dump");
302 if (gethead(&spcl
) == FAIL
) {
303 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
304 panic("no header after volume mark!\n");
307 if (spcl
.c_type
!= TS_CLRI
)
308 errx(1, "Cannot find file removal list");
309 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
310 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
311 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
313 errx(1, "no memory for active inode map");
315 curfile
.action
= USING
;
316 getfile(xtrmap
, xtrmapskip
);
317 if (spcl
.c_type
!= TS_BITS
)
318 errx(1, "Cannot find file dump list");
319 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
320 if (map
== (char *)NULL
)
321 errx(1, "no memory for file dump list");
323 curfile
.action
= USING
;
324 getfile(xtrmap
, xtrmapskip
);
326 * If there may be whiteout entries on the tape, pretend that the
327 * whiteout inode exists, so that the whiteout entries can be
331 SETINO(WINO
, dumpmap
);
335 * Prompt user to load a new dump volume.
336 * "Nextvol" is the next suggested volume to use.
337 * This suggested volume is enforced when doing full
338 * or incremental restores, but can be overridden by
339 * the user when only extracting a subset of the files.
344 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
345 union u_spcl tmpspcl
;
346 # define tmpbuf tmpspcl.s_spcl
356 panic("Changing volumes on pipe input?\n");
364 exit(1); /* pipes do not get a second chance */
365 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
372 while (newvol
<= 0) {
373 if (tapesread
== 0) {
374 fprintf(stderr
, "%s%s%s%s%s",
375 "You have not read any tapes yet.\n",
376 "Unless you know which volume your",
377 " file(s) are on you should start\n",
378 "with the last volume and work",
379 " towards the first.\n");
381 fprintf(stderr
, "You have read volumes");
383 for (i
= 1; i
< 32; i
++)
384 if (tapesread
& (1 << i
)) {
385 fprintf(stderr
, "%s%ld", buf
, (long)i
);
388 fprintf(stderr
, "\n");
391 fprintf(stderr
, "Specify next volume #: ");
392 (void) fflush(stderr
);
393 (void) fgets(buf
, TP_BSIZE
, terminal
);
394 } while (!feof(terminal
) && buf
[0] == '\n');
400 "Volume numbers are positive numerics\n");
403 if (newvol
== volno
) {
404 tapesread
|= 1 << volno
;
409 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
410 magtape
[MAXPATHLEN
- 1] = '\0';
412 if (!Mflag
|| haderror
) {
414 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
415 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
416 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
417 (void) fflush(stderr
);
418 (void) fgets(buf
, TP_BSIZE
, terminal
);
421 if (!strcmp(buf
, "none\n")) {
425 if (buf
[0] != '\n') {
426 (void) strcpy(magtape
, buf
);
427 magtape
[strlen(magtape
) - 1] = '\0';
432 mt
= rmtopen(magtape
, 0);
435 mt
= open(magtape
, O_RDONLY
, 0);
438 fprintf(stderr
, "Cannot open %s\n", magtape
);
447 if (gethead(&tmpbuf
) == FAIL
) {
448 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
449 fprintf(stderr
, "tape is not dump tape\n");
454 if (tmpbuf
.c_volume
!= volno
) {
455 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
460 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
462 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
463 ctime4(&tmpbuf
.c_date
));
464 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
466 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
467 ctime(&tmpbuf
.c_date
));
468 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
474 tapesread
|= 1 << volno
;
477 * If continuing from the previous volume, skip over any
478 * blocks read already at the end of the previous volume.
480 * If coming to this volume at random, skip to the beginning
481 * of the next record.
483 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
484 tpblksread
, (long)tmpbuf
.c_firstrec
);
485 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
487 tpblksread
= tmpbuf
.c_firstrec
;
488 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
490 } else if (tmpbuf
.c_firstrec
> 0 &&
491 tmpbuf
.c_firstrec
< tpblksread
- 1) {
493 * -1 since we've read the volume header
495 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
496 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
497 (long)i
, i
> 1 ? "s" : "");
502 if (curfile
.action
== USING
) {
504 panic("active file into volume 1\n");
508 * Skip up to the beginning of the next record
510 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
511 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
513 (void) gethead(&spcl
);
517 siglongjmp(restart
, 1);
522 * Handle unexpected EOF.
528 if (gettingfile
&& curfile
.action
== USING
) {
529 printf("Warning: %s %s\n",
530 "End-of-input encountered while extracting", curfile
.name
);
532 curfile
.name
= "<name unknown>";
533 curfile
.action
= UNKNOWN
;
535 curfile
.ino
= maxino
;
538 siglongjmp(restart
, 1);
543 * handle multiple dumps per tape by skipping forward to the
551 if (dumpnum
== 1 || volno
!= 1)
554 errx(1, "Cannot have multiple dumps on pipe input");
556 tcom
.mt_count
= dumpnum
- 1;
559 rmtioctl(MTFSF
, dumpnum
- 1);
562 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
570 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
571 fprintf(stdout
, "Dumped from: %s",
572 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
574 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
575 fprintf(stdout
, "Dumped from: %s",
576 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
578 if (spcl
.c_host
[0] == '\0')
580 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
581 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
582 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
586 extractfile(char *name
)
590 struct timeval timep
[2];
594 curfile
.action
= USING
;
596 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
597 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
598 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
599 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
600 #else /* __linux__ */
601 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
602 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
603 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
604 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
605 #endif /* __linux__ */
606 mode
= curfile
.dip
->di_mode
;
607 flags
= curfile
.dip
->di_flags
;
608 switch (mode
& IFMT
) {
611 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
616 Vprintf(stdout
, "skipped socket %s\n", name
);
622 ep
= lookupname(name
);
623 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
624 panic("unextracted directory %s\n", name
);
628 Vprintf(stdout
, "extract file %s\n", name
);
629 return (genliteraldir(name
, curfile
.ino
));
634 uid_t luid
= curfile
.dip
->di_uid
;
635 gid_t lgid
= curfile
.dip
->di_gid
;
639 getfile(xtrlnkfile
, xtrlnkskip
);
642 "%s: zero length symbolic link (ignored)\n", name
);
645 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
648 (void) lchown(name
, luid
, lgid
);
654 Vprintf(stdout
, "extract fifo %s\n", name
);
661 if (mkfifo(name
, mode
) < 0) {
662 warn("%s: cannot create fifo", name
);
666 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
667 (void) chmod(name
, mode
);
670 (void) fsetflags(name
, flags
);
672 (void) chflags(name
, flags
);
680 Vprintf(stdout
, "extract special file %s\n", name
);
687 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
688 warn("%s: cannot create special file", name
);
692 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
693 (void) chmod(name
, mode
);
697 warn("%s: fsetflags called on a special file", name
);
698 (void) fsetflags(name
, flags
);
701 (void) chflags(name
, flags
);
708 Vprintf(stdout
, "extract file %s\n", name
);
715 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
717 warn("%s: cannot create file", name
);
721 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
722 (void) fchmod(ofile
, mode
);
725 (void) setflags(ofile
, flags
);
727 (void) fchflags(ofile
, flags
);
729 getfile(xtrfile
, xtrskip
);
738 * skip over bit maps on the tape
744 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
749 * skip over a file on the tape
755 curfile
.action
= SKIP
;
756 getfile(xtrnull
, xtrnull
);
760 * Extract a file from the tape.
761 * When an allocated block is found it is passed to the fill function;
762 * when an unallocated block (hole) is found, a zeroed buffer is passed
763 * to the skip function.
766 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
769 volatile int curblk
= 0;
770 volatile quad_t size
= spcl
.c_dinode
.di_size
;
771 volatile int last_write_was_hole
= 0;
772 quad_t origsize
= size
;
773 static char clearedbuf
[MAXBSIZE
];
774 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
777 if (spcl
.c_type
== TS_END
)
778 panic("ran off end of tape\n");
779 if (spcl
.c_magic
!= NFS_MAGIC
)
780 panic("not at beginning of a file\n");
781 if (!gettingfile
&& setjmp(restart
) != 0)
785 for (i
= 0; i
< spcl
.c_count
; i
++) {
786 if (readmapflag
|| spcl
.c_addr
[i
]) {
787 readtape(&buf
[curblk
++][0]);
788 if (curblk
== fssize
/ TP_BSIZE
) {
789 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
790 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
792 last_write_was_hole
= 0;
796 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
798 (curblk
- 1) * TP_BSIZE
+ size
));
801 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
803 last_write_was_hole
= 1;
805 if ((size
-= TP_BSIZE
) <= 0) {
806 for (i
++; i
< spcl
.c_count
; i
++)
807 if (readmapflag
|| spcl
.c_addr
[i
])
812 if (gethead(&spcl
) == GOOD
&& size
> 0) {
813 if (spcl
.c_type
== TS_ADDR
)
816 "Missing address (header) block for %s at %ld blocks\n",
817 curfile
.name
, (long)blksread
);
820 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
821 last_write_was_hole
= 0;
823 if (last_write_was_hole
) {
824 ftruncate(ofile
, origsize
);
831 * Write out the next block of a file.
834 xtrfile(char *buf
, size_t size
)
839 if (write(ofile
, buf
, (int) size
) == -1)
840 err(1, "write error extracting inode %lu, name %s\nwrite",
841 (unsigned long)curfile
.ino
, curfile
.name
);
845 * Skip over a hole in a file.
849 xtrskip(char *buf
, size_t size
)
852 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
853 err(1, "seek error extracting inode %lu, name %s\nlseek",
854 (unsigned long)curfile
.ino
, curfile
.name
);
858 * Collect the next block of a symbolic link.
861 xtrlnkfile(char *buf
, size_t size
)
865 if (pathlen
> MAXPATHLEN
)
866 errx(1, "symbolic link name: %s->%s%s; too long %d",
867 curfile
.name
, lnkbuf
, buf
, pathlen
);
868 (void) strcat(lnkbuf
, buf
);
872 * Skip over a hole in a symbolic link (should never happen).
876 xtrlnkskip(char *buf
, size_t size
)
879 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
883 * Collect the next block of a bit map.
886 xtrmap(char *buf
, size_t size
)
889 memmove(map
, buf
, size
);
894 * Skip over a hole in a bit map (should never happen).
898 xtrmapskip(char *buf
, size_t size
)
901 panic("hole in map\n");
906 * Noop, when an extraction function is not needed.
910 xtrnull(char *buf
, size_t size
)
918 * Compare the next block of a file.
921 xtrcmpfile(char *buf
, size_t size
)
923 static char cmpbuf
[MAXBSIZE
];
928 if (read(ifile
, cmpbuf
, size
) != size
) {
929 fprintf(stderr
, "%s: size has changed.\n",
935 if (memcmp(buf
, cmpbuf
, size
) != 0) {
936 fprintf(stderr
, "%s: tape and disk copies are different\n",
944 * Skip over a hole in a file.
947 xtrcmpskip(char *buf
, size_t size
)
949 static char cmpbuf
[MAXBSIZE
];
955 if (read(ifile
, cmpbuf
, size
) != size
) {
956 fprintf(stderr
, "%s: size has changed.\n",
962 for (i
= 0; i
< size
; ++i
)
963 if (cmpbuf
[i
] != '\0') {
964 fprintf(stderr
, "%s: tape and disk copies are different\n",
970 #endif /* COMPARE_ONTHEFLY */
972 #if !COMPARE_ONTHEFLY
974 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
976 static char buf_tape
[BUFSIZ
];
977 static char buf_disk
[BUFSIZ
];
982 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
983 close(fd_tape
), close(fd_disk
);
984 panic("do_cmpfiles: unexpected EOF[1]");
986 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
987 close(fd_tape
), close(fd_disk
);
988 panic("do_cmpfiles: unexpected EOF[2]");
990 if (n_tape
!= n_disk
) {
991 close(fd_tape
), close(fd_disk
);
992 panic("do_cmpfiles: sizes different!");
994 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1000 /* for debugging compare problems */
1001 #undef COMPARE_FAIL_KEEP_FILE
1004 #ifdef COMPARE_FAIL_KEEP_FILE
1005 /* return true if tapefile should be unlinked after compare */
1010 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
1012 struct stat sbuf_tape
;
1013 int fd_tape
, fd_disk
;
1015 if (stat(tapefile
, &sbuf_tape
) != 0) {
1016 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1021 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1023 "%s: size changed from %ld to %ld.\n",
1024 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1026 #ifdef COMPARE_FAIL_KEEP_FILE
1033 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
1034 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1037 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
1039 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1043 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1044 fprintf(stderr
, "%s: tape and disk copies are different\n",
1049 #ifdef COMPARE_FAIL_KEEP_FILE
1050 /* rename the file to live in /tmp */
1051 /* rename `tapefile' to /tmp/<basename of diskfile> */
1053 char *p
= strrchr(diskfile
, '/');
1054 char newname
[MAXPATHLEN
];
1056 panic("can't find / in %s\n", diskfile
);
1058 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1059 if (rename(tapefile
, newname
)) {
1060 panic("rename from %s to %s failed: %s\n",
1064 fprintf(stderr
, "*** %s saved to %s\n",
1069 /* don't unlink the file (it's not there anymore */
1078 #ifdef COMPARE_FAIL_KEEP_FILE
1082 #endif /* !COMPARE_ONTHEFLY */
1084 #if !COMPARE_ONTHEFLY
1085 static char tmpfilename
[MAXPATHLEN
];
1089 comparefile(char *name
)
1094 #if !COMPARE_ONTHEFLY
1095 static char *tmpfile
= NULL
;
1099 if ((r
= lstat(name
, &sb
)) != 0) {
1100 warn("%s: does not exist (%d)", name
, r
);
1106 curfile
.name
= name
;
1107 curfile
.action
= USING
;
1108 mode
= curfile
.dip
->di_mode
;
1110 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1111 (long)sb
.st_size
, mode
);
1113 if (sb
.st_mode
!= mode
) {
1114 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1115 name
, mode
& 07777, sb
.st_mode
& 07777);
1118 switch (mode
& IFMT
) {
1132 char lbuf
[MAXPATHLEN
+ 1];
1135 if (!(sb
.st_mode
& S_IFLNK
)) {
1136 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1143 getfile(xtrlnkfile
, xtrlnkskip
);
1146 "%s: zero length symbolic link (ignored)\n",
1151 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1152 panic("readlink of %s failed: %s", name
,
1157 if (strcmp(lbuf
, lnkbuf
) != 0) {
1159 "%s: symbolic link changed from %s to %s.\n",
1160 name
, lnkbuf
, lbuf
);
1169 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1170 fprintf(stderr
, "%s: no longer a special file\n",
1177 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1179 "%s: device changed from %d,%d to %d,%d.\n",
1181 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1182 (int)curfile
.dip
->di_rdev
& 0xff,
1183 ((int)sb
.st_rdev
>> 8) & 0xff,
1184 (int)sb
.st_rdev
& 0xff);
1191 #if COMPARE_ONTHEFLY
1192 if ((ifile
= open(name
, O_RDONLY
)) < 0) {
1193 panic("Can't open %s: %s\n", name
, strerror(errno
));
1199 getfile(xtrcmpfile
, xtrcmpskip
);
1202 if (read(ifile
, &c
, 1) != 0) {
1203 fprintf(stderr
, "%s: size has changed.\n",
1213 if (tmpfile
== NULL
) {
1214 /* argument to mktemp() must not be in RO space: */
1215 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1216 tmpfile
= mktemp(&tmpfilename
[0]);
1218 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1219 panic("cannot delete tmp file %s: %s\n",
1220 tmpfile
, strerror(errno
));
1222 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1223 panic("cannot create file temp file %s: %s\n",
1224 name
, strerror(errno
));
1226 getfile(xtrfile
, xtrskip
);
1227 (void) close(ofile
);
1228 #ifdef COMPARE_FAIL_KEEP_FILE
1229 if (cmpfiles(tmpfile
, name
, &sb
))
1232 cmpfiles(tmpfile
, name
, &sb
);
1235 #endif /* COMPARE_ONTHEFLY */
1242 * Read TP_BSIZE blocks from the input.
1243 * Handle read errors, and end of media.
1248 ssize_t rd
, newvol
, i
;
1249 int cnt
, seek_failed
;
1252 struct tapebuf
* tpb
;
1253 unsigned long worklen
;
1256 if (blkcnt
< numtrec
) {
1257 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1263 for (i
= 0; i
< ntrec
; i
++)
1264 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1267 cnt
= ntrec
* TP_BSIZE
;
1272 i
= rmtread(&tapebuf
[rd
], cnt
);
1275 i
= read(mt
, &tapebuf
[rd
], cnt
);
1280 if (i
== 0 && !pipein
)
1283 if (zflag
&& i
!= ntrec
* TP_BSIZE
) {
1284 tpb
= (struct tapebuf
*) tapebuf
;
1285 if (i
!= tpb
->clen
+ sizeof(struct tapebuf
))
1286 errx(1,"tape is not a compressed dump tape");
1288 cresult
= uncompress(comprbuf
, &worklen
, tpb
->buf
, tpb
->clen
);
1289 if (cresult
!= Z_OK
)
1290 errx(1,"tape is not a compressed dump tape");
1291 if (worklen
!= tpb
->unclen
)
1292 errx(1,"decompression error, length mismatch");
1296 #endif /* HAVE_ZLIB */
1299 * Check for mid-tape short read error.
1300 * If found, skip rest of buffer and start with the next.
1302 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1303 Dprintf(stdout
, "mid-media short read error.\n");
1307 * Handle partial block read.
1309 if (pipein
&& i
== 0 && rd
> 0)
1311 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1320 * Short read. Process the blocks read.
1322 if (i
% TP_BSIZE
!= 0)
1324 "partial block read: %ld should be %ld\n",
1325 (long)i
, ntrec
* TP_BSIZE
);
1326 numtrec
= i
/ TP_BSIZE
;
1330 * Handle read error.
1336 fprintf(stderr
, "Tape read error while ");
1337 switch (curfile
.action
) {
1339 fprintf(stderr
, "trying to set up tape\n");
1342 fprintf(stderr
, "trying to resynchronize\n");
1345 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1348 fprintf(stderr
, "skipping over inode %lu\n",
1349 (unsigned long)curfile
.ino
);
1352 if (!yflag
&& !reply("continue"))
1354 i
= ntrec
* TP_BSIZE
;
1355 memset(tapebuf
, 0, (size_t)i
);
1358 seek_failed
= (rmtseek(i
, 1) < 0);
1361 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1364 warn("continuation failed");
1365 if (!yflag
&& !reply("assume end-of-tape and continue"))
1371 * Handle end of tape.
1377 Vprintf(stdout
, "End-of-tape encountered\n");
1386 if (rd
% TP_BSIZE
!= 0)
1387 panic("partial block read: %d should be %d\n",
1388 rd
, ntrec
* TP_BSIZE
);
1390 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1393 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1399 findtapeblksize(void)
1403 for (i
= 0; i
< ntrec
; i
++)
1404 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1408 i
= rmtread(tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1411 i
= read(mt
, tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1414 err(1, "tape read error");
1415 if (i
% TP_BSIZE
!= 0)
1416 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1418 ntrec
= i
/ TP_BSIZE
;
1421 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1439 * Read the next block from the tape.
1440 * Check to see if it is one of several vintage headers.
1441 * If it is an old style header, convert it to a new style header.
1442 * If it is not any valid header, return an error.
1445 gethead(struct s_spcl
*buf
)
1453 char dummy
[TP_BSIZE
];
1460 u_int16_t c_inumber
;
1465 u_int16_t odi_nlink
;
1481 readtape((char *)buf
);
1482 if (buf
->c_magic
!= NFS_MAGIC
) {
1483 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1486 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1490 if (checksum((int *)buf
) == FAIL
)
1493 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1496 readtape((char *)(&u_ospcl
.s_ospcl
));
1497 memset((char *)buf
, 0, (long)TP_BSIZE
);
1498 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1499 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1500 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1501 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1502 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1503 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1504 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1505 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1506 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1507 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1508 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1509 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1510 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1511 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1513 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1514 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1515 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1516 #else /* __linux__ */
1517 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1518 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1519 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1520 #endif /* __linux__ */
1521 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1522 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1523 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1524 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1526 buf
->c_magic
= NFS_MAGIC
;
1529 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1530 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1531 qcvt
.qval
= buf
->c_dinode
.di_size
;
1532 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1533 printf("Note: Doing Quad swapping\n");
1538 qcvt
.qval
= buf
->c_dinode
.di_size
;
1540 qcvt
.val
[1] = qcvt
.val
[0];
1542 buf
->c_dinode
.di_size
= qcvt
.qval
;
1546 switch (buf
->c_type
) {
1551 * Have to patch up missing information in bit map headers
1554 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1555 if (buf
->c_count
> TP_NINDIR
)
1558 for (i
= 0; i
< buf
->c_count
; i
++)
1563 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1575 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1579 * If we are restoring a filesystem with old format inodes,
1580 * copy the uid/gid to the new location.
1583 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1584 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1592 * Check that a header is where it belongs and predict the next header
1595 accthdr(struct s_spcl
*header
)
1597 static ino_t previno
= 0x7fffffff;
1598 static int prevtype
;
1599 static long predict
;
1602 if (header
->c_type
== TS_TAPE
) {
1603 fprintf(stderr
, "Volume header (%s inode format) ",
1604 oldinofmt
? "old" : "new");
1605 if (header
->c_firstrec
)
1606 fprintf(stderr
, "begins with record %d",
1607 header
->c_firstrec
);
1608 fprintf(stderr
, "\n");
1609 previno
= 0x7fffffff;
1612 if (previno
== 0x7fffffff)
1616 fprintf(stderr
, "Dumped inodes map header");
1619 fprintf(stderr
, "Used inodes map header");
1622 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
1625 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
1628 fprintf(stderr
, "End of tape header");
1631 if (predict
!= blksread
- 1)
1632 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1633 predict
, blksread
- 1);
1634 fprintf(stderr
, "\n");
1637 if (header
->c_type
!= TS_END
)
1638 for (i
= 0; i
< header
->c_count
; i
++)
1639 if (readmapflag
|| header
->c_addr
[i
] != 0)
1643 prevtype
= header
->c_type
;
1644 previno
= header
->c_inumber
;
1648 * Find an inode header.
1649 * Complain if had to skip, and complain is set.
1652 findinode(struct s_spcl
*header
)
1654 static long skipcnt
= 0;
1658 curfile
.name
= "<name unknown>";
1659 curfile
.action
= UNKNOWN
;
1663 if (header
->c_magic
!= NFS_MAGIC
) {
1665 while (gethead(header
) == FAIL
||
1666 header
->c_date
!= dumpdate
)
1669 switch (header
->c_type
) {
1673 * Skip up to the beginning of the next record
1675 for (i
= 0; i
< header
->c_count
; i
++)
1676 if (header
->c_addr
[i
])
1678 while (gethead(header
) == FAIL
||
1679 header
->c_date
!= dumpdate
)
1684 curfile
.dip
= &header
->c_dinode
;
1685 curfile
.ino
= header
->c_inumber
;
1689 curfile
.ino
= maxino
;
1693 curfile
.name
= "<file removal list>";
1697 curfile
.name
= "<file dump list>";
1701 panic("unexpected tape header\n");
1705 panic("unknown tape header type %d\n", spcl
.c_type
);
1709 } while (header
->c_type
== TS_ADDR
);
1711 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1721 j
= sizeof(union u_spcl
) / sizeof(int);
1728 /* What happens if we want to read restore tapes
1729 for a 16bit int machine??? */
1735 if (i
!= CHECKSUM
) {
1736 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
1737 (unsigned long)curfile
.ino
, curfile
.name
);
1747 #include <varargs.h>
1752 msg(const char *fmt
, ...)
1765 (void)vfprintf(stderr
, fmt
, ap
);
1768 #endif /* RRESTORE */
1771 swab16(u_char
*sp
, int n
)
1776 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1783 swab32(u_char
*sp
, int n
)
1788 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1789 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
1796 swab64(u_char
*sp
, int n
)
1801 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
1802 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
1803 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
1804 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
1811 swabst(u_char
*cp
, u_char
*sp
)
1817 case '0': case '1': case '2': case '3': case '4':
1818 case '5': case '6': case '7': case '8': case '9':
1819 n
= (n
* 10) + (*cp
++ - '0');
1822 case 's': case 'w': case 'h':
1840 default: /* Any other character, like 'b' counts as byte. */
1854 swabst((u_char
*)"i", (u_char
*)&x
);
1862 swabst((u_char
*)"l", (u_char
*)&x
);