]>
git.wh0rd.org - dump.git/blob - restore/tape.c
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 static const char rcsid
[] =
49 "$Id: tape.c,v 1.59 2002/03/28 14:53:01 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
;
178 static int readingmaps
; /* set to 1 while reading the maps */
181 * Set up an input source. This is called from main.c before setup() is.
184 setinput(char *source
)
190 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
194 if (strchr(source
, ':')) {
196 source
= strchr(host
, ':');
198 if (rmthost(host
) == 0)
202 if (strcmp(source
, "-") == 0) {
204 * Since input is coming from a pipe we must establish
205 * our own connection to the terminal.
207 terminal
= fopen(_PATH_TTY
, "r");
208 if (terminal
== NULL
) {
209 warn("cannot open %s", _PATH_TTY
);
210 terminal
= fopen(_PATH_DEVNULL
, "r");
211 if (terminal
== NULL
)
212 err(1, "cannot open %s", _PATH_DEVNULL
);
216 setuid(getuid()); /* no longer need or want root privileges */
218 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
219 magtapeprefix
[MAXPATHLEN
-1] = '\0';
220 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
223 strncpy(magtape
, source
, MAXPATHLEN
);
224 magtape
[MAXPATHLEN
- 1] = '\0';
228 newtapebuf(long size
)
230 static int tapebufsize
= -1;
233 bufsize
= ntrec
* TP_BSIZE
;
234 if (size
<= tapebufsize
)
238 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
240 errx(1, "Cannot allocate space for tape buffer");
244 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
246 newcomprbuf(int size
)
248 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
249 if (buf_size
<= comprlen
)
252 if (comprbuf
!= NULL
)
254 comprbuf
= malloc(comprlen
);
255 if (comprbuf
== NULL
)
256 errx(1, "Cannot allocate space for decompress buffer");
258 #endif /* HAVE_ZLIB || HAVE_BZLIB */
261 * Verify that the tape drive can be accessed and
262 * that it actually is a dump tape.
267 int i
, j
, *ip
, bot_code
;
271 Vprintf(stdout
, "Verify tape and initialize maps\n");
272 if (Afile
== NULL
&& bot_script
) {
273 msg("Launching %s\n", bot_script
);
274 bot_code
= system_command(bot_script
, magtape
, 1);
275 if (bot_code
!= 0 && bot_code
!= 1) {
276 msg("Restore aborted by the beginning of tape script\n");
288 mt
= rmtopen(temptape
, 0);
294 mt
= OPEN(temptape
, O_RDONLY
, 0);
296 err(1, "%s", magtape
);
302 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
303 readtape_func
= readtape_set
;
307 if (gethead(&spcl
) == FAIL
) {
308 blkcnt
--; /* push back this block */
312 if (gethead(&spcl
) == FAIL
)
313 errx(1, "Tape is not a dump tape");
314 fprintf(stderr
, "Converting to new file system format.\n");
318 fprintf(stderr
, "Dump tape is compressed.\n");
319 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
320 errx(1,"This restore version doesn't support decompression");
321 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
324 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
325 endoftapemark
.s_spcl
.c_type
= TS_END
;
326 ip
= (int *)&endoftapemark
;
327 j
= sizeof(union u_spcl
) / sizeof(int);
332 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
334 if (vflag
|| command
== 't' || command
== 'C')
337 if (tapeposflag
&& spcl
.c_date
!= qfadumpdate
)
338 errx(1, "different QFA/dumpdates detected\n");
340 if (filesys
[0] == '\0') {
342 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
343 filesys
[NAMELEN
- 1] = '\0';
344 dirptr
= strstr(filesys
, " (dir");
348 dumptime
= spcl
.c_ddate
;
349 dumpdate
= spcl
.c_date
;
350 if (STAT(".", &stbuf
) < 0)
351 err(1, "cannot stat .");
352 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
354 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
355 fssize
= stbuf
.st_blksize
;
356 if (((fssize
- 1) & fssize
) != 0)
357 errx(1, "bad block size %ld", fssize
);
358 if (spcl
.c_volume
!= 1)
359 errx(1, "Tape is not volume 1 of the dump");
360 if (gethead(&spcl
) == FAIL
) {
361 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
362 panic("no header after volume mark!\n");
366 if (spcl
.c_type
!= TS_CLRI
)
367 errx(1, "Cannot find file removal list");
368 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
369 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
371 errx(1, "no memory for active inode map");
373 curfile
.action
= USING
;
374 getfile(xtrmap
, xtrmapskip
);
375 while (spcl
.c_type
== TS_ADDR
) {
376 /* Recompute maxino and the map */
377 char *oldmap
= usedinomap
;
378 dump_ino_t oldmaxino
= maxino
;
379 maxino
+= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
380 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
382 errx(1, "no memory for active inode map");
384 memcpy(usedinomap
, oldmap
, howmany(oldmaxino
, NBBY
));
387 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
388 getfile(xtrmap
, xtrmapskip
);
390 Dprintf(stdout
, "maxino = %lu\n", (unsigned long)maxino
);
391 if (spcl
.c_type
!= TS_BITS
)
392 errx(1, "Cannot find file dump list");
393 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
394 if (map
== (char *)NULL
)
395 errx(1, "no memory for file dump list");
397 curfile
.action
= USING
;
398 getfile(xtrmap
, xtrmapskip
);
399 while (spcl
.c_type
== TS_ADDR
) {
400 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
401 getfile(xtrmap
, xtrmapskip
);
404 * If there may be whiteout entries on the tape, pretend that the
405 * whiteout inode exists, so that the whiteout entries can be
409 SETINO(WINO
, dumpmap
);
415 * Prompt user to load a new dump volume.
416 * "Nextvol" is the next suggested volume to use.
417 * This suggested volume is enforced when doing full
418 * or incremental restores, but can be overridden by
419 * the user when only extracting a subset of the files.
424 long newvol
= 0, wantnext
= 0, i
;
425 long saved_blksread
= 0, saved_tpblksread
= 0;
426 union u_spcl tmpspcl
;
427 # define tmpbuf tmpspcl.s_spcl
429 int haderror
= 0, bot_code
= 1;
437 panic("Changing volumes on pipe input?\n");
442 saved_blksread
= blksread
;
443 saved_tpblksread
= tpblksread
;
444 #if defined(USE_QFA) && defined(sunos)
445 if (createtapeposflag
|| tapeposflag
)
450 exit(1); /* pipes do not get a second chance */
451 if (aflag
|| curfile
.action
!= SKIP
) {
458 while (newvol
<= 0) {
459 if (tapesread
== 0) {
460 fprintf(stderr
, "%s%s%s%s%s",
461 "You have not read any volumes yet.\n",
462 "Unless you know which volume your",
463 " file(s) are on you should start\n",
464 "with the last volume and work",
465 " towards the first.\n");
467 fprintf(stderr
, "You have read volumes");
469 for (i
= 1; i
< 32; i
++)
470 if (tapesread
& (1 << i
)) {
471 fprintf(stderr
, "%s%ld", buf
, (long)i
);
474 fprintf(stderr
, "\n");
477 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
478 (void) fflush(stderr
);
479 (void) fgets(buf
, TP_BSIZE
, terminal
);
480 } while (!feof(terminal
) && buf
[0] == '\n');
483 if (!strcmp(buf
, "none\n")) {
490 "Volume numbers are positive numerics\n");
493 if (newvol
== volno
) {
494 tapesread
|= 1 << volno
;
495 #if defined(USE_QFA) && defined(sunos)
496 if (createtapeposflag
|| tapeposflag
) {
497 if (OpenSMTCmt(magtape
) < 0) {
508 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
509 magtape
[MAXPATHLEN
- 1] = '\0';
511 if (bot_script
&& !haderror
) {
512 msg("Launching %s\n", bot_script
);
513 bot_code
= system_command(bot_script
, magtape
, newvol
);
514 if (bot_code
!= 0 && bot_code
!= 1) {
515 msg("Restore aborted by the beginning of tape script\n");
519 if (haderror
|| (bot_code
&& !Mflag
)) {
521 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
522 fprintf(stderr
, "Enter ``none'' if there are no more volumes\n");
523 fprintf(stderr
, "otherwise enter volume name (default: %s) ", magtape
);
524 (void) fflush(stderr
);
525 (void) fgets(buf
, TP_BSIZE
, terminal
);
528 if (!strcmp(buf
, "none\n")) {
532 if (buf
[0] != '\n') {
534 (void) strncpy(magtape
, buf
, sizeof(magtape
));
535 magtape
[sizeof(magtape
) - 1] = '\0';
536 if ((pos
= strchr(magtape
, '\n')))
537 magtape
[pos
- magtape
] = '\0';
542 mt
= rmtopen(magtape
, 0);
545 mt
= OPEN(magtape
, O_RDONLY
, 0);
548 fprintf(stderr
, "Cannot open %s\n", magtape
);
555 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
556 readtape_func
= readtape_set
;
562 if (gethead(&tmpbuf
) == FAIL
) {
563 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
564 fprintf(stderr
, "tape is not dump tape\n");
567 blksread
= saved_blksread
;
568 tpblksread
= saved_tpblksread
;
571 if (tmpbuf
.c_volume
!= volno
) {
572 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
575 blksread
= saved_blksread
;
576 tpblksread
= saved_tpblksread
;
579 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
580 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
581 ctime4(&tmpbuf
.c_date
));
582 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
585 blksread
= saved_blksread
;
586 tpblksread
= saved_tpblksread
;
589 tapesread
|= 1 << volno
;
590 blksread
= saved_blksread
;
591 tpblksread
= saved_tpblksread
;
593 * If continuing from the previous volume, skip over any
594 * blocks read already at the end of the previous volume.
596 * If coming to this volume at random, skip to the beginning
597 * of the next record.
600 fprintf(stderr
, "Dump tape is compressed.\n");
601 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
602 errx(1,"This restore version doesn't support decompression");
603 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
605 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
606 tpblksread
, (long)tmpbuf
.c_firstrec
);
607 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
609 tpblksread
= tmpbuf
.c_firstrec
;
610 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
612 } else if (tmpbuf
.c_firstrec
> 0 &&
613 tmpbuf
.c_firstrec
< tpblksread
- 1) {
615 * -1 since we've read the volume header
617 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
618 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
619 (long)i
, i
> 1 ? "s" : "");
624 if (curfile
.action
== USING
) {
626 panic("active file into volume 1\n");
630 * Skip up to the beginning of the next record
632 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
633 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
635 (void) gethead(&spcl
);
639 siglongjmp(restart
, 1);
644 * Handle unexpected EOF.
650 if (gettingfile
&& curfile
.action
== USING
) {
651 printf("Warning: %s %s\n",
652 "End-of-input encountered while extracting", curfile
.name
);
654 curfile
.name
= "<name unknown>";
655 curfile
.action
= UNKNOWN
;
657 curfile
.ino
= maxino
;
660 siglongjmp(restart
, 1);
665 * handle multiple dumps per tape by skipping forward to the
673 if (dumpnum
== 1 || volno
!= 1)
676 errx(1, "Cannot have multiple dumps on pipe input");
678 tcom
.mt_count
= dumpnum
- 1;
681 rmtioctl(MTFSF
, dumpnum
- 1);
684 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
691 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
692 fprintf(stdout
, "Dumped from: %s",
693 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
694 if (spcl
.c_host
[0] == '\0')
696 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
697 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
698 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
706 if (volinfo
[1] == ROOTINO
) {
707 printf("Starting inode numbers by volume:\n");
708 for (i
= 1; i
< TP_NINOS
&& volinfo
[i
] != 0; ++i
)
709 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
715 time_t tv_sec
; /* seconds */
716 suseconds_t tv_usec
; /* and microseconds */
721 extractfile(char *name
)
725 struct timeval timep
[2];
729 curfile
.action
= USING
;
730 #if defined(__linux__) || defined(sunos)
731 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
732 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
733 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
734 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
735 #else /* __linux__ || sunos */
736 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
737 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
738 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
739 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
740 #endif /* __linux__ */
741 mode
= curfile
.dip
->di_mode
;
742 flags
= curfile
.dip
->di_flags
;
743 switch (mode
& IFMT
) {
746 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
751 Vprintf(stdout
, "skipped socket %s\n", name
);
757 ep
= lookupname(name
);
758 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
759 panic("unextracted directory %s\n", name
);
763 Vprintf(stdout
, "extract file %s\n", name
);
764 return (genliteraldir(name
, curfile
.ino
));
769 uid_t luid
= curfile
.dip
->di_uid
;
770 gid_t lgid
= curfile
.dip
->di_gid
;
774 getfile(xtrlnkfile
, xtrlnkskip
);
777 "%s: zero length symbolic link (ignored)\n", name
);
780 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
783 (void) lchown(name
, luid
, lgid
);
789 Vprintf(stdout
, "extract fifo %s\n", name
);
796 if (mkfifo(name
, mode
) < 0) {
797 warn("%s: cannot create fifo", name
);
801 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
802 (void) chmod(name
, mode
);
805 (void) fsetflags(name
, flags
);
807 (void) chflags(name
, flags
);
815 Vprintf(stdout
, "extract special file %s\n", name
);
822 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
823 warn("%s: cannot create special file", name
);
827 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
828 (void) chmod(name
, mode
);
832 warn("%s: fsetflags called on a special file", name
);
833 (void) fsetflags(name
, flags
);
836 (void) chflags(name
, flags
);
843 Vprintf(stdout
, "extract file %s\n", name
);
850 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
852 warn("%s: cannot create file", name
);
856 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
857 (void) fchmod(ofile
, mode
);
860 (void) setflags(ofile
, flags
);
862 (void) fchflags(ofile
, flags
);
864 getfile(xtrfile
, xtrskip
);
873 * skip over bit maps on the tape
879 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
884 * skip over a file on the tape
890 curfile
.action
= SKIP
;
891 getfile(xtrnull
, xtrnull
);
895 * Extract a file from the tape.
896 * When an allocated block is found it is passed to the fill function;
897 * when an unallocated block (hole) is found, a zeroed buffer is passed
898 * to the skip function.
901 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
904 volatile int curblk
= 0;
905 volatile quad_t size
= spcl
.c_dinode
.di_size
;
906 volatile int last_write_was_hole
= 0;
907 quad_t origsize
= size
;
908 static char clearedbuf
[MAXBSIZE
];
909 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
912 if (spcl
.c_type
== TS_END
)
913 panic("ran off end of tape\n");
914 if (spcl
.c_magic
!= NFS_MAGIC
)
915 panic("not at beginning of a file\n");
916 if (!gettingfile
&& setjmp(restart
) != 0)
920 for (i
= 0; i
< spcl
.c_count
; i
++) {
921 if (readmapflag
|| spcl
.c_addr
[i
]) {
922 readtape(&buf
[curblk
++][0]);
923 if (curblk
== fssize
/ TP_BSIZE
) {
924 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
925 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
927 last_write_was_hole
= 0;
931 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
933 (curblk
- 1) * TP_BSIZE
+ size
));
936 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
938 last_write_was_hole
= 1;
940 if ((size
-= TP_BSIZE
) <= 0) {
941 for (i
++; i
< spcl
.c_count
; i
++)
942 if (readmapflag
|| spcl
.c_addr
[i
])
947 if (gethead(&spcl
) == GOOD
&& size
> 0) {
948 if (spcl
.c_type
== TS_ADDR
)
951 "Missing address (header) block for %s at %ld blocks\n",
952 curfile
.name
, (long)blksread
);
955 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
956 last_write_was_hole
= 0;
959 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
961 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
962 (*skip
)(clearedbuf
, skp
);
965 last_write_was_hole
= 1;
967 if (last_write_was_hole
) {
968 FTRUNCATE(ofile
, origsize
);
976 * Write out the next block of a file.
979 xtrfile(char *buf
, size_t size
)
984 if (write(ofile
, buf
, (int) size
) == -1)
985 err(1, "write error extracting inode %lu, name %s\nwrite",
986 (unsigned long)curfile
.ino
, curfile
.name
);
990 * Skip over a hole in a file.
994 xtrskip(char *buf
, size_t size
)
997 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
998 err(1, "seek error extracting inode %lu, name %s\nlseek",
999 (unsigned long)curfile
.ino
, curfile
.name
);
1003 * Collect the next block of a symbolic link.
1006 xtrlnkfile(char *buf
, size_t size
)
1010 if (pathlen
> MAXPATHLEN
)
1011 errx(1, "symbolic link name: %s->%s%s; too long %d",
1012 curfile
.name
, lnkbuf
, buf
, pathlen
);
1013 (void) strcat(lnkbuf
, buf
);
1017 * Skip over a hole in a symbolic link (should never happen).
1021 xtrlnkskip(char *buf
, size_t size
)
1024 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1028 * Collect the next block of a bit map.
1031 xtrmap(char *buf
, size_t size
)
1034 memmove(map
, buf
, size
);
1039 * Skip over a hole in a bit map (should never happen).
1043 xtrmapskip(char *buf
, size_t size
)
1046 panic("hole in map\n");
1051 * Noop, when an extraction function is not needed.
1055 xtrnull(char *buf
, size_t size
)
1061 #if COMPARE_ONTHEFLY
1063 * Compare the next block of a file.
1066 xtrcmpfile(char *buf
, size_t size
)
1068 static char cmpbuf
[MAXBSIZE
];
1073 if (read(ifile
, cmpbuf
, size
) != size
) {
1074 fprintf(stderr
, "%s: size has changed.\n",
1080 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1081 fprintf(stderr
, "%s: tape and disk copies are different\n",
1089 * Skip over a hole in a file.
1092 xtrcmpskip(char *buf
, size_t size
)
1094 static char cmpbuf
[MAXBSIZE
];
1100 if (read(ifile
, cmpbuf
, size
) != size
) {
1101 fprintf(stderr
, "%s: size has changed.\n",
1107 for (i
= 0; i
< size
; ++i
)
1108 if (cmpbuf
[i
] != '\0') {
1109 fprintf(stderr
, "%s: tape and disk copies are different\n",
1115 #endif /* COMPARE_ONTHEFLY */
1117 #if !COMPARE_ONTHEFLY
1119 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1121 static char buf_tape
[BUFSIZ
];
1122 static char buf_disk
[BUFSIZ
];
1127 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1128 close(fd_tape
), close(fd_disk
);
1129 panic("do_cmpfiles: unexpected EOF[1]");
1131 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1132 close(fd_tape
), close(fd_disk
);
1133 panic("do_cmpfiles: unexpected EOF[2]");
1135 if (n_tape
!= n_disk
) {
1136 close(fd_tape
), close(fd_disk
);
1137 panic("do_cmpfiles: sizes different!");
1139 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1145 /* for debugging compare problems */
1146 #undef COMPARE_FAIL_KEEP_FILE
1149 #ifdef COMPARE_FAIL_KEEP_FILE
1150 /* return true if tapefile should be unlinked after compare */
1155 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1157 struct STAT sbuf_tape
;
1158 int fd_tape
, fd_disk
;
1160 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1161 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1166 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1168 "%s: size changed from %ld to %ld.\n",
1169 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1171 #ifdef COMPARE_FAIL_KEEP_FILE
1178 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1179 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1182 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1184 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1188 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1189 fprintf(stderr
, "%s: tape and disk copies are different\n",
1194 #ifdef COMPARE_FAIL_KEEP_FILE
1195 /* rename the file to live in /tmp */
1196 /* rename `tapefile' to /tmp/<basename of diskfile> */
1198 char *p
= strrchr(diskfile
, '/');
1199 char newname
[MAXPATHLEN
];
1201 panic("can't find / in %s\n", diskfile
);
1203 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1204 if (rename(tapefile
, newname
)) {
1205 panic("rename from %s to %s failed: %s\n",
1209 fprintf(stderr
, "*** %s saved to %s\n",
1214 /* don't unlink the file (it's not there anymore */
1223 #ifdef COMPARE_FAIL_KEEP_FILE
1227 #endif /* !COMPARE_ONTHEFLY */
1229 #if !COMPARE_ONTHEFLY
1230 static char tmpfilename
[MAXPATHLEN
];
1234 comparefile(char *name
)
1239 #if !COMPARE_ONTHEFLY
1240 static char *tmpfile
= NULL
;
1244 if ((r
= LSTAT(name
, &sb
)) != 0) {
1245 warn("%s: does not exist (%d)", name
, r
);
1251 curfile
.name
= name
;
1252 curfile
.action
= USING
;
1253 mode
= curfile
.dip
->di_mode
;
1255 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1256 (long)sb
.st_size
, mode
);
1258 if (sb
.st_mode
!= mode
) {
1259 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1260 name
, mode
& 07777, sb
.st_mode
& 07777);
1263 switch (mode
& IFMT
) {
1277 char lbuf
[MAXPATHLEN
+ 1];
1280 if (!(sb
.st_mode
& S_IFLNK
)) {
1281 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1288 getfile(xtrlnkfile
, xtrlnkskip
);
1291 "%s: zero length symbolic link (ignored)\n",
1296 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1297 panic("readlink of %s failed: %s", name
,
1302 if (strcmp(lbuf
, lnkbuf
) != 0) {
1304 "%s: symbolic link changed from %s to %s.\n",
1305 name
, lnkbuf
, lbuf
);
1314 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1315 fprintf(stderr
, "%s: no longer a special file\n",
1322 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1324 "%s: device changed from %d,%d to %d,%d.\n",
1326 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1327 (int)curfile
.dip
->di_rdev
& 0xff,
1328 ((int)sb
.st_rdev
>> 8) & 0xff,
1329 (int)sb
.st_rdev
& 0xff);
1336 #if COMPARE_ONTHEFLY
1337 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1338 panic("Can't open %s: %s\n", name
, strerror(errno
));
1344 getfile(xtrcmpfile
, xtrcmpskip
);
1347 if (read(ifile
, &c
, 1) != 0) {
1348 fprintf(stderr
, "%s: size has changed.\n",
1358 if (tmpfile
== NULL
) {
1359 /* argument to mktemp() must not be in RO space: */
1360 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1361 tmpfile
= mktemp(&tmpfilename
[0]);
1363 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1364 panic("cannot delete tmp file %s: %s\n",
1365 tmpfile
, strerror(errno
));
1367 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1368 panic("cannot create file temp file %s: %s\n",
1369 name
, strerror(errno
));
1371 getfile(xtrfile
, xtrskip
);
1372 (void) close(ofile
);
1373 #ifdef COMPARE_FAIL_KEEP_FILE
1374 if (cmpfiles(tmpfile
, name
, &sb
))
1377 cmpfiles(tmpfile
, name
, &sb
);
1380 #endif /* COMPARE_ONTHEFLY */
1386 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1387 static void (*readtape_func
)(char *) = readtape_set
;
1390 * Read TP_BSIZE blocks from the input.
1391 * Handle read errors, and end of media.
1392 * Decompress compressed blocks.
1397 (*readtape_func
)(buf
); /* call the actual processing routine */
1401 * Set function pointer for readtape() routine. zflag and magtapein must
1402 * be correctly set before the first call to readtape().
1405 readtape_set(char *buf
)
1408 readtape_func
= readtape_uncompr
;
1412 readtape_func
= readtape_comprtape
;
1414 readtape_func
= readtape_comprfile
;
1419 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1422 * This is the original readtape(), it's used for reading uncompressed input.
1423 * Read TP_BSIZE blocks from the input.
1424 * Handle read errors, and end of media.
1427 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1428 readtape_uncompr(char *buf
)
1433 ssize_t rd
, newvol
, i
;
1434 int cnt
, seek_failed
;
1436 if (blkcnt
< numtrec
) {
1437 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1443 for (i
= 0; i
< ntrec
; i
++)
1444 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1447 cnt
= ntrec
* TP_BSIZE
;
1450 if (createtapeposflag
)
1451 (void)GetTapePos(&curtapepos
);
1456 i
= rmtread(&tapebuf
[rd
], cnt
);
1459 i
= read(mt
, &tapebuf
[rd
], cnt
);
1462 * Check for mid-tape short read error.
1463 * If found, skip rest of buffer and start with the next.
1465 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1466 Dprintf(stdout
, "mid-media short read error.\n");
1470 * Handle partial block read.
1472 if (pipein
&& i
== 0 && rd
> 0)
1474 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1483 * Short read. Process the blocks read.
1485 if (i
% TP_BSIZE
!= 0)
1487 "partial block read: %ld should be %ld\n",
1488 (long)i
, ntrec
* TP_BSIZE
);
1489 numtrec
= i
/ TP_BSIZE
;
1493 * Handle read error.
1496 fprintf(stderr
, "Tape read error while ");
1497 switch (curfile
.action
) {
1499 fprintf(stderr
, "trying to set up tape\n");
1502 fprintf(stderr
, "trying to resynchronize\n");
1505 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1508 fprintf(stderr
, "skipping over inode %lu\n",
1509 (unsigned long)curfile
.ino
);
1512 if (!yflag
&& !reply("continue"))
1514 i
= ntrec
* TP_BSIZE
;
1515 memset(tapebuf
, 0, (size_t)i
);
1518 seek_failed
= (rmtseek(i
, 1) < 0);
1521 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1524 warn("continuation failed");
1525 if (!yflag
&& !reply("assume end-of-tape and continue"))
1531 * Handle end of tape.
1534 Vprintf(stdout
, "End-of-tape encountered\n");
1543 if (rd
% TP_BSIZE
!= 0)
1544 panic("partial block read: %d should be %d\n",
1545 rd
, ntrec
* TP_BSIZE
);
1547 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1550 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1555 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1558 * Read a compressed format block from a file or pipe and uncompress it.
1559 * Attempt to handle read errors, and end of file.
1562 readtape_comprfile(char *buf
)
1564 long rl
, size
, i
, ret
;
1566 struct tapebuf
*tpb
;
1568 if (blkcnt
< numtrec
) {
1569 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1574 /* need to read the next block */
1576 for (i
= 0; i
< ntrec
; i
++)
1577 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1579 tpb
= (struct tapebuf
*) tapebuf
;
1581 /* read the block prefix */
1582 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1584 if (Vflag
&& (ret
== 0 || rl
< PREFIXSIZE
|| tpb
->length
== 0))
1591 if (size
> bufsize
) {
1592 /* something's wrong */
1593 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1596 tpb
->length
= bufsize
;
1598 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1602 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1603 if (tbufptr
== NULL
) {
1604 msg_read_error("File decompression error while");
1605 if (!yflag
&& !reply("continue"))
1607 memset(tapebuf
, 0, bufsize
);
1612 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1618 /* Errors while reading from a file or pipe are catastrophic. Since
1619 * there are no block boundaries, it's impossible to bypass the
1620 * block in error and find the start of the next block.
1623 /* It's possible to have multiple input files using -M
1624 * and -f file1,file2...
1626 Vprintf(stdout
, "End-of-File encountered\n");
1636 msg_read_error("Read error while");
1637 /* if (!yflag && !reply("continue")) */
1642 * Read compressed data from a tape and uncompress it.
1643 * Handle read errors, and end of media.
1644 * Since a tape consists of separate physical blocks, we try
1645 * to recover from errors by repositioning the tape to the next
1649 readtape_comprtape(char *buf
)
1653 struct tapebuf
*tpb
;
1656 if (blkcnt
< numtrec
) {
1657 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1662 /* need to read the next block */
1664 for (i
= 0; i
< ntrec
; i
++)
1665 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1667 tpb
= (struct tapebuf
*) tapebuf
;
1669 /* read the block */
1670 size
= bufsize
+ PREFIXSIZE
;
1671 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1675 tbufptr
= decompress_tapebuf(tpb
, rl
);
1676 if (tbufptr
== NULL
) {
1677 msg_read_error("Tape decompression error while");
1678 if (!yflag
&& !reply("continue"))
1680 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1686 /* Handle errors: EOT switches to the next volume, other errors
1687 * attempt to position the tape to the next block.
1690 Vprintf(stdout
, "End-of-tape encountered\n");
1699 msg_read_error("Tape read error while");
1700 if (!yflag
&& !reply("continue"))
1702 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1707 rl
= rmtioctl(MTFSR
, 1);
1713 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1717 warn("continuation failed");
1718 if (!yflag
&& !reply("assume end-of-tape and continue"))
1720 ret
= 0; /* end of tape */
1726 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1732 * Decompress a struct tapebuf into a buffer. readsize is the size read
1733 * from the tape/file and is used for error messages. Returns a pointer
1734 * to the location of the uncompressed buffer or NULL on errors.
1735 * Adjust numtrec and complain for a short block.
1738 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1740 /* If zflag is on, all blocks have a struct tapebuf prefix */
1741 /* zflag gets set in setup() from the dump header */
1742 int cresult
, blocklen
;
1743 unsigned long worklen
;
1745 unsigned int worklen2
;
1747 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1749 /* build a length error message */
1750 blocklen
= tpbin
->length
;
1751 if (readsize
< blocklen
+ PREFIXSIZE
)
1752 lengtherr
= "short";
1754 if (readsize
> blocklen
+ PREFIXSIZE
)
1759 if (tpbin
->compressed
) {
1760 /* uncompress whatever we read, if it fails, complain later */
1761 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1763 errx(1,"This restore version doesn't support zlib decompression");
1765 cresult
= uncompress(comprbuf
, &worklen
,
1766 tpbin
->buf
, blocklen
);
1772 reason
= "not enough memory";
1775 reason
= "buffer too small";
1778 reason
= "data error";
1783 if (cresult
== Z_OK
)
1787 #endif /* HAVE_ZLIB */
1789 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1791 errx(1,"This restore version doesn't support bzlib decompression");
1794 cresult
= BZ2_bzBuffToBuffDecompress(
1795 comprbuf
, &worklen2
,
1796 tpbin
->buf
, blocklen
, 0, 0);
1803 reason
= "not enough memory";
1805 case BZ_OUTBUFF_FULL
:
1806 reason
= "buffer too small";
1809 case BZ_DATA_ERROR_MAGIC
:
1810 case BZ_UNEXPECTED_EOF
:
1811 reason
= "data error";
1816 if (cresult
== BZ_OK
)
1820 #endif /* HAVE_BZLIB */
1824 output
= tpbin
->buf
;
1828 numtrec
= worklen
/ TP_BSIZE
;
1829 if (worklen
% TP_BSIZE
!= 0)
1830 reason
= "length mismatch";
1834 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1835 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1836 fprintf(stderr
, "decompression error, block %ld: %s\n",
1837 tpblksread
+1, reason
);
1845 * Print an error message for a read error.
1846 * This was exteracted from the original readtape().
1849 msg_read_error(char *m
)
1851 switch (curfile
.action
) {
1853 fprintf(stderr
, "%s trying to set up tape\n", m
);
1856 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1859 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1862 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1863 (unsigned long)curfile
.ino
);
1867 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1870 * Read the first block and get the blocksize from it. Test
1871 * for a compressed dump tape/file. setup() will make the final
1872 * determination by checking the compressed flag if gethead()
1873 * finds a valid header. The test here is necessary to offset the buffer
1874 * by the size of the compressed prefix. zflag is set here so that
1875 * readtape_set can set the correct function pointer for readtape().
1876 * Note that the first block of each tape/file is not compressed
1877 * and does not have a prefix.
1880 findtapeblksize(void)
1884 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1885 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1887 for (i
= 0; i
< ntrec
; i
++)
1888 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1892 * For a pipe or file, read in the first record. For a tape, read
1895 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1897 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1898 errx(1, "Tape read error on first record");
1901 * If the input is from a file or a pipe, we read TP_BSIZE
1902 * bytes looking for a dump header. If the dump is compressed
1903 * we need to read in the rest of the block, as determined
1904 * by c_ntrec in the dump header. The first block of the
1905 * dump is not compressed and does not have a prefix.
1908 if (spclpt
->c_type
== TS_TAPE
1909 && spclpt
->c_flags
& DR_COMPRESSED
) {
1910 /* It's a compressed dump file, read in the */
1911 /* rest of the block based on spclpt->c_ntrec. */
1912 if (spclpt
->c_ntrec
> ntrec
)
1913 errx(1, "Tape blocksize is too large, use "
1914 "\'-b %d\' ", spclpt
->c_ntrec
);
1915 ntrec
= spclpt
->c_ntrec
;
1916 len
= (ntrec
- 1) * TP_BSIZE
;
1920 /* read in the rest of the block based on bufsize */
1921 len
= bufsize
- TP_BSIZE
;
1923 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1924 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1925 errx(1,"Error reading dump file header");
1928 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1930 } /* if (!magtapein) */
1933 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1934 * If the value of ntrec is too large, we read less than
1935 * what we asked for; adjust the value of ntrec and test for
1936 * a compressed dump tape.
1939 if (i
% TP_BSIZE
!= 0) {
1940 /* may be old format compressed dump tape with a prefix */
1941 spclpt
= (struct s_spcl
*) tpb
->buf
;
1942 if (i
% TP_BSIZE
== PREFIXSIZE
1943 && tpb
->compressed
== 0
1944 && spclpt
->c_type
== TS_TAPE
1945 && spclpt
->c_flags
& DR_COMPRESSED
) {
1948 if (tpb
->length
> bufsize
)
1949 errx(1, "Tape blocksize is too large, use "
1950 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1953 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1956 ntrec
= i
/ TP_BSIZE
;
1957 if (spclpt
->c_type
== TS_TAPE
) {
1958 if (spclpt
->c_flags
& DR_COMPRESSED
)
1960 if (spclpt
->c_ntrec
> ntrec
)
1961 errx(1, "Tape blocksize is too large, use "
1962 "\'-b %d\' ", spclpt
->c_ntrec
);
1965 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1969 * Read a block of data handling all of the messy details.
1971 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1979 i
= rmtread(buf
, size
);
1982 i
= read(fd
, buf
, size
);
1985 break; /* EOD or error */
1988 break; /* block at a time for mt */
1991 *lengthread
= len
- size
;
2010 setmagtapein(void) {
2011 struct mtget mt_stat
;
2012 static int done
= 0;
2017 /* need to know if input is really from a tape */
2023 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2026 Vprintf(stdout
,"Input is from %s\n",
2027 magtapein
? "tape" :
2028 Vflag
? "multi-volume (no tape)" : "file/pipe");
2032 * Read the next block from the tape.
2033 * Check to see if it is one of several vintage headers.
2034 * If it is an old style header, convert it to a new style header.
2035 * If it is not any valid header, return an error.
2038 gethead(struct s_spcl
*buf
)
2046 char dummy
[TP_BSIZE
];
2053 u_int16_t c_inumber
;
2058 u_int16_t odi_nlink
;
2074 readtape((char *)buf
);
2075 if (buf
->c_magic
!= NFS_MAGIC
) {
2076 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2079 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2083 if (checksum((int *)buf
) == FAIL
)
2086 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
2089 readtape((char *)(&u_ospcl
.s_ospcl
));
2090 memset((char *)buf
, 0, (long)TP_BSIZE
);
2091 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2092 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2093 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2094 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2095 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2096 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2097 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2098 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2099 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2100 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2101 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2102 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2103 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2104 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2105 #if defined(__linux__) || defined(sunos)
2106 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2107 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2108 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2109 #else /* __linux__ || sunos */
2110 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2111 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2112 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2113 #endif /* __linux__ || sunos */
2114 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2115 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2116 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2117 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2119 buf
->c_magic
= NFS_MAGIC
;
2122 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2123 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2124 qcvt
.qval
= buf
->c_dinode
.di_size
;
2125 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2126 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2131 qcvt
.qval
= buf
->c_dinode
.di_size
;
2133 qcvt
.val
[1] = qcvt
.val
[0];
2135 buf
->c_dinode
.di_size
= qcvt
.qval
;
2139 switch (buf
->c_type
) {
2144 * Have to patch up missing information in bit map headers
2147 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2148 if (buf
->c_count
> TP_NINDIR
)
2151 for (i
= 0; i
< buf
->c_count
; i
++)
2156 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2161 if (buf
->c_flags
& DR_INODEINFO
) {
2162 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2164 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2173 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2177 * If we are restoring a filesystem with old format inodes,
2178 * copy the uid/gid to the new location.
2181 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2182 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2190 * Check that a header is where it belongs and predict the next header
2193 accthdr(struct s_spcl
*header
)
2195 static dump_ino_t previno
= 0x7fffffff;
2196 static int prevtype
;
2197 static long predict
;
2200 if (header
->c_type
== TS_TAPE
) {
2201 fprintf(stderr
, "Volume header (%s inode format) ",
2202 oldinofmt
? "old" : "new");
2203 if (header
->c_firstrec
)
2204 fprintf(stderr
, "begins with record %d",
2205 header
->c_firstrec
);
2206 fprintf(stderr
, "\n");
2207 previno
= 0x7fffffff;
2210 if (previno
== 0x7fffffff)
2214 fprintf(stderr
, "Dumped inodes map header");
2217 fprintf(stderr
, "Used inodes map header");
2220 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2223 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2226 fprintf(stderr
, "End of tape header");
2229 if (predict
!= blksread
- 1)
2230 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2231 predict
, blksread
- 1);
2232 fprintf(stderr
, "\n");
2235 if (header
->c_type
!= TS_END
)
2236 for (i
= 0; i
< header
->c_count
; i
++)
2237 if (readmapflag
|| header
->c_addr
[i
] != 0)
2241 prevtype
= header
->c_type
;
2242 previno
= header
->c_inumber
;
2246 * Find an inode header.
2247 * Complain if had to skip, and complain is set.
2250 findinode(struct s_spcl
*header
)
2252 static long skipcnt
= 0;
2256 curfile
.name
= "<name unknown>";
2257 curfile
.action
= UNKNOWN
;
2261 if (header
->c_magic
!= NFS_MAGIC
) {
2263 while (gethead(header
) == FAIL
||
2264 header
->c_date
!= dumpdate
)
2267 switch (header
->c_type
) {
2271 * Skip up to the beginning of the next record
2273 for (i
= 0; i
< header
->c_count
; i
++)
2274 if (header
->c_addr
[i
])
2276 while (gethead(header
) == FAIL
||
2277 header
->c_date
!= dumpdate
)
2282 curfile
.dip
= &header
->c_dinode
;
2283 curfile
.ino
= header
->c_inumber
;
2287 curfile
.ino
= maxino
;
2291 curfile
.name
= "<file removal list>";
2295 curfile
.name
= "<file dump list>";
2299 panic("unexpected tape header\n");
2303 panic("unknown tape header type %d\n", spcl
.c_type
);
2307 } while (header
->c_type
== TS_ADDR
);
2312 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2322 j
= sizeof(union u_spcl
) / sizeof(int);
2329 /* What happens if we want to read restore tapes
2330 for a 16bit int machine??? */
2336 if (i
!= CHECKSUM
) {
2337 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2338 (unsigned long)curfile
.ino
, curfile
.name
);
2348 #include <varargs.h>
2353 msg(const char *fmt
, ...)
2366 (void)vfprintf(stderr
, fmt
, ap
);
2369 #endif /* RRESTORE */
2372 swab16(u_char
*sp
, int n
)
2377 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2384 swab32(u_char
*sp
, int n
)
2389 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2390 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2397 swab64(u_char
*sp
, int n
)
2402 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2403 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2404 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2405 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2412 swabst(u_char
*cp
, u_char
*sp
)
2418 case '0': case '1': case '2': case '3': case '4':
2419 case '5': case '6': case '7': case '8': case '9':
2420 n
= (n
* 10) + (*cp
++ - '0');
2423 case 's': case 'w': case 'h':
2441 default: /* Any other character, like 'b' counts as byte. */
2455 swabst((u_char
*)"i", (u_char
*)&x
);
2463 swabst((u_char
*)"l", (u_char
*)&x
);
2470 * get the current position of the tape
2473 GetTapePos(long long *pos
)
2479 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2488 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2489 *pos
= (long long)mtpos
;
2492 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2498 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2499 (unsigned long)getpid(), err
, *pos
);
2505 typedef struct mt_pos
{
2508 } MTPosRec
, *MTPosPtr
;
2511 * go to specified position on tape
2514 GotoTapePos(long long pos
)
2520 err
= (rmtseek((long)pos
, SEEK_SET
) < 0);
2527 buf
.mt_count
= (int) pos
;
2528 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2531 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2537 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2538 (unsigned long)getpid(), err
, pos
);
2545 * read next data from tape to re-sync
2548 ReReadFromTape(void)
2552 if (gethead(&spcl
) == FAIL
) {
2554 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2562 ReReadInodeFromTape(dump_ino_t theino
)
2571 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
) && (cntloop
< 32));
2573 fprintf(stderr
, "%ld reads\n", cntloop
);
2574 if (cntloop
== 32) {
2575 fprintf(stderr
, "DEBUG: bufsize %d\n", bufsize
);
2576 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
2577 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
2583 #endif /* USE_QFA */
2586 RequestVol(long tnum
)