tape.c revision 1.32 1 /* $NetBSD: tape.c,v 1.32 1997/11/18 02:56:35 enami 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.32 1997/11/18 02:56:35 enami 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 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, (int)curfile.dip->di_rdev) < 0) {
582 fprintf(stderr, "%s: cannot create special file: %s\n",
583 name, strerror(errno));
584 skipfile();
585 return (FAIL);
586 }
587 skipfile();
588 (void) utimes(name, timep);
589 (void) chown(name, uid, gid);
590 (void) chmod(name, mode);
591 (void) chflags(name, flags);
592 return (GOOD);
593
594 case IFIFO:
595 vprintf(stdout, "extract fifo %s\n", name);
596 if (Nflag) {
597 skipfile();
598 return (GOOD);
599 }
600 if (mkfifo(name, mode) < 0) {
601 fprintf(stderr, "%s: cannot create fifo: %s\n",
602 name, strerror(errno));
603 skipfile();
604 return (FAIL);
605 }
606 skipfile();
607 (void) utimes(name, timep);
608 (void) chown(name, uid, gid);
609 (void) chmod(name, mode);
610 (void) chflags(name, flags);
611 return (GOOD);
612
613 case IFREG:
614 vprintf(stdout, "extract file %s\n", name);
615 if (Nflag) {
616 skipfile();
617 return (GOOD);
618 }
619 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
620 0600)) < 0) {
621 fprintf(stderr, "%s: cannot create file: %s\n",
622 name, strerror(errno));
623 skipfile();
624 return (FAIL);
625 }
626 getfile(xtrfile, xtrskip);
627 (void) futimes(ofile, timep);
628 (void) fchown(ofile, uid, gid);
629 (void) fchmod(ofile, mode);
630 (void) fchflags(ofile, flags);
631 (void) close(ofile);
632 return (GOOD);
633 }
634 /* NOTREACHED */
635 }
636
637 /*
638 * skip over bit maps on the tape
639 */
640 void
641 skipmaps()
642 {
643
644 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
645 skipfile();
646 }
647
648 /*
649 * skip over a file on the tape
650 */
651 void
652 skipfile()
653 {
654
655 curfile.action = SKIP;
656 getfile(xtrnull, xtrnull);
657 }
658
659 /*
660 * Extract a file from the tape.
661 * When an allocated block is found it is passed to the fill function;
662 * when an unallocated block (hole) is found, a zeroed buffer is passed
663 * to the skip function.
664 */
665 void
666 getfile(fill, skip)
667 void (*fill) __P((char *, long));
668 void (*skip) __P((char *, long));
669 {
670 int i;
671 int curblk = 0;
672 quad_t size = spcl.c_dinode.di_size;
673 static char clearedbuf[MAXBSIZE];
674 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
675 char junk[TP_BSIZE];
676
677 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
678 (void)&curblk;
679 (void)&size;
680 #endif
681
682 if (spcl.c_type == TS_END)
683 panic("ran off end of tape\n");
684 if (spcl.c_magic != NFS_MAGIC)
685 panic("not at beginning of a file\n");
686 if (!gettingfile && setjmp(restart) != 0)
687 return;
688 gettingfile++;
689 loop:
690 for (i = 0; i < spcl.c_count; i++) {
691 if (spcl.c_addr[i]) {
692 readtape(&buf[curblk++][0]);
693 if (curblk == fssize / TP_BSIZE) {
694 (*fill)((char *)buf, (long)(size > TP_BSIZE ?
695 fssize : (curblk - 1) * TP_BSIZE + size));
696 curblk = 0;
697 }
698 } else {
699 if (curblk > 0) {
700 (*fill)((char *)buf, (long)(size > TP_BSIZE ?
701 curblk * TP_BSIZE :
702 (curblk - 1) * TP_BSIZE + size));
703 curblk = 0;
704 }
705 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
706 TP_BSIZE : size));
707 }
708 if ((size -= TP_BSIZE) <= 0) {
709 for (i++; i < spcl.c_count; i++)
710 if (spcl.c_addr[i])
711 readtape(junk);
712 break;
713 }
714 }
715 if (gethead(&spcl) == GOOD && size > 0) {
716 if (spcl.c_type == TS_ADDR)
717 goto loop;
718 dprintf(stdout,
719 "Missing address (header) block for %s at %d blocks\n",
720 curfile.name, blksread);
721 }
722 if (curblk > 0)
723 (*fill)((char *)buf, (long)((curblk * TP_BSIZE) + size));
724 findinode(&spcl);
725 gettingfile = 0;
726 }
727
728 /*
729 * Write out the next block of a file.
730 */
731 static void
732 xtrfile(buf, size)
733 char *buf;
734 long size;
735 {
736
737 if (Nflag)
738 return;
739 if (write(ofile, buf, (int) size) == -1) {
740 fprintf(stderr,
741 "write error extracting inode %d, name %s\nwrite: %s\n",
742 curfile.ino, curfile.name, strerror(errno));
743 exit(1);
744 }
745 }
746
747 /*
748 * Skip over a hole in a file.
749 */
750 /* ARGSUSED */
751 static void
752 xtrskip(buf, size)
753 char *buf;
754 long size;
755 {
756
757 if (lseek(ofile, size, SEEK_CUR) == -1) {
758 fprintf(stderr,
759 "seek error extracting inode %d, name %s\nlseek: %s\n",
760 curfile.ino, curfile.name, strerror(errno));
761 exit(1);
762 }
763 }
764
765 /*
766 * Collect the next block of a symbolic link.
767 */
768 static void
769 xtrlnkfile(buf, size)
770 char *buf;
771 long size;
772 {
773
774 pathlen += size;
775 if (pathlen > MAXPATHLEN) {
776 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
777 curfile.name, lnkbuf, buf, pathlen);
778 exit(1);
779 }
780 (void) strcat(lnkbuf, buf);
781 }
782
783 /*
784 * Skip over a hole in a symbolic link (should never happen).
785 */
786 /* ARGSUSED */
787 static void
788 xtrlnkskip(buf, size)
789 char *buf;
790 long size;
791 {
792
793 fprintf(stderr, "unallocated block in symbolic link %s\n",
794 curfile.name);
795 exit(1);
796 }
797
798 /*
799 * Collect the next block of a bit map.
800 */
801 static void
802 xtrmap(buf, size)
803 char *buf;
804 long size;
805 {
806
807 memmove(map, buf, size);
808 map += size;
809 }
810
811 /*
812 * Skip over a hole in a bit map (should never happen).
813 */
814 /* ARGSUSED */
815 static void
816 xtrmapskip(buf, size)
817 char *buf;
818 long size;
819 {
820
821 panic("hole in map\n");
822 map += size;
823 }
824
825 /*
826 * Noop, when an extraction function is not needed.
827 */
828 /* ARGSUSED */
829 void
830 xtrnull(buf, size)
831 char *buf;
832 long size;
833 {
834
835 return;
836 }
837
838 /*
839 * Read TP_BSIZE blocks from the input.
840 * Handle read errors, and end of media.
841 */
842 static void
843 readtape(buf)
844 char *buf;
845 {
846 int rd, newvol, i;
847 int cnt, seek_failed;
848
849 if (blkcnt < numtrec) {
850 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
851 blksread++;
852 tpblksread++;
853 return;
854 }
855 for (i = 0; i < ntrec; i++)
856 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
857 if (numtrec == 0)
858 numtrec = ntrec;
859 cnt = ntrec * TP_BSIZE;
860 rd = 0;
861 getmore:
862 #ifdef RRESTORE
863 if (host)
864 i = rmtread(&tapebuf[rd], cnt);
865 else
866 #endif
867 i = read(mt, &tapebuf[rd], cnt);
868 /*
869 * Check for mid-tape short read error.
870 * If found, skip rest of buffer and start with the next.
871 */
872 if (!pipein && numtrec < ntrec && i > 0) {
873 dprintf(stdout, "mid-media short read error.\n");
874 numtrec = ntrec;
875 }
876 /*
877 * Handle partial block read.
878 */
879 if (pipein && i == 0 && rd > 0)
880 i = rd;
881 else if (i > 0 && i != ntrec * TP_BSIZE) {
882 if (pipein) {
883 rd += i;
884 cnt -= i;
885 if (cnt > 0)
886 goto getmore;
887 i = rd;
888 } else {
889 /*
890 * Short read. Process the blocks read.
891 */
892 if (i % TP_BSIZE != 0)
893 vprintf(stdout,
894 "partial block read: %d should be %d\n",
895 i, ntrec * TP_BSIZE);
896 numtrec = i / TP_BSIZE;
897 }
898 }
899 /*
900 * Handle read error.
901 */
902 if (i < 0) {
903 fprintf(stderr, "Tape read error while ");
904 switch (curfile.action) {
905 default:
906 fprintf(stderr, "trying to set up tape\n");
907 break;
908 case UNKNOWN:
909 fprintf(stderr, "trying to resynchronize\n");
910 break;
911 case USING:
912 fprintf(stderr, "restoring %s\n", curfile.name);
913 break;
914 case SKIP:
915 fprintf(stderr, "skipping over inode %d\n",
916 curfile.ino);
917 break;
918 }
919 if (!yflag && !reply("continue"))
920 exit(1);
921 i = ntrec * TP_BSIZE;
922 memset(tapebuf, 0, i);
923 #ifdef RRESTORE
924 if (host)
925 seek_failed = (rmtseek(i, 1) < 0);
926 else
927 #endif
928 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
929
930 if (seek_failed) {
931 fprintf(stderr,
932 "continuation failed: %s\n", strerror(errno));
933 exit(1);
934 }
935 }
936 /*
937 * Handle end of tape.
938 */
939 if (i == 0) {
940 vprintf(stdout, "End-of-tape encountered\n");
941 if (!pipein) {
942 newvol = volno + 1;
943 volno = 0;
944 numtrec = 0;
945 getvol(newvol);
946 readtape(buf);
947 return;
948 }
949 if (rd % TP_BSIZE != 0)
950 panic("partial block read: %d should be %d\n",
951 rd, ntrec * TP_BSIZE);
952 terminateinput();
953 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
954 }
955 blkcnt = 0;
956 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
957 blksread++;
958 tpblksread++;
959 }
960
961 static void
962 findtapeblksize()
963 {
964 long i;
965
966 for (i = 0; i < ntrec; i++)
967 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
968 blkcnt = 0;
969 #ifdef RRESTORE
970 if (host)
971 i = rmtread(tapebuf, ntrec * TP_BSIZE);
972 else
973 #endif
974 i = read(mt, tapebuf, ntrec * TP_BSIZE);
975
976 if (i <= 0) {
977 fprintf(stderr, "tape read error: %s\n", strerror(errno));
978 exit(1);
979 }
980 if (i % TP_BSIZE != 0) {
981 fprintf(stderr, "Tape block size (%ld) %s (%ld)\n",
982 (long)i, "is not a multiple of dump block size",
983 (long)TP_BSIZE);
984 exit(1);
985 }
986 ntrec = i / TP_BSIZE;
987 numtrec = ntrec;
988 vprintf(stdout, "Tape block size is %d\n", ntrec);
989 }
990
991 void
992 closemt()
993 {
994
995 if (mt < 0)
996 return;
997 #ifdef RRESTORE
998 if (host)
999 rmtclose();
1000 else
1001 #endif
1002 (void) close(mt);
1003 }
1004
1005 /*
1006 * Read the next block from the tape.
1007 * Check to see if it is one of several vintage headers.
1008 * If it is an old style header, convert it to a new style header.
1009 * If it is not any valid header, return an error.
1010 */
1011 static int
1012 gethead(buf)
1013 struct s_spcl *buf;
1014 {
1015 long i;
1016 union {
1017 quad_t qval;
1018 int32_t val[2];
1019 } qcvt;
1020 union u_ospcl {
1021 char dummy[TP_BSIZE];
1022 struct s_ospcl {
1023 int32_t c_type;
1024 int32_t c_date;
1025 int32_t c_ddate;
1026 int32_t c_volume;
1027 int32_t c_tapea;
1028 u_int16_t c_inumber;
1029 int32_t c_magic;
1030 int32_t c_checksum;
1031 struct odinode {
1032 unsigned short odi_mode;
1033 u_int16_t odi_nlink;
1034 u_int16_t odi_uid;
1035 u_int16_t odi_gid;
1036 int32_t odi_size;
1037 int32_t odi_rdev;
1038 char odi_addr[36];
1039 int32_t odi_atime;
1040 int32_t odi_mtime;
1041 int32_t odi_ctime;
1042 } c_dinode;
1043 int32_t c_count;
1044 char c_addr[256];
1045 } s_ospcl;
1046 } u_ospcl;
1047
1048 if (!cvtflag) {
1049 readtape((char *)buf);
1050 if (buf->c_magic != NFS_MAGIC) {
1051 if (swabl(buf->c_magic) != NFS_MAGIC)
1052 return (FAIL);
1053 if (!Bcvt) {
1054 vprintf(stdout, "Note: Doing Byte swapping\n");
1055 Bcvt = 1;
1056 }
1057 }
1058 if (checksum((int *)buf) == FAIL)
1059 return (FAIL);
1060 if (Bcvt)
1061 swabst((u_char *)"8l4s31l528b1l192b2l", (u_char *)buf);
1062 goto good;
1063 }
1064 readtape((char *)(&u_ospcl.s_ospcl));
1065 memset(buf, 0, (long)TP_BSIZE);
1066 buf->c_type = u_ospcl.s_ospcl.c_type;
1067 buf->c_date = u_ospcl.s_ospcl.c_date;
1068 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1069 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1070 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1071 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1072 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1073 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1074 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1075 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1076 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1077 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1078 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1079 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1080 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1081 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1082 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1083 buf->c_count = u_ospcl.s_ospcl.c_count;
1084 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1085 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1086 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1087 return(FAIL);
1088 buf->c_magic = NFS_MAGIC;
1089
1090 good:
1091 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1092 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1093 qcvt.qval = buf->c_dinode.di_size;
1094 if (qcvt.val[0] || qcvt.val[1]) {
1095 printf("Note: Doing Quad swapping\n");
1096 Qcvt = 1;
1097 }
1098 }
1099 if (Qcvt) {
1100 qcvt.qval = buf->c_dinode.di_size;
1101 i = qcvt.val[1];
1102 qcvt.val[1] = qcvt.val[0];
1103 qcvt.val[0] = i;
1104 buf->c_dinode.di_size = qcvt.qval;
1105 }
1106
1107 switch (buf->c_type) {
1108
1109 case TS_CLRI:
1110 case TS_BITS:
1111 /*
1112 * Have to patch up missing information in bit map headers
1113 */
1114 buf->c_inumber = 0;
1115 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1116 for (i = 0; i < buf->c_count; i++)
1117 buf->c_addr[i]++;
1118 break;
1119
1120 case TS_TAPE:
1121 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1122 oldinofmt = 1;
1123 /* fall through */
1124 case TS_END:
1125 buf->c_inumber = 0;
1126 break;
1127
1128 case TS_INODE:
1129 case TS_ADDR:
1130 break;
1131
1132 default:
1133 panic("gethead: unknown inode type %d\n", buf->c_type);
1134 break;
1135 }
1136 /*
1137 * If we are restoring a filesystem with old format inodes,
1138 * copy the uid/gid to the new location.
1139 */
1140 if (oldinofmt) {
1141 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1142 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1143 }
1144 if (dflag)
1145 accthdr(buf);
1146 return(GOOD);
1147 }
1148
1149 /*
1150 * Check that a header is where it belongs and predict the next header
1151 */
1152 static void
1153 accthdr(header)
1154 struct s_spcl *header;
1155 {
1156 static ino_t previno = 0x7fffffff;
1157 static int prevtype;
1158 static long predict;
1159 long blks, i;
1160
1161 if (header->c_type == TS_TAPE) {
1162 fprintf(stderr, "Volume header (%s inode format) ",
1163 oldinofmt ? "old" : "new");
1164 if (header->c_firstrec)
1165 fprintf(stderr, "begins with record %d",
1166 header->c_firstrec);
1167 fprintf(stderr, "\n");
1168 previno = 0x7fffffff;
1169 return;
1170 }
1171 if (previno == 0x7fffffff)
1172 goto newcalc;
1173 switch (prevtype) {
1174 case TS_BITS:
1175 fprintf(stderr, "Dumped inodes map header");
1176 break;
1177 case TS_CLRI:
1178 fprintf(stderr, "Used inodes map header");
1179 break;
1180 case TS_INODE:
1181 fprintf(stderr, "File header, ino %d", previno);
1182 break;
1183 case TS_ADDR:
1184 fprintf(stderr, "File continuation header, ino %d", previno);
1185 break;
1186 case TS_END:
1187 fprintf(stderr, "End of tape header");
1188 break;
1189 }
1190 if (predict != blksread - 1)
1191 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1192 (long)predict, (long)(blksread - 1));
1193 fprintf(stderr, "\n");
1194 newcalc:
1195 blks = 0;
1196 if (header->c_type != TS_END)
1197 for (i = 0; i < header->c_count; i++)
1198 if (header->c_addr[i] != 0)
1199 blks++;
1200 predict = blks;
1201 blksread = 0;
1202 prevtype = header->c_type;
1203 previno = header->c_inumber;
1204 }
1205
1206 /*
1207 * Find an inode header.
1208 * Complain if had to skip, and complain is set.
1209 */
1210 static void
1211 findinode(header)
1212 struct s_spcl *header;
1213 {
1214 static long skipcnt = 0;
1215 long i;
1216 char buf[TP_BSIZE];
1217
1218 curfile.name = "<name unknown>";
1219 curfile.action = UNKNOWN;
1220 curfile.dip = NULL;
1221 curfile.ino = 0;
1222 do {
1223 if (header->c_magic != NFS_MAGIC) {
1224 skipcnt++;
1225 while (gethead(header) == FAIL ||
1226 header->c_date != dumpdate)
1227 skipcnt++;
1228 }
1229 switch (header->c_type) {
1230
1231 case TS_ADDR:
1232 /*
1233 * Skip up to the beginning of the next record
1234 */
1235 for (i = 0; i < header->c_count; i++)
1236 if (header->c_addr[i])
1237 readtape(buf);
1238 while (gethead(header) == FAIL ||
1239 header->c_date != dumpdate)
1240 skipcnt++;
1241 break;
1242
1243 case TS_INODE:
1244 curfile.dip = &header->c_dinode;
1245 curfile.ino = header->c_inumber;
1246 break;
1247
1248 case TS_END:
1249 curfile.ino = maxino;
1250 break;
1251
1252 case TS_CLRI:
1253 curfile.name = "<file removal list>";
1254 break;
1255
1256 case TS_BITS:
1257 curfile.name = "<file dump list>";
1258 break;
1259
1260 case TS_TAPE:
1261 panic("unexpected tape header\n");
1262 /* NOTREACHED */
1263
1264 default:
1265 panic("unknown tape header type %d\n", spcl.c_type);
1266 /* NOTREACHED */
1267
1268 }
1269 } while (header->c_type == TS_ADDR);
1270 if (skipcnt > 0)
1271 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1272 (long)skipcnt);
1273 skipcnt = 0;
1274 }
1275
1276 static int
1277 checksum(buf)
1278 int *buf;
1279 {
1280 int i, j;
1281
1282 j = sizeof(union u_spcl) / sizeof(int);
1283 i = 0;
1284 if(!Bcvt) {
1285 do
1286 i += *buf++;
1287 while (--j);
1288 } else {
1289 /* What happens if we want to read restore tapes
1290 for a 16bit int machine??? */
1291 do
1292 i += swabl(*buf++);
1293 while (--j);
1294 }
1295
1296 if (i != CHECKSUM) {
1297 fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
1298 curfile.ino, curfile.name);
1299 return(FAIL);
1300 }
1301 return(GOOD);
1302 }
1303
1304 #ifdef RRESTORE
1305 #if __STDC__
1306 #include <stdarg.h>
1307 #else
1308 #include <varargs.h>
1309 #endif
1310
1311 void
1312 #if __STDC__
1313 msg(const char *fmt, ...)
1314 #else
1315 msg(fmt, va_alist)
1316 char *fmt;
1317 va_dcl
1318 #endif
1319 {
1320 va_list ap;
1321 #if __STDC__
1322 va_start(ap, fmt);
1323 #else
1324 va_start(ap);
1325 #endif
1326 (void)vfprintf(stderr, fmt, ap);
1327 va_end(ap);
1328 }
1329 #endif /* RRESTORE */
1330
1331 static u_char *
1332 swabshort(sp, n)
1333 u_char *sp;
1334 int n;
1335 {
1336 char c;
1337
1338 while (--n >= 0) {
1339 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1340 sp += 2;
1341 }
1342 return (sp);
1343 }
1344
1345 static u_char *
1346 swablong(sp, n)
1347 u_char *sp;
1348 int n;
1349 {
1350 char c;
1351
1352 while (--n >= 0) {
1353 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1354 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1355 sp += 4;
1356 }
1357 return (sp);
1358 }
1359
1360 void
1361 swabst(cp, sp)
1362 u_char *cp, *sp;
1363 {
1364 int n = 0;
1365
1366 while (*cp) {
1367 switch (*cp) {
1368 case '0': case '1': case '2': case '3': case '4':
1369 case '5': case '6': case '7': case '8': case '9':
1370 n = (n * 10) + (*cp++ - '0');
1371 continue;
1372
1373 case 's': case 'w': case 'h':
1374 if (n == 0)
1375 n = 1;
1376 sp = swabshort(sp, n);
1377 break;
1378
1379 case 'l':
1380 if (n == 0)
1381 n = 1;
1382 sp = swablong(sp, n);
1383 break;
1384
1385 default: /* Any other character, like 'b' counts as byte. */
1386 if (n == 0)
1387 n = 1;
1388 sp += n;
1389 break;
1390 }
1391 cp++;
1392 n = 0;
1393 }
1394 }
1395
1396 static u_long
1397 swabl(x)
1398 u_long x;
1399 {
1400 swabst((u_char *)"l", (u_char *)&x);
1401 return (x);
1402 }
1403