]>
git.wh0rd.org - dump.git/blob - restore/tape.c
3195ab53b4746893ca50f62ef3e085980bd3db60
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.64 2002/07/29 12:00:34 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
, O_RDONLY
);
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
&& (unsigned long)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;
439 panic("Changing volumes on pipe input?\n");
444 saved_blksread
= blksread
;
445 saved_tpblksread
= tpblksread
;
446 #if defined(USE_QFA) && defined(sunos)
447 if (createtapeposflag
|| tapeposflag
)
452 exit(1); /* pipes do not get a second chance */
453 if (aflag
|| curfile
.action
!= SKIP
) {
460 while (newvol
<= 0) {
461 if (tapesread
== 0) {
462 fprintf(stderr
, "%s%s%s%s%s",
463 "You have not read any volumes yet.\n",
464 "Unless you know which volume your",
465 " file(s) are on you should start\n",
466 "with the last volume and work",
467 " towards the first.\n");
469 fprintf(stderr
, "You have read volumes");
471 for (i
= 1; i
< 32; i
++)
472 if (tapesread
& (1 << i
)) {
473 fprintf(stderr
, "%s%ld", buf
, (long)i
);
476 fprintf(stderr
, "\n");
479 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
480 (void) fflush(stderr
);
481 (void) fgets(buf
, TP_BSIZE
, terminal
);
482 } while (!feof(terminal
) && buf
[0] == '\n');
485 if (!strcmp(buf
, "none\n")) {
492 "Volume numbers are positive numerics\n");
495 if (newvol
== volno
) {
496 tapesread
|= 1 << volno
;
497 #if defined(USE_QFA) && defined(sunos)
498 if (createtapeposflag
|| tapeposflag
) {
499 if (OpenSMTCmt(magtape
) < 0) {
510 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
511 magtape
[MAXPATHLEN
- 1] = '\0';
513 if (bot_script
&& !haderror
) {
514 msg("Launching %s\n", bot_script
);
515 bot_code
= system_command(bot_script
, magtape
, newvol
);
516 if (bot_code
!= 0 && bot_code
!= 1) {
517 msg("Restore aborted by the beginning of tape script\n");
521 if (haderror
|| (bot_code
&& !Mflag
)) {
523 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
524 fprintf(stderr
, "Enter ``none'' if there are no more volumes\n");
525 fprintf(stderr
, "otherwise enter volume name (default: %s) ", magtape
);
526 (void) fflush(stderr
);
527 (void) fgets(buf
, TP_BSIZE
, terminal
);
530 if (!strcmp(buf
, "none\n")) {
534 if (buf
[0] != '\n') {
536 (void) strncpy(magtape
, buf
, sizeof(magtape
));
537 magtape
[sizeof(magtape
) - 1] = '\0';
538 if ((pos
= strchr(magtape
, '\n')))
539 magtape
[pos
- magtape
] = '\0';
544 mt
= rmtopen(magtape
, O_RDONLY
);
547 mt
= OPEN(magtape
, O_RDONLY
, 0);
550 fprintf(stderr
, "Cannot open %s\n", magtape
);
557 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
558 readtape_func
= readtape_set
;
564 if (gethead(&tmpbuf
) == FAIL
) {
565 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
566 fprintf(stderr
, "tape is not dump tape\n");
569 blksread
= saved_blksread
;
570 tpblksread
= saved_tpblksread
;
573 if (tmpbuf
.c_volume
!= volno
) {
574 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
577 blksread
= saved_blksread
;
578 tpblksread
= saved_tpblksread
;
581 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
582 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
583 ctime4(&tmpbuf
.c_date
));
584 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
587 blksread
= saved_blksread
;
588 tpblksread
= saved_tpblksread
;
591 tapesread
|= 1 << volno
;
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
- 1, (long)tmpbuf
.c_firstrec
);
607 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
609 tpblksread
= tmpbuf
.c_firstrec
+ 1;
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
< (int)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(struct entry
*ep
, int doremove
)
725 struct timeval timep
[2];
726 char *name
= myname(ep
);
728 /* If removal is requested (-r mode) do remove it unless
729 * we are extracting a metadata only inode */
730 if (spcl
.c_flags
& DR_METAONLY
) {
731 Vprintf(stdout
, "file %s is metadata only\n", name
);
736 ep
->e_flags
&= ~REMOVED
;
741 curfile
.action
= USING
;
742 #if defined(__linux__) || defined(sunos)
743 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
744 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
745 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
746 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
747 #else /* __linux__ || sunos */
748 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
749 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
750 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
751 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
752 #endif /* __linux__ */
753 mode
= curfile
.dip
->di_mode
;
754 flags
= curfile
.dip
->di_flags
;
755 switch (mode
& IFMT
) {
758 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
763 Vprintf(stdout
, "skipped socket %s\n", name
);
769 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
770 panic("unextracted directory %s\n", name
);
774 Vprintf(stdout
, "extract file %s\n", name
);
775 return (genliteraldir(name
, curfile
.ino
));
780 uid_t luid
= curfile
.dip
->di_uid
;
781 gid_t lgid
= curfile
.dip
->di_gid
;
783 if (! (spcl
.c_flags
& DR_METAONLY
)) {
786 getfile(xtrlnkfile
, xtrlnkskip
);
789 "%s: zero length symbolic link (ignored)\n", name
);
792 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
799 (void) lchown(name
, luid
, lgid
);
805 Vprintf(stdout
, "extract fifo %s\n", name
);
810 if (! (spcl
.c_flags
& DR_METAONLY
)) {
813 if (mkfifo(name
, mode
) < 0) {
814 warn("%s: cannot create fifo", name
);
819 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
820 (void) chmod(name
, mode
);
823 (void) fsetflags(name
, flags
);
825 (void) chflags(name
, flags
);
833 Vprintf(stdout
, "extract special file %s\n", name
);
838 if (! (spcl
.c_flags
& DR_METAONLY
)) {
841 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
842 warn("%s: cannot create special file", name
);
847 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
848 (void) chmod(name
, mode
);
852 warn("%s: fsetflags called on a special file", name
);
853 (void) fsetflags(name
, flags
);
856 (void) chflags(name
, flags
);
864 uid_t luid
= curfile
.dip
->di_uid
;
865 gid_t lgid
= curfile
.dip
->di_gid
;
867 Vprintf(stdout
, "extract file %s\n", name
);
872 if (! (spcl
.c_flags
& DR_METAONLY
)) {
875 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
877 warn("%s: cannot create file", name
);
881 getfile(xtrfile
, xtrskip
);
886 (void) chown(name
, luid
, lgid
);
887 (void) chmod(name
, mode
);
890 (void) fsetflags(name
, flags
);
892 (void) chflags(name
, flags
);
902 * skip over bit maps on the tape
908 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
913 * skip over a file on the tape
919 curfile
.action
= SKIP
;
920 getfile(xtrnull
, xtrnull
);
924 * Extract a file from the tape.
925 * When an allocated block is found it is passed to the fill function;
926 * when an unallocated block (hole) is found, a zeroed buffer is passed
927 * to the skip function.
930 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
933 volatile int curblk
= 0;
934 volatile quad_t size
= spcl
.c_dinode
.di_size
;
935 volatile int last_write_was_hole
= 0;
936 quad_t origsize
= size
;
937 static char clearedbuf
[MAXBSIZE
];
938 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
941 if (spcl
.c_type
== TS_END
)
942 panic("ran off end of tape\n");
943 if (spcl
.c_magic
!= NFS_MAGIC
)
944 panic("not at beginning of a file\n");
945 if (!gettingfile
&& setjmp(restart
) != 0)
949 for (i
= 0; i
< spcl
.c_count
; i
++) {
950 if (readmapflag
|| spcl
.c_addr
[i
]) {
951 readtape(&buf
[curblk
++][0]);
952 if (curblk
== fssize
/ TP_BSIZE
) {
953 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
954 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
956 last_write_was_hole
= 0;
960 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
962 (curblk
- 1) * TP_BSIZE
+ size
));
965 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
967 last_write_was_hole
= 1;
969 if ((size
-= TP_BSIZE
) <= 0) {
970 for (i
++; i
< spcl
.c_count
; i
++)
971 if (readmapflag
|| spcl
.c_addr
[i
])
976 if (gethead(&spcl
) == GOOD
&& size
> 0) {
977 if (spcl
.c_type
== TS_ADDR
)
980 "Missing address (header) block for %s at %ld blocks\n",
981 curfile
.name
, (long)blksread
);
984 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
985 last_write_was_hole
= 0;
988 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
990 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
991 (*skip
)(clearedbuf
, skp
);
994 last_write_was_hole
= 1;
996 if (last_write_was_hole
) {
997 FTRUNCATE(ofile
, origsize
);
1005 * Write out the next block of a file.
1008 xtrfile(char *buf
, size_t size
)
1013 if (write(ofile
, buf
, (int) size
) == -1)
1014 err(1, "write error extracting inode %lu, name %s\nwrite",
1015 (unsigned long)curfile
.ino
, curfile
.name
);
1019 * Skip over a hole in a file.
1023 xtrskip(UNUSED(char *buf
), size_t size
)
1026 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
1027 err(1, "seek error extracting inode %lu, name %s\nlseek",
1028 (unsigned long)curfile
.ino
, curfile
.name
);
1032 * Collect the next block of a symbolic link.
1035 xtrlnkfile(char *buf
, size_t size
)
1039 if (pathlen
> MAXPATHLEN
)
1040 errx(1, "symbolic link name: %s->%s%s; too long %d",
1041 curfile
.name
, lnkbuf
, buf
, pathlen
);
1042 (void) strcat(lnkbuf
, buf
);
1046 * Skip over a hole in a symbolic link (should never happen).
1050 xtrlnkskip(UNUSED(char *buf
), UNUSED(size_t size
))
1053 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1057 * Collect the next block of a bit map.
1060 xtrmap(char *buf
, size_t size
)
1063 memmove(map
, buf
, size
);
1068 * Skip over a hole in a bit map (should never happen).
1072 xtrmapskip(UNUSED(char *buf
), size_t size
)
1075 panic("hole in map\n");
1080 * Noop, when an extraction function is not needed.
1084 xtrnull(UNUSED(char *buf
), UNUSED(size_t size
))
1090 #if COMPARE_ONTHEFLY
1092 * Compare the next block of a file.
1095 xtrcmpfile(char *buf
, size_t size
)
1097 static char cmpbuf
[MAXBSIZE
];
1102 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1103 fprintf(stderr
, "%s: size has changed.\n",
1109 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1110 fprintf(stderr
, "%s: tape and disk copies are different\n",
1118 * Skip over a hole in a file.
1121 xtrcmpskip(UNUSED(char *buf
), size_t size
)
1123 static char cmpbuf
[MAXBSIZE
];
1129 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1130 fprintf(stderr
, "%s: size has changed.\n",
1136 for (i
= 0; i
< (int)size
; ++i
)
1137 if (cmpbuf
[i
] != '\0') {
1138 fprintf(stderr
, "%s: tape and disk copies are different\n",
1144 #endif /* COMPARE_ONTHEFLY */
1146 #if !COMPARE_ONTHEFLY
1148 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1150 static char buf_tape
[BUFSIZ
];
1151 static char buf_disk
[BUFSIZ
];
1156 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1157 close(fd_tape
), close(fd_disk
);
1158 panic("do_cmpfiles: unexpected EOF[1]");
1160 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1161 close(fd_tape
), close(fd_disk
);
1162 panic("do_cmpfiles: unexpected EOF[2]");
1164 if (n_tape
!= n_disk
) {
1165 close(fd_tape
), close(fd_disk
);
1166 panic("do_cmpfiles: sizes different!");
1168 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1174 /* for debugging compare problems */
1175 #undef COMPARE_FAIL_KEEP_FILE
1178 #ifdef COMPARE_FAIL_KEEP_FILE
1179 /* return true if tapefile should be unlinked after compare */
1184 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1186 struct STAT sbuf_tape
;
1187 int fd_tape
, fd_disk
;
1189 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1190 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1195 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1197 "%s: size changed from %ld to %ld.\n",
1198 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1200 #ifdef COMPARE_FAIL_KEEP_FILE
1207 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1208 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1211 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1213 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1217 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1218 fprintf(stderr
, "%s: tape and disk copies are different\n",
1223 #ifdef COMPARE_FAIL_KEEP_FILE
1224 /* rename the file to live in /tmp */
1225 /* rename `tapefile' to /tmp/<basename of diskfile> */
1227 char *p
= strrchr(diskfile
, '/');
1228 char newname
[MAXPATHLEN
];
1230 panic("can't find / in %s\n", diskfile
);
1232 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1233 if (rename(tapefile
, newname
)) {
1234 panic("rename from %s to %s failed: %s\n",
1238 fprintf(stderr
, "*** %s saved to %s\n",
1243 /* don't unlink the file (it's not there anymore */
1252 #ifdef COMPARE_FAIL_KEEP_FILE
1256 #endif /* !COMPARE_ONTHEFLY */
1258 #if !COMPARE_ONTHEFLY
1259 static char tmpfilename
[MAXPATHLEN
];
1263 comparefile(char *name
)
1268 #if !COMPARE_ONTHEFLY
1269 static char *tmpfile
= NULL
;
1273 if ((r
= LSTAT(name
, &sb
)) != 0) {
1274 warn("%s: does not exist (%d)", name
, r
);
1280 curfile
.name
= name
;
1281 curfile
.action
= USING
;
1282 mode
= curfile
.dip
->di_mode
;
1284 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1285 (long)sb
.st_size
, mode
);
1287 if (sb
.st_mode
!= mode
) {
1288 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1289 name
, mode
& 07777, sb
.st_mode
& 07777);
1292 if (spcl
.c_flags
& DR_METAONLY
) {
1296 switch (mode
& IFMT
) {
1310 char lbuf
[MAXPATHLEN
+ 1];
1313 if (!(sb
.st_mode
& S_IFLNK
)) {
1314 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1321 getfile(xtrlnkfile
, xtrlnkskip
);
1324 "%s: zero length symbolic link (ignored)\n",
1329 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1330 panic("readlink of %s failed: %s", name
,
1335 if (strcmp(lbuf
, lnkbuf
) != 0) {
1337 "%s: symbolic link changed from %s to %s.\n",
1338 name
, lnkbuf
, lbuf
);
1347 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1348 fprintf(stderr
, "%s: no longer a special file\n",
1355 if (sb
.st_rdev
!= (dev_t
)curfile
.dip
->di_rdev
) {
1357 "%s: device changed from %d,%d to %d,%d.\n",
1359 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1360 (int)curfile
.dip
->di_rdev
& 0xff,
1361 ((int)sb
.st_rdev
>> 8) & 0xff,
1362 (int)sb
.st_rdev
& 0xff);
1369 #if COMPARE_ONTHEFLY
1370 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1371 panic("Can't open %s: %s\n", name
, strerror(errno
));
1377 getfile(xtrcmpfile
, xtrcmpskip
);
1380 if (read(ifile
, &c
, 1) != 0) {
1381 fprintf(stderr
, "%s: size has changed.\n",
1391 if (tmpfile
== NULL
) {
1392 /* argument to mktemp() must not be in RO space: */
1393 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1394 tmpfile
= mktemp(&tmpfilename
[0]);
1396 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1397 panic("cannot delete tmp file %s: %s\n",
1398 tmpfile
, strerror(errno
));
1400 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1401 panic("cannot create file temp file %s: %s\n",
1402 name
, strerror(errno
));
1404 getfile(xtrfile
, xtrskip
);
1405 (void) close(ofile
);
1406 #ifdef COMPARE_FAIL_KEEP_FILE
1407 if (cmpfiles(tmpfile
, name
, &sb
))
1410 cmpfiles(tmpfile
, name
, &sb
);
1413 #endif /* COMPARE_ONTHEFLY */
1419 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1420 static void (*readtape_func
)(char *) = readtape_set
;
1423 * Read TP_BSIZE blocks from the input.
1424 * Handle read errors, and end of media.
1425 * Decompress compressed blocks.
1430 (*readtape_func
)(buf
); /* call the actual processing routine */
1434 * Set function pointer for readtape() routine. zflag and magtapein must
1435 * be correctly set before the first call to readtape().
1438 readtape_set(char *buf
)
1441 readtape_func
= readtape_uncompr
;
1445 readtape_func
= readtape_comprtape
;
1447 readtape_func
= readtape_comprfile
;
1452 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1455 * This is the original readtape(), it's used for reading uncompressed input.
1456 * Read TP_BSIZE blocks from the input.
1457 * Handle read errors, and end of media.
1460 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1461 readtape_uncompr(char *buf
)
1466 ssize_t rd
, newvol
, i
;
1467 int cnt
, seek_failed
;
1469 if (blkcnt
< numtrec
) {
1470 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1476 for (i
= 0; i
< ntrec
; i
++)
1477 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1480 cnt
= ntrec
* TP_BSIZE
;
1483 if (createtapeposflag
)
1484 (void)GetTapePos(&curtapepos
);
1489 i
= rmtread(&tapebuf
[rd
], cnt
);
1492 i
= read(mt
, &tapebuf
[rd
], cnt
);
1495 * Check for mid-tape short read error.
1496 * If found, skip rest of buffer and start with the next.
1498 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1499 Dprintf(stdout
, "mid-media short read error.\n");
1503 * Handle partial block read.
1505 if (pipein
&& i
== 0 && rd
> 0)
1507 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1516 * Short read. Process the blocks read.
1518 if (i
% TP_BSIZE
!= 0)
1520 "partial block read: %ld should be %ld\n",
1521 (long)i
, ntrec
* TP_BSIZE
);
1522 numtrec
= i
/ TP_BSIZE
;
1526 * Handle read error.
1529 fprintf(stderr
, "Tape read error while ");
1530 switch (curfile
.action
) {
1532 fprintf(stderr
, "trying to set up tape\n");
1535 fprintf(stderr
, "trying to resynchronize\n");
1538 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1541 fprintf(stderr
, "skipping over inode %lu\n",
1542 (unsigned long)curfile
.ino
);
1545 if (!yflag
&& !reply("continue"))
1547 i
= ntrec
* TP_BSIZE
;
1548 memset(tapebuf
, 0, (size_t)i
);
1551 seek_failed
= (rmtseek(i
, 1) < 0);
1554 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1557 warn("continuation failed");
1558 if (!yflag
&& !reply("assume end-of-tape and continue"))
1564 * Handle end of tape.
1567 Vprintf(stdout
, "End-of-tape encountered\n");
1576 if (rd
% TP_BSIZE
!= 0)
1577 panic("partial block read: %d should be %d\n",
1578 rd
, ntrec
* TP_BSIZE
);
1580 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1583 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1588 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1591 * Read a compressed format block from a file or pipe and uncompress it.
1592 * Attempt to handle read errors, and end of file.
1595 readtape_comprfile(char *buf
)
1597 long rl
, size
, i
, ret
;
1599 struct tapebuf
*tpb
;
1601 if (blkcnt
< numtrec
) {
1602 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1607 /* need to read the next block */
1609 for (i
= 0; i
< ntrec
; i
++)
1610 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1612 tpb
= (struct tapebuf
*) tapebuf
;
1614 /* read the block prefix */
1615 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1617 if (Vflag
&& (ret
== 0 || rl
< (int)PREFIXSIZE
|| tpb
->length
== 0))
1624 if (size
> bufsize
) {
1625 /* something's wrong */
1626 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1629 tpb
->length
= bufsize
;
1631 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1635 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1636 if (tbufptr
== NULL
) {
1637 msg_read_error("File decompression error while");
1638 if (!yflag
&& !reply("continue"))
1640 memset(tapebuf
, 0, bufsize
);
1645 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1651 /* Errors while reading from a file or pipe are catastrophic. Since
1652 * there are no block boundaries, it's impossible to bypass the
1653 * block in error and find the start of the next block.
1656 /* It's possible to have multiple input files using -M
1657 * and -f file1,file2...
1659 Vprintf(stdout
, "End-of-File encountered\n");
1669 msg_read_error("Read error while");
1670 /* if (!yflag && !reply("continue")) */
1675 * Read compressed data from a tape and uncompress it.
1676 * Handle read errors, and end of media.
1677 * Since a tape consists of separate physical blocks, we try
1678 * to recover from errors by repositioning the tape to the next
1682 readtape_comprtape(char *buf
)
1686 struct tapebuf
*tpb
;
1689 if (blkcnt
< numtrec
) {
1690 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1695 /* need to read the next block */
1697 for (i
= 0; i
< ntrec
; i
++)
1698 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1700 tpb
= (struct tapebuf
*) tapebuf
;
1702 /* read the block */
1703 size
= bufsize
+ PREFIXSIZE
;
1704 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1708 tbufptr
= decompress_tapebuf(tpb
, rl
);
1709 if (tbufptr
== NULL
) {
1710 msg_read_error("Tape decompression error while");
1711 if (!yflag
&& !reply("continue"))
1713 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1719 /* Handle errors: EOT switches to the next volume, other errors
1720 * attempt to position the tape to the next block.
1723 Vprintf(stdout
, "End-of-tape encountered\n");
1732 msg_read_error("Tape read error while");
1733 if (!yflag
&& !reply("continue"))
1735 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1740 rl
= rmtioctl(MTFSR
, 1);
1746 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1750 warn("continuation failed");
1751 if (!yflag
&& !reply("assume end-of-tape and continue"))
1753 ret
= 0; /* end of tape */
1759 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1765 * Decompress a struct tapebuf into a buffer. readsize is the size read
1766 * from the tape/file and is used for error messages. Returns a pointer
1767 * to the location of the uncompressed buffer or NULL on errors.
1768 * Adjust numtrec and complain for a short block.
1771 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1773 /* If zflag is on, all blocks have a struct tapebuf prefix */
1774 /* zflag gets set in setup() from the dump header */
1775 int cresult
, blocklen
;
1776 unsigned long worklen
;
1778 unsigned int worklen2
;
1780 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1782 /* build a length error message */
1783 blocklen
= tpbin
->length
;
1784 if (readsize
< blocklen
+ (int)PREFIXSIZE
)
1785 lengtherr
= "short";
1787 if (readsize
> blocklen
+ (int)PREFIXSIZE
)
1792 if (tpbin
->compressed
) {
1793 /* uncompress whatever we read, if it fails, complain later */
1794 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1796 errx(1,"This restore version doesn't support zlib decompression");
1798 cresult
= uncompress(comprbuf
, &worklen
,
1799 tpbin
->buf
, blocklen
);
1805 reason
= "not enough memory";
1808 reason
= "buffer too small";
1811 reason
= "data error";
1816 if (cresult
== Z_OK
)
1820 #endif /* HAVE_ZLIB */
1822 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1824 errx(1,"This restore version doesn't support bzlib decompression");
1827 cresult
= BZ2_bzBuffToBuffDecompress(
1828 comprbuf
, &worklen2
,
1829 tpbin
->buf
, blocklen
, 0, 0);
1836 reason
= "not enough memory";
1838 case BZ_OUTBUFF_FULL
:
1839 reason
= "buffer too small";
1842 case BZ_DATA_ERROR_MAGIC
:
1843 case BZ_UNEXPECTED_EOF
:
1844 reason
= "data error";
1849 if (cresult
== BZ_OK
)
1853 #endif /* HAVE_BZLIB */
1857 output
= tpbin
->buf
;
1861 numtrec
= worklen
/ TP_BSIZE
;
1862 if (worklen
% TP_BSIZE
!= 0)
1863 reason
= "length mismatch";
1867 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1868 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1869 fprintf(stderr
, "decompression error, block %ld: %s\n",
1870 tpblksread
+1, reason
);
1878 * Print an error message for a read error.
1879 * This was exteracted from the original readtape().
1882 msg_read_error(char *m
)
1884 switch (curfile
.action
) {
1886 fprintf(stderr
, "%s trying to set up tape\n", m
);
1889 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1892 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1895 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1896 (unsigned long)curfile
.ino
);
1900 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1903 * Read the first block and get the blocksize from it. Test
1904 * for a compressed dump tape/file. setup() will make the final
1905 * determination by checking the compressed flag if gethead()
1906 * finds a valid header. The test here is necessary to offset the buffer
1907 * by the size of the compressed prefix. zflag is set here so that
1908 * readtape_set can set the correct function pointer for readtape().
1909 * Note that the first block of each tape/file is not compressed
1910 * and does not have a prefix.
1913 findtapeblksize(void)
1917 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1918 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1920 for (i
= 0; i
< ntrec
; i
++)
1921 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1925 * For a pipe or file, read in the first record. For a tape, read
1928 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1930 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1931 errx(1, "Tape read error on first record");
1934 * If the input is from a file or a pipe, we read TP_BSIZE
1935 * bytes looking for a dump header. If the dump is compressed
1936 * we need to read in the rest of the block, as determined
1937 * by c_ntrec in the dump header. The first block of the
1938 * dump is not compressed and does not have a prefix.
1941 if (spclpt
->c_type
== TS_TAPE
1942 && spclpt
->c_flags
& DR_COMPRESSED
) {
1943 /* It's a compressed dump file, read in the */
1944 /* rest of the block based on spclpt->c_ntrec. */
1945 if (spclpt
->c_ntrec
> ntrec
)
1946 errx(1, "Tape blocksize is too large, use "
1947 "\'-b %d\' ", spclpt
->c_ntrec
);
1948 ntrec
= spclpt
->c_ntrec
;
1949 len
= (ntrec
- 1) * TP_BSIZE
;
1953 /* read in the rest of the block based on bufsize */
1954 len
= bufsize
- TP_BSIZE
;
1956 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1957 || (i
!= (long)len
&& i
% TP_BSIZE
!= 0))
1958 errx(1,"Error reading dump file header");
1961 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1963 } /* if (!magtapein) */
1966 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1967 * If the value of ntrec is too large, we read less than
1968 * what we asked for; adjust the value of ntrec and test for
1969 * a compressed dump tape.
1972 if (i
% TP_BSIZE
!= 0) {
1973 /* may be old format compressed dump tape with a prefix */
1974 spclpt
= (struct s_spcl
*) tpb
->buf
;
1975 if (i
% TP_BSIZE
== PREFIXSIZE
1976 && tpb
->compressed
== 0
1977 && spclpt
->c_type
== TS_TAPE
1978 && spclpt
->c_flags
& DR_COMPRESSED
) {
1981 if (tpb
->length
> bufsize
)
1982 errx(1, "Tape blocksize is too large, use "
1983 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1986 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1989 ntrec
= i
/ TP_BSIZE
;
1990 if (spclpt
->c_type
== TS_TAPE
) {
1991 if (spclpt
->c_flags
& DR_COMPRESSED
)
1993 if (spclpt
->c_ntrec
> ntrec
)
1994 errx(1, "Tape blocksize is too large, use "
1995 "\'-b %d\' ", spclpt
->c_ntrec
);
1998 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
2002 * Read a block of data handling all of the messy details.
2004 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
2012 i
= rmtread(buf
, size
);
2015 i
= read(fd
, buf
, size
);
2018 break; /* EOD or error */
2021 break; /* block at a time for mt */
2024 *lengthread
= len
- size
;
2043 setmagtapein(void) {
2044 struct mtget mt_stat
;
2045 static int done
= 0;
2050 /* need to know if input is really from a tape */
2056 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2059 Vprintf(stdout
,"Input is from %s\n",
2060 magtapein
? "tape" :
2061 Vflag
? "multi-volume (no tape)" : "file/pipe");
2065 * Read the next block from the tape.
2066 * Check to see if it is one of several vintage headers.
2067 * If it is an old style header, convert it to a new style header.
2068 * If it is not any valid header, return an error.
2071 gethead(struct s_spcl
*buf
)
2079 char dummy
[TP_BSIZE
];
2086 u_int16_t c_inumber
;
2091 u_int16_t odi_nlink
;
2107 readtape((char *)buf
);
2108 if (buf
->c_magic
!= NFS_MAGIC
) {
2109 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2112 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2116 if (checksum((int *)buf
) == FAIL
)
2119 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
2122 readtape((char *)(&u_ospcl
.s_ospcl
));
2123 memset((char *)buf
, 0, (long)TP_BSIZE
);
2124 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2125 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2126 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2127 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2128 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2129 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2130 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2131 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2132 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2133 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2134 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2135 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2136 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2137 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2138 #if defined(__linux__) || defined(sunos)
2139 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2140 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2141 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2142 #else /* __linux__ || sunos */
2143 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2144 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2145 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2146 #endif /* __linux__ || sunos */
2147 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2148 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2149 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2150 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2152 buf
->c_magic
= NFS_MAGIC
;
2155 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2156 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2157 qcvt
.qval
= buf
->c_dinode
.di_size
;
2158 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2159 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2164 qcvt
.qval
= buf
->c_dinode
.di_size
;
2166 qcvt
.val
[1] = qcvt
.val
[0];
2168 buf
->c_dinode
.di_size
= qcvt
.qval
;
2172 switch (buf
->c_type
) {
2177 * Have to patch up missing information in bit map headers
2180 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2181 if (buf
->c_count
> TP_NINDIR
)
2184 for (i
= 0; i
< buf
->c_count
; i
++)
2189 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2194 if (buf
->c_flags
& DR_INODEINFO
) {
2195 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2197 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2206 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2210 * If we are restoring a filesystem with old format inodes,
2211 * copy the uid/gid to the new location.
2214 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2215 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2223 * Check that a header is where it belongs and predict the next header
2226 accthdr(struct s_spcl
*header
)
2228 static dump_ino_t previno
= 0x7fffffff;
2229 static int prevtype
;
2230 static long predict
;
2233 if (header
->c_type
== TS_TAPE
) {
2234 fprintf(stderr
, "Volume header (%s inode format) ",
2235 oldinofmt
? "old" : "new");
2236 if (header
->c_firstrec
)
2237 fprintf(stderr
, "begins with record %d",
2238 header
->c_firstrec
);
2239 fprintf(stderr
, "\n");
2240 previno
= 0x7fffffff;
2243 if (previno
== 0x7fffffff)
2247 fprintf(stderr
, "Dumped inodes map header");
2250 fprintf(stderr
, "Used inodes map header");
2253 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2256 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2259 fprintf(stderr
, "End of tape header");
2262 if (predict
!= blksread
- 1)
2263 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2264 predict
, blksread
- 1);
2265 fprintf(stderr
, "\n");
2268 if (header
->c_type
!= TS_END
)
2269 for (i
= 0; i
< header
->c_count
; i
++)
2270 if (readmapflag
|| header
->c_addr
[i
] != 0)
2274 prevtype
= header
->c_type
;
2275 previno
= header
->c_inumber
;
2279 * Find an inode header.
2280 * Complain if had to skip, and complain is set.
2283 findinode(struct s_spcl
*header
)
2285 static long skipcnt
= 0;
2289 curfile
.name
= "<name unknown>";
2290 curfile
.action
= UNKNOWN
;
2294 if (header
->c_magic
!= NFS_MAGIC
) {
2296 while (gethead(header
) == FAIL
||
2297 header
->c_date
!= dumpdate
)
2300 switch (header
->c_type
) {
2304 * Skip up to the beginning of the next record
2306 for (i
= 0; i
< header
->c_count
; i
++)
2307 if (header
->c_addr
[i
])
2309 while (gethead(header
) == FAIL
||
2310 header
->c_date
!= dumpdate
)
2315 curfile
.dip
= &header
->c_dinode
;
2316 curfile
.ino
= header
->c_inumber
;
2320 curfile
.ino
= maxino
;
2324 curfile
.name
= "<file removal list>";
2328 curfile
.name
= "<file dump list>";
2332 panic("unexpected tape header\n");
2336 panic("unknown tape header type %d\n", spcl
.c_type
);
2340 } while (header
->c_type
== TS_ADDR
);
2345 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2355 j
= sizeof(union u_spcl
) / sizeof(int);
2362 /* What happens if we want to read restore tapes
2363 for a 16bit int machine??? */
2369 if (i
!= CHECKSUM
) {
2370 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2371 (unsigned long)curfile
.ino
, curfile
.name
);
2381 #include <varargs.h>
2386 msg(const char *fmt
, ...)
2399 (void)vfprintf(stderr
, fmt
, ap
);
2402 #endif /* RRESTORE */
2405 swab16(u_char
*sp
, int n
)
2410 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2417 swab32(u_char
*sp
, int n
)
2422 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2423 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2430 swab64(u_char
*sp
, int n
)
2435 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2436 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2437 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2438 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2445 swabst(u_char
*cp
, u_char
*sp
)
2451 case '0': case '1': case '2': case '3': case '4':
2452 case '5': case '6': case '7': case '8': case '9':
2453 n
= (n
* 10) + (*cp
++ - '0');
2456 case 's': case 'w': case 'h':
2474 default: /* Any other character, like 'b' counts as byte. */
2488 swabst((u_char
*)"i", (u_char
*)&x
);
2496 swabst((u_char
*)"l", (u_char
*)&x
);
2503 * get the current position of the tape
2506 GetTapePos(long long *pos
)
2512 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2521 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2522 *pos
= (long long)mtpos
;
2525 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2531 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2532 (unsigned long)getpid(), err
, *pos
);
2538 typedef struct mt_pos
{
2541 } MTPosRec
, *MTPosPtr
;
2544 * go to specified position on tape
2547 GotoTapePos(long long pos
)
2553 err
= (rmtseek((long)pos
, SEEK_SET
) < 0);
2560 buf
.mt_count
= (int) pos
;
2561 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2564 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2570 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2571 (unsigned long)getpid(), err
, pos
);
2578 * read next data from tape to re-sync
2581 ReReadFromTape(void)
2585 if (gethead(&spcl
) == FAIL
) {
2587 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2595 ReReadInodeFromTape(dump_ino_t theino
)
2604 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
) && (cntloop
< 32));
2606 fprintf(stderr
, "%ld reads\n", cntloop
);
2607 if (cntloop
== 32) {
2608 fprintf(stderr
, "DEBUG: bufsize %d\n", bufsize
);
2609 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
2610 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
2616 #endif /* USE_QFA */
2619 RequestVol(long tnum
)