]>
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
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
49 static char sccsid
[] = "@(#)tape.c 8.9 (Berkeley) 5/1/95";
51 static const char rcsid
[] =
52 "$Id: tape.c,v 1.2 1999/10/11 12:53:24 stelian Exp $";
55 #include <sys/param.h>
62 #include <linux/ext2_fs.h>
63 #include <bsdcompat.h>
65 #include <ufs/ufs/dinode.h>
66 #endif /* __linux__ */
67 #include <protocols/dumprestore.h>
77 #include <ext2fs/ext2fs.h>
82 #include "pathnames.h"
84 static long fssize
= MAXBSIZE
;
86 static int pipein
= 0;
91 static union u_spcl endoftapemark
;
92 static long blksread
; /* blocks read since last header */
93 static long tpblksread
= 0; /* TP_BSIZE blocks read */
94 static long tapesread
;
95 static jmp_buf restart
;
96 static int gettingfile
= 0; /* restart has a valid frame */
97 static char *host
= NULL
;
101 static char lnkbuf
[MAXPATHLEN
+ 1];
104 int oldinofmt
; /* old inode format conversion required */
105 int Bcvt
; /* Swap Bytes (for CCI or sun) */
106 static int Qcvt
; /* Swap quads (for sun) */
108 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
110 static void accthdr
__P((struct s_spcl
*));
111 static int checksum
__P((int *));
112 static void findinode
__P((struct s_spcl
*));
113 static void findtapeblksize
__P((void));
114 static int gethead
__P((struct s_spcl
*));
115 static void readtape
__P((char *));
116 static void setdumpnum
__P((void));
117 static u_long swabl
__P((u_long
));
118 static u_char
*swablong
__P((u_char
*, int));
119 static u_char
*swabshort
__P((u_char
*, int));
120 static void terminateinput
__P((void));
121 static void xtrfile
__P((char *, long));
122 static void xtrlnkfile
__P((char *, long));
123 static void xtrlnkskip
__P((char *, long));
124 static void xtrmap
__P((char *, long));
125 static void xtrmapskip
__P((char *, long));
126 static void xtrskip
__P((char *, long));
128 static int readmapflag
;
131 * Set up an input source
141 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
145 if (strchr(source
, ':')) {
147 source
= strchr(host
, ':');
149 if (rmthost(host
) == 0)
153 if (strcmp(source
, "-") == 0) {
155 * Since input is coming from a pipe we must establish
156 * our own connection to the terminal.
158 terminal
= fopen(_PATH_TTY
, "r");
159 if (terminal
== NULL
) {
160 (void)fprintf(stderr
, "cannot open %s: %s\n",
161 _PATH_TTY
, strerror(errno
));
162 terminal
= fopen(_PATH_DEVNULL
, "r");
163 if (terminal
== NULL
) {
164 (void)fprintf(stderr
, "cannot open %s: %s\n",
165 _PATH_DEVNULL
, strerror(errno
));
171 setuid(getuid()); /* no longer need or want root privileges */
172 magtape
= strdup(source
);
173 if (magtape
== NULL
) {
174 fprintf(stderr
, "Cannot allocate space for magtape buffer\n");
183 static tapebufsize
= -1;
186 if (size
<= tapebufsize
)
190 tapebuf
= malloc(size
* TP_BSIZE
);
191 if (tapebuf
== NULL
) {
192 fprintf(stderr
, "Cannot allocate space for tape buffer\n");
199 * Verify that the tape drive can be accessed and
200 * that it actually is a dump tape.
208 vprintf(stdout
, "Verify tape and initialize maps\n");
211 mt
= rmtopen(magtape
, 0);
217 mt
= open(magtape
, O_RDONLY
, 0);
219 fprintf(stderr
, "%s: %s\n", magtape
, strerror(errno
));
225 if (!pipein
&& !bflag
)
227 if (gethead(&spcl
) == FAIL
) {
228 blkcnt
--; /* push back this block */
232 if (gethead(&spcl
) == FAIL
) {
233 fprintf(stderr
, "Tape is not a dump tape\n");
236 fprintf(stderr
, "Converting to new file system format.\n");
239 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
240 endoftapemark
.s_spcl
.c_type
= TS_END
;
241 ip
= (int *)&endoftapemark
;
242 j
= sizeof(union u_spcl
) / sizeof(int);
247 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
249 if (vflag
|| command
== 't' || command
== 'C')
251 if (filesys
== NULL
) {
252 filesys
= spcl
.c_filesys
;
254 dumptime
= spcl
.c_ddate
;
255 dumpdate
= spcl
.c_date
;
256 if (stat(".", &stbuf
) < 0) {
257 fprintf(stderr
, "cannot stat .: %s\n", strerror(errno
));
260 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
262 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
263 fssize
= stbuf
.st_blksize
;
264 if (((fssize
- 1) & fssize
) != 0) {
265 fprintf(stderr
, "bad block size %ld\n", fssize
);
268 if (spcl
.c_volume
!= 1) {
269 fprintf(stderr
, "Tape is not volume 1 of the dump\n");
272 if (gethead(&spcl
) == FAIL
) {
273 dprintf(stdout
, "header read failed at %ld blocks\n", blksread
);
274 panic("no header after volume mark!\n");
277 if (spcl
.c_type
!= TS_CLRI
) {
278 fprintf(stderr
, "Cannot find file removal list\n");
281 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
282 dprintf(stdout
, "maxino = %ld\n", maxino
);
283 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
285 panic("no memory for active inode map\n");
287 curfile
.action
= USING
;
288 getfile(xtrmap
, xtrmapskip
);
289 if (spcl
.c_type
!= TS_BITS
) {
290 fprintf(stderr
, "Cannot find file dump list\n");
293 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
294 if (map
== (char *)NULL
)
295 panic("no memory for file dump list\n");
297 curfile
.action
= USING
;
298 getfile(xtrmap
, xtrmapskip
);
300 * If there may be whiteout entries on the tape, pretend that the
301 * whiteout inode exists, so that the whiteout entries can be
305 SETINO(WINO
, dumpmap
);
309 * Prompt user to load a new dump volume.
310 * "Nextvol" is the next suggested volume to use.
311 * This suggested volume is enforced when doing full
312 * or incremental restores, but can be overridden by
313 * the user when only extracting a subset of the files.
319 long newvol
=0, savecnt
=0, wantnext
=0, i
;
320 union u_spcl tmpspcl
;
321 # define tmpbuf tmpspcl.s_spcl
330 panic("Changing volumes on pipe input?\n");
338 done(1); /* pipes do not get a second chance */
339 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
346 while (newvol
<= 0) {
347 if (tapesread
== 0) {
348 fprintf(stderr
, "%s%s%s%s%s",
349 "You have not read any tapes yet.\n",
350 "Unless you know which volume your",
351 " file(s) are on you should start\n",
352 "with the last volume and work",
353 " towards the first.\n");
355 fprintf(stderr
, "You have read volumes");
357 for (i
= 1; i
< 32; i
++)
358 if (tapesread
& (1 << i
)) {
359 fprintf(stderr
, "%s%ld", buf
, i
);
362 fprintf(stderr
, "\n");
365 fprintf(stderr
, "Specify next volume #: ");
366 (void) fflush(stderr
);
367 (void) fgets(buf
, BUFSIZ
, terminal
);
368 } while (!feof(terminal
) && buf
[0] == '\n');
374 "Volume numbers are positive numerics\n");
377 if (newvol
== volno
) {
378 tapesread
|= 1 << volno
;
382 fprintf(stderr
, "Mount tape volume %ld\n", newvol
);
383 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
384 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
385 (void) fflush(stderr
);
386 (void) fgets(buf
, BUFSIZ
, terminal
);
389 if (!strcmp(buf
, "none\n")) {
393 if (buf
[0] != '\n') {
394 (void) strcpy(magtape
, buf
);
395 magtape
[strlen(magtape
) - 1] = '\0';
399 mt
= rmtopen(magtape
, 0);
402 mt
= open(magtape
, O_RDONLY
, 0);
405 fprintf(stderr
, "Cannot open %s\n", magtape
);
413 if (gethead(&tmpbuf
) == FAIL
) {
414 dprintf(stdout
, "header read failed at %ld blocks\n", blksread
);
415 fprintf(stderr
, "tape is not dump tape\n");
419 if (tmpbuf
.c_volume
!= volno
) {
420 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
424 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
426 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
427 ctime4(&tmpbuf
.c_date
));
428 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
430 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
431 ctime(&tmpbuf
.c_date
));
432 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
437 tapesread
|= 1 << volno
;
440 * If continuing from the previous volume, skip over any
441 * blocks read already at the end of the previous volume.
443 * If coming to this volume at random, skip to the beginning
444 * of the next record.
446 dprintf(stdout
, "read %ld recs, tape starts with %d\n",
447 tpblksread
, tmpbuf
.c_firstrec
);
448 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
450 tpblksread
= tmpbuf
.c_firstrec
;
451 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
453 } else if (tmpbuf
.c_firstrec
> 0 &&
454 tmpbuf
.c_firstrec
< tpblksread
- 1) {
456 * -1 since we've read the volume header
458 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
459 dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
460 i
, i
> 1 ? "s" : "");
465 if (curfile
.action
== USING
) {
467 panic("active file into volume 1\n");
471 * Skip up to the beginning of the next record
473 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
474 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
476 (void) gethead(&spcl
);
485 * Handle unexpected EOF.
491 if (gettingfile
&& curfile
.action
== USING
) {
492 printf("Warning: %s %s\n",
493 "End-of-input encountered while extracting", curfile
.name
);
495 curfile
.name
= "<name unknown>";
496 curfile
.action
= UNKNOWN
;
498 curfile
.ino
= maxino
;
506 * handle multiple dumps per tape by skipping forward to the
514 if (dumpnum
== 1 || volno
!= 1)
517 fprintf(stderr
, "Cannot have multiple dumps on pipe input\n");
521 tcom
.mt_count
= dumpnum
- 1;
524 rmtioctl(MTFSF
, dumpnum
- 1);
527 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
528 fprintf(stderr
, "ioctl MTFSF: %s\n", strerror(errno
));
535 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
536 fprintf(stdout
, "Dumped from: %s",
537 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
539 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
540 fprintf(stdout
, "Dumped from: %s",
541 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
543 if (spcl
.c_host
[0] == '\0')
545 fprintf(stderr
, "Level %d dump of %s on %s:%s\n",
546 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
547 fprintf(stderr
, "Label: %s\n", spcl
.c_label
);
556 struct timeval timep
[2];
565 curfile
.action
= USING
;
567 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
568 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
569 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
570 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
571 #else /* __linux__ */
572 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
573 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
574 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
575 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
576 #endif /* __linux__ */
577 mode
= curfile
.dip
->di_mode
;
578 flags
= curfile
.dip
->di_flags
;
579 switch (mode
& IFMT
) {
582 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
587 vprintf(stdout
, "skipped socket %s\n", name
);
593 ep
= lookupname(name
);
594 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
595 panic("unextracted directory %s\n", name
);
599 vprintf(stdout
, "extract file %s\n", name
);
600 return (genliteraldir(name
, curfile
.ino
));
606 uid
= curfile
.dip
->di_uid
;
607 gid
= curfile
.dip
->di_gid
;
609 getfile(xtrlnkfile
, xtrlnkskip
);
612 "%s: zero length symbolic link (ignored)\n", name
);
616 err
= linkit(lnkbuf
, name
, SYMLINK
);
618 (void) chown(name
, uid
, gid
);
621 return (linkit(lnkbuf
, name
, SYMLINK
));
625 vprintf(stdout
, "extract fifo %s\n", name
);
632 if (mkfifo(name
, mode
) < 0) {
633 fprintf(stderr
, "%s: cannot create fifo: %s\n",
634 name
, strerror(errno
));
638 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
639 (void) chmod(name
, mode
);
641 (void) fsetflags(name
, flags
);
643 (void) chflags(name
, flags
);
651 vprintf(stdout
, "extract special file %s\n", name
);
658 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
659 fprintf(stderr
, "%s: cannot create special file: %s\n",
660 name
, strerror(errno
));
664 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
665 (void) chmod(name
, mode
);
667 (void) fsetflags(name
, flags
);
669 (void) chflags(name
, flags
);
676 vprintf(stdout
, "extract file %s\n", name
);
683 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
685 fprintf(stderr
, "%s: cannot create file: %s\n",
686 name
, strerror(errno
));
690 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
691 (void) fchmod(ofile
, mode
);
693 (void) fsetflags(ofile
, flags
);
695 (void) fchflags(ofile
, flags
);
697 getfile(xtrfile
, xtrskip
);
706 * skip over bit maps on the tape
712 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
717 * skip over a file on the tape
723 curfile
.action
= SKIP
;
724 getfile(xtrnull
, xtrnull
);
728 * Extract a file from the tape.
729 * When an allocated block is found it is passed to the fill function;
730 * when an unallocated block (hole) is found, a zeroed buffer is passed
731 * to the skip function.
735 void (*fill
) __P((char *, long));
736 void (*skip
) __P((char *, long));
740 quad_t size
= spcl
.c_dinode
.di_size
;
741 int last_write_was_hole
= 0;
742 long origsize
= size
;
743 static char clearedbuf
[MAXBSIZE
];
744 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
747 if (spcl
.c_type
== TS_END
)
748 panic("ran off end of tape\n");
749 if (spcl
.c_magic
!= NFS_MAGIC
)
750 panic("not at beginning of a file\n");
751 if (!gettingfile
&& setjmp(restart
) != 0)
755 for (i
= 0; i
< spcl
.c_count
; i
++) {
756 if (readmapflag
|| spcl
.c_addr
[i
]) {
757 readtape(&buf
[curblk
++][0]);
758 if (curblk
== fssize
/ TP_BSIZE
) {
759 (*fill
)((char *)buf
, (long)(size
> TP_BSIZE
?
760 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
762 last_write_was_hole
= 0;
766 (*fill
)((char *)buf
, (long)(size
> TP_BSIZE
?
768 (curblk
- 1) * TP_BSIZE
+ size
));
771 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
773 last_write_was_hole
= 1;
775 if ((size
-= TP_BSIZE
) <= 0) {
776 for (i
++; i
< spcl
.c_count
; i
++)
777 if (readmapflag
|| spcl
.c_addr
[i
])
782 if (gethead(&spcl
) == GOOD
&& size
> 0) {
783 if (spcl
.c_type
== TS_ADDR
)
786 "Missing address (header) block for %s at %ld blocks\n",
787 curfile
.name
, blksread
);
790 (*fill
)((char *)buf
, (long)((curblk
* TP_BSIZE
) + size
));
791 last_write_was_hole
= 0;
793 if (last_write_was_hole
) {
794 ftruncate(ofile
, origsize
);
801 * Write out the next block of a file.
811 if (write(ofile
, buf
, (int) size
) == -1) {
813 "write error extracting inode %ld, name %s\nwrite: %s\n",
814 curfile
.ino
, curfile
.name
, strerror(errno
));
820 * Skip over a hole in a file.
829 if (lseek(ofile
, size
, SEEK_CUR
) == -1) {
831 "seek error extracting inode %ld, name %s\nlseek: %s\n",
832 curfile
.ino
, curfile
.name
, strerror(errno
));
838 * Collect the next block of a symbolic link.
841 xtrlnkfile(buf
, size
)
847 if (pathlen
> MAXPATHLEN
) {
848 fprintf(stderr
, "symbolic link name: %s->%s%s; too long %d\n",
849 curfile
.name
, lnkbuf
, buf
, pathlen
);
852 (void) strcat(lnkbuf
, buf
);
856 * Skip over a hole in a symbolic link (should never happen).
860 xtrlnkskip(buf
, size
)
865 fprintf(stderr
, "unallocated block in symbolic link %s\n",
871 * Collect the next block of a bit map.
879 memmove(map
, buf
, size
);
884 * Skip over a hole in a bit map (should never happen).
888 xtrmapskip(buf
, size
)
893 panic("hole in map\n");
898 * Noop, when an extraction function is not needed.
911 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
916 static char buf_tape
[BUFSIZ
];
917 static char buf_disk
[BUFSIZ
];
922 if ((n_tape
= read(fd_tape
, buf_tape
, BUFSIZE
)) < 1) {
923 close(fd_tape
), close(fd_disk
);
924 panic("do_cmpfiles: unexpected EOF[1]");
926 if ((n_disk
= read(fd_disk
, buf_disk
, BUFSIZE
)) < 1) {
927 close(fd_tape
), close(fd_disk
);
928 panic("do_cmpfiles: unexpected EOF[2]");
930 if (n_tape
!= n_disk
) {
931 close(fd_tape
), close(fd_disk
);
932 panic("do_cmpfiles: sizes different!");
934 if (memcmp(buf_tape
, buf_disk
, n_tape
) != 0) return (1);
940 /* for debugging compare problems */
941 #undef COMPARE_FAIL_KEEP_FILE
943 #ifdef COMPARE_FAIL_KEEP_FILE
944 /* return true if tapefile should be unlinked after compare */
949 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
951 struct stat sbuf_tape
;
952 int fd_tape
, fd_disk
;
954 if (stat(tapefile
, &sbuf_tape
) != 0) {
955 panic("Can't lstat tmp file %s: %s\n", tapefile
,
959 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
961 "%s: size changed from %ld to %ld.\n",
962 diskfile
, sbuf_tape
.st_size
, sbuf_disk
->st_size
);
963 #ifdef COMPARE_FAIL_KEEP_FILE
970 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
971 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
973 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
975 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
978 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
979 fprintf(stderr
, "%s: tape and disk copies are different\n",
983 #ifdef COMPARE_FAIL_KEEP_FILE
984 /* rename the file to live in /tmp */
985 /* rename `tapefile' to /tmp/<basename of diskfile> */
987 char *p
= strrchr(diskfile
, '/');
988 char newname
[MAXPATHLEN
];
990 panic("can't find / in %s\n", diskfile
);
992 sprintf(newname
, "%s/debug/%s", tmpdir
, p
+ 1);
993 if (rename(tapefile
, newname
)) {
994 panic("rename from %s to %s failed: %s\n",
998 fprintf(stderr
, "*** %s saved to %s\n",
1003 /* don't unlink the file (it's not there anymore */
1012 #ifdef COMPARE_FAIL_KEEP_FILE
1017 static char tmpfilename
[128];
1023 static char *tmpfile
= NULL
;
1025 struct stat sb
, stemp
;
1028 if ((r
= lstat(name
, &sb
)) != 0) {
1029 fprintf(stderr
, "%s: does not exist (%d, %d).\n", name
, r
, errno
);
1034 curfile
.name
= name
;
1035 curfile
.action
= USING
;
1036 mode
= curfile
.dip
->di_mode
;
1038 vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1041 if (sb
.st_mode
!= mode
) {
1042 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1043 name
, mode
& 07777, sb
.st_mode
& 07777);
1045 switch (mode
& IFMT
) {
1059 char lbuf
[MAXPATHLEN
+ 1];
1062 if (!(sb
.st_mode
& S_IFLNK
)) {
1063 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1069 getfile(xtrlnkfile
, xtrlnkskip
);
1072 "%s: zero length symbolic link (ignored)\n",
1076 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1077 panic("readlink of %s failed: %s", name
,
1081 if (strcmp(lbuf
, lnkbuf
) != 0) {
1083 "%s: symbolic link changed from %s to %s.\n",
1084 name
, lnkbuf
, lbuf
);
1092 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1093 fprintf(stderr
, "%s: no longer a special file\n",
1099 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1101 "%s: device changed from %d,%d to %d,%d.\n",
1103 ((int)curfile
.dip
->di_rdev
>> 8) & 0xf,
1104 (int)curfile
.dip
->di_rdev
& 0xf,
1105 ((int)sb
.st_rdev
>> 8) & 0xf,
1106 (int)sb
.st_rdev
& 0xf);
1112 if (tmpfile
== NULL
) {
1113 /* argument to mktemp() must not be in RO space: */
1114 sprintf(tmpfilename
, "%s/restoreCXXXXXX", tmpdir
);
1115 tmpfile
= mktemp(&tmpfilename
[0]);
1117 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1118 panic("cannot delete tmp file %s: %s\n",
1119 tmpfile
, strerror(errno
));
1121 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1122 panic("cannot create file temp file %s: %s\n",
1123 name
, strerror(errno
));
1125 getfile(xtrfile
, xtrskip
);
1126 (void) close(ofile
);
1127 #ifdef COMPARE_FAIL_KEEP_FILE
1128 if (cmpfiles(tmpfile
, name
, &sb
))
1131 cmpfiles(tmpfile
, name
, &sb
);
1140 * Read TP_BSIZE blocks from the input.
1141 * Handle read errors, and end of media.
1148 int cnt
, seek_failed
;
1150 if (blkcnt
< numtrec
) {
1151 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], (long)TP_BSIZE
);
1156 for (i
= 0; i
< ntrec
; i
++)
1157 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1160 cnt
= ntrec
* TP_BSIZE
;
1165 i
= rmtread(&tapebuf
[rd
], cnt
);
1168 i
= read(mt
, &tapebuf
[rd
], cnt
);
1170 * Check for mid-tape short read error.
1171 * If found, skip rest of buffer and start with the next.
1173 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1174 dprintf(stdout
, "mid-media short read error.\n");
1178 * Handle partial block read.
1180 if (pipein
&& i
== 0 && rd
> 0)
1182 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1191 * Short read. Process the blocks read.
1193 if (i
% TP_BSIZE
!= 0)
1195 "partial block read: %ld should be %ld\n",
1196 i
, ntrec
* TP_BSIZE
);
1197 numtrec
= i
/ TP_BSIZE
;
1201 * Handle read error.
1204 fprintf(stderr
, "Tape read error while ");
1205 switch (curfile
.action
) {
1207 fprintf(stderr
, "trying to set up tape\n");
1210 fprintf(stderr
, "trying to resynchronize\n");
1213 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1216 fprintf(stderr
, "skipping over inode %ld\n",
1220 if (!yflag
&& !reply("continue"))
1222 i
= ntrec
* TP_BSIZE
;
1223 memset(tapebuf
, 0, i
);
1226 seek_failed
= (rmtseek(i
, 1) < 0);
1229 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1233 "continuation failed: %s\n", strerror(errno
));
1238 * Handle end of tape.
1241 vprintf(stdout
, "End-of-tape encountered\n");
1250 if (rd
% TP_BSIZE
!= 0)
1251 panic("partial block read: %d should be %d\n",
1252 rd
, ntrec
* TP_BSIZE
);
1254 memmove(&tapebuf
[rd
], &endoftapemark
, (long)TP_BSIZE
);
1257 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], (long)TP_BSIZE
);
1267 for (i
= 0; i
< ntrec
; i
++)
1268 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1272 i
= rmtread(tapebuf
, ntrec
* TP_BSIZE
);
1275 i
= read(mt
, tapebuf
, ntrec
* TP_BSIZE
);
1278 fprintf(stderr
, "tape read error: %s\n", strerror(errno
));
1281 if (i
% TP_BSIZE
!= 0) {
1282 fprintf(stderr
, "Tape block size (%ld) %s (%d)\n",
1283 i
, "is not a multiple of dump block size", TP_BSIZE
);
1286 ntrec
= i
/ TP_BSIZE
;
1288 vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1306 * Read the next block from the tape.
1307 * Check to see if it is one of several vintage headers.
1308 * If it is an old style header, convert it to a new style header.
1309 * If it is not any valid header, return an error.
1321 char dummy
[TP_BSIZE
];
1332 unsigned short odi_mode
;
1349 readtape((char *)buf
);
1350 if (buf
->c_magic
!= NFS_MAGIC
) {
1351 if (swabl(buf
->c_magic
) != NFS_MAGIC
)
1354 vprintf(stdout
, "Note: Doing Byte swapping\n");
1358 if (checksum((int *)buf
) == FAIL
)
1361 swabst((u_char
*)"8l4s31l", (u_char
*)buf
);
1362 swabst((u_char
*)"l",(u_char
*) &buf
->c_level
);
1363 swabst((u_char
*)"2l",(u_char
*) &buf
->c_flags
);
1367 readtape((char *)(&u_ospcl
.s_ospcl
));
1368 memset(buf
, 0, (long)TP_BSIZE
);
1369 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1370 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1371 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1372 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1373 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1374 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1375 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1376 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1377 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1378 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1379 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1380 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1381 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1382 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1384 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1385 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1386 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1387 #else /* __linux__ */
1388 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1389 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1390 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1391 #endif /* __linux__ */
1392 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1393 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1394 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1395 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1397 buf
->c_magic
= NFS_MAGIC
;
1400 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1401 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1402 qcvt
.qval
= buf
->c_dinode
.di_size
;
1403 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1404 printf("Note: Doing Quad swapping\n");
1409 qcvt
.qval
= buf
->c_dinode
.di_size
;
1411 qcvt
.val
[1] = qcvt
.val
[0];
1413 buf
->c_dinode
.di_size
= qcvt
.qval
;
1417 switch (buf
->c_type
) {
1422 * Have to patch up missing information in bit map headers
1425 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1426 if (buf
->c_count
> TP_NINDIR
)
1429 for (i
= 0; i
< buf
->c_count
; i
++)
1434 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1446 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1450 * If we are restoring a filesystem with old format inodes,
1451 * copy the uid/gid to the new location.
1454 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1455 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1463 * Check that a header is where it belongs and predict the next header
1467 struct s_spcl
*header
;
1469 static ino_t previno
= 0x7fffffff;
1470 static int prevtype
;
1471 static long predict
;
1474 if (header
->c_type
== TS_TAPE
) {
1475 fprintf(stderr
, "Volume header (%s inode format) ",
1476 oldinofmt
? "old" : "new");
1477 if (header
->c_firstrec
)
1478 fprintf(stderr
, "begins with record %d",
1479 header
->c_firstrec
);
1480 fprintf(stderr
, "\n");
1481 previno
= 0x7fffffff;
1484 if (previno
== 0x7fffffff)
1488 fprintf(stderr
, "Dumped inodes map header");
1491 fprintf(stderr
, "Used inodes map header");
1494 fprintf(stderr
, "File header, ino %ld", previno
);
1497 fprintf(stderr
, "File continuation header, ino %ld", previno
);
1500 fprintf(stderr
, "End of tape header");
1503 if (predict
!= blksread
- 1)
1504 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1505 predict
, blksread
- 1);
1506 fprintf(stderr
, "\n");
1509 if (header
->c_type
!= TS_END
)
1510 for (i
= 0; i
< header
->c_count
; i
++)
1511 if (readmapflag
|| header
->c_addr
[i
] != 0)
1515 prevtype
= header
->c_type
;
1516 previno
= header
->c_inumber
;
1520 * Find an inode header.
1521 * Complain if had to skip, and complain is set.
1525 struct s_spcl
*header
;
1527 static long skipcnt
= 0;
1531 curfile
.name
= "<name unknown>";
1532 curfile
.action
= UNKNOWN
;
1536 if (header
->c_magic
!= NFS_MAGIC
) {
1538 while (gethead(header
) == FAIL
||
1539 header
->c_date
!= dumpdate
)
1542 switch (header
->c_type
) {
1546 * Skip up to the beginning of the next record
1548 for (i
= 0; i
< header
->c_count
; i
++)
1549 if (header
->c_addr
[i
])
1551 while (gethead(header
) == FAIL
||
1552 header
->c_date
!= dumpdate
)
1557 curfile
.dip
= &header
->c_dinode
;
1558 curfile
.ino
= header
->c_inumber
;
1562 curfile
.ino
= maxino
;
1566 curfile
.name
= "<file removal list>";
1570 curfile
.name
= "<file dump list>";
1574 panic("unexpected tape header\n");
1578 panic("unknown tape header type %d\n", spcl
.c_type
);
1582 } while (header
->c_type
== TS_ADDR
);
1584 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1595 j
= sizeof(union u_spcl
) / sizeof(int);
1602 /* What happens if we want to read restore tapes
1603 for a 16bit int machine??? */
1609 if (i
!= CHECKSUM
) {
1610 fprintf(stderr
, "Checksum error %o, inode %ld file %s\n", i
,
1611 curfile
.ino
, curfile
.name
);
1621 #include <varargs.h>
1626 msg(const char *fmt
, ...)
1639 (void)vfprintf(stderr
, fmt
, ap
);
1642 #endif /* RRESTORE */
1646 register u_char
*sp
;
1652 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1660 register u_char
*sp
;
1666 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1667 c
= sp
[2]; sp
[2] = sp
[1]; sp
[1] = c
;
1675 register u_char
*cp
, *sp
;
1681 case '0': case '1': case '2': case '3': case '4':
1682 case '5': case '6': case '7': case '8': case '9':
1683 n
= (n
* 10) + (*cp
++ - '0');
1686 case 's': case 'w': case 'h':
1689 sp
= swabshort(sp
, n
);
1695 sp
= swablong(sp
, n
);
1698 default: /* Any other character, like 'b' counts as byte. */
1713 swabst((u_char
*)"l", (u_char
*)&x
);