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