2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 static const char rcsid[] =
49 "$Id: tape.c,v 1.41 2001/07/18 11:46:31 stelian Exp $";
53 #include <compatlfs.h>
55 #include <compaterr.h>
62 #include <sys/param.h>
70 #ifdef HAVE_EXT2FS_EXT2_FS_H
71 #include <ext2fs/ext2_fs.h>
73 #include <linux/ext2_fs.h>
75 #include <ext2fs/ext2fs.h>
76 #include <bsdcompat.h>
78 #include <ufs/ufs/dinode.h>
79 #endif /* __linux__ */
80 #include <protocols/dumprestore.h>
84 #endif /* HAVE_ZLIB */
88 #include "pathnames.h"
93 static long fssize = MAXBSIZE;
95 static int pipein = 0;
96 static int magtapein = 0; /* input is from magtape */
97 static char magtape[MAXPATHLEN];
98 static char magtapeprefix[MAXPATHLEN];
101 static char *tapebuf; /* input buffer for read */
102 static int bufsize; /* buffer size without prefix */
103 static char *tbufptr = NULL; /* active tape buffer */
105 static char *comprbuf; /* uncompress work buf */
106 static size_t comprlen; /* size including prefix */
108 static union u_spcl endoftapemark;
109 static long blksread; /* blocks read since last header */
110 static long tpblksread = 0; /* TP_BSIZE blocks read */
111 static long tapesread;
112 static sigjmp_buf restart;
113 static int gettingfile = 0; /* restart has a valid frame */
114 static char *host = NULL;
118 static char lnkbuf[MAXPATHLEN + 1];
121 int oldinofmt; /* old inode format conversion required */
122 int Bcvt; /* Swap Bytes (for CCI or sun) */
123 static int Qcvt; /* Swap quads (for sun) */
125 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
127 static void accthdr __P((struct s_spcl *));
128 static int checksum __P((int *));
129 static void findinode __P((struct s_spcl *));
130 static void findtapeblksize __P((void));
131 static int gethead __P((struct s_spcl *));
132 static void readtape __P((char *));
133 static void setdumpnum __P((void));
134 static u_int swabi __P((u_int));
136 static u_long swabl __P((u_long));
138 static u_char *swab64 __P((u_char *, int));
139 static u_char *swab32 __P((u_char *, int));
140 static u_char *swab16 __P((u_char *, int));
141 static void terminateinput __P((void));
142 static void xtrfile __P((char *, size_t));
143 static void xtrlnkfile __P((char *, size_t));
144 static void xtrlnkskip __P((char *, size_t));
145 static void xtrmap __P((char *, size_t));
146 static void xtrmapskip __P((char *, size_t));
147 static void xtrskip __P((char *, size_t));
148 static void setmagtapein __P((void));
151 static void newcomprbuf __P((int));
152 static void readtape_set __P((char *));
153 static void readtape_uncompr __P((char *));
154 static void readtape_comprfile __P((char *));
155 static void readtape_comprtape __P((char *));
156 static char *decompress_tapebuf __P((struct tapebuf *, int));
157 static void msg_read_error __P((char *));
159 static int read_a_block __P((int, char *, size_t, long *));
160 #define PREFIXSIZE sizeof(struct tapebuf)
162 #define COMPARE_ONTHEFLY 1
165 static int ifile; /* input file for compare */
166 static int cmperror; /* compare error */
167 static void xtrcmpfile __P((char *, size_t));
168 static void xtrcmpskip __P((char *, size_t));
171 static int readmapflag;
174 * Set up an input source. This is called from main.c before setup() is.
177 setinput(char *source)
183 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
187 if (strchr(source, ':')) {
189 source = strchr(host, ':');
191 if (rmthost(host) == 0)
195 if (strcmp(source, "-") == 0) {
197 * Since input is coming from a pipe we must establish
198 * our own connection to the terminal.
200 terminal = fopen(_PATH_TTY, "r");
201 if (terminal == NULL) {
202 warn("cannot open %s", _PATH_TTY);
203 terminal = fopen(_PATH_DEVNULL, "r");
204 if (terminal == NULL)
205 err(1, "cannot open %s", _PATH_DEVNULL);
209 setuid(getuid()); /* no longer need or want root privileges */
211 strncpy(magtapeprefix, source, MAXPATHLEN);
212 magtapeprefix[MAXPATHLEN-1] = '\0';
213 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
216 strncpy(magtape, source, MAXPATHLEN);
217 magtape[MAXPATHLEN - 1] = '\0';
221 newtapebuf(long size)
223 static int tapebufsize = -1;
226 bufsize = ntrec * TP_BSIZE;
227 if (size <= tapebufsize)
231 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
233 errx(1, "Cannot allocate space for tape buffer");
239 newcomprbuf(int size)
241 size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
242 if (buf_size <= comprlen)
245 if (comprbuf != NULL)
247 comprbuf = malloc(comprlen);
248 if (comprbuf == NULL)
249 errx(1, "Cannot allocate space for decompress buffer");
251 #endif /* HAVE_ZLIB */
254 * Verify that the tape drive can be accessed and
255 * that it actually is a dump tape.
263 Vprintf(stdout, "Verify tape and initialize maps\n");
266 mt = rmtopen(magtape, 0);
272 mt = OPEN(magtape, O_RDONLY, 0);
274 err(1, "%s", magtape);
280 if (gethead(&spcl) == FAIL) {
281 blkcnt--; /* push back this block */
285 if (gethead(&spcl) == FAIL)
286 errx(1, "Tape is not a dump tape");
287 fprintf(stderr, "Converting to new file system format.\n");
291 fprintf(stderr, "Dump tape is compressed.\n");
295 errx(1,"This restore version doesn't support decompression");
296 #endif /* HAVE_ZLIB */
299 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
300 endoftapemark.s_spcl.c_type = TS_END;
301 ip = (int *)&endoftapemark;
302 j = sizeof(union u_spcl) / sizeof(int);
307 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
309 if (vflag || command == 't' || command == 'C')
311 if (filesys[0] == '\0') {
313 strncpy(filesys, spcl.c_filesys, NAMELEN);
314 filesys[NAMELEN - 1] = '\0';
315 dirptr = strstr(filesys, " (dir");
319 dumptime = spcl.c_ddate;
320 dumpdate = spcl.c_date;
321 if (STAT(".", &stbuf) < 0)
322 err(1, "cannot stat .");
323 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
325 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
326 fssize = stbuf.st_blksize;
327 if (((fssize - 1) & fssize) != 0)
328 errx(1, "bad block size %ld", fssize);
329 if (spcl.c_volume != 1)
330 errx(1, "Tape is not volume 1 of the dump");
331 if (gethead(&spcl) == FAIL) {
332 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
333 panic("no header after volume mark!\n");
336 if (spcl.c_type != TS_CLRI)
337 errx(1, "Cannot find file removal list");
338 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
339 Dprintf(stdout, "maxino = %ld\n", (long)maxino);
340 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
342 errx(1, "no memory for active inode map");
344 curfile.action = USING;
345 getfile(xtrmap, xtrmapskip);
346 if (spcl.c_type != TS_BITS)
347 errx(1, "Cannot find file dump list");
348 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
349 if (map == (char *)NULL)
350 errx(1, "no memory for file dump list");
352 curfile.action = USING;
353 getfile(xtrmap, xtrmapskip);
355 * If there may be whiteout entries on the tape, pretend that the
356 * whiteout inode exists, so that the whiteout entries can be
360 SETINO(WINO, dumpmap);
364 * Prompt user to load a new dump volume.
365 * "Nextvol" is the next suggested volume to use.
366 * This suggested volume is enforced when doing full
367 * or incremental restores, but can be overridden by
368 * the user when only extracting a subset of the files.
373 long newvol = 0, savecnt = 0, wantnext = 0, i;
374 union u_spcl tmpspcl;
375 # define tmpbuf tmpspcl.s_spcl
385 panic("Changing volumes on pipe input?\n");
393 exit(1); /* pipes do not get a second chance */
394 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
401 while (newvol <= 0) {
402 if (tapesread == 0) {
403 fprintf(stderr, "%s%s%s%s%s",
404 "You have not read any tapes yet.\n",
405 "Unless you know which volume your",
406 " file(s) are on you should start\n",
407 "with the last volume and work",
408 " towards the first.\n");
410 fprintf(stderr, "You have read volumes");
412 for (i = 1; i < 32; i++)
413 if (tapesread & (1 << i)) {
414 fprintf(stderr, "%s%ld", buf, (long)i);
417 fprintf(stderr, "\n");
420 fprintf(stderr, "Specify next volume #: ");
421 (void) fflush(stderr);
422 (void) fgets(buf, TP_BSIZE, terminal);
423 } while (!feof(terminal) && buf[0] == '\n');
429 "Volume numbers are positive numerics\n");
432 if (newvol == volno) {
433 tapesread |= 1 << volno;
438 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
439 magtape[MAXPATHLEN - 1] = '\0';
441 if (!Mflag || haderror) {
443 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
444 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
445 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
446 (void) fflush(stderr);
447 (void) fgets(buf, TP_BSIZE, terminal);
450 if (!strcmp(buf, "none\n")) {
454 if (buf[0] != '\n') {
456 (void) strncpy(magtape, buf, sizeof(magtape));
457 magtape[sizeof(magtape) - 1] = '\0';
458 if ((pos = strchr(magtape, '\n')))
459 magtape[pos - magtape] = '\0';
464 mt = rmtopen(magtape, 0);
467 mt = OPEN(magtape, O_RDONLY, 0);
470 fprintf(stderr, "Cannot open %s\n", magtape);
480 if (gethead(&tmpbuf) == FAIL) {
481 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
482 fprintf(stderr, "tape is not dump tape\n");
487 if (tmpbuf.c_volume != volno) {
488 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
493 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
494 fprintf(stderr, "Wrong dump date\n\tgot: %s",
495 ctime4(&tmpbuf.c_date));
496 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
501 tapesread |= 1 << volno;
504 * If continuing from the previous volume, skip over any
505 * blocks read already at the end of the previous volume.
507 * If coming to this volume at random, skip to the beginning
508 * of the next record.
510 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
511 tpblksread, (long)tmpbuf.c_firstrec);
512 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
514 tpblksread = tmpbuf.c_firstrec;
515 for (i = tmpbuf.c_count; i > 0; i--)
517 } else if (tmpbuf.c_firstrec > 0 &&
518 tmpbuf.c_firstrec < tpblksread - 1) {
520 * -1 since we've read the volume header
522 i = tpblksread - tmpbuf.c_firstrec - 1;
523 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
524 (long)i, i > 1 ? "s" : "");
529 if (curfile.action == USING) {
531 panic("active file into volume 1\n");
535 * Skip up to the beginning of the next record
537 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
538 for (i = tmpbuf.c_count; i > 0; i--)
540 (void) gethead(&spcl);
544 siglongjmp(restart, 1);
549 * Handle unexpected EOF.
555 if (gettingfile && curfile.action == USING) {
556 printf("Warning: %s %s\n",
557 "End-of-input encountered while extracting", curfile.name);
559 curfile.name = "<name unknown>";
560 curfile.action = UNKNOWN;
562 curfile.ino = maxino;
565 siglongjmp(restart, 1);
570 * handle multiple dumps per tape by skipping forward to the
578 if (dumpnum == 1 || volno != 1)
581 errx(1, "Cannot have multiple dumps on pipe input");
583 tcom.mt_count = dumpnum - 1;
586 rmtioctl(MTFSF, dumpnum - 1);
589 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
596 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
597 fprintf(stdout, "Dumped from: %s",
598 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
599 if (spcl.c_host[0] == '\0')
601 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
602 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
603 fprintf(stdout, "Label: %s\n", spcl.c_label);
607 extractfile(char *name)
611 struct timeval timep[2];
615 curfile.action = USING;
617 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
618 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
619 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
620 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
621 #else /* __linux__ */
622 timep[0].tv_sec = curfile.dip->di_atime;
623 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
624 timep[1].tv_sec = curfile.dip->di_mtime;
625 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
626 #endif /* __linux__ */
627 mode = curfile.dip->di_mode;
628 flags = curfile.dip->di_flags;
629 switch (mode & IFMT) {
632 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
637 Vprintf(stdout, "skipped socket %s\n", name);
643 ep = lookupname(name);
644 if (ep == NULL || ep->e_flags & EXTRACT)
645 panic("unextracted directory %s\n", name);
649 Vprintf(stdout, "extract file %s\n", name);
650 return (genliteraldir(name, curfile.ino));
655 uid_t luid = curfile.dip->di_uid;
656 gid_t lgid = curfile.dip->di_gid;
660 getfile(xtrlnkfile, xtrlnkskip);
663 "%s: zero length symbolic link (ignored)\n", name);
666 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
669 (void) lchown(name, luid, lgid);
675 Vprintf(stdout, "extract fifo %s\n", name);
682 if (mkfifo(name, mode) < 0) {
683 warn("%s: cannot create fifo", name);
687 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
688 (void) chmod(name, mode);
691 (void) fsetflags(name, flags);
693 (void) chflags(name, flags);
701 Vprintf(stdout, "extract special file %s\n", name);
708 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
709 warn("%s: cannot create special file", name);
713 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
714 (void) chmod(name, mode);
718 warn("%s: fsetflags called on a special file", name);
719 (void) fsetflags(name, flags);
722 (void) chflags(name, flags);
729 Vprintf(stdout, "extract file %s\n", name);
736 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
738 warn("%s: cannot create file", name);
742 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
743 (void) fchmod(ofile, mode);
746 (void) setflags(ofile, flags);
748 (void) fchflags(ofile, flags);
750 getfile(xtrfile, xtrskip);
759 * skip over bit maps on the tape
765 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
770 * skip over a file on the tape
776 curfile.action = SKIP;
777 getfile(xtrnull, xtrnull);
781 * Extract a file from the tape.
782 * When an allocated block is found it is passed to the fill function;
783 * when an unallocated block (hole) is found, a zeroed buffer is passed
784 * to the skip function.
787 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
790 volatile int curblk = 0;
791 volatile quad_t size = spcl.c_dinode.di_size;
792 volatile int last_write_was_hole = 0;
793 quad_t origsize = size;
794 static char clearedbuf[MAXBSIZE];
795 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
798 if (spcl.c_type == TS_END)
799 panic("ran off end of tape\n");
800 if (spcl.c_magic != NFS_MAGIC)
801 panic("not at beginning of a file\n");
802 if (!gettingfile && setjmp(restart) != 0)
806 for (i = 0; i < spcl.c_count; i++) {
807 if (readmapflag || spcl.c_addr[i]) {
808 readtape(&buf[curblk++][0]);
809 if (curblk == fssize / TP_BSIZE) {
810 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
811 fssize : (curblk - 1) * TP_BSIZE + size));
813 last_write_was_hole = 0;
817 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
819 (curblk - 1) * TP_BSIZE + size));
822 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
824 last_write_was_hole = 1;
826 if ((size -= TP_BSIZE) <= 0) {
827 for (i++; i < spcl.c_count; i++)
828 if (readmapflag || spcl.c_addr[i])
833 if (gethead(&spcl) == GOOD && size > 0) {
834 if (spcl.c_type == TS_ADDR)
837 "Missing address (header) block for %s at %ld blocks\n",
838 curfile.name, (long)blksread);
841 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
842 last_write_was_hole = 0;
845 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
847 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
848 (*skip)(clearedbuf, skp);
851 last_write_was_hole = 1;
853 if (last_write_was_hole) {
854 FTRUNCATE(ofile, origsize);
861 * Write out the next block of a file.
864 xtrfile(char *buf, size_t size)
869 if (write(ofile, buf, (int) size) == -1)
870 err(1, "write error extracting inode %lu, name %s\nwrite",
871 (unsigned long)curfile.ino, curfile.name);
875 * Skip over a hole in a file.
879 xtrskip(char *buf, size_t size)
882 if (LSEEK(ofile, (off_t)size, SEEK_CUR) == -1)
883 err(1, "seek error extracting inode %lu, name %s\nlseek",
884 (unsigned long)curfile.ino, curfile.name);
888 * Collect the next block of a symbolic link.
891 xtrlnkfile(char *buf, size_t size)
895 if (pathlen > MAXPATHLEN)
896 errx(1, "symbolic link name: %s->%s%s; too long %d",
897 curfile.name, lnkbuf, buf, pathlen);
898 (void) strcat(lnkbuf, buf);
902 * Skip over a hole in a symbolic link (should never happen).
906 xtrlnkskip(char *buf, size_t size)
909 errx(1, "unallocated block in symbolic link %s", curfile.name);
913 * Collect the next block of a bit map.
916 xtrmap(char *buf, size_t size)
919 memmove(map, buf, size);
924 * Skip over a hole in a bit map (should never happen).
928 xtrmapskip(char *buf, size_t size)
931 panic("hole in map\n");
936 * Noop, when an extraction function is not needed.
940 xtrnull(char *buf, size_t size)
948 * Compare the next block of a file.
951 xtrcmpfile(char *buf, size_t size)
953 static char cmpbuf[MAXBSIZE];
958 if (read(ifile, cmpbuf, size) != size) {
959 fprintf(stderr, "%s: size has changed.\n",
965 if (memcmp(buf, cmpbuf, size) != 0) {
966 fprintf(stderr, "%s: tape and disk copies are different\n",
974 * Skip over a hole in a file.
977 xtrcmpskip(char *buf, size_t size)
979 static char cmpbuf[MAXBSIZE];
985 if (read(ifile, cmpbuf, size) != size) {
986 fprintf(stderr, "%s: size has changed.\n",
992 for (i = 0; i < size; ++i)
993 if (cmpbuf[i] != '\0') {
994 fprintf(stderr, "%s: tape and disk copies are different\n",
1000 #endif /* COMPARE_ONTHEFLY */
1002 #if !COMPARE_ONTHEFLY
1004 do_cmpfiles(int fd_tape, int fd_disk, long size)
1006 static char buf_tape[BUFSIZ];
1007 static char buf_disk[BUFSIZ];
1012 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1013 close(fd_tape), close(fd_disk);
1014 panic("do_cmpfiles: unexpected EOF[1]");
1016 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1017 close(fd_tape), close(fd_disk);
1018 panic("do_cmpfiles: unexpected EOF[2]");
1020 if (n_tape != n_disk) {
1021 close(fd_tape), close(fd_disk);
1022 panic("do_cmpfiles: sizes different!");
1024 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1030 /* for debugging compare problems */
1031 #undef COMPARE_FAIL_KEEP_FILE
1034 #ifdef COMPARE_FAIL_KEEP_FILE
1035 /* return true if tapefile should be unlinked after compare */
1040 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1042 struct STAT sbuf_tape;
1043 int fd_tape, fd_disk;
1045 if (STAT(tapefile, &sbuf_tape) != 0) {
1046 panic("Can't lstat tmp file %s: %s\n", tapefile,
1051 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1053 "%s: size changed from %ld to %ld.\n",
1054 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1056 #ifdef COMPARE_FAIL_KEEP_FILE
1063 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1064 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1067 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1069 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1073 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1074 fprintf(stderr, "%s: tape and disk copies are different\n",
1079 #ifdef COMPARE_FAIL_KEEP_FILE
1080 /* rename the file to live in /tmp */
1081 /* rename `tapefile' to /tmp/<basename of diskfile> */
1083 char *p = strrchr(diskfile, '/');
1084 char newname[MAXPATHLEN];
1086 panic("can't find / in %s\n", diskfile);
1088 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1089 if (rename(tapefile, newname)) {
1090 panic("rename from %s to %s failed: %s\n",
1094 fprintf(stderr, "*** %s saved to %s\n",
1099 /* don't unlink the file (it's not there anymore */
1108 #ifdef COMPARE_FAIL_KEEP_FILE
1112 #endif /* !COMPARE_ONTHEFLY */
1114 #if !COMPARE_ONTHEFLY
1115 static char tmpfilename[MAXPATHLEN];
1119 comparefile(char *name)
1124 #if !COMPARE_ONTHEFLY
1125 static char *tmpfile = NULL;
1129 if ((r = LSTAT(name, &sb)) != 0) {
1130 warn("%s: does not exist (%d)", name, r);
1136 curfile.name = name;
1137 curfile.action = USING;
1138 mode = curfile.dip->di_mode;
1140 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1141 (long)sb.st_size, mode);
1143 if (sb.st_mode != mode) {
1144 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1145 name, mode & 07777, sb.st_mode & 07777);
1148 switch (mode & IFMT) {
1162 char lbuf[MAXPATHLEN + 1];
1165 if (!(sb.st_mode & S_IFLNK)) {
1166 fprintf(stderr, "%s: is no longer a symbolic link\n",
1173 getfile(xtrlnkfile, xtrlnkskip);
1176 "%s: zero length symbolic link (ignored)\n",
1181 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1182 panic("readlink of %s failed: %s", name,
1187 if (strcmp(lbuf, lnkbuf) != 0) {
1189 "%s: symbolic link changed from %s to %s.\n",
1190 name, lnkbuf, lbuf);
1199 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1200 fprintf(stderr, "%s: no longer a special file\n",
1207 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1209 "%s: device changed from %d,%d to %d,%d.\n",
1211 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1212 (int)curfile.dip->di_rdev & 0xff,
1213 ((int)sb.st_rdev >> 8) & 0xff,
1214 (int)sb.st_rdev & 0xff);
1221 #if COMPARE_ONTHEFLY
1222 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1223 panic("Can't open %s: %s\n", name, strerror(errno));
1229 getfile(xtrcmpfile, xtrcmpskip);
1232 if (read(ifile, &c, 1) != 0) {
1233 fprintf(stderr, "%s: size has changed.\n",
1243 if (tmpfile == NULL) {
1244 /* argument to mktemp() must not be in RO space: */
1245 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1246 tmpfile = mktemp(&tmpfilename[0]);
1248 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1249 panic("cannot delete tmp file %s: %s\n",
1250 tmpfile, strerror(errno));
1252 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1253 panic("cannot create file temp file %s: %s\n",
1254 name, strerror(errno));
1256 getfile(xtrfile, xtrskip);
1257 (void) close(ofile);
1258 #ifdef COMPARE_FAIL_KEEP_FILE
1259 if (cmpfiles(tmpfile, name, &sb))
1262 cmpfiles(tmpfile, name, &sb);
1265 #endif /* COMPARE_ONTHEFLY */
1272 static void (*readtape_func)(char *) = readtape_set;
1275 * Read TP_BSIZE blocks from the input.
1276 * Handle read errors, and end of media.
1277 * Decompress compressed blocks.
1282 (*readtape_func)(buf); /* call the actual processing routine */
1286 * Set function pointer for readtape() routine. zflag and magtapein must
1287 * be correctly set before the first call to readtape().
1290 readtape_set(char *buf)
1293 readtape_func = readtape_uncompr;
1297 readtape_func = readtape_comprtape;
1299 readtape_func = readtape_comprfile;
1304 #endif /* HAVE_ZLIB */
1307 * This is the original readtape(), it's used for reading uncompressed input.
1308 * Read TP_BSIZE blocks from the input.
1309 * Handle read errors, and end of media.
1313 readtape_uncompr(char *buf)
1318 ssize_t rd, newvol, i;
1319 int cnt, seek_failed;
1321 if (blkcnt < numtrec) {
1322 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1328 for (i = 0; i < ntrec; i++)
1329 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1332 cnt = ntrec * TP_BSIZE;
1337 i = rmtread(&tapebuf[rd], cnt);
1340 i = read(mt, &tapebuf[rd], cnt);
1343 * Check for mid-tape short read error.
1344 * If found, skip rest of buffer and start with the next.
1346 if (!pipein && numtrec < ntrec && i > 0) {
1347 Dprintf(stdout, "mid-media short read error.\n");
1351 * Handle partial block read.
1353 if (pipein && i == 0 && rd > 0)
1355 else if (i > 0 && i != ntrec * TP_BSIZE) {
1364 * Short read. Process the blocks read.
1366 if (i % TP_BSIZE != 0)
1368 "partial block read: %ld should be %ld\n",
1369 (long)i, ntrec * TP_BSIZE);
1370 numtrec = i / TP_BSIZE;
1374 * Handle read error.
1377 fprintf(stderr, "Tape read error while ");
1378 switch (curfile.action) {
1380 fprintf(stderr, "trying to set up tape\n");
1383 fprintf(stderr, "trying to resynchronize\n");
1386 fprintf(stderr, "restoring %s\n", curfile.name);
1389 fprintf(stderr, "skipping over inode %lu\n",
1390 (unsigned long)curfile.ino);
1393 if (!yflag && !reply("continue"))
1395 i = ntrec * TP_BSIZE;
1396 memset(tapebuf, 0, (size_t)i);
1399 seek_failed = (rmtseek(i, 1) < 0);
1402 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (off_t)-1);
1405 warn("continuation failed");
1406 if (!yflag && !reply("assume end-of-tape and continue"))
1412 * Handle end of tape.
1415 Vprintf(stdout, "End-of-tape encountered\n");
1424 if (rd % TP_BSIZE != 0)
1425 panic("partial block read: %d should be %d\n",
1426 rd, ntrec * TP_BSIZE);
1428 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1431 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1439 * Read a compressed format block from a file or pipe and uncompress it.
1440 * Attempt to handle read errors, and end of file.
1443 readtape_comprfile(char *buf)
1445 long rl, size, i, ret;
1447 struct tapebuf *tpb;
1449 if (blkcnt < numtrec) {
1450 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1455 /* need to read the next block */
1457 for (i = 0; i < ntrec; i++)
1458 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1460 tpb = (struct tapebuf *) tapebuf;
1462 /* read the block prefix */
1463 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1469 if (size > bufsize) {
1470 /* something's wrong */
1471 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1474 tpb->length = bufsize;
1476 ret = read_a_block(mt, tpb->buf, size, &rl);
1480 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1481 if (tbufptr == NULL) {
1482 msg_read_error("File decompression error while");
1483 if (!yflag && !reply("continue"))
1485 memset(tapebuf, 0, bufsize);
1490 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1496 /* Errors while reading from a file or pipe are catastrophic. Since
1497 * there are no block boundaries, it's impossible to bypass the
1498 * block in error and find the start of the next block.
1501 /* It's possible to have multiple input files using -M
1502 * and -f file1,file2...
1504 Vprintf(stdout, "End-of-File encountered\n");
1514 msg_read_error("Read error while");
1515 /* if (!yflag && !reply("continue")) */
1520 * Read compressed data from a tape and uncompress it.
1521 * Handle read errors, and end of media.
1522 * Since a tape consists of separate physical blocks, we try
1523 * to recover from errors by repositioning the tape to the next
1527 readtape_comprtape(char *buf)
1531 struct tapebuf *tpb;
1534 if (blkcnt < numtrec) {
1535 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1540 /* need to read the next block */
1542 for (i = 0; i < ntrec; i++)
1543 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1545 tpb = (struct tapebuf *) tapebuf;
1547 /* read the block */
1548 size = bufsize + PREFIXSIZE;
1549 ret = read_a_block(mt, tapebuf, size, &rl);
1553 tbufptr = decompress_tapebuf(tpb, rl);
1554 if (tbufptr == NULL) {
1555 msg_read_error("Tape decompression error while");
1556 if (!yflag && !reply("continue"))
1558 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1564 /* Handle errors: EOT switches to the next volume, other errors
1565 * attempt to position the tape to the next block.
1568 Vprintf(stdout, "End-of-tape encountered\n");
1577 msg_read_error("Tape read error while");
1578 if (!yflag && !reply("continue"))
1580 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1585 rl = rmtioctl(MTFSR, 1);
1591 rl = ioctl(mt, MTIOCTOP, &tcom);
1595 warn("continuation failed");
1596 if (!yflag && !reply("assume end-of-tape and continue"))
1598 ret = 0; /* end of tape */
1604 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1610 * Decompress a struct tapebuf into a buffer. readsize is the size read
1611 * from the tape/file and is used for error messages. Returns a pointer
1612 * to the location of the uncompressed buffer or NULL on errors.
1613 * Adjust numtrec and complain for a short block.
1616 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
1618 /* If zflag is on, all blocks have a struct tapebuf prefix */
1619 /* zflag gets set in setup() from the dump header */
1620 int cresult, blocklen;
1621 unsigned long worklen;
1622 char *output = NULL,*reason = NULL, *lengtherr = NULL;
1624 /* build a length error message */
1625 blocklen = tpbin->length;
1626 if (readsize < blocklen + PREFIXSIZE)
1627 lengtherr = "short";
1629 if (readsize > blocklen + PREFIXSIZE)
1634 if (tpbin->compressed) {
1635 /* uncompress whatever we read, if it fails, complain later */
1636 cresult = uncompress(comprbuf, &worklen, tpbin->buf, blocklen);
1640 output = tpbin->buf;
1645 numtrec = worklen / TP_BSIZE;
1646 if (worklen % TP_BSIZE != 0)
1647 reason = "length mismatch";
1650 reason = "not enough memory";
1653 reason = "buffer too small";
1656 reason = "data error";
1663 fprintf(stderr, "%s compressed block: %d expected: %d\n",
1664 lengtherr, readsize, tpbin->length + PREFIXSIZE);
1665 fprintf(stderr, "decompression error, block %ld: %s\n",
1666 tpblksread+1, reason);
1667 if (cresult != Z_OK) output = NULL;
1673 * Print an error message for a read error.
1674 * This was exteracted from the original readtape().
1677 msg_read_error(char *m)
1679 switch (curfile.action) {
1681 fprintf(stderr, "%s trying to set up tape\n", m);
1684 fprintf(stderr, "%s trying to resynchronize\n", m);
1687 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
1690 fprintf(stderr, "%s skipping over inode %lu\n", m,
1691 (unsigned long)curfile.ino);
1695 #endif /* HAVE_ZLIB */
1698 * Read the first block and get the blocksize from it. Test
1699 * for a compressed dump tape/file. setup() will make the final
1700 * determination by checking the compressed flag if gethead()
1701 * finds a valid header. The test here is necessary to offset the buffer
1702 * by the size of the compressed prefix. zflag is set here so that
1703 * readtape_set can set the correct function pointer for readtape().
1704 * Note that the first block of each tape/file is not compressed
1705 * and does not have a prefix.
1708 findtapeblksize(void)
1712 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1713 struct s_spcl *spclpt = (struct s_spcl *) tapebuf;
1715 for (i = 0; i < ntrec; i++)
1716 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1720 * For a pipe or file, read in the first record. For a tape, read
1723 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
1725 if (read_a_block(mt, tapebuf, len, &i) <= 0)
1726 errx(1, "Tape read error on first record");
1729 * If the input is from a file or a pipe, we read TP_BSIZE
1730 * bytes looking for a dump header. If the dump is compressed
1731 * we need to read in the rest of the block, as determined
1732 * by c_ntrec in the dump header. The first block of the
1733 * dump is not compressed and does not have a prefix.
1736 if (spclpt->c_type == TS_TAPE
1737 && spclpt->c_flags & DR_COMPRESSED) {
1738 /* It's a compressed dump file, read in the */
1739 /* rest of the block based on spclpt->c_ntrec. */
1740 if (spclpt->c_ntrec > ntrec)
1741 errx(1, "Tape blocksize is too large, use "
1742 "\'-b %d\' ", spclpt->c_ntrec);
1743 ntrec = spclpt->c_ntrec;
1744 len = (ntrec - 1) * TP_BSIZE;
1748 /* read in the rest of the block based on bufsize */
1749 len = bufsize - TP_BSIZE;
1751 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1752 || (i != len && i % TP_BSIZE != 0))
1753 errx(1,"Error reading dump file header");
1756 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1758 } /* if (!magtapein) */
1761 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1762 * If the value of ntrec is too large, we read less than
1763 * what we asked for; adjust the value of ntrec and test for
1764 * a compressed dump tape.
1767 if (i % TP_BSIZE != 0) {
1768 /* may be old format compressed dump tape with a prefix */
1769 spclpt = (struct s_spcl *) tpb->buf;
1770 if (i % TP_BSIZE == PREFIXSIZE
1771 && tpb->compressed == 0
1772 && spclpt->c_type == TS_TAPE
1773 && spclpt->c_flags & DR_COMPRESSED) {
1776 if (tpb->length > bufsize)
1777 errx(1, "Tape blocksize is too large, use "
1778 "\'-b %d\' ", tpb->length / TP_BSIZE);
1781 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1784 ntrec = i / TP_BSIZE;
1785 if (spclpt->c_type == TS_TAPE) {
1786 if (spclpt->c_flags & DR_COMPRESSED)
1788 if (spclpt->c_ntrec > ntrec)
1789 errx(1, "Tape blocksize is too large, use "
1790 "\'-b %d\' ", spclpt->c_ntrec);
1793 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1797 * Read a block of data handling all of the messy details.
1799 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1807 i = rmtread(buf, size);
1810 i = read(fd, buf, size);
1813 break; /* EOD or error */
1816 break; /* block at a time for mt */
1819 *lengthread = len - size;
1838 setmagtapein(void) {
1839 struct mtget mt_stat;
1840 static int done = 0;
1845 /* need to know if input is really from a tape */
1848 magtapein = rmtioctl(MTNOP, 1) != -1;
1851 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
1854 Vprintf(stdout,"Input is from %s\n", magtapein? "tape": "file/pipe");
1858 * Read the next block from the tape.
1859 * Check to see if it is one of several vintage headers.
1860 * If it is an old style header, convert it to a new style header.
1861 * If it is not any valid header, return an error.
1864 gethead(struct s_spcl *buf)
1872 char dummy[TP_BSIZE];
1879 u_int16_t c_inumber;
1884 u_int16_t odi_nlink;
1900 readtape((char *)buf);
1901 if (buf->c_magic != NFS_MAGIC) {
1902 if (swabi(buf->c_magic) != NFS_MAGIC)
1905 Vprintf(stdout, "Note: Doing Byte swapping\n");
1909 if (checksum((int *)buf) == FAIL)
1912 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1915 readtape((char *)(&u_ospcl.s_ospcl));
1916 memset((char *)buf, 0, (long)TP_BSIZE);
1917 buf->c_type = u_ospcl.s_ospcl.c_type;
1918 buf->c_date = u_ospcl.s_ospcl.c_date;
1919 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1920 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1921 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1922 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1923 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1924 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1925 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1926 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1927 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1928 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1929 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1930 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1932 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1933 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1934 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1935 #else /* __linux__ */
1936 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1937 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1938 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1939 #endif /* __linux__ */
1940 buf->c_count = u_ospcl.s_ospcl.c_count;
1941 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1942 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1943 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1945 buf->c_magic = NFS_MAGIC;
1948 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1949 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1950 qcvt.qval = buf->c_dinode.di_size;
1951 if (qcvt.val[0] || qcvt.val[1]) {
1952 printf("Note: Doing Quad swapping\n");
1957 qcvt.qval = buf->c_dinode.di_size;
1959 qcvt.val[1] = qcvt.val[0];
1961 buf->c_dinode.di_size = qcvt.qval;
1965 switch (buf->c_type) {
1970 * Have to patch up missing information in bit map headers
1973 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1974 if (buf->c_count > TP_NINDIR)
1977 for (i = 0; i < buf->c_count; i++)
1982 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1994 panic("gethead: unknown inode type %d\n", buf->c_type);
1998 * If we are restoring a filesystem with old format inodes,
1999 * copy the uid/gid to the new location.
2002 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2003 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2011 * Check that a header is where it belongs and predict the next header
2014 accthdr(struct s_spcl *header)
2016 static dump_ino_t previno = 0x7fffffff;
2017 static int prevtype;
2018 static long predict;
2021 if (header->c_type == TS_TAPE) {
2022 fprintf(stderr, "Volume header (%s inode format) ",
2023 oldinofmt ? "old" : "new");
2024 if (header->c_firstrec)
2025 fprintf(stderr, "begins with record %d",
2026 header->c_firstrec);
2027 fprintf(stderr, "\n");
2028 previno = 0x7fffffff;
2031 if (previno == 0x7fffffff)
2035 fprintf(stderr, "Dumped inodes map header");
2038 fprintf(stderr, "Used inodes map header");
2041 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2044 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2047 fprintf(stderr, "End of tape header");
2050 if (predict != blksread - 1)
2051 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2052 predict, blksread - 1);
2053 fprintf(stderr, "\n");
2056 if (header->c_type != TS_END)
2057 for (i = 0; i < header->c_count; i++)
2058 if (readmapflag || header->c_addr[i] != 0)
2062 prevtype = header->c_type;
2063 previno = header->c_inumber;
2067 * Find an inode header.
2068 * Complain if had to skip, and complain is set.
2071 findinode(struct s_spcl *header)
2073 static long skipcnt = 0;
2077 curfile.name = "<name unknown>";
2078 curfile.action = UNKNOWN;
2082 if (header->c_magic != NFS_MAGIC) {
2084 while (gethead(header) == FAIL ||
2085 header->c_date != dumpdate)
2088 switch (header->c_type) {
2092 * Skip up to the beginning of the next record
2094 for (i = 0; i < header->c_count; i++)
2095 if (header->c_addr[i])
2097 while (gethead(header) == FAIL ||
2098 header->c_date != dumpdate)
2103 curfile.dip = &header->c_dinode;
2104 curfile.ino = header->c_inumber;
2108 curfile.ino = maxino;
2112 curfile.name = "<file removal list>";
2116 curfile.name = "<file dump list>";
2120 panic("unexpected tape header\n");
2124 panic("unknown tape header type %d\n", spcl.c_type);
2128 } while (header->c_type == TS_ADDR);
2133 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2143 j = sizeof(union u_spcl) / sizeof(int);
2150 /* What happens if we want to read restore tapes
2151 for a 16bit int machine??? */
2157 if (i != CHECKSUM) {
2158 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2159 (unsigned long)curfile.ino, curfile.name);
2169 #include <varargs.h>
2174 msg(const char *fmt, ...)
2187 (void)vfprintf(stderr, fmt, ap);
2190 #endif /* RRESTORE */
2193 swab16(u_char *sp, int n)
2198 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2205 swab32(u_char *sp, int n)
2210 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2211 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2218 swab64(u_char *sp, int n)
2223 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2224 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2225 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2226 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2233 swabst(u_char *cp, u_char *sp)
2239 case '0': case '1': case '2': case '3': case '4':
2240 case '5': case '6': case '7': case '8': case '9':
2241 n = (n * 10) + (*cp++ - '0');
2244 case 's': case 'w': case 'h':
2262 default: /* Any other character, like 'b' counts as byte. */
2276 swabst((u_char *)"i", (u_char *)&x);
2284 swabst((u_char *)"l", (u_char *)&x);
2291 * get the current position of the tape
2294 GetTapePos(long *pos)
2299 if (ioctl(mt, MTIOCPOS, pos) == -1) {
2301 fprintf(stdout, "[%ld] error: %d (getting tapepos: %ld)\n",
2302 (unsigned long)getpid(), err, *pos);
2308 typedef struct mt_pos {
2311 } MTPosRec, *MTPosPtr;
2314 * go to specified position on tape
2317 GotoTapePos(long pos)
2324 if (ioctl(mt, MTIOCTOP, &buf) == -1) {
2326 fprintf(stdout, "[%ld] error: %d (setting tapepos: %ld)\n",
2327 (unsigned long)getpid(), err, pos);
2334 * read next data from tape to re-sync
2337 ReReadFromTape(void)
2341 if (gethead(&spcl) == FAIL) {
2343 fprintf(stdout, "DEBUG 1 gethead failed\n");
2351 RequestVol(long tnum)
2356 #endif /* USE_QFA */