]>
git.wh0rd.org - dump.git/blob - restore/tape.c
94ceab46fdfa9a706cac851acb3108936dd05fcf
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.57 2002/01/31 10:25:55 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')
335 if (filesys
[0] == '\0') {
337 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
338 filesys
[NAMELEN
- 1] = '\0';
339 dirptr
= strstr(filesys
, " (dir");
343 dumptime
= spcl
.c_ddate
;
344 dumpdate
= spcl
.c_date
;
345 if (STAT(".", &stbuf
) < 0)
346 err(1, "cannot stat .");
347 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
349 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
350 fssize
= stbuf
.st_blksize
;
351 if (((fssize
- 1) & fssize
) != 0)
352 errx(1, "bad block size %ld", fssize
);
353 if (spcl
.c_volume
!= 1)
354 errx(1, "Tape is not volume 1 of the dump");
355 if (gethead(&spcl
) == FAIL
) {
356 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
357 panic("no header after volume mark!\n");
360 if (spcl
.c_type
!= TS_CLRI
)
361 errx(1, "Cannot find file removal list");
362 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
363 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
364 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
366 errx(1, "no memory for active inode map");
368 curfile
.action
= USING
;
369 getfile(xtrmap
, xtrmapskip
);
371 if (spcl
.c_type
!= TS_BITS
)
372 errx(1, "Cannot find file dump list");
373 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
374 if (map
== (char *)NULL
)
375 errx(1, "no memory for file dump list");
377 curfile
.action
= USING
;
378 getfile(xtrmap
, xtrmapskip
);
380 * If there may be whiteout entries on the tape, pretend that the
381 * whiteout inode exists, so that the whiteout entries can be
385 SETINO(WINO
, dumpmap
);
390 * Prompt user to load a new dump volume.
391 * "Nextvol" is the next suggested volume to use.
392 * This suggested volume is enforced when doing full
393 * or incremental restores, but can be overridden by
394 * the user when only extracting a subset of the files.
399 long newvol
= 0, wantnext
= 0, i
;
400 long saved_blksread
= 0, saved_tpblksread
= 0;
401 union u_spcl tmpspcl
;
402 # define tmpbuf tmpspcl.s_spcl
404 int haderror
= 0, bot_code
= 1;
412 panic("Changing volumes on pipe input?\n");
417 saved_blksread
= blksread
;
418 saved_tpblksread
= tpblksread
;
421 exit(1); /* pipes do not get a second chance */
422 if (aflag
|| curfile
.action
!= SKIP
) {
429 while (newvol
<= 0) {
430 if (tapesread
== 0) {
431 fprintf(stderr
, "%s%s%s%s%s",
432 "You have not read any tapes yet.\n",
433 "Unless you know which volume your",
434 " file(s) are on you should start\n",
435 "with the last volume and work",
436 " towards the first.\n");
438 fprintf(stderr
, "You have read volumes");
440 for (i
= 1; i
< 32; i
++)
441 if (tapesread
& (1 << i
)) {
442 fprintf(stderr
, "%s%ld", buf
, (long)i
);
445 fprintf(stderr
, "\n");
448 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
449 (void) fflush(stderr
);
450 (void) fgets(buf
, TP_BSIZE
, terminal
);
451 } while (!feof(terminal
) && buf
[0] == '\n');
454 if (!strcmp(buf
, "none\n")) {
461 "Volume numbers are positive numerics\n");
464 if (newvol
== volno
) {
465 tapesread
|= 1 << volno
;
470 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
471 magtape
[MAXPATHLEN
- 1] = '\0';
473 if (bot_script
&& !haderror
) {
474 msg("Launching %s\n", bot_script
);
475 bot_code
= system_command(bot_script
, magtape
, newvol
);
476 if (bot_code
!= 0 && bot_code
!= 1) {
477 msg("Restore aborted by the beginning of tape script\n");
481 if (haderror
|| (bot_code
&& !Mflag
)) {
483 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
484 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
485 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
486 (void) fflush(stderr
);
487 (void) fgets(buf
, TP_BSIZE
, terminal
);
490 if (!strcmp(buf
, "none\n")) {
494 if (buf
[0] != '\n') {
496 (void) strncpy(magtape
, buf
, sizeof(magtape
));
497 magtape
[sizeof(magtape
) - 1] = '\0';
498 if ((pos
= strchr(magtape
, '\n')))
499 magtape
[pos
- magtape
] = '\0';
504 mt
= rmtopen(magtape
, 0);
507 mt
= OPEN(magtape
, O_RDONLY
, 0);
510 fprintf(stderr
, "Cannot open %s\n", magtape
);
517 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
518 readtape_func
= readtape_set
;
524 if (gethead(&tmpbuf
) == FAIL
) {
525 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
526 fprintf(stderr
, "tape is not dump tape\n");
529 blksread
= saved_blksread
;
530 tpblksread
= saved_tpblksread
;
533 if (tmpbuf
.c_volume
!= volno
) {
534 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
537 blksread
= saved_blksread
;
538 tpblksread
= saved_tpblksread
;
541 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
542 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
543 ctime4(&tmpbuf
.c_date
));
544 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
547 blksread
= saved_blksread
;
548 tpblksread
= saved_tpblksread
;
551 tapesread
|= 1 << volno
;
552 blksread
= saved_blksread
;
553 tpblksread
= saved_tpblksread
;
555 * If continuing from the previous volume, skip over any
556 * blocks read already at the end of the previous volume.
558 * If coming to this volume at random, skip to the beginning
559 * of the next record.
562 fprintf(stderr
, "Dump tape is compressed.\n");
563 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
564 errx(1,"This restore version doesn't support decompression");
565 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
567 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
568 tpblksread
, (long)tmpbuf
.c_firstrec
);
569 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
571 tpblksread
= tmpbuf
.c_firstrec
;
572 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
574 } else if (tmpbuf
.c_firstrec
> 0 &&
575 tmpbuf
.c_firstrec
< tpblksread
- 1) {
577 * -1 since we've read the volume header
579 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
580 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
581 (long)i
, i
> 1 ? "s" : "");
586 if (curfile
.action
== USING
) {
588 panic("active file into volume 1\n");
592 * Skip up to the beginning of the next record
594 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
595 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
597 (void) gethead(&spcl
);
601 siglongjmp(restart
, 1);
606 * Handle unexpected EOF.
612 if (gettingfile
&& curfile
.action
== USING
) {
613 printf("Warning: %s %s\n",
614 "End-of-input encountered while extracting", curfile
.name
);
616 curfile
.name
= "<name unknown>";
617 curfile
.action
= UNKNOWN
;
619 curfile
.ino
= maxino
;
622 siglongjmp(restart
, 1);
627 * handle multiple dumps per tape by skipping forward to the
635 if (dumpnum
== 1 || volno
!= 1)
638 errx(1, "Cannot have multiple dumps on pipe input");
640 tcom
.mt_count
= dumpnum
- 1;
643 rmtioctl(MTFSF
, dumpnum
- 1);
646 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
653 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
654 fprintf(stdout
, "Dumped from: %s",
655 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
656 if (spcl
.c_host
[0] == '\0')
658 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
659 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
660 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
668 if (volinfo
[1] == ROOTINO
) {
669 printf("Starting inode numbers by volume:\n");
670 for (i
= 1; i
< TP_NINOS
&& volinfo
[i
] != 0; ++i
)
671 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
676 extractfile(char *name
)
680 struct timeval timep
[2];
684 curfile
.action
= USING
;
686 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
687 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
688 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
689 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
690 #else /* __linux__ */
691 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
692 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
693 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
694 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
695 #endif /* __linux__ */
696 mode
= curfile
.dip
->di_mode
;
697 flags
= curfile
.dip
->di_flags
;
698 switch (mode
& IFMT
) {
701 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
706 Vprintf(stdout
, "skipped socket %s\n", name
);
712 ep
= lookupname(name
);
713 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
714 panic("unextracted directory %s\n", name
);
718 Vprintf(stdout
, "extract file %s\n", name
);
719 return (genliteraldir(name
, curfile
.ino
));
724 uid_t luid
= curfile
.dip
->di_uid
;
725 gid_t lgid
= curfile
.dip
->di_gid
;
729 getfile(xtrlnkfile
, xtrlnkskip
);
732 "%s: zero length symbolic link (ignored)\n", name
);
735 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
738 (void) lchown(name
, luid
, lgid
);
744 Vprintf(stdout
, "extract fifo %s\n", name
);
751 if (mkfifo(name
, mode
) < 0) {
752 warn("%s: cannot create fifo", name
);
756 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
757 (void) chmod(name
, mode
);
760 (void) fsetflags(name
, flags
);
762 (void) chflags(name
, flags
);
770 Vprintf(stdout
, "extract special file %s\n", name
);
777 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
778 warn("%s: cannot create special file", name
);
782 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
783 (void) chmod(name
, mode
);
787 warn("%s: fsetflags called on a special file", name
);
788 (void) fsetflags(name
, flags
);
791 (void) chflags(name
, flags
);
798 Vprintf(stdout
, "extract file %s\n", name
);
805 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
807 warn("%s: cannot create file", name
);
811 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
812 (void) fchmod(ofile
, mode
);
815 (void) setflags(ofile
, flags
);
817 (void) fchflags(ofile
, flags
);
819 getfile(xtrfile
, xtrskip
);
828 * skip over bit maps on the tape
834 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
839 * skip over a file on the tape
845 curfile
.action
= SKIP
;
846 getfile(xtrnull
, xtrnull
);
850 * Extract a file from the tape.
851 * When an allocated block is found it is passed to the fill function;
852 * when an unallocated block (hole) is found, a zeroed buffer is passed
853 * to the skip function.
856 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
859 volatile int curblk
= 0;
860 volatile quad_t size
= spcl
.c_dinode
.di_size
;
861 volatile int last_write_was_hole
= 0;
862 quad_t origsize
= size
;
863 static char clearedbuf
[MAXBSIZE
];
864 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
867 if (spcl
.c_type
== TS_END
)
868 panic("ran off end of tape\n");
869 if (spcl
.c_magic
!= NFS_MAGIC
)
870 panic("not at beginning of a file\n");
871 if (!gettingfile
&& setjmp(restart
) != 0)
875 for (i
= 0; i
< spcl
.c_count
; i
++) {
876 if (readmapflag
|| spcl
.c_addr
[i
]) {
877 readtape(&buf
[curblk
++][0]);
878 if (curblk
== fssize
/ TP_BSIZE
) {
879 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
880 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
882 last_write_was_hole
= 0;
886 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
888 (curblk
- 1) * TP_BSIZE
+ size
));
891 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
893 last_write_was_hole
= 1;
895 if ((size
-= TP_BSIZE
) <= 0) {
896 for (i
++; i
< spcl
.c_count
; i
++)
897 if (readmapflag
|| spcl
.c_addr
[i
])
902 if (gethead(&spcl
) == GOOD
&& size
> 0) {
903 if (spcl
.c_type
== TS_ADDR
)
906 "Missing address (header) block for %s at %ld blocks\n",
907 curfile
.name
, (long)blksread
);
910 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
911 last_write_was_hole
= 0;
914 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
916 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
917 (*skip
)(clearedbuf
, skp
);
920 last_write_was_hole
= 1;
922 if (last_write_was_hole
) {
923 FTRUNCATE(ofile
, origsize
);
930 * Write out the next block of a file.
933 xtrfile(char *buf
, size_t size
)
938 if (write(ofile
, buf
, (int) size
) == -1)
939 err(1, "write error extracting inode %lu, name %s\nwrite",
940 (unsigned long)curfile
.ino
, curfile
.name
);
944 * Skip over a hole in a file.
948 xtrskip(char *buf
, size_t size
)
951 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
952 err(1, "seek error extracting inode %lu, name %s\nlseek",
953 (unsigned long)curfile
.ino
, curfile
.name
);
957 * Collect the next block of a symbolic link.
960 xtrlnkfile(char *buf
, size_t size
)
964 if (pathlen
> MAXPATHLEN
)
965 errx(1, "symbolic link name: %s->%s%s; too long %d",
966 curfile
.name
, lnkbuf
, buf
, pathlen
);
967 (void) strcat(lnkbuf
, buf
);
971 * Skip over a hole in a symbolic link (should never happen).
975 xtrlnkskip(char *buf
, size_t size
)
978 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
982 * Collect the next block of a bit map.
985 xtrmap(char *buf
, size_t size
)
988 memmove(map
, buf
, size
);
993 * Skip over a hole in a bit map (should never happen).
997 xtrmapskip(char *buf
, size_t size
)
1000 panic("hole in map\n");
1005 * Noop, when an extraction function is not needed.
1009 xtrnull(char *buf
, size_t size
)
1015 #if COMPARE_ONTHEFLY
1017 * Compare the next block of a file.
1020 xtrcmpfile(char *buf
, size_t size
)
1022 static char cmpbuf
[MAXBSIZE
];
1027 if (read(ifile
, cmpbuf
, size
) != size
) {
1028 fprintf(stderr
, "%s: size has changed.\n",
1034 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1035 fprintf(stderr
, "%s: tape and disk copies are different\n",
1043 * Skip over a hole in a file.
1046 xtrcmpskip(char *buf
, size_t size
)
1048 static char cmpbuf
[MAXBSIZE
];
1054 if (read(ifile
, cmpbuf
, size
) != size
) {
1055 fprintf(stderr
, "%s: size has changed.\n",
1061 for (i
= 0; i
< size
; ++i
)
1062 if (cmpbuf
[i
] != '\0') {
1063 fprintf(stderr
, "%s: tape and disk copies are different\n",
1069 #endif /* COMPARE_ONTHEFLY */
1071 #if !COMPARE_ONTHEFLY
1073 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1075 static char buf_tape
[BUFSIZ
];
1076 static char buf_disk
[BUFSIZ
];
1081 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1082 close(fd_tape
), close(fd_disk
);
1083 panic("do_cmpfiles: unexpected EOF[1]");
1085 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1086 close(fd_tape
), close(fd_disk
);
1087 panic("do_cmpfiles: unexpected EOF[2]");
1089 if (n_tape
!= n_disk
) {
1090 close(fd_tape
), close(fd_disk
);
1091 panic("do_cmpfiles: sizes different!");
1093 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1099 /* for debugging compare problems */
1100 #undef COMPARE_FAIL_KEEP_FILE
1103 #ifdef COMPARE_FAIL_KEEP_FILE
1104 /* return true if tapefile should be unlinked after compare */
1109 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1111 struct STAT sbuf_tape
;
1112 int fd_tape
, fd_disk
;
1114 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1115 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1120 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1122 "%s: size changed from %ld to %ld.\n",
1123 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1125 #ifdef COMPARE_FAIL_KEEP_FILE
1132 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1133 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1136 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1138 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1142 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1143 fprintf(stderr
, "%s: tape and disk copies are different\n",
1148 #ifdef COMPARE_FAIL_KEEP_FILE
1149 /* rename the file to live in /tmp */
1150 /* rename `tapefile' to /tmp/<basename of diskfile> */
1152 char *p
= strrchr(diskfile
, '/');
1153 char newname
[MAXPATHLEN
];
1155 panic("can't find / in %s\n", diskfile
);
1157 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1158 if (rename(tapefile
, newname
)) {
1159 panic("rename from %s to %s failed: %s\n",
1163 fprintf(stderr
, "*** %s saved to %s\n",
1168 /* don't unlink the file (it's not there anymore */
1177 #ifdef COMPARE_FAIL_KEEP_FILE
1181 #endif /* !COMPARE_ONTHEFLY */
1183 #if !COMPARE_ONTHEFLY
1184 static char tmpfilename
[MAXPATHLEN
];
1188 comparefile(char *name
)
1193 #if !COMPARE_ONTHEFLY
1194 static char *tmpfile
= NULL
;
1198 if ((r
= LSTAT(name
, &sb
)) != 0) {
1199 warn("%s: does not exist (%d)", name
, r
);
1205 curfile
.name
= name
;
1206 curfile
.action
= USING
;
1207 mode
= curfile
.dip
->di_mode
;
1209 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1210 (long)sb
.st_size
, mode
);
1212 if (sb
.st_mode
!= mode
) {
1213 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1214 name
, mode
& 07777, sb
.st_mode
& 07777);
1217 switch (mode
& IFMT
) {
1231 char lbuf
[MAXPATHLEN
+ 1];
1234 if (!(sb
.st_mode
& S_IFLNK
)) {
1235 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1242 getfile(xtrlnkfile
, xtrlnkskip
);
1245 "%s: zero length symbolic link (ignored)\n",
1250 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1251 panic("readlink of %s failed: %s", name
,
1256 if (strcmp(lbuf
, lnkbuf
) != 0) {
1258 "%s: symbolic link changed from %s to %s.\n",
1259 name
, lnkbuf
, lbuf
);
1268 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1269 fprintf(stderr
, "%s: no longer a special file\n",
1276 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1278 "%s: device changed from %d,%d to %d,%d.\n",
1280 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1281 (int)curfile
.dip
->di_rdev
& 0xff,
1282 ((int)sb
.st_rdev
>> 8) & 0xff,
1283 (int)sb
.st_rdev
& 0xff);
1290 #if COMPARE_ONTHEFLY
1291 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1292 panic("Can't open %s: %s\n", name
, strerror(errno
));
1298 getfile(xtrcmpfile
, xtrcmpskip
);
1301 if (read(ifile
, &c
, 1) != 0) {
1302 fprintf(stderr
, "%s: size has changed.\n",
1312 if (tmpfile
== NULL
) {
1313 /* argument to mktemp() must not be in RO space: */
1314 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1315 tmpfile
= mktemp(&tmpfilename
[0]);
1317 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1318 panic("cannot delete tmp file %s: %s\n",
1319 tmpfile
, strerror(errno
));
1321 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1322 panic("cannot create file temp file %s: %s\n",
1323 name
, strerror(errno
));
1325 getfile(xtrfile
, xtrskip
);
1326 (void) close(ofile
);
1327 #ifdef COMPARE_FAIL_KEEP_FILE
1328 if (cmpfiles(tmpfile
, name
, &sb
))
1331 cmpfiles(tmpfile
, name
, &sb
);
1334 #endif /* COMPARE_ONTHEFLY */
1340 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1341 static void (*readtape_func
)(char *) = readtape_set
;
1344 * Read TP_BSIZE blocks from the input.
1345 * Handle read errors, and end of media.
1346 * Decompress compressed blocks.
1351 (*readtape_func
)(buf
); /* call the actual processing routine */
1355 * Set function pointer for readtape() routine. zflag and magtapein must
1356 * be correctly set before the first call to readtape().
1359 readtape_set(char *buf
)
1362 readtape_func
= readtape_uncompr
;
1366 readtape_func
= readtape_comprtape
;
1368 readtape_func
= readtape_comprfile
;
1373 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1376 * This is the original readtape(), it's used for reading uncompressed input.
1377 * Read TP_BSIZE blocks from the input.
1378 * Handle read errors, and end of media.
1381 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1382 readtape_uncompr(char *buf
)
1387 ssize_t rd
, newvol
, i
;
1388 int cnt
, seek_failed
;
1390 if (blkcnt
< numtrec
) {
1391 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1397 for (i
= 0; i
< ntrec
; i
++)
1398 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1401 cnt
= ntrec
* TP_BSIZE
;
1406 i
= rmtread(&tapebuf
[rd
], cnt
);
1409 i
= read(mt
, &tapebuf
[rd
], cnt
);
1412 * Check for mid-tape short read error.
1413 * If found, skip rest of buffer and start with the next.
1415 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1416 Dprintf(stdout
, "mid-media short read error.\n");
1420 * Handle partial block read.
1422 if (pipein
&& i
== 0 && rd
> 0)
1424 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1433 * Short read. Process the blocks read.
1435 if (i
% TP_BSIZE
!= 0)
1437 "partial block read: %ld should be %ld\n",
1438 (long)i
, ntrec
* TP_BSIZE
);
1439 numtrec
= i
/ TP_BSIZE
;
1443 * Handle read error.
1446 fprintf(stderr
, "Tape read error while ");
1447 switch (curfile
.action
) {
1449 fprintf(stderr
, "trying to set up tape\n");
1452 fprintf(stderr
, "trying to resynchronize\n");
1455 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1458 fprintf(stderr
, "skipping over inode %lu\n",
1459 (unsigned long)curfile
.ino
);
1462 if (!yflag
&& !reply("continue"))
1464 i
= ntrec
* TP_BSIZE
;
1465 memset(tapebuf
, 0, (size_t)i
);
1468 seek_failed
= (rmtseek(i
, 1) < 0);
1471 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1474 warn("continuation failed");
1475 if (!yflag
&& !reply("assume end-of-tape and continue"))
1481 * Handle end of tape.
1484 Vprintf(stdout
, "End-of-tape encountered\n");
1493 if (rd
% TP_BSIZE
!= 0)
1494 panic("partial block read: %d should be %d\n",
1495 rd
, ntrec
* TP_BSIZE
);
1497 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1500 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1505 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1508 * Read a compressed format block from a file or pipe and uncompress it.
1509 * Attempt to handle read errors, and end of file.
1512 readtape_comprfile(char *buf
)
1514 long rl
, size
, i
, ret
;
1516 struct tapebuf
*tpb
;
1518 if (blkcnt
< numtrec
) {
1519 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1524 /* need to read the next block */
1526 for (i
= 0; i
< ntrec
; i
++)
1527 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1529 tpb
= (struct tapebuf
*) tapebuf
;
1531 /* read the block prefix */
1532 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1534 if (Vflag
&& (ret
== 0 || rl
< PREFIXSIZE
|| tpb
->length
== 0))
1541 if (size
> bufsize
) {
1542 /* something's wrong */
1543 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1546 tpb
->length
= bufsize
;
1548 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1552 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1553 if (tbufptr
== NULL
) {
1554 msg_read_error("File decompression error while");
1555 if (!yflag
&& !reply("continue"))
1557 memset(tapebuf
, 0, bufsize
);
1562 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1568 /* Errors while reading from a file or pipe are catastrophic. Since
1569 * there are no block boundaries, it's impossible to bypass the
1570 * block in error and find the start of the next block.
1573 /* It's possible to have multiple input files using -M
1574 * and -f file1,file2...
1576 Vprintf(stdout
, "End-of-File encountered\n");
1586 msg_read_error("Read error while");
1587 /* if (!yflag && !reply("continue")) */
1592 * Read compressed data from a tape and uncompress it.
1593 * Handle read errors, and end of media.
1594 * Since a tape consists of separate physical blocks, we try
1595 * to recover from errors by repositioning the tape to the next
1599 readtape_comprtape(char *buf
)
1603 struct tapebuf
*tpb
;
1606 if (blkcnt
< numtrec
) {
1607 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1612 /* need to read the next block */
1614 for (i
= 0; i
< ntrec
; i
++)
1615 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1617 tpb
= (struct tapebuf
*) tapebuf
;
1619 /* read the block */
1620 size
= bufsize
+ PREFIXSIZE
;
1621 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1625 tbufptr
= decompress_tapebuf(tpb
, rl
);
1626 if (tbufptr
== NULL
) {
1627 msg_read_error("Tape decompression error while");
1628 if (!yflag
&& !reply("continue"))
1630 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1636 /* Handle errors: EOT switches to the next volume, other errors
1637 * attempt to position the tape to the next block.
1640 Vprintf(stdout
, "End-of-tape encountered\n");
1649 msg_read_error("Tape read error while");
1650 if (!yflag
&& !reply("continue"))
1652 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1657 rl
= rmtioctl(MTFSR
, 1);
1663 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1667 warn("continuation failed");
1668 if (!yflag
&& !reply("assume end-of-tape and continue"))
1670 ret
= 0; /* end of tape */
1676 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1682 * Decompress a struct tapebuf into a buffer. readsize is the size read
1683 * from the tape/file and is used for error messages. Returns a pointer
1684 * to the location of the uncompressed buffer or NULL on errors.
1685 * Adjust numtrec and complain for a short block.
1688 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1690 /* If zflag is on, all blocks have a struct tapebuf prefix */
1691 /* zflag gets set in setup() from the dump header */
1692 int cresult
, blocklen
;
1693 unsigned long worklen
;
1695 unsigned int worklen2
;
1697 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1699 /* build a length error message */
1700 blocklen
= tpbin
->length
;
1701 if (readsize
< blocklen
+ PREFIXSIZE
)
1702 lengtherr
= "short";
1704 if (readsize
> blocklen
+ PREFIXSIZE
)
1709 if (tpbin
->compressed
) {
1710 /* uncompress whatever we read, if it fails, complain later */
1711 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1713 errx(1,"This restore version doesn't support zlib decompression");
1715 cresult
= uncompress(comprbuf
, &worklen
,
1716 tpbin
->buf
, blocklen
);
1722 reason
= "not enough memory";
1725 reason
= "buffer too small";
1728 reason
= "data error";
1733 if (cresult
== Z_OK
)
1737 #endif /* HAVE_ZLIB */
1739 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1741 errx(1,"This restore version doesn't support bzlib decompression");
1744 cresult
= BZ2_bzBuffToBuffDecompress(
1745 comprbuf
, &worklen2
,
1746 tpbin
->buf
, blocklen
, 0, 0);
1753 reason
= "not enough memory";
1755 case BZ_OUTBUFF_FULL
:
1756 reason
= "buffer too small";
1759 case BZ_DATA_ERROR_MAGIC
:
1760 case BZ_UNEXPECTED_EOF
:
1761 reason
= "data error";
1766 if (cresult
== BZ_OK
)
1770 #endif /* HAVE_BZLIB */
1774 output
= tpbin
->buf
;
1778 numtrec
= worklen
/ TP_BSIZE
;
1779 if (worklen
% TP_BSIZE
!= 0)
1780 reason
= "length mismatch";
1784 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1785 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1786 fprintf(stderr
, "decompression error, block %ld: %s\n",
1787 tpblksread
+1, reason
);
1795 * Print an error message for a read error.
1796 * This was exteracted from the original readtape().
1799 msg_read_error(char *m
)
1801 switch (curfile
.action
) {
1803 fprintf(stderr
, "%s trying to set up tape\n", m
);
1806 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1809 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1812 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1813 (unsigned long)curfile
.ino
);
1817 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1820 * Read the first block and get the blocksize from it. Test
1821 * for a compressed dump tape/file. setup() will make the final
1822 * determination by checking the compressed flag if gethead()
1823 * finds a valid header. The test here is necessary to offset the buffer
1824 * by the size of the compressed prefix. zflag is set here so that
1825 * readtape_set can set the correct function pointer for readtape().
1826 * Note that the first block of each tape/file is not compressed
1827 * and does not have a prefix.
1830 findtapeblksize(void)
1834 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1835 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1837 for (i
= 0; i
< ntrec
; i
++)
1838 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1842 * For a pipe or file, read in the first record. For a tape, read
1845 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1847 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1848 errx(1, "Tape read error on first record");
1851 * If the input is from a file or a pipe, we read TP_BSIZE
1852 * bytes looking for a dump header. If the dump is compressed
1853 * we need to read in the rest of the block, as determined
1854 * by c_ntrec in the dump header. The first block of the
1855 * dump is not compressed and does not have a prefix.
1858 if (spclpt
->c_type
== TS_TAPE
1859 && spclpt
->c_flags
& DR_COMPRESSED
) {
1860 /* It's a compressed dump file, read in the */
1861 /* rest of the block based on spclpt->c_ntrec. */
1862 if (spclpt
->c_ntrec
> ntrec
)
1863 errx(1, "Tape blocksize is too large, use "
1864 "\'-b %d\' ", spclpt
->c_ntrec
);
1865 ntrec
= spclpt
->c_ntrec
;
1866 len
= (ntrec
- 1) * TP_BSIZE
;
1870 /* read in the rest of the block based on bufsize */
1871 len
= bufsize
- TP_BSIZE
;
1873 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1874 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1875 errx(1,"Error reading dump file header");
1878 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1880 } /* if (!magtapein) */
1883 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1884 * If the value of ntrec is too large, we read less than
1885 * what we asked for; adjust the value of ntrec and test for
1886 * a compressed dump tape.
1889 if (i
% TP_BSIZE
!= 0) {
1890 /* may be old format compressed dump tape with a prefix */
1891 spclpt
= (struct s_spcl
*) tpb
->buf
;
1892 if (i
% TP_BSIZE
== PREFIXSIZE
1893 && tpb
->compressed
== 0
1894 && spclpt
->c_type
== TS_TAPE
1895 && spclpt
->c_flags
& DR_COMPRESSED
) {
1898 if (tpb
->length
> bufsize
)
1899 errx(1, "Tape blocksize is too large, use "
1900 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1903 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1906 ntrec
= i
/ TP_BSIZE
;
1907 if (spclpt
->c_type
== TS_TAPE
) {
1908 if (spclpt
->c_flags
& DR_COMPRESSED
)
1910 if (spclpt
->c_ntrec
> ntrec
)
1911 errx(1, "Tape blocksize is too large, use "
1912 "\'-b %d\' ", spclpt
->c_ntrec
);
1915 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1919 * Read a block of data handling all of the messy details.
1921 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1929 i
= rmtread(buf
, size
);
1932 i
= read(fd
, buf
, size
);
1935 break; /* EOD or error */
1938 break; /* block at a time for mt */
1941 *lengthread
= len
- size
;
1960 setmagtapein(void) {
1961 struct mtget mt_stat
;
1962 static int done
= 0;
1967 /* need to know if input is really from a tape */
1973 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
1976 Vprintf(stdout
,"Input is from %s\n",
1977 magtapein
? "tape" :
1978 Vflag
? "multi-volume (no tape)" : "file/pipe");
1982 * Read the next block from the tape.
1983 * Check to see if it is one of several vintage headers.
1984 * If it is an old style header, convert it to a new style header.
1985 * If it is not any valid header, return an error.
1988 gethead(struct s_spcl
*buf
)
1996 char dummy
[TP_BSIZE
];
2003 u_int16_t c_inumber
;
2008 u_int16_t odi_nlink
;
2024 readtape((char *)buf
);
2025 if (buf
->c_magic
!= NFS_MAGIC
) {
2026 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2029 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2033 if (checksum((int *)buf
) == FAIL
)
2036 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
2039 readtape((char *)(&u_ospcl
.s_ospcl
));
2040 memset((char *)buf
, 0, (long)TP_BSIZE
);
2041 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2042 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2043 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2044 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2045 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2046 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2047 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2048 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2049 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2050 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2051 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2052 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2053 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2054 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2056 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2057 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2058 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2059 #else /* __linux__ */
2060 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2061 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2062 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2063 #endif /* __linux__ */
2064 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2065 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2066 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2067 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2069 buf
->c_magic
= NFS_MAGIC
;
2072 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2073 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2074 qcvt
.qval
= buf
->c_dinode
.di_size
;
2075 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2076 printf("Note: Doing Quad swapping\n");
2081 qcvt
.qval
= buf
->c_dinode
.di_size
;
2083 qcvt
.val
[1] = qcvt
.val
[0];
2085 buf
->c_dinode
.di_size
= qcvt
.qval
;
2089 switch (buf
->c_type
) {
2094 * Have to patch up missing information in bit map headers
2097 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2098 if (buf
->c_count
> TP_NINDIR
)
2101 for (i
= 0; i
< buf
->c_count
; i
++)
2106 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2111 if (buf
->c_flags
& DR_INODEINFO
) {
2112 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2114 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2123 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2127 * If we are restoring a filesystem with old format inodes,
2128 * copy the uid/gid to the new location.
2131 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2132 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2140 * Check that a header is where it belongs and predict the next header
2143 accthdr(struct s_spcl
*header
)
2145 static dump_ino_t previno
= 0x7fffffff;
2146 static int prevtype
;
2147 static long predict
;
2150 if (header
->c_type
== TS_TAPE
) {
2151 fprintf(stderr
, "Volume header (%s inode format) ",
2152 oldinofmt
? "old" : "new");
2153 if (header
->c_firstrec
)
2154 fprintf(stderr
, "begins with record %d",
2155 header
->c_firstrec
);
2156 fprintf(stderr
, "\n");
2157 previno
= 0x7fffffff;
2160 if (previno
== 0x7fffffff)
2164 fprintf(stderr
, "Dumped inodes map header");
2167 fprintf(stderr
, "Used inodes map header");
2170 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2173 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2176 fprintf(stderr
, "End of tape header");
2179 if (predict
!= blksread
- 1)
2180 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2181 predict
, blksread
- 1);
2182 fprintf(stderr
, "\n");
2185 if (header
->c_type
!= TS_END
)
2186 for (i
= 0; i
< header
->c_count
; i
++)
2187 if (readmapflag
|| header
->c_addr
[i
] != 0)
2191 prevtype
= header
->c_type
;
2192 previno
= header
->c_inumber
;
2196 * Find an inode header.
2197 * Complain if had to skip, and complain is set.
2200 findinode(struct s_spcl
*header
)
2202 static long skipcnt
= 0;
2206 curfile
.name
= "<name unknown>";
2207 curfile
.action
= UNKNOWN
;
2211 if (header
->c_magic
!= NFS_MAGIC
) {
2213 while (gethead(header
) == FAIL
||
2214 header
->c_date
!= dumpdate
)
2217 switch (header
->c_type
) {
2221 * Skip up to the beginning of the next record
2223 for (i
= 0; i
< header
->c_count
; i
++)
2224 if (header
->c_addr
[i
])
2226 while (gethead(header
) == FAIL
||
2227 header
->c_date
!= dumpdate
)
2232 curfile
.dip
= &header
->c_dinode
;
2233 curfile
.ino
= header
->c_inumber
;
2237 curfile
.ino
= maxino
;
2241 curfile
.name
= "<file removal list>";
2245 curfile
.name
= "<file dump list>";
2249 panic("unexpected tape header\n");
2253 panic("unknown tape header type %d\n", spcl
.c_type
);
2257 } while (header
->c_type
== TS_ADDR
);
2262 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2272 j
= sizeof(union u_spcl
) / sizeof(int);
2279 /* What happens if we want to read restore tapes
2280 for a 16bit int machine??? */
2286 if (i
!= CHECKSUM
) {
2287 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2288 (unsigned long)curfile
.ino
, curfile
.name
);
2298 #include <varargs.h>
2303 msg(const char *fmt
, ...)
2316 (void)vfprintf(stderr
, fmt
, ap
);
2319 #endif /* RRESTORE */
2322 swab16(u_char
*sp
, int n
)
2327 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2334 swab32(u_char
*sp
, int n
)
2339 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2340 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2347 swab64(u_char
*sp
, int n
)
2352 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2353 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2354 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2355 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2362 swabst(u_char
*cp
, u_char
*sp
)
2368 case '0': case '1': case '2': case '3': case '4':
2369 case '5': case '6': case '7': case '8': case '9':
2370 n
= (n
* 10) + (*cp
++ - '0');
2373 case 's': case 'w': case 'h':
2391 default: /* Any other character, like 'b' counts as byte. */
2405 swabst((u_char
*)"i", (u_char
*)&x
);
2413 swabst((u_char
*)"l", (u_char
*)&x
);
2420 * get the current position of the tape
2423 GetTapePos(long long *pos
)
2429 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2438 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2439 *pos
= (long long)mtpos
;
2442 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2448 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2449 (unsigned long)getpid(), err
, *pos
);
2455 typedef struct mt_pos
{
2458 } MTPosRec
, *MTPosPtr
;
2461 * go to specified position on tape
2464 GotoTapePos(long long pos
)
2470 err
= (rmtseek((long)pos
, SEEK_SET
) < 0);
2477 buf
.mt_count
= (int) pos
;
2478 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2481 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2487 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2488 (unsigned long)getpid(), err
, pos
);
2495 * read next data from tape to re-sync
2498 ReReadFromTape(void)
2502 if (gethead(&spcl
) == FAIL
) {
2504 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2510 #endif /* USE_QFA */
2513 RequestVol(long tnum
)