]>
git.wh0rd.org - dump.git/blob - restore/tape.c
4b4f7362ccf6a97713c9edcfd7b8e315cf1f543a
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.50 2001/12/24 15:53:41 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 #endif /* HAVE_BZLIB */
93 #include "pathnames.h"
98 static long fssize
= MAXBSIZE
;
101 static int magtapein
= 0; /* input is from magtape */
102 static char magtape
[MAXPATHLEN
];
103 static char magtapeprefix
[MAXPATHLEN
];
106 static char *tapebuf
; /* input buffer for read */
107 static int bufsize
; /* buffer size without prefix */
108 static char *tbufptr
= NULL
; /* active tape buffer */
109 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
110 static char *comprbuf
; /* uncompress work buf */
111 static size_t comprlen
; /* size including prefix */
113 static union u_spcl endoftapemark
;
114 static long blksread
; /* blocks read since last header */
115 static long tpblksread
= 0; /* TP_BSIZE blocks read */
116 static long tapesread
;
117 static sigjmp_buf restart
;
118 static int gettingfile
= 0; /* restart has a valid frame */
119 static char *host
= NULL
;
123 static char lnkbuf
[MAXPATHLEN
+ 1];
126 int oldinofmt
; /* old inode format conversion required */
127 int Bcvt
; /* Swap Bytes (for CCI or sun) */
128 static int Qcvt
; /* Swap quads (for sun) */
130 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
132 static void accthdr
__P((struct s_spcl
*));
133 static int checksum
__P((int *));
134 static void findinode
__P((struct s_spcl
*));
135 static void findtapeblksize
__P((void));
136 static int gethead
__P((struct s_spcl
*));
137 static void readtape
__P((char *));
138 static void setdumpnum
__P((void));
139 static u_int swabi
__P((u_int
));
141 static u_long swabl
__P((u_long
));
143 static u_char
*swab64
__P((u_char
*, int));
144 static u_char
*swab32
__P((u_char
*, int));
145 static u_char
*swab16
__P((u_char
*, int));
146 static void terminateinput
__P((void));
147 static void xtrfile
__P((char *, size_t));
148 static void xtrlnkfile
__P((char *, size_t));
149 static void xtrlnkskip
__P((char *, size_t));
150 static void xtrmap
__P((char *, size_t));
151 static void xtrmapskip
__P((char *, size_t));
152 static void xtrskip
__P((char *, size_t));
153 static void setmagtapein
__P((void));
155 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
156 static void newcomprbuf
__P((int));
157 static void readtape_set
__P((char *));
158 static void readtape_uncompr
__P((char *));
159 static void readtape_comprfile
__P((char *));
160 static void readtape_comprtape
__P((char *));
161 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
162 static void msg_read_error
__P((char *));
164 static int read_a_block
__P((int, char *, size_t, long *));
165 #define PREFIXSIZE sizeof(struct tapebuf)
167 #define COMPARE_ONTHEFLY 1
170 static int ifile
; /* input file for compare */
171 static int cmperror
; /* compare error */
172 static void xtrcmpfile
__P((char *, size_t));
173 static void xtrcmpskip
__P((char *, size_t));
176 static int readmapflag
;
179 * Set up an input source. This is called from main.c before setup() is.
182 setinput(char *source
)
188 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
192 if (strchr(source
, ':')) {
194 source
= strchr(host
, ':');
196 if (rmthost(host
) == 0)
200 if (strcmp(source
, "-") == 0) {
202 * Since input is coming from a pipe we must establish
203 * our own connection to the terminal.
205 terminal
= fopen(_PATH_TTY
, "r");
206 if (terminal
== NULL
) {
207 warn("cannot open %s", _PATH_TTY
);
208 terminal
= fopen(_PATH_DEVNULL
, "r");
209 if (terminal
== NULL
)
210 err(1, "cannot open %s", _PATH_DEVNULL
);
214 setuid(getuid()); /* no longer need or want root privileges */
216 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
217 magtapeprefix
[MAXPATHLEN
-1] = '\0';
218 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
221 strncpy(magtape
, source
, MAXPATHLEN
);
222 magtape
[MAXPATHLEN
- 1] = '\0';
226 newtapebuf(long size
)
228 static int tapebufsize
= -1;
231 bufsize
= ntrec
* TP_BSIZE
;
232 if (size
<= tapebufsize
)
236 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
238 errx(1, "Cannot allocate space for tape buffer");
242 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
244 newcomprbuf(int size
)
246 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
247 if (buf_size
<= comprlen
)
250 if (comprbuf
!= NULL
)
252 comprbuf
= malloc(comprlen
);
253 if (comprbuf
== NULL
)
254 errx(1, "Cannot allocate space for decompress buffer");
256 #endif /* HAVE_ZLIB || HAVE_BZLIB */
259 * Verify that the tape drive can be accessed and
260 * that it actually is a dump tape.
265 int i
, j
, *ip
, bot_code
;
268 Vprintf(stdout
, "Verify tape and initialize maps\n");
270 msg("Launching %s\n", bot_script
);
271 bot_code
= system_command(bot_script
, magtape
, 1);
272 if (bot_code
!= 0 && bot_code
!= 1) {
273 msg("Restore aborted by the beginning of tape script\n");
279 mt
= rmtopen(magtape
, 0);
285 mt
= OPEN(magtape
, O_RDONLY
, 0);
287 err(1, "%s", magtape
);
293 if (gethead(&spcl
) == FAIL
) {
294 blkcnt
--; /* push back this block */
298 if (gethead(&spcl
) == FAIL
)
299 errx(1, "Tape is not a dump tape");
300 fprintf(stderr
, "Converting to new file system format.\n");
304 fprintf(stderr
, "Dump tape is compressed.\n");
305 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
308 errx(1,"This restore version doesn't support decompression");
309 #endif /* HAVE_ZLIB */
312 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
313 endoftapemark
.s_spcl
.c_type
= TS_END
;
314 ip
= (int *)&endoftapemark
;
315 j
= sizeof(union u_spcl
) / sizeof(int);
320 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
322 if (vflag
|| command
== 't' || command
== 'C')
324 if (filesys
[0] == '\0') {
326 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
327 filesys
[NAMELEN
- 1] = '\0';
328 dirptr
= strstr(filesys
, " (dir");
332 dumptime
= spcl
.c_ddate
;
333 dumpdate
= spcl
.c_date
;
334 if (STAT(".", &stbuf
) < 0)
335 err(1, "cannot stat .");
336 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
338 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
339 fssize
= stbuf
.st_blksize
;
340 if (((fssize
- 1) & fssize
) != 0)
341 errx(1, "bad block size %ld", fssize
);
342 if (spcl
.c_volume
!= 1)
343 errx(1, "Tape is not volume 1 of the dump");
344 if (gethead(&spcl
) == FAIL
) {
345 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
346 panic("no header after volume mark!\n");
349 if (spcl
.c_type
!= TS_CLRI
)
350 errx(1, "Cannot find file removal list");
351 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
352 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
353 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
355 errx(1, "no memory for active inode map");
357 curfile
.action
= USING
;
358 getfile(xtrmap
, xtrmapskip
);
359 if (spcl
.c_type
!= TS_BITS
)
360 errx(1, "Cannot find file dump list");
361 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
362 if (map
== (char *)NULL
)
363 errx(1, "no memory for file dump list");
365 curfile
.action
= USING
;
366 getfile(xtrmap
, xtrmapskip
);
368 * If there may be whiteout entries on the tape, pretend that the
369 * whiteout inode exists, so that the whiteout entries can be
373 SETINO(WINO
, dumpmap
);
377 * Prompt user to load a new dump volume.
378 * "Nextvol" is the next suggested volume to use.
379 * This suggested volume is enforced when doing full
380 * or incremental restores, but can be overridden by
381 * the user when only extracting a subset of the files.
386 long newvol
= 0, wantnext
= 0, i
;
387 long saved_blksread
= 0, saved_tpblksread
= 0;
388 union u_spcl tmpspcl
;
389 # define tmpbuf tmpspcl.s_spcl
391 int haderror
= 0, bot_code
= 1;
399 panic("Changing volumes on pipe input?\n");
404 saved_blksread
= blksread
;
405 saved_tpblksread
= tpblksread
;
408 exit(1); /* pipes do not get a second chance */
409 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
416 while (newvol
<= 0) {
417 if (tapesread
== 0) {
418 fprintf(stderr
, "%s%s%s%s%s",
419 "You have not read any tapes yet.\n",
420 "Unless you know which volume your",
421 " file(s) are on you should start\n",
422 "with the last volume and work",
423 " towards the first.\n");
425 fprintf(stderr
, "You have read volumes");
427 for (i
= 1; i
< 32; i
++)
428 if (tapesread
& (1 << i
)) {
429 fprintf(stderr
, "%s%ld", buf
, (long)i
);
432 fprintf(stderr
, "\n");
435 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
436 (void) fflush(stderr
);
437 (void) fgets(buf
, TP_BSIZE
, terminal
);
438 } while (!feof(terminal
) && buf
[0] == '\n');
441 if (!strcmp(buf
, "none\n")) {
448 "Volume numbers are positive numerics\n");
451 if (newvol
== volno
) {
452 tapesread
|= 1 << volno
;
457 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
458 magtape
[MAXPATHLEN
- 1] = '\0';
460 if (bot_script
&& !haderror
) {
461 msg("Launching %s\n", bot_script
);
462 bot_code
= system_command(bot_script
, magtape
, newvol
);
463 if (bot_code
!= 0 && bot_code
!= 1) {
464 msg("Restore aborted by the beginning of tape script\n");
468 if (haderror
|| (bot_code
&& !Mflag
)) {
470 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
471 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
472 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
473 (void) fflush(stderr
);
474 (void) fgets(buf
, TP_BSIZE
, terminal
);
477 if (!strcmp(buf
, "none\n")) {
481 if (buf
[0] != '\n') {
483 (void) strncpy(magtape
, buf
, sizeof(magtape
));
484 magtape
[sizeof(magtape
) - 1] = '\0';
485 if ((pos
= strchr(magtape
, '\n')))
486 magtape
[pos
- magtape
] = '\0';
491 mt
= rmtopen(magtape
, 0);
494 mt
= OPEN(magtape
, O_RDONLY
, 0);
497 fprintf(stderr
, "Cannot open %s\n", magtape
);
508 if (gethead(&tmpbuf
) == FAIL
) {
509 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
510 fprintf(stderr
, "tape is not dump tape\n");
513 blksread
= saved_blksread
;
514 tpblksread
= saved_tpblksread
;
517 if (tmpbuf
.c_volume
!= volno
) {
518 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
521 blksread
= saved_blksread
;
522 tpblksread
= saved_tpblksread
;
525 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
526 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
527 ctime4(&tmpbuf
.c_date
));
528 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
531 blksread
= saved_blksread
;
532 tpblksread
= saved_tpblksread
;
535 tapesread
|= 1 << volno
;
536 blksread
= saved_blksread
;
537 tpblksread
= saved_tpblksread
;
539 * If continuing from the previous volume, skip over any
540 * blocks read already at the end of the previous volume.
542 * If coming to this volume at random, skip to the beginning
543 * of the next record.
545 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
546 tpblksread
, (long)tmpbuf
.c_firstrec
);
547 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
549 tpblksread
= tmpbuf
.c_firstrec
;
550 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
552 } else if (tmpbuf
.c_firstrec
> 0 &&
553 tmpbuf
.c_firstrec
< tpblksread
- 1) {
555 * -1 since we've read the volume header
557 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
558 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
559 (long)i
, i
> 1 ? "s" : "");
564 if (curfile
.action
== USING
) {
566 panic("active file into volume 1\n");
570 * Skip up to the beginning of the next record
572 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
573 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
575 (void) gethead(&spcl
);
579 siglongjmp(restart
, 1);
584 * Handle unexpected EOF.
590 if (gettingfile
&& curfile
.action
== USING
) {
591 printf("Warning: %s %s\n",
592 "End-of-input encountered while extracting", curfile
.name
);
594 curfile
.name
= "<name unknown>";
595 curfile
.action
= UNKNOWN
;
597 curfile
.ino
= maxino
;
600 siglongjmp(restart
, 1);
605 * handle multiple dumps per tape by skipping forward to the
613 if (dumpnum
== 1 || volno
!= 1)
616 errx(1, "Cannot have multiple dumps on pipe input");
618 tcom
.mt_count
= dumpnum
- 1;
621 rmtioctl(MTFSF
, dumpnum
- 1);
624 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
631 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
632 fprintf(stdout
, "Dumped from: %s",
633 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
634 if (spcl
.c_host
[0] == '\0')
636 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
637 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
638 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
642 extractfile(char *name
)
646 struct timeval timep
[2];
650 curfile
.action
= USING
;
652 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
653 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
654 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
655 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
656 #else /* __linux__ */
657 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
658 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
659 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
660 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
661 #endif /* __linux__ */
662 mode
= curfile
.dip
->di_mode
;
663 flags
= curfile
.dip
->di_flags
;
664 switch (mode
& IFMT
) {
667 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
672 Vprintf(stdout
, "skipped socket %s\n", name
);
678 ep
= lookupname(name
);
679 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
680 panic("unextracted directory %s\n", name
);
684 Vprintf(stdout
, "extract file %s\n", name
);
685 return (genliteraldir(name
, curfile
.ino
));
690 uid_t luid
= curfile
.dip
->di_uid
;
691 gid_t lgid
= curfile
.dip
->di_gid
;
695 getfile(xtrlnkfile
, xtrlnkskip
);
698 "%s: zero length symbolic link (ignored)\n", name
);
701 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
704 (void) lchown(name
, luid
, lgid
);
710 Vprintf(stdout
, "extract fifo %s\n", name
);
717 if (mkfifo(name
, mode
) < 0) {
718 warn("%s: cannot create fifo", name
);
722 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
723 (void) chmod(name
, mode
);
726 (void) fsetflags(name
, flags
);
728 (void) chflags(name
, flags
);
736 Vprintf(stdout
, "extract special file %s\n", name
);
743 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
744 warn("%s: cannot create special file", name
);
748 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
749 (void) chmod(name
, mode
);
753 warn("%s: fsetflags called on a special file", name
);
754 (void) fsetflags(name
, flags
);
757 (void) chflags(name
, flags
);
764 Vprintf(stdout
, "extract file %s\n", name
);
771 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
773 warn("%s: cannot create file", name
);
777 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
778 (void) fchmod(ofile
, mode
);
781 (void) setflags(ofile
, flags
);
783 (void) fchflags(ofile
, flags
);
785 getfile(xtrfile
, xtrskip
);
794 * skip over bit maps on the tape
800 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
805 * skip over a file on the tape
811 curfile
.action
= SKIP
;
812 getfile(xtrnull
, xtrnull
);
816 * Extract a file from the tape.
817 * When an allocated block is found it is passed to the fill function;
818 * when an unallocated block (hole) is found, a zeroed buffer is passed
819 * to the skip function.
822 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
825 volatile int curblk
= 0;
826 volatile quad_t size
= spcl
.c_dinode
.di_size
;
827 volatile int last_write_was_hole
= 0;
828 quad_t origsize
= size
;
829 static char clearedbuf
[MAXBSIZE
];
830 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
833 if (spcl
.c_type
== TS_END
)
834 panic("ran off end of tape\n");
835 if (spcl
.c_magic
!= NFS_MAGIC
)
836 panic("not at beginning of a file\n");
837 if (!gettingfile
&& setjmp(restart
) != 0)
841 for (i
= 0; i
< spcl
.c_count
; i
++) {
842 if (readmapflag
|| spcl
.c_addr
[i
]) {
843 readtape(&buf
[curblk
++][0]);
844 if (curblk
== fssize
/ TP_BSIZE
) {
845 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
846 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
848 last_write_was_hole
= 0;
852 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
854 (curblk
- 1) * TP_BSIZE
+ size
));
857 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
859 last_write_was_hole
= 1;
861 if ((size
-= TP_BSIZE
) <= 0) {
862 for (i
++; i
< spcl
.c_count
; i
++)
863 if (readmapflag
|| spcl
.c_addr
[i
])
868 if (gethead(&spcl
) == GOOD
&& size
> 0) {
869 if (spcl
.c_type
== TS_ADDR
)
872 "Missing address (header) block for %s at %ld blocks\n",
873 curfile
.name
, (long)blksread
);
876 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
877 last_write_was_hole
= 0;
880 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
882 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
883 (*skip
)(clearedbuf
, skp
);
886 last_write_was_hole
= 1;
888 if (last_write_was_hole
) {
889 FTRUNCATE(ofile
, origsize
);
896 * Write out the next block of a file.
899 xtrfile(char *buf
, size_t size
)
904 if (write(ofile
, buf
, (int) size
) == -1)
905 err(1, "write error extracting inode %lu, name %s\nwrite",
906 (unsigned long)curfile
.ino
, curfile
.name
);
910 * Skip over a hole in a file.
914 xtrskip(char *buf
, size_t size
)
917 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
918 err(1, "seek error extracting inode %lu, name %s\nlseek",
919 (unsigned long)curfile
.ino
, curfile
.name
);
923 * Collect the next block of a symbolic link.
926 xtrlnkfile(char *buf
, size_t size
)
930 if (pathlen
> MAXPATHLEN
)
931 errx(1, "symbolic link name: %s->%s%s; too long %d",
932 curfile
.name
, lnkbuf
, buf
, pathlen
);
933 (void) strcat(lnkbuf
, buf
);
937 * Skip over a hole in a symbolic link (should never happen).
941 xtrlnkskip(char *buf
, size_t size
)
944 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
948 * Collect the next block of a bit map.
951 xtrmap(char *buf
, size_t size
)
954 memmove(map
, buf
, size
);
959 * Skip over a hole in a bit map (should never happen).
963 xtrmapskip(char *buf
, size_t size
)
966 panic("hole in map\n");
971 * Noop, when an extraction function is not needed.
975 xtrnull(char *buf
, size_t size
)
983 * Compare the next block of a file.
986 xtrcmpfile(char *buf
, size_t size
)
988 static char cmpbuf
[MAXBSIZE
];
993 if (read(ifile
, cmpbuf
, size
) != size
) {
994 fprintf(stderr
, "%s: size has changed.\n",
1000 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1001 fprintf(stderr
, "%s: tape and disk copies are different\n",
1009 * Skip over a hole in a file.
1012 xtrcmpskip(char *buf
, size_t size
)
1014 static char cmpbuf
[MAXBSIZE
];
1020 if (read(ifile
, cmpbuf
, size
) != size
) {
1021 fprintf(stderr
, "%s: size has changed.\n",
1027 for (i
= 0; i
< size
; ++i
)
1028 if (cmpbuf
[i
] != '\0') {
1029 fprintf(stderr
, "%s: tape and disk copies are different\n",
1035 #endif /* COMPARE_ONTHEFLY */
1037 #if !COMPARE_ONTHEFLY
1039 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1041 static char buf_tape
[BUFSIZ
];
1042 static char buf_disk
[BUFSIZ
];
1047 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1048 close(fd_tape
), close(fd_disk
);
1049 panic("do_cmpfiles: unexpected EOF[1]");
1051 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1052 close(fd_tape
), close(fd_disk
);
1053 panic("do_cmpfiles: unexpected EOF[2]");
1055 if (n_tape
!= n_disk
) {
1056 close(fd_tape
), close(fd_disk
);
1057 panic("do_cmpfiles: sizes different!");
1059 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1065 /* for debugging compare problems */
1066 #undef COMPARE_FAIL_KEEP_FILE
1069 #ifdef COMPARE_FAIL_KEEP_FILE
1070 /* return true if tapefile should be unlinked after compare */
1075 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1077 struct STAT sbuf_tape
;
1078 int fd_tape
, fd_disk
;
1080 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1081 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1086 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1088 "%s: size changed from %ld to %ld.\n",
1089 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1091 #ifdef COMPARE_FAIL_KEEP_FILE
1098 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1099 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1102 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1104 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1108 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1109 fprintf(stderr
, "%s: tape and disk copies are different\n",
1114 #ifdef COMPARE_FAIL_KEEP_FILE
1115 /* rename the file to live in /tmp */
1116 /* rename `tapefile' to /tmp/<basename of diskfile> */
1118 char *p
= strrchr(diskfile
, '/');
1119 char newname
[MAXPATHLEN
];
1121 panic("can't find / in %s\n", diskfile
);
1123 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1124 if (rename(tapefile
, newname
)) {
1125 panic("rename from %s to %s failed: %s\n",
1129 fprintf(stderr
, "*** %s saved to %s\n",
1134 /* don't unlink the file (it's not there anymore */
1143 #ifdef COMPARE_FAIL_KEEP_FILE
1147 #endif /* !COMPARE_ONTHEFLY */
1149 #if !COMPARE_ONTHEFLY
1150 static char tmpfilename
[MAXPATHLEN
];
1154 comparefile(char *name
)
1159 #if !COMPARE_ONTHEFLY
1160 static char *tmpfile
= NULL
;
1164 if ((r
= LSTAT(name
, &sb
)) != 0) {
1165 warn("%s: does not exist (%d)", name
, r
);
1171 curfile
.name
= name
;
1172 curfile
.action
= USING
;
1173 mode
= curfile
.dip
->di_mode
;
1175 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1176 (long)sb
.st_size
, mode
);
1178 if (sb
.st_mode
!= mode
) {
1179 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1180 name
, mode
& 07777, sb
.st_mode
& 07777);
1183 switch (mode
& IFMT
) {
1197 char lbuf
[MAXPATHLEN
+ 1];
1200 if (!(sb
.st_mode
& S_IFLNK
)) {
1201 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1208 getfile(xtrlnkfile
, xtrlnkskip
);
1211 "%s: zero length symbolic link (ignored)\n",
1216 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1217 panic("readlink of %s failed: %s", name
,
1222 if (strcmp(lbuf
, lnkbuf
) != 0) {
1224 "%s: symbolic link changed from %s to %s.\n",
1225 name
, lnkbuf
, lbuf
);
1234 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1235 fprintf(stderr
, "%s: no longer a special file\n",
1242 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1244 "%s: device changed from %d,%d to %d,%d.\n",
1246 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1247 (int)curfile
.dip
->di_rdev
& 0xff,
1248 ((int)sb
.st_rdev
>> 8) & 0xff,
1249 (int)sb
.st_rdev
& 0xff);
1256 #if COMPARE_ONTHEFLY
1257 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1258 panic("Can't open %s: %s\n", name
, strerror(errno
));
1264 getfile(xtrcmpfile
, xtrcmpskip
);
1267 if (read(ifile
, &c
, 1) != 0) {
1268 fprintf(stderr
, "%s: size has changed.\n",
1278 if (tmpfile
== NULL
) {
1279 /* argument to mktemp() must not be in RO space: */
1280 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1281 tmpfile
= mktemp(&tmpfilename
[0]);
1283 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1284 panic("cannot delete tmp file %s: %s\n",
1285 tmpfile
, strerror(errno
));
1287 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1288 panic("cannot create file temp file %s: %s\n",
1289 name
, strerror(errno
));
1291 getfile(xtrfile
, xtrskip
);
1292 (void) close(ofile
);
1293 #ifdef COMPARE_FAIL_KEEP_FILE
1294 if (cmpfiles(tmpfile
, name
, &sb
))
1297 cmpfiles(tmpfile
, name
, &sb
);
1300 #endif /* COMPARE_ONTHEFLY */
1306 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1307 static void (*readtape_func
)(char *) = readtape_set
;
1310 * Read TP_BSIZE blocks from the input.
1311 * Handle read errors, and end of media.
1312 * Decompress compressed blocks.
1317 (*readtape_func
)(buf
); /* call the actual processing routine */
1321 * Set function pointer for readtape() routine. zflag and magtapein must
1322 * be correctly set before the first call to readtape().
1325 readtape_set(char *buf
)
1328 readtape_func
= readtape_uncompr
;
1332 readtape_func
= readtape_comprtape
;
1334 readtape_func
= readtape_comprfile
;
1339 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1342 * This is the original readtape(), it's used for reading uncompressed input.
1343 * Read TP_BSIZE blocks from the input.
1344 * Handle read errors, and end of media.
1347 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1348 readtape_uncompr(char *buf
)
1353 ssize_t rd
, newvol
, i
;
1354 int cnt
, seek_failed
;
1356 if (blkcnt
< numtrec
) {
1357 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1363 for (i
= 0; i
< ntrec
; i
++)
1364 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1367 cnt
= ntrec
* TP_BSIZE
;
1372 i
= rmtread(&tapebuf
[rd
], cnt
);
1375 i
= read(mt
, &tapebuf
[rd
], cnt
);
1378 * Check for mid-tape short read error.
1379 * If found, skip rest of buffer and start with the next.
1381 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1382 Dprintf(stdout
, "mid-media short read error.\n");
1386 * Handle partial block read.
1388 if (pipein
&& i
== 0 && rd
> 0)
1390 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1399 * Short read. Process the blocks read.
1401 if (i
% TP_BSIZE
!= 0)
1403 "partial block read: %ld should be %ld\n",
1404 (long)i
, ntrec
* TP_BSIZE
);
1405 numtrec
= i
/ TP_BSIZE
;
1409 * Handle read error.
1412 fprintf(stderr
, "Tape read error while ");
1413 switch (curfile
.action
) {
1415 fprintf(stderr
, "trying to set up tape\n");
1418 fprintf(stderr
, "trying to resynchronize\n");
1421 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1424 fprintf(stderr
, "skipping over inode %lu\n",
1425 (unsigned long)curfile
.ino
);
1428 if (!yflag
&& !reply("continue"))
1430 i
= ntrec
* TP_BSIZE
;
1431 memset(tapebuf
, 0, (size_t)i
);
1434 seek_failed
= (rmtseek(i
, 1) < 0);
1437 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1440 warn("continuation failed");
1441 if (!yflag
&& !reply("assume end-of-tape and continue"))
1447 * Handle end of tape.
1450 Vprintf(stdout
, "End-of-tape encountered\n");
1459 if (rd
% TP_BSIZE
!= 0)
1460 panic("partial block read: %d should be %d\n",
1461 rd
, ntrec
* TP_BSIZE
);
1463 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1466 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1471 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1474 * Read a compressed format block from a file or pipe and uncompress it.
1475 * Attempt to handle read errors, and end of file.
1478 readtape_comprfile(char *buf
)
1480 long rl
, size
, i
, ret
;
1482 struct tapebuf
*tpb
;
1484 if (blkcnt
< numtrec
) {
1485 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1490 /* need to read the next block */
1492 for (i
= 0; i
< ntrec
; i
++)
1493 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1495 tpb
= (struct tapebuf
*) tapebuf
;
1497 /* read the block prefix */
1498 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1500 if (Vflag
&& (ret
== 0 || rl
< PREFIXSIZE
|| tpb
->length
== 0))
1507 if (size
> bufsize
) {
1508 /* something's wrong */
1509 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1512 tpb
->length
= bufsize
;
1514 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1518 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1519 if (tbufptr
== NULL
) {
1520 msg_read_error("File decompression error while");
1521 if (!yflag
&& !reply("continue"))
1523 memset(tapebuf
, 0, bufsize
);
1528 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1534 /* Errors while reading from a file or pipe are catastrophic. Since
1535 * there are no block boundaries, it's impossible to bypass the
1536 * block in error and find the start of the next block.
1539 /* It's possible to have multiple input files using -M
1540 * and -f file1,file2...
1542 Vprintf(stdout
, "End-of-File encountered\n");
1552 msg_read_error("Read error while");
1553 /* if (!yflag && !reply("continue")) */
1558 * Read compressed data from a tape and uncompress it.
1559 * Handle read errors, and end of media.
1560 * Since a tape consists of separate physical blocks, we try
1561 * to recover from errors by repositioning the tape to the next
1565 readtape_comprtape(char *buf
)
1569 struct tapebuf
*tpb
;
1572 if (blkcnt
< numtrec
) {
1573 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1578 /* need to read the next block */
1580 for (i
= 0; i
< ntrec
; i
++)
1581 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1583 tpb
= (struct tapebuf
*) tapebuf
;
1585 /* read the block */
1586 size
= bufsize
+ PREFIXSIZE
;
1587 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1591 tbufptr
= decompress_tapebuf(tpb
, rl
);
1592 if (tbufptr
== NULL
) {
1593 msg_read_error("Tape decompression error while");
1594 if (!yflag
&& !reply("continue"))
1596 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1602 /* Handle errors: EOT switches to the next volume, other errors
1603 * attempt to position the tape to the next block.
1606 Vprintf(stdout
, "End-of-tape encountered\n");
1615 msg_read_error("Tape read error while");
1616 if (!yflag
&& !reply("continue"))
1618 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1623 rl
= rmtioctl(MTFSR
, 1);
1629 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1633 warn("continuation failed");
1634 if (!yflag
&& !reply("assume end-of-tape and continue"))
1636 ret
= 0; /* end of tape */
1642 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1648 * Decompress a struct tapebuf into a buffer. readsize is the size read
1649 * from the tape/file and is used for error messages. Returns a pointer
1650 * to the location of the uncompressed buffer or NULL on errors.
1651 * Adjust numtrec and complain for a short block.
1654 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1656 /* If zflag is on, all blocks have a struct tapebuf prefix */
1657 /* zflag gets set in setup() from the dump header */
1658 int cresult
, blocklen
;
1659 unsigned long worklen
;
1661 unsigned int worklen2
;
1663 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1665 /* build a length error message */
1666 blocklen
= tpbin
->length
;
1667 if (readsize
< blocklen
+ PREFIXSIZE
)
1668 lengtherr
= "short";
1670 if (readsize
> blocklen
+ PREFIXSIZE
)
1675 if (tpbin
->compressed
) {
1676 /* uncompress whatever we read, if it fails, complain later */
1677 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1679 errx(1,"This restore version doesn't support zlib decompression");
1681 cresult
= uncompress(comprbuf
, &worklen
,
1682 tpbin
->buf
, blocklen
);
1688 reason
= "not enough memory";
1691 reason
= "buffer too small";
1694 reason
= "data error";
1699 if (cresult
== Z_OK
)
1703 #endif /* HAVE_ZLIB */
1705 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1707 errx(1,"This restore version doesn't support bzlib decompression");
1710 cresult
= BZ2_bzBuffToBuffDecompress(
1711 comprbuf
, &worklen2
,
1712 tpbin
->buf
, blocklen
, 0, 0);
1719 reason
= "not enough memory";
1721 case BZ_OUTBUFF_FULL
:
1722 reason
= "buffer too small";
1725 case BZ_DATA_ERROR_MAGIC
:
1726 case BZ_UNEXPECTED_EOF
:
1727 reason
= "data error";
1732 if (cresult
== BZ_OK
)
1736 #endif /* HAVE_BZLIB */
1740 output
= tpbin
->buf
;
1744 numtrec
= worklen
/ TP_BSIZE
;
1745 if (worklen
% TP_BSIZE
!= 0)
1746 reason
= "length mismatch";
1750 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1751 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1752 fprintf(stderr
, "decompression error, block %ld: %s\n",
1753 tpblksread
+1, reason
);
1761 * Print an error message for a read error.
1762 * This was exteracted from the original readtape().
1765 msg_read_error(char *m
)
1767 switch (curfile
.action
) {
1769 fprintf(stderr
, "%s trying to set up tape\n", m
);
1772 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1775 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1778 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1779 (unsigned long)curfile
.ino
);
1783 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1786 * Read the first block and get the blocksize from it. Test
1787 * for a compressed dump tape/file. setup() will make the final
1788 * determination by checking the compressed flag if gethead()
1789 * finds a valid header. The test here is necessary to offset the buffer
1790 * by the size of the compressed prefix. zflag is set here so that
1791 * readtape_set can set the correct function pointer for readtape().
1792 * Note that the first block of each tape/file is not compressed
1793 * and does not have a prefix.
1796 findtapeblksize(void)
1800 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1801 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1803 for (i
= 0; i
< ntrec
; i
++)
1804 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1808 * For a pipe or file, read in the first record. For a tape, read
1811 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1813 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1814 errx(1, "Tape read error on first record");
1817 * If the input is from a file or a pipe, we read TP_BSIZE
1818 * bytes looking for a dump header. If the dump is compressed
1819 * we need to read in the rest of the block, as determined
1820 * by c_ntrec in the dump header. The first block of the
1821 * dump is not compressed and does not have a prefix.
1824 if (spclpt
->c_type
== TS_TAPE
1825 && spclpt
->c_flags
& DR_COMPRESSED
) {
1826 /* It's a compressed dump file, read in the */
1827 /* rest of the block based on spclpt->c_ntrec. */
1828 if (spclpt
->c_ntrec
> ntrec
)
1829 errx(1, "Tape blocksize is too large, use "
1830 "\'-b %d\' ", spclpt
->c_ntrec
);
1831 ntrec
= spclpt
->c_ntrec
;
1832 len
= (ntrec
- 1) * TP_BSIZE
;
1836 /* read in the rest of the block based on bufsize */
1837 len
= bufsize
- TP_BSIZE
;
1839 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1840 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1841 errx(1,"Error reading dump file header");
1844 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1846 } /* if (!magtapein) */
1849 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1850 * If the value of ntrec is too large, we read less than
1851 * what we asked for; adjust the value of ntrec and test for
1852 * a compressed dump tape.
1855 if (i
% TP_BSIZE
!= 0) {
1856 /* may be old format compressed dump tape with a prefix */
1857 spclpt
= (struct s_spcl
*) tpb
->buf
;
1858 if (i
% TP_BSIZE
== PREFIXSIZE
1859 && tpb
->compressed
== 0
1860 && spclpt
->c_type
== TS_TAPE
1861 && spclpt
->c_flags
& DR_COMPRESSED
) {
1864 if (tpb
->length
> bufsize
)
1865 errx(1, "Tape blocksize is too large, use "
1866 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1869 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1872 ntrec
= i
/ TP_BSIZE
;
1873 if (spclpt
->c_type
== TS_TAPE
) {
1874 if (spclpt
->c_flags
& DR_COMPRESSED
)
1876 if (spclpt
->c_ntrec
> ntrec
)
1877 errx(1, "Tape blocksize is too large, use "
1878 "\'-b %d\' ", spclpt
->c_ntrec
);
1881 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1885 * Read a block of data handling all of the messy details.
1887 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1895 i
= rmtread(buf
, size
);
1898 i
= read(fd
, buf
, size
);
1901 break; /* EOD or error */
1904 break; /* block at a time for mt */
1907 *lengthread
= len
- size
;
1926 setmagtapein(void) {
1927 struct mtget mt_stat
;
1928 static int done
= 0;
1933 /* need to know if input is really from a tape */
1939 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
1942 Vprintf(stdout
,"Input is from %s\n",
1943 magtapein
? "tape" :
1944 Vflag
? "multi-volume (no tape)" : "file/pipe");
1948 * Read the next block from the tape.
1949 * Check to see if it is one of several vintage headers.
1950 * If it is an old style header, convert it to a new style header.
1951 * If it is not any valid header, return an error.
1954 gethead(struct s_spcl
*buf
)
1962 char dummy
[TP_BSIZE
];
1969 u_int16_t c_inumber
;
1974 u_int16_t odi_nlink
;
1990 readtape((char *)buf
);
1991 if (buf
->c_magic
!= NFS_MAGIC
) {
1992 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1995 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1999 if (checksum((int *)buf
) == FAIL
)
2002 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
2005 readtape((char *)(&u_ospcl
.s_ospcl
));
2006 memset((char *)buf
, 0, (long)TP_BSIZE
);
2007 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2008 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2009 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2010 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2011 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2012 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2013 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2014 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2015 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2016 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2017 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2018 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2019 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2020 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2022 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2023 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2024 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2025 #else /* __linux__ */
2026 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2027 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2028 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2029 #endif /* __linux__ */
2030 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2031 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
2032 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2033 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2035 buf
->c_magic
= NFS_MAGIC
;
2038 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2039 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2040 qcvt
.qval
= buf
->c_dinode
.di_size
;
2041 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2042 printf("Note: Doing Quad swapping\n");
2047 qcvt
.qval
= buf
->c_dinode
.di_size
;
2049 qcvt
.val
[1] = qcvt
.val
[0];
2051 buf
->c_dinode
.di_size
= qcvt
.qval
;
2055 switch (buf
->c_type
) {
2060 * Have to patch up missing information in bit map headers
2063 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2064 if (buf
->c_count
> TP_NINDIR
)
2067 for (i
= 0; i
< buf
->c_count
; i
++)
2072 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2084 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2088 * If we are restoring a filesystem with old format inodes,
2089 * copy the uid/gid to the new location.
2092 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2093 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2101 * Check that a header is where it belongs and predict the next header
2104 accthdr(struct s_spcl
*header
)
2106 static dump_ino_t previno
= 0x7fffffff;
2107 static int prevtype
;
2108 static long predict
;
2111 if (header
->c_type
== TS_TAPE
) {
2112 fprintf(stderr
, "Volume header (%s inode format) ",
2113 oldinofmt
? "old" : "new");
2114 if (header
->c_firstrec
)
2115 fprintf(stderr
, "begins with record %d",
2116 header
->c_firstrec
);
2117 fprintf(stderr
, "\n");
2118 previno
= 0x7fffffff;
2121 if (previno
== 0x7fffffff)
2125 fprintf(stderr
, "Dumped inodes map header");
2128 fprintf(stderr
, "Used inodes map header");
2131 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2134 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2137 fprintf(stderr
, "End of tape header");
2140 if (predict
!= blksread
- 1)
2141 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2142 predict
, blksread
- 1);
2143 fprintf(stderr
, "\n");
2146 if (header
->c_type
!= TS_END
)
2147 for (i
= 0; i
< header
->c_count
; i
++)
2148 if (readmapflag
|| header
->c_addr
[i
] != 0)
2152 prevtype
= header
->c_type
;
2153 previno
= header
->c_inumber
;
2157 * Find an inode header.
2158 * Complain if had to skip, and complain is set.
2161 findinode(struct s_spcl
*header
)
2163 static long skipcnt
= 0;
2167 curfile
.name
= "<name unknown>";
2168 curfile
.action
= UNKNOWN
;
2172 if (header
->c_magic
!= NFS_MAGIC
) {
2174 while (gethead(header
) == FAIL
||
2175 header
->c_date
!= dumpdate
)
2178 switch (header
->c_type
) {
2182 * Skip up to the beginning of the next record
2184 for (i
= 0; i
< header
->c_count
; i
++)
2185 if (header
->c_addr
[i
])
2187 while (gethead(header
) == FAIL
||
2188 header
->c_date
!= dumpdate
)
2193 curfile
.dip
= &header
->c_dinode
;
2194 curfile
.ino
= header
->c_inumber
;
2198 curfile
.ino
= maxino
;
2202 curfile
.name
= "<file removal list>";
2206 curfile
.name
= "<file dump list>";
2210 panic("unexpected tape header\n");
2214 panic("unknown tape header type %d\n", spcl
.c_type
);
2218 } while (header
->c_type
== TS_ADDR
);
2223 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2233 j
= sizeof(union u_spcl
) / sizeof(int);
2240 /* What happens if we want to read restore tapes
2241 for a 16bit int machine??? */
2247 if (i
!= CHECKSUM
) {
2248 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2249 (unsigned long)curfile
.ino
, curfile
.name
);
2259 #include <varargs.h>
2264 msg(const char *fmt
, ...)
2277 (void)vfprintf(stderr
, fmt
, ap
);
2280 #endif /* RRESTORE */
2283 swab16(u_char
*sp
, int n
)
2288 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2295 swab32(u_char
*sp
, int n
)
2300 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2301 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2308 swab64(u_char
*sp
, int n
)
2313 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2314 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2315 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2316 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2323 swabst(u_char
*cp
, u_char
*sp
)
2329 case '0': case '1': case '2': case '3': case '4':
2330 case '5': case '6': case '7': case '8': case '9':
2331 n
= (n
* 10) + (*cp
++ - '0');
2334 case 's': case 'w': case 'h':
2352 default: /* Any other character, like 'b' counts as byte. */
2366 swabst((u_char
*)"i", (u_char
*)&x
);
2374 swabst((u_char
*)"l", (u_char
*)&x
);
2381 * get the current position of the tape
2384 GetTapePos(long *pos
)
2389 if (ioctl(mt
, MTIOCPOS
, pos
) == -1) {
2391 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %ld)\n",
2392 (unsigned long)getpid(), err
, *pos
);
2398 typedef struct mt_pos
{
2401 } MTPosRec
, *MTPosPtr
;
2404 * go to specified position on tape
2407 GotoTapePos(long pos
)
2414 if (ioctl(mt
, MTIOCTOP
, &buf
) == -1) {
2416 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %ld)\n",
2417 (unsigned long)getpid(), err
, pos
);
2424 * read next data from tape to re-sync
2427 ReReadFromTape(void)
2431 if (gethead(&spcl
) == FAIL
) {
2433 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2441 RequestVol(long tnum
)
2446 #endif /* USE_QFA */