]>
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.37 2001/04/27 15:22:47 stelian Exp $";
53 #include <compatlfs.h>
55 #include <compaterr.h>
62 #include <sys/param.h>
70 #include <linux/ext2_fs.h>
71 #include <ext2fs/ext2fs.h>
72 #include <bsdcompat.h>
74 #include <ufs/ufs/dinode.h>
75 #endif /* __linux__ */
76 #include <protocols/dumprestore.h>
80 #endif /* HAVE_ZLIB */
84 #include "pathnames.h"
89 static long fssize
= MAXBSIZE
;
91 static int pipein
= 0;
92 static int magtapein
= 0; /* input is from magtape */
93 static char magtape
[MAXPATHLEN
];
94 static char magtapeprefix
[MAXPATHLEN
];
97 static char *tapebuf
; /* input buffer for read */
98 static int bufsize
; /* buffer size without prefix */
99 static char *tbufptr
= NULL
; /* active tape buffer */
101 static char *comprbuf
; /* uncompress work buf */
102 static size_t comprlen
; /* size including prefix */
104 static union u_spcl endoftapemark
;
105 static long blksread
; /* blocks read since last header */
106 static long tpblksread
= 0; /* TP_BSIZE blocks read */
107 static long tapesread
;
108 static sigjmp_buf restart
;
109 static int gettingfile
= 0; /* restart has a valid frame */
110 static char *host
= NULL
;
114 static char lnkbuf
[MAXPATHLEN
+ 1];
117 int oldinofmt
; /* old inode format conversion required */
118 int Bcvt
; /* Swap Bytes (for CCI or sun) */
119 static int Qcvt
; /* Swap quads (for sun) */
121 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
123 static void accthdr
__P((struct s_spcl
*));
124 static int checksum
__P((int *));
125 static void findinode
__P((struct s_spcl
*));
126 static void findtapeblksize
__P((void));
127 static int gethead
__P((struct s_spcl
*));
128 static void readtape
__P((char *));
129 static void setdumpnum
__P((void));
130 static u_int swabi
__P((u_int
));
132 static u_long swabl
__P((u_long
));
134 static u_char
*swab64
__P((u_char
*, int));
135 static u_char
*swab32
__P((u_char
*, int));
136 static u_char
*swab16
__P((u_char
*, int));
137 static void terminateinput
__P((void));
138 static void xtrfile
__P((char *, size_t));
139 static void xtrlnkfile
__P((char *, size_t));
140 static void xtrlnkskip
__P((char *, size_t));
141 static void xtrmap
__P((char *, size_t));
142 static void xtrmapskip
__P((char *, size_t));
143 static void xtrskip
__P((char *, size_t));
146 static void newcomprbuf
__P((long));
147 static void readtape_set
__P((char *));
148 static void readtape_uncompr
__P((char *));
149 static void readtape_comprfile
__P((char *));
150 static void readtape_comprtape
__P((char *));
151 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
152 static void msg_read_error
__P((char *));
154 static int read_a_block
__P((int, char *, size_t, long *));
155 #define PREFIXSIZE sizeof(struct tapebuf)
157 #define COMPARE_ONTHEFLY 1
160 static int ifile
; /* input file for compare */
161 static int cmperror
; /* compare error */
162 static void xtrcmpfile
__P((char *, size_t));
163 static void xtrcmpskip
__P((char *, size_t));
166 static int readmapflag
;
169 * Set up an input source. This is called from main.c before setup() is.
172 setinput(char *source
)
178 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
182 if (strchr(source
, ':')) {
184 source
= strchr(host
, ':');
186 if (rmthost(host
) == 0)
190 if (strcmp(source
, "-") == 0) {
192 * Since input is coming from a pipe we must establish
193 * our own connection to the terminal.
195 terminal
= fopen(_PATH_TTY
, "r");
196 if (terminal
== NULL
) {
197 warn("cannot open %s", _PATH_TTY
);
198 terminal
= fopen(_PATH_DEVNULL
, "r");
199 if (terminal
== NULL
)
200 err(1, "cannot open %s", _PATH_DEVNULL
);
204 setuid(getuid()); /* no longer need or want root privileges */
206 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
207 magtapeprefix
[MAXPATHLEN
-1] = '\0';
208 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
211 strncpy(magtape
, source
, MAXPATHLEN
);
212 magtape
[MAXPATHLEN
- 1] = '\0';
216 newtapebuf(long size
)
218 static int tapebufsize
= -1;
221 bufsize
= ntrec
* TP_BSIZE
;
222 if (size
<= tapebufsize
)
226 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
228 errx(1, "Cannot allocate space for tape buffer");
234 newcomprbuf(long size
)
236 if (size
<= comprlen
)
238 comprlen
= size
+ sizeof(struct tapebuf
);
239 if (comprbuf
!= NULL
)
241 comprbuf
= malloc(comprlen
);
242 if (comprbuf
== NULL
)
243 errx(1, "Cannot allocate space for decompress buffer");
245 #endif /* HAVE_ZLIB */
248 * Verify that the tape drive can be accessed and
249 * that it actually is a dump tape.
256 struct mtget mt_stat
;
258 Vprintf(stdout
, "Verify tape and initialize maps\n");
261 mt
= rmtopen(magtape
, 0);
267 mt
= OPEN(magtape
, O_RDONLY
, 0);
269 err(1, "%s", magtape
);
272 /* need to know if input is really from a tape */
275 magtapein
= rmtioctl(MTNOP
, 1) != -1;
278 if (ioctl(mt
, MTIOCGET
, (char *) &mt_stat
) == 0) {
279 if (mt_stat
.mt_dsreg
& 0xffff)
280 magtapein
= 1; /* fixed blocksize */
282 magtapein
= 2; /* variable blocksize */
286 Vprintf(stdout
,"Input is from %s\n", magtapein
? "tape": "file/pipe");
290 if (gethead(&spcl
) == FAIL
) {
291 blkcnt
--; /* push back this block */
295 if (gethead(&spcl
) == FAIL
)
296 errx(1, "Tape is not a dump tape");
297 fprintf(stderr
, "Converting to new file system format.\n");
301 fprintf(stderr
, "Dump tape is compressed.\n");
303 newcomprbuf(bufsize
);
305 errx(1,"This restore version doesn't support decompression");
306 #endif /* HAVE_ZLIB */
309 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
310 endoftapemark
.s_spcl
.c_type
= TS_END
;
311 ip
= (int *)&endoftapemark
;
312 j
= sizeof(union u_spcl
) / sizeof(int);
317 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
319 if (vflag
|| command
== 't' || command
== 'C')
321 if (filesys
[0] == '\0') {
323 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
324 filesys
[NAMELEN
- 1] = '\0';
325 dirptr
= strstr(filesys
, " (dir");
329 dumptime
= spcl
.c_ddate
;
330 dumpdate
= spcl
.c_date
;
331 if (STAT(".", &stbuf
) < 0)
332 err(1, "cannot stat .");
333 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
335 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
336 fssize
= stbuf
.st_blksize
;
337 if (((fssize
- 1) & fssize
) != 0)
338 errx(1, "bad block size %ld", fssize
);
339 if (spcl
.c_volume
!= 1)
340 errx(1, "Tape is not volume 1 of the dump");
341 if (gethead(&spcl
) == FAIL
) {
342 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
343 panic("no header after volume mark!\n");
346 if (spcl
.c_type
!= TS_CLRI
)
347 errx(1, "Cannot find file removal list");
348 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
349 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
350 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
352 errx(1, "no memory for active inode map");
354 curfile
.action
= USING
;
355 getfile(xtrmap
, xtrmapskip
);
356 if (spcl
.c_type
!= TS_BITS
)
357 errx(1, "Cannot find file dump list");
358 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
359 if (map
== (char *)NULL
)
360 errx(1, "no memory for file dump list");
362 curfile
.action
= USING
;
363 getfile(xtrmap
, xtrmapskip
);
365 * If there may be whiteout entries on the tape, pretend that the
366 * whiteout inode exists, so that the whiteout entries can be
370 SETINO(WINO
, dumpmap
);
374 * Prompt user to load a new dump volume.
375 * "Nextvol" is the next suggested volume to use.
376 * This suggested volume is enforced when doing full
377 * or incremental restores, but can be overridden by
378 * the user when only extracting a subset of the files.
383 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
384 union u_spcl tmpspcl
;
385 # define tmpbuf tmpspcl.s_spcl
395 panic("Changing volumes on pipe input?\n");
403 exit(1); /* pipes do not get a second chance */
404 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
411 while (newvol
<= 0) {
412 if (tapesread
== 0) {
413 fprintf(stderr
, "%s%s%s%s%s",
414 "You have not read any tapes yet.\n",
415 "Unless you know which volume your",
416 " file(s) are on you should start\n",
417 "with the last volume and work",
418 " towards the first.\n");
420 fprintf(stderr
, "You have read volumes");
422 for (i
= 1; i
< 32; i
++)
423 if (tapesread
& (1 << i
)) {
424 fprintf(stderr
, "%s%ld", buf
, (long)i
);
427 fprintf(stderr
, "\n");
430 fprintf(stderr
, "Specify next volume #: ");
431 (void) fflush(stderr
);
432 (void) fgets(buf
, TP_BSIZE
, terminal
);
433 } while (!feof(terminal
) && buf
[0] == '\n');
439 "Volume numbers are positive numerics\n");
442 if (newvol
== volno
) {
443 tapesread
|= 1 << volno
;
448 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
449 magtape
[MAXPATHLEN
- 1] = '\0';
451 if (!Mflag
|| haderror
) {
453 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
454 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
455 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
456 (void) fflush(stderr
);
457 (void) fgets(buf
, TP_BSIZE
, terminal
);
460 if (!strcmp(buf
, "none\n")) {
464 if (buf
[0] != '\n') {
465 (void) strcpy(magtape
, buf
);
466 magtape
[strlen(magtape
) - 1] = '\0';
471 mt
= rmtopen(magtape
, 0);
474 mt
= OPEN(magtape
, O_RDONLY
, 0);
477 fprintf(stderr
, "Cannot open %s\n", magtape
);
486 if (gethead(&tmpbuf
) == FAIL
) {
487 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
488 fprintf(stderr
, "tape is not dump tape\n");
493 if (tmpbuf
.c_volume
!= volno
) {
494 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
499 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
500 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
501 ctime4(&tmpbuf
.c_date
));
502 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
507 tapesread
|= 1 << volno
;
510 * If continuing from the previous volume, skip over any
511 * blocks read already at the end of the previous volume.
513 * If coming to this volume at random, skip to the beginning
514 * of the next record.
516 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
517 tpblksread
, (long)tmpbuf
.c_firstrec
);
518 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
520 tpblksread
= tmpbuf
.c_firstrec
;
521 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
523 } else if (tmpbuf
.c_firstrec
> 0 &&
524 tmpbuf
.c_firstrec
< tpblksread
- 1) {
526 * -1 since we've read the volume header
528 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
529 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
530 (long)i
, i
> 1 ? "s" : "");
535 if (curfile
.action
== USING
) {
537 panic("active file into volume 1\n");
541 * Skip up to the beginning of the next record
543 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
544 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
546 (void) gethead(&spcl
);
550 siglongjmp(restart
, 1);
555 * Handle unexpected EOF.
561 if (gettingfile
&& curfile
.action
== USING
) {
562 printf("Warning: %s %s\n",
563 "End-of-input encountered while extracting", curfile
.name
);
565 curfile
.name
= "<name unknown>";
566 curfile
.action
= UNKNOWN
;
568 curfile
.ino
= maxino
;
571 siglongjmp(restart
, 1);
576 * handle multiple dumps per tape by skipping forward to the
584 if (dumpnum
== 1 || volno
!= 1)
587 errx(1, "Cannot have multiple dumps on pipe input");
589 tcom
.mt_count
= dumpnum
- 1;
592 rmtioctl(MTFSF
, dumpnum
- 1);
595 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
602 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
603 fprintf(stdout
, "Dumped from: %s",
604 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
605 if (spcl
.c_host
[0] == '\0')
607 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
608 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
609 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
613 extractfile(char *name
)
617 struct timeval timep
[2];
621 curfile
.action
= USING
;
623 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
624 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
625 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
626 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
627 #else /* __linux__ */
628 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
629 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
630 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
631 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
632 #endif /* __linux__ */
633 mode
= curfile
.dip
->di_mode
;
634 flags
= curfile
.dip
->di_flags
;
635 switch (mode
& IFMT
) {
638 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
643 Vprintf(stdout
, "skipped socket %s\n", name
);
649 ep
= lookupname(name
);
650 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
651 panic("unextracted directory %s\n", name
);
655 Vprintf(stdout
, "extract file %s\n", name
);
656 return (genliteraldir(name
, curfile
.ino
));
661 uid_t luid
= curfile
.dip
->di_uid
;
662 gid_t lgid
= curfile
.dip
->di_gid
;
666 getfile(xtrlnkfile
, xtrlnkskip
);
669 "%s: zero length symbolic link (ignored)\n", name
);
672 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
675 (void) lchown(name
, luid
, lgid
);
681 Vprintf(stdout
, "extract fifo %s\n", name
);
688 if (mkfifo(name
, mode
) < 0) {
689 warn("%s: cannot create fifo", name
);
693 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
694 (void) chmod(name
, mode
);
697 (void) fsetflags(name
, flags
);
699 (void) chflags(name
, flags
);
707 Vprintf(stdout
, "extract special file %s\n", name
);
714 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
715 warn("%s: cannot create special file", name
);
719 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
720 (void) chmod(name
, mode
);
724 warn("%s: fsetflags called on a special file", name
);
725 (void) fsetflags(name
, flags
);
728 (void) chflags(name
, flags
);
735 Vprintf(stdout
, "extract file %s\n", name
);
742 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
744 warn("%s: cannot create file", name
);
748 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
749 (void) fchmod(ofile
, mode
);
752 (void) setflags(ofile
, flags
);
754 (void) fchflags(ofile
, flags
);
756 getfile(xtrfile
, xtrskip
);
765 * skip over bit maps on the tape
771 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
776 * skip over a file on the tape
782 curfile
.action
= SKIP
;
783 getfile(xtrnull
, xtrnull
);
787 * Extract a file from the tape.
788 * When an allocated block is found it is passed to the fill function;
789 * when an unallocated block (hole) is found, a zeroed buffer is passed
790 * to the skip function.
793 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
796 volatile int curblk
= 0;
797 volatile quad_t size
= spcl
.c_dinode
.di_size
;
798 volatile int last_write_was_hole
= 0;
799 quad_t origsize
= size
;
800 static char clearedbuf
[MAXBSIZE
];
801 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
804 if (spcl
.c_type
== TS_END
)
805 panic("ran off end of tape\n");
806 if (spcl
.c_magic
!= NFS_MAGIC
)
807 panic("not at beginning of a file\n");
808 if (!gettingfile
&& setjmp(restart
) != 0)
812 for (i
= 0; i
< spcl
.c_count
; i
++) {
813 if (readmapflag
|| spcl
.c_addr
[i
]) {
814 readtape(&buf
[curblk
++][0]);
815 if (curblk
== fssize
/ TP_BSIZE
) {
816 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
817 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
819 last_write_was_hole
= 0;
823 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
825 (curblk
- 1) * TP_BSIZE
+ size
));
828 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
830 last_write_was_hole
= 1;
832 if ((size
-= TP_BSIZE
) <= 0) {
833 for (i
++; i
< spcl
.c_count
; i
++)
834 if (readmapflag
|| spcl
.c_addr
[i
])
839 if (gethead(&spcl
) == GOOD
&& size
> 0) {
840 if (spcl
.c_type
== TS_ADDR
)
843 "Missing address (header) block for %s at %ld blocks\n",
844 curfile
.name
, (long)blksread
);
847 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
848 last_write_was_hole
= 0;
851 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
853 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
854 (*skip
)(clearedbuf
, skp
);
857 last_write_was_hole
= 1;
859 if (last_write_was_hole
) {
860 FTRUNCATE(ofile
, origsize
);
867 * Write out the next block of a file.
870 xtrfile(char *buf
, size_t size
)
875 if (write(ofile
, buf
, (int) size
) == -1)
876 err(1, "write error extracting inode %lu, name %s\nwrite",
877 (unsigned long)curfile
.ino
, curfile
.name
);
881 * Skip over a hole in a file.
885 xtrskip(char *buf
, size_t size
)
888 if (LSEEK(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
889 err(1, "seek error extracting inode %lu, name %s\nlseek",
890 (unsigned long)curfile
.ino
, curfile
.name
);
894 * Collect the next block of a symbolic link.
897 xtrlnkfile(char *buf
, size_t size
)
901 if (pathlen
> MAXPATHLEN
)
902 errx(1, "symbolic link name: %s->%s%s; too long %d",
903 curfile
.name
, lnkbuf
, buf
, pathlen
);
904 (void) strcat(lnkbuf
, buf
);
908 * Skip over a hole in a symbolic link (should never happen).
912 xtrlnkskip(char *buf
, size_t size
)
915 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
919 * Collect the next block of a bit map.
922 xtrmap(char *buf
, size_t size
)
925 memmove(map
, buf
, size
);
930 * Skip over a hole in a bit map (should never happen).
934 xtrmapskip(char *buf
, size_t size
)
937 panic("hole in map\n");
942 * Noop, when an extraction function is not needed.
946 xtrnull(char *buf
, size_t size
)
954 * Compare the next block of a file.
957 xtrcmpfile(char *buf
, size_t size
)
959 static char cmpbuf
[MAXBSIZE
];
964 if (read(ifile
, cmpbuf
, size
) != size
) {
965 fprintf(stderr
, "%s: size has changed.\n",
971 if (memcmp(buf
, cmpbuf
, size
) != 0) {
972 fprintf(stderr
, "%s: tape and disk copies are different\n",
980 * Skip over a hole in a file.
983 xtrcmpskip(char *buf
, size_t size
)
985 static char cmpbuf
[MAXBSIZE
];
991 if (read(ifile
, cmpbuf
, size
) != size
) {
992 fprintf(stderr
, "%s: size has changed.\n",
998 for (i
= 0; i
< size
; ++i
)
999 if (cmpbuf
[i
] != '\0') {
1000 fprintf(stderr
, "%s: tape and disk copies are different\n",
1006 #endif /* COMPARE_ONTHEFLY */
1008 #if !COMPARE_ONTHEFLY
1010 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1012 static char buf_tape
[BUFSIZ
];
1013 static char buf_disk
[BUFSIZ
];
1018 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1019 close(fd_tape
), close(fd_disk
);
1020 panic("do_cmpfiles: unexpected EOF[1]");
1022 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1023 close(fd_tape
), close(fd_disk
);
1024 panic("do_cmpfiles: unexpected EOF[2]");
1026 if (n_tape
!= n_disk
) {
1027 close(fd_tape
), close(fd_disk
);
1028 panic("do_cmpfiles: sizes different!");
1030 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1036 /* for debugging compare problems */
1037 #undef COMPARE_FAIL_KEEP_FILE
1040 #ifdef COMPARE_FAIL_KEEP_FILE
1041 /* return true if tapefile should be unlinked after compare */
1046 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1048 struct STAT sbuf_tape
;
1049 int fd_tape
, fd_disk
;
1051 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1052 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1057 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1059 "%s: size changed from %ld to %ld.\n",
1060 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1062 #ifdef COMPARE_FAIL_KEEP_FILE
1069 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1070 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1073 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1075 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1079 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1080 fprintf(stderr
, "%s: tape and disk copies are different\n",
1085 #ifdef COMPARE_FAIL_KEEP_FILE
1086 /* rename the file to live in /tmp */
1087 /* rename `tapefile' to /tmp/<basename of diskfile> */
1089 char *p
= strrchr(diskfile
, '/');
1090 char newname
[MAXPATHLEN
];
1092 panic("can't find / in %s\n", diskfile
);
1094 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1095 if (rename(tapefile
, newname
)) {
1096 panic("rename from %s to %s failed: %s\n",
1100 fprintf(stderr
, "*** %s saved to %s\n",
1105 /* don't unlink the file (it's not there anymore */
1114 #ifdef COMPARE_FAIL_KEEP_FILE
1118 #endif /* !COMPARE_ONTHEFLY */
1120 #if !COMPARE_ONTHEFLY
1121 static char tmpfilename
[MAXPATHLEN
];
1125 comparefile(char *name
)
1130 #if !COMPARE_ONTHEFLY
1131 static char *tmpfile
= NULL
;
1135 if ((r
= LSTAT(name
, &sb
)) != 0) {
1136 warn("%s: does not exist (%d)", name
, r
);
1142 curfile
.name
= name
;
1143 curfile
.action
= USING
;
1144 mode
= curfile
.dip
->di_mode
;
1146 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1147 (long)sb
.st_size
, mode
);
1149 if (sb
.st_mode
!= mode
) {
1150 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1151 name
, mode
& 07777, sb
.st_mode
& 07777);
1154 switch (mode
& IFMT
) {
1168 char lbuf
[MAXPATHLEN
+ 1];
1171 if (!(sb
.st_mode
& S_IFLNK
)) {
1172 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1179 getfile(xtrlnkfile
, xtrlnkskip
);
1182 "%s: zero length symbolic link (ignored)\n",
1187 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1188 panic("readlink of %s failed: %s", name
,
1193 if (strcmp(lbuf
, lnkbuf
) != 0) {
1195 "%s: symbolic link changed from %s to %s.\n",
1196 name
, lnkbuf
, lbuf
);
1205 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1206 fprintf(stderr
, "%s: no longer a special file\n",
1213 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1215 "%s: device changed from %d,%d to %d,%d.\n",
1217 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1218 (int)curfile
.dip
->di_rdev
& 0xff,
1219 ((int)sb
.st_rdev
>> 8) & 0xff,
1220 (int)sb
.st_rdev
& 0xff);
1227 #if COMPARE_ONTHEFLY
1228 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1229 panic("Can't open %s: %s\n", name
, strerror(errno
));
1235 getfile(xtrcmpfile
, xtrcmpskip
);
1238 if (read(ifile
, &c
, 1) != 0) {
1239 fprintf(stderr
, "%s: size has changed.\n",
1249 if (tmpfile
== NULL
) {
1250 /* argument to mktemp() must not be in RO space: */
1251 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1252 tmpfile
= mktemp(&tmpfilename
[0]);
1254 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1255 panic("cannot delete tmp file %s: %s\n",
1256 tmpfile
, strerror(errno
));
1258 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1259 panic("cannot create file temp file %s: %s\n",
1260 name
, strerror(errno
));
1262 getfile(xtrfile
, xtrskip
);
1263 (void) close(ofile
);
1264 #ifdef COMPARE_FAIL_KEEP_FILE
1265 if (cmpfiles(tmpfile
, name
, &sb
))
1268 cmpfiles(tmpfile
, name
, &sb
);
1271 #endif /* COMPARE_ONTHEFLY */
1278 static void (*readtape_func
)(char *) = readtape_set
;
1281 * Read TP_BSIZE blocks from the input.
1282 * Handle read errors, and end of media.
1283 * Decompress compressed blocks.
1288 (*readtape_func
)(buf
); /* call the actual processing routine */
1292 * Set function pointer for readtape() routine. zflag and magtapein must
1293 * be correctly set before the first call to readtape().
1296 readtape_set(char *buf
)
1299 readtape_func
= readtape_uncompr
;
1302 readtape_func
= readtape_comprtape
;
1304 readtape_func
= readtape_comprfile
;
1309 #endif /* HAVE_ZLIB */
1312 * This is the original readtape(), it's used for reading uncompressed input.
1313 * Read TP_BSIZE blocks from the input.
1314 * Handle read errors, and end of media.
1318 readtape_uncompr(char *buf
)
1323 ssize_t rd
, newvol
, i
;
1324 int cnt
, seek_failed
;
1326 if (blkcnt
< numtrec
) {
1327 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1333 for (i
= 0; i
< ntrec
; i
++)
1334 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1337 cnt
= ntrec
* TP_BSIZE
;
1342 i
= rmtread(&tapebuf
[rd
], cnt
);
1345 i
= read(mt
, &tapebuf
[rd
], cnt
);
1348 * Check for mid-tape short read error.
1349 * If found, skip rest of buffer and start with the next.
1351 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1352 Dprintf(stdout
, "mid-media short read error.\n");
1356 * Handle partial block read.
1358 if (pipein
&& i
== 0 && rd
> 0)
1360 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1369 * Short read. Process the blocks read.
1371 if (i
% TP_BSIZE
!= 0)
1373 "partial block read: %ld should be %ld\n",
1374 (long)i
, ntrec
* TP_BSIZE
);
1375 numtrec
= i
/ TP_BSIZE
;
1379 * Handle read error.
1382 fprintf(stderr
, "Tape read error while ");
1383 switch (curfile
.action
) {
1385 fprintf(stderr
, "trying to set up tape\n");
1388 fprintf(stderr
, "trying to resynchronize\n");
1391 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1394 fprintf(stderr
, "skipping over inode %lu\n",
1395 (unsigned long)curfile
.ino
);
1398 if (!yflag
&& !reply("continue"))
1400 i
= ntrec
* TP_BSIZE
;
1401 memset(tapebuf
, 0, (size_t)i
);
1404 seek_failed
= (rmtseek(i
, 1) < 0);
1407 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1410 warn("continuation failed");
1411 if (!yflag
&& !reply("assume end-of-tape and continue"))
1417 * Handle end of tape.
1420 Vprintf(stdout
, "End-of-tape encountered\n");
1429 if (rd
% TP_BSIZE
!= 0)
1430 panic("partial block read: %d should be %d\n",
1431 rd
, ntrec
* TP_BSIZE
);
1433 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1436 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1444 * Read a compressed format block from a file or pipe and uncompress it.
1445 * Attempt to handle read errors, and end of file.
1448 readtape_comprfile(char *buf
)
1450 long rl
, size
, i
, ret
;
1452 struct tapebuf
*tpb
;
1454 if (blkcnt
< numtrec
) {
1455 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1460 /* need to read the next block */
1462 for (i
= 0; i
< ntrec
; i
++)
1463 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1465 tpb
= (struct tapebuf
*) tapebuf
;
1467 /* read the block prefix */
1468 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1474 if (size
> bufsize
) {
1475 /* something's wrong */
1476 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1479 tpb
->length
= bufsize
;
1481 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1485 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1486 if (tbufptr
== NULL
) {
1487 msg_read_error("File decompression error while");
1488 if (!yflag
&& !reply("continue"))
1490 memset(tapebuf
, 0, bufsize
);
1495 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1501 /* Errors while reading from a file or pipe are catastrophic. Since
1502 * there are no block boundaries, it's impossible to bypass the
1503 * block in error and find the start of the next block.
1506 /* It's possible to have multiple input files using -M
1507 * and -f file1,file2...
1509 Vprintf(stdout
, "End-of-File encountered\n");
1519 msg_read_error("Read error while");
1520 /* if (!yflag && !reply("continue")) */
1525 * Read compressed data from a tape and uncompress it.
1526 * Handle read errors, and end of media.
1527 * Since a tape consists of separate physical blocks, we try
1528 * to recover from errors by repositioning the tape to the next
1532 readtape_comprtape(char *buf
)
1536 struct tapebuf
*tpb
;
1539 if (blkcnt
< numtrec
) {
1540 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1545 /* need to read the next block */
1547 for (i
= 0; i
< ntrec
; i
++)
1548 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1550 tpb
= (struct tapebuf
*) tapebuf
;
1552 /* read the block */
1553 size
= bufsize
+ PREFIXSIZE
;
1554 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1558 tbufptr
= decompress_tapebuf(tpb
, rl
);
1559 if (tbufptr
== NULL
) {
1560 msg_read_error("Tape decompression error while");
1561 if (!yflag
&& !reply("continue"))
1563 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1569 /* Handle errors: EOT switches to the next volume, other errors
1570 * attempt to position the tape to the next block.
1573 Vprintf(stdout
, "End-of-tape encountered\n");
1582 msg_read_error("Tape read error while");
1583 if (!yflag
&& !reply("continue"))
1585 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1590 rl
= rmtioctl(MTFSR
, 1);
1596 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1600 warn("continuation failed");
1601 if (!yflag
&& !reply("assume end-of-tape and continue"))
1603 ret
= 0; /* end of tape */
1609 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1615 * Decompress a struct tapebuf into a buffer. readsize is the size read
1616 * from the tape/file and is used for error messages. Returns a pointer
1617 * to the location of the uncompressed buffer or NULL on errors.
1618 * Adjust numtrec and complain for a short block.
1621 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1623 /* If zflag is on, all blocks have a struct tapebuf prefix */
1624 /* zflag gets set in setup() from the dump header */
1625 int cresult
, blocklen
;
1626 unsigned long worklen
;
1627 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1629 /* build a length error message */
1630 blocklen
= tpbin
->length
;
1631 if (readsize
< blocklen
+ PREFIXSIZE
)
1632 lengtherr
= "short";
1634 if (readsize
> blocklen
+ PREFIXSIZE
)
1639 if (tpbin
->compressed
) {
1640 /* uncompress whatever we read, if it fails, complain later */
1641 cresult
= uncompress(comprbuf
, &worklen
, tpbin
->buf
, blocklen
);
1645 output
= tpbin
->buf
;
1650 numtrec
= worklen
/ TP_BSIZE
;
1651 if (worklen
% TP_BSIZE
!= 0)
1652 reason
= "length mismatch";
1655 reason
= "not enough memory";
1658 reason
= "buffer too small";
1661 reason
= "data error";
1668 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1669 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1670 fprintf(stderr
, "decompression error, block %ld: %s\n",
1671 tpblksread
+1, reason
);
1672 if (cresult
!= Z_OK
) output
= NULL
;
1678 * Print an error message for a read error.
1679 * This was exteracted from the original readtape().
1682 msg_read_error(char *m
)
1684 switch (curfile
.action
) {
1686 fprintf(stderr
, "%s trying to set up tape\n", m
);
1689 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1692 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1695 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1696 (unsigned long)curfile
.ino
);
1700 #endif /* HAVE_ZLIB */
1703 * Read the first block and set the blocksize from its length. Test
1704 * if the block looks like a compressed dump tape. setup() will make
1705 * the final determination by checking the compressed flag if gethead()
1706 * finds a valid header. The test here is necessary to offset the buffer
1707 * by the size of the compressed prefix. zflag is set here so that
1708 * readtape_set can set the correct function pointer for readtape().
1709 * Note that the first block of each tape/file will not be compressed.
1712 findtapeblksize(void)
1716 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1717 struct s_spcl
*spclpt
= (struct s_spcl
*) tpb
->buf
;
1719 for (i
= 0; i
< ntrec
; i
++)
1720 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1724 * For a pipe or file, read in the first record. For a tape, read
1727 if (magtapein
== 1) /* fixed blocksize tape, not compressed */
1728 len
= ntrec
* TP_BSIZE
;
1729 else if (magtapein
== 2)/* variable blocksize tape */
1730 len
= bufsize
+ PREFIXSIZE
;
1731 else /* not mag tape */
1734 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1735 errx(1, "Tape read error on first record");
1738 * If the input is from a file or a pipe, we read TP_BSIZE
1739 * bytes looking for a compressed dump header, we then
1740 * need to read in the rest of the record, as determined by
1741 * tpb->length or bufsize. The first block of the dump is
1742 * guaranteed to not be compressed so we look at the header.
1745 if (tpb
->length
% TP_BSIZE
== 0
1746 && tpb
->length
<= bufsize
1747 && tpb
->compressed
== 0
1748 && spclpt
->c_type
== TS_TAPE
1749 && spclpt
->c_flags
& DR_COMPRESSED
) {
1750 /* Looks like it's a compressed dump block prefix, */
1751 /* read in the rest of the block based on tpb->length. */
1752 len
= tpb
->length
- TP_BSIZE
+ PREFIXSIZE
;
1753 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) <= 0
1755 errx(1,"Error reading dump file header");
1757 numtrec
= ntrec
= tpb
->length
/ TP_BSIZE
;
1761 /* read in the rest of the block based on bufsize */
1762 len
= bufsize
- TP_BSIZE
;
1763 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
1764 || (i
!= len
&& i
% TP_BSIZE
!= 0))
1765 errx(1,"Error reading dump file header");
1769 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1774 * If the input is a variable block size tape, we tried to
1775 * read PREFIXSIZE + ntrec * TP_BSIZE bytes.
1776 * If it's not a compressed dump tape or the value of ntrec is
1777 * too large, we have read less than * what we asked for;
1778 * adjust the value of ntrec and test for * a compressed dump
1782 if (i
% TP_BSIZE
!= 0) {
1783 if (i
% TP_BSIZE
== PREFIXSIZE
1784 && tpb
->compressed
== 0
1785 && spclpt
->c_type
== TS_TAPE
1786 && spclpt
->c_flags
& DR_COMPRESSED
) {
1790 if (tpb
->length
> bufsize
)
1791 errx(1, "Tape blocksize is too large, use "
1792 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1795 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1798 ntrec
= i
/ TP_BSIZE
;
1800 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1804 * Read a block of data handling all of the messy details.
1806 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
1814 i
= rmtread(buf
, size
);
1817 i
= read(fd
, buf
, size
);
1820 break; /* EOD or error */
1823 break; /* block at a time for mt */
1826 *lengthread
= len
- size
;
1845 * Read the next block from the tape.
1846 * Check to see if it is one of several vintage headers.
1847 * If it is an old style header, convert it to a new style header.
1848 * If it is not any valid header, return an error.
1851 gethead(struct s_spcl
*buf
)
1859 char dummy
[TP_BSIZE
];
1866 u_int16_t c_inumber
;
1871 u_int16_t odi_nlink
;
1887 readtape((char *)buf
);
1888 if (buf
->c_magic
!= NFS_MAGIC
) {
1889 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1892 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1896 if (checksum((int *)buf
) == FAIL
)
1899 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1902 readtape((char *)(&u_ospcl
.s_ospcl
));
1903 memset((char *)buf
, 0, (long)TP_BSIZE
);
1904 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1905 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1906 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1907 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1908 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1909 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1910 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1911 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1912 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1913 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1914 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1915 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1916 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1917 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1919 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1920 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1921 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1922 #else /* __linux__ */
1923 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1924 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1925 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1926 #endif /* __linux__ */
1927 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1928 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1929 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1930 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1932 buf
->c_magic
= NFS_MAGIC
;
1935 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1936 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1937 qcvt
.qval
= buf
->c_dinode
.di_size
;
1938 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1939 printf("Note: Doing Quad swapping\n");
1944 qcvt
.qval
= buf
->c_dinode
.di_size
;
1946 qcvt
.val
[1] = qcvt
.val
[0];
1948 buf
->c_dinode
.di_size
= qcvt
.qval
;
1952 switch (buf
->c_type
) {
1957 * Have to patch up missing information in bit map headers
1960 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1961 if (buf
->c_count
> TP_NINDIR
)
1964 for (i
= 0; i
< buf
->c_count
; i
++)
1969 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1981 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1985 * If we are restoring a filesystem with old format inodes,
1986 * copy the uid/gid to the new location.
1989 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1990 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1998 * Check that a header is where it belongs and predict the next header
2001 accthdr(struct s_spcl
*header
)
2003 static dump_ino_t previno
= 0x7fffffff;
2004 static int prevtype
;
2005 static long predict
;
2008 if (header
->c_type
== TS_TAPE
) {
2009 fprintf(stderr
, "Volume header (%s inode format) ",
2010 oldinofmt
? "old" : "new");
2011 if (header
->c_firstrec
)
2012 fprintf(stderr
, "begins with record %d",
2013 header
->c_firstrec
);
2014 fprintf(stderr
, "\n");
2015 previno
= 0x7fffffff;
2018 if (previno
== 0x7fffffff)
2022 fprintf(stderr
, "Dumped inodes map header");
2025 fprintf(stderr
, "Used inodes map header");
2028 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2031 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2034 fprintf(stderr
, "End of tape header");
2037 if (predict
!= blksread
- 1)
2038 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2039 predict
, blksread
- 1);
2040 fprintf(stderr
, "\n");
2043 if (header
->c_type
!= TS_END
)
2044 for (i
= 0; i
< header
->c_count
; i
++)
2045 if (readmapflag
|| header
->c_addr
[i
] != 0)
2049 prevtype
= header
->c_type
;
2050 previno
= header
->c_inumber
;
2054 * Find an inode header.
2055 * Complain if had to skip, and complain is set.
2058 findinode(struct s_spcl
*header
)
2060 static long skipcnt
= 0;
2064 curfile
.name
= "<name unknown>";
2065 curfile
.action
= UNKNOWN
;
2069 if (header
->c_magic
!= NFS_MAGIC
) {
2071 while (gethead(header
) == FAIL
||
2072 header
->c_date
!= dumpdate
)
2075 switch (header
->c_type
) {
2079 * Skip up to the beginning of the next record
2081 for (i
= 0; i
< header
->c_count
; i
++)
2082 if (header
->c_addr
[i
])
2084 while (gethead(header
) == FAIL
||
2085 header
->c_date
!= dumpdate
)
2090 curfile
.dip
= &header
->c_dinode
;
2091 curfile
.ino
= header
->c_inumber
;
2095 curfile
.ino
= maxino
;
2099 curfile
.name
= "<file removal list>";
2103 curfile
.name
= "<file dump list>";
2107 panic("unexpected tape header\n");
2111 panic("unknown tape header type %d\n", spcl
.c_type
);
2115 } while (header
->c_type
== TS_ADDR
);
2120 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2130 j
= sizeof(union u_spcl
) / sizeof(int);
2137 /* What happens if we want to read restore tapes
2138 for a 16bit int machine??? */
2144 if (i
!= CHECKSUM
) {
2145 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2146 (unsigned long)curfile
.ino
, curfile
.name
);
2156 #include <varargs.h>
2161 msg(const char *fmt
, ...)
2174 (void)vfprintf(stderr
, fmt
, ap
);
2177 #endif /* RRESTORE */
2180 swab16(u_char
*sp
, int n
)
2185 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2192 swab32(u_char
*sp
, int n
)
2197 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2198 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2205 swab64(u_char
*sp
, int n
)
2210 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2211 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2212 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2213 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2220 swabst(u_char
*cp
, u_char
*sp
)
2226 case '0': case '1': case '2': case '3': case '4':
2227 case '5': case '6': case '7': case '8': case '9':
2228 n
= (n
* 10) + (*cp
++ - '0');
2231 case 's': case 'w': case 'h':
2249 default: /* Any other character, like 'b' counts as byte. */
2263 swabst((u_char
*)"i", (u_char
*)&x
);
2271 swabst((u_char
*)"l", (u_char
*)&x
);
2278 * get the current position of the tape
2281 GetTapePos(long *pos
)
2286 if (ioctl(mt
, MTIOCPOS
, pos
) == -1) {
2288 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %ld)\n",
2289 (unsigned long)getpid(), err
, *pos
);
2295 typedef struct mt_pos
{
2298 } MTPosRec
, *MTPosPtr
;
2301 * go to specified position on tape
2304 GotoTapePos(long pos
)
2311 if (ioctl(mt
, MTIOCTOP
, &buf
) == -1) {
2313 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %ld)\n",
2314 (unsigned long)getpid(), err
, pos
);
2321 * read next data from tape to re-sync
2324 ReReadFromTape(void)
2328 if (gethead(&spcl
) == FAIL
) {
2330 fprintf(stdout
, "DEBUG 1 gethead failed\n");
2338 RequestVol(long tnum
)
2343 #endif /* USE_QFA */