]>
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.45 2001/07/20 09:01:46 stelian Exp $";
53 #include <compatlfs.h>
55 #include <compaterr.h>
63 #include <sys/param.h>
71 #ifdef HAVE_EXT2FS_EXT2_FS_H
72 #include <ext2fs/ext2_fs.h>
74 #include <linux/ext2_fs.h>
76 #include <ext2fs/ext2fs.h>
77 #include <bsdcompat.h>
79 #include <ufs/ufs/dinode.h>
80 #endif /* __linux__ */
81 #include <protocols/dumprestore.h>
85 #endif /* HAVE_ZLIB */
89 #include "pathnames.h"
94 static long fssize
= MAXBSIZE
;
96 static int pipein
= 0;
97 static int magtapein
= 0; /* input is from magtape */
98 static char magtape
[MAXPATHLEN
];
99 static char magtapeprefix
[MAXPATHLEN
];
102 static char *tapebuf
; /* input buffer for read */
103 static int bufsize
; /* buffer size without prefix */
104 static char *tbufptr
= NULL
; /* active tape buffer */
106 static char *comprbuf
; /* uncompress work buf */
107 static size_t comprlen
; /* size including prefix */
109 static union u_spcl endoftapemark
;
110 static long blksread
; /* blocks read since last header */
111 static long tpblksread
= 0; /* TP_BSIZE blocks read */
112 static long tapesread
;
113 static sigjmp_buf restart
;
114 static int gettingfile
= 0; /* restart has a valid frame */
115 static char *host
= NULL
;
119 static char lnkbuf
[MAXPATHLEN
+ 1];
122 int oldinofmt
; /* old inode format conversion required */
123 int Bcvt
; /* Swap Bytes (for CCI or sun) */
124 static int Qcvt
; /* Swap quads (for sun) */
126 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
128 static void accthdr
__P((struct s_spcl
*));
129 static int checksum
__P((int *));
130 static void findinode
__P((struct s_spcl
*));
131 static void findtapeblksize
__P((void));
132 static int gethead
__P((struct s_spcl
*));
133 static void readtape
__P((char *));
134 static void setdumpnum
__P((void));
135 static u_int swabi
__P((u_int
));
137 static u_long swabl
__P((u_long
));
139 static u_char
*swab64
__P((u_char
*, int));
140 static u_char
*swab32
__P((u_char
*, int));
141 static u_char
*swab16
__P((u_char
*, int));
142 static void terminateinput
__P((void));
143 static void xtrfile
__P((char *, size_t));
144 static void xtrlnkfile
__P((char *, size_t));
145 static void xtrlnkskip
__P((char *, size_t));
146 static void xtrmap
__P((char *, size_t));
147 static void xtrmapskip
__P((char *, size_t));
148 static void xtrskip
__P((char *, size_t));
149 static void setmagtapein
__P((void));
152 static void newcomprbuf
__P((int));
153 static void readtape_set
__P((char *));
154 static void readtape_uncompr
__P((char *));
155 static void readtape_comprfile
__P((char *));
156 static void readtape_comprtape
__P((char *));
157 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
158 static void msg_read_error
__P((char *));
160 static int read_a_block
__P((int, char *, size_t, long *));
161 #define PREFIXSIZE sizeof(struct tapebuf)
163 #define COMPARE_ONTHEFLY 1
166 static int ifile
; /* input file for compare */
167 static int cmperror
; /* compare error */
168 static void xtrcmpfile
__P((char *, size_t));
169 static void xtrcmpskip
__P((char *, size_t));
172 static int readmapflag
;
175 * Set up an input source. This is called from main.c before setup() is.
178 setinput(char *source
)
184 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
188 if (strchr(source
, ':')) {
190 source
= strchr(host
, ':');
192 if (rmthost(host
) == 0)
196 if (strcmp(source
, "-") == 0) {
198 * Since input is coming from a pipe we must establish
199 * our own connection to the terminal.
201 terminal
= fopen(_PATH_TTY
, "r");
202 if (terminal
== NULL
) {
203 warn("cannot open %s", _PATH_TTY
);
204 terminal
= fopen(_PATH_DEVNULL
, "r");
205 if (terminal
== NULL
)
206 err(1, "cannot open %s", _PATH_DEVNULL
);
210 setuid(getuid()); /* no longer need or want root privileges */
212 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
213 magtapeprefix
[MAXPATHLEN
-1] = '\0';
214 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
217 strncpy(magtape
, source
, MAXPATHLEN
);
218 magtape
[MAXPATHLEN
- 1] = '\0';
222 newtapebuf(long size
)
224 static int tapebufsize
= -1;
227 bufsize
= ntrec
* TP_BSIZE
;
228 if (size
<= tapebufsize
)
232 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
234 errx(1, "Cannot allocate space for tape buffer");
240 newcomprbuf(int size
)
242 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
243 if (buf_size
<= comprlen
)
246 if (comprbuf
!= NULL
)
248 comprbuf
= malloc(comprlen
);
249 if (comprbuf
== NULL
)
250 errx(1, "Cannot allocate space for decompress buffer");
252 #endif /* HAVE_ZLIB */
255 * Verify that the tape drive can be accessed and
256 * that it actually is a dump tape.
261 int i
, j
, *ip
, bot_code
;
264 Vprintf(stdout
, "Verify tape and initialize maps\n");
266 msg("Launching %s\n", bot_script
);
267 bot_code
= system_command(bot_script
, magtape
, 1);
268 if (bot_code
!= 0 && bot_code
!= 1) {
269 msg("Restore aborted by the beginning of tape script\n");
275 mt
= rmtopen(magtape
, 0);
281 mt
= OPEN(magtape
, O_RDONLY
, 0);
283 err(1, "%s", magtape
);
289 if (gethead(&spcl
) == FAIL
) {
290 blkcnt
--; /* push back this block */
294 if (gethead(&spcl
) == FAIL
)
295 errx(1, "Tape is not a dump tape");
296 fprintf(stderr
, "Converting to new file system format.\n");
300 fprintf(stderr
, "Dump tape is compressed.\n");
304 errx(1,"This restore version doesn't support decompression");
305 #endif /* HAVE_ZLIB */
308 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
309 endoftapemark
.s_spcl
.c_type
= TS_END
;
310 ip
= (int *)&endoftapemark
;
311 j
= sizeof(union u_spcl
) / sizeof(int);
316 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
318 if (vflag
|| command
== 't' || command
== 'C')
320 if (filesys
[0] == '\0') {
322 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
323 filesys
[NAMELEN
- 1] = '\0';
324 dirptr
= strstr(filesys
, " (dir");
328 dumptime
= spcl
.c_ddate
;
329 dumpdate
= spcl
.c_date
;
330 if (STAT(".", &stbuf
) < 0)
331 err(1, "cannot stat .");
332 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
334 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
335 fssize
= stbuf
.st_blksize
;
336 if (((fssize
- 1) & fssize
) != 0)
337 errx(1, "bad block size %ld", fssize
);
338 if (spcl
.c_volume
!= 1)
339 errx(1, "Tape is not volume 1 of the dump");
340 if (gethead(&spcl
) == FAIL
) {
341 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
342 panic("no header after volume mark!\n");
345 if (spcl
.c_type
!= TS_CLRI
)
346 errx(1, "Cannot find file removal list");
347 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
348 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
349 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
351 errx(1, "no memory for active inode map");
353 curfile
.action
= USING
;
354 getfile(xtrmap
, xtrmapskip
);
355 if (spcl
.c_type
!= TS_BITS
)
356 errx(1, "Cannot find file dump list");
357 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
358 if (map
== (char *)NULL
)
359 errx(1, "no memory for file dump list");
361 curfile
.action
= USING
;
362 getfile(xtrmap
, xtrmapskip
);
364 * If there may be whiteout entries on the tape, pretend that the
365 * whiteout inode exists, so that the whiteout entries can be
369 SETINO(WINO
, dumpmap
);
373 * Prompt user to load a new dump volume.
374 * "Nextvol" is the next suggested volume to use.
375 * This suggested volume is enforced when doing full
376 * or incremental restores, but can be overridden by
377 * the user when only extracting a subset of the files.
382 long newvol
= 0, wantnext
= 0, i
;
383 long saved_blksread
= 0, saved_tpblksread
= 0;
384 union u_spcl tmpspcl
;
385 # define tmpbuf tmpspcl.s_spcl
387 int haderror
= 0, bot_code
= 1;
395 panic("Changing volumes on pipe input?\n");
400 saved_blksread
= blksread
;
401 saved_tpblksread
= tpblksread
;
404 exit(1); /* pipes do not get a second chance */
405 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
412 while (newvol
<= 0) {
413 if (tapesread
== 0) {
414 fprintf(stderr
, "%s%s%s%s%s",
415 "You have not read any tapes yet.\n",
416 "Unless you know which volume your",
417 " file(s) are on you should start\n",
418 "with the last volume and work",
419 " towards the first.\n");
421 fprintf(stderr
, "You have read volumes");
423 for (i
= 1; i
< 32; i
++)
424 if (tapesread
& (1 << i
)) {
425 fprintf(stderr
, "%s%ld", buf
, (long)i
);
428 fprintf(stderr
, "\n");
431 fprintf(stderr
, "Specify next volume #: ");
432 (void) fflush(stderr
);
433 (void) fgets(buf
, TP_BSIZE
, terminal
);
434 } while (!feof(terminal
) && buf
[0] == '\n');
440 "Volume numbers are positive numerics\n");
443 if (newvol
== volno
) {
444 tapesread
|= 1 << volno
;
449 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
450 magtape
[MAXPATHLEN
- 1] = '\0';
452 if (bot_script
&& !haderror
) {
453 msg("Launching %s\n", bot_script
);
454 bot_code
= system_command(bot_script
, magtape
, newvol
);
455 if (bot_code
!= 0 && bot_code
!= 1) {
456 msg("Restore aborted by the beginning of tape script\n");
460 if (haderror
|| (bot_code
&& !Mflag
)) {
462 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
463 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
464 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
465 (void) fflush(stderr
);
466 (void) fgets(buf
, TP_BSIZE
, terminal
);
469 if (!strcmp(buf
, "none\n")) {
473 if (buf
[0] != '\n') {
475 (void) strncpy(magtape
, buf
, sizeof(magtape
));
476 magtape
[sizeof(magtape
) - 1] = '\0';
477 if ((pos
= strchr(magtape
, '\n')))
478 magtape
[pos
- magtape
] = '\0';
483 mt
= rmtopen(magtape
, 0);
486 mt
= OPEN(magtape
, O_RDONLY
, 0);
489 fprintf(stderr
, "Cannot open %s\n", magtape
);
500 if (gethead(&tmpbuf
) == FAIL
) {
501 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
502 fprintf(stderr
, "tape is not dump tape\n");
505 blksread
= saved_blksread
;
506 tpblksread
= saved_tpblksread
;
509 if (tmpbuf
.c_volume
!= volno
) {
510 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
513 blksread
= saved_blksread
;
514 tpblksread
= saved_tpblksread
;
517 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
518 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
519 ctime4(&tmpbuf
.c_date
));
520 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
523 blksread
= saved_blksread
;
524 tpblksread
= saved_tpblksread
;
527 tapesread
|= 1 << volno
;
528 blksread
= saved_blksread
;
529 tpblksread
= saved_tpblksread
;
531 * If continuing from the previous volume, skip over any
532 * blocks read already at the end of the previous volume.
534 * If coming to this volume at random, skip to the beginning
535 * of the next record.
537 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
538 tpblksread
, (long)tmpbuf
.c_firstrec
);
539 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
541 tpblksread
= tmpbuf
.c_firstrec
;
542 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
544 } else if (tmpbuf
.c_firstrec
> 0 &&
545 tmpbuf
.c_firstrec
< tpblksread
- 1) {
547 * -1 since we've read the volume header
549 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
550 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
551 (long)i
, i
> 1 ? "s" : "");
556 if (curfile
.action
== USING
) {
558 panic("active file into volume 1\n");
562 * Skip up to the beginning of the next record
564 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
565 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
567 (void) gethead(&spcl
);
571 siglongjmp(restart
, 1);
576 * Handle unexpected EOF.
582 if (gettingfile
&& curfile
.action
== USING
) {
583 printf("Warning: %s %s\n",
584 "End-of-input encountered while extracting", curfile
.name
);
586 curfile
.name
= "<name unknown>";
587 curfile
.action
= UNKNOWN
;
589 curfile
.ino
= maxino
;
592 siglongjmp(restart
, 1);
597 * handle multiple dumps per tape by skipping forward to the
605 if (dumpnum
== 1 || volno
!= 1)
608 errx(1, "Cannot have multiple dumps on pipe input");
610 tcom
.mt_count
= dumpnum
- 1;
613 rmtioctl(MTFSF
, dumpnum
- 1);
616 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
623 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
624 fprintf(stdout
, "Dumped from: %s",
625 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
626 if (spcl
.c_host
[0] == '\0')
628 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
629 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
630 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
634 extractfile(char *name
)
638 struct timeval timep
[2];
642 curfile
.action
= USING
;
644 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
645 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
646 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
647 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
648 #else /* __linux__ */
649 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
650 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
651 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
652 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
653 #endif /* __linux__ */
654 mode
= curfile
.dip
->di_mode
;
655 flags
= curfile
.dip
->di_flags
;
656 switch (mode
& IFMT
) {
659 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
664 Vprintf(stdout
, "skipped socket %s\n", name
);
670 ep
= lookupname(name
);
671 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
672 panic("unextracted directory %s\n", name
);
676 Vprintf(stdout
, "extract file %s\n", name
);
677 return (genliteraldir(name
, curfile
.ino
));
682 uid_t luid
= curfile
.dip
->di_uid
;
683 gid_t lgid
= curfile
.dip
->di_gid
;
687 getfile(xtrlnkfile
, xtrlnkskip
);
690 "%s: zero length symbolic link (ignored)\n", name
);
693 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
696 (void) lchown(name
, luid
, lgid
);
702 Vprintf(stdout
, "extract fifo %s\n", name
);
709 if (mkfifo(name
, mode
) < 0) {
710 warn("%s: cannot create fifo", name
);
714 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
715 (void) chmod(name
, mode
);
718 (void) fsetflags(name
, flags
);
720 (void) chflags(name
, flags
);
728 Vprintf(stdout
, "extract special file %s\n", name
);
735 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
736 warn("%s: cannot create special file", name
);
740 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
741 (void) chmod(name
, mode
);
745 warn("%s: fsetflags called on a special file", name
);
746 (void) fsetflags(name
, flags
);
749 (void) chflags(name
, flags
);
756 Vprintf(stdout
, "extract file %s\n", name
);
763 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
765 warn("%s: cannot create file", name
);
769 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
770 (void) fchmod(ofile
, mode
);
773 (void) setflags(ofile
, flags
);
775 (void) fchflags(ofile
, flags
);
777 getfile(xtrfile
, xtrskip
);
786 * skip over bit maps on the tape
792 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
797 * skip over a file on the tape
803 curfile
.action
= SKIP
;
804 getfile(xtrnull
, xtrnull
);
808 * Extract a file from the tape.
809 * When an allocated block is found it is passed to the fill function;
810 * when an unallocated block (hole) is found, a zeroed buffer is passed
811 * to the skip function.
814 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
817 volatile int curblk
= 0;
818 volatile quad_t size
= spcl
.c_dinode
.di_size
;
819 volatile int last_write_was_hole
= 0;
820 quad_t origsize
= size
;
821 static char clearedbuf
[MAXBSIZE
];
822 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
825 if (spcl
.c_type
== TS_END
)
826 panic("ran off end of tape\n");
827 if (spcl
.c_magic
!= NFS_MAGIC
)
828 panic("not at beginning of a file\n");
829 if (!gettingfile
&& setjmp(restart
) != 0)
833 for (i
= 0; i
< spcl
.c_count
; i
++) {
834 if (readmapflag
|| spcl
.c_addr
[i
]) {
835 readtape(&buf
[curblk
++][0]);
836 if (curblk
== fssize
/ TP_BSIZE
) {
837 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
838 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
840 last_write_was_hole
= 0;
844 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
846 (curblk
- 1) * TP_BSIZE
+ size
));
849 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
851 last_write_was_hole
= 1;
853 if ((size
-= TP_BSIZE
) <= 0) {
854 for (i
++; i
< spcl
.c_count
; i
++)
855 if (readmapflag
|| spcl
.c_addr
[i
])
860 if (gethead(&spcl
) == GOOD
&& size
> 0) {
861 if (spcl
.c_type
== TS_ADDR
)
864 "Missing address (header) block for %s at %ld blocks\n",
865 curfile
.name
, (long)blksread
);
868 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
869 last_write_was_hole
= 0;
872 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
874 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
875 (*skip
)(clearedbuf
, skp
);
878 last_write_was_hole
= 1;
880 if (last_write_was_hole
) {
881 FTRUNCATE(ofile
, origsize
);
888 * Write out the next block of a file.
891 xtrfile(char *buf
, size_t size
)
896 if (write(ofile
, buf
, (int) size
) == -1)
897 err(1, "write error extracting inode %lu, name %s\nwrite",
898 (unsigned long)curfile
.ino
, curfile
.name
);
902 * Skip over a hole in a file.
906 xtrskip(char *buf
, size_t size
)
909 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
910 err(1, "seek error extracting inode %lu, name %s\nlseek",
911 (unsigned long)curfile
.ino
, curfile
.name
);
915 * Collect the next block of a symbolic link.
918 xtrlnkfile(char *buf
, size_t size
)
922 if (pathlen
> MAXPATHLEN
)
923 errx(1, "symbolic link name: %s->%s%s; too long %d",
924 curfile
.name
, lnkbuf
, buf
, pathlen
);
925 (void) strcat(lnkbuf
, buf
);
929 * Skip over a hole in a symbolic link (should never happen).
933 xtrlnkskip(char *buf
, size_t size
)
936 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
940 * Collect the next block of a bit map.
943 xtrmap(char *buf
, size_t size
)
946 memmove(map
, buf
, size
);
951 * Skip over a hole in a bit map (should never happen).
955 xtrmapskip(char *buf
, size_t size
)
958 panic("hole in map\n");
963 * Noop, when an extraction function is not needed.
967 xtrnull(char *buf
, size_t size
)
975 * Compare the next block of a file.
978 xtrcmpfile(char *buf
, size_t size
)
980 static char cmpbuf
[MAXBSIZE
];
985 if (read(ifile
, cmpbuf
, size
) != size
) {
986 fprintf(stderr
, "%s: size has changed.\n",
992 if (memcmp(buf
, cmpbuf
, size
) != 0) {
993 fprintf(stderr
, "%s: tape and disk copies are different\n",
1001 * Skip over a hole in a file.
1004 xtrcmpskip(char *buf
, size_t size
)
1006 static char cmpbuf
[MAXBSIZE
];
1012 if (read(ifile
, cmpbuf
, size
) != size
) {
1013 fprintf(stderr
, "%s: size has changed.\n",
1019 for (i
= 0; i
< size
; ++i
)
1020 if (cmpbuf
[i
] != '\0') {
1021 fprintf(stderr
, "%s: tape and disk copies are different\n",
1027 #endif /* COMPARE_ONTHEFLY */
1029 #if !COMPARE_ONTHEFLY
1031 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1033 static char buf_tape
[BUFSIZ
];
1034 static char buf_disk
[BUFSIZ
];
1039 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1040 close(fd_tape
), close(fd_disk
);
1041 panic("do_cmpfiles: unexpected EOF[1]");
1043 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1044 close(fd_tape
), close(fd_disk
);
1045 panic("do_cmpfiles: unexpected EOF[2]");
1047 if (n_tape
!= n_disk
) {
1048 close(fd_tape
), close(fd_disk
);
1049 panic("do_cmpfiles: sizes different!");
1051 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1057 /* for debugging compare problems */
1058 #undef COMPARE_FAIL_KEEP_FILE
1061 #ifdef COMPARE_FAIL_KEEP_FILE
1062 /* return true if tapefile should be unlinked after compare */
1067 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1069 struct STAT sbuf_tape
;
1070 int fd_tape
, fd_disk
;
1072 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1073 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1078 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1080 "%s: size changed from %ld to %ld.\n",
1081 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1083 #ifdef COMPARE_FAIL_KEEP_FILE
1090 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1091 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1094 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1096 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1100 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1101 fprintf(stderr
, "%s: tape and disk copies are different\n",
1106 #ifdef COMPARE_FAIL_KEEP_FILE
1107 /* rename the file to live in /tmp */
1108 /* rename `tapefile' to /tmp/<basename of diskfile> */
1110 char *p
= strrchr(diskfile
, '/');
1111 char newname
[MAXPATHLEN
];
1113 panic("can't find / in %s\n", diskfile
);
1115 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1116 if (rename(tapefile
, newname
)) {
1117 panic("rename from %s to %s failed: %s\n",
1121 fprintf(stderr
, "*** %s saved to %s\n",
1126 /* don't unlink the file (it's not there anymore */
1135 #ifdef COMPARE_FAIL_KEEP_FILE
1139 #endif /* !COMPARE_ONTHEFLY */
1141 #if !COMPARE_ONTHEFLY
1142 static char tmpfilename
[MAXPATHLEN
];
1146 comparefile(char *name
)
1151 #if !COMPARE_ONTHEFLY
1152 static char *tmpfile
= NULL
;
1156 if ((r
= LSTAT(name
, &sb
)) != 0) {
1157 warn("%s: does not exist (%d)", name
, r
);
1163 curfile
.name
= name
;
1164 curfile
.action
= USING
;
1165 mode
= curfile
.dip
->di_mode
;
1167 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1168 (long)sb
.st_size
, mode
);
1170 if (sb
.st_mode
!= mode
) {
1171 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1172 name
, mode
& 07777, sb
.st_mode
& 07777);
1175 switch (mode
& IFMT
) {
1189 char lbuf
[MAXPATHLEN
+ 1];
1192 if (!(sb
.st_mode
& S_IFLNK
)) {
1193 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1200 getfile(xtrlnkfile
, xtrlnkskip
);
1203 "%s: zero length symbolic link (ignored)\n",
1208 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1209 panic("readlink of %s failed: %s", name
,
1214 if (strcmp(lbuf
, lnkbuf
) != 0) {
1216 "%s: symbolic link changed from %s to %s.\n",
1217 name
, lnkbuf
, lbuf
);
1226 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1227 fprintf(stderr
, "%s: no longer a special file\n",
1234 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1236 "%s: device changed from %d,%d to %d,%d.\n",
1238 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1239 (int)curfile
.dip
->di_rdev
& 0xff,
1240 ((int)sb
.st_rdev
>> 8) & 0xff,
1241 (int)sb
.st_rdev
& 0xff);
1248 #if COMPARE_ONTHEFLY
1249 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1250 panic("Can't open %s: %s\n", name
, strerror(errno
));
1256 getfile(xtrcmpfile
, xtrcmpskip
);
1259 if (read(ifile
, &c
, 1) != 0) {
1260 fprintf(stderr
, "%s: size has changed.\n",
1270 if (tmpfile
== NULL
) {
1271 /* argument to mktemp() must not be in RO space: */
1272 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1273 tmpfile
= mktemp(&tmpfilename
[0]);
1275 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1276 panic("cannot delete tmp file %s: %s\n",
1277 tmpfile
, strerror(errno
));
1279 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1280 panic("cannot create file temp file %s: %s\n",
1281 name
, strerror(errno
));
1283 getfile(xtrfile
, xtrskip
);
1284 (void) close(ofile
);
1285 #ifdef COMPARE_FAIL_KEEP_FILE
1286 if (cmpfiles(tmpfile
, name
, &sb
))
1289 cmpfiles(tmpfile
, name
, &sb
);
1292 #endif /* COMPARE_ONTHEFLY */
1299 static void (*readtape_func
)(char *) = readtape_set
;
1302 * Read TP_BSIZE blocks from the input.
1303 * Handle read errors, and end of media.
1304 * Decompress compressed blocks.
1309 (*readtape_func
)(buf
); /* call the actual processing routine */
1313 * Set function pointer for readtape() routine. zflag and magtapein must
1314 * be correctly set before the first call to readtape().
1317 readtape_set(char *buf
)
1320 readtape_func
= readtape_uncompr
;
1324 readtape_func
= readtape_comprtape
;
1326 readtape_func
= readtape_comprfile
;
1331 #endif /* HAVE_ZLIB */
1334 * This is the original readtape(), it's used for reading uncompressed input.
1335 * Read TP_BSIZE blocks from the input.
1336 * Handle read errors, and end of media.
1340 readtape_uncompr(char *buf
)
1345 ssize_t rd
, newvol
, i
;
1346 int cnt
, seek_failed
;
1348 if (blkcnt
< numtrec
) {
1349 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1355 for (i
= 0; i
< ntrec
; i
++)
1356 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1359 cnt
= ntrec
* TP_BSIZE
;
1364 i
= rmtread(&tapebuf
[rd
], cnt
);
1367 i
= read(mt
, &tapebuf
[rd
], cnt
);
1370 * Check for mid-tape short read error.
1371 * If found, skip rest of buffer and start with the next.
1373 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1374 Dprintf(stdout
, "mid-media short read error.\n");
1378 * Handle partial block read.
1380 if (pipein
&& i
== 0 && rd
> 0)
1382 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1391 * Short read. Process the blocks read.
1393 if (i
% TP_BSIZE
!= 0)
1395 "partial block read: %ld should be %ld\n",
1396 (long)i
, ntrec
* TP_BSIZE
);
1397 numtrec
= i
/ TP_BSIZE
;
1401 * Handle read error.
1404 fprintf(stderr
, "Tape read error while ");
1405 switch (curfile
.action
) {
1407 fprintf(stderr
, "trying to set up tape\n");
1410 fprintf(stderr
, "trying to resynchronize\n");
1413 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1416 fprintf(stderr
, "skipping over inode %lu\n",
1417 (unsigned long)curfile
.ino
);
1420 if (!yflag
&& !reply("continue"))
1422 i
= ntrec
* TP_BSIZE
;
1423 memset(tapebuf
, 0, (size_t)i
);
1426 seek_failed
= (rmtseek(i
, 1) < 0);
1429 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1432 warn("continuation failed");
1433 if (!yflag
&& !reply("assume end-of-tape and continue"))
1439 * Handle end of tape.
1442 Vprintf(stdout
, "End-of-tape encountered\n");
1451 if (rd
% TP_BSIZE
!= 0)
1452 panic("partial block read: %d should be %d\n",
1453 rd
, ntrec
* TP_BSIZE
);
1455 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1458 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1466 * Read a compressed format block from a file or pipe and uncompress it.
1467 * Attempt to handle read errors, and end of file.
1470 readtape_comprfile(char *buf
)
1472 long rl
, size
, i
, ret
;
1474 struct tapebuf
*tpb
;
1476 if (blkcnt
< numtrec
) {
1477 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1482 /* need to read the next block */
1484 for (i
= 0; i
< ntrec
; i
++)
1485 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1487 tpb
= (struct tapebuf
*) tapebuf
;
1489 /* read the block prefix */
1490 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1496 if (size
> bufsize
) {
1497 /* something's wrong */
1498 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1501 tpb
->length
= bufsize
;
1503 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1507 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1508 if (tbufptr
== NULL
) {
1509 msg_read_error("File decompression error while");
1510 if (!yflag
&& !reply("continue"))
1512 memset(tapebuf
, 0, bufsize
);
1517 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1523 /* Errors while reading from a file or pipe are catastrophic. Since
1524 * there are no block boundaries, it's impossible to bypass the
1525 * block in error and find the start of the next block.
1528 /* It's possible to have multiple input files using -M
1529 * and -f file1,file2...
1531 Vprintf(stdout
, "End-of-File encountered\n");
1541 msg_read_error("Read error while");
1542 /* if (!yflag && !reply("continue")) */
1547 * Read compressed data from a tape and uncompress it.
1548 * Handle read errors, and end of media.
1549 * Since a tape consists of separate physical blocks, we try
1550 * to recover from errors by repositioning the tape to the next
1554 readtape_comprtape(char *buf
)
1558 struct tapebuf
*tpb
;
1561 if (blkcnt
< numtrec
) {
1562 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1567 /* need to read the next block */
1569 for (i
= 0; i
< ntrec
; i
++)
1570 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1572 tpb
= (struct tapebuf
*) tapebuf
;
1574 /* read the block */
1575 size
= bufsize
+ PREFIXSIZE
;
1576 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1580 tbufptr
= decompress_tapebuf(tpb
, rl
);
1581 if (tbufptr
== NULL
) {
1582 msg_read_error("Tape decompression error while");
1583 if (!yflag
&& !reply("continue"))
1585 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1591 /* Handle errors: EOT switches to the next volume, other errors
1592 * attempt to position the tape to the next block.
1595 Vprintf(stdout
, "End-of-tape encountered\n");
1604 msg_read_error("Tape read error while");
1605 if (!yflag
&& !reply("continue"))
1607 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1612 rl
= rmtioctl(MTFSR
, 1);
1618 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1622 warn("continuation failed");
1623 if (!yflag
&& !reply("assume end-of-tape and continue"))
1625 ret
= 0; /* end of tape */
1631 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1637 * Decompress a struct tapebuf into a buffer. readsize is the size read
1638 * from the tape/file and is used for error messages. Returns a pointer
1639 * to the location of the uncompressed buffer or NULL on errors.
1640 * Adjust numtrec and complain for a short block.
1643 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1645 /* If zflag is on, all blocks have a struct tapebuf prefix */
1646 /* zflag gets set in setup() from the dump header */
1647 int cresult
, blocklen
;
1648 unsigned long worklen
;
1649 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1651 /* build a length error message */
1652 blocklen
= tpbin
->length
;
1653 if (readsize
< blocklen
+ PREFIXSIZE
)
1654 lengtherr
= "short";
1656 if (readsize
> blocklen
+ PREFIXSIZE
)
1661 if (tpbin
->compressed
) {
1662 /* uncompress whatever we read, if it fails, complain later */
1663 cresult
= uncompress(comprbuf
, &worklen
, tpbin
->buf
, blocklen
);
1667 output
= tpbin
->buf
;
1672 numtrec
= worklen
/ TP_BSIZE
;
1673 if (worklen
% TP_BSIZE
!= 0)
1674 reason
= "length mismatch";
1677 reason
= "not enough memory";
1680 reason
= "buffer too small";
1683 reason
= "data error";
1690 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1691 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1692 fprintf(stderr
, "decompression error, block %ld: %s\n",
1693 tpblksread
+1, reason
);
1694 if (cresult
!= Z_OK
) output
= NULL
;
1700 * Print an error message for a read error.
1701 * This was exteracted from the original readtape().
1704 msg_read_error(char *m
)
1706 switch (curfile
.action
) {
1708 fprintf(stderr
, "%s trying to set up tape\n", m
);
1711 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1714 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1717 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1718 (unsigned long)curfile
.ino
);
1722 #endif /* HAVE_ZLIB */
1725 * Read the first block and get the blocksize from it. Test
1726 * for a compressed dump tape/file. setup() will make the final
1727 * determination by checking the compressed flag if gethead()
1728 * finds a valid header. The test here is necessary to offset the buffer
1729 * by the size of the compressed prefix. zflag is set here so that
1730 * readtape_set can set the correct function pointer for readtape().
1731 * Note that the first block of each tape/file is not compressed
1732 * and does not have a prefix.
1735 findtapeblksize(void)
1739 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1740 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1742 for (i
= 0; i
< ntrec
; i
++)
1743 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1747 * For a pipe or file, read in the first record. For a tape, read
1750 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1752 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1753 errx(1, "Tape read error on first record");
1756 * If the input is from a file or a pipe, we read TP_BSIZE
1757 * bytes looking for a dump header. If the dump is compressed
1758 * we need to read in the rest of the block, as determined
1759 * by c_ntrec in the dump header. The first block of the
1760 * dump is not compressed and does not have a prefix.
1763 if (spclpt
->c_type
== TS_TAPE
1764 && spclpt
->c_flags
& DR_COMPRESSED
) {
1765 /* It's a compressed dump file, read in the */
1766 /* rest of the block based on spclpt->c_ntrec. */
1767 if (spclpt
->c_ntrec
> ntrec
)
1768 errx(1, "Tape blocksize is too large, use "
1769 "\'-b %d\' ", spclpt
->c_ntrec
);
1770 ntrec
= spclpt
->c_ntrec
;
1771 len
= (ntrec
- 1) * TP_BSIZE
;
1775 /* read in the rest of the block based on bufsize */
1776 len
= bufsize
- TP_BSIZE
;
1778 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1779 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1780 errx(1,"Error reading dump file header");
1783 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1785 } /* if (!magtapein) */
1788 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1789 * If the value of ntrec is too large, we read less than
1790 * what we asked for; adjust the value of ntrec and test for
1791 * a compressed dump tape.
1794 if (i
% TP_BSIZE
!= 0) {
1795 /* may be old format compressed dump tape with a prefix */
1796 spclpt
= (struct s_spcl
*) tpb
->buf
;
1797 if (i
% TP_BSIZE
== PREFIXSIZE
1798 && tpb
->compressed
== 0
1799 && spclpt
->c_type
== TS_TAPE
1800 && spclpt
->c_flags
& DR_COMPRESSED
) {
1803 if (tpb
->length
> bufsize
)
1804 errx(1, "Tape blocksize is too large, use "
1805 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1808 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1811 ntrec
= i
/ TP_BSIZE
;
1812 if (spclpt
->c_type
== TS_TAPE
) {
1813 if (spclpt
->c_flags
& DR_COMPRESSED
)
1815 if (spclpt
->c_ntrec
> ntrec
)
1816 errx(1, "Tape blocksize is too large, use "
1817 "\'-b %d\' ", spclpt
->c_ntrec
);
1820 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1824 * Read a block of data handling all of the messy details.
1826 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1834 i
= rmtread(buf
, size
);
1837 i
= read(fd
, buf
, size
);
1840 break; /* EOD or error */
1843 break; /* block at a time for mt */
1846 *lengthread
= len
- size
;
1865 setmagtapein(void) {
1866 struct mtget mt_stat
;
1867 static int done
= 0;
1872 /* need to know if input is really from a tape */
1875 magtapein
= rmtioctl(MTNOP
, 1) != -1;
1878 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
1881 Vprintf(stdout
,"Input is from %s\n", magtapein
? "tape": "file/pipe");
1885 * Read the next block from the tape.
1886 * Check to see if it is one of several vintage headers.
1887 * If it is an old style header, convert it to a new style header.
1888 * If it is not any valid header, return an error.
1891 gethead(struct s_spcl
*buf
)
1899 char dummy
[TP_BSIZE
];
1906 u_int16_t c_inumber
;
1911 u_int16_t odi_nlink
;
1927 readtape((char *)buf
);
1928 if (buf
->c_magic
!= NFS_MAGIC
) {
1929 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1932 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1936 if (checksum((int *)buf
) == FAIL
)
1939 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1942 readtape((char *)(&u_ospcl
.s_ospcl
));
1943 memset((char *)buf
, 0, (long)TP_BSIZE
);
1944 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1945 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1946 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1947 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1948 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1949 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1950 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1951 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1952 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1953 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1954 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1955 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1956 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1957 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1959 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1960 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1961 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1962 #else /* __linux__ */
1963 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1964 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1965 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1966 #endif /* __linux__ */
1967 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1968 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1969 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1970 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1972 buf
->c_magic
= NFS_MAGIC
;
1975 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1976 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1977 qcvt
.qval
= buf
->c_dinode
.di_size
;
1978 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1979 printf("Note: Doing Quad swapping\n");
1984 qcvt
.qval
= buf
->c_dinode
.di_size
;
1986 qcvt
.val
[1] = qcvt
.val
[0];
1988 buf
->c_dinode
.di_size
= qcvt
.qval
;
1992 switch (buf
->c_type
) {
1997 * Have to patch up missing information in bit map headers
2000 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2001 if (buf
->c_count
> TP_NINDIR
)
2004 for (i
= 0; i
< buf
->c_count
; i
++)
2009 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2021 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2025 * If we are restoring a filesystem with old format inodes,
2026 * copy the uid/gid to the new location.
2029 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2030 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2038 * Check that a header is where it belongs and predict the next header
2041 accthdr(struct s_spcl
*header
)
2043 static dump_ino_t previno
= 0x7fffffff;
2044 static int prevtype
;
2045 static long predict
;
2048 if (header
->c_type
== TS_TAPE
) {
2049 fprintf(stderr
, "Volume header (%s inode format) ",
2050 oldinofmt
? "old" : "new");
2051 if (header
->c_firstrec
)
2052 fprintf(stderr
, "begins with record %d",
2053 header
->c_firstrec
);
2054 fprintf(stderr
, "\n");
2055 previno
= 0x7fffffff;
2058 if (previno
== 0x7fffffff)
2062 fprintf(stderr
, "Dumped inodes map header");
2065 fprintf(stderr
, "Used inodes map header");
2068 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2071 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2074 fprintf(stderr
, "End of tape header");
2077 if (predict
!= blksread
- 1)
2078 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2079 predict
, blksread
- 1);
2080 fprintf(stderr
, "\n");
2083 if (header
->c_type
!= TS_END
)
2084 for (i
= 0; i
< header
->c_count
; i
++)
2085 if (readmapflag
|| header
->c_addr
[i
] != 0)
2089 prevtype
= header
->c_type
;
2090 previno
= header
->c_inumber
;
2094 * Find an inode header.
2095 * Complain if had to skip, and complain is set.
2098 findinode(struct s_spcl
*header
)
2100 static long skipcnt
= 0;
2104 curfile
.name
= "<name unknown>";
2105 curfile
.action
= UNKNOWN
;
2109 if (header
->c_magic
!= NFS_MAGIC
) {
2111 while (gethead(header
) == FAIL
||
2112 header
->c_date
!= dumpdate
)
2115 switch (header
->c_type
) {
2119 * Skip up to the beginning of the next record
2121 for (i
= 0; i
< header
->c_count
; i
++)
2122 if (header
->c_addr
[i
])
2124 while (gethead(header
) == FAIL
||
2125 header
->c_date
!= dumpdate
)
2130 curfile
.dip
= &header
->c_dinode
;
2131 curfile
.ino
= header
->c_inumber
;
2135 curfile
.ino
= maxino
;
2139 curfile
.name
= "<file removal list>";
2143 curfile
.name
= "<file dump list>";
2147 panic("unexpected tape header\n");
2151 panic("unknown tape header type %d\n", spcl
.c_type
);
2155 } while (header
->c_type
== TS_ADDR
);
2160 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2170 j
= sizeof(union u_spcl
) / sizeof(int);
2177 /* What happens if we want to read restore tapes
2178 for a 16bit int machine??? */
2184 if (i
!= CHECKSUM
) {
2185 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2186 (unsigned long)curfile
.ino
, curfile
.name
);
2196 #include <varargs.h>
2201 msg(const char *fmt
, ...)
2214 (void)vfprintf(stderr
, fmt
, ap
);
2217 #endif /* RRESTORE */
2220 swab16(u_char
*sp
, int n
)
2225 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2232 swab32(u_char
*sp
, int n
)
2237 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2238 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2245 swab64(u_char
*sp
, int n
)
2250 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2251 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2252 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2253 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2260 swabst(u_char
*cp
, u_char
*sp
)
2266 case '0': case '1': case '2': case '3': case '4':
2267 case '5': case '6': case '7': case '8': case '9':
2268 n
= (n
* 10) + (*cp
++ - '0');
2271 case 's': case 'w': case 'h':
2289 default: /* Any other character, like 'b' counts as byte. */
2303 swabst((u_char
*)"i", (u_char
*)&x
);
2311 swabst((u_char
*)"l", (u_char
*)&x
);
2318 * get the current position of the tape
2321 GetTapePos(long *pos
)
2326 if (ioctl(mt
, MTIOCPOS
, pos
) == -1) {
2328 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %ld)\n",
2329 (unsigned long)getpid(), err
, *pos
);
2335 typedef struct mt_pos
{
2338 } MTPosRec
, *MTPosPtr
;
2341 * go to specified position on tape
2344 GotoTapePos(long pos
)
2351 if (ioctl(mt
, MTIOCTOP
, &buf
) == -1) {
2353 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %ld)\n",
2354 (unsigned long)getpid(), err
, pos
);
2361 * read next data from tape to re-sync
2364 ReReadFromTape(void)
2368 if (gethead(&spcl
) == FAIL
) {
2370 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2378 RequestVol(long tnum
)
2383 #endif /* USE_QFA */