]>
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@cybercable.fr>, 1999-2000
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 * (c) UNIX System Laboratories, Inc.
12 * All or some portions of this file are derived from material licensed
13 * to the University of California by American Telephone and Telegraph
14 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
15 * the permission of UNIX System Laboratories, Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 static const char rcsid
[] =
48 "$Id: tape.c,v 1.15 2000/05/28 16:52:21 stelian Exp $";
51 #include <sys/param.h>
58 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
61 #include <ufs/ufs/dinode.h>
62 #endif /* __linux__ */
63 #include <protocols/dumprestore.h>
66 #include <compaterr.h>
74 #include <ext2fs/ext2fs.h>
79 #include "pathnames.h"
81 static long fssize
= MAXBSIZE
;
83 static int pipein
= 0;
84 static char magtape
[MAXPATHLEN
];
85 static char magtapeprefix
[MAXPATHLEN
];
89 static union u_spcl endoftapemark
;
90 static long blksread
; /* blocks read since last header */
91 static long tpblksread
= 0; /* TP_BSIZE blocks read */
92 static long tapesread
;
93 static sigjmp_buf restart
;
94 static int gettingfile
= 0; /* restart has a valid frame */
95 static char *host
= NULL
;
99 static char lnkbuf
[MAXPATHLEN
+ 1];
102 int oldinofmt
; /* old inode format conversion required */
103 int Bcvt
; /* Swap Bytes (for CCI or sun) */
104 static int Qcvt
; /* Swap quads (for sun) */
106 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
108 static void accthdr
__P((struct s_spcl
*));
109 static int checksum
__P((int *));
110 static void findinode
__P((struct s_spcl
*));
111 static void findtapeblksize
__P((void));
112 static int gethead
__P((struct s_spcl
*));
113 static void readtape
__P((char *));
114 static void setdumpnum
__P((void));
115 static u_int swabi
__P((u_int
));
116 static u_long swabl
__P((u_long
));
117 static u_char
*swab64
__P((u_char
*, int));
118 static u_char
*swab32
__P((u_char
*, int));
119 static u_char
*swab16
__P((u_char
*, int));
120 static void terminateinput
__P((void));
121 static void xtrfile
__P((char *, size_t));
122 static void xtrlnkfile
__P((char *, size_t));
123 static void xtrlnkskip
__P((char *, size_t));
124 static void xtrmap
__P((char *, size_t));
125 static void xtrmapskip
__P((char *, size_t));
126 static void xtrskip
__P((char *, size_t));
128 static int readmapflag
;
131 * Set up an input source
134 setinput(char *source
)
140 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
144 if (strchr(source
, ':')) {
146 source
= strchr(host
, ':');
148 if (rmthost(host
) == 0)
152 if (strcmp(source
, "-") == 0) {
154 * Since input is coming from a pipe we must establish
155 * our own connection to the terminal.
157 terminal
= fopen(_PATH_TTY
, "r");
158 if (terminal
== NULL
) {
159 warn("cannot open %s", _PATH_TTY
);
160 terminal
= fopen(_PATH_DEVNULL
, "r");
161 if (terminal
== NULL
)
162 err(1, "cannot open %s", _PATH_DEVNULL
);
166 setuid(getuid()); /* no longer need or want root privileges */
168 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
169 magtapeprefix
[MAXPATHLEN
-1] = '\0';
170 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
173 strncpy(magtape
, source
, MAXPATHLEN
);
174 magtape
[MAXPATHLEN
- 1] = '\0';
178 newtapebuf(long size
)
180 static int tapebufsize
= -1;
183 if (size
<= tapebufsize
)
187 tapebuf
= malloc(size
* TP_BSIZE
);
189 errx(1, "Cannot allocate space for tape buffer");
194 * Verify that the tape drive can be accessed and
195 * that it actually is a dump tape.
203 Vprintf(stdout
, "Verify tape and initialize maps\n");
206 mt
= rmtopen(magtape
, 0);
212 mt
= open(magtape
, O_RDONLY
, 0);
214 err(1, "%s", magtape
);
218 if (!pipein
&& !bflag
)
220 if (gethead(&spcl
) == FAIL
) {
221 blkcnt
--; /* push back this block */
225 if (gethead(&spcl
) == FAIL
)
226 errx(1, "Tape is not a dump tape");
227 fprintf(stderr
, "Converting to new file system format.\n");
230 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
231 endoftapemark
.s_spcl
.c_type
= TS_END
;
232 ip
= (int *)&endoftapemark
;
233 j
= sizeof(union u_spcl
) / sizeof(int);
238 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
240 if (vflag
|| command
== 't' || command
== 'C')
242 if (filesys
[0] == '\0') {
244 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
245 filesys
[NAMELEN
- 1] = '\0';
246 dirptr
= strstr(filesys
, " (dir");
250 dumptime
= spcl
.c_ddate
;
251 dumpdate
= spcl
.c_date
;
252 if (stat(".", &stbuf
) < 0)
253 err(1, "cannot stat .");
254 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
256 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
257 fssize
= stbuf
.st_blksize
;
258 if (((fssize
- 1) & fssize
) != 0)
259 errx(1, "bad block size %ld", fssize
);
260 if (spcl
.c_volume
!= 1)
261 errx(1, "Tape is not volume 1 of the dump");
262 if (gethead(&spcl
) == FAIL
) {
263 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
264 panic("no header after volume mark!\n");
267 if (spcl
.c_type
!= TS_CLRI
)
268 errx(1, "Cannot find file removal list");
269 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
270 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
271 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
273 panic("no memory for active inode map\n");
275 curfile
.action
= USING
;
276 getfile(xtrmap
, xtrmapskip
);
277 if (spcl
.c_type
!= TS_BITS
)
278 errx(1, "Cannot find file dump list");
279 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
280 if (map
== (char *)NULL
)
281 panic("no memory for file dump list\n");
283 curfile
.action
= USING
;
284 getfile(xtrmap
, xtrmapskip
);
286 * If there may be whiteout entries on the tape, pretend that the
287 * whiteout inode exists, so that the whiteout entries can be
291 SETINO(WINO
, dumpmap
);
295 * Prompt user to load a new dump volume.
296 * "Nextvol" is the next suggested volume to use.
297 * This suggested volume is enforced when doing full
298 * or incremental restores, but can be overridden by
299 * the user when only extracting a subset of the files.
304 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
305 union u_spcl tmpspcl
;
306 # define tmpbuf tmpspcl.s_spcl
316 panic("Changing volumes on pipe input?\n");
324 exit(1); /* pipes do not get a second chance */
325 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
332 while (newvol
<= 0) {
333 if (tapesread
== 0) {
334 fprintf(stderr
, "%s%s%s%s%s",
335 "You have not read any tapes yet.\n",
336 "Unless you know which volume your",
337 " file(s) are on you should start\n",
338 "with the last volume and work",
339 " towards the first.\n");
341 fprintf(stderr
, "You have read volumes");
343 for (i
= 1; i
< 32; i
++)
344 if (tapesread
& (1 << i
)) {
345 fprintf(stderr
, "%s%ld", buf
, (long)i
);
348 fprintf(stderr
, "\n");
351 fprintf(stderr
, "Specify next volume #: ");
352 (void) fflush(stderr
);
353 (void) fgets(buf
, BUFSIZ
, terminal
);
354 } while (!feof(terminal
) && buf
[0] == '\n');
360 "Volume numbers are positive numerics\n");
363 if (newvol
== volno
) {
364 tapesread
|= 1 << volno
;
369 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
370 magtape
[MAXPATHLEN
- 1] = '\0';
372 if (!Mflag
|| haderror
) {
374 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
375 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
376 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
377 (void) fflush(stderr
);
378 (void) fgets(buf
, BUFSIZ
, terminal
);
381 if (!strcmp(buf
, "none\n")) {
385 if (buf
[0] != '\n') {
386 (void) strcpy(magtape
, buf
);
387 magtape
[strlen(magtape
) - 1] = '\0';
392 mt
= rmtopen(magtape
, 0);
395 mt
= open(magtape
, O_RDONLY
, 0);
398 fprintf(stderr
, "Cannot open %s\n", magtape
);
407 if (gethead(&tmpbuf
) == FAIL
) {
408 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
409 fprintf(stderr
, "tape is not dump tape\n");
414 if (tmpbuf
.c_volume
!= volno
) {
415 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
420 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
422 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
423 ctime4(&tmpbuf
.c_date
));
424 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
426 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
427 ctime(&tmpbuf
.c_date
));
428 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
434 tapesread
|= 1 << volno
;
437 * If continuing from the previous volume, skip over any
438 * blocks read already at the end of the previous volume.
440 * If coming to this volume at random, skip to the beginning
441 * of the next record.
443 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
444 tpblksread
, (long)tmpbuf
.c_firstrec
);
445 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
447 tpblksread
= tmpbuf
.c_firstrec
;
448 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
450 } else if (tmpbuf
.c_firstrec
> 0 &&
451 tmpbuf
.c_firstrec
< tpblksread
- 1) {
453 * -1 since we've read the volume header
455 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
456 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
457 (long)i
, i
> 1 ? "s" : "");
462 if (curfile
.action
== USING
) {
464 panic("active file into volume 1\n");
468 * Skip up to the beginning of the next record
470 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
471 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
473 (void) gethead(&spcl
);
477 siglongjmp(restart
, 1);
482 * Handle unexpected EOF.
488 if (gettingfile
&& curfile
.action
== USING
) {
489 printf("Warning: %s %s\n",
490 "End-of-input encountered while extracting", curfile
.name
);
492 curfile
.name
= "<name unknown>";
493 curfile
.action
= UNKNOWN
;
495 curfile
.ino
= maxino
;
498 siglongjmp(restart
, 1);
503 * handle multiple dumps per tape by skipping forward to the
511 if (dumpnum
== 1 || volno
!= 1)
514 errx(1, "Cannot have multiple dumps on pipe input");
516 tcom
.mt_count
= dumpnum
- 1;
519 rmtioctl(MTFSF
, dumpnum
- 1);
522 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
530 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
531 fprintf(stdout
, "Dumped from: %s",
532 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
534 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
535 fprintf(stdout
, "Dumped from: %s",
536 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
538 if (spcl
.c_host
[0] == '\0')
540 fprintf(stderr
, "Level %d dump of %s on %s:%s\n",
541 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
542 fprintf(stderr
, "Label: %s\n", spcl
.c_label
);
546 extractfile(char *name
)
550 struct timeval timep
[2];
554 curfile
.action
= USING
;
556 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
557 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
558 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
559 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
560 #else /* __linux__ */
561 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
562 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
563 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
564 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
565 #endif /* __linux__ */
566 mode
= curfile
.dip
->di_mode
;
567 flags
= curfile
.dip
->di_flags
;
568 switch (mode
& IFMT
) {
571 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
576 Vprintf(stdout
, "skipped socket %s\n", name
);
582 ep
= lookupname(name
);
583 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
584 panic("unextracted directory %s\n", name
);
588 Vprintf(stdout
, "extract file %s\n", name
);
589 return (genliteraldir(name
, curfile
.ino
));
594 uid_t luid
= curfile
.dip
->di_uid
;
595 gid_t lgid
= curfile
.dip
->di_gid
;
599 getfile(xtrlnkfile
, xtrlnkskip
);
602 "%s: zero length symbolic link (ignored)\n", name
);
605 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
608 (void) lchown(name
, luid
, lgid
);
614 Vprintf(stdout
, "extract fifo %s\n", name
);
621 if (mkfifo(name
, mode
) < 0) {
622 warn("%s: cannot create fifo", name
);
626 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
627 (void) chmod(name
, mode
);
630 (void) fsetflags(name
, flags
);
632 (void) chflags(name
, flags
);
640 Vprintf(stdout
, "extract special file %s\n", name
);
647 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
648 warn("%s: cannot create special file", name
);
652 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
653 (void) chmod(name
, mode
);
657 warn("%s: fsetflags called on a special file", name
);
658 (void) fsetflags(name
, flags
);
661 (void) chflags(name
, flags
);
668 Vprintf(stdout
, "extract file %s\n", name
);
675 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
677 warn("%s: cannot create file", name
);
681 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
682 (void) fchmod(ofile
, mode
);
685 (void) setflags(ofile
, flags
);
687 (void) fchflags(ofile
, flags
);
689 getfile(xtrfile
, xtrskip
);
698 * skip over bit maps on the tape
704 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
709 * skip over a file on the tape
715 curfile
.action
= SKIP
;
716 getfile(xtrnull
, xtrnull
);
720 * Extract a file from the tape.
721 * When an allocated block is found it is passed to the fill function;
722 * when an unallocated block (hole) is found, a zeroed buffer is passed
723 * to the skip function.
726 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
729 volatile int curblk
= 0;
730 volatile quad_t size
= spcl
.c_dinode
.di_size
;
731 volatile int last_write_was_hole
= 0;
732 quad_t origsize
= size
;
733 static char clearedbuf
[MAXBSIZE
];
734 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
737 if (spcl
.c_type
== TS_END
)
738 panic("ran off end of tape\n");
739 if (spcl
.c_magic
!= NFS_MAGIC
)
740 panic("not at beginning of a file\n");
741 if (!gettingfile
&& setjmp(restart
) != 0)
745 for (i
= 0; i
< spcl
.c_count
; i
++) {
746 if (readmapflag
|| spcl
.c_addr
[i
]) {
747 readtape(&buf
[curblk
++][0]);
748 if (curblk
== fssize
/ TP_BSIZE
) {
749 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
750 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
752 last_write_was_hole
= 0;
756 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
758 (curblk
- 1) * TP_BSIZE
+ size
));
761 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
763 last_write_was_hole
= 1;
765 if ((size
-= TP_BSIZE
) <= 0) {
766 for (i
++; i
< spcl
.c_count
; i
++)
767 if (readmapflag
|| spcl
.c_addr
[i
])
772 if (gethead(&spcl
) == GOOD
&& size
> 0) {
773 if (spcl
.c_type
== TS_ADDR
)
776 "Missing address (header) block for %s at %ld blocks\n",
777 curfile
.name
, (long)blksread
);
780 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
781 last_write_was_hole
= 0;
783 if (last_write_was_hole
) {
784 ftruncate(ofile
, origsize
);
791 * Write out the next block of a file.
794 xtrfile(char *buf
, size_t size
)
799 if (write(ofile
, buf
, (int) size
) == -1)
800 err(1, "write error extracting inode %lu, name %s\nwrite",
801 (unsigned long)curfile
.ino
, curfile
.name
);
805 * Skip over a hole in a file.
809 xtrskip(char *buf
, size_t size
)
812 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
813 err(1, "seek error extracting inode %lu, name %s\nlseek",
814 (unsigned long)curfile
.ino
, curfile
.name
);
818 * Collect the next block of a symbolic link.
821 xtrlnkfile(char *buf
, size_t size
)
825 if (pathlen
> MAXPATHLEN
)
826 errx(1, "symbolic link name: %s->%s%s; too long %d",
827 curfile
.name
, lnkbuf
, buf
, pathlen
);
828 (void) strcat(lnkbuf
, buf
);
832 * Skip over a hole in a symbolic link (should never happen).
836 xtrlnkskip(char *buf
, size_t size
)
839 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
843 * Collect the next block of a bit map.
846 xtrmap(char *buf
, size_t size
)
849 memmove(map
, buf
, size
);
854 * Skip over a hole in a bit map (should never happen).
858 xtrmapskip(char *buf
, size_t size
)
861 panic("hole in map\n");
866 * Noop, when an extraction function is not needed.
870 xtrnull(char *buf
, size_t size
)
877 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
879 static char buf_tape
[BUFSIZ
];
880 static char buf_disk
[BUFSIZ
];
885 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
886 close(fd_tape
), close(fd_disk
);
887 panic("do_cmpfiles: unexpected EOF[1]");
889 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
890 close(fd_tape
), close(fd_disk
);
891 panic("do_cmpfiles: unexpected EOF[2]");
893 if (n_tape
!= n_disk
) {
894 close(fd_tape
), close(fd_disk
);
895 panic("do_cmpfiles: sizes different!");
897 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
903 /* for debugging compare problems */
904 #undef COMPARE_FAIL_KEEP_FILE
907 #ifdef COMPARE_FAIL_KEEP_FILE
908 /* return true if tapefile should be unlinked after compare */
913 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
915 struct stat sbuf_tape
;
916 int fd_tape
, fd_disk
;
918 if (stat(tapefile
, &sbuf_tape
) != 0) {
919 panic("Can't lstat tmp file %s: %s\n", tapefile
,
924 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
926 "%s: size changed from %ld to %ld.\n",
927 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
929 #ifdef COMPARE_FAIL_KEEP_FILE
936 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
937 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
940 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
942 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
946 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
947 fprintf(stderr
, "%s: tape and disk copies are different\n",
952 #ifdef COMPARE_FAIL_KEEP_FILE
953 /* rename the file to live in /tmp */
954 /* rename `tapefile' to /tmp/<basename of diskfile> */
956 char *p
= strrchr(diskfile
, '/');
957 char newname
[MAXPATHLEN
];
959 panic("can't find / in %s\n", diskfile
);
961 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
962 if (rename(tapefile
, newname
)) {
963 panic("rename from %s to %s failed: %s\n",
967 fprintf(stderr
, "*** %s saved to %s\n",
972 /* don't unlink the file (it's not there anymore */
981 #ifdef COMPARE_FAIL_KEEP_FILE
986 static char tmpfilename
[MAXPATHLEN
];
989 comparefile(char *name
)
991 static char *tmpfile
= NULL
;
993 struct stat sb
, stemp
;
996 if ((r
= lstat(name
, &sb
)) != 0) {
997 warn("%s: does not exist (%d)", name
, r
);
1003 curfile
.name
= name
;
1004 curfile
.action
= USING
;
1005 mode
= curfile
.dip
->di_mode
;
1007 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1008 (long)sb
.st_size
, mode
);
1010 if (sb
.st_mode
!= mode
) {
1011 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1012 name
, mode
& 07777, sb
.st_mode
& 07777);
1015 switch (mode
& IFMT
) {
1029 char lbuf
[MAXPATHLEN
+ 1];
1032 if (!(sb
.st_mode
& S_IFLNK
)) {
1033 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1040 getfile(xtrlnkfile
, xtrlnkskip
);
1043 "%s: zero length symbolic link (ignored)\n",
1048 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1049 panic("readlink of %s failed: %s", name
,
1054 if (strcmp(lbuf
, lnkbuf
) != 0) {
1056 "%s: symbolic link changed from %s to %s.\n",
1057 name
, lnkbuf
, lbuf
);
1066 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1067 fprintf(stderr
, "%s: no longer a special file\n",
1074 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1076 "%s: device changed from %d,%d to %d,%d.\n",
1078 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1079 (int)curfile
.dip
->di_rdev
& 0xff,
1080 ((int)sb
.st_rdev
>> 8) & 0xff,
1081 (int)sb
.st_rdev
& 0xff);
1088 if (tmpfile
== NULL
) {
1089 /* argument to mktemp() must not be in RO space: */
1090 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1091 tmpfile
= mktemp(&tmpfilename
[0]);
1093 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1094 panic("cannot delete tmp file %s: %s\n",
1095 tmpfile
, strerror(errno
));
1097 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1098 panic("cannot create file temp file %s: %s\n",
1099 name
, strerror(errno
));
1101 getfile(xtrfile
, xtrskip
);
1102 (void) close(ofile
);
1103 #ifdef COMPARE_FAIL_KEEP_FILE
1104 if (cmpfiles(tmpfile
, name
, &sb
))
1107 cmpfiles(tmpfile
, name
, &sb
);
1116 * Read TP_BSIZE blocks from the input.
1117 * Handle read errors, and end of media.
1122 ssize_t rd
, newvol
, i
;
1123 int cnt
, seek_failed
;
1125 if (blkcnt
< numtrec
) {
1126 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1131 for (i
= 0; i
< ntrec
; i
++)
1132 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1135 cnt
= ntrec
* TP_BSIZE
;
1140 i
= rmtread(&tapebuf
[rd
], cnt
);
1143 i
= read(mt
, &tapebuf
[rd
], cnt
);
1145 * Check for mid-tape short read error.
1146 * If found, skip rest of buffer and start with the next.
1148 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1149 Dprintf(stdout
, "mid-media short read error.\n");
1153 * Handle partial block read.
1155 if (pipein
&& i
== 0 && rd
> 0)
1157 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1166 * Short read. Process the blocks read.
1168 if (i
% TP_BSIZE
!= 0)
1170 "partial block read: %ld should be %ld\n",
1171 (long)i
, ntrec
* TP_BSIZE
);
1172 numtrec
= i
/ TP_BSIZE
;
1176 * Handle read error.
1179 fprintf(stderr
, "Tape read error while ");
1180 switch (curfile
.action
) {
1182 fprintf(stderr
, "trying to set up tape\n");
1185 fprintf(stderr
, "trying to resynchronize\n");
1188 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1191 fprintf(stderr
, "skipping over inode %lu\n",
1192 (unsigned long)curfile
.ino
);
1195 if (!yflag
&& !reply("continue"))
1197 i
= ntrec
* TP_BSIZE
;
1198 memset(tapebuf
, 0, (size_t)i
);
1201 seek_failed
= (rmtseek(i
, 1) < 0);
1204 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1207 err(1, "continuation failed");
1210 * Handle end of tape.
1213 Vprintf(stdout
, "End-of-tape encountered\n");
1222 if (rd
% TP_BSIZE
!= 0)
1223 panic("partial block read: %d should be %d\n",
1224 rd
, ntrec
* TP_BSIZE
);
1226 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1229 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1235 findtapeblksize(void)
1239 for (i
= 0; i
< ntrec
; i
++)
1240 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1244 i
= rmtread(tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1247 i
= read(mt
, tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1250 err(1, "tape read error");
1251 if (i
% TP_BSIZE
!= 0)
1252 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1254 ntrec
= i
/ TP_BSIZE
;
1256 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1274 * Read the next block from the tape.
1275 * Check to see if it is one of several vintage headers.
1276 * If it is an old style header, convert it to a new style header.
1277 * If it is not any valid header, return an error.
1280 gethead(struct s_spcl
*buf
)
1288 char dummy
[TP_BSIZE
];
1295 u_int16_t c_inumber
;
1300 u_int16_t odi_nlink
;
1316 readtape((char *)buf
);
1317 if (buf
->c_magic
!= NFS_MAGIC
) {
1318 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1321 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1325 if (checksum((int *)buf
) == FAIL
)
1328 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1331 readtape((char *)(&u_ospcl
.s_ospcl
));
1332 memset((char *)buf
, 0, (long)TP_BSIZE
);
1333 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1334 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1335 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1336 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1337 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1338 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1339 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1340 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1341 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1342 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1343 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1344 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1345 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1346 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1348 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1349 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1350 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1351 #else /* __linux__ */
1352 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1353 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1354 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1355 #endif /* __linux__ */
1356 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1357 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1358 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1359 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1361 buf
->c_magic
= NFS_MAGIC
;
1364 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1365 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1366 qcvt
.qval
= buf
->c_dinode
.di_size
;
1367 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1368 printf("Note: Doing Quad swapping\n");
1373 qcvt
.qval
= buf
->c_dinode
.di_size
;
1375 qcvt
.val
[1] = qcvt
.val
[0];
1377 buf
->c_dinode
.di_size
= qcvt
.qval
;
1381 switch (buf
->c_type
) {
1386 * Have to patch up missing information in bit map headers
1389 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1390 if (buf
->c_count
> TP_NINDIR
)
1393 for (i
= 0; i
< buf
->c_count
; i
++)
1398 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1410 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1414 * If we are restoring a filesystem with old format inodes,
1415 * copy the uid/gid to the new location.
1418 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1419 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1427 * Check that a header is where it belongs and predict the next header
1430 accthdr(struct s_spcl
*header
)
1432 static ino_t previno
= 0x7fffffff;
1433 static int prevtype
;
1434 static long predict
;
1437 if (header
->c_type
== TS_TAPE
) {
1438 fprintf(stderr
, "Volume header (%s inode format) ",
1439 oldinofmt
? "old" : "new");
1440 if (header
->c_firstrec
)
1441 fprintf(stderr
, "begins with record %d",
1442 header
->c_firstrec
);
1443 fprintf(stderr
, "\n");
1444 previno
= 0x7fffffff;
1447 if (previno
== 0x7fffffff)
1451 fprintf(stderr
, "Dumped inodes map header");
1454 fprintf(stderr
, "Used inodes map header");
1457 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
1460 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
1463 fprintf(stderr
, "End of tape header");
1466 if (predict
!= blksread
- 1)
1467 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1468 predict
, blksread
- 1);
1469 fprintf(stderr
, "\n");
1472 if (header
->c_type
!= TS_END
)
1473 for (i
= 0; i
< header
->c_count
; i
++)
1474 if (readmapflag
|| header
->c_addr
[i
] != 0)
1478 prevtype
= header
->c_type
;
1479 previno
= header
->c_inumber
;
1483 * Find an inode header.
1484 * Complain if had to skip, and complain is set.
1487 findinode(struct s_spcl
*header
)
1489 static long skipcnt
= 0;
1493 curfile
.name
= "<name unknown>";
1494 curfile
.action
= UNKNOWN
;
1498 if (header
->c_magic
!= NFS_MAGIC
) {
1500 while (gethead(header
) == FAIL
||
1501 header
->c_date
!= dumpdate
)
1504 switch (header
->c_type
) {
1508 * Skip up to the beginning of the next record
1510 for (i
= 0; i
< header
->c_count
; i
++)
1511 if (header
->c_addr
[i
])
1513 while (gethead(header
) == FAIL
||
1514 header
->c_date
!= dumpdate
)
1519 curfile
.dip
= &header
->c_dinode
;
1520 curfile
.ino
= header
->c_inumber
;
1524 curfile
.ino
= maxino
;
1528 curfile
.name
= "<file removal list>";
1532 curfile
.name
= "<file dump list>";
1536 panic("unexpected tape header\n");
1540 panic("unknown tape header type %d\n", spcl
.c_type
);
1544 } while (header
->c_type
== TS_ADDR
);
1546 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1556 j
= sizeof(union u_spcl
) / sizeof(int);
1563 /* What happens if we want to read restore tapes
1564 for a 16bit int machine??? */
1570 if (i
!= CHECKSUM
) {
1571 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
1572 (unsigned long)curfile
.ino
, curfile
.name
);
1582 #include <varargs.h>
1587 msg(const char *fmt
, ...)
1600 (void)vfprintf(stderr
, fmt
, ap
);
1603 #endif /* RRESTORE */
1606 swab16(u_char
*sp
, int n
)
1611 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1618 swab32(u_char
*sp
, int n
)
1623 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1624 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
1631 swab64(u_char
*sp
, int n
)
1636 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
1637 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
1638 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
1639 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
1646 swabst(u_char
*cp
, u_char
*sp
)
1652 case '0': case '1': case '2': case '3': case '4':
1653 case '5': case '6': case '7': case '8': case '9':
1654 n
= (n
* 10) + (*cp
++ - '0');
1657 case 's': case 'w': case 'h':
1675 default: /* Any other character, like 'b' counts as byte. */
1689 swabst((u_char
*)"i", (u_char
*)&x
);
1696 swabst((u_char
*)"l", (u_char
*)&x
);