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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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.54 2002/01/22 11:12:28 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;
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, ':')) {
195 errx(1, "cannot use -Q option on remote media");
198 source = strchr(host, ':');
200 if (rmthost(host) == 0)
204 if (strcmp(source, "-") == 0) {
206 * Since input is coming from a pipe we must establish
207 * our own connection to the terminal.
209 terminal = fopen(_PATH_TTY, "r");
210 if (terminal == NULL) {
211 warn("cannot open %s", _PATH_TTY);
212 terminal = fopen(_PATH_DEVNULL, "r");
213 if (terminal == NULL)
214 err(1, "cannot open %s", _PATH_DEVNULL);
218 setuid(getuid()); /* no longer need or want root privileges */
220 strncpy(magtapeprefix, source, MAXPATHLEN);
221 magtapeprefix[MAXPATHLEN-1] = '\0';
222 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
225 strncpy(magtape, source, MAXPATHLEN);
226 magtape[MAXPATHLEN - 1] = '\0';
230 newtapebuf(long size)
232 static int tapebufsize = -1;
235 bufsize = ntrec * TP_BSIZE;
236 if (size <= tapebufsize)
240 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
242 errx(1, "Cannot allocate space for tape buffer");
246 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
248 newcomprbuf(int size)
250 size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
251 if (buf_size <= comprlen)
254 if (comprbuf != NULL)
256 comprbuf = malloc(comprlen);
257 if (comprbuf == NULL)
258 errx(1, "Cannot allocate space for decompress buffer");
260 #endif /* HAVE_ZLIB || HAVE_BZLIB */
263 * Verify that the tape drive can be accessed and
264 * that it actually is a dump tape.
269 int i, j, *ip, bot_code;
272 Vprintf(stdout, "Verify tape and initialize maps\n");
274 msg("Launching %s\n", bot_script);
275 bot_code = system_command(bot_script, magtape, 1);
276 if (bot_code != 0 && bot_code != 1) {
277 msg("Restore aborted by the beginning of tape script\n");
283 mt = rmtopen(magtape, 0);
289 mt = OPEN(magtape, O_RDONLY, 0);
291 err(1, "%s", magtape);
297 if (gethead(&spcl) == FAIL) {
298 blkcnt--; /* push back this block */
302 if (gethead(&spcl) == FAIL)
303 errx(1, "Tape is not a dump tape");
304 fprintf(stderr, "Converting to new file system format.\n");
308 fprintf(stderr, "Dump tape is compressed.\n");
309 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
312 errx(1,"This restore version doesn't support decompression");
313 #endif /* HAVE_ZLIB */
316 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
317 endoftapemark.s_spcl.c_type = TS_END;
318 ip = (int *)&endoftapemark;
319 j = sizeof(union u_spcl) / sizeof(int);
324 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
326 if (vflag || command == 't' || command == 'C')
328 if (filesys[0] == '\0') {
330 strncpy(filesys, spcl.c_filesys, NAMELEN);
331 filesys[NAMELEN - 1] = '\0';
332 dirptr = strstr(filesys, " (dir");
336 dumptime = spcl.c_ddate;
337 dumpdate = spcl.c_date;
338 if (STAT(".", &stbuf) < 0)
339 err(1, "cannot stat .");
340 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
342 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
343 fssize = stbuf.st_blksize;
344 if (((fssize - 1) & fssize) != 0)
345 errx(1, "bad block size %ld", fssize);
346 if (spcl.c_volume != 1)
347 errx(1, "Tape is not volume 1 of the dump");
348 if (gethead(&spcl) == FAIL) {
349 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
350 panic("no header after volume mark!\n");
353 if (spcl.c_type != TS_CLRI)
354 errx(1, "Cannot find file removal list");
355 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
356 Dprintf(stdout, "maxino = %ld\n", (long)maxino);
357 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
359 errx(1, "no memory for active inode map");
361 curfile.action = USING;
362 getfile(xtrmap, xtrmapskip);
363 if (spcl.c_type != TS_BITS)
364 errx(1, "Cannot find file dump list");
365 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
366 if (map == (char *)NULL)
367 errx(1, "no memory for file dump list");
369 curfile.action = USING;
370 getfile(xtrmap, xtrmapskip);
372 * If there may be whiteout entries on the tape, pretend that the
373 * whiteout inode exists, so that the whiteout entries can be
377 SETINO(WINO, dumpmap);
381 * Prompt user to load a new dump volume.
382 * "Nextvol" is the next suggested volume to use.
383 * This suggested volume is enforced when doing full
384 * or incremental restores, but can be overridden by
385 * the user when only extracting a subset of the files.
390 long newvol = 0, wantnext = 0, i;
391 long saved_blksread = 0, saved_tpblksread = 0;
392 union u_spcl tmpspcl;
393 # define tmpbuf tmpspcl.s_spcl
395 int haderror = 0, bot_code = 1;
403 panic("Changing volumes on pipe input?\n");
408 saved_blksread = blksread;
409 saved_tpblksread = tpblksread;
412 exit(1); /* pipes do not get a second chance */
413 if (aflag || curfile.action != SKIP) {
420 while (newvol <= 0) {
421 if (tapesread == 0) {
422 fprintf(stderr, "%s%s%s%s%s",
423 "You have not read any tapes yet.\n",
424 "Unless you know which volume your",
425 " file(s) are on you should start\n",
426 "with the last volume and work",
427 " towards the first.\n");
429 fprintf(stderr, "You have read volumes");
431 for (i = 1; i < 32; i++)
432 if (tapesread & (1 << i)) {
433 fprintf(stderr, "%s%ld", buf, (long)i);
436 fprintf(stderr, "\n");
439 fprintf(stderr, "Specify next volume # (none if no more volumes): ");
440 (void) fflush(stderr);
441 (void) fgets(buf, TP_BSIZE, terminal);
442 } while (!feof(terminal) && buf[0] == '\n');
445 if (!strcmp(buf, "none\n")) {
452 "Volume numbers are positive numerics\n");
455 if (newvol == volno) {
456 tapesread |= 1 << volno;
461 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
462 magtape[MAXPATHLEN - 1] = '\0';
464 if (bot_script && !haderror) {
465 msg("Launching %s\n", bot_script);
466 bot_code = system_command(bot_script, magtape, newvol);
467 if (bot_code != 0 && bot_code != 1) {
468 msg("Restore aborted by the beginning of tape script\n");
472 if (haderror || (bot_code && !Mflag)) {
474 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
475 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
476 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
477 (void) fflush(stderr);
478 (void) fgets(buf, TP_BSIZE, terminal);
481 if (!strcmp(buf, "none\n")) {
485 if (buf[0] != '\n') {
487 (void) strncpy(magtape, buf, sizeof(magtape));
488 magtape[sizeof(magtape) - 1] = '\0';
489 if ((pos = strchr(magtape, '\n')))
490 magtape[pos - magtape] = '\0';
495 mt = rmtopen(magtape, 0);
498 mt = OPEN(magtape, O_RDONLY, 0);
501 fprintf(stderr, "Cannot open %s\n", magtape);
512 if (gethead(&tmpbuf) == FAIL) {
513 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
514 fprintf(stderr, "tape is not dump tape\n");
517 blksread = saved_blksread;
518 tpblksread = saved_tpblksread;
521 if (tmpbuf.c_volume != volno) {
522 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
525 blksread = saved_blksread;
526 tpblksread = saved_tpblksread;
529 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
530 fprintf(stderr, "Wrong dump date\n\tgot: %s",
531 ctime4(&tmpbuf.c_date));
532 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
535 blksread = saved_blksread;
536 tpblksread = saved_tpblksread;
539 tapesread |= 1 << volno;
540 blksread = saved_blksread;
541 tpblksread = saved_tpblksread;
543 * If continuing from the previous volume, skip over any
544 * blocks read already at the end of the previous volume.
546 * If coming to this volume at random, skip to the beginning
547 * of the next record.
549 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
550 tpblksread, (long)tmpbuf.c_firstrec);
551 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
553 tpblksread = tmpbuf.c_firstrec;
554 for (i = tmpbuf.c_count; i > 0; i--)
556 } else if (tmpbuf.c_firstrec > 0 &&
557 tmpbuf.c_firstrec < tpblksread - 1) {
559 * -1 since we've read the volume header
561 i = tpblksread - tmpbuf.c_firstrec - 1;
562 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
563 (long)i, i > 1 ? "s" : "");
568 if (curfile.action == USING) {
570 panic("active file into volume 1\n");
574 * Skip up to the beginning of the next record
576 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
577 for (i = tmpbuf.c_count; i > 0; i--)
579 (void) gethead(&spcl);
583 siglongjmp(restart, 1);
588 * Handle unexpected EOF.
594 if (gettingfile && curfile.action == USING) {
595 printf("Warning: %s %s\n",
596 "End-of-input encountered while extracting", curfile.name);
598 curfile.name = "<name unknown>";
599 curfile.action = UNKNOWN;
601 curfile.ino = maxino;
604 siglongjmp(restart, 1);
609 * handle multiple dumps per tape by skipping forward to the
617 if (dumpnum == 1 || volno != 1)
620 errx(1, "Cannot have multiple dumps on pipe input");
622 tcom.mt_count = dumpnum - 1;
625 rmtioctl(MTFSF, dumpnum - 1);
628 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
635 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
636 fprintf(stdout, "Dumped from: %s",
637 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
638 if (spcl.c_host[0] == '\0')
640 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
641 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
642 fprintf(stdout, "Label: %s\n", spcl.c_label);
646 extractfile(char *name)
650 struct timeval timep[2];
654 curfile.action = USING;
656 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
657 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
658 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
659 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
660 #else /* __linux__ */
661 timep[0].tv_sec = curfile.dip->di_atime;
662 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
663 timep[1].tv_sec = curfile.dip->di_mtime;
664 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
665 #endif /* __linux__ */
666 mode = curfile.dip->di_mode;
667 flags = curfile.dip->di_flags;
668 switch (mode & IFMT) {
671 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
676 Vprintf(stdout, "skipped socket %s\n", name);
682 ep = lookupname(name);
683 if (ep == NULL || ep->e_flags & EXTRACT)
684 panic("unextracted directory %s\n", name);
688 Vprintf(stdout, "extract file %s\n", name);
689 return (genliteraldir(name, curfile.ino));
694 uid_t luid = curfile.dip->di_uid;
695 gid_t lgid = curfile.dip->di_gid;
699 getfile(xtrlnkfile, xtrlnkskip);
702 "%s: zero length symbolic link (ignored)\n", name);
705 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
708 (void) lchown(name, luid, lgid);
714 Vprintf(stdout, "extract fifo %s\n", name);
721 if (mkfifo(name, mode) < 0) {
722 warn("%s: cannot create fifo", name);
726 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
727 (void) chmod(name, mode);
730 (void) fsetflags(name, flags);
732 (void) chflags(name, flags);
740 Vprintf(stdout, "extract special file %s\n", name);
747 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
748 warn("%s: cannot create special file", name);
752 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
753 (void) chmod(name, mode);
757 warn("%s: fsetflags called on a special file", name);
758 (void) fsetflags(name, flags);
761 (void) chflags(name, flags);
768 Vprintf(stdout, "extract file %s\n", name);
775 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
777 warn("%s: cannot create file", name);
781 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
782 (void) fchmod(ofile, mode);
785 (void) setflags(ofile, flags);
787 (void) fchflags(ofile, flags);
789 getfile(xtrfile, xtrskip);
798 * skip over bit maps on the tape
804 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
809 * skip over a file on the tape
815 curfile.action = SKIP;
816 getfile(xtrnull, xtrnull);
820 * Extract a file from the tape.
821 * When an allocated block is found it is passed to the fill function;
822 * when an unallocated block (hole) is found, a zeroed buffer is passed
823 * to the skip function.
826 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
829 volatile int curblk = 0;
830 volatile quad_t size = spcl.c_dinode.di_size;
831 volatile int last_write_was_hole = 0;
832 quad_t origsize = size;
833 static char clearedbuf[MAXBSIZE];
834 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
837 if (spcl.c_type == TS_END)
838 panic("ran off end of tape\n");
839 if (spcl.c_magic != NFS_MAGIC)
840 panic("not at beginning of a file\n");
841 if (!gettingfile && setjmp(restart) != 0)
845 for (i = 0; i < spcl.c_count; i++) {
846 if (readmapflag || spcl.c_addr[i]) {
847 readtape(&buf[curblk++][0]);
848 if (curblk == fssize / TP_BSIZE) {
849 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
850 fssize : (curblk - 1) * TP_BSIZE + size));
852 last_write_was_hole = 0;
856 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
858 (curblk - 1) * TP_BSIZE + size));
861 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
863 last_write_was_hole = 1;
865 if ((size -= TP_BSIZE) <= 0) {
866 for (i++; i < spcl.c_count; i++)
867 if (readmapflag || spcl.c_addr[i])
872 if (gethead(&spcl) == GOOD && size > 0) {
873 if (spcl.c_type == TS_ADDR)
876 "Missing address (header) block for %s at %ld blocks\n",
877 curfile.name, (long)blksread);
880 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
881 last_write_was_hole = 0;
884 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
886 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
887 (*skip)(clearedbuf, skp);
890 last_write_was_hole = 1;
892 if (last_write_was_hole) {
893 FTRUNCATE(ofile, origsize);
900 * Write out the next block of a file.
903 xtrfile(char *buf, size_t size)
908 if (write(ofile, buf, (int) size) == -1)
909 err(1, "write error extracting inode %lu, name %s\nwrite",
910 (unsigned long)curfile.ino, curfile.name);
914 * Skip over a hole in a file.
918 xtrskip(char *buf, size_t size)
921 if (LSEEK(ofile, (off_t)size, SEEK_CUR) == -1)
922 err(1, "seek error extracting inode %lu, name %s\nlseek",
923 (unsigned long)curfile.ino, curfile.name);
927 * Collect the next block of a symbolic link.
930 xtrlnkfile(char *buf, size_t size)
934 if (pathlen > MAXPATHLEN)
935 errx(1, "symbolic link name: %s->%s%s; too long %d",
936 curfile.name, lnkbuf, buf, pathlen);
937 (void) strcat(lnkbuf, buf);
941 * Skip over a hole in a symbolic link (should never happen).
945 xtrlnkskip(char *buf, size_t size)
948 errx(1, "unallocated block in symbolic link %s", curfile.name);
952 * Collect the next block of a bit map.
955 xtrmap(char *buf, size_t size)
958 memmove(map, buf, size);
963 * Skip over a hole in a bit map (should never happen).
967 xtrmapskip(char *buf, size_t size)
970 panic("hole in map\n");
975 * Noop, when an extraction function is not needed.
979 xtrnull(char *buf, size_t size)
987 * Compare the next block of a file.
990 xtrcmpfile(char *buf, size_t size)
992 static char cmpbuf[MAXBSIZE];
997 if (read(ifile, cmpbuf, size) != size) {
998 fprintf(stderr, "%s: size has changed.\n",
1004 if (memcmp(buf, cmpbuf, size) != 0) {
1005 fprintf(stderr, "%s: tape and disk copies are different\n",
1013 * Skip over a hole in a file.
1016 xtrcmpskip(char *buf, size_t size)
1018 static char cmpbuf[MAXBSIZE];
1024 if (read(ifile, cmpbuf, size) != size) {
1025 fprintf(stderr, "%s: size has changed.\n",
1031 for (i = 0; i < size; ++i)
1032 if (cmpbuf[i] != '\0') {
1033 fprintf(stderr, "%s: tape and disk copies are different\n",
1039 #endif /* COMPARE_ONTHEFLY */
1041 #if !COMPARE_ONTHEFLY
1043 do_cmpfiles(int fd_tape, int fd_disk, long size)
1045 static char buf_tape[BUFSIZ];
1046 static char buf_disk[BUFSIZ];
1051 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1052 close(fd_tape), close(fd_disk);
1053 panic("do_cmpfiles: unexpected EOF[1]");
1055 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1056 close(fd_tape), close(fd_disk);
1057 panic("do_cmpfiles: unexpected EOF[2]");
1059 if (n_tape != n_disk) {
1060 close(fd_tape), close(fd_disk);
1061 panic("do_cmpfiles: sizes different!");
1063 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1069 /* for debugging compare problems */
1070 #undef COMPARE_FAIL_KEEP_FILE
1073 #ifdef COMPARE_FAIL_KEEP_FILE
1074 /* return true if tapefile should be unlinked after compare */
1079 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1081 struct STAT sbuf_tape;
1082 int fd_tape, fd_disk;
1084 if (STAT(tapefile, &sbuf_tape) != 0) {
1085 panic("Can't lstat tmp file %s: %s\n", tapefile,
1090 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1092 "%s: size changed from %ld to %ld.\n",
1093 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1095 #ifdef COMPARE_FAIL_KEEP_FILE
1102 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1103 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1106 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1108 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1112 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1113 fprintf(stderr, "%s: tape and disk copies are different\n",
1118 #ifdef COMPARE_FAIL_KEEP_FILE
1119 /* rename the file to live in /tmp */
1120 /* rename `tapefile' to /tmp/<basename of diskfile> */
1122 char *p = strrchr(diskfile, '/');
1123 char newname[MAXPATHLEN];
1125 panic("can't find / in %s\n", diskfile);
1127 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1128 if (rename(tapefile, newname)) {
1129 panic("rename from %s to %s failed: %s\n",
1133 fprintf(stderr, "*** %s saved to %s\n",
1138 /* don't unlink the file (it's not there anymore */
1147 #ifdef COMPARE_FAIL_KEEP_FILE
1151 #endif /* !COMPARE_ONTHEFLY */
1153 #if !COMPARE_ONTHEFLY
1154 static char tmpfilename[MAXPATHLEN];
1158 comparefile(char *name)
1163 #if !COMPARE_ONTHEFLY
1164 static char *tmpfile = NULL;
1168 if ((r = LSTAT(name, &sb)) != 0) {
1169 warn("%s: does not exist (%d)", name, r);
1175 curfile.name = name;
1176 curfile.action = USING;
1177 mode = curfile.dip->di_mode;
1179 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1180 (long)sb.st_size, mode);
1182 if (sb.st_mode != mode) {
1183 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1184 name, mode & 07777, sb.st_mode & 07777);
1187 switch (mode & IFMT) {
1201 char lbuf[MAXPATHLEN + 1];
1204 if (!(sb.st_mode & S_IFLNK)) {
1205 fprintf(stderr, "%s: is no longer a symbolic link\n",
1212 getfile(xtrlnkfile, xtrlnkskip);
1215 "%s: zero length symbolic link (ignored)\n",
1220 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1221 panic("readlink of %s failed: %s", name,
1226 if (strcmp(lbuf, lnkbuf) != 0) {
1228 "%s: symbolic link changed from %s to %s.\n",
1229 name, lnkbuf, lbuf);
1238 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1239 fprintf(stderr, "%s: no longer a special file\n",
1246 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1248 "%s: device changed from %d,%d to %d,%d.\n",
1250 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1251 (int)curfile.dip->di_rdev & 0xff,
1252 ((int)sb.st_rdev >> 8) & 0xff,
1253 (int)sb.st_rdev & 0xff);
1260 #if COMPARE_ONTHEFLY
1261 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1262 panic("Can't open %s: %s\n", name, strerror(errno));
1268 getfile(xtrcmpfile, xtrcmpskip);
1271 if (read(ifile, &c, 1) != 0) {
1272 fprintf(stderr, "%s: size has changed.\n",
1282 if (tmpfile == NULL) {
1283 /* argument to mktemp() must not be in RO space: */
1284 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1285 tmpfile = mktemp(&tmpfilename[0]);
1287 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1288 panic("cannot delete tmp file %s: %s\n",
1289 tmpfile, strerror(errno));
1291 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1292 panic("cannot create file temp file %s: %s\n",
1293 name, strerror(errno));
1295 getfile(xtrfile, xtrskip);
1296 (void) close(ofile);
1297 #ifdef COMPARE_FAIL_KEEP_FILE
1298 if (cmpfiles(tmpfile, name, &sb))
1301 cmpfiles(tmpfile, name, &sb);
1304 #endif /* COMPARE_ONTHEFLY */
1310 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1311 static void (*readtape_func)(char *) = readtape_set;
1314 * Read TP_BSIZE blocks from the input.
1315 * Handle read errors, and end of media.
1316 * Decompress compressed blocks.
1321 (*readtape_func)(buf); /* call the actual processing routine */
1325 * Set function pointer for readtape() routine. zflag and magtapein must
1326 * be correctly set before the first call to readtape().
1329 readtape_set(char *buf)
1332 readtape_func = readtape_uncompr;
1336 readtape_func = readtape_comprtape;
1338 readtape_func = readtape_comprfile;
1343 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1346 * This is the original readtape(), it's used for reading uncompressed input.
1347 * Read TP_BSIZE blocks from the input.
1348 * Handle read errors, and end of media.
1351 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1352 readtape_uncompr(char *buf)
1357 ssize_t rd, newvol, i;
1358 int cnt, seek_failed;
1360 if (blkcnt < numtrec) {
1361 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1367 for (i = 0; i < ntrec; i++)
1368 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1371 cnt = ntrec * TP_BSIZE;
1376 i = rmtread(&tapebuf[rd], cnt);
1379 i = read(mt, &tapebuf[rd], cnt);
1382 * Check for mid-tape short read error.
1383 * If found, skip rest of buffer and start with the next.
1385 if (!pipein && numtrec < ntrec && i > 0) {
1386 Dprintf(stdout, "mid-media short read error.\n");
1390 * Handle partial block read.
1392 if (pipein && i == 0 && rd > 0)
1394 else if (i > 0 && i != ntrec * TP_BSIZE) {
1403 * Short read. Process the blocks read.
1405 if (i % TP_BSIZE != 0)
1407 "partial block read: %ld should be %ld\n",
1408 (long)i, ntrec * TP_BSIZE);
1409 numtrec = i / TP_BSIZE;
1413 * Handle read error.
1416 fprintf(stderr, "Tape read error while ");
1417 switch (curfile.action) {
1419 fprintf(stderr, "trying to set up tape\n");
1422 fprintf(stderr, "trying to resynchronize\n");
1425 fprintf(stderr, "restoring %s\n", curfile.name);
1428 fprintf(stderr, "skipping over inode %lu\n",
1429 (unsigned long)curfile.ino);
1432 if (!yflag && !reply("continue"))
1434 i = ntrec * TP_BSIZE;
1435 memset(tapebuf, 0, (size_t)i);
1438 seek_failed = (rmtseek(i, 1) < 0);
1441 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (off_t)-1);
1444 warn("continuation failed");
1445 if (!yflag && !reply("assume end-of-tape and continue"))
1451 * Handle end of tape.
1454 Vprintf(stdout, "End-of-tape encountered\n");
1463 if (rd % TP_BSIZE != 0)
1464 panic("partial block read: %d should be %d\n",
1465 rd, ntrec * TP_BSIZE);
1467 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1470 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1475 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1478 * Read a compressed format block from a file or pipe and uncompress it.
1479 * Attempt to handle read errors, and end of file.
1482 readtape_comprfile(char *buf)
1484 long rl, size, i, ret;
1486 struct tapebuf *tpb;
1488 if (blkcnt < numtrec) {
1489 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1494 /* need to read the next block */
1496 for (i = 0; i < ntrec; i++)
1497 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1499 tpb = (struct tapebuf *) tapebuf;
1501 /* read the block prefix */
1502 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1504 if (Vflag && (ret == 0 || rl < PREFIXSIZE || tpb->length == 0))
1511 if (size > bufsize) {
1512 /* something's wrong */
1513 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1516 tpb->length = bufsize;
1518 ret = read_a_block(mt, tpb->buf, size, &rl);
1522 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1523 if (tbufptr == NULL) {
1524 msg_read_error("File decompression error while");
1525 if (!yflag && !reply("continue"))
1527 memset(tapebuf, 0, bufsize);
1532 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1538 /* Errors while reading from a file or pipe are catastrophic. Since
1539 * there are no block boundaries, it's impossible to bypass the
1540 * block in error and find the start of the next block.
1543 /* It's possible to have multiple input files using -M
1544 * and -f file1,file2...
1546 Vprintf(stdout, "End-of-File encountered\n");
1556 msg_read_error("Read error while");
1557 /* if (!yflag && !reply("continue")) */
1562 * Read compressed data from a tape and uncompress it.
1563 * Handle read errors, and end of media.
1564 * Since a tape consists of separate physical blocks, we try
1565 * to recover from errors by repositioning the tape to the next
1569 readtape_comprtape(char *buf)
1573 struct tapebuf *tpb;
1576 if (blkcnt < numtrec) {
1577 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1582 /* need to read the next block */
1584 for (i = 0; i < ntrec; i++)
1585 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1587 tpb = (struct tapebuf *) tapebuf;
1589 /* read the block */
1590 size = bufsize + PREFIXSIZE;
1591 ret = read_a_block(mt, tapebuf, size, &rl);
1595 tbufptr = decompress_tapebuf(tpb, rl);
1596 if (tbufptr == NULL) {
1597 msg_read_error("Tape decompression error while");
1598 if (!yflag && !reply("continue"))
1600 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1606 /* Handle errors: EOT switches to the next volume, other errors
1607 * attempt to position the tape to the next block.
1610 Vprintf(stdout, "End-of-tape encountered\n");
1619 msg_read_error("Tape read error while");
1620 if (!yflag && !reply("continue"))
1622 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1627 rl = rmtioctl(MTFSR, 1);
1633 rl = ioctl(mt, MTIOCTOP, &tcom);
1637 warn("continuation failed");
1638 if (!yflag && !reply("assume end-of-tape and continue"))
1640 ret = 0; /* end of tape */
1646 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1652 * Decompress a struct tapebuf into a buffer. readsize is the size read
1653 * from the tape/file and is used for error messages. Returns a pointer
1654 * to the location of the uncompressed buffer or NULL on errors.
1655 * Adjust numtrec and complain for a short block.
1658 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
1660 /* If zflag is on, all blocks have a struct tapebuf prefix */
1661 /* zflag gets set in setup() from the dump header */
1662 int cresult, blocklen;
1663 unsigned long worklen;
1665 unsigned int worklen2;
1667 char *output = NULL,*reason = NULL, *lengtherr = NULL;
1669 /* build a length error message */
1670 blocklen = tpbin->length;
1671 if (readsize < blocklen + PREFIXSIZE)
1672 lengtherr = "short";
1674 if (readsize > blocklen + PREFIXSIZE)
1679 if (tpbin->compressed) {
1680 /* uncompress whatever we read, if it fails, complain later */
1681 if (tpbin->flags == COMPRESS_ZLIB) {
1683 errx(1,"This restore version doesn't support zlib decompression");
1685 cresult = uncompress(comprbuf, &worklen,
1686 tpbin->buf, blocklen);
1692 reason = "not enough memory";
1695 reason = "buffer too small";
1698 reason = "data error";
1703 if (cresult == Z_OK)
1707 #endif /* HAVE_ZLIB */
1709 if (tpbin->flags == COMPRESS_BZLIB) {
1711 errx(1,"This restore version doesn't support bzlib decompression");
1714 cresult = BZ2_bzBuffToBuffDecompress(
1715 comprbuf, &worklen2,
1716 tpbin->buf, blocklen, 0, 0);
1723 reason = "not enough memory";
1725 case BZ_OUTBUFF_FULL:
1726 reason = "buffer too small";
1729 case BZ_DATA_ERROR_MAGIC:
1730 case BZ_UNEXPECTED_EOF:
1731 reason = "data error";
1736 if (cresult == BZ_OK)
1740 #endif /* HAVE_BZLIB */
1744 output = tpbin->buf;
1748 numtrec = worklen / TP_BSIZE;
1749 if (worklen % TP_BSIZE != 0)
1750 reason = "length mismatch";
1754 fprintf(stderr, "%s compressed block: %d expected: %d\n",
1755 lengtherr, readsize, tpbin->length + PREFIXSIZE);
1756 fprintf(stderr, "decompression error, block %ld: %s\n",
1757 tpblksread+1, reason);
1765 * Print an error message for a read error.
1766 * This was exteracted from the original readtape().
1769 msg_read_error(char *m)
1771 switch (curfile.action) {
1773 fprintf(stderr, "%s trying to set up tape\n", m);
1776 fprintf(stderr, "%s trying to resynchronize\n", m);
1779 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
1782 fprintf(stderr, "%s skipping over inode %lu\n", m,
1783 (unsigned long)curfile.ino);
1787 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1790 * Read the first block and get the blocksize from it. Test
1791 * for a compressed dump tape/file. setup() will make the final
1792 * determination by checking the compressed flag if gethead()
1793 * finds a valid header. The test here is necessary to offset the buffer
1794 * by the size of the compressed prefix. zflag is set here so that
1795 * readtape_set can set the correct function pointer for readtape().
1796 * Note that the first block of each tape/file is not compressed
1797 * and does not have a prefix.
1800 findtapeblksize(void)
1804 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1805 struct s_spcl *spclpt = (struct s_spcl *) tapebuf;
1807 for (i = 0; i < ntrec; i++)
1808 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1812 * For a pipe or file, read in the first record. For a tape, read
1815 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
1817 if (read_a_block(mt, tapebuf, len, &i) <= 0)
1818 errx(1, "Tape read error on first record");
1821 * If the input is from a file or a pipe, we read TP_BSIZE
1822 * bytes looking for a dump header. If the dump is compressed
1823 * we need to read in the rest of the block, as determined
1824 * by c_ntrec in the dump header. The first block of the
1825 * dump is not compressed and does not have a prefix.
1828 if (spclpt->c_type == TS_TAPE
1829 && spclpt->c_flags & DR_COMPRESSED) {
1830 /* It's a compressed dump file, read in the */
1831 /* rest of the block based on spclpt->c_ntrec. */
1832 if (spclpt->c_ntrec > ntrec)
1833 errx(1, "Tape blocksize is too large, use "
1834 "\'-b %d\' ", spclpt->c_ntrec);
1835 ntrec = spclpt->c_ntrec;
1836 len = (ntrec - 1) * TP_BSIZE;
1840 /* read in the rest of the block based on bufsize */
1841 len = bufsize - TP_BSIZE;
1843 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1844 || (i != len && i % TP_BSIZE != 0))
1845 errx(1,"Error reading dump file header");
1848 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1850 } /* if (!magtapein) */
1853 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1854 * If the value of ntrec is too large, we read less than
1855 * what we asked for; adjust the value of ntrec and test for
1856 * a compressed dump tape.
1859 if (i % TP_BSIZE != 0) {
1860 /* may be old format compressed dump tape with a prefix */
1861 spclpt = (struct s_spcl *) tpb->buf;
1862 if (i % TP_BSIZE == PREFIXSIZE
1863 && tpb->compressed == 0
1864 && spclpt->c_type == TS_TAPE
1865 && spclpt->c_flags & DR_COMPRESSED) {
1868 if (tpb->length > bufsize)
1869 errx(1, "Tape blocksize is too large, use "
1870 "\'-b %d\' ", tpb->length / TP_BSIZE);
1873 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1876 ntrec = i / TP_BSIZE;
1877 if (spclpt->c_type == TS_TAPE) {
1878 if (spclpt->c_flags & DR_COMPRESSED)
1880 if (spclpt->c_ntrec > ntrec)
1881 errx(1, "Tape blocksize is too large, use "
1882 "\'-b %d\' ", spclpt->c_ntrec);
1885 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1889 * Read a block of data handling all of the messy details.
1891 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1899 i = rmtread(buf, size);
1902 i = read(fd, buf, size);
1905 break; /* EOD or error */
1908 break; /* block at a time for mt */
1911 *lengthread = len - size;
1930 setmagtapein(void) {
1931 struct mtget mt_stat;
1932 static int done = 0;
1937 /* need to know if input is really from a tape */
1943 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
1946 Vprintf(stdout,"Input is from %s\n",
1947 magtapein ? "tape" :
1948 Vflag ? "multi-volume (no tape)" : "file/pipe");
1952 * Read the next block from the tape.
1953 * Check to see if it is one of several vintage headers.
1954 * If it is an old style header, convert it to a new style header.
1955 * If it is not any valid header, return an error.
1958 gethead(struct s_spcl *buf)
1966 char dummy[TP_BSIZE];
1973 u_int16_t c_inumber;
1978 u_int16_t odi_nlink;
1994 readtape((char *)buf);
1995 if (buf->c_magic != NFS_MAGIC) {
1996 if (swabi(buf->c_magic) != NFS_MAGIC)
1999 Vprintf(stdout, "Note: Doing Byte swapping\n");
2003 if (checksum((int *)buf) == FAIL)
2006 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
2009 readtape((char *)(&u_ospcl.s_ospcl));
2010 memset((char *)buf, 0, (long)TP_BSIZE);
2011 buf->c_type = u_ospcl.s_ospcl.c_type;
2012 buf->c_date = u_ospcl.s_ospcl.c_date;
2013 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
2014 buf->c_volume = u_ospcl.s_ospcl.c_volume;
2015 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
2016 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
2017 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
2018 buf->c_magic = u_ospcl.s_ospcl.c_magic;
2019 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
2020 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
2021 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
2022 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
2023 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
2024 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
2026 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
2027 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2028 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2029 #else /* __linux__ */
2030 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
2031 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2032 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2033 #endif /* __linux__ */
2034 buf->c_count = u_ospcl.s_ospcl.c_count;
2035 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
2036 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
2037 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
2039 buf->c_magic = NFS_MAGIC;
2042 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
2043 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
2044 qcvt.qval = buf->c_dinode.di_size;
2045 if (qcvt.val[0] || qcvt.val[1]) {
2046 printf("Note: Doing Quad swapping\n");
2051 qcvt.qval = buf->c_dinode.di_size;
2053 qcvt.val[1] = qcvt.val[0];
2055 buf->c_dinode.di_size = qcvt.qval;
2059 switch (buf->c_type) {
2064 * Have to patch up missing information in bit map headers
2067 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
2068 if (buf->c_count > TP_NINDIR)
2071 for (i = 0; i < buf->c_count; i++)
2076 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
2088 panic("gethead: unknown inode type %d\n", buf->c_type);
2092 * If we are restoring a filesystem with old format inodes,
2093 * copy the uid/gid to the new location.
2096 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2097 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2105 * Check that a header is where it belongs and predict the next header
2108 accthdr(struct s_spcl *header)
2110 static dump_ino_t previno = 0x7fffffff;
2111 static int prevtype;
2112 static long predict;
2115 if (header->c_type == TS_TAPE) {
2116 fprintf(stderr, "Volume header (%s inode format) ",
2117 oldinofmt ? "old" : "new");
2118 if (header->c_firstrec)
2119 fprintf(stderr, "begins with record %d",
2120 header->c_firstrec);
2121 fprintf(stderr, "\n");
2122 previno = 0x7fffffff;
2125 if (previno == 0x7fffffff)
2129 fprintf(stderr, "Dumped inodes map header");
2132 fprintf(stderr, "Used inodes map header");
2135 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2138 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2141 fprintf(stderr, "End of tape header");
2144 if (predict != blksread - 1)
2145 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2146 predict, blksread - 1);
2147 fprintf(stderr, "\n");
2150 if (header->c_type != TS_END)
2151 for (i = 0; i < header->c_count; i++)
2152 if (readmapflag || header->c_addr[i] != 0)
2156 prevtype = header->c_type;
2157 previno = header->c_inumber;
2161 * Find an inode header.
2162 * Complain if had to skip, and complain is set.
2165 findinode(struct s_spcl *header)
2167 static long skipcnt = 0;
2171 curfile.name = "<name unknown>";
2172 curfile.action = UNKNOWN;
2176 if (header->c_magic != NFS_MAGIC) {
2178 while (gethead(header) == FAIL ||
2179 header->c_date != dumpdate)
2182 switch (header->c_type) {
2186 * Skip up to the beginning of the next record
2188 for (i = 0; i < header->c_count; i++)
2189 if (header->c_addr[i])
2191 while (gethead(header) == FAIL ||
2192 header->c_date != dumpdate)
2197 curfile.dip = &header->c_dinode;
2198 curfile.ino = header->c_inumber;
2202 curfile.ino = maxino;
2206 curfile.name = "<file removal list>";
2210 curfile.name = "<file dump list>";
2214 panic("unexpected tape header\n");
2218 panic("unknown tape header type %d\n", spcl.c_type);
2222 } while (header->c_type == TS_ADDR);
2227 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2237 j = sizeof(union u_spcl) / sizeof(int);
2244 /* What happens if we want to read restore tapes
2245 for a 16bit int machine??? */
2251 if (i != CHECKSUM) {
2252 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2253 (unsigned long)curfile.ino, curfile.name);
2263 #include <varargs.h>
2268 msg(const char *fmt, ...)
2281 (void)vfprintf(stderr, fmt, ap);
2284 #endif /* RRESTORE */
2287 swab16(u_char *sp, int n)
2292 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2299 swab32(u_char *sp, int n)
2304 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2305 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2312 swab64(u_char *sp, int n)
2317 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2318 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2319 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2320 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2327 swabst(u_char *cp, u_char *sp)
2333 case '0': case '1': case '2': case '3': case '4':
2334 case '5': case '6': case '7': case '8': case '9':
2335 n = (n * 10) + (*cp++ - '0');
2338 case 's': case 'w': case 'h':
2356 default: /* Any other character, like 'b' counts as byte. */
2370 swabst((u_char *)"i", (u_char *)&x);
2378 swabst((u_char *)"l", (u_char *)&x);
2385 * get the current position of the tape
2388 GetTapePos(long *pos)
2394 err = (ioctl(mt, MTIOCPOS, pos) < 0);
2397 *pos = lseek(mt, 0, SEEK_CUR);
2402 fprintf(stdout, "[%ld] error: %d (getting tapepos: %ld)\n",
2403 (unsigned long)getpid(), err, *pos);
2409 typedef struct mt_pos {
2412 } MTPosRec, *MTPosPtr;
2415 * go to specified position on tape
2418 GotoTapePos(long pos)
2426 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
2429 pos = lseek(mt, pos, SEEK_SET);
2434 fprintf(stdout, "[%ld] error: %d (setting tapepos: %ld)\n",
2435 (unsigned long)getpid(), err, pos);
2442 * read next data from tape to re-sync
2445 ReReadFromTape(void)
2449 if (gethead(&spcl) == FAIL) {
2451 fprintf(stdout, "DEBUG 1 gethead failed\n");
2459 RequestVol(long tnum)
2464 #endif /* USE_QFA */