]>
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. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static const char rcsid
[] =
45 "$Id: tape.c,v 1.74 2003/03/31 09:42:59 stelian Exp $";
49 #include <compatlfs.h>
51 #include <compaterr.h>
59 #include <sys/param.h>
67 #ifdef HAVE_EXT2FS_EXT2_FS_H
68 #include <ext2fs/ext2_fs.h>
70 #include <linux/ext2_fs.h>
72 #include <ext2fs/ext2fs.h>
73 #include <bsdcompat.h>
75 #include <ufs/ufs/dinode.h>
76 #endif /* __linux__ */
77 #include <protocols/dumprestore.h>
81 #endif /* HAVE_ZLIB */
85 #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) || defined(HAVE_LZO)
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 int converthead
__P((struct s_spcl
*));
138 static void converttapebuf
__P((struct tapebuf
*));
139 static void readtape
__P((char *));
140 static void setdumpnum
__P((void));
141 static u_int swabi
__P((u_int
));
143 static u_long swabl
__P((u_long
));
145 static u_char
*swab64
__P((u_char
*, int));
146 static u_char
*swab32
__P((u_char
*, int));
147 static u_char
*swab16
__P((u_char
*, int));
148 static void terminateinput
__P((void));
149 static void xtrfile
__P((char *, size_t));
150 static void xtrlnkfile
__P((char *, size_t));
151 static void xtrlnkskip
__P((char *, size_t));
152 static void xtrmap
__P((char *, size_t));
153 static void xtrmapskip
__P((char *, size_t));
154 static void xtrskip
__P((char *, size_t));
155 static void setmagtapein
__P((void));
157 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
158 static void newcomprbuf
__P((int));
159 static void (*readtape_func
) __P((char *));
160 static void readtape_set
__P((char *));
161 static void readtape_uncompr
__P((char *));
162 static void readtape_comprfile
__P((char *));
163 static void readtape_comprtape
__P((char *));
164 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
165 static void msg_read_error
__P((char *));
167 static int read_a_block
__P((int, char *, size_t, long *));
168 #define PREFIXSIZE sizeof(struct tapebuf)
170 #define COMPARE_ONTHEFLY 1
173 static int ifile
; /* input file for compare */
174 static int cmperror
; /* compare error */
175 static void xtrcmpfile
__P((char *, size_t));
176 static void xtrcmpskip
__P((char *, size_t));
179 static int readmapflag
;
180 static int readingmaps
; /* set to 1 while reading the maps */
183 * Set up an input source. This is called from main.c before setup() is.
186 setinput(char *source
)
192 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
196 if (strchr(source
, ':')) {
198 source
= strchr(host
, ':');
200 if (rmthost(host
) == 0)
204 if (strcmp(source
, "-") == 0) {
206 * Since input is coming from a pipe we must establish
207 * our own connection to the terminal.
209 terminal
= fopen(_PATH_TTY
, "r");
210 if (terminal
== NULL
) {
211 warn("cannot open %s", _PATH_TTY
);
212 terminal
= fopen(_PATH_DEVNULL
, "r");
213 if (terminal
== NULL
)
214 err(1, "cannot open %s", _PATH_DEVNULL
);
218 setuid(getuid()); /* no longer need or want root privileges */
220 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
221 magtapeprefix
[MAXPATHLEN
-1] = '\0';
222 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
225 strncpy(magtape
, source
, MAXPATHLEN
);
226 magtape
[MAXPATHLEN
- 1] = '\0';
230 newtapebuf(long size
)
232 static int tapebufsize
= -1;
235 bufsize
= ntrec
* TP_BSIZE
;
236 if (size
<= tapebufsize
)
240 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
242 errx(1, "Cannot allocate space for tape buffer");
246 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
248 newcomprbuf(int size
)
250 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
251 if (buf_size
<= comprlen
)
254 if (comprbuf
!= NULL
)
256 comprbuf
= malloc(comprlen
);
257 if (comprbuf
== NULL
)
258 errx(1, "Cannot allocate space for decompress buffer");
260 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
263 * Verify that the tape drive can be accessed and
264 * that it actually is a dump tape.
269 int i
, j
, *ip
, bot_code
;
273 Vprintf(stdout
, "Verify tape and initialize maps\n");
274 if (Afile
== NULL
&& bot_script
) {
275 msg("Launching %s\n", bot_script
);
276 bot_code
= system_command(bot_script
, magtape
, 1);
277 if (bot_code
!= 0 && bot_code
!= 1) {
278 msg("Restore aborted by the beginning of tape script\n");
290 mt
= rmtopen(temptape
, O_RDONLY
);
296 mt
= OPEN(temptape
, O_RDONLY
, 0);
298 err(1, "%s", temptape
);
304 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
305 readtape_func
= readtape_set
;
306 #if defined(HAVE_LZO)
307 if (lzo_init() != LZO_E_OK
) {
308 msg("internal error - lzo_init failed \n");
315 if (gethead(&spcl
) == FAIL
) {
316 blkcnt
--; /* push back this block */
320 if (gethead(&spcl
) == FAIL
)
321 errx(1, "Tape is not a dump tape");
322 fprintf(stderr
, "Converting to new file system format.\n");
326 fprintf(stderr
, "Dump tape is compressed.\n");
327 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
328 errx(1,"This restore version doesn't support decompression");
329 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
332 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
333 endoftapemark
.s_spcl
.c_type
= TS_END
;
334 ip
= (int *)&endoftapemark
;
335 j
= sizeof(union u_spcl
) / sizeof(int);
340 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
342 if (vflag
|| command
== 't' || command
== 'C')
345 if (tapeposflag
&& (unsigned long)spcl
.c_date
!= qfadumpdate
)
346 errx(1, "different QFA/dumpdates detected\n");
348 if (filesys
[0] == '\0') {
350 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
351 filesys
[NAMELEN
- 1] = '\0';
352 dirptr
= strstr(filesys
, " (dir");
356 dumptime
= spcl
.c_ddate
;
357 dumpdate
= spcl
.c_date
;
358 if (STAT(".", &stbuf
) < 0)
359 err(1, "cannot stat .");
360 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
362 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
363 fssize
= stbuf
.st_blksize
;
364 if (((fssize
- 1) & fssize
) != 0)
365 errx(1, "bad block size %ld", fssize
);
366 if (spcl
.c_volume
!= 1)
367 errx(1, "Tape is not volume 1 of the dump");
368 if (gethead(&spcl
) == FAIL
) {
369 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
370 panic("no header after volume mark!\n");
374 if (spcl
.c_type
!= TS_CLRI
)
375 errx(1, "Cannot find file removal list");
376 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
377 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
379 errx(1, "no memory for active inode map");
381 curfile
.action
= USING
;
382 getfile(xtrmap
, xtrmapskip
);
383 while (spcl
.c_type
== TS_ADDR
) {
384 /* Recompute maxino and the map */
385 dump_ino_t oldmaxino
= maxino
;
386 maxino
+= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
387 resizemaps(oldmaxino
, maxino
);
389 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
390 getfile(xtrmap
, xtrmapskip
);
392 Dprintf(stdout
, "maxino = %lu\n", (unsigned long)maxino
);
393 if (spcl
.c_type
!= TS_BITS
)
394 errx(1, "Cannot find file dump list");
395 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
396 if (map
== (char *)NULL
)
397 errx(1, "no memory for file dump list");
399 curfile
.action
= USING
;
400 getfile(xtrmap
, xtrmapskip
);
401 while (spcl
.c_type
== TS_ADDR
) {
402 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
403 getfile(xtrmap
, xtrmapskip
);
406 * If there may be whiteout entries on the tape, pretend that the
407 * whiteout inode exists, so that the whiteout entries can be
411 SETINO(WINO
, dumpmap
);
417 * Prompt user to load a new dump volume.
418 * "Nextvol" is the next suggested volume to use.
419 * This suggested volume is enforced when doing full
420 * or incremental restores, but can be overridden by
421 * the user when only extracting a subset of the files.
426 long newvol
= 0, wantnext
= 0, i
;
427 long saved_blksread
= 0, saved_tpblksread
= 0;
428 union u_spcl tmpspcl
;
429 # define tmpbuf tmpspcl.s_spcl
431 int haderror
= 0, bot_code
= 1;
441 panic("Changing volumes on pipe input?\n");
446 saved_blksread
= blksread
;
447 saved_tpblksread
= tpblksread
;
448 #if defined(USE_QFA) && defined(sunos)
449 if (createtapeposflag
|| tapeposflag
)
454 exit(1); /* pipes do not get a second chance */
455 if (aflag
|| curfile
.action
!= SKIP
) {
462 while (newvol
<= 0) {
463 if (tapesread
== 0) {
464 fprintf(stderr
, "%s%s%s%s%s",
465 "You have not read any volumes yet.\n",
466 "Unless you know which volume your",
467 " file(s) are on you should start\n",
468 "with the last volume and work",
469 " towards the first.\n");
471 fprintf(stderr
, "You have read volumes");
473 for (i
= 1; i
< 32; i
++)
474 if (tapesread
& (1 << i
)) {
475 fprintf(stderr
, "%s%ld", buf
, (long)i
);
478 fprintf(stderr
, "\n");
481 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
482 (void) fflush(stderr
);
483 (void) fgets(buf
, TP_BSIZE
, terminal
);
484 } while (!feof(terminal
) && buf
[0] == '\n');
487 if (!strcmp(buf
, "none\n")) {
494 "Volume numbers are positive numerics\n");
497 if (newvol
== volno
) {
498 tapesread
|= 1 << volno
;
499 #if defined(USE_QFA) && defined(sunos)
500 if (createtapeposflag
|| tapeposflag
) {
501 if (OpenSMTCmt(magtape
) < 0) {
513 * if using an archive file, reset its name so readtape()
514 * could properly use remote access.
519 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
520 magtape
[MAXPATHLEN
- 1] = '\0';
522 if (bot_script
&& !haderror
) {
523 msg("Launching %s\n", bot_script
);
524 bot_code
= system_command(bot_script
, magtape
, newvol
);
525 if (bot_code
!= 0 && bot_code
!= 1) {
526 msg("Restore aborted by the beginning of tape script\n");
530 if (haderror
|| (bot_code
&& !Mflag
)) {
532 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
533 fprintf(stderr
, "Enter ``none'' if there are no more volumes\n");
534 fprintf(stderr
, "otherwise enter volume name (default: %s) ", magtape
);
535 (void) fflush(stderr
);
536 (void) fgets(buf
, TP_BSIZE
, terminal
);
539 if (!strcmp(buf
, "none\n")) {
543 if (buf
[0] != '\n') {
545 (void) strncpy(magtape
, buf
, sizeof(magtape
));
546 magtape
[sizeof(magtape
) - 1] = '\0';
547 if ((pos
= strchr(magtape
, '\n')))
548 magtape
[pos
- magtape
] = '\0';
553 mt
= rmtopen(magtape
, O_RDONLY
);
556 mt
= OPEN(magtape
, O_RDONLY
, 0);
559 fprintf(stderr
, "Cannot open %s\n", magtape
);
566 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
567 readtape_func
= readtape_set
;
573 if (gethead(&tmpbuf
) == FAIL
) {
574 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
575 fprintf(stderr
, "tape is not dump tape\n");
578 blksread
= saved_blksread
;
579 tpblksread
= saved_tpblksread
;
582 if (tmpbuf
.c_volume
!= volno
) {
583 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
586 blksread
= saved_blksread
;
587 tpblksread
= saved_tpblksread
;
590 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
591 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
592 ctime4(&tmpbuf
.c_date
));
593 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
596 blksread
= saved_blksread
;
597 tpblksread
= saved_tpblksread
;
600 tapesread
|= 1 << volno
;
602 * If continuing from the previous volume, skip over any
603 * blocks read already at the end of the previous volume.
605 * If coming to this volume at random, skip to the beginning
606 * of the next record.
609 fprintf(stderr
, "Dump tape is compressed.\n");
610 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
611 errx(1,"This restore version doesn't support decompression");
612 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
614 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
615 tpblksread
- 1, (long)tmpbuf
.c_firstrec
);
616 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
618 tpblksread
= tmpbuf
.c_firstrec
+ 1;
619 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
621 } else if (tmpbuf
.c_firstrec
> 0 &&
622 tmpbuf
.c_firstrec
< tpblksread
- 1) {
624 * -1 since we've read the volume header
626 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
627 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
628 (long)i
, i
> 1 ? "s" : "");
633 if (curfile
.action
== USING
) {
635 panic("active file into volume 1\n");
639 * Skip up to the beginning of the next record
641 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
642 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
644 (void) gethead(&spcl
);
648 siglongjmp(restart
, 1);
653 * Handle unexpected EOF.
659 if (gettingfile
&& curfile
.action
== USING
) {
660 printf("Warning: %s %s\n",
661 "End-of-input encountered while extracting", curfile
.name
);
663 curfile
.name
= "<name unknown>";
664 curfile
.action
= UNKNOWN
;
666 curfile
.ino
= maxino
;
669 siglongjmp(restart
, 1);
674 * handle multiple dumps per tape by skipping forward to the
682 if (dumpnum
== 1 || volno
!= 1)
685 errx(1, "Cannot have multiple dumps on pipe input");
687 tcom
.mt_count
= dumpnum
- 1;
690 rmtioctl(MTFSF
, dumpnum
- 1);
693 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
700 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
701 fprintf(stdout
, "Dumped from: %s",
702 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
703 if (spcl
.c_host
[0] == '\0')
705 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
706 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
707 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
715 if (volinfo
[1] == ROOTINO
) {
716 printf("Starting inode numbers by volume:\n");
717 for (i
= 1; i
< (int)TP_NINOS
&& volinfo
[i
] != 0; ++i
)
718 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
724 time_t tv_sec
; /* seconds */
725 suseconds_t tv_usec
; /* and microseconds */
730 extractfile(struct entry
*ep
, int doremove
)
734 struct timeval timep
[2];
735 char *name
= myname(ep
);
737 /* If removal is requested (-r mode) do remove it unless
738 * we are extracting a metadata only inode */
739 if (spcl
.c_flags
& DR_METAONLY
) {
740 Vprintf(stdout
, "file %s is metadata only\n", name
);
745 ep
->e_flags
&= ~REMOVED
;
750 curfile
.action
= USING
;
751 #if defined(__linux__) || defined(sunos)
752 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
753 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
754 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
755 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
756 #else /* __linux__ || sunos */
757 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
758 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
759 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
760 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
761 #endif /* __linux__ */
762 mode
= curfile
.dip
->di_mode
;
763 flags
= curfile
.dip
->di_flags
;
764 switch (mode
& IFMT
) {
767 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
772 Vprintf(stdout
, "skipped socket %s\n", name
);
778 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
779 panic("unextracted directory %s\n", name
);
783 Vprintf(stdout
, "extract file %s\n", name
);
784 return (genliteraldir(name
, curfile
.ino
));
789 uid_t luid
= curfile
.dip
->di_uid
;
790 gid_t lgid
= curfile
.dip
->di_gid
;
792 if (! (spcl
.c_flags
& DR_METAONLY
)) {
795 getfile(xtrlnkfile
, xtrlnkskip
);
798 "%s: zero length symbolic link (ignored)\n", name
);
801 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
808 (void) lchown(name
, luid
, lgid
);
814 Vprintf(stdout
, "extract fifo %s\n", name
);
819 if (! (spcl
.c_flags
& DR_METAONLY
)) {
822 if (mkfifo(name
, mode
) < 0) {
823 warn("%s: cannot create fifo", name
);
828 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
829 (void) chmod(name
, mode
);
832 (void) fsetflags(name
, flags
);
834 (void) chflags(name
, flags
);
842 Vprintf(stdout
, "extract special file %s\n", name
);
847 if (! (spcl
.c_flags
& DR_METAONLY
)) {
850 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
851 warn("%s: cannot create special file", name
);
856 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
857 (void) chmod(name
, mode
);
861 warn("%s: fsetflags called on a special file", name
);
862 (void) fsetflags(name
, flags
);
865 (void) chflags(name
, flags
);
873 uid_t luid
= curfile
.dip
->di_uid
;
874 gid_t lgid
= curfile
.dip
->di_gid
;
876 Vprintf(stdout
, "extract file %s\n", name
);
881 if (! (spcl
.c_flags
& DR_METAONLY
)) {
884 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
886 warn("%s: cannot create file", name
);
890 getfile(xtrfile
, xtrskip
);
895 (void) chown(name
, luid
, lgid
);
896 (void) chmod(name
, mode
);
899 (void) fsetflags(name
, flags
);
901 (void) chflags(name
, flags
);
911 * skip over bit maps on the tape
917 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
922 * skip over a file on the tape
928 curfile
.action
= SKIP
;
929 getfile(xtrnull
, xtrnull
);
933 * Extract a file from the tape.
934 * When an allocated block is found it is passed to the fill function;
935 * when an unallocated block (hole) is found, a zeroed buffer is passed
936 * to the skip function.
939 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
942 volatile int curblk
= 0;
943 volatile quad_t size
= spcl
.c_dinode
.di_size
;
944 volatile int last_write_was_hole
= 0;
945 quad_t origsize
= size
;
946 static char clearedbuf
[MAXBSIZE
];
947 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
950 if (spcl
.c_type
== TS_END
)
951 panic("ran off end of tape\n");
952 if (spcl
.c_magic
!= NFS_MAGIC
)
953 panic("not at beginning of a file\n");
954 if (!gettingfile
&& setjmp(restart
) != 0)
958 for (i
= 0; i
< spcl
.c_count
; i
++) {
959 if (readmapflag
|| spcl
.c_addr
[i
]) {
960 readtape(&buf
[curblk
++][0]);
961 if (curblk
== fssize
/ TP_BSIZE
) {
962 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
963 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
965 last_write_was_hole
= 0;
969 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
971 (curblk
- 1) * TP_BSIZE
+ size
));
974 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
976 last_write_was_hole
= 1;
978 if ((size
-= TP_BSIZE
) <= 0) {
979 for (i
++; i
< spcl
.c_count
; i
++)
980 if (readmapflag
|| spcl
.c_addr
[i
])
985 if (gethead(&spcl
) == GOOD
&& size
> 0) {
986 if (spcl
.c_type
== TS_ADDR
)
989 "Missing address (header) block for %s at %ld blocks\n",
990 curfile
.name
, (long)blksread
);
993 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
994 last_write_was_hole
= 0;
997 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
999 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
1000 (*skip
)(clearedbuf
, skp
);
1003 last_write_was_hole
= 1;
1005 if (last_write_was_hole
) {
1006 FTRUNCATE(ofile
, origsize
);
1014 * Write out the next block of a file.
1017 xtrfile(char *buf
, size_t size
)
1022 if (write(ofile
, buf
, (int) size
) == -1)
1023 err(1, "write error extracting inode %lu, name %s\nwrite",
1024 (unsigned long)curfile
.ino
, curfile
.name
);
1028 * Skip over a hole in a file.
1032 xtrskip(UNUSED(char *buf
), size_t size
)
1035 if (LSEEK(ofile
, (OFF_T
)size
, SEEK_CUR
) == -1)
1036 err(1, "seek error extracting inode %lu, name %s\nlseek",
1037 (unsigned long)curfile
.ino
, curfile
.name
);
1041 * Collect the next block of a symbolic link.
1044 xtrlnkfile(char *buf
, size_t size
)
1048 if (pathlen
> MAXPATHLEN
)
1049 errx(1, "symbolic link name: %s->%s%s; too long %d",
1050 curfile
.name
, lnkbuf
, buf
, pathlen
);
1051 (void) strcat(lnkbuf
, buf
);
1055 * Skip over a hole in a symbolic link (should never happen).
1059 xtrlnkskip(UNUSED(char *buf
), UNUSED(size_t size
))
1062 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1066 * Collect the next block of a bit map.
1069 xtrmap(char *buf
, size_t size
)
1072 memmove(map
, buf
, size
);
1077 * Skip over a hole in a bit map (should never happen).
1081 xtrmapskip(UNUSED(char *buf
), size_t size
)
1084 panic("hole in map\n");
1089 * Noop, when an extraction function is not needed.
1093 xtrnull(UNUSED(char *buf
), UNUSED(size_t size
))
1099 #if COMPARE_ONTHEFLY
1101 * Compare the next block of a file.
1104 xtrcmpfile(char *buf
, size_t size
)
1106 static char cmpbuf
[MAXBSIZE
];
1111 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1112 fprintf(stderr
, "%s: size has changed.\n",
1118 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1119 fprintf(stderr
, "%s: tape and disk copies are different\n",
1127 * Skip over a hole in a file.
1130 xtrcmpskip(UNUSED(char *buf
), size_t size
)
1132 static char cmpbuf
[MAXBSIZE
];
1138 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1139 fprintf(stderr
, "%s: size has changed.\n",
1145 for (i
= 0; i
< (int)size
; ++i
)
1146 if (cmpbuf
[i
] != '\0') {
1147 fprintf(stderr
, "%s: tape and disk copies are different\n",
1153 #endif /* COMPARE_ONTHEFLY */
1155 #if !COMPARE_ONTHEFLY
1157 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1159 static char buf_tape
[BUFSIZ
];
1160 static char buf_disk
[BUFSIZ
];
1165 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1166 close(fd_tape
), close(fd_disk
);
1167 panic("do_cmpfiles: unexpected EOF[1]");
1169 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1170 close(fd_tape
), close(fd_disk
);
1171 panic("do_cmpfiles: unexpected EOF[2]");
1173 if (n_tape
!= n_disk
) {
1174 close(fd_tape
), close(fd_disk
);
1175 panic("do_cmpfiles: sizes different!");
1177 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1183 /* for debugging compare problems */
1184 #undef COMPARE_FAIL_KEEP_FILE
1187 #ifdef COMPARE_FAIL_KEEP_FILE
1188 /* return true if tapefile should be unlinked after compare */
1193 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1195 struct STAT sbuf_tape
;
1196 int fd_tape
, fd_disk
;
1198 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1199 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1204 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1206 "%s: size changed from %ld to %ld.\n",
1207 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1209 #ifdef COMPARE_FAIL_KEEP_FILE
1216 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1217 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1220 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1222 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1226 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1227 fprintf(stderr
, "%s: tape and disk copies are different\n",
1232 #ifdef COMPARE_FAIL_KEEP_FILE
1233 /* rename the file to live in /tmp */
1234 /* rename `tapefile' to /tmp/<basename of diskfile> */
1236 char *p
= strrchr(diskfile
, '/');
1237 char newname
[MAXPATHLEN
];
1239 panic("can't find / in %s\n", diskfile
);
1241 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1242 if (rename(tapefile
, newname
)) {
1243 panic("rename from %s to %s failed: %s\n",
1247 fprintf(stderr
, "*** %s saved to %s\n",
1252 /* don't unlink the file (it's not there anymore */
1261 #ifdef COMPARE_FAIL_KEEP_FILE
1265 #endif /* !COMPARE_ONTHEFLY */
1267 #if !COMPARE_ONTHEFLY
1268 static char tmpfilename
[MAXPATHLEN
];
1272 comparefile(char *name
)
1277 #if !COMPARE_ONTHEFLY
1278 static char *tmpfile
= NULL
;
1282 curfile
.name
= name
;
1283 curfile
.action
= USING
;
1284 mode
= curfile
.dip
->di_mode
;
1286 if ((mode
& IFMT
) == IFSOCK
) {
1287 Vprintf(stdout
, "skipped socket %s\n", name
);
1292 if ((r
= LSTAT(name
, &sb
)) != 0) {
1293 warn("%s: does not exist (%d)", name
, r
);
1299 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1300 (long)sb
.st_size
, mode
);
1302 if (sb
.st_mode
!= mode
) {
1303 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1304 name
, mode
& 07777, sb
.st_mode
& 07777);
1307 if (spcl
.c_flags
& DR_METAONLY
) {
1311 switch (mode
& IFMT
) {
1325 char lbuf
[MAXPATHLEN
+ 1];
1328 if (!(sb
.st_mode
& S_IFLNK
)) {
1329 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1336 getfile(xtrlnkfile
, xtrlnkskip
);
1339 "%s: zero length symbolic link (ignored)\n",
1344 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1345 panic("readlink of %s failed: %s", name
,
1350 if (strcmp(lbuf
, lnkbuf
) != 0) {
1352 "%s: symbolic link changed from %s to %s.\n",
1353 name
, lnkbuf
, lbuf
);
1362 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1363 fprintf(stderr
, "%s: no longer a special file\n",
1370 if (sb
.st_rdev
!= (dev_t
)curfile
.dip
->di_rdev
) {
1372 "%s: device changed from %d,%d to %d,%d.\n",
1374 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1375 (int)curfile
.dip
->di_rdev
& 0xff,
1376 ((int)sb
.st_rdev
>> 8) & 0xff,
1377 (int)sb
.st_rdev
& 0xff);
1384 #if COMPARE_ONTHEFLY
1385 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1386 panic("Can't open %s: %s\n", name
, strerror(errno
));
1392 getfile(xtrcmpfile
, xtrcmpskip
);
1395 if (read(ifile
, &c
, 1) != 0) {
1396 fprintf(stderr
, "%s: size has changed.\n",
1406 if (tmpfile
== NULL
) {
1407 /* argument to mktemp() must not be in RO space: */
1408 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1409 tmpfile
= mktemp(&tmpfilename
[0]);
1411 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1412 panic("cannot delete tmp file %s: %s\n",
1413 tmpfile
, strerror(errno
));
1415 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1416 panic("cannot create file temp file %s: %s\n",
1417 name
, strerror(errno
));
1419 getfile(xtrfile
, xtrskip
);
1420 (void) close(ofile
);
1421 #ifdef COMPARE_FAIL_KEEP_FILE
1422 if (cmpfiles(tmpfile
, name
, &sb
))
1425 cmpfiles(tmpfile
, name
, &sb
);
1428 #endif /* COMPARE_ONTHEFLY */
1434 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1435 static void (*readtape_func
)(char *) = readtape_set
;
1438 * Read TP_BSIZE blocks from the input.
1439 * Handle read errors, and end of media.
1440 * Decompress compressed blocks.
1445 (*readtape_func
)(buf
); /* call the actual processing routine */
1449 * Set function pointer for readtape() routine. zflag and magtapein must
1450 * be correctly set before the first call to readtape().
1453 readtape_set(char *buf
)
1456 readtape_func
= readtape_uncompr
;
1460 readtape_func
= readtape_comprtape
;
1462 readtape_func
= readtape_comprfile
;
1467 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1470 * This is the original readtape(), it's used for reading uncompressed input.
1471 * Read TP_BSIZE blocks from the input.
1472 * Handle read errors, and end of media.
1475 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1476 readtape_uncompr(char *buf
)
1481 ssize_t rd
, newvol
, i
;
1482 int cnt
, seek_failed
;
1484 if (blkcnt
< numtrec
) {
1485 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1491 for (i
= 0; i
< ntrec
; i
++)
1492 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1495 cnt
= ntrec
* TP_BSIZE
;
1498 if (createtapeposflag
)
1499 (void)GetTapePos(&curtapepos
);
1504 i
= rmtread(&tapebuf
[rd
], cnt
);
1507 i
= read(mt
, &tapebuf
[rd
], cnt
);
1510 * Check for mid-tape short read error.
1511 * If found, skip rest of buffer and start with the next.
1513 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1514 Dprintf(stdout
, "mid-media short read error.\n");
1518 * Handle partial block read.
1520 if (pipein
&& i
== 0 && rd
> 0)
1522 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1531 * Short read. Process the blocks read.
1533 if (i
% TP_BSIZE
!= 0)
1535 "partial block read: %ld should be %ld\n",
1536 (long)i
, ntrec
* TP_BSIZE
);
1537 numtrec
= i
/ TP_BSIZE
;
1541 * Handle read error.
1544 fprintf(stderr
, "Tape read error while ");
1545 switch (curfile
.action
) {
1547 fprintf(stderr
, "trying to set up tape\n");
1550 fprintf(stderr
, "trying to resynchronize\n");
1553 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1556 fprintf(stderr
, "skipping over inode %lu\n",
1557 (unsigned long)curfile
.ino
);
1560 if (!yflag
&& !reply("continue"))
1562 i
= ntrec
* TP_BSIZE
;
1563 memset(tapebuf
, 0, (size_t)i
);
1566 seek_failed
= (rmtseek(i
, 1) < 0);
1569 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (OFF_T
)-1);
1572 warn("continuation failed");
1573 if (!yflag
&& !reply("assume end-of-tape and continue"))
1579 * Handle end of tape.
1582 Vprintf(stdout
, "End-of-tape encountered\n");
1591 if (rd
% TP_BSIZE
!= 0)
1592 panic("partial block read: %d should be %d\n",
1593 rd
, ntrec
* TP_BSIZE
);
1595 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1598 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1603 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1606 * Read a compressed format block from a file or pipe and uncompress it.
1607 * Attempt to handle read errors, and end of file.
1610 readtape_comprfile(char *buf
)
1612 long rl
, size
, i
, ret
;
1614 struct tapebuf
*tpb
;
1616 if (blkcnt
< numtrec
) {
1617 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1622 /* need to read the next block */
1624 for (i
= 0; i
< ntrec
; i
++)
1625 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1627 tpb
= (struct tapebuf
*) tapebuf
;
1629 /* read the block prefix */
1630 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1631 converttapebuf(tpb
);
1633 if (Vflag
&& (ret
== 0 || rl
< (int)PREFIXSIZE
|| tpb
->length
== 0))
1640 if (size
> bufsize
) {
1641 /* something's wrong */
1642 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1645 tpb
->length
= bufsize
;
1647 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1651 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1652 if (tbufptr
== NULL
) {
1653 msg_read_error("File decompression error while");
1654 if (!yflag
&& !reply("continue"))
1656 memset(tapebuf
, 0, bufsize
);
1661 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1667 /* Errors while reading from a file or pipe are catastrophic. Since
1668 * there are no block boundaries, it's impossible to bypass the
1669 * block in error and find the start of the next block.
1672 /* It's possible to have multiple input files using -M
1673 * and -f file1,file2...
1675 Vprintf(stdout
, "End-of-File encountered\n");
1685 msg_read_error("Read error while");
1686 /* if (!yflag && !reply("continue")) */
1691 * Read compressed data from a tape and uncompress it.
1692 * Handle read errors, and end of media.
1693 * Since a tape consists of separate physical blocks, we try
1694 * to recover from errors by repositioning the tape to the next
1698 readtape_comprtape(char *buf
)
1702 struct tapebuf
*tpb
;
1705 if (blkcnt
< numtrec
) {
1706 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1711 /* need to read the next block */
1713 for (i
= 0; i
< ntrec
; i
++)
1714 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1716 tpb
= (struct tapebuf
*) tapebuf
;
1718 /* read the block */
1719 size
= bufsize
+ PREFIXSIZE
;
1720 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1724 converttapebuf(tpb
);
1725 tbufptr
= decompress_tapebuf(tpb
, rl
);
1726 if (tbufptr
== NULL
) {
1727 msg_read_error("Tape decompression error while");
1728 if (!yflag
&& !reply("continue"))
1730 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1736 /* Handle errors: EOT switches to the next volume, other errors
1737 * attempt to position the tape to the next block.
1740 Vprintf(stdout
, "End-of-tape encountered\n");
1749 msg_read_error("Tape read error while");
1750 if (!yflag
&& !reply("continue"))
1752 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1757 rl
= rmtioctl(MTFSR
, 1);
1763 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1767 warn("continuation failed");
1768 if (!yflag
&& !reply("assume end-of-tape and continue"))
1770 ret
= 0; /* end of tape */
1776 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1782 * Decompress a struct tapebuf into a buffer. readsize is the size read
1783 * from the tape/file and is used for error messages. Returns a pointer
1784 * to the location of the uncompressed buffer or NULL on errors.
1785 * Adjust numtrec and complain for a short block.
1788 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1790 /* If zflag is on, all blocks have a struct tapebuf prefix */
1791 /* zflag gets set in setup() from the dump header */
1792 int cresult
, blocklen
;
1793 unsigned long worklen
;
1795 unsigned int worklen2
;
1797 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1799 /* build a length error message */
1800 blocklen
= tpbin
->length
;
1801 if (readsize
< blocklen
+ (int)PREFIXSIZE
)
1802 lengtherr
= "short";
1804 if (readsize
> blocklen
+ (int)PREFIXSIZE
)
1809 if (tpbin
->compressed
) {
1810 /* uncompress whatever we read, if it fails, complain later */
1811 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1813 errx(1,"This restore version doesn't support zlib decompression");
1815 cresult
= uncompress(comprbuf
, &worklen
,
1816 tpbin
->buf
, blocklen
);
1822 reason
= "not enough memory";
1825 reason
= "buffer too small";
1828 reason
= "data error";
1833 if (cresult
== Z_OK
)
1837 #endif /* HAVE_ZLIB */
1839 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1841 errx(1,"This restore version doesn't support bzlib decompression");
1844 cresult
= BZ2_bzBuffToBuffDecompress(
1845 comprbuf
, &worklen2
,
1846 tpbin
->buf
, blocklen
, 0, 0);
1853 reason
= "not enough memory";
1855 case BZ_OUTBUFF_FULL
:
1856 reason
= "buffer too small";
1859 case BZ_DATA_ERROR_MAGIC
:
1860 case BZ_UNEXPECTED_EOF
:
1861 reason
= "data error";
1866 if (cresult
== BZ_OK
)
1870 #endif /* HAVE_BZLIB */
1872 if (tpbin
->flags
== COMPRESS_LZO
) {
1874 errx(1,"This restore version doesn't support lzo decompression");
1876 cresult
= lzo1x_decompress(tpbin
->buf
, blocklen
,
1877 comprbuf
, (lzo_uintp
) &worklen
,NULL
);
1883 case LZO_E_EOF_NOT_FOUND
:
1884 reason
= "data error";
1889 if (cresult
== LZO_E_OK
)
1893 #endif /* HAVE_LZO */
1897 output
= tpbin
->buf
;
1901 numtrec
= worklen
/ TP_BSIZE
;
1902 if (worklen
% TP_BSIZE
!= 0)
1903 reason
= "length mismatch";
1907 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1908 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1909 fprintf(stderr
, "decompression error, block %ld: %s\n",
1910 tpblksread
+1, reason
);
1918 * Print an error message for a read error.
1919 * This was exteracted from the original readtape().
1922 msg_read_error(char *m
)
1924 switch (curfile
.action
) {
1926 fprintf(stderr
, "%s trying to set up tape\n", m
);
1929 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1932 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1935 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1936 (unsigned long)curfile
.ino
);
1940 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1943 * Read the first block and get the blocksize from it. Test
1944 * for a compressed dump tape/file. setup() will make the final
1945 * determination by checking the compressed flag if gethead()
1946 * finds a valid header. The test here is necessary to offset the buffer
1947 * by the size of the compressed prefix. zflag is set here so that
1948 * readtape_set can set the correct function pointer for readtape().
1949 * Note that the first block of each tape/file is not compressed
1950 * and does not have a prefix.
1953 findtapeblksize(void)
1957 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1958 struct s_spcl spclpt
;
1960 for (i
= 0; i
< ntrec
; i
++)
1961 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1965 * For a pipe or file, read in the first record. For a tape, read
1968 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1970 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1971 errx(1, "Tape read error on first record");
1973 memcpy(&spclpt
, tapebuf
, TP_BSIZE
);
1974 if (converthead(&spclpt
) == FAIL
) {
1976 if (converthead(&spclpt
) == FAIL
) {
1977 /* Special case for old compressed tapes with prefix */
1978 if (magtapein
&& (i
% TP_BSIZE
!= 0))
1980 errx(1, "Tape is not a dump tape");
1982 fprintf(stderr
, "Converting to new file system format.\n");
1985 * If the input is from a file or a pipe, we read TP_BSIZE
1986 * bytes looking for a dump header. If the dump is compressed
1987 * we need to read in the rest of the block, as determined
1988 * by c_ntrec in the dump header. The first block of the
1989 * dump is not compressed and does not have a prefix.
1992 if (spclpt
.c_type
== TS_TAPE
1993 && spclpt
.c_flags
& DR_COMPRESSED
) {
1994 /* It's a compressed dump file, read in the */
1995 /* rest of the block based on spclpt.c_ntrec. */
1996 if (spclpt
.c_ntrec
> ntrec
)
1997 errx(1, "Tape blocksize is too large, use "
1998 "\'-b %d\' ", spclpt
.c_ntrec
);
1999 ntrec
= spclpt
.c_ntrec
;
2000 len
= (ntrec
- 1) * TP_BSIZE
;
2004 /* read in the rest of the block based on bufsize */
2005 len
= bufsize
- TP_BSIZE
;
2007 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
2008 || (i
!= (long)len
&& i
% TP_BSIZE
!= 0))
2009 errx(1,"Error reading dump file header");
2012 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
2014 } /* if (!magtapein) */
2017 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
2018 * If the value of ntrec is too large, we read less than
2019 * what we asked for; adjust the value of ntrec and test for
2020 * a compressed dump tape.
2022 if (i
% TP_BSIZE
!= 0) {
2024 /* may be old format compressed dump tape with a prefix */
2025 memcpy(&spclpt
, tpb
->buf
, TP_BSIZE
);
2027 if (converthead(&spclpt
) == FAIL
) {
2029 if (converthead(&spclpt
) == FAIL
)
2030 errx(1, "Tape is not a dump tape");
2031 fprintf(stderr
, "Converting to new file system format.\n");
2033 if (i
% TP_BSIZE
== PREFIXSIZE
2034 && tpb
->compressed
== 0
2035 && spclpt
.c_type
== TS_TAPE
2036 && spclpt
.c_flags
& DR_COMPRESSED
) {
2039 if (tpb
->length
> bufsize
)
2040 errx(1, "Tape blocksize is too large, use "
2041 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
2044 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2047 ntrec
= i
/ TP_BSIZE
;
2048 if (spclpt
.c_type
== TS_TAPE
) {
2049 if (spclpt
.c_flags
& DR_COMPRESSED
)
2051 if (spclpt
.c_ntrec
> ntrec
)
2052 errx(1, "Tape blocksize is too large, use "
2053 "\'-b %d\' ", spclpt
.c_ntrec
);
2056 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
2060 * Read a block of data handling all of the messy details.
2062 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
2070 i
= rmtread(buf
, size
);
2073 i
= read(fd
, buf
, size
);
2076 break; /* EOD or error */
2079 break; /* block at a time for mt */
2082 *lengthread
= len
- size
;
2101 setmagtapein(void) {
2102 struct mtget mt_stat
;
2103 static int done
= 0;
2108 /* need to know if input is really from a tape */
2114 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2117 Vprintf(stdout
,"Input is from a %s %s\n",
2118 host
? "remote" : "local",
2119 magtapein
? "tape" :
2120 Vflag
? "multi-volume (no tape)" : "file/pipe");
2124 * Read the next block from the tape.
2125 * Check to see if it is one of several vintage headers.
2126 * If it is an old style header, convert it to a new style header.
2127 * If it is not any valid header, return an error.
2130 gethead(struct s_spcl
*buf
)
2132 readtape((char *)buf
);
2133 return converthead(buf
);
2137 converthead(struct s_spcl
*buf
)
2145 char dummy
[TP_BSIZE
];
2152 u_int16_t c_inumber
;
2157 u_int16_t odi_nlink
;
2173 if (buf
->c_magic
!= NFS_MAGIC
) {
2174 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2177 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2181 if (checksum((int *)buf
) == FAIL
)
2184 swabst((u_char
*)"8i4s31i528bi192b3i", (u_char
*)buf
);
2187 memcpy(&u_ospcl
.s_ospcl
, buf
, TP_BSIZE
);
2188 memset((char *)buf
, 0, (long)TP_BSIZE
);
2189 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2190 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2191 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2192 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2193 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2194 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2195 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2196 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2197 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2198 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2199 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2200 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2201 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2202 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2203 #if defined(__linux__) || defined(sunos)
2204 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2205 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2206 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2207 #else /* __linux__ || sunos */
2208 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2209 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2210 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2211 #endif /* __linux__ || sunos */
2212 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2213 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2214 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2215 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2217 buf
->c_magic
= NFS_MAGIC
;
2220 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2221 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2222 qcvt
.qval
= buf
->c_dinode
.di_size
;
2223 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2224 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2229 qcvt
.qval
= buf
->c_dinode
.di_size
;
2231 qcvt
.val
[1] = qcvt
.val
[0];
2233 buf
->c_dinode
.di_size
= qcvt
.qval
;
2237 switch (buf
->c_type
) {
2242 * Have to patch up missing information in bit map headers
2245 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2246 if (buf
->c_count
> TP_NINDIR
)
2249 for (i
= 0; i
< buf
->c_count
; i
++)
2254 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2259 if (buf
->c_flags
& DR_INODEINFO
) {
2260 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2262 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2271 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2275 * If we are restoring a filesystem with old format inodes,
2276 * copy the uid/gid to the new location.
2279 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2280 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2288 converttapebuf(struct tapebuf
*tpb
)
2292 unsigned int length
:28;
2293 unsigned int flags
:3;
2294 unsigned int compressed
:1;
2296 swabst((u_char
*)"i", (u_char
*)tpb
);
2297 memcpy(&tb
, tpb
, 4);
2298 tpb
->length
= tb
.length
;
2299 tpb
->flags
= tb
.flags
;
2300 tpb
->compressed
= tb
.compressed
;
2305 * Check that a header is where it belongs and predict the next header
2308 accthdr(struct s_spcl
*header
)
2310 static dump_ino_t previno
= 0x7fffffff;
2311 static int prevtype
;
2312 static long predict
;
2315 if (header
->c_type
== TS_TAPE
) {
2316 fprintf(stderr
, "Volume header (%s inode format) ",
2317 oldinofmt
? "old" : "new");
2318 if (header
->c_firstrec
)
2319 fprintf(stderr
, "begins with record %d",
2320 header
->c_firstrec
);
2321 fprintf(stderr
, "\n");
2322 previno
= 0x7fffffff;
2325 if (previno
== 0x7fffffff)
2329 fprintf(stderr
, "Dumped inodes map header");
2332 fprintf(stderr
, "Used inodes map header");
2335 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2338 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2341 fprintf(stderr
, "End of tape header");
2344 if (predict
!= blksread
- 1)
2345 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2346 predict
, blksread
- 1);
2347 fprintf(stderr
, "\n");
2350 if (header
->c_type
!= TS_END
)
2351 for (i
= 0; i
< header
->c_count
; i
++)
2352 if (readmapflag
|| header
->c_addr
[i
] != 0)
2356 prevtype
= header
->c_type
;
2357 previno
= header
->c_inumber
;
2361 * Find an inode header.
2362 * Complain if had to skip, and complain is set.
2365 findinode(struct s_spcl
*header
)
2367 static long skipcnt
= 0;
2371 curfile
.name
= "<name unknown>";
2372 curfile
.action
= UNKNOWN
;
2376 if (header
->c_magic
!= NFS_MAGIC
) {
2378 while (gethead(header
) == FAIL
||
2379 header
->c_date
!= dumpdate
)
2382 switch (header
->c_type
) {
2386 * Skip up to the beginning of the next record
2388 for (i
= 0; i
< header
->c_count
; i
++)
2389 if (header
->c_addr
[i
])
2391 while (gethead(header
) == FAIL
||
2392 header
->c_date
!= dumpdate
)
2397 curfile
.dip
= &header
->c_dinode
;
2398 curfile
.ino
= header
->c_inumber
;
2402 curfile
.ino
= maxino
;
2406 curfile
.name
= "<file removal list>";
2410 curfile
.name
= "<file dump list>";
2414 panic("unexpected tape header\n");
2418 panic("unknown tape header type %d\n", spcl
.c_type
);
2422 } while (header
->c_type
== TS_ADDR
);
2427 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2437 j
= sizeof(union u_spcl
) / sizeof(int);
2444 /* What happens if we want to read restore tapes
2445 for a 16bit int machine??? */
2451 if (i
!= CHECKSUM
) {
2452 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2453 (unsigned long)curfile
.ino
, curfile
.name
);
2463 #include <varargs.h>
2468 msg(const char *fmt
, ...)
2481 (void)vfprintf(stderr
, fmt
, ap
);
2484 #endif /* RRESTORE */
2487 swab16(u_char
*sp
, int n
)
2492 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2499 swab32(u_char
*sp
, int n
)
2504 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2505 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2512 swab64(u_char
*sp
, int n
)
2517 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2518 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2519 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2520 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2527 swabst(u_char
*cp
, u_char
*sp
)
2533 case '0': case '1': case '2': case '3': case '4':
2534 case '5': case '6': case '7': case '8': case '9':
2535 n
= (n
* 10) + (*cp
++ - '0');
2538 case 's': case 'w': case 'h':
2556 default: /* Any other character, like 'b' counts as byte. */
2570 swabst((u_char
*)"i", (u_char
*)&x
);
2578 swabst((u_char
*)"l", (u_char
*)&x
);
2585 * get the current position of the tape
2588 GetTapePos(long long *pos
)
2594 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2603 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2604 *pos
= (long long)mtpos
;
2607 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2613 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2614 (unsigned long)getpid(), err
, *pos
);
2620 typedef struct mt_pos
{
2623 } MTPosRec
, *MTPosPtr
;
2626 * go to specified position on tape
2629 GotoTapePos(long long pos
)
2635 err
= (rmtseek(pos
, SEEK_SET
) < 0);
2642 buf
.mt_count
= (int) pos
;
2643 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2646 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2652 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2653 (unsigned long)getpid(), err
, pos
);
2660 * read next data from tape to re-sync
2663 ReReadFromTape(void)
2667 if (gethead(&spcl
) == FAIL
) {
2669 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2677 ReReadInodeFromTape(dump_ino_t theino
)
2686 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
) && (cntloop
< ntrec
));
2688 fprintf(stderr
, "%ld reads\n", cntloop
);
2689 if (cntloop
== ntrec
) {
2690 fprintf(stderr
, "DEBUG: bufsize %d\n", bufsize
);
2691 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
2697 #endif /* USE_QFA */
2700 RequestVol(long tnum
)