]>
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.65 2002/11/15 09:25:42 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 dump_ino_t oldmaxino
= maxino
;
378 maxino
+= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
379 resizemaps(oldmaxino
, maxino
);
381 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
382 getfile(xtrmap
, xtrmapskip
);
384 Dprintf(stdout
, "maxino = %lu\n", (unsigned long)maxino
);
385 if (spcl
.c_type
!= TS_BITS
)
386 errx(1, "Cannot find file dump list");
387 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
388 if (map
== (char *)NULL
)
389 errx(1, "no memory for file dump list");
391 curfile
.action
= USING
;
392 getfile(xtrmap
, xtrmapskip
);
393 while (spcl
.c_type
== TS_ADDR
) {
394 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
395 getfile(xtrmap
, xtrmapskip
);
398 * If there may be whiteout entries on the tape, pretend that the
399 * whiteout inode exists, so that the whiteout entries can be
403 SETINO(WINO
, dumpmap
);
409 * Prompt user to load a new dump volume.
410 * "Nextvol" is the next suggested volume to use.
411 * This suggested volume is enforced when doing full
412 * or incremental restores, but can be overridden by
413 * the user when only extracting a subset of the files.
418 long newvol
= 0, wantnext
= 0, i
;
419 long saved_blksread
= 0, saved_tpblksread
= 0;
420 union u_spcl tmpspcl
;
421 # define tmpbuf tmpspcl.s_spcl
423 int haderror
= 0, bot_code
= 1;
433 panic("Changing volumes on pipe input?\n");
438 saved_blksread
= blksread
;
439 saved_tpblksread
= tpblksread
;
440 #if defined(USE_QFA) && defined(sunos)
441 if (createtapeposflag
|| tapeposflag
)
446 exit(1); /* pipes do not get a second chance */
447 if (aflag
|| curfile
.action
!= SKIP
) {
454 while (newvol
<= 0) {
455 if (tapesread
== 0) {
456 fprintf(stderr
, "%s%s%s%s%s",
457 "You have not read any volumes yet.\n",
458 "Unless you know which volume your",
459 " file(s) are on you should start\n",
460 "with the last volume and work",
461 " towards the first.\n");
463 fprintf(stderr
, "You have read volumes");
465 for (i
= 1; i
< 32; i
++)
466 if (tapesread
& (1 << i
)) {
467 fprintf(stderr
, "%s%ld", buf
, (long)i
);
470 fprintf(stderr
, "\n");
473 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
474 (void) fflush(stderr
);
475 (void) fgets(buf
, TP_BSIZE
, terminal
);
476 } while (!feof(terminal
) && buf
[0] == '\n');
479 if (!strcmp(buf
, "none\n")) {
486 "Volume numbers are positive numerics\n");
489 if (newvol
== volno
) {
490 tapesread
|= 1 << volno
;
491 #if defined(USE_QFA) && defined(sunos)
492 if (createtapeposflag
|| tapeposflag
) {
493 if (OpenSMTCmt(magtape
) < 0) {
504 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
505 magtape
[MAXPATHLEN
- 1] = '\0';
507 if (bot_script
&& !haderror
) {
508 msg("Launching %s\n", bot_script
);
509 bot_code
= system_command(bot_script
, magtape
, newvol
);
510 if (bot_code
!= 0 && bot_code
!= 1) {
511 msg("Restore aborted by the beginning of tape script\n");
515 if (haderror
|| (bot_code
&& !Mflag
)) {
517 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
518 fprintf(stderr
, "Enter ``none'' if there are no more volumes\n");
519 fprintf(stderr
, "otherwise enter volume name (default: %s) ", magtape
);
520 (void) fflush(stderr
);
521 (void) fgets(buf
, TP_BSIZE
, terminal
);
524 if (!strcmp(buf
, "none\n")) {
528 if (buf
[0] != '\n') {
530 (void) strncpy(magtape
, buf
, sizeof(magtape
));
531 magtape
[sizeof(magtape
) - 1] = '\0';
532 if ((pos
= strchr(magtape
, '\n')))
533 magtape
[pos
- magtape
] = '\0';
538 mt
= rmtopen(magtape
, O_RDONLY
);
541 mt
= OPEN(magtape
, O_RDONLY
, 0);
544 fprintf(stderr
, "Cannot open %s\n", magtape
);
551 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
552 readtape_func
= readtape_set
;
558 if (gethead(&tmpbuf
) == FAIL
) {
559 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
560 fprintf(stderr
, "tape is not dump tape\n");
563 blksread
= saved_blksread
;
564 tpblksread
= saved_tpblksread
;
567 if (tmpbuf
.c_volume
!= volno
) {
568 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
571 blksread
= saved_blksread
;
572 tpblksread
= saved_tpblksread
;
575 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
576 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
577 ctime4(&tmpbuf
.c_date
));
578 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
581 blksread
= saved_blksread
;
582 tpblksread
= saved_tpblksread
;
585 tapesread
|= 1 << volno
;
587 * If continuing from the previous volume, skip over any
588 * blocks read already at the end of the previous volume.
590 * If coming to this volume at random, skip to the beginning
591 * of the next record.
594 fprintf(stderr
, "Dump tape is compressed.\n");
595 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
596 errx(1,"This restore version doesn't support decompression");
597 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
599 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
600 tpblksread
- 1, (long)tmpbuf
.c_firstrec
);
601 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
603 tpblksread
= tmpbuf
.c_firstrec
+ 1;
604 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
606 } else if (tmpbuf
.c_firstrec
> 0 &&
607 tmpbuf
.c_firstrec
< tpblksread
- 1) {
609 * -1 since we've read the volume header
611 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
612 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
613 (long)i
, i
> 1 ? "s" : "");
618 if (curfile
.action
== USING
) {
620 panic("active file into volume 1\n");
624 * Skip up to the beginning of the next record
626 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
627 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
629 (void) gethead(&spcl
);
633 siglongjmp(restart
, 1);
638 * Handle unexpected EOF.
644 if (gettingfile
&& curfile
.action
== USING
) {
645 printf("Warning: %s %s\n",
646 "End-of-input encountered while extracting", curfile
.name
);
648 curfile
.name
= "<name unknown>";
649 curfile
.action
= UNKNOWN
;
651 curfile
.ino
= maxino
;
654 siglongjmp(restart
, 1);
659 * handle multiple dumps per tape by skipping forward to the
667 if (dumpnum
== 1 || volno
!= 1)
670 errx(1, "Cannot have multiple dumps on pipe input");
672 tcom
.mt_count
= dumpnum
- 1;
675 rmtioctl(MTFSF
, dumpnum
- 1);
678 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
685 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
686 fprintf(stdout
, "Dumped from: %s",
687 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
688 if (spcl
.c_host
[0] == '\0')
690 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
691 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
692 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
700 if (volinfo
[1] == ROOTINO
) {
701 printf("Starting inode numbers by volume:\n");
702 for (i
= 1; i
< (int)TP_NINOS
&& volinfo
[i
] != 0; ++i
)
703 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
709 time_t tv_sec
; /* seconds */
710 suseconds_t tv_usec
; /* and microseconds */
715 extractfile(struct entry
*ep
, int doremove
)
719 struct timeval timep
[2];
720 char *name
= myname(ep
);
722 /* If removal is requested (-r mode) do remove it unless
723 * we are extracting a metadata only inode */
724 if (spcl
.c_flags
& DR_METAONLY
) {
725 Vprintf(stdout
, "file %s is metadata only\n", name
);
730 ep
->e_flags
&= ~REMOVED
;
735 curfile
.action
= USING
;
736 #if defined(__linux__) || defined(sunos)
737 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
738 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
739 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
740 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
741 #else /* __linux__ || sunos */
742 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
743 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
744 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
745 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
746 #endif /* __linux__ */
747 mode
= curfile
.dip
->di_mode
;
748 flags
= curfile
.dip
->di_flags
;
749 switch (mode
& IFMT
) {
752 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
757 Vprintf(stdout
, "skipped socket %s\n", name
);
763 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
764 panic("unextracted directory %s\n", name
);
768 Vprintf(stdout
, "extract file %s\n", name
);
769 return (genliteraldir(name
, curfile
.ino
));
774 uid_t luid
= curfile
.dip
->di_uid
;
775 gid_t lgid
= curfile
.dip
->di_gid
;
777 if (! (spcl
.c_flags
& DR_METAONLY
)) {
780 getfile(xtrlnkfile
, xtrlnkskip
);
783 "%s: zero length symbolic link (ignored)\n", name
);
786 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
793 (void) lchown(name
, luid
, lgid
);
799 Vprintf(stdout
, "extract fifo %s\n", name
);
804 if (! (spcl
.c_flags
& DR_METAONLY
)) {
807 if (mkfifo(name
, mode
) < 0) {
808 warn("%s: cannot create fifo", name
);
813 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
814 (void) chmod(name
, mode
);
817 (void) fsetflags(name
, flags
);
819 (void) chflags(name
, flags
);
827 Vprintf(stdout
, "extract special file %s\n", name
);
832 if (! (spcl
.c_flags
& DR_METAONLY
)) {
835 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
836 warn("%s: cannot create special file", name
);
841 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
842 (void) chmod(name
, mode
);
846 warn("%s: fsetflags called on a special file", name
);
847 (void) fsetflags(name
, flags
);
850 (void) chflags(name
, flags
);
858 uid_t luid
= curfile
.dip
->di_uid
;
859 gid_t lgid
= curfile
.dip
->di_gid
;
861 Vprintf(stdout
, "extract file %s\n", name
);
866 if (! (spcl
.c_flags
& DR_METAONLY
)) {
869 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
871 warn("%s: cannot create file", name
);
875 getfile(xtrfile
, xtrskip
);
880 (void) chown(name
, luid
, lgid
);
881 (void) chmod(name
, mode
);
884 (void) fsetflags(name
, flags
);
886 (void) chflags(name
, flags
);
896 * skip over bit maps on the tape
902 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
907 * skip over a file on the tape
913 curfile
.action
= SKIP
;
914 getfile(xtrnull
, xtrnull
);
918 * Extract a file from the tape.
919 * When an allocated block is found it is passed to the fill function;
920 * when an unallocated block (hole) is found, a zeroed buffer is passed
921 * to the skip function.
924 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
927 volatile int curblk
= 0;
928 volatile quad_t size
= spcl
.c_dinode
.di_size
;
929 volatile int last_write_was_hole
= 0;
930 quad_t origsize
= size
;
931 static char clearedbuf
[MAXBSIZE
];
932 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
935 if (spcl
.c_type
== TS_END
)
936 panic("ran off end of tape\n");
937 if (spcl
.c_magic
!= NFS_MAGIC
)
938 panic("not at beginning of a file\n");
939 if (!gettingfile
&& setjmp(restart
) != 0)
943 for (i
= 0; i
< spcl
.c_count
; i
++) {
944 if (readmapflag
|| spcl
.c_addr
[i
]) {
945 readtape(&buf
[curblk
++][0]);
946 if (curblk
== fssize
/ TP_BSIZE
) {
947 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
948 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
950 last_write_was_hole
= 0;
954 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
956 (curblk
- 1) * TP_BSIZE
+ size
));
959 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
961 last_write_was_hole
= 1;
963 if ((size
-= TP_BSIZE
) <= 0) {
964 for (i
++; i
< spcl
.c_count
; i
++)
965 if (readmapflag
|| spcl
.c_addr
[i
])
970 if (gethead(&spcl
) == GOOD
&& size
> 0) {
971 if (spcl
.c_type
== TS_ADDR
)
974 "Missing address (header) block for %s at %ld blocks\n",
975 curfile
.name
, (long)blksread
);
978 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
979 last_write_was_hole
= 0;
982 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
984 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
985 (*skip
)(clearedbuf
, skp
);
988 last_write_was_hole
= 1;
990 if (last_write_was_hole
) {
991 FTRUNCATE(ofile
, origsize
);
999 * Write out the next block of a file.
1002 xtrfile(char *buf
, size_t size
)
1007 if (write(ofile
, buf
, (int) size
) == -1)
1008 err(1, "write error extracting inode %lu, name %s\nwrite",
1009 (unsigned long)curfile
.ino
, curfile
.name
);
1013 * Skip over a hole in a file.
1017 xtrskip(UNUSED(char *buf
), size_t size
)
1020 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
1021 err(1, "seek error extracting inode %lu, name %s\nlseek",
1022 (unsigned long)curfile
.ino
, curfile
.name
);
1026 * Collect the next block of a symbolic link.
1029 xtrlnkfile(char *buf
, size_t size
)
1033 if (pathlen
> MAXPATHLEN
)
1034 errx(1, "symbolic link name: %s->%s%s; too long %d",
1035 curfile
.name
, lnkbuf
, buf
, pathlen
);
1036 (void) strcat(lnkbuf
, buf
);
1040 * Skip over a hole in a symbolic link (should never happen).
1044 xtrlnkskip(UNUSED(char *buf
), UNUSED(size_t size
))
1047 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1051 * Collect the next block of a bit map.
1054 xtrmap(char *buf
, size_t size
)
1057 memmove(map
, buf
, size
);
1062 * Skip over a hole in a bit map (should never happen).
1066 xtrmapskip(UNUSED(char *buf
), size_t size
)
1069 panic("hole in map\n");
1074 * Noop, when an extraction function is not needed.
1078 xtrnull(UNUSED(char *buf
), UNUSED(size_t size
))
1084 #if COMPARE_ONTHEFLY
1086 * Compare the next block of a file.
1089 xtrcmpfile(char *buf
, size_t size
)
1091 static char cmpbuf
[MAXBSIZE
];
1096 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1097 fprintf(stderr
, "%s: size has changed.\n",
1103 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1104 fprintf(stderr
, "%s: tape and disk copies are different\n",
1112 * Skip over a hole in a file.
1115 xtrcmpskip(UNUSED(char *buf
), size_t size
)
1117 static char cmpbuf
[MAXBSIZE
];
1123 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1124 fprintf(stderr
, "%s: size has changed.\n",
1130 for (i
= 0; i
< (int)size
; ++i
)
1131 if (cmpbuf
[i
] != '\0') {
1132 fprintf(stderr
, "%s: tape and disk copies are different\n",
1138 #endif /* COMPARE_ONTHEFLY */
1140 #if !COMPARE_ONTHEFLY
1142 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1144 static char buf_tape
[BUFSIZ
];
1145 static char buf_disk
[BUFSIZ
];
1150 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1151 close(fd_tape
), close(fd_disk
);
1152 panic("do_cmpfiles: unexpected EOF[1]");
1154 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1155 close(fd_tape
), close(fd_disk
);
1156 panic("do_cmpfiles: unexpected EOF[2]");
1158 if (n_tape
!= n_disk
) {
1159 close(fd_tape
), close(fd_disk
);
1160 panic("do_cmpfiles: sizes different!");
1162 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1168 /* for debugging compare problems */
1169 #undef COMPARE_FAIL_KEEP_FILE
1172 #ifdef COMPARE_FAIL_KEEP_FILE
1173 /* return true if tapefile should be unlinked after compare */
1178 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1180 struct STAT sbuf_tape
;
1181 int fd_tape
, fd_disk
;
1183 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1184 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1189 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1191 "%s: size changed from %ld to %ld.\n",
1192 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1194 #ifdef COMPARE_FAIL_KEEP_FILE
1201 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1202 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1205 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1207 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1211 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1212 fprintf(stderr
, "%s: tape and disk copies are different\n",
1217 #ifdef COMPARE_FAIL_KEEP_FILE
1218 /* rename the file to live in /tmp */
1219 /* rename `tapefile' to /tmp/<basename of diskfile> */
1221 char *p
= strrchr(diskfile
, '/');
1222 char newname
[MAXPATHLEN
];
1224 panic("can't find / in %s\n", diskfile
);
1226 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1227 if (rename(tapefile
, newname
)) {
1228 panic("rename from %s to %s failed: %s\n",
1232 fprintf(stderr
, "*** %s saved to %s\n",
1237 /* don't unlink the file (it's not there anymore */
1246 #ifdef COMPARE_FAIL_KEEP_FILE
1250 #endif /* !COMPARE_ONTHEFLY */
1252 #if !COMPARE_ONTHEFLY
1253 static char tmpfilename
[MAXPATHLEN
];
1257 comparefile(char *name
)
1262 #if !COMPARE_ONTHEFLY
1263 static char *tmpfile
= NULL
;
1267 if ((r
= LSTAT(name
, &sb
)) != 0) {
1268 warn("%s: does not exist (%d)", name
, r
);
1274 curfile
.name
= name
;
1275 curfile
.action
= USING
;
1276 mode
= curfile
.dip
->di_mode
;
1278 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1279 (long)sb
.st_size
, mode
);
1281 if (sb
.st_mode
!= mode
) {
1282 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1283 name
, mode
& 07777, sb
.st_mode
& 07777);
1286 if (spcl
.c_flags
& DR_METAONLY
) {
1290 switch (mode
& IFMT
) {
1304 char lbuf
[MAXPATHLEN
+ 1];
1307 if (!(sb
.st_mode
& S_IFLNK
)) {
1308 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1315 getfile(xtrlnkfile
, xtrlnkskip
);
1318 "%s: zero length symbolic link (ignored)\n",
1323 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1324 panic("readlink of %s failed: %s", name
,
1329 if (strcmp(lbuf
, lnkbuf
) != 0) {
1331 "%s: symbolic link changed from %s to %s.\n",
1332 name
, lnkbuf
, lbuf
);
1341 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1342 fprintf(stderr
, "%s: no longer a special file\n",
1349 if (sb
.st_rdev
!= (dev_t
)curfile
.dip
->di_rdev
) {
1351 "%s: device changed from %d,%d to %d,%d.\n",
1353 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1354 (int)curfile
.dip
->di_rdev
& 0xff,
1355 ((int)sb
.st_rdev
>> 8) & 0xff,
1356 (int)sb
.st_rdev
& 0xff);
1363 #if COMPARE_ONTHEFLY
1364 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1365 panic("Can't open %s: %s\n", name
, strerror(errno
));
1371 getfile(xtrcmpfile
, xtrcmpskip
);
1374 if (read(ifile
, &c
, 1) != 0) {
1375 fprintf(stderr
, "%s: size has changed.\n",
1385 if (tmpfile
== NULL
) {
1386 /* argument to mktemp() must not be in RO space: */
1387 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1388 tmpfile
= mktemp(&tmpfilename
[0]);
1390 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1391 panic("cannot delete tmp file %s: %s\n",
1392 tmpfile
, strerror(errno
));
1394 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1395 panic("cannot create file temp file %s: %s\n",
1396 name
, strerror(errno
));
1398 getfile(xtrfile
, xtrskip
);
1399 (void) close(ofile
);
1400 #ifdef COMPARE_FAIL_KEEP_FILE
1401 if (cmpfiles(tmpfile
, name
, &sb
))
1404 cmpfiles(tmpfile
, name
, &sb
);
1407 #endif /* COMPARE_ONTHEFLY */
1413 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1414 static void (*readtape_func
)(char *) = readtape_set
;
1417 * Read TP_BSIZE blocks from the input.
1418 * Handle read errors, and end of media.
1419 * Decompress compressed blocks.
1424 (*readtape_func
)(buf
); /* call the actual processing routine */
1428 * Set function pointer for readtape() routine. zflag and magtapein must
1429 * be correctly set before the first call to readtape().
1432 readtape_set(char *buf
)
1435 readtape_func
= readtape_uncompr
;
1439 readtape_func
= readtape_comprtape
;
1441 readtape_func
= readtape_comprfile
;
1446 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1449 * This is the original readtape(), it's used for reading uncompressed input.
1450 * Read TP_BSIZE blocks from the input.
1451 * Handle read errors, and end of media.
1454 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1455 readtape_uncompr(char *buf
)
1460 ssize_t rd
, newvol
, i
;
1461 int cnt
, seek_failed
;
1463 if (blkcnt
< numtrec
) {
1464 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1470 for (i
= 0; i
< ntrec
; i
++)
1471 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1474 cnt
= ntrec
* TP_BSIZE
;
1477 if (createtapeposflag
)
1478 (void)GetTapePos(&curtapepos
);
1483 i
= rmtread(&tapebuf
[rd
], cnt
);
1486 i
= read(mt
, &tapebuf
[rd
], cnt
);
1489 * Check for mid-tape short read error.
1490 * If found, skip rest of buffer and start with the next.
1492 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1493 Dprintf(stdout
, "mid-media short read error.\n");
1497 * Handle partial block read.
1499 if (pipein
&& i
== 0 && rd
> 0)
1501 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1510 * Short read. Process the blocks read.
1512 if (i
% TP_BSIZE
!= 0)
1514 "partial block read: %ld should be %ld\n",
1515 (long)i
, ntrec
* TP_BSIZE
);
1516 numtrec
= i
/ TP_BSIZE
;
1520 * Handle read error.
1523 fprintf(stderr
, "Tape read error while ");
1524 switch (curfile
.action
) {
1526 fprintf(stderr
, "trying to set up tape\n");
1529 fprintf(stderr
, "trying to resynchronize\n");
1532 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1535 fprintf(stderr
, "skipping over inode %lu\n",
1536 (unsigned long)curfile
.ino
);
1539 if (!yflag
&& !reply("continue"))
1541 i
= ntrec
* TP_BSIZE
;
1542 memset(tapebuf
, 0, (size_t)i
);
1545 seek_failed
= (rmtseek(i
, 1) < 0);
1548 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1551 warn("continuation failed");
1552 if (!yflag
&& !reply("assume end-of-tape and continue"))
1558 * Handle end of tape.
1561 Vprintf(stdout
, "End-of-tape encountered\n");
1570 if (rd
% TP_BSIZE
!= 0)
1571 panic("partial block read: %d should be %d\n",
1572 rd
, ntrec
* TP_BSIZE
);
1574 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1577 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1582 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1585 * Read a compressed format block from a file or pipe and uncompress it.
1586 * Attempt to handle read errors, and end of file.
1589 readtape_comprfile(char *buf
)
1591 long rl
, size
, i
, ret
;
1593 struct tapebuf
*tpb
;
1595 if (blkcnt
< numtrec
) {
1596 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1601 /* need to read the next block */
1603 for (i
= 0; i
< ntrec
; i
++)
1604 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1606 tpb
= (struct tapebuf
*) tapebuf
;
1608 /* read the block prefix */
1609 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1611 if (Vflag
&& (ret
== 0 || rl
< (int)PREFIXSIZE
|| tpb
->length
== 0))
1618 if (size
> bufsize
) {
1619 /* something's wrong */
1620 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1623 tpb
->length
= bufsize
;
1625 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1629 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1630 if (tbufptr
== NULL
) {
1631 msg_read_error("File decompression error while");
1632 if (!yflag
&& !reply("continue"))
1634 memset(tapebuf
, 0, bufsize
);
1639 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1645 /* Errors while reading from a file or pipe are catastrophic. Since
1646 * there are no block boundaries, it's impossible to bypass the
1647 * block in error and find the start of the next block.
1650 /* It's possible to have multiple input files using -M
1651 * and -f file1,file2...
1653 Vprintf(stdout
, "End-of-File encountered\n");
1663 msg_read_error("Read error while");
1664 /* if (!yflag && !reply("continue")) */
1669 * Read compressed data from a tape and uncompress it.
1670 * Handle read errors, and end of media.
1671 * Since a tape consists of separate physical blocks, we try
1672 * to recover from errors by repositioning the tape to the next
1676 readtape_comprtape(char *buf
)
1680 struct tapebuf
*tpb
;
1683 if (blkcnt
< numtrec
) {
1684 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1689 /* need to read the next block */
1691 for (i
= 0; i
< ntrec
; i
++)
1692 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1694 tpb
= (struct tapebuf
*) tapebuf
;
1696 /* read the block */
1697 size
= bufsize
+ PREFIXSIZE
;
1698 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1702 tbufptr
= decompress_tapebuf(tpb
, rl
);
1703 if (tbufptr
== NULL
) {
1704 msg_read_error("Tape decompression error while");
1705 if (!yflag
&& !reply("continue"))
1707 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1713 /* Handle errors: EOT switches to the next volume, other errors
1714 * attempt to position the tape to the next block.
1717 Vprintf(stdout
, "End-of-tape encountered\n");
1726 msg_read_error("Tape read error while");
1727 if (!yflag
&& !reply("continue"))
1729 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1734 rl
= rmtioctl(MTFSR
, 1);
1740 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1744 warn("continuation failed");
1745 if (!yflag
&& !reply("assume end-of-tape and continue"))
1747 ret
= 0; /* end of tape */
1753 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1759 * Decompress a struct tapebuf into a buffer. readsize is the size read
1760 * from the tape/file and is used for error messages. Returns a pointer
1761 * to the location of the uncompressed buffer or NULL on errors.
1762 * Adjust numtrec and complain for a short block.
1765 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1767 /* If zflag is on, all blocks have a struct tapebuf prefix */
1768 /* zflag gets set in setup() from the dump header */
1769 int cresult
, blocklen
;
1770 unsigned long worklen
;
1772 unsigned int worklen2
;
1774 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1776 /* build a length error message */
1777 blocklen
= tpbin
->length
;
1778 if (readsize
< blocklen
+ (int)PREFIXSIZE
)
1779 lengtherr
= "short";
1781 if (readsize
> blocklen
+ (int)PREFIXSIZE
)
1786 if (tpbin
->compressed
) {
1787 /* uncompress whatever we read, if it fails, complain later */
1788 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1790 errx(1,"This restore version doesn't support zlib decompression");
1792 cresult
= uncompress(comprbuf
, &worklen
,
1793 tpbin
->buf
, blocklen
);
1799 reason
= "not enough memory";
1802 reason
= "buffer too small";
1805 reason
= "data error";
1810 if (cresult
== Z_OK
)
1814 #endif /* HAVE_ZLIB */
1816 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1818 errx(1,"This restore version doesn't support bzlib decompression");
1821 cresult
= BZ2_bzBuffToBuffDecompress(
1822 comprbuf
, &worklen2
,
1823 tpbin
->buf
, blocklen
, 0, 0);
1830 reason
= "not enough memory";
1832 case BZ_OUTBUFF_FULL
:
1833 reason
= "buffer too small";
1836 case BZ_DATA_ERROR_MAGIC
:
1837 case BZ_UNEXPECTED_EOF
:
1838 reason
= "data error";
1843 if (cresult
== BZ_OK
)
1847 #endif /* HAVE_BZLIB */
1851 output
= tpbin
->buf
;
1855 numtrec
= worklen
/ TP_BSIZE
;
1856 if (worklen
% TP_BSIZE
!= 0)
1857 reason
= "length mismatch";
1861 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1862 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1863 fprintf(stderr
, "decompression error, block %ld: %s\n",
1864 tpblksread
+1, reason
);
1872 * Print an error message for a read error.
1873 * This was exteracted from the original readtape().
1876 msg_read_error(char *m
)
1878 switch (curfile
.action
) {
1880 fprintf(stderr
, "%s trying to set up tape\n", m
);
1883 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1886 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1889 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1890 (unsigned long)curfile
.ino
);
1894 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1897 * Read the first block and get the blocksize from it. Test
1898 * for a compressed dump tape/file. setup() will make the final
1899 * determination by checking the compressed flag if gethead()
1900 * finds a valid header. The test here is necessary to offset the buffer
1901 * by the size of the compressed prefix. zflag is set here so that
1902 * readtape_set can set the correct function pointer for readtape().
1903 * Note that the first block of each tape/file is not compressed
1904 * and does not have a prefix.
1907 findtapeblksize(void)
1911 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1912 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1914 for (i
= 0; i
< ntrec
; i
++)
1915 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1919 * For a pipe or file, read in the first record. For a tape, read
1922 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1924 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1925 errx(1, "Tape read error on first record");
1928 * If the input is from a file or a pipe, we read TP_BSIZE
1929 * bytes looking for a dump header. If the dump is compressed
1930 * we need to read in the rest of the block, as determined
1931 * by c_ntrec in the dump header. The first block of the
1932 * dump is not compressed and does not have a prefix.
1935 if (spclpt
->c_type
== TS_TAPE
1936 && spclpt
->c_flags
& DR_COMPRESSED
) {
1937 /* It's a compressed dump file, read in the */
1938 /* rest of the block based on spclpt->c_ntrec. */
1939 if (spclpt
->c_ntrec
> ntrec
)
1940 errx(1, "Tape blocksize is too large, use "
1941 "\'-b %d\' ", spclpt
->c_ntrec
);
1942 ntrec
= spclpt
->c_ntrec
;
1943 len
= (ntrec
- 1) * TP_BSIZE
;
1947 /* read in the rest of the block based on bufsize */
1948 len
= bufsize
- TP_BSIZE
;
1950 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1951 || (i
!= (long)len
&& i
% TP_BSIZE
!= 0))
1952 errx(1,"Error reading dump file header");
1955 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1957 } /* if (!magtapein) */
1960 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1961 * If the value of ntrec is too large, we read less than
1962 * what we asked for; adjust the value of ntrec and test for
1963 * a compressed dump tape.
1966 if (i
% TP_BSIZE
!= 0) {
1967 /* may be old format compressed dump tape with a prefix */
1968 spclpt
= (struct s_spcl
*) tpb
->buf
;
1969 if (i
% TP_BSIZE
== PREFIXSIZE
1970 && tpb
->compressed
== 0
1971 && spclpt
->c_type
== TS_TAPE
1972 && spclpt
->c_flags
& DR_COMPRESSED
) {
1975 if (tpb
->length
> bufsize
)
1976 errx(1, "Tape blocksize is too large, use "
1977 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1980 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1983 ntrec
= i
/ TP_BSIZE
;
1984 if (spclpt
->c_type
== TS_TAPE
) {
1985 if (spclpt
->c_flags
& DR_COMPRESSED
)
1987 if (spclpt
->c_ntrec
> ntrec
)
1988 errx(1, "Tape blocksize is too large, use "
1989 "\'-b %d\' ", spclpt
->c_ntrec
);
1992 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1996 * Read a block of data handling all of the messy details.
1998 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
2006 i
= rmtread(buf
, size
);
2009 i
= read(fd
, buf
, size
);
2012 break; /* EOD or error */
2015 break; /* block at a time for mt */
2018 *lengthread
= len
- size
;
2037 setmagtapein(void) {
2038 struct mtget mt_stat
;
2039 static int done
= 0;
2044 /* need to know if input is really from a tape */
2050 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2053 Vprintf(stdout
,"Input is from %s\n",
2054 magtapein
? "tape" :
2055 Vflag
? "multi-volume (no tape)" : "file/pipe");
2059 * Read the next block from the tape.
2060 * Check to see if it is one of several vintage headers.
2061 * If it is an old style header, convert it to a new style header.
2062 * If it is not any valid header, return an error.
2065 gethead(struct s_spcl
*buf
)
2073 char dummy
[TP_BSIZE
];
2080 u_int16_t c_inumber
;
2085 u_int16_t odi_nlink
;
2101 readtape((char *)buf
);
2102 if (buf
->c_magic
!= NFS_MAGIC
) {
2103 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2106 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2110 if (checksum((int *)buf
) == FAIL
)
2113 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
2116 readtape((char *)(&u_ospcl
.s_ospcl
));
2117 memset((char *)buf
, 0, (long)TP_BSIZE
);
2118 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2119 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2120 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2121 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2122 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2123 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2124 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2125 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2126 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2127 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2128 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2129 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2130 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2131 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2132 #if defined(__linux__) || defined(sunos)
2133 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2134 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2135 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2136 #else /* __linux__ || sunos */
2137 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2138 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2139 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2140 #endif /* __linux__ || sunos */
2141 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2142 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2143 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2144 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2146 buf
->c_magic
= NFS_MAGIC
;
2149 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2150 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2151 qcvt
.qval
= buf
->c_dinode
.di_size
;
2152 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2153 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2158 qcvt
.qval
= buf
->c_dinode
.di_size
;
2160 qcvt
.val
[1] = qcvt
.val
[0];
2162 buf
->c_dinode
.di_size
= qcvt
.qval
;
2166 switch (buf
->c_type
) {
2171 * Have to patch up missing information in bit map headers
2174 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2175 if (buf
->c_count
> TP_NINDIR
)
2178 for (i
= 0; i
< buf
->c_count
; i
++)
2183 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2188 if (buf
->c_flags
& DR_INODEINFO
) {
2189 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2191 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2200 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2204 * If we are restoring a filesystem with old format inodes,
2205 * copy the uid/gid to the new location.
2208 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2209 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2217 * Check that a header is where it belongs and predict the next header
2220 accthdr(struct s_spcl
*header
)
2222 static dump_ino_t previno
= 0x7fffffff;
2223 static int prevtype
;
2224 static long predict
;
2227 if (header
->c_type
== TS_TAPE
) {
2228 fprintf(stderr
, "Volume header (%s inode format) ",
2229 oldinofmt
? "old" : "new");
2230 if (header
->c_firstrec
)
2231 fprintf(stderr
, "begins with record %d",
2232 header
->c_firstrec
);
2233 fprintf(stderr
, "\n");
2234 previno
= 0x7fffffff;
2237 if (previno
== 0x7fffffff)
2241 fprintf(stderr
, "Dumped inodes map header");
2244 fprintf(stderr
, "Used inodes map header");
2247 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2250 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2253 fprintf(stderr
, "End of tape header");
2256 if (predict
!= blksread
- 1)
2257 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2258 predict
, blksread
- 1);
2259 fprintf(stderr
, "\n");
2262 if (header
->c_type
!= TS_END
)
2263 for (i
= 0; i
< header
->c_count
; i
++)
2264 if (readmapflag
|| header
->c_addr
[i
] != 0)
2268 prevtype
= header
->c_type
;
2269 previno
= header
->c_inumber
;
2273 * Find an inode header.
2274 * Complain if had to skip, and complain is set.
2277 findinode(struct s_spcl
*header
)
2279 static long skipcnt
= 0;
2283 curfile
.name
= "<name unknown>";
2284 curfile
.action
= UNKNOWN
;
2288 if (header
->c_magic
!= NFS_MAGIC
) {
2290 while (gethead(header
) == FAIL
||
2291 header
->c_date
!= dumpdate
)
2294 switch (header
->c_type
) {
2298 * Skip up to the beginning of the next record
2300 for (i
= 0; i
< header
->c_count
; i
++)
2301 if (header
->c_addr
[i
])
2303 while (gethead(header
) == FAIL
||
2304 header
->c_date
!= dumpdate
)
2309 curfile
.dip
= &header
->c_dinode
;
2310 curfile
.ino
= header
->c_inumber
;
2314 curfile
.ino
= maxino
;
2318 curfile
.name
= "<file removal list>";
2322 curfile
.name
= "<file dump list>";
2326 panic("unexpected tape header\n");
2330 panic("unknown tape header type %d\n", spcl
.c_type
);
2334 } while (header
->c_type
== TS_ADDR
);
2339 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2349 j
= sizeof(union u_spcl
) / sizeof(int);
2356 /* What happens if we want to read restore tapes
2357 for a 16bit int machine??? */
2363 if (i
!= CHECKSUM
) {
2364 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2365 (unsigned long)curfile
.ino
, curfile
.name
);
2375 #include <varargs.h>
2380 msg(const char *fmt
, ...)
2393 (void)vfprintf(stderr
, fmt
, ap
);
2396 #endif /* RRESTORE */
2399 swab16(u_char
*sp
, int n
)
2404 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2411 swab32(u_char
*sp
, int n
)
2416 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2417 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2424 swab64(u_char
*sp
, int n
)
2429 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2430 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2431 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2432 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2439 swabst(u_char
*cp
, u_char
*sp
)
2445 case '0': case '1': case '2': case '3': case '4':
2446 case '5': case '6': case '7': case '8': case '9':
2447 n
= (n
* 10) + (*cp
++ - '0');
2450 case 's': case 'w': case 'h':
2468 default: /* Any other character, like 'b' counts as byte. */
2482 swabst((u_char
*)"i", (u_char
*)&x
);
2490 swabst((u_char
*)"l", (u_char
*)&x
);
2497 * get the current position of the tape
2500 GetTapePos(long long *pos
)
2506 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2515 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2516 *pos
= (long long)mtpos
;
2519 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2525 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2526 (unsigned long)getpid(), err
, *pos
);
2532 typedef struct mt_pos
{
2535 } MTPosRec
, *MTPosPtr
;
2538 * go to specified position on tape
2541 GotoTapePos(long long pos
)
2547 err
= (rmtseek((long)pos
, SEEK_SET
) < 0);
2554 buf
.mt_count
= (int) pos
;
2555 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2558 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2564 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2565 (unsigned long)getpid(), err
, pos
);
2572 * read next data from tape to re-sync
2575 ReReadFromTape(void)
2579 if (gethead(&spcl
) == FAIL
) {
2581 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2589 ReReadInodeFromTape(dump_ino_t theino
)
2598 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
) && (cntloop
< 32));
2600 fprintf(stderr
, "%ld reads\n", cntloop
);
2601 if (cntloop
== 32) {
2602 fprintf(stderr
, "DEBUG: bufsize %d\n", bufsize
);
2603 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
2604 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
2610 #endif /* USE_QFA */
2613 RequestVol(long tnum
)