]>
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.72 2003/02/17 11:21:29 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 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)
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)
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 */
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)
305 readtape_func
= readtape_set
;
309 if (gethead(&spcl
) == FAIL
) {
310 blkcnt
--; /* push back this block */
314 if (gethead(&spcl
) == FAIL
)
315 errx(1, "Tape is not a dump tape");
316 fprintf(stderr
, "Converting to new file system format.\n");
320 fprintf(stderr
, "Dump tape is compressed.\n");
321 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
322 errx(1,"This restore version doesn't support decompression");
323 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
326 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
327 endoftapemark
.s_spcl
.c_type
= TS_END
;
328 ip
= (int *)&endoftapemark
;
329 j
= sizeof(union u_spcl
) / sizeof(int);
334 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
336 if (vflag
|| command
== 't' || command
== 'C')
339 if (tapeposflag
&& (unsigned long)spcl
.c_date
!= qfadumpdate
)
340 errx(1, "different QFA/dumpdates detected\n");
342 if (filesys
[0] == '\0') {
344 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
345 filesys
[NAMELEN
- 1] = '\0';
346 dirptr
= strstr(filesys
, " (dir");
350 dumptime
= spcl
.c_ddate
;
351 dumpdate
= spcl
.c_date
;
352 if (STAT(".", &stbuf
) < 0)
353 err(1, "cannot stat .");
354 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
356 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
357 fssize
= stbuf
.st_blksize
;
358 if (((fssize
- 1) & fssize
) != 0)
359 errx(1, "bad block size %ld", fssize
);
360 if (spcl
.c_volume
!= 1)
361 errx(1, "Tape is not volume 1 of the dump");
362 if (gethead(&spcl
) == FAIL
) {
363 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
364 panic("no header after volume mark!\n");
368 if (spcl
.c_type
!= TS_CLRI
)
369 errx(1, "Cannot find file removal list");
370 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
371 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
373 errx(1, "no memory for active inode map");
375 curfile
.action
= USING
;
376 getfile(xtrmap
, xtrmapskip
);
377 while (spcl
.c_type
== TS_ADDR
) {
378 /* Recompute maxino and the map */
379 dump_ino_t oldmaxino
= maxino
;
380 maxino
+= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
381 resizemaps(oldmaxino
, maxino
);
383 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
384 getfile(xtrmap
, xtrmapskip
);
386 Dprintf(stdout
, "maxino = %lu\n", (unsigned long)maxino
);
387 if (spcl
.c_type
!= TS_BITS
)
388 errx(1, "Cannot find file dump list");
389 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
390 if (map
== (char *)NULL
)
391 errx(1, "no memory for file dump list");
393 curfile
.action
= USING
;
394 getfile(xtrmap
, xtrmapskip
);
395 while (spcl
.c_type
== TS_ADDR
) {
396 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
397 getfile(xtrmap
, xtrmapskip
);
400 * If there may be whiteout entries on the tape, pretend that the
401 * whiteout inode exists, so that the whiteout entries can be
405 SETINO(WINO
, dumpmap
);
411 * Prompt user to load a new dump volume.
412 * "Nextvol" is the next suggested volume to use.
413 * This suggested volume is enforced when doing full
414 * or incremental restores, but can be overridden by
415 * the user when only extracting a subset of the files.
420 long newvol
= 0, wantnext
= 0, i
;
421 long saved_blksread
= 0, saved_tpblksread
= 0;
422 union u_spcl tmpspcl
;
423 # define tmpbuf tmpspcl.s_spcl
425 int haderror
= 0, bot_code
= 1;
435 panic("Changing volumes on pipe input?\n");
440 saved_blksread
= blksread
;
441 saved_tpblksread
= tpblksread
;
442 #if defined(USE_QFA) && defined(sunos)
443 if (createtapeposflag
|| tapeposflag
)
448 exit(1); /* pipes do not get a second chance */
449 if (aflag
|| curfile
.action
!= SKIP
) {
456 while (newvol
<= 0) {
457 if (tapesread
== 0) {
458 fprintf(stderr
, "%s%s%s%s%s",
459 "You have not read any volumes yet.\n",
460 "Unless you know which volume your",
461 " file(s) are on you should start\n",
462 "with the last volume and work",
463 " towards the first.\n");
465 fprintf(stderr
, "You have read volumes");
467 for (i
= 1; i
< 32; i
++)
468 if (tapesread
& (1 << i
)) {
469 fprintf(stderr
, "%s%ld", buf
, (long)i
);
472 fprintf(stderr
, "\n");
475 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
476 (void) fflush(stderr
);
477 (void) fgets(buf
, TP_BSIZE
, terminal
);
478 } while (!feof(terminal
) && buf
[0] == '\n');
481 if (!strcmp(buf
, "none\n")) {
488 "Volume numbers are positive numerics\n");
491 if (newvol
== volno
) {
492 tapesread
|= 1 << volno
;
493 #if defined(USE_QFA) && defined(sunos)
494 if (createtapeposflag
|| tapeposflag
) {
495 if (OpenSMTCmt(magtape
) < 0) {
507 * if using an archive file, reset its name so readtape()
508 * could properly use remote access.
513 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
514 magtape
[MAXPATHLEN
- 1] = '\0';
516 if (bot_script
&& !haderror
) {
517 msg("Launching %s\n", bot_script
);
518 bot_code
= system_command(bot_script
, magtape
, newvol
);
519 if (bot_code
!= 0 && bot_code
!= 1) {
520 msg("Restore aborted by the beginning of tape script\n");
524 if (haderror
|| (bot_code
&& !Mflag
)) {
526 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
527 fprintf(stderr
, "Enter ``none'' if there are no more volumes\n");
528 fprintf(stderr
, "otherwise enter volume name (default: %s) ", magtape
);
529 (void) fflush(stderr
);
530 (void) fgets(buf
, TP_BSIZE
, terminal
);
533 if (!strcmp(buf
, "none\n")) {
537 if (buf
[0] != '\n') {
539 (void) strncpy(magtape
, buf
, sizeof(magtape
));
540 magtape
[sizeof(magtape
) - 1] = '\0';
541 if ((pos
= strchr(magtape
, '\n')))
542 magtape
[pos
- magtape
] = '\0';
547 mt
= rmtopen(magtape
, O_RDONLY
);
550 mt
= OPEN(magtape
, O_RDONLY
, 0);
553 fprintf(stderr
, "Cannot open %s\n", magtape
);
560 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
561 readtape_func
= readtape_set
;
567 if (gethead(&tmpbuf
) == FAIL
) {
568 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
569 fprintf(stderr
, "tape is not dump tape\n");
572 blksread
= saved_blksread
;
573 tpblksread
= saved_tpblksread
;
576 if (tmpbuf
.c_volume
!= volno
) {
577 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
580 blksread
= saved_blksread
;
581 tpblksread
= saved_tpblksread
;
584 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
585 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
586 ctime4(&tmpbuf
.c_date
));
587 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
590 blksread
= saved_blksread
;
591 tpblksread
= saved_tpblksread
;
594 tapesread
|= 1 << volno
;
596 * If continuing from the previous volume, skip over any
597 * blocks read already at the end of the previous volume.
599 * If coming to this volume at random, skip to the beginning
600 * of the next record.
603 fprintf(stderr
, "Dump tape is compressed.\n");
604 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
605 errx(1,"This restore version doesn't support decompression");
606 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
608 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
609 tpblksread
- 1, (long)tmpbuf
.c_firstrec
);
610 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
612 tpblksread
= tmpbuf
.c_firstrec
+ 1;
613 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
615 } else if (tmpbuf
.c_firstrec
> 0 &&
616 tmpbuf
.c_firstrec
< tpblksread
- 1) {
618 * -1 since we've read the volume header
620 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
621 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
622 (long)i
, i
> 1 ? "s" : "");
627 if (curfile
.action
== USING
) {
629 panic("active file into volume 1\n");
633 * Skip up to the beginning of the next record
635 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
636 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
638 (void) gethead(&spcl
);
642 siglongjmp(restart
, 1);
647 * Handle unexpected EOF.
653 if (gettingfile
&& curfile
.action
== USING
) {
654 printf("Warning: %s %s\n",
655 "End-of-input encountered while extracting", curfile
.name
);
657 curfile
.name
= "<name unknown>";
658 curfile
.action
= UNKNOWN
;
660 curfile
.ino
= maxino
;
663 siglongjmp(restart
, 1);
668 * handle multiple dumps per tape by skipping forward to the
676 if (dumpnum
== 1 || volno
!= 1)
679 errx(1, "Cannot have multiple dumps on pipe input");
681 tcom
.mt_count
= dumpnum
- 1;
684 rmtioctl(MTFSF
, dumpnum
- 1);
687 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
694 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
695 fprintf(stdout
, "Dumped from: %s",
696 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
697 if (spcl
.c_host
[0] == '\0')
699 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
700 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
701 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
709 if (volinfo
[1] == ROOTINO
) {
710 printf("Starting inode numbers by volume:\n");
711 for (i
= 1; i
< (int)TP_NINOS
&& volinfo
[i
] != 0; ++i
)
712 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
718 time_t tv_sec
; /* seconds */
719 suseconds_t tv_usec
; /* and microseconds */
724 extractfile(struct entry
*ep
, int doremove
)
728 struct timeval timep
[2];
729 char *name
= myname(ep
);
731 /* If removal is requested (-r mode) do remove it unless
732 * we are extracting a metadata only inode */
733 if (spcl
.c_flags
& DR_METAONLY
) {
734 Vprintf(stdout
, "file %s is metadata only\n", name
);
739 ep
->e_flags
&= ~REMOVED
;
744 curfile
.action
= USING
;
745 #if defined(__linux__) || defined(sunos)
746 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
747 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
748 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
749 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
750 #else /* __linux__ || sunos */
751 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
752 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
753 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
754 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
755 #endif /* __linux__ */
756 mode
= curfile
.dip
->di_mode
;
757 flags
= curfile
.dip
->di_flags
;
758 switch (mode
& IFMT
) {
761 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
766 Vprintf(stdout
, "skipped socket %s\n", name
);
772 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
773 panic("unextracted directory %s\n", name
);
777 Vprintf(stdout
, "extract file %s\n", name
);
778 return (genliteraldir(name
, curfile
.ino
));
783 uid_t luid
= curfile
.dip
->di_uid
;
784 gid_t lgid
= curfile
.dip
->di_gid
;
786 if (! (spcl
.c_flags
& DR_METAONLY
)) {
789 getfile(xtrlnkfile
, xtrlnkskip
);
792 "%s: zero length symbolic link (ignored)\n", name
);
795 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
802 (void) lchown(name
, luid
, lgid
);
808 Vprintf(stdout
, "extract fifo %s\n", name
);
813 if (! (spcl
.c_flags
& DR_METAONLY
)) {
816 if (mkfifo(name
, mode
) < 0) {
817 warn("%s: cannot create fifo", name
);
822 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
823 (void) chmod(name
, mode
);
826 (void) fsetflags(name
, flags
);
828 (void) chflags(name
, flags
);
836 Vprintf(stdout
, "extract special file %s\n", name
);
841 if (! (spcl
.c_flags
& DR_METAONLY
)) {
844 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
845 warn("%s: cannot create special file", name
);
850 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
851 (void) chmod(name
, mode
);
855 warn("%s: fsetflags called on a special file", name
);
856 (void) fsetflags(name
, flags
);
859 (void) chflags(name
, flags
);
867 uid_t luid
= curfile
.dip
->di_uid
;
868 gid_t lgid
= curfile
.dip
->di_gid
;
870 Vprintf(stdout
, "extract file %s\n", name
);
875 if (! (spcl
.c_flags
& DR_METAONLY
)) {
878 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
880 warn("%s: cannot create file", name
);
884 getfile(xtrfile
, xtrskip
);
889 (void) chown(name
, luid
, lgid
);
890 (void) chmod(name
, mode
);
893 (void) fsetflags(name
, flags
);
895 (void) chflags(name
, flags
);
905 * skip over bit maps on the tape
911 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
916 * skip over a file on the tape
922 curfile
.action
= SKIP
;
923 getfile(xtrnull
, xtrnull
);
927 * Extract a file from the tape.
928 * When an allocated block is found it is passed to the fill function;
929 * when an unallocated block (hole) is found, a zeroed buffer is passed
930 * to the skip function.
933 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
936 volatile int curblk
= 0;
937 volatile quad_t size
= spcl
.c_dinode
.di_size
;
938 volatile int last_write_was_hole
= 0;
939 quad_t origsize
= size
;
940 static char clearedbuf
[MAXBSIZE
];
941 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
944 if (spcl
.c_type
== TS_END
)
945 panic("ran off end of tape\n");
946 if (spcl
.c_magic
!= NFS_MAGIC
)
947 panic("not at beginning of a file\n");
948 if (!gettingfile
&& setjmp(restart
) != 0)
952 for (i
= 0; i
< spcl
.c_count
; i
++) {
953 if (readmapflag
|| spcl
.c_addr
[i
]) {
954 readtape(&buf
[curblk
++][0]);
955 if (curblk
== fssize
/ TP_BSIZE
) {
956 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
957 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
959 last_write_was_hole
= 0;
963 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
965 (curblk
- 1) * TP_BSIZE
+ size
));
968 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
970 last_write_was_hole
= 1;
972 if ((size
-= TP_BSIZE
) <= 0) {
973 for (i
++; i
< spcl
.c_count
; i
++)
974 if (readmapflag
|| spcl
.c_addr
[i
])
979 if (gethead(&spcl
) == GOOD
&& size
> 0) {
980 if (spcl
.c_type
== TS_ADDR
)
983 "Missing address (header) block for %s at %ld blocks\n",
984 curfile
.name
, (long)blksread
);
987 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
988 last_write_was_hole
= 0;
991 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
993 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
994 (*skip
)(clearedbuf
, skp
);
997 last_write_was_hole
= 1;
999 if (last_write_was_hole
) {
1000 FTRUNCATE(ofile
, origsize
);
1008 * Write out the next block of a file.
1011 xtrfile(char *buf
, size_t size
)
1016 if (write(ofile
, buf
, (int) size
) == -1)
1017 err(1, "write error extracting inode %lu, name %s\nwrite",
1018 (unsigned long)curfile
.ino
, curfile
.name
);
1022 * Skip over a hole in a file.
1026 xtrskip(UNUSED(char *buf
), size_t size
)
1029 if (LSEEK(ofile
, (OFF_T
)size
, SEEK_CUR
) == -1)
1030 err(1, "seek error extracting inode %lu, name %s\nlseek",
1031 (unsigned long)curfile
.ino
, curfile
.name
);
1035 * Collect the next block of a symbolic link.
1038 xtrlnkfile(char *buf
, size_t size
)
1042 if (pathlen
> MAXPATHLEN
)
1043 errx(1, "symbolic link name: %s->%s%s; too long %d",
1044 curfile
.name
, lnkbuf
, buf
, pathlen
);
1045 (void) strcat(lnkbuf
, buf
);
1049 * Skip over a hole in a symbolic link (should never happen).
1053 xtrlnkskip(UNUSED(char *buf
), UNUSED(size_t size
))
1056 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1060 * Collect the next block of a bit map.
1063 xtrmap(char *buf
, size_t size
)
1066 memmove(map
, buf
, size
);
1071 * Skip over a hole in a bit map (should never happen).
1075 xtrmapskip(UNUSED(char *buf
), size_t size
)
1078 panic("hole in map\n");
1083 * Noop, when an extraction function is not needed.
1087 xtrnull(UNUSED(char *buf
), UNUSED(size_t size
))
1093 #if COMPARE_ONTHEFLY
1095 * Compare the next block of a file.
1098 xtrcmpfile(char *buf
, size_t size
)
1100 static char cmpbuf
[MAXBSIZE
];
1105 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1106 fprintf(stderr
, "%s: size has changed.\n",
1112 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1113 fprintf(stderr
, "%s: tape and disk copies are different\n",
1121 * Skip over a hole in a file.
1124 xtrcmpskip(UNUSED(char *buf
), size_t size
)
1126 static char cmpbuf
[MAXBSIZE
];
1132 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1133 fprintf(stderr
, "%s: size has changed.\n",
1139 for (i
= 0; i
< (int)size
; ++i
)
1140 if (cmpbuf
[i
] != '\0') {
1141 fprintf(stderr
, "%s: tape and disk copies are different\n",
1147 #endif /* COMPARE_ONTHEFLY */
1149 #if !COMPARE_ONTHEFLY
1151 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1153 static char buf_tape
[BUFSIZ
];
1154 static char buf_disk
[BUFSIZ
];
1159 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1160 close(fd_tape
), close(fd_disk
);
1161 panic("do_cmpfiles: unexpected EOF[1]");
1163 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1164 close(fd_tape
), close(fd_disk
);
1165 panic("do_cmpfiles: unexpected EOF[2]");
1167 if (n_tape
!= n_disk
) {
1168 close(fd_tape
), close(fd_disk
);
1169 panic("do_cmpfiles: sizes different!");
1171 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1177 /* for debugging compare problems */
1178 #undef COMPARE_FAIL_KEEP_FILE
1181 #ifdef COMPARE_FAIL_KEEP_FILE
1182 /* return true if tapefile should be unlinked after compare */
1187 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1189 struct STAT sbuf_tape
;
1190 int fd_tape
, fd_disk
;
1192 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1193 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1198 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1200 "%s: size changed from %ld to %ld.\n",
1201 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1203 #ifdef COMPARE_FAIL_KEEP_FILE
1210 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1211 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1214 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1216 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1220 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1221 fprintf(stderr
, "%s: tape and disk copies are different\n",
1226 #ifdef COMPARE_FAIL_KEEP_FILE
1227 /* rename the file to live in /tmp */
1228 /* rename `tapefile' to /tmp/<basename of diskfile> */
1230 char *p
= strrchr(diskfile
, '/');
1231 char newname
[MAXPATHLEN
];
1233 panic("can't find / in %s\n", diskfile
);
1235 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1236 if (rename(tapefile
, newname
)) {
1237 panic("rename from %s to %s failed: %s\n",
1241 fprintf(stderr
, "*** %s saved to %s\n",
1246 /* don't unlink the file (it's not there anymore */
1255 #ifdef COMPARE_FAIL_KEEP_FILE
1259 #endif /* !COMPARE_ONTHEFLY */
1261 #if !COMPARE_ONTHEFLY
1262 static char tmpfilename
[MAXPATHLEN
];
1266 comparefile(char *name
)
1271 #if !COMPARE_ONTHEFLY
1272 static char *tmpfile
= NULL
;
1276 curfile
.name
= name
;
1277 curfile
.action
= USING
;
1278 mode
= curfile
.dip
->di_mode
;
1280 if ((mode
& IFMT
) == IFSOCK
) {
1281 Vprintf(stdout
, "skipped socket %s\n", name
);
1286 if ((r
= LSTAT(name
, &sb
)) != 0) {
1287 warn("%s: does not exist (%d)", name
, r
);
1293 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1294 (long)sb
.st_size
, mode
);
1296 if (sb
.st_mode
!= mode
) {
1297 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1298 name
, mode
& 07777, sb
.st_mode
& 07777);
1301 if (spcl
.c_flags
& DR_METAONLY
) {
1305 switch (mode
& IFMT
) {
1319 char lbuf
[MAXPATHLEN
+ 1];
1322 if (!(sb
.st_mode
& S_IFLNK
)) {
1323 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1330 getfile(xtrlnkfile
, xtrlnkskip
);
1333 "%s: zero length symbolic link (ignored)\n",
1338 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1339 panic("readlink of %s failed: %s", name
,
1344 if (strcmp(lbuf
, lnkbuf
) != 0) {
1346 "%s: symbolic link changed from %s to %s.\n",
1347 name
, lnkbuf
, lbuf
);
1356 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1357 fprintf(stderr
, "%s: no longer a special file\n",
1364 if (sb
.st_rdev
!= (dev_t
)curfile
.dip
->di_rdev
) {
1366 "%s: device changed from %d,%d to %d,%d.\n",
1368 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1369 (int)curfile
.dip
->di_rdev
& 0xff,
1370 ((int)sb
.st_rdev
>> 8) & 0xff,
1371 (int)sb
.st_rdev
& 0xff);
1378 #if COMPARE_ONTHEFLY
1379 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1380 panic("Can't open %s: %s\n", name
, strerror(errno
));
1386 getfile(xtrcmpfile
, xtrcmpskip
);
1389 if (read(ifile
, &c
, 1) != 0) {
1390 fprintf(stderr
, "%s: size has changed.\n",
1400 if (tmpfile
== NULL
) {
1401 /* argument to mktemp() must not be in RO space: */
1402 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1403 tmpfile
= mktemp(&tmpfilename
[0]);
1405 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1406 panic("cannot delete tmp file %s: %s\n",
1407 tmpfile
, strerror(errno
));
1409 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1410 panic("cannot create file temp file %s: %s\n",
1411 name
, strerror(errno
));
1413 getfile(xtrfile
, xtrskip
);
1414 (void) close(ofile
);
1415 #ifdef COMPARE_FAIL_KEEP_FILE
1416 if (cmpfiles(tmpfile
, name
, &sb
))
1419 cmpfiles(tmpfile
, name
, &sb
);
1422 #endif /* COMPARE_ONTHEFLY */
1428 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1429 static void (*readtape_func
)(char *) = readtape_set
;
1432 * Read TP_BSIZE blocks from the input.
1433 * Handle read errors, and end of media.
1434 * Decompress compressed blocks.
1439 (*readtape_func
)(buf
); /* call the actual processing routine */
1443 * Set function pointer for readtape() routine. zflag and magtapein must
1444 * be correctly set before the first call to readtape().
1447 readtape_set(char *buf
)
1450 readtape_func
= readtape_uncompr
;
1454 readtape_func
= readtape_comprtape
;
1456 readtape_func
= readtape_comprfile
;
1461 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1464 * This is the original readtape(), it's used for reading uncompressed input.
1465 * Read TP_BSIZE blocks from the input.
1466 * Handle read errors, and end of media.
1469 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1470 readtape_uncompr(char *buf
)
1475 ssize_t rd
, newvol
, i
;
1476 int cnt
, seek_failed
;
1478 if (blkcnt
< numtrec
) {
1479 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1485 for (i
= 0; i
< ntrec
; i
++)
1486 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1489 cnt
= ntrec
* TP_BSIZE
;
1492 if (createtapeposflag
)
1493 (void)GetTapePos(&curtapepos
);
1498 i
= rmtread(&tapebuf
[rd
], cnt
);
1501 i
= read(mt
, &tapebuf
[rd
], cnt
);
1504 * Check for mid-tape short read error.
1505 * If found, skip rest of buffer and start with the next.
1507 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1508 Dprintf(stdout
, "mid-media short read error.\n");
1512 * Handle partial block read.
1514 if (pipein
&& i
== 0 && rd
> 0)
1516 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1525 * Short read. Process the blocks read.
1527 if (i
% TP_BSIZE
!= 0)
1529 "partial block read: %ld should be %ld\n",
1530 (long)i
, ntrec
* TP_BSIZE
);
1531 numtrec
= i
/ TP_BSIZE
;
1535 * Handle read error.
1538 fprintf(stderr
, "Tape read error while ");
1539 switch (curfile
.action
) {
1541 fprintf(stderr
, "trying to set up tape\n");
1544 fprintf(stderr
, "trying to resynchronize\n");
1547 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1550 fprintf(stderr
, "skipping over inode %lu\n",
1551 (unsigned long)curfile
.ino
);
1554 if (!yflag
&& !reply("continue"))
1556 i
= ntrec
* TP_BSIZE
;
1557 memset(tapebuf
, 0, (size_t)i
);
1560 seek_failed
= (rmtseek(i
, 1) < 0);
1563 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (OFF_T
)-1);
1566 warn("continuation failed");
1567 if (!yflag
&& !reply("assume end-of-tape and continue"))
1573 * Handle end of tape.
1576 Vprintf(stdout
, "End-of-tape encountered\n");
1585 if (rd
% TP_BSIZE
!= 0)
1586 panic("partial block read: %d should be %d\n",
1587 rd
, ntrec
* TP_BSIZE
);
1589 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1592 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1597 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1600 * Read a compressed format block from a file or pipe and uncompress it.
1601 * Attempt to handle read errors, and end of file.
1604 readtape_comprfile(char *buf
)
1606 long rl
, size
, i
, ret
;
1608 struct tapebuf
*tpb
;
1610 if (blkcnt
< numtrec
) {
1611 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1616 /* need to read the next block */
1618 for (i
= 0; i
< ntrec
; i
++)
1619 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1621 tpb
= (struct tapebuf
*) tapebuf
;
1623 /* read the block prefix */
1624 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1625 converttapebuf(tpb
);
1627 if (Vflag
&& (ret
== 0 || rl
< (int)PREFIXSIZE
|| tpb
->length
== 0))
1634 if (size
> bufsize
) {
1635 /* something's wrong */
1636 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1639 tpb
->length
= bufsize
;
1641 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1645 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1646 if (tbufptr
== NULL
) {
1647 msg_read_error("File decompression error while");
1648 if (!yflag
&& !reply("continue"))
1650 memset(tapebuf
, 0, bufsize
);
1655 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1661 /* Errors while reading from a file or pipe are catastrophic. Since
1662 * there are no block boundaries, it's impossible to bypass the
1663 * block in error and find the start of the next block.
1666 /* It's possible to have multiple input files using -M
1667 * and -f file1,file2...
1669 Vprintf(stdout
, "End-of-File encountered\n");
1679 msg_read_error("Read error while");
1680 /* if (!yflag && !reply("continue")) */
1685 * Read compressed data from a tape and uncompress it.
1686 * Handle read errors, and end of media.
1687 * Since a tape consists of separate physical blocks, we try
1688 * to recover from errors by repositioning the tape to the next
1692 readtape_comprtape(char *buf
)
1696 struct tapebuf
*tpb
;
1699 if (blkcnt
< numtrec
) {
1700 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1705 /* need to read the next block */
1707 for (i
= 0; i
< ntrec
; i
++)
1708 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1710 tpb
= (struct tapebuf
*) tapebuf
;
1712 /* read the block */
1713 size
= bufsize
+ PREFIXSIZE
;
1714 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1718 converttapebuf(tpb
);
1719 tbufptr
= decompress_tapebuf(tpb
, rl
);
1720 if (tbufptr
== NULL
) {
1721 msg_read_error("Tape decompression error while");
1722 if (!yflag
&& !reply("continue"))
1724 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1730 /* Handle errors: EOT switches to the next volume, other errors
1731 * attempt to position the tape to the next block.
1734 Vprintf(stdout
, "End-of-tape encountered\n");
1743 msg_read_error("Tape read error while");
1744 if (!yflag
&& !reply("continue"))
1746 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1751 rl
= rmtioctl(MTFSR
, 1);
1757 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1761 warn("continuation failed");
1762 if (!yflag
&& !reply("assume end-of-tape and continue"))
1764 ret
= 0; /* end of tape */
1770 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1776 * Decompress a struct tapebuf into a buffer. readsize is the size read
1777 * from the tape/file and is used for error messages. Returns a pointer
1778 * to the location of the uncompressed buffer or NULL on errors.
1779 * Adjust numtrec and complain for a short block.
1782 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1784 /* If zflag is on, all blocks have a struct tapebuf prefix */
1785 /* zflag gets set in setup() from the dump header */
1786 int cresult
, blocklen
;
1787 unsigned long worklen
;
1789 unsigned int worklen2
;
1791 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1793 /* build a length error message */
1794 blocklen
= tpbin
->length
;
1795 if (readsize
< blocklen
+ (int)PREFIXSIZE
)
1796 lengtherr
= "short";
1798 if (readsize
> blocklen
+ (int)PREFIXSIZE
)
1803 if (tpbin
->compressed
) {
1804 /* uncompress whatever we read, if it fails, complain later */
1805 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1807 errx(1,"This restore version doesn't support zlib decompression");
1809 cresult
= uncompress(comprbuf
, &worklen
,
1810 tpbin
->buf
, blocklen
);
1816 reason
= "not enough memory";
1819 reason
= "buffer too small";
1822 reason
= "data error";
1827 if (cresult
== Z_OK
)
1831 #endif /* HAVE_ZLIB */
1833 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1835 errx(1,"This restore version doesn't support bzlib decompression");
1838 cresult
= BZ2_bzBuffToBuffDecompress(
1839 comprbuf
, &worklen2
,
1840 tpbin
->buf
, blocklen
, 0, 0);
1847 reason
= "not enough memory";
1849 case BZ_OUTBUFF_FULL
:
1850 reason
= "buffer too small";
1853 case BZ_DATA_ERROR_MAGIC
:
1854 case BZ_UNEXPECTED_EOF
:
1855 reason
= "data error";
1860 if (cresult
== BZ_OK
)
1864 #endif /* HAVE_BZLIB */
1868 output
= tpbin
->buf
;
1872 numtrec
= worklen
/ TP_BSIZE
;
1873 if (worklen
% TP_BSIZE
!= 0)
1874 reason
= "length mismatch";
1878 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1879 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1880 fprintf(stderr
, "decompression error, block %ld: %s\n",
1881 tpblksread
+1, reason
);
1889 * Print an error message for a read error.
1890 * This was exteracted from the original readtape().
1893 msg_read_error(char *m
)
1895 switch (curfile
.action
) {
1897 fprintf(stderr
, "%s trying to set up tape\n", m
);
1900 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1903 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1906 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1907 (unsigned long)curfile
.ino
);
1911 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1914 * Read the first block and get the blocksize from it. Test
1915 * for a compressed dump tape/file. setup() will make the final
1916 * determination by checking the compressed flag if gethead()
1917 * finds a valid header. The test here is necessary to offset the buffer
1918 * by the size of the compressed prefix. zflag is set here so that
1919 * readtape_set can set the correct function pointer for readtape().
1920 * Note that the first block of each tape/file is not compressed
1921 * and does not have a prefix.
1924 findtapeblksize(void)
1928 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1929 struct s_spcl spclpt
;
1931 for (i
= 0; i
< ntrec
; i
++)
1932 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1936 * For a pipe or file, read in the first record. For a tape, read
1939 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1941 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1942 errx(1, "Tape read error on first record");
1944 memcpy(&spclpt
, tapebuf
, TP_BSIZE
);
1945 if (converthead(&spclpt
) == FAIL
) {
1947 if (converthead(&spclpt
) == FAIL
) {
1948 /* Special case for old compressed tapes with prefix */
1949 if (magtapein
&& (i
% TP_BSIZE
!= 0))
1951 errx(1, "Tape is not a dump tape");
1953 fprintf(stderr
, "Converting to new file system format.\n");
1956 * If the input is from a file or a pipe, we read TP_BSIZE
1957 * bytes looking for a dump header. If the dump is compressed
1958 * we need to read in the rest of the block, as determined
1959 * by c_ntrec in the dump header. The first block of the
1960 * dump is not compressed and does not have a prefix.
1963 if (spclpt
.c_type
== TS_TAPE
1964 && spclpt
.c_flags
& DR_COMPRESSED
) {
1965 /* It's a compressed dump file, read in the */
1966 /* rest of the block based on spclpt.c_ntrec. */
1967 if (spclpt
.c_ntrec
> ntrec
)
1968 errx(1, "Tape blocksize is too large, use "
1969 "\'-b %d\' ", spclpt
.c_ntrec
);
1970 ntrec
= spclpt
.c_ntrec
;
1971 len
= (ntrec
- 1) * TP_BSIZE
;
1975 /* read in the rest of the block based on bufsize */
1976 len
= bufsize
- TP_BSIZE
;
1978 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1979 || (i
!= (long)len
&& i
% TP_BSIZE
!= 0))
1980 errx(1,"Error reading dump file header");
1983 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1985 } /* if (!magtapein) */
1988 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1989 * If the value of ntrec is too large, we read less than
1990 * what we asked for; adjust the value of ntrec and test for
1991 * a compressed dump tape.
1993 if (i
% TP_BSIZE
!= 0) {
1995 /* may be old format compressed dump tape with a prefix */
1996 memcpy(&spclpt
, tpb
->buf
, TP_BSIZE
);
1998 if (converthead(&spclpt
) == FAIL
) {
2000 if (converthead(&spclpt
) == FAIL
)
2001 errx(1, "Tape is not a dump tape");
2002 fprintf(stderr
, "Converting to new file system format.\n");
2004 if (i
% TP_BSIZE
== PREFIXSIZE
2005 && tpb
->compressed
== 0
2006 && spclpt
.c_type
== TS_TAPE
2007 && spclpt
.c_flags
& DR_COMPRESSED
) {
2010 if (tpb
->length
> bufsize
)
2011 errx(1, "Tape blocksize is too large, use "
2012 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
2015 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2018 ntrec
= i
/ TP_BSIZE
;
2019 if (spclpt
.c_type
== TS_TAPE
) {
2020 if (spclpt
.c_flags
& DR_COMPRESSED
)
2022 if (spclpt
.c_ntrec
> ntrec
)
2023 errx(1, "Tape blocksize is too large, use "
2024 "\'-b %d\' ", spclpt
.c_ntrec
);
2027 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
2031 * Read a block of data handling all of the messy details.
2033 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
2041 i
= rmtread(buf
, size
);
2044 i
= read(fd
, buf
, size
);
2047 break; /* EOD or error */
2050 break; /* block at a time for mt */
2053 *lengthread
= len
- size
;
2072 setmagtapein(void) {
2073 struct mtget mt_stat
;
2074 static int done
= 0;
2079 /* need to know if input is really from a tape */
2085 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2088 Vprintf(stdout
,"Input is from a %s %s\n",
2089 host
? "remote" : "local",
2090 magtapein
? "tape" :
2091 Vflag
? "multi-volume (no tape)" : "file/pipe");
2095 * Read the next block from the tape.
2096 * Check to see if it is one of several vintage headers.
2097 * If it is an old style header, convert it to a new style header.
2098 * If it is not any valid header, return an error.
2101 gethead(struct s_spcl
*buf
)
2103 readtape((char *)buf
);
2104 return converthead(buf
);
2108 converthead(struct s_spcl
*buf
)
2116 char dummy
[TP_BSIZE
];
2123 u_int16_t c_inumber
;
2128 u_int16_t odi_nlink
;
2144 if (buf
->c_magic
!= NFS_MAGIC
) {
2145 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2148 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2152 if (checksum((int *)buf
) == FAIL
)
2155 swabst((u_char
*)"8i4s31i528bi192b3i", (u_char
*)buf
);
2158 memcpy(&u_ospcl
.s_ospcl
, buf
, TP_BSIZE
);
2159 memset((char *)buf
, 0, (long)TP_BSIZE
);
2160 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2161 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2162 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2163 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2164 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2165 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2166 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2167 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2168 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2169 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2170 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2171 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2172 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2173 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2174 #if defined(__linux__) || defined(sunos)
2175 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2176 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2177 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2178 #else /* __linux__ || sunos */
2179 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2180 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2181 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2182 #endif /* __linux__ || sunos */
2183 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2184 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2185 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2186 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2188 buf
->c_magic
= NFS_MAGIC
;
2191 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2192 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2193 qcvt
.qval
= buf
->c_dinode
.di_size
;
2194 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2195 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2200 qcvt
.qval
= buf
->c_dinode
.di_size
;
2202 qcvt
.val
[1] = qcvt
.val
[0];
2204 buf
->c_dinode
.di_size
= qcvt
.qval
;
2208 switch (buf
->c_type
) {
2213 * Have to patch up missing information in bit map headers
2216 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2217 if (buf
->c_count
> TP_NINDIR
)
2220 for (i
= 0; i
< buf
->c_count
; i
++)
2225 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2230 if (buf
->c_flags
& DR_INODEINFO
) {
2231 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2233 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2242 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2246 * If we are restoring a filesystem with old format inodes,
2247 * copy the uid/gid to the new location.
2250 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2251 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2259 converttapebuf(struct tapebuf
*tpb
)
2263 unsigned int length
:28;
2264 unsigned int flags
:3;
2265 unsigned int compressed
:1;
2267 swabst((u_char
*)"i", (u_char
*)tpb
);
2268 memcpy(&tb
, tpb
, 4);
2269 tpb
->length
= tb
.length
;
2270 tpb
->flags
= tb
.flags
;
2271 tpb
->compressed
= tb
.compressed
;
2276 * Check that a header is where it belongs and predict the next header
2279 accthdr(struct s_spcl
*header
)
2281 static dump_ino_t previno
= 0x7fffffff;
2282 static int prevtype
;
2283 static long predict
;
2286 if (header
->c_type
== TS_TAPE
) {
2287 fprintf(stderr
, "Volume header (%s inode format) ",
2288 oldinofmt
? "old" : "new");
2289 if (header
->c_firstrec
)
2290 fprintf(stderr
, "begins with record %d",
2291 header
->c_firstrec
);
2292 fprintf(stderr
, "\n");
2293 previno
= 0x7fffffff;
2296 if (previno
== 0x7fffffff)
2300 fprintf(stderr
, "Dumped inodes map header");
2303 fprintf(stderr
, "Used inodes map header");
2306 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2309 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2312 fprintf(stderr
, "End of tape header");
2315 if (predict
!= blksread
- 1)
2316 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2317 predict
, blksread
- 1);
2318 fprintf(stderr
, "\n");
2321 if (header
->c_type
!= TS_END
)
2322 for (i
= 0; i
< header
->c_count
; i
++)
2323 if (readmapflag
|| header
->c_addr
[i
] != 0)
2327 prevtype
= header
->c_type
;
2328 previno
= header
->c_inumber
;
2332 * Find an inode header.
2333 * Complain if had to skip, and complain is set.
2336 findinode(struct s_spcl
*header
)
2338 static long skipcnt
= 0;
2342 curfile
.name
= "<name unknown>";
2343 curfile
.action
= UNKNOWN
;
2347 if (header
->c_magic
!= NFS_MAGIC
) {
2349 while (gethead(header
) == FAIL
||
2350 header
->c_date
!= dumpdate
)
2353 switch (header
->c_type
) {
2357 * Skip up to the beginning of the next record
2359 for (i
= 0; i
< header
->c_count
; i
++)
2360 if (header
->c_addr
[i
])
2362 while (gethead(header
) == FAIL
||
2363 header
->c_date
!= dumpdate
)
2368 curfile
.dip
= &header
->c_dinode
;
2369 curfile
.ino
= header
->c_inumber
;
2373 curfile
.ino
= maxino
;
2377 curfile
.name
= "<file removal list>";
2381 curfile
.name
= "<file dump list>";
2385 panic("unexpected tape header\n");
2389 panic("unknown tape header type %d\n", spcl
.c_type
);
2393 } while (header
->c_type
== TS_ADDR
);
2398 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2408 j
= sizeof(union u_spcl
) / sizeof(int);
2415 /* What happens if we want to read restore tapes
2416 for a 16bit int machine??? */
2422 if (i
!= CHECKSUM
) {
2423 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2424 (unsigned long)curfile
.ino
, curfile
.name
);
2434 #include <varargs.h>
2439 msg(const char *fmt
, ...)
2452 (void)vfprintf(stderr
, fmt
, ap
);
2455 #endif /* RRESTORE */
2458 swab16(u_char
*sp
, int n
)
2463 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2470 swab32(u_char
*sp
, int n
)
2475 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2476 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2483 swab64(u_char
*sp
, int n
)
2488 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2489 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2490 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2491 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2498 swabst(u_char
*cp
, u_char
*sp
)
2504 case '0': case '1': case '2': case '3': case '4':
2505 case '5': case '6': case '7': case '8': case '9':
2506 n
= (n
* 10) + (*cp
++ - '0');
2509 case 's': case 'w': case 'h':
2527 default: /* Any other character, like 'b' counts as byte. */
2541 swabst((u_char
*)"i", (u_char
*)&x
);
2549 swabst((u_char
*)"l", (u_char
*)&x
);
2556 * get the current position of the tape
2559 GetTapePos(long long *pos
)
2565 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2574 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2575 *pos
= (long long)mtpos
;
2578 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2584 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2585 (unsigned long)getpid(), err
, *pos
);
2591 typedef struct mt_pos
{
2594 } MTPosRec
, *MTPosPtr
;
2597 * go to specified position on tape
2600 GotoTapePos(long long pos
)
2606 err
= (rmtseek(pos
, SEEK_SET
) < 0);
2613 buf
.mt_count
= (int) pos
;
2614 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2617 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2623 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2624 (unsigned long)getpid(), err
, pos
);
2631 * read next data from tape to re-sync
2634 ReReadFromTape(void)
2638 if (gethead(&spcl
) == FAIL
) {
2640 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2648 ReReadInodeFromTape(dump_ino_t theino
)
2657 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
) && (cntloop
< ntrec
));
2659 fprintf(stderr
, "%ld reads\n", cntloop
);
2660 if (cntloop
== ntrec
) {
2661 fprintf(stderr
, "DEBUG: bufsize %d\n", bufsize
);
2662 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
2668 #endif /* USE_QFA */
2671 RequestVol(long tnum
)