]>
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.27 2001/03/19 13:22:49 stelian Exp $";
54 #include <compaterr.h>
61 #include <sys/param.h>
69 #include <linux/ext2_fs.h>
70 #include <ext2fs/ext2fs.h>
71 #include <bsdcompat.h>
73 #include <ufs/ufs/dinode.h>
74 #endif /* __linux__ */
75 #include <protocols/dumprestore.h>
79 #endif /* HAVE_ZLIB */
83 #include "pathnames.h"
85 static long fssize
= MAXBSIZE
;
87 static int pipein
= 0;
88 static int magtapein
= 0; /* input is from magtape */
89 static char magtape
[MAXPATHLEN
];
90 static char magtapeprefix
[MAXPATHLEN
];
93 static char *tapebuf
; /* input buffer for read */
94 static int bufsize
; /* buffer size without prefix */
95 static char *tbufptr
= NULL
; /* active tape buffer */
97 static char *comprbuf
; /* uncompress work buf */
98 static size_t comprlen
; /* size including prefix */
100 static union u_spcl endoftapemark
;
101 static long blksread
; /* blocks read since last header */
102 static long tpblksread
= 0; /* TP_BSIZE blocks read */
103 static long tapesread
;
104 static sigjmp_buf restart
;
105 static int gettingfile
= 0; /* restart has a valid frame */
106 static char *host
= NULL
;
110 static char lnkbuf
[MAXPATHLEN
+ 1];
113 int oldinofmt
; /* old inode format conversion required */
114 int Bcvt
; /* Swap Bytes (for CCI or sun) */
115 static int Qcvt
; /* Swap quads (for sun) */
117 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
119 static void accthdr
__P((struct s_spcl
*));
120 static int checksum
__P((int *));
121 static void findinode
__P((struct s_spcl
*));
122 static void findtapeblksize
__P((void));
123 static int gethead
__P((struct s_spcl
*));
124 static void readtape
__P((char *));
125 static void setdumpnum
__P((void));
126 static u_int swabi
__P((u_int
));
128 static u_long swabl
__P((u_long
));
130 static u_char
*swab64
__P((u_char
*, int));
131 static u_char
*swab32
__P((u_char
*, int));
132 static u_char
*swab16
__P((u_char
*, int));
133 static void terminateinput
__P((void));
134 static void xtrfile
__P((char *, size_t));
135 static void xtrlnkfile
__P((char *, size_t));
136 static void xtrlnkskip
__P((char *, size_t));
137 static void xtrmap
__P((char *, size_t));
138 static void xtrmapskip
__P((char *, size_t));
139 static void xtrskip
__P((char *, size_t));
142 static void newcomprbuf
__P((long));
143 static void readtape_set
__P((char *));
144 static void readtape_uncompr
__P((char *));
145 static void readtape_comprfile
__P((char *));
146 static void readtape_comprtape
__P((char *));
147 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
148 static void msg_read_error
__P((char *));
150 static int read_a_block
__P((int, void *, size_t, long *));
151 #define PREFIXSIZE sizeof(struct tapebuf)
153 #define COMPARE_ONTHEFLY 1
156 static int ifile
; /* input file for compare */
157 static int cmperror
; /* compare error */
158 static void xtrcmpfile
__P((char *, size_t));
159 static void xtrcmpskip
__P((char *, size_t));
162 static int readmapflag
;
165 * Set up an input source. This is called from main.c before setup() is.
168 setinput(char *source
)
174 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
178 if (strchr(source
, ':')) {
180 source
= strchr(host
, ':');
182 if (rmthost(host
) == 0)
186 if (strcmp(source
, "-") == 0) {
188 * Since input is coming from a pipe we must establish
189 * our own connection to the terminal.
191 terminal
= fopen(_PATH_TTY
, "r");
192 if (terminal
== NULL
) {
193 warn("cannot open %s", _PATH_TTY
);
194 terminal
= fopen(_PATH_DEVNULL
, "r");
195 if (terminal
== NULL
)
196 err(1, "cannot open %s", _PATH_DEVNULL
);
200 setuid(getuid()); /* no longer need or want root privileges */
202 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
203 magtapeprefix
[MAXPATHLEN
-1] = '\0';
204 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
207 strncpy(magtape
, source
, MAXPATHLEN
);
208 magtape
[MAXPATHLEN
- 1] = '\0';
212 newtapebuf(long size
)
214 static int tapebufsize
= -1;
217 bufsize
= ntrec
* TP_BSIZE
;
218 if (size
<= tapebufsize
)
222 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
224 errx(1, "Cannot allocate space for tape buffer");
230 newcomprbuf(long size
)
232 if (size
<= comprlen
)
234 comprlen
= size
+ sizeof(struct tapebuf
);
235 if (comprbuf
!= NULL
)
237 comprbuf
= malloc(comprlen
);
238 if (comprbuf
== NULL
)
239 errx(1, "Cannot allocate space for decompress buffer");
241 #endif /* HAVE_ZLIB */
244 * Verify that the tape drive can be accessed and
245 * that it actually is a dump tape.
252 struct mtget mt_stat
;
254 Vprintf(stdout
, "Verify tape and initialize maps\n");
257 mt
= rmtopen(magtape
, 0);
263 mt
= open(magtape
, O_RDONLY
, 0);
265 err(1, "%s", magtape
);
268 /* need to know if input is really from a tape */
271 magtapein
= rmtioctl(MTNOP
, 1) != -1;
274 magtapein
= ioctl(mt
, MTIOCGET
, (char *) &mt_stat
) == 0;
277 Vprintf(stdout
,"Input is from %s\n", magtapein
? "tape": "file/pipe");
281 if (gethead(&spcl
) == FAIL
) {
282 blkcnt
--; /* push back this block */
286 if (gethead(&spcl
) == FAIL
)
287 errx(1, "Tape is not a dump tape");
288 fprintf(stderr
, "Converting to new file system format.\n");
292 fprintf(stderr
, "Dump tape is compressed.\n");
294 newcomprbuf(bufsize
);
296 errx(1,"This restore version doesn't support decompression");
297 #endif /* HAVE_ZLIB */
300 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
301 endoftapemark
.s_spcl
.c_type
= TS_END
;
302 ip
= (int *)&endoftapemark
;
303 j
= sizeof(union u_spcl
) / sizeof(int);
308 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
310 if (vflag
|| command
== 't' || command
== 'C')
312 if (filesys
[0] == '\0') {
314 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
315 filesys
[NAMELEN
- 1] = '\0';
316 dirptr
= strstr(filesys
, " (dir");
320 dumptime
= spcl
.c_ddate
;
321 dumpdate
= spcl
.c_date
;
322 if (stat(".", &stbuf
) < 0)
323 err(1, "cannot stat .");
324 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
326 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
327 fssize
= stbuf
.st_blksize
;
328 if (((fssize
- 1) & fssize
) != 0)
329 errx(1, "bad block size %ld", fssize
);
330 if (spcl
.c_volume
!= 1)
331 errx(1, "Tape is not volume 1 of the dump");
332 if (gethead(&spcl
) == FAIL
) {
333 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
334 panic("no header after volume mark!\n");
337 if (spcl
.c_type
!= TS_CLRI
)
338 errx(1, "Cannot find file removal list");
339 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
340 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
341 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
343 errx(1, "no memory for active inode map");
345 curfile
.action
= USING
;
346 getfile(xtrmap
, xtrmapskip
);
347 if (spcl
.c_type
!= TS_BITS
)
348 errx(1, "Cannot find file dump list");
349 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
350 if (map
== (char *)NULL
)
351 errx(1, "no memory for file dump list");
353 curfile
.action
= USING
;
354 getfile(xtrmap
, xtrmapskip
);
356 * If there may be whiteout entries on the tape, pretend that the
357 * whiteout inode exists, so that the whiteout entries can be
361 SETINO(WINO
, dumpmap
);
365 * Prompt user to load a new dump volume.
366 * "Nextvol" is the next suggested volume to use.
367 * This suggested volume is enforced when doing full
368 * or incremental restores, but can be overridden by
369 * the user when only extracting a subset of the files.
374 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
375 union u_spcl tmpspcl
;
376 # define tmpbuf tmpspcl.s_spcl
386 panic("Changing volumes on pipe input?\n");
394 exit(1); /* pipes do not get a second chance */
395 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
402 while (newvol
<= 0) {
403 if (tapesread
== 0) {
404 fprintf(stderr
, "%s%s%s%s%s",
405 "You have not read any tapes yet.\n",
406 "Unless you know which volume your",
407 " file(s) are on you should start\n",
408 "with the last volume and work",
409 " towards the first.\n");
411 fprintf(stderr
, "You have read volumes");
413 for (i
= 1; i
< 32; i
++)
414 if (tapesread
& (1 << i
)) {
415 fprintf(stderr
, "%s%ld", buf
, (long)i
);
418 fprintf(stderr
, "\n");
421 fprintf(stderr
, "Specify next volume #: ");
422 (void) fflush(stderr
);
423 (void) fgets(buf
, TP_BSIZE
, terminal
);
424 } while (!feof(terminal
) && buf
[0] == '\n');
430 "Volume numbers are positive numerics\n");
433 if (newvol
== volno
) {
434 tapesread
|= 1 << volno
;
439 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
440 magtape
[MAXPATHLEN
- 1] = '\0';
442 if (!Mflag
|| haderror
) {
444 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
445 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
446 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
447 (void) fflush(stderr
);
448 (void) fgets(buf
, TP_BSIZE
, terminal
);
451 if (!strcmp(buf
, "none\n")) {
455 if (buf
[0] != '\n') {
456 (void) strcpy(magtape
, buf
);
457 magtape
[strlen(magtape
) - 1] = '\0';
462 mt
= rmtopen(magtape
, 0);
465 mt
= open(magtape
, O_RDONLY
, 0);
468 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
) {
492 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
493 ctime4(&tmpbuf
.c_date
));
494 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
496 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
497 ctime(&tmpbuf
.c_date
));
498 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
504 tapesread
|= 1 << volno
;
507 * If continuing from the previous volume, skip over any
508 * blocks read already at the end of the previous volume.
510 * If coming to this volume at random, skip to the beginning
511 * of the next record.
513 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
514 tpblksread
, (long)tmpbuf
.c_firstrec
);
515 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
517 tpblksread
= tmpbuf
.c_firstrec
;
518 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
520 } else if (tmpbuf
.c_firstrec
> 0 &&
521 tmpbuf
.c_firstrec
< tpblksread
- 1) {
523 * -1 since we've read the volume header
525 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
526 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
527 (long)i
, i
> 1 ? "s" : "");
532 if (curfile
.action
== USING
) {
534 panic("active file into volume 1\n");
538 * Skip up to the beginning of the next record
540 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
541 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
543 (void) gethead(&spcl
);
547 siglongjmp(restart
, 1);
552 * Handle unexpected EOF.
558 if (gettingfile
&& curfile
.action
== USING
) {
559 printf("Warning: %s %s\n",
560 "End-of-input encountered while extracting", curfile
.name
);
562 curfile
.name
= "<name unknown>";
563 curfile
.action
= UNKNOWN
;
565 curfile
.ino
= maxino
;
568 siglongjmp(restart
, 1);
573 * handle multiple dumps per tape by skipping forward to the
581 if (dumpnum
== 1 || volno
!= 1)
584 errx(1, "Cannot have multiple dumps on pipe input");
586 tcom
.mt_count
= dumpnum
- 1;
589 rmtioctl(MTFSF
, dumpnum
- 1);
592 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
600 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
601 fprintf(stdout
, "Dumped from: %s",
602 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
604 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
605 fprintf(stdout
, "Dumped from: %s",
606 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
608 if (spcl
.c_host
[0] == '\0')
610 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
611 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
612 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
616 extractfile(char *name
)
620 struct timeval timep
[2];
624 curfile
.action
= USING
;
626 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
627 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
628 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
629 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
630 #else /* __linux__ */
631 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
632 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
633 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
634 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
635 #endif /* __linux__ */
636 mode
= curfile
.dip
->di_mode
;
637 flags
= curfile
.dip
->di_flags
;
638 switch (mode
& IFMT
) {
641 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
646 Vprintf(stdout
, "skipped socket %s\n", name
);
652 ep
= lookupname(name
);
653 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
654 panic("unextracted directory %s\n", name
);
658 Vprintf(stdout
, "extract file %s\n", name
);
659 return (genliteraldir(name
, curfile
.ino
));
664 uid_t luid
= curfile
.dip
->di_uid
;
665 gid_t lgid
= curfile
.dip
->di_gid
;
669 getfile(xtrlnkfile
, xtrlnkskip
);
672 "%s: zero length symbolic link (ignored)\n", name
);
675 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
678 (void) lchown(name
, luid
, lgid
);
684 Vprintf(stdout
, "extract fifo %s\n", name
);
691 if (mkfifo(name
, mode
) < 0) {
692 warn("%s: cannot create fifo", name
);
696 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
697 (void) chmod(name
, mode
);
700 (void) fsetflags(name
, flags
);
702 (void) chflags(name
, flags
);
710 Vprintf(stdout
, "extract special file %s\n", name
);
717 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
718 warn("%s: cannot create special file", name
);
722 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
723 (void) chmod(name
, mode
);
727 warn("%s: fsetflags called on a special file", name
);
728 (void) fsetflags(name
, flags
);
731 (void) chflags(name
, flags
);
738 Vprintf(stdout
, "extract file %s\n", name
);
745 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
747 warn("%s: cannot create file", name
);
751 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
752 (void) fchmod(ofile
, mode
);
755 (void) setflags(ofile
, flags
);
757 (void) fchflags(ofile
, flags
);
759 getfile(xtrfile
, xtrskip
);
768 * skip over bit maps on the tape
774 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
779 * skip over a file on the tape
785 curfile
.action
= SKIP
;
786 getfile(xtrnull
, xtrnull
);
790 * Extract a file from the tape.
791 * When an allocated block is found it is passed to the fill function;
792 * when an unallocated block (hole) is found, a zeroed buffer is passed
793 * to the skip function.
796 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
799 volatile int curblk
= 0;
800 volatile quad_t size
= spcl
.c_dinode
.di_size
;
801 volatile int last_write_was_hole
= 0;
802 quad_t origsize
= size
;
803 static char clearedbuf
[MAXBSIZE
];
804 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
807 if (spcl
.c_type
== TS_END
)
808 panic("ran off end of tape\n");
809 if (spcl
.c_magic
!= NFS_MAGIC
)
810 panic("not at beginning of a file\n");
811 if (!gettingfile
&& setjmp(restart
) != 0)
815 for (i
= 0; i
< spcl
.c_count
; i
++) {
816 if (readmapflag
|| spcl
.c_addr
[i
]) {
817 readtape(&buf
[curblk
++][0]);
818 if (curblk
== fssize
/ TP_BSIZE
) {
819 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
820 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
822 last_write_was_hole
= 0;
826 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
828 (curblk
- 1) * TP_BSIZE
+ size
));
831 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
833 last_write_was_hole
= 1;
835 if ((size
-= TP_BSIZE
) <= 0) {
836 for (i
++; i
< spcl
.c_count
; i
++)
837 if (readmapflag
|| spcl
.c_addr
[i
])
842 if (gethead(&spcl
) == GOOD
&& size
> 0) {
843 if (spcl
.c_type
== TS_ADDR
)
846 "Missing address (header) block for %s at %ld blocks\n",
847 curfile
.name
, (long)blksread
);
850 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
851 last_write_was_hole
= 0;
853 if (last_write_was_hole
) {
854 ftruncate(ofile
, origsize
);
861 * Write out the next block of a file.
864 xtrfile(char *buf
, size_t size
)
869 if (write(ofile
, buf
, (int) size
) == -1)
870 err(1, "write error extracting inode %lu, name %s\nwrite",
871 (unsigned long)curfile
.ino
, curfile
.name
);
875 * Skip over a hole in a file.
879 xtrskip(char *buf
, size_t size
)
882 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
883 err(1, "seek error extracting inode %lu, name %s\nlseek",
884 (unsigned long)curfile
.ino
, curfile
.name
);
888 * Collect the next block of a symbolic link.
891 xtrlnkfile(char *buf
, size_t size
)
895 if (pathlen
> MAXPATHLEN
)
896 errx(1, "symbolic link name: %s->%s%s; too long %d",
897 curfile
.name
, lnkbuf
, buf
, pathlen
);
898 (void) strcat(lnkbuf
, buf
);
902 * Skip over a hole in a symbolic link (should never happen).
906 xtrlnkskip(char *buf
, size_t size
)
909 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
913 * Collect the next block of a bit map.
916 xtrmap(char *buf
, size_t size
)
919 memmove(map
, buf
, size
);
924 * Skip over a hole in a bit map (should never happen).
928 xtrmapskip(char *buf
, size_t size
)
931 panic("hole in map\n");
936 * Noop, when an extraction function is not needed.
940 xtrnull(char *buf
, size_t size
)
948 * Compare the next block of a file.
951 xtrcmpfile(char *buf
, size_t size
)
953 static char cmpbuf
[MAXBSIZE
];
958 if (read(ifile
, cmpbuf
, size
) != size
) {
959 fprintf(stderr
, "%s: size has changed.\n",
965 if (memcmp(buf
, cmpbuf
, size
) != 0) {
966 fprintf(stderr
, "%s: tape and disk copies are different\n",
974 * Skip over a hole in a file.
977 xtrcmpskip(char *buf
, size_t size
)
979 static char cmpbuf
[MAXBSIZE
];
985 if (read(ifile
, cmpbuf
, size
) != size
) {
986 fprintf(stderr
, "%s: size has changed.\n",
992 for (i
= 0; i
< size
; ++i
)
993 if (cmpbuf
[i
] != '\0') {
994 fprintf(stderr
, "%s: tape and disk copies are different\n",
1000 #endif /* COMPARE_ONTHEFLY */
1002 #if !COMPARE_ONTHEFLY
1004 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
1006 static char buf_tape
[BUFSIZ
];
1007 static char buf_disk
[BUFSIZ
];
1012 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1013 close(fd_tape
), close(fd_disk
);
1014 panic("do_cmpfiles: unexpected EOF[1]");
1016 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1017 close(fd_tape
), close(fd_disk
);
1018 panic("do_cmpfiles: unexpected EOF[2]");
1020 if (n_tape
!= n_disk
) {
1021 close(fd_tape
), close(fd_disk
);
1022 panic("do_cmpfiles: sizes different!");
1024 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1030 /* for debugging compare problems */
1031 #undef COMPARE_FAIL_KEEP_FILE
1034 #ifdef COMPARE_FAIL_KEEP_FILE
1035 /* return true if tapefile should be unlinked after compare */
1040 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
1042 struct stat sbuf_tape
;
1043 int fd_tape
, fd_disk
;
1045 if (stat(tapefile
, &sbuf_tape
) != 0) {
1046 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1051 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1053 "%s: size changed from %ld to %ld.\n",
1054 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
1056 #ifdef COMPARE_FAIL_KEEP_FILE
1063 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
1064 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1067 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
1069 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1073 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1074 fprintf(stderr
, "%s: tape and disk copies are different\n",
1079 #ifdef COMPARE_FAIL_KEEP_FILE
1080 /* rename the file to live in /tmp */
1081 /* rename `tapefile' to /tmp/<basename of diskfile> */
1083 char *p
= strrchr(diskfile
, '/');
1084 char newname
[MAXPATHLEN
];
1086 panic("can't find / in %s\n", diskfile
);
1088 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1089 if (rename(tapefile
, newname
)) {
1090 panic("rename from %s to %s failed: %s\n",
1094 fprintf(stderr
, "*** %s saved to %s\n",
1099 /* don't unlink the file (it's not there anymore */
1108 #ifdef COMPARE_FAIL_KEEP_FILE
1112 #endif /* !COMPARE_ONTHEFLY */
1114 #if !COMPARE_ONTHEFLY
1115 static char tmpfilename
[MAXPATHLEN
];
1119 comparefile(char *name
)
1124 #if !COMPARE_ONTHEFLY
1125 static char *tmpfile
= NULL
;
1129 if ((r
= lstat(name
, &sb
)) != 0) {
1130 warn("%s: does not exist (%d)", name
, r
);
1136 curfile
.name
= name
;
1137 curfile
.action
= USING
;
1138 mode
= curfile
.dip
->di_mode
;
1140 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1141 (long)sb
.st_size
, mode
);
1143 if (sb
.st_mode
!= mode
) {
1144 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1145 name
, mode
& 07777, sb
.st_mode
& 07777);
1148 switch (mode
& IFMT
) {
1162 char lbuf
[MAXPATHLEN
+ 1];
1165 if (!(sb
.st_mode
& S_IFLNK
)) {
1166 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1173 getfile(xtrlnkfile
, xtrlnkskip
);
1176 "%s: zero length symbolic link (ignored)\n",
1181 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1182 panic("readlink of %s failed: %s", name
,
1187 if (strcmp(lbuf
, lnkbuf
) != 0) {
1189 "%s: symbolic link changed from %s to %s.\n",
1190 name
, lnkbuf
, lbuf
);
1199 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1200 fprintf(stderr
, "%s: no longer a special file\n",
1207 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1209 "%s: device changed from %d,%d to %d,%d.\n",
1211 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1212 (int)curfile
.dip
->di_rdev
& 0xff,
1213 ((int)sb
.st_rdev
>> 8) & 0xff,
1214 (int)sb
.st_rdev
& 0xff);
1221 #if COMPARE_ONTHEFLY
1222 if ((ifile
= open(name
, O_RDONLY
)) < 0) {
1223 panic("Can't open %s: %s\n", name
, strerror(errno
));
1229 getfile(xtrcmpfile
, xtrcmpskip
);
1232 if (read(ifile
, &c
, 1) != 0) {
1233 fprintf(stderr
, "%s: size has changed.\n",
1243 if (tmpfile
== NULL
) {
1244 /* argument to mktemp() must not be in RO space: */
1245 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1246 tmpfile
= mktemp(&tmpfilename
[0]);
1248 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1249 panic("cannot delete tmp file %s: %s\n",
1250 tmpfile
, strerror(errno
));
1252 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1253 panic("cannot create file temp file %s: %s\n",
1254 name
, strerror(errno
));
1256 getfile(xtrfile
, xtrskip
);
1257 (void) close(ofile
);
1258 #ifdef COMPARE_FAIL_KEEP_FILE
1259 if (cmpfiles(tmpfile
, name
, &sb
))
1262 cmpfiles(tmpfile
, name
, &sb
);
1265 #endif /* COMPARE_ONTHEFLY */
1272 static void (*readtape_func
)(char *) = readtape_set
;
1275 * Read TP_BSIZE blocks from the input.
1276 * Handle read errors, and end of media.
1277 * Decompress compressed blocks.
1282 (*readtape_func
)(buf
); /* call the actual processing routine */
1286 * Set function pointer for readtape() routine. zflag and magtapein must
1287 * be correctly set before the first call to readtape().
1290 readtape_set(char *buf
)
1293 readtape_func
= readtape_uncompr
;
1296 readtape_func
= readtape_comprtape
;
1298 readtape_func
= readtape_comprfile
;
1303 #endif /* HAVE_ZLIB */
1306 * This is the original readtape(), it's used for reading uncompressed input.
1307 * Read TP_BSIZE blocks from the input.
1308 * Handle read errors, and end of media.
1312 readtape_uncompr(char *buf
)
1317 ssize_t rd
, newvol
, i
;
1318 int cnt
, seek_failed
;
1320 if (blkcnt
< numtrec
) {
1321 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1327 for (i
= 0; i
< ntrec
; i
++)
1328 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1331 cnt
= ntrec
* TP_BSIZE
;
1338 i
= rmtread(&tapebuf
[rd
], cnt
);
1341 i
= read(mt
, &tapebuf
[rd
], cnt
);
1344 * Check for mid-tape short read error.
1345 * If found, skip rest of buffer and start with the next.
1347 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1348 Dprintf(stdout
, "mid-media short read error.\n");
1352 * Handle partial block read.
1354 if (pipein
&& i
== 0 && rd
> 0)
1356 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1365 * Short read. Process the blocks read.
1367 if (i
% TP_BSIZE
!= 0)
1369 "partial block read: %ld should be %ld\n",
1370 (long)i
, ntrec
* TP_BSIZE
);
1371 numtrec
= i
/ TP_BSIZE
;
1375 * Handle read error.
1378 fprintf(stderr
, "Tape read error while ");
1379 switch (curfile
.action
) {
1381 fprintf(stderr
, "trying to set up tape\n");
1384 fprintf(stderr
, "trying to resynchronize\n");
1387 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1390 fprintf(stderr
, "skipping over inode %lu\n",
1391 (unsigned long)curfile
.ino
);
1394 if (!yflag
&& !reply("continue"))
1396 i
= ntrec
* TP_BSIZE
;
1397 memset(tapebuf
, 0, (size_t)i
);
1400 seek_failed
= (rmtseek(i
, 1) < 0);
1403 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1406 warn("continuation failed");
1407 if (!yflag
&& !reply("assume end-of-tape and continue"))
1413 * Handle end of tape.
1416 Vprintf(stdout
, "End-of-tape encountered\n");
1425 if (rd
% TP_BSIZE
!= 0)
1426 panic("partial block read: %d should be %d\n",
1427 rd
, ntrec
* TP_BSIZE
);
1429 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1432 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1440 * Read a compressed format block from a file or pipe and uncompress it.
1441 * Attempt to handle read errors, and end of file.
1444 readtape_comprfile(char *buf
)
1446 long rl
, size
, i
, ret
;
1448 struct tapebuf
*tpb
;
1450 if (blkcnt
< numtrec
) {
1451 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1456 /* need to read the next block */
1458 for (i
= 0; i
< ntrec
; i
++)
1459 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1461 tpb
= (struct tapebuf
*) tapebuf
;
1463 /* read the block prefix */
1464 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
1470 if (size
> bufsize
) {
1471 /* something's wrong */
1472 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
1475 tpb
->length
= bufsize
;
1477 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
1481 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
1482 if (tbufptr
== NULL
) {
1483 msg_read_error("File decompression error while");
1484 if (!yflag
&& !reply("continue"))
1486 memset(tapebuf
, 0, bufsize
);
1491 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1497 /* Errors while reading from a file or pipe are catastrophic. Since
1498 * there are no block boundaries, it's impossible to bypass the
1499 * block in error and find the start of the next block.
1502 /* It's possible to have multiple input files using -M
1503 * and -f file1,file2...
1505 Vprintf(stdout
, "End-of-File encountered\n");
1515 msg_read_error("Read error while");
1516 /* if (!yflag && !reply("continue")) */
1521 * Read compressed data from a tape and uncompress it.
1522 * Handle read errors, and end of media.
1523 * Since a tape consists of separate physical blocks, we try
1524 * to recover from errors by repositioning the tape to the next
1528 readtape_comprtape(char *buf
)
1532 struct tapebuf
*tpb
;
1535 if (blkcnt
< numtrec
) {
1536 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1541 /* need to read the next block */
1543 for (i
= 0; i
< ntrec
; i
++)
1544 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1546 tpb
= (struct tapebuf
*) tapebuf
;
1548 /* read the block */
1549 size
= bufsize
+ PREFIXSIZE
;
1550 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
1554 tbufptr
= decompress_tapebuf(tpb
, rl
);
1555 if (tbufptr
== NULL
) {
1556 msg_read_error("Tape decompression error while");
1557 if (!yflag
&& !reply("continue"))
1559 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1565 /* Handle errors: EOT switches to the next volume, other errors
1566 * attempt to position the tape to the next block.
1569 Vprintf(stdout
, "End-of-tape encountered\n");
1578 msg_read_error("Tape read error while");
1579 if (!yflag
&& !reply("continue"))
1581 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
1586 rl
= rmtioctl(MTFSR
, 1);
1592 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
1596 warn("continuation failed");
1597 if (!yflag
&& !reply("assume end-of-tape and continue"))
1599 ret
= 0; /* end of tape */
1605 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1611 * Decompress a struct tapebuf into a buffer. readsize is the size read
1612 * from the tape/file and is used for error messages. Returns a pointer
1613 * to the location of the uncompressed buffer or NULL on errors.
1614 * Adjust numtrec and complain for a short block.
1617 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
1619 /* If zflag is on, all blocks have a struct tapebuf prefix */
1620 /* zflag gets set in setup() from the dump header */
1621 int cresult
, blocklen
;
1622 unsigned long worklen
;
1623 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
1625 /* build a length error message */
1626 blocklen
= tpbin
->length
;
1627 if (readsize
< blocklen
+ PREFIXSIZE
)
1628 lengtherr
= "short";
1630 if (readsize
> blocklen
+ PREFIXSIZE
)
1635 if (tpbin
->compressed
) {
1636 /* uncompress whatever we read, if it fails, complain later */
1637 cresult
= uncompress(comprbuf
, &worklen
, tpbin
->buf
, blocklen
);
1641 output
= tpbin
->buf
;
1646 if (worklen
!= ntrec
* TP_BSIZE
) {
1647 /* short block, shouldn't happen, but... */
1648 reason
= "length mismatch";
1649 if (worklen
% TP_BSIZE
== 0)
1650 numtrec
= worklen
/ TP_BSIZE
;
1654 reason
= "not enough memory";
1657 reason
= "buffer too small";
1660 reason
= "data error";
1667 fprintf(stderr
, "%s compressed block: %d expected: %d\n",
1668 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
1669 fprintf(stderr
, "decompression error, block %ld: %s\n",
1670 tpblksread
+1, reason
);
1671 if (cresult
!= Z_OK
) output
= NULL
;
1677 * Print an error message for a read error.
1678 * This was exteracted from the original readtape().
1681 msg_read_error(char *m
)
1683 switch (curfile
.action
) {
1685 fprintf(stderr
, "%s trying to set up tape\n", m
);
1688 fprintf(stderr
, "%s trying to resynchronize\n", m
);
1691 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
1694 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
1695 (unsigned long)curfile
.ino
);
1699 #endif /* HAVE_ZLIB */
1702 * Read the first block and set the blocksize from its length. Test
1703 * if the block looks like a compressed dump tape. setup() will make
1704 * the final determination by checking the compressed flag if gethead()
1705 * finds a valid header. The test here is necessary to offset the buffer
1706 * by the size of the compressed prefix. zflag is set here so that
1707 * readtape_set can set the correct function pointer for readtape().
1708 * Note that the first block of each tape/file will not be compressed.
1711 findtapeblksize(void)
1715 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
1716 struct s_spcl
*spclpt
= (struct s_spcl
*) tpb
->buf
;
1718 for (i
= 0; i
< ntrec
; i
++)
1719 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1723 * For a pipe or file, read in the first record. For a tape, read
1726 len
= magtapein
? bufsize
+ PREFIXSIZE
: TP_BSIZE
;
1728 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
1729 errx(1, "Tape read error on first record");
1732 * If the input is from a file or a pipe, we read TP_BSIZE
1733 * bytes looking for a compressed dump header, we then
1734 * need to read in the rest of the record, as determined by
1735 * tpb->length or bufsize. The first block of the dump is
1736 * guaranteed to not be compressed so we look at the header.
1739 if (tpb
->length
% TP_BSIZE
== 0
1740 && tpb
->length
<= bufsize
1741 && tpb
->compressed
== 0
1742 && spclpt
->c_type
== TS_TAPE
1743 && spclpt
->c_flags
& DR_COMPRESSED
) {
1744 /* Looks like it's a compressed dump block prefix, */
1745 /* read in the rest of the block based on tpb->length. */
1746 len
= tpb
->length
- TP_BSIZE
+ PREFIXSIZE
;
1747 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) <= 0
1749 errx(1,"Error reading dump file header");
1751 numtrec
= ntrec
= tpb
->length
/ TP_BSIZE
;
1755 /* read in the rest of the block based on bufsize */
1756 len
= bufsize
- TP_BSIZE
;
1757 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) <= 0
1759 errx(1,"Error reading dump file header");
1763 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
1768 * If the input is a tape, we tried to read PREFIXSIZE +
1769 * ntrec * TP_BSIZE bytes. If it's not a compressed dump tape
1770 * or the value of ntrec is too large, we have read less than
1771 * what we asked for; adjust the value of ntrec and test for
1772 * a compressed dump tape prefix.
1775 if (i
% TP_BSIZE
!= 0) {
1776 if (i
% TP_BSIZE
== PREFIXSIZE
1777 && tpb
->compressed
== 0
1778 && spclpt
->c_type
== TS_TAPE
1779 && spclpt
->c_flags
& DR_COMPRESSED
) {
1783 if (tpb
->length
> bufsize
)
1784 errx(1, "Tape blocksize is too large, use "
1785 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
1788 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1791 ntrec
= i
/ TP_BSIZE
;
1793 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1797 * Read a block of data handling all of the messy details.
1799 static int read_a_block(int fd
, void *buf
, size_t len
, long *lengthread
)
1807 i
= rmtread(buf
, size
);
1810 i
= read(fd
, buf
, size
);
1813 break; /* EOD or error */
1816 break; /* block at a time for mt */
1819 *lengthread
= len
- size
;
1838 * Read the next block from the tape.
1839 * Check to see if it is one of several vintage headers.
1840 * If it is an old style header, convert it to a new style header.
1841 * If it is not any valid header, return an error.
1844 gethead(struct s_spcl
*buf
)
1852 char dummy
[TP_BSIZE
];
1859 u_int16_t c_inumber
;
1864 u_int16_t odi_nlink
;
1880 readtape((char *)buf
);
1881 if (buf
->c_magic
!= NFS_MAGIC
) {
1882 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1885 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1889 if (checksum((int *)buf
) == FAIL
)
1892 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1895 readtape((char *)(&u_ospcl
.s_ospcl
));
1896 memset((char *)buf
, 0, (long)TP_BSIZE
);
1897 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1898 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1899 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1900 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1901 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1902 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1903 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1904 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1905 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1906 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1907 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1908 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1909 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1910 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1912 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1913 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1914 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1915 #else /* __linux__ */
1916 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1917 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1918 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1919 #endif /* __linux__ */
1920 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1921 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1922 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1923 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1925 buf
->c_magic
= NFS_MAGIC
;
1928 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1929 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1930 qcvt
.qval
= buf
->c_dinode
.di_size
;
1931 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1932 printf("Note: Doing Quad swapping\n");
1937 qcvt
.qval
= buf
->c_dinode
.di_size
;
1939 qcvt
.val
[1] = qcvt
.val
[0];
1941 buf
->c_dinode
.di_size
= qcvt
.qval
;
1945 switch (buf
->c_type
) {
1950 * Have to patch up missing information in bit map headers
1953 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1954 if (buf
->c_count
> TP_NINDIR
)
1957 for (i
= 0; i
< buf
->c_count
; i
++)
1962 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1974 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1978 * If we are restoring a filesystem with old format inodes,
1979 * copy the uid/gid to the new location.
1982 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1983 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1991 * Check that a header is where it belongs and predict the next header
1994 accthdr(struct s_spcl
*header
)
1996 static ino_t previno
= 0x7fffffff;
1997 static int prevtype
;
1998 static long predict
;
2001 if (header
->c_type
== TS_TAPE
) {
2002 fprintf(stderr
, "Volume header (%s inode format) ",
2003 oldinofmt
? "old" : "new");
2004 if (header
->c_firstrec
)
2005 fprintf(stderr
, "begins with record %d",
2006 header
->c_firstrec
);
2007 fprintf(stderr
, "\n");
2008 previno
= 0x7fffffff;
2011 if (previno
== 0x7fffffff)
2015 fprintf(stderr
, "Dumped inodes map header");
2018 fprintf(stderr
, "Used inodes map header");
2021 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2024 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2027 fprintf(stderr
, "End of tape header");
2030 if (predict
!= blksread
- 1)
2031 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2032 predict
, blksread
- 1);
2033 fprintf(stderr
, "\n");
2036 if (header
->c_type
!= TS_END
)
2037 for (i
= 0; i
< header
->c_count
; i
++)
2038 if (readmapflag
|| header
->c_addr
[i
] != 0)
2042 prevtype
= header
->c_type
;
2043 previno
= header
->c_inumber
;
2047 * Find an inode header.
2048 * Complain if had to skip, and complain is set.
2051 findinode(struct s_spcl
*header
)
2053 static long skipcnt
= 0;
2057 curfile
.name
= "<name unknown>";
2058 curfile
.action
= UNKNOWN
;
2062 if (header
->c_magic
!= NFS_MAGIC
) {
2064 while (gethead(header
) == FAIL
||
2065 header
->c_date
!= dumpdate
)
2068 switch (header
->c_type
) {
2072 * Skip up to the beginning of the next record
2074 for (i
= 0; i
< header
->c_count
; i
++)
2075 if (header
->c_addr
[i
])
2077 while (gethead(header
) == FAIL
||
2078 header
->c_date
!= dumpdate
)
2083 curfile
.dip
= &header
->c_dinode
;
2084 curfile
.ino
= header
->c_inumber
;
2088 curfile
.ino
= maxino
;
2092 curfile
.name
= "<file removal list>";
2096 curfile
.name
= "<file dump list>";
2100 panic("unexpected tape header\n");
2104 panic("unknown tape header type %d\n", spcl
.c_type
);
2108 } while (header
->c_type
== TS_ADDR
);
2110 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2120 j
= sizeof(union u_spcl
) / sizeof(int);
2127 /* What happens if we want to read restore tapes
2128 for a 16bit int machine??? */
2134 if (i
!= CHECKSUM
) {
2135 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2136 (unsigned long)curfile
.ino
, curfile
.name
);
2146 #include <varargs.h>
2151 msg(const char *fmt
, ...)
2164 (void)vfprintf(stderr
, fmt
, ap
);
2167 #endif /* RRESTORE */
2170 swab16(u_char
*sp
, int n
)
2175 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2182 swab32(u_char
*sp
, int n
)
2187 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2188 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2195 swab64(u_char
*sp
, int n
)
2200 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2201 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2202 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2203 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2210 swabst(u_char
*cp
, u_char
*sp
)
2216 case '0': case '1': case '2': case '3': case '4':
2217 case '5': case '6': case '7': case '8': case '9':
2218 n
= (n
* 10) + (*cp
++ - '0');
2221 case 's': case 'w': case 'h':
2239 default: /* Any other character, like 'b' counts as byte. */
2253 swabst((u_char
*)"i", (u_char
*)&x
);
2261 swabst((u_char
*)"l", (u_char
*)&x
);