]>
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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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.58 2002/02/04 11:18: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 #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 */
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_func
) __P((char *));
158 static void readtape_set
__P((char *));
159 static void readtape_uncompr
__P((char *));
160 static void readtape_comprfile
__P((char *));
161 static void readtape_comprtape
__P((char *));
162 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
163 static void msg_read_error
__P((char *));
165 static int read_a_block
__P((int, char *, size_t, long *));
166 #define PREFIXSIZE sizeof(struct tapebuf)
168 #define COMPARE_ONTHEFLY 1
171 static int ifile
; /* input file for compare */
172 static int cmperror
; /* compare error */
173 static void xtrcmpfile
__P((char *, size_t));
174 static void xtrcmpskip
__P((char *, size_t));
177 static int readmapflag
;
180 * Set up an input source. This is called from main.c before setup() is.
183 setinput(char *source
)
189 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
193 if (strchr(source
, ':')) {
195 source
= strchr(host
, ':');
197 if (rmthost(host
) == 0)
201 if (strcmp(source
, "-") == 0) {
203 * Since input is coming from a pipe we must establish
204 * our own connection to the terminal.
206 terminal
= fopen(_PATH_TTY
, "r");
207 if (terminal
== NULL
) {
208 warn("cannot open %s", _PATH_TTY
);
209 terminal
= fopen(_PATH_DEVNULL
, "r");
210 if (terminal
== NULL
)
211 err(1, "cannot open %s", _PATH_DEVNULL
);
215 setuid(getuid()); /* no longer need or want root privileges */
217 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
218 magtapeprefix
[MAXPATHLEN
-1] = '\0';
219 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
222 strncpy(magtape
, source
, MAXPATHLEN
);
223 magtape
[MAXPATHLEN
- 1] = '\0';
227 newtapebuf(long size
)
229 static int tapebufsize
= -1;
232 bufsize
= ntrec
* TP_BSIZE
;
233 if (size
<= tapebufsize
)
237 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
239 errx(1, "Cannot allocate space for tape buffer");
243 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
245 newcomprbuf(int size
)
247 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
248 if (buf_size
<= comprlen
)
251 if (comprbuf
!= NULL
)
253 comprbuf
= malloc(comprlen
);
254 if (comprbuf
== NULL
)
255 errx(1, "Cannot allocate space for decompress buffer");
257 #endif /* HAVE_ZLIB || HAVE_BZLIB */
260 * Verify that the tape drive can be accessed and
261 * that it actually is a dump tape.
266 int i
, j
, *ip
, bot_code
;
270 Vprintf(stdout
, "Verify tape and initialize maps\n");
271 if (Afile
== NULL
&& bot_script
) {
272 msg("Launching %s\n", bot_script
);
273 bot_code
= system_command(bot_script
, magtape
, 1);
274 if (bot_code
!= 0 && bot_code
!= 1) {
275 msg("Restore aborted by the beginning of tape script\n");
287 mt
= rmtopen(temptape
, 0);
293 mt
= OPEN(temptape
, O_RDONLY
, 0);
295 err(1, "%s", magtape
);
301 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
302 readtape_func
= readtape_set
;
306 if (gethead(&spcl
) == FAIL
) {
307 blkcnt
--; /* push back this block */
311 if (gethead(&spcl
) == FAIL
)
312 errx(1, "Tape is not a dump tape");
313 fprintf(stderr
, "Converting to new file system format.\n");
317 fprintf(stderr
, "Dump tape is compressed.\n");
318 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
319 errx(1,"This restore version doesn't support decompression");
320 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
323 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
324 endoftapemark
.s_spcl
.c_type
= TS_END
;
325 ip
= (int *)&endoftapemark
;
326 j
= sizeof(union u_spcl
) / sizeof(int);
331 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
333 if (vflag
|| command
== 't' || command
== 'C')
336 if (tapeposflag
&& spcl
.c_date
!= qfadumpdate
)
337 errx(1, "different QFA/dumpdates detected\n");
339 if (filesys
[0] == '\0') {
341 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
342 filesys
[NAMELEN
- 1] = '\0';
343 dirptr
= strstr(filesys
, " (dir");
347 dumptime
= spcl
.c_ddate
;
348 dumpdate
= spcl
.c_date
;
349 if (STAT(".", &stbuf
) < 0)
350 err(1, "cannot stat .");
351 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
353 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
354 fssize
= stbuf
.st_blksize
;
355 if (((fssize
- 1) & fssize
) != 0)
356 errx(1, "bad block size %ld", fssize
);
357 if (spcl
.c_volume
!= 1)
358 errx(1, "Tape is not volume 1 of the dump");
359 if (gethead(&spcl
) == FAIL
) {
360 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
361 panic("no header after volume mark!\n");
364 if (spcl
.c_type
!= TS_CLRI
)
365 errx(1, "Cannot find file removal list");
366 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
367 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
368 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
370 errx(1, "no memory for active inode map");
372 curfile
.action
= USING
;
373 getfile(xtrmap
, xtrmapskip
);
375 if (spcl
.c_type
!= TS_BITS
)
376 errx(1, "Cannot find file dump list");
377 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
378 if (map
== (char *)NULL
)
379 errx(1, "no memory for file dump list");
381 curfile
.action
= USING
;
382 getfile(xtrmap
, xtrmapskip
);
384 * If there may be whiteout entries on the tape, pretend that the
385 * whiteout inode exists, so that the whiteout entries can be
389 SETINO(WINO
, dumpmap
);
394 * Prompt user to load a new dump volume.
395 * "Nextvol" is the next suggested volume to use.
396 * This suggested volume is enforced when doing full
397 * or incremental restores, but can be overridden by
398 * the user when only extracting a subset of the files.
403 long newvol
= 0, wantnext
= 0, i
;
404 long saved_blksread
= 0, saved_tpblksread
= 0;
405 union u_spcl tmpspcl
;
406 # define tmpbuf tmpspcl.s_spcl
408 int haderror
= 0, bot_code
= 1;
416 panic("Changing volumes on pipe input?\n");
421 saved_blksread
= blksread
;
422 saved_tpblksread
= tpblksread
;
423 #if defined(USE_QFA) && defined(sunos)
424 if (createtapeposflag
|| tapeposflag
)
429 exit(1); /* pipes do not get a second chance */
430 if (aflag
|| curfile
.action
!= SKIP
) {
437 while (newvol
<= 0) {
438 if (tapesread
== 0) {
439 fprintf(stderr
, "%s%s%s%s%s",
440 "You have not read any volumes yet.\n",
441 "Unless you know which volume your",
442 " file(s) are on you should start\n",
443 "with the last volume and work",
444 " towards the first.\n");
446 fprintf(stderr
, "You have read volumes");
448 for (i
= 1; i
< 32; i
++)
449 if (tapesread
& (1 << i
)) {
450 fprintf(stderr
, "%s%ld", buf
, (long)i
);
453 fprintf(stderr
, "\n");
456 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
457 (void) fflush(stderr
);
458 (void) fgets(buf
, TP_BSIZE
, terminal
);
459 } while (!feof(terminal
) && buf
[0] == '\n');
462 if (!strcmp(buf
, "none\n")) {
469 "Volume numbers are positive numerics\n");
472 if (newvol
== volno
) {
473 tapesread
|= 1 << volno
;
474 #if defined(USE_QFA) && defined(sunos)
475 if (createtapeposflag
|| tapeposflag
) {
476 if (OpenSMTCmt(magtape
) < 0) {
487 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
488 magtape
[MAXPATHLEN
- 1] = '\0';
490 if (bot_script
&& !haderror
) {
491 msg("Launching %s\n", bot_script
);
492 bot_code
= system_command(bot_script
, magtape
, newvol
);
493 if (bot_code
!= 0 && bot_code
!= 1) {
494 msg("Restore aborted by the beginning of tape script\n");
498 if (haderror
|| (bot_code
&& !Mflag
)) {
500 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
501 fprintf(stderr
, "Enter ``none'' if there are no more volumes\n");
502 fprintf(stderr
, "otherwise enter volume name (default: %s) ", magtape
);
503 (void) fflush(stderr
);
504 (void) fgets(buf
, TP_BSIZE
, terminal
);
507 if (!strcmp(buf
, "none\n")) {
511 if (buf
[0] != '\n') {
513 (void) strncpy(magtape
, buf
, sizeof(magtape
));
514 magtape
[sizeof(magtape
) - 1] = '\0';
515 if ((pos
= strchr(magtape
, '\n')))
516 magtape
[pos
- magtape
] = '\0';
521 mt
= rmtopen(magtape
, 0);
524 mt
= OPEN(magtape
, O_RDONLY
, 0);
527 fprintf(stderr
, "Cannot open %s\n", magtape
);
534 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
535 readtape_func
= readtape_set
;
541 if (gethead(&tmpbuf
) == FAIL
) {
542 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
543 fprintf(stderr
, "tape is not dump tape\n");
546 blksread
= saved_blksread
;
547 tpblksread
= saved_tpblksread
;
550 if (tmpbuf
.c_volume
!= volno
) {
551 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
554 blksread
= saved_blksread
;
555 tpblksread
= saved_tpblksread
;
558 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
559 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
560 ctime4(&tmpbuf
.c_date
));
561 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
564 blksread
= saved_blksread
;
565 tpblksread
= saved_tpblksread
;
568 tapesread
|= 1 << volno
;
569 blksread
= saved_blksread
;
570 tpblksread
= saved_tpblksread
;
572 * If continuing from the previous volume, skip over any
573 * blocks read already at the end of the previous volume.
575 * If coming to this volume at random, skip to the beginning
576 * of the next record.
579 fprintf(stderr
, "Dump tape is compressed.\n");
580 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
581 errx(1,"This restore version doesn't support decompression");
582 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
584 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
585 tpblksread
, (long)tmpbuf
.c_firstrec
);
586 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
588 tpblksread
= tmpbuf
.c_firstrec
;
589 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
591 } else if (tmpbuf
.c_firstrec
> 0 &&
592 tmpbuf
.c_firstrec
< tpblksread
- 1) {
594 * -1 since we've read the volume header
596 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
597 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
598 (long)i
, i
> 1 ? "s" : "");
603 if (curfile
.action
== USING
) {
605 panic("active file into volume 1\n");
609 * Skip up to the beginning of the next record
611 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
612 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
614 (void) gethead(&spcl
);
618 siglongjmp(restart
, 1);
623 * Handle unexpected EOF.
629 if (gettingfile
&& curfile
.action
== USING
) {
630 printf("Warning: %s %s\n",
631 "End-of-input encountered while extracting", curfile
.name
);
633 curfile
.name
= "<name unknown>";
634 curfile
.action
= UNKNOWN
;
636 curfile
.ino
= maxino
;
639 siglongjmp(restart
, 1);
644 * handle multiple dumps per tape by skipping forward to the
652 if (dumpnum
== 1 || volno
!= 1)
655 errx(1, "Cannot have multiple dumps on pipe input");
657 tcom
.mt_count
= dumpnum
- 1;
660 rmtioctl(MTFSF
, dumpnum
- 1);
663 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
670 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
671 fprintf(stdout
, "Dumped from: %s",
672 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
673 if (spcl
.c_host
[0] == '\0')
675 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
676 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
677 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
685 if (volinfo
[1] == ROOTINO
) {
686 printf("Starting inode numbers by volume:\n");
687 for (i
= 1; i
< TP_NINOS
&& volinfo
[i
] != 0; ++i
)
688 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
694 time_t tv_sec
; /* seconds */
695 suseconds_t tv_usec
; /* and microseconds */
700 extractfile(char *name
)
704 struct timeval timep
[2];
708 curfile
.action
= USING
;
709 #if defined(__linux__) || defined(sunos)
710 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
711 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
712 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
713 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
714 #else /* __linux__ || sunos */
715 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
716 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
717 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
718 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
719 #endif /* __linux__ */
720 mode
= curfile
.dip
->di_mode
;
721 flags
= curfile
.dip
->di_flags
;
722 switch (mode
& IFMT
) {
725 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
730 Vprintf(stdout
, "skipped socket %s\n", name
);
736 ep
= lookupname(name
);
737 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
738 panic("unextracted directory %s\n", name
);
742 Vprintf(stdout
, "extract file %s\n", name
);
743 return (genliteraldir(name
, curfile
.ino
));
748 uid_t luid
= curfile
.dip
->di_uid
;
749 gid_t lgid
= curfile
.dip
->di_gid
;
753 getfile(xtrlnkfile
, xtrlnkskip
);
756 "%s: zero length symbolic link (ignored)\n", name
);
759 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
762 (void) lchown(name
, luid
, lgid
);
768 Vprintf(stdout
, "extract fifo %s\n", name
);
775 if (mkfifo(name
, mode
) < 0) {
776 warn("%s: cannot create fifo", name
);
780 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
781 (void) chmod(name
, mode
);
784 (void) fsetflags(name
, flags
);
786 (void) chflags(name
, flags
);
794 Vprintf(stdout
, "extract special file %s\n", name
);
801 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
802 warn("%s: cannot create special file", name
);
806 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
807 (void) chmod(name
, mode
);
811 warn("%s: fsetflags called on a special file", name
);
812 (void) fsetflags(name
, flags
);
815 (void) chflags(name
, flags
);
822 Vprintf(stdout
, "extract file %s\n", name
);
829 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
831 warn("%s: cannot create file", name
);
835 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
836 (void) fchmod(ofile
, mode
);
839 (void) setflags(ofile
, flags
);
841 (void) fchflags(ofile
, flags
);
843 getfile(xtrfile
, xtrskip
);
852 * skip over bit maps on the tape
858 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
863 * skip over a file on the tape
869 curfile
.action
= SKIP
;
870 getfile(xtrnull
, xtrnull
);
874 * Extract a file from the tape.
875 * When an allocated block is found it is passed to the fill function;
876 * when an unallocated block (hole) is found, a zeroed buffer is passed
877 * to the skip function.
880 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
883 volatile int curblk
= 0;
884 volatile quad_t size
= spcl
.c_dinode
.di_size
;
885 volatile int last_write_was_hole
= 0;
886 quad_t origsize
= size
;
887 static char clearedbuf
[MAXBSIZE
];
888 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
891 if (spcl
.c_type
== TS_END
)
892 panic("ran off end of tape\n");
893 if (spcl
.c_magic
!= NFS_MAGIC
)
894 panic("not at beginning of a file\n");
895 if (!gettingfile
&& setjmp(restart
) != 0)
899 for (i
= 0; i
< spcl
.c_count
; i
++) {
900 if (readmapflag
|| spcl
.c_addr
[i
]) {
901 readtape(&buf
[curblk
++][0]);
902 if (curblk
== fssize
/ TP_BSIZE
) {
903 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
904 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
906 last_write_was_hole
= 0;
910 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
912 (curblk
- 1) * TP_BSIZE
+ size
));
915 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
917 last_write_was_hole
= 1;
919 if ((size
-= TP_BSIZE
) <= 0) {
920 for (i
++; i
< spcl
.c_count
; i
++)
921 if (readmapflag
|| spcl
.c_addr
[i
])
926 if (gethead(&spcl
) == GOOD
&& size
> 0) {
927 if (spcl
.c_type
== TS_ADDR
)
930 "Missing address (header) block for %s at %ld blocks\n",
931 curfile
.name
, (long)blksread
);
934 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
935 last_write_was_hole
= 0;
938 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
940 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
941 (*skip
)(clearedbuf
, skp
);
944 last_write_was_hole
= 1;
946 if (last_write_was_hole
) {
947 FTRUNCATE(ofile
, origsize
);
954 * Write out the next block of a file.
957 xtrfile(char *buf
, size_t size
)
962 if (write(ofile
, buf
, (int) size
) == -1)
963 err(1, "write error extracting inode %lu, name %s\nwrite",
964 (unsigned long)curfile
.ino
, curfile
.name
);
968 * Skip over a hole in a file.
972 xtrskip(char *buf
, size_t size
)
975 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
976 err(1, "seek error extracting inode %lu, name %s\nlseek",
977 (unsigned long)curfile
.ino
, curfile
.name
);
981 * Collect the next block of a symbolic link.
984 xtrlnkfile(char *buf
, size_t size
)
988 if (pathlen
> MAXPATHLEN
)
989 errx(1, "symbolic link name: %s->%s%s; too long %d",
990 curfile
.name
, lnkbuf
, buf
, pathlen
);
991 (void) strcat(lnkbuf
, buf
);
995 * Skip over a hole in a symbolic link (should never happen).
999 xtrlnkskip(char *buf
, size_t size
)
1002 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1006 * Collect the next block of a bit map.
1009 xtrmap(char *buf
, size_t size
)
1012 memmove(map
, buf
, size
);
1017 * Skip over a hole in a bit map (should never happen).
1021 xtrmapskip(char *buf
, size_t size
)
1024 panic("hole in map\n");
1029 * Noop, when an extraction function is not needed.
1033 xtrnull(char *buf
, size_t size
)
1039 #if COMPARE_ONTHEFLY
1041 * Compare the next block of a file.
1044 xtrcmpfile(char *buf
, size_t size
)
1046 static char cmpbuf
[MAXBSIZE
];
1051 if (read(ifile
, cmpbuf
, size
) != size
) {
1052 fprintf(stderr
, "%s: size has changed.\n",
1058 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1059 fprintf(stderr
, "%s: tape and disk copies are different\n",
1067 * Skip over a hole in a file.
1070 xtrcmpskip(char *buf
, size_t size
)
1072 static char cmpbuf
[MAXBSIZE
];
1078 if (read(ifile
, cmpbuf
, size
) != size
) {
1079 fprintf(stderr
, "%s: size has changed.\n",
1085 for (i
= 0; i
< size
; ++i
)
1086 if (cmpbuf
[i
] != '\0') {
1087 fprintf(stderr
, "%s: tape and disk copies are different\n",
1093 #endif /* COMPARE_ONTHEFLY */
1095 #if !COMPARE_ONTHEFLY
1097 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1099 static char buf_tape
[BUFSIZ
];
1100 static char buf_disk
[BUFSIZ
];
1105 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1106 close(fd_tape
), close(fd_disk
);
1107 panic("do_cmpfiles: unexpected EOF[1]");
1109 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1110 close(fd_tape
), close(fd_disk
);
1111 panic("do_cmpfiles: unexpected EOF[2]");
1113 if (n_tape
!= n_disk
) {
1114 close(fd_tape
), close(fd_disk
);
1115 panic("do_cmpfiles: sizes different!");
1117 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1123 /* for debugging compare problems */
1124 #undef COMPARE_FAIL_KEEP_FILE
1127 #ifdef COMPARE_FAIL_KEEP_FILE
1128 /* return true if tapefile should be unlinked after compare */
1133 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1135 struct STAT sbuf_tape
;
1136 int fd_tape
, fd_disk
;
1138 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1139 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1144 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1146 "%s: size changed from %ld to %ld.\n",
1147 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1149 #ifdef COMPARE_FAIL_KEEP_FILE
1156 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1157 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1160 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1162 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1166 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1167 fprintf(stderr
, "%s: tape and disk copies are different\n",
1172 #ifdef COMPARE_FAIL_KEEP_FILE
1173 /* rename the file to live in /tmp */
1174 /* rename `tapefile' to /tmp/<basename of diskfile> */
1176 char *p
= strrchr(diskfile
, '/');
1177 char newname
[MAXPATHLEN
];
1179 panic("can't find / in %s\n", diskfile
);
1181 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1182 if (rename(tapefile
, newname
)) {
1183 panic("rename from %s to %s failed: %s\n",
1187 fprintf(stderr
, "*** %s saved to %s\n",
1192 /* don't unlink the file (it's not there anymore */
1201 #ifdef COMPARE_FAIL_KEEP_FILE
1205 #endif /* !COMPARE_ONTHEFLY */
1207 #if !COMPARE_ONTHEFLY
1208 static char tmpfilename
[MAXPATHLEN
];
1212 comparefile(char *name
)
1217 #if !COMPARE_ONTHEFLY
1218 static char *tmpfile
= NULL
;
1222 if ((r
= LSTAT(name
, &sb
)) != 0) {
1223 warn("%s: does not exist (%d)", name
, r
);
1229 curfile
.name
= name
;
1230 curfile
.action
= USING
;
1231 mode
= curfile
.dip
->di_mode
;
1233 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1234 (long)sb
.st_size
, mode
);
1236 if (sb
.st_mode
!= mode
) {
1237 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1238 name
, mode
& 07777, sb
.st_mode
& 07777);
1241 switch (mode
& IFMT
) {
1255 char lbuf
[MAXPATHLEN
+ 1];
1258 if (!(sb
.st_mode
& S_IFLNK
)) {
1259 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1266 getfile(xtrlnkfile
, xtrlnkskip
);
1269 "%s: zero length symbolic link (ignored)\n",
1274 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1275 panic("readlink of %s failed: %s", name
,
1280 if (strcmp(lbuf
, lnkbuf
) != 0) {
1282 "%s: symbolic link changed from %s to %s.\n",
1283 name
, lnkbuf
, lbuf
);
1292 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1293 fprintf(stderr
, "%s: no longer a special file\n",
1300 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1302 "%s: device changed from %d,%d to %d,%d.\n",
1304 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1305 (int)curfile
.dip
->di_rdev
& 0xff,
1306 ((int)sb
.st_rdev
>> 8) & 0xff,
1307 (int)sb
.st_rdev
& 0xff);
1314 #if COMPARE_ONTHEFLY
1315 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1316 panic("Can't open %s: %s\n", name
, strerror(errno
));
1322 getfile(xtrcmpfile
, xtrcmpskip
);
1325 if (read(ifile
, &c
, 1) != 0) {
1326 fprintf(stderr
, "%s: size has changed.\n",
1336 if (tmpfile
== NULL
) {
1337 /* argument to mktemp() must not be in RO space: */
1338 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1339 tmpfile
= mktemp(&tmpfilename
[0]);
1341 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1342 panic("cannot delete tmp file %s: %s\n",
1343 tmpfile
, strerror(errno
));
1345 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1346 panic("cannot create file temp file %s: %s\n",
1347 name
, strerror(errno
));
1349 getfile(xtrfile
, xtrskip
);
1350 (void) close(ofile
);
1351 #ifdef COMPARE_FAIL_KEEP_FILE
1352 if (cmpfiles(tmpfile
, name
, &sb
))
1355 cmpfiles(tmpfile
, name
, &sb
);
1358 #endif /* COMPARE_ONTHEFLY */
1364 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1365 static void (*readtape_func
)(char *) = readtape_set
;
1368 * Read TP_BSIZE blocks from the input.
1369 * Handle read errors, and end of media.
1370 * Decompress compressed blocks.
1375 (*readtape_func
)(buf
); /* call the actual processing routine */
1379 * Set function pointer for readtape() routine. zflag and magtapein must
1380 * be correctly set before the first call to readtape().
1383 readtape_set(char *buf
)
1386 readtape_func
= readtape_uncompr
;
1390 readtape_func
= readtape_comprtape
;
1392 readtape_func
= readtape_comprfile
;
1397 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1400 * This is the original readtape(), it's used for reading uncompressed input.
1401 * Read TP_BSIZE blocks from the input.
1402 * Handle read errors, and end of media.
1405 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1406 readtape_uncompr(char *buf
)
1411 ssize_t rd
, newvol
, i
;
1412 int cnt
, seek_failed
;
1414 if (blkcnt
< numtrec
) {
1415 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1421 for (i
= 0; i
< ntrec
; i
++)
1422 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1425 cnt
= ntrec
* TP_BSIZE
;
1428 if (createtapeposflag
)
1429 (void)GetTapePos(&curtapepos
);
1434 i
= rmtread(&tapebuf
[rd
], cnt
);
1437 i
= read(mt
, &tapebuf
[rd
], cnt
);
1440 * Check for mid-tape short read error.
1441 * If found, skip rest of buffer and start with the next.
1443 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1444 Dprintf(stdout
, "mid-media short read error.\n");
1448 * Handle partial block read.
1450 if (pipein
&& i
== 0 && rd
> 0)
1452 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1461 * Short read. Process the blocks read.
1463 if (i
% TP_BSIZE
!= 0)
1465 "partial block read: %ld should be %ld\n",
1466 (long)i
, ntrec
* TP_BSIZE
);
1467 numtrec
= i
/ TP_BSIZE
;
1471 * Handle read error.
1474 fprintf(stderr
, "Tape read error while ");
1475 switch (curfile
.action
) {
1477 fprintf(stderr
, "trying to set up tape\n");
1480 fprintf(stderr
, "trying to resynchronize\n");
1483 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1486 fprintf(stderr
, "skipping over inode %lu\n",
1487 (unsigned long)curfile
.ino
);
1490 if (!yflag
&& !reply("continue"))
1492 i
= ntrec
* TP_BSIZE
;
1493 memset(tapebuf
, 0, (size_t)i
);
1496 seek_failed
= (rmtseek(i
, 1) < 0);
1499 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1502 warn("continuation failed");
1503 if (!yflag
&& !reply("assume end-of-tape and continue"))
1509 * Handle end of tape.
1512 Vprintf(stdout
, "End-of-tape encountered\n");
1521 if (rd
% TP_BSIZE
!= 0)
1522 panic("partial block read: %d should be %d\n",
1523 rd
, ntrec
* TP_BSIZE
);
1525 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1528 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1533 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1536 * Read a compressed format block from a file or pipe and uncompress it.
1537 * Attempt to handle read errors, and end of file.
1540 readtape_comprfile(char *buf
)
1542 long rl
, size
, i
, ret
;
1544 struct tapebuf
*tpb
;
1546 if (blkcnt
< numtrec
) {
1547 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1552 /* need to read the next block */
1554 for (i
= 0; i
< ntrec
; i
++)
1555 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1557 tpb
= (struct tapebuf
*) tapebuf
;
1559 /* read the block prefix */
1560 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1562 if (Vflag
&& (ret
== 0 || rl
< PREFIXSIZE
|| tpb
->length
== 0))
1569 if (size
> bufsize
) {
1570 /* something's wrong */
1571 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1574 tpb
->length
= bufsize
;
1576 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1580 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1581 if (tbufptr
== NULL
) {
1582 msg_read_error("File decompression error while");
1583 if (!yflag
&& !reply("continue"))
1585 memset(tapebuf
, 0, bufsize
);
1590 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1596 /* Errors while reading from a file or pipe are catastrophic. Since
1597 * there are no block boundaries, it's impossible to bypass the
1598 * block in error and find the start of the next block.
1601 /* It's possible to have multiple input files using -M
1602 * and -f file1,file2...
1604 Vprintf(stdout
, "End-of-File encountered\n");
1614 msg_read_error("Read error while");
1615 /* if (!yflag && !reply("continue")) */
1620 * Read compressed data from a tape and uncompress it.
1621 * Handle read errors, and end of media.
1622 * Since a tape consists of separate physical blocks, we try
1623 * to recover from errors by repositioning the tape to the next
1627 readtape_comprtape(char *buf
)
1631 struct tapebuf
*tpb
;
1634 if (blkcnt
< numtrec
) {
1635 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1640 /* need to read the next block */
1642 for (i
= 0; i
< ntrec
; i
++)
1643 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1645 tpb
= (struct tapebuf
*) tapebuf
;
1647 /* read the block */
1648 size
= bufsize
+ PREFIXSIZE
;
1649 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1653 tbufptr
= decompress_tapebuf(tpb
, rl
);
1654 if (tbufptr
== NULL
) {
1655 msg_read_error("Tape decompression error while");
1656 if (!yflag
&& !reply("continue"))
1658 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1664 /* Handle errors: EOT switches to the next volume, other errors
1665 * attempt to position the tape to the next block.
1668 Vprintf(stdout
, "End-of-tape encountered\n");
1677 msg_read_error("Tape read error while");
1678 if (!yflag
&& !reply("continue"))
1680 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1685 rl
= rmtioctl(MTFSR
, 1);
1691 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1695 warn("continuation failed");
1696 if (!yflag
&& !reply("assume end-of-tape and continue"))
1698 ret
= 0; /* end of tape */
1704 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1710 * Decompress a struct tapebuf into a buffer. readsize is the size read
1711 * from the tape/file and is used for error messages. Returns a pointer
1712 * to the location of the uncompressed buffer or NULL on errors.
1713 * Adjust numtrec and complain for a short block.
1716 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1718 /* If zflag is on, all blocks have a struct tapebuf prefix */
1719 /* zflag gets set in setup() from the dump header */
1720 int cresult
, blocklen
;
1721 unsigned long worklen
;
1723 unsigned int worklen2
;
1725 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1727 /* build a length error message */
1728 blocklen
= tpbin
->length
;
1729 if (readsize
< blocklen
+ PREFIXSIZE
)
1730 lengtherr
= "short";
1732 if (readsize
> blocklen
+ PREFIXSIZE
)
1737 if (tpbin
->compressed
) {
1738 /* uncompress whatever we read, if it fails, complain later */
1739 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1741 errx(1,"This restore version doesn't support zlib decompression");
1743 cresult
= uncompress(comprbuf
, &worklen
,
1744 tpbin
->buf
, blocklen
);
1750 reason
= "not enough memory";
1753 reason
= "buffer too small";
1756 reason
= "data error";
1761 if (cresult
== Z_OK
)
1765 #endif /* HAVE_ZLIB */
1767 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1769 errx(1,"This restore version doesn't support bzlib decompression");
1772 cresult
= BZ2_bzBuffToBuffDecompress(
1773 comprbuf
, &worklen2
,
1774 tpbin
->buf
, blocklen
, 0, 0);
1781 reason
= "not enough memory";
1783 case BZ_OUTBUFF_FULL
:
1784 reason
= "buffer too small";
1787 case BZ_DATA_ERROR_MAGIC
:
1788 case BZ_UNEXPECTED_EOF
:
1789 reason
= "data error";
1794 if (cresult
== BZ_OK
)
1798 #endif /* HAVE_BZLIB */
1802 output
= tpbin
->buf
;
1806 numtrec
= worklen
/ TP_BSIZE
;
1807 if (worklen
% TP_BSIZE
!= 0)
1808 reason
= "length mismatch";
1812 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1813 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1814 fprintf(stderr
, "decompression error, block %ld: %s\n",
1815 tpblksread
+1, reason
);
1823 * Print an error message for a read error.
1824 * This was exteracted from the original readtape().
1827 msg_read_error(char *m
)
1829 switch (curfile
.action
) {
1831 fprintf(stderr
, "%s trying to set up tape\n", m
);
1834 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1837 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1840 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1841 (unsigned long)curfile
.ino
);
1845 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1848 * Read the first block and get the blocksize from it. Test
1849 * for a compressed dump tape/file. setup() will make the final
1850 * determination by checking the compressed flag if gethead()
1851 * finds a valid header. The test here is necessary to offset the buffer
1852 * by the size of the compressed prefix. zflag is set here so that
1853 * readtape_set can set the correct function pointer for readtape().
1854 * Note that the first block of each tape/file is not compressed
1855 * and does not have a prefix.
1858 findtapeblksize(void)
1862 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1863 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1865 for (i
= 0; i
< ntrec
; i
++)
1866 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1870 * For a pipe or file, read in the first record. For a tape, read
1873 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1875 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1876 errx(1, "Tape read error on first record");
1879 * If the input is from a file or a pipe, we read TP_BSIZE
1880 * bytes looking for a dump header. If the dump is compressed
1881 * we need to read in the rest of the block, as determined
1882 * by c_ntrec in the dump header. The first block of the
1883 * dump is not compressed and does not have a prefix.
1886 if (spclpt
->c_type
== TS_TAPE
1887 && spclpt
->c_flags
& DR_COMPRESSED
) {
1888 /* It's a compressed dump file, read in the */
1889 /* rest of the block based on spclpt->c_ntrec. */
1890 if (spclpt
->c_ntrec
> ntrec
)
1891 errx(1, "Tape blocksize is too large, use "
1892 "\'-b %d\' ", spclpt
->c_ntrec
);
1893 ntrec
= spclpt
->c_ntrec
;
1894 len
= (ntrec
- 1) * TP_BSIZE
;
1898 /* read in the rest of the block based on bufsize */
1899 len
= bufsize
- TP_BSIZE
;
1901 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1902 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1903 errx(1,"Error reading dump file header");
1906 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1908 } /* if (!magtapein) */
1911 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1912 * If the value of ntrec is too large, we read less than
1913 * what we asked for; adjust the value of ntrec and test for
1914 * a compressed dump tape.
1917 if (i
% TP_BSIZE
!= 0) {
1918 /* may be old format compressed dump tape with a prefix */
1919 spclpt
= (struct s_spcl
*) tpb
->buf
;
1920 if (i
% TP_BSIZE
== PREFIXSIZE
1921 && tpb
->compressed
== 0
1922 && spclpt
->c_type
== TS_TAPE
1923 && spclpt
->c_flags
& DR_COMPRESSED
) {
1926 if (tpb
->length
> bufsize
)
1927 errx(1, "Tape blocksize is too large, use "
1928 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1931 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1934 ntrec
= i
/ TP_BSIZE
;
1935 if (spclpt
->c_type
== TS_TAPE
) {
1936 if (spclpt
->c_flags
& DR_COMPRESSED
)
1938 if (spclpt
->c_ntrec
> ntrec
)
1939 errx(1, "Tape blocksize is too large, use "
1940 "\'-b %d\' ", spclpt
->c_ntrec
);
1943 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1947 * Read a block of data handling all of the messy details.
1949 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1957 i
= rmtread(buf
, size
);
1960 i
= read(fd
, buf
, size
);
1963 break; /* EOD or error */
1966 break; /* block at a time for mt */
1969 *lengthread
= len
- size
;
1988 setmagtapein(void) {
1989 struct mtget mt_stat
;
1990 static int done
= 0;
1995 /* need to know if input is really from a tape */
2001 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2004 Vprintf(stdout
,"Input is from %s\n",
2005 magtapein
? "tape" :
2006 Vflag
? "multi-volume (no tape)" : "file/pipe");
2010 * Read the next block from the tape.
2011 * Check to see if it is one of several vintage headers.
2012 * If it is an old style header, convert it to a new style header.
2013 * If it is not any valid header, return an error.
2016 gethead(struct s_spcl
*buf
)
2024 char dummy
[TP_BSIZE
];
2031 u_int16_t c_inumber
;
2036 u_int16_t odi_nlink
;
2052 readtape((char *)buf
);
2053 if (buf
->c_magic
!= NFS_MAGIC
) {
2054 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2057 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2061 if (checksum((int *)buf
) == FAIL
)
2064 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
2067 readtape((char *)(&u_ospcl
.s_ospcl
));
2068 memset((char *)buf
, 0, (long)TP_BSIZE
);
2069 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2070 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2071 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2072 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2073 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2074 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2075 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2076 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2077 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2078 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2079 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2080 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2081 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2082 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2083 #if defined(__linux__) || defined(sunos)
2084 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2085 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2086 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2087 #else /* __linux__ || sunos */
2088 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2089 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2090 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2091 #endif /* __linux__ || sunos */
2092 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2093 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2094 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2095 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2097 buf
->c_magic
= NFS_MAGIC
;
2100 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2101 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2102 qcvt
.qval
= buf
->c_dinode
.di_size
;
2103 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2104 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2109 qcvt
.qval
= buf
->c_dinode
.di_size
;
2111 qcvt
.val
[1] = qcvt
.val
[0];
2113 buf
->c_dinode
.di_size
= qcvt
.qval
;
2117 switch (buf
->c_type
) {
2122 * Have to patch up missing information in bit map headers
2125 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2126 if (buf
->c_count
> TP_NINDIR
)
2129 for (i
= 0; i
< buf
->c_count
; i
++)
2134 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2139 if (buf
->c_flags
& DR_INODEINFO
) {
2140 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2142 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2151 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2155 * If we are restoring a filesystem with old format inodes,
2156 * copy the uid/gid to the new location.
2159 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2160 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2168 * Check that a header is where it belongs and predict the next header
2171 accthdr(struct s_spcl
*header
)
2173 static dump_ino_t previno
= 0x7fffffff;
2174 static int prevtype
;
2175 static long predict
;
2178 if (header
->c_type
== TS_TAPE
) {
2179 fprintf(stderr
, "Volume header (%s inode format) ",
2180 oldinofmt
? "old" : "new");
2181 if (header
->c_firstrec
)
2182 fprintf(stderr
, "begins with record %d",
2183 header
->c_firstrec
);
2184 fprintf(stderr
, "\n");
2185 previno
= 0x7fffffff;
2188 if (previno
== 0x7fffffff)
2192 fprintf(stderr
, "Dumped inodes map header");
2195 fprintf(stderr
, "Used inodes map header");
2198 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2201 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2204 fprintf(stderr
, "End of tape header");
2207 if (predict
!= blksread
- 1)
2208 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2209 predict
, blksread
- 1);
2210 fprintf(stderr
, "\n");
2213 if (header
->c_type
!= TS_END
)
2214 for (i
= 0; i
< header
->c_count
; i
++)
2215 if (readmapflag
|| header
->c_addr
[i
] != 0)
2219 prevtype
= header
->c_type
;
2220 previno
= header
->c_inumber
;
2224 * Find an inode header.
2225 * Complain if had to skip, and complain is set.
2228 findinode(struct s_spcl
*header
)
2230 static long skipcnt
= 0;
2234 curfile
.name
= "<name unknown>";
2235 curfile
.action
= UNKNOWN
;
2239 if (header
->c_magic
!= NFS_MAGIC
) {
2241 while (gethead(header
) == FAIL
||
2242 header
->c_date
!= dumpdate
)
2245 switch (header
->c_type
) {
2249 * Skip up to the beginning of the next record
2251 for (i
= 0; i
< header
->c_count
; i
++)
2252 if (header
->c_addr
[i
])
2254 while (gethead(header
) == FAIL
||
2255 header
->c_date
!= dumpdate
)
2260 curfile
.dip
= &header
->c_dinode
;
2261 curfile
.ino
= header
->c_inumber
;
2265 curfile
.ino
= maxino
;
2269 curfile
.name
= "<file removal list>";
2273 curfile
.name
= "<file dump list>";
2277 panic("unexpected tape header\n");
2281 panic("unknown tape header type %d\n", spcl
.c_type
);
2285 } while (header
->c_type
== TS_ADDR
);
2290 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2300 j
= sizeof(union u_spcl
) / sizeof(int);
2307 /* What happens if we want to read restore tapes
2308 for a 16bit int machine??? */
2314 if (i
!= CHECKSUM
) {
2315 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2316 (unsigned long)curfile
.ino
, curfile
.name
);
2326 #include <varargs.h>
2331 msg(const char *fmt
, ...)
2344 (void)vfprintf(stderr
, fmt
, ap
);
2347 #endif /* RRESTORE */
2350 swab16(u_char
*sp
, int n
)
2355 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2362 swab32(u_char
*sp
, int n
)
2367 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2368 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2375 swab64(u_char
*sp
, int n
)
2380 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2381 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2382 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2383 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2390 swabst(u_char
*cp
, u_char
*sp
)
2396 case '0': case '1': case '2': case '3': case '4':
2397 case '5': case '6': case '7': case '8': case '9':
2398 n
= (n
* 10) + (*cp
++ - '0');
2401 case 's': case 'w': case 'h':
2419 default: /* Any other character, like 'b' counts as byte. */
2433 swabst((u_char
*)"i", (u_char
*)&x
);
2441 swabst((u_char
*)"l", (u_char
*)&x
);
2448 * get the current position of the tape
2451 GetTapePos(long long *pos
)
2457 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2466 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2467 *pos
= (long long)mtpos
;
2470 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2476 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2477 (unsigned long)getpid(), err
, *pos
);
2483 typedef struct mt_pos
{
2486 } MTPosRec
, *MTPosPtr
;
2489 * go to specified position on tape
2492 GotoTapePos(long long pos
)
2498 err
= (rmtseek((long)pos
, SEEK_SET
) < 0);
2505 buf
.mt_count
= (int) pos
;
2506 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2509 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2515 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2516 (unsigned long)getpid(), err
, pos
);
2523 * read next data from tape to re-sync
2526 ReReadFromTape(void)
2530 if (gethead(&spcl
) == FAIL
) {
2532 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2540 ReReadInodeFromTape(dump_ino_t theino
)
2549 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
) && (cntloop
< 32));
2551 fprintf(stderr
, "%ld reads\n", cntloop
);
2552 if (cntloop
== 32) {
2553 fprintf(stderr
, "DEBUG: bufsize %d\n", bufsize
);
2554 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
2555 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
2561 #endif /* USE_QFA */
2564 RequestVol(long tnum
)