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