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