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