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