]>
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.43 2001/07/18 13:46:17 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, savecnt
= 0, wantnext
= 0, i
;
383 union u_spcl tmpspcl
;
384 # define tmpbuf tmpspcl.s_spcl
386 int haderror
= 0, bot_code
= 1;
394 panic("Changing volumes on pipe input?\n");
402 exit(1); /* pipes do not get a second chance */
403 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
410 while (newvol
<= 0) {
411 if (tapesread
== 0) {
412 fprintf(stderr
, "%s%s%s%s%s",
413 "You have not read any tapes yet.\n",
414 "Unless you know which volume your",
415 " file(s) are on you should start\n",
416 "with the last volume and work",
417 " towards the first.\n");
419 fprintf(stderr
, "You have read volumes");
421 for (i
= 1; i
< 32; i
++)
422 if (tapesread
& (1 << i
)) {
423 fprintf(stderr
, "%s%ld", buf
, (long)i
);
426 fprintf(stderr
, "\n");
429 fprintf(stderr
, "Specify next volume #: ");
430 (void) fflush(stderr
);
431 (void) fgets(buf
, TP_BSIZE
, terminal
);
432 } while (!feof(terminal
) && buf
[0] == '\n');
438 "Volume numbers are positive numerics\n");
441 if (newvol
== volno
) {
442 tapesread
|= 1 << volno
;
447 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
448 magtape
[MAXPATHLEN
- 1] = '\0';
450 if (bot_script
&& !haderror
) {
451 msg("Launching %s\n", bot_script
);
452 bot_code
= system_command(bot_script
, magtape
, newvol
);
453 if (bot_code
!= 0 && bot_code
!= 1) {
454 msg("Restore aborted by the beginning of tape script\n");
458 if (haderror
|| (bot_code
&& !Mflag
)) {
460 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
461 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
462 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
463 (void) fflush(stderr
);
464 (void) fgets(buf
, TP_BSIZE
, terminal
);
467 if (!strcmp(buf
, "none\n")) {
471 if (buf
[0] != '\n') {
473 (void) strncpy(magtape
, buf
, sizeof(magtape
));
474 magtape
[sizeof(magtape
) - 1] = '\0';
475 if ((pos
= strchr(magtape
, '\n')))
476 magtape
[pos
- magtape
] = '\0';
481 mt
= rmtopen(magtape
, 0);
484 mt
= OPEN(magtape
, O_RDONLY
, 0);
487 fprintf(stderr
, "Cannot open %s\n", magtape
);
497 if (gethead(&tmpbuf
) == FAIL
) {
498 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
499 fprintf(stderr
, "tape is not dump tape\n");
504 if (tmpbuf
.c_volume
!= volno
) {
505 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
510 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
511 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
512 ctime4(&tmpbuf
.c_date
));
513 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
518 tapesread
|= 1 << volno
;
521 * If continuing from the previous volume, skip over any
522 * blocks read already at the end of the previous volume.
524 * If coming to this volume at random, skip to the beginning
525 * of the next record.
527 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
528 tpblksread
, (long)tmpbuf
.c_firstrec
);
529 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
531 tpblksread
= tmpbuf
.c_firstrec
;
532 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
534 } else if (tmpbuf
.c_firstrec
> 0 &&
535 tmpbuf
.c_firstrec
< tpblksread
- 1) {
537 * -1 since we've read the volume header
539 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
540 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
541 (long)i
, i
> 1 ? "s" : "");
546 if (curfile
.action
== USING
) {
548 panic("active file into volume 1\n");
552 * Skip up to the beginning of the next record
554 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
555 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
557 (void) gethead(&spcl
);
561 siglongjmp(restart
, 1);
566 * Handle unexpected EOF.
572 if (gettingfile
&& curfile
.action
== USING
) {
573 printf("Warning: %s %s\n",
574 "End-of-input encountered while extracting", curfile
.name
);
576 curfile
.name
= "<name unknown>";
577 curfile
.action
= UNKNOWN
;
579 curfile
.ino
= maxino
;
582 siglongjmp(restart
, 1);
587 * handle multiple dumps per tape by skipping forward to the
595 if (dumpnum
== 1 || volno
!= 1)
598 errx(1, "Cannot have multiple dumps on pipe input");
600 tcom
.mt_count
= dumpnum
- 1;
603 rmtioctl(MTFSF
, dumpnum
- 1);
606 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
613 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
614 fprintf(stdout
, "Dumped from: %s",
615 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
616 if (spcl
.c_host
[0] == '\0')
618 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
619 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
620 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
624 extractfile(char *name
)
628 struct timeval timep
[2];
632 curfile
.action
= USING
;
634 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
635 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
636 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
637 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
638 #else /* __linux__ */
639 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
640 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
641 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
642 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
643 #endif /* __linux__ */
644 mode
= curfile
.dip
->di_mode
;
645 flags
= curfile
.dip
->di_flags
;
646 switch (mode
& IFMT
) {
649 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
654 Vprintf(stdout
, "skipped socket %s\n", name
);
660 ep
= lookupname(name
);
661 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
662 panic("unextracted directory %s\n", name
);
666 Vprintf(stdout
, "extract file %s\n", name
);
667 return (genliteraldir(name
, curfile
.ino
));
672 uid_t luid
= curfile
.dip
->di_uid
;
673 gid_t lgid
= curfile
.dip
->di_gid
;
677 getfile(xtrlnkfile
, xtrlnkskip
);
680 "%s: zero length symbolic link (ignored)\n", name
);
683 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
686 (void) lchown(name
, luid
, lgid
);
692 Vprintf(stdout
, "extract fifo %s\n", name
);
699 if (mkfifo(name
, mode
) < 0) {
700 warn("%s: cannot create fifo", name
);
704 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
705 (void) chmod(name
, mode
);
708 (void) fsetflags(name
, flags
);
710 (void) chflags(name
, flags
);
718 Vprintf(stdout
, "extract special file %s\n", name
);
725 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
726 warn("%s: cannot create special file", name
);
730 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
731 (void) chmod(name
, mode
);
735 warn("%s: fsetflags called on a special file", name
);
736 (void) fsetflags(name
, flags
);
739 (void) chflags(name
, flags
);
746 Vprintf(stdout
, "extract file %s\n", name
);
753 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
755 warn("%s: cannot create file", name
);
759 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
760 (void) fchmod(ofile
, mode
);
763 (void) setflags(ofile
, flags
);
765 (void) fchflags(ofile
, flags
);
767 getfile(xtrfile
, xtrskip
);
776 * skip over bit maps on the tape
782 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
787 * skip over a file on the tape
793 curfile
.action
= SKIP
;
794 getfile(xtrnull
, xtrnull
);
798 * Extract a file from the tape.
799 * When an allocated block is found it is passed to the fill function;
800 * when an unallocated block (hole) is found, a zeroed buffer is passed
801 * to the skip function.
804 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
807 volatile int curblk
= 0;
808 volatile quad_t size
= spcl
.c_dinode
.di_size
;
809 volatile int last_write_was_hole
= 0;
810 quad_t origsize
= size
;
811 static char clearedbuf
[MAXBSIZE
];
812 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
815 if (spcl
.c_type
== TS_END
)
816 panic("ran off end of tape\n");
817 if (spcl
.c_magic
!= NFS_MAGIC
)
818 panic("not at beginning of a file\n");
819 if (!gettingfile
&& setjmp(restart
) != 0)
823 for (i
= 0; i
< spcl
.c_count
; i
++) {
824 if (readmapflag
|| spcl
.c_addr
[i
]) {
825 readtape(&buf
[curblk
++][0]);
826 if (curblk
== fssize
/ TP_BSIZE
) {
827 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
828 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
830 last_write_was_hole
= 0;
834 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
836 (curblk
- 1) * TP_BSIZE
+ size
));
839 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
841 last_write_was_hole
= 1;
843 if ((size
-= TP_BSIZE
) <= 0) {
844 for (i
++; i
< spcl
.c_count
; i
++)
845 if (readmapflag
|| spcl
.c_addr
[i
])
850 if (gethead(&spcl
) == GOOD
&& size
> 0) {
851 if (spcl
.c_type
== TS_ADDR
)
854 "Missing address (header) block for %s at %ld blocks\n",
855 curfile
.name
, (long)blksread
);
858 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
859 last_write_was_hole
= 0;
862 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
864 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
865 (*skip
)(clearedbuf
, skp
);
868 last_write_was_hole
= 1;
870 if (last_write_was_hole
) {
871 FTRUNCATE(ofile
, origsize
);
878 * Write out the next block of a file.
881 xtrfile(char *buf
, size_t size
)
886 if (write(ofile
, buf
, (int) size
) == -1)
887 err(1, "write error extracting inode %lu, name %s\nwrite",
888 (unsigned long)curfile
.ino
, curfile
.name
);
892 * Skip over a hole in a file.
896 xtrskip(char *buf
, size_t size
)
899 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
900 err(1, "seek error extracting inode %lu, name %s\nlseek",
901 (unsigned long)curfile
.ino
, curfile
.name
);
905 * Collect the next block of a symbolic link.
908 xtrlnkfile(char *buf
, size_t size
)
912 if (pathlen
> MAXPATHLEN
)
913 errx(1, "symbolic link name: %s->%s%s; too long %d",
914 curfile
.name
, lnkbuf
, buf
, pathlen
);
915 (void) strcat(lnkbuf
, buf
);
919 * Skip over a hole in a symbolic link (should never happen).
923 xtrlnkskip(char *buf
, size_t size
)
926 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
930 * Collect the next block of a bit map.
933 xtrmap(char *buf
, size_t size
)
936 memmove(map
, buf
, size
);
941 * Skip over a hole in a bit map (should never happen).
945 xtrmapskip(char *buf
, size_t size
)
948 panic("hole in map\n");
953 * Noop, when an extraction function is not needed.
957 xtrnull(char *buf
, size_t size
)
965 * Compare the next block of a file.
968 xtrcmpfile(char *buf
, size_t size
)
970 static char cmpbuf
[MAXBSIZE
];
975 if (read(ifile
, cmpbuf
, size
) != size
) {
976 fprintf(stderr
, "%s: size has changed.\n",
982 if (memcmp(buf
, cmpbuf
, size
) != 0) {
983 fprintf(stderr
, "%s: tape and disk copies are different\n",
991 * Skip over a hole in a file.
994 xtrcmpskip(char *buf
, size_t size
)
996 static char cmpbuf
[MAXBSIZE
];
1002 if (read(ifile
, cmpbuf
, size
) != size
) {
1003 fprintf(stderr
, "%s: size has changed.\n",
1009 for (i
= 0; i
< size
; ++i
)
1010 if (cmpbuf
[i
] != '\0') {
1011 fprintf(stderr
, "%s: tape and disk copies are different\n",
1017 #endif /* COMPARE_ONTHEFLY */
1019 #if !COMPARE_ONTHEFLY
1021 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1023 static char buf_tape
[BUFSIZ
];
1024 static char buf_disk
[BUFSIZ
];
1029 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1030 close(fd_tape
), close(fd_disk
);
1031 panic("do_cmpfiles: unexpected EOF[1]");
1033 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1034 close(fd_tape
), close(fd_disk
);
1035 panic("do_cmpfiles: unexpected EOF[2]");
1037 if (n_tape
!= n_disk
) {
1038 close(fd_tape
), close(fd_disk
);
1039 panic("do_cmpfiles: sizes different!");
1041 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1047 /* for debugging compare problems */
1048 #undef COMPARE_FAIL_KEEP_FILE
1051 #ifdef COMPARE_FAIL_KEEP_FILE
1052 /* return true if tapefile should be unlinked after compare */
1057 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1059 struct STAT sbuf_tape
;
1060 int fd_tape
, fd_disk
;
1062 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1063 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1068 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1070 "%s: size changed from %ld to %ld.\n",
1071 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1073 #ifdef COMPARE_FAIL_KEEP_FILE
1080 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1081 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1084 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1086 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1090 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1091 fprintf(stderr
, "%s: tape and disk copies are different\n",
1096 #ifdef COMPARE_FAIL_KEEP_FILE
1097 /* rename the file to live in /tmp */
1098 /* rename `tapefile' to /tmp/<basename of diskfile> */
1100 char *p
= strrchr(diskfile
, '/');
1101 char newname
[MAXPATHLEN
];
1103 panic("can't find / in %s\n", diskfile
);
1105 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1106 if (rename(tapefile
, newname
)) {
1107 panic("rename from %s to %s failed: %s\n",
1111 fprintf(stderr
, "*** %s saved to %s\n",
1116 /* don't unlink the file (it's not there anymore */
1125 #ifdef COMPARE_FAIL_KEEP_FILE
1129 #endif /* !COMPARE_ONTHEFLY */
1131 #if !COMPARE_ONTHEFLY
1132 static char tmpfilename
[MAXPATHLEN
];
1136 comparefile(char *name
)
1141 #if !COMPARE_ONTHEFLY
1142 static char *tmpfile
= NULL
;
1146 if ((r
= LSTAT(name
, &sb
)) != 0) {
1147 warn("%s: does not exist (%d)", name
, r
);
1153 curfile
.name
= name
;
1154 curfile
.action
= USING
;
1155 mode
= curfile
.dip
->di_mode
;
1157 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1158 (long)sb
.st_size
, mode
);
1160 if (sb
.st_mode
!= mode
) {
1161 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1162 name
, mode
& 07777, sb
.st_mode
& 07777);
1165 switch (mode
& IFMT
) {
1179 char lbuf
[MAXPATHLEN
+ 1];
1182 if (!(sb
.st_mode
& S_IFLNK
)) {
1183 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1190 getfile(xtrlnkfile
, xtrlnkskip
);
1193 "%s: zero length symbolic link (ignored)\n",
1198 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1199 panic("readlink of %s failed: %s", name
,
1204 if (strcmp(lbuf
, lnkbuf
) != 0) {
1206 "%s: symbolic link changed from %s to %s.\n",
1207 name
, lnkbuf
, lbuf
);
1216 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1217 fprintf(stderr
, "%s: no longer a special file\n",
1224 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1226 "%s: device changed from %d,%d to %d,%d.\n",
1228 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1229 (int)curfile
.dip
->di_rdev
& 0xff,
1230 ((int)sb
.st_rdev
>> 8) & 0xff,
1231 (int)sb
.st_rdev
& 0xff);
1238 #if COMPARE_ONTHEFLY
1239 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1240 panic("Can't open %s: %s\n", name
, strerror(errno
));
1246 getfile(xtrcmpfile
, xtrcmpskip
);
1249 if (read(ifile
, &c
, 1) != 0) {
1250 fprintf(stderr
, "%s: size has changed.\n",
1260 if (tmpfile
== NULL
) {
1261 /* argument to mktemp() must not be in RO space: */
1262 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1263 tmpfile
= mktemp(&tmpfilename
[0]);
1265 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1266 panic("cannot delete tmp file %s: %s\n",
1267 tmpfile
, strerror(errno
));
1269 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1270 panic("cannot create file temp file %s: %s\n",
1271 name
, strerror(errno
));
1273 getfile(xtrfile
, xtrskip
);
1274 (void) close(ofile
);
1275 #ifdef COMPARE_FAIL_KEEP_FILE
1276 if (cmpfiles(tmpfile
, name
, &sb
))
1279 cmpfiles(tmpfile
, name
, &sb
);
1282 #endif /* COMPARE_ONTHEFLY */
1289 static void (*readtape_func
)(char *) = readtape_set
;
1292 * Read TP_BSIZE blocks from the input.
1293 * Handle read errors, and end of media.
1294 * Decompress compressed blocks.
1299 (*readtape_func
)(buf
); /* call the actual processing routine */
1303 * Set function pointer for readtape() routine. zflag and magtapein must
1304 * be correctly set before the first call to readtape().
1307 readtape_set(char *buf
)
1310 readtape_func
= readtape_uncompr
;
1314 readtape_func
= readtape_comprtape
;
1316 readtape_func
= readtape_comprfile
;
1321 #endif /* HAVE_ZLIB */
1324 * This is the original readtape(), it's used for reading uncompressed input.
1325 * Read TP_BSIZE blocks from the input.
1326 * Handle read errors, and end of media.
1330 readtape_uncompr(char *buf
)
1335 ssize_t rd
, newvol
, i
;
1336 int cnt
, seek_failed
;
1338 if (blkcnt
< numtrec
) {
1339 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1345 for (i
= 0; i
< ntrec
; i
++)
1346 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1349 cnt
= ntrec
* TP_BSIZE
;
1354 i
= rmtread(&tapebuf
[rd
], cnt
);
1357 i
= read(mt
, &tapebuf
[rd
], cnt
);
1360 * Check for mid-tape short read error.
1361 * If found, skip rest of buffer and start with the next.
1363 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1364 Dprintf(stdout
, "mid-media short read error.\n");
1368 * Handle partial block read.
1370 if (pipein
&& i
== 0 && rd
> 0)
1372 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1381 * Short read. Process the blocks read.
1383 if (i
% TP_BSIZE
!= 0)
1385 "partial block read: %ld should be %ld\n",
1386 (long)i
, ntrec
* TP_BSIZE
);
1387 numtrec
= i
/ TP_BSIZE
;
1391 * Handle read error.
1394 fprintf(stderr
, "Tape read error while ");
1395 switch (curfile
.action
) {
1397 fprintf(stderr
, "trying to set up tape\n");
1400 fprintf(stderr
, "trying to resynchronize\n");
1403 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1406 fprintf(stderr
, "skipping over inode %lu\n",
1407 (unsigned long)curfile
.ino
);
1410 if (!yflag
&& !reply("continue"))
1412 i
= ntrec
* TP_BSIZE
;
1413 memset(tapebuf
, 0, (size_t)i
);
1416 seek_failed
= (rmtseek(i
, 1) < 0);
1419 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1422 warn("continuation failed");
1423 if (!yflag
&& !reply("assume end-of-tape and continue"))
1429 * Handle end of tape.
1432 Vprintf(stdout
, "End-of-tape encountered\n");
1441 if (rd
% TP_BSIZE
!= 0)
1442 panic("partial block read: %d should be %d\n",
1443 rd
, ntrec
* TP_BSIZE
);
1445 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1448 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1456 * Read a compressed format block from a file or pipe and uncompress it.
1457 * Attempt to handle read errors, and end of file.
1460 readtape_comprfile(char *buf
)
1462 long rl
, size
, i
, ret
;
1464 struct tapebuf
*tpb
;
1466 if (blkcnt
< numtrec
) {
1467 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1472 /* need to read the next block */
1474 for (i
= 0; i
< ntrec
; i
++)
1475 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1477 tpb
= (struct tapebuf
*) tapebuf
;
1479 /* read the block prefix */
1480 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1486 if (size
> bufsize
) {
1487 /* something's wrong */
1488 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1491 tpb
->length
= bufsize
;
1493 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1497 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1498 if (tbufptr
== NULL
) {
1499 msg_read_error("File decompression error while");
1500 if (!yflag
&& !reply("continue"))
1502 memset(tapebuf
, 0, bufsize
);
1507 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1513 /* Errors while reading from a file or pipe are catastrophic. Since
1514 * there are no block boundaries, it's impossible to bypass the
1515 * block in error and find the start of the next block.
1518 /* It's possible to have multiple input files using -M
1519 * and -f file1,file2...
1521 Vprintf(stdout
, "End-of-File encountered\n");
1531 msg_read_error("Read error while");
1532 /* if (!yflag && !reply("continue")) */
1537 * Read compressed data from a tape and uncompress it.
1538 * Handle read errors, and end of media.
1539 * Since a tape consists of separate physical blocks, we try
1540 * to recover from errors by repositioning the tape to the next
1544 readtape_comprtape(char *buf
)
1548 struct tapebuf
*tpb
;
1551 if (blkcnt
< numtrec
) {
1552 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1557 /* need to read the next block */
1559 for (i
= 0; i
< ntrec
; i
++)
1560 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1562 tpb
= (struct tapebuf
*) tapebuf
;
1564 /* read the block */
1565 size
= bufsize
+ PREFIXSIZE
;
1566 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1570 tbufptr
= decompress_tapebuf(tpb
, rl
);
1571 if (tbufptr
== NULL
) {
1572 msg_read_error("Tape decompression error while");
1573 if (!yflag
&& !reply("continue"))
1575 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1581 /* Handle errors: EOT switches to the next volume, other errors
1582 * attempt to position the tape to the next block.
1585 Vprintf(stdout
, "End-of-tape encountered\n");
1594 msg_read_error("Tape read error while");
1595 if (!yflag
&& !reply("continue"))
1597 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1602 rl
= rmtioctl(MTFSR
, 1);
1608 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1612 warn("continuation failed");
1613 if (!yflag
&& !reply("assume end-of-tape and continue"))
1615 ret
= 0; /* end of tape */
1621 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1627 * Decompress a struct tapebuf into a buffer. readsize is the size read
1628 * from the tape/file and is used for error messages. Returns a pointer
1629 * to the location of the uncompressed buffer or NULL on errors.
1630 * Adjust numtrec and complain for a short block.
1633 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1635 /* If zflag is on, all blocks have a struct tapebuf prefix */
1636 /* zflag gets set in setup() from the dump header */
1637 int cresult
, blocklen
;
1638 unsigned long worklen
;
1639 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1641 /* build a length error message */
1642 blocklen
= tpbin
->length
;
1643 if (readsize
< blocklen
+ PREFIXSIZE
)
1644 lengtherr
= "short";
1646 if (readsize
> blocklen
+ PREFIXSIZE
)
1651 if (tpbin
->compressed
) {
1652 /* uncompress whatever we read, if it fails, complain later */
1653 cresult
= uncompress(comprbuf
, &worklen
, tpbin
->buf
, blocklen
);
1657 output
= tpbin
->buf
;
1662 numtrec
= worklen
/ TP_BSIZE
;
1663 if (worklen
% TP_BSIZE
!= 0)
1664 reason
= "length mismatch";
1667 reason
= "not enough memory";
1670 reason
= "buffer too small";
1673 reason
= "data error";
1680 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1681 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1682 fprintf(stderr
, "decompression error, block %ld: %s\n",
1683 tpblksread
+1, reason
);
1684 if (cresult
!= Z_OK
) output
= NULL
;
1690 * Print an error message for a read error.
1691 * This was exteracted from the original readtape().
1694 msg_read_error(char *m
)
1696 switch (curfile
.action
) {
1698 fprintf(stderr
, "%s trying to set up tape\n", m
);
1701 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1704 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1707 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1708 (unsigned long)curfile
.ino
);
1712 #endif /* HAVE_ZLIB */
1715 * Read the first block and get the blocksize from it. Test
1716 * for a compressed dump tape/file. setup() will make the final
1717 * determination by checking the compressed flag if gethead()
1718 * finds a valid header. The test here is necessary to offset the buffer
1719 * by the size of the compressed prefix. zflag is set here so that
1720 * readtape_set can set the correct function pointer for readtape().
1721 * Note that the first block of each tape/file is not compressed
1722 * and does not have a prefix.
1725 findtapeblksize(void)
1729 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1730 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1732 for (i
= 0; i
< ntrec
; i
++)
1733 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1737 * For a pipe or file, read in the first record. For a tape, read
1740 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1742 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1743 errx(1, "Tape read error on first record");
1746 * If the input is from a file or a pipe, we read TP_BSIZE
1747 * bytes looking for a dump header. If the dump is compressed
1748 * we need to read in the rest of the block, as determined
1749 * by c_ntrec in the dump header. The first block of the
1750 * dump is not compressed and does not have a prefix.
1753 if (spclpt
->c_type
== TS_TAPE
1754 && spclpt
->c_flags
& DR_COMPRESSED
) {
1755 /* It's a compressed dump file, read in the */
1756 /* rest of the block based on spclpt->c_ntrec. */
1757 if (spclpt
->c_ntrec
> ntrec
)
1758 errx(1, "Tape blocksize is too large, use "
1759 "\'-b %d\' ", spclpt
->c_ntrec
);
1760 ntrec
= spclpt
->c_ntrec
;
1761 len
= (ntrec
- 1) * TP_BSIZE
;
1765 /* read in the rest of the block based on bufsize */
1766 len
= bufsize
- TP_BSIZE
;
1768 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1769 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1770 errx(1,"Error reading dump file header");
1773 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1775 } /* if (!magtapein) */
1778 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1779 * If the value of ntrec is too large, we read less than
1780 * what we asked for; adjust the value of ntrec and test for
1781 * a compressed dump tape.
1784 if (i
% TP_BSIZE
!= 0) {
1785 /* may be old format compressed dump tape with a prefix */
1786 spclpt
= (struct s_spcl
*) tpb
->buf
;
1787 if (i
% TP_BSIZE
== PREFIXSIZE
1788 && tpb
->compressed
== 0
1789 && spclpt
->c_type
== TS_TAPE
1790 && spclpt
->c_flags
& DR_COMPRESSED
) {
1793 if (tpb
->length
> bufsize
)
1794 errx(1, "Tape blocksize is too large, use "
1795 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1798 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1801 ntrec
= i
/ TP_BSIZE
;
1802 if (spclpt
->c_type
== TS_TAPE
) {
1803 if (spclpt
->c_flags
& DR_COMPRESSED
)
1805 if (spclpt
->c_ntrec
> ntrec
)
1806 errx(1, "Tape blocksize is too large, use "
1807 "\'-b %d\' ", spclpt
->c_ntrec
);
1810 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1814 * Read a block of data handling all of the messy details.
1816 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1824 i
= rmtread(buf
, size
);
1827 i
= read(fd
, buf
, size
);
1830 break; /* EOD or error */
1833 break; /* block at a time for mt */
1836 *lengthread
= len
- size
;
1855 setmagtapein(void) {
1856 struct mtget mt_stat
;
1857 static int done
= 0;
1862 /* need to know if input is really from a tape */
1865 magtapein
= rmtioctl(MTNOP
, 1) != -1;
1868 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
1871 Vprintf(stdout
,"Input is from %s\n", magtapein
? "tape": "file/pipe");
1875 * Read the next block from the tape.
1876 * Check to see if it is one of several vintage headers.
1877 * If it is an old style header, convert it to a new style header.
1878 * If it is not any valid header, return an error.
1881 gethead(struct s_spcl
*buf
)
1889 char dummy
[TP_BSIZE
];
1896 u_int16_t c_inumber
;
1901 u_int16_t odi_nlink
;
1917 readtape((char *)buf
);
1918 if (buf
->c_magic
!= NFS_MAGIC
) {
1919 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1922 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1926 if (checksum((int *)buf
) == FAIL
)
1929 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1932 readtape((char *)(&u_ospcl
.s_ospcl
));
1933 memset((char *)buf
, 0, (long)TP_BSIZE
);
1934 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1935 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1936 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1937 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1938 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1939 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1940 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1941 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1942 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1943 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1944 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1945 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1946 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1947 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1949 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1950 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1951 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1952 #else /* __linux__ */
1953 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1954 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1955 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1956 #endif /* __linux__ */
1957 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1958 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1959 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1960 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1962 buf
->c_magic
= NFS_MAGIC
;
1965 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1966 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1967 qcvt
.qval
= buf
->c_dinode
.di_size
;
1968 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1969 printf("Note: Doing Quad swapping\n");
1974 qcvt
.qval
= buf
->c_dinode
.di_size
;
1976 qcvt
.val
[1] = qcvt
.val
[0];
1978 buf
->c_dinode
.di_size
= qcvt
.qval
;
1982 switch (buf
->c_type
) {
1987 * Have to patch up missing information in bit map headers
1990 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1991 if (buf
->c_count
> TP_NINDIR
)
1994 for (i
= 0; i
< buf
->c_count
; i
++)
1999 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2011 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2015 * If we are restoring a filesystem with old format inodes,
2016 * copy the uid/gid to the new location.
2019 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2020 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2028 * Check that a header is where it belongs and predict the next header
2031 accthdr(struct s_spcl
*header
)
2033 static dump_ino_t previno
= 0x7fffffff;
2034 static int prevtype
;
2035 static long predict
;
2038 if (header
->c_type
== TS_TAPE
) {
2039 fprintf(stderr
, "Volume header (%s inode format) ",
2040 oldinofmt
? "old" : "new");
2041 if (header
->c_firstrec
)
2042 fprintf(stderr
, "begins with record %d",
2043 header
->c_firstrec
);
2044 fprintf(stderr
, "\n");
2045 previno
= 0x7fffffff;
2048 if (previno
== 0x7fffffff)
2052 fprintf(stderr
, "Dumped inodes map header");
2055 fprintf(stderr
, "Used inodes map header");
2058 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2061 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2064 fprintf(stderr
, "End of tape header");
2067 if (predict
!= blksread
- 1)
2068 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2069 predict
, blksread
- 1);
2070 fprintf(stderr
, "\n");
2073 if (header
->c_type
!= TS_END
)
2074 for (i
= 0; i
< header
->c_count
; i
++)
2075 if (readmapflag
|| header
->c_addr
[i
] != 0)
2079 prevtype
= header
->c_type
;
2080 previno
= header
->c_inumber
;
2084 * Find an inode header.
2085 * Complain if had to skip, and complain is set.
2088 findinode(struct s_spcl
*header
)
2090 static long skipcnt
= 0;
2094 curfile
.name
= "<name unknown>";
2095 curfile
.action
= UNKNOWN
;
2099 if (header
->c_magic
!= NFS_MAGIC
) {
2101 while (gethead(header
) == FAIL
||
2102 header
->c_date
!= dumpdate
)
2105 switch (header
->c_type
) {
2109 * Skip up to the beginning of the next record
2111 for (i
= 0; i
< header
->c_count
; i
++)
2112 if (header
->c_addr
[i
])
2114 while (gethead(header
) == FAIL
||
2115 header
->c_date
!= dumpdate
)
2120 curfile
.dip
= &header
->c_dinode
;
2121 curfile
.ino
= header
->c_inumber
;
2125 curfile
.ino
= maxino
;
2129 curfile
.name
= "<file removal list>";
2133 curfile
.name
= "<file dump list>";
2137 panic("unexpected tape header\n");
2141 panic("unknown tape header type %d\n", spcl
.c_type
);
2145 } while (header
->c_type
== TS_ADDR
);
2150 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2160 j
= sizeof(union u_spcl
) / sizeof(int);
2167 /* What happens if we want to read restore tapes
2168 for a 16bit int machine??? */
2174 if (i
!= CHECKSUM
) {
2175 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2176 (unsigned long)curfile
.ino
, curfile
.name
);
2186 #include <varargs.h>
2191 msg(const char *fmt
, ...)
2204 (void)vfprintf(stderr
, fmt
, ap
);
2207 #endif /* RRESTORE */
2210 swab16(u_char
*sp
, int n
)
2215 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2222 swab32(u_char
*sp
, int n
)
2227 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2228 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2235 swab64(u_char
*sp
, int n
)
2240 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2241 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2242 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2243 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2250 swabst(u_char
*cp
, u_char
*sp
)
2256 case '0': case '1': case '2': case '3': case '4':
2257 case '5': case '6': case '7': case '8': case '9':
2258 n
= (n
* 10) + (*cp
++ - '0');
2261 case 's': case 'w': case 'h':
2279 default: /* Any other character, like 'b' counts as byte. */
2293 swabst((u_char
*)"i", (u_char
*)&x
);
2301 swabst((u_char
*)"l", (u_char
*)&x
);
2308 * get the current position of the tape
2311 GetTapePos(long *pos
)
2316 if (ioctl(mt
, MTIOCPOS
, pos
) == -1) {
2318 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %ld)\n",
2319 (unsigned long)getpid(), err
, *pos
);
2325 typedef struct mt_pos
{
2328 } MTPosRec
, *MTPosPtr
;
2331 * go to specified position on tape
2334 GotoTapePos(long pos
)
2341 if (ioctl(mt
, MTIOCTOP
, &buf
) == -1) {
2343 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %ld)\n",
2344 (unsigned long)getpid(), err
, pos
);
2351 * read next data from tape to re-sync
2354 ReReadFromTape(void)
2358 if (gethead(&spcl
) == FAIL
) {
2360 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2368 RequestVol(long tnum
)
2373 #endif /* USE_QFA */