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