]>
git.wh0rd.org - dump.git/blob - restore/tape.c
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 static const char rcsid
[] =
49 "$Id: tape.c,v 1.22 2000/12/21 11:14:54 stelian Exp $";
53 #include <sys/param.h>
60 #include <linux/ext2_fs.h>
61 #include <bsdcompat.h>
63 #include <ufs/ufs/dinode.h>
64 #endif /* __linux__ */
65 #include <protocols/dumprestore.h>
68 #include <compaterr.h>
76 #include <ext2fs/ext2fs.h>
81 #include "pathnames.h"
83 static long fssize
= MAXBSIZE
;
85 static int pipein
= 0;
86 static char magtape
[MAXPATHLEN
];
87 static char magtapeprefix
[MAXPATHLEN
];
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 sigjmp_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_int swabi
__P((u_int
));
118 static u_long swabl
__P((u_long
));
119 static u_char
*swab64
__P((u_char
*, int));
120 static u_char
*swab32
__P((u_char
*, int));
121 static u_char
*swab16
__P((u_char
*, int));
122 static void terminateinput
__P((void));
123 static void xtrfile
__P((char *, size_t));
124 static void xtrlnkfile
__P((char *, size_t));
125 static void xtrlnkskip
__P((char *, size_t));
126 static void xtrmap
__P((char *, size_t));
127 static void xtrmapskip
__P((char *, size_t));
128 static void xtrskip
__P((char *, size_t));
130 #define COMPARE_ONTHEFLY 1
133 static int ifile
; /* input file for compare */
134 static int cmperror
; /* compare error */
135 static void xtrcmpfile
__P((char *, size_t));
136 static void xtrcmpskip
__P((char *, size_t));
139 static int readmapflag
;
142 * Set up an input source
145 setinput(char *source
)
151 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
155 if (strchr(source
, ':')) {
157 source
= strchr(host
, ':');
159 if (rmthost(host
) == 0)
163 if (strcmp(source
, "-") == 0) {
165 * Since input is coming from a pipe we must establish
166 * our own connection to the terminal.
168 terminal
= fopen(_PATH_TTY
, "r");
169 if (terminal
== NULL
) {
170 warn("cannot open %s", _PATH_TTY
);
171 terminal
= fopen(_PATH_DEVNULL
, "r");
172 if (terminal
== NULL
)
173 err(1, "cannot open %s", _PATH_DEVNULL
);
177 setuid(getuid()); /* no longer need or want root privileges */
179 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
180 magtapeprefix
[MAXPATHLEN
-1] = '\0';
181 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
184 strncpy(magtape
, source
, MAXPATHLEN
);
185 magtape
[MAXPATHLEN
- 1] = '\0';
189 newtapebuf(long size
)
191 static int tapebufsize
= -1;
194 if (size
<= tapebufsize
)
198 tapebuf
= malloc(size
* TP_BSIZE
);
200 errx(1, "Cannot allocate space for tape buffer");
205 * Verify that the tape drive can be accessed and
206 * that it actually is a dump tape.
214 Vprintf(stdout
, "Verify tape and initialize maps\n");
217 mt
= rmtopen(magtape
, 0);
223 mt
= open(magtape
, O_RDONLY
, 0);
225 err(1, "%s", magtape
);
229 if (!pipein
&& !bflag
)
231 if (gethead(&spcl
) == FAIL
) {
232 blkcnt
--; /* push back this block */
236 if (gethead(&spcl
) == FAIL
)
237 errx(1, "Tape is not a dump tape");
238 fprintf(stderr
, "Converting to new file system format.\n");
241 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
242 endoftapemark
.s_spcl
.c_type
= TS_END
;
243 ip
= (int *)&endoftapemark
;
244 j
= sizeof(union u_spcl
) / sizeof(int);
249 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
251 if (vflag
|| command
== 't' || command
== 'C')
253 if (filesys
[0] == '\0') {
255 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
256 filesys
[NAMELEN
- 1] = '\0';
257 dirptr
= strstr(filesys
, " (dir");
261 dumptime
= spcl
.c_ddate
;
262 dumpdate
= spcl
.c_date
;
263 if (stat(".", &stbuf
) < 0)
264 err(1, "cannot stat .");
265 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
267 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
268 fssize
= stbuf
.st_blksize
;
269 if (((fssize
- 1) & fssize
) != 0)
270 errx(1, "bad block size %ld", fssize
);
271 if (spcl
.c_volume
!= 1)
272 errx(1, "Tape is not volume 1 of the dump");
273 if (gethead(&spcl
) == FAIL
) {
274 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
275 panic("no header after volume mark!\n");
278 if (spcl
.c_type
!= TS_CLRI
)
279 errx(1, "Cannot find file removal list");
280 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
281 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
282 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
284 errx(1, "no memory for active inode map");
286 curfile
.action
= USING
;
287 getfile(xtrmap
, xtrmapskip
);
288 if (spcl
.c_type
!= TS_BITS
)
289 errx(1, "Cannot find file dump list");
290 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
291 if (map
== (char *)NULL
)
292 errx(1, "no memory for file dump list");
294 curfile
.action
= USING
;
295 getfile(xtrmap
, xtrmapskip
);
297 * If there may be whiteout entries on the tape, pretend that the
298 * whiteout inode exists, so that the whiteout entries can be
302 SETINO(WINO
, dumpmap
);
306 * Prompt user to load a new dump volume.
307 * "Nextvol" is the next suggested volume to use.
308 * This suggested volume is enforced when doing full
309 * or incremental restores, but can be overridden by
310 * the user when only extracting a subset of the files.
315 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
316 union u_spcl tmpspcl
;
317 # define tmpbuf tmpspcl.s_spcl
327 panic("Changing volumes on pipe input?\n");
335 exit(1); /* pipes do not get a second chance */
336 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
343 while (newvol
<= 0) {
344 if (tapesread
== 0) {
345 fprintf(stderr
, "%s%s%s%s%s",
346 "You have not read any tapes yet.\n",
347 "Unless you know which volume your",
348 " file(s) are on you should start\n",
349 "with the last volume and work",
350 " towards the first.\n");
352 fprintf(stderr
, "You have read volumes");
354 for (i
= 1; i
< 32; i
++)
355 if (tapesread
& (1 << i
)) {
356 fprintf(stderr
, "%s%ld", buf
, (long)i
);
359 fprintf(stderr
, "\n");
362 fprintf(stderr
, "Specify next volume #: ");
363 (void) fflush(stderr
);
364 (void) fgets(buf
, TP_BSIZE
, terminal
);
365 } while (!feof(terminal
) && buf
[0] == '\n');
371 "Volume numbers are positive numerics\n");
374 if (newvol
== volno
) {
375 tapesread
|= 1 << volno
;
380 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
381 magtape
[MAXPATHLEN
- 1] = '\0';
383 if (!Mflag
|| haderror
) {
385 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
386 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
387 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
388 (void) fflush(stderr
);
389 (void) fgets(buf
, TP_BSIZE
, terminal
);
392 if (!strcmp(buf
, "none\n")) {
396 if (buf
[0] != '\n') {
397 (void) strcpy(magtape
, buf
);
398 magtape
[strlen(magtape
) - 1] = '\0';
403 mt
= rmtopen(magtape
, 0);
406 mt
= open(magtape
, O_RDONLY
, 0);
409 fprintf(stderr
, "Cannot open %s\n", magtape
);
418 if (gethead(&tmpbuf
) == FAIL
) {
419 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
420 fprintf(stderr
, "tape is not dump tape\n");
425 if (tmpbuf
.c_volume
!= volno
) {
426 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
431 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
433 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
434 ctime4(&tmpbuf
.c_date
));
435 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
437 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
438 ctime(&tmpbuf
.c_date
));
439 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
445 tapesread
|= 1 << volno
;
448 * If continuing from the previous volume, skip over any
449 * blocks read already at the end of the previous volume.
451 * If coming to this volume at random, skip to the beginning
452 * of the next record.
454 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
455 tpblksread
, (long)tmpbuf
.c_firstrec
);
456 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
458 tpblksread
= tmpbuf
.c_firstrec
;
459 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
461 } else if (tmpbuf
.c_firstrec
> 0 &&
462 tmpbuf
.c_firstrec
< tpblksread
- 1) {
464 * -1 since we've read the volume header
466 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
467 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
468 (long)i
, i
> 1 ? "s" : "");
473 if (curfile
.action
== USING
) {
475 panic("active file into volume 1\n");
479 * Skip up to the beginning of the next record
481 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
482 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
484 (void) gethead(&spcl
);
488 siglongjmp(restart
, 1);
493 * Handle unexpected EOF.
499 if (gettingfile
&& curfile
.action
== USING
) {
500 printf("Warning: %s %s\n",
501 "End-of-input encountered while extracting", curfile
.name
);
503 curfile
.name
= "<name unknown>";
504 curfile
.action
= UNKNOWN
;
506 curfile
.ino
= maxino
;
509 siglongjmp(restart
, 1);
514 * handle multiple dumps per tape by skipping forward to the
522 if (dumpnum
== 1 || volno
!= 1)
525 errx(1, "Cannot have multiple dumps on pipe input");
527 tcom
.mt_count
= dumpnum
- 1;
530 rmtioctl(MTFSF
, dumpnum
- 1);
533 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
541 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
542 fprintf(stdout
, "Dumped from: %s",
543 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
545 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
546 fprintf(stdout
, "Dumped from: %s",
547 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
549 if (spcl
.c_host
[0] == '\0')
551 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
552 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
553 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
557 extractfile(char *name
)
561 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
));
605 uid_t luid
= curfile
.dip
->di_uid
;
606 gid_t lgid
= curfile
.dip
->di_gid
;
610 getfile(xtrlnkfile
, xtrlnkskip
);
613 "%s: zero length symbolic link (ignored)\n", name
);
616 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
619 (void) lchown(name
, luid
, lgid
);
625 Vprintf(stdout
, "extract fifo %s\n", name
);
632 if (mkfifo(name
, mode
) < 0) {
633 warn("%s: cannot create fifo", name
);
637 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
638 (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 warn("%s: cannot create special file", name
);
663 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
664 (void) chmod(name
, mode
);
668 warn("%s: fsetflags called on a special file", name
);
669 (void) fsetflags(name
, flags
);
672 (void) chflags(name
, flags
);
679 Vprintf(stdout
, "extract file %s\n", name
);
686 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
688 warn("%s: cannot create file", name
);
692 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
693 (void) fchmod(ofile
, mode
);
696 (void) setflags(ofile
, flags
);
698 (void) fchflags(ofile
, flags
);
700 getfile(xtrfile
, xtrskip
);
709 * skip over bit maps on the tape
715 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
720 * skip over a file on the tape
726 curfile
.action
= SKIP
;
727 getfile(xtrnull
, xtrnull
);
731 * Extract a file from the tape.
732 * When an allocated block is found it is passed to the fill function;
733 * when an unallocated block (hole) is found, a zeroed buffer is passed
734 * to the skip function.
737 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
740 volatile int curblk
= 0;
741 volatile quad_t size
= spcl
.c_dinode
.di_size
;
742 volatile int last_write_was_hole
= 0;
743 quad_t origsize
= size
;
744 static char clearedbuf
[MAXBSIZE
];
745 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
748 if (spcl
.c_type
== TS_END
)
749 panic("ran off end of tape\n");
750 if (spcl
.c_magic
!= NFS_MAGIC
)
751 panic("not at beginning of a file\n");
752 if (!gettingfile
&& setjmp(restart
) != 0)
756 for (i
= 0; i
< spcl
.c_count
; i
++) {
757 if (readmapflag
|| spcl
.c_addr
[i
]) {
758 readtape(&buf
[curblk
++][0]);
759 if (curblk
== fssize
/ TP_BSIZE
) {
760 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
761 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
763 last_write_was_hole
= 0;
767 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
769 (curblk
- 1) * TP_BSIZE
+ size
));
772 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
774 last_write_was_hole
= 1;
776 if ((size
-= TP_BSIZE
) <= 0) {
777 for (i
++; i
< spcl
.c_count
; i
++)
778 if (readmapflag
|| spcl
.c_addr
[i
])
783 if (gethead(&spcl
) == GOOD
&& size
> 0) {
784 if (spcl
.c_type
== TS_ADDR
)
787 "Missing address (header) block for %s at %ld blocks\n",
788 curfile
.name
, (long)blksread
);
791 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
792 last_write_was_hole
= 0;
794 if (last_write_was_hole
) {
795 ftruncate(ofile
, origsize
);
802 * Write out the next block of a file.
805 xtrfile(char *buf
, size_t size
)
810 if (write(ofile
, buf
, (int) size
) == -1)
811 err(1, "write error extracting inode %lu, name %s\nwrite",
812 (unsigned long)curfile
.ino
, curfile
.name
);
816 * Skip over a hole in a file.
820 xtrskip(char *buf
, size_t size
)
823 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
824 err(1, "seek error extracting inode %lu, name %s\nlseek",
825 (unsigned long)curfile
.ino
, curfile
.name
);
829 * Collect the next block of a symbolic link.
832 xtrlnkfile(char *buf
, size_t size
)
836 if (pathlen
> MAXPATHLEN
)
837 errx(1, "symbolic link name: %s->%s%s; too long %d",
838 curfile
.name
, lnkbuf
, buf
, pathlen
);
839 (void) strcat(lnkbuf
, buf
);
843 * Skip over a hole in a symbolic link (should never happen).
847 xtrlnkskip(char *buf
, size_t size
)
850 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
854 * Collect the next block of a bit map.
857 xtrmap(char *buf
, size_t size
)
860 memmove(map
, buf
, size
);
865 * Skip over a hole in a bit map (should never happen).
869 xtrmapskip(char *buf
, size_t size
)
872 panic("hole in map\n");
877 * Noop, when an extraction function is not needed.
881 xtrnull(char *buf
, size_t size
)
889 * Compare the next block of a file.
892 xtrcmpfile(char *buf
, size_t size
)
894 static char cmpbuf
[MAXBSIZE
];
899 if (read(ifile
, cmpbuf
, size
) != size
) {
900 fprintf(stderr
, "%s: size has changed.\n",
906 if (memcmp(buf
, cmpbuf
, size
) != 0) {
907 fprintf(stderr
, "%s: tape and disk copies are different\n",
915 * Skip over a hole in a file.
918 xtrcmpskip(char *buf
, size_t size
)
920 static char cmpbuf
[MAXBSIZE
];
926 if (read(ifile
, cmpbuf
, size
) != size
) {
927 fprintf(stderr
, "%s: size has changed.\n",
933 for (i
= 0; i
< size
; ++i
)
934 if (cmpbuf
[i
] != '\0') {
935 fprintf(stderr
, "%s: tape and disk copies are different\n",
941 #endif /* COMPARE_ONTHEFLY */
943 #if !COMPARE_ONTHEFLY
945 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
947 static char buf_tape
[BUFSIZ
];
948 static char buf_disk
[BUFSIZ
];
953 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
954 close(fd_tape
), close(fd_disk
);
955 panic("do_cmpfiles: unexpected EOF[1]");
957 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
958 close(fd_tape
), close(fd_disk
);
959 panic("do_cmpfiles: unexpected EOF[2]");
961 if (n_tape
!= n_disk
) {
962 close(fd_tape
), close(fd_disk
);
963 panic("do_cmpfiles: sizes different!");
965 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
971 /* for debugging compare problems */
972 #undef COMPARE_FAIL_KEEP_FILE
975 #ifdef COMPARE_FAIL_KEEP_FILE
976 /* return true if tapefile should be unlinked after compare */
981 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
983 struct stat sbuf_tape
;
984 int fd_tape
, fd_disk
;
986 if (stat(tapefile
, &sbuf_tape
) != 0) {
987 panic("Can't lstat tmp file %s: %s\n", tapefile
,
992 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
994 "%s: size changed from %ld to %ld.\n",
995 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
997 #ifdef COMPARE_FAIL_KEEP_FILE
1004 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
1005 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1008 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
1010 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1014 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1015 fprintf(stderr
, "%s: tape and disk copies are different\n",
1020 #ifdef COMPARE_FAIL_KEEP_FILE
1021 /* rename the file to live in /tmp */
1022 /* rename `tapefile' to /tmp/<basename of diskfile> */
1024 char *p
= strrchr(diskfile
, '/');
1025 char newname
[MAXPATHLEN
];
1027 panic("can't find / in %s\n", diskfile
);
1029 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1030 if (rename(tapefile
, newname
)) {
1031 panic("rename from %s to %s failed: %s\n",
1035 fprintf(stderr
, "*** %s saved to %s\n",
1040 /* don't unlink the file (it's not there anymore */
1049 #ifdef COMPARE_FAIL_KEEP_FILE
1053 #endif /* !COMPARE_ONTHEFLY */
1055 #if !COMPARE_ONTHEFLY
1056 static char tmpfilename
[MAXPATHLEN
];
1060 comparefile(char *name
)
1065 #if !COMPARE_ONTHEFLY
1066 static char *tmpfile
= NULL
;
1070 if ((r
= lstat(name
, &sb
)) != 0) {
1071 warn("%s: does not exist (%d)", name
, r
);
1077 curfile
.name
= name
;
1078 curfile
.action
= USING
;
1079 mode
= curfile
.dip
->di_mode
;
1081 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1082 (long)sb
.st_size
, mode
);
1084 if (sb
.st_mode
!= mode
) {
1085 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1086 name
, mode
& 07777, sb
.st_mode
& 07777);
1089 switch (mode
& IFMT
) {
1103 char lbuf
[MAXPATHLEN
+ 1];
1106 if (!(sb
.st_mode
& S_IFLNK
)) {
1107 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1114 getfile(xtrlnkfile
, xtrlnkskip
);
1117 "%s: zero length symbolic link (ignored)\n",
1122 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1123 panic("readlink of %s failed: %s", name
,
1128 if (strcmp(lbuf
, lnkbuf
) != 0) {
1130 "%s: symbolic link changed from %s to %s.\n",
1131 name
, lnkbuf
, lbuf
);
1140 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1141 fprintf(stderr
, "%s: no longer a special file\n",
1148 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1150 "%s: device changed from %d,%d to %d,%d.\n",
1152 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1153 (int)curfile
.dip
->di_rdev
& 0xff,
1154 ((int)sb
.st_rdev
>> 8) & 0xff,
1155 (int)sb
.st_rdev
& 0xff);
1162 #if COMPARE_ONTHEFLY
1163 if ((ifile
= open(name
, O_RDONLY
)) < 0) {
1164 panic("Can't open %s: %s\n", name
, strerror(errno
));
1170 getfile(xtrcmpfile
, xtrcmpskip
);
1173 if (read(ifile
, &c
, 1) != 0) {
1174 fprintf(stderr
, "%s: size has changed.\n",
1184 if (tmpfile
== NULL
) {
1185 /* argument to mktemp() must not be in RO space: */
1186 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1187 tmpfile
= mktemp(&tmpfilename
[0]);
1189 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1190 panic("cannot delete tmp file %s: %s\n",
1191 tmpfile
, strerror(errno
));
1193 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1194 panic("cannot create file temp file %s: %s\n",
1195 name
, strerror(errno
));
1197 getfile(xtrfile
, xtrskip
);
1198 (void) close(ofile
);
1199 #ifdef COMPARE_FAIL_KEEP_FILE
1200 if (cmpfiles(tmpfile
, name
, &sb
))
1203 cmpfiles(tmpfile
, name
, &sb
);
1206 #endif /* COMPARE_ONTHEFLY */
1213 * Read TP_BSIZE blocks from the input.
1214 * Handle read errors, and end of media.
1219 ssize_t rd
, newvol
, i
;
1220 int cnt
, seek_failed
;
1222 if (blkcnt
< numtrec
) {
1223 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1228 for (i
= 0; i
< ntrec
; i
++)
1229 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1232 cnt
= ntrec
* TP_BSIZE
;
1237 i
= rmtread(&tapebuf
[rd
], cnt
);
1240 i
= read(mt
, &tapebuf
[rd
], cnt
);
1242 * Check for mid-tape short read error.
1243 * If found, skip rest of buffer and start with the next.
1245 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1246 Dprintf(stdout
, "mid-media short read error.\n");
1250 * Handle partial block read.
1252 if (pipein
&& i
== 0 && rd
> 0)
1254 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1263 * Short read. Process the blocks read.
1265 if (i
% TP_BSIZE
!= 0)
1267 "partial block read: %ld should be %ld\n",
1268 (long)i
, ntrec
* TP_BSIZE
);
1269 numtrec
= i
/ TP_BSIZE
;
1273 * Handle read error.
1276 fprintf(stderr
, "Tape read error while ");
1277 switch (curfile
.action
) {
1279 fprintf(stderr
, "trying to set up tape\n");
1282 fprintf(stderr
, "trying to resynchronize\n");
1285 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1288 fprintf(stderr
, "skipping over inode %lu\n",
1289 (unsigned long)curfile
.ino
);
1292 if (!yflag
&& !reply("continue"))
1294 i
= ntrec
* TP_BSIZE
;
1295 memset(tapebuf
, 0, (size_t)i
);
1298 seek_failed
= (rmtseek(i
, 1) < 0);
1301 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1304 err(1, "continuation failed");
1307 * Handle end of tape.
1310 Vprintf(stdout
, "End-of-tape encountered\n");
1319 if (rd
% TP_BSIZE
!= 0)
1320 panic("partial block read: %d should be %d\n",
1321 rd
, ntrec
* TP_BSIZE
);
1323 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1326 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1332 findtapeblksize(void)
1336 for (i
= 0; i
< ntrec
; i
++)
1337 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1341 i
= rmtread(tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1344 i
= read(mt
, tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1347 err(1, "tape read error");
1348 if (i
% TP_BSIZE
!= 0)
1349 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1351 ntrec
= i
/ TP_BSIZE
;
1353 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1371 * Read the next block from the tape.
1372 * Check to see if it is one of several vintage headers.
1373 * If it is an old style header, convert it to a new style header.
1374 * If it is not any valid header, return an error.
1377 gethead(struct s_spcl
*buf
)
1385 char dummy
[TP_BSIZE
];
1392 u_int16_t c_inumber
;
1397 u_int16_t odi_nlink
;
1413 readtape((char *)buf
);
1414 if (buf
->c_magic
!= NFS_MAGIC
) {
1415 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1418 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1422 if (checksum((int *)buf
) == FAIL
)
1425 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1428 readtape((char *)(&u_ospcl
.s_ospcl
));
1429 memset((char *)buf
, 0, (long)TP_BSIZE
);
1430 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1431 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1432 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1433 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1434 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1435 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1436 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1437 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1438 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1439 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1440 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1441 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1442 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1443 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1445 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1446 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1447 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1448 #else /* __linux__ */
1449 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1450 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1451 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1452 #endif /* __linux__ */
1453 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1454 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1455 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1456 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1458 buf
->c_magic
= NFS_MAGIC
;
1461 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1462 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1463 qcvt
.qval
= buf
->c_dinode
.di_size
;
1464 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1465 printf("Note: Doing Quad swapping\n");
1470 qcvt
.qval
= buf
->c_dinode
.di_size
;
1472 qcvt
.val
[1] = qcvt
.val
[0];
1474 buf
->c_dinode
.di_size
= qcvt
.qval
;
1478 switch (buf
->c_type
) {
1483 * Have to patch up missing information in bit map headers
1486 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1487 if (buf
->c_count
> TP_NINDIR
)
1490 for (i
= 0; i
< buf
->c_count
; i
++)
1495 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1507 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1511 * If we are restoring a filesystem with old format inodes,
1512 * copy the uid/gid to the new location.
1515 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1516 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1524 * Check that a header is where it belongs and predict the next header
1527 accthdr(struct s_spcl
*header
)
1529 static ino_t previno
= 0x7fffffff;
1530 static int prevtype
;
1531 static long predict
;
1534 if (header
->c_type
== TS_TAPE
) {
1535 fprintf(stderr
, "Volume header (%s inode format) ",
1536 oldinofmt
? "old" : "new");
1537 if (header
->c_firstrec
)
1538 fprintf(stderr
, "begins with record %d",
1539 header
->c_firstrec
);
1540 fprintf(stderr
, "\n");
1541 previno
= 0x7fffffff;
1544 if (previno
== 0x7fffffff)
1548 fprintf(stderr
, "Dumped inodes map header");
1551 fprintf(stderr
, "Used inodes map header");
1554 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
1557 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
1560 fprintf(stderr
, "End of tape header");
1563 if (predict
!= blksread
- 1)
1564 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1565 predict
, blksread
- 1);
1566 fprintf(stderr
, "\n");
1569 if (header
->c_type
!= TS_END
)
1570 for (i
= 0; i
< header
->c_count
; i
++)
1571 if (readmapflag
|| header
->c_addr
[i
] != 0)
1575 prevtype
= header
->c_type
;
1576 previno
= header
->c_inumber
;
1580 * Find an inode header.
1581 * Complain if had to skip, and complain is set.
1584 findinode(struct s_spcl
*header
)
1586 static long skipcnt
= 0;
1590 curfile
.name
= "<name unknown>";
1591 curfile
.action
= UNKNOWN
;
1595 if (header
->c_magic
!= NFS_MAGIC
) {
1597 while (gethead(header
) == FAIL
||
1598 header
->c_date
!= dumpdate
)
1601 switch (header
->c_type
) {
1605 * Skip up to the beginning of the next record
1607 for (i
= 0; i
< header
->c_count
; i
++)
1608 if (header
->c_addr
[i
])
1610 while (gethead(header
) == FAIL
||
1611 header
->c_date
!= dumpdate
)
1616 curfile
.dip
= &header
->c_dinode
;
1617 curfile
.ino
= header
->c_inumber
;
1621 curfile
.ino
= maxino
;
1625 curfile
.name
= "<file removal list>";
1629 curfile
.name
= "<file dump list>";
1633 panic("unexpected tape header\n");
1637 panic("unknown tape header type %d\n", spcl
.c_type
);
1641 } while (header
->c_type
== TS_ADDR
);
1643 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1653 j
= sizeof(union u_spcl
) / sizeof(int);
1660 /* What happens if we want to read restore tapes
1661 for a 16bit int machine??? */
1667 if (i
!= CHECKSUM
) {
1668 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
1669 (unsigned long)curfile
.ino
, curfile
.name
);
1679 #include <varargs.h>
1684 msg(const char *fmt
, ...)
1697 (void)vfprintf(stderr
, fmt
, ap
);
1700 #endif /* RRESTORE */
1703 swab16(u_char
*sp
, int n
)
1708 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1715 swab32(u_char
*sp
, int n
)
1720 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1721 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
1728 swab64(u_char
*sp
, int n
)
1733 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
1734 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
1735 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
1736 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
1743 swabst(u_char
*cp
, u_char
*sp
)
1749 case '0': case '1': case '2': case '3': case '4':
1750 case '5': case '6': case '7': case '8': case '9':
1751 n
= (n
* 10) + (*cp
++ - '0');
1754 case 's': case 'w': case 'h':
1772 default: /* Any other character, like 'b' counts as byte. */
1786 swabst((u_char
*)"i", (u_char
*)&x
);
1793 swabst((u_char
*)"l", (u_char
*)&x
);