]>
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@cybercable.fr>, 1999-2000
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 * (c) UNIX System Laboratories, Inc.
12 * All or some portions of this file are derived from material licensed
13 * to the University of California by American Telephone and Telegraph
14 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
15 * the permission of UNIX System Laboratories, Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 static const char rcsid
[] =
48 "$Id: tape.c,v 1.19 2000/08/19 22:39:35 stelian Exp $";
51 #include <sys/param.h>
58 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
61 #include <ufs/ufs/dinode.h>
62 #endif /* __linux__ */
63 #include <protocols/dumprestore.h>
66 #include <compaterr.h>
74 #include <ext2fs/ext2fs.h>
79 #include "pathnames.h"
81 static long fssize
= MAXBSIZE
;
83 static int pipein
= 0;
84 static char magtape
[MAXPATHLEN
];
85 static char magtapeprefix
[MAXPATHLEN
];
89 static union u_spcl endoftapemark
;
90 static long blksread
; /* blocks read since last header */
91 static long tpblksread
= 0; /* TP_BSIZE blocks read */
92 static long tapesread
;
93 static sigjmp_buf restart
;
94 static int gettingfile
= 0; /* restart has a valid frame */
95 static char *host
= NULL
;
99 static char lnkbuf
[MAXPATHLEN
+ 1];
102 int oldinofmt
; /* old inode format conversion required */
103 int Bcvt
; /* Swap Bytes (for CCI or sun) */
104 static int Qcvt
; /* Swap quads (for sun) */
106 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
108 static void accthdr
__P((struct s_spcl
*));
109 static int checksum
__P((int *));
110 static void findinode
__P((struct s_spcl
*));
111 static void findtapeblksize
__P((void));
112 static int gethead
__P((struct s_spcl
*));
113 static void readtape
__P((char *));
114 static void setdumpnum
__P((void));
115 static u_int swabi
__P((u_int
));
116 static u_long swabl
__P((u_long
));
117 static u_char
*swab64
__P((u_char
*, int));
118 static u_char
*swab32
__P((u_char
*, int));
119 static u_char
*swab16
__P((u_char
*, int));
120 static void terminateinput
__P((void));
121 static void xtrfile
__P((char *, size_t));
122 static void xtrlnkfile
__P((char *, size_t));
123 static void xtrlnkskip
__P((char *, size_t));
124 static void xtrmap
__P((char *, size_t));
125 static void xtrmapskip
__P((char *, size_t));
126 static void xtrskip
__P((char *, size_t));
128 #define COMPARE_ONTHEFLY 1
131 static int ifile
; /* input file for compare */
132 static int cmperror
; /* compare error */
133 static void xtrcmpfile
__P((char *, size_t));
134 static void xtrcmpskip
__P((char *, size_t));
137 static int readmapflag
;
140 * Set up an input source
143 setinput(char *source
)
149 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
153 if (strchr(source
, ':')) {
155 source
= strchr(host
, ':');
157 if (rmthost(host
) == 0)
161 if (strcmp(source
, "-") == 0) {
163 * Since input is coming from a pipe we must establish
164 * our own connection to the terminal.
166 terminal
= fopen(_PATH_TTY
, "r");
167 if (terminal
== NULL
) {
168 warn("cannot open %s", _PATH_TTY
);
169 terminal
= fopen(_PATH_DEVNULL
, "r");
170 if (terminal
== NULL
)
171 err(1, "cannot open %s", _PATH_DEVNULL
);
175 setuid(getuid()); /* no longer need or want root privileges */
177 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
178 magtapeprefix
[MAXPATHLEN
-1] = '\0';
179 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
182 strncpy(magtape
, source
, MAXPATHLEN
);
183 magtape
[MAXPATHLEN
- 1] = '\0';
187 newtapebuf(long size
)
189 static int tapebufsize
= -1;
192 if (size
<= tapebufsize
)
196 tapebuf
= malloc(size
* TP_BSIZE
);
198 errx(1, "Cannot allocate space for tape buffer");
203 * Verify that the tape drive can be accessed and
204 * that it actually is a dump tape.
212 Vprintf(stdout
, "Verify tape and initialize maps\n");
215 mt
= rmtopen(magtape
, 0);
221 mt
= open(magtape
, O_RDONLY
, 0);
223 err(1, "%s", magtape
);
227 if (!pipein
&& !bflag
)
229 if (gethead(&spcl
) == FAIL
) {
230 blkcnt
--; /* push back this block */
234 if (gethead(&spcl
) == FAIL
)
235 errx(1, "Tape is not a dump tape");
236 fprintf(stderr
, "Converting to new file system format.\n");
239 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
240 endoftapemark
.s_spcl
.c_type
= TS_END
;
241 ip
= (int *)&endoftapemark
;
242 j
= sizeof(union u_spcl
) / sizeof(int);
247 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
249 if (vflag
|| command
== 't' || command
== 'C')
251 if (filesys
[0] == '\0') {
253 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
254 filesys
[NAMELEN
- 1] = '\0';
255 dirptr
= strstr(filesys
, " (dir");
259 dumptime
= spcl
.c_ddate
;
260 dumpdate
= spcl
.c_date
;
261 if (stat(".", &stbuf
) < 0)
262 err(1, "cannot stat .");
263 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
265 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
266 fssize
= stbuf
.st_blksize
;
267 if (((fssize
- 1) & fssize
) != 0)
268 errx(1, "bad block size %ld", fssize
);
269 if (spcl
.c_volume
!= 1)
270 errx(1, "Tape is not volume 1 of the dump");
271 if (gethead(&spcl
) == FAIL
) {
272 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
273 panic("no header after volume mark!\n");
276 if (spcl
.c_type
!= TS_CLRI
)
277 errx(1, "Cannot find file removal list");
278 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
279 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
280 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
282 errx(1, "no memory for active inode map");
284 curfile
.action
= USING
;
285 getfile(xtrmap
, xtrmapskip
);
286 if (spcl
.c_type
!= TS_BITS
)
287 errx(1, "Cannot find file dump list");
288 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
289 if (map
== (char *)NULL
)
290 errx(1, "no memory for file dump list");
292 curfile
.action
= USING
;
293 getfile(xtrmap
, xtrmapskip
);
295 * If there may be whiteout entries on the tape, pretend that the
296 * whiteout inode exists, so that the whiteout entries can be
300 SETINO(WINO
, dumpmap
);
304 * Prompt user to load a new dump volume.
305 * "Nextvol" is the next suggested volume to use.
306 * This suggested volume is enforced when doing full
307 * or incremental restores, but can be overridden by
308 * the user when only extracting a subset of the files.
313 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
314 union u_spcl tmpspcl
;
315 # define tmpbuf tmpspcl.s_spcl
325 panic("Changing volumes on pipe input?\n");
333 exit(1); /* pipes do not get a second chance */
334 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
341 while (newvol
<= 0) {
342 if (tapesread
== 0) {
343 fprintf(stderr
, "%s%s%s%s%s",
344 "You have not read any tapes yet.\n",
345 "Unless you know which volume your",
346 " file(s) are on you should start\n",
347 "with the last volume and work",
348 " towards the first.\n");
350 fprintf(stderr
, "You have read volumes");
352 for (i
= 1; i
< 32; i
++)
353 if (tapesread
& (1 << i
)) {
354 fprintf(stderr
, "%s%ld", buf
, (long)i
);
357 fprintf(stderr
, "\n");
360 fprintf(stderr
, "Specify next volume #: ");
361 (void) fflush(stderr
);
362 (void) fgets(buf
, TP_BSIZE
, terminal
);
363 } while (!feof(terminal
) && buf
[0] == '\n');
369 "Volume numbers are positive numerics\n");
372 if (newvol
== volno
) {
373 tapesread
|= 1 << volno
;
378 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
379 magtape
[MAXPATHLEN
- 1] = '\0';
381 if (!Mflag
|| haderror
) {
383 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
384 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
385 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
386 (void) fflush(stderr
);
387 (void) fgets(buf
, TP_BSIZE
, terminal
);
390 if (!strcmp(buf
, "none\n")) {
394 if (buf
[0] != '\n') {
395 (void) strcpy(magtape
, buf
);
396 magtape
[strlen(magtape
) - 1] = '\0';
401 mt
= rmtopen(magtape
, 0);
404 mt
= open(magtape
, O_RDONLY
, 0);
407 fprintf(stderr
, "Cannot open %s\n", magtape
);
416 if (gethead(&tmpbuf
) == FAIL
) {
417 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
418 fprintf(stderr
, "tape is not dump tape\n");
423 if (tmpbuf
.c_volume
!= volno
) {
424 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
429 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
431 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
432 ctime4(&tmpbuf
.c_date
));
433 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
435 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
436 ctime(&tmpbuf
.c_date
));
437 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
443 tapesread
|= 1 << volno
;
446 * If continuing from the previous volume, skip over any
447 * blocks read already at the end of the previous volume.
449 * If coming to this volume at random, skip to the beginning
450 * of the next record.
452 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
453 tpblksread
, (long)tmpbuf
.c_firstrec
);
454 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
456 tpblksread
= tmpbuf
.c_firstrec
;
457 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
459 } else if (tmpbuf
.c_firstrec
> 0 &&
460 tmpbuf
.c_firstrec
< tpblksread
- 1) {
462 * -1 since we've read the volume header
464 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
465 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
466 (long)i
, i
> 1 ? "s" : "");
471 if (curfile
.action
== USING
) {
473 panic("active file into volume 1\n");
477 * Skip up to the beginning of the next record
479 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
480 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
482 (void) gethead(&spcl
);
486 siglongjmp(restart
, 1);
491 * Handle unexpected EOF.
497 if (gettingfile
&& curfile
.action
== USING
) {
498 printf("Warning: %s %s\n",
499 "End-of-input encountered while extracting", curfile
.name
);
501 curfile
.name
= "<name unknown>";
502 curfile
.action
= UNKNOWN
;
504 curfile
.ino
= maxino
;
507 siglongjmp(restart
, 1);
512 * handle multiple dumps per tape by skipping forward to the
520 if (dumpnum
== 1 || volno
!= 1)
523 errx(1, "Cannot have multiple dumps on pipe input");
525 tcom
.mt_count
= dumpnum
- 1;
528 rmtioctl(MTFSF
, dumpnum
- 1);
531 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
539 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
540 fprintf(stdout
, "Dumped from: %s",
541 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
543 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
544 fprintf(stdout
, "Dumped from: %s",
545 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
547 if (spcl
.c_host
[0] == '\0')
549 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
550 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
551 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
555 extractfile(char *name
)
559 struct timeval timep
[2];
563 curfile
.action
= USING
;
565 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
566 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
567 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
568 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
569 #else /* __linux__ */
570 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
571 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
572 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
573 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
574 #endif /* __linux__ */
575 mode
= curfile
.dip
->di_mode
;
576 flags
= curfile
.dip
->di_flags
;
577 switch (mode
& IFMT
) {
580 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
585 Vprintf(stdout
, "skipped socket %s\n", name
);
591 ep
= lookupname(name
);
592 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
593 panic("unextracted directory %s\n", name
);
597 Vprintf(stdout
, "extract file %s\n", name
);
598 return (genliteraldir(name
, curfile
.ino
));
603 uid_t luid
= curfile
.dip
->di_uid
;
604 gid_t lgid
= curfile
.dip
->di_gid
;
608 getfile(xtrlnkfile
, xtrlnkskip
);
611 "%s: zero length symbolic link (ignored)\n", name
);
614 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
617 (void) lchown(name
, luid
, lgid
);
623 Vprintf(stdout
, "extract fifo %s\n", name
);
630 if (mkfifo(name
, mode
) < 0) {
631 warn("%s: cannot create fifo", name
);
635 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
636 (void) chmod(name
, mode
);
639 (void) fsetflags(name
, flags
);
641 (void) chflags(name
, flags
);
649 Vprintf(stdout
, "extract special file %s\n", name
);
656 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
657 warn("%s: cannot create special file", name
);
661 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
662 (void) chmod(name
, mode
);
666 warn("%s: fsetflags called on a special file", name
);
667 (void) fsetflags(name
, flags
);
670 (void) chflags(name
, flags
);
677 Vprintf(stdout
, "extract file %s\n", name
);
684 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
686 warn("%s: cannot create file", name
);
690 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
691 (void) fchmod(ofile
, mode
);
694 (void) setflags(ofile
, flags
);
696 (void) fchflags(ofile
, flags
);
698 getfile(xtrfile
, xtrskip
);
707 * skip over bit maps on the tape
713 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
718 * skip over a file on the tape
724 curfile
.action
= SKIP
;
725 getfile(xtrnull
, xtrnull
);
729 * Extract a file from the tape.
730 * When an allocated block is found it is passed to the fill function;
731 * when an unallocated block (hole) is found, a zeroed buffer is passed
732 * to the skip function.
735 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
738 volatile int curblk
= 0;
739 volatile quad_t size
= spcl
.c_dinode
.di_size
;
740 volatile int last_write_was_hole
= 0;
741 quad_t origsize
= size
;
742 static char clearedbuf
[MAXBSIZE
];
743 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
746 if (spcl
.c_type
== TS_END
)
747 panic("ran off end of tape\n");
748 if (spcl
.c_magic
!= NFS_MAGIC
)
749 panic("not at beginning of a file\n");
750 if (!gettingfile
&& setjmp(restart
) != 0)
754 for (i
= 0; i
< spcl
.c_count
; i
++) {
755 if (readmapflag
|| spcl
.c_addr
[i
]) {
756 readtape(&buf
[curblk
++][0]);
757 if (curblk
== fssize
/ TP_BSIZE
) {
758 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
759 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
761 last_write_was_hole
= 0;
765 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
767 (curblk
- 1) * TP_BSIZE
+ size
));
770 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
772 last_write_was_hole
= 1;
774 if ((size
-= TP_BSIZE
) <= 0) {
775 for (i
++; i
< spcl
.c_count
; i
++)
776 if (readmapflag
|| spcl
.c_addr
[i
])
781 if (gethead(&spcl
) == GOOD
&& size
> 0) {
782 if (spcl
.c_type
== TS_ADDR
)
785 "Missing address (header) block for %s at %ld blocks\n",
786 curfile
.name
, (long)blksread
);
789 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
790 last_write_was_hole
= 0;
792 if (last_write_was_hole
) {
793 ftruncate(ofile
, origsize
);
800 * Write out the next block of a file.
803 xtrfile(char *buf
, size_t size
)
808 if (write(ofile
, buf
, (int) size
) == -1)
809 err(1, "write error extracting inode %lu, name %s\nwrite",
810 (unsigned long)curfile
.ino
, curfile
.name
);
814 * Skip over a hole in a file.
818 xtrskip(char *buf
, size_t size
)
821 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
822 err(1, "seek error extracting inode %lu, name %s\nlseek",
823 (unsigned long)curfile
.ino
, curfile
.name
);
827 * Collect the next block of a symbolic link.
830 xtrlnkfile(char *buf
, size_t size
)
834 if (pathlen
> MAXPATHLEN
)
835 errx(1, "symbolic link name: %s->%s%s; too long %d",
836 curfile
.name
, lnkbuf
, buf
, pathlen
);
837 (void) strcat(lnkbuf
, buf
);
841 * Skip over a hole in a symbolic link (should never happen).
845 xtrlnkskip(char *buf
, size_t size
)
848 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
852 * Collect the next block of a bit map.
855 xtrmap(char *buf
, size_t size
)
858 memmove(map
, buf
, size
);
863 * Skip over a hole in a bit map (should never happen).
867 xtrmapskip(char *buf
, size_t size
)
870 panic("hole in map\n");
875 * Noop, when an extraction function is not needed.
879 xtrnull(char *buf
, size_t size
)
887 * Compare the next block of a file.
890 xtrcmpfile(char *buf
, size_t size
)
892 static char cmpbuf
[MAXBSIZE
];
897 if (read(ifile
, cmpbuf
, size
) != size
) {
898 fprintf(stderr
, "%s: size has changed.\n",
904 if (memcmp(buf
, cmpbuf
, size
) != 0) {
905 fprintf(stderr
, "%s: tape and disk copies are different\n",
913 * Skip over a hole in a file.
916 xtrcmpskip(char *buf
, size_t size
)
918 static char cmpbuf
[MAXBSIZE
];
924 if (read(ifile
, cmpbuf
, size
) != size
) {
925 fprintf(stderr
, "%s: size has changed.\n",
931 for (i
= 0; i
< size
; ++i
)
932 if (cmpbuf
[i
] != '\0') {
933 fprintf(stderr
, "%s: tape and disk copies are different\n",
939 #endif /* COMPARE_ONTHEFLY */
941 #if !COMPARE_ONTHEFLY
943 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
945 static char buf_tape
[BUFSIZ
];
946 static char buf_disk
[BUFSIZ
];
951 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
952 close(fd_tape
), close(fd_disk
);
953 panic("do_cmpfiles: unexpected EOF[1]");
955 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
956 close(fd_tape
), close(fd_disk
);
957 panic("do_cmpfiles: unexpected EOF[2]");
959 if (n_tape
!= n_disk
) {
960 close(fd_tape
), close(fd_disk
);
961 panic("do_cmpfiles: sizes different!");
963 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
969 /* for debugging compare problems */
970 #undef COMPARE_FAIL_KEEP_FILE
973 #ifdef COMPARE_FAIL_KEEP_FILE
974 /* return true if tapefile should be unlinked after compare */
979 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
981 struct stat sbuf_tape
;
982 int fd_tape
, fd_disk
;
984 if (stat(tapefile
, &sbuf_tape
) != 0) {
985 panic("Can't lstat tmp file %s: %s\n", tapefile
,
990 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
992 "%s: size changed from %ld to %ld.\n",
993 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
995 #ifdef COMPARE_FAIL_KEEP_FILE
1002 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
1003 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1006 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
1008 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1012 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1013 fprintf(stderr
, "%s: tape and disk copies are different\n",
1018 #ifdef COMPARE_FAIL_KEEP_FILE
1019 /* rename the file to live in /tmp */
1020 /* rename `tapefile' to /tmp/<basename of diskfile> */
1022 char *p
= strrchr(diskfile
, '/');
1023 char newname
[MAXPATHLEN
];
1025 panic("can't find / in %s\n", diskfile
);
1027 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1028 if (rename(tapefile
, newname
)) {
1029 panic("rename from %s to %s failed: %s\n",
1033 fprintf(stderr
, "*** %s saved to %s\n",
1038 /* don't unlink the file (it's not there anymore */
1047 #ifdef COMPARE_FAIL_KEEP_FILE
1051 #endif /* !COMPARE_ONTHEFLY */
1053 #if !COMPARE_ONTHEFLY
1054 static char tmpfilename
[MAXPATHLEN
];
1058 comparefile(char *name
)
1063 #if !COMPARE_ONTHEFLY
1064 static char *tmpfile
= NULL
;
1068 if ((r
= lstat(name
, &sb
)) != 0) {
1069 warn("%s: does not exist (%d)", name
, r
);
1075 curfile
.name
= name
;
1076 curfile
.action
= USING
;
1077 mode
= curfile
.dip
->di_mode
;
1079 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1080 (long)sb
.st_size
, mode
);
1082 if (sb
.st_mode
!= mode
) {
1083 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1084 name
, mode
& 07777, sb
.st_mode
& 07777);
1087 switch (mode
& IFMT
) {
1101 char lbuf
[MAXPATHLEN
+ 1];
1104 if (!(sb
.st_mode
& S_IFLNK
)) {
1105 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1112 getfile(xtrlnkfile
, xtrlnkskip
);
1115 "%s: zero length symbolic link (ignored)\n",
1120 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1121 panic("readlink of %s failed: %s", name
,
1126 if (strcmp(lbuf
, lnkbuf
) != 0) {
1128 "%s: symbolic link changed from %s to %s.\n",
1129 name
, lnkbuf
, lbuf
);
1138 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1139 fprintf(stderr
, "%s: no longer a special file\n",
1146 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1148 "%s: device changed from %d,%d to %d,%d.\n",
1150 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1151 (int)curfile
.dip
->di_rdev
& 0xff,
1152 ((int)sb
.st_rdev
>> 8) & 0xff,
1153 (int)sb
.st_rdev
& 0xff);
1160 #if COMPARE_ONTHEFLY
1161 if ((ifile
= open(name
, O_RDONLY
)) < 0) {
1162 panic("Can't open %s: %s\n", name
, strerror(errno
));
1168 getfile(xtrcmpfile
, xtrcmpskip
);
1171 if (read(ifile
, &c
, 1) != 0) {
1172 fprintf(stderr
, "%s: size has changed.\n",
1182 if (tmpfile
== NULL
) {
1183 /* argument to mktemp() must not be in RO space: */
1184 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1185 tmpfile
= mktemp(&tmpfilename
[0]);
1187 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1188 panic("cannot delete tmp file %s: %s\n",
1189 tmpfile
, strerror(errno
));
1191 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1192 panic("cannot create file temp file %s: %s\n",
1193 name
, strerror(errno
));
1195 getfile(xtrfile
, xtrskip
);
1196 (void) close(ofile
);
1197 #ifdef COMPARE_FAIL_KEEP_FILE
1198 if (cmpfiles(tmpfile
, name
, &sb
))
1201 cmpfiles(tmpfile
, name
, &sb
);
1204 #endif /* COMPARE_ONTHEFLY */
1211 * Read TP_BSIZE blocks from the input.
1212 * Handle read errors, and end of media.
1217 ssize_t rd
, newvol
, i
;
1218 int cnt
, seek_failed
;
1220 if (blkcnt
< numtrec
) {
1221 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1226 for (i
= 0; i
< ntrec
; i
++)
1227 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1230 cnt
= ntrec
* TP_BSIZE
;
1235 i
= rmtread(&tapebuf
[rd
], cnt
);
1238 i
= read(mt
, &tapebuf
[rd
], cnt
);
1240 * Check for mid-tape short read error.
1241 * If found, skip rest of buffer and start with the next.
1243 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1244 Dprintf(stdout
, "mid-media short read error.\n");
1248 * Handle partial block read.
1250 if (pipein
&& i
== 0 && rd
> 0)
1252 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1261 * Short read. Process the blocks read.
1263 if (i
% TP_BSIZE
!= 0)
1265 "partial block read: %ld should be %ld\n",
1266 (long)i
, ntrec
* TP_BSIZE
);
1267 numtrec
= i
/ TP_BSIZE
;
1271 * Handle read error.
1274 fprintf(stderr
, "Tape read error while ");
1275 switch (curfile
.action
) {
1277 fprintf(stderr
, "trying to set up tape\n");
1280 fprintf(stderr
, "trying to resynchronize\n");
1283 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1286 fprintf(stderr
, "skipping over inode %lu\n",
1287 (unsigned long)curfile
.ino
);
1290 if (!yflag
&& !reply("continue"))
1292 i
= ntrec
* TP_BSIZE
;
1293 memset(tapebuf
, 0, (size_t)i
);
1296 seek_failed
= (rmtseek(i
, 1) < 0);
1299 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1302 err(1, "continuation failed");
1305 * Handle end of tape.
1308 Vprintf(stdout
, "End-of-tape encountered\n");
1317 if (rd
% TP_BSIZE
!= 0)
1318 panic("partial block read: %d should be %d\n",
1319 rd
, ntrec
* TP_BSIZE
);
1321 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1324 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1330 findtapeblksize(void)
1334 for (i
= 0; i
< ntrec
; i
++)
1335 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1339 i
= rmtread(tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1342 i
= read(mt
, tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1345 err(1, "tape read error");
1346 if (i
% TP_BSIZE
!= 0)
1347 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1349 ntrec
= i
/ TP_BSIZE
;
1351 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1369 * Read the next block from the tape.
1370 * Check to see if it is one of several vintage headers.
1371 * If it is an old style header, convert it to a new style header.
1372 * If it is not any valid header, return an error.
1375 gethead(struct s_spcl
*buf
)
1383 char dummy
[TP_BSIZE
];
1390 u_int16_t c_inumber
;
1395 u_int16_t odi_nlink
;
1411 readtape((char *)buf
);
1412 if (buf
->c_magic
!= NFS_MAGIC
) {
1413 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1416 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1420 if (checksum((int *)buf
) == FAIL
)
1423 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1426 readtape((char *)(&u_ospcl
.s_ospcl
));
1427 memset((char *)buf
, 0, (long)TP_BSIZE
);
1428 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1429 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1430 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1431 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1432 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1433 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1434 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1435 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1436 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1437 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1438 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1439 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1440 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1441 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1443 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1444 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1445 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1446 #else /* __linux__ */
1447 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1448 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1449 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1450 #endif /* __linux__ */
1451 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1452 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1453 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1454 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1456 buf
->c_magic
= NFS_MAGIC
;
1459 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1460 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1461 qcvt
.qval
= buf
->c_dinode
.di_size
;
1462 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1463 printf("Note: Doing Quad swapping\n");
1468 qcvt
.qval
= buf
->c_dinode
.di_size
;
1470 qcvt
.val
[1] = qcvt
.val
[0];
1472 buf
->c_dinode
.di_size
= qcvt
.qval
;
1476 switch (buf
->c_type
) {
1481 * Have to patch up missing information in bit map headers
1484 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1485 if (buf
->c_count
> TP_NINDIR
)
1488 for (i
= 0; i
< buf
->c_count
; i
++)
1493 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1505 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1509 * If we are restoring a filesystem with old format inodes,
1510 * copy the uid/gid to the new location.
1513 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1514 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1522 * Check that a header is where it belongs and predict the next header
1525 accthdr(struct s_spcl
*header
)
1527 static ino_t previno
= 0x7fffffff;
1528 static int prevtype
;
1529 static long predict
;
1532 if (header
->c_type
== TS_TAPE
) {
1533 fprintf(stderr
, "Volume header (%s inode format) ",
1534 oldinofmt
? "old" : "new");
1535 if (header
->c_firstrec
)
1536 fprintf(stderr
, "begins with record %d",
1537 header
->c_firstrec
);
1538 fprintf(stderr
, "\n");
1539 previno
= 0x7fffffff;
1542 if (previno
== 0x7fffffff)
1546 fprintf(stderr
, "Dumped inodes map header");
1549 fprintf(stderr
, "Used inodes map header");
1552 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
1555 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
1558 fprintf(stderr
, "End of tape header");
1561 if (predict
!= blksread
- 1)
1562 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1563 predict
, blksread
- 1);
1564 fprintf(stderr
, "\n");
1567 if (header
->c_type
!= TS_END
)
1568 for (i
= 0; i
< header
->c_count
; i
++)
1569 if (readmapflag
|| header
->c_addr
[i
] != 0)
1573 prevtype
= header
->c_type
;
1574 previno
= header
->c_inumber
;
1578 * Find an inode header.
1579 * Complain if had to skip, and complain is set.
1582 findinode(struct s_spcl
*header
)
1584 static long skipcnt
= 0;
1588 curfile
.name
= "<name unknown>";
1589 curfile
.action
= UNKNOWN
;
1593 if (header
->c_magic
!= NFS_MAGIC
) {
1595 while (gethead(header
) == FAIL
||
1596 header
->c_date
!= dumpdate
)
1599 switch (header
->c_type
) {
1603 * Skip up to the beginning of the next record
1605 for (i
= 0; i
< header
->c_count
; i
++)
1606 if (header
->c_addr
[i
])
1608 while (gethead(header
) == FAIL
||
1609 header
->c_date
!= dumpdate
)
1614 curfile
.dip
= &header
->c_dinode
;
1615 curfile
.ino
= header
->c_inumber
;
1619 curfile
.ino
= maxino
;
1623 curfile
.name
= "<file removal list>";
1627 curfile
.name
= "<file dump list>";
1631 panic("unexpected tape header\n");
1635 panic("unknown tape header type %d\n", spcl
.c_type
);
1639 } while (header
->c_type
== TS_ADDR
);
1641 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1651 j
= sizeof(union u_spcl
) / sizeof(int);
1658 /* What happens if we want to read restore tapes
1659 for a 16bit int machine??? */
1665 if (i
!= CHECKSUM
) {
1666 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
1667 (unsigned long)curfile
.ino
, curfile
.name
);
1677 #include <varargs.h>
1682 msg(const char *fmt
, ...)
1695 (void)vfprintf(stderr
, fmt
, ap
);
1698 #endif /* RRESTORE */
1701 swab16(u_char
*sp
, int n
)
1706 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1713 swab32(u_char
*sp
, int n
)
1718 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1719 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
1726 swab64(u_char
*sp
, int n
)
1731 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
1732 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
1733 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
1734 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
1741 swabst(u_char
*cp
, u_char
*sp
)
1747 case '0': case '1': case '2': case '3': case '4':
1748 case '5': case '6': case '7': case '8': case '9':
1749 n
= (n
* 10) + (*cp
++ - '0');
1752 case 's': case 'w': case 'h':
1770 default: /* Any other character, like 'b' counts as byte. */
1784 swabst((u_char
*)"i", (u_char
*)&x
);
1791 swabst((u_char
*)"l", (u_char
*)&x
);