]> git.wh0rd.org - dump.git/blob - restore/tape.c
Fixed the compressed multi-volume dump + restore.
[dump.git] / restore / tape.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
7 */
8
9 /*
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.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
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.
33 *
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
44 * SUCH DAMAGE.
45 */
46
47 #ifndef lint
48 static const char rcsid[] =
49 "$Id: tape.c,v 1.45 2001/07/20 09:01:46 stelian Exp $";
50 #endif /* not lint */
51
52 #include <config.h>
53 #include <compatlfs.h>
54 #include <errno.h>
55 #include <compaterr.h>
56 #include <system.h>
57 #include <setjmp.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include <sys/param.h>
64 #include <sys/file.h>
65 #include <sys/mtio.h>
66 #include <sys/stat.h>
67
68 #ifdef __linux__
69 #include <sys/time.h>
70 #include <time.h>
71 #ifdef HAVE_EXT2FS_EXT2_FS_H
72 #include <ext2fs/ext2_fs.h>
73 #else
74 #include <linux/ext2_fs.h>
75 #endif
76 #include <ext2fs/ext2fs.h>
77 #include <bsdcompat.h>
78 #else /* __linux__ */
79 #include <ufs/ufs/dinode.h>
80 #endif /* __linux__ */
81 #include <protocols/dumprestore.h>
82
83 #ifdef HAVE_ZLIB
84 #include <zlib.h>
85 #endif /* HAVE_ZLIB */
86
87 #include "restore.h"
88 #include "extern.h"
89 #include "pathnames.h"
90
91 #ifdef USE_QFA
92 int noresyncmesg = 0;
93 #endif /* USE_QFA */
94 static long fssize = MAXBSIZE;
95 static int mt = -1;
96 static int pipein = 0;
97 static int magtapein = 0; /* input is from magtape */
98 static char magtape[MAXPATHLEN];
99 static char magtapeprefix[MAXPATHLEN];
100 static int blkcnt;
101 static int numtrec;
102 static char *tapebuf; /* input buffer for read */
103 static int bufsize; /* buffer size without prefix */
104 static char *tbufptr = NULL; /* active tape buffer */
105 #ifdef HAVE_ZLIB
106 static char *comprbuf; /* uncompress work buf */
107 static size_t comprlen; /* size including prefix */
108 #endif
109 static union u_spcl endoftapemark;
110 static long blksread; /* blocks read since last header */
111 static long tpblksread = 0; /* TP_BSIZE blocks read */
112 static long tapesread;
113 static sigjmp_buf restart;
114 static int gettingfile = 0; /* restart has a valid frame */
115 static char *host = NULL;
116
117 static int ofile;
118 static char *map;
119 static char lnkbuf[MAXPATHLEN + 1];
120 static int pathlen;
121
122 int oldinofmt; /* old inode format conversion required */
123 int Bcvt; /* Swap Bytes (for CCI or sun) */
124 static int Qcvt; /* Swap quads (for sun) */
125
126 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
127
128 static void accthdr __P((struct s_spcl *));
129 static int checksum __P((int *));
130 static void findinode __P((struct s_spcl *));
131 static void findtapeblksize __P((void));
132 static int gethead __P((struct s_spcl *));
133 static void readtape __P((char *));
134 static void setdumpnum __P((void));
135 static u_int swabi __P((u_int));
136 #if 0
137 static u_long swabl __P((u_long));
138 #endif
139 static u_char *swab64 __P((u_char *, int));
140 static u_char *swab32 __P((u_char *, int));
141 static u_char *swab16 __P((u_char *, int));
142 static void terminateinput __P((void));
143 static void xtrfile __P((char *, size_t));
144 static void xtrlnkfile __P((char *, size_t));
145 static void xtrlnkskip __P((char *, size_t));
146 static void xtrmap __P((char *, size_t));
147 static void xtrmapskip __P((char *, size_t));
148 static void xtrskip __P((char *, size_t));
149 static void setmagtapein __P((void));
150
151 #ifdef HAVE_ZLIB
152 static void newcomprbuf __P((int));
153 static void readtape_set __P((char *));
154 static void readtape_uncompr __P((char *));
155 static void readtape_comprfile __P((char *));
156 static void readtape_comprtape __P((char *));
157 static char *decompress_tapebuf __P((struct tapebuf *, int));
158 static void msg_read_error __P((char *));
159 #endif
160 static int read_a_block __P((int, char *, size_t, long *));
161 #define PREFIXSIZE sizeof(struct tapebuf)
162
163 #define COMPARE_ONTHEFLY 1
164
165 #if COMPARE_ONTHEFLY
166 static int ifile; /* input file for compare */
167 static int cmperror; /* compare error */
168 static void xtrcmpfile __P((char *, size_t));
169 static void xtrcmpskip __P((char *, size_t));
170 #endif
171
172 static int readmapflag;
173
174 /*
175 * Set up an input source. This is called from main.c before setup() is.
176 */
177 void
178 setinput(char *source)
179 {
180 FLUSHTAPEBUF();
181 if (bflag)
182 newtapebuf(ntrec);
183 else
184 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
185 terminal = stdin;
186
187 #ifdef RRESTORE
188 if (strchr(source, ':')) {
189 host = source;
190 source = strchr(host, ':');
191 *source++ = '\0';
192 if (rmthost(host) == 0)
193 exit(1);
194 } else
195 #endif
196 if (strcmp(source, "-") == 0) {
197 /*
198 * Since input is coming from a pipe we must establish
199 * our own connection to the terminal.
200 */
201 terminal = fopen(_PATH_TTY, "r");
202 if (terminal == NULL) {
203 warn("cannot open %s", _PATH_TTY);
204 terminal = fopen(_PATH_DEVNULL, "r");
205 if (terminal == NULL)
206 err(1, "cannot open %s", _PATH_DEVNULL);
207 }
208 pipein++;
209 }
210 setuid(getuid()); /* no longer need or want root privileges */
211 if (Mflag) {
212 strncpy(magtapeprefix, source, MAXPATHLEN);
213 magtapeprefix[MAXPATHLEN-1] = '\0';
214 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
215 }
216 else
217 strncpy(magtape, source, MAXPATHLEN);
218 magtape[MAXPATHLEN - 1] = '\0';
219 }
220
221 void
222 newtapebuf(long size)
223 {
224 static int tapebufsize = -1;
225
226 ntrec = size;
227 bufsize = ntrec * TP_BSIZE;
228 if (size <= tapebufsize)
229 return;
230 if (tapebuf != NULL)
231 free(tapebuf);
232 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
233 if (tapebuf == NULL)
234 errx(1, "Cannot allocate space for tape buffer");
235 tapebufsize = size;
236 }
237
238 #ifdef HAVE_ZLIB
239 static void
240 newcomprbuf(int size)
241 {
242 size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
243 if (buf_size <= comprlen)
244 return;
245 comprlen = buf_size;
246 if (comprbuf != NULL)
247 free(comprbuf);
248 comprbuf = malloc(comprlen);
249 if (comprbuf == NULL)
250 errx(1, "Cannot allocate space for decompress buffer");
251 }
252 #endif /* HAVE_ZLIB */
253
254 /*
255 * Verify that the tape drive can be accessed and
256 * that it actually is a dump tape.
257 */
258 void
259 setup(void)
260 {
261 int i, j, *ip, bot_code;
262 struct STAT stbuf;
263
264 Vprintf(stdout, "Verify tape and initialize maps\n");
265 if (bot_script) {
266 msg("Launching %s\n", bot_script);
267 bot_code = system_command(bot_script, magtape, 1);
268 if (bot_code != 0 && bot_code != 1) {
269 msg("Restore aborted by the beginning of tape script\n");
270 exit(1);
271 }
272 }
273 #ifdef RRESTORE
274 if (host)
275 mt = rmtopen(magtape, 0);
276 else
277 #endif
278 if (pipein)
279 mt = 0;
280 else
281 mt = OPEN(magtape, O_RDONLY, 0);
282 if (mt < 0)
283 err(1, "%s", magtape);
284 volno = 1;
285 setmagtapein();
286 setdumpnum();
287 FLUSHTAPEBUF();
288 findtapeblksize();
289 if (gethead(&spcl) == FAIL) {
290 blkcnt--; /* push back this block */
291 blksread--;
292 tpblksread--;
293 cvtflag++;
294 if (gethead(&spcl) == FAIL)
295 errx(1, "Tape is not a dump tape");
296 fprintf(stderr, "Converting to new file system format.\n");
297 }
298
299 if (zflag) {
300 fprintf(stderr, "Dump tape is compressed.\n");
301 #ifdef HAVE_ZLIB
302 newcomprbuf(ntrec);
303 #else
304 errx(1,"This restore version doesn't support decompression");
305 #endif /* HAVE_ZLIB */
306 }
307 if (pipein) {
308 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
309 endoftapemark.s_spcl.c_type = TS_END;
310 ip = (int *)&endoftapemark;
311 j = sizeof(union u_spcl) / sizeof(int);
312 i = 0;
313 do
314 i += *ip++;
315 while (--j);
316 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
317 }
318 if (vflag || command == 't' || command == 'C')
319 printdumpinfo();
320 if (filesys[0] == '\0') {
321 char *dirptr;
322 strncpy(filesys, spcl.c_filesys, NAMELEN);
323 filesys[NAMELEN - 1] = '\0';
324 dirptr = strstr(filesys, " (dir");
325 if (dirptr != NULL)
326 *dirptr = '\0';
327 }
328 dumptime = spcl.c_ddate;
329 dumpdate = spcl.c_date;
330 if (STAT(".", &stbuf) < 0)
331 err(1, "cannot stat .");
332 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
333 fssize = TP_BSIZE;
334 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
335 fssize = stbuf.st_blksize;
336 if (((fssize - 1) & fssize) != 0)
337 errx(1, "bad block size %ld", fssize);
338 if (spcl.c_volume != 1)
339 errx(1, "Tape is not volume 1 of the dump");
340 if (gethead(&spcl) == FAIL) {
341 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
342 panic("no header after volume mark!\n");
343 }
344 findinode(&spcl);
345 if (spcl.c_type != TS_CLRI)
346 errx(1, "Cannot find file removal list");
347 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
348 Dprintf(stdout, "maxino = %ld\n", (long)maxino);
349 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
350 if (map == NULL)
351 errx(1, "no memory for active inode map");
352 usedinomap = map;
353 curfile.action = USING;
354 getfile(xtrmap, xtrmapskip);
355 if (spcl.c_type != TS_BITS)
356 errx(1, "Cannot find file dump list");
357 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
358 if (map == (char *)NULL)
359 errx(1, "no memory for file dump list");
360 dumpmap = map;
361 curfile.action = USING;
362 getfile(xtrmap, xtrmapskip);
363 /*
364 * If there may be whiteout entries on the tape, pretend that the
365 * whiteout inode exists, so that the whiteout entries can be
366 * extracted.
367 */
368 if (oldinofmt == 0)
369 SETINO(WINO, dumpmap);
370 }
371
372 /*
373 * Prompt user to load a new dump volume.
374 * "Nextvol" is the next suggested volume to use.
375 * This suggested volume is enforced when doing full
376 * or incremental restores, but can be overridden by
377 * the user when only extracting a subset of the files.
378 */
379 void
380 getvol(long nextvol)
381 {
382 long newvol = 0, wantnext = 0, i;
383 long saved_blksread = 0, saved_tpblksread = 0;
384 union u_spcl tmpspcl;
385 # define tmpbuf tmpspcl.s_spcl
386 char buf[TP_BSIZE];
387 int haderror = 0, bot_code = 1;
388
389 if (nextvol == 1) {
390 tapesread = 0;
391 gettingfile = 0;
392 }
393 if (pipein) {
394 if (nextvol != 1)
395 panic("Changing volumes on pipe input?\n");
396 if (volno == 1)
397 return;
398 goto gethdr;
399 }
400 saved_blksread = blksread;
401 saved_tpblksread = tpblksread;
402 again:
403 if (pipein)
404 exit(1); /* pipes do not get a second chance */
405 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
406 newvol = nextvol;
407 wantnext = 1;
408 } else {
409 newvol = 0;
410 wantnext = 0;
411 }
412 while (newvol <= 0) {
413 if (tapesread == 0) {
414 fprintf(stderr, "%s%s%s%s%s",
415 "You have not read any tapes yet.\n",
416 "Unless you know which volume your",
417 " file(s) are on you should start\n",
418 "with the last volume and work",
419 " towards the first.\n");
420 } else {
421 fprintf(stderr, "You have read volumes");
422 strcpy(buf, ": ");
423 for (i = 1; i < 32; i++)
424 if (tapesread & (1 << i)) {
425 fprintf(stderr, "%s%ld", buf, (long)i);
426 strcpy(buf, ", ");
427 }
428 fprintf(stderr, "\n");
429 }
430 do {
431 fprintf(stderr, "Specify next volume #: ");
432 (void) fflush(stderr);
433 (void) fgets(buf, TP_BSIZE, terminal);
434 } while (!feof(terminal) && buf[0] == '\n');
435 if (feof(terminal))
436 exit(1);
437 newvol = atoi(buf);
438 if (newvol <= 0) {
439 fprintf(stderr,
440 "Volume numbers are positive numerics\n");
441 }
442 }
443 if (newvol == volno) {
444 tapesread |= 1 << volno;
445 return;
446 }
447 closemt();
448 if (Mflag) {
449 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
450 magtape[MAXPATHLEN - 1] = '\0';
451 }
452 if (bot_script && !haderror) {
453 msg("Launching %s\n", bot_script);
454 bot_code = system_command(bot_script, magtape, newvol);
455 if (bot_code != 0 && bot_code != 1) {
456 msg("Restore aborted by the beginning of tape script\n");
457 exit(1);
458 }
459 }
460 if (haderror || (bot_code && !Mflag)) {
461 haderror = 0;
462 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
463 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
464 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
465 (void) fflush(stderr);
466 (void) fgets(buf, TP_BSIZE, terminal);
467 if (feof(terminal))
468 exit(1);
469 if (!strcmp(buf, "none\n")) {
470 terminateinput();
471 return;
472 }
473 if (buf[0] != '\n') {
474 char *pos;
475 (void) strncpy(magtape, buf, sizeof(magtape));
476 magtape[sizeof(magtape) - 1] = '\0';
477 if ((pos = strchr(magtape, '\n')))
478 magtape[pos - magtape] = '\0';
479 }
480 }
481 #ifdef RRESTORE
482 if (host)
483 mt = rmtopen(magtape, 0);
484 else
485 #endif
486 mt = OPEN(magtape, O_RDONLY, 0);
487
488 if (mt == -1) {
489 fprintf(stderr, "Cannot open %s\n", magtape);
490 volno = -1;
491 haderror = 1;
492 goto again;
493 }
494 gethdr:
495 setmagtapein();
496 volno = newvol;
497 setdumpnum();
498 FLUSHTAPEBUF();
499 findtapeblksize();
500 if (gethead(&tmpbuf) == FAIL) {
501 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
502 fprintf(stderr, "tape is not dump tape\n");
503 volno = 0;
504 haderror = 1;
505 blksread = saved_blksread;
506 tpblksread = saved_tpblksread;
507 goto again;
508 }
509 if (tmpbuf.c_volume != volno) {
510 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
511 volno = 0;
512 haderror = 1;
513 blksread = saved_blksread;
514 tpblksread = saved_tpblksread;
515 goto again;
516 }
517 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
518 fprintf(stderr, "Wrong dump date\n\tgot: %s",
519 ctime4(&tmpbuf.c_date));
520 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
521 volno = 0;
522 haderror = 1;
523 blksread = saved_blksread;
524 tpblksread = saved_tpblksread;
525 goto again;
526 }
527 tapesread |= 1 << volno;
528 blksread = saved_blksread;
529 tpblksread = saved_tpblksread;
530 /*
531 * If continuing from the previous volume, skip over any
532 * blocks read already at the end of the previous volume.
533 *
534 * If coming to this volume at random, skip to the beginning
535 * of the next record.
536 */
537 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
538 tpblksread, (long)tmpbuf.c_firstrec);
539 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
540 if (!wantnext) {
541 tpblksread = tmpbuf.c_firstrec;
542 for (i = tmpbuf.c_count; i > 0; i--)
543 readtape(buf);
544 } else if (tmpbuf.c_firstrec > 0 &&
545 tmpbuf.c_firstrec < tpblksread - 1) {
546 /*
547 * -1 since we've read the volume header
548 */
549 i = tpblksread - tmpbuf.c_firstrec - 1;
550 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
551 (long)i, i > 1 ? "s" : "");
552 while (--i >= 0)
553 readtape(buf);
554 }
555 }
556 if (curfile.action == USING) {
557 if (volno == 1)
558 panic("active file into volume 1\n");
559 return;
560 }
561 /*
562 * Skip up to the beginning of the next record
563 */
564 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
565 for (i = tmpbuf.c_count; i > 0; i--)
566 readtape(buf);
567 (void) gethead(&spcl);
568 findinode(&spcl);
569 if (gettingfile) {
570 gettingfile = 0;
571 siglongjmp(restart, 1);
572 }
573 }
574
575 /*
576 * Handle unexpected EOF.
577 */
578 static void
579 terminateinput(void)
580 {
581
582 if (gettingfile && curfile.action == USING) {
583 printf("Warning: %s %s\n",
584 "End-of-input encountered while extracting", curfile.name);
585 }
586 curfile.name = "<name unknown>";
587 curfile.action = UNKNOWN;
588 curfile.dip = NULL;
589 curfile.ino = maxino;
590 if (gettingfile) {
591 gettingfile = 0;
592 siglongjmp(restart, 1);
593 }
594 }
595
596 /*
597 * handle multiple dumps per tape by skipping forward to the
598 * appropriate one.
599 */
600 static void
601 setdumpnum(void)
602 {
603 struct mtop tcom;
604
605 if (dumpnum == 1 || volno != 1)
606 return;
607 if (pipein)
608 errx(1, "Cannot have multiple dumps on pipe input");
609 tcom.mt_op = MTFSF;
610 tcom.mt_count = dumpnum - 1;
611 #ifdef RRESTORE
612 if (host)
613 rmtioctl(MTFSF, dumpnum - 1);
614 else
615 #endif
616 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
617 warn("ioctl MTFSF");
618 }
619
620 void
621 printdumpinfo(void)
622 {
623 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
624 fprintf(stdout, "Dumped from: %s",
625 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
626 if (spcl.c_host[0] == '\0')
627 return;
628 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
629 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
630 fprintf(stdout, "Label: %s\n", spcl.c_label);
631 }
632
633 int
634 extractfile(char *name)
635 {
636 unsigned int flags;
637 mode_t mode;
638 struct timeval timep[2];
639 struct entry *ep;
640
641 curfile.name = name;
642 curfile.action = USING;
643 #ifdef __linux__
644 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
645 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
646 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
647 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
648 #else /* __linux__ */
649 timep[0].tv_sec = curfile.dip->di_atime;
650 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
651 timep[1].tv_sec = curfile.dip->di_mtime;
652 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
653 #endif /* __linux__ */
654 mode = curfile.dip->di_mode;
655 flags = curfile.dip->di_flags;
656 switch (mode & IFMT) {
657
658 default:
659 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
660 skipfile();
661 return (FAIL);
662
663 case IFSOCK:
664 Vprintf(stdout, "skipped socket %s\n", name);
665 skipfile();
666 return (GOOD);
667
668 case IFDIR:
669 if (mflag) {
670 ep = lookupname(name);
671 if (ep == NULL || ep->e_flags & EXTRACT)
672 panic("unextracted directory %s\n", name);
673 skipfile();
674 return (GOOD);
675 }
676 Vprintf(stdout, "extract file %s\n", name);
677 return (genliteraldir(name, curfile.ino));
678
679 case IFLNK:
680 {
681 #ifdef HAVE_LCHOWN
682 uid_t luid = curfile.dip->di_uid;
683 gid_t lgid = curfile.dip->di_gid;
684 #endif
685 lnkbuf[0] = '\0';
686 pathlen = 0;
687 getfile(xtrlnkfile, xtrlnkskip);
688 if (pathlen == 0) {
689 Vprintf(stdout,
690 "%s: zero length symbolic link (ignored)\n", name);
691 return (GOOD);
692 }
693 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
694 return (FAIL);
695 #ifdef HAVE_LCHOWN
696 (void) lchown(name, luid, lgid);
697 #endif
698 return (GOOD);
699 }
700
701 case IFIFO:
702 Vprintf(stdout, "extract fifo %s\n", name);
703 if (Nflag) {
704 skipfile();
705 return (GOOD);
706 }
707 if (uflag && !Nflag)
708 (void)unlink(name);
709 if (mkfifo(name, mode) < 0) {
710 warn("%s: cannot create fifo", name);
711 skipfile();
712 return (FAIL);
713 }
714 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
715 (void) chmod(name, mode);
716 if (flags)
717 #ifdef __linux__
718 (void) fsetflags(name, flags);
719 #else
720 (void) chflags(name, flags);
721 #endif
722 skipfile();
723 utimes(name, timep);
724 return (GOOD);
725
726 case IFCHR:
727 case IFBLK:
728 Vprintf(stdout, "extract special file %s\n", name);
729 if (Nflag) {
730 skipfile();
731 return (GOOD);
732 }
733 if (uflag)
734 (void)unlink(name);
735 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
736 warn("%s: cannot create special file", name);
737 skipfile();
738 return (FAIL);
739 }
740 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
741 (void) chmod(name, mode);
742 if (flags)
743 #ifdef __linux__
744 {
745 warn("%s: fsetflags called on a special file", name);
746 (void) fsetflags(name, flags);
747 }
748 #else
749 (void) chflags(name, flags);
750 #endif
751 skipfile();
752 utimes(name, timep);
753 return (GOOD);
754
755 case IFREG:
756 Vprintf(stdout, "extract file %s\n", name);
757 if (Nflag) {
758 skipfile();
759 return (GOOD);
760 }
761 if (uflag)
762 (void)unlink(name);
763 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
764 0666)) < 0) {
765 warn("%s: cannot create file", name);
766 skipfile();
767 return (FAIL);
768 }
769 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
770 (void) fchmod(ofile, mode);
771 if (flags)
772 #ifdef __linux__
773 (void) setflags(ofile, flags);
774 #else
775 (void) fchflags(ofile, flags);
776 #endif
777 getfile(xtrfile, xtrskip);
778 (void) close(ofile);
779 utimes(name, timep);
780 return (GOOD);
781 }
782 /* NOTREACHED */
783 }
784
785 /*
786 * skip over bit maps on the tape
787 */
788 void
789 skipmaps(void)
790 {
791
792 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
793 skipfile();
794 }
795
796 /*
797 * skip over a file on the tape
798 */
799 void
800 skipfile(void)
801 {
802
803 curfile.action = SKIP;
804 getfile(xtrnull, xtrnull);
805 }
806
807 /*
808 * Extract a file from the tape.
809 * When an allocated block is found it is passed to the fill function;
810 * when an unallocated block (hole) is found, a zeroed buffer is passed
811 * to the skip function.
812 */
813 void
814 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
815 {
816 register int i;
817 volatile int curblk = 0;
818 volatile quad_t size = spcl.c_dinode.di_size;
819 volatile int last_write_was_hole = 0;
820 quad_t origsize = size;
821 static char clearedbuf[MAXBSIZE];
822 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
823 char junk[TP_BSIZE];
824
825 if (spcl.c_type == TS_END)
826 panic("ran off end of tape\n");
827 if (spcl.c_magic != NFS_MAGIC)
828 panic("not at beginning of a file\n");
829 if (!gettingfile && setjmp(restart) != 0)
830 return;
831 gettingfile++;
832 loop:
833 for (i = 0; i < spcl.c_count; i++) {
834 if (readmapflag || spcl.c_addr[i]) {
835 readtape(&buf[curblk++][0]);
836 if (curblk == fssize / TP_BSIZE) {
837 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
838 fssize : (curblk - 1) * TP_BSIZE + size));
839 curblk = 0;
840 last_write_was_hole = 0;
841 }
842 } else {
843 if (curblk > 0) {
844 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
845 curblk * TP_BSIZE :
846 (curblk - 1) * TP_BSIZE + size));
847 curblk = 0;
848 }
849 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
850 TP_BSIZE : size));
851 last_write_was_hole = 1;
852 }
853 if ((size -= TP_BSIZE) <= 0) {
854 for (i++; i < spcl.c_count; i++)
855 if (readmapflag || spcl.c_addr[i])
856 readtape(junk);
857 break;
858 }
859 }
860 if (gethead(&spcl) == GOOD && size > 0) {
861 if (spcl.c_type == TS_ADDR)
862 goto loop;
863 Dprintf(stdout,
864 "Missing address (header) block for %s at %ld blocks\n",
865 curfile.name, (long)blksread);
866 }
867 if (curblk > 0) {
868 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
869 last_write_was_hole = 0;
870 }
871 if (size > 0) {
872 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
873 while (size > 0) {
874 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
875 (*skip)(clearedbuf, skp);
876 size -= skp;
877 }
878 last_write_was_hole = 1;
879 }
880 if (last_write_was_hole) {
881 FTRUNCATE(ofile, origsize);
882 }
883 findinode(&spcl);
884 gettingfile = 0;
885 }
886
887 /*
888 * Write out the next block of a file.
889 */
890 static void
891 xtrfile(char *buf, size_t size)
892 {
893
894 if (Nflag)
895 return;
896 if (write(ofile, buf, (int) size) == -1)
897 err(1, "write error extracting inode %lu, name %s\nwrite",
898 (unsigned long)curfile.ino, curfile.name);
899 }
900
901 /*
902 * Skip over a hole in a file.
903 */
904 /* ARGSUSED */
905 static void
906 xtrskip(char *buf, size_t size)
907 {
908
909 if (LSEEK(ofile, (off_t)size, SEEK_CUR) == -1)
910 err(1, "seek error extracting inode %lu, name %s\nlseek",
911 (unsigned long)curfile.ino, curfile.name);
912 }
913
914 /*
915 * Collect the next block of a symbolic link.
916 */
917 static void
918 xtrlnkfile(char *buf, size_t size)
919 {
920
921 pathlen += size;
922 if (pathlen > MAXPATHLEN)
923 errx(1, "symbolic link name: %s->%s%s; too long %d",
924 curfile.name, lnkbuf, buf, pathlen);
925 (void) strcat(lnkbuf, buf);
926 }
927
928 /*
929 * Skip over a hole in a symbolic link (should never happen).
930 */
931 /* ARGSUSED */
932 static void
933 xtrlnkskip(char *buf, size_t size)
934 {
935
936 errx(1, "unallocated block in symbolic link %s", curfile.name);
937 }
938
939 /*
940 * Collect the next block of a bit map.
941 */
942 static void
943 xtrmap(char *buf, size_t size)
944 {
945
946 memmove(map, buf, size);
947 map += size;
948 }
949
950 /*
951 * Skip over a hole in a bit map (should never happen).
952 */
953 /* ARGSUSED */
954 static void
955 xtrmapskip(char *buf, size_t size)
956 {
957
958 panic("hole in map\n");
959 map += size;
960 }
961
962 /*
963 * Noop, when an extraction function is not needed.
964 */
965 /* ARGSUSED */
966 void
967 xtrnull(char *buf, size_t size)
968 {
969
970 return;
971 }
972
973 #if COMPARE_ONTHEFLY
974 /*
975 * Compare the next block of a file.
976 */
977 static void
978 xtrcmpfile(char *buf, size_t size)
979 {
980 static char cmpbuf[MAXBSIZE];
981
982 if (cmperror)
983 return;
984
985 if (read(ifile, cmpbuf, size) != size) {
986 fprintf(stderr, "%s: size has changed.\n",
987 curfile.name);
988 cmperror = 1;
989 return;
990 }
991
992 if (memcmp(buf, cmpbuf, size) != 0) {
993 fprintf(stderr, "%s: tape and disk copies are different\n",
994 curfile.name);
995 cmperror = 1;
996 return;
997 }
998 }
999
1000 /*
1001 * Skip over a hole in a file.
1002 */
1003 static void
1004 xtrcmpskip(char *buf, size_t size)
1005 {
1006 static char cmpbuf[MAXBSIZE];
1007 int i;
1008
1009 if (cmperror)
1010 return;
1011
1012 if (read(ifile, cmpbuf, size) != size) {
1013 fprintf(stderr, "%s: size has changed.\n",
1014 curfile.name);
1015 cmperror = 1;
1016 return;
1017 }
1018
1019 for (i = 0; i < size; ++i)
1020 if (cmpbuf[i] != '\0') {
1021 fprintf(stderr, "%s: tape and disk copies are different\n",
1022 curfile.name);
1023 cmperror = 1;
1024 return;
1025 }
1026 }
1027 #endif /* COMPARE_ONTHEFLY */
1028
1029 #if !COMPARE_ONTHEFLY
1030 static int
1031 do_cmpfiles(int fd_tape, int fd_disk, long size)
1032 {
1033 static char buf_tape[BUFSIZ];
1034 static char buf_disk[BUFSIZ];
1035 ssize_t n_tape;
1036 ssize_t n_disk;
1037
1038 while (size > 0) {
1039 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1040 close(fd_tape), close(fd_disk);
1041 panic("do_cmpfiles: unexpected EOF[1]");
1042 }
1043 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1044 close(fd_tape), close(fd_disk);
1045 panic("do_cmpfiles: unexpected EOF[2]");
1046 }
1047 if (n_tape != n_disk) {
1048 close(fd_tape), close(fd_disk);
1049 panic("do_cmpfiles: sizes different!");
1050 }
1051 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1052 size -= n_tape;
1053 }
1054 return (0);
1055 }
1056
1057 /* for debugging compare problems */
1058 #undef COMPARE_FAIL_KEEP_FILE
1059
1060 static
1061 #ifdef COMPARE_FAIL_KEEP_FILE
1062 /* return true if tapefile should be unlinked after compare */
1063 int
1064 #else
1065 void
1066 #endif
1067 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1068 {
1069 struct STAT sbuf_tape;
1070 int fd_tape, fd_disk;
1071
1072 if (STAT(tapefile, &sbuf_tape) != 0) {
1073 panic("Can't lstat tmp file %s: %s\n", tapefile,
1074 strerror(errno));
1075 compare_errors = 1;
1076 }
1077
1078 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1079 fprintf(stderr,
1080 "%s: size changed from %ld to %ld.\n",
1081 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1082 compare_errors = 1;
1083 #ifdef COMPARE_FAIL_KEEP_FILE
1084 return (0);
1085 #else
1086 return;
1087 #endif
1088 }
1089
1090 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1091 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1092 compare_errors = 1;
1093 }
1094 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1095 close(fd_tape);
1096 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1097 compare_errors = 1;
1098 }
1099
1100 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1101 fprintf(stderr, "%s: tape and disk copies are different\n",
1102 diskfile);
1103 close(fd_tape);
1104 close(fd_disk);
1105 compare_errors = 1;
1106 #ifdef COMPARE_FAIL_KEEP_FILE
1107 /* rename the file to live in /tmp */
1108 /* rename `tapefile' to /tmp/<basename of diskfile> */
1109 {
1110 char *p = strrchr(diskfile, '/');
1111 char newname[MAXPATHLEN];
1112 if (!p) {
1113 panic("can't find / in %s\n", diskfile);
1114 }
1115 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1116 if (rename(tapefile, newname)) {
1117 panic("rename from %s to %s failed: %s\n",
1118 tapefile, newname,
1119 strerror(errno));
1120 } else {
1121 fprintf(stderr, "*** %s saved to %s\n",
1122 tapefile, newname);
1123 }
1124 }
1125
1126 /* don't unlink the file (it's not there anymore */
1127 /* anyway) */
1128 return (0);
1129 #else
1130 return;
1131 #endif
1132 }
1133 close(fd_tape);
1134 close(fd_disk);
1135 #ifdef COMPARE_FAIL_KEEP_FILE
1136 return (1);
1137 #endif
1138 }
1139 #endif /* !COMPARE_ONTHEFLY */
1140
1141 #if !COMPARE_ONTHEFLY
1142 static char tmpfilename[MAXPATHLEN];
1143 #endif
1144
1145 void
1146 comparefile(char *name)
1147 {
1148 int mode;
1149 struct STAT sb;
1150 int r;
1151 #if !COMPARE_ONTHEFLY
1152 static char *tmpfile = NULL;
1153 struct STAT stemp;
1154 #endif
1155
1156 if ((r = LSTAT(name, &sb)) != 0) {
1157 warn("%s: does not exist (%d)", name, r);
1158 compare_errors = 1;
1159 skipfile();
1160 return;
1161 }
1162
1163 curfile.name = name;
1164 curfile.action = USING;
1165 mode = curfile.dip->di_mode;
1166
1167 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1168 (long)sb.st_size, mode);
1169
1170 if (sb.st_mode != mode) {
1171 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1172 name, mode & 07777, sb.st_mode & 07777);
1173 compare_errors = 1;
1174 }
1175 switch (mode & IFMT) {
1176 default:
1177 skipfile();
1178 return;
1179
1180 case IFSOCK:
1181 skipfile();
1182 return;
1183
1184 case IFDIR:
1185 skipfile();
1186 return;
1187
1188 case IFLNK: {
1189 char lbuf[MAXPATHLEN + 1];
1190 int lsize;
1191
1192 if (!(sb.st_mode & S_IFLNK)) {
1193 fprintf(stderr, "%s: is no longer a symbolic link\n",
1194 name);
1195 compare_errors = 1;
1196 return;
1197 }
1198 lnkbuf[0] = '\0';
1199 pathlen = 0;
1200 getfile(xtrlnkfile, xtrlnkskip);
1201 if (pathlen == 0) {
1202 fprintf(stderr,
1203 "%s: zero length symbolic link (ignored)\n",
1204 name);
1205 compare_errors = 1;
1206 return;
1207 }
1208 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1209 panic("readlink of %s failed: %s", name,
1210 strerror(errno));
1211 compare_errors = 1;
1212 }
1213 lbuf[lsize] = 0;
1214 if (strcmp(lbuf, lnkbuf) != 0) {
1215 fprintf(stderr,
1216 "%s: symbolic link changed from %s to %s.\n",
1217 name, lnkbuf, lbuf);
1218 compare_errors = 1;
1219 return;
1220 }
1221 return;
1222 }
1223
1224 case IFCHR:
1225 case IFBLK:
1226 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1227 fprintf(stderr, "%s: no longer a special file\n",
1228 name);
1229 compare_errors = 1;
1230 skipfile();
1231 return;
1232 }
1233
1234 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1235 fprintf(stderr,
1236 "%s: device changed from %d,%d to %d,%d.\n",
1237 name,
1238 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1239 (int)curfile.dip->di_rdev & 0xff,
1240 ((int)sb.st_rdev >> 8) & 0xff,
1241 (int)sb.st_rdev & 0xff);
1242 compare_errors = 1;
1243 }
1244 skipfile();
1245 return;
1246
1247 case IFREG:
1248 #if COMPARE_ONTHEFLY
1249 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1250 panic("Can't open %s: %s\n", name, strerror(errno));
1251 skipfile();
1252 compare_errors = 1;
1253 }
1254 else {
1255 cmperror = 0;
1256 getfile(xtrcmpfile, xtrcmpskip);
1257 if (!cmperror) {
1258 char c;
1259 if (read(ifile, &c, 1) != 0) {
1260 fprintf(stderr, "%s: size has changed.\n",
1261 name);
1262 cmperror = 1;
1263 }
1264 }
1265 if (cmperror)
1266 compare_errors = 1;
1267 close(ifile);
1268 }
1269 #else
1270 if (tmpfile == NULL) {
1271 /* argument to mktemp() must not be in RO space: */
1272 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1273 tmpfile = mktemp(&tmpfilename[0]);
1274 }
1275 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1276 panic("cannot delete tmp file %s: %s\n",
1277 tmpfile, strerror(errno));
1278 }
1279 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1280 panic("cannot create file temp file %s: %s\n",
1281 name, strerror(errno));
1282 }
1283 getfile(xtrfile, xtrskip);
1284 (void) close(ofile);
1285 #ifdef COMPARE_FAIL_KEEP_FILE
1286 if (cmpfiles(tmpfile, name, &sb))
1287 unlink(tmpfile);
1288 #else
1289 cmpfiles(tmpfile, name, &sb);
1290 unlink(tmpfile);
1291 #endif
1292 #endif /* COMPARE_ONTHEFLY */
1293 return;
1294 }
1295 /* NOTREACHED */
1296 }
1297
1298 #ifdef HAVE_ZLIB
1299 static void (*readtape_func)(char *) = readtape_set;
1300
1301 /*
1302 * Read TP_BSIZE blocks from the input.
1303 * Handle read errors, and end of media.
1304 * Decompress compressed blocks.
1305 */
1306 static void
1307 readtape(char *buf)
1308 {
1309 (*readtape_func)(buf); /* call the actual processing routine */
1310 }
1311
1312 /*
1313 * Set function pointer for readtape() routine. zflag and magtapein must
1314 * be correctly set before the first call to readtape().
1315 */
1316 static void
1317 readtape_set(char *buf)
1318 {
1319 if (!zflag)
1320 readtape_func = readtape_uncompr;
1321 else {
1322 newcomprbuf(ntrec);
1323 if (magtapein)
1324 readtape_func = readtape_comprtape;
1325 else
1326 readtape_func = readtape_comprfile;
1327 }
1328 readtape(buf);
1329 }
1330
1331 #endif /* HAVE_ZLIB */
1332
1333 /*
1334 * This is the original readtape(), it's used for reading uncompressed input.
1335 * Read TP_BSIZE blocks from the input.
1336 * Handle read errors, and end of media.
1337 */
1338 static void
1339 #ifdef HAVE_ZLIB
1340 readtape_uncompr(char *buf)
1341 #else
1342 readtape(char *buf)
1343 #endif
1344 {
1345 ssize_t rd, newvol, i;
1346 int cnt, seek_failed;
1347
1348 if (blkcnt < numtrec) {
1349 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1350 blksread++;
1351 tpblksread++;
1352 return;
1353 }
1354 tbufptr = tapebuf;
1355 for (i = 0; i < ntrec; i++)
1356 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1357 if (numtrec == 0)
1358 numtrec = ntrec;
1359 cnt = ntrec * TP_BSIZE;
1360 rd = 0;
1361 getmore:
1362 #ifdef RRESTORE
1363 if (host)
1364 i = rmtread(&tapebuf[rd], cnt);
1365 else
1366 #endif
1367 i = read(mt, &tapebuf[rd], cnt);
1368
1369 /*
1370 * Check for mid-tape short read error.
1371 * If found, skip rest of buffer and start with the next.
1372 */
1373 if (!pipein && numtrec < ntrec && i > 0) {
1374 Dprintf(stdout, "mid-media short read error.\n");
1375 numtrec = ntrec;
1376 }
1377 /*
1378 * Handle partial block read.
1379 */
1380 if (pipein && i == 0 && rd > 0)
1381 i = rd;
1382 else if (i > 0 && i != ntrec * TP_BSIZE) {
1383 if (pipein) {
1384 rd += i;
1385 cnt -= i;
1386 if (cnt > 0)
1387 goto getmore;
1388 i = rd;
1389 } else {
1390 /*
1391 * Short read. Process the blocks read.
1392 */
1393 if (i % TP_BSIZE != 0)
1394 Vprintf(stdout,
1395 "partial block read: %ld should be %ld\n",
1396 (long)i, ntrec * TP_BSIZE);
1397 numtrec = i / TP_BSIZE;
1398 }
1399 }
1400 /*
1401 * Handle read error.
1402 */
1403 if (i < 0) {
1404 fprintf(stderr, "Tape read error while ");
1405 switch (curfile.action) {
1406 default:
1407 fprintf(stderr, "trying to set up tape\n");
1408 break;
1409 case UNKNOWN:
1410 fprintf(stderr, "trying to resynchronize\n");
1411 break;
1412 case USING:
1413 fprintf(stderr, "restoring %s\n", curfile.name);
1414 break;
1415 case SKIP:
1416 fprintf(stderr, "skipping over inode %lu\n",
1417 (unsigned long)curfile.ino);
1418 break;
1419 }
1420 if (!yflag && !reply("continue"))
1421 exit(1);
1422 i = ntrec * TP_BSIZE;
1423 memset(tapebuf, 0, (size_t)i);
1424 #ifdef RRESTORE
1425 if (host)
1426 seek_failed = (rmtseek(i, 1) < 0);
1427 else
1428 #endif
1429 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (off_t)-1);
1430
1431 if (seek_failed) {
1432 warn("continuation failed");
1433 if (!yflag && !reply("assume end-of-tape and continue"))
1434 exit(1);
1435 i = 0;
1436 }
1437 }
1438 /*
1439 * Handle end of tape.
1440 */
1441 if (i == 0) {
1442 Vprintf(stdout, "End-of-tape encountered\n");
1443 if (!pipein) {
1444 newvol = volno + 1;
1445 volno = 0;
1446 numtrec = 0;
1447 getvol(newvol);
1448 readtape(buf);
1449 return;
1450 }
1451 if (rd % TP_BSIZE != 0)
1452 panic("partial block read: %d should be %d\n",
1453 rd, ntrec * TP_BSIZE);
1454 terminateinput();
1455 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1456 }
1457 blkcnt = 0;
1458 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1459 blksread++;
1460 tpblksread++;
1461 }
1462
1463 #ifdef HAVE_ZLIB
1464
1465 /*
1466 * Read a compressed format block from a file or pipe and uncompress it.
1467 * Attempt to handle read errors, and end of file.
1468 */
1469 static void
1470 readtape_comprfile(char *buf)
1471 {
1472 long rl, size, i, ret;
1473 int newvol;
1474 struct tapebuf *tpb;
1475
1476 if (blkcnt < numtrec) {
1477 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1478 blksread++;
1479 tpblksread++;
1480 return;
1481 }
1482 /* need to read the next block */
1483 tbufptr = tapebuf;
1484 for (i = 0; i < ntrec; i++)
1485 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1486 numtrec = ntrec;
1487 tpb = (struct tapebuf *) tapebuf;
1488
1489 /* read the block prefix */
1490 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1491 if (ret <= 0)
1492 goto readerr;
1493
1494 /* read the data */
1495 size = tpb->length;
1496 if (size > bufsize) {
1497 /* something's wrong */
1498 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1499 bufsize, size);
1500 size = bufsize;
1501 tpb->length = bufsize;
1502 }
1503 ret = read_a_block(mt, tpb->buf, size, &rl);
1504 if (ret <= 0)
1505 goto readerr;
1506
1507 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1508 if (tbufptr == NULL) {
1509 msg_read_error("File decompression error while");
1510 if (!yflag && !reply("continue"))
1511 exit(1);
1512 memset(tapebuf, 0, bufsize);
1513 tbufptr = tapebuf;
1514 }
1515
1516 blkcnt = 0;
1517 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1518 blksread++;
1519 tpblksread++;
1520 return;
1521
1522 readerr:
1523 /* Errors while reading from a file or pipe are catastrophic. Since
1524 * there are no block boundaries, it's impossible to bypass the
1525 * block in error and find the start of the next block.
1526 */
1527 if (ret == 0) {
1528 /* It's possible to have multiple input files using -M
1529 * and -f file1,file2...
1530 */
1531 Vprintf(stdout, "End-of-File encountered\n");
1532 if (!pipein) {
1533 newvol = volno + 1;
1534 volno = 0;
1535 numtrec = 0;
1536 getvol(newvol);
1537 readtape(buf);
1538 return;
1539 }
1540 }
1541 msg_read_error("Read error while");
1542 /* if (!yflag && !reply("continue")) */
1543 exit(1);
1544 }
1545
1546 /*
1547 * Read compressed data from a tape and uncompress it.
1548 * Handle read errors, and end of media.
1549 * Since a tape consists of separate physical blocks, we try
1550 * to recover from errors by repositioning the tape to the next
1551 * block.
1552 */
1553 static void
1554 readtape_comprtape(char *buf)
1555 {
1556 long rl, size, i;
1557 int ret, newvol;
1558 struct tapebuf *tpb;
1559 struct mtop tcom;
1560
1561 if (blkcnt < numtrec) {
1562 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1563 blksread++;
1564 tpblksread++;
1565 return;
1566 }
1567 /* need to read the next block */
1568 tbufptr = tapebuf;
1569 for (i = 0; i < ntrec; i++)
1570 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1571 numtrec = ntrec;
1572 tpb = (struct tapebuf *) tapebuf;
1573
1574 /* read the block */
1575 size = bufsize + PREFIXSIZE;
1576 ret = read_a_block(mt, tapebuf, size, &rl);
1577 if (ret <= 0)
1578 goto readerr;
1579
1580 tbufptr = decompress_tapebuf(tpb, rl);
1581 if (tbufptr == NULL) {
1582 msg_read_error("Tape decompression error while");
1583 if (!yflag && !reply("continue"))
1584 exit(1);
1585 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1586 tbufptr = tapebuf;
1587 }
1588 goto moverecord;
1589
1590 readerr:
1591 /* Handle errors: EOT switches to the next volume, other errors
1592 * attempt to position the tape to the next block.
1593 */
1594 if (ret == 0) {
1595 Vprintf(stdout, "End-of-tape encountered\n");
1596 newvol = volno + 1;
1597 volno = 0;
1598 numtrec = 0;
1599 getvol(newvol);
1600 readtape(buf);
1601 return;
1602 }
1603
1604 msg_read_error("Tape read error while");
1605 if (!yflag && !reply("continue"))
1606 exit(1);
1607 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1608 tbufptr = tapebuf;
1609
1610 #ifdef RRESTORE
1611 if (host)
1612 rl = rmtioctl(MTFSR, 1);
1613 else
1614 #endif
1615 {
1616 tcom.mt_op = MTFSR;
1617 tcom.mt_count = 1;
1618 rl = ioctl(mt, MTIOCTOP, &tcom);
1619 }
1620
1621 if (rl < 0) {
1622 warn("continuation failed");
1623 if (!yflag && !reply("assume end-of-tape and continue"))
1624 exit(1);
1625 ret = 0; /* end of tape */
1626 goto readerr;
1627 }
1628
1629 moverecord:
1630 blkcnt = 0;
1631 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1632 blksread++;
1633 tpblksread++;
1634 }
1635
1636 /*
1637 * Decompress a struct tapebuf into a buffer. readsize is the size read
1638 * from the tape/file and is used for error messages. Returns a pointer
1639 * to the location of the uncompressed buffer or NULL on errors.
1640 * Adjust numtrec and complain for a short block.
1641 */
1642 static char *
1643 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
1644 {
1645 /* If zflag is on, all blocks have a struct tapebuf prefix */
1646 /* zflag gets set in setup() from the dump header */
1647 int cresult, blocklen;
1648 unsigned long worklen;
1649 char *output = NULL,*reason = NULL, *lengtherr = NULL;
1650
1651 /* build a length error message */
1652 blocklen = tpbin->length;
1653 if (readsize < blocklen + PREFIXSIZE)
1654 lengtherr = "short";
1655 else
1656 if (readsize > blocklen + PREFIXSIZE)
1657 lengtherr = "long";
1658
1659 worklen = comprlen;
1660 cresult = Z_OK;
1661 if (tpbin->compressed) {
1662 /* uncompress whatever we read, if it fails, complain later */
1663 cresult = uncompress(comprbuf, &worklen, tpbin->buf, blocklen);
1664 output = comprbuf;
1665 }
1666 else {
1667 output = tpbin->buf;
1668 worklen = blocklen;
1669 }
1670 switch (cresult) {
1671 case Z_OK:
1672 numtrec = worklen / TP_BSIZE;
1673 if (worklen % TP_BSIZE != 0)
1674 reason = "length mismatch";
1675 break;
1676 case Z_MEM_ERROR:
1677 reason = "not enough memory";
1678 break;
1679 case Z_BUF_ERROR:
1680 reason = "buffer too small";
1681 break;
1682 case Z_DATA_ERROR:
1683 reason = "data error";
1684 break;
1685 default:
1686 reason = "unknown";
1687 } /*switch */
1688 if (reason) {
1689 if (lengtherr)
1690 fprintf(stderr, "%s compressed block: %d expected: %d\n",
1691 lengtherr, readsize, tpbin->length + PREFIXSIZE);
1692 fprintf(stderr, "decompression error, block %ld: %s\n",
1693 tpblksread+1, reason);
1694 if (cresult != Z_OK) output = NULL;
1695 }
1696 return output;
1697 }
1698
1699 /*
1700 * Print an error message for a read error.
1701 * This was exteracted from the original readtape().
1702 */
1703 static void
1704 msg_read_error(char *m)
1705 {
1706 switch (curfile.action) {
1707 default:
1708 fprintf(stderr, "%s trying to set up tape\n", m);
1709 break;
1710 case UNKNOWN:
1711 fprintf(stderr, "%s trying to resynchronize\n", m);
1712 break;
1713 case USING:
1714 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
1715 break;
1716 case SKIP:
1717 fprintf(stderr, "%s skipping over inode %lu\n", m,
1718 (unsigned long)curfile.ino);
1719 break;
1720 }
1721 }
1722 #endif /* HAVE_ZLIB */
1723
1724 /*
1725 * Read the first block and get the blocksize from it. Test
1726 * for a compressed dump tape/file. setup() will make the final
1727 * determination by checking the compressed flag if gethead()
1728 * finds a valid header. The test here is necessary to offset the buffer
1729 * by the size of the compressed prefix. zflag is set here so that
1730 * readtape_set can set the correct function pointer for readtape().
1731 * Note that the first block of each tape/file is not compressed
1732 * and does not have a prefix.
1733 */
1734 static void
1735 findtapeblksize(void)
1736 {
1737 long i;
1738 size_t len;
1739 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1740 struct s_spcl *spclpt = (struct s_spcl *) tapebuf;
1741
1742 for (i = 0; i < ntrec; i++)
1743 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1744 blkcnt = 0;
1745 tbufptr = tapebuf;
1746 /*
1747 * For a pipe or file, read in the first record. For a tape, read
1748 * the first block.
1749 */
1750 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
1751
1752 if (read_a_block(mt, tapebuf, len, &i) <= 0)
1753 errx(1, "Tape read error on first record");
1754
1755 /*
1756 * If the input is from a file or a pipe, we read TP_BSIZE
1757 * bytes looking for a dump header. If the dump is compressed
1758 * we need to read in the rest of the block, as determined
1759 * by c_ntrec in the dump header. The first block of the
1760 * dump is not compressed and does not have a prefix.
1761 */
1762 if (!magtapein) {
1763 if (spclpt->c_type == TS_TAPE
1764 && spclpt->c_flags & DR_COMPRESSED) {
1765 /* It's a compressed dump file, read in the */
1766 /* rest of the block based on spclpt->c_ntrec. */
1767 if (spclpt->c_ntrec > ntrec)
1768 errx(1, "Tape blocksize is too large, use "
1769 "\'-b %d\' ", spclpt->c_ntrec);
1770 ntrec = spclpt->c_ntrec;
1771 len = (ntrec - 1) * TP_BSIZE;
1772 zflag = 1;
1773 }
1774 else {
1775 /* read in the rest of the block based on bufsize */
1776 len = bufsize - TP_BSIZE;
1777 }
1778 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1779 || (i != len && i % TP_BSIZE != 0))
1780 errx(1,"Error reading dump file header");
1781 tbufptr = tapebuf;
1782 numtrec = ntrec;
1783 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1784 return;
1785 } /* if (!magtapein) */
1786
1787 /*
1788 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1789 * If the value of ntrec is too large, we read less than
1790 * what we asked for; adjust the value of ntrec and test for
1791 * a compressed dump tape.
1792 */
1793
1794 if (i % TP_BSIZE != 0) {
1795 /* may be old format compressed dump tape with a prefix */
1796 spclpt = (struct s_spcl *) tpb->buf;
1797 if (i % TP_BSIZE == PREFIXSIZE
1798 && tpb->compressed == 0
1799 && spclpt->c_type == TS_TAPE
1800 && spclpt->c_flags & DR_COMPRESSED) {
1801 zflag = 1;
1802 tbufptr = tpb->buf;
1803 if (tpb->length > bufsize)
1804 errx(1, "Tape blocksize is too large, use "
1805 "\'-b %d\' ", tpb->length / TP_BSIZE);
1806 }
1807 else
1808 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1809 i, TP_BSIZE);
1810 }
1811 ntrec = i / TP_BSIZE;
1812 if (spclpt->c_type == TS_TAPE) {
1813 if (spclpt->c_flags & DR_COMPRESSED)
1814 zflag = 1;
1815 if (spclpt->c_ntrec > ntrec)
1816 errx(1, "Tape blocksize is too large, use "
1817 "\'-b %d\' ", spclpt->c_ntrec);
1818 }
1819 numtrec = ntrec;
1820 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1821 }
1822
1823 /*
1824 * Read a block of data handling all of the messy details.
1825 */
1826 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1827 {
1828 long i = 1, size;
1829
1830 size = len;
1831 while (size > 0) {
1832 #ifdef RRESTORE
1833 if (host)
1834 i = rmtread(buf, size);
1835 else
1836 #endif
1837 i = read(fd, buf, size);
1838
1839 if (i <= 0)
1840 break; /* EOD or error */
1841 size -= i;
1842 if (magtapein)
1843 break; /* block at a time for mt */
1844 buf += i;
1845 }
1846 *lengthread = len - size;
1847 return i;
1848 }
1849
1850 void
1851 closemt(void)
1852 {
1853
1854 if (mt < 0)
1855 return;
1856 #ifdef RRESTORE
1857 if (host)
1858 rmtclose();
1859 else
1860 #endif
1861 (void) close(mt);
1862 }
1863
1864 static void
1865 setmagtapein(void) {
1866 struct mtget mt_stat;
1867 static int done = 0;
1868 if (done)
1869 return;
1870 done = 1;
1871 if (!pipein) {
1872 /* need to know if input is really from a tape */
1873 #ifdef RRESTORE
1874 if (host)
1875 magtapein = rmtioctl(MTNOP, 1) != -1;
1876 else
1877 #endif
1878 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
1879 }
1880
1881 Vprintf(stdout,"Input is from %s\n", magtapein? "tape": "file/pipe");
1882 }
1883
1884 /*
1885 * Read the next block from the tape.
1886 * Check to see if it is one of several vintage headers.
1887 * If it is an old style header, convert it to a new style header.
1888 * If it is not any valid header, return an error.
1889 */
1890 static int
1891 gethead(struct s_spcl *buf)
1892 {
1893 int32_t i;
1894 union {
1895 quad_t qval;
1896 int32_t val[2];
1897 } qcvt;
1898 union u_ospcl {
1899 char dummy[TP_BSIZE];
1900 struct s_ospcl {
1901 int32_t c_type;
1902 int32_t c_date;
1903 int32_t c_ddate;
1904 int32_t c_volume;
1905 int32_t c_tapea;
1906 u_int16_t c_inumber;
1907 int32_t c_magic;
1908 int32_t c_checksum;
1909 struct odinode {
1910 u_int16_t odi_mode;
1911 u_int16_t odi_nlink;
1912 u_int16_t odi_uid;
1913 u_int16_t odi_gid;
1914 int32_t odi_size;
1915 int32_t odi_rdev;
1916 char odi_addr[36];
1917 int32_t odi_atime;
1918 int32_t odi_mtime;
1919 int32_t odi_ctime;
1920 } c_dinode;
1921 int32_t c_count;
1922 char c_addr[256];
1923 } s_ospcl;
1924 } u_ospcl;
1925
1926 if (!cvtflag) {
1927 readtape((char *)buf);
1928 if (buf->c_magic != NFS_MAGIC) {
1929 if (swabi(buf->c_magic) != NFS_MAGIC)
1930 return (FAIL);
1931 if (!Bcvt) {
1932 Vprintf(stdout, "Note: Doing Byte swapping\n");
1933 Bcvt = 1;
1934 }
1935 }
1936 if (checksum((int *)buf) == FAIL)
1937 return (FAIL);
1938 if (Bcvt)
1939 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1940 goto good;
1941 }
1942 readtape((char *)(&u_ospcl.s_ospcl));
1943 memset((char *)buf, 0, (long)TP_BSIZE);
1944 buf->c_type = u_ospcl.s_ospcl.c_type;
1945 buf->c_date = u_ospcl.s_ospcl.c_date;
1946 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1947 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1948 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1949 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1950 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1951 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1952 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1953 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1954 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1955 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1956 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1957 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1958 #ifdef __linux__
1959 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1960 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1961 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1962 #else /* __linux__ */
1963 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1964 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1965 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1966 #endif /* __linux__ */
1967 buf->c_count = u_ospcl.s_ospcl.c_count;
1968 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1969 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1970 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1971 return(FAIL);
1972 buf->c_magic = NFS_MAGIC;
1973
1974 good:
1975 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1976 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1977 qcvt.qval = buf->c_dinode.di_size;
1978 if (qcvt.val[0] || qcvt.val[1]) {
1979 printf("Note: Doing Quad swapping\n");
1980 Qcvt = 1;
1981 }
1982 }
1983 if (Qcvt) {
1984 qcvt.qval = buf->c_dinode.di_size;
1985 i = qcvt.val[1];
1986 qcvt.val[1] = qcvt.val[0];
1987 qcvt.val[0] = i;
1988 buf->c_dinode.di_size = qcvt.qval;
1989 }
1990 readmapflag = 0;
1991
1992 switch (buf->c_type) {
1993
1994 case TS_CLRI:
1995 case TS_BITS:
1996 /*
1997 * Have to patch up missing information in bit map headers
1998 */
1999 buf->c_inumber = 0;
2000 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
2001 if (buf->c_count > TP_NINDIR)
2002 readmapflag = 1;
2003 else
2004 for (i = 0; i < buf->c_count; i++)
2005 buf->c_addr[i]++;
2006 break;
2007
2008 case TS_TAPE:
2009 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
2010 oldinofmt = 1;
2011 /* fall through */
2012 case TS_END:
2013 buf->c_inumber = 0;
2014 break;
2015
2016 case TS_INODE:
2017 case TS_ADDR:
2018 break;
2019
2020 default:
2021 panic("gethead: unknown inode type %d\n", buf->c_type);
2022 break;
2023 }
2024 /*
2025 * If we are restoring a filesystem with old format inodes,
2026 * copy the uid/gid to the new location.
2027 */
2028 if (oldinofmt) {
2029 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2030 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2031 }
2032 if (dflag)
2033 accthdr(buf);
2034 return(GOOD);
2035 }
2036
2037 /*
2038 * Check that a header is where it belongs and predict the next header
2039 */
2040 static void
2041 accthdr(struct s_spcl *header)
2042 {
2043 static dump_ino_t previno = 0x7fffffff;
2044 static int prevtype;
2045 static long predict;
2046 long blks, i;
2047
2048 if (header->c_type == TS_TAPE) {
2049 fprintf(stderr, "Volume header (%s inode format) ",
2050 oldinofmt ? "old" : "new");
2051 if (header->c_firstrec)
2052 fprintf(stderr, "begins with record %d",
2053 header->c_firstrec);
2054 fprintf(stderr, "\n");
2055 previno = 0x7fffffff;
2056 return;
2057 }
2058 if (previno == 0x7fffffff)
2059 goto newcalc;
2060 switch (prevtype) {
2061 case TS_BITS:
2062 fprintf(stderr, "Dumped inodes map header");
2063 break;
2064 case TS_CLRI:
2065 fprintf(stderr, "Used inodes map header");
2066 break;
2067 case TS_INODE:
2068 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2069 break;
2070 case TS_ADDR:
2071 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2072 break;
2073 case TS_END:
2074 fprintf(stderr, "End of tape header");
2075 break;
2076 }
2077 if (predict != blksread - 1)
2078 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2079 predict, blksread - 1);
2080 fprintf(stderr, "\n");
2081 newcalc:
2082 blks = 0;
2083 if (header->c_type != TS_END)
2084 for (i = 0; i < header->c_count; i++)
2085 if (readmapflag || header->c_addr[i] != 0)
2086 blks++;
2087 predict = blks;
2088 blksread = 0;
2089 prevtype = header->c_type;
2090 previno = header->c_inumber;
2091 }
2092
2093 /*
2094 * Find an inode header.
2095 * Complain if had to skip, and complain is set.
2096 */
2097 static void
2098 findinode(struct s_spcl *header)
2099 {
2100 static long skipcnt = 0;
2101 long i;
2102 char buf[TP_BSIZE];
2103
2104 curfile.name = "<name unknown>";
2105 curfile.action = UNKNOWN;
2106 curfile.dip = NULL;
2107 curfile.ino = 0;
2108 do {
2109 if (header->c_magic != NFS_MAGIC) {
2110 skipcnt++;
2111 while (gethead(header) == FAIL ||
2112 header->c_date != dumpdate)
2113 skipcnt++;
2114 }
2115 switch (header->c_type) {
2116
2117 case TS_ADDR:
2118 /*
2119 * Skip up to the beginning of the next record
2120 */
2121 for (i = 0; i < header->c_count; i++)
2122 if (header->c_addr[i])
2123 readtape(buf);
2124 while (gethead(header) == FAIL ||
2125 header->c_date != dumpdate)
2126 skipcnt++;
2127 break;
2128
2129 case TS_INODE:
2130 curfile.dip = &header->c_dinode;
2131 curfile.ino = header->c_inumber;
2132 break;
2133
2134 case TS_END:
2135 curfile.ino = maxino;
2136 break;
2137
2138 case TS_CLRI:
2139 curfile.name = "<file removal list>";
2140 break;
2141
2142 case TS_BITS:
2143 curfile.name = "<file dump list>";
2144 break;
2145
2146 case TS_TAPE:
2147 panic("unexpected tape header\n");
2148 /* NOTREACHED */
2149
2150 default:
2151 panic("unknown tape header type %d\n", spcl.c_type);
2152 /* NOTREACHED */
2153
2154 }
2155 } while (header->c_type == TS_ADDR);
2156 if (skipcnt > 0)
2157 #ifdef USE_QFA
2158 if (!noresyncmesg)
2159 #endif
2160 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2161 skipcnt);
2162 skipcnt = 0;
2163 }
2164
2165 static int
2166 checksum(int *buf)
2167 {
2168 register int i, j;
2169
2170 j = sizeof(union u_spcl) / sizeof(int);
2171 i = 0;
2172 if(!Bcvt) {
2173 do
2174 i += *buf++;
2175 while (--j);
2176 } else {
2177 /* What happens if we want to read restore tapes
2178 for a 16bit int machine??? */
2179 do
2180 i += swabi(*buf++);
2181 while (--j);
2182 }
2183
2184 if (i != CHECKSUM) {
2185 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2186 (unsigned long)curfile.ino, curfile.name);
2187 return(FAIL);
2188 }
2189 return(GOOD);
2190 }
2191
2192 #ifdef RRESTORE
2193 #ifdef __STDC__
2194 #include <stdarg.h>
2195 #else
2196 #include <varargs.h>
2197 #endif
2198
2199 void
2200 #ifdef __STDC__
2201 msg(const char *fmt, ...)
2202 #else
2203 msg(fmt, va_alist)
2204 char *fmt;
2205 va_dcl
2206 #endif
2207 {
2208 va_list ap;
2209 #ifdef __STDC__
2210 va_start(ap, fmt);
2211 #else
2212 va_start(ap);
2213 #endif
2214 (void)vfprintf(stderr, fmt, ap);
2215 va_end(ap);
2216 }
2217 #endif /* RRESTORE */
2218
2219 static u_char *
2220 swab16(u_char *sp, int n)
2221 {
2222 char c;
2223
2224 while (--n >= 0) {
2225 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2226 sp += 2;
2227 }
2228 return (sp);
2229 }
2230
2231 static u_char *
2232 swab32(u_char *sp, int n)
2233 {
2234 char c;
2235
2236 while (--n >= 0) {
2237 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2238 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2239 sp += 4;
2240 }
2241 return (sp);
2242 }
2243
2244 static u_char *
2245 swab64(u_char *sp, int n)
2246 {
2247 char c;
2248
2249 while (--n >= 0) {
2250 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2251 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2252 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2253 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2254 sp += 8;
2255 }
2256 return (sp);
2257 }
2258
2259 void
2260 swabst(u_char *cp, u_char *sp)
2261 {
2262 int n = 0;
2263
2264 while (*cp) {
2265 switch (*cp) {
2266 case '0': case '1': case '2': case '3': case '4':
2267 case '5': case '6': case '7': case '8': case '9':
2268 n = (n * 10) + (*cp++ - '0');
2269 continue;
2270
2271 case 's': case 'w': case 'h':
2272 if (n == 0)
2273 n = 1;
2274 sp = swab16(sp, n);
2275 break;
2276
2277 case 'i':
2278 if (n == 0)
2279 n = 1;
2280 sp = swab32(sp, n);
2281 break;
2282
2283 case 'l':
2284 if (n == 0)
2285 n = 1;
2286 sp = swab64(sp, n);
2287 break;
2288
2289 default: /* Any other character, like 'b' counts as byte. */
2290 if (n == 0)
2291 n = 1;
2292 sp += n;
2293 break;
2294 }
2295 cp++;
2296 n = 0;
2297 }
2298 }
2299
2300 static u_int
2301 swabi(u_int x)
2302 {
2303 swabst((u_char *)"i", (u_char *)&x);
2304 return (x);
2305 }
2306
2307 #if 0
2308 static u_long
2309 swabl(u_long x)
2310 {
2311 swabst((u_char *)"l", (u_char *)&x);
2312 return (x);
2313 }
2314 #endif
2315
2316 #ifdef USE_QFA
2317 /*
2318 * get the current position of the tape
2319 */
2320 int
2321 GetTapePos(long *pos)
2322 {
2323 int err = 0;
2324
2325 *pos = 0;
2326 if (ioctl(mt, MTIOCPOS, pos) == -1) {
2327 err = errno;
2328 fprintf(stdout, "[%ld] error: %d (getting tapepos: %ld)\n",
2329 (unsigned long)getpid(), err, *pos);
2330 return err;
2331 }
2332 return err;
2333 }
2334
2335 typedef struct mt_pos {
2336 short mt_op;
2337 int mt_count;
2338 } MTPosRec, *MTPosPtr;
2339
2340 /*
2341 * go to specified position on tape
2342 */
2343 int
2344 GotoTapePos(long pos)
2345 {
2346 int err = 0;
2347 struct mt_pos buf;
2348
2349 buf.mt_op = MTSEEK;
2350 buf.mt_count = pos;
2351 if (ioctl(mt, MTIOCTOP, &buf) == -1) {
2352 err = errno;
2353 fprintf(stdout, "[%ld] error: %d (setting tapepos: %ld)\n",
2354 (unsigned long)getpid(), err, pos);
2355 return err;
2356 }
2357 return err;
2358 }
2359
2360 /*
2361 * read next data from tape to re-sync
2362 */
2363 void
2364 ReReadFromTape(void)
2365 {
2366 FLUSHTAPEBUF();
2367 noresyncmesg = 1;
2368 if (gethead(&spcl) == FAIL) {
2369 #ifdef DEBUG_QFA
2370 fprintf(stdout, "DEBUG 1 gethead failed\n");
2371 #endif
2372 }
2373 findinode(&spcl);
2374 noresyncmesg = 0;
2375 }
2376
2377 void
2378 RequestVol(long tnum)
2379 {
2380 FLUSHTAPEBUF();
2381 getvol(tnum);
2382 }
2383 #endif /* USE_QFA */