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