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