tape.c revision 1.35 1 /* $NetBSD: tape.c,v 1.35 1998/03/30 02:13:35 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)tape.c 8.9 (Berkeley) 5/1/95";
45 #else
46 __RCSID("$NetBSD: tape.c,v 1.35 1998/03/30 02:13:35 mrg Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/file.h>
52 #include <sys/ioctl.h>
53 #include <sys/mtio.h>
54 #include <sys/stat.h>
55
56 #include <ufs/ufs/dinode.h>
57 #include <protocols/dumprestore.h>
58
59 #include <errno.h>
60 #include <paths.h>
61 #include <setjmp.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66
67 #include "restore.h"
68 #include "extern.h"
69
70 static u_int32_t fssize = MAXBSIZE;
71 static int mt = -1;
72 static int pipein = 0;
73 static char magtape[BUFSIZ];
74 static int blkcnt;
75 static int numtrec;
76 static char *tapebuf;
77 static union u_spcl endoftapemark;
78 static int blksread; /* blocks read since last header */
79 static int tpblksread = 0; /* TP_BSIZE blocks read */
80 static int tapesread;
81 static jmp_buf restart;
82 static int gettingfile = 0; /* restart has a valid frame */
83 static char *host = NULL;
84
85 static int ofile;
86 static char *map;
87 static char lnkbuf[MAXPATHLEN + 1];
88 static int pathlen;
89
90 int oldinofmt; /* old inode format conversion required */
91 int Bcvt; /* Swap Bytes (for CCI or sun) */
92 static int Qcvt; /* Swap quads (for sun) */
93
94 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
95
96 static void accthdr __P((struct s_spcl *));
97 static int checksum __P((int *));
98 static void findinode __P((struct s_spcl *));
99 static void findtapeblksize __P((void));
100 static int gethead __P((struct s_spcl *));
101 static void readtape __P((char *));
102 static void setdumpnum __P((void));
103 static u_long swabl __P((u_long));
104 static u_char *swablong __P((u_char *, int));
105 static u_char *swabshort __P((u_char *, int));
106 static void terminateinput __P((void));
107 static void xtrfile __P((char *, long));
108 static void xtrlnkfile __P((char *, long));
109 static void xtrlnkskip __P((char *, long));
110 static void xtrmap __P((char *, long));
111 static void xtrmapskip __P((char *, long));
112 static void xtrskip __P((char *, long));
113
114 /*
115 * Set up an input source
116 */
117 void
118 setinput(source)
119 char *source;
120 {
121 FLUSHTAPEBUF();
122 if (bflag)
123 newtapebuf(ntrec);
124 else
125 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
126 terminal = stdin;
127
128 #ifdef RRESTORE
129 if (strchr(source, ':')) {
130 host = source;
131 source = strchr(host, ':');
132 *source++ = '\0';
133 if (rmthost(host) == 0)
134 exit(1);
135 } else
136 #endif
137 if (strcmp(source, "-") == 0) {
138 /*
139 * Since input is coming from a pipe we must establish
140 * our own connection to the terminal.
141 */
142 terminal = fopen(_PATH_TTY, "r");
143 if (terminal == NULL) {
144 (void)fprintf(stderr, "cannot open %s: %s\n",
145 _PATH_TTY, strerror(errno));
146 terminal = fopen(_PATH_DEVNULL, "r");
147 if (terminal == NULL) {
148 (void)fprintf(stderr, "cannot open %s: %s\n",
149 _PATH_DEVNULL, strerror(errno));
150 exit(1);
151 }
152 }
153 pipein++;
154 }
155 (void) strcpy(magtape, source);
156 }
157
158 void
159 newtapebuf(size)
160 long size;
161 {
162 static int tapebufsize = -1;
163
164 ntrec = size;
165 if (size <= tapebufsize)
166 return;
167 if (tapebuf != NULL)
168 free(tapebuf);
169 tapebuf = malloc(size * TP_BSIZE);
170 if (tapebuf == NULL) {
171 fprintf(stderr, "Cannot allocate space for tape buffer\n");
172 exit(1);
173 }
174 tapebufsize = size;
175 }
176
177 /*
178 * Verify that the tape drive can be accessed and
179 * that it actually is a dump tape.
180 */
181 void
182 setup()
183 {
184 int i, j, *ip;
185 struct stat stbuf;
186
187 vprintf(stdout, "Verify tape and initialize maps\n");
188 #ifdef RRESTORE
189 if (host)
190 mt = rmtopen(magtape, 0);
191 else
192 #endif
193 if (pipein)
194 mt = 0;
195 else
196 mt = open(magtape, O_RDONLY, 0);
197 if (mt < 0) {
198 fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
199 exit(1);
200 }
201 volno = 1;
202 setdumpnum();
203 FLUSHTAPEBUF();
204 if (!pipein && !bflag)
205 findtapeblksize();
206 if (gethead(&spcl) == FAIL) {
207 blkcnt--; /* push back this block */
208 blksread--;
209 tpblksread--;
210 cvtflag++;
211 if (gethead(&spcl) == FAIL) {
212 fprintf(stderr, "Tape is not a dump tape\n");
213 exit(1);
214 }
215 fprintf(stderr, "Converting to new file system format.\n");
216 }
217 if (pipein) {
218 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
219 endoftapemark.s_spcl.c_type = TS_END;
220 ip = (int *)&endoftapemark;
221 j = sizeof(union u_spcl) / sizeof(int);
222 i = 0;
223 do
224 i += *ip++;
225 while (--j);
226 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
227 }
228 if (vflag || command == 't')
229 printdumpinfo();
230 dumptime = spcl.c_ddate;
231 dumpdate = spcl.c_date;
232 if (stat(".", &stbuf) < 0) {
233 fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
234 exit(1);
235 }
236 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
237 fssize = stbuf.st_blksize;
238 if (((fssize - 1) & fssize) != 0) {
239 fprintf(stderr, "bad block size %d\n", fssize);
240 exit(1);
241 }
242 if (spcl.c_volume != 1) {
243 fprintf(stderr, "Tape is not volume 1 of the dump\n");
244 exit(1);
245 }
246 if (gethead(&spcl) == FAIL) {
247 dprintf(stdout, "header read failed at %d blocks\n", blksread);
248 panic("no header after volume mark!\n");
249 }
250 findinode(&spcl);
251 if (spcl.c_type != TS_CLRI) {
252 fprintf(stderr, "Cannot find file removal list\n");
253 exit(1);
254 }
255 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
256 dprintf(stdout, "maxino = %d\n", maxino);
257 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
258 if (map == NULL)
259 panic("no memory for active inode map\n");
260 usedinomap = map;
261 curfile.action = USING;
262 getfile(xtrmap, xtrmapskip);
263 if (spcl.c_type != TS_BITS) {
264 fprintf(stderr, "Cannot find file dump list\n");
265 exit(1);
266 }
267 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
268 if (map == (char *)NULL)
269 panic("no memory for file dump list\n");
270 dumpmap = map;
271 curfile.action = USING;
272 getfile(xtrmap, xtrmapskip);
273 /*
274 * If there may be whiteout entries on the tape, pretend that the
275 * whiteout inode exists, so that the whiteout entries can be
276 * extracted.
277 */
278 if (oldinofmt == 0)
279 SETINO(WINO, dumpmap);
280 }
281
282 /*
283 * Prompt user to load a new dump volume.
284 * "Nextvol" is the next suggested volume to use.
285 * This suggested volume is enforced when doing full
286 * or incremental restores, but can be overrridden by
287 * the user when only extracting a subset of the files.
288 */
289 void
290 getvol(nextvol)
291 int nextvol;
292 {
293 int newvol, savecnt, wantnext, i;
294 union u_spcl tmpspcl;
295 # define tmpbuf tmpspcl.s_spcl
296 char buf[TP_BSIZE];
297
298 newvol = savecnt = wantnext = 0;
299 if (nextvol == 1) {
300 tapesread = 0;
301 gettingfile = 0;
302 }
303 if (pipein) {
304 if (nextvol != 1)
305 panic("Changing volumes on pipe input?\n");
306 if (volno == 1)
307 return;
308 goto gethdr;
309 }
310 savecnt = blksread;
311 again:
312 if (pipein)
313 exit(1); /* pipes do not get a second chance */
314 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
315 newvol = nextvol;
316 wantnext = 1;
317 } else {
318 newvol = 0;
319 wantnext = 0;
320 }
321 while (newvol <= 0) {
322 if (tapesread == 0) {
323 fprintf(stderr, "%s%s%s%s%s",
324 "You have not read any tapes yet.\n",
325 "Unless you know which volume your",
326 " file(s) are on you should start\n",
327 "with the last volume and work",
328 " towards the first.\n");
329 } else {
330 fprintf(stderr, "You have read volumes");
331 strcpy(buf, ": ");
332 for (i = 1; i < 32; i++)
333 if (tapesread & (1 << i)) {
334 fprintf(stderr, "%s%d", buf, i);
335 strcpy(buf, ", ");
336 }
337 fprintf(stderr, "\n");
338 }
339 do {
340 fprintf(stderr, "Specify next volume #: ");
341 (void) fflush(stderr);
342 (void) fgets(buf, BUFSIZ, terminal);
343 } while (!feof(terminal) && buf[0] == '\n');
344 if (feof(terminal))
345 exit(1);
346 newvol = atoi(buf);
347 if (newvol <= 0) {
348 fprintf(stderr,
349 "Volume numbers are positive numerics\n");
350 }
351 }
352 if (newvol == volno) {
353 tapesread |= 1 << volno;
354 return;
355 }
356 closemt();
357 fprintf(stderr, "Mount tape volume %d\n", newvol);
358 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
359 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
360 (void) fflush(stderr);
361 (void) fgets(buf, BUFSIZ, terminal);
362 if (feof(terminal))
363 exit(1);
364 if (!strcmp(buf, "none\n")) {
365 terminateinput();
366 return;
367 }
368 if (buf[0] != '\n') {
369 (void) strcpy(magtape, buf);
370 magtape[strlen(magtape) - 1] = '\0';
371 }
372 #ifdef RRESTORE
373 if (host)
374 mt = rmtopen(magtape, 0);
375 else
376 #endif
377 mt = open(magtape, O_RDONLY, 0);
378
379 if (mt == -1) {
380 fprintf(stderr, "Cannot open %s\n", magtape);
381 volno = -1;
382 goto again;
383 }
384 gethdr:
385 volno = newvol;
386 setdumpnum();
387 FLUSHTAPEBUF();
388 if (gethead(&tmpbuf) == FAIL) {
389 dprintf(stdout, "header read failed at %d blocks\n", blksread);
390 fprintf(stderr, "tape is not dump tape\n");
391 volno = 0;
392 goto again;
393 }
394 if (tmpbuf.c_volume != volno) {
395 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
396 volno = 0;
397 goto again;
398 }
399 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
400 fprintf(stderr, "Wrong dump date\n\tgot: %s",
401 ctime(&tmpbuf.c_date));
402 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
403 volno = 0;
404 goto again;
405 }
406 tapesread |= 1 << volno;
407 blksread = savecnt;
408 /*
409 * If continuing from the previous volume, skip over any
410 * blocks read already at the end of the previous volume.
411 *
412 * If coming to this volume at random, skip to the beginning
413 * of the next record.
414 */
415 dprintf(stdout, "read %ld recs, tape starts with %ld\n",
416 (long)tpblksread, (long)tmpbuf.c_firstrec);
417 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
418 if (!wantnext) {
419 tpblksread = tmpbuf.c_firstrec;
420 for (i = tmpbuf.c_count; i > 0; i--)
421 readtape(buf);
422 } else if (tmpbuf.c_firstrec > 0 &&
423 tmpbuf.c_firstrec < tpblksread - 1) {
424 /*
425 * -1 since we've read the volume header
426 */
427 i = tpblksread - tmpbuf.c_firstrec - 1;
428 dprintf(stderr, "Skipping %d duplicate record%s.\n",
429 i, i > 1 ? "s" : "");
430 while (--i >= 0)
431 readtape(buf);
432 }
433 }
434 if (curfile.action == USING) {
435 if (volno == 1)
436 panic("active file into volume 1\n");
437 return;
438 }
439 /*
440 * Skip up to the beginning of the next record
441 */
442 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
443 for (i = tmpbuf.c_count; i > 0; i--)
444 readtape(buf);
445 (void) gethead(&spcl);
446 findinode(&spcl);
447 if (gettingfile) {
448 gettingfile = 0;
449 longjmp(restart, 1);
450 }
451 }
452
453 /*
454 * Handle unexpected EOF.
455 */
456 static void
457 terminateinput()
458 {
459
460 if (gettingfile && curfile.action == USING) {
461 printf("Warning: %s %s\n",
462 "End-of-input encountered while extracting", curfile.name);
463 }
464 curfile.name = "<name unknown>";
465 curfile.action = UNKNOWN;
466 curfile.dip = NULL;
467 curfile.ino = maxino;
468 if (gettingfile) {
469 gettingfile = 0;
470 longjmp(restart, 1);
471 }
472 }
473
474 /*
475 * handle multiple dumps per tape by skipping forward to the
476 * appropriate one.
477 */
478 static void
479 setdumpnum()
480 {
481 struct mtop tcom;
482
483 if (dumpnum == 1 || volno != 1)
484 return;
485 if (pipein) {
486 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
487 exit(1);
488 }
489 tcom.mt_op = MTFSF;
490 tcom.mt_count = dumpnum - 1;
491 #ifdef RRESTORE
492 if (host)
493 rmtioctl(MTFSF, dumpnum - 1);
494 else
495 #endif
496 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
497 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
498 }
499
500 void
501 printdumpinfo()
502 {
503 fprintf(stdout, "Dump date: %s", ctime(&spcl.c_date));
504 fprintf(stdout, "Dumped from: %s",
505 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
506 if (spcl.c_host[0] == '\0')
507 return;
508 fprintf(stderr, "Level %d dump of %s on %s:%s\n",
509 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
510 fprintf(stderr, "Label: %s\n", spcl.c_label);
511 }
512
513 int
514 extractfile(name)
515 char *name;
516 {
517 int flags;
518 uid_t uid;
519 gid_t gid;
520 mode_t mode;
521 struct timeval timep[2];
522 struct entry *ep;
523
524 curfile.name = name;
525 curfile.action = USING;
526 timep[0].tv_sec = curfile.dip->di_atime;
527 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
528 timep[1].tv_sec = curfile.dip->di_mtime;
529 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
530 uid = curfile.dip->di_uid;
531 gid = curfile.dip->di_gid;
532 mode = curfile.dip->di_mode;
533 flags = curfile.dip->di_flags;
534 switch (mode & IFMT) {
535
536 default:
537 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
538 skipfile();
539 return (FAIL);
540
541 case IFSOCK:
542 vprintf(stdout, "skipped socket %s\n", name);
543 skipfile();
544 return (GOOD);
545
546 case IFDIR:
547 if (mflag) {
548 ep = lookupname(name);
549 if (ep == NULL || ep->e_flags & EXTRACT)
550 panic("unextracted directory %s\n", name);
551 skipfile();
552 return (GOOD);
553 }
554 vprintf(stdout, "extract file %s\n", name);
555 return (genliteraldir(name, curfile.ino));
556
557 case IFLNK:
558 lnkbuf[0] = '\0';
559 pathlen = 0;
560 getfile(xtrlnkfile, xtrlnkskip);
561 if (pathlen == 0) {
562 vprintf(stdout,
563 "%s: zero length symbolic link (ignored)\n", name);
564 return (GOOD);
565 }
566 if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
567 (void) lutimes(name, timep);
568 (void) lchown(name, uid, gid);
569 (void) lchmod(name, mode);
570 return (GOOD);
571 }
572 return (FAIL);
573
574 case IFCHR:
575 case IFBLK:
576 vprintf(stdout, "extract special file %s\n", name);
577 if (Nflag) {
578 skipfile();
579 return (GOOD);
580 }
581 if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600,
582 (int)curfile.dip->di_rdev) < 0) {
583 fprintf(stderr, "%s: cannot create special file: %s\n",
584 name, strerror(errno));
585 skipfile();
586 return (FAIL);
587 }
588 skipfile();
589 (void) utimes(name, timep);
590 (void) chown(name, uid, gid);
591 (void) chmod(name, mode);
592 (void) chflags(name, flags);
593 return (GOOD);
594
595 case IFIFO:
596 vprintf(stdout, "extract fifo %s\n", name);
597 if (Nflag) {
598 skipfile();
599 return (GOOD);
600 }
601 if (mkfifo(name, 0600) < 0) {
602 fprintf(stderr, "%s: cannot create fifo: %s\n",
603 name, strerror(errno));
604 skipfile();
605 return (FAIL);
606 }
607 skipfile();
608 (void) utimes(name, timep);
609 (void) chown(name, uid, gid);
610 (void) chmod(name, mode);
611 (void) chflags(name, flags);
612 return (GOOD);
613
614 case IFREG:
615 vprintf(stdout, "extract file %s\n", name);
616 if (Nflag) {
617 skipfile();
618 return (GOOD);
619 }
620 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
621 0600)) < 0) {
622 fprintf(stderr, "%s: cannot create file: %s\n",
623 name, strerror(errno));
624 skipfile();
625 return (FAIL);
626 }
627 getfile(xtrfile, xtrskip);
628 (void) futimes(ofile, timep);
629 (void) fchown(ofile, uid, gid);
630 (void) fchmod(ofile, mode);
631 (void) fchflags(ofile, flags);
632 (void) close(ofile);
633 return (GOOD);
634 }
635 /* NOTREACHED */
636 }
637
638 /*
639 * skip over bit maps on the tape
640 */
641 void
642 skipmaps()
643 {
644
645 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
646 skipfile();
647 }
648
649 /*
650 * skip over a file on the tape
651 */
652 void
653 skipfile()
654 {
655
656 curfile.action = SKIP;
657 getfile(xtrnull, xtrnull);
658 }
659
660 /*
661 * Extract a file from the tape.
662 * When an allocated block is found it is passed to the fill function;
663 * when an unallocated block (hole) is found, a zeroed buffer is passed
664 * to the skip function.
665 */
666 void
667 getfile(fill, skip)
668 void (*fill) __P((char *, long));
669 void (*skip) __P((char *, long));
670 {
671 int i;
672 int curblk = 0;
673 quad_t size = spcl.c_dinode.di_size;
674 static char clearedbuf[MAXBSIZE];
675 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
676 char junk[TP_BSIZE];
677
678 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
679 (void)&curblk;
680 (void)&size;
681 #endif
682
683 if (spcl.c_type == TS_END)
684 panic("ran off end of tape\n");
685 if (spcl.c_magic != NFS_MAGIC)
686 panic("not at beginning of a file\n");
687 if (!gettingfile && setjmp(restart) != 0)
688 return;
689 gettingfile++;
690 loop:
691 for (i = 0; i < spcl.c_count; i++) {
692 if (spcl.c_addr[i]) {
693 readtape(&buf[curblk++][0]);
694 if (curblk == fssize / TP_BSIZE) {
695 (*fill)((char *)buf, (long)(size > TP_BSIZE ?
696 fssize : (curblk - 1) * TP_BSIZE + size));
697 curblk = 0;
698 }
699 } else {
700 if (curblk > 0) {
701 (*fill)((char *)buf, (long)(size > TP_BSIZE ?
702 curblk * TP_BSIZE :
703 (curblk - 1) * TP_BSIZE + size));
704 curblk = 0;
705 }
706 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
707 TP_BSIZE : size));
708 }
709 if ((size -= TP_BSIZE) <= 0) {
710 for (i++; i < spcl.c_count; i++)
711 if (spcl.c_addr[i])
712 readtape(junk);
713 break;
714 }
715 }
716 if (gethead(&spcl) == GOOD && size > 0) {
717 if (spcl.c_type == TS_ADDR)
718 goto loop;
719 dprintf(stdout,
720 "Missing address (header) block for %s at %d blocks\n",
721 curfile.name, blksread);
722 }
723 if (curblk > 0)
724 (*fill)((char *)buf, (long)((curblk * TP_BSIZE) + size));
725 findinode(&spcl);
726 gettingfile = 0;
727 }
728
729 /*
730 * Write out the next block of a file.
731 */
732 static void
733 xtrfile(buf, size)
734 char *buf;
735 long size;
736 {
737
738 if (Nflag)
739 return;
740 if (write(ofile, buf, (int) size) == -1) {
741 fprintf(stderr,
742 "write error extracting inode %d, name %s\nwrite: %s\n",
743 curfile.ino, curfile.name, strerror(errno));
744 exit(1);
745 }
746 }
747
748 /*
749 * Skip over a hole in a file.
750 */
751 /* ARGSUSED */
752 static void
753 xtrskip(buf, size)
754 char *buf;
755 long size;
756 {
757
758 if (lseek(ofile, size, SEEK_CUR) == -1) {
759 fprintf(stderr,
760 "seek error extracting inode %d, name %s\nlseek: %s\n",
761 curfile.ino, curfile.name, strerror(errno));
762 exit(1);
763 }
764 }
765
766 /*
767 * Collect the next block of a symbolic link.
768 */
769 static void
770 xtrlnkfile(buf, size)
771 char *buf;
772 long size;
773 {
774
775 pathlen += size;
776 if (pathlen > MAXPATHLEN) {
777 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
778 curfile.name, lnkbuf, buf, pathlen);
779 exit(1);
780 }
781 (void) strcat(lnkbuf, buf);
782 }
783
784 /*
785 * Skip over a hole in a symbolic link (should never happen).
786 */
787 /* ARGSUSED */
788 static void
789 xtrlnkskip(buf, size)
790 char *buf;
791 long size;
792 {
793
794 fprintf(stderr, "unallocated block in symbolic link %s\n",
795 curfile.name);
796 exit(1);
797 }
798
799 /*
800 * Collect the next block of a bit map.
801 */
802 static void
803 xtrmap(buf, size)
804 char *buf;
805 long size;
806 {
807
808 memmove(map, buf, size);
809 map += size;
810 }
811
812 /*
813 * Skip over a hole in a bit map (should never happen).
814 */
815 /* ARGSUSED */
816 static void
817 xtrmapskip(buf, size)
818 char *buf;
819 long size;
820 {
821
822 panic("hole in map\n");
823 map += size;
824 }
825
826 /*
827 * Noop, when an extraction function is not needed.
828 */
829 /* ARGSUSED */
830 void
831 xtrnull(buf, size)
832 char *buf;
833 long size;
834 {
835
836 return;
837 }
838
839 /*
840 * Read TP_BSIZE blocks from the input.
841 * Handle read errors, and end of media.
842 */
843 static void
844 readtape(buf)
845 char *buf;
846 {
847 int rd, newvol, i;
848 int cnt, seek_failed;
849
850 if (blkcnt < numtrec) {
851 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
852 blksread++;
853 tpblksread++;
854 return;
855 }
856 for (i = 0; i < ntrec; i++)
857 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
858 if (numtrec == 0)
859 numtrec = ntrec;
860 cnt = ntrec * TP_BSIZE;
861 rd = 0;
862 getmore:
863 #ifdef RRESTORE
864 if (host)
865 i = rmtread(&tapebuf[rd], cnt);
866 else
867 #endif
868 i = read(mt, &tapebuf[rd], cnt);
869 /*
870 * Check for mid-tape short read error.
871 * If found, skip rest of buffer and start with the next.
872 */
873 if (!pipein && numtrec < ntrec && i > 0) {
874 dprintf(stdout, "mid-media short read error.\n");
875 numtrec = ntrec;
876 }
877 /*
878 * Handle partial block read.
879 */
880 if (pipein && i == 0 && rd > 0)
881 i = rd;
882 else if (i > 0 && i != ntrec * TP_BSIZE) {
883 if (pipein) {
884 rd += i;
885 cnt -= i;
886 if (cnt > 0)
887 goto getmore;
888 i = rd;
889 } else {
890 /*
891 * Short read. Process the blocks read.
892 */
893 if (i % TP_BSIZE != 0)
894 vprintf(stdout,
895 "partial block read: %d should be %d\n",
896 i, ntrec * TP_BSIZE);
897 numtrec = i / TP_BSIZE;
898 }
899 }
900 /*
901 * Handle read error.
902 */
903 if (i < 0) {
904 fprintf(stderr, "Tape read error while ");
905 switch (curfile.action) {
906 default:
907 fprintf(stderr, "trying to set up tape\n");
908 break;
909 case UNKNOWN:
910 fprintf(stderr, "trying to resynchronize\n");
911 break;
912 case USING:
913 fprintf(stderr, "restoring %s\n", curfile.name);
914 break;
915 case SKIP:
916 fprintf(stderr, "skipping over inode %d\n",
917 curfile.ino);
918 break;
919 }
920 if (!yflag && !reply("continue"))
921 exit(1);
922 i = ntrec * TP_BSIZE;
923 memset(tapebuf, 0, i);
924 #ifdef RRESTORE
925 if (host)
926 seek_failed = (rmtseek(i, 1) < 0);
927 else
928 #endif
929 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
930
931 if (seek_failed) {
932 fprintf(stderr,
933 "continuation failed: %s\n", strerror(errno));
934 exit(1);
935 }
936 }
937 /*
938 * Handle end of tape.
939 */
940 if (i == 0) {
941 vprintf(stdout, "End-of-tape encountered\n");
942 if (!pipein) {
943 newvol = volno + 1;
944 volno = 0;
945 numtrec = 0;
946 getvol(newvol);
947 readtape(buf);
948 return;
949 }
950 if (rd % TP_BSIZE != 0)
951 panic("partial block read: %d should be %d\n",
952 rd, ntrec * TP_BSIZE);
953 terminateinput();
954 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
955 }
956 blkcnt = 0;
957 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
958 blksread++;
959 tpblksread++;
960 }
961
962 static void
963 findtapeblksize()
964 {
965 long i;
966
967 for (i = 0; i < ntrec; i++)
968 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
969 blkcnt = 0;
970 #ifdef RRESTORE
971 if (host)
972 i = rmtread(tapebuf, ntrec * TP_BSIZE);
973 else
974 #endif
975 i = read(mt, tapebuf, ntrec * TP_BSIZE);
976
977 if (i <= 0) {
978 fprintf(stderr, "tape read error: %s\n", strerror(errno));
979 exit(1);
980 }
981 if (i % TP_BSIZE != 0) {
982 fprintf(stderr, "Tape block size (%ld) %s (%ld)\n",
983 (long)i, "is not a multiple of dump block size",
984 (long)TP_BSIZE);
985 exit(1);
986 }
987 ntrec = i / TP_BSIZE;
988 numtrec = ntrec;
989 vprintf(stdout, "Tape block size is %d\n", ntrec);
990 }
991
992 void
993 closemt()
994 {
995
996 if (mt < 0)
997 return;
998 #ifdef RRESTORE
999 if (host)
1000 rmtclose();
1001 else
1002 #endif
1003 (void) close(mt);
1004 }
1005
1006 /*
1007 * Read the next block from the tape.
1008 * Check to see if it is one of several vintage headers.
1009 * If it is an old style header, convert it to a new style header.
1010 * If it is not any valid header, return an error.
1011 */
1012 static int
1013 gethead(buf)
1014 struct s_spcl *buf;
1015 {
1016 long i;
1017 union {
1018 quad_t qval;
1019 int32_t val[2];
1020 } qcvt;
1021 union u_ospcl {
1022 char dummy[TP_BSIZE];
1023 struct s_ospcl {
1024 int32_t c_type;
1025 int32_t c_date;
1026 int32_t c_ddate;
1027 int32_t c_volume;
1028 int32_t c_tapea;
1029 u_int16_t c_inumber;
1030 int32_t c_magic;
1031 int32_t c_checksum;
1032 struct odinode {
1033 unsigned short odi_mode;
1034 u_int16_t odi_nlink;
1035 u_int16_t odi_uid;
1036 u_int16_t odi_gid;
1037 int32_t odi_size;
1038 int32_t odi_rdev;
1039 char odi_addr[36];
1040 int32_t odi_atime;
1041 int32_t odi_mtime;
1042 int32_t odi_ctime;
1043 } c_dinode;
1044 int32_t c_count;
1045 char c_addr[256];
1046 } s_ospcl;
1047 } u_ospcl;
1048
1049 if (!cvtflag) {
1050 readtape((char *)buf);
1051 if (buf->c_magic != NFS_MAGIC) {
1052 if (swabl(buf->c_magic) != NFS_MAGIC)
1053 return (FAIL);
1054 if (!Bcvt) {
1055 vprintf(stdout, "Note: Doing Byte swapping\n");
1056 Bcvt = 1;
1057 }
1058 }
1059 if (checksum((int *)buf) == FAIL)
1060 return (FAIL);
1061 if (Bcvt)
1062 swabst((u_char *)"8l4s31l528b1l192b2l", (u_char *)buf);
1063 goto good;
1064 }
1065 readtape((char *)(&u_ospcl.s_ospcl));
1066 memset(buf, 0, (long)TP_BSIZE);
1067 buf->c_type = u_ospcl.s_ospcl.c_type;
1068 buf->c_date = u_ospcl.s_ospcl.c_date;
1069 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1070 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1071 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1072 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1073 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1074 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1075 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1076 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1077 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1078 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1079 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1080 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1081 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1082 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1083 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1084 buf->c_count = u_ospcl.s_ospcl.c_count;
1085 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1086 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1087 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1088 return(FAIL);
1089 buf->c_magic = NFS_MAGIC;
1090
1091 good:
1092 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1093 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1094 qcvt.qval = buf->c_dinode.di_size;
1095 if (qcvt.val[0] || qcvt.val[1]) {
1096 printf("Note: Doing Quad swapping\n");
1097 Qcvt = 1;
1098 }
1099 }
1100 if (Qcvt) {
1101 qcvt.qval = buf->c_dinode.di_size;
1102 i = qcvt.val[1];
1103 qcvt.val[1] = qcvt.val[0];
1104 qcvt.val[0] = i;
1105 buf->c_dinode.di_size = qcvt.qval;
1106 }
1107
1108 switch (buf->c_type) {
1109
1110 case TS_CLRI:
1111 case TS_BITS:
1112 /*
1113 * Have to patch up missing information in bit map headers
1114 */
1115 buf->c_inumber = 0;
1116 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1117 for (i = 0; i < buf->c_count; i++)
1118 buf->c_addr[i]++;
1119 break;
1120
1121 case TS_TAPE:
1122 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1123 oldinofmt = 1;
1124 /* fall through */
1125 case TS_END:
1126 buf->c_inumber = 0;
1127 break;
1128
1129 case TS_INODE:
1130 case TS_ADDR:
1131 break;
1132
1133 default:
1134 panic("gethead: unknown inode type %d\n", buf->c_type);
1135 break;
1136 }
1137 /*
1138 * If we are restoring a filesystem with old format inodes,
1139 * copy the uid/gid to the new location.
1140 */
1141 if (oldinofmt) {
1142 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1143 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1144 }
1145 if (dflag)
1146 accthdr(buf);
1147 return(GOOD);
1148 }
1149
1150 /*
1151 * Check that a header is where it belongs and predict the next header
1152 */
1153 static void
1154 accthdr(header)
1155 struct s_spcl *header;
1156 {
1157 static ino_t previno = 0x7fffffff;
1158 static int prevtype;
1159 static long predict;
1160 long blks, i;
1161
1162 if (header->c_type == TS_TAPE) {
1163 fprintf(stderr, "Volume header (%s inode format) ",
1164 oldinofmt ? "old" : "new");
1165 if (header->c_firstrec)
1166 fprintf(stderr, "begins with record %d",
1167 header->c_firstrec);
1168 fprintf(stderr, "\n");
1169 previno = 0x7fffffff;
1170 return;
1171 }
1172 if (previno == 0x7fffffff)
1173 goto newcalc;
1174 switch (prevtype) {
1175 case TS_BITS:
1176 fprintf(stderr, "Dumped inodes map header");
1177 break;
1178 case TS_CLRI:
1179 fprintf(stderr, "Used inodes map header");
1180 break;
1181 case TS_INODE:
1182 fprintf(stderr, "File header, ino %d", previno);
1183 break;
1184 case TS_ADDR:
1185 fprintf(stderr, "File continuation header, ino %d", previno);
1186 break;
1187 case TS_END:
1188 fprintf(stderr, "End of tape header");
1189 break;
1190 }
1191 if (predict != blksread - 1)
1192 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1193 (long)predict, (long)(blksread - 1));
1194 fprintf(stderr, "\n");
1195 newcalc:
1196 blks = 0;
1197 if (header->c_type != TS_END)
1198 for (i = 0; i < header->c_count; i++)
1199 if (header->c_addr[i] != 0)
1200 blks++;
1201 predict = blks;
1202 blksread = 0;
1203 prevtype = header->c_type;
1204 previno = header->c_inumber;
1205 }
1206
1207 /*
1208 * Find an inode header.
1209 * Complain if had to skip, and complain is set.
1210 */
1211 static void
1212 findinode(header)
1213 struct s_spcl *header;
1214 {
1215 static long skipcnt = 0;
1216 long i;
1217 char buf[TP_BSIZE];
1218
1219 curfile.name = "<name unknown>";
1220 curfile.action = UNKNOWN;
1221 curfile.dip = NULL;
1222 curfile.ino = 0;
1223 do {
1224 if (header->c_magic != NFS_MAGIC) {
1225 skipcnt++;
1226 while (gethead(header) == FAIL ||
1227 header->c_date != dumpdate)
1228 skipcnt++;
1229 }
1230 switch (header->c_type) {
1231
1232 case TS_ADDR:
1233 /*
1234 * Skip up to the beginning of the next record
1235 */
1236 for (i = 0; i < header->c_count; i++)
1237 if (header->c_addr[i])
1238 readtape(buf);
1239 while (gethead(header) == FAIL ||
1240 header->c_date != dumpdate)
1241 skipcnt++;
1242 break;
1243
1244 case TS_INODE:
1245 curfile.dip = &header->c_dinode;
1246 curfile.ino = header->c_inumber;
1247 break;
1248
1249 case TS_END:
1250 curfile.ino = maxino;
1251 break;
1252
1253 case TS_CLRI:
1254 curfile.name = "<file removal list>";
1255 break;
1256
1257 case TS_BITS:
1258 curfile.name = "<file dump list>";
1259 break;
1260
1261 case TS_TAPE:
1262 panic("unexpected tape header\n");
1263 /* NOTREACHED */
1264
1265 default:
1266 panic("unknown tape header type %d\n", spcl.c_type);
1267 /* NOTREACHED */
1268
1269 }
1270 } while (header->c_type == TS_ADDR);
1271 if (skipcnt > 0)
1272 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1273 (long)skipcnt);
1274 skipcnt = 0;
1275 }
1276
1277 static int
1278 checksum(buf)
1279 int *buf;
1280 {
1281 int i, j;
1282
1283 j = sizeof(union u_spcl) / sizeof(int);
1284 i = 0;
1285 if(!Bcvt) {
1286 do
1287 i += *buf++;
1288 while (--j);
1289 } else {
1290 /* What happens if we want to read restore tapes
1291 for a 16bit int machine??? */
1292 do
1293 i += swabl(*buf++);
1294 while (--j);
1295 }
1296
1297 if (i != CHECKSUM) {
1298 fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
1299 curfile.ino, curfile.name);
1300 return(FAIL);
1301 }
1302 return(GOOD);
1303 }
1304
1305 #ifdef RRESTORE
1306 #if __STDC__
1307 #include <stdarg.h>
1308 #else
1309 #include <varargs.h>
1310 #endif
1311
1312 void
1313 #if __STDC__
1314 msg(const char *fmt, ...)
1315 #else
1316 msg(fmt, va_alist)
1317 char *fmt;
1318 va_dcl
1319 #endif
1320 {
1321 va_list ap;
1322 #if __STDC__
1323 va_start(ap, fmt);
1324 #else
1325 va_start(ap);
1326 #endif
1327 (void)vfprintf(stderr, fmt, ap);
1328 va_end(ap);
1329 }
1330 #endif /* RRESTORE */
1331
1332 static u_char *
1333 swabshort(sp, n)
1334 u_char *sp;
1335 int n;
1336 {
1337 char c;
1338
1339 while (--n >= 0) {
1340 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1341 sp += 2;
1342 }
1343 return (sp);
1344 }
1345
1346 static u_char *
1347 swablong(sp, n)
1348 u_char *sp;
1349 int n;
1350 {
1351 char c;
1352
1353 while (--n >= 0) {
1354 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1355 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1356 sp += 4;
1357 }
1358 return (sp);
1359 }
1360
1361 void
1362 swabst(cp, sp)
1363 u_char *cp, *sp;
1364 {
1365 int n = 0;
1366
1367 while (*cp) {
1368 switch (*cp) {
1369 case '0': case '1': case '2': case '3': case '4':
1370 case '5': case '6': case '7': case '8': case '9':
1371 n = (n * 10) + (*cp++ - '0');
1372 continue;
1373
1374 case 's': case 'w': case 'h':
1375 if (n == 0)
1376 n = 1;
1377 sp = swabshort(sp, n);
1378 break;
1379
1380 case 'l':
1381 if (n == 0)
1382 n = 1;
1383 sp = swablong(sp, n);
1384 break;
1385
1386 default: /* Any other character, like 'b' counts as byte. */
1387 if (n == 0)
1388 n = 1;
1389 sp += n;
1390 break;
1391 }
1392 cp++;
1393 n = 0;
1394 }
1395 }
1396
1397 static u_long
1398 swabl(x)
1399 u_long x;
1400 {
1401 swabst((u_char *)"l", (u_char *)&x);
1402 return (x);
1403 }
1404