]>
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.13 2000/03/02 11:34:51 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 static int readmapflag
;
131 * Set up an input source
134 setinput(char *source
)
140 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
144 if (strchr(source
, ':')) {
146 source
= strchr(host
, ':');
148 if (rmthost(host
) == 0)
152 if (strcmp(source
, "-") == 0) {
154 * Since input is coming from a pipe we must establish
155 * our own connection to the terminal.
157 terminal
= fopen(_PATH_TTY
, "r");
158 if (terminal
== NULL
) {
159 warn("cannot open %s", _PATH_TTY
);
160 terminal
= fopen(_PATH_DEVNULL
, "r");
161 if (terminal
== NULL
)
162 err(1, "cannot open %s", _PATH_DEVNULL
);
166 setuid(getuid()); /* no longer need or want root privileges */
168 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
169 magtapeprefix
[MAXPATHLEN
-1] = '\0';
170 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
173 strncpy(magtape
, source
, MAXPATHLEN
);
174 magtape
[MAXPATHLEN
- 1] = '\0';
178 newtapebuf(long size
)
180 static int tapebufsize
= -1;
183 if (size
<= tapebufsize
)
187 tapebuf
= malloc(size
* TP_BSIZE
);
189 errx(1, "Cannot allocate space for tape buffer");
194 * Verify that the tape drive can be accessed and
195 * that it actually is a dump tape.
203 Vprintf(stdout
, "Verify tape and initialize maps\n");
206 mt
= rmtopen(magtape
, 0);
212 mt
= open(magtape
, O_RDONLY
, 0);
214 err(1, "%s", magtape
);
218 if (!pipein
&& !bflag
)
220 if (gethead(&spcl
) == FAIL
) {
221 blkcnt
--; /* push back this block */
225 if (gethead(&spcl
) == FAIL
)
226 errx(1, "Tape is not a dump tape");
227 fprintf(stderr
, "Converting to new file system format.\n");
230 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
231 endoftapemark
.s_spcl
.c_type
= TS_END
;
232 ip
= (int *)&endoftapemark
;
233 j
= sizeof(union u_spcl
) / sizeof(int);
238 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
240 if (vflag
|| command
== 't' || command
== 'C')
242 if (filesys
[0] == '\0') {
244 printf("spcl.c_filesys = %s\n", spcl
.c_filesys
);
245 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
246 filesys
[NAMELEN
- 1] = '\0';
247 dirptr
= strstr(filesys
, " (dir");
248 printf("dirptr = %s\n", dirptr
);
251 printf("dirptr = %s\n", dirptr
);
252 printf("filesys = %s\n", filesys
);
254 dumptime
= spcl
.c_ddate
;
255 dumpdate
= spcl
.c_date
;
256 if (stat(".", &stbuf
) < 0)
257 err(1, "cannot stat .");
258 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
260 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
261 fssize
= stbuf
.st_blksize
;
262 if (((fssize
- 1) & fssize
) != 0)
263 errx(1, "bad block size %ld", fssize
);
264 if (spcl
.c_volume
!= 1)
265 errx(1, "Tape is not volume 1 of the dump");
266 if (gethead(&spcl
) == FAIL
) {
267 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
268 panic("no header after volume mark!\n");
271 if (spcl
.c_type
!= TS_CLRI
)
272 errx(1, "Cannot find file removal list");
273 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
274 Dprintf(stdout
, "maxino = %ld\n", (long)maxino
);
275 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
277 panic("no memory for active inode map\n");
279 curfile
.action
= USING
;
280 getfile(xtrmap
, xtrmapskip
);
281 if (spcl
.c_type
!= TS_BITS
)
282 errx(1, "Cannot find file dump list");
283 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
284 if (map
== (char *)NULL
)
285 panic("no memory for file dump list\n");
287 curfile
.action
= USING
;
288 getfile(xtrmap
, xtrmapskip
);
290 * If there may be whiteout entries on the tape, pretend that the
291 * whiteout inode exists, so that the whiteout entries can be
295 SETINO(WINO
, dumpmap
);
299 * Prompt user to load a new dump volume.
300 * "Nextvol" is the next suggested volume to use.
301 * This suggested volume is enforced when doing full
302 * or incremental restores, but can be overridden by
303 * the user when only extracting a subset of the files.
308 long newvol
= 0, savecnt
= 0, wantnext
= 0, i
;
309 union u_spcl tmpspcl
;
310 # define tmpbuf tmpspcl.s_spcl
320 panic("Changing volumes on pipe input?\n");
328 exit(1); /* pipes do not get a second chance */
329 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
336 while (newvol
<= 0) {
337 if (tapesread
== 0) {
338 fprintf(stderr
, "%s%s%s%s%s",
339 "You have not read any tapes yet.\n",
340 "Unless you know which volume your",
341 " file(s) are on you should start\n",
342 "with the last volume and work",
343 " towards the first.\n");
345 fprintf(stderr
, "You have read volumes");
347 for (i
= 1; i
< 32; i
++)
348 if (tapesread
& (1 << i
)) {
349 fprintf(stderr
, "%s%ld", buf
, (long)i
);
352 fprintf(stderr
, "\n");
355 fprintf(stderr
, "Specify next volume #: ");
356 (void) fflush(stderr
);
357 (void) fgets(buf
, BUFSIZ
, terminal
);
358 } while (!feof(terminal
) && buf
[0] == '\n');
364 "Volume numbers are positive numerics\n");
367 if (newvol
== volno
) {
368 tapesread
|= 1 << volno
;
373 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
374 magtape
[MAXPATHLEN
- 1] = '\0';
376 if (!Mflag
|| haderror
) {
378 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
379 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
380 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
381 (void) fflush(stderr
);
382 (void) fgets(buf
, BUFSIZ
, terminal
);
385 if (!strcmp(buf
, "none\n")) {
389 if (buf
[0] != '\n') {
390 (void) strcpy(magtape
, buf
);
391 magtape
[strlen(magtape
) - 1] = '\0';
396 mt
= rmtopen(magtape
, 0);
399 mt
= open(magtape
, O_RDONLY
, 0);
402 fprintf(stderr
, "Cannot open %s\n", magtape
);
411 if (gethead(&tmpbuf
) == FAIL
) {
412 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
413 fprintf(stderr
, "tape is not dump tape\n");
418 if (tmpbuf
.c_volume
!= volno
) {
419 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
424 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
426 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
427 ctime4(&tmpbuf
.c_date
));
428 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
430 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
431 ctime(&tmpbuf
.c_date
));
432 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
438 tapesread
|= 1 << volno
;
441 * If continuing from the previous volume, skip over any
442 * blocks read already at the end of the previous volume.
444 * If coming to this volume at random, skip to the beginning
445 * of the next record.
447 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
448 tpblksread
, (long)tmpbuf
.c_firstrec
);
449 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
451 tpblksread
= tmpbuf
.c_firstrec
;
452 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
454 } else if (tmpbuf
.c_firstrec
> 0 &&
455 tmpbuf
.c_firstrec
< tpblksread
- 1) {
457 * -1 since we've read the volume header
459 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
460 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
461 (long)i
, i
> 1 ? "s" : "");
466 if (curfile
.action
== USING
) {
468 panic("active file into volume 1\n");
472 * Skip up to the beginning of the next record
474 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
475 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
477 (void) gethead(&spcl
);
481 siglongjmp(restart
, 1);
486 * Handle unexpected EOF.
492 if (gettingfile
&& curfile
.action
== USING
) {
493 printf("Warning: %s %s\n",
494 "End-of-input encountered while extracting", curfile
.name
);
496 curfile
.name
= "<name unknown>";
497 curfile
.action
= UNKNOWN
;
499 curfile
.ino
= maxino
;
502 siglongjmp(restart
, 1);
507 * handle multiple dumps per tape by skipping forward to the
515 if (dumpnum
== 1 || volno
!= 1)
518 errx(1, "Cannot have multiple dumps on pipe input");
520 tcom
.mt_count
= dumpnum
- 1;
523 rmtioctl(MTFSF
, dumpnum
- 1);
526 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
534 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
535 fprintf(stdout
, "Dumped from: %s",
536 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
538 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
539 fprintf(stdout
, "Dumped from: %s",
540 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
542 if (spcl
.c_host
[0] == '\0')
544 fprintf(stderr
, "Level %d dump of %s on %s:%s\n",
545 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
546 fprintf(stderr
, "Label: %s\n", spcl
.c_label
);
550 extractfile(char *name
)
554 struct timeval timep
[2];
558 curfile
.action
= USING
;
560 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
561 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
562 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
563 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
564 #else /* __linux__ */
565 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
566 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
567 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
568 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
569 #endif /* __linux__ */
570 mode
= curfile
.dip
->di_mode
;
571 flags
= curfile
.dip
->di_flags
;
572 switch (mode
& IFMT
) {
575 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
580 Vprintf(stdout
, "skipped socket %s\n", name
);
586 ep
= lookupname(name
);
587 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
588 panic("unextracted directory %s\n", name
);
592 Vprintf(stdout
, "extract file %s\n", name
);
593 return (genliteraldir(name
, curfile
.ino
));
598 uid_t luid
= curfile
.dip
->di_uid
;
599 gid_t lgid
= curfile
.dip
->di_gid
;
603 getfile(xtrlnkfile
, xtrlnkskip
);
606 "%s: zero length symbolic link (ignored)\n", name
);
609 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
612 (void) lchown(name
, luid
, lgid
);
618 Vprintf(stdout
, "extract fifo %s\n", name
);
625 if (mkfifo(name
, mode
) < 0) {
626 warn("%s: cannot create fifo", name
);
630 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
631 (void) chmod(name
, mode
);
634 (void) fsetflags(name
, flags
);
636 (void) chflags(name
, flags
);
644 Vprintf(stdout
, "extract special file %s\n", name
);
651 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
652 warn("%s: cannot create special file", name
);
656 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
657 (void) chmod(name
, mode
);
661 warn("%s: fsetflags called on a special file", name
);
662 (void) fsetflags(name
, flags
);
665 (void) chflags(name
, flags
);
672 Vprintf(stdout
, "extract file %s\n", name
);
679 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
681 warn("%s: cannot create file", name
);
685 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
686 (void) fchmod(ofile
, mode
);
689 (void) setflags(ofile
, flags
);
691 (void) fchflags(ofile
, flags
);
693 getfile(xtrfile
, xtrskip
);
702 * skip over bit maps on the tape
708 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
713 * skip over a file on the tape
719 curfile
.action
= SKIP
;
720 getfile(xtrnull
, xtrnull
);
724 * Extract a file from the tape.
725 * When an allocated block is found it is passed to the fill function;
726 * when an unallocated block (hole) is found, a zeroed buffer is passed
727 * to the skip function.
730 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
733 volatile int curblk
= 0;
734 volatile quad_t size
= spcl
.c_dinode
.di_size
;
735 volatile int last_write_was_hole
= 0;
736 quad_t origsize
= size
;
737 static char clearedbuf
[MAXBSIZE
];
738 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
741 if (spcl
.c_type
== TS_END
)
742 panic("ran off end of tape\n");
743 if (spcl
.c_magic
!= NFS_MAGIC
)
744 panic("not at beginning of a file\n");
745 if (!gettingfile
&& setjmp(restart
) != 0)
749 for (i
= 0; i
< spcl
.c_count
; i
++) {
750 if (readmapflag
|| spcl
.c_addr
[i
]) {
751 readtape(&buf
[curblk
++][0]);
752 if (curblk
== fssize
/ TP_BSIZE
) {
753 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
754 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
756 last_write_was_hole
= 0;
760 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
762 (curblk
- 1) * TP_BSIZE
+ size
));
765 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
767 last_write_was_hole
= 1;
769 if ((size
-= TP_BSIZE
) <= 0) {
770 for (i
++; i
< spcl
.c_count
; i
++)
771 if (readmapflag
|| spcl
.c_addr
[i
])
776 if (gethead(&spcl
) == GOOD
&& size
> 0) {
777 if (spcl
.c_type
== TS_ADDR
)
780 "Missing address (header) block for %s at %ld blocks\n",
781 curfile
.name
, (long)blksread
);
784 (*fill
)((char *)buf
, (size_t)(curblk
* TP_BSIZE
) + size
);
785 last_write_was_hole
= 0;
787 if (last_write_was_hole
) {
788 ftruncate(ofile
, origsize
);
795 * Write out the next block of a file.
798 xtrfile(char *buf
, size_t size
)
803 if (write(ofile
, buf
, (int) size
) == -1)
804 err(1, "write error extracting inode %lu, name %s\nwrite",
805 (unsigned long)curfile
.ino
, curfile
.name
);
809 * Skip over a hole in a file.
813 xtrskip(char *buf
, size_t size
)
816 if (lseek(ofile
, (off_t
)size
, SEEK_CUR
) == -1)
817 err(1, "seek error extracting inode %lu, name %s\nlseek",
818 (unsigned long)curfile
.ino
, curfile
.name
);
822 * Collect the next block of a symbolic link.
825 xtrlnkfile(char *buf
, size_t size
)
829 if (pathlen
> MAXPATHLEN
)
830 errx(1, "symbolic link name: %s->%s%s; too long %d",
831 curfile
.name
, lnkbuf
, buf
, pathlen
);
832 (void) strcat(lnkbuf
, buf
);
836 * Skip over a hole in a symbolic link (should never happen).
840 xtrlnkskip(char *buf
, size_t size
)
843 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
847 * Collect the next block of a bit map.
850 xtrmap(char *buf
, size_t size
)
853 memmove(map
, buf
, size
);
858 * Skip over a hole in a bit map (should never happen).
862 xtrmapskip(char *buf
, size_t size
)
865 panic("hole in map\n");
870 * Noop, when an extraction function is not needed.
874 xtrnull(char *buf
, size_t size
)
881 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
883 static char buf_tape
[BUFSIZ
];
884 static char buf_disk
[BUFSIZ
];
889 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
890 close(fd_tape
), close(fd_disk
);
891 panic("do_cmpfiles: unexpected EOF[1]");
893 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
894 close(fd_tape
), close(fd_disk
);
895 panic("do_cmpfiles: unexpected EOF[2]");
897 if (n_tape
!= n_disk
) {
898 close(fd_tape
), close(fd_disk
);
899 panic("do_cmpfiles: sizes different!");
901 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
907 /* for debugging compare problems */
908 #undef COMPARE_FAIL_KEEP_FILE
911 #ifdef COMPARE_FAIL_KEEP_FILE
912 /* return true if tapefile should be unlinked after compare */
917 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
919 struct stat sbuf_tape
;
920 int fd_tape
, fd_disk
;
922 if (stat(tapefile
, &sbuf_tape
) != 0) {
923 panic("Can't lstat tmp file %s: %s\n", tapefile
,
927 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
929 "%s: size changed from %ld to %ld.\n",
930 diskfile
, (long)sbuf_tape
.st_size
, (long)sbuf_disk
->st_size
);
931 #ifdef COMPARE_FAIL_KEEP_FILE
938 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
939 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
941 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
943 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
946 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
947 fprintf(stderr
, "%s: tape and disk copies are different\n",
951 #ifdef COMPARE_FAIL_KEEP_FILE
952 /* rename the file to live in /tmp */
953 /* rename `tapefile' to /tmp/<basename of diskfile> */
955 char *p
= strrchr(diskfile
, '/');
956 char newname
[MAXPATHLEN
];
958 panic("can't find / in %s\n", diskfile
);
960 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
961 if (rename(tapefile
, newname
)) {
962 panic("rename from %s to %s failed: %s\n",
966 fprintf(stderr
, "*** %s saved to %s\n",
971 /* don't unlink the file (it's not there anymore */
980 #ifdef COMPARE_FAIL_KEEP_FILE
985 static char tmpfilename
[MAXPATHLEN
];
988 comparefile(char *name
)
990 static char *tmpfile
= NULL
;
992 struct stat sb
, stemp
;
995 if ((r
= lstat(name
, &sb
)) != 0) {
996 warn("%s: does not exist (%d)", name
, r
);
1001 curfile
.name
= name
;
1002 curfile
.action
= USING
;
1003 mode
= curfile
.dip
->di_mode
;
1005 Vprintf(stdout
, "comparing %s (size: %ld, mode: 0%o)\n", name
,
1006 (long)sb
.st_size
, mode
);
1008 if (sb
.st_mode
!= mode
) {
1009 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1010 name
, mode
& 07777, sb
.st_mode
& 07777);
1012 switch (mode
& IFMT
) {
1026 char lbuf
[MAXPATHLEN
+ 1];
1029 if (!(sb
.st_mode
& S_IFLNK
)) {
1030 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1036 getfile(xtrlnkfile
, xtrlnkskip
);
1039 "%s: zero length symbolic link (ignored)\n",
1043 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1044 panic("readlink of %s failed: %s", name
,
1048 if (strcmp(lbuf
, lnkbuf
) != 0) {
1050 "%s: symbolic link changed from %s to %s.\n",
1051 name
, lnkbuf
, lbuf
);
1059 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1060 fprintf(stderr
, "%s: no longer a special file\n",
1066 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1068 "%s: device changed from %d,%d to %d,%d.\n",
1070 ((int)curfile
.dip
->di_rdev
>> 8) & 0xff,
1071 (int)curfile
.dip
->di_rdev
& 0xff,
1072 ((int)sb
.st_rdev
>> 8) & 0xff,
1073 (int)sb
.st_rdev
& 0xff);
1079 if (tmpfile
== NULL
) {
1080 /* argument to mktemp() must not be in RO space: */
1081 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1082 tmpfile
= mktemp(&tmpfilename
[0]);
1084 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1085 panic("cannot delete tmp file %s: %s\n",
1086 tmpfile
, strerror(errno
));
1088 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1089 panic("cannot create file temp file %s: %s\n",
1090 name
, strerror(errno
));
1092 getfile(xtrfile
, xtrskip
);
1093 (void) close(ofile
);
1094 #ifdef COMPARE_FAIL_KEEP_FILE
1095 if (cmpfiles(tmpfile
, name
, &sb
))
1098 cmpfiles(tmpfile
, name
, &sb
);
1107 * Read TP_BSIZE blocks from the input.
1108 * Handle read errors, and end of media.
1113 ssize_t rd
, newvol
, i
;
1114 int cnt
, seek_failed
;
1116 if (blkcnt
< numtrec
) {
1117 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1122 for (i
= 0; i
< ntrec
; i
++)
1123 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1126 cnt
= ntrec
* TP_BSIZE
;
1131 i
= rmtread(&tapebuf
[rd
], cnt
);
1134 i
= read(mt
, &tapebuf
[rd
], cnt
);
1136 * Check for mid-tape short read error.
1137 * If found, skip rest of buffer and start with the next.
1139 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1140 Dprintf(stdout
, "mid-media short read error.\n");
1144 * Handle partial block read.
1146 if (pipein
&& i
== 0 && rd
> 0)
1148 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1157 * Short read. Process the blocks read.
1159 if (i
% TP_BSIZE
!= 0)
1161 "partial block read: %ld should be %ld\n",
1162 (long)i
, ntrec
* TP_BSIZE
);
1163 numtrec
= i
/ TP_BSIZE
;
1167 * Handle read error.
1170 fprintf(stderr
, "Tape read error while ");
1171 switch (curfile
.action
) {
1173 fprintf(stderr
, "trying to set up tape\n");
1176 fprintf(stderr
, "trying to resynchronize\n");
1179 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1182 fprintf(stderr
, "skipping over inode %lu\n",
1183 (unsigned long)curfile
.ino
);
1186 if (!yflag
&& !reply("continue"))
1188 i
= ntrec
* TP_BSIZE
;
1189 memset(tapebuf
, 0, (size_t)i
);
1192 seek_failed
= (rmtseek(i
, 1) < 0);
1195 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1198 err(1, "continuation failed");
1201 * Handle end of tape.
1204 Vprintf(stdout
, "End-of-tape encountered\n");
1213 if (rd
% TP_BSIZE
!= 0)
1214 panic("partial block read: %d should be %d\n",
1215 rd
, ntrec
* TP_BSIZE
);
1217 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1220 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1226 findtapeblksize(void)
1230 for (i
= 0; i
< ntrec
; i
++)
1231 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1235 i
= rmtread(tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1238 i
= read(mt
, tapebuf
, (size_t)(ntrec
* TP_BSIZE
));
1241 err(1, "tape read error");
1242 if (i
% TP_BSIZE
!= 0)
1243 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1245 ntrec
= i
/ TP_BSIZE
;
1247 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
1265 * Read the next block from the tape.
1266 * Check to see if it is one of several vintage headers.
1267 * If it is an old style header, convert it to a new style header.
1268 * If it is not any valid header, return an error.
1271 gethead(struct s_spcl
*buf
)
1279 char dummy
[TP_BSIZE
];
1286 u_int16_t c_inumber
;
1291 u_int16_t odi_nlink
;
1307 readtape((char *)buf
);
1308 if (buf
->c_magic
!= NFS_MAGIC
) {
1309 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
1312 Vprintf(stdout
, "Note: Doing Byte swapping\n");
1316 if (checksum((int *)buf
) == FAIL
)
1319 swabst((u_char
*)"8i4s31i528bi192b2i", (u_char
*)buf
);
1322 readtape((char *)(&u_ospcl
.s_ospcl
));
1323 memset((char *)buf
, 0, (long)TP_BSIZE
);
1324 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1325 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1326 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1327 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1328 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1329 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1330 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1331 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1332 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1333 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1334 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1335 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1336 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1337 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1339 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1340 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1341 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1342 #else /* __linux__ */
1343 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1344 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1345 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1346 #endif /* __linux__ */
1347 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1348 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1349 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1350 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1352 buf
->c_magic
= NFS_MAGIC
;
1355 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1356 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1357 qcvt
.qval
= buf
->c_dinode
.di_size
;
1358 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1359 printf("Note: Doing Quad swapping\n");
1364 qcvt
.qval
= buf
->c_dinode
.di_size
;
1366 qcvt
.val
[1] = qcvt
.val
[0];
1368 buf
->c_dinode
.di_size
= qcvt
.qval
;
1372 switch (buf
->c_type
) {
1377 * Have to patch up missing information in bit map headers
1380 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1381 if (buf
->c_count
> TP_NINDIR
)
1384 for (i
= 0; i
< buf
->c_count
; i
++)
1389 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1401 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1405 * If we are restoring a filesystem with old format inodes,
1406 * copy the uid/gid to the new location.
1409 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1410 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1418 * Check that a header is where it belongs and predict the next header
1421 accthdr(struct s_spcl
*header
)
1423 static ino_t previno
= 0x7fffffff;
1424 static int prevtype
;
1425 static long predict
;
1428 if (header
->c_type
== TS_TAPE
) {
1429 fprintf(stderr
, "Volume header (%s inode format) ",
1430 oldinofmt
? "old" : "new");
1431 if (header
->c_firstrec
)
1432 fprintf(stderr
, "begins with record %d",
1433 header
->c_firstrec
);
1434 fprintf(stderr
, "\n");
1435 previno
= 0x7fffffff;
1438 if (previno
== 0x7fffffff)
1442 fprintf(stderr
, "Dumped inodes map header");
1445 fprintf(stderr
, "Used inodes map header");
1448 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
1451 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
1454 fprintf(stderr
, "End of tape header");
1457 if (predict
!= blksread
- 1)
1458 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
1459 predict
, blksread
- 1);
1460 fprintf(stderr
, "\n");
1463 if (header
->c_type
!= TS_END
)
1464 for (i
= 0; i
< header
->c_count
; i
++)
1465 if (readmapflag
|| header
->c_addr
[i
] != 0)
1469 prevtype
= header
->c_type
;
1470 previno
= header
->c_inumber
;
1474 * Find an inode header.
1475 * Complain if had to skip, and complain is set.
1478 findinode(struct s_spcl
*header
)
1480 static long skipcnt
= 0;
1484 curfile
.name
= "<name unknown>";
1485 curfile
.action
= UNKNOWN
;
1489 if (header
->c_magic
!= NFS_MAGIC
) {
1491 while (gethead(header
) == FAIL
||
1492 header
->c_date
!= dumpdate
)
1495 switch (header
->c_type
) {
1499 * Skip up to the beginning of the next record
1501 for (i
= 0; i
< header
->c_count
; i
++)
1502 if (header
->c_addr
[i
])
1504 while (gethead(header
) == FAIL
||
1505 header
->c_date
!= dumpdate
)
1510 curfile
.dip
= &header
->c_dinode
;
1511 curfile
.ino
= header
->c_inumber
;
1515 curfile
.ino
= maxino
;
1519 curfile
.name
= "<file removal list>";
1523 curfile
.name
= "<file dump list>";
1527 panic("unexpected tape header\n");
1531 panic("unknown tape header type %d\n", spcl
.c_type
);
1535 } while (header
->c_type
== TS_ADDR
);
1537 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
1547 j
= sizeof(union u_spcl
) / sizeof(int);
1554 /* What happens if we want to read restore tapes
1555 for a 16bit int machine??? */
1561 if (i
!= CHECKSUM
) {
1562 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
1563 (unsigned long)curfile
.ino
, curfile
.name
);
1573 #include <varargs.h>
1578 msg(const char *fmt
, ...)
1591 (void)vfprintf(stderr
, fmt
, ap
);
1594 #endif /* RRESTORE */
1597 swab16(u_char
*sp
, int n
)
1602 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1609 swab32(u_char
*sp
, int n
)
1614 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1615 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
1622 swab64(u_char
*sp
, int n
)
1627 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
1628 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
1629 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
1630 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
1637 swabst(u_char
*cp
, u_char
*sp
)
1643 case '0': case '1': case '2': case '3': case '4':
1644 case '5': case '6': case '7': case '8': case '9':
1645 n
= (n
* 10) + (*cp
++ - '0');
1648 case 's': case 'w': case 'h':
1666 default: /* Any other character, like 'b' counts as byte. */
1680 swabst((u_char
*)"i", (u_char
*)&x
);
1687 swabst((u_char
*)"l", (u_char
*)&x
);