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.38 2001/05/12 11:36:12 stelian Exp $";
53 #include <compatlfs.h>
55 #include <compaterr.h>
62 #include <sys/param.h>
70 #include <linux/ext2_fs.h>
71 #include <ext2fs/ext2fs.h>
72 #include <bsdcompat.h>
74 #include <ufs/ufs/dinode.h>
75 #endif /* __linux__ */
76 #include <protocols/dumprestore.h>
80 #endif /* HAVE_ZLIB */
84 #include "pathnames.h"
89 static long fssize = MAXBSIZE;
91 static int pipein = 0;
92 static int magtapein = 0; /* input is from magtape */
93 static char magtape[MAXPATHLEN];
94 static char magtapeprefix[MAXPATHLEN];
97 static char *tapebuf; /* input buffer for read */
98 static int bufsize; /* buffer size without prefix */
99 static char *tbufptr = NULL; /* active tape buffer */
101 static char *comprbuf; /* uncompress work buf */
102 static size_t comprlen; /* size including prefix */
104 static union u_spcl endoftapemark;
105 static long blksread; /* blocks read since last header */
106 static long tpblksread = 0; /* TP_BSIZE blocks read */
107 static long tapesread;
108 static sigjmp_buf restart;
109 static int gettingfile = 0; /* restart has a valid frame */
110 static char *host = NULL;
114 static char lnkbuf[MAXPATHLEN + 1];
117 int oldinofmt; /* old inode format conversion required */
118 int Bcvt; /* Swap Bytes (for CCI or sun) */
119 static int Qcvt; /* Swap quads (for sun) */
121 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
123 static void accthdr __P((struct s_spcl *));
124 static int checksum __P((int *));
125 static void findinode __P((struct s_spcl *));
126 static void findtapeblksize __P((void));
127 static int gethead __P((struct s_spcl *));
128 static void readtape __P((char *));
129 static void setdumpnum __P((void));
130 static u_int swabi __P((u_int));
132 static u_long swabl __P((u_long));
134 static u_char *swab64 __P((u_char *, int));
135 static u_char *swab32 __P((u_char *, int));
136 static u_char *swab16 __P((u_char *, int));
137 static void terminateinput __P((void));
138 static void xtrfile __P((char *, size_t));
139 static void xtrlnkfile __P((char *, size_t));
140 static void xtrlnkskip __P((char *, size_t));
141 static void xtrmap __P((char *, size_t));
142 static void xtrmapskip __P((char *, size_t));
143 static void xtrskip __P((char *, size_t));
144 static void setmagtapein __P((void));
147 static void newcomprbuf __P((int));
148 static void readtape_set __P((char *));
149 static void readtape_uncompr __P((char *));
150 static void readtape_comprfile __P((char *));
151 static void readtape_comprtape __P((char *));
152 static char *decompress_tapebuf __P((struct tapebuf *, int));
153 static void msg_read_error __P((char *));
155 static int read_a_block __P((int, char *, size_t, long *));
156 #define PREFIXSIZE sizeof(struct tapebuf)
158 #define COMPARE_ONTHEFLY 1
161 static int ifile; /* input file for compare */
162 static int cmperror; /* compare error */
163 static void xtrcmpfile __P((char *, size_t));
164 static void xtrcmpskip __P((char *, size_t));
167 static int readmapflag;
170 * Set up an input source. This is called from main.c before setup() is.
173 setinput(char *source)
179 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
183 if (strchr(source, ':')) {
185 source = strchr(host, ':');
187 if (rmthost(host) == 0)
191 if (strcmp(source, "-") == 0) {
193 * Since input is coming from a pipe we must establish
194 * our own connection to the terminal.
196 terminal = fopen(_PATH_TTY, "r");
197 if (terminal == NULL) {
198 warn("cannot open %s", _PATH_TTY);
199 terminal = fopen(_PATH_DEVNULL, "r");
200 if (terminal == NULL)
201 err(1, "cannot open %s", _PATH_DEVNULL);
205 setuid(getuid()); /* no longer need or want root privileges */
207 strncpy(magtapeprefix, source, MAXPATHLEN);
208 magtapeprefix[MAXPATHLEN-1] = '\0';
209 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
212 strncpy(magtape, source, MAXPATHLEN);
213 magtape[MAXPATHLEN - 1] = '\0';
217 newtapebuf(long size)
219 static int tapebufsize = -1;
222 bufsize = ntrec * TP_BSIZE;
223 if (size <= tapebufsize)
227 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
229 errx(1, "Cannot allocate space for tape buffer");
235 newcomprbuf(int size)
237 size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
238 if (buf_size <= comprlen)
241 if (comprbuf != NULL)
243 comprbuf = malloc(comprlen);
244 if (comprbuf == NULL)
245 errx(1, "Cannot allocate space for decompress buffer");
247 #endif /* HAVE_ZLIB */
250 * Verify that the tape drive can be accessed and
251 * that it actually is a dump tape.
259 Vprintf(stdout, "Verify tape and initialize maps\n");
262 mt = rmtopen(magtape, 0);
268 mt = OPEN(magtape, O_RDONLY, 0);
270 err(1, "%s", magtape);
276 if (gethead(&spcl) == FAIL) {
277 blkcnt--; /* push back this block */
281 if (gethead(&spcl) == FAIL)
282 errx(1, "Tape is not a dump tape");
283 fprintf(stderr, "Converting to new file system format.\n");
287 fprintf(stderr, "Dump tape is compressed.\n");
291 errx(1,"This restore version doesn't support decompression");
292 #endif /* HAVE_ZLIB */
295 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
296 endoftapemark.s_spcl.c_type = TS_END;
297 ip = (int *)&endoftapemark;
298 j = sizeof(union u_spcl) / sizeof(int);
303 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
305 if (vflag || command == 't' || command == 'C')
307 if (filesys[0] == '\0') {
309 strncpy(filesys, spcl.c_filesys, NAMELEN);
310 filesys[NAMELEN - 1] = '\0';
311 dirptr = strstr(filesys, " (dir");
315 dumptime = spcl.c_ddate;
316 dumpdate = spcl.c_date;
317 if (STAT(".", &stbuf) < 0)
318 err(1, "cannot stat .");
319 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
321 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
322 fssize = stbuf.st_blksize;
323 if (((fssize - 1) & fssize) != 0)
324 errx(1, "bad block size %ld", fssize);
325 if (spcl.c_volume != 1)
326 errx(1, "Tape is not volume 1 of the dump");
327 if (gethead(&spcl) == FAIL) {
328 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
329 panic("no header after volume mark!\n");
332 if (spcl.c_type != TS_CLRI)
333 errx(1, "Cannot find file removal list");
334 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
335 Dprintf(stdout, "maxino = %ld\n", (long)maxino);
336 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
338 errx(1, "no memory for active inode map");
340 curfile.action = USING;
341 getfile(xtrmap, xtrmapskip);
342 if (spcl.c_type != TS_BITS)
343 errx(1, "Cannot find file dump list");
344 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
345 if (map == (char *)NULL)
346 errx(1, "no memory for file dump list");
348 curfile.action = USING;
349 getfile(xtrmap, xtrmapskip);
351 * If there may be whiteout entries on the tape, pretend that the
352 * whiteout inode exists, so that the whiteout entries can be
356 SETINO(WINO, dumpmap);
360 * Prompt user to load a new dump volume.
361 * "Nextvol" is the next suggested volume to use.
362 * This suggested volume is enforced when doing full
363 * or incremental restores, but can be overridden by
364 * the user when only extracting a subset of the files.
369 long newvol = 0, savecnt = 0, wantnext = 0, i;
370 union u_spcl tmpspcl;
371 # define tmpbuf tmpspcl.s_spcl
381 panic("Changing volumes on pipe input?\n");
389 exit(1); /* pipes do not get a second chance */
390 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
397 while (newvol <= 0) {
398 if (tapesread == 0) {
399 fprintf(stderr, "%s%s%s%s%s",
400 "You have not read any tapes yet.\n",
401 "Unless you know which volume your",
402 " file(s) are on you should start\n",
403 "with the last volume and work",
404 " towards the first.\n");
406 fprintf(stderr, "You have read volumes");
408 for (i = 1; i < 32; i++)
409 if (tapesread & (1 << i)) {
410 fprintf(stderr, "%s%ld", buf, (long)i);
413 fprintf(stderr, "\n");
416 fprintf(stderr, "Specify next volume #: ");
417 (void) fflush(stderr);
418 (void) fgets(buf, TP_BSIZE, terminal);
419 } while (!feof(terminal) && buf[0] == '\n');
425 "Volume numbers are positive numerics\n");
428 if (newvol == volno) {
429 tapesread |= 1 << volno;
434 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
435 magtape[MAXPATHLEN - 1] = '\0';
437 if (!Mflag || haderror) {
439 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
440 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
441 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
442 (void) fflush(stderr);
443 (void) fgets(buf, TP_BSIZE, terminal);
446 if (!strcmp(buf, "none\n")) {
450 if (buf[0] != '\n') {
451 (void) strcpy(magtape, buf);
452 magtape[strlen(magtape) - 1] = '\0';
457 mt = rmtopen(magtape, 0);
460 mt = OPEN(magtape, O_RDONLY, 0);
463 fprintf(stderr, "Cannot open %s\n", magtape);
473 if (gethead(&tmpbuf) == FAIL) {
474 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
475 fprintf(stderr, "tape is not dump tape\n");
480 if (tmpbuf.c_volume != volno) {
481 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
486 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
487 fprintf(stderr, "Wrong dump date\n\tgot: %s",
488 ctime4(&tmpbuf.c_date));
489 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
494 tapesread |= 1 << volno;
497 * If continuing from the previous volume, skip over any
498 * blocks read already at the end of the previous volume.
500 * If coming to this volume at random, skip to the beginning
501 * of the next record.
503 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
504 tpblksread, (long)tmpbuf.c_firstrec);
505 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
507 tpblksread = tmpbuf.c_firstrec;
508 for (i = tmpbuf.c_count; i > 0; i--)
510 } else if (tmpbuf.c_firstrec > 0 &&
511 tmpbuf.c_firstrec < tpblksread - 1) {
513 * -1 since we've read the volume header
515 i = tpblksread - tmpbuf.c_firstrec - 1;
516 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
517 (long)i, i > 1 ? "s" : "");
522 if (curfile.action == USING) {
524 panic("active file into volume 1\n");
528 * Skip up to the beginning of the next record
530 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
531 for (i = tmpbuf.c_count; i > 0; i--)
533 (void) gethead(&spcl);
537 siglongjmp(restart, 1);
542 * Handle unexpected EOF.
548 if (gettingfile && curfile.action == USING) {
549 printf("Warning: %s %s\n",
550 "End-of-input encountered while extracting", curfile.name);
552 curfile.name = "<name unknown>";
553 curfile.action = UNKNOWN;
555 curfile.ino = maxino;
558 siglongjmp(restart, 1);
563 * handle multiple dumps per tape by skipping forward to the
571 if (dumpnum == 1 || volno != 1)
574 errx(1, "Cannot have multiple dumps on pipe input");
576 tcom.mt_count = dumpnum - 1;
579 rmtioctl(MTFSF, dumpnum - 1);
582 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
589 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
590 fprintf(stdout, "Dumped from: %s",
591 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
592 if (spcl.c_host[0] == '\0')
594 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
595 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
596 fprintf(stdout, "Label: %s\n", spcl.c_label);
600 extractfile(char *name)
604 struct timeval timep[2];
608 curfile.action = USING;
610 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
611 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
612 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
613 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
614 #else /* __linux__ */
615 timep[0].tv_sec = curfile.dip->di_atime;
616 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
617 timep[1].tv_sec = curfile.dip->di_mtime;
618 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
619 #endif /* __linux__ */
620 mode = curfile.dip->di_mode;
621 flags = curfile.dip->di_flags;
622 switch (mode & IFMT) {
625 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
630 Vprintf(stdout, "skipped socket %s\n", name);
636 ep = lookupname(name);
637 if (ep == NULL || ep->e_flags & EXTRACT)
638 panic("unextracted directory %s\n", name);
642 Vprintf(stdout, "extract file %s\n", name);
643 return (genliteraldir(name, curfile.ino));
648 uid_t luid = curfile.dip->di_uid;
649 gid_t lgid = curfile.dip->di_gid;
653 getfile(xtrlnkfile, xtrlnkskip);
656 "%s: zero length symbolic link (ignored)\n", name);
659 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
662 (void) lchown(name, luid, lgid);
668 Vprintf(stdout, "extract fifo %s\n", name);
675 if (mkfifo(name, mode) < 0) {
676 warn("%s: cannot create fifo", name);
680 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
681 (void) chmod(name, mode);
684 (void) fsetflags(name, flags);
686 (void) chflags(name, flags);
694 Vprintf(stdout, "extract special file %s\n", name);
701 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
702 warn("%s: cannot create special file", name);
706 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
707 (void) chmod(name, mode);
711 warn("%s: fsetflags called on a special file", name);
712 (void) fsetflags(name, flags);
715 (void) chflags(name, flags);
722 Vprintf(stdout, "extract file %s\n", name);
729 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
731 warn("%s: cannot create file", name);
735 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
736 (void) fchmod(ofile, mode);
739 (void) setflags(ofile, flags);
741 (void) fchflags(ofile, flags);
743 getfile(xtrfile, xtrskip);
752 * skip over bit maps on the tape
758 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
763 * skip over a file on the tape
769 curfile.action = SKIP;
770 getfile(xtrnull, xtrnull);
774 * Extract a file from the tape.
775 * When an allocated block is found it is passed to the fill function;
776 * when an unallocated block (hole) is found, a zeroed buffer is passed
777 * to the skip function.
780 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
783 volatile int curblk = 0;
784 volatile quad_t size = spcl.c_dinode.di_size;
785 volatile int last_write_was_hole = 0;
786 quad_t origsize = size;
787 static char clearedbuf[MAXBSIZE];
788 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
791 if (spcl.c_type == TS_END)
792 panic("ran off end of tape\n");
793 if (spcl.c_magic != NFS_MAGIC)
794 panic("not at beginning of a file\n");
795 if (!gettingfile && setjmp(restart) != 0)
799 for (i = 0; i < spcl.c_count; i++) {
800 if (readmapflag || spcl.c_addr[i]) {
801 readtape(&buf[curblk++][0]);
802 if (curblk == fssize / TP_BSIZE) {
803 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
804 fssize : (curblk - 1) * TP_BSIZE + size));
806 last_write_was_hole = 0;
810 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
812 (curblk - 1) * TP_BSIZE + size));
815 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
817 last_write_was_hole = 1;
819 if ((size -= TP_BSIZE) <= 0) {
820 for (i++; i < spcl.c_count; i++)
821 if (readmapflag || spcl.c_addr[i])
826 if (gethead(&spcl) == GOOD && size > 0) {
827 if (spcl.c_type == TS_ADDR)
830 "Missing address (header) block for %s at %ld blocks\n",
831 curfile.name, (long)blksread);
834 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
835 last_write_was_hole = 0;
838 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
840 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
841 (*skip)(clearedbuf, skp);
844 last_write_was_hole = 1;
846 if (last_write_was_hole) {
847 FTRUNCATE(ofile, origsize);
854 * Write out the next block of a file.
857 xtrfile(char *buf, size_t size)
862 if (write(ofile, buf, (int) size) == -1)
863 err(1, "write error extracting inode %lu, name %s\nwrite",
864 (unsigned long)curfile.ino, curfile.name);
868 * Skip over a hole in a file.
872 xtrskip(char *buf, size_t size)
875 if (LSEEK(ofile, (off_t)size, SEEK_CUR) == -1)
876 err(1, "seek error extracting inode %lu, name %s\nlseek",
877 (unsigned long)curfile.ino, curfile.name);
881 * Collect the next block of a symbolic link.
884 xtrlnkfile(char *buf, size_t size)
888 if (pathlen > MAXPATHLEN)
889 errx(1, "symbolic link name: %s->%s%s; too long %d",
890 curfile.name, lnkbuf, buf, pathlen);
891 (void) strcat(lnkbuf, buf);
895 * Skip over a hole in a symbolic link (should never happen).
899 xtrlnkskip(char *buf, size_t size)
902 errx(1, "unallocated block in symbolic link %s", curfile.name);
906 * Collect the next block of a bit map.
909 xtrmap(char *buf, size_t size)
912 memmove(map, buf, size);
917 * Skip over a hole in a bit map (should never happen).
921 xtrmapskip(char *buf, size_t size)
924 panic("hole in map\n");
929 * Noop, when an extraction function is not needed.
933 xtrnull(char *buf, size_t size)
941 * Compare the next block of a file.
944 xtrcmpfile(char *buf, size_t size)
946 static char cmpbuf[MAXBSIZE];
951 if (read(ifile, cmpbuf, size) != size) {
952 fprintf(stderr, "%s: size has changed.\n",
958 if (memcmp(buf, cmpbuf, size) != 0) {
959 fprintf(stderr, "%s: tape and disk copies are different\n",
967 * Skip over a hole in a file.
970 xtrcmpskip(char *buf, size_t size)
972 static char cmpbuf[MAXBSIZE];
978 if (read(ifile, cmpbuf, size) != size) {
979 fprintf(stderr, "%s: size has changed.\n",
985 for (i = 0; i < size; ++i)
986 if (cmpbuf[i] != '\0') {
987 fprintf(stderr, "%s: tape and disk copies are different\n",
993 #endif /* COMPARE_ONTHEFLY */
995 #if !COMPARE_ONTHEFLY
997 do_cmpfiles(int fd_tape, int fd_disk, long size)
999 static char buf_tape[BUFSIZ];
1000 static char buf_disk[BUFSIZ];
1005 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1006 close(fd_tape), close(fd_disk);
1007 panic("do_cmpfiles: unexpected EOF[1]");
1009 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1010 close(fd_tape), close(fd_disk);
1011 panic("do_cmpfiles: unexpected EOF[2]");
1013 if (n_tape != n_disk) {
1014 close(fd_tape), close(fd_disk);
1015 panic("do_cmpfiles: sizes different!");
1017 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1023 /* for debugging compare problems */
1024 #undef COMPARE_FAIL_KEEP_FILE
1027 #ifdef COMPARE_FAIL_KEEP_FILE
1028 /* return true if tapefile should be unlinked after compare */
1033 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1035 struct STAT sbuf_tape;
1036 int fd_tape, fd_disk;
1038 if (STAT(tapefile, &sbuf_tape) != 0) {
1039 panic("Can't lstat tmp file %s: %s\n", tapefile,
1044 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1046 "%s: size changed from %ld to %ld.\n",
1047 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1049 #ifdef COMPARE_FAIL_KEEP_FILE
1056 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1057 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1060 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1062 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1066 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1067 fprintf(stderr, "%s: tape and disk copies are different\n",
1072 #ifdef COMPARE_FAIL_KEEP_FILE
1073 /* rename the file to live in /tmp */
1074 /* rename `tapefile' to /tmp/<basename of diskfile> */
1076 char *p = strrchr(diskfile, '/');
1077 char newname[MAXPATHLEN];
1079 panic("can't find / in %s\n", diskfile);
1081 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1082 if (rename(tapefile, newname)) {
1083 panic("rename from %s to %s failed: %s\n",
1087 fprintf(stderr, "*** %s saved to %s\n",
1092 /* don't unlink the file (it's not there anymore */
1101 #ifdef COMPARE_FAIL_KEEP_FILE
1105 #endif /* !COMPARE_ONTHEFLY */
1107 #if !COMPARE_ONTHEFLY
1108 static char tmpfilename[MAXPATHLEN];
1112 comparefile(char *name)
1117 #if !COMPARE_ONTHEFLY
1118 static char *tmpfile = NULL;
1122 if ((r = LSTAT(name, &sb)) != 0) {
1123 warn("%s: does not exist (%d)", name, r);
1129 curfile.name = name;
1130 curfile.action = USING;
1131 mode = curfile.dip->di_mode;
1133 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1134 (long)sb.st_size, mode);
1136 if (sb.st_mode != mode) {
1137 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1138 name, mode & 07777, sb.st_mode & 07777);
1141 switch (mode & IFMT) {
1155 char lbuf[MAXPATHLEN + 1];
1158 if (!(sb.st_mode & S_IFLNK)) {
1159 fprintf(stderr, "%s: is no longer a symbolic link\n",
1166 getfile(xtrlnkfile, xtrlnkskip);
1169 "%s: zero length symbolic link (ignored)\n",
1174 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1175 panic("readlink of %s failed: %s", name,
1180 if (strcmp(lbuf, lnkbuf) != 0) {
1182 "%s: symbolic link changed from %s to %s.\n",
1183 name, lnkbuf, lbuf);
1192 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1193 fprintf(stderr, "%s: no longer a special file\n",
1200 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1202 "%s: device changed from %d,%d to %d,%d.\n",
1204 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1205 (int)curfile.dip->di_rdev & 0xff,
1206 ((int)sb.st_rdev >> 8) & 0xff,
1207 (int)sb.st_rdev & 0xff);
1214 #if COMPARE_ONTHEFLY
1215 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1216 panic("Can't open %s: %s\n", name, strerror(errno));
1222 getfile(xtrcmpfile, xtrcmpskip);
1225 if (read(ifile, &c, 1) != 0) {
1226 fprintf(stderr, "%s: size has changed.\n",
1236 if (tmpfile == NULL) {
1237 /* argument to mktemp() must not be in RO space: */
1238 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1239 tmpfile = mktemp(&tmpfilename[0]);
1241 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1242 panic("cannot delete tmp file %s: %s\n",
1243 tmpfile, strerror(errno));
1245 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1246 panic("cannot create file temp file %s: %s\n",
1247 name, strerror(errno));
1249 getfile(xtrfile, xtrskip);
1250 (void) close(ofile);
1251 #ifdef COMPARE_FAIL_KEEP_FILE
1252 if (cmpfiles(tmpfile, name, &sb))
1255 cmpfiles(tmpfile, name, &sb);
1258 #endif /* COMPARE_ONTHEFLY */
1265 static void (*readtape_func)(char *) = readtape_set;
1268 * Read TP_BSIZE blocks from the input.
1269 * Handle read errors, and end of media.
1270 * Decompress compressed blocks.
1275 (*readtape_func)(buf); /* call the actual processing routine */
1279 * Set function pointer for readtape() routine. zflag and magtapein must
1280 * be correctly set before the first call to readtape().
1283 readtape_set(char *buf)
1286 readtape_func = readtape_uncompr;
1290 readtape_func = readtape_comprtape;
1292 readtape_func = readtape_comprfile;
1297 #endif /* HAVE_ZLIB */
1300 * This is the original readtape(), it's used for reading uncompressed input.
1301 * Read TP_BSIZE blocks from the input.
1302 * Handle read errors, and end of media.
1306 readtape_uncompr(char *buf)
1311 ssize_t rd, newvol, i;
1312 int cnt, seek_failed;
1314 if (blkcnt < numtrec) {
1315 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1321 for (i = 0; i < ntrec; i++)
1322 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1325 cnt = ntrec * TP_BSIZE;
1330 i = rmtread(&tapebuf[rd], cnt);
1333 i = read(mt, &tapebuf[rd], cnt);
1336 * Check for mid-tape short read error.
1337 * If found, skip rest of buffer and start with the next.
1339 if (!pipein && numtrec < ntrec && i > 0) {
1340 Dprintf(stdout, "mid-media short read error.\n");
1344 * Handle partial block read.
1346 if (pipein && i == 0 && rd > 0)
1348 else if (i > 0 && i != ntrec * TP_BSIZE) {
1357 * Short read. Process the blocks read.
1359 if (i % TP_BSIZE != 0)
1361 "partial block read: %ld should be %ld\n",
1362 (long)i, ntrec * TP_BSIZE);
1363 numtrec = i / TP_BSIZE;
1367 * Handle read error.
1370 fprintf(stderr, "Tape read error while ");
1371 switch (curfile.action) {
1373 fprintf(stderr, "trying to set up tape\n");
1376 fprintf(stderr, "trying to resynchronize\n");
1379 fprintf(stderr, "restoring %s\n", curfile.name);
1382 fprintf(stderr, "skipping over inode %lu\n",
1383 (unsigned long)curfile.ino);
1386 if (!yflag && !reply("continue"))
1388 i = ntrec * TP_BSIZE;
1389 memset(tapebuf, 0, (size_t)i);
1392 seek_failed = (rmtseek(i, 1) < 0);
1395 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (off_t)-1);
1398 warn("continuation failed");
1399 if (!yflag && !reply("assume end-of-tape and continue"))
1405 * Handle end of tape.
1408 Vprintf(stdout, "End-of-tape encountered\n");
1417 if (rd % TP_BSIZE != 0)
1418 panic("partial block read: %d should be %d\n",
1419 rd, ntrec * TP_BSIZE);
1421 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1424 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1432 * Read a compressed format block from a file or pipe and uncompress it.
1433 * Attempt to handle read errors, and end of file.
1436 readtape_comprfile(char *buf)
1438 long rl, size, i, ret;
1440 struct tapebuf *tpb;
1442 if (blkcnt < numtrec) {
1443 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1448 /* need to read the next block */
1450 for (i = 0; i < ntrec; i++)
1451 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1453 tpb = (struct tapebuf *) tapebuf;
1455 /* read the block prefix */
1456 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1462 if (size > bufsize) {
1463 /* something's wrong */
1464 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1467 tpb->length = bufsize;
1469 ret = read_a_block(mt, tpb->buf, size, &rl);
1473 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1474 if (tbufptr == NULL) {
1475 msg_read_error("File decompression error while");
1476 if (!yflag && !reply("continue"))
1478 memset(tapebuf, 0, bufsize);
1483 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1489 /* Errors while reading from a file or pipe are catastrophic. Since
1490 * there are no block boundaries, it's impossible to bypass the
1491 * block in error and find the start of the next block.
1494 /* It's possible to have multiple input files using -M
1495 * and -f file1,file2...
1497 Vprintf(stdout, "End-of-File encountered\n");
1507 msg_read_error("Read error while");
1508 /* if (!yflag && !reply("continue")) */
1513 * Read compressed data from a tape and uncompress it.
1514 * Handle read errors, and end of media.
1515 * Since a tape consists of separate physical blocks, we try
1516 * to recover from errors by repositioning the tape to the next
1520 readtape_comprtape(char *buf)
1524 struct tapebuf *tpb;
1527 if (blkcnt < numtrec) {
1528 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1533 /* need to read the next block */
1535 for (i = 0; i < ntrec; i++)
1536 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1538 tpb = (struct tapebuf *) tapebuf;
1540 /* read the block */
1541 size = bufsize + PREFIXSIZE;
1542 ret = read_a_block(mt, tapebuf, size, &rl);
1546 tbufptr = decompress_tapebuf(tpb, rl);
1547 if (tbufptr == NULL) {
1548 msg_read_error("Tape decompression error while");
1549 if (!yflag && !reply("continue"))
1551 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1557 /* Handle errors: EOT switches to the next volume, other errors
1558 * attempt to position the tape to the next block.
1561 Vprintf(stdout, "End-of-tape encountered\n");
1570 msg_read_error("Tape read error while");
1571 if (!yflag && !reply("continue"))
1573 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1578 rl = rmtioctl(MTFSR, 1);
1584 rl = ioctl(mt, MTIOCTOP, &tcom);
1588 warn("continuation failed");
1589 if (!yflag && !reply("assume end-of-tape and continue"))
1591 ret = 0; /* end of tape */
1597 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1603 * Decompress a struct tapebuf into a buffer. readsize is the size read
1604 * from the tape/file and is used for error messages. Returns a pointer
1605 * to the location of the uncompressed buffer or NULL on errors.
1606 * Adjust numtrec and complain for a short block.
1609 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
1611 /* If zflag is on, all blocks have a struct tapebuf prefix */
1612 /* zflag gets set in setup() from the dump header */
1613 int cresult, blocklen;
1614 unsigned long worklen;
1615 char *output = NULL,*reason = NULL, *lengtherr = NULL;
1617 /* build a length error message */
1618 blocklen = tpbin->length;
1619 if (readsize < blocklen + PREFIXSIZE)
1620 lengtherr = "short";
1622 if (readsize > blocklen + PREFIXSIZE)
1627 if (tpbin->compressed) {
1628 /* uncompress whatever we read, if it fails, complain later */
1629 cresult = uncompress(comprbuf, &worklen, tpbin->buf, blocklen);
1633 output = tpbin->buf;
1638 numtrec = worklen / TP_BSIZE;
1639 if (worklen % TP_BSIZE != 0)
1640 reason = "length mismatch";
1643 reason = "not enough memory";
1646 reason = "buffer too small";
1649 reason = "data error";
1656 fprintf(stderr, "%s compressed block: %d expected: %d\n",
1657 lengtherr, readsize, tpbin->length + PREFIXSIZE);
1658 fprintf(stderr, "decompression error, block %ld: %s\n",
1659 tpblksread+1, reason);
1660 if (cresult != Z_OK) output = NULL;
1666 * Print an error message for a read error.
1667 * This was exteracted from the original readtape().
1670 msg_read_error(char *m)
1672 switch (curfile.action) {
1674 fprintf(stderr, "%s trying to set up tape\n", m);
1677 fprintf(stderr, "%s trying to resynchronize\n", m);
1680 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
1683 fprintf(stderr, "%s skipping over inode %lu\n", m,
1684 (unsigned long)curfile.ino);
1688 #endif /* HAVE_ZLIB */
1691 * Read the first block and get the blocksize from it. Test
1692 * for a compressed dump tape/file. setup() will make the final
1693 * determination by checking the compressed flag if gethead()
1694 * finds a valid header. The test here is necessary to offset the buffer
1695 * by the size of the compressed prefix. zflag is set here so that
1696 * readtape_set can set the correct function pointer for readtape().
1697 * Note that the first block of each tape/file is not compressed
1698 * and does not have a prefix.
1701 findtapeblksize(void)
1705 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1706 struct s_spcl *spclpt = (struct s_spcl *) tapebuf;
1708 for (i = 0; i < ntrec; i++)
1709 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1713 * For a pipe or file, read in the first record. For a tape, read
1716 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
1718 if (read_a_block(mt, tapebuf, len, &i) <= 0)
1719 errx(1, "Tape read error on first record");
1722 * If the input is from a file or a pipe, we read TP_BSIZE
1723 * bytes looking for a dump header. If the dump is compressed
1724 * we need to read in the rest of the block, as determined
1725 * by c_ntrec in the dump header. The first block of the
1726 * dump is not compressed and does not have a prefix.
1729 if (spclpt->c_type == TS_TAPE
1730 && spclpt->c_flags & DR_COMPRESSED) {
1731 /* It's a compressed dump file, read in the */
1732 /* rest of the block based on spclpt->c_ntrec. */
1733 if (spclpt->c_ntrec > ntrec)
1734 errx(1, "Tape blocksize is too large, use "
1735 "\'-b %d\' ", spclpt->c_ntrec);
1736 ntrec = spclpt->c_ntrec;
1737 len = (ntrec - 1) * TP_BSIZE;
1741 /* read in the rest of the block based on bufsize */
1742 len = bufsize - TP_BSIZE;
1744 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1745 || (i != len && i % TP_BSIZE != 0))
1746 errx(1,"Error reading dump file header");
1749 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1751 } /* if (!magtapein) */
1754 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1755 * If the value of ntrec is too large, we read less than
1756 * what we asked for; adjust the value of ntrec and test for
1757 * a compressed dump tape.
1760 if (i % TP_BSIZE != 0) {
1761 /* may be old format compressed dump tape with a prefix */
1762 spclpt = (struct s_spcl *) tpb->buf;
1763 if (i % TP_BSIZE == PREFIXSIZE
1764 && tpb->compressed == 0
1765 && spclpt->c_type == TS_TAPE
1766 && spclpt->c_flags & DR_COMPRESSED) {
1769 if (tpb->length > bufsize)
1770 errx(1, "Tape blocksize is too large, use "
1771 "\'-b %d\' ", tpb->length / TP_BSIZE);
1774 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1777 ntrec = i / TP_BSIZE;
1778 if (spclpt->c_type == TS_TAPE) {
1779 if (spclpt->c_flags & DR_COMPRESSED)
1781 if (spclpt->c_ntrec > ntrec)
1782 errx(1, "Tape blocksize is too large, use "
1783 "\'-b %d\' ", spclpt->c_ntrec);
1786 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1790 * Read a block of data handling all of the messy details.
1792 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1800 i = rmtread(buf, size);
1803 i = read(fd, buf, size);
1806 break; /* EOD or error */
1809 break; /* block at a time for mt */
1812 *lengthread = len - size;
1831 setmagtapein(void) {
1832 struct mtget mt_stat;
1833 static int done = 0;
1838 /* need to know if input is really from a tape */
1841 magtapein = rmtioctl(MTNOP, 1) != -1;
1844 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
1847 Vprintf(stdout,"Input is from %s\n", magtapein? "tape": "file/pipe");
1851 * Read the next block from the tape.
1852 * Check to see if it is one of several vintage headers.
1853 * If it is an old style header, convert it to a new style header.
1854 * If it is not any valid header, return an error.
1857 gethead(struct s_spcl *buf)
1865 char dummy[TP_BSIZE];
1872 u_int16_t c_inumber;
1877 u_int16_t odi_nlink;
1893 readtape((char *)buf);
1894 if (buf->c_magic != NFS_MAGIC) {
1895 if (swabi(buf->c_magic) != NFS_MAGIC)
1898 Vprintf(stdout, "Note: Doing Byte swapping\n");
1902 if (checksum((int *)buf) == FAIL)
1905 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1908 readtape((char *)(&u_ospcl.s_ospcl));
1909 memset((char *)buf, 0, (long)TP_BSIZE);
1910 buf->c_type = u_ospcl.s_ospcl.c_type;
1911 buf->c_date = u_ospcl.s_ospcl.c_date;
1912 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1913 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1914 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1915 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1916 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1917 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1918 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1919 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1920 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1921 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1922 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1923 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1925 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1926 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1927 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1928 #else /* __linux__ */
1929 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1930 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1931 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1932 #endif /* __linux__ */
1933 buf->c_count = u_ospcl.s_ospcl.c_count;
1934 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1935 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1936 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1938 buf->c_magic = NFS_MAGIC;
1941 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1942 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1943 qcvt.qval = buf->c_dinode.di_size;
1944 if (qcvt.val[0] || qcvt.val[1]) {
1945 printf("Note: Doing Quad swapping\n");
1950 qcvt.qval = buf->c_dinode.di_size;
1952 qcvt.val[1] = qcvt.val[0];
1954 buf->c_dinode.di_size = qcvt.qval;
1958 switch (buf->c_type) {
1963 * Have to patch up missing information in bit map headers
1966 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1967 if (buf->c_count > TP_NINDIR)
1970 for (i = 0; i < buf->c_count; i++)
1975 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1987 panic("gethead: unknown inode type %d\n", buf->c_type);
1991 * If we are restoring a filesystem with old format inodes,
1992 * copy the uid/gid to the new location.
1995 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1996 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2004 * Check that a header is where it belongs and predict the next header
2007 accthdr(struct s_spcl *header)
2009 static dump_ino_t previno = 0x7fffffff;
2010 static int prevtype;
2011 static long predict;
2014 if (header->c_type == TS_TAPE) {
2015 fprintf(stderr, "Volume header (%s inode format) ",
2016 oldinofmt ? "old" : "new");
2017 if (header->c_firstrec)
2018 fprintf(stderr, "begins with record %d",
2019 header->c_firstrec);
2020 fprintf(stderr, "\n");
2021 previno = 0x7fffffff;
2024 if (previno == 0x7fffffff)
2028 fprintf(stderr, "Dumped inodes map header");
2031 fprintf(stderr, "Used inodes map header");
2034 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2037 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2040 fprintf(stderr, "End of tape header");
2043 if (predict != blksread - 1)
2044 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2045 predict, blksread - 1);
2046 fprintf(stderr, "\n");
2049 if (header->c_type != TS_END)
2050 for (i = 0; i < header->c_count; i++)
2051 if (readmapflag || header->c_addr[i] != 0)
2055 prevtype = header->c_type;
2056 previno = header->c_inumber;
2060 * Find an inode header.
2061 * Complain if had to skip, and complain is set.
2064 findinode(struct s_spcl *header)
2066 static long skipcnt = 0;
2070 curfile.name = "<name unknown>";
2071 curfile.action = UNKNOWN;
2075 if (header->c_magic != NFS_MAGIC) {
2077 while (gethead(header) == FAIL ||
2078 header->c_date != dumpdate)
2081 switch (header->c_type) {
2085 * Skip up to the beginning of the next record
2087 for (i = 0; i < header->c_count; i++)
2088 if (header->c_addr[i])
2090 while (gethead(header) == FAIL ||
2091 header->c_date != dumpdate)
2096 curfile.dip = &header->c_dinode;
2097 curfile.ino = header->c_inumber;
2101 curfile.ino = maxino;
2105 curfile.name = "<file removal list>";
2109 curfile.name = "<file dump list>";
2113 panic("unexpected tape header\n");
2117 panic("unknown tape header type %d\n", spcl.c_type);
2121 } while (header->c_type == TS_ADDR);
2126 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2136 j = sizeof(union u_spcl) / sizeof(int);
2143 /* What happens if we want to read restore tapes
2144 for a 16bit int machine??? */
2150 if (i != CHECKSUM) {
2151 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2152 (unsigned long)curfile.ino, curfile.name);
2162 #include <varargs.h>
2167 msg(const char *fmt, ...)
2180 (void)vfprintf(stderr, fmt, ap);
2183 #endif /* RRESTORE */
2186 swab16(u_char *sp, int n)
2191 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2198 swab32(u_char *sp, int n)
2203 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2204 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2211 swab64(u_char *sp, int n)
2216 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2217 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2218 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2219 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2226 swabst(u_char *cp, u_char *sp)
2232 case '0': case '1': case '2': case '3': case '4':
2233 case '5': case '6': case '7': case '8': case '9':
2234 n = (n * 10) + (*cp++ - '0');
2237 case 's': case 'w': case 'h':
2255 default: /* Any other character, like 'b' counts as byte. */
2269 swabst((u_char *)"i", (u_char *)&x);
2277 swabst((u_char *)"l", (u_char *)&x);
2284 * get the current position of the tape
2287 GetTapePos(long *pos)
2292 if (ioctl(mt, MTIOCPOS, pos) == -1) {
2294 fprintf(stdout, "[%ld] error: %d (getting tapepos: %ld)\n",
2295 (unsigned long)getpid(), err, *pos);
2301 typedef struct mt_pos {
2304 } MTPosRec, *MTPosPtr;
2307 * go to specified position on tape
2310 GotoTapePos(long pos)
2317 if (ioctl(mt, MTIOCTOP, &buf) == -1) {
2319 fprintf(stdout, "[%ld] error: %d (setting tapepos: %ld)\n",
2320 (unsigned long)getpid(), err, pos);
2327 * read next data from tape to re-sync
2330 ReReadFromTape(void)
2334 if (gethead(&spcl) == FAIL) {
2336 fprintf(stdout, "DEBUG 1 gethead failed\n");
2344 RequestVol(long tnum)
2349 #endif /* USE_QFA */