2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)tape.c 8.9 (Berkeley) 5/1/95
39 * $FreeBSD: src/sbin/restore/tape.c,v 1.16.2.8 2002/06/30 22:57:52 iedowse Exp $
40 * $DragonFly: src/sbin/restore/tape.c,v 1.9 2005/11/06 12:49:25 swildner Exp $
43 #include <sys/param.h>
49 #include <vfs/ufs/dinode.h>
50 #include <protocols/dumprestore.h>
64 static long fssize
= MAXBSIZE
;
66 static int pipein
= 0;
71 static union u_spcl endoftapemark
;
72 static long blksread
; /* blocks read since last header */
73 static long tapeaddr
= 0; /* current TP_BSIZE tape record */
74 static long tapesread
;
75 static jmp_buf restart
;
76 static int gettingfile
= 0; /* restart has a valid frame */
77 static char *host
= NULL
;
81 static char lnkbuf
[MAXPATHLEN
+ 1];
84 int oldinofmt
; /* old inode format conversion required */
85 int Bcvt
; /* Swap Bytes (for CCI or sun) */
86 static int Qcvt
; /* Swap quads (for sun) */
88 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
90 static void accthdr(struct s_spcl
*);
91 static int checksum(int *);
92 static void findinode(struct s_spcl
*);
93 static void findtapeblksize(void);
94 static int gethead(struct s_spcl
*);
95 static void readtape(char *);
96 static void setdumpnum(void);
97 static u_long
swabl(u_long
);
98 static u_char
*swablong(u_char
*, int);
99 static u_char
*swabshort(u_char
*, int);
100 static void terminateinput(void);
101 static void xtrfile(char *, long);
102 static void xtrlnkfile(char *, long);
103 static void xtrlnkskip(char *, long);
104 static void xtrmap(char *, long);
105 static void xtrmapskip(char *, long);
106 static void xtrskip(char *, long);
108 static int readmapflag
;
111 * Set up an input source
114 setinput(char *source
)
120 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
124 if (strchr(source
, ':')) {
126 source
= strchr(host
, ':');
128 if (rmthost(host
) == 0)
132 if (strcmp(source
, "-") == 0) {
134 * Since input is coming from a pipe we must establish
135 * our own connection to the terminal.
137 terminal
= fopen(_PATH_TTY
, "r");
138 if (terminal
== NULL
) {
139 fprintf(stderr
, "cannot open %s: %s\n",
140 _PATH_TTY
, strerror(errno
));
141 terminal
= fopen(_PATH_DEVNULL
, "r");
142 if (terminal
== NULL
) {
143 fprintf(stderr
, "cannot open %s: %s\n",
144 _PATH_DEVNULL
, strerror(errno
));
150 setuid(getuid()); /* no longer need or want root privileges */
151 magtape
= strdup(source
);
152 if (magtape
== NULL
) {
153 fprintf(stderr
, "Cannot allocate space for magtape buffer\n");
159 newtapebuf(long size
)
161 static long tapebufsize
= -1;
164 if (size
<= tapebufsize
)
168 tapebuf
= malloc(size
* TP_BSIZE
);
169 if (tapebuf
== NULL
) {
170 fprintf(stderr
, "Cannot allocate space for tape buffer\n");
177 * Verify that the tape drive can be accessed and
178 * that it actually is a dump tape.
186 vprintf(stdout
, "Verify tape and initialize maps\n");
189 mt
= rmtopen(magtape
, 0);
195 mt
= open(magtape
, O_RDONLY
, 0);
197 fprintf(stderr
, "%s: %s\n", magtape
, strerror(errno
));
203 if (!pipein
&& !bflag
)
205 if (gethead(&spcl
) == FAIL
) {
206 blkcnt
--; /* push back this block */
209 if (gethead(&spcl
) == FAIL
) {
210 fprintf(stderr
, "Tape is not a dump tape\n");
213 fprintf(stderr
, "Converting to new file system format.\n");
216 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
217 endoftapemark
.s_spcl
.c_type
= TS_END
;
218 ip
= (int *)&endoftapemark
;
219 j
= sizeof(union u_spcl
) / sizeof(int);
224 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
226 if (vflag
|| command
== 't')
228 dumptime
= spcl
.c_ddate
;
229 dumpdate
= spcl
.c_date
;
230 if (stat(".", &stbuf
) < 0) {
231 fprintf(stderr
, "cannot stat .: %s\n", strerror(errno
));
234 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
236 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
237 fssize
= stbuf
.st_blksize
;
238 if (((fssize
- 1) & fssize
) != 0) {
239 fprintf(stderr
, "bad block size %ld\n", fssize
);
242 if (spcl
.c_volume
!= 1) {
243 fprintf(stderr
, "Tape is not volume 1 of the dump\n");
246 if (gethead(&spcl
) == FAIL
) {
247 dprintf(stdout
, "header read failed at %ld blocks\n", blksread
);
248 panic("no header after volume mark!\n");
251 if (spcl
.c_type
!= TS_CLRI
) {
252 fprintf(stderr
, "Cannot find file removal list\n");
255 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
256 dprintf(stdout
, "maxino = %d\n", maxino
);
257 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
259 panic("no memory for active inode map\n");
261 curfile
.action
= USING
;
262 getfile(xtrmap
, xtrmapskip
);
263 if (spcl
.c_type
!= TS_BITS
) {
264 fprintf(stderr
, "Cannot find file dump list\n");
267 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
269 panic("no memory for file dump list\n");
271 curfile
.action
= USING
;
272 getfile(xtrmap
, xtrmapskip
);
274 * If there may be whiteout entries on the tape, pretend that the
275 * whiteout inode exists, so that the whiteout entries can be
279 SETINO(WINO
, dumpmap
);
280 /* 'r' restores don't call getvol() for tape 1, so mark it as read. */
286 * Prompt user to load a new dump volume.
287 * "Nextvol" is the next suggested volume to use.
288 * This suggested volume is enforced when doing full
289 * or incremental restores, but can be overridden by
290 * the user when only extracting a subset of the files.
295 long newvol
= 0, prevtapea
, savecnt
, i
;
296 union u_spcl tmpspcl
;
297 # define tmpbuf tmpspcl.s_spcl
304 prevtapea
= tapeaddr
;
308 panic("Changing volumes on pipe input?\n");
309 /* Avoid looping if we couldn't ask the user. */
310 if (yflag
|| ferror(terminal
) || feof(terminal
))
319 done(1); /* pipes do not get a second chance */
320 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
)
324 while (newvol
<= 0) {
325 if (tapesread
== 0) {
326 fprintf(stderr
, "%s%s%s%s%s%s%s",
327 "You have not read any tapes yet.\n",
328 "If you are extracting just a few files,",
329 " start with the last volume\n",
330 "and work towards the first; restore",
331 " can quickly skip tapes that\n",
332 "have no further files to extract.",
333 " Otherwise, begin with volume 1.\n");
335 fprintf(stderr
, "You have read volumes");
337 for (i
= 0; i
< 32; i
++)
338 if (tapesread
& (1 << i
)) {
339 fprintf(stderr
, "%s%ld", buf
, i
+ 1);
342 fprintf(stderr
, "\n");
345 fprintf(stderr
, "Specify next volume #: ");
347 if (fgets(buf
, BUFSIZ
, terminal
) == NULL
)
349 } while (buf
[0] == '\n');
353 "Volume numbers are positive numerics\n");
356 if (newvol
== volno
) {
357 tapesread
|= 1 << (volno
- 1);
361 fprintf(stderr
, "Mount tape volume %ld\n", newvol
);
362 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
363 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
365 if (fgets(buf
, BUFSIZ
, terminal
) == NULL
)
367 if (!strcmp(buf
, "none\n")) {
371 if (buf
[0] != '\n') {
372 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", blksread
);
393 fprintf(stderr
, "tape is not dump tape\n");
397 if (tmpbuf
.c_volume
!= volno
) {
398 fprintf(stderr
, "Wrong volume (%ld)\n", (long)tmpbuf
.c_volume
);
402 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
403 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
404 ctime(&tmpbuf
.c_date
));
405 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
409 tapesread
|= 1 << (volno
- 1);
412 * If continuing from the previous volume, skip over any
413 * blocks read already at the end of the previous volume.
415 * If coming to this volume at random, skip to the beginning
416 * of the next record.
418 dprintf(stdout
, "last rec %ld, tape starts with %ld\n", prevtapea
,
419 (long)tmpbuf
.c_tapea
);
420 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
421 if (curfile
.action
!= USING
) {
423 * XXX Dump incorrectly sets c_count to 1 in the
424 * volume header of the first tape, so ignore
425 * c_count when volno == 1.
428 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
430 } else if (tmpbuf
.c_tapea
<= prevtapea
) {
432 * Normally the value of c_tapea in the volume
433 * header is the record number of the header itself.
434 * However in the volume header following an EOT-
435 * terminated tape, it is the record number of the
436 * first continuation data block (dump bug?).
438 * The next record we want is `prevtapea + 1'.
440 i
= prevtapea
+ 1 - tmpbuf
.c_tapea
;
441 dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
442 i
, i
> 1 ? "s" : "");
447 if (curfile
.action
== USING
) {
449 panic("active file into volume 1\n");
461 * Handle unexpected EOF.
467 if (gettingfile
&& curfile
.action
== USING
) {
468 printf("Warning: %s %s\n",
469 "End-of-input encountered while extracting", curfile
.name
);
471 curfile
.name
= "<name unknown>";
472 curfile
.action
= UNKNOWN
;
474 curfile
.ino
= maxino
;
482 * handle multiple dumps per tape by skipping forward to the
490 if (dumpnum
== 1 || volno
!= 1)
493 fprintf(stderr
, "Cannot have multiple dumps on pipe input\n");
497 tcom
.mt_count
= dumpnum
- 1;
500 rmtioctl(MTFSF
, dumpnum
- 1);
503 if (ioctl(mt
, MTIOCTOP
, (char *)&tcom
) < 0)
504 fprintf(stderr
, "ioctl MTFSF: %s\n", strerror(errno
));
510 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
511 fprintf(stdout
, "Dumped from: %s",
512 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
513 if (spcl
.c_host
[0] == '\0')
515 fprintf(stderr
, "Level %ld dump of %s on %s:%s\n",
516 (long)spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
517 fprintf(stderr
, "Label: %s\n", spcl
.c_label
);
521 extractfile(char *name
)
527 struct timeval timep
[2];
531 curfile
.action
= USING
;
532 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
533 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
534 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
535 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
536 uid
= curfile
.dip
->di_uid
;
537 gid
= curfile
.dip
->di_gid
;
538 mode
= curfile
.dip
->di_mode
;
539 flags
= curfile
.dip
->di_flags
;
540 switch (mode
& IFMT
) {
543 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
548 vprintf(stdout
, "skipped socket %s\n", name
);
554 ep
= lookupname(name
);
555 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
556 panic("unextracted directory %s\n", name
);
560 vprintf(stdout
, "extract file %s\n", name
);
561 return (genliteraldir(name
, curfile
.ino
));
566 getfile(xtrlnkfile
, xtrlnkskip
);
569 "%s: zero length symbolic link (ignored)\n", name
);
572 if (linkit(lnkbuf
, name
, SYMLINK
) == GOOD
) {
573 lchown(name
, uid
, gid
);
575 lutimes(name
, timep
);
581 vprintf(stdout
, "extract fifo %s\n", name
);
588 if (mkfifo(name
, mode
) < 0) {
589 fprintf(stderr
, "%s: cannot create fifo: %s\n",
590 name
, strerror(errno
));
594 chown(name
, uid
, gid
);
597 chflags(name
, flags
);
603 vprintf(stdout
, "extract special file %s\n", name
);
610 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
611 fprintf(stderr
, "%s: cannot create special file: %s\n",
612 name
, strerror(errno
));
616 chown(name
, uid
, gid
);
619 chflags(name
, flags
);
624 vprintf(stdout
, "extract file %s\n", name
);
631 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
633 fprintf(stderr
, "%s: cannot create file: %s\n",
634 name
, strerror(errno
));
638 fchown(ofile
, uid
, gid
);
640 getfile(xtrfile
, xtrskip
);
643 chflags(name
, flags
);
650 * skip over bit maps on the tape
656 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
661 * skip over a file on the tape
667 curfile
.action
= SKIP
;
668 getfile(xtrnull
, xtrnull
);
672 * Extract a file from the tape.
673 * When an allocated block is found it is passed to the fill function;
674 * when an unallocated block (hole) is found, a zeroed buffer is passed
675 * to the skip function.
678 getfile(void (*fill
) (char *, long), void (*skip
) (char *, long))
682 quad_t size
= spcl
.c_dinode
.di_size
;
683 static char clearedbuf
[MAXBSIZE
];
684 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
687 if (spcl
.c_type
== TS_END
)
688 panic("ran off end of tape\n");
689 if (spcl
.c_magic
!= NFS_MAGIC
)
690 panic("not at beginning of a file\n");
691 if (!gettingfile
&& setjmp(restart
) != 0)
695 for (i
= 0; i
< spcl
.c_count
; i
++) {
696 if (readmapflag
|| spcl
.c_addr
[i
]) {
697 readtape(&buf
[curblk
++][0]);
698 if (curblk
== fssize
/ TP_BSIZE
) {
699 (*fill
)((char *)buf
, (long)(size
> TP_BSIZE
?
700 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
705 (*fill
)((char *)buf
, (long)(size
> TP_BSIZE
?
707 (curblk
- 1) * TP_BSIZE
+ size
));
710 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
713 if ((size
-= TP_BSIZE
) <= 0) {
714 for (i
++; i
< spcl
.c_count
; i
++)
715 if (readmapflag
|| spcl
.c_addr
[i
])
720 if (gethead(&spcl
) == GOOD
&& size
> 0) {
721 if (spcl
.c_type
== TS_ADDR
)
724 "Missing address (header) block for %s at %ld blocks\n",
725 curfile
.name
, blksread
);
728 (*fill
)((char *)buf
, (long)((curblk
* TP_BSIZE
) + size
));
734 * Write out the next block of a file.
737 xtrfile(char *buf
, long size
)
742 if (write(ofile
, buf
, (int) size
) == -1) {
744 "write error extracting inode %d, name %s\nwrite: %s\n",
745 curfile
.ino
, curfile
.name
, strerror(errno
));
750 * Skip over a hole in a file.
754 xtrskip(char *buf
, long size
)
757 if (lseek(ofile
, size
, SEEK_CUR
) == -1) {
759 "seek error extracting inode %d, name %s\nlseek: %s\n",
760 curfile
.ino
, curfile
.name
, strerror(errno
));
766 * Collect the next block of a symbolic link.
769 xtrlnkfile(char *buf
, long size
)
773 if (pathlen
> MAXPATHLEN
) {
774 fprintf(stderr
, "symbolic link name: %s->%s%s; too long %d\n",
775 curfile
.name
, lnkbuf
, buf
, pathlen
);
782 * Skip over a hole in a symbolic link (should never happen).
786 xtrlnkskip(char *buf
, long size
)
789 fprintf(stderr
, "unallocated block in symbolic link %s\n",
795 * Collect the next block of a bit map.
798 xtrmap(char *buf
, long size
)
801 memmove(map
, buf
, size
);
806 * Skip over a hole in a bit map (should never happen).
810 xtrmapskip(char *buf
, long size
)
813 panic("hole in map\n");
818 * Noop, when an extraction function is not needed.
822 xtrnull(char *buf
, long size
)
829 * Read TP_BSIZE blocks from the input.
830 * Handle read errors, and end of media.
836 int cnt
, seek_failed
;
838 if (blkcnt
< numtrec
) {
839 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], (long)TP_BSIZE
);
844 for (i
= 0; i
< ntrec
; i
++)
845 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
848 cnt
= ntrec
* TP_BSIZE
;
853 i
= rmtread(&tapebuf
[rd
], cnt
);
856 i
= read(mt
, &tapebuf
[rd
], cnt
);
858 * Check for mid-tape short read error.
859 * If found, skip rest of buffer and start with the next.
861 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
862 dprintf(stdout
, "mid-media short read error.\n");
866 * Handle partial block read.
868 if (pipein
&& i
== 0 && rd
> 0)
870 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
879 * Short read. Process the blocks read.
881 if (i
% TP_BSIZE
!= 0)
883 "partial block read: %ld should be %ld\n",
884 i
, ntrec
* TP_BSIZE
);
885 numtrec
= i
/ TP_BSIZE
;
892 fprintf(stderr
, "Tape read error while ");
893 switch (curfile
.action
) {
895 fprintf(stderr
, "trying to set up tape\n");
898 fprintf(stderr
, "trying to resynchronize\n");
901 fprintf(stderr
, "restoring %s\n", curfile
.name
);
904 fprintf(stderr
, "skipping over inode %d\n",
908 if (!yflag
&& !reply("continue"))
910 i
= ntrec
* TP_BSIZE
;
911 memset(tapebuf
, 0, i
);
914 seek_failed
= (rmtseek(i
, 1) < 0);
917 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
921 "continuation failed: %s\n", strerror(errno
));
926 * Handle end of tape.
929 vprintf(stdout
, "End-of-tape encountered\n");
938 if (rd
% TP_BSIZE
!= 0)
939 panic("partial block read: %d should be %d\n",
940 rd
, ntrec
* TP_BSIZE
);
942 memmove(&tapebuf
[rd
], &endoftapemark
, (long)TP_BSIZE
);
945 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], (long)TP_BSIZE
);
951 findtapeblksize(void)
955 for (i
= 0; i
< ntrec
; i
++)
956 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
960 i
= rmtread(tapebuf
, ntrec
* TP_BSIZE
);
963 i
= read(mt
, tapebuf
, ntrec
* TP_BSIZE
);
966 fprintf(stderr
, "tape read error: %s\n", strerror(errno
));
969 if (i
% TP_BSIZE
!= 0) {
970 fprintf(stderr
, "Tape block size (%ld) %s (%d)\n",
971 i
, "is not a multiple of dump block size", TP_BSIZE
);
974 ntrec
= i
/ TP_BSIZE
;
976 vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
994 * Read the next block from the tape.
995 * Check to see if it is one of several vintage headers.
996 * If it is an old style header, convert it to a new style header.
997 * If it is not any valid header, return an error.
1000 gethead(struct s_spcl
*buf
)
1008 char dummy
[TP_BSIZE
];
1019 unsigned short odi_mode
;
1036 readtape((char *)buf
);
1037 if (buf
->c_magic
!= NFS_MAGIC
) {
1038 if (swabl(buf
->c_magic
) != NFS_MAGIC
)
1041 vprintf(stdout
, "Note: Doing Byte swapping\n");
1045 if (checksum((int *)buf
) == FAIL
)
1048 swabst((u_char
*)"8l4s31l", (u_char
*)buf
);
1049 swabst((u_char
*)"l",(u_char
*) &buf
->c_level
);
1050 swabst((u_char
*)"2l",(u_char
*) &buf
->c_flags
);
1054 readtape((char *)(&u_ospcl
.s_ospcl
));
1055 memset(buf
, 0, (long)TP_BSIZE
);
1056 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1057 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1058 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1059 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1060 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1061 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1062 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1063 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1064 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1065 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1066 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1067 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1068 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1069 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1070 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1071 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1072 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1073 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1074 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1075 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1076 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1078 buf
->c_magic
= NFS_MAGIC
;
1081 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1082 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1083 qcvt
.qval
= buf
->c_dinode
.di_size
;
1084 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1085 printf("Note: Doing Quad swapping\n");
1090 qcvt
.qval
= buf
->c_dinode
.di_size
;
1092 qcvt
.val
[1] = qcvt
.val
[0];
1094 buf
->c_dinode
.di_size
= qcvt
.qval
;
1098 switch (buf
->c_type
) {
1103 * Have to patch up missing information in bit map headers
1106 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1107 if (buf
->c_count
> TP_NINDIR
)
1110 for (i
= 0; i
< buf
->c_count
; i
++)
1115 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1127 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1131 * If we are restoring a filesystem with old format inodes,
1132 * copy the uid/gid to the new location.
1135 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1136 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1138 tapeaddr
= buf
->c_tapea
;
1145 * Check that a header is where it belongs and predict the next header
1148 accthdr(struct s_spcl
*header
)
1150 static ufs1_ino_t previno
= 0x7fffffff;
1151 static int prevtype
;
1152 static long predict
;
1155 if (header
->c_type
== TS_TAPE
) {
1156 fprintf(stderr
, "Volume header (%s inode format) ",
1157 oldinofmt
? "old" : "new");
1158 if (header
->c_firstrec
)
1159 fprintf(stderr
, "begins with record %ld",
1160 (long)header
->c_firstrec
);
1161 fprintf(stderr
, "\n");
1162 previno
= 0x7fffffff;
1165 if (previno
== 0x7fffffff)
1169 fprintf(stderr
, "Dumped inodes map header");
1172 fprintf(stderr
, "Used inodes map header");
1175 fprintf(stderr
, "File header, ino %d", previno
);
1178 fprintf(stderr
, "File continuation header, ino %d", previno
);
1181 fprintf(stderr
, "End of tape header");
1184 if (predict
!= blksread
- 1)
1185 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1186 predict
, blksread
- 1);
1187 fprintf(stderr
, "\n");
1190 if (header
->c_type
!= TS_END
)
1191 for (i
= 0; i
< header
->c_count
; i
++)
1192 if (readmapflag
|| header
->c_addr
[i
] != 0)
1196 prevtype
= header
->c_type
;
1197 previno
= header
->c_inumber
;
1201 * Find an inode header.
1202 * Complain if had to skip.
1205 findinode(struct s_spcl
*header
)
1207 static long skipcnt
= 0;
1212 curfile
.name
= "<name unknown>";
1213 curfile
.action
= UNKNOWN
;
1217 if (header
->c_magic
!= NFS_MAGIC
) {
1219 while (gethead(header
) == FAIL
||
1220 header
->c_date
!= dumpdate
)
1223 htype
= header
->c_type
;
1228 * Skip up to the beginning of the next record
1230 for (i
= 0; i
< header
->c_count
; i
++)
1231 if (header
->c_addr
[i
])
1233 while (gethead(header
) == FAIL
||
1234 header
->c_date
!= dumpdate
)
1239 curfile
.dip
= &header
->c_dinode
;
1240 curfile
.ino
= header
->c_inumber
;
1244 /* If we missed some tapes, get another volume. */
1245 if (tapesread
& (tapesread
+ 1)) {
1249 curfile
.ino
= maxino
;
1253 curfile
.name
= "<file removal list>";
1257 curfile
.name
= "<file dump list>";
1261 panic("unexpected tape header\n");
1265 panic("unknown tape header type %d\n", spcl
.c_type
);
1269 } while (htype
== TS_ADDR
);
1271 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1281 j
= sizeof(union u_spcl
) / sizeof(int);
1288 /* What happens if we want to read restore tapes
1289 for a 16bit int machine??? */
1295 if (i
!= CHECKSUM
) {
1296 fprintf(stderr
, "Checksum error %o, inode %d file %s\n", i
,
1297 curfile
.ino
, curfile
.name
);
1307 msg(const char *fmt
, ...)
1311 vfprintf(stderr
, fmt
, ap
);
1314 #endif /* RRESTORE */
1317 swabshort(u_char
*sp
, int n
)
1322 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1329 swablong(u_char
*sp
, int n
)
1334 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1335 c
= sp
[2]; sp
[2] = sp
[1]; sp
[1] = c
;
1342 swabst(u_char
*cp
, u_char
*sp
)
1348 case '0': case '1': case '2': case '3': case '4':
1349 case '5': case '6': case '7': case '8': case '9':
1350 n
= (n
* 10) + (*cp
++ - '0');
1353 case 's': case 'w': case 'h':
1356 sp
= swabshort(sp
, n
);
1362 sp
= swablong(sp
, n
);
1365 default: /* Any other character, like 'b' counts as byte. */
1379 swabst((u_char
*)"l", (u_char
*)&x
);