]>
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
6 * Stelian Pop <pop@cybercable.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.20 2000/11/10 14:42:25 stelian Exp $";
52 #include <sys/param.h>
59 #include <linux/ext2_fs.h>
60 #include <bsdcompat.h>
62 #include <ufs/ufs/dinode.h>
63 #endif /* __linux__ */
64 #include <protocols/dumprestore.h>
67 #include <compaterr.h>
75 #include <ext2fs/ext2fs.h>
80 #include "pathnames.h"
82 static long fssize
= MAXBSIZE
;
84 static int pipein
= 0;
85 static char magtape
[MAXPATHLEN
];
86 static char magtapeprefix
[MAXPATHLEN
];
90 static union u_spcl endoftapemark
;
91 static long blksread
; /* blocks read since last header */
92 static long tpblksread
= 0; /* TP_BSIZE blocks read */
93 static long tapesread
;
94 static sigjmp_buf restart
;
95 static int gettingfile
= 0; /* restart has a valid frame */
96 static char *host
= NULL
;
100 static char lnkbuf
[MAXPATHLEN
+ 1];
103 int oldinofmt
; /* old inode format conversion required */
104 int Bcvt
; /* Swap Bytes (for CCI or sun) */
105 static int Qcvt
; /* Swap quads (for sun) */
107 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
109 static void accthdr
__P((struct s_spcl
*));
110 static int checksum
__P((int *));
111 static void findinode
__P((struct s_spcl
*));
112 static void findtapeblksize
__P((void));
113 static int gethead
__P((struct s_spcl
*));
114 static void readtape
__P((char *));
115 static void setdumpnum
__P((void));
116 static u_int swabi
__P((u_int
));
117 static u_long swabl
__P((u_long
));
118 static u_char
*swab64
__P((u_char
*, int));
119 static u_char
*swab32
__P((u_char
*, int));
120 static u_char
*swab16
__P((u_char
*, int));
121 static void terminateinput
__P((void));
122 static void xtrfile
__P((char *, size_t));
123 static void xtrlnkfile
__P((char *, size_t));
124 static void xtrlnkskip
__P((char *, size_t));
125 static void xtrmap
__P((char *, size_t));
126 static void xtrmapskip
__P((char *, size_t));
127 static void xtrskip
__P((char *, size_t));
129 #define COMPARE_ONTHEFLY 1
132 static int ifile
; /* input file for compare */
133 static int cmperror
; /* compare error */
134 static void xtrcmpfile
__P((char *, size_t));
135 static void xtrcmpskip
__P((char *, size_t));
138 static int readmapflag
;
141 * Set up an input source
144 setinput(char *source
)
150 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
154 if (strchr(source
, ':')) {
156 source
= strchr(host
, ':');
158 if (rmthost(host
) == 0)
162 if (strcmp(source
, "-") == 0) {
164 * Since input is coming from a pipe we must establish
165 * our own connection to the terminal.
167 terminal
= fopen(_PATH_TTY
, "r");
168 if (terminal
== NULL
) {
169 warn("cannot open %s", _PATH_TTY
);
170 terminal
= fopen(_PATH_DEVNULL
, "r");
171 if (terminal
== NULL
)
172 err(1, "cannot open %s", _PATH_DEVNULL
);
176 setuid(getuid()); /* no longer need or want root privileges */
178 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
179 magtapeprefix
[MAXPATHLEN
-1] = '\0';
180 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
183 strncpy(magtape
, source
, MAXPATHLEN
);
184 magtape
[MAXPATHLEN
- 1] = '\0';
188 newtapebuf(long size
)
190 static int tapebufsize
= -1;
193 if (size
<= tapebufsize
)
197 tapebuf
= malloc(size
* TP_BSIZE
);
199 errx(1, "Cannot allocate space for tape buffer");
204 * Verify that the tape drive can be accessed and
205 * that it actually is a dump tape.
213 Vprintf(stdout
, "Verify tape and initialize maps\n");
216 mt
= rmtopen(magtape
, 0);
222 mt
= open(magtape
, O_RDONLY
, 0);
224 err(1, "%s", magtape
);
228 if (!pipein
&& !bflag
)
230 if (gethead(&spcl
) == FAIL
) {
231 blkcnt
--; /* push back this block */
235 if (gethead(&spcl
) == FAIL
)
236 errx(1, "Tape is not a dump tape");
237 fprintf(stderr
, "Converting to new file system format.\n");
240 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
241 endoftapemark
.s_spcl
.c_type
= TS_END
;
242 ip
= (int *)&endoftapemark
;
243 j
= sizeof(union u_spcl
) / sizeof(int);
248 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
250 if (vflag
|| command
== 't' || command
== 'C')
252 if (filesys
[0] == '\0') {
254 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
255 filesys
[NAMELEN
- 1] = '\0';
256 dirptr
= strstr(filesys
, " (dir");
260 dumptime
= spcl
.c_ddate
;
261 dumpdate
= spcl
.c_date
;
262 if (stat(".", &stbuf
) < 0)
263 err(1, "cannot stat .");
264 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
266 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
267 fssize
= stbuf
.st_blksize
;
268 if (((fssize
- 1) & fssize
) != 0)
269 errx(1, "bad block size %ld", fssize
);
270 if (spcl
.c_volume
!= 1)
271 errx(1, "Tape is not volume 1 of the dump");
272 if (gethead(&spcl
) == FAIL
) {
273 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
274 panic("no header after volume mark!\n");
277 if (spcl
.c_type
!= TS_CLRI
)
278 errx(1, "Cannot find file removal list");
279 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
280 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
281 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
283 errx(1, "no memory for active inode map");
285 curfile
.action
= USING
;
286 getfile(xtrmap
, xtrmapskip
);
287 if (spcl
.c_type
!= TS_BITS
)
288 errx(1, "Cannot find file dump list");
289 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
290 if (map
== (char *)NULL
)
291 errx(1, "no memory for file dump list");
293 curfile
.action
= USING
;
294 getfile(xtrmap
, xtrmapskip
);
296 * If there may be whiteout entries on the tape, pretend that the
297 * whiteout inode exists, so that the whiteout entries can be
301 SETINO(WINO
, dumpmap
);
305 * Prompt user to load a new dump volume.
306 * "Nextvol" is the next suggested volume to use.
307 * This suggested volume is enforced when doing full
308 * or incremental restores, but can be overridden by
309 * the user when only extracting a subset of the files.
314 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
315 union u_spcl tmpspcl
;
316 # define tmpbuf tmpspcl.s_spcl
326 panic("Changing volumes on pipe input?\n");
334 exit(1); /* pipes do not get a second chance */
335 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
342 while (newvol
<= 0) {
343 if (tapesread
== 0) {
344 fprintf(stderr
, "%s%s%s%s%s",
345 "You have not read any tapes yet.\n",
346 "Unless you know which volume your",
347 " file(s) are on you should start\n",
348 "with the last volume and work",
349 " towards the first.\n");
351 fprintf(stderr
, "You have read volumes");
353 for (i
= 1; i
< 32; i
++)
354 if (tapesread
& (1 << i
)) {
355 fprintf(stderr
, "%s%ld", buf
, (long)i
);
358 fprintf(stderr
, "\n");
361 fprintf(stderr
, "Specify next volume #: ");
362 (void) fflush(stderr
);
363 (void) fgets(buf
, TP_BSIZE
, terminal
);
364 } while (!feof(terminal
) && buf
[0] == '\n');
370 "Volume numbers are positive numerics\n");
373 if (newvol
== volno
) {
374 tapesread
|= 1 << volno
;
379 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
380 magtape
[MAXPATHLEN
- 1] = '\0';
382 if (!Mflag
|| haderror
) {
384 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
385 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
386 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
387 (void) fflush(stderr
);
388 (void) fgets(buf
, TP_BSIZE
, terminal
);
391 if (!strcmp(buf
, "none\n")) {
395 if (buf
[0] != '\n') {
396 (void) strcpy(magtape
, buf
);
397 magtape
[strlen(magtape
) - 1] = '\0';
402 mt
= rmtopen(magtape
, 0);
405 mt
= open(magtape
, O_RDONLY
, 0);
408 fprintf(stderr
, "Cannot open %s\n", magtape
);
417 if (gethead(&tmpbuf
) == FAIL
) {
418 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
419 fprintf(stderr
, "tape is not dump tape\n");
424 if (tmpbuf
.c_volume
!= volno
) {
425 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
430 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
432 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
433 ctime4(&tmpbuf
.c_date
));
434 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
436 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
437 ctime(&tmpbuf
.c_date
));
438 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
444 tapesread
|= 1 << volno
;
447 * If continuing from the previous volume, skip over any
448 * blocks read already at the end of the previous volume.
450 * If coming to this volume at random, skip to the beginning
451 * of the next record.
453 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
454 tpblksread
, (long)tmpbuf
.c_firstrec
);
455 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
457 tpblksread
= tmpbuf
.c_firstrec
;
458 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
460 } else if (tmpbuf
.c_firstrec
> 0 &&
461 tmpbuf
.c_firstrec
< tpblksread
- 1) {
463 * -1 since we've read the volume header
465 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
466 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
467 (long)i
, i
> 1 ? "s" : "");
472 if (curfile
.action
== USING
) {
474 panic("active file into volume 1\n");
478 * Skip up to the beginning of the next record
480 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
481 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
483 (void) gethead(&spcl
);
487 siglongjmp(restart
, 1);
492 * Handle unexpected EOF.
498 if (gettingfile
&& curfile
.action
== USING
) {
499 printf("Warning: %s %s\n",
500 "End-of-input encountered while extracting", curfile
.name
);
502 curfile
.name
= "<name unknown>";
503 curfile
.action
= UNKNOWN
;
505 curfile
.ino
= maxino
;
508 siglongjmp(restart
, 1);
513 * handle multiple dumps per tape by skipping forward to the
521 if (dumpnum
== 1 || volno
!= 1)
524 errx(1, "Cannot have multiple dumps on pipe input");
526 tcom
.mt_count
= dumpnum
- 1;
529 rmtioctl(MTFSF
, dumpnum
- 1);
532 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
540 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
541 fprintf(stdout
, "Dumped from: %s",
542 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
544 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
545 fprintf(stdout
, "Dumped from: %s",
546 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
548 if (spcl
.c_host
[0] == '\0')
550 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
551 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
552 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
556 extractfile(char *name
)
560 struct timeval timep
[2];
564 curfile
.action
= USING
;
566 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
567 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
568 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
569 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
570 #else /* __linux__ */
571 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
572 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
573 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
574 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
575 #endif /* __linux__ */
576 mode
= curfile
.dip
->di_mode
;
577 flags
= curfile
.dip
->di_flags
;
578 switch (mode
& IFMT
) {
581 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
586 Vprintf(stdout
, "skipped socket %s\n", name
);
592 ep
= lookupname(name
);
593 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
594 panic("unextracted directory %s\n", name
);
598 Vprintf(stdout
, "extract file %s\n", name
);
599 return (genliteraldir(name
, curfile
.ino
));
604 uid_t luid
= curfile
.dip
->di_uid
;
605 gid_t lgid
= curfile
.dip
->di_gid
;
609 getfile(xtrlnkfile
, xtrlnkskip
);
612 "%s: zero length symbolic link (ignored)\n", name
);
615 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
618 (void) lchown(name
, luid
, lgid
);
624 Vprintf(stdout
, "extract fifo %s\n", name
);
631 if (mkfifo(name
, mode
) < 0) {
632 warn("%s: cannot create fifo", name
);
636 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
637 (void) chmod(name
, mode
);
640 (void) fsetflags(name
, flags
);
642 (void) chflags(name
, flags
);
650 Vprintf(stdout
, "extract special file %s\n", name
);
657 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
658 warn("%s: cannot create special file", name
);
662 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
663 (void) chmod(name
, mode
);
667 warn("%s: fsetflags called on a special file", name
);
668 (void) fsetflags(name
, flags
);
671 (void) chflags(name
, flags
);
678 Vprintf(stdout
, "extract file %s\n", name
);
685 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
687 warn("%s: cannot create file", name
);
691 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
692 (void) fchmod(ofile
, mode
);
695 (void) setflags(ofile
, flags
);
697 (void) fchflags(ofile
, flags
);
699 getfile(xtrfile
, xtrskip
);
708 * skip over bit maps on the tape
714 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
719 * skip over a file on the tape
725 curfile
.action
= SKIP
;
726 getfile(xtrnull
, xtrnull
);
730 * Extract a file from the tape.
731 * When an allocated block is found it is passed to the fill function;
732 * when an unallocated block (hole) is found, a zeroed buffer is passed
733 * to the skip function.
736 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
739 volatile int curblk
= 0;
740 volatile quad_t size
= spcl
.c_dinode
.di_size
;
741 volatile int last_write_was_hole
= 0;
742 quad_t origsize
= size
;
743 static char clearedbuf
[MAXBSIZE
];
744 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
747 if (spcl
.c_type
== TS_END
)
748 panic("ran off end of tape\n");
749 if (spcl
.c_magic
!= NFS_MAGIC
)
750 panic("not at beginning of a file\n");
751 if (!gettingfile
&& setjmp(restart
) != 0)
755 for (i
= 0; i
< spcl
.c_count
; i
++) {
756 if (readmapflag
|| spcl
.c_addr
[i
]) {
757 readtape(&buf
[curblk
++][0]);
758 if (curblk
== fssize
/ TP_BSIZE
) {
759 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
760 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
762 last_write_was_hole
= 0;
766 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
768 (curblk
- 1) * TP_BSIZE
+ size
));
771 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
773 last_write_was_hole
= 1;
775 if ((size
-= TP_BSIZE
) <= 0) {
776 for (i
++; i
< spcl
.c_count
; i
++)
777 if (readmapflag
|| spcl
.c_addr
[i
])
782 if (gethead(&spcl
) == GOOD
&& size
> 0) {
783 if (spcl
.c_type
== TS_ADDR
)
786 "Missing address (header) block for %s at %ld blocks\n",
787 curfile
.name
, (long)blksread
);
790 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
791 last_write_was_hole
= 0;
793 if (last_write_was_hole
) {
794 ftruncate(ofile
, origsize
);
801 * Write out the next block of a file.
804 xtrfile(char *buf
, size_t size
)
809 if (write(ofile
, buf
, (int) size
) == -1)
810 err(1, "write error extracting inode %lu, name %s\nwrite",
811 (unsigned long)curfile
.ino
, curfile
.name
);
815 * Skip over a hole in a file.
819 xtrskip(char *buf
, size_t size
)
822 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
823 err(1, "seek error extracting inode %lu, name %s\nlseek",
824 (unsigned long)curfile
.ino
, curfile
.name
);
828 * Collect the next block of a symbolic link.
831 xtrlnkfile(char *buf
, size_t size
)
835 if (pathlen
> MAXPATHLEN
)
836 errx(1, "symbolic link name: %s->%s%s; too long %d",
837 curfile
.name
, lnkbuf
, buf
, pathlen
);
838 (void) strcat(lnkbuf
, buf
);
842 * Skip over a hole in a symbolic link (should never happen).
846 xtrlnkskip(char *buf
, size_t size
)
849 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
853 * Collect the next block of a bit map.
856 xtrmap(char *buf
, size_t size
)
859 memmove(map
, buf
, size
);
864 * Skip over a hole in a bit map (should never happen).
868 xtrmapskip(char *buf
, size_t size
)
871 panic("hole in map\n");
876 * Noop, when an extraction function is not needed.
880 xtrnull(char *buf
, size_t size
)
888 * Compare the next block of a file.
891 xtrcmpfile(char *buf
, size_t size
)
893 static char cmpbuf
[MAXBSIZE
];
898 if (read(ifile
, cmpbuf
, size
) != size
) {
899 fprintf(stderr
, "%s: size has changed.\n",
905 if (memcmp(buf
, cmpbuf
, size
) != 0) {
906 fprintf(stderr
, "%s: tape and disk copies are different\n",
914 * Skip over a hole in a file.
917 xtrcmpskip(char *buf
, size_t size
)
919 static char cmpbuf
[MAXBSIZE
];
925 if (read(ifile
, cmpbuf
, size
) != size
) {
926 fprintf(stderr
, "%s: size has changed.\n",
932 for (i
= 0; i
< size
; ++i
)
933 if (cmpbuf
[i
] != '\0') {
934 fprintf(stderr
, "%s: tape and disk copies are different\n",
940 #endif /* COMPARE_ONTHEFLY */
942 #if !COMPARE_ONTHEFLY
944 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
946 static char buf_tape
[BUFSIZ
];
947 static char buf_disk
[BUFSIZ
];
952 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
953 close(fd_tape
), close(fd_disk
);
954 panic("do_cmpfiles: unexpected EOF[1]");
956 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
957 close(fd_tape
), close(fd_disk
);
958 panic("do_cmpfiles: unexpected EOF[2]");
960 if (n_tape
!= n_disk
) {
961 close(fd_tape
), close(fd_disk
);
962 panic("do_cmpfiles: sizes different!");
964 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
970 /* for debugging compare problems */
971 #undef COMPARE_FAIL_KEEP_FILE
974 #ifdef COMPARE_FAIL_KEEP_FILE
975 /* return true if tapefile should be unlinked after compare */
980 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
982 struct stat sbuf_tape
;
983 int fd_tape
, fd_disk
;
985 if (stat(tapefile
, &sbuf_tape
) != 0) {
986 panic("Can't lstat tmp file %s: %s\n", tapefile
,
991 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
993 "%s: size changed from %ld to %ld.\n",
994 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
996 #ifdef COMPARE_FAIL_KEEP_FILE
1003 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
1004 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1007 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
1009 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1013 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1014 fprintf(stderr
, "%s: tape and disk copies are different\n",
1019 #ifdef COMPARE_FAIL_KEEP_FILE
1020 /* rename the file to live in /tmp */
1021 /* rename `tapefile' to /tmp/<basename of diskfile> */
1023 char *p
= strrchr(diskfile
, '/');
1024 char newname
[MAXPATHLEN
];
1026 panic("can't find / in %s\n", diskfile
);
1028 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1029 if (rename(tapefile
, newname
)) {
1030 panic("rename from %s to %s failed: %s\n",
1034 fprintf(stderr
, "*** %s saved to %s\n",
1039 /* don't unlink the file (it's not there anymore */
1048 #ifdef COMPARE_FAIL_KEEP_FILE
1052 #endif /* !COMPARE_ONTHEFLY */
1054 #if !COMPARE_ONTHEFLY
1055 static char tmpfilename
[MAXPATHLEN
];
1059 comparefile(char *name
)
1064 #if !COMPARE_ONTHEFLY
1065 static char *tmpfile
= NULL
;
1069 if ((r
= lstat(name
, &sb
)) != 0) {
1070 warn("%s: does not exist (%d)", name
, r
);
1076 curfile
.name
= name
;
1077 curfile
.action
= USING
;
1078 mode
= curfile
.dip
->di_mode
;
1080 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1081 (long)sb
.st_size
, mode
);
1083 if (sb
.st_mode
!= mode
) {
1084 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1085 name
, mode
& 07777, sb
.st_mode
& 07777);
1088 switch (mode
& IFMT
) {
1102 char lbuf
[MAXPATHLEN
+ 1];
1105 if (!(sb
.st_mode
& S_IFLNK
)) {
1106 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1113 getfile(xtrlnkfile
, xtrlnkskip
);
1116 "%s: zero length symbolic link (ignored)\n",
1121 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1122 panic("readlink of %s failed: %s", name
,
1127 if (strcmp(lbuf
, lnkbuf
) != 0) {
1129 "%s: symbolic link changed from %s to %s.\n",
1130 name
, lnkbuf
, lbuf
);
1139 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1140 fprintf(stderr
, "%s: no longer a special file\n",
1147 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1149 "%s: device changed from %d,%d to %d,%d.\n",
1151 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1152 (int)curfile
.dip
->di_rdev
& 0xff,
1153 ((int)sb
.st_rdev
>> 8) & 0xff,
1154 (int)sb
.st_rdev
& 0xff);
1161 #if COMPARE_ONTHEFLY
1162 if ((ifile
= open(name
, O_RDONLY
)) < 0) {
1163 panic("Can't open %s: %s\n", name
, strerror(errno
));
1169 getfile(xtrcmpfile
, xtrcmpskip
);
1172 if (read(ifile
, &c
, 1) != 0) {
1173 fprintf(stderr
, "%s: size has changed.\n",
1183 if (tmpfile
== NULL
) {
1184 /* argument to mktemp() must not be in RO space: */
1185 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1186 tmpfile
= mktemp(&tmpfilename
[0]);
1188 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1189 panic("cannot delete tmp file %s: %s\n",
1190 tmpfile
, strerror(errno
));
1192 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1193 panic("cannot create file temp file %s: %s\n",
1194 name
, strerror(errno
));
1196 getfile(xtrfile
, xtrskip
);
1197 (void) close(ofile
);
1198 #ifdef COMPARE_FAIL_KEEP_FILE
1199 if (cmpfiles(tmpfile
, name
, &sb
))
1202 cmpfiles(tmpfile
, name
, &sb
);
1205 #endif /* COMPARE_ONTHEFLY */
1212 * Read TP_BSIZE blocks from the input.
1213 * Handle read errors, and end of media.
1218 ssize_t rd
, newvol
, i
;
1219 int cnt
, seek_failed
;
1221 if (blkcnt
< numtrec
) {
1222 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1227 for (i
= 0; i
< ntrec
; i
++)
1228 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1231 cnt
= ntrec
* TP_BSIZE
;
1236 i
= rmtread(&tapebuf
[rd
], cnt
);
1239 i
= read(mt
, &tapebuf
[rd
], cnt
);
1241 * Check for mid-tape short read error.
1242 * If found, skip rest of buffer and start with the next.
1244 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1245 Dprintf(stdout
, "mid-media short read error.\n");
1249 * Handle partial block read.
1251 if (pipein
&& i
== 0 && rd
> 0)
1253 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1262 * Short read. Process the blocks read.
1264 if (i
% TP_BSIZE
!= 0)
1266 "partial block read: %ld should be %ld\n",
1267 (long)i
, ntrec
* TP_BSIZE
);
1268 numtrec
= i
/ TP_BSIZE
;
1272 * Handle read error.
1275 fprintf(stderr
, "Tape read error while ");
1276 switch (curfile
.action
) {
1278 fprintf(stderr
, "trying to set up tape\n");
1281 fprintf(stderr
, "trying to resynchronize\n");
1284 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1287 fprintf(stderr
, "skipping over inode %lu\n",
1288 (unsigned long)curfile
.ino
);
1291 if (!yflag
&& !reply("continue"))
1293 i
= ntrec
* TP_BSIZE
;
1294 memset(tapebuf
, 0, (size_t)i
);
1297 seek_failed
= (rmtseek(i
, 1) < 0);
1300 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1303 err(1, "continuation failed");
1306 * Handle end of tape.
1309 Vprintf(stdout
, "End-of-tape encountered\n");
1318 if (rd
% TP_BSIZE
!= 0)
1319 panic("partial block read: %d should be %d\n",
1320 rd
, ntrec
* TP_BSIZE
);
1322 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1325 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1331 findtapeblksize(void)
1335 for (i
= 0; i
< ntrec
; i
++)
1336 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1340 i
= rmtread(tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1343 i
= read(mt
, tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1346 err(1, "tape read error");
1347 if (i
% TP_BSIZE
!= 0)
1348 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1350 ntrec
= i
/ TP_BSIZE
;
1352 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1370 * Read the next block from the tape.
1371 * Check to see if it is one of several vintage headers.
1372 * If it is an old style header, convert it to a new style header.
1373 * If it is not any valid header, return an error.
1376 gethead(struct s_spcl
*buf
)
1384 char dummy
[TP_BSIZE
];
1391 u_int16_t c_inumber
;
1396 u_int16_t odi_nlink
;
1412 readtape((char *)buf
);
1413 if (buf
->c_magic
!= NFS_MAGIC
) {
1414 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1417 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1421 if (checksum((int *)buf
) == FAIL
)
1424 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1427 readtape((char *)(&u_ospcl
.s_ospcl
));
1428 memset((char *)buf
, 0, (long)TP_BSIZE
);
1429 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1430 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1431 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1432 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1433 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1434 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1435 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1436 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1437 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1438 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1439 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1440 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1441 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1442 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1444 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1445 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1446 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1447 #else /* __linux__ */
1448 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1449 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1450 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1451 #endif /* __linux__ */
1452 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1453 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1454 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1455 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1457 buf
->c_magic
= NFS_MAGIC
;
1460 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1461 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1462 qcvt
.qval
= buf
->c_dinode
.di_size
;
1463 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1464 printf("Note: Doing Quad swapping\n");
1469 qcvt
.qval
= buf
->c_dinode
.di_size
;
1471 qcvt
.val
[1] = qcvt
.val
[0];
1473 buf
->c_dinode
.di_size
= qcvt
.qval
;
1477 switch (buf
->c_type
) {
1482 * Have to patch up missing information in bit map headers
1485 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1486 if (buf
->c_count
> TP_NINDIR
)
1489 for (i
= 0; i
< buf
->c_count
; i
++)
1494 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1506 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1510 * If we are restoring a filesystem with old format inodes,
1511 * copy the uid/gid to the new location.
1514 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1515 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1523 * Check that a header is where it belongs and predict the next header
1526 accthdr(struct s_spcl
*header
)
1528 static ino_t previno
= 0x7fffffff;
1529 static int prevtype
;
1530 static long predict
;
1533 if (header
->c_type
== TS_TAPE
) {
1534 fprintf(stderr
, "Volume header (%s inode format) ",
1535 oldinofmt
? "old" : "new");
1536 if (header
->c_firstrec
)
1537 fprintf(stderr
, "begins with record %d",
1538 header
->c_firstrec
);
1539 fprintf(stderr
, "\n");
1540 previno
= 0x7fffffff;
1543 if (previno
== 0x7fffffff)
1547 fprintf(stderr
, "Dumped inodes map header");
1550 fprintf(stderr
, "Used inodes map header");
1553 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
1556 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
1559 fprintf(stderr
, "End of tape header");
1562 if (predict
!= blksread
- 1)
1563 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1564 predict
, blksread
- 1);
1565 fprintf(stderr
, "\n");
1568 if (header
->c_type
!= TS_END
)
1569 for (i
= 0; i
< header
->c_count
; i
++)
1570 if (readmapflag
|| header
->c_addr
[i
] != 0)
1574 prevtype
= header
->c_type
;
1575 previno
= header
->c_inumber
;
1579 * Find an inode header.
1580 * Complain if had to skip, and complain is set.
1583 findinode(struct s_spcl
*header
)
1585 static long skipcnt
= 0;
1589 curfile
.name
= "<name unknown>";
1590 curfile
.action
= UNKNOWN
;
1594 if (header
->c_magic
!= NFS_MAGIC
) {
1596 while (gethead(header
) == FAIL
||
1597 header
->c_date
!= dumpdate
)
1600 switch (header
->c_type
) {
1604 * Skip up to the beginning of the next record
1606 for (i
= 0; i
< header
->c_count
; i
++)
1607 if (header
->c_addr
[i
])
1609 while (gethead(header
) == FAIL
||
1610 header
->c_date
!= dumpdate
)
1615 curfile
.dip
= &header
->c_dinode
;
1616 curfile
.ino
= header
->c_inumber
;
1620 curfile
.ino
= maxino
;
1624 curfile
.name
= "<file removal list>";
1628 curfile
.name
= "<file dump list>";
1632 panic("unexpected tape header\n");
1636 panic("unknown tape header type %d\n", spcl
.c_type
);
1640 } while (header
->c_type
== TS_ADDR
);
1642 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1652 j
= sizeof(union u_spcl
) / sizeof(int);
1659 /* What happens if we want to read restore tapes
1660 for a 16bit int machine??? */
1666 if (i
!= CHECKSUM
) {
1667 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
1668 (unsigned long)curfile
.ino
, curfile
.name
);
1678 #include <varargs.h>
1683 msg(const char *fmt
, ...)
1696 (void)vfprintf(stderr
, fmt
, ap
);
1699 #endif /* RRESTORE */
1702 swab16(u_char
*sp
, int n
)
1707 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1714 swab32(u_char
*sp
, int n
)
1719 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1720 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
1727 swab64(u_char
*sp
, int n
)
1732 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
1733 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
1734 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
1735 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
1742 swabst(u_char
*cp
, u_char
*sp
)
1748 case '0': case '1': case '2': case '3': case '4':
1749 case '5': case '6': case '7': case '8': case '9':
1750 n
= (n
* 10) + (*cp
++ - '0');
1753 case 's': case 'w': case 'h':
1771 default: /* Any other character, like 'b' counts as byte. */
1785 swabst((u_char
*)"i", (u_char
*)&x
);
1792 swabst((u_char
*)"l", (u_char
*)&x
);