]>
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 <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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.44 2001/07/18 14:57:46 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 #include "pathnames.h"
94 static long fssize
= MAXBSIZE
;
96 static int pipein
= 0;
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 */
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 */
115 static char *host
= NULL
;
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 void readtape
__P((char *));
134 static void setdumpnum
__P((void));
135 static u_int swabi
__P((u_int
));
137 static u_long swabl
__P((u_long
));
139 static u_char
*swab64
__P((u_char
*, int));
140 static u_char
*swab32
__P((u_char
*, int));
141 static u_char
*swab16
__P((u_char
*, int));
142 static void terminateinput
__P((void));
143 static void xtrfile
__P((char *, size_t));
144 static void xtrlnkfile
__P((char *, size_t));
145 static void xtrlnkskip
__P((char *, size_t));
146 static void xtrmap
__P((char *, size_t));
147 static void xtrmapskip
__P((char *, size_t));
148 static void xtrskip
__P((char *, size_t));
149 static void setmagtapein
__P((void));
152 static void newcomprbuf
__P((int));
153 static void readtape_set
__P((char *));
154 static void readtape_uncompr
__P((char *));
155 static void readtape_comprfile
__P((char *));
156 static void readtape_comprtape
__P((char *));
157 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
158 static void msg_read_error
__P((char *));
160 static int read_a_block
__P((int, char *, size_t, long *));
161 #define PREFIXSIZE sizeof(struct tapebuf)
163 #define COMPARE_ONTHEFLY 1
166 static int ifile
; /* input file for compare */
167 static int cmperror
; /* compare error */
168 static void xtrcmpfile
__P((char *, size_t));
169 static void xtrcmpskip
__P((char *, size_t));
172 static int readmapflag
;
175 * Set up an input source. This is called from main.c before setup() is.
178 setinput(char *source
)
184 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
188 if (strchr(source
, ':')) {
190 source
= strchr(host
, ':');
192 if (rmthost(host
) == 0)
196 if (strcmp(source
, "-") == 0) {
198 * Since input is coming from a pipe we must establish
199 * our own connection to the terminal.
201 terminal
= fopen(_PATH_TTY
, "r");
202 if (terminal
== NULL
) {
203 warn("cannot open %s", _PATH_TTY
);
204 terminal
= fopen(_PATH_DEVNULL
, "r");
205 if (terminal
== NULL
)
206 err(1, "cannot open %s", _PATH_DEVNULL
);
210 setuid(getuid()); /* no longer need or want root privileges */
212 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
213 magtapeprefix
[MAXPATHLEN
-1] = '\0';
214 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
217 strncpy(magtape
, source
, MAXPATHLEN
);
218 magtape
[MAXPATHLEN
- 1] = '\0';
222 newtapebuf(long size
)
224 static int tapebufsize
= -1;
227 bufsize
= ntrec
* TP_BSIZE
;
228 if (size
<= tapebufsize
)
232 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
234 errx(1, "Cannot allocate space for tape buffer");
240 newcomprbuf(int size
)
242 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
243 if (buf_size
<= comprlen
)
246 if (comprbuf
!= NULL
)
248 comprbuf
= malloc(comprlen
);
249 if (comprbuf
== NULL
)
250 errx(1, "Cannot allocate space for decompress buffer");
252 #endif /* HAVE_ZLIB */
255 * Verify that the tape drive can be accessed and
256 * that it actually is a dump tape.
261 int i
, j
, *ip
, bot_code
;
264 Vprintf(stdout
, "Verify tape and initialize maps\n");
266 msg("Launching %s\n", bot_script
);
267 bot_code
= system_command(bot_script
, magtape
, 1);
268 if (bot_code
!= 0 && bot_code
!= 1) {
269 msg("Restore aborted by the beginning of tape script\n");
275 mt
= rmtopen(magtape
, 0);
281 mt
= OPEN(magtape
, O_RDONLY
, 0);
283 err(1, "%s", magtape
);
289 if (gethead(&spcl
) == FAIL
) {
290 blkcnt
--; /* push back this block */
294 if (gethead(&spcl
) == FAIL
)
295 errx(1, "Tape is not a dump tape");
296 fprintf(stderr
, "Converting to new file system format.\n");
300 fprintf(stderr
, "Dump tape is compressed.\n");
304 errx(1,"This restore version doesn't support decompression");
305 #endif /* HAVE_ZLIB */
308 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
309 endoftapemark
.s_spcl
.c_type
= TS_END
;
310 ip
= (int *)&endoftapemark
;
311 j
= sizeof(union u_spcl
) / sizeof(int);
316 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
318 if (vflag
|| command
== 't' || command
== 'C')
320 if (filesys
[0] == '\0') {
322 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
323 filesys
[NAMELEN
- 1] = '\0';
324 dirptr
= strstr(filesys
, " (dir");
328 dumptime
= spcl
.c_ddate
;
329 dumpdate
= spcl
.c_date
;
330 if (STAT(".", &stbuf
) < 0)
331 err(1, "cannot stat .");
332 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
334 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
335 fssize
= stbuf
.st_blksize
;
336 if (((fssize
- 1) & fssize
) != 0)
337 errx(1, "bad block size %ld", fssize
);
338 if (spcl
.c_volume
!= 1)
339 errx(1, "Tape is not volume 1 of the dump");
340 if (gethead(&spcl
) == FAIL
) {
341 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
342 panic("no header after volume mark!\n");
345 if (spcl
.c_type
!= TS_CLRI
)
346 errx(1, "Cannot find file removal list");
347 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
348 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
349 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
351 errx(1, "no memory for active inode map");
353 curfile
.action
= USING
;
354 getfile(xtrmap
, xtrmapskip
);
355 if (spcl
.c_type
!= TS_BITS
)
356 errx(1, "Cannot find file dump list");
357 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
358 if (map
== (char *)NULL
)
359 errx(1, "no memory for file dump list");
361 curfile
.action
= USING
;
362 getfile(xtrmap
, xtrmapskip
);
364 * If there may be whiteout entries on the tape, pretend that the
365 * whiteout inode exists, so that the whiteout entries can be
369 SETINO(WINO
, dumpmap
);
373 * Prompt user to load a new dump volume.
374 * "Nextvol" is the next suggested volume to use.
375 * This suggested volume is enforced when doing full
376 * or incremental restores, but can be overridden by
377 * the user when only extracting a subset of the files.
382 long newvol
= 0, wantnext
= 0, i
;
383 long saved_blksread
= 0, saved_tpblksread
= 0;
384 union u_spcl tmpspcl
;
385 # define tmpbuf tmpspcl.s_spcl
387 int haderror
= 0, bot_code
= 1;
395 panic("Changing volumes on pipe input?\n");
400 saved_blksread
= blksread
;
401 saved_tpblksread
= tpblksread
;
404 exit(1); /* pipes do not get a second chance */
405 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
412 while (newvol
<= 0) {
413 if (tapesread
== 0) {
414 fprintf(stderr
, "%s%s%s%s%s",
415 "You have not read any tapes yet.\n",
416 "Unless you know which volume your",
417 " file(s) are on you should start\n",
418 "with the last volume and work",
419 " towards the first.\n");
421 fprintf(stderr
, "You have read volumes");
423 for (i
= 1; i
< 32; i
++)
424 if (tapesread
& (1 << i
)) {
425 fprintf(stderr
, "%s%ld", buf
, (long)i
);
428 fprintf(stderr
, "\n");
431 fprintf(stderr
, "Specify next volume #: ");
432 (void) fflush(stderr
);
433 (void) fgets(buf
, TP_BSIZE
, terminal
);
434 } while (!feof(terminal
) && buf
[0] == '\n');
440 "Volume numbers are positive numerics\n");
443 if (newvol
== volno
) {
444 tapesread
|= 1 << volno
;
449 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
450 magtape
[MAXPATHLEN
- 1] = '\0';
452 if (bot_script
&& !haderror
) {
453 msg("Launching %s\n", bot_script
);
454 bot_code
= system_command(bot_script
, magtape
, newvol
);
455 if (bot_code
!= 0 && bot_code
!= 1) {
456 msg("Restore aborted by the beginning of tape script\n");
460 if (haderror
|| (bot_code
&& !Mflag
)) {
462 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
463 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
464 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
465 (void) fflush(stderr
);
466 (void) fgets(buf
, TP_BSIZE
, terminal
);
469 if (!strcmp(buf
, "none\n")) {
473 if (buf
[0] != '\n') {
475 (void) strncpy(magtape
, buf
, sizeof(magtape
));
476 magtape
[sizeof(magtape
) - 1] = '\0';
477 if ((pos
= strchr(magtape
, '\n')))
478 magtape
[pos
- magtape
] = '\0';
483 mt
= rmtopen(magtape
, 0);
486 mt
= OPEN(magtape
, O_RDONLY
, 0);
489 fprintf(stderr
, "Cannot open %s\n", magtape
);
499 if (gethead(&tmpbuf
) == FAIL
) {
500 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
501 fprintf(stderr
, "tape is not dump tape\n");
504 blksread
= saved_blksread
;
505 tpblksread
= saved_tpblksread
;
508 if (tmpbuf
.c_volume
!= volno
) {
509 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
512 blksread
= saved_blksread
;
513 tpblksread
= saved_tpblksread
;
516 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
517 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
518 ctime4(&tmpbuf
.c_date
));
519 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
522 blksread
= saved_blksread
;
523 tpblksread
= saved_tpblksread
;
526 tapesread
|= 1 << volno
;
527 blksread
= saved_blksread
;
528 tpblksread
= saved_tpblksread
;
530 * If continuing from the previous volume, skip over any
531 * blocks read already at the end of the previous volume.
533 * If coming to this volume at random, skip to the beginning
534 * of the next record.
536 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
537 tpblksread
, (long)tmpbuf
.c_firstrec
);
538 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
540 tpblksread
= tmpbuf
.c_firstrec
;
541 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
543 } else if (tmpbuf
.c_firstrec
> 0 &&
544 tmpbuf
.c_firstrec
< tpblksread
- 1) {
546 * -1 since we've read the volume header
548 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
549 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
550 (long)i
, i
> 1 ? "s" : "");
555 if (curfile
.action
== USING
) {
557 panic("active file into volume 1\n");
561 * Skip up to the beginning of the next record
563 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
564 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
566 (void) gethead(&spcl
);
570 siglongjmp(restart
, 1);
575 * Handle unexpected EOF.
581 if (gettingfile
&& curfile
.action
== USING
) {
582 printf("Warning: %s %s\n",
583 "End-of-input encountered while extracting", curfile
.name
);
585 curfile
.name
= "<name unknown>";
586 curfile
.action
= UNKNOWN
;
588 curfile
.ino
= maxino
;
591 siglongjmp(restart
, 1);
596 * handle multiple dumps per tape by skipping forward to the
604 if (dumpnum
== 1 || volno
!= 1)
607 errx(1, "Cannot have multiple dumps on pipe input");
609 tcom
.mt_count
= dumpnum
- 1;
612 rmtioctl(MTFSF
, dumpnum
- 1);
615 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
622 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
623 fprintf(stdout
, "Dumped from: %s",
624 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
625 if (spcl
.c_host
[0] == '\0')
627 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
628 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
629 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
633 extractfile(char *name
)
637 struct timeval timep
[2];
641 curfile
.action
= USING
;
643 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
644 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
645 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
646 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
647 #else /* __linux__ */
648 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
649 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
650 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
651 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
652 #endif /* __linux__ */
653 mode
= curfile
.dip
->di_mode
;
654 flags
= curfile
.dip
->di_flags
;
655 switch (mode
& IFMT
) {
658 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
663 Vprintf(stdout
, "skipped socket %s\n", name
);
669 ep
= lookupname(name
);
670 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
671 panic("unextracted directory %s\n", name
);
675 Vprintf(stdout
, "extract file %s\n", name
);
676 return (genliteraldir(name
, curfile
.ino
));
681 uid_t luid
= curfile
.dip
->di_uid
;
682 gid_t lgid
= curfile
.dip
->di_gid
;
686 getfile(xtrlnkfile
, xtrlnkskip
);
689 "%s: zero length symbolic link (ignored)\n", name
);
692 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
695 (void) lchown(name
, luid
, lgid
);
701 Vprintf(stdout
, "extract fifo %s\n", name
);
708 if (mkfifo(name
, mode
) < 0) {
709 warn("%s: cannot create fifo", name
);
713 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
714 (void) chmod(name
, mode
);
717 (void) fsetflags(name
, flags
);
719 (void) chflags(name
, flags
);
727 Vprintf(stdout
, "extract special file %s\n", name
);
734 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
735 warn("%s: cannot create special file", name
);
739 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
740 (void) chmod(name
, mode
);
744 warn("%s: fsetflags called on a special file", name
);
745 (void) fsetflags(name
, flags
);
748 (void) chflags(name
, flags
);
755 Vprintf(stdout
, "extract file %s\n", name
);
762 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
764 warn("%s: cannot create file", name
);
768 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
769 (void) fchmod(ofile
, mode
);
772 (void) setflags(ofile
, flags
);
774 (void) fchflags(ofile
, flags
);
776 getfile(xtrfile
, xtrskip
);
785 * skip over bit maps on the tape
791 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
796 * skip over a file on the tape
802 curfile
.action
= SKIP
;
803 getfile(xtrnull
, xtrnull
);
807 * Extract a file from the tape.
808 * When an allocated block is found it is passed to the fill function;
809 * when an unallocated block (hole) is found, a zeroed buffer is passed
810 * to the skip function.
813 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
816 volatile int curblk
= 0;
817 volatile quad_t size
= spcl
.c_dinode
.di_size
;
818 volatile int last_write_was_hole
= 0;
819 quad_t origsize
= size
;
820 static char clearedbuf
[MAXBSIZE
];
821 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
824 if (spcl
.c_type
== TS_END
)
825 panic("ran off end of tape\n");
826 if (spcl
.c_magic
!= NFS_MAGIC
)
827 panic("not at beginning of a file\n");
828 if (!gettingfile
&& setjmp(restart
) != 0)
832 for (i
= 0; i
< spcl
.c_count
; i
++) {
833 if (readmapflag
|| spcl
.c_addr
[i
]) {
834 readtape(&buf
[curblk
++][0]);
835 if (curblk
== fssize
/ TP_BSIZE
) {
836 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
837 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
839 last_write_was_hole
= 0;
843 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
845 (curblk
- 1) * TP_BSIZE
+ size
));
848 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
850 last_write_was_hole
= 1;
852 if ((size
-= TP_BSIZE
) <= 0) {
853 for (i
++; i
< spcl
.c_count
; i
++)
854 if (readmapflag
|| spcl
.c_addr
[i
])
859 if (gethead(&spcl
) == GOOD
&& size
> 0) {
860 if (spcl
.c_type
== TS_ADDR
)
863 "Missing address (header) block for %s at %ld blocks\n",
864 curfile
.name
, (long)blksread
);
867 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
868 last_write_was_hole
= 0;
871 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
873 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
874 (*skip
)(clearedbuf
, skp
);
877 last_write_was_hole
= 1;
879 if (last_write_was_hole
) {
880 FTRUNCATE(ofile
, origsize
);
887 * Write out the next block of a file.
890 xtrfile(char *buf
, size_t size
)
895 if (write(ofile
, buf
, (int) size
) == -1)
896 err(1, "write error extracting inode %lu, name %s\nwrite",
897 (unsigned long)curfile
.ino
, curfile
.name
);
901 * Skip over a hole in a file.
905 xtrskip(char *buf
, size_t size
)
908 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
909 err(1, "seek error extracting inode %lu, name %s\nlseek",
910 (unsigned long)curfile
.ino
, curfile
.name
);
914 * Collect the next block of a symbolic link.
917 xtrlnkfile(char *buf
, size_t size
)
921 if (pathlen
> MAXPATHLEN
)
922 errx(1, "symbolic link name: %s->%s%s; too long %d",
923 curfile
.name
, lnkbuf
, buf
, pathlen
);
924 (void) strcat(lnkbuf
, buf
);
928 * Skip over a hole in a symbolic link (should never happen).
932 xtrlnkskip(char *buf
, size_t size
)
935 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
939 * Collect the next block of a bit map.
942 xtrmap(char *buf
, size_t size
)
945 memmove(map
, buf
, size
);
950 * Skip over a hole in a bit map (should never happen).
954 xtrmapskip(char *buf
, size_t size
)
957 panic("hole in map\n");
962 * Noop, when an extraction function is not needed.
966 xtrnull(char *buf
, size_t size
)
974 * Compare the next block of a file.
977 xtrcmpfile(char *buf
, size_t size
)
979 static char cmpbuf
[MAXBSIZE
];
984 if (read(ifile
, cmpbuf
, size
) != size
) {
985 fprintf(stderr
, "%s: size has changed.\n",
991 if (memcmp(buf
, cmpbuf
, size
) != 0) {
992 fprintf(stderr
, "%s: tape and disk copies are different\n",
1000 * Skip over a hole in a file.
1003 xtrcmpskip(char *buf
, size_t size
)
1005 static char cmpbuf
[MAXBSIZE
];
1011 if (read(ifile
, cmpbuf
, size
) != size
) {
1012 fprintf(stderr
, "%s: size has changed.\n",
1018 for (i
= 0; i
< size
; ++i
)
1019 if (cmpbuf
[i
] != '\0') {
1020 fprintf(stderr
, "%s: tape and disk copies are different\n",
1026 #endif /* COMPARE_ONTHEFLY */
1028 #if !COMPARE_ONTHEFLY
1030 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1032 static char buf_tape
[BUFSIZ
];
1033 static char buf_disk
[BUFSIZ
];
1038 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1039 close(fd_tape
), close(fd_disk
);
1040 panic("do_cmpfiles: unexpected EOF[1]");
1042 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1043 close(fd_tape
), close(fd_disk
);
1044 panic("do_cmpfiles: unexpected EOF[2]");
1046 if (n_tape
!= n_disk
) {
1047 close(fd_tape
), close(fd_disk
);
1048 panic("do_cmpfiles: sizes different!");
1050 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1056 /* for debugging compare problems */
1057 #undef COMPARE_FAIL_KEEP_FILE
1060 #ifdef COMPARE_FAIL_KEEP_FILE
1061 /* return true if tapefile should be unlinked after compare */
1066 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1068 struct STAT sbuf_tape
;
1069 int fd_tape
, fd_disk
;
1071 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1072 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1077 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1079 "%s: size changed from %ld to %ld.\n",
1080 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1082 #ifdef COMPARE_FAIL_KEEP_FILE
1089 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1090 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1093 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1095 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1099 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1100 fprintf(stderr
, "%s: tape and disk copies are different\n",
1105 #ifdef COMPARE_FAIL_KEEP_FILE
1106 /* rename the file to live in /tmp */
1107 /* rename `tapefile' to /tmp/<basename of diskfile> */
1109 char *p
= strrchr(diskfile
, '/');
1110 char newname
[MAXPATHLEN
];
1112 panic("can't find / in %s\n", diskfile
);
1114 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1115 if (rename(tapefile
, newname
)) {
1116 panic("rename from %s to %s failed: %s\n",
1120 fprintf(stderr
, "*** %s saved to %s\n",
1125 /* don't unlink the file (it's not there anymore */
1134 #ifdef COMPARE_FAIL_KEEP_FILE
1138 #endif /* !COMPARE_ONTHEFLY */
1140 #if !COMPARE_ONTHEFLY
1141 static char tmpfilename
[MAXPATHLEN
];
1145 comparefile(char *name
)
1150 #if !COMPARE_ONTHEFLY
1151 static char *tmpfile
= NULL
;
1155 if ((r
= LSTAT(name
, &sb
)) != 0) {
1156 warn("%s: does not exist (%d)", name
, r
);
1162 curfile
.name
= name
;
1163 curfile
.action
= USING
;
1164 mode
= curfile
.dip
->di_mode
;
1166 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1167 (long)sb
.st_size
, mode
);
1169 if (sb
.st_mode
!= mode
) {
1170 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1171 name
, mode
& 07777, sb
.st_mode
& 07777);
1174 switch (mode
& IFMT
) {
1188 char lbuf
[MAXPATHLEN
+ 1];
1191 if (!(sb
.st_mode
& S_IFLNK
)) {
1192 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1199 getfile(xtrlnkfile
, xtrlnkskip
);
1202 "%s: zero length symbolic link (ignored)\n",
1207 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1208 panic("readlink of %s failed: %s", name
,
1213 if (strcmp(lbuf
, lnkbuf
) != 0) {
1215 "%s: symbolic link changed from %s to %s.\n",
1216 name
, lnkbuf
, lbuf
);
1225 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1226 fprintf(stderr
, "%s: no longer a special file\n",
1233 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1235 "%s: device changed from %d,%d to %d,%d.\n",
1237 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1238 (int)curfile
.dip
->di_rdev
& 0xff,
1239 ((int)sb
.st_rdev
>> 8) & 0xff,
1240 (int)sb
.st_rdev
& 0xff);
1247 #if COMPARE_ONTHEFLY
1248 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1249 panic("Can't open %s: %s\n", name
, strerror(errno
));
1255 getfile(xtrcmpfile
, xtrcmpskip
);
1258 if (read(ifile
, &c
, 1) != 0) {
1259 fprintf(stderr
, "%s: size has changed.\n",
1269 if (tmpfile
== NULL
) {
1270 /* argument to mktemp() must not be in RO space: */
1271 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1272 tmpfile
= mktemp(&tmpfilename
[0]);
1274 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1275 panic("cannot delete tmp file %s: %s\n",
1276 tmpfile
, strerror(errno
));
1278 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1279 panic("cannot create file temp file %s: %s\n",
1280 name
, strerror(errno
));
1282 getfile(xtrfile
, xtrskip
);
1283 (void) close(ofile
);
1284 #ifdef COMPARE_FAIL_KEEP_FILE
1285 if (cmpfiles(tmpfile
, name
, &sb
))
1288 cmpfiles(tmpfile
, name
, &sb
);
1291 #endif /* COMPARE_ONTHEFLY */
1298 static void (*readtape_func
)(char *) = readtape_set
;
1301 * Read TP_BSIZE blocks from the input.
1302 * Handle read errors, and end of media.
1303 * Decompress compressed blocks.
1308 (*readtape_func
)(buf
); /* call the actual processing routine */
1312 * Set function pointer for readtape() routine. zflag and magtapein must
1313 * be correctly set before the first call to readtape().
1316 readtape_set(char *buf
)
1319 readtape_func
= readtape_uncompr
;
1323 readtape_func
= readtape_comprtape
;
1325 readtape_func
= readtape_comprfile
;
1330 #endif /* HAVE_ZLIB */
1333 * This is the original readtape(), it's used for reading uncompressed input.
1334 * Read TP_BSIZE blocks from the input.
1335 * Handle read errors, and end of media.
1339 readtape_uncompr(char *buf
)
1344 ssize_t rd
, newvol
, i
;
1345 int cnt
, seek_failed
;
1347 if (blkcnt
< numtrec
) {
1348 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1354 for (i
= 0; i
< ntrec
; i
++)
1355 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1358 cnt
= ntrec
* TP_BSIZE
;
1363 i
= rmtread(&tapebuf
[rd
], cnt
);
1366 i
= read(mt
, &tapebuf
[rd
], cnt
);
1369 * Check for mid-tape short read error.
1370 * If found, skip rest of buffer and start with the next.
1372 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1373 Dprintf(stdout
, "mid-media short read error.\n");
1377 * Handle partial block read.
1379 if (pipein
&& i
== 0 && rd
> 0)
1381 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1390 * Short read. Process the blocks read.
1392 if (i
% TP_BSIZE
!= 0)
1394 "partial block read: %ld should be %ld\n",
1395 (long)i
, ntrec
* TP_BSIZE
);
1396 numtrec
= i
/ TP_BSIZE
;
1400 * Handle read error.
1403 fprintf(stderr
, "Tape read error while ");
1404 switch (curfile
.action
) {
1406 fprintf(stderr
, "trying to set up tape\n");
1409 fprintf(stderr
, "trying to resynchronize\n");
1412 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1415 fprintf(stderr
, "skipping over inode %lu\n",
1416 (unsigned long)curfile
.ino
);
1419 if (!yflag
&& !reply("continue"))
1421 i
= ntrec
* TP_BSIZE
;
1422 memset(tapebuf
, 0, (size_t)i
);
1425 seek_failed
= (rmtseek(i
, 1) < 0);
1428 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1431 warn("continuation failed");
1432 if (!yflag
&& !reply("assume end-of-tape and continue"))
1438 * Handle end of tape.
1441 Vprintf(stdout
, "End-of-tape encountered\n");
1450 if (rd
% TP_BSIZE
!= 0)
1451 panic("partial block read: %d should be %d\n",
1452 rd
, ntrec
* TP_BSIZE
);
1454 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1457 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1465 * Read a compressed format block from a file or pipe and uncompress it.
1466 * Attempt to handle read errors, and end of file.
1469 readtape_comprfile(char *buf
)
1471 long rl
, size
, i
, ret
;
1473 struct tapebuf
*tpb
;
1475 if (blkcnt
< numtrec
) {
1476 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1481 /* need to read the next block */
1483 for (i
= 0; i
< ntrec
; i
++)
1484 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1486 tpb
= (struct tapebuf
*) tapebuf
;
1488 /* read the block prefix */
1489 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1495 if (size
> bufsize
) {
1496 /* something's wrong */
1497 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1500 tpb
->length
= bufsize
;
1502 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1506 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1507 if (tbufptr
== NULL
) {
1508 msg_read_error("File decompression error while");
1509 if (!yflag
&& !reply("continue"))
1511 memset(tapebuf
, 0, bufsize
);
1516 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1522 /* Errors while reading from a file or pipe are catastrophic. Since
1523 * there are no block boundaries, it's impossible to bypass the
1524 * block in error and find the start of the next block.
1527 /* It's possible to have multiple input files using -M
1528 * and -f file1,file2...
1530 Vprintf(stdout
, "End-of-File encountered\n");
1540 msg_read_error("Read error while");
1541 /* if (!yflag && !reply("continue")) */
1546 * Read compressed data from a tape and uncompress it.
1547 * Handle read errors, and end of media.
1548 * Since a tape consists of separate physical blocks, we try
1549 * to recover from errors by repositioning the tape to the next
1553 readtape_comprtape(char *buf
)
1557 struct tapebuf
*tpb
;
1560 if (blkcnt
< numtrec
) {
1561 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1566 /* need to read the next block */
1568 for (i
= 0; i
< ntrec
; i
++)
1569 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1571 tpb
= (struct tapebuf
*) tapebuf
;
1573 /* read the block */
1574 size
= bufsize
+ PREFIXSIZE
;
1575 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1579 tbufptr
= decompress_tapebuf(tpb
, rl
);
1580 if (tbufptr
== NULL
) {
1581 msg_read_error("Tape decompression error while");
1582 if (!yflag
&& !reply("continue"))
1584 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1590 /* Handle errors: EOT switches to the next volume, other errors
1591 * attempt to position the tape to the next block.
1594 Vprintf(stdout
, "End-of-tape encountered\n");
1603 msg_read_error("Tape read error while");
1604 if (!yflag
&& !reply("continue"))
1606 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1611 rl
= rmtioctl(MTFSR
, 1);
1617 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1621 warn("continuation failed");
1622 if (!yflag
&& !reply("assume end-of-tape and continue"))
1624 ret
= 0; /* end of tape */
1630 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1636 * Decompress a struct tapebuf into a buffer. readsize is the size read
1637 * from the tape/file and is used for error messages. Returns a pointer
1638 * to the location of the uncompressed buffer or NULL on errors.
1639 * Adjust numtrec and complain for a short block.
1642 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1644 /* If zflag is on, all blocks have a struct tapebuf prefix */
1645 /* zflag gets set in setup() from the dump header */
1646 int cresult
, blocklen
;
1647 unsigned long worklen
;
1648 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1650 /* build a length error message */
1651 blocklen
= tpbin
->length
;
1652 if (readsize
< blocklen
+ PREFIXSIZE
)
1653 lengtherr
= "short";
1655 if (readsize
> blocklen
+ PREFIXSIZE
)
1660 if (tpbin
->compressed
) {
1661 /* uncompress whatever we read, if it fails, complain later */
1662 cresult
= uncompress(comprbuf
, &worklen
, tpbin
->buf
, blocklen
);
1666 output
= tpbin
->buf
;
1671 numtrec
= worklen
/ TP_BSIZE
;
1672 if (worklen
% TP_BSIZE
!= 0)
1673 reason
= "length mismatch";
1676 reason
= "not enough memory";
1679 reason
= "buffer too small";
1682 reason
= "data error";
1689 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1690 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1691 fprintf(stderr
, "decompression error, block %ld: %s\n",
1692 tpblksread
+1, reason
);
1693 if (cresult
!= Z_OK
) output
= NULL
;
1699 * Print an error message for a read error.
1700 * This was exteracted from the original readtape().
1703 msg_read_error(char *m
)
1705 switch (curfile
.action
) {
1707 fprintf(stderr
, "%s trying to set up tape\n", m
);
1710 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1713 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1716 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1717 (unsigned long)curfile
.ino
);
1721 #endif /* HAVE_ZLIB */
1724 * Read the first block and get the blocksize from it. Test
1725 * for a compressed dump tape/file. setup() will make the final
1726 * determination by checking the compressed flag if gethead()
1727 * finds a valid header. The test here is necessary to offset the buffer
1728 * by the size of the compressed prefix. zflag is set here so that
1729 * readtape_set can set the correct function pointer for readtape().
1730 * Note that the first block of each tape/file is not compressed
1731 * and does not have a prefix.
1734 findtapeblksize(void)
1738 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1739 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1741 for (i
= 0; i
< ntrec
; i
++)
1742 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1746 * For a pipe or file, read in the first record. For a tape, read
1749 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1751 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1752 errx(1, "Tape read error on first record");
1755 * If the input is from a file or a pipe, we read TP_BSIZE
1756 * bytes looking for a dump header. If the dump is compressed
1757 * we need to read in the rest of the block, as determined
1758 * by c_ntrec in the dump header. The first block of the
1759 * dump is not compressed and does not have a prefix.
1762 if (spclpt
->c_type
== TS_TAPE
1763 && spclpt
->c_flags
& DR_COMPRESSED
) {
1764 /* It's a compressed dump file, read in the */
1765 /* rest of the block based on spclpt->c_ntrec. */
1766 if (spclpt
->c_ntrec
> ntrec
)
1767 errx(1, "Tape blocksize is too large, use "
1768 "\'-b %d\' ", spclpt
->c_ntrec
);
1769 ntrec
= spclpt
->c_ntrec
;
1770 len
= (ntrec
- 1) * TP_BSIZE
;
1774 /* read in the rest of the block based on bufsize */
1775 len
= bufsize
- TP_BSIZE
;
1777 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1778 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1779 errx(1,"Error reading dump file header");
1782 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1784 } /* if (!magtapein) */
1787 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1788 * If the value of ntrec is too large, we read less than
1789 * what we asked for; adjust the value of ntrec and test for
1790 * a compressed dump tape.
1793 if (i
% TP_BSIZE
!= 0) {
1794 /* may be old format compressed dump tape with a prefix */
1795 spclpt
= (struct s_spcl
*) tpb
->buf
;
1796 if (i
% TP_BSIZE
== PREFIXSIZE
1797 && tpb
->compressed
== 0
1798 && spclpt
->c_type
== TS_TAPE
1799 && spclpt
->c_flags
& DR_COMPRESSED
) {
1802 if (tpb
->length
> bufsize
)
1803 errx(1, "Tape blocksize is too large, use "
1804 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1807 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1810 ntrec
= i
/ TP_BSIZE
;
1811 if (spclpt
->c_type
== TS_TAPE
) {
1812 if (spclpt
->c_flags
& DR_COMPRESSED
)
1814 if (spclpt
->c_ntrec
> ntrec
)
1815 errx(1, "Tape blocksize is too large, use "
1816 "\'-b %d\' ", spclpt
->c_ntrec
);
1819 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1823 * Read a block of data handling all of the messy details.
1825 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1833 i
= rmtread(buf
, size
);
1836 i
= read(fd
, buf
, size
);
1839 break; /* EOD or error */
1842 break; /* block at a time for mt */
1845 *lengthread
= len
- size
;
1864 setmagtapein(void) {
1865 struct mtget mt_stat
;
1866 static int done
= 0;
1871 /* need to know if input is really from a tape */
1874 magtapein
= rmtioctl(MTNOP
, 1) != -1;
1877 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
1880 Vprintf(stdout
,"Input is from %s\n", magtapein
? "tape": "file/pipe");
1884 * Read the next block from the tape.
1885 * Check to see if it is one of several vintage headers.
1886 * If it is an old style header, convert it to a new style header.
1887 * If it is not any valid header, return an error.
1890 gethead(struct s_spcl
*buf
)
1898 char dummy
[TP_BSIZE
];
1905 u_int16_t c_inumber
;
1910 u_int16_t odi_nlink
;
1926 readtape((char *)buf
);
1927 if (buf
->c_magic
!= NFS_MAGIC
) {
1928 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1931 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1935 if (checksum((int *)buf
) == FAIL
)
1938 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1941 readtape((char *)(&u_ospcl
.s_ospcl
));
1942 memset((char *)buf
, 0, (long)TP_BSIZE
);
1943 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1944 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1945 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1946 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1947 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1948 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1949 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1950 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1951 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1952 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1953 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1954 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1955 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1956 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1958 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1959 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1960 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1961 #else /* __linux__ */
1962 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1963 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1964 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1965 #endif /* __linux__ */
1966 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1967 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1968 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1969 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1971 buf
->c_magic
= NFS_MAGIC
;
1974 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1975 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1976 qcvt
.qval
= buf
->c_dinode
.di_size
;
1977 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1978 printf("Note: Doing Quad swapping\n");
1983 qcvt
.qval
= buf
->c_dinode
.di_size
;
1985 qcvt
.val
[1] = qcvt
.val
[0];
1987 buf
->c_dinode
.di_size
= qcvt
.qval
;
1991 switch (buf
->c_type
) {
1996 * Have to patch up missing information in bit map headers
1999 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2000 if (buf
->c_count
> TP_NINDIR
)
2003 for (i
= 0; i
< buf
->c_count
; i
++)
2008 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2020 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2024 * If we are restoring a filesystem with old format inodes,
2025 * copy the uid/gid to the new location.
2028 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2029 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2037 * Check that a header is where it belongs and predict the next header
2040 accthdr(struct s_spcl
*header
)
2042 static dump_ino_t previno
= 0x7fffffff;
2043 static int prevtype
;
2044 static long predict
;
2047 if (header
->c_type
== TS_TAPE
) {
2048 fprintf(stderr
, "Volume header (%s inode format) ",
2049 oldinofmt
? "old" : "new");
2050 if (header
->c_firstrec
)
2051 fprintf(stderr
, "begins with record %d",
2052 header
->c_firstrec
);
2053 fprintf(stderr
, "\n");
2054 previno
= 0x7fffffff;
2057 if (previno
== 0x7fffffff)
2061 fprintf(stderr
, "Dumped inodes map header");
2064 fprintf(stderr
, "Used inodes map header");
2067 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2070 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2073 fprintf(stderr
, "End of tape header");
2076 if (predict
!= blksread
- 1)
2077 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2078 predict
, blksread
- 1);
2079 fprintf(stderr
, "\n");
2082 if (header
->c_type
!= TS_END
)
2083 for (i
= 0; i
< header
->c_count
; i
++)
2084 if (readmapflag
|| header
->c_addr
[i
] != 0)
2088 prevtype
= header
->c_type
;
2089 previno
= header
->c_inumber
;
2093 * Find an inode header.
2094 * Complain if had to skip, and complain is set.
2097 findinode(struct s_spcl
*header
)
2099 static long skipcnt
= 0;
2103 curfile
.name
= "<name unknown>";
2104 curfile
.action
= UNKNOWN
;
2108 if (header
->c_magic
!= NFS_MAGIC
) {
2110 while (gethead(header
) == FAIL
||
2111 header
->c_date
!= dumpdate
)
2114 switch (header
->c_type
) {
2118 * Skip up to the beginning of the next record
2120 for (i
= 0; i
< header
->c_count
; i
++)
2121 if (header
->c_addr
[i
])
2123 while (gethead(header
) == FAIL
||
2124 header
->c_date
!= dumpdate
)
2129 curfile
.dip
= &header
->c_dinode
;
2130 curfile
.ino
= header
->c_inumber
;
2134 curfile
.ino
= maxino
;
2138 curfile
.name
= "<file removal list>";
2142 curfile
.name
= "<file dump list>";
2146 panic("unexpected tape header\n");
2150 panic("unknown tape header type %d\n", spcl
.c_type
);
2154 } while (header
->c_type
== TS_ADDR
);
2159 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2169 j
= sizeof(union u_spcl
) / sizeof(int);
2176 /* What happens if we want to read restore tapes
2177 for a 16bit int machine??? */
2183 if (i
!= CHECKSUM
) {
2184 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2185 (unsigned long)curfile
.ino
, curfile
.name
);
2195 #include <varargs.h>
2200 msg(const char *fmt
, ...)
2213 (void)vfprintf(stderr
, fmt
, ap
);
2216 #endif /* RRESTORE */
2219 swab16(u_char
*sp
, int n
)
2224 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2231 swab32(u_char
*sp
, int n
)
2236 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2237 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2244 swab64(u_char
*sp
, int n
)
2249 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2250 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2251 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2252 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2259 swabst(u_char
*cp
, u_char
*sp
)
2265 case '0': case '1': case '2': case '3': case '4':
2266 case '5': case '6': case '7': case '8': case '9':
2267 n
= (n
* 10) + (*cp
++ - '0');
2270 case 's': case 'w': case 'h':
2288 default: /* Any other character, like 'b' counts as byte. */
2302 swabst((u_char
*)"i", (u_char
*)&x
);
2310 swabst((u_char
*)"l", (u_char
*)&x
);
2317 * get the current position of the tape
2320 GetTapePos(long *pos
)
2325 if (ioctl(mt
, MTIOCPOS
, pos
) == -1) {
2327 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %ld)\n",
2328 (unsigned long)getpid(), err
, *pos
);
2334 typedef struct mt_pos
{
2337 } MTPosRec
, *MTPosPtr
;
2340 * go to specified position on tape
2343 GotoTapePos(long pos
)
2350 if (ioctl(mt
, MTIOCTOP
, &buf
) == -1) {
2352 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %ld)\n",
2353 (unsigned long)getpid(), err
, pos
);
2360 * read next data from tape to re-sync
2363 ReReadFromTape(void)
2367 if (gethead(&spcl
) == FAIL
) {
2369 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2377 RequestVol(long tnum
)
2382 #endif /* USE_QFA */