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.46 2001/08/16 15:24:22 stelian Exp $";
53 #include <compatlfs.h>
55 #include <compaterr.h>
63 #include <sys/param.h>
71 #ifdef HAVE_EXT2FS_EXT2_FS_H
72 #include <ext2fs/ext2_fs.h>
74 #include <linux/ext2_fs.h>
76 #include <ext2fs/ext2fs.h>
77 #include <bsdcompat.h>
79 #include <ufs/ufs/dinode.h>
80 #endif /* __linux__ */
81 #include <protocols/dumprestore.h>
85 #endif /* HAVE_ZLIB */
89 #endif /* HAVE_BZLIB */
93 #include "pathnames.h"
98 static long fssize = MAXBSIZE;
100 static int pipein = 0;
101 static int magtapein = 0; /* input is from magtape */
102 static char magtape[MAXPATHLEN];
103 static char magtapeprefix[MAXPATHLEN];
106 static char *tapebuf; /* input buffer for read */
107 static int bufsize; /* buffer size without prefix */
108 static char *tbufptr = NULL; /* active tape buffer */
109 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
110 static char *comprbuf; /* uncompress work buf */
111 static size_t comprlen; /* size including prefix */
113 static union u_spcl endoftapemark;
114 static long blksread; /* blocks read since last header */
115 static long tpblksread = 0; /* TP_BSIZE blocks read */
116 static long tapesread;
117 static sigjmp_buf restart;
118 static int gettingfile = 0; /* restart has a valid frame */
119 static char *host = NULL;
123 static char lnkbuf[MAXPATHLEN + 1];
126 int oldinofmt; /* old inode format conversion required */
127 int Bcvt; /* Swap Bytes (for CCI or sun) */
128 static int Qcvt; /* Swap quads (for sun) */
130 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
132 static void accthdr __P((struct s_spcl *));
133 static int checksum __P((int *));
134 static void findinode __P((struct s_spcl *));
135 static void findtapeblksize __P((void));
136 static int gethead __P((struct s_spcl *));
137 static void readtape __P((char *));
138 static void setdumpnum __P((void));
139 static u_int swabi __P((u_int));
141 static u_long swabl __P((u_long));
143 static u_char *swab64 __P((u_char *, int));
144 static u_char *swab32 __P((u_char *, int));
145 static u_char *swab16 __P((u_char *, int));
146 static void terminateinput __P((void));
147 static void xtrfile __P((char *, size_t));
148 static void xtrlnkfile __P((char *, size_t));
149 static void xtrlnkskip __P((char *, size_t));
150 static void xtrmap __P((char *, size_t));
151 static void xtrmapskip __P((char *, size_t));
152 static void xtrskip __P((char *, size_t));
153 static void setmagtapein __P((void));
155 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
156 static void newcomprbuf __P((int));
157 static void readtape_set __P((char *));
158 static void readtape_uncompr __P((char *));
159 static void readtape_comprfile __P((char *));
160 static void readtape_comprtape __P((char *));
161 static char *decompress_tapebuf __P((struct tapebuf *, int));
162 static void msg_read_error __P((char *));
164 static int read_a_block __P((int, char *, size_t, long *));
165 #define PREFIXSIZE sizeof(struct tapebuf)
167 #define COMPARE_ONTHEFLY 1
170 static int ifile; /* input file for compare */
171 static int cmperror; /* compare error */
172 static void xtrcmpfile __P((char *, size_t));
173 static void xtrcmpskip __P((char *, size_t));
176 static int readmapflag;
179 * Set up an input source. This is called from main.c before setup() is.
182 setinput(char *source)
188 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
192 if (strchr(source, ':')) {
194 source = strchr(host, ':');
196 if (rmthost(host) == 0)
200 if (strcmp(source, "-") == 0) {
202 * Since input is coming from a pipe we must establish
203 * our own connection to the terminal.
205 terminal = fopen(_PATH_TTY, "r");
206 if (terminal == NULL) {
207 warn("cannot open %s", _PATH_TTY);
208 terminal = fopen(_PATH_DEVNULL, "r");
209 if (terminal == NULL)
210 err(1, "cannot open %s", _PATH_DEVNULL);
214 setuid(getuid()); /* no longer need or want root privileges */
216 strncpy(magtapeprefix, source, MAXPATHLEN);
217 magtapeprefix[MAXPATHLEN-1] = '\0';
218 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
221 strncpy(magtape, source, MAXPATHLEN);
222 magtape[MAXPATHLEN - 1] = '\0';
226 newtapebuf(long size)
228 static int tapebufsize = -1;
231 bufsize = ntrec * TP_BSIZE;
232 if (size <= tapebufsize)
236 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
238 errx(1, "Cannot allocate space for tape buffer");
242 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
244 newcomprbuf(int size)
246 size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
247 if (buf_size <= comprlen)
250 if (comprbuf != NULL)
252 comprbuf = malloc(comprlen);
253 if (comprbuf == NULL)
254 errx(1, "Cannot allocate space for decompress buffer");
256 #endif /* HAVE_ZLIB || HAVE_BZLIB */
259 * Verify that the tape drive can be accessed and
260 * that it actually is a dump tape.
265 int i, j, *ip, bot_code;
268 Vprintf(stdout, "Verify tape and initialize maps\n");
270 msg("Launching %s\n", bot_script);
271 bot_code = system_command(bot_script, magtape, 1);
272 if (bot_code != 0 && bot_code != 1) {
273 msg("Restore aborted by the beginning of tape script\n");
279 mt = rmtopen(magtape, 0);
285 mt = OPEN(magtape, O_RDONLY, 0);
287 err(1, "%s", magtape);
293 if (gethead(&spcl) == FAIL) {
294 blkcnt--; /* push back this block */
298 if (gethead(&spcl) == FAIL)
299 errx(1, "Tape is not a dump tape");
300 fprintf(stderr, "Converting to new file system format.\n");
304 fprintf(stderr, "Dump tape is compressed.\n");
305 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
308 errx(1,"This restore version doesn't support decompression");
309 #endif /* HAVE_ZLIB */
312 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
313 endoftapemark.s_spcl.c_type = TS_END;
314 ip = (int *)&endoftapemark;
315 j = sizeof(union u_spcl) / sizeof(int);
320 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
322 if (vflag || command == 't' || command == 'C')
324 if (filesys[0] == '\0') {
326 strncpy(filesys, spcl.c_filesys, NAMELEN);
327 filesys[NAMELEN - 1] = '\0';
328 dirptr = strstr(filesys, " (dir");
332 dumptime = spcl.c_ddate;
333 dumpdate = spcl.c_date;
334 if (STAT(".", &stbuf) < 0)
335 err(1, "cannot stat .");
336 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
338 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
339 fssize = stbuf.st_blksize;
340 if (((fssize - 1) & fssize) != 0)
341 errx(1, "bad block size %ld", fssize);
342 if (spcl.c_volume != 1)
343 errx(1, "Tape is not volume 1 of the dump");
344 if (gethead(&spcl) == FAIL) {
345 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
346 panic("no header after volume mark!\n");
349 if (spcl.c_type != TS_CLRI)
350 errx(1, "Cannot find file removal list");
351 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
352 Dprintf(stdout, "maxino = %ld\n", (long)maxino);
353 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
355 errx(1, "no memory for active inode map");
357 curfile.action = USING;
358 getfile(xtrmap, xtrmapskip);
359 if (spcl.c_type != TS_BITS)
360 errx(1, "Cannot find file dump list");
361 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
362 if (map == (char *)NULL)
363 errx(1, "no memory for file dump list");
365 curfile.action = USING;
366 getfile(xtrmap, xtrmapskip);
368 * If there may be whiteout entries on the tape, pretend that the
369 * whiteout inode exists, so that the whiteout entries can be
373 SETINO(WINO, dumpmap);
377 * Prompt user to load a new dump volume.
378 * "Nextvol" is the next suggested volume to use.
379 * This suggested volume is enforced when doing full
380 * or incremental restores, but can be overridden by
381 * the user when only extracting a subset of the files.
386 long newvol = 0, wantnext = 0, i;
387 long saved_blksread = 0, saved_tpblksread = 0;
388 union u_spcl tmpspcl;
389 # define tmpbuf tmpspcl.s_spcl
391 int haderror = 0, bot_code = 1;
399 panic("Changing volumes on pipe input?\n");
404 saved_blksread = blksread;
405 saved_tpblksread = tpblksread;
408 exit(1); /* pipes do not get a second chance */
409 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
416 while (newvol <= 0) {
417 if (tapesread == 0) {
418 fprintf(stderr, "%s%s%s%s%s",
419 "You have not read any tapes yet.\n",
420 "Unless you know which volume your",
421 " file(s) are on you should start\n",
422 "with the last volume and work",
423 " towards the first.\n");
425 fprintf(stderr, "You have read volumes");
427 for (i = 1; i < 32; i++)
428 if (tapesread & (1 << i)) {
429 fprintf(stderr, "%s%ld", buf, (long)i);
432 fprintf(stderr, "\n");
435 fprintf(stderr, "Specify next volume #: ");
436 (void) fflush(stderr);
437 (void) fgets(buf, TP_BSIZE, terminal);
438 } while (!feof(terminal) && buf[0] == '\n');
444 "Volume numbers are positive numerics\n");
447 if (newvol == volno) {
448 tapesread |= 1 << volno;
453 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
454 magtape[MAXPATHLEN - 1] = '\0';
456 if (bot_script && !haderror) {
457 msg("Launching %s\n", bot_script);
458 bot_code = system_command(bot_script, magtape, newvol);
459 if (bot_code != 0 && bot_code != 1) {
460 msg("Restore aborted by the beginning of tape script\n");
464 if (haderror || (bot_code && !Mflag)) {
466 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
467 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
468 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
469 (void) fflush(stderr);
470 (void) fgets(buf, TP_BSIZE, terminal);
473 if (!strcmp(buf, "none\n")) {
477 if (buf[0] != '\n') {
479 (void) strncpy(magtape, buf, sizeof(magtape));
480 magtape[sizeof(magtape) - 1] = '\0';
481 if ((pos = strchr(magtape, '\n')))
482 magtape[pos - magtape] = '\0';
487 mt = rmtopen(magtape, 0);
490 mt = OPEN(magtape, O_RDONLY, 0);
493 fprintf(stderr, "Cannot open %s\n", magtape);
504 if (gethead(&tmpbuf) == FAIL) {
505 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
506 fprintf(stderr, "tape is not dump tape\n");
509 blksread = saved_blksread;
510 tpblksread = saved_tpblksread;
513 if (tmpbuf.c_volume != volno) {
514 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
517 blksread = saved_blksread;
518 tpblksread = saved_tpblksread;
521 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
522 fprintf(stderr, "Wrong dump date\n\tgot: %s",
523 ctime4(&tmpbuf.c_date));
524 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
527 blksread = saved_blksread;
528 tpblksread = saved_tpblksread;
531 tapesread |= 1 << volno;
532 blksread = saved_blksread;
533 tpblksread = saved_tpblksread;
535 * If continuing from the previous volume, skip over any
536 * blocks read already at the end of the previous volume.
538 * If coming to this volume at random, skip to the beginning
539 * of the next record.
541 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
542 tpblksread, (long)tmpbuf.c_firstrec);
543 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
545 tpblksread = tmpbuf.c_firstrec;
546 for (i = tmpbuf.c_count; i > 0; i--)
548 } else if (tmpbuf.c_firstrec > 0 &&
549 tmpbuf.c_firstrec < tpblksread - 1) {
551 * -1 since we've read the volume header
553 i = tpblksread - tmpbuf.c_firstrec - 1;
554 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
555 (long)i, i > 1 ? "s" : "");
560 if (curfile.action == USING) {
562 panic("active file into volume 1\n");
566 * Skip up to the beginning of the next record
568 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
569 for (i = tmpbuf.c_count; i > 0; i--)
571 (void) gethead(&spcl);
575 siglongjmp(restart, 1);
580 * Handle unexpected EOF.
586 if (gettingfile && curfile.action == USING) {
587 printf("Warning: %s %s\n",
588 "End-of-input encountered while extracting", curfile.name);
590 curfile.name = "<name unknown>";
591 curfile.action = UNKNOWN;
593 curfile.ino = maxino;
596 siglongjmp(restart, 1);
601 * handle multiple dumps per tape by skipping forward to the
609 if (dumpnum == 1 || volno != 1)
612 errx(1, "Cannot have multiple dumps on pipe input");
614 tcom.mt_count = dumpnum - 1;
617 rmtioctl(MTFSF, dumpnum - 1);
620 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
627 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
628 fprintf(stdout, "Dumped from: %s",
629 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
630 if (spcl.c_host[0] == '\0')
632 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
633 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
634 fprintf(stdout, "Label: %s\n", spcl.c_label);
638 extractfile(char *name)
642 struct timeval timep[2];
646 curfile.action = USING;
648 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
649 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
650 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
651 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
652 #else /* __linux__ */
653 timep[0].tv_sec = curfile.dip->di_atime;
654 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
655 timep[1].tv_sec = curfile.dip->di_mtime;
656 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
657 #endif /* __linux__ */
658 mode = curfile.dip->di_mode;
659 flags = curfile.dip->di_flags;
660 switch (mode & IFMT) {
663 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
668 Vprintf(stdout, "skipped socket %s\n", name);
674 ep = lookupname(name);
675 if (ep == NULL || ep->e_flags & EXTRACT)
676 panic("unextracted directory %s\n", name);
680 Vprintf(stdout, "extract file %s\n", name);
681 return (genliteraldir(name, curfile.ino));
686 uid_t luid = curfile.dip->di_uid;
687 gid_t lgid = curfile.dip->di_gid;
691 getfile(xtrlnkfile, xtrlnkskip);
694 "%s: zero length symbolic link (ignored)\n", name);
697 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
700 (void) lchown(name, luid, lgid);
706 Vprintf(stdout, "extract fifo %s\n", name);
713 if (mkfifo(name, mode) < 0) {
714 warn("%s: cannot create fifo", name);
718 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
719 (void) chmod(name, mode);
722 (void) fsetflags(name, flags);
724 (void) chflags(name, flags);
732 Vprintf(stdout, "extract special file %s\n", name);
739 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
740 warn("%s: cannot create special file", name);
744 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
745 (void) chmod(name, mode);
749 warn("%s: fsetflags called on a special file", name);
750 (void) fsetflags(name, flags);
753 (void) chflags(name, flags);
760 Vprintf(stdout, "extract file %s\n", name);
767 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
769 warn("%s: cannot create file", name);
773 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
774 (void) fchmod(ofile, mode);
777 (void) setflags(ofile, flags);
779 (void) fchflags(ofile, flags);
781 getfile(xtrfile, xtrskip);
790 * skip over bit maps on the tape
796 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
801 * skip over a file on the tape
807 curfile.action = SKIP;
808 getfile(xtrnull, xtrnull);
812 * Extract a file from the tape.
813 * When an allocated block is found it is passed to the fill function;
814 * when an unallocated block (hole) is found, a zeroed buffer is passed
815 * to the skip function.
818 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
821 volatile int curblk = 0;
822 volatile quad_t size = spcl.c_dinode.di_size;
823 volatile int last_write_was_hole = 0;
824 quad_t origsize = size;
825 static char clearedbuf[MAXBSIZE];
826 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
829 if (spcl.c_type == TS_END)
830 panic("ran off end of tape\n");
831 if (spcl.c_magic != NFS_MAGIC)
832 panic("not at beginning of a file\n");
833 if (!gettingfile && setjmp(restart) != 0)
837 for (i = 0; i < spcl.c_count; i++) {
838 if (readmapflag || spcl.c_addr[i]) {
839 readtape(&buf[curblk++][0]);
840 if (curblk == fssize / TP_BSIZE) {
841 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
842 fssize : (curblk - 1) * TP_BSIZE + size));
844 last_write_was_hole = 0;
848 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
850 (curblk - 1) * TP_BSIZE + size));
853 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
855 last_write_was_hole = 1;
857 if ((size -= TP_BSIZE) <= 0) {
858 for (i++; i < spcl.c_count; i++)
859 if (readmapflag || spcl.c_addr[i])
864 if (gethead(&spcl) == GOOD && size > 0) {
865 if (spcl.c_type == TS_ADDR)
868 "Missing address (header) block for %s at %ld blocks\n",
869 curfile.name, (long)blksread);
872 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
873 last_write_was_hole = 0;
876 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
878 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
879 (*skip)(clearedbuf, skp);
882 last_write_was_hole = 1;
884 if (last_write_was_hole) {
885 FTRUNCATE(ofile, origsize);
892 * Write out the next block of a file.
895 xtrfile(char *buf, size_t size)
900 if (write(ofile, buf, (int) size) == -1)
901 err(1, "write error extracting inode %lu, name %s\nwrite",
902 (unsigned long)curfile.ino, curfile.name);
906 * Skip over a hole in a file.
910 xtrskip(char *buf, size_t size)
913 if (LSEEK(ofile, (off_t)size, SEEK_CUR) == -1)
914 err(1, "seek error extracting inode %lu, name %s\nlseek",
915 (unsigned long)curfile.ino, curfile.name);
919 * Collect the next block of a symbolic link.
922 xtrlnkfile(char *buf, size_t size)
926 if (pathlen > MAXPATHLEN)
927 errx(1, "symbolic link name: %s->%s%s; too long %d",
928 curfile.name, lnkbuf, buf, pathlen);
929 (void) strcat(lnkbuf, buf);
933 * Skip over a hole in a symbolic link (should never happen).
937 xtrlnkskip(char *buf, size_t size)
940 errx(1, "unallocated block in symbolic link %s", curfile.name);
944 * Collect the next block of a bit map.
947 xtrmap(char *buf, size_t size)
950 memmove(map, buf, size);
955 * Skip over a hole in a bit map (should never happen).
959 xtrmapskip(char *buf, size_t size)
962 panic("hole in map\n");
967 * Noop, when an extraction function is not needed.
971 xtrnull(char *buf, size_t size)
979 * Compare the next block of a file.
982 xtrcmpfile(char *buf, size_t size)
984 static char cmpbuf[MAXBSIZE];
989 if (read(ifile, cmpbuf, size) != size) {
990 fprintf(stderr, "%s: size has changed.\n",
996 if (memcmp(buf, cmpbuf, size) != 0) {
997 fprintf(stderr, "%s: tape and disk copies are different\n",
1005 * Skip over a hole in a file.
1008 xtrcmpskip(char *buf, size_t size)
1010 static char cmpbuf[MAXBSIZE];
1016 if (read(ifile, cmpbuf, size) != size) {
1017 fprintf(stderr, "%s: size has changed.\n",
1023 for (i = 0; i < size; ++i)
1024 if (cmpbuf[i] != '\0') {
1025 fprintf(stderr, "%s: tape and disk copies are different\n",
1031 #endif /* COMPARE_ONTHEFLY */
1033 #if !COMPARE_ONTHEFLY
1035 do_cmpfiles(int fd_tape, int fd_disk, long size)
1037 static char buf_tape[BUFSIZ];
1038 static char buf_disk[BUFSIZ];
1043 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1044 close(fd_tape), close(fd_disk);
1045 panic("do_cmpfiles: unexpected EOF[1]");
1047 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1048 close(fd_tape), close(fd_disk);
1049 panic("do_cmpfiles: unexpected EOF[2]");
1051 if (n_tape != n_disk) {
1052 close(fd_tape), close(fd_disk);
1053 panic("do_cmpfiles: sizes different!");
1055 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1061 /* for debugging compare problems */
1062 #undef COMPARE_FAIL_KEEP_FILE
1065 #ifdef COMPARE_FAIL_KEEP_FILE
1066 /* return true if tapefile should be unlinked after compare */
1071 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1073 struct STAT sbuf_tape;
1074 int fd_tape, fd_disk;
1076 if (STAT(tapefile, &sbuf_tape) != 0) {
1077 panic("Can't lstat tmp file %s: %s\n", tapefile,
1082 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1084 "%s: size changed from %ld to %ld.\n",
1085 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1087 #ifdef COMPARE_FAIL_KEEP_FILE
1094 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1095 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1098 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1100 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1104 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1105 fprintf(stderr, "%s: tape and disk copies are different\n",
1110 #ifdef COMPARE_FAIL_KEEP_FILE
1111 /* rename the file to live in /tmp */
1112 /* rename `tapefile' to /tmp/<basename of diskfile> */
1114 char *p = strrchr(diskfile, '/');
1115 char newname[MAXPATHLEN];
1117 panic("can't find / in %s\n", diskfile);
1119 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1120 if (rename(tapefile, newname)) {
1121 panic("rename from %s to %s failed: %s\n",
1125 fprintf(stderr, "*** %s saved to %s\n",
1130 /* don't unlink the file (it's not there anymore */
1139 #ifdef COMPARE_FAIL_KEEP_FILE
1143 #endif /* !COMPARE_ONTHEFLY */
1145 #if !COMPARE_ONTHEFLY
1146 static char tmpfilename[MAXPATHLEN];
1150 comparefile(char *name)
1155 #if !COMPARE_ONTHEFLY
1156 static char *tmpfile = NULL;
1160 if ((r = LSTAT(name, &sb)) != 0) {
1161 warn("%s: does not exist (%d)", name, r);
1167 curfile.name = name;
1168 curfile.action = USING;
1169 mode = curfile.dip->di_mode;
1171 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1172 (long)sb.st_size, mode);
1174 if (sb.st_mode != mode) {
1175 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1176 name, mode & 07777, sb.st_mode & 07777);
1179 switch (mode & IFMT) {
1193 char lbuf[MAXPATHLEN + 1];
1196 if (!(sb.st_mode & S_IFLNK)) {
1197 fprintf(stderr, "%s: is no longer a symbolic link\n",
1204 getfile(xtrlnkfile, xtrlnkskip);
1207 "%s: zero length symbolic link (ignored)\n",
1212 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1213 panic("readlink of %s failed: %s", name,
1218 if (strcmp(lbuf, lnkbuf) != 0) {
1220 "%s: symbolic link changed from %s to %s.\n",
1221 name, lnkbuf, lbuf);
1230 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1231 fprintf(stderr, "%s: no longer a special file\n",
1238 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1240 "%s: device changed from %d,%d to %d,%d.\n",
1242 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1243 (int)curfile.dip->di_rdev & 0xff,
1244 ((int)sb.st_rdev >> 8) & 0xff,
1245 (int)sb.st_rdev & 0xff);
1252 #if COMPARE_ONTHEFLY
1253 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1254 panic("Can't open %s: %s\n", name, strerror(errno));
1260 getfile(xtrcmpfile, xtrcmpskip);
1263 if (read(ifile, &c, 1) != 0) {
1264 fprintf(stderr, "%s: size has changed.\n",
1274 if (tmpfile == NULL) {
1275 /* argument to mktemp() must not be in RO space: */
1276 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1277 tmpfile = mktemp(&tmpfilename[0]);
1279 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1280 panic("cannot delete tmp file %s: %s\n",
1281 tmpfile, strerror(errno));
1283 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1284 panic("cannot create file temp file %s: %s\n",
1285 name, strerror(errno));
1287 getfile(xtrfile, xtrskip);
1288 (void) close(ofile);
1289 #ifdef COMPARE_FAIL_KEEP_FILE
1290 if (cmpfiles(tmpfile, name, &sb))
1293 cmpfiles(tmpfile, name, &sb);
1296 #endif /* COMPARE_ONTHEFLY */
1302 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1303 static void (*readtape_func)(char *) = readtape_set;
1306 * Read TP_BSIZE blocks from the input.
1307 * Handle read errors, and end of media.
1308 * Decompress compressed blocks.
1313 (*readtape_func)(buf); /* call the actual processing routine */
1317 * Set function pointer for readtape() routine. zflag and magtapein must
1318 * be correctly set before the first call to readtape().
1321 readtape_set(char *buf)
1324 readtape_func = readtape_uncompr;
1328 readtape_func = readtape_comprtape;
1330 readtape_func = readtape_comprfile;
1335 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1338 * This is the original readtape(), it's used for reading uncompressed input.
1339 * Read TP_BSIZE blocks from the input.
1340 * Handle read errors, and end of media.
1343 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1344 readtape_uncompr(char *buf)
1349 ssize_t rd, newvol, i;
1350 int cnt, seek_failed;
1352 if (blkcnt < numtrec) {
1353 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1359 for (i = 0; i < ntrec; i++)
1360 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1363 cnt = ntrec * TP_BSIZE;
1368 i = rmtread(&tapebuf[rd], cnt);
1371 i = read(mt, &tapebuf[rd], cnt);
1374 * Check for mid-tape short read error.
1375 * If found, skip rest of buffer and start with the next.
1377 if (!pipein && numtrec < ntrec && i > 0) {
1378 Dprintf(stdout, "mid-media short read error.\n");
1382 * Handle partial block read.
1384 if (pipein && i == 0 && rd > 0)
1386 else if (i > 0 && i != ntrec * TP_BSIZE) {
1395 * Short read. Process the blocks read.
1397 if (i % TP_BSIZE != 0)
1399 "partial block read: %ld should be %ld\n",
1400 (long)i, ntrec * TP_BSIZE);
1401 numtrec = i / TP_BSIZE;
1405 * Handle read error.
1408 fprintf(stderr, "Tape read error while ");
1409 switch (curfile.action) {
1411 fprintf(stderr, "trying to set up tape\n");
1414 fprintf(stderr, "trying to resynchronize\n");
1417 fprintf(stderr, "restoring %s\n", curfile.name);
1420 fprintf(stderr, "skipping over inode %lu\n",
1421 (unsigned long)curfile.ino);
1424 if (!yflag && !reply("continue"))
1426 i = ntrec * TP_BSIZE;
1427 memset(tapebuf, 0, (size_t)i);
1430 seek_failed = (rmtseek(i, 1) < 0);
1433 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (off_t)-1);
1436 warn("continuation failed");
1437 if (!yflag && !reply("assume end-of-tape and continue"))
1443 * Handle end of tape.
1446 Vprintf(stdout, "End-of-tape encountered\n");
1455 if (rd % TP_BSIZE != 0)
1456 panic("partial block read: %d should be %d\n",
1457 rd, ntrec * TP_BSIZE);
1459 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1462 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1467 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1470 * Read a compressed format block from a file or pipe and uncompress it.
1471 * Attempt to handle read errors, and end of file.
1474 readtape_comprfile(char *buf)
1476 long rl, size, i, ret;
1478 struct tapebuf *tpb;
1480 if (blkcnt < numtrec) {
1481 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1486 /* need to read the next block */
1488 for (i = 0; i < ntrec; i++)
1489 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1491 tpb = (struct tapebuf *) tapebuf;
1493 /* read the block prefix */
1494 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1500 if (size > bufsize) {
1501 /* something's wrong */
1502 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1505 tpb->length = bufsize;
1507 ret = read_a_block(mt, tpb->buf, size, &rl);
1511 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1512 if (tbufptr == NULL) {
1513 msg_read_error("File decompression error while");
1514 if (!yflag && !reply("continue"))
1516 memset(tapebuf, 0, bufsize);
1521 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1527 /* Errors while reading from a file or pipe are catastrophic. Since
1528 * there are no block boundaries, it's impossible to bypass the
1529 * block in error and find the start of the next block.
1532 /* It's possible to have multiple input files using -M
1533 * and -f file1,file2...
1535 Vprintf(stdout, "End-of-File encountered\n");
1545 msg_read_error("Read error while");
1546 /* if (!yflag && !reply("continue")) */
1551 * Read compressed data from a tape and uncompress it.
1552 * Handle read errors, and end of media.
1553 * Since a tape consists of separate physical blocks, we try
1554 * to recover from errors by repositioning the tape to the next
1558 readtape_comprtape(char *buf)
1562 struct tapebuf *tpb;
1565 if (blkcnt < numtrec) {
1566 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1571 /* need to read the next block */
1573 for (i = 0; i < ntrec; i++)
1574 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1576 tpb = (struct tapebuf *) tapebuf;
1578 /* read the block */
1579 size = bufsize + PREFIXSIZE;
1580 ret = read_a_block(mt, tapebuf, size, &rl);
1584 tbufptr = decompress_tapebuf(tpb, rl);
1585 if (tbufptr == NULL) {
1586 msg_read_error("Tape decompression error while");
1587 if (!yflag && !reply("continue"))
1589 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1595 /* Handle errors: EOT switches to the next volume, other errors
1596 * attempt to position the tape to the next block.
1599 Vprintf(stdout, "End-of-tape encountered\n");
1608 msg_read_error("Tape read error while");
1609 if (!yflag && !reply("continue"))
1611 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1616 rl = rmtioctl(MTFSR, 1);
1622 rl = ioctl(mt, MTIOCTOP, &tcom);
1626 warn("continuation failed");
1627 if (!yflag && !reply("assume end-of-tape and continue"))
1629 ret = 0; /* end of tape */
1635 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1641 * Decompress a struct tapebuf into a buffer. readsize is the size read
1642 * from the tape/file and is used for error messages. Returns a pointer
1643 * to the location of the uncompressed buffer or NULL on errors.
1644 * Adjust numtrec and complain for a short block.
1647 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
1649 /* If zflag is on, all blocks have a struct tapebuf prefix */
1650 /* zflag gets set in setup() from the dump header */
1651 int cresult, blocklen;
1652 unsigned long worklen;
1654 unsigned int worklen2;
1656 char *output = NULL,*reason = NULL, *lengtherr = NULL;
1658 /* build a length error message */
1659 blocklen = tpbin->length;
1660 if (readsize < blocklen + PREFIXSIZE)
1661 lengtherr = "short";
1663 if (readsize > blocklen + PREFIXSIZE)
1668 if (tpbin->compressed) {
1669 /* uncompress whatever we read, if it fails, complain later */
1670 if (tpbin->flags == COMPRESS_ZLIB) {
1672 errx(1,"This restore version doesn't support zlib decompression");
1674 cresult = uncompress(comprbuf, &worklen,
1675 tpbin->buf, blocklen);
1681 reason = "not enough memory";
1684 reason = "buffer too small";
1687 reason = "data error";
1692 if (cresult == Z_OK)
1696 #endif /* HAVE_ZLIB */
1698 if (tpbin->flags == COMPRESS_BZLIB) {
1700 errx(1,"This restore version doesn't support bzlib decompression");
1703 cresult = BZ2_bzBuffToBuffDecompress(
1704 comprbuf, &worklen2,
1705 tpbin->buf, blocklen, 0, 0);
1712 reason = "not enough memory";
1714 case BZ_OUTBUFF_FULL:
1715 reason = "buffer too small";
1718 case BZ_DATA_ERROR_MAGIC:
1719 case BZ_UNEXPECTED_EOF:
1720 reason = "data error";
1725 if (cresult == BZ_OK)
1729 #endif /* HAVE_BZLIB */
1733 output = tpbin->buf;
1737 numtrec = worklen / TP_BSIZE;
1738 if (worklen % TP_BSIZE != 0)
1739 reason = "length mismatch";
1743 fprintf(stderr, "%s compressed block: %d expected: %d\n",
1744 lengtherr, readsize, tpbin->length + PREFIXSIZE);
1745 fprintf(stderr, "decompression error, block %ld: %s\n",
1746 tpblksread+1, reason);
1754 * Print an error message for a read error.
1755 * This was exteracted from the original readtape().
1758 msg_read_error(char *m)
1760 switch (curfile.action) {
1762 fprintf(stderr, "%s trying to set up tape\n", m);
1765 fprintf(stderr, "%s trying to resynchronize\n", m);
1768 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
1771 fprintf(stderr, "%s skipping over inode %lu\n", m,
1772 (unsigned long)curfile.ino);
1776 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1779 * Read the first block and get the blocksize from it. Test
1780 * for a compressed dump tape/file. setup() will make the final
1781 * determination by checking the compressed flag if gethead()
1782 * finds a valid header. The test here is necessary to offset the buffer
1783 * by the size of the compressed prefix. zflag is set here so that
1784 * readtape_set can set the correct function pointer for readtape().
1785 * Note that the first block of each tape/file is not compressed
1786 * and does not have a prefix.
1789 findtapeblksize(void)
1793 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1794 struct s_spcl *spclpt = (struct s_spcl *) tapebuf;
1796 for (i = 0; i < ntrec; i++)
1797 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1801 * For a pipe or file, read in the first record. For a tape, read
1804 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
1806 if (read_a_block(mt, tapebuf, len, &i) <= 0)
1807 errx(1, "Tape read error on first record");
1810 * If the input is from a file or a pipe, we read TP_BSIZE
1811 * bytes looking for a dump header. If the dump is compressed
1812 * we need to read in the rest of the block, as determined
1813 * by c_ntrec in the dump header. The first block of the
1814 * dump is not compressed and does not have a prefix.
1817 if (spclpt->c_type == TS_TAPE
1818 && spclpt->c_flags & DR_COMPRESSED) {
1819 /* It's a compressed dump file, read in the */
1820 /* rest of the block based on spclpt->c_ntrec. */
1821 if (spclpt->c_ntrec > ntrec)
1822 errx(1, "Tape blocksize is too large, use "
1823 "\'-b %d\' ", spclpt->c_ntrec);
1824 ntrec = spclpt->c_ntrec;
1825 len = (ntrec - 1) * TP_BSIZE;
1829 /* read in the rest of the block based on bufsize */
1830 len = bufsize - TP_BSIZE;
1832 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1833 || (i != len && i % TP_BSIZE != 0))
1834 errx(1,"Error reading dump file header");
1837 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1839 } /* if (!magtapein) */
1842 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1843 * If the value of ntrec is too large, we read less than
1844 * what we asked for; adjust the value of ntrec and test for
1845 * a compressed dump tape.
1848 if (i % TP_BSIZE != 0) {
1849 /* may be old format compressed dump tape with a prefix */
1850 spclpt = (struct s_spcl *) tpb->buf;
1851 if (i % TP_BSIZE == PREFIXSIZE
1852 && tpb->compressed == 0
1853 && spclpt->c_type == TS_TAPE
1854 && spclpt->c_flags & DR_COMPRESSED) {
1857 if (tpb->length > bufsize)
1858 errx(1, "Tape blocksize is too large, use "
1859 "\'-b %d\' ", tpb->length / TP_BSIZE);
1862 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1865 ntrec = i / TP_BSIZE;
1866 if (spclpt->c_type == TS_TAPE) {
1867 if (spclpt->c_flags & DR_COMPRESSED)
1869 if (spclpt->c_ntrec > ntrec)
1870 errx(1, "Tape blocksize is too large, use "
1871 "\'-b %d\' ", spclpt->c_ntrec);
1874 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1878 * Read a block of data handling all of the messy details.
1880 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1888 i = rmtread(buf, size);
1891 i = read(fd, buf, size);
1894 break; /* EOD or error */
1897 break; /* block at a time for mt */
1900 *lengthread = len - size;
1919 setmagtapein(void) {
1920 struct mtget mt_stat;
1921 static int done = 0;
1926 /* need to know if input is really from a tape */
1929 magtapein = rmtioctl(MTNOP, 1) != -1;
1932 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
1935 Vprintf(stdout,"Input is from %s\n", magtapein? "tape": "file/pipe");
1939 * Read the next block from the tape.
1940 * Check to see if it is one of several vintage headers.
1941 * If it is an old style header, convert it to a new style header.
1942 * If it is not any valid header, return an error.
1945 gethead(struct s_spcl *buf)
1953 char dummy[TP_BSIZE];
1960 u_int16_t c_inumber;
1965 u_int16_t odi_nlink;
1981 readtape((char *)buf);
1982 if (buf->c_magic != NFS_MAGIC) {
1983 if (swabi(buf->c_magic) != NFS_MAGIC)
1986 Vprintf(stdout, "Note: Doing Byte swapping\n");
1990 if (checksum((int *)buf) == FAIL)
1993 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1996 readtape((char *)(&u_ospcl.s_ospcl));
1997 memset((char *)buf, 0, (long)TP_BSIZE);
1998 buf->c_type = u_ospcl.s_ospcl.c_type;
1999 buf->c_date = u_ospcl.s_ospcl.c_date;
2000 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
2001 buf->c_volume = u_ospcl.s_ospcl.c_volume;
2002 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
2003 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
2004 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
2005 buf->c_magic = u_ospcl.s_ospcl.c_magic;
2006 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
2007 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
2008 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
2009 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
2010 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
2011 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
2013 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
2014 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2015 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2016 #else /* __linux__ */
2017 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
2018 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2019 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2020 #endif /* __linux__ */
2021 buf->c_count = u_ospcl.s_ospcl.c_count;
2022 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
2023 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
2024 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
2026 buf->c_magic = NFS_MAGIC;
2029 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
2030 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
2031 qcvt.qval = buf->c_dinode.di_size;
2032 if (qcvt.val[0] || qcvt.val[1]) {
2033 printf("Note: Doing Quad swapping\n");
2038 qcvt.qval = buf->c_dinode.di_size;
2040 qcvt.val[1] = qcvt.val[0];
2042 buf->c_dinode.di_size = qcvt.qval;
2046 switch (buf->c_type) {
2051 * Have to patch up missing information in bit map headers
2054 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
2055 if (buf->c_count > TP_NINDIR)
2058 for (i = 0; i < buf->c_count; i++)
2063 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
2075 panic("gethead: unknown inode type %d\n", buf->c_type);
2079 * If we are restoring a filesystem with old format inodes,
2080 * copy the uid/gid to the new location.
2083 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2084 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2092 * Check that a header is where it belongs and predict the next header
2095 accthdr(struct s_spcl *header)
2097 static dump_ino_t previno = 0x7fffffff;
2098 static int prevtype;
2099 static long predict;
2102 if (header->c_type == TS_TAPE) {
2103 fprintf(stderr, "Volume header (%s inode format) ",
2104 oldinofmt ? "old" : "new");
2105 if (header->c_firstrec)
2106 fprintf(stderr, "begins with record %d",
2107 header->c_firstrec);
2108 fprintf(stderr, "\n");
2109 previno = 0x7fffffff;
2112 if (previno == 0x7fffffff)
2116 fprintf(stderr, "Dumped inodes map header");
2119 fprintf(stderr, "Used inodes map header");
2122 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2125 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2128 fprintf(stderr, "End of tape header");
2131 if (predict != blksread - 1)
2132 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2133 predict, blksread - 1);
2134 fprintf(stderr, "\n");
2137 if (header->c_type != TS_END)
2138 for (i = 0; i < header->c_count; i++)
2139 if (readmapflag || header->c_addr[i] != 0)
2143 prevtype = header->c_type;
2144 previno = header->c_inumber;
2148 * Find an inode header.
2149 * Complain if had to skip, and complain is set.
2152 findinode(struct s_spcl *header)
2154 static long skipcnt = 0;
2158 curfile.name = "<name unknown>";
2159 curfile.action = UNKNOWN;
2163 if (header->c_magic != NFS_MAGIC) {
2165 while (gethead(header) == FAIL ||
2166 header->c_date != dumpdate)
2169 switch (header->c_type) {
2173 * Skip up to the beginning of the next record
2175 for (i = 0; i < header->c_count; i++)
2176 if (header->c_addr[i])
2178 while (gethead(header) == FAIL ||
2179 header->c_date != dumpdate)
2184 curfile.dip = &header->c_dinode;
2185 curfile.ino = header->c_inumber;
2189 curfile.ino = maxino;
2193 curfile.name = "<file removal list>";
2197 curfile.name = "<file dump list>";
2201 panic("unexpected tape header\n");
2205 panic("unknown tape header type %d\n", spcl.c_type);
2209 } while (header->c_type == TS_ADDR);
2214 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2224 j = sizeof(union u_spcl) / sizeof(int);
2231 /* What happens if we want to read restore tapes
2232 for a 16bit int machine??? */
2238 if (i != CHECKSUM) {
2239 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2240 (unsigned long)curfile.ino, curfile.name);
2250 #include <varargs.h>
2255 msg(const char *fmt, ...)
2268 (void)vfprintf(stderr, fmt, ap);
2271 #endif /* RRESTORE */
2274 swab16(u_char *sp, int n)
2279 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2286 swab32(u_char *sp, int n)
2291 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2292 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2299 swab64(u_char *sp, int n)
2304 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2305 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2306 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2307 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2314 swabst(u_char *cp, u_char *sp)
2320 case '0': case '1': case '2': case '3': case '4':
2321 case '5': case '6': case '7': case '8': case '9':
2322 n = (n * 10) + (*cp++ - '0');
2325 case 's': case 'w': case 'h':
2343 default: /* Any other character, like 'b' counts as byte. */
2357 swabst((u_char *)"i", (u_char *)&x);
2365 swabst((u_char *)"l", (u_char *)&x);
2372 * get the current position of the tape
2375 GetTapePos(long *pos)
2380 if (ioctl(mt, MTIOCPOS, pos) == -1) {
2382 fprintf(stdout, "[%ld] error: %d (getting tapepos: %ld)\n",
2383 (unsigned long)getpid(), err, *pos);
2389 typedef struct mt_pos {
2392 } MTPosRec, *MTPosPtr;
2395 * go to specified position on tape
2398 GotoTapePos(long pos)
2405 if (ioctl(mt, MTIOCTOP, &buf) == -1) {
2407 fprintf(stdout, "[%ld] error: %d (setting tapepos: %ld)\n",
2408 (unsigned long)getpid(), err, pos);
2415 * read next data from tape to re-sync
2418 ReReadFromTape(void)
2422 if (gethead(&spcl) == FAIL) {
2424 fprintf(stdout, "DEBUG 1 gethead failed\n");
2432 RequestVol(long tnum)
2437 #endif /* USE_QFA */