]>
git.wh0rd.org - dump.git/blob - restore/tape.c
ee5c72fc35392003c72bd8e560219afc38a4ed1b
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.40 2001/06/18 10:58:28 stelian Exp $";
53 #include <compatlfs.h>
55 #include <compaterr.h>
62 #include <sys/param.h>
70 #ifdef HAVE_EXT2FS_EXT2_FS_H
71 #include <ext2fs/ext2_fs.h>
73 #include <linux/ext2_fs.h>
75 #include <ext2fs/ext2fs.h>
76 #include <bsdcompat.h>
78 #include <ufs/ufs/dinode.h>
79 #endif /* __linux__ */
80 #include <protocols/dumprestore.h>
84 #endif /* HAVE_ZLIB */
88 #include "pathnames.h"
93 static long fssize
= MAXBSIZE
;
95 static int pipein
= 0;
96 static int magtapein
= 0; /* input is from magtape */
97 static char magtape
[MAXPATHLEN
];
98 static char magtapeprefix
[MAXPATHLEN
];
101 static char *tapebuf
; /* input buffer for read */
102 static int bufsize
; /* buffer size without prefix */
103 static char *tbufptr
= NULL
; /* active tape buffer */
105 static char *comprbuf
; /* uncompress work buf */
106 static size_t comprlen
; /* size including prefix */
108 static union u_spcl endoftapemark
;
109 static long blksread
; /* blocks read since last header */
110 static long tpblksread
= 0; /* TP_BSIZE blocks read */
111 static long tapesread
;
112 static sigjmp_buf restart
;
113 static int gettingfile
= 0; /* restart has a valid frame */
114 static char *host
= NULL
;
118 static char lnkbuf
[MAXPATHLEN
+ 1];
121 int oldinofmt
; /* old inode format conversion required */
122 int Bcvt
; /* Swap Bytes (for CCI or sun) */
123 static int Qcvt
; /* Swap quads (for sun) */
125 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
127 static void accthdr
__P((struct s_spcl
*));
128 static int checksum
__P((int *));
129 static void findinode
__P((struct s_spcl
*));
130 static void findtapeblksize
__P((void));
131 static int gethead
__P((struct s_spcl
*));
132 static void readtape
__P((char *));
133 static void setdumpnum
__P((void));
134 static u_int swabi
__P((u_int
));
136 static u_long swabl
__P((u_long
));
138 static u_char
*swab64
__P((u_char
*, int));
139 static u_char
*swab32
__P((u_char
*, int));
140 static u_char
*swab16
__P((u_char
*, int));
141 static void terminateinput
__P((void));
142 static void xtrfile
__P((char *, size_t));
143 static void xtrlnkfile
__P((char *, size_t));
144 static void xtrlnkskip
__P((char *, size_t));
145 static void xtrmap
__P((char *, size_t));
146 static void xtrmapskip
__P((char *, size_t));
147 static void xtrskip
__P((char *, size_t));
148 static void setmagtapein
__P((void));
151 static void newcomprbuf
__P((int));
152 static void readtape_set
__P((char *));
153 static void readtape_uncompr
__P((char *));
154 static void readtape_comprfile
__P((char *));
155 static void readtape_comprtape
__P((char *));
156 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
157 static void msg_read_error
__P((char *));
159 static int read_a_block
__P((int, char *, size_t, long *));
160 #define PREFIXSIZE sizeof(struct tapebuf)
162 #define COMPARE_ONTHEFLY 1
165 static int ifile
; /* input file for compare */
166 static int cmperror
; /* compare error */
167 static void xtrcmpfile
__P((char *, size_t));
168 static void xtrcmpskip
__P((char *, size_t));
171 static int readmapflag
;
174 * Set up an input source. This is called from main.c before setup() is.
177 setinput(char *source
)
183 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
187 if (strchr(source
, ':')) {
189 source
= strchr(host
, ':');
191 if (rmthost(host
) == 0)
195 if (strcmp(source
, "-") == 0) {
197 * Since input is coming from a pipe we must establish
198 * our own connection to the terminal.
200 terminal
= fopen(_PATH_TTY
, "r");
201 if (terminal
== NULL
) {
202 warn("cannot open %s", _PATH_TTY
);
203 terminal
= fopen(_PATH_DEVNULL
, "r");
204 if (terminal
== NULL
)
205 err(1, "cannot open %s", _PATH_DEVNULL
);
209 setuid(getuid()); /* no longer need or want root privileges */
211 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
212 magtapeprefix
[MAXPATHLEN
-1] = '\0';
213 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
216 strncpy(magtape
, source
, MAXPATHLEN
);
217 magtape
[MAXPATHLEN
- 1] = '\0';
221 newtapebuf(long size
)
223 static int tapebufsize
= -1;
226 bufsize
= ntrec
* TP_BSIZE
;
227 if (size
<= tapebufsize
)
231 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
233 errx(1, "Cannot allocate space for tape buffer");
239 newcomprbuf(int size
)
241 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
242 if (buf_size
<= comprlen
)
245 if (comprbuf
!= NULL
)
247 comprbuf
= malloc(comprlen
);
248 if (comprbuf
== NULL
)
249 errx(1, "Cannot allocate space for decompress buffer");
251 #endif /* HAVE_ZLIB */
254 * Verify that the tape drive can be accessed and
255 * that it actually is a dump tape.
263 Vprintf(stdout
, "Verify tape and initialize maps\n");
266 mt
= rmtopen(magtape
, 0);
272 mt
= OPEN(magtape
, O_RDONLY
, 0);
274 err(1, "%s", magtape
);
280 if (gethead(&spcl
) == FAIL
) {
281 blkcnt
--; /* push back this block */
285 if (gethead(&spcl
) == FAIL
)
286 errx(1, "Tape is not a dump tape");
287 fprintf(stderr
, "Converting to new file system format.\n");
291 fprintf(stderr
, "Dump tape is compressed.\n");
295 errx(1,"This restore version doesn't support decompression");
296 #endif /* HAVE_ZLIB */
299 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
300 endoftapemark
.s_spcl
.c_type
= TS_END
;
301 ip
= (int *)&endoftapemark
;
302 j
= sizeof(union u_spcl
) / sizeof(int);
307 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
309 if (vflag
|| command
== 't' || command
== 'C')
311 if (filesys
[0] == '\0') {
313 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
314 filesys
[NAMELEN
- 1] = '\0';
315 dirptr
= strstr(filesys
, " (dir");
319 dumptime
= spcl
.c_ddate
;
320 dumpdate
= spcl
.c_date
;
321 if (STAT(".", &stbuf
) < 0)
322 err(1, "cannot stat .");
323 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
325 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
326 fssize
= stbuf
.st_blksize
;
327 if (((fssize
- 1) & fssize
) != 0)
328 errx(1, "bad block size %ld", fssize
);
329 if (spcl
.c_volume
!= 1)
330 errx(1, "Tape is not volume 1 of the dump");
331 if (gethead(&spcl
) == FAIL
) {
332 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
333 panic("no header after volume mark!\n");
336 if (spcl
.c_type
!= TS_CLRI
)
337 errx(1, "Cannot find file removal list");
338 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
339 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
340 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
342 errx(1, "no memory for active inode map");
344 curfile
.action
= USING
;
345 getfile(xtrmap
, xtrmapskip
);
346 if (spcl
.c_type
!= TS_BITS
)
347 errx(1, "Cannot find file dump list");
348 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
349 if (map
== (char *)NULL
)
350 errx(1, "no memory for file dump list");
352 curfile
.action
= USING
;
353 getfile(xtrmap
, xtrmapskip
);
355 * If there may be whiteout entries on the tape, pretend that the
356 * whiteout inode exists, so that the whiteout entries can be
360 SETINO(WINO
, dumpmap
);
364 * Prompt user to load a new dump volume.
365 * "Nextvol" is the next suggested volume to use.
366 * This suggested volume is enforced when doing full
367 * or incremental restores, but can be overridden by
368 * the user when only extracting a subset of the files.
373 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
374 union u_spcl tmpspcl
;
375 # define tmpbuf tmpspcl.s_spcl
385 panic("Changing volumes on pipe input?\n");
393 exit(1); /* pipes do not get a second chance */
394 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
401 while (newvol
<= 0) {
402 if (tapesread
== 0) {
403 fprintf(stderr
, "%s%s%s%s%s",
404 "You have not read any tapes yet.\n",
405 "Unless you know which volume your",
406 " file(s) are on you should start\n",
407 "with the last volume and work",
408 " towards the first.\n");
410 fprintf(stderr
, "You have read volumes");
412 for (i
= 1; i
< 32; i
++)
413 if (tapesread
& (1 << i
)) {
414 fprintf(stderr
, "%s%ld", buf
, (long)i
);
417 fprintf(stderr
, "\n");
420 fprintf(stderr
, "Specify next volume #: ");
421 (void) fflush(stderr
);
422 (void) fgets(buf
, TP_BSIZE
, terminal
);
423 } while (!feof(terminal
) && buf
[0] == '\n');
429 "Volume numbers are positive numerics\n");
432 if (newvol
== volno
) {
433 tapesread
|= 1 << volno
;
438 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
439 magtape
[MAXPATHLEN
- 1] = '\0';
441 if (!Mflag
|| haderror
) {
443 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
444 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
445 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
446 (void) fflush(stderr
);
447 (void) fgets(buf
, TP_BSIZE
, terminal
);
450 if (!strcmp(buf
, "none\n")) {
454 if (buf
[0] != '\n') {
455 (void) strncpy(magtape
, buf
, sizeof(magtape
));
456 magtape
[sizeof(magtape
) - 1] = '\0';
461 mt
= rmtopen(magtape
, 0);
464 mt
= OPEN(magtape
, O_RDONLY
, 0);
467 fprintf(stderr
, "Cannot open %s\n", magtape
);
477 if (gethead(&tmpbuf
) == FAIL
) {
478 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
479 fprintf(stderr
, "tape is not dump tape\n");
484 if (tmpbuf
.c_volume
!= volno
) {
485 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
490 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
491 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
492 ctime4(&tmpbuf
.c_date
));
493 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
498 tapesread
|= 1 << volno
;
501 * If continuing from the previous volume, skip over any
502 * blocks read already at the end of the previous volume.
504 * If coming to this volume at random, skip to the beginning
505 * of the next record.
507 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
508 tpblksread
, (long)tmpbuf
.c_firstrec
);
509 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
511 tpblksread
= tmpbuf
.c_firstrec
;
512 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
514 } else if (tmpbuf
.c_firstrec
> 0 &&
515 tmpbuf
.c_firstrec
< tpblksread
- 1) {
517 * -1 since we've read the volume header
519 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
520 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
521 (long)i
, i
> 1 ? "s" : "");
526 if (curfile
.action
== USING
) {
528 panic("active file into volume 1\n");
532 * Skip up to the beginning of the next record
534 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
535 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
537 (void) gethead(&spcl
);
541 siglongjmp(restart
, 1);
546 * Handle unexpected EOF.
552 if (gettingfile
&& curfile
.action
== USING
) {
553 printf("Warning: %s %s\n",
554 "End-of-input encountered while extracting", curfile
.name
);
556 curfile
.name
= "<name unknown>";
557 curfile
.action
= UNKNOWN
;
559 curfile
.ino
= maxino
;
562 siglongjmp(restart
, 1);
567 * handle multiple dumps per tape by skipping forward to the
575 if (dumpnum
== 1 || volno
!= 1)
578 errx(1, "Cannot have multiple dumps on pipe input");
580 tcom
.mt_count
= dumpnum
- 1;
583 rmtioctl(MTFSF
, dumpnum
- 1);
586 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
593 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
594 fprintf(stdout
, "Dumped from: %s",
595 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
596 if (spcl
.c_host
[0] == '\0')
598 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
599 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
600 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
604 extractfile(char *name
)
608 struct timeval timep
[2];
612 curfile
.action
= USING
;
614 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
615 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
616 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
617 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
618 #else /* __linux__ */
619 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
620 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
621 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
622 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
623 #endif /* __linux__ */
624 mode
= curfile
.dip
->di_mode
;
625 flags
= curfile
.dip
->di_flags
;
626 switch (mode
& IFMT
) {
629 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
634 Vprintf(stdout
, "skipped socket %s\n", name
);
640 ep
= lookupname(name
);
641 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
642 panic("unextracted directory %s\n", name
);
646 Vprintf(stdout
, "extract file %s\n", name
);
647 return (genliteraldir(name
, curfile
.ino
));
652 uid_t luid
= curfile
.dip
->di_uid
;
653 gid_t lgid
= curfile
.dip
->di_gid
;
657 getfile(xtrlnkfile
, xtrlnkskip
);
660 "%s: zero length symbolic link (ignored)\n", name
);
663 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
666 (void) lchown(name
, luid
, lgid
);
672 Vprintf(stdout
, "extract fifo %s\n", name
);
679 if (mkfifo(name
, mode
) < 0) {
680 warn("%s: cannot create fifo", name
);
684 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
685 (void) chmod(name
, mode
);
688 (void) fsetflags(name
, flags
);
690 (void) chflags(name
, flags
);
698 Vprintf(stdout
, "extract special file %s\n", name
);
705 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
706 warn("%s: cannot create special file", name
);
710 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
711 (void) chmod(name
, mode
);
715 warn("%s: fsetflags called on a special file", name
);
716 (void) fsetflags(name
, flags
);
719 (void) chflags(name
, flags
);
726 Vprintf(stdout
, "extract file %s\n", name
);
733 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
735 warn("%s: cannot create file", name
);
739 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
740 (void) fchmod(ofile
, mode
);
743 (void) setflags(ofile
, flags
);
745 (void) fchflags(ofile
, flags
);
747 getfile(xtrfile
, xtrskip
);
756 * skip over bit maps on the tape
762 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
767 * skip over a file on the tape
773 curfile
.action
= SKIP
;
774 getfile(xtrnull
, xtrnull
);
778 * Extract a file from the tape.
779 * When an allocated block is found it is passed to the fill function;
780 * when an unallocated block (hole) is found, a zeroed buffer is passed
781 * to the skip function.
784 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
787 volatile int curblk
= 0;
788 volatile quad_t size
= spcl
.c_dinode
.di_size
;
789 volatile int last_write_was_hole
= 0;
790 quad_t origsize
= size
;
791 static char clearedbuf
[MAXBSIZE
];
792 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
795 if (spcl
.c_type
== TS_END
)
796 panic("ran off end of tape\n");
797 if (spcl
.c_magic
!= NFS_MAGIC
)
798 panic("not at beginning of a file\n");
799 if (!gettingfile
&& setjmp(restart
) != 0)
803 for (i
= 0; i
< spcl
.c_count
; i
++) {
804 if (readmapflag
|| spcl
.c_addr
[i
]) {
805 readtape(&buf
[curblk
++][0]);
806 if (curblk
== fssize
/ TP_BSIZE
) {
807 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
808 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
810 last_write_was_hole
= 0;
814 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
816 (curblk
- 1) * TP_BSIZE
+ size
));
819 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
821 last_write_was_hole
= 1;
823 if ((size
-= TP_BSIZE
) <= 0) {
824 for (i
++; i
< spcl
.c_count
; i
++)
825 if (readmapflag
|| spcl
.c_addr
[i
])
830 if (gethead(&spcl
) == GOOD
&& size
> 0) {
831 if (spcl
.c_type
== TS_ADDR
)
834 "Missing address (header) block for %s at %ld blocks\n",
835 curfile
.name
, (long)blksread
);
838 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
839 last_write_was_hole
= 0;
842 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
844 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
845 (*skip
)(clearedbuf
, skp
);
848 last_write_was_hole
= 1;
850 if (last_write_was_hole
) {
851 FTRUNCATE(ofile
, origsize
);
858 * Write out the next block of a file.
861 xtrfile(char *buf
, size_t size
)
866 if (write(ofile
, buf
, (int) size
) == -1)
867 err(1, "write error extracting inode %lu, name %s\nwrite",
868 (unsigned long)curfile
.ino
, curfile
.name
);
872 * Skip over a hole in a file.
876 xtrskip(char *buf
, size_t size
)
879 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
880 err(1, "seek error extracting inode %lu, name %s\nlseek",
881 (unsigned long)curfile
.ino
, curfile
.name
);
885 * Collect the next block of a symbolic link.
888 xtrlnkfile(char *buf
, size_t size
)
892 if (pathlen
> MAXPATHLEN
)
893 errx(1, "symbolic link name: %s->%s%s; too long %d",
894 curfile
.name
, lnkbuf
, buf
, pathlen
);
895 (void) strcat(lnkbuf
, buf
);
899 * Skip over a hole in a symbolic link (should never happen).
903 xtrlnkskip(char *buf
, size_t size
)
906 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
910 * Collect the next block of a bit map.
913 xtrmap(char *buf
, size_t size
)
916 memmove(map
, buf
, size
);
921 * Skip over a hole in a bit map (should never happen).
925 xtrmapskip(char *buf
, size_t size
)
928 panic("hole in map\n");
933 * Noop, when an extraction function is not needed.
937 xtrnull(char *buf
, size_t size
)
945 * Compare the next block of a file.
948 xtrcmpfile(char *buf
, size_t size
)
950 static char cmpbuf
[MAXBSIZE
];
955 if (read(ifile
, cmpbuf
, size
) != size
) {
956 fprintf(stderr
, "%s: size has changed.\n",
962 if (memcmp(buf
, cmpbuf
, size
) != 0) {
963 fprintf(stderr
, "%s: tape and disk copies are different\n",
971 * Skip over a hole in a file.
974 xtrcmpskip(char *buf
, size_t size
)
976 static char cmpbuf
[MAXBSIZE
];
982 if (read(ifile
, cmpbuf
, size
) != size
) {
983 fprintf(stderr
, "%s: size has changed.\n",
989 for (i
= 0; i
< size
; ++i
)
990 if (cmpbuf
[i
] != '\0') {
991 fprintf(stderr
, "%s: tape and disk copies are different\n",
997 #endif /* COMPARE_ONTHEFLY */
999 #if !COMPARE_ONTHEFLY
1001 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1003 static char buf_tape
[BUFSIZ
];
1004 static char buf_disk
[BUFSIZ
];
1009 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1010 close(fd_tape
), close(fd_disk
);
1011 panic("do_cmpfiles: unexpected EOF[1]");
1013 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1014 close(fd_tape
), close(fd_disk
);
1015 panic("do_cmpfiles: unexpected EOF[2]");
1017 if (n_tape
!= n_disk
) {
1018 close(fd_tape
), close(fd_disk
);
1019 panic("do_cmpfiles: sizes different!");
1021 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1027 /* for debugging compare problems */
1028 #undef COMPARE_FAIL_KEEP_FILE
1031 #ifdef COMPARE_FAIL_KEEP_FILE
1032 /* return true if tapefile should be unlinked after compare */
1037 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1039 struct STAT sbuf_tape
;
1040 int fd_tape
, fd_disk
;
1042 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1043 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1048 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1050 "%s: size changed from %ld to %ld.\n",
1051 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1053 #ifdef COMPARE_FAIL_KEEP_FILE
1060 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1061 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1064 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1066 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1070 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1071 fprintf(stderr
, "%s: tape and disk copies are different\n",
1076 #ifdef COMPARE_FAIL_KEEP_FILE
1077 /* rename the file to live in /tmp */
1078 /* rename `tapefile' to /tmp/<basename of diskfile> */
1080 char *p
= strrchr(diskfile
, '/');
1081 char newname
[MAXPATHLEN
];
1083 panic("can't find / in %s\n", diskfile
);
1085 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1086 if (rename(tapefile
, newname
)) {
1087 panic("rename from %s to %s failed: %s\n",
1091 fprintf(stderr
, "*** %s saved to %s\n",
1096 /* don't unlink the file (it's not there anymore */
1105 #ifdef COMPARE_FAIL_KEEP_FILE
1109 #endif /* !COMPARE_ONTHEFLY */
1111 #if !COMPARE_ONTHEFLY
1112 static char tmpfilename
[MAXPATHLEN
];
1116 comparefile(char *name
)
1121 #if !COMPARE_ONTHEFLY
1122 static char *tmpfile
= NULL
;
1126 if ((r
= LSTAT(name
, &sb
)) != 0) {
1127 warn("%s: does not exist (%d)", name
, r
);
1133 curfile
.name
= name
;
1134 curfile
.action
= USING
;
1135 mode
= curfile
.dip
->di_mode
;
1137 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1138 (long)sb
.st_size
, mode
);
1140 if (sb
.st_mode
!= mode
) {
1141 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1142 name
, mode
& 07777, sb
.st_mode
& 07777);
1145 switch (mode
& IFMT
) {
1159 char lbuf
[MAXPATHLEN
+ 1];
1162 if (!(sb
.st_mode
& S_IFLNK
)) {
1163 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1170 getfile(xtrlnkfile
, xtrlnkskip
);
1173 "%s: zero length symbolic link (ignored)\n",
1178 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1179 panic("readlink of %s failed: %s", name
,
1184 if (strcmp(lbuf
, lnkbuf
) != 0) {
1186 "%s: symbolic link changed from %s to %s.\n",
1187 name
, lnkbuf
, lbuf
);
1196 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1197 fprintf(stderr
, "%s: no longer a special file\n",
1204 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1206 "%s: device changed from %d,%d to %d,%d.\n",
1208 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1209 (int)curfile
.dip
->di_rdev
& 0xff,
1210 ((int)sb
.st_rdev
>> 8) & 0xff,
1211 (int)sb
.st_rdev
& 0xff);
1218 #if COMPARE_ONTHEFLY
1219 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1220 panic("Can't open %s: %s\n", name
, strerror(errno
));
1226 getfile(xtrcmpfile
, xtrcmpskip
);
1229 if (read(ifile
, &c
, 1) != 0) {
1230 fprintf(stderr
, "%s: size has changed.\n",
1240 if (tmpfile
== NULL
) {
1241 /* argument to mktemp() must not be in RO space: */
1242 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1243 tmpfile
= mktemp(&tmpfilename
[0]);
1245 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1246 panic("cannot delete tmp file %s: %s\n",
1247 tmpfile
, strerror(errno
));
1249 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1250 panic("cannot create file temp file %s: %s\n",
1251 name
, strerror(errno
));
1253 getfile(xtrfile
, xtrskip
);
1254 (void) close(ofile
);
1255 #ifdef COMPARE_FAIL_KEEP_FILE
1256 if (cmpfiles(tmpfile
, name
, &sb
))
1259 cmpfiles(tmpfile
, name
, &sb
);
1262 #endif /* COMPARE_ONTHEFLY */
1269 static void (*readtape_func
)(char *) = readtape_set
;
1272 * Read TP_BSIZE blocks from the input.
1273 * Handle read errors, and end of media.
1274 * Decompress compressed blocks.
1279 (*readtape_func
)(buf
); /* call the actual processing routine */
1283 * Set function pointer for readtape() routine. zflag and magtapein must
1284 * be correctly set before the first call to readtape().
1287 readtape_set(char *buf
)
1290 readtape_func
= readtape_uncompr
;
1294 readtape_func
= readtape_comprtape
;
1296 readtape_func
= readtape_comprfile
;
1301 #endif /* HAVE_ZLIB */
1304 * This is the original readtape(), it's used for reading uncompressed input.
1305 * Read TP_BSIZE blocks from the input.
1306 * Handle read errors, and end of media.
1310 readtape_uncompr(char *buf
)
1315 ssize_t rd
, newvol
, i
;
1316 int cnt
, seek_failed
;
1318 if (blkcnt
< numtrec
) {
1319 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1325 for (i
= 0; i
< ntrec
; i
++)
1326 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1329 cnt
= ntrec
* TP_BSIZE
;
1334 i
= rmtread(&tapebuf
[rd
], cnt
);
1337 i
= read(mt
, &tapebuf
[rd
], cnt
);
1340 * Check for mid-tape short read error.
1341 * If found, skip rest of buffer and start with the next.
1343 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1344 Dprintf(stdout
, "mid-media short read error.\n");
1348 * Handle partial block read.
1350 if (pipein
&& i
== 0 && rd
> 0)
1352 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1361 * Short read. Process the blocks read.
1363 if (i
% TP_BSIZE
!= 0)
1365 "partial block read: %ld should be %ld\n",
1366 (long)i
, ntrec
* TP_BSIZE
);
1367 numtrec
= i
/ TP_BSIZE
;
1371 * Handle read error.
1374 fprintf(stderr
, "Tape read error while ");
1375 switch (curfile
.action
) {
1377 fprintf(stderr
, "trying to set up tape\n");
1380 fprintf(stderr
, "trying to resynchronize\n");
1383 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1386 fprintf(stderr
, "skipping over inode %lu\n",
1387 (unsigned long)curfile
.ino
);
1390 if (!yflag
&& !reply("continue"))
1392 i
= ntrec
* TP_BSIZE
;
1393 memset(tapebuf
, 0, (size_t)i
);
1396 seek_failed
= (rmtseek(i
, 1) < 0);
1399 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1402 warn("continuation failed");
1403 if (!yflag
&& !reply("assume end-of-tape and continue"))
1409 * Handle end of tape.
1412 Vprintf(stdout
, "End-of-tape encountered\n");
1421 if (rd
% TP_BSIZE
!= 0)
1422 panic("partial block read: %d should be %d\n",
1423 rd
, ntrec
* TP_BSIZE
);
1425 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1428 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1436 * Read a compressed format block from a file or pipe and uncompress it.
1437 * Attempt to handle read errors, and end of file.
1440 readtape_comprfile(char *buf
)
1442 long rl
, size
, i
, ret
;
1444 struct tapebuf
*tpb
;
1446 if (blkcnt
< numtrec
) {
1447 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1452 /* need to read the next block */
1454 for (i
= 0; i
< ntrec
; i
++)
1455 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1457 tpb
= (struct tapebuf
*) tapebuf
;
1459 /* read the block prefix */
1460 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1466 if (size
> bufsize
) {
1467 /* something's wrong */
1468 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1471 tpb
->length
= bufsize
;
1473 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1477 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1478 if (tbufptr
== NULL
) {
1479 msg_read_error("File decompression error while");
1480 if (!yflag
&& !reply("continue"))
1482 memset(tapebuf
, 0, bufsize
);
1487 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1493 /* Errors while reading from a file or pipe are catastrophic. Since
1494 * there are no block boundaries, it's impossible to bypass the
1495 * block in error and find the start of the next block.
1498 /* It's possible to have multiple input files using -M
1499 * and -f file1,file2...
1501 Vprintf(stdout
, "End-of-File encountered\n");
1511 msg_read_error("Read error while");
1512 /* if (!yflag && !reply("continue")) */
1517 * Read compressed data from a tape and uncompress it.
1518 * Handle read errors, and end of media.
1519 * Since a tape consists of separate physical blocks, we try
1520 * to recover from errors by repositioning the tape to the next
1524 readtape_comprtape(char *buf
)
1528 struct tapebuf
*tpb
;
1531 if (blkcnt
< numtrec
) {
1532 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1537 /* need to read the next block */
1539 for (i
= 0; i
< ntrec
; i
++)
1540 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1542 tpb
= (struct tapebuf
*) tapebuf
;
1544 /* read the block */
1545 size
= bufsize
+ PREFIXSIZE
;
1546 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1550 tbufptr
= decompress_tapebuf(tpb
, rl
);
1551 if (tbufptr
== NULL
) {
1552 msg_read_error("Tape decompression error while");
1553 if (!yflag
&& !reply("continue"))
1555 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1561 /* Handle errors: EOT switches to the next volume, other errors
1562 * attempt to position the tape to the next block.
1565 Vprintf(stdout
, "End-of-tape encountered\n");
1574 msg_read_error("Tape read error while");
1575 if (!yflag
&& !reply("continue"))
1577 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1582 rl
= rmtioctl(MTFSR
, 1);
1588 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1592 warn("continuation failed");
1593 if (!yflag
&& !reply("assume end-of-tape and continue"))
1595 ret
= 0; /* end of tape */
1601 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1607 * Decompress a struct tapebuf into a buffer. readsize is the size read
1608 * from the tape/file and is used for error messages. Returns a pointer
1609 * to the location of the uncompressed buffer or NULL on errors.
1610 * Adjust numtrec and complain for a short block.
1613 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1615 /* If zflag is on, all blocks have a struct tapebuf prefix */
1616 /* zflag gets set in setup() from the dump header */
1617 int cresult
, blocklen
;
1618 unsigned long worklen
;
1619 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1621 /* build a length error message */
1622 blocklen
= tpbin
->length
;
1623 if (readsize
< blocklen
+ PREFIXSIZE
)
1624 lengtherr
= "short";
1626 if (readsize
> blocklen
+ PREFIXSIZE
)
1631 if (tpbin
->compressed
) {
1632 /* uncompress whatever we read, if it fails, complain later */
1633 cresult
= uncompress(comprbuf
, &worklen
, tpbin
->buf
, blocklen
);
1637 output
= tpbin
->buf
;
1642 numtrec
= worklen
/ TP_BSIZE
;
1643 if (worklen
% TP_BSIZE
!= 0)
1644 reason
= "length mismatch";
1647 reason
= "not enough memory";
1650 reason
= "buffer too small";
1653 reason
= "data error";
1660 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1661 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1662 fprintf(stderr
, "decompression error, block %ld: %s\n",
1663 tpblksread
+1, reason
);
1664 if (cresult
!= Z_OK
) output
= NULL
;
1670 * Print an error message for a read error.
1671 * This was exteracted from the original readtape().
1674 msg_read_error(char *m
)
1676 switch (curfile
.action
) {
1678 fprintf(stderr
, "%s trying to set up tape\n", m
);
1681 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1684 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1687 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1688 (unsigned long)curfile
.ino
);
1692 #endif /* HAVE_ZLIB */
1695 * Read the first block and get the blocksize from it. Test
1696 * for a compressed dump tape/file. setup() will make the final
1697 * determination by checking the compressed flag if gethead()
1698 * finds a valid header. The test here is necessary to offset the buffer
1699 * by the size of the compressed prefix. zflag is set here so that
1700 * readtape_set can set the correct function pointer for readtape().
1701 * Note that the first block of each tape/file is not compressed
1702 * and does not have a prefix.
1705 findtapeblksize(void)
1709 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1710 struct s_spcl
*spclpt
= (struct s_spcl
*) tapebuf
;
1712 for (i
= 0; i
< ntrec
; i
++)
1713 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1717 * For a pipe or file, read in the first record. For a tape, read
1720 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
1722 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1723 errx(1, "Tape read error on first record");
1726 * If the input is from a file or a pipe, we read TP_BSIZE
1727 * bytes looking for a dump header. If the dump is compressed
1728 * we need to read in the rest of the block, as determined
1729 * by c_ntrec in the dump header. The first block of the
1730 * dump is not compressed and does not have a prefix.
1733 if (spclpt
->c_type
== TS_TAPE
1734 && spclpt
->c_flags
& DR_COMPRESSED
) {
1735 /* It's a compressed dump file, read in the */
1736 /* rest of the block based on spclpt->c_ntrec. */
1737 if (spclpt
->c_ntrec
> ntrec
)
1738 errx(1, "Tape blocksize is too large, use "
1739 "\'-b %d\' ", spclpt
->c_ntrec
);
1740 ntrec
= spclpt
->c_ntrec
;
1741 len
= (ntrec
- 1) * TP_BSIZE
;
1745 /* read in the rest of the block based on bufsize */
1746 len
= bufsize
- TP_BSIZE
;
1748 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1749 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1750 errx(1,"Error reading dump file header");
1753 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1755 } /* if (!magtapein) */
1758 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1759 * If the value of ntrec is too large, we read less than
1760 * what we asked for; adjust the value of ntrec and test for
1761 * a compressed dump tape.
1764 if (i
% TP_BSIZE
!= 0) {
1765 /* may be old format compressed dump tape with a prefix */
1766 spclpt
= (struct s_spcl
*) tpb
->buf
;
1767 if (i
% TP_BSIZE
== PREFIXSIZE
1768 && tpb
->compressed
== 0
1769 && spclpt
->c_type
== TS_TAPE
1770 && spclpt
->c_flags
& DR_COMPRESSED
) {
1773 if (tpb
->length
> bufsize
)
1774 errx(1, "Tape blocksize is too large, use "
1775 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1778 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1781 ntrec
= i
/ TP_BSIZE
;
1782 if (spclpt
->c_type
== TS_TAPE
) {
1783 if (spclpt
->c_flags
& DR_COMPRESSED
)
1785 if (spclpt
->c_ntrec
> ntrec
)
1786 errx(1, "Tape blocksize is too large, use "
1787 "\'-b %d\' ", spclpt
->c_ntrec
);
1790 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1794 * Read a block of data handling all of the messy details.
1796 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1804 i
= rmtread(buf
, size
);
1807 i
= read(fd
, buf
, size
);
1810 break; /* EOD or error */
1813 break; /* block at a time for mt */
1816 *lengthread
= len
- size
;
1835 setmagtapein(void) {
1836 struct mtget mt_stat
;
1837 static int done
= 0;
1842 /* need to know if input is really from a tape */
1845 magtapein
= rmtioctl(MTNOP
, 1) != -1;
1848 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
1851 Vprintf(stdout
,"Input is from %s\n", magtapein
? "tape": "file/pipe");
1855 * Read the next block from the tape.
1856 * Check to see if it is one of several vintage headers.
1857 * If it is an old style header, convert it to a new style header.
1858 * If it is not any valid header, return an error.
1861 gethead(struct s_spcl
*buf
)
1869 char dummy
[TP_BSIZE
];
1876 u_int16_t c_inumber
;
1881 u_int16_t odi_nlink
;
1897 readtape((char *)buf
);
1898 if (buf
->c_magic
!= NFS_MAGIC
) {
1899 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1902 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1906 if (checksum((int *)buf
) == FAIL
)
1909 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1912 readtape((char *)(&u_ospcl
.s_ospcl
));
1913 memset((char *)buf
, 0, (long)TP_BSIZE
);
1914 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1915 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1916 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1917 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1918 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1919 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1920 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1921 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1922 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1923 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1924 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1925 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1926 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1927 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1929 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1930 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1931 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1932 #else /* __linux__ */
1933 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1934 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1935 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1936 #endif /* __linux__ */
1937 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1938 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1939 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1940 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1942 buf
->c_magic
= NFS_MAGIC
;
1945 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1946 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1947 qcvt
.qval
= buf
->c_dinode
.di_size
;
1948 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1949 printf("Note: Doing Quad swapping\n");
1954 qcvt
.qval
= buf
->c_dinode
.di_size
;
1956 qcvt
.val
[1] = qcvt
.val
[0];
1958 buf
->c_dinode
.di_size
= qcvt
.qval
;
1962 switch (buf
->c_type
) {
1967 * Have to patch up missing information in bit map headers
1970 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1971 if (buf
->c_count
> TP_NINDIR
)
1974 for (i
= 0; i
< buf
->c_count
; i
++)
1979 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1991 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1995 * If we are restoring a filesystem with old format inodes,
1996 * copy the uid/gid to the new location.
1999 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2000 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2008 * Check that a header is where it belongs and predict the next header
2011 accthdr(struct s_spcl
*header
)
2013 static dump_ino_t previno
= 0x7fffffff;
2014 static int prevtype
;
2015 static long predict
;
2018 if (header
->c_type
== TS_TAPE
) {
2019 fprintf(stderr
, "Volume header (%s inode format) ",
2020 oldinofmt
? "old" : "new");
2021 if (header
->c_firstrec
)
2022 fprintf(stderr
, "begins with record %d",
2023 header
->c_firstrec
);
2024 fprintf(stderr
, "\n");
2025 previno
= 0x7fffffff;
2028 if (previno
== 0x7fffffff)
2032 fprintf(stderr
, "Dumped inodes map header");
2035 fprintf(stderr
, "Used inodes map header");
2038 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2041 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2044 fprintf(stderr
, "End of tape header");
2047 if (predict
!= blksread
- 1)
2048 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2049 predict
, blksread
- 1);
2050 fprintf(stderr
, "\n");
2053 if (header
->c_type
!= TS_END
)
2054 for (i
= 0; i
< header
->c_count
; i
++)
2055 if (readmapflag
|| header
->c_addr
[i
] != 0)
2059 prevtype
= header
->c_type
;
2060 previno
= header
->c_inumber
;
2064 * Find an inode header.
2065 * Complain if had to skip, and complain is set.
2068 findinode(struct s_spcl
*header
)
2070 static long skipcnt
= 0;
2074 curfile
.name
= "<name unknown>";
2075 curfile
.action
= UNKNOWN
;
2079 if (header
->c_magic
!= NFS_MAGIC
) {
2081 while (gethead(header
) == FAIL
||
2082 header
->c_date
!= dumpdate
)
2085 switch (header
->c_type
) {
2089 * Skip up to the beginning of the next record
2091 for (i
= 0; i
< header
->c_count
; i
++)
2092 if (header
->c_addr
[i
])
2094 while (gethead(header
) == FAIL
||
2095 header
->c_date
!= dumpdate
)
2100 curfile
.dip
= &header
->c_dinode
;
2101 curfile
.ino
= header
->c_inumber
;
2105 curfile
.ino
= maxino
;
2109 curfile
.name
= "<file removal list>";
2113 curfile
.name
= "<file dump list>";
2117 panic("unexpected tape header\n");
2121 panic("unknown tape header type %d\n", spcl
.c_type
);
2125 } while (header
->c_type
== TS_ADDR
);
2130 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2140 j
= sizeof(union u_spcl
) / sizeof(int);
2147 /* What happens if we want to read restore tapes
2148 for a 16bit int machine??? */
2154 if (i
!= CHECKSUM
) {
2155 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2156 (unsigned long)curfile
.ino
, curfile
.name
);
2166 #include <varargs.h>
2171 msg(const char *fmt
, ...)
2184 (void)vfprintf(stderr
, fmt
, ap
);
2187 #endif /* RRESTORE */
2190 swab16(u_char
*sp
, int n
)
2195 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2202 swab32(u_char
*sp
, int n
)
2207 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2208 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2215 swab64(u_char
*sp
, int n
)
2220 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2221 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2222 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2223 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2230 swabst(u_char
*cp
, u_char
*sp
)
2236 case '0': case '1': case '2': case '3': case '4':
2237 case '5': case '6': case '7': case '8': case '9':
2238 n
= (n
* 10) + (*cp
++ - '0');
2241 case 's': case 'w': case 'h':
2259 default: /* Any other character, like 'b' counts as byte. */
2273 swabst((u_char
*)"i", (u_char
*)&x
);
2281 swabst((u_char
*)"l", (u_char
*)&x
);
2288 * get the current position of the tape
2291 GetTapePos(long *pos
)
2296 if (ioctl(mt
, MTIOCPOS
, pos
) == -1) {
2298 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %ld)\n",
2299 (unsigned long)getpid(), err
, *pos
);
2305 typedef struct mt_pos
{
2308 } MTPosRec
, *MTPosPtr
;
2311 * go to specified position on tape
2314 GotoTapePos(long pos
)
2321 if (ioctl(mt
, MTIOCTOP
, &buf
) == -1) {
2323 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %ld)\n",
2324 (unsigned long)getpid(), err
, pos
);
2331 * read next data from tape to re-sync
2334 ReReadFromTape(void)
2338 if (gethead(&spcl
) == FAIL
) {
2340 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2348 RequestVol(long tnum
)
2353 #endif /* USE_QFA */