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.67 2003/01/10 14:42:51 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 */
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 int converthead __P((struct s_spcl *));
138 static void converttapebuf __P((struct tapebuf *));
139 static void readtape __P((char *));
140 static void setdumpnum __P((void));
141 static u_int swabi __P((u_int));
143 static u_long swabl __P((u_long));
145 static u_char *swab64 __P((u_char *, int));
146 static u_char *swab32 __P((u_char *, int));
147 static u_char *swab16 __P((u_char *, int));
148 static void terminateinput __P((void));
149 static void xtrfile __P((char *, size_t));
150 static void xtrlnkfile __P((char *, size_t));
151 static void xtrlnkskip __P((char *, size_t));
152 static void xtrmap __P((char *, size_t));
153 static void xtrmapskip __P((char *, size_t));
154 static void xtrskip __P((char *, size_t));
155 static void setmagtapein __P((void));
157 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
158 static void newcomprbuf __P((int));
159 static void (*readtape_func) __P((char *));
160 static void readtape_set __P((char *));
161 static void readtape_uncompr __P((char *));
162 static void readtape_comprfile __P((char *));
163 static void readtape_comprtape __P((char *));
164 static char *decompress_tapebuf __P((struct tapebuf *, int));
165 static void msg_read_error __P((char *));
167 static int read_a_block __P((int, char *, size_t, long *));
168 #define PREFIXSIZE sizeof(struct tapebuf)
170 #define COMPARE_ONTHEFLY 1
173 static int ifile; /* input file for compare */
174 static int cmperror; /* compare error */
175 static void xtrcmpfile __P((char *, size_t));
176 static void xtrcmpskip __P((char *, size_t));
179 static int readmapflag;
180 static int readingmaps; /* set to 1 while reading the maps */
183 * Set up an input source. This is called from main.c before setup() is.
186 setinput(char *source)
192 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
196 if (strchr(source, ':')) {
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;
273 Vprintf(stdout, "Verify tape and initialize maps\n");
274 if (Afile == NULL && bot_script) {
275 msg("Launching %s\n", bot_script);
276 bot_code = system_command(bot_script, magtape, 1);
277 if (bot_code != 0 && bot_code != 1) {
278 msg("Restore aborted by the beginning of tape script\n");
290 mt = rmtopen(temptape, O_RDONLY);
296 mt = OPEN(temptape, O_RDONLY, 0);
298 err(1, "%s", magtape);
304 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
305 readtape_func = readtape_set;
309 if (gethead(&spcl) == FAIL) {
310 blkcnt--; /* push back this block */
314 if (gethead(&spcl) == FAIL)
315 errx(1, "Tape is not a dump tape");
316 fprintf(stderr, "Converting to new file system format.\n");
320 fprintf(stderr, "Dump tape is compressed.\n");
321 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
322 errx(1,"This restore version doesn't support decompression");
323 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
326 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
327 endoftapemark.s_spcl.c_type = TS_END;
328 ip = (int *)&endoftapemark;
329 j = sizeof(union u_spcl) / sizeof(int);
334 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
336 if (vflag || command == 't' || command == 'C')
339 if (tapeposflag && (unsigned long)spcl.c_date != qfadumpdate)
340 errx(1, "different QFA/dumpdates detected\n");
342 if (filesys[0] == '\0') {
344 strncpy(filesys, spcl.c_filesys, NAMELEN);
345 filesys[NAMELEN - 1] = '\0';
346 dirptr = strstr(filesys, " (dir");
350 dumptime = spcl.c_ddate;
351 dumpdate = spcl.c_date;
352 if (STAT(".", &stbuf) < 0)
353 err(1, "cannot stat .");
354 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
356 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
357 fssize = stbuf.st_blksize;
358 if (((fssize - 1) & fssize) != 0)
359 errx(1, "bad block size %ld", fssize);
360 if (spcl.c_volume != 1)
361 errx(1, "Tape is not volume 1 of the dump");
362 if (gethead(&spcl) == FAIL) {
363 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
364 panic("no header after volume mark!\n");
368 if (spcl.c_type != TS_CLRI)
369 errx(1, "Cannot find file removal list");
370 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
371 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
373 errx(1, "no memory for active inode map");
375 curfile.action = USING;
376 getfile(xtrmap, xtrmapskip);
377 while (spcl.c_type == TS_ADDR) {
378 /* Recompute maxino and the map */
379 dump_ino_t oldmaxino = maxino;
380 maxino += (spcl.c_count * TP_BSIZE * NBBY) + 1;
381 resizemaps(oldmaxino, maxino);
383 spcl.c_dinode.di_size = spcl.c_count * TP_BSIZE;
384 getfile(xtrmap, xtrmapskip);
386 Dprintf(stdout, "maxino = %lu\n", (unsigned long)maxino);
387 if (spcl.c_type != TS_BITS)
388 errx(1, "Cannot find file dump list");
389 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
390 if (map == (char *)NULL)
391 errx(1, "no memory for file dump list");
393 curfile.action = USING;
394 getfile(xtrmap, xtrmapskip);
395 while (spcl.c_type == TS_ADDR) {
396 spcl.c_dinode.di_size = spcl.c_count * TP_BSIZE;
397 getfile(xtrmap, xtrmapskip);
400 * If there may be whiteout entries on the tape, pretend that the
401 * whiteout inode exists, so that the whiteout entries can be
405 SETINO(WINO, dumpmap);
411 * Prompt user to load a new dump volume.
412 * "Nextvol" is the next suggested volume to use.
413 * This suggested volume is enforced when doing full
414 * or incremental restores, but can be overridden by
415 * the user when only extracting a subset of the files.
420 long newvol = 0, wantnext = 0, i;
421 long saved_blksread = 0, saved_tpblksread = 0;
422 union u_spcl tmpspcl;
423 # define tmpbuf tmpspcl.s_spcl
425 int haderror = 0, bot_code = 1;
435 panic("Changing volumes on pipe input?\n");
440 saved_blksread = blksread;
441 saved_tpblksread = tpblksread;
442 #if defined(USE_QFA) && defined(sunos)
443 if (createtapeposflag || tapeposflag)
448 exit(1); /* pipes do not get a second chance */
449 if (aflag || curfile.action != SKIP) {
456 while (newvol <= 0) {
457 if (tapesread == 0) {
458 fprintf(stderr, "%s%s%s%s%s",
459 "You have not read any volumes yet.\n",
460 "Unless you know which volume your",
461 " file(s) are on you should start\n",
462 "with the last volume and work",
463 " towards the first.\n");
465 fprintf(stderr, "You have read volumes");
467 for (i = 1; i < 32; i++)
468 if (tapesread & (1 << i)) {
469 fprintf(stderr, "%s%ld", buf, (long)i);
472 fprintf(stderr, "\n");
475 fprintf(stderr, "Specify next volume # (none if no more volumes): ");
476 (void) fflush(stderr);
477 (void) fgets(buf, TP_BSIZE, terminal);
478 } while (!feof(terminal) && buf[0] == '\n');
481 if (!strcmp(buf, "none\n")) {
488 "Volume numbers are positive numerics\n");
491 if (newvol == volno) {
492 tapesread |= 1 << volno;
493 #if defined(USE_QFA) && defined(sunos)
494 if (createtapeposflag || tapeposflag) {
495 if (OpenSMTCmt(magtape) < 0) {
506 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
507 magtape[MAXPATHLEN - 1] = '\0';
509 if (bot_script && !haderror) {
510 msg("Launching %s\n", bot_script);
511 bot_code = system_command(bot_script, magtape, newvol);
512 if (bot_code != 0 && bot_code != 1) {
513 msg("Restore aborted by the beginning of tape script\n");
517 if (haderror || (bot_code && !Mflag)) {
519 fprintf(stderr, "Mount volume %ld\n", (long)newvol);
520 fprintf(stderr, "Enter ``none'' if there are no more volumes\n");
521 fprintf(stderr, "otherwise enter volume name (default: %s) ", magtape);
522 (void) fflush(stderr);
523 (void) fgets(buf, TP_BSIZE, terminal);
526 if (!strcmp(buf, "none\n")) {
530 if (buf[0] != '\n') {
532 (void) strncpy(magtape, buf, sizeof(magtape));
533 magtape[sizeof(magtape) - 1] = '\0';
534 if ((pos = strchr(magtape, '\n')))
535 magtape[pos - magtape] = '\0';
540 mt = rmtopen(magtape, O_RDONLY);
543 mt = OPEN(magtape, O_RDONLY, 0);
546 fprintf(stderr, "Cannot open %s\n", magtape);
553 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
554 readtape_func = readtape_set;
560 if (gethead(&tmpbuf) == FAIL) {
561 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
562 fprintf(stderr, "tape is not dump tape\n");
565 blksread = saved_blksread;
566 tpblksread = saved_tpblksread;
569 if (tmpbuf.c_volume != volno) {
570 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
573 blksread = saved_blksread;
574 tpblksread = saved_tpblksread;
577 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
578 fprintf(stderr, "Wrong dump date\n\tgot: %s",
579 ctime4(&tmpbuf.c_date));
580 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
583 blksread = saved_blksread;
584 tpblksread = saved_tpblksread;
587 tapesread |= 1 << volno;
589 * If continuing from the previous volume, skip over any
590 * blocks read already at the end of the previous volume.
592 * If coming to this volume at random, skip to the beginning
593 * of the next record.
596 fprintf(stderr, "Dump tape is compressed.\n");
597 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
598 errx(1,"This restore version doesn't support decompression");
599 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
601 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
602 tpblksread - 1, (long)tmpbuf.c_firstrec);
603 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
605 tpblksread = tmpbuf.c_firstrec + 1;
606 for (i = tmpbuf.c_count; i > 0; i--)
608 } else if (tmpbuf.c_firstrec > 0 &&
609 tmpbuf.c_firstrec < tpblksread - 1) {
611 * -1 since we've read the volume header
613 i = tpblksread - tmpbuf.c_firstrec - 1;
614 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
615 (long)i, i > 1 ? "s" : "");
620 if (curfile.action == USING) {
622 panic("active file into volume 1\n");
626 * Skip up to the beginning of the next record
628 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
629 for (i = tmpbuf.c_count; i > 0; i--)
631 (void) gethead(&spcl);
635 siglongjmp(restart, 1);
640 * Handle unexpected EOF.
646 if (gettingfile && curfile.action == USING) {
647 printf("Warning: %s %s\n",
648 "End-of-input encountered while extracting", curfile.name);
650 curfile.name = "<name unknown>";
651 curfile.action = UNKNOWN;
653 curfile.ino = maxino;
656 siglongjmp(restart, 1);
661 * handle multiple dumps per tape by skipping forward to the
669 if (dumpnum == 1 || volno != 1)
672 errx(1, "Cannot have multiple dumps on pipe input");
674 tcom.mt_count = dumpnum - 1;
677 rmtioctl(MTFSF, dumpnum - 1);
680 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
687 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
688 fprintf(stdout, "Dumped from: %s",
689 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
690 if (spcl.c_host[0] == '\0')
692 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
693 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
694 fprintf(stdout, "Label: %s\n", spcl.c_label);
702 if (volinfo[1] == ROOTINO) {
703 printf("Starting inode numbers by volume:\n");
704 for (i = 1; i < (int)TP_NINOS && volinfo[i] != 0; ++i)
705 printf("\tVolume %d: %lu\n", i, (unsigned long)volinfo[i]);
711 time_t tv_sec; /* seconds */
712 suseconds_t tv_usec; /* and microseconds */
717 extractfile(struct entry *ep, int doremove)
721 struct timeval timep[2];
722 char *name = myname(ep);
724 /* If removal is requested (-r mode) do remove it unless
725 * we are extracting a metadata only inode */
726 if (spcl.c_flags & DR_METAONLY) {
727 Vprintf(stdout, "file %s is metadata only\n", name);
732 ep->e_flags &= ~REMOVED;
737 curfile.action = USING;
738 #if defined(__linux__) || defined(sunos)
739 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
740 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
741 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
742 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
743 #else /* __linux__ || sunos */
744 timep[0].tv_sec = curfile.dip->di_atime;
745 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
746 timep[1].tv_sec = curfile.dip->di_mtime;
747 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
748 #endif /* __linux__ */
749 mode = curfile.dip->di_mode;
750 flags = curfile.dip->di_flags;
751 switch (mode & IFMT) {
754 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
759 Vprintf(stdout, "skipped socket %s\n", name);
765 if (ep == NULL || ep->e_flags & EXTRACT)
766 panic("unextracted directory %s\n", name);
770 Vprintf(stdout, "extract file %s\n", name);
771 return (genliteraldir(name, curfile.ino));
776 uid_t luid = curfile.dip->di_uid;
777 gid_t lgid = curfile.dip->di_gid;
779 if (! (spcl.c_flags & DR_METAONLY)) {
782 getfile(xtrlnkfile, xtrlnkskip);
785 "%s: zero length symbolic link (ignored)\n", name);
788 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
795 (void) lchown(name, luid, lgid);
801 Vprintf(stdout, "extract fifo %s\n", name);
806 if (! (spcl.c_flags & DR_METAONLY)) {
809 if (mkfifo(name, mode) < 0) {
810 warn("%s: cannot create fifo", name);
815 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
816 (void) chmod(name, mode);
819 (void) fsetflags(name, flags);
821 (void) chflags(name, flags);
829 Vprintf(stdout, "extract special file %s\n", name);
834 if (! (spcl.c_flags & DR_METAONLY)) {
837 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
838 warn("%s: cannot create special file", name);
843 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
844 (void) chmod(name, mode);
848 warn("%s: fsetflags called on a special file", name);
849 (void) fsetflags(name, flags);
852 (void) chflags(name, flags);
860 uid_t luid = curfile.dip->di_uid;
861 gid_t lgid = curfile.dip->di_gid;
863 Vprintf(stdout, "extract file %s\n", name);
868 if (! (spcl.c_flags & DR_METAONLY)) {
871 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
873 warn("%s: cannot create file", name);
877 getfile(xtrfile, xtrskip);
882 (void) chown(name, luid, lgid);
883 (void) chmod(name, mode);
886 (void) fsetflags(name, flags);
888 (void) chflags(name, flags);
898 * skip over bit maps on the tape
904 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
909 * skip over a file on the tape
915 curfile.action = SKIP;
916 getfile(xtrnull, xtrnull);
920 * Extract a file from the tape.
921 * When an allocated block is found it is passed to the fill function;
922 * when an unallocated block (hole) is found, a zeroed buffer is passed
923 * to the skip function.
926 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
929 volatile int curblk = 0;
930 volatile quad_t size = spcl.c_dinode.di_size;
931 volatile int last_write_was_hole = 0;
932 quad_t origsize = size;
933 static char clearedbuf[MAXBSIZE];
934 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
937 if (spcl.c_type == TS_END)
938 panic("ran off end of tape\n");
939 if (spcl.c_magic != NFS_MAGIC)
940 panic("not at beginning of a file\n");
941 if (!gettingfile && setjmp(restart) != 0)
945 for (i = 0; i < spcl.c_count; i++) {
946 if (readmapflag || spcl.c_addr[i]) {
947 readtape(&buf[curblk++][0]);
948 if (curblk == fssize / TP_BSIZE) {
949 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
950 fssize : (curblk - 1) * TP_BSIZE + size));
952 last_write_was_hole = 0;
956 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
958 (curblk - 1) * TP_BSIZE + size));
961 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
963 last_write_was_hole = 1;
965 if ((size -= TP_BSIZE) <= 0) {
966 for (i++; i < spcl.c_count; i++)
967 if (readmapflag || spcl.c_addr[i])
972 if (gethead(&spcl) == GOOD && size > 0) {
973 if (spcl.c_type == TS_ADDR)
976 "Missing address (header) block for %s at %ld blocks\n",
977 curfile.name, (long)blksread);
980 (*fill)((char *)buf, (size_t)((curblk * TP_BSIZE) + size));
981 last_write_was_hole = 0;
984 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
986 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
987 (*skip)(clearedbuf, skp);
990 last_write_was_hole = 1;
992 if (last_write_was_hole) {
993 FTRUNCATE(ofile, origsize);
1001 * Write out the next block of a file.
1004 xtrfile(char *buf, size_t size)
1009 if (write(ofile, buf, (int) size) == -1)
1010 err(1, "write error extracting inode %lu, name %s\nwrite",
1011 (unsigned long)curfile.ino, curfile.name);
1015 * Skip over a hole in a file.
1019 xtrskip(UNUSED(char *buf), size_t size)
1022 if (LSEEK(ofile, (OFF_T)size, SEEK_CUR) == -1)
1023 err(1, "seek error extracting inode %lu, name %s\nlseek",
1024 (unsigned long)curfile.ino, curfile.name);
1028 * Collect the next block of a symbolic link.
1031 xtrlnkfile(char *buf, size_t size)
1035 if (pathlen > MAXPATHLEN)
1036 errx(1, "symbolic link name: %s->%s%s; too long %d",
1037 curfile.name, lnkbuf, buf, pathlen);
1038 (void) strcat(lnkbuf, buf);
1042 * Skip over a hole in a symbolic link (should never happen).
1046 xtrlnkskip(UNUSED(char *buf), UNUSED(size_t size))
1049 errx(1, "unallocated block in symbolic link %s", curfile.name);
1053 * Collect the next block of a bit map.
1056 xtrmap(char *buf, size_t size)
1059 memmove(map, buf, size);
1064 * Skip over a hole in a bit map (should never happen).
1068 xtrmapskip(UNUSED(char *buf), size_t size)
1071 panic("hole in map\n");
1076 * Noop, when an extraction function is not needed.
1080 xtrnull(UNUSED(char *buf), UNUSED(size_t size))
1086 #if COMPARE_ONTHEFLY
1088 * Compare the next block of a file.
1091 xtrcmpfile(char *buf, size_t size)
1093 static char cmpbuf[MAXBSIZE];
1098 if (read(ifile, cmpbuf, size) != (ssize_t)size) {
1099 fprintf(stderr, "%s: size has changed.\n",
1105 if (memcmp(buf, cmpbuf, size) != 0) {
1106 fprintf(stderr, "%s: tape and disk copies are different\n",
1114 * Skip over a hole in a file.
1117 xtrcmpskip(UNUSED(char *buf), size_t size)
1119 static char cmpbuf[MAXBSIZE];
1125 if (read(ifile, cmpbuf, size) != (ssize_t)size) {
1126 fprintf(stderr, "%s: size has changed.\n",
1132 for (i = 0; i < (int)size; ++i)
1133 if (cmpbuf[i] != '\0') {
1134 fprintf(stderr, "%s: tape and disk copies are different\n",
1140 #endif /* COMPARE_ONTHEFLY */
1142 #if !COMPARE_ONTHEFLY
1144 do_cmpfiles(int fd_tape, int fd_disk, long size)
1146 static char buf_tape[BUFSIZ];
1147 static char buf_disk[BUFSIZ];
1152 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1153 close(fd_tape), close(fd_disk);
1154 panic("do_cmpfiles: unexpected EOF[1]");
1156 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1157 close(fd_tape), close(fd_disk);
1158 panic("do_cmpfiles: unexpected EOF[2]");
1160 if (n_tape != n_disk) {
1161 close(fd_tape), close(fd_disk);
1162 panic("do_cmpfiles: sizes different!");
1164 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1170 /* for debugging compare problems */
1171 #undef COMPARE_FAIL_KEEP_FILE
1174 #ifdef COMPARE_FAIL_KEEP_FILE
1175 /* return true if tapefile should be unlinked after compare */
1180 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1182 struct STAT sbuf_tape;
1183 int fd_tape, fd_disk;
1185 if (STAT(tapefile, &sbuf_tape) != 0) {
1186 panic("Can't lstat tmp file %s: %s\n", tapefile,
1191 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1193 "%s: size changed from %ld to %ld.\n",
1194 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1196 #ifdef COMPARE_FAIL_KEEP_FILE
1203 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1204 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1207 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1209 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1213 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1214 fprintf(stderr, "%s: tape and disk copies are different\n",
1219 #ifdef COMPARE_FAIL_KEEP_FILE
1220 /* rename the file to live in /tmp */
1221 /* rename `tapefile' to /tmp/<basename of diskfile> */
1223 char *p = strrchr(diskfile, '/');
1224 char newname[MAXPATHLEN];
1226 panic("can't find / in %s\n", diskfile);
1228 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1229 if (rename(tapefile, newname)) {
1230 panic("rename from %s to %s failed: %s\n",
1234 fprintf(stderr, "*** %s saved to %s\n",
1239 /* don't unlink the file (it's not there anymore */
1248 #ifdef COMPARE_FAIL_KEEP_FILE
1252 #endif /* !COMPARE_ONTHEFLY */
1254 #if !COMPARE_ONTHEFLY
1255 static char tmpfilename[MAXPATHLEN];
1259 comparefile(char *name)
1264 #if !COMPARE_ONTHEFLY
1265 static char *tmpfile = NULL;
1269 if ((r = LSTAT(name, &sb)) != 0) {
1270 warn("%s: does not exist (%d)", name, r);
1276 curfile.name = name;
1277 curfile.action = USING;
1278 mode = curfile.dip->di_mode;
1280 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1281 (long)sb.st_size, mode);
1283 if (sb.st_mode != mode) {
1284 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1285 name, mode & 07777, sb.st_mode & 07777);
1288 if (spcl.c_flags & DR_METAONLY) {
1292 switch (mode & IFMT) {
1306 char lbuf[MAXPATHLEN + 1];
1309 if (!(sb.st_mode & S_IFLNK)) {
1310 fprintf(stderr, "%s: is no longer a symbolic link\n",
1317 getfile(xtrlnkfile, xtrlnkskip);
1320 "%s: zero length symbolic link (ignored)\n",
1325 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1326 panic("readlink of %s failed: %s", name,
1331 if (strcmp(lbuf, lnkbuf) != 0) {
1333 "%s: symbolic link changed from %s to %s.\n",
1334 name, lnkbuf, lbuf);
1343 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1344 fprintf(stderr, "%s: no longer a special file\n",
1351 if (sb.st_rdev != (dev_t)curfile.dip->di_rdev) {
1353 "%s: device changed from %d,%d to %d,%d.\n",
1355 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1356 (int)curfile.dip->di_rdev & 0xff,
1357 ((int)sb.st_rdev >> 8) & 0xff,
1358 (int)sb.st_rdev & 0xff);
1365 #if COMPARE_ONTHEFLY
1366 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1367 panic("Can't open %s: %s\n", name, strerror(errno));
1373 getfile(xtrcmpfile, xtrcmpskip);
1376 if (read(ifile, &c, 1) != 0) {
1377 fprintf(stderr, "%s: size has changed.\n",
1387 if (tmpfile == NULL) {
1388 /* argument to mktemp() must not be in RO space: */
1389 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1390 tmpfile = mktemp(&tmpfilename[0]);
1392 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1393 panic("cannot delete tmp file %s: %s\n",
1394 tmpfile, strerror(errno));
1396 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1397 panic("cannot create file temp file %s: %s\n",
1398 name, strerror(errno));
1400 getfile(xtrfile, xtrskip);
1401 (void) close(ofile);
1402 #ifdef COMPARE_FAIL_KEEP_FILE
1403 if (cmpfiles(tmpfile, name, &sb))
1406 cmpfiles(tmpfile, name, &sb);
1409 #endif /* COMPARE_ONTHEFLY */
1415 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1416 static void (*readtape_func)(char *) = readtape_set;
1419 * Read TP_BSIZE blocks from the input.
1420 * Handle read errors, and end of media.
1421 * Decompress compressed blocks.
1426 (*readtape_func)(buf); /* call the actual processing routine */
1430 * Set function pointer for readtape() routine. zflag and magtapein must
1431 * be correctly set before the first call to readtape().
1434 readtape_set(char *buf)
1437 readtape_func = readtape_uncompr;
1441 readtape_func = readtape_comprtape;
1443 readtape_func = readtape_comprfile;
1448 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1451 * This is the original readtape(), it's used for reading uncompressed input.
1452 * Read TP_BSIZE blocks from the input.
1453 * Handle read errors, and end of media.
1456 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1457 readtape_uncompr(char *buf)
1462 ssize_t rd, newvol, i;
1463 int cnt, seek_failed;
1465 if (blkcnt < numtrec) {
1466 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1472 for (i = 0; i < ntrec; i++)
1473 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1476 cnt = ntrec * TP_BSIZE;
1479 if (createtapeposflag)
1480 (void)GetTapePos(&curtapepos);
1485 i = rmtread(&tapebuf[rd], cnt);
1488 i = read(mt, &tapebuf[rd], cnt);
1491 * Check for mid-tape short read error.
1492 * If found, skip rest of buffer and start with the next.
1494 if (!pipein && numtrec < ntrec && i > 0) {
1495 Dprintf(stdout, "mid-media short read error.\n");
1499 * Handle partial block read.
1501 if (pipein && i == 0 && rd > 0)
1503 else if (i > 0 && i != ntrec * TP_BSIZE) {
1512 * Short read. Process the blocks read.
1514 if (i % TP_BSIZE != 0)
1516 "partial block read: %ld should be %ld\n",
1517 (long)i, ntrec * TP_BSIZE);
1518 numtrec = i / TP_BSIZE;
1522 * Handle read error.
1525 fprintf(stderr, "Tape read error while ");
1526 switch (curfile.action) {
1528 fprintf(stderr, "trying to set up tape\n");
1531 fprintf(stderr, "trying to resynchronize\n");
1534 fprintf(stderr, "restoring %s\n", curfile.name);
1537 fprintf(stderr, "skipping over inode %lu\n",
1538 (unsigned long)curfile.ino);
1541 if (!yflag && !reply("continue"))
1543 i = ntrec * TP_BSIZE;
1544 memset(tapebuf, 0, (size_t)i);
1547 seek_failed = (rmtseek(i, 1) < 0);
1550 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (OFF_T)-1);
1553 warn("continuation failed");
1554 if (!yflag && !reply("assume end-of-tape and continue"))
1560 * Handle end of tape.
1563 Vprintf(stdout, "End-of-tape encountered\n");
1572 if (rd % TP_BSIZE != 0)
1573 panic("partial block read: %d should be %d\n",
1574 rd, ntrec * TP_BSIZE);
1576 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1579 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1584 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1587 * Read a compressed format block from a file or pipe and uncompress it.
1588 * Attempt to handle read errors, and end of file.
1591 readtape_comprfile(char *buf)
1593 long rl, size, i, ret;
1595 struct tapebuf *tpb;
1597 if (blkcnt < numtrec) {
1598 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1603 /* need to read the next block */
1605 for (i = 0; i < ntrec; i++)
1606 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1608 tpb = (struct tapebuf *) tapebuf;
1610 /* read the block prefix */
1611 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1612 converttapebuf(tpb);
1614 if (Vflag && (ret == 0 || rl < (int)PREFIXSIZE || tpb->length == 0))
1621 if (size > bufsize) {
1622 /* something's wrong */
1623 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1626 tpb->length = bufsize;
1628 ret = read_a_block(mt, tpb->buf, size, &rl);
1632 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1633 if (tbufptr == NULL) {
1634 msg_read_error("File decompression error while");
1635 if (!yflag && !reply("continue"))
1637 memset(tapebuf, 0, bufsize);
1642 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1648 /* Errors while reading from a file or pipe are catastrophic. Since
1649 * there are no block boundaries, it's impossible to bypass the
1650 * block in error and find the start of the next block.
1653 /* It's possible to have multiple input files using -M
1654 * and -f file1,file2...
1656 Vprintf(stdout, "End-of-File encountered\n");
1666 msg_read_error("Read error while");
1667 /* if (!yflag && !reply("continue")) */
1672 * Read compressed data from a tape and uncompress it.
1673 * Handle read errors, and end of media.
1674 * Since a tape consists of separate physical blocks, we try
1675 * to recover from errors by repositioning the tape to the next
1679 readtape_comprtape(char *buf)
1683 struct tapebuf *tpb;
1686 if (blkcnt < numtrec) {
1687 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1692 /* need to read the next block */
1694 for (i = 0; i < ntrec; i++)
1695 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1697 tpb = (struct tapebuf *) tapebuf;
1699 /* read the block */
1700 size = bufsize + PREFIXSIZE;
1701 ret = read_a_block(mt, tapebuf, size, &rl);
1705 converttapebuf(tpb);
1706 tbufptr = decompress_tapebuf(tpb, rl);
1707 if (tbufptr == NULL) {
1708 msg_read_error("Tape decompression error while");
1709 if (!yflag && !reply("continue"))
1711 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1717 /* Handle errors: EOT switches to the next volume, other errors
1718 * attempt to position the tape to the next block.
1721 Vprintf(stdout, "End-of-tape encountered\n");
1730 msg_read_error("Tape read error while");
1731 if (!yflag && !reply("continue"))
1733 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1738 rl = rmtioctl(MTFSR, 1);
1744 rl = ioctl(mt, MTIOCTOP, &tcom);
1748 warn("continuation failed");
1749 if (!yflag && !reply("assume end-of-tape and continue"))
1751 ret = 0; /* end of tape */
1757 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1763 * Decompress a struct tapebuf into a buffer. readsize is the size read
1764 * from the tape/file and is used for error messages. Returns a pointer
1765 * to the location of the uncompressed buffer or NULL on errors.
1766 * Adjust numtrec and complain for a short block.
1769 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
1771 /* If zflag is on, all blocks have a struct tapebuf prefix */
1772 /* zflag gets set in setup() from the dump header */
1773 int cresult, blocklen;
1774 unsigned long worklen;
1776 unsigned int worklen2;
1778 char *output = NULL,*reason = NULL, *lengtherr = NULL;
1780 /* build a length error message */
1781 blocklen = tpbin->length;
1782 if (readsize < blocklen + (int)PREFIXSIZE)
1783 lengtherr = "short";
1785 if (readsize > blocklen + (int)PREFIXSIZE)
1790 if (tpbin->compressed) {
1791 /* uncompress whatever we read, if it fails, complain later */
1792 if (tpbin->flags == COMPRESS_ZLIB) {
1794 errx(1,"This restore version doesn't support zlib decompression");
1796 cresult = uncompress(comprbuf, &worklen,
1797 tpbin->buf, blocklen);
1803 reason = "not enough memory";
1806 reason = "buffer too small";
1809 reason = "data error";
1814 if (cresult == Z_OK)
1818 #endif /* HAVE_ZLIB */
1820 if (tpbin->flags == COMPRESS_BZLIB) {
1822 errx(1,"This restore version doesn't support bzlib decompression");
1825 cresult = BZ2_bzBuffToBuffDecompress(
1826 comprbuf, &worklen2,
1827 tpbin->buf, blocklen, 0, 0);
1834 reason = "not enough memory";
1836 case BZ_OUTBUFF_FULL:
1837 reason = "buffer too small";
1840 case BZ_DATA_ERROR_MAGIC:
1841 case BZ_UNEXPECTED_EOF:
1842 reason = "data error";
1847 if (cresult == BZ_OK)
1851 #endif /* HAVE_BZLIB */
1855 output = tpbin->buf;
1859 numtrec = worklen / TP_BSIZE;
1860 if (worklen % TP_BSIZE != 0)
1861 reason = "length mismatch";
1865 fprintf(stderr, "%s compressed block: %d expected: %d\n",
1866 lengtherr, readsize, tpbin->length + PREFIXSIZE);
1867 fprintf(stderr, "decompression error, block %ld: %s\n",
1868 tpblksread+1, reason);
1876 * Print an error message for a read error.
1877 * This was exteracted from the original readtape().
1880 msg_read_error(char *m)
1882 switch (curfile.action) {
1884 fprintf(stderr, "%s trying to set up tape\n", m);
1887 fprintf(stderr, "%s trying to resynchronize\n", m);
1890 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
1893 fprintf(stderr, "%s skipping over inode %lu\n", m,
1894 (unsigned long)curfile.ino);
1898 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1901 * Read the first block and get the blocksize from it. Test
1902 * for a compressed dump tape/file. setup() will make the final
1903 * determination by checking the compressed flag if gethead()
1904 * finds a valid header. The test here is necessary to offset the buffer
1905 * by the size of the compressed prefix. zflag is set here so that
1906 * readtape_set can set the correct function pointer for readtape().
1907 * Note that the first block of each tape/file is not compressed
1908 * and does not have a prefix.
1911 findtapeblksize(void)
1915 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1916 struct s_spcl spclpt;
1918 for (i = 0; i < ntrec; i++)
1919 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1923 * For a pipe or file, read in the first record. For a tape, read
1926 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
1928 if (read_a_block(mt, tapebuf, len, &i) <= 0)
1929 errx(1, "Tape read error on first record");
1931 memcpy(&spclpt, tapebuf, TP_BSIZE);
1932 if (converthead(&spclpt) == FAIL) {
1934 if (converthead(&spclpt) == FAIL) {
1935 /* Special case for old compressed tapes with prefix */
1936 if (magtapein && (i % TP_BSIZE != 0))
1938 errx(1, "Tape is not a dump tape");
1940 fprintf(stderr, "Converting to new file system format.\n");
1943 * If the input is from a file or a pipe, we read TP_BSIZE
1944 * bytes looking for a dump header. If the dump is compressed
1945 * we need to read in the rest of the block, as determined
1946 * by c_ntrec in the dump header. The first block of the
1947 * dump is not compressed and does not have a prefix.
1950 if (spclpt.c_type == TS_TAPE
1951 && spclpt.c_flags & DR_COMPRESSED) {
1952 /* It's a compressed dump file, read in the */
1953 /* rest of the block based on spclpt.c_ntrec. */
1954 if (spclpt.c_ntrec > ntrec)
1955 errx(1, "Tape blocksize is too large, use "
1956 "\'-b %d\' ", spclpt.c_ntrec);
1957 ntrec = spclpt.c_ntrec;
1958 len = (ntrec - 1) * TP_BSIZE;
1962 /* read in the rest of the block based on bufsize */
1963 len = bufsize - TP_BSIZE;
1965 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1966 || (i != (long)len && i % TP_BSIZE != 0))
1967 errx(1,"Error reading dump file header");
1970 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1972 } /* if (!magtapein) */
1975 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1976 * If the value of ntrec is too large, we read less than
1977 * what we asked for; adjust the value of ntrec and test for
1978 * a compressed dump tape.
1980 if (i % TP_BSIZE != 0) {
1982 /* may be old format compressed dump tape with a prefix */
1983 memcpy(&spclpt, tpb->buf, TP_BSIZE);
1985 if (converthead(&spclpt) == FAIL) {
1987 if (converthead(&spclpt) == FAIL)
1988 errx(1, "Tape is not a dump tape");
1989 fprintf(stderr, "Converting to new file system format.\n");
1991 if (i % TP_BSIZE == PREFIXSIZE
1992 && tpb->compressed == 0
1993 && spclpt.c_type == TS_TAPE
1994 && spclpt.c_flags & DR_COMPRESSED) {
1997 if (tpb->length > bufsize)
1998 errx(1, "Tape blocksize is too large, use "
1999 "\'-b %d\' ", tpb->length / TP_BSIZE);
2002 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2005 ntrec = i / TP_BSIZE;
2006 if (spclpt.c_type == TS_TAPE) {
2007 if (spclpt.c_flags & DR_COMPRESSED)
2009 if (spclpt.c_ntrec > ntrec)
2010 errx(1, "Tape blocksize is too large, use "
2011 "\'-b %d\' ", spclpt.c_ntrec);
2014 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
2018 * Read a block of data handling all of the messy details.
2020 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
2028 i = rmtread(buf, size);
2031 i = read(fd, buf, size);
2034 break; /* EOD or error */
2037 break; /* block at a time for mt */
2040 *lengthread = len - size;
2059 setmagtapein(void) {
2060 struct mtget mt_stat;
2061 static int done = 0;
2066 /* need to know if input is really from a tape */
2072 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
2075 Vprintf(stdout,"Input is from %s\n",
2076 magtapein ? "tape" :
2077 Vflag ? "multi-volume (no tape)" : "file/pipe");
2081 * Read the next block from the tape.
2082 * Check to see if it is one of several vintage headers.
2083 * If it is an old style header, convert it to a new style header.
2084 * If it is not any valid header, return an error.
2087 gethead(struct s_spcl *buf)
2089 readtape((char *)buf);
2090 return converthead(buf);
2094 converthead(struct s_spcl *buf)
2102 char dummy[TP_BSIZE];
2109 u_int16_t c_inumber;
2114 u_int16_t odi_nlink;
2130 if (buf->c_magic != NFS_MAGIC) {
2131 if (swabi(buf->c_magic) != NFS_MAGIC)
2134 Vprintf(stdout, "Note: Doing Byte swapping\n");
2138 if (checksum((int *)buf) == FAIL)
2141 swabst((u_char *)"8i4s31i528bi192b3i", (u_char *)buf);
2144 memcpy(&u_ospcl.s_ospcl, buf, TP_BSIZE);
2145 memset((char *)buf, 0, (long)TP_BSIZE);
2146 buf->c_type = u_ospcl.s_ospcl.c_type;
2147 buf->c_date = u_ospcl.s_ospcl.c_date;
2148 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
2149 buf->c_volume = u_ospcl.s_ospcl.c_volume;
2150 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
2151 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
2152 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
2153 buf->c_magic = u_ospcl.s_ospcl.c_magic;
2154 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
2155 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
2156 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
2157 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
2158 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
2159 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
2160 #if defined(__linux__) || defined(sunos)
2161 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
2162 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2163 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2164 #else /* __linux__ || sunos */
2165 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
2166 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2167 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2168 #endif /* __linux__ || sunos */
2169 buf->c_count = u_ospcl.s_ospcl.c_count;
2170 memmove(buf->c_addr, u_ospcl.s_ospcl.c_fill, (long)256);
2171 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
2172 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
2174 buf->c_magic = NFS_MAGIC;
2177 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
2178 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
2179 qcvt.qval = buf->c_dinode.di_size;
2180 if (qcvt.val[0] || qcvt.val[1]) {
2181 Vprintf(stdout, "Note: Doing Quad swapping\n");
2186 qcvt.qval = buf->c_dinode.di_size;
2188 qcvt.val[1] = qcvt.val[0];
2190 buf->c_dinode.di_size = qcvt.qval;
2194 switch (buf->c_type) {
2199 * Have to patch up missing information in bit map headers
2202 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
2203 if (buf->c_count > TP_NINDIR)
2206 for (i = 0; i < buf->c_count; i++)
2211 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
2216 if (buf->c_flags & DR_INODEINFO) {
2217 memcpy(volinfo, buf->c_inos, TP_NINOS * sizeof(dump_ino_t));
2219 swabst((u_char *)"128i", (u_char *)volinfo);
2228 panic("gethead: unknown inode type %d\n", buf->c_type);
2232 * If we are restoring a filesystem with old format inodes,
2233 * copy the uid/gid to the new location.
2236 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2237 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2245 converttapebuf(struct tapebuf *tpb)
2249 unsigned int length:28;
2250 unsigned int flags:3;
2251 unsigned int compressed:1;
2253 swabst((u_char *)"i", (u_char *)tpb);
2254 memcpy(&tb, tpb, 4);
2255 tpb->length = tb.length;
2256 tpb->flags = tb.flags;
2257 tpb->compressed = tb.compressed;
2262 * Check that a header is where it belongs and predict the next header
2265 accthdr(struct s_spcl *header)
2267 static dump_ino_t previno = 0x7fffffff;
2268 static int prevtype;
2269 static long predict;
2272 if (header->c_type == TS_TAPE) {
2273 fprintf(stderr, "Volume header (%s inode format) ",
2274 oldinofmt ? "old" : "new");
2275 if (header->c_firstrec)
2276 fprintf(stderr, "begins with record %d",
2277 header->c_firstrec);
2278 fprintf(stderr, "\n");
2279 previno = 0x7fffffff;
2282 if (previno == 0x7fffffff)
2286 fprintf(stderr, "Dumped inodes map header");
2289 fprintf(stderr, "Used inodes map header");
2292 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2295 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2298 fprintf(stderr, "End of tape header");
2301 if (predict != blksread - 1)
2302 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2303 predict, blksread - 1);
2304 fprintf(stderr, "\n");
2307 if (header->c_type != TS_END)
2308 for (i = 0; i < header->c_count; i++)
2309 if (readmapflag || header->c_addr[i] != 0)
2313 prevtype = header->c_type;
2314 previno = header->c_inumber;
2318 * Find an inode header.
2319 * Complain if had to skip, and complain is set.
2322 findinode(struct s_spcl *header)
2324 static long skipcnt = 0;
2328 curfile.name = "<name unknown>";
2329 curfile.action = UNKNOWN;
2333 if (header->c_magic != NFS_MAGIC) {
2335 while (gethead(header) == FAIL ||
2336 header->c_date != dumpdate)
2339 switch (header->c_type) {
2343 * Skip up to the beginning of the next record
2345 for (i = 0; i < header->c_count; i++)
2346 if (header->c_addr[i])
2348 while (gethead(header) == FAIL ||
2349 header->c_date != dumpdate)
2354 curfile.dip = &header->c_dinode;
2355 curfile.ino = header->c_inumber;
2359 curfile.ino = maxino;
2363 curfile.name = "<file removal list>";
2367 curfile.name = "<file dump list>";
2371 panic("unexpected tape header\n");
2375 panic("unknown tape header type %d\n", spcl.c_type);
2379 } while (header->c_type == TS_ADDR);
2384 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2394 j = sizeof(union u_spcl) / sizeof(int);
2401 /* What happens if we want to read restore tapes
2402 for a 16bit int machine??? */
2408 if (i != CHECKSUM) {
2409 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2410 (unsigned long)curfile.ino, curfile.name);
2420 #include <varargs.h>
2425 msg(const char *fmt, ...)
2438 (void)vfprintf(stderr, fmt, ap);
2441 #endif /* RRESTORE */
2444 swab16(u_char *sp, int n)
2449 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2456 swab32(u_char *sp, int n)
2461 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2462 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2469 swab64(u_char *sp, int n)
2474 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2475 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2476 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2477 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2484 swabst(u_char *cp, u_char *sp)
2490 case '0': case '1': case '2': case '3': case '4':
2491 case '5': case '6': case '7': case '8': case '9':
2492 n = (n * 10) + (*cp++ - '0');
2495 case 's': case 'w': case 'h':
2513 default: /* Any other character, like 'b' counts as byte. */
2527 swabst((u_char *)"i", (u_char *)&x);
2535 swabst((u_char *)"l", (u_char *)&x);
2542 * get the current position of the tape
2545 GetTapePos(long long *pos)
2551 *pos = (long long) rmtseek(0, SEEK_CUR);
2560 err = (ioctl(mt, MTIOCPOS, &mtpos) < 0);
2561 *pos = (long long)mtpos;
2564 *pos = LSEEK(mt, 0, SEEK_CUR);
2570 fprintf(stdout, "[%ld] error: %d (getting tapepos: %lld)\n",
2571 (unsigned long)getpid(), err, *pos);
2577 typedef struct mt_pos {
2580 } MTPosRec, *MTPosPtr;
2583 * go to specified position on tape
2586 GotoTapePos(long long pos)
2592 err = (rmtseek(pos, SEEK_SET) < 0);
2599 buf.mt_count = (int) pos;
2600 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
2603 pos = LSEEK(mt, pos, SEEK_SET);
2609 fprintf(stdout, "[%ld] error: %d (setting tapepos: %lld)\n",
2610 (unsigned long)getpid(), err, pos);
2617 * read next data from tape to re-sync
2620 ReReadFromTape(void)
2624 if (gethead(&spcl) == FAIL) {
2626 fprintf(stdout, "DEBUG 1 gethead failed\n");
2634 ReReadInodeFromTape(dump_ino_t theino)
2643 } while (!(spcl.c_inumber == theino && spcl.c_type == TS_INODE && spcl.c_date == dumpdate) && (cntloop < 32));
2645 fprintf(stderr, "%ld reads\n", cntloop);
2646 if (cntloop == 32) {
2647 fprintf(stderr, "DEBUG: bufsize %d\n", bufsize);
2648 fprintf(stderr, "DEBUG: ntrec %ld\n", ntrec);
2649 fprintf(stderr, "DEBUG: %ld reads\n", cntloop);
2655 #endif /* USE_QFA */
2658 RequestVol(long tnum)