]>
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.4 1999/10/11 13:08:10 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>
70 #include <compaterr.h>
78 #include <ext2fs/ext2fs.h>
83 #include "pathnames.h"
85 static long fssize
= MAXBSIZE
;
87 static int pipein
= 0;
92 static union u_spcl endoftapemark
;
93 static long blksread
; /* blocks read since last header */
94 static long tpblksread
= 0; /* TP_BSIZE blocks read */
95 static long tapesread
;
96 static sigjmp_buf restart
;
97 static int gettingfile
= 0; /* restart has a valid frame */
98 static char *host
= NULL
;
102 static char lnkbuf
[MAXPATHLEN
+ 1];
105 int oldinofmt
; /* old inode format conversion required */
106 int Bcvt
; /* Swap Bytes (for CCI or sun) */
107 static int Qcvt
; /* Swap quads (for sun) */
109 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
111 static void accthdr
__P((struct s_spcl
*));
112 static int checksum
__P((int *));
113 static void findinode
__P((struct s_spcl
*));
114 static void findtapeblksize
__P((void));
115 static int gethead
__P((struct s_spcl
*));
116 static void readtape
__P((char *));
117 static void setdumpnum
__P((void));
118 static u_int swabi
__P((u_int
));
119 static u_long swabl
__P((u_long
));
120 static u_char
*swab64
__P((u_char
*, int));
121 static u_char
*swab32
__P((u_char
*, int));
122 static u_char
*swab16
__P((u_char
*, int));
123 static void terminateinput
__P((void));
124 static void xtrfile
__P((char *, size_t));
125 static void xtrlnkfile
__P((char *, size_t));
126 static void xtrlnkskip
__P((char *, size_t));
127 static void xtrmap
__P((char *, size_t));
128 static void xtrmapskip
__P((char *, size_t));
129 static void xtrskip
__P((char *, size_t));
131 static int readmapflag
;
134 * Set up an input source
137 setinput(char *source
)
143 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
147 if (strchr(source
, ':')) {
149 source
= strchr(host
, ':');
151 if (rmthost(host
) == 0)
155 if (strcmp(source
, "-") == 0) {
157 * Since input is coming from a pipe we must establish
158 * our own connection to the terminal.
160 terminal
= fopen(_PATH_TTY
, "r");
161 if (terminal
== NULL
) {
162 warn("cannot open %s", _PATH_TTY
);
163 terminal
= fopen(_PATH_DEVNULL
, "r");
164 if (terminal
== NULL
)
165 err(1, "cannot open %s", _PATH_DEVNULL
);
169 setuid(getuid()); /* no longer need or want root privileges */
170 magtape
= strdup(source
);
172 errx(1, "Cannot allocate space for magtape buffer");
176 newtapebuf(long size
)
178 static int tapebufsize
= -1;
181 if (size
<= tapebufsize
)
185 tapebuf
= malloc(size
* TP_BSIZE
);
187 errx(1, "Cannot allocate space for tape buffer");
192 * Verify that the tape drive can be accessed and
193 * that it actually is a dump tape.
201 Vprintf(stdout
, "Verify tape and initialize maps\n");
204 mt
= rmtopen(magtape
, 0);
210 mt
= open(magtape
, O_RDONLY
, 0);
212 err(1, "%s", magtape
);
216 if (!pipein
&& !bflag
)
218 if (gethead(&spcl
) == FAIL
) {
219 blkcnt
--; /* push back this block */
223 if (gethead(&spcl
) == FAIL
)
224 errx(1, "Tape is not a dump tape");
225 fprintf(stderr
, "Converting to new file system format.\n");
228 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
229 endoftapemark
.s_spcl
.c_type
= TS_END
;
230 ip
= (int *)&endoftapemark
;
231 j
= sizeof(union u_spcl
) / sizeof(int);
236 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
238 if (vflag
|| command
== 't' || command
== 'C')
240 if (filesys
== NULL
) {
241 filesys
= spcl
.c_filesys
;
243 dumptime
= spcl
.c_ddate
;
244 dumpdate
= spcl
.c_date
;
245 if (stat(".", &stbuf
) < 0)
246 err(1, "cannot stat .");
247 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
249 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
250 fssize
= stbuf
.st_blksize
;
251 if (((fssize
- 1) & fssize
) != 0)
252 errx(1, "bad block size %ld", fssize
);
253 if (spcl
.c_volume
!= 1)
254 errx(1, "Tape is not volume 1 of the dump");
255 if (gethead(&spcl
) == FAIL
) {
256 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
257 panic("no header after volume mark!\n");
260 if (spcl
.c_type
!= TS_CLRI
)
261 errx(1, "Cannot find file removal list");
262 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
263 Dprintf(stdout
, "maxino = %ld\n", maxino
);
264 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
266 panic("no memory for active inode map\n");
268 curfile
.action
= USING
;
269 getfile(xtrmap
, xtrmapskip
);
270 if (spcl
.c_type
!= TS_BITS
)
271 errx(1, "Cannot find file dump list");
272 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
273 if (map
== (char *)NULL
)
274 panic("no memory for file dump list\n");
276 curfile
.action
= USING
;
277 getfile(xtrmap
, xtrmapskip
);
279 * If there may be whiteout entries on the tape, pretend that the
280 * whiteout inode exists, so that the whiteout entries can be
284 SETINO(WINO
, dumpmap
);
288 * Prompt user to load a new dump volume.
289 * "Nextvol" is the next suggested volume to use.
290 * This suggested volume is enforced when doing full
291 * or incremental restores, but can be overridden by
292 * the user when only extracting a subset of the files.
297 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
298 union u_spcl tmpspcl
;
299 # define tmpbuf tmpspcl.s_spcl
308 panic("Changing volumes on pipe input?\n");
316 exit(1); /* pipes do not get a second chance */
317 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
324 while (newvol
<= 0) {
325 if (tapesread
== 0) {
326 fprintf(stderr
, "%s%s%s%s%s",
327 "You have not read any tapes yet.\n",
328 "Unless you know which volume your",
329 " file(s) are on you should start\n",
330 "with the last volume and work",
331 " towards the first.\n");
333 fprintf(stderr
, "You have read volumes");
335 for (i
= 1; i
< 32; i
++)
336 if (tapesread
& (1 << i
)) {
337 fprintf(stderr
, "%s%ld", buf
, (long)i
);
340 fprintf(stderr
, "\n");
343 fprintf(stderr
, "Specify next volume #: ");
344 (void) fflush(stderr
);
345 (void) fgets(buf
, BUFSIZ
, terminal
);
346 } while (!feof(terminal
) && buf
[0] == '\n');
352 "Volume numbers are positive numerics\n");
355 if (newvol
== volno
) {
356 tapesread
|= 1 << volno
;
360 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
361 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
362 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
363 (void) fflush(stderr
);
364 (void) fgets(buf
, BUFSIZ
, terminal
);
367 if (!strcmp(buf
, "none\n")) {
371 if (buf
[0] != '\n') {
372 (void) strcpy(magtape
, buf
);
373 magtape
[strlen(magtape
) - 1] = '\0';
377 mt
= rmtopen(magtape
, 0);
380 mt
= open(magtape
, O_RDONLY
, 0);
383 fprintf(stderr
, "Cannot open %s\n", magtape
);
391 if (gethead(&tmpbuf
) == FAIL
) {
392 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
393 fprintf(stderr
, "tape is not dump tape\n");
397 if (tmpbuf
.c_volume
!= volno
) {
398 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
402 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
404 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
405 ctime4(&tmpbuf
.c_date
));
406 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
408 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
409 ctime(&tmpbuf
.c_date
));
410 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
415 tapesread
|= 1 << volno
;
418 * If continuing from the previous volume, skip over any
419 * blocks read already at the end of the previous volume.
421 * If coming to this volume at random, skip to the beginning
422 * of the next record.
424 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
425 tpblksread
, (long)tmpbuf
.c_firstrec
);
426 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
428 tpblksread
= tmpbuf
.c_firstrec
;
429 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
431 } else if (tmpbuf
.c_firstrec
> 0 &&
432 tmpbuf
.c_firstrec
< tpblksread
- 1) {
434 * -1 since we've read the volume header
436 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
437 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
438 (long)i
, i
> 1 ? "s" : "");
443 if (curfile
.action
== USING
) {
445 panic("active file into volume 1\n");
449 * Skip up to the beginning of the next record
451 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
452 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
454 (void) gethead(&spcl
);
458 siglongjmp(restart
, 1);
463 * Handle unexpected EOF.
469 if (gettingfile
&& curfile
.action
== USING
) {
470 printf("Warning: %s %s\n",
471 "End-of-input encountered while extracting", curfile
.name
);
473 curfile
.name
= "<name unknown>";
474 curfile
.action
= UNKNOWN
;
476 curfile
.ino
= maxino
;
479 siglongjmp(restart
, 1);
484 * handle multiple dumps per tape by skipping forward to the
492 if (dumpnum
== 1 || volno
!= 1)
495 errx(1, "Cannot have multiple dumps on pipe input");
497 tcom
.mt_count
= dumpnum
- 1;
500 rmtioctl(MTFSF
, dumpnum
- 1);
503 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
511 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
512 fprintf(stdout
, "Dumped from: %s",
513 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
515 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
516 fprintf(stdout
, "Dumped from: %s",
517 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
519 if (spcl
.c_host
[0] == '\0')
521 fprintf(stderr
, "Level %d dump of %s on %s:%s\n",
522 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
523 fprintf(stderr
, "Label: %s\n", spcl
.c_label
);
527 extractfile(char *name
)
531 struct timeval timep
[2];
535 curfile
.action
= USING
;
537 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
538 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
539 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
540 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
541 #else /* __linux__ */
542 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
543 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
544 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
545 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
546 #endif /* __linux__ */
547 mode
= curfile
.dip
->di_mode
;
548 flags
= curfile
.dip
->di_flags
;
549 switch (mode
& IFMT
) {
552 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
557 Vprintf(stdout
, "skipped socket %s\n", name
);
563 ep
= lookupname(name
);
564 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
565 panic("unextracted directory %s\n", name
);
569 Vprintf(stdout
, "extract file %s\n", name
);
570 return (genliteraldir(name
, curfile
.ino
));
573 { uid_t luid
= curfile
.dip
->di_uid
;
574 gid_t lgid
= curfile
.dip
->di_gid
;
578 getfile(xtrlnkfile
, xtrlnkskip
);
581 "%s: zero length symbolic link (ignored)\n", name
);
584 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
586 (void) chown(name
, luid
, lgid
);
591 Vprintf(stdout
, "extract fifo %s\n", name
);
598 if (mkfifo(name
, mode
) < 0) {
599 warn("%s: cannot create fifo", name
);
603 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
604 (void) chmod(name
, mode
);
607 (void) fsetflags(name
, flags
);
609 (void) chflags(name
, flags
);
617 Vprintf(stdout
, "extract special file %s\n", name
);
624 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
625 warn("%s: cannot create special file", name
);
629 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
630 (void) chmod(name
, mode
);
634 warn("%s: fsetflags called on a special file", name
);
635 (void) fsetflags(name
, flags
);
638 (void) chflags(name
, flags
);
645 Vprintf(stdout
, "extract file %s\n", name
);
652 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
654 warn("%s: cannot create file", name
);
658 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
659 (void) fchmod(ofile
, mode
);
662 (void) setflags(ofile
, flags
);
664 (void) fchflags(ofile
, flags
);
666 getfile(xtrfile
, xtrskip
);
675 * skip over bit maps on the tape
681 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
686 * skip over a file on the tape
692 curfile
.action
= SKIP
;
693 getfile(xtrnull
, xtrnull
);
697 * Extract a file from the tape.
698 * When an allocated block is found it is passed to the fill function;
699 * when an unallocated block (hole) is found, a zeroed buffer is passed
700 * to the skip function.
703 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
706 volatile int curblk
= 0;
707 volatile quad_t size
= spcl
.c_dinode
.di_size
;
708 volatile int last_write_was_hole
= 0;
709 quad_t origsize
= size
;
710 static char clearedbuf
[MAXBSIZE
];
711 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
714 if (spcl
.c_type
== TS_END
)
715 panic("ran off end of tape\n");
716 if (spcl
.c_magic
!= NFS_MAGIC
)
717 panic("not at beginning of a file\n");
718 if (!gettingfile
&& setjmp(restart
) != 0)
722 for (i
= 0; i
< spcl
.c_count
; i
++) {
723 if (readmapflag
|| spcl
.c_addr
[i
]) {
724 readtape(&buf
[curblk
++][0]);
725 if (curblk
== fssize
/ TP_BSIZE
) {
726 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
727 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
729 last_write_was_hole
= 0;
733 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
735 (curblk
- 1) * TP_BSIZE
+ size
));
738 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
740 last_write_was_hole
= 1;
742 if ((size
-= TP_BSIZE
) <= 0) {
743 for (i
++; i
< spcl
.c_count
; i
++)
744 if (readmapflag
|| spcl
.c_addr
[i
])
749 if (gethead(&spcl
) == GOOD
&& size
> 0) {
750 if (spcl
.c_type
== TS_ADDR
)
753 "Missing address (header) block for %s at %ld blocks\n",
754 curfile
.name
, (long)blksread
);
757 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
758 last_write_was_hole
= 0;
760 if (last_write_was_hole
) {
761 ftruncate(ofile
, origsize
);
768 * Write out the next block of a file.
771 xtrfile(char *buf
, size_t size
)
776 if (write(ofile
, buf
, (int) size
) == -1)
777 err(1, "write error extracting inode %lu, name %s\nwrite",
778 (unsigned long)curfile
.ino
, curfile
.name
);
782 * Skip over a hole in a file.
786 xtrskip(char *buf
, size_t size
)
789 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
790 err(1, "seek error extracting inode %lu, name %s\nlseek",
791 (unsigned long)curfile
.ino
, curfile
.name
);
795 * Collect the next block of a symbolic link.
798 xtrlnkfile(char *buf
, size_t size
)
802 if (pathlen
> MAXPATHLEN
)
803 errx(1, "symbolic link name: %s->%s%s; too long %d",
804 curfile
.name
, lnkbuf
, buf
, pathlen
);
805 (void) strcat(lnkbuf
, buf
);
809 * Skip over a hole in a symbolic link (should never happen).
813 xtrlnkskip(char *buf
, size_t size
)
816 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
820 * Collect the next block of a bit map.
823 xtrmap(char *buf
, size_t size
)
826 memmove(map
, buf
, size
);
831 * Skip over a hole in a bit map (should never happen).
835 xtrmapskip(char *buf
, size_t size
)
838 panic("hole in map\n");
843 * Noop, when an extraction function is not needed.
847 xtrnull(char *buf
, size_t size
)
854 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
856 static char buf_tape
[BUFSIZ
];
857 static char buf_disk
[BUFSIZ
];
862 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
863 close(fd_tape
), close(fd_disk
);
864 panic("do_cmpfiles: unexpected EOF[1]");
866 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
867 close(fd_tape
), close(fd_disk
);
868 panic("do_cmpfiles: unexpected EOF[2]");
870 if (n_tape
!= n_disk
) {
871 close(fd_tape
), close(fd_disk
);
872 panic("do_cmpfiles: sizes different!");
874 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
880 /* for debugging compare problems */
881 #undef COMPARE_FAIL_KEEP_FILE
884 #ifdef COMPARE_FAIL_KEEP_FILE
885 /* return true if tapefile should be unlinked after compare */
890 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
892 struct stat sbuf_tape
;
893 int fd_tape
, fd_disk
;
895 if (stat(tapefile
, &sbuf_tape
) != 0) {
896 panic("Can't lstat tmp file %s: %s\n", tapefile
,
900 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
902 "%s: size changed from %ld to %ld.\n",
903 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
904 #ifdef COMPARE_FAIL_KEEP_FILE
911 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
912 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
914 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
916 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
919 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
920 fprintf(stderr
, "%s: tape and disk copies are different\n",
924 #ifdef COMPARE_FAIL_KEEP_FILE
925 /* rename the file to live in /tmp */
926 /* rename `tapefile' to /tmp/<basename of diskfile> */
928 char *p
= strrchr(diskfile
, '/');
929 char newname
[MAXPATHLEN
];
931 panic("can't find / in %s\n", diskfile
);
933 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
934 if (rename(tapefile
, newname
)) {
935 panic("rename from %s to %s failed: %s\n",
939 fprintf(stderr
, "*** %s saved to %s\n",
944 /* don't unlink the file (it's not there anymore */
953 #ifdef COMPARE_FAIL_KEEP_FILE
958 static char tmpfilename
[MAXPATHLEN
];
961 comparefile(char *name
)
963 static char *tmpfile
= NULL
;
965 struct stat sb
, stemp
;
968 if ((r
= lstat(name
, &sb
)) != 0) {
969 warn("%s: does not exist (%d)", name
, r
);
975 curfile
.action
= USING
;
976 mode
= curfile
.dip
->di_mode
;
978 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
979 (long)sb
.st_size
, mode
);
981 if (sb
.st_mode
!= mode
) {
982 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
983 name
, mode
& 07777, sb
.st_mode
& 07777);
985 switch (mode
& IFMT
) {
999 char lbuf
[MAXPATHLEN
+ 1];
1002 if (!(sb
.st_mode
& S_IFLNK
)) {
1003 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1009 getfile(xtrlnkfile
, xtrlnkskip
);
1012 "%s: zero length symbolic link (ignored)\n",
1016 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1017 panic("readlink of %s failed: %s", name
,
1021 if (strcmp(lbuf
, lnkbuf
) != 0) {
1023 "%s: symbolic link changed from %s to %s.\n",
1024 name
, lnkbuf
, lbuf
);
1032 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1033 fprintf(stderr
, "%s: no longer a special file\n",
1039 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1041 "%s: device changed from %d,%d to %d,%d.\n",
1043 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1044 (int)curfile
.dip
->di_rdev
& 0xff,
1045 ((int)sb
.st_rdev
>> 8) & 0xff,
1046 (int)sb
.st_rdev
& 0xff);
1052 if (tmpfile
== NULL
) {
1053 /* argument to mktemp() must not be in RO space: */
1054 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1055 tmpfile
= mktemp(&tmpfilename
[0]);
1057 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1058 panic("cannot delete tmp file %s: %s\n",
1059 tmpfile
, strerror(errno
));
1061 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1062 panic("cannot create file temp file %s: %s\n",
1063 name
, strerror(errno
));
1065 getfile(xtrfile
, xtrskip
);
1066 (void) close(ofile
);
1067 #ifdef COMPARE_FAIL_KEEP_FILE
1068 if (cmpfiles(tmpfile
, name
, &sb
))
1071 cmpfiles(tmpfile
, name
, &sb
);
1080 * Read TP_BSIZE blocks from the input.
1081 * Handle read errors, and end of media.
1086 ssize_t rd
, newvol
, i
;
1087 int cnt
, seek_failed
;
1089 if (blkcnt
< numtrec
) {
1090 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1095 for (i
= 0; i
< ntrec
; i
++)
1096 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1099 cnt
= ntrec
* TP_BSIZE
;
1104 i
= rmtread(&tapebuf
[rd
], cnt
);
1107 i
= read(mt
, &tapebuf
[rd
], cnt
);
1109 * Check for mid-tape short read error.
1110 * If found, skip rest of buffer and start with the next.
1112 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1113 Dprintf(stdout
, "mid-media short read error.\n");
1117 * Handle partial block read.
1119 if (pipein
&& i
== 0 && rd
> 0)
1121 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1130 * Short read. Process the blocks read.
1132 if (i
% TP_BSIZE
!= 0)
1134 "partial block read: %ld should be %ld\n",
1135 (long)i
, ntrec
* TP_BSIZE
);
1136 numtrec
= i
/ TP_BSIZE
;
1140 * Handle read error.
1143 fprintf(stderr
, "Tape read error while ");
1144 switch (curfile
.action
) {
1146 fprintf(stderr
, "trying to set up tape\n");
1149 fprintf(stderr
, "trying to resynchronize\n");
1152 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1155 fprintf(stderr
, "skipping over inode %lu\n",
1156 (unsigned long)curfile
.ino
);
1159 if (!yflag
&& !reply("continue"))
1161 i
= ntrec
* TP_BSIZE
;
1162 memset(tapebuf
, 0, (size_t)i
);
1165 seek_failed
= (rmtseek(i
, 1) < 0);
1168 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1171 err(1, "continuation failed");
1174 * Handle end of tape.
1177 Vprintf(stdout
, "End-of-tape encountered\n");
1186 if (rd
% TP_BSIZE
!= 0)
1187 panic("partial block read: %d should be %d\n",
1188 rd
, ntrec
* TP_BSIZE
);
1190 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1193 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1199 findtapeblksize(void)
1203 for (i
= 0; i
< ntrec
; i
++)
1204 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1208 i
= rmtread(tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1211 i
= read(mt
, tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1214 err(1, "tape read error");
1215 if (i
% TP_BSIZE
!= 0)
1216 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1218 ntrec
= i
/ TP_BSIZE
;
1220 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1238 * Read the next block from the tape.
1239 * Check to see if it is one of several vintage headers.
1240 * If it is an old style header, convert it to a new style header.
1241 * If it is not any valid header, return an error.
1244 gethead(struct s_spcl
*buf
)
1252 char dummy
[TP_BSIZE
];
1259 u_int16_t c_inumber
;
1264 u_int16_t odi_nlink
;
1280 readtape((char *)buf
);
1281 if (buf
->c_magic
!= NFS_MAGIC
) {
1282 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1285 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1289 if (checksum((int *)buf
) == FAIL
)
1292 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1295 readtape((char *)(&u_ospcl
.s_ospcl
));
1296 memset((char *)buf
, 0, (long)TP_BSIZE
);
1297 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1298 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1299 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1300 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1301 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1302 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1303 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1304 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1305 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1306 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1307 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1308 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1309 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1310 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1312 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1313 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1314 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1315 #else /* __linux__ */
1316 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1317 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1318 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1319 #endif /* __linux__ */
1320 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1321 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1322 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1323 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1325 buf
->c_magic
= NFS_MAGIC
;
1328 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1329 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1330 qcvt
.qval
= buf
->c_dinode
.di_size
;
1331 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1332 printf("Note: Doing Quad swapping\n");
1337 qcvt
.qval
= buf
->c_dinode
.di_size
;
1339 qcvt
.val
[1] = qcvt
.val
[0];
1341 buf
->c_dinode
.di_size
= qcvt
.qval
;
1345 switch (buf
->c_type
) {
1350 * Have to patch up missing information in bit map headers
1353 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1354 if (buf
->c_count
> TP_NINDIR
)
1357 for (i
= 0; i
< buf
->c_count
; i
++)
1362 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1374 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1378 * If we are restoring a filesystem with old format inodes,
1379 * copy the uid/gid to the new location.
1382 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1383 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1391 * Check that a header is where it belongs and predict the next header
1394 accthdr(struct s_spcl
*header
)
1396 static ino_t previno
= 0x7fffffff;
1397 static int prevtype
;
1398 static long predict
;
1401 if (header
->c_type
== TS_TAPE
) {
1402 fprintf(stderr
, "Volume header (%s inode format) ",
1403 oldinofmt
? "old" : "new");
1404 if (header
->c_firstrec
)
1405 fprintf(stderr
, "begins with record %d",
1406 header
->c_firstrec
);
1407 fprintf(stderr
, "\n");
1408 previno
= 0x7fffffff;
1411 if (previno
== 0x7fffffff)
1415 fprintf(stderr
, "Dumped inodes map header");
1418 fprintf(stderr
, "Used inodes map header");
1421 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
1424 fprintf(stderr
, "File continuation header, ino %ld", previno
);
1427 fprintf(stderr
, "End of tape header");
1430 if (predict
!= blksread
- 1)
1431 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1432 predict
, blksread
- 1);
1433 fprintf(stderr
, "\n");
1436 if (header
->c_type
!= TS_END
)
1437 for (i
= 0; i
< header
->c_count
; i
++)
1438 if (readmapflag
|| header
->c_addr
[i
] != 0)
1442 prevtype
= header
->c_type
;
1443 previno
= header
->c_inumber
;
1447 * Find an inode header.
1448 * Complain if had to skip, and complain is set.
1451 findinode(struct s_spcl
*header
)
1453 static long skipcnt
= 0;
1457 curfile
.name
= "<name unknown>";
1458 curfile
.action
= UNKNOWN
;
1462 if (header
->c_magic
!= NFS_MAGIC
) {
1464 while (gethead(header
) == FAIL
||
1465 header
->c_date
!= dumpdate
)
1468 switch (header
->c_type
) {
1472 * Skip up to the beginning of the next record
1474 for (i
= 0; i
< header
->c_count
; i
++)
1475 if (header
->c_addr
[i
])
1477 while (gethead(header
) == FAIL
||
1478 header
->c_date
!= dumpdate
)
1483 curfile
.dip
= &header
->c_dinode
;
1484 curfile
.ino
= header
->c_inumber
;
1488 curfile
.ino
= maxino
;
1492 curfile
.name
= "<file removal list>";
1496 curfile
.name
= "<file dump list>";
1500 panic("unexpected tape header\n");
1504 panic("unknown tape header type %d\n", spcl
.c_type
);
1508 } while (header
->c_type
== TS_ADDR
);
1510 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1520 j
= sizeof(union u_spcl
) / sizeof(int);
1527 /* What happens if we want to read restore tapes
1528 for a 16bit int machine??? */
1534 if (i
!= CHECKSUM
) {
1535 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
1536 (unsigned long)curfile
.ino
, curfile
.name
);
1546 #include <varargs.h>
1551 msg(const char *fmt
, ...)
1564 (void)vfprintf(stderr
, fmt
, ap
);
1567 #endif /* RRESTORE */
1570 swab16(u_char
*sp
, int n
)
1575 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1582 swab32(u_char
*sp
, int n
)
1587 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1588 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
1595 swab64(u_char
*sp
, int n
)
1600 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
1601 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
1602 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
1603 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
1610 swabst(u_char
*cp
, u_char
*sp
)
1616 case '0': case '1': case '2': case '3': case '4':
1617 case '5': case '6': case '7': case '8': case '9':
1618 n
= (n
* 10) + (*cp
++ - '0');
1621 case 's': case 'w': case 'h':
1639 default: /* Any other character, like 'b' counts as byte. */
1653 swabst((u_char
*)"i", (u_char
*)&x
);
1660 swabst((u_char
*)"l", (u_char
*)&x
);