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