]>
git.wh0rd.org - dump.git/blob - restore/tape.c
3c9085bfece3e55b61c8493e4c63ef815343190a
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.56 2002/01/25 15:09:00 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
);
370 if (spcl
.c_type
!= TS_BITS
)
371 errx(1, "Cannot find file dump list");
372 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
373 if (map
== (char *)NULL
)
374 errx(1, "no memory for file dump list");
376 curfile
.action
= USING
;
377 getfile(xtrmap
, xtrmapskip
);
379 * If there may be whiteout entries on the tape, pretend that the
380 * whiteout inode exists, so that the whiteout entries can be
384 SETINO(WINO
, dumpmap
);
388 * Prompt user to load a new dump volume.
389 * "Nextvol" is the next suggested volume to use.
390 * This suggested volume is enforced when doing full
391 * or incremental restores, but can be overridden by
392 * the user when only extracting a subset of the files.
397 long newvol
= 0, wantnext
= 0, i
;
398 long saved_blksread
= 0, saved_tpblksread
= 0;
399 union u_spcl tmpspcl
;
400 # define tmpbuf tmpspcl.s_spcl
402 int haderror
= 0, bot_code
= 1;
410 panic("Changing volumes on pipe input?\n");
415 saved_blksread
= blksread
;
416 saved_tpblksread
= tpblksread
;
419 exit(1); /* pipes do not get a second chance */
420 if (aflag
|| curfile
.action
!= SKIP
) {
427 while (newvol
<= 0) {
428 if (tapesread
== 0) {
429 fprintf(stderr
, "%s%s%s%s%s",
430 "You have not read any tapes yet.\n",
431 "Unless you know which volume your",
432 " file(s) are on you should start\n",
433 "with the last volume and work",
434 " towards the first.\n");
436 fprintf(stderr
, "You have read volumes");
438 for (i
= 1; i
< 32; i
++)
439 if (tapesread
& (1 << i
)) {
440 fprintf(stderr
, "%s%ld", buf
, (long)i
);
443 fprintf(stderr
, "\n");
446 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
447 (void) fflush(stderr
);
448 (void) fgets(buf
, TP_BSIZE
, terminal
);
449 } while (!feof(terminal
) && buf
[0] == '\n');
452 if (!strcmp(buf
, "none\n")) {
459 "Volume numbers are positive numerics\n");
462 if (newvol
== volno
) {
463 tapesread
|= 1 << volno
;
468 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
469 magtape
[MAXPATHLEN
- 1] = '\0';
471 if (bot_script
&& !haderror
) {
472 msg("Launching %s\n", bot_script
);
473 bot_code
= system_command(bot_script
, magtape
, newvol
);
474 if (bot_code
!= 0 && bot_code
!= 1) {
475 msg("Restore aborted by the beginning of tape script\n");
479 if (haderror
|| (bot_code
&& !Mflag
)) {
481 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
482 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
483 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
484 (void) fflush(stderr
);
485 (void) fgets(buf
, TP_BSIZE
, terminal
);
488 if (!strcmp(buf
, "none\n")) {
492 if (buf
[0] != '\n') {
494 (void) strncpy(magtape
, buf
, sizeof(magtape
));
495 magtape
[sizeof(magtape
) - 1] = '\0';
496 if ((pos
= strchr(magtape
, '\n')))
497 magtape
[pos
- magtape
] = '\0';
502 mt
= rmtopen(magtape
, 0);
505 mt
= OPEN(magtape
, O_RDONLY
, 0);
508 fprintf(stderr
, "Cannot open %s\n", magtape
);
515 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
516 readtape_func
= readtape_set
;
522 if (gethead(&tmpbuf
) == FAIL
) {
523 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
524 fprintf(stderr
, "tape is not dump tape\n");
527 blksread
= saved_blksread
;
528 tpblksread
= saved_tpblksread
;
531 if (tmpbuf
.c_volume
!= volno
) {
532 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
535 blksread
= saved_blksread
;
536 tpblksread
= saved_tpblksread
;
539 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
540 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
541 ctime4(&tmpbuf
.c_date
));
542 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
545 blksread
= saved_blksread
;
546 tpblksread
= saved_tpblksread
;
549 tapesread
|= 1 << volno
;
550 blksread
= saved_blksread
;
551 tpblksread
= saved_tpblksread
;
553 * If continuing from the previous volume, skip over any
554 * blocks read already at the end of the previous volume.
556 * If coming to this volume at random, skip to the beginning
557 * of the next record.
560 fprintf(stderr
, "Dump tape is compressed.\n");
561 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
562 errx(1,"This restore version doesn't support decompression");
563 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
565 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
566 tpblksread
, (long)tmpbuf
.c_firstrec
);
567 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
569 tpblksread
= tmpbuf
.c_firstrec
;
570 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
572 } else if (tmpbuf
.c_firstrec
> 0 &&
573 tmpbuf
.c_firstrec
< tpblksread
- 1) {
575 * -1 since we've read the volume header
577 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
578 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
579 (long)i
, i
> 1 ? "s" : "");
584 if (curfile
.action
== USING
) {
586 panic("active file into volume 1\n");
590 * Skip up to the beginning of the next record
592 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
593 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
595 (void) gethead(&spcl
);
599 siglongjmp(restart
, 1);
604 * Handle unexpected EOF.
610 if (gettingfile
&& curfile
.action
== USING
) {
611 printf("Warning: %s %s\n",
612 "End-of-input encountered while extracting", curfile
.name
);
614 curfile
.name
= "<name unknown>";
615 curfile
.action
= UNKNOWN
;
617 curfile
.ino
= maxino
;
620 siglongjmp(restart
, 1);
625 * handle multiple dumps per tape by skipping forward to the
633 if (dumpnum
== 1 || volno
!= 1)
636 errx(1, "Cannot have multiple dumps on pipe input");
638 tcom
.mt_count
= dumpnum
- 1;
641 rmtioctl(MTFSF
, dumpnum
- 1);
644 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
651 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
652 fprintf(stdout
, "Dumped from: %s",
653 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
654 if (spcl
.c_host
[0] == '\0')
656 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
657 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
658 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
666 if (volinfo
[1] == ROOTINO
) {
667 printf("Starting inode numbers by volume:\n");
668 for (i
= 1; i
< TP_NINOS
&& volinfo
[i
] != 0; ++i
)
669 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
674 extractfile(char *name
)
678 struct timeval timep
[2];
682 curfile
.action
= USING
;
684 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
685 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
686 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
687 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
688 #else /* __linux__ */
689 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
690 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
691 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
692 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
693 #endif /* __linux__ */
694 mode
= curfile
.dip
->di_mode
;
695 flags
= curfile
.dip
->di_flags
;
696 switch (mode
& IFMT
) {
699 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
704 Vprintf(stdout
, "skipped socket %s\n", name
);
710 ep
= lookupname(name
);
711 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
712 panic("unextracted directory %s\n", name
);
716 Vprintf(stdout
, "extract file %s\n", name
);
717 return (genliteraldir(name
, curfile
.ino
));
722 uid_t luid
= curfile
.dip
->di_uid
;
723 gid_t lgid
= curfile
.dip
->di_gid
;
727 getfile(xtrlnkfile
, xtrlnkskip
);
730 "%s: zero length symbolic link (ignored)\n", name
);
733 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
736 (void) lchown(name
, luid
, lgid
);
742 Vprintf(stdout
, "extract fifo %s\n", name
);
749 if (mkfifo(name
, mode
) < 0) {
750 warn("%s: cannot create fifo", name
);
754 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
755 (void) chmod(name
, mode
);
758 (void) fsetflags(name
, flags
);
760 (void) chflags(name
, flags
);
768 Vprintf(stdout
, "extract special file %s\n", name
);
775 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
776 warn("%s: cannot create special file", name
);
780 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
781 (void) chmod(name
, mode
);
785 warn("%s: fsetflags called on a special file", name
);
786 (void) fsetflags(name
, flags
);
789 (void) chflags(name
, flags
);
796 Vprintf(stdout
, "extract file %s\n", name
);
803 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
805 warn("%s: cannot create file", name
);
809 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
810 (void) fchmod(ofile
, mode
);
813 (void) setflags(ofile
, flags
);
815 (void) fchflags(ofile
, flags
);
817 getfile(xtrfile
, xtrskip
);
826 * skip over bit maps on the tape
832 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
837 * skip over a file on the tape
843 curfile
.action
= SKIP
;
844 getfile(xtrnull
, xtrnull
);
848 * Extract a file from the tape.
849 * When an allocated block is found it is passed to the fill function;
850 * when an unallocated block (hole) is found, a zeroed buffer is passed
851 * to the skip function.
854 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
857 volatile int curblk
= 0;
858 volatile quad_t size
= spcl
.c_dinode
.di_size
;
859 volatile int last_write_was_hole
= 0;
860 quad_t origsize
= size
;
861 static char clearedbuf
[MAXBSIZE
];
862 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
865 if (spcl
.c_type
== TS_END
)
866 panic("ran off end of tape\n");
867 if (spcl
.c_magic
!= NFS_MAGIC
)
868 panic("not at beginning of a file\n");
869 if (!gettingfile
&& setjmp(restart
) != 0)
873 for (i
= 0; i
< spcl
.c_count
; i
++) {
874 if (readmapflag
|| spcl
.c_addr
[i
]) {
875 readtape(&buf
[curblk
++][0]);
876 if (curblk
== fssize
/ TP_BSIZE
) {
877 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
878 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
880 last_write_was_hole
= 0;
884 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
886 (curblk
- 1) * TP_BSIZE
+ size
));
889 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
891 last_write_was_hole
= 1;
893 if ((size
-= TP_BSIZE
) <= 0) {
894 for (i
++; i
< spcl
.c_count
; i
++)
895 if (readmapflag
|| spcl
.c_addr
[i
])
900 if (gethead(&spcl
) == GOOD
&& size
> 0) {
901 if (spcl
.c_type
== TS_ADDR
)
904 "Missing address (header) block for %s at %ld blocks\n",
905 curfile
.name
, (long)blksread
);
908 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
909 last_write_was_hole
= 0;
912 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
914 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
915 (*skip
)(clearedbuf
, skp
);
918 last_write_was_hole
= 1;
920 if (last_write_was_hole
) {
921 FTRUNCATE(ofile
, origsize
);
928 * Write out the next block of a file.
931 xtrfile(char *buf
, size_t size
)
936 if (write(ofile
, buf
, (int) size
) == -1)
937 err(1, "write error extracting inode %lu, name %s\nwrite",
938 (unsigned long)curfile
.ino
, curfile
.name
);
942 * Skip over a hole in a file.
946 xtrskip(char *buf
, size_t size
)
949 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
950 err(1, "seek error extracting inode %lu, name %s\nlseek",
951 (unsigned long)curfile
.ino
, curfile
.name
);
955 * Collect the next block of a symbolic link.
958 xtrlnkfile(char *buf
, size_t size
)
962 if (pathlen
> MAXPATHLEN
)
963 errx(1, "symbolic link name: %s->%s%s; too long %d",
964 curfile
.name
, lnkbuf
, buf
, pathlen
);
965 (void) strcat(lnkbuf
, buf
);
969 * Skip over a hole in a symbolic link (should never happen).
973 xtrlnkskip(char *buf
, size_t size
)
976 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
980 * Collect the next block of a bit map.
983 xtrmap(char *buf
, size_t size
)
986 memmove(map
, buf
, size
);
991 * Skip over a hole in a bit map (should never happen).
995 xtrmapskip(char *buf
, size_t size
)
998 panic("hole in map\n");
1003 * Noop, when an extraction function is not needed.
1007 xtrnull(char *buf
, size_t size
)
1013 #if COMPARE_ONTHEFLY
1015 * Compare the next block of a file.
1018 xtrcmpfile(char *buf
, size_t size
)
1020 static char cmpbuf
[MAXBSIZE
];
1025 if (read(ifile
, cmpbuf
, size
) != size
) {
1026 fprintf(stderr
, "%s: size has changed.\n",
1032 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1033 fprintf(stderr
, "%s: tape and disk copies are different\n",
1041 * Skip over a hole in a file.
1044 xtrcmpskip(char *buf
, size_t size
)
1046 static char cmpbuf
[MAXBSIZE
];
1052 if (read(ifile
, cmpbuf
, size
) != size
) {
1053 fprintf(stderr
, "%s: size has changed.\n",
1059 for (i
= 0; i
< size
; ++i
)
1060 if (cmpbuf
[i
] != '\0') {
1061 fprintf(stderr
, "%s: tape and disk copies are different\n",
1067 #endif /* COMPARE_ONTHEFLY */
1069 #if !COMPARE_ONTHEFLY
1071 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1073 static char buf_tape
[BUFSIZ
];
1074 static char buf_disk
[BUFSIZ
];
1079 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1080 close(fd_tape
), close(fd_disk
);
1081 panic("do_cmpfiles: unexpected EOF[1]");
1083 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1084 close(fd_tape
), close(fd_disk
);
1085 panic("do_cmpfiles: unexpected EOF[2]");
1087 if (n_tape
!= n_disk
) {
1088 close(fd_tape
), close(fd_disk
);
1089 panic("do_cmpfiles: sizes different!");
1091 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1097 /* for debugging compare problems */
1098 #undef COMPARE_FAIL_KEEP_FILE
1101 #ifdef COMPARE_FAIL_KEEP_FILE
1102 /* return true if tapefile should be unlinked after compare */
1107 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1109 struct STAT sbuf_tape
;
1110 int fd_tape
, fd_disk
;
1112 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1113 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1118 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1120 "%s: size changed from %ld to %ld.\n",
1121 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1123 #ifdef COMPARE_FAIL_KEEP_FILE
1130 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1131 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1134 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1136 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1140 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1141 fprintf(stderr
, "%s: tape and disk copies are different\n",
1146 #ifdef COMPARE_FAIL_KEEP_FILE
1147 /* rename the file to live in /tmp */
1148 /* rename `tapefile' to /tmp/<basename of diskfile> */
1150 char *p
= strrchr(diskfile
, '/');
1151 char newname
[MAXPATHLEN
];
1153 panic("can't find / in %s\n", diskfile
);
1155 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1156 if (rename(tapefile
, newname
)) {
1157 panic("rename from %s to %s failed: %s\n",
1161 fprintf(stderr
, "*** %s saved to %s\n",
1166 /* don't unlink the file (it's not there anymore */
1175 #ifdef COMPARE_FAIL_KEEP_FILE
1179 #endif /* !COMPARE_ONTHEFLY */
1181 #if !COMPARE_ONTHEFLY
1182 static char tmpfilename
[MAXPATHLEN
];
1186 comparefile(char *name
)
1191 #if !COMPARE_ONTHEFLY
1192 static char *tmpfile
= NULL
;
1196 if ((r
= LSTAT(name
, &sb
)) != 0) {
1197 warn("%s: does not exist (%d)", name
, r
);
1203 curfile
.name
= name
;
1204 curfile
.action
= USING
;
1205 mode
= curfile
.dip
->di_mode
;
1207 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1208 (long)sb
.st_size
, mode
);
1210 if (sb
.st_mode
!= mode
) {
1211 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1212 name
, mode
& 07777, sb
.st_mode
& 07777);
1215 switch (mode
& IFMT
) {
1229 char lbuf
[MAXPATHLEN
+ 1];
1232 if (!(sb
.st_mode
& S_IFLNK
)) {
1233 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1240 getfile(xtrlnkfile
, xtrlnkskip
);
1243 "%s: zero length symbolic link (ignored)\n",
1248 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1249 panic("readlink of %s failed: %s", name
,
1254 if (strcmp(lbuf
, lnkbuf
) != 0) {
1256 "%s: symbolic link changed from %s to %s.\n",
1257 name
, lnkbuf
, lbuf
);
1266 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1267 fprintf(stderr
, "%s: no longer a special file\n",
1274 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1276 "%s: device changed from %d,%d to %d,%d.\n",
1278 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1279 (int)curfile
.dip
->di_rdev
& 0xff,
1280 ((int)sb
.st_rdev
>> 8) & 0xff,
1281 (int)sb
.st_rdev
& 0xff);
1288 #if COMPARE_ONTHEFLY
1289 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1290 panic("Can't open %s: %s\n", name
, strerror(errno
));
1296 getfile(xtrcmpfile
, xtrcmpskip
);
1299 if (read(ifile
, &c
, 1) != 0) {
1300 fprintf(stderr
, "%s: size has changed.\n",
1310 if (tmpfile
== NULL
) {
1311 /* argument to mktemp() must not be in RO space: */
1312 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1313 tmpfile
= mktemp(&tmpfilename
[0]);
1315 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1316 panic("cannot delete tmp file %s: %s\n",
1317 tmpfile
, strerror(errno
));
1319 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1320 panic("cannot create file temp file %s: %s\n",
1321 name
, strerror(errno
));
1323 getfile(xtrfile
, xtrskip
);
1324 (void) close(ofile
);
1325 #ifdef COMPARE_FAIL_KEEP_FILE
1326 if (cmpfiles(tmpfile
, name
, &sb
))
1329 cmpfiles(tmpfile
, name
, &sb
);
1332 #endif /* COMPARE_ONTHEFLY */
1338 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1339 static void (*readtape_func
)(char *) = readtape_set
;
1342 * Read TP_BSIZE blocks from the input.
1343 * Handle read errors, and end of media.
1344 * Decompress compressed blocks.
1349 (*readtape_func
)(buf
); /* call the actual processing routine */
1353 * Set function pointer for readtape() routine. zflag and magtapein must
1354 * be correctly set before the first call to readtape().
1357 readtape_set(char *buf
)
1360 readtape_func
= readtape_uncompr
;
1364 readtape_func
= readtape_comprtape
;
1366 readtape_func
= readtape_comprfile
;
1371 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1374 * This is the original readtape(), it's used for reading uncompressed input.
1375 * Read TP_BSIZE blocks from the input.
1376 * Handle read errors, and end of media.
1379 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1380 readtape_uncompr(char *buf
)
1385 ssize_t rd
, newvol
, i
;
1386 int cnt
, seek_failed
;
1388 if (blkcnt
< numtrec
) {
1389 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1395 for (i
= 0; i
< ntrec
; i
++)
1396 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1399 cnt
= ntrec
* TP_BSIZE
;
1404 i
= rmtread(&tapebuf
[rd
], cnt
);
1407 i
= read(mt
, &tapebuf
[rd
], cnt
);
1410 * Check for mid-tape short read error.
1411 * If found, skip rest of buffer and start with the next.
1413 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1414 Dprintf(stdout
, "mid-media short read error.\n");
1418 * Handle partial block read.
1420 if (pipein
&& i
== 0 && rd
> 0)
1422 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1431 * Short read. Process the blocks read.
1433 if (i
% TP_BSIZE
!= 0)
1435 "partial block read: %ld should be %ld\n",
1436 (long)i
, ntrec
* TP_BSIZE
);
1437 numtrec
= i
/ TP_BSIZE
;
1441 * Handle read error.
1444 fprintf(stderr
, "Tape read error while ");
1445 switch (curfile
.action
) {
1447 fprintf(stderr
, "trying to set up tape\n");
1450 fprintf(stderr
, "trying to resynchronize\n");
1453 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1456 fprintf(stderr
, "skipping over inode %lu\n",
1457 (unsigned long)curfile
.ino
);
1460 if (!yflag
&& !reply("continue"))
1462 i
= ntrec
* TP_BSIZE
;
1463 memset(tapebuf
, 0, (size_t)i
);
1466 seek_failed
= (rmtseek(i
, 1) < 0);
1469 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1472 warn("continuation failed");
1473 if (!yflag
&& !reply("assume end-of-tape and continue"))
1479 * Handle end of tape.
1482 Vprintf(stdout
, "End-of-tape encountered\n");
1491 if (rd
% TP_BSIZE
!= 0)
1492 panic("partial block read: %d should be %d\n",
1493 rd
, ntrec
* TP_BSIZE
);
1495 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1498 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1503 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1506 * Read a compressed format block from a file or pipe and uncompress it.
1507 * Attempt to handle read errors, and end of file.
1510 readtape_comprfile(char *buf
)
1512 long rl
, size
, i
, ret
;
1514 struct tapebuf
*tpb
;
1516 if (blkcnt
< numtrec
) {
1517 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1522 /* need to read the next block */
1524 for (i
= 0; i
< ntrec
; i
++)
1525 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1527 tpb
= (struct tapebuf
*) tapebuf
;
1529 /* read the block prefix */
1530 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1532 if (Vflag
&& (ret
== 0 || rl
< PREFIXSIZE
|| tpb
->length
== 0))
1539 if (size
> bufsize
) {
1540 /* something's wrong */
1541 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1544 tpb
->length
= bufsize
;
1546 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1550 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1551 if (tbufptr
== NULL
) {
1552 msg_read_error("File decompression error while");
1553 if (!yflag
&& !reply("continue"))
1555 memset(tapebuf
, 0, bufsize
);
1560 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1566 /* Errors while reading from a file or pipe are catastrophic. Since
1567 * there are no block boundaries, it's impossible to bypass the
1568 * block in error and find the start of the next block.
1571 /* It's possible to have multiple input files using -M
1572 * and -f file1,file2...
1574 Vprintf(stdout
, "End-of-File encountered\n");
1584 msg_read_error("Read error while");
1585 /* if (!yflag && !reply("continue")) */
1590 * Read compressed data from a tape and uncompress it.
1591 * Handle read errors, and end of media.
1592 * Since a tape consists of separate physical blocks, we try
1593 * to recover from errors by repositioning the tape to the next
1597 readtape_comprtape(char *buf
)
1601 struct tapebuf
*tpb
;
1604 if (blkcnt
< numtrec
) {
1605 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1610 /* need to read the next block */
1612 for (i
= 0; i
< ntrec
; i
++)
1613 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1615 tpb
= (struct tapebuf
*) tapebuf
;
1617 /* read the block */
1618 size
= bufsize
+ PREFIXSIZE
;
1619 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1623 tbufptr
= decompress_tapebuf(tpb
, rl
);
1624 if (tbufptr
== NULL
) {
1625 msg_read_error("Tape decompression error while");
1626 if (!yflag
&& !reply("continue"))
1628 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1634 /* Handle errors: EOT switches to the next volume, other errors
1635 * attempt to position the tape to the next block.
1638 Vprintf(stdout
, "End-of-tape encountered\n");
1647 msg_read_error("Tape read error while");
1648 if (!yflag
&& !reply("continue"))
1650 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1655 rl
= rmtioctl(MTFSR
, 1);
1661 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1665 warn("continuation failed");
1666 if (!yflag
&& !reply("assume end-of-tape and continue"))
1668 ret
= 0; /* end of tape */
1674 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1680 * Decompress a struct tapebuf into a buffer. readsize is the size read
1681 * from the tape/file and is used for error messages. Returns a pointer
1682 * to the location of the uncompressed buffer or NULL on errors.
1683 * Adjust numtrec and complain for a short block.
1686 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1688 /* If zflag is on, all blocks have a struct tapebuf prefix */
1689 /* zflag gets set in setup() from the dump header */
1690 int cresult
, blocklen
;
1691 unsigned long worklen
;
1693 unsigned int worklen2
;
1695 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1697 /* build a length error message */
1698 blocklen
= tpbin
->length
;
1699 if (readsize
< blocklen
+ PREFIXSIZE
)
1700 lengtherr
= "short";
1702 if (readsize
> blocklen
+ PREFIXSIZE
)
1707 if (tpbin
->compressed
) {
1708 /* uncompress whatever we read, if it fails, complain later */
1709 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1711 errx(1,"This restore version doesn't support zlib decompression");
1713 cresult
= uncompress(comprbuf
, &worklen
,
1714 tpbin
->buf
, blocklen
);
1720 reason
= "not enough memory";
1723 reason
= "buffer too small";
1726 reason
= "data error";
1731 if (cresult
== Z_OK
)
1735 #endif /* HAVE_ZLIB */
1737 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1739 errx(1,"This restore version doesn't support bzlib decompression");
1742 cresult
= BZ2_bzBuffToBuffDecompress(
1743 comprbuf
, &worklen2
,
1744 tpbin
->buf
, blocklen
, 0, 0);
1751 reason
= "not enough memory";
1753 case BZ_OUTBUFF_FULL
:
1754 reason
= "buffer too small";
1757 case BZ_DATA_ERROR_MAGIC
:
1758 case BZ_UNEXPECTED_EOF
:
1759 reason
= "data error";
1764 if (cresult
== BZ_OK
)
1768 #endif /* HAVE_BZLIB */
1772 output
= tpbin
->buf
;
1776 numtrec
= worklen
/ TP_BSIZE
;
1777 if (worklen
% TP_BSIZE
!= 0)
1778 reason
= "length mismatch";
1782 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1783 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1784 fprintf(stderr
, "decompression error, block %ld: %s\n",
1785 tpblksread
+1, reason
);
1793 * Print an error message for a read error.
1794 * This was exteracted from the original readtape().
1797 msg_read_error(char *m
)
1799 switch (curfile
.action
) {
1801 fprintf(stderr
, "%s trying to set up tape\n", m
);
1804 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1807 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1810 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1811 (unsigned long)curfile
.ino
);
1815 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1818 * Read the first block and get the blocksize from it. Test
1819 * for a compressed dump tape/file. setup() will make the final
1820 * determination by checking the compressed flag if gethead()
1821 * finds a valid header. The test here is necessary to offset the buffer
1822 * by the size of the compressed prefix. zflag is set here so that
1823 * readtape_set can set the correct function pointer for readtape().
1824 * Note that the first block of each tape/file is not compressed
1825 * and does not have a prefix.
1828 findtapeblksize(void)
1832 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1833 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1835 for (i
= 0; i
< ntrec
; i
++)
1836 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1840 * For a pipe or file, read in the first record. For a tape, read
1843 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1845 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1846 errx(1, "Tape read error on first record");
1849 * If the input is from a file or a pipe, we read TP_BSIZE
1850 * bytes looking for a dump header. If the dump is compressed
1851 * we need to read in the rest of the block, as determined
1852 * by c_ntrec in the dump header. The first block of the
1853 * dump is not compressed and does not have a prefix.
1856 if (spclpt
->c_type
== TS_TAPE
1857 && spclpt
->c_flags
& DR_COMPRESSED
) {
1858 /* It's a compressed dump file, read in the */
1859 /* rest of the block based on spclpt->c_ntrec. */
1860 if (spclpt
->c_ntrec
> ntrec
)
1861 errx(1, "Tape blocksize is too large, use "
1862 "\'-b %d\' ", spclpt
->c_ntrec
);
1863 ntrec
= spclpt
->c_ntrec
;
1864 len
= (ntrec
- 1) * TP_BSIZE
;
1868 /* read in the rest of the block based on bufsize */
1869 len
= bufsize
- TP_BSIZE
;
1871 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1872 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1873 errx(1,"Error reading dump file header");
1876 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1878 } /* if (!magtapein) */
1881 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1882 * If the value of ntrec is too large, we read less than
1883 * what we asked for; adjust the value of ntrec and test for
1884 * a compressed dump tape.
1887 if (i
% TP_BSIZE
!= 0) {
1888 /* may be old format compressed dump tape with a prefix */
1889 spclpt
= (struct s_spcl
*) tpb
->buf
;
1890 if (i
% TP_BSIZE
== PREFIXSIZE
1891 && tpb
->compressed
== 0
1892 && spclpt
->c_type
== TS_TAPE
1893 && spclpt
->c_flags
& DR_COMPRESSED
) {
1896 if (tpb
->length
> bufsize
)
1897 errx(1, "Tape blocksize is too large, use "
1898 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1901 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1904 ntrec
= i
/ TP_BSIZE
;
1905 if (spclpt
->c_type
== TS_TAPE
) {
1906 if (spclpt
->c_flags
& DR_COMPRESSED
)
1908 if (spclpt
->c_ntrec
> ntrec
)
1909 errx(1, "Tape blocksize is too large, use "
1910 "\'-b %d\' ", spclpt
->c_ntrec
);
1913 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1917 * Read a block of data handling all of the messy details.
1919 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1927 i
= rmtread(buf
, size
);
1930 i
= read(fd
, buf
, size
);
1933 break; /* EOD or error */
1936 break; /* block at a time for mt */
1939 *lengthread
= len
- size
;
1958 setmagtapein(void) {
1959 struct mtget mt_stat
;
1960 static int done
= 0;
1965 /* need to know if input is really from a tape */
1971 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
1974 Vprintf(stdout
,"Input is from %s\n",
1975 magtapein
? "tape" :
1976 Vflag
? "multi-volume (no tape)" : "file/pipe");
1980 * Read the next block from the tape.
1981 * Check to see if it is one of several vintage headers.
1982 * If it is an old style header, convert it to a new style header.
1983 * If it is not any valid header, return an error.
1986 gethead(struct s_spcl
*buf
)
1994 char dummy
[TP_BSIZE
];
2001 u_int16_t c_inumber
;
2006 u_int16_t odi_nlink
;
2022 readtape((char *)buf
);
2023 if (buf
->c_magic
!= NFS_MAGIC
) {
2024 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2027 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2031 if (checksum((int *)buf
) == FAIL
)
2034 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
2037 readtape((char *)(&u_ospcl
.s_ospcl
));
2038 memset((char *)buf
, 0, (long)TP_BSIZE
);
2039 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2040 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2041 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2042 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2043 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2044 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2045 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2046 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2047 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2048 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2049 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2050 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2051 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2052 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2054 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2055 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2056 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2057 #else /* __linux__ */
2058 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2059 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2060 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2061 #endif /* __linux__ */
2062 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2063 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2064 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2065 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2067 buf
->c_magic
= NFS_MAGIC
;
2070 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2071 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2072 qcvt
.qval
= buf
->c_dinode
.di_size
;
2073 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2074 printf("Note: Doing Quad swapping\n");
2079 qcvt
.qval
= buf
->c_dinode
.di_size
;
2081 qcvt
.val
[1] = qcvt
.val
[0];
2083 buf
->c_dinode
.di_size
= qcvt
.qval
;
2087 switch (buf
->c_type
) {
2092 * Have to patch up missing information in bit map headers
2095 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2096 if (buf
->c_count
> TP_NINDIR
)
2099 for (i
= 0; i
< buf
->c_count
; i
++)
2104 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2109 if (buf
->c_flags
& DR_INODEINFO
) {
2110 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2112 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2121 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2125 * If we are restoring a filesystem with old format inodes,
2126 * copy the uid/gid to the new location.
2129 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2130 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2138 * Check that a header is where it belongs and predict the next header
2141 accthdr(struct s_spcl
*header
)
2143 static dump_ino_t previno
= 0x7fffffff;
2144 static int prevtype
;
2145 static long predict
;
2148 if (header
->c_type
== TS_TAPE
) {
2149 fprintf(stderr
, "Volume header (%s inode format) ",
2150 oldinofmt
? "old" : "new");
2151 if (header
->c_firstrec
)
2152 fprintf(stderr
, "begins with record %d",
2153 header
->c_firstrec
);
2154 fprintf(stderr
, "\n");
2155 previno
= 0x7fffffff;
2158 if (previno
== 0x7fffffff)
2162 fprintf(stderr
, "Dumped inodes map header");
2165 fprintf(stderr
, "Used inodes map header");
2168 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2171 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2174 fprintf(stderr
, "End of tape header");
2177 if (predict
!= blksread
- 1)
2178 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2179 predict
, blksread
- 1);
2180 fprintf(stderr
, "\n");
2183 if (header
->c_type
!= TS_END
)
2184 for (i
= 0; i
< header
->c_count
; i
++)
2185 if (readmapflag
|| header
->c_addr
[i
] != 0)
2189 prevtype
= header
->c_type
;
2190 previno
= header
->c_inumber
;
2194 * Find an inode header.
2195 * Complain if had to skip, and complain is set.
2198 findinode(struct s_spcl
*header
)
2200 static long skipcnt
= 0;
2204 curfile
.name
= "<name unknown>";
2205 curfile
.action
= UNKNOWN
;
2209 if (header
->c_magic
!= NFS_MAGIC
) {
2211 while (gethead(header
) == FAIL
||
2212 header
->c_date
!= dumpdate
)
2215 switch (header
->c_type
) {
2219 * Skip up to the beginning of the next record
2221 for (i
= 0; i
< header
->c_count
; i
++)
2222 if (header
->c_addr
[i
])
2224 while (gethead(header
) == FAIL
||
2225 header
->c_date
!= dumpdate
)
2230 curfile
.dip
= &header
->c_dinode
;
2231 curfile
.ino
= header
->c_inumber
;
2235 curfile
.ino
= maxino
;
2239 curfile
.name
= "<file removal list>";
2243 curfile
.name
= "<file dump list>";
2247 panic("unexpected tape header\n");
2251 panic("unknown tape header type %d\n", spcl
.c_type
);
2255 } while (header
->c_type
== TS_ADDR
);
2260 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2270 j
= sizeof(union u_spcl
) / sizeof(int);
2277 /* What happens if we want to read restore tapes
2278 for a 16bit int machine??? */
2284 if (i
!= CHECKSUM
) {
2285 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2286 (unsigned long)curfile
.ino
, curfile
.name
);
2296 #include <varargs.h>
2301 msg(const char *fmt
, ...)
2314 (void)vfprintf(stderr
, fmt
, ap
);
2317 #endif /* RRESTORE */
2320 swab16(u_char
*sp
, int n
)
2325 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2332 swab32(u_char
*sp
, int n
)
2337 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2338 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2345 swab64(u_char
*sp
, int n
)
2350 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2351 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2352 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2353 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2360 swabst(u_char
*cp
, u_char
*sp
)
2366 case '0': case '1': case '2': case '3': case '4':
2367 case '5': case '6': case '7': case '8': case '9':
2368 n
= (n
* 10) + (*cp
++ - '0');
2371 case 's': case 'w': case 'h':
2389 default: /* Any other character, like 'b' counts as byte. */
2403 swabst((u_char
*)"i", (u_char
*)&x
);
2411 swabst((u_char
*)"l", (u_char
*)&x
);
2418 * get the current position of the tape
2421 GetTapePos(long long *pos
)
2427 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2436 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2437 *pos
= (long long)mtpos
;
2440 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2446 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2447 (unsigned long)getpid(), err
, *pos
);
2453 typedef struct mt_pos
{
2456 } MTPosRec
, *MTPosPtr
;
2459 * go to specified position on tape
2462 GotoTapePos(long long pos
)
2468 err
= (rmtseek((long)pos
, SEEK_SET
) < 0);
2475 buf
.mt_count
= (int) pos
;
2476 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2479 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2485 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2486 (unsigned long)getpid(), err
, pos
);
2493 * read next data from tape to re-sync
2496 ReReadFromTape(void)
2500 if (gethead(&spcl
) == FAIL
) {
2502 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2508 #endif /* USE_QFA */
2511 RequestVol(long tnum
)