]>
git.wh0rd.org - dump.git/blob - restore/tape.c
56830c4f8174944f9ccf9d8ae97f39d8cdf851ba
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.73 2003/03/30 15:40:40 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 */
89 #include "pathnames.h"
94 static long fssize
= MAXBSIZE
;
97 static int magtapein
= 0; /* input is from magtape */
98 static char magtape
[MAXPATHLEN
];
99 static char magtapeprefix
[MAXPATHLEN
];
102 static char *tapebuf
; /* input buffer for read */
103 static int bufsize
; /* buffer size without prefix */
104 static char *tbufptr
= NULL
; /* active tape buffer */
105 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
106 static char *comprbuf
; /* uncompress work buf */
107 static size_t comprlen
; /* size including prefix */
109 static union u_spcl endoftapemark
;
110 static long blksread
; /* blocks read since last header */
111 static long tpblksread
= 0; /* TP_BSIZE blocks read */
112 static long tapesread
;
113 static sigjmp_buf restart
;
114 static int gettingfile
= 0; /* restart has a valid frame */
119 static char lnkbuf
[MAXPATHLEN
+ 1];
122 int oldinofmt
; /* old inode format conversion required */
123 int Bcvt
; /* Swap Bytes (for CCI or sun) */
124 static int Qcvt
; /* Swap quads (for sun) */
126 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
128 static void accthdr
__P((struct s_spcl
*));
129 static int checksum
__P((int *));
130 static void findinode
__P((struct s_spcl
*));
131 static void findtapeblksize
__P((void));
132 static int gethead
__P((struct s_spcl
*));
133 static int converthead
__P((struct s_spcl
*));
134 static void converttapebuf
__P((struct tapebuf
*));
135 static void readtape
__P((char *));
136 static void setdumpnum
__P((void));
137 static u_int swabi
__P((u_int
));
139 static u_long swabl
__P((u_long
));
141 static u_char
*swab64
__P((u_char
*, int));
142 static u_char
*swab32
__P((u_char
*, int));
143 static u_char
*swab16
__P((u_char
*, int));
144 static void terminateinput
__P((void));
145 static void xtrfile
__P((char *, size_t));
146 static void xtrlnkfile
__P((char *, size_t));
147 static void xtrlnkskip
__P((char *, size_t));
148 static void xtrmap
__P((char *, size_t));
149 static void xtrmapskip
__P((char *, size_t));
150 static void xtrskip
__P((char *, size_t));
151 static void setmagtapein
__P((void));
153 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
154 static void newcomprbuf
__P((int));
155 static void (*readtape_func
) __P((char *));
156 static void readtape_set
__P((char *));
157 static void readtape_uncompr
__P((char *));
158 static void readtape_comprfile
__P((char *));
159 static void readtape_comprtape
__P((char *));
160 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
161 static void msg_read_error
__P((char *));
163 static int read_a_block
__P((int, char *, size_t, long *));
164 #define PREFIXSIZE sizeof(struct tapebuf)
166 #define COMPARE_ONTHEFLY 1
169 static int ifile
; /* input file for compare */
170 static int cmperror
; /* compare error */
171 static void xtrcmpfile
__P((char *, size_t));
172 static void xtrcmpskip
__P((char *, size_t));
175 static int readmapflag
;
176 static int readingmaps
; /* set to 1 while reading the maps */
179 * Set up an input source. This is called from main.c before setup() is.
182 setinput(char *source
)
188 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
192 if (strchr(source
, ':')) {
194 source
= strchr(host
, ':');
196 if (rmthost(host
) == 0)
200 if (strcmp(source
, "-") == 0) {
202 * Since input is coming from a pipe we must establish
203 * our own connection to the terminal.
205 terminal
= fopen(_PATH_TTY
, "r");
206 if (terminal
== NULL
) {
207 warn("cannot open %s", _PATH_TTY
);
208 terminal
= fopen(_PATH_DEVNULL
, "r");
209 if (terminal
== NULL
)
210 err(1, "cannot open %s", _PATH_DEVNULL
);
214 setuid(getuid()); /* no longer need or want root privileges */
216 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
217 magtapeprefix
[MAXPATHLEN
-1] = '\0';
218 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
221 strncpy(magtape
, source
, MAXPATHLEN
);
222 magtape
[MAXPATHLEN
- 1] = '\0';
226 newtapebuf(long size
)
228 static int tapebufsize
= -1;
231 bufsize
= ntrec
* TP_BSIZE
;
232 if (size
<= tapebufsize
)
236 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
238 errx(1, "Cannot allocate space for tape buffer");
242 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
244 newcomprbuf(int size
)
246 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
247 if (buf_size
<= comprlen
)
250 if (comprbuf
!= NULL
)
252 comprbuf
= malloc(comprlen
);
253 if (comprbuf
== NULL
)
254 errx(1, "Cannot allocate space for decompress buffer");
256 #endif /* HAVE_ZLIB || HAVE_BZLIB */
259 * Verify that the tape drive can be accessed and
260 * that it actually is a dump tape.
265 int i
, j
, *ip
, bot_code
;
269 Vprintf(stdout
, "Verify tape and initialize maps\n");
270 if (Afile
== NULL
&& bot_script
) {
271 msg("Launching %s\n", bot_script
);
272 bot_code
= system_command(bot_script
, magtape
, 1);
273 if (bot_code
!= 0 && bot_code
!= 1) {
274 msg("Restore aborted by the beginning of tape script\n");
286 mt
= rmtopen(temptape
, O_RDONLY
);
292 mt
= OPEN(temptape
, O_RDONLY
, 0);
294 err(1, "%s", temptape
);
300 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
301 readtape_func
= readtape_set
;
305 if (gethead(&spcl
) == FAIL
) {
306 blkcnt
--; /* push back this block */
310 if (gethead(&spcl
) == FAIL
)
311 errx(1, "Tape is not a dump tape");
312 fprintf(stderr
, "Converting to new file system format.\n");
316 fprintf(stderr
, "Dump tape is compressed.\n");
317 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
318 errx(1,"This restore version doesn't support decompression");
319 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
322 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
323 endoftapemark
.s_spcl
.c_type
= TS_END
;
324 ip
= (int *)&endoftapemark
;
325 j
= sizeof(union u_spcl
) / sizeof(int);
330 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
332 if (vflag
|| command
== 't' || command
== 'C')
335 if (tapeposflag
&& (unsigned long)spcl
.c_date
!= qfadumpdate
)
336 errx(1, "different QFA/dumpdates detected\n");
338 if (filesys
[0] == '\0') {
340 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
341 filesys
[NAMELEN
- 1] = '\0';
342 dirptr
= strstr(filesys
, " (dir");
346 dumptime
= spcl
.c_ddate
;
347 dumpdate
= spcl
.c_date
;
348 if (STAT(".", &stbuf
) < 0)
349 err(1, "cannot stat .");
350 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
352 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
353 fssize
= stbuf
.st_blksize
;
354 if (((fssize
- 1) & fssize
) != 0)
355 errx(1, "bad block size %ld", fssize
);
356 if (spcl
.c_volume
!= 1)
357 errx(1, "Tape is not volume 1 of the dump");
358 if (gethead(&spcl
) == FAIL
) {
359 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
360 panic("no header after volume mark!\n");
364 if (spcl
.c_type
!= TS_CLRI
)
365 errx(1, "Cannot find file removal list");
366 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
367 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
369 errx(1, "no memory for active inode map");
371 curfile
.action
= USING
;
372 getfile(xtrmap
, xtrmapskip
);
373 while (spcl
.c_type
== TS_ADDR
) {
374 /* Recompute maxino and the map */
375 dump_ino_t oldmaxino
= maxino
;
376 maxino
+= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
377 resizemaps(oldmaxino
, maxino
);
379 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
380 getfile(xtrmap
, xtrmapskip
);
382 Dprintf(stdout
, "maxino = %lu\n", (unsigned long)maxino
);
383 if (spcl
.c_type
!= TS_BITS
)
384 errx(1, "Cannot find file dump list");
385 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
386 if (map
== (char *)NULL
)
387 errx(1, "no memory for file dump list");
389 curfile
.action
= USING
;
390 getfile(xtrmap
, xtrmapskip
);
391 while (spcl
.c_type
== TS_ADDR
) {
392 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
393 getfile(xtrmap
, xtrmapskip
);
396 * If there may be whiteout entries on the tape, pretend that the
397 * whiteout inode exists, so that the whiteout entries can be
401 SETINO(WINO
, dumpmap
);
407 * Prompt user to load a new dump volume.
408 * "Nextvol" is the next suggested volume to use.
409 * This suggested volume is enforced when doing full
410 * or incremental restores, but can be overridden by
411 * the user when only extracting a subset of the files.
416 long newvol
= 0, wantnext
= 0, i
;
417 long saved_blksread
= 0, saved_tpblksread
= 0;
418 union u_spcl tmpspcl
;
419 # define tmpbuf tmpspcl.s_spcl
421 int haderror
= 0, bot_code
= 1;
431 panic("Changing volumes on pipe input?\n");
436 saved_blksread
= blksread
;
437 saved_tpblksread
= tpblksread
;
438 #if defined(USE_QFA) && defined(sunos)
439 if (createtapeposflag
|| tapeposflag
)
444 exit(1); /* pipes do not get a second chance */
445 if (aflag
|| curfile
.action
!= SKIP
) {
452 while (newvol
<= 0) {
453 if (tapesread
== 0) {
454 fprintf(stderr
, "%s%s%s%s%s",
455 "You have not read any volumes yet.\n",
456 "Unless you know which volume your",
457 " file(s) are on you should start\n",
458 "with the last volume and work",
459 " towards the first.\n");
461 fprintf(stderr
, "You have read volumes");
463 for (i
= 1; i
< 32; i
++)
464 if (tapesread
& (1 << i
)) {
465 fprintf(stderr
, "%s%ld", buf
, (long)i
);
468 fprintf(stderr
, "\n");
471 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
472 (void) fflush(stderr
);
473 (void) fgets(buf
, TP_BSIZE
, terminal
);
474 } while (!feof(terminal
) && buf
[0] == '\n');
477 if (!strcmp(buf
, "none\n")) {
484 "Volume numbers are positive numerics\n");
487 if (newvol
== volno
) {
488 tapesread
|= 1 << volno
;
489 #if defined(USE_QFA) && defined(sunos)
490 if (createtapeposflag
|| tapeposflag
) {
491 if (OpenSMTCmt(magtape
) < 0) {
503 * if using an archive file, reset its name so readtape()
504 * could properly use remote access.
509 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
510 magtape
[MAXPATHLEN
- 1] = '\0';
512 if (bot_script
&& !haderror
) {
513 msg("Launching %s\n", bot_script
);
514 bot_code
= system_command(bot_script
, magtape
, newvol
);
515 if (bot_code
!= 0 && bot_code
!= 1) {
516 msg("Restore aborted by the beginning of tape script\n");
520 if (haderror
|| (bot_code
&& !Mflag
)) {
522 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
523 fprintf(stderr
, "Enter ``none'' if there are no more volumes\n");
524 fprintf(stderr
, "otherwise enter volume name (default: %s) ", magtape
);
525 (void) fflush(stderr
);
526 (void) fgets(buf
, TP_BSIZE
, terminal
);
529 if (!strcmp(buf
, "none\n")) {
533 if (buf
[0] != '\n') {
535 (void) strncpy(magtape
, buf
, sizeof(magtape
));
536 magtape
[sizeof(magtape
) - 1] = '\0';
537 if ((pos
= strchr(magtape
, '\n')))
538 magtape
[pos
- magtape
] = '\0';
543 mt
= rmtopen(magtape
, O_RDONLY
);
546 mt
= OPEN(magtape
, O_RDONLY
, 0);
549 fprintf(stderr
, "Cannot open %s\n", magtape
);
556 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
557 readtape_func
= readtape_set
;
563 if (gethead(&tmpbuf
) == FAIL
) {
564 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
565 fprintf(stderr
, "tape is not dump tape\n");
568 blksread
= saved_blksread
;
569 tpblksread
= saved_tpblksread
;
572 if (tmpbuf
.c_volume
!= volno
) {
573 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
576 blksread
= saved_blksread
;
577 tpblksread
= saved_tpblksread
;
580 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
581 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
582 ctime4(&tmpbuf
.c_date
));
583 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
586 blksread
= saved_blksread
;
587 tpblksread
= saved_tpblksread
;
590 tapesread
|= 1 << volno
;
592 * If continuing from the previous volume, skip over any
593 * blocks read already at the end of the previous volume.
595 * If coming to this volume at random, skip to the beginning
596 * of the next record.
599 fprintf(stderr
, "Dump tape is compressed.\n");
600 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
601 errx(1,"This restore version doesn't support decompression");
602 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
604 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
605 tpblksread
- 1, (long)tmpbuf
.c_firstrec
);
606 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
608 tpblksread
= tmpbuf
.c_firstrec
+ 1;
609 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
611 } else if (tmpbuf
.c_firstrec
> 0 &&
612 tmpbuf
.c_firstrec
< tpblksread
- 1) {
614 * -1 since we've read the volume header
616 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
617 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
618 (long)i
, i
> 1 ? "s" : "");
623 if (curfile
.action
== USING
) {
625 panic("active file into volume 1\n");
629 * Skip up to the beginning of the next record
631 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
632 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
634 (void) gethead(&spcl
);
638 siglongjmp(restart
, 1);
643 * Handle unexpected EOF.
649 if (gettingfile
&& curfile
.action
== USING
) {
650 printf("Warning: %s %s\n",
651 "End-of-input encountered while extracting", curfile
.name
);
653 curfile
.name
= "<name unknown>";
654 curfile
.action
= UNKNOWN
;
656 curfile
.ino
= maxino
;
659 siglongjmp(restart
, 1);
664 * handle multiple dumps per tape by skipping forward to the
672 if (dumpnum
== 1 || volno
!= 1)
675 errx(1, "Cannot have multiple dumps on pipe input");
677 tcom
.mt_count
= dumpnum
- 1;
680 rmtioctl(MTFSF
, dumpnum
- 1);
683 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
690 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
691 fprintf(stdout
, "Dumped from: %s",
692 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
693 if (spcl
.c_host
[0] == '\0')
695 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
696 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
697 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
705 if (volinfo
[1] == ROOTINO
) {
706 printf("Starting inode numbers by volume:\n");
707 for (i
= 1; i
< (int)TP_NINOS
&& volinfo
[i
] != 0; ++i
)
708 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
714 time_t tv_sec
; /* seconds */
715 suseconds_t tv_usec
; /* and microseconds */
720 extractfile(struct entry
*ep
, int doremove
)
724 struct timeval timep
[2];
725 char *name
= myname(ep
);
727 /* If removal is requested (-r mode) do remove it unless
728 * we are extracting a metadata only inode */
729 if (spcl
.c_flags
& DR_METAONLY
) {
730 Vprintf(stdout
, "file %s is metadata only\n", name
);
735 ep
->e_flags
&= ~REMOVED
;
740 curfile
.action
= USING
;
741 #if defined(__linux__) || defined(sunos)
742 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
743 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
744 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
745 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
746 #else /* __linux__ || sunos */
747 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
748 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
749 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
750 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
751 #endif /* __linux__ */
752 mode
= curfile
.dip
->di_mode
;
753 flags
= curfile
.dip
->di_flags
;
754 switch (mode
& IFMT
) {
757 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
762 Vprintf(stdout
, "skipped socket %s\n", name
);
768 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
769 panic("unextracted directory %s\n", name
);
773 Vprintf(stdout
, "extract file %s\n", name
);
774 return (genliteraldir(name
, curfile
.ino
));
779 uid_t luid
= curfile
.dip
->di_uid
;
780 gid_t lgid
= curfile
.dip
->di_gid
;
782 if (! (spcl
.c_flags
& DR_METAONLY
)) {
785 getfile(xtrlnkfile
, xtrlnkskip
);
788 "%s: zero length symbolic link (ignored)\n", name
);
791 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
798 (void) lchown(name
, luid
, lgid
);
804 Vprintf(stdout
, "extract fifo %s\n", name
);
809 if (! (spcl
.c_flags
& DR_METAONLY
)) {
812 if (mkfifo(name
, mode
) < 0) {
813 warn("%s: cannot create fifo", name
);
818 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
819 (void) chmod(name
, mode
);
822 (void) fsetflags(name
, flags
);
824 (void) chflags(name
, flags
);
832 Vprintf(stdout
, "extract special file %s\n", name
);
837 if (! (spcl
.c_flags
& DR_METAONLY
)) {
840 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
841 warn("%s: cannot create special file", name
);
846 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
847 (void) chmod(name
, mode
);
851 warn("%s: fsetflags called on a special file", name
);
852 (void) fsetflags(name
, flags
);
855 (void) chflags(name
, flags
);
863 uid_t luid
= curfile
.dip
->di_uid
;
864 gid_t lgid
= curfile
.dip
->di_gid
;
866 Vprintf(stdout
, "extract file %s\n", name
);
871 if (! (spcl
.c_flags
& DR_METAONLY
)) {
874 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
876 warn("%s: cannot create file", name
);
880 getfile(xtrfile
, xtrskip
);
885 (void) chown(name
, luid
, lgid
);
886 (void) chmod(name
, mode
);
889 (void) fsetflags(name
, flags
);
891 (void) chflags(name
, flags
);
901 * skip over bit maps on the tape
907 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
912 * skip over a file on the tape
918 curfile
.action
= SKIP
;
919 getfile(xtrnull
, xtrnull
);
923 * Extract a file from the tape.
924 * When an allocated block is found it is passed to the fill function;
925 * when an unallocated block (hole) is found, a zeroed buffer is passed
926 * to the skip function.
929 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
932 volatile int curblk
= 0;
933 volatile quad_t size
= spcl
.c_dinode
.di_size
;
934 volatile int last_write_was_hole
= 0;
935 quad_t origsize
= size
;
936 static char clearedbuf
[MAXBSIZE
];
937 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
940 if (spcl
.c_type
== TS_END
)
941 panic("ran off end of tape\n");
942 if (spcl
.c_magic
!= NFS_MAGIC
)
943 panic("not at beginning of a file\n");
944 if (!gettingfile
&& setjmp(restart
) != 0)
948 for (i
= 0; i
< spcl
.c_count
; i
++) {
949 if (readmapflag
|| spcl
.c_addr
[i
]) {
950 readtape(&buf
[curblk
++][0]);
951 if (curblk
== fssize
/ TP_BSIZE
) {
952 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
953 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
955 last_write_was_hole
= 0;
959 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
961 (curblk
- 1) * TP_BSIZE
+ size
));
964 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
966 last_write_was_hole
= 1;
968 if ((size
-= TP_BSIZE
) <= 0) {
969 for (i
++; i
< spcl
.c_count
; i
++)
970 if (readmapflag
|| spcl
.c_addr
[i
])
975 if (gethead(&spcl
) == GOOD
&& size
> 0) {
976 if (spcl
.c_type
== TS_ADDR
)
979 "Missing address (header) block for %s at %ld blocks\n",
980 curfile
.name
, (long)blksread
);
983 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
984 last_write_was_hole
= 0;
987 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
989 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
990 (*skip
)(clearedbuf
, skp
);
993 last_write_was_hole
= 1;
995 if (last_write_was_hole
) {
996 FTRUNCATE(ofile
, origsize
);
1004 * Write out the next block of a file.
1007 xtrfile(char *buf
, size_t size
)
1012 if (write(ofile
, buf
, (int) size
) == -1)
1013 err(1, "write error extracting inode %lu, name %s\nwrite",
1014 (unsigned long)curfile
.ino
, curfile
.name
);
1018 * Skip over a hole in a file.
1022 xtrskip(UNUSED(char *buf
), size_t size
)
1025 if (LSEEK(ofile
, (OFF_T
)size
, SEEK_CUR
) == -1)
1026 err(1, "seek error extracting inode %lu, name %s\nlseek",
1027 (unsigned long)curfile
.ino
, curfile
.name
);
1031 * Collect the next block of a symbolic link.
1034 xtrlnkfile(char *buf
, size_t size
)
1038 if (pathlen
> MAXPATHLEN
)
1039 errx(1, "symbolic link name: %s->%s%s; too long %d",
1040 curfile
.name
, lnkbuf
, buf
, pathlen
);
1041 (void) strcat(lnkbuf
, buf
);
1045 * Skip over a hole in a symbolic link (should never happen).
1049 xtrlnkskip(UNUSED(char *buf
), UNUSED(size_t size
))
1052 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1056 * Collect the next block of a bit map.
1059 xtrmap(char *buf
, size_t size
)
1062 memmove(map
, buf
, size
);
1067 * Skip over a hole in a bit map (should never happen).
1071 xtrmapskip(UNUSED(char *buf
), size_t size
)
1074 panic("hole in map\n");
1079 * Noop, when an extraction function is not needed.
1083 xtrnull(UNUSED(char *buf
), UNUSED(size_t size
))
1089 #if COMPARE_ONTHEFLY
1091 * Compare the next block of a file.
1094 xtrcmpfile(char *buf
, size_t size
)
1096 static char cmpbuf
[MAXBSIZE
];
1101 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1102 fprintf(stderr
, "%s: size has changed.\n",
1108 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1109 fprintf(stderr
, "%s: tape and disk copies are different\n",
1117 * Skip over a hole in a file.
1120 xtrcmpskip(UNUSED(char *buf
), size_t size
)
1122 static char cmpbuf
[MAXBSIZE
];
1128 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1129 fprintf(stderr
, "%s: size has changed.\n",
1135 for (i
= 0; i
< (int)size
; ++i
)
1136 if (cmpbuf
[i
] != '\0') {
1137 fprintf(stderr
, "%s: tape and disk copies are different\n",
1143 #endif /* COMPARE_ONTHEFLY */
1145 #if !COMPARE_ONTHEFLY
1147 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1149 static char buf_tape
[BUFSIZ
];
1150 static char buf_disk
[BUFSIZ
];
1155 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1156 close(fd_tape
), close(fd_disk
);
1157 panic("do_cmpfiles: unexpected EOF[1]");
1159 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1160 close(fd_tape
), close(fd_disk
);
1161 panic("do_cmpfiles: unexpected EOF[2]");
1163 if (n_tape
!= n_disk
) {
1164 close(fd_tape
), close(fd_disk
);
1165 panic("do_cmpfiles: sizes different!");
1167 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1173 /* for debugging compare problems */
1174 #undef COMPARE_FAIL_KEEP_FILE
1177 #ifdef COMPARE_FAIL_KEEP_FILE
1178 /* return true if tapefile should be unlinked after compare */
1183 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1185 struct STAT sbuf_tape
;
1186 int fd_tape
, fd_disk
;
1188 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1189 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1194 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1196 "%s: size changed from %ld to %ld.\n",
1197 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1199 #ifdef COMPARE_FAIL_KEEP_FILE
1206 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1207 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1210 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1212 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1216 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1217 fprintf(stderr
, "%s: tape and disk copies are different\n",
1222 #ifdef COMPARE_FAIL_KEEP_FILE
1223 /* rename the file to live in /tmp */
1224 /* rename `tapefile' to /tmp/<basename of diskfile> */
1226 char *p
= strrchr(diskfile
, '/');
1227 char newname
[MAXPATHLEN
];
1229 panic("can't find / in %s\n", diskfile
);
1231 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1232 if (rename(tapefile
, newname
)) {
1233 panic("rename from %s to %s failed: %s\n",
1237 fprintf(stderr
, "*** %s saved to %s\n",
1242 /* don't unlink the file (it's not there anymore */
1251 #ifdef COMPARE_FAIL_KEEP_FILE
1255 #endif /* !COMPARE_ONTHEFLY */
1257 #if !COMPARE_ONTHEFLY
1258 static char tmpfilename
[MAXPATHLEN
];
1262 comparefile(char *name
)
1267 #if !COMPARE_ONTHEFLY
1268 static char *tmpfile
= NULL
;
1272 curfile
.name
= name
;
1273 curfile
.action
= USING
;
1274 mode
= curfile
.dip
->di_mode
;
1276 if ((mode
& IFMT
) == IFSOCK
) {
1277 Vprintf(stdout
, "skipped socket %s\n", name
);
1282 if ((r
= LSTAT(name
, &sb
)) != 0) {
1283 warn("%s: does not exist (%d)", name
, r
);
1289 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1290 (long)sb
.st_size
, mode
);
1292 if (sb
.st_mode
!= mode
) {
1293 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1294 name
, mode
& 07777, sb
.st_mode
& 07777);
1297 if (spcl
.c_flags
& DR_METAONLY
) {
1301 switch (mode
& IFMT
) {
1315 char lbuf
[MAXPATHLEN
+ 1];
1318 if (!(sb
.st_mode
& S_IFLNK
)) {
1319 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1326 getfile(xtrlnkfile
, xtrlnkskip
);
1329 "%s: zero length symbolic link (ignored)\n",
1334 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1335 panic("readlink of %s failed: %s", name
,
1340 if (strcmp(lbuf
, lnkbuf
) != 0) {
1342 "%s: symbolic link changed from %s to %s.\n",
1343 name
, lnkbuf
, lbuf
);
1352 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1353 fprintf(stderr
, "%s: no longer a special file\n",
1360 if (sb
.st_rdev
!= (dev_t
)curfile
.dip
->di_rdev
) {
1362 "%s: device changed from %d,%d to %d,%d.\n",
1364 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1365 (int)curfile
.dip
->di_rdev
& 0xff,
1366 ((int)sb
.st_rdev
>> 8) & 0xff,
1367 (int)sb
.st_rdev
& 0xff);
1374 #if COMPARE_ONTHEFLY
1375 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1376 panic("Can't open %s: %s\n", name
, strerror(errno
));
1382 getfile(xtrcmpfile
, xtrcmpskip
);
1385 if (read(ifile
, &c
, 1) != 0) {
1386 fprintf(stderr
, "%s: size has changed.\n",
1396 if (tmpfile
== NULL
) {
1397 /* argument to mktemp() must not be in RO space: */
1398 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1399 tmpfile
= mktemp(&tmpfilename
[0]);
1401 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1402 panic("cannot delete tmp file %s: %s\n",
1403 tmpfile
, strerror(errno
));
1405 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1406 panic("cannot create file temp file %s: %s\n",
1407 name
, strerror(errno
));
1409 getfile(xtrfile
, xtrskip
);
1410 (void) close(ofile
);
1411 #ifdef COMPARE_FAIL_KEEP_FILE
1412 if (cmpfiles(tmpfile
, name
, &sb
))
1415 cmpfiles(tmpfile
, name
, &sb
);
1418 #endif /* COMPARE_ONTHEFLY */
1424 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1425 static void (*readtape_func
)(char *) = readtape_set
;
1428 * Read TP_BSIZE blocks from the input.
1429 * Handle read errors, and end of media.
1430 * Decompress compressed blocks.
1435 (*readtape_func
)(buf
); /* call the actual processing routine */
1439 * Set function pointer for readtape() routine. zflag and magtapein must
1440 * be correctly set before the first call to readtape().
1443 readtape_set(char *buf
)
1446 readtape_func
= readtape_uncompr
;
1450 readtape_func
= readtape_comprtape
;
1452 readtape_func
= readtape_comprfile
;
1457 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1460 * This is the original readtape(), it's used for reading uncompressed input.
1461 * Read TP_BSIZE blocks from the input.
1462 * Handle read errors, and end of media.
1465 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1466 readtape_uncompr(char *buf
)
1471 ssize_t rd
, newvol
, i
;
1472 int cnt
, seek_failed
;
1474 if (blkcnt
< numtrec
) {
1475 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1481 for (i
= 0; i
< ntrec
; i
++)
1482 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1485 cnt
= ntrec
* TP_BSIZE
;
1488 if (createtapeposflag
)
1489 (void)GetTapePos(&curtapepos
);
1494 i
= rmtread(&tapebuf
[rd
], cnt
);
1497 i
= read(mt
, &tapebuf
[rd
], cnt
);
1500 * Check for mid-tape short read error.
1501 * If found, skip rest of buffer and start with the next.
1503 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1504 Dprintf(stdout
, "mid-media short read error.\n");
1508 * Handle partial block read.
1510 if (pipein
&& i
== 0 && rd
> 0)
1512 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1521 * Short read. Process the blocks read.
1523 if (i
% TP_BSIZE
!= 0)
1525 "partial block read: %ld should be %ld\n",
1526 (long)i
, ntrec
* TP_BSIZE
);
1527 numtrec
= i
/ TP_BSIZE
;
1531 * Handle read error.
1534 fprintf(stderr
, "Tape read error while ");
1535 switch (curfile
.action
) {
1537 fprintf(stderr
, "trying to set up tape\n");
1540 fprintf(stderr
, "trying to resynchronize\n");
1543 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1546 fprintf(stderr
, "skipping over inode %lu\n",
1547 (unsigned long)curfile
.ino
);
1550 if (!yflag
&& !reply("continue"))
1552 i
= ntrec
* TP_BSIZE
;
1553 memset(tapebuf
, 0, (size_t)i
);
1556 seek_failed
= (rmtseek(i
, 1) < 0);
1559 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (OFF_T
)-1);
1562 warn("continuation failed");
1563 if (!yflag
&& !reply("assume end-of-tape and continue"))
1569 * Handle end of tape.
1572 Vprintf(stdout
, "End-of-tape encountered\n");
1581 if (rd
% TP_BSIZE
!= 0)
1582 panic("partial block read: %d should be %d\n",
1583 rd
, ntrec
* TP_BSIZE
);
1585 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1588 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1593 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1596 * Read a compressed format block from a file or pipe and uncompress it.
1597 * Attempt to handle read errors, and end of file.
1600 readtape_comprfile(char *buf
)
1602 long rl
, size
, i
, ret
;
1604 struct tapebuf
*tpb
;
1606 if (blkcnt
< numtrec
) {
1607 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1612 /* need to read the next block */
1614 for (i
= 0; i
< ntrec
; i
++)
1615 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1617 tpb
= (struct tapebuf
*) tapebuf
;
1619 /* read the block prefix */
1620 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1621 converttapebuf(tpb
);
1623 if (Vflag
&& (ret
== 0 || rl
< (int)PREFIXSIZE
|| tpb
->length
== 0))
1630 if (size
> bufsize
) {
1631 /* something's wrong */
1632 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1635 tpb
->length
= bufsize
;
1637 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1641 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1642 if (tbufptr
== NULL
) {
1643 msg_read_error("File decompression error while");
1644 if (!yflag
&& !reply("continue"))
1646 memset(tapebuf
, 0, bufsize
);
1651 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1657 /* Errors while reading from a file or pipe are catastrophic. Since
1658 * there are no block boundaries, it's impossible to bypass the
1659 * block in error and find the start of the next block.
1662 /* It's possible to have multiple input files using -M
1663 * and -f file1,file2...
1665 Vprintf(stdout
, "End-of-File encountered\n");
1675 msg_read_error("Read error while");
1676 /* if (!yflag && !reply("continue")) */
1681 * Read compressed data from a tape and uncompress it.
1682 * Handle read errors, and end of media.
1683 * Since a tape consists of separate physical blocks, we try
1684 * to recover from errors by repositioning the tape to the next
1688 readtape_comprtape(char *buf
)
1692 struct tapebuf
*tpb
;
1695 if (blkcnt
< numtrec
) {
1696 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1701 /* need to read the next block */
1703 for (i
= 0; i
< ntrec
; i
++)
1704 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1706 tpb
= (struct tapebuf
*) tapebuf
;
1708 /* read the block */
1709 size
= bufsize
+ PREFIXSIZE
;
1710 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1714 converttapebuf(tpb
);
1715 tbufptr
= decompress_tapebuf(tpb
, rl
);
1716 if (tbufptr
== NULL
) {
1717 msg_read_error("Tape decompression error while");
1718 if (!yflag
&& !reply("continue"))
1720 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1726 /* Handle errors: EOT switches to the next volume, other errors
1727 * attempt to position the tape to the next block.
1730 Vprintf(stdout
, "End-of-tape encountered\n");
1739 msg_read_error("Tape read error while");
1740 if (!yflag
&& !reply("continue"))
1742 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1747 rl
= rmtioctl(MTFSR
, 1);
1753 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1757 warn("continuation failed");
1758 if (!yflag
&& !reply("assume end-of-tape and continue"))
1760 ret
= 0; /* end of tape */
1766 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1772 * Decompress a struct tapebuf into a buffer. readsize is the size read
1773 * from the tape/file and is used for error messages. Returns a pointer
1774 * to the location of the uncompressed buffer or NULL on errors.
1775 * Adjust numtrec and complain for a short block.
1778 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1780 /* If zflag is on, all blocks have a struct tapebuf prefix */
1781 /* zflag gets set in setup() from the dump header */
1782 int cresult
, blocklen
;
1783 unsigned long worklen
;
1785 unsigned int worklen2
;
1787 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1789 /* build a length error message */
1790 blocklen
= tpbin
->length
;
1791 if (readsize
< blocklen
+ (int)PREFIXSIZE
)
1792 lengtherr
= "short";
1794 if (readsize
> blocklen
+ (int)PREFIXSIZE
)
1799 if (tpbin
->compressed
) {
1800 /* uncompress whatever we read, if it fails, complain later */
1801 if (tpbin
->flags
== COMPRESS_ZLIB
) {
1803 errx(1,"This restore version doesn't support zlib decompression");
1805 cresult
= uncompress(comprbuf
, &worklen
,
1806 tpbin
->buf
, blocklen
);
1812 reason
= "not enough memory";
1815 reason
= "buffer too small";
1818 reason
= "data error";
1823 if (cresult
== Z_OK
)
1827 #endif /* HAVE_ZLIB */
1829 if (tpbin
->flags
== COMPRESS_BZLIB
) {
1831 errx(1,"This restore version doesn't support bzlib decompression");
1834 cresult
= BZ2_bzBuffToBuffDecompress(
1835 comprbuf
, &worklen2
,
1836 tpbin
->buf
, blocklen
, 0, 0);
1843 reason
= "not enough memory";
1845 case BZ_OUTBUFF_FULL
:
1846 reason
= "buffer too small";
1849 case BZ_DATA_ERROR_MAGIC
:
1850 case BZ_UNEXPECTED_EOF
:
1851 reason
= "data error";
1856 if (cresult
== BZ_OK
)
1860 #endif /* HAVE_BZLIB */
1864 output
= tpbin
->buf
;
1868 numtrec
= worklen
/ TP_BSIZE
;
1869 if (worklen
% TP_BSIZE
!= 0)
1870 reason
= "length mismatch";
1874 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1875 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1876 fprintf(stderr
, "decompression error, block %ld: %s\n",
1877 tpblksread
+1, reason
);
1885 * Print an error message for a read error.
1886 * This was exteracted from the original readtape().
1889 msg_read_error(char *m
)
1891 switch (curfile
.action
) {
1893 fprintf(stderr
, "%s trying to set up tape\n", m
);
1896 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1899 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1902 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1903 (unsigned long)curfile
.ino
);
1907 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1910 * Read the first block and get the blocksize from it. Test
1911 * for a compressed dump tape/file. setup() will make the final
1912 * determination by checking the compressed flag if gethead()
1913 * finds a valid header. The test here is necessary to offset the buffer
1914 * by the size of the compressed prefix. zflag is set here so that
1915 * readtape_set can set the correct function pointer for readtape().
1916 * Note that the first block of each tape/file is not compressed
1917 * and does not have a prefix.
1920 findtapeblksize(void)
1924 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1925 struct s_spcl spclpt
;
1927 for (i
= 0; i
< ntrec
; i
++)
1928 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1932 * For a pipe or file, read in the first record. For a tape, read
1935 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1937 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1938 errx(1, "Tape read error on first record");
1940 memcpy(&spclpt
, tapebuf
, TP_BSIZE
);
1941 if (converthead(&spclpt
) == FAIL
) {
1943 if (converthead(&spclpt
) == FAIL
) {
1944 /* Special case for old compressed tapes with prefix */
1945 if (magtapein
&& (i
% TP_BSIZE
!= 0))
1947 errx(1, "Tape is not a dump tape");
1949 fprintf(stderr
, "Converting to new file system format.\n");
1952 * If the input is from a file or a pipe, we read TP_BSIZE
1953 * bytes looking for a dump header. If the dump is compressed
1954 * we need to read in the rest of the block, as determined
1955 * by c_ntrec in the dump header. The first block of the
1956 * dump is not compressed and does not have a prefix.
1959 if (spclpt
.c_type
== TS_TAPE
1960 && spclpt
.c_flags
& DR_COMPRESSED
) {
1961 /* It's a compressed dump file, read in the */
1962 /* rest of the block based on spclpt.c_ntrec. */
1963 if (spclpt
.c_ntrec
> ntrec
)
1964 errx(1, "Tape blocksize is too large, use "
1965 "\'-b %d\' ", spclpt
.c_ntrec
);
1966 ntrec
= spclpt
.c_ntrec
;
1967 len
= (ntrec
- 1) * TP_BSIZE
;
1971 /* read in the rest of the block based on bufsize */
1972 len
= bufsize
- TP_BSIZE
;
1974 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1975 || (i
!= (long)len
&& i
% TP_BSIZE
!= 0))
1976 errx(1,"Error reading dump file header");
1979 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1981 } /* if (!magtapein) */
1984 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1985 * If the value of ntrec is too large, we read less than
1986 * what we asked for; adjust the value of ntrec and test for
1987 * a compressed dump tape.
1989 if (i
% TP_BSIZE
!= 0) {
1991 /* may be old format compressed dump tape with a prefix */
1992 memcpy(&spclpt
, tpb
->buf
, TP_BSIZE
);
1994 if (converthead(&spclpt
) == FAIL
) {
1996 if (converthead(&spclpt
) == FAIL
)
1997 errx(1, "Tape is not a dump tape");
1998 fprintf(stderr
, "Converting to new file system format.\n");
2000 if (i
% TP_BSIZE
== PREFIXSIZE
2001 && tpb
->compressed
== 0
2002 && spclpt
.c_type
== TS_TAPE
2003 && spclpt
.c_flags
& DR_COMPRESSED
) {
2006 if (tpb
->length
> bufsize
)
2007 errx(1, "Tape blocksize is too large, use "
2008 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
2011 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2014 ntrec
= i
/ TP_BSIZE
;
2015 if (spclpt
.c_type
== TS_TAPE
) {
2016 if (spclpt
.c_flags
& DR_COMPRESSED
)
2018 if (spclpt
.c_ntrec
> ntrec
)
2019 errx(1, "Tape blocksize is too large, use "
2020 "\'-b %d\' ", spclpt
.c_ntrec
);
2023 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
2027 * Read a block of data handling all of the messy details.
2029 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
2037 i
= rmtread(buf
, size
);
2040 i
= read(fd
, buf
, size
);
2043 break; /* EOD or error */
2046 break; /* block at a time for mt */
2049 *lengthread
= len
- size
;
2068 setmagtapein(void) {
2069 struct mtget mt_stat
;
2070 static int done
= 0;
2075 /* need to know if input is really from a tape */
2081 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2084 Vprintf(stdout
,"Input is from a %s %s\n",
2085 host
? "remote" : "local",
2086 magtapein
? "tape" :
2087 Vflag
? "multi-volume (no tape)" : "file/pipe");
2091 * Read the next block from the tape.
2092 * Check to see if it is one of several vintage headers.
2093 * If it is an old style header, convert it to a new style header.
2094 * If it is not any valid header, return an error.
2097 gethead(struct s_spcl
*buf
)
2099 readtape((char *)buf
);
2100 return converthead(buf
);
2104 converthead(struct s_spcl
*buf
)
2112 char dummy
[TP_BSIZE
];
2119 u_int16_t c_inumber
;
2124 u_int16_t odi_nlink
;
2140 if (buf
->c_magic
!= NFS_MAGIC
) {
2141 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2144 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2148 if (checksum((int *)buf
) == FAIL
)
2151 swabst((u_char
*)"8i4s31i528bi192b3i", (u_char
*)buf
);
2154 memcpy(&u_ospcl
.s_ospcl
, buf
, TP_BSIZE
);
2155 memset((char *)buf
, 0, (long)TP_BSIZE
);
2156 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2157 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2158 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2159 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2160 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2161 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2162 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2163 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2164 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2165 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2166 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2167 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2168 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2169 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2170 #if defined(__linux__) || defined(sunos)
2171 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2172 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2173 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2174 #else /* __linux__ || sunos */
2175 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2176 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2177 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2178 #endif /* __linux__ || sunos */
2179 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2180 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2181 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
2182 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2184 buf
->c_magic
= NFS_MAGIC
;
2187 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2188 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2189 qcvt
.qval
= buf
->c_dinode
.di_size
;
2190 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2191 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2196 qcvt
.qval
= buf
->c_dinode
.di_size
;
2198 qcvt
.val
[1] = qcvt
.val
[0];
2200 buf
->c_dinode
.di_size
= qcvt
.qval
;
2204 switch (buf
->c_type
) {
2209 * Have to patch up missing information in bit map headers
2212 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2213 if (buf
->c_count
> TP_NINDIR
)
2216 for (i
= 0; i
< buf
->c_count
; i
++)
2221 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2226 if (buf
->c_flags
& DR_INODEINFO
) {
2227 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2229 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2238 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2242 * If we are restoring a filesystem with old format inodes,
2243 * copy the uid/gid to the new location.
2246 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2247 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2255 converttapebuf(struct tapebuf
*tpb
)
2259 unsigned int length
:28;
2260 unsigned int flags
:3;
2261 unsigned int compressed
:1;
2263 swabst((u_char
*)"i", (u_char
*)tpb
);
2264 memcpy(&tb
, tpb
, 4);
2265 tpb
->length
= tb
.length
;
2266 tpb
->flags
= tb
.flags
;
2267 tpb
->compressed
= tb
.compressed
;
2272 * Check that a header is where it belongs and predict the next header
2275 accthdr(struct s_spcl
*header
)
2277 static dump_ino_t previno
= 0x7fffffff;
2278 static int prevtype
;
2279 static long predict
;
2282 if (header
->c_type
== TS_TAPE
) {
2283 fprintf(stderr
, "Volume header (%s inode format) ",
2284 oldinofmt
? "old" : "new");
2285 if (header
->c_firstrec
)
2286 fprintf(stderr
, "begins with record %d",
2287 header
->c_firstrec
);
2288 fprintf(stderr
, "\n");
2289 previno
= 0x7fffffff;
2292 if (previno
== 0x7fffffff)
2296 fprintf(stderr
, "Dumped inodes map header");
2299 fprintf(stderr
, "Used inodes map header");
2302 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2305 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2308 fprintf(stderr
, "End of tape header");
2311 if (predict
!= blksread
- 1)
2312 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2313 predict
, blksread
- 1);
2314 fprintf(stderr
, "\n");
2317 if (header
->c_type
!= TS_END
)
2318 for (i
= 0; i
< header
->c_count
; i
++)
2319 if (readmapflag
|| header
->c_addr
[i
] != 0)
2323 prevtype
= header
->c_type
;
2324 previno
= header
->c_inumber
;
2328 * Find an inode header.
2329 * Complain if had to skip, and complain is set.
2332 findinode(struct s_spcl
*header
)
2334 static long skipcnt
= 0;
2338 curfile
.name
= "<name unknown>";
2339 curfile
.action
= UNKNOWN
;
2343 if (header
->c_magic
!= NFS_MAGIC
) {
2345 while (gethead(header
) == FAIL
||
2346 header
->c_date
!= dumpdate
)
2349 switch (header
->c_type
) {
2353 * Skip up to the beginning of the next record
2355 for (i
= 0; i
< header
->c_count
; i
++)
2356 if (header
->c_addr
[i
])
2358 while (gethead(header
) == FAIL
||
2359 header
->c_date
!= dumpdate
)
2364 curfile
.dip
= &header
->c_dinode
;
2365 curfile
.ino
= header
->c_inumber
;
2369 curfile
.ino
= maxino
;
2373 curfile
.name
= "<file removal list>";
2377 curfile
.name
= "<file dump list>";
2381 panic("unexpected tape header\n");
2385 panic("unknown tape header type %d\n", spcl
.c_type
);
2389 } while (header
->c_type
== TS_ADDR
);
2394 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2404 j
= sizeof(union u_spcl
) / sizeof(int);
2411 /* What happens if we want to read restore tapes
2412 for a 16bit int machine??? */
2418 if (i
!= CHECKSUM
) {
2419 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2420 (unsigned long)curfile
.ino
, curfile
.name
);
2430 #include <varargs.h>
2435 msg(const char *fmt
, ...)
2448 (void)vfprintf(stderr
, fmt
, ap
);
2451 #endif /* RRESTORE */
2454 swab16(u_char
*sp
, int n
)
2459 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2466 swab32(u_char
*sp
, int n
)
2471 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2472 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2479 swab64(u_char
*sp
, int n
)
2484 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2485 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2486 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2487 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2494 swabst(u_char
*cp
, u_char
*sp
)
2500 case '0': case '1': case '2': case '3': case '4':
2501 case '5': case '6': case '7': case '8': case '9':
2502 n
= (n
* 10) + (*cp
++ - '0');
2505 case 's': case 'w': case 'h':
2523 default: /* Any other character, like 'b' counts as byte. */
2537 swabst((u_char
*)"i", (u_char
*)&x
);
2545 swabst((u_char
*)"l", (u_char
*)&x
);
2552 * get the current position of the tape
2555 GetTapePos(long long *pos
)
2561 *pos
= (long long) rmtseek(0, SEEK_CUR
);
2570 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
2571 *pos
= (long long)mtpos
;
2574 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
2580 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
2581 (unsigned long)getpid(), err
, *pos
);
2587 typedef struct mt_pos
{
2590 } MTPosRec
, *MTPosPtr
;
2593 * go to specified position on tape
2596 GotoTapePos(long long pos
)
2602 err
= (rmtseek(pos
, SEEK_SET
) < 0);
2609 buf
.mt_count
= (int) pos
;
2610 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
2613 pos
= LSEEK(mt
, pos
, SEEK_SET
);
2619 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
2620 (unsigned long)getpid(), err
, pos
);
2627 * read next data from tape to re-sync
2630 ReReadFromTape(void)
2634 if (gethead(&spcl
) == FAIL
) {
2636 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2644 ReReadInodeFromTape(dump_ino_t theino
)
2653 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
) && (cntloop
< ntrec
));
2655 fprintf(stderr
, "%ld reads\n", cntloop
);
2656 if (cntloop
== ntrec
) {
2657 fprintf(stderr
, "DEBUG: bufsize %d\n", bufsize
);
2658 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
2664 #endif /* USE_QFA */
2667 RequestVol(long tnum
)