]>
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, 1995, 1996
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 char sccsid
[] = "@(#)tape.c 8.9 (Berkeley) 5/1/95";
50 #include <sys/param.h>
52 #include <sys/ioctl.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>
73 #include <ext2fs/ext2fs.h>
78 #include "pathnames.h"
80 static long fssize
= MAXBSIZE
;
82 static int pipein
= 0;
83 static char magtape
[BUFSIZ
];
87 static union u_spcl endoftapemark
;
88 static long blksread
; /* blocks read since last header */
89 static long tpblksread
= 0; /* TP_BSIZE blocks read */
90 static long tapesread
;
91 static jmp_buf restart
;
92 static int gettingfile
= 0; /* restart has a valid frame */
93 static char *host
= NULL
;
97 static char lnkbuf
[MAXPATHLEN
+ 1];
100 int oldinofmt
; /* old inode format conversion required */
101 int Bcvt
; /* Swap Bytes (for CCI or sun) */
102 static int Qcvt
; /* Swap quads (for sun) */
104 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
106 static void accthdr
__P((struct s_spcl
*));
107 static int checksum
__P((int *));
108 static void findinode
__P((struct s_spcl
*));
109 static void findtapeblksize
__P((void));
110 static int gethead
__P((struct s_spcl
*));
111 static void readtape
__P((char *));
112 static void setdumpnum
__P((void));
113 static u_long swabl
__P((u_long
));
114 static u_char
*swablong
__P((u_char
*, int));
115 static u_char
*swabshort
__P((u_char
*, int));
116 static void terminateinput
__P((void));
117 static void xtrfile
__P((char *, long));
118 static void xtrlnkfile
__P((char *, long));
119 static void xtrlnkskip
__P((char *, long));
120 static void xtrmap
__P((char *, long));
121 static void xtrmapskip
__P((char *, long));
122 static void xtrskip
__P((char *, long));
125 * Set up an input source
135 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
139 if (strchr(source
, ':')) {
141 source
= strchr(host
, ':');
143 if (rmthost(host
) == 0)
147 if (strcmp(source
, "-") == 0) {
149 * Since input is coming from a pipe we must establish
150 * our own connection to the terminal.
152 terminal
= fopen(_PATH_TTY
, "r");
153 if (terminal
== NULL
) {
154 (void)fprintf(stderr
, "cannot open %s: %s\n",
155 _PATH_TTY
, strerror(errno
));
156 terminal
= fopen(_PATH_DEVNULL
, "r");
157 if (terminal
== NULL
) {
158 (void)fprintf(stderr
, "cannot open %s: %s\n",
159 _PATH_DEVNULL
, strerror(errno
));
165 setuid(getuid()); /* no longer need or want root privileges */
166 (void) strcpy(magtape
, source
);
173 static tapebufsize
= -1;
176 if (size
<= tapebufsize
)
180 tapebuf
= malloc(size
* TP_BSIZE
);
181 if (tapebuf
== NULL
) {
182 fprintf(stderr
, "Cannot allocate space for tape buffer\n");
189 * Verify that the tape drive can be accessed and
190 * that it actually is a dump tape.
198 vprintf(stdout
, "Verify tape and initialize maps\n");
201 mt
= rmtopen(magtape
, 0);
207 mt
= open(magtape
, O_RDONLY
, 0);
209 fprintf(stderr
, "%s: %s\n", magtape
, strerror(errno
));
215 if (!pipein
&& !bflag
)
217 if (gethead(&spcl
) == FAIL
) {
218 blkcnt
--; /* push back this block */
222 if (gethead(&spcl
) == FAIL
) {
223 fprintf(stderr
, "Tape is not a dump tape\n");
226 fprintf(stderr
, "Converting to new file system format.\n");
229 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
230 endoftapemark
.s_spcl
.c_type
= TS_END
;
231 ip
= (int *)&endoftapemark
;
232 j
= sizeof(union u_spcl
) / sizeof(int);
237 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
239 if (vflag
|| command
== 't' || command
== 'C')
241 if (filesys
== NULL
) {
242 filesys
= spcl
.c_filesys
;
244 dumptime
= spcl
.c_ddate
;
245 dumpdate
= spcl
.c_date
;
246 if (stat(".", &stbuf
) < 0) {
247 fprintf(stderr
, "cannot stat .: %s\n", strerror(errno
));
250 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
<= MAXBSIZE
)
251 fssize
= stbuf
.st_blksize
;
252 if (((fssize
- 1) & fssize
) != 0) {
253 fprintf(stderr
, "bad block size %d\n", fssize
);
256 if (spcl
.c_volume
!= 1) {
257 fprintf(stderr
, "Tape is not volume 1 of the dump\n");
260 if (gethead(&spcl
) == FAIL
) {
261 dprintf(stdout
, "header read failed at %d blocks\n", blksread
);
262 panic("no header after volume mark!\n");
265 if (spcl
.c_type
!= TS_CLRI
) {
266 fprintf(stderr
, "Cannot find file removal list\n");
269 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
270 dprintf(stdout
, "maxino = %d\n", maxino
);
271 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
273 panic("no memory for active inode map\n");
275 curfile
.action
= USING
;
276 getfile(xtrmap
, xtrmapskip
);
277 if (spcl
.c_type
!= TS_BITS
) {
278 fprintf(stderr
, "Cannot find file dump list\n");
281 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
282 if (map
== (char *)NULL
)
283 panic("no memory for file dump list\n");
285 curfile
.action
= USING
;
286 getfile(xtrmap
, xtrmapskip
);
288 * If there may be whiteout entries on the tape, pretend that the
289 * whiteout inode exists, so that the whiteout entries can be
293 SETINO(WINO
, dumpmap
);
297 * Prompt user to load a new dump volume.
298 * "Nextvol" is the next suggested volume to use.
299 * This suggested volume is enforced when doing full
300 * or incremental restores, but can be overrridden by
301 * the user when only extracting a subset of the files.
307 long newvol
, savecnt
, wantnext
, i
;
308 union u_spcl tmpspcl
;
309 # define tmpbuf tmpspcl.s_spcl
318 panic("Changing volumes on pipe input?\n");
326 done(1); /* pipes do not get a second chance */
327 if (command
== 'R' || command
== 'r' || curfile
.action
!= SKIP
) {
334 while (newvol
<= 0) {
335 if (tapesread
== 0) {
336 fprintf(stderr
, "%s%s%s%s%s",
337 "You have not read any tapes yet.\n",
338 "Unless you know which volume your",
339 " file(s) are on you should start\n",
340 "with the last volume and work",
341 " towards the first.\n");
343 fprintf(stderr
, "You have read volumes");
345 for (i
= 1; i
< 32; i
++)
346 if (tapesread
& (1 << i
)) {
347 fprintf(stderr
, "%s%d", buf
, i
);
350 fprintf(stderr
, "\n");
353 fprintf(stderr
, "Specify next volume #: ");
354 (void) fflush(stderr
);
355 (void) fgets(buf
, BUFSIZ
, terminal
);
356 } while (!feof(terminal
) && buf
[0] == '\n');
362 "Volume numbers are positive numerics\n");
365 if (newvol
== volno
) {
366 tapesread
|= 1 << volno
;
370 fprintf(stderr
, "Mount tape volume %d\n", newvol
);
371 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
372 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
373 (void) fflush(stderr
);
374 (void) fgets(buf
, BUFSIZ
, terminal
);
377 if (!strcmp(buf
, "none\n")) {
381 if (buf
[0] != '\n') {
382 (void) strcpy(magtape
, buf
);
383 magtape
[strlen(magtape
) - 1] = '\0';
387 mt
= rmtopen(magtape
, 0);
390 mt
= open(magtape
, O_RDONLY
, 0);
393 fprintf(stderr
, "Cannot open %s\n", magtape
);
401 if (gethead(&tmpbuf
) == FAIL
) {
402 dprintf(stdout
, "header read failed at %d blocks\n", blksread
);
403 fprintf(stderr
, "tape is not dump tape\n");
407 if (tmpbuf
.c_volume
!= volno
) {
408 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
412 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
414 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
415 ctime4(&tmpbuf
.c_date
));
416 fprintf(stderr
, "\twanted: %s", ctime4(&dumpdate
));
418 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
419 ctime(&tmpbuf
.c_date
));
420 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
425 tapesread
|= 1 << volno
;
428 * If continuing from the previous volume, skip over any
429 * blocks read already at the end of the previous volume.
431 * If coming to this volume at random, skip to the beginning
432 * of the next record.
434 dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
435 tpblksread
, tmpbuf
.c_firstrec
);
436 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
438 tpblksread
= tmpbuf
.c_firstrec
;
439 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
441 } else if (tmpbuf
.c_firstrec
> 0 &&
442 tmpbuf
.c_firstrec
< tpblksread
- 1) {
444 * -1 since we've read the volume header
446 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
447 dprintf(stderr
, "Skipping %d duplicate record%s.\n",
448 i
, i
> 1 ? "s" : "");
453 if (curfile
.action
== USING
) {
455 panic("active file into volume 1\n");
459 * Skip up to the beginning of the next record
461 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
462 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
464 (void) gethead(&spcl
);
473 * Handle unexpected EOF.
479 if (gettingfile
&& curfile
.action
== USING
) {
480 printf("Warning: %s %s\n",
481 "End-of-input encountered while extracting", curfile
.name
);
483 curfile
.name
= "<name unknown>";
484 curfile
.action
= UNKNOWN
;
486 curfile
.ino
= maxino
;
494 * handle multiple dumps per tape by skipping forward to the
502 if (dumpnum
== 1 || volno
!= 1)
505 fprintf(stderr
, "Cannot have multiple dumps on pipe input\n");
509 tcom
.mt_count
= dumpnum
- 1;
512 rmtioctl(MTFSF
, dumpnum
- 1);
515 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0)
516 fprintf(stderr
, "ioctl MTFSF: %s\n", strerror(errno
));
523 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
524 fprintf(stdout
, "Dumped from: %s",
525 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
527 fprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
528 fprintf(stdout
, "Dumped from: %s",
529 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
531 if (spcl
.c_host
[0] == '\0')
533 fprintf(stderr
, "Level %d dump of %s on %s:%s\n",
534 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
535 fprintf(stderr
, "Label: %s\n", spcl
.c_label
);
544 struct timeval timep
[2];
553 curfile
.action
= USING
;
555 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
556 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
557 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
558 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
559 #else /* __linux__ */
560 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
561 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
562 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
563 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
564 #endif /* __linux__ */
565 mode
= curfile
.dip
->di_mode
;
566 flags
= curfile
.dip
->di_flags
;
567 switch (mode
& IFMT
) {
570 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
575 vprintf(stdout
, "skipped socket %s\n", name
);
581 ep
= lookupname(name
);
582 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
583 panic("unextracted directory %s\n", name
);
587 vprintf(stdout
, "extract file %s\n", name
);
588 return (genliteraldir(name
, curfile
.ino
));
594 uid
= curfile
.dip
->di_uid
;
595 gid
= curfile
.dip
->di_gid
;
597 getfile(xtrlnkfile
, xtrlnkskip
);
600 "%s: zero length symbolic link (ignored)\n", name
);
604 err
= linkit(lnkbuf
, name
, SYMLINK
);
606 (void) chown(name
, uid
, gid
);
609 return (linkit(lnkbuf
, name
, SYMLINK
));
614 vprintf(stdout
, "extract special file %s\n", name
);
619 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
620 fprintf(stderr
, "%s: cannot create special file: %s\n",
621 name
, strerror(errno
));
625 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
626 (void) chmod(name
, mode
);
628 (void) fsetflags(name
, flags
);
630 (void) chflags(name
, flags
);
637 vprintf(stdout
, "extract fifo %s\n", name
);
642 if (mkfifo(name
, mode
) < 0) {
643 fprintf(stderr
, "%s: cannot create fifo: %s\n",
644 name
, strerror(errno
));
648 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
649 (void) chmod(name
, mode
);
651 (void) fsetflags(name
, flags
);
653 (void) chflags(name
, flags
);
660 vprintf(stdout
, "extract file %s\n", name
);
665 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
667 fprintf(stderr
, "%s: cannot create file: %s\n",
668 name
, strerror(errno
));
672 (void) fchown(ofile
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
673 (void) fchmod(ofile
, mode
);
675 (void) fsetflags(ofile
, flags
);
677 (void) fchflags(ofile
, flags
);
679 getfile(xtrfile
, xtrskip
);
688 * skip over bit maps on the tape
694 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
699 * skip over a file on the tape
705 curfile
.action
= SKIP
;
706 getfile(xtrnull
, xtrnull
);
710 * Extract a file from the tape.
711 * When an allocated block is found it is passed to the fill function;
712 * when an unallocated block (hole) is found, a zeroed buffer is passed
713 * to the skip function.
717 void (*fill
) __P((char *, long));
718 void (*skip
) __P((char *, long));
722 quad_t size
= spcl
.c_dinode
.di_size
;
723 int last_write_was_hole
= 0;
724 long origsize
= size
;
725 static char clearedbuf
[MAXBSIZE
];
726 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
729 if (spcl
.c_type
== TS_END
)
730 panic("ran off end of tape\n");
731 if (spcl
.c_magic
!= NFS_MAGIC
)
732 panic("not at beginning of a file\n");
733 if (!gettingfile
&& setjmp(restart
) != 0)
737 for (i
= 0; i
< spcl
.c_count
; i
++) {
738 if (spcl
.c_addr
[i
]) {
739 readtape(&buf
[curblk
++][0]);
740 if (curblk
== fssize
/ TP_BSIZE
) {
741 (*fill
)((char *)buf
, (long)(size
> TP_BSIZE
?
742 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
744 last_write_was_hole
= 0;
748 (*fill
)((char *)buf
, (long)(size
> TP_BSIZE
?
750 (curblk
- 1) * TP_BSIZE
+ size
));
753 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
755 last_write_was_hole
= 1;
757 if ((size
-= TP_BSIZE
) <= 0) {
758 for (i
++; i
< spcl
.c_count
; i
++)
764 if (gethead(&spcl
) == GOOD
&& size
> 0) {
765 if (spcl
.c_type
== TS_ADDR
)
768 "Missing address (header) block for %s at %d blocks\n",
769 curfile
.name
, blksread
);
772 (*fill
)((char *)buf
, (curblk
* TP_BSIZE
) + size
);
773 last_write_was_hole
= 0;
775 if (last_write_was_hole
) {
776 ftruncate(ofile
, origsize
);
783 * Write out the next block of a file.
793 if (write(ofile
, buf
, (int) size
) == -1) {
795 "write error extracting inode %d, name %s\nwrite: %s\n",
796 curfile
.ino
, curfile
.name
, strerror(errno
));
802 * Skip over a hole in a file.
811 if (lseek(ofile
, size
, SEEK_CUR
) == -1) {
813 "seek error extracting inode %d, name %s\nlseek: %s\n",
814 curfile
.ino
, curfile
.name
, strerror(errno
));
820 * Collect the next block of a symbolic link.
823 xtrlnkfile(buf
, size
)
829 if (pathlen
> MAXPATHLEN
) {
830 fprintf(stderr
, "symbolic link name: %s->%s%s; too long %d\n",
831 curfile
.name
, lnkbuf
, buf
, pathlen
);
834 (void) strcat(lnkbuf
, buf
);
838 * Skip over a hole in a symbolic link (should never happen).
842 xtrlnkskip(buf
, size
)
847 fprintf(stderr
, "unallocated block in symbolic link %s\n",
853 * Collect the next block of a bit map.
861 memmove(map
, buf
, size
);
866 * Skip over a hole in a bit map (should never happen).
870 xtrmapskip(buf
, size
)
875 panic("hole in map\n");
880 * Noop, when an extraction function is not needed.
893 do_cmpfiles(int fd_tape
, int fd_disk
, long size
)
898 static char buf_tape
[BUFSIZ
];
899 static char buf_disk
[BUFSIZ
];
905 if ((n_tape
= read(fd_tape
, buf_tape
, BUFSIZE
)) < 1) {
906 close(fd_tape
), close(fd_disk
);
907 panic("do_cmpfiles: unexpected EOF[1]");
909 if ((n_disk
= read(fd_disk
, buf_disk
, BUFSIZE
)) < 1) {
910 close(fd_tape
), close(fd_disk
);
911 panic("do_cmpfiles: unexpected EOF[2]");
913 if (n_tape
!= n_disk
) {
914 close(fd_tape
), close(fd_disk
);
915 panic("do_cmpfiles: sizes different!");
917 if (memcmp(buf_tape
, buf_disk
, n_tape
) != 0) return (1);
923 /* for debugging compare problems */
924 #undef COMPARE_FAIL_KEEP_FILE
926 #ifdef COMPARE_FAIL_KEEP_FILE
927 /* return true if tapefile should be unlinked after compare */
932 cmpfiles(char *tapefile
, char *diskfile
, struct stat
*sbuf_disk
)
934 struct stat sbuf_tape
;
935 int fd_tape
, fd_disk
;
937 if (stat(tapefile
, &sbuf_tape
) != 0) {
938 panic("Can't lstat tmp file %s: %s\n", tapefile
,
942 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
944 "%s: size changed from %d to %d.\n",
945 diskfile
, sbuf_tape
.st_size
, sbuf_disk
->st_size
);
946 #ifdef COMPARE_FAIL_KEEP_FILE
953 if ((fd_tape
= open(tapefile
, O_RDONLY
)) < 0) {
954 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
956 if ((fd_disk
= open(diskfile
, O_RDONLY
)) < 0) {
958 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
961 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
962 fprintf(stderr
, "%s: tape and disk copies are different\n",
966 #ifdef COMPARE_FAIL_KEEP_FILE
967 /* rename the file to live in /tmp */
968 /* rename `tapefile' to /tmp/<basename of diskfile> */
970 char *p
= strrchr(diskfile
, '/');
971 char newname
[MAXPATHLEN
];
973 panic("can't find / in %s\n", diskfile
);
975 sprintf(newname
, "%s/debug/%s", tmpdir
, p
+ 1);
976 if (rename(tapefile
, newname
)) {
977 panic("rename from %s to %s failed: %s\n",
981 fprintf(stderr
, "*** %s saved to %s\n",
986 /* don't unlink the file (it's not there anymore */
995 #ifdef COMPARE_FAIL_KEEP_FILE
1000 static char tmpfilename
[128];
1006 static char *tmpfile
= NULL
;
1008 struct stat sb
, stemp
;
1011 if ((r
= lstat(name
, &sb
)) != 0) {
1012 fprintf(stderr
, "%s: does not exist (%d, %d).\n", name
, r
, errno
);
1017 curfile
.name
= name
;
1018 mode
= curfile
.dip
->di_mode
;
1020 vprintf(stdout
, "comparing %s (size: %d, mode: 0%o)\n", name
,
1023 if (sb
.st_mode
!= mode
) {
1024 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1025 name
, mode
& 07777, sb
.st_mode
& 07777);
1027 switch (mode
& IFMT
) {
1041 char lbuf
[MAXPATHLEN
+ 1];
1044 if (!(sb
.st_mode
& S_IFLNK
)) {
1045 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1051 getfile(xtrlnkfile
, xtrlnkskip
);
1054 "%s: zero length symbolic link (ignored)\n",
1058 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1059 panic("readlink of %s failed: %s", name
,
1063 if (strcmp(lbuf
, lnkbuf
) != 0) {
1065 "%s: symbolic link changed from %s to %s.\n",
1066 name
, lnkbuf
, lbuf
);
1074 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1075 fprintf(stderr
, "%s: no longer a special file\n",
1081 if (sb
.st_rdev
!= (int)curfile
.dip
->di_rdev
) {
1083 "%s: device changed from %d,%d to %d,%d.\n",
1085 ((int)curfile
.dip
->di_rdev
>> 8) & 0xf,
1086 (int)curfile
.dip
->di_rdev
& 0xf,
1087 (sb
.st_rdev
>> 8) & 0xf,
1094 if (tmpfile
== NULL
) {
1095 /* argument to mktemp() must not be in RO space: */
1096 sprintf(tmpfilename
, "%s/restoreCXXXXXX", tmpdir
);
1097 tmpfile
= mktemp(&tmpfilename
[0]);
1099 if ((stat(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1100 panic("cannot delete tmp file %s: %s\n",
1101 tmpfile
, strerror(errno
));
1103 if ((ofile
= creat(tmpfile
, 0600)) < 0) {
1104 panic("cannot create file temp file %s: %s\n",
1105 name
, strerror(errno
));
1107 getfile(xtrfile
, xtrskip
);
1108 (void) close(ofile
);
1109 #ifdef COMPARE_FAIL_KEEP_FILE
1110 if (cmpfiles(tmpfile
, name
, &sb
))
1113 cmpfiles(tmpfile
, name
, &sb
);
1122 * Read TP_BSIZE blocks from the input.
1123 * Handle read errors, and end of media.
1130 int cnt
, seek_failed
;
1132 if (blkcnt
< numtrec
) {
1133 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], (long)TP_BSIZE
);
1138 for (i
= 0; i
< ntrec
; i
++)
1139 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1142 cnt
= ntrec
* TP_BSIZE
;
1147 i
= rmtread(&tapebuf
[rd
], cnt
);
1150 i
= read(mt
, &tapebuf
[rd
], cnt
);
1152 * Check for mid-tape short read error.
1153 * If found, skip rest of buffer and start with the next.
1155 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1156 dprintf(stdout
, "mid-media short read error.\n");
1160 * Handle partial block read.
1162 if (pipein
&& i
== 0 && rd
> 0)
1164 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1173 * Short read. Process the blocks read.
1175 if (i
% TP_BSIZE
!= 0)
1177 "partial block read: %d should be %d\n",
1178 i
, ntrec
* TP_BSIZE
);
1179 numtrec
= i
/ TP_BSIZE
;
1183 * Handle read error.
1186 fprintf(stderr
, "Tape read error while ");
1187 switch (curfile
.action
) {
1189 fprintf(stderr
, "trying to set up tape\n");
1192 fprintf(stderr
, "trying to resynchronize\n");
1195 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1198 fprintf(stderr
, "skipping over inode %d\n",
1202 if (!yflag
&& !reply("continue"))
1204 i
= ntrec
* TP_BSIZE
;
1205 memset(tapebuf
, 0, i
);
1208 seek_failed
= (rmtseek(i
, 1) < 0);
1211 seek_failed
= (lseek(mt
, i
, SEEK_CUR
) == (off_t
)-1);
1215 "continuation failed: %s\n", strerror(errno
));
1220 * Handle end of tape.
1223 vprintf(stdout
, "End-of-tape encountered\n");
1232 if (rd
% TP_BSIZE
!= 0)
1233 panic("partial block read: %d should be %d\n",
1234 rd
, ntrec
* TP_BSIZE
);
1236 memmove(&tapebuf
[rd
], &endoftapemark
, (long)TP_BSIZE
);
1239 memmove(buf
, &tapebuf
[(blkcnt
++ * TP_BSIZE
)], (long)TP_BSIZE
);
1249 for (i
= 0; i
< ntrec
; i
++)
1250 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1254 i
= rmtread(tapebuf
, ntrec
* TP_BSIZE
);
1257 i
= read(mt
, tapebuf
, ntrec
* TP_BSIZE
);
1260 fprintf(stderr
, "tape read error: %s\n", strerror(errno
));
1263 if (i
% TP_BSIZE
!= 0) {
1264 fprintf(stderr
, "Tape block size (%d) %s (%d)\n",
1265 i
, "is not a multiple of dump block size", TP_BSIZE
);
1268 ntrec
= i
/ TP_BSIZE
;
1270 vprintf(stdout
, "Tape block size is %d\n", ntrec
);
1288 * Read the next block from the tape.
1289 * Check to see if it is one of several vintage headers.
1290 * If it is an old style header, convert it to a new style header.
1291 * If it is not any valid header, return an error.
1303 char dummy
[TP_BSIZE
];
1314 unsigned short odi_mode
;
1331 readtape((char *)buf
);
1332 if (buf
->c_magic
!= NFS_MAGIC
) {
1333 if (swabl(buf
->c_magic
) != NFS_MAGIC
)
1336 vprintf(stdout
, "Note: Doing Byte swapping\n");
1340 if (checksum((int *)buf
) == FAIL
)
1343 swabst((u_char
*)"8l4s31l", (u_char
*)buf
);
1346 readtape((char *)(&u_ospcl
.s_ospcl
));
1347 memset((char *)buf
, 0, (long)TP_BSIZE
);
1348 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
1349 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
1350 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
1351 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
1352 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
1353 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
1354 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
1355 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
1356 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
1357 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
1358 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
1359 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
1360 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
1361 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
1363 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1364 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1365 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1366 #else /* __linux__ */
1367 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
1368 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
1369 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
1370 #endif /* __linux__ */
1371 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
1372 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_addr
, (long)256);
1373 if (u_ospcl
.s_ospcl
.c_magic
!= OFS_MAGIC
||
1374 checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
1376 buf
->c_magic
= NFS_MAGIC
;
1379 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
1380 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
1381 qcvt
.qval
= buf
->c_dinode
.di_size
;
1382 if (qcvt
.val
[0] || qcvt
.val
[1]) {
1383 printf("Note: Doing Quad swapping\n");
1388 qcvt
.qval
= buf
->c_dinode
.di_size
;
1390 qcvt
.val
[1] = qcvt
.val
[0];
1392 buf
->c_dinode
.di_size
= qcvt
.qval
;
1395 switch (buf
->c_type
) {
1400 * Have to patch up missing information in bit map headers
1403 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
1404 for (i
= 0; i
< buf
->c_count
; i
++)
1409 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
1421 panic("gethead: unknown inode type %d\n", buf
->c_type
);
1425 * If we are restoring a filesystem with old format inodes,
1426 * copy the uid/gid to the new location.
1429 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
1430 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
1438 * Check that a header is where it belongs and predict the next header
1442 struct s_spcl
*header
;
1444 static ino_t previno
= 0x7fffffff;
1445 static int prevtype
;
1446 static long predict
;
1449 if (header
->c_type
== TS_TAPE
) {
1450 fprintf(stderr
, "Volume header (%s inode format) ",
1451 oldinofmt
? "old" : "new");
1452 if (header
->c_firstrec
)
1453 fprintf(stderr
, "begins with record %d",
1454 header
->c_firstrec
);
1455 fprintf(stderr
, "\n");
1456 previno
= 0x7fffffff;
1459 if (previno
== 0x7fffffff)
1463 fprintf(stderr
, "Dumped inodes map header");
1466 fprintf(stderr
, "Used inodes map header");
1469 fprintf(stderr
, "File header, ino %d", previno
);
1472 fprintf(stderr
, "File continuation header, ino %d", previno
);
1475 fprintf(stderr
, "End of tape header");
1478 if (predict
!= blksread
- 1)
1479 fprintf(stderr
, "; predicted %d blocks, got %d blocks",
1480 predict
, blksread
- 1);
1481 fprintf(stderr
, "\n");
1484 if (header
->c_type
!= TS_END
)
1485 for (i
= 0; i
< header
->c_count
; i
++)
1486 if (header
->c_addr
[i
] != 0)
1490 prevtype
= header
->c_type
;
1491 previno
= header
->c_inumber
;
1495 * Find an inode header.
1496 * Complain if had to skip, and complain is set.
1500 struct s_spcl
*header
;
1502 static long skipcnt
= 0;
1506 curfile
.name
= "<name unknown>";
1507 curfile
.action
= UNKNOWN
;
1511 if (header
->c_magic
!= NFS_MAGIC
) {
1513 while (gethead(header
) == FAIL
||
1514 header
->c_date
!= dumpdate
)
1517 switch (header
->c_type
) {
1521 * Skip up to the beginning of the next record
1523 for (i
= 0; i
< header
->c_count
; i
++)
1524 if (header
->c_addr
[i
])
1526 while (gethead(header
) == FAIL
||
1527 header
->c_date
!= dumpdate
)
1532 curfile
.dip
= &header
->c_dinode
;
1533 curfile
.ino
= header
->c_inumber
;
1537 curfile
.ino
= maxino
;
1541 curfile
.name
= "<file removal list>";
1545 curfile
.name
= "<file dump list>";
1549 panic("unexpected tape header\n");
1553 panic("unknown tape header type %d\n", spcl
.c_type
);
1557 } while (header
->c_type
== TS_ADDR
);
1559 fprintf(stderr
, "resync restore, skipped %d blocks\n", skipcnt
);
1569 j
= sizeof(union u_spcl
) / sizeof(int);
1576 /* What happens if we want to read restore tapes
1577 for a 16bit int machine??? */
1583 if (i
!= CHECKSUM
) {
1584 fprintf(stderr
, "Checksum error %o, inode %d file %s\n", i
,
1585 curfile
.ino
, curfile
.name
);
1595 #include <varargs.h>
1600 msg(const char *fmt
, ...)
1613 (void)vfprintf(stderr
, fmt
, ap
);
1616 #endif /* RRESTORE */
1620 register u_char
*sp
;
1626 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
1634 register u_char
*sp
;
1640 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
1641 c
= sp
[2]; sp
[2] = sp
[1]; sp
[1] = c
;
1649 register u_char
*cp
, *sp
;
1655 case '0': case '1': case '2': case '3': case '4':
1656 case '5': case '6': case '7': case '8': case '9':
1657 n
= (n
* 10) + (*cp
++ - '0');
1660 case 's': case 'w': case 'h':
1663 sp
= swabshort(sp
, n
);
1669 sp
= swablong(sp
, n
);
1672 default: /* Any other character, like 'b' counts as byte. */
1687 swabst((u_char
*)"l", (u_char
*)&x
);