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.36 2001/04/27 12:23:23 stelian Exp $";
54 #include <compaterr.h>
61 #include <sys/param.h>
69 #include <linux/ext2_fs.h>
70 #include <ext2fs/ext2fs.h>
71 #include <bsdcompat.h>
73 #include <ufs/ufs/dinode.h>
74 #endif /* __linux__ */
75 #include <protocols/dumprestore.h>
79 #endif /* HAVE_ZLIB */
83 #include "pathnames.h"
88 static long fssize = MAXBSIZE;
90 static int pipein = 0;
91 static int magtapein = 0; /* input is from magtape */
92 static char magtape[MAXPATHLEN];
93 static char magtapeprefix[MAXPATHLEN];
96 static char *tapebuf; /* input buffer for read */
97 static int bufsize; /* buffer size without prefix */
98 static char *tbufptr = NULL; /* active tape buffer */
100 static char *comprbuf; /* uncompress work buf */
101 static size_t comprlen; /* size including prefix */
103 static union u_spcl endoftapemark;
104 static long blksread; /* blocks read since last header */
105 static long tpblksread = 0; /* TP_BSIZE blocks read */
106 static long tapesread;
107 static sigjmp_buf restart;
108 static int gettingfile = 0; /* restart has a valid frame */
109 static char *host = NULL;
113 static char lnkbuf[MAXPATHLEN + 1];
116 int oldinofmt; /* old inode format conversion required */
117 int Bcvt; /* Swap Bytes (for CCI or sun) */
118 static int Qcvt; /* Swap quads (for sun) */
120 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
122 static void accthdr __P((struct s_spcl *));
123 static int checksum __P((int *));
124 static void findinode __P((struct s_spcl *));
125 static void findtapeblksize __P((void));
126 static int gethead __P((struct s_spcl *));
127 static void readtape __P((char *));
128 static void setdumpnum __P((void));
129 static u_int swabi __P((u_int));
131 static u_long swabl __P((u_long));
133 static u_char *swab64 __P((u_char *, int));
134 static u_char *swab32 __P((u_char *, int));
135 static u_char *swab16 __P((u_char *, int));
136 static void terminateinput __P((void));
137 static void xtrfile __P((char *, size_t));
138 static void xtrlnkfile __P((char *, size_t));
139 static void xtrlnkskip __P((char *, size_t));
140 static void xtrmap __P((char *, size_t));
141 static void xtrmapskip __P((char *, size_t));
142 static void xtrskip __P((char *, size_t));
145 static void newcomprbuf __P((long));
146 static void readtape_set __P((char *));
147 static void readtape_uncompr __P((char *));
148 static void readtape_comprfile __P((char *));
149 static void readtape_comprtape __P((char *));
150 static char *decompress_tapebuf __P((struct tapebuf *, int));
151 static void msg_read_error __P((char *));
153 static int read_a_block __P((int, char *, size_t, long *));
154 #define PREFIXSIZE sizeof(struct tapebuf)
156 #define COMPARE_ONTHEFLY 1
159 static int ifile; /* input file for compare */
160 static int cmperror; /* compare error */
161 static void xtrcmpfile __P((char *, size_t));
162 static void xtrcmpskip __P((char *, size_t));
165 static int readmapflag;
168 * Set up an input source. This is called from main.c before setup() is.
171 setinput(char *source)
177 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
181 if (strchr(source, ':')) {
183 source = strchr(host, ':');
185 if (rmthost(host) == 0)
189 if (strcmp(source, "-") == 0) {
191 * Since input is coming from a pipe we must establish
192 * our own connection to the terminal.
194 terminal = fopen(_PATH_TTY, "r");
195 if (terminal == NULL) {
196 warn("cannot open %s", _PATH_TTY);
197 terminal = fopen(_PATH_DEVNULL, "r");
198 if (terminal == NULL)
199 err(1, "cannot open %s", _PATH_DEVNULL);
203 setuid(getuid()); /* no longer need or want root privileges */
205 strncpy(magtapeprefix, source, MAXPATHLEN);
206 magtapeprefix[MAXPATHLEN-1] = '\0';
207 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
210 strncpy(magtape, source, MAXPATHLEN);
211 magtape[MAXPATHLEN - 1] = '\0';
215 newtapebuf(long size)
217 static int tapebufsize = -1;
220 bufsize = ntrec * TP_BSIZE;
221 if (size <= tapebufsize)
225 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
227 errx(1, "Cannot allocate space for tape buffer");
233 newcomprbuf(long size)
235 if (size <= comprlen)
237 comprlen = size + sizeof(struct tapebuf);
238 if (comprbuf != NULL)
240 comprbuf = malloc(comprlen);
241 if (comprbuf == NULL)
242 errx(1, "Cannot allocate space for decompress buffer");
244 #endif /* HAVE_ZLIB */
247 * Verify that the tape drive can be accessed and
248 * that it actually is a dump tape.
255 struct mtget mt_stat;
257 Vprintf(stdout, "Verify tape and initialize maps\n");
260 mt = rmtopen(magtape, 0);
266 mt = open(magtape, O_RDONLY, 0);
268 err(1, "%s", magtape);
271 /* need to know if input is really from a tape */
274 magtapein = rmtioctl(MTNOP, 1) != -1;
277 if (ioctl(mt, MTIOCGET, (char *) &mt_stat) == 0) {
278 if (mt_stat.mt_dsreg & 0xffff)
279 magtapein = 1; /* fixed blocksize */
281 magtapein = 2; /* variable blocksize */
285 Vprintf(stdout,"Input is from %s\n", magtapein? "tape": "file/pipe");
289 if (gethead(&spcl) == FAIL) {
290 blkcnt--; /* push back this block */
294 if (gethead(&spcl) == FAIL)
295 errx(1, "Tape is not a dump tape");
296 fprintf(stderr, "Converting to new file system format.\n");
300 fprintf(stderr, "Dump tape is compressed.\n");
302 newcomprbuf(bufsize);
304 errx(1,"This restore version doesn't support decompression");
305 #endif /* HAVE_ZLIB */
308 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
309 endoftapemark.s_spcl.c_type = TS_END;
310 ip = (int *)&endoftapemark;
311 j = sizeof(union u_spcl) / sizeof(int);
316 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
318 if (vflag || command == 't' || command == 'C')
320 if (filesys[0] == '\0') {
322 strncpy(filesys, spcl.c_filesys, NAMELEN);
323 filesys[NAMELEN - 1] = '\0';
324 dirptr = strstr(filesys, " (dir");
328 dumptime = spcl.c_ddate;
329 dumpdate = spcl.c_date;
330 if (stat(".", &stbuf) < 0)
331 err(1, "cannot stat .");
332 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
334 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
335 fssize = stbuf.st_blksize;
336 if (((fssize - 1) & fssize) != 0)
337 errx(1, "bad block size %ld", fssize);
338 if (spcl.c_volume != 1)
339 errx(1, "Tape is not volume 1 of the dump");
340 if (gethead(&spcl) == FAIL) {
341 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
342 panic("no header after volume mark!\n");
345 if (spcl.c_type != TS_CLRI)
346 errx(1, "Cannot find file removal list");
347 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
348 Dprintf(stdout, "maxino = %ld\n", (long)maxino);
349 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
351 errx(1, "no memory for active inode map");
353 curfile.action = USING;
354 getfile(xtrmap, xtrmapskip);
355 if (spcl.c_type != TS_BITS)
356 errx(1, "Cannot find file dump list");
357 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
358 if (map == (char *)NULL)
359 errx(1, "no memory for file dump list");
361 curfile.action = USING;
362 getfile(xtrmap, xtrmapskip);
364 * If there may be whiteout entries on the tape, pretend that the
365 * whiteout inode exists, so that the whiteout entries can be
369 SETINO(WINO, dumpmap);
373 * Prompt user to load a new dump volume.
374 * "Nextvol" is the next suggested volume to use.
375 * This suggested volume is enforced when doing full
376 * or incremental restores, but can be overridden by
377 * the user when only extracting a subset of the files.
382 long newvol = 0, savecnt = 0, wantnext = 0, i;
383 union u_spcl tmpspcl;
384 # define tmpbuf tmpspcl.s_spcl
394 panic("Changing volumes on pipe input?\n");
402 exit(1); /* pipes do not get a second chance */
403 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
410 while (newvol <= 0) {
411 if (tapesread == 0) {
412 fprintf(stderr, "%s%s%s%s%s",
413 "You have not read any tapes yet.\n",
414 "Unless you know which volume your",
415 " file(s) are on you should start\n",
416 "with the last volume and work",
417 " towards the first.\n");
419 fprintf(stderr, "You have read volumes");
421 for (i = 1; i < 32; i++)
422 if (tapesread & (1 << i)) {
423 fprintf(stderr, "%s%ld", buf, (long)i);
426 fprintf(stderr, "\n");
429 fprintf(stderr, "Specify next volume #: ");
430 (void) fflush(stderr);
431 (void) fgets(buf, TP_BSIZE, terminal);
432 } while (!feof(terminal) && buf[0] == '\n');
438 "Volume numbers are positive numerics\n");
441 if (newvol == volno) {
442 tapesread |= 1 << volno;
447 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
448 magtape[MAXPATHLEN - 1] = '\0';
450 if (!Mflag || haderror) {
452 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
453 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
454 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
455 (void) fflush(stderr);
456 (void) fgets(buf, TP_BSIZE, terminal);
459 if (!strcmp(buf, "none\n")) {
463 if (buf[0] != '\n') {
464 (void) strcpy(magtape, buf);
465 magtape[strlen(magtape) - 1] = '\0';
470 mt = rmtopen(magtape, 0);
473 mt = open(magtape, O_RDONLY, 0);
476 fprintf(stderr, "Cannot open %s\n", magtape);
485 if (gethead(&tmpbuf) == FAIL) {
486 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
487 fprintf(stderr, "tape is not dump tape\n");
492 if (tmpbuf.c_volume != volno) {
493 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
498 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
499 fprintf(stderr, "Wrong dump date\n\tgot: %s",
500 ctime4(&tmpbuf.c_date));
501 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
506 tapesread |= 1 << volno;
509 * If continuing from the previous volume, skip over any
510 * blocks read already at the end of the previous volume.
512 * If coming to this volume at random, skip to the beginning
513 * of the next record.
515 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
516 tpblksread, (long)tmpbuf.c_firstrec);
517 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
519 tpblksread = tmpbuf.c_firstrec;
520 for (i = tmpbuf.c_count; i > 0; i--)
522 } else if (tmpbuf.c_firstrec > 0 &&
523 tmpbuf.c_firstrec < tpblksread - 1) {
525 * -1 since we've read the volume header
527 i = tpblksread - tmpbuf.c_firstrec - 1;
528 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
529 (long)i, i > 1 ? "s" : "");
534 if (curfile.action == USING) {
536 panic("active file into volume 1\n");
540 * Skip up to the beginning of the next record
542 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
543 for (i = tmpbuf.c_count; i > 0; i--)
545 (void) gethead(&spcl);
549 siglongjmp(restart, 1);
554 * Handle unexpected EOF.
560 if (gettingfile && curfile.action == USING) {
561 printf("Warning: %s %s\n",
562 "End-of-input encountered while extracting", curfile.name);
564 curfile.name = "<name unknown>";
565 curfile.action = UNKNOWN;
567 curfile.ino = maxino;
570 siglongjmp(restart, 1);
575 * handle multiple dumps per tape by skipping forward to the
583 if (dumpnum == 1 || volno != 1)
586 errx(1, "Cannot have multiple dumps on pipe input");
588 tcom.mt_count = dumpnum - 1;
591 rmtioctl(MTFSF, dumpnum - 1);
594 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
601 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
602 fprintf(stdout, "Dumped from: %s",
603 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
604 if (spcl.c_host[0] == '\0')
606 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
607 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
608 fprintf(stdout, "Label: %s\n", spcl.c_label);
612 extractfile(char *name)
616 struct timeval timep[2];
620 curfile.action = USING;
622 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
623 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
624 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
625 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
626 #else /* __linux__ */
627 timep[0].tv_sec = curfile.dip->di_atime;
628 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
629 timep[1].tv_sec = curfile.dip->di_mtime;
630 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
631 #endif /* __linux__ */
632 mode = curfile.dip->di_mode;
633 flags = curfile.dip->di_flags;
634 switch (mode & IFMT) {
637 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
642 Vprintf(stdout, "skipped socket %s\n", name);
648 ep = lookupname(name);
649 if (ep == NULL || ep->e_flags & EXTRACT)
650 panic("unextracted directory %s\n", name);
654 Vprintf(stdout, "extract file %s\n", name);
655 return (genliteraldir(name, curfile.ino));
660 uid_t luid = curfile.dip->di_uid;
661 gid_t lgid = curfile.dip->di_gid;
665 getfile(xtrlnkfile, xtrlnkskip);
668 "%s: zero length symbolic link (ignored)\n", name);
671 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
674 (void) lchown(name, luid, lgid);
680 Vprintf(stdout, "extract fifo %s\n", name);
687 if (mkfifo(name, mode) < 0) {
688 warn("%s: cannot create fifo", name);
692 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
693 (void) chmod(name, mode);
696 (void) fsetflags(name, flags);
698 (void) chflags(name, flags);
706 Vprintf(stdout, "extract special file %s\n", name);
713 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
714 warn("%s: cannot create special file", name);
718 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
719 (void) chmod(name, mode);
723 warn("%s: fsetflags called on a special file", name);
724 (void) fsetflags(name, flags);
727 (void) chflags(name, flags);
734 Vprintf(stdout, "extract file %s\n", name);
741 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
743 warn("%s: cannot create file", name);
747 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
748 (void) fchmod(ofile, mode);
751 (void) setflags(ofile, flags);
753 (void) fchflags(ofile, flags);
755 getfile(xtrfile, xtrskip);
764 * skip over bit maps on the tape
770 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
775 * skip over a file on the tape
781 curfile.action = SKIP;
782 getfile(xtrnull, xtrnull);
786 * Extract a file from the tape.
787 * When an allocated block is found it is passed to the fill function;
788 * when an unallocated block (hole) is found, a zeroed buffer is passed
789 * to the skip function.
792 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
795 volatile int curblk = 0;
796 volatile quad_t size = spcl.c_dinode.di_size;
797 volatile int last_write_was_hole = 0;
798 quad_t origsize = size;
799 static char clearedbuf[MAXBSIZE];
800 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
803 if (spcl.c_type == TS_END)
804 panic("ran off end of tape\n");
805 if (spcl.c_magic != NFS_MAGIC)
806 panic("not at beginning of a file\n");
807 if (!gettingfile && setjmp(restart) != 0)
811 for (i = 0; i < spcl.c_count; i++) {
812 if (readmapflag || spcl.c_addr[i]) {
813 readtape(&buf[curblk++][0]);
814 if (curblk == fssize / TP_BSIZE) {
815 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
816 fssize : (curblk - 1) * TP_BSIZE + size));
818 last_write_was_hole = 0;
822 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
824 (curblk - 1) * TP_BSIZE + size));
827 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
829 last_write_was_hole = 1;
831 if ((size -= TP_BSIZE) <= 0) {
832 for (i++; i < spcl.c_count; i++)
833 if (readmapflag || spcl.c_addr[i])
838 if (gethead(&spcl) == GOOD && size > 0) {
839 if (spcl.c_type == TS_ADDR)
842 "Missing address (header) block for %s at %ld blocks\n",
843 curfile.name, (long)blksread);
846 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
847 last_write_was_hole = 0;
850 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
851 (*skip)(clearedbuf, size);
852 last_write_was_hole = 1;
854 if (last_write_was_hole) {
855 ftruncate(ofile, origsize);
862 * Write out the next block of a file.
865 xtrfile(char *buf, size_t size)
870 if (write(ofile, buf, (int) size) == -1)
871 err(1, "write error extracting inode %lu, name %s\nwrite",
872 (unsigned long)curfile.ino, curfile.name);
876 * Skip over a hole in a file.
880 xtrskip(char *buf, size_t size)
883 if (lseek(ofile, (off_t)size, SEEK_CUR) == -1)
884 err(1, "seek error extracting inode %lu, name %s\nlseek",
885 (unsigned long)curfile.ino, curfile.name);
889 * Collect the next block of a symbolic link.
892 xtrlnkfile(char *buf, size_t size)
896 if (pathlen > MAXPATHLEN)
897 errx(1, "symbolic link name: %s->%s%s; too long %d",
898 curfile.name, lnkbuf, buf, pathlen);
899 (void) strcat(lnkbuf, buf);
903 * Skip over a hole in a symbolic link (should never happen).
907 xtrlnkskip(char *buf, size_t size)
910 errx(1, "unallocated block in symbolic link %s", curfile.name);
914 * Collect the next block of a bit map.
917 xtrmap(char *buf, size_t size)
920 memmove(map, buf, size);
925 * Skip over a hole in a bit map (should never happen).
929 xtrmapskip(char *buf, size_t size)
932 panic("hole in map\n");
937 * Noop, when an extraction function is not needed.
941 xtrnull(char *buf, size_t size)
949 * Compare the next block of a file.
952 xtrcmpfile(char *buf, size_t size)
954 static char cmpbuf[MAXBSIZE];
959 if (read(ifile, cmpbuf, size) != size) {
960 fprintf(stderr, "%s: size has changed.\n",
966 if (memcmp(buf, cmpbuf, size) != 0) {
967 fprintf(stderr, "%s: tape and disk copies are different\n",
975 * Skip over a hole in a file.
978 xtrcmpskip(char *buf, size_t size)
980 static char cmpbuf[MAXBSIZE];
986 if (read(ifile, cmpbuf, size) != size) {
987 fprintf(stderr, "%s: size has changed.\n",
993 for (i = 0; i < size; ++i)
994 if (cmpbuf[i] != '\0') {
995 fprintf(stderr, "%s: tape and disk copies are different\n",
1001 #endif /* COMPARE_ONTHEFLY */
1003 #if !COMPARE_ONTHEFLY
1005 do_cmpfiles(int fd_tape, int fd_disk, long size)
1007 static char buf_tape[BUFSIZ];
1008 static char buf_disk[BUFSIZ];
1013 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1014 close(fd_tape), close(fd_disk);
1015 panic("do_cmpfiles: unexpected EOF[1]");
1017 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1018 close(fd_tape), close(fd_disk);
1019 panic("do_cmpfiles: unexpected EOF[2]");
1021 if (n_tape != n_disk) {
1022 close(fd_tape), close(fd_disk);
1023 panic("do_cmpfiles: sizes different!");
1025 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1031 /* for debugging compare problems */
1032 #undef COMPARE_FAIL_KEEP_FILE
1035 #ifdef COMPARE_FAIL_KEEP_FILE
1036 /* return true if tapefile should be unlinked after compare */
1041 cmpfiles(char *tapefile, char *diskfile, struct stat *sbuf_disk)
1043 struct stat sbuf_tape;
1044 int fd_tape, fd_disk;
1046 if (stat(tapefile, &sbuf_tape) != 0) {
1047 panic("Can't lstat tmp file %s: %s\n", tapefile,
1052 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1054 "%s: size changed from %ld to %ld.\n",
1055 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1057 #ifdef COMPARE_FAIL_KEEP_FILE
1064 if ((fd_tape = open(tapefile, O_RDONLY)) < 0) {
1065 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1068 if ((fd_disk = open(diskfile, O_RDONLY)) < 0) {
1070 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1074 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1075 fprintf(stderr, "%s: tape and disk copies are different\n",
1080 #ifdef COMPARE_FAIL_KEEP_FILE
1081 /* rename the file to live in /tmp */
1082 /* rename `tapefile' to /tmp/<basename of diskfile> */
1084 char *p = strrchr(diskfile, '/');
1085 char newname[MAXPATHLEN];
1087 panic("can't find / in %s\n", diskfile);
1089 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1090 if (rename(tapefile, newname)) {
1091 panic("rename from %s to %s failed: %s\n",
1095 fprintf(stderr, "*** %s saved to %s\n",
1100 /* don't unlink the file (it's not there anymore */
1109 #ifdef COMPARE_FAIL_KEEP_FILE
1113 #endif /* !COMPARE_ONTHEFLY */
1115 #if !COMPARE_ONTHEFLY
1116 static char tmpfilename[MAXPATHLEN];
1120 comparefile(char *name)
1125 #if !COMPARE_ONTHEFLY
1126 static char *tmpfile = NULL;
1130 if ((r = lstat(name, &sb)) != 0) {
1131 warn("%s: does not exist (%d)", name, r);
1137 curfile.name = name;
1138 curfile.action = USING;
1139 mode = curfile.dip->di_mode;
1141 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1142 (long)sb.st_size, mode);
1144 if (sb.st_mode != mode) {
1145 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1146 name, mode & 07777, sb.st_mode & 07777);
1149 switch (mode & IFMT) {
1163 char lbuf[MAXPATHLEN + 1];
1166 if (!(sb.st_mode & S_IFLNK)) {
1167 fprintf(stderr, "%s: is no longer a symbolic link\n",
1174 getfile(xtrlnkfile, xtrlnkskip);
1177 "%s: zero length symbolic link (ignored)\n",
1182 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1183 panic("readlink of %s failed: %s", name,
1188 if (strcmp(lbuf, lnkbuf) != 0) {
1190 "%s: symbolic link changed from %s to %s.\n",
1191 name, lnkbuf, lbuf);
1200 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1201 fprintf(stderr, "%s: no longer a special file\n",
1208 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1210 "%s: device changed from %d,%d to %d,%d.\n",
1212 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1213 (int)curfile.dip->di_rdev & 0xff,
1214 ((int)sb.st_rdev >> 8) & 0xff,
1215 (int)sb.st_rdev & 0xff);
1222 #if COMPARE_ONTHEFLY
1223 if ((ifile = open(name, O_RDONLY)) < 0) {
1224 panic("Can't open %s: %s\n", name, strerror(errno));
1230 getfile(xtrcmpfile, xtrcmpskip);
1233 if (read(ifile, &c, 1) != 0) {
1234 fprintf(stderr, "%s: size has changed.\n",
1244 if (tmpfile == NULL) {
1245 /* argument to mktemp() must not be in RO space: */
1246 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1247 tmpfile = mktemp(&tmpfilename[0]);
1249 if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1250 panic("cannot delete tmp file %s: %s\n",
1251 tmpfile, strerror(errno));
1253 if ((ofile = open(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1254 panic("cannot create file temp file %s: %s\n",
1255 name, strerror(errno));
1257 getfile(xtrfile, xtrskip);
1258 (void) close(ofile);
1259 #ifdef COMPARE_FAIL_KEEP_FILE
1260 if (cmpfiles(tmpfile, name, &sb))
1263 cmpfiles(tmpfile, name, &sb);
1266 #endif /* COMPARE_ONTHEFLY */
1273 static void (*readtape_func)(char *) = readtape_set;
1276 * Read TP_BSIZE blocks from the input.
1277 * Handle read errors, and end of media.
1278 * Decompress compressed blocks.
1283 (*readtape_func)(buf); /* call the actual processing routine */
1287 * Set function pointer for readtape() routine. zflag and magtapein must
1288 * be correctly set before the first call to readtape().
1291 readtape_set(char *buf)
1294 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 set the blocksize from its length. Test
1699 * if the block looks like a compressed dump tape. setup() will make
1700 * the final 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 will not be compressed.
1707 findtapeblksize(void)
1711 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1712 struct s_spcl *spclpt = (struct s_spcl *) tpb->buf;
1714 for (i = 0; i < ntrec; i++)
1715 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1719 * For a pipe or file, read in the first record. For a tape, read
1722 if (magtapein == 1) /* fixed blocksize tape, not compressed */
1723 len = ntrec * TP_BSIZE;
1724 else if (magtapein == 2)/* variable blocksize tape */
1725 len = bufsize + PREFIXSIZE;
1726 else /* not mag tape */
1729 if (read_a_block(mt, tapebuf, len, &i) <= 0)
1730 errx(1, "Tape read error on first record");
1733 * If the input is from a file or a pipe, we read TP_BSIZE
1734 * bytes looking for a compressed dump header, we then
1735 * need to read in the rest of the record, as determined by
1736 * tpb->length or bufsize. The first block of the dump is
1737 * guaranteed to not be compressed so we look at the header.
1740 if (tpb->length % TP_BSIZE == 0
1741 && tpb->length <= bufsize
1742 && tpb->compressed == 0
1743 && spclpt->c_type == TS_TAPE
1744 && spclpt->c_flags & DR_COMPRESSED) {
1745 /* Looks like it's a compressed dump block prefix, */
1746 /* read in the rest of the block based on tpb->length. */
1747 len = tpb->length - TP_BSIZE + PREFIXSIZE;
1748 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) <= 0
1750 errx(1,"Error reading dump file header");
1752 numtrec = ntrec = tpb->length / TP_BSIZE;
1756 /* read in the rest of the block based on bufsize */
1757 len = bufsize - TP_BSIZE;
1758 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1759 || (i != len && i % TP_BSIZE != 0))
1760 errx(1,"Error reading dump file header");
1764 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1769 * If the input is a variable block size tape, we tried to
1770 * read PREFIXSIZE + ntrec * TP_BSIZE bytes.
1771 * If it's not a compressed dump tape or the value of ntrec is
1772 * too large, we have read less than * what we asked for;
1773 * adjust the value of ntrec and test for * a compressed dump
1777 if (i % TP_BSIZE != 0) {
1778 if (i % TP_BSIZE == PREFIXSIZE
1779 && tpb->compressed == 0
1780 && spclpt->c_type == TS_TAPE
1781 && spclpt->c_flags & DR_COMPRESSED) {
1785 if (tpb->length > bufsize)
1786 errx(1, "Tape blocksize is too large, use "
1787 "\'-b %d\' ", tpb->length / TP_BSIZE);
1790 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1793 ntrec = i / TP_BSIZE;
1795 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1799 * Read a block of data handling all of the messy details.
1801 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1809 i = rmtread(buf, size);
1812 i = read(fd, buf, size);
1815 break; /* EOD or error */
1818 break; /* block at a time for mt */
1821 *lengthread = len - size;
1840 * Read the next block from the tape.
1841 * Check to see if it is one of several vintage headers.
1842 * If it is an old style header, convert it to a new style header.
1843 * If it is not any valid header, return an error.
1846 gethead(struct s_spcl *buf)
1854 char dummy[TP_BSIZE];
1861 u_int16_t c_inumber;
1866 u_int16_t odi_nlink;
1882 readtape((char *)buf);
1883 if (buf->c_magic != NFS_MAGIC) {
1884 if (swabi(buf->c_magic) != NFS_MAGIC)
1887 Vprintf(stdout, "Note: Doing Byte swapping\n");
1891 if (checksum((int *)buf) == FAIL)
1894 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1897 readtape((char *)(&u_ospcl.s_ospcl));
1898 memset((char *)buf, 0, (long)TP_BSIZE);
1899 buf->c_type = u_ospcl.s_ospcl.c_type;
1900 buf->c_date = u_ospcl.s_ospcl.c_date;
1901 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1902 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1903 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1904 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1905 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1906 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1907 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1908 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1909 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1910 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1911 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1912 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1914 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1915 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1916 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1917 #else /* __linux__ */
1918 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1919 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1920 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1921 #endif /* __linux__ */
1922 buf->c_count = u_ospcl.s_ospcl.c_count;
1923 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1924 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1925 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1927 buf->c_magic = NFS_MAGIC;
1930 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1931 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1932 qcvt.qval = buf->c_dinode.di_size;
1933 if (qcvt.val[0] || qcvt.val[1]) {
1934 printf("Note: Doing Quad swapping\n");
1939 qcvt.qval = buf->c_dinode.di_size;
1941 qcvt.val[1] = qcvt.val[0];
1943 buf->c_dinode.di_size = qcvt.qval;
1947 switch (buf->c_type) {
1952 * Have to patch up missing information in bit map headers
1955 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1956 if (buf->c_count > TP_NINDIR)
1959 for (i = 0; i < buf->c_count; i++)
1964 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1976 panic("gethead: unknown inode type %d\n", buf->c_type);
1980 * If we are restoring a filesystem with old format inodes,
1981 * copy the uid/gid to the new location.
1984 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1985 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1993 * Check that a header is where it belongs and predict the next header
1996 accthdr(struct s_spcl *header)
1998 static dump_ino_t previno = 0x7fffffff;
1999 static int prevtype;
2000 static long predict;
2003 if (header->c_type == TS_TAPE) {
2004 fprintf(stderr, "Volume header (%s inode format) ",
2005 oldinofmt ? "old" : "new");
2006 if (header->c_firstrec)
2007 fprintf(stderr, "begins with record %d",
2008 header->c_firstrec);
2009 fprintf(stderr, "\n");
2010 previno = 0x7fffffff;
2013 if (previno == 0x7fffffff)
2017 fprintf(stderr, "Dumped inodes map header");
2020 fprintf(stderr, "Used inodes map header");
2023 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2026 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2029 fprintf(stderr, "End of tape header");
2032 if (predict != blksread - 1)
2033 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2034 predict, blksread - 1);
2035 fprintf(stderr, "\n");
2038 if (header->c_type != TS_END)
2039 for (i = 0; i < header->c_count; i++)
2040 if (readmapflag || header->c_addr[i] != 0)
2044 prevtype = header->c_type;
2045 previno = header->c_inumber;
2049 * Find an inode header.
2050 * Complain if had to skip, and complain is set.
2053 findinode(struct s_spcl *header)
2055 static long skipcnt = 0;
2059 curfile.name = "<name unknown>";
2060 curfile.action = UNKNOWN;
2064 if (header->c_magic != NFS_MAGIC) {
2066 while (gethead(header) == FAIL ||
2067 header->c_date != dumpdate)
2070 switch (header->c_type) {
2074 * Skip up to the beginning of the next record
2076 for (i = 0; i < header->c_count; i++)
2077 if (header->c_addr[i])
2079 while (gethead(header) == FAIL ||
2080 header->c_date != dumpdate)
2085 curfile.dip = &header->c_dinode;
2086 curfile.ino = header->c_inumber;
2090 curfile.ino = maxino;
2094 curfile.name = "<file removal list>";
2098 curfile.name = "<file dump list>";
2102 panic("unexpected tape header\n");
2106 panic("unknown tape header type %d\n", spcl.c_type);
2110 } while (header->c_type == TS_ADDR);
2115 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2125 j = sizeof(union u_spcl) / sizeof(int);
2132 /* What happens if we want to read restore tapes
2133 for a 16bit int machine??? */
2139 if (i != CHECKSUM) {
2140 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2141 (unsigned long)curfile.ino, curfile.name);
2151 #include <varargs.h>
2156 msg(const char *fmt, ...)
2169 (void)vfprintf(stderr, fmt, ap);
2172 #endif /* RRESTORE */
2175 swab16(u_char *sp, int n)
2180 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2187 swab32(u_char *sp, int n)
2192 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2193 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2200 swab64(u_char *sp, int n)
2205 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2206 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2207 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2208 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2215 swabst(u_char *cp, u_char *sp)
2221 case '0': case '1': case '2': case '3': case '4':
2222 case '5': case '6': case '7': case '8': case '9':
2223 n = (n * 10) + (*cp++ - '0');
2226 case 's': case 'w': case 'h':
2244 default: /* Any other character, like 'b' counts as byte. */
2258 swabst((u_char *)"i", (u_char *)&x);
2266 swabst((u_char *)"l", (u_char *)&x);
2273 * get the current position of the tape
2276 GetTapePos(long *pos)
2281 if (ioctl(mt, MTIOCPOS, pos) == -1) {
2283 fprintf(stdout, "[%ld] error: %d (getting tapepos: %ld)\n",
2284 (unsigned long)getpid(), err, *pos);
2290 typedef struct mt_pos {
2293 } MTPosRec, *MTPosPtr;
2296 * go to specified position on tape
2299 GotoTapePos(long pos)
2306 if (ioctl(mt, MTIOCTOP, &buf) == -1) {
2308 fprintf(stdout, "[%ld] error: %d (setting tapepos: %ld)\n",
2309 (unsigned long)getpid(), err, pos);
2316 * read next data from tape to re-sync
2319 ReReadFromTape(void)
2323 if (gethead(&spcl) == FAIL) {
2325 fprintf(stdout, "DEBUG 1 gethead failed\n");
2333 RequestVol(long tnum)
2338 #endif /* USE_QFA */