]> git.wh0rd.org - dump.git/blob - restore/tape.c
ba772a72fff19d0ea652052c04231ec43170c2c3
[dump.git] / restore / tape.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
6 */
7
8 /*
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 * (c) UNIX System Laboratories, Inc.
12 * All or some portions of this file are derived from material licensed
13 * to the University of California by American Telephone and Telegraph
14 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
15 * the permission of UNIX System Laboratories, Inc.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 */
45
46 #ifndef lint
47 static const char rcsid[] =
48 "$Id: tape.c,v 1.8 1999/11/11 16:14:01 tiniou Exp $";
49 #endif /* not lint */
50
51 #include <sys/param.h>
52 #include <sys/file.h>
53 #include <sys/mtio.h>
54 #include <sys/stat.h>
55
56 #ifdef __linux__
57 #include <sys/time.h>
58 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
60 #else /* __linux__ */
61 #include <ufs/ufs/dinode.h>
62 #endif /* __linux__ */
63 #include <protocols/dumprestore.h>
64
65 #include <errno.h>
66 #include <compaterr.h>
67 #include <setjmp.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72
73 #ifdef __linux__
74 #include <ext2fs/ext2fs.h>
75 #endif
76
77 #include "restore.h"
78 #include "extern.h"
79 #include "pathnames.h"
80
81 static long fssize = MAXBSIZE;
82 static int mt = -1;
83 static int pipein = 0;
84 static char *magtape;
85 static int blkcnt;
86 static int numtrec;
87 static char *tapebuf;
88 static union u_spcl endoftapemark;
89 static long blksread; /* blocks read since last header */
90 static long tpblksread = 0; /* TP_BSIZE blocks read */
91 static long tapesread;
92 static sigjmp_buf restart;
93 static int gettingfile = 0; /* restart has a valid frame */
94 static char *host = NULL;
95
96 static int ofile;
97 static char *map;
98 static char lnkbuf[MAXPATHLEN + 1];
99 static int pathlen;
100
101 int oldinofmt; /* old inode format conversion required */
102 int Bcvt; /* Swap Bytes (for CCI or sun) */
103 static int Qcvt; /* Swap quads (for sun) */
104
105 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
106
107 static void accthdr __P((struct s_spcl *));
108 static int checksum __P((int *));
109 static void findinode __P((struct s_spcl *));
110 static void findtapeblksize __P((void));
111 static int gethead __P((struct s_spcl *));
112 static void readtape __P((char *));
113 static void setdumpnum __P((void));
114 static u_int swabi __P((u_int));
115 static u_long swabl __P((u_long));
116 static u_char *swab64 __P((u_char *, int));
117 static u_char *swab32 __P((u_char *, int));
118 static u_char *swab16 __P((u_char *, int));
119 static void terminateinput __P((void));
120 static void xtrfile __P((char *, size_t));
121 static void xtrlnkfile __P((char *, size_t));
122 static void xtrlnkskip __P((char *, size_t));
123 static void xtrmap __P((char *, size_t));
124 static void xtrmapskip __P((char *, size_t));
125 static void xtrskip __P((char *, size_t));
126
127 static int readmapflag;
128
129 /*
130 * Set up an input source
131 */
132 void
133 setinput(char *source)
134 {
135 FLUSHTAPEBUF();
136 if (bflag)
137 newtapebuf(ntrec);
138 else
139 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
140 terminal = stdin;
141
142 #ifdef RRESTORE
143 if (strchr(source, ':')) {
144 host = source;
145 source = strchr(host, ':');
146 *source++ = '\0';
147 if (rmthost(host) == 0)
148 exit(1);
149 } else
150 #endif
151 if (strcmp(source, "-") == 0) {
152 /*
153 * Since input is coming from a pipe we must establish
154 * our own connection to the terminal.
155 */
156 terminal = fopen(_PATH_TTY, "r");
157 if (terminal == NULL) {
158 warn("cannot open %s", _PATH_TTY);
159 terminal = fopen(_PATH_DEVNULL, "r");
160 if (terminal == NULL)
161 err(1, "cannot open %s", _PATH_DEVNULL);
162 }
163 pipein++;
164 }
165 setuid(getuid()); /* no longer need or want root privileges */
166 magtape = strdup(source);
167 if (magtape == NULL)
168 errx(1, "Cannot allocate space for magtape buffer");
169 }
170
171 void
172 newtapebuf(long size)
173 {
174 static int tapebufsize = -1;
175
176 ntrec = size;
177 if (size <= tapebufsize)
178 return;
179 if (tapebuf != NULL)
180 free(tapebuf);
181 tapebuf = malloc(size * TP_BSIZE);
182 if (tapebuf == NULL)
183 errx(1, "Cannot allocate space for tape buffer");
184 tapebufsize = size;
185 }
186
187 /*
188 * Verify that the tape drive can be accessed and
189 * that it actually is a dump tape.
190 */
191 void
192 setup(void)
193 {
194 int i, j, *ip;
195 struct stat stbuf;
196
197 Vprintf(stdout, "Verify tape and initialize maps\n");
198 #ifdef RRESTORE
199 if (host)
200 mt = rmtopen(magtape, 0);
201 else
202 #endif
203 if (pipein)
204 mt = 0;
205 else
206 mt = open(magtape, O_RDONLY, 0);
207 if (mt < 0)
208 err(1, "%s", magtape);
209 volno = 1;
210 setdumpnum();
211 FLUSHTAPEBUF();
212 if (!pipein && !bflag)
213 findtapeblksize();
214 if (gethead(&spcl) == FAIL) {
215 blkcnt--; /* push back this block */
216 blksread--;
217 tpblksread--;
218 cvtflag++;
219 if (gethead(&spcl) == FAIL)
220 errx(1, "Tape is not a dump tape");
221 fprintf(stderr, "Converting to new file system format.\n");
222 }
223 if (pipein) {
224 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
225 endoftapemark.s_spcl.c_type = TS_END;
226 ip = (int *)&endoftapemark;
227 j = sizeof(union u_spcl) / sizeof(int);
228 i = 0;
229 do
230 i += *ip++;
231 while (--j);
232 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
233 }
234 if (vflag || command == 't' || command == 'C')
235 printdumpinfo();
236 if (filesys == NULL) {
237 filesys = spcl.c_filesys;
238 }
239 dumptime = spcl.c_ddate;
240 dumpdate = spcl.c_date;
241 if (stat(".", &stbuf) < 0)
242 err(1, "cannot stat .");
243 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
244 fssize = TP_BSIZE;
245 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
246 fssize = stbuf.st_blksize;
247 if (((fssize - 1) & fssize) != 0)
248 errx(1, "bad block size %ld", fssize);
249 if (spcl.c_volume != 1)
250 errx(1, "Tape is not volume 1 of the dump");
251 if (gethead(&spcl) == FAIL) {
252 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
253 panic("no header after volume mark!\n");
254 }
255 findinode(&spcl);
256 if (spcl.c_type != TS_CLRI)
257 errx(1, "Cannot find file removal list");
258 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
259 Dprintf(stdout, "maxino = %ld\n", maxino);
260 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
261 if (map == NULL)
262 panic("no memory for active inode map\n");
263 usedinomap = map;
264 curfile.action = USING;
265 getfile(xtrmap, xtrmapskip);
266 if (spcl.c_type != TS_BITS)
267 errx(1, "Cannot find file dump list");
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 overridden by
288 * the user when only extracting a subset of the files.
289 */
290 void
291 getvol(long nextvol)
292 {
293 long newvol = 0, savecnt = 0, wantnext = 0, i;
294 union u_spcl tmpspcl;
295 # define tmpbuf tmpspcl.s_spcl
296 char buf[TP_BSIZE];
297
298 if (nextvol == 1) {
299 tapesread = 0;
300 gettingfile = 0;
301 }
302 if (pipein) {
303 if (nextvol != 1)
304 panic("Changing volumes on pipe input?\n");
305 if (volno == 1)
306 return;
307 goto gethdr;
308 }
309 savecnt = blksread;
310 again:
311 if (pipein)
312 exit(1); /* pipes do not get a second chance */
313 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
314 newvol = nextvol;
315 wantnext = 1;
316 } else {
317 newvol = 0;
318 wantnext = 0;
319 }
320 while (newvol <= 0) {
321 if (tapesread == 0) {
322 fprintf(stderr, "%s%s%s%s%s",
323 "You have not read any tapes yet.\n",
324 "Unless you know which volume your",
325 " file(s) are on you should start\n",
326 "with the last volume and work",
327 " towards the first.\n");
328 } else {
329 fprintf(stderr, "You have read volumes");
330 strcpy(buf, ": ");
331 for (i = 1; i < 32; i++)
332 if (tapesread & (1 << i)) {
333 fprintf(stderr, "%s%ld", buf, (long)i);
334 strcpy(buf, ", ");
335 }
336 fprintf(stderr, "\n");
337 }
338 do {
339 fprintf(stderr, "Specify next volume #: ");
340 (void) fflush(stderr);
341 (void) fgets(buf, BUFSIZ, terminal);
342 } while (!feof(terminal) && buf[0] == '\n');
343 if (feof(terminal))
344 exit(1);
345 newvol = atoi(buf);
346 if (newvol <= 0) {
347 fprintf(stderr,
348 "Volume numbers are positive numerics\n");
349 }
350 }
351 if (newvol == volno) {
352 tapesread |= 1 << volno;
353 return;
354 }
355 closemt();
356 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
357 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
358 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
359 (void) fflush(stderr);
360 (void) fgets(buf, BUFSIZ, terminal);
361 if (feof(terminal))
362 exit(1);
363 if (!strcmp(buf, "none\n")) {
364 terminateinput();
365 return;
366 }
367 if (buf[0] != '\n') {
368 (void) strcpy(magtape, buf);
369 magtape[strlen(magtape) - 1] = '\0';
370 }
371 #ifdef RRESTORE
372 if (host)
373 mt = rmtopen(magtape, 0);
374 else
375 #endif
376 mt = open(magtape, O_RDONLY, 0);
377
378 if (mt == -1) {
379 fprintf(stderr, "Cannot open %s\n", magtape);
380 volno = -1;
381 goto again;
382 }
383 gethdr:
384 volno = newvol;
385 setdumpnum();
386 FLUSHTAPEBUF();
387 if (gethead(&tmpbuf) == FAIL) {
388 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
389 fprintf(stderr, "tape is not dump tape\n");
390 volno = 0;
391 goto again;
392 }
393 if (tmpbuf.c_volume != volno) {
394 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
395 volno = 0;
396 goto again;
397 }
398 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
399 #ifdef __linux__
400 fprintf(stderr, "Wrong dump date\n\tgot: %s",
401 ctime4(&tmpbuf.c_date));
402 fprintf(stderr, "\twanted: %s", ctime4(&dumpdate));
403 #else
404 fprintf(stderr, "Wrong dump date\n\tgot: %s",
405 ctime(&tmpbuf.c_date));
406 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
407 #endif
408 volno = 0;
409 goto again;
410 }
411 tapesread |= 1 << volno;
412 blksread = savecnt;
413 /*
414 * If continuing from the previous volume, skip over any
415 * blocks read already at the end of the previous volume.
416 *
417 * If coming to this volume at random, skip to the beginning
418 * of the next record.
419 */
420 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
421 tpblksread, (long)tmpbuf.c_firstrec);
422 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
423 if (!wantnext) {
424 tpblksread = tmpbuf.c_firstrec;
425 for (i = tmpbuf.c_count; i > 0; i--)
426 readtape(buf);
427 } else if (tmpbuf.c_firstrec > 0 &&
428 tmpbuf.c_firstrec < tpblksread - 1) {
429 /*
430 * -1 since we've read the volume header
431 */
432 i = tpblksread - tmpbuf.c_firstrec - 1;
433 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
434 (long)i, i > 1 ? "s" : "");
435 while (--i >= 0)
436 readtape(buf);
437 }
438 }
439 if (curfile.action == USING) {
440 if (volno == 1)
441 panic("active file into volume 1\n");
442 return;
443 }
444 /*
445 * Skip up to the beginning of the next record
446 */
447 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
448 for (i = tmpbuf.c_count; i > 0; i--)
449 readtape(buf);
450 (void) gethead(&spcl);
451 findinode(&spcl);
452 if (gettingfile) {
453 gettingfile = 0;
454 siglongjmp(restart, 1);
455 }
456 }
457
458 /*
459 * Handle unexpected EOF.
460 */
461 static void
462 terminateinput(void)
463 {
464
465 if (gettingfile && curfile.action == USING) {
466 printf("Warning: %s %s\n",
467 "End-of-input encountered while extracting", curfile.name);
468 }
469 curfile.name = "<name unknown>";
470 curfile.action = UNKNOWN;
471 curfile.dip = NULL;
472 curfile.ino = maxino;
473 if (gettingfile) {
474 gettingfile = 0;
475 siglongjmp(restart, 1);
476 }
477 }
478
479 /*
480 * handle multiple dumps per tape by skipping forward to the
481 * appropriate one.
482 */
483 static void
484 setdumpnum(void)
485 {
486 struct mtop tcom;
487
488 if (dumpnum == 1 || volno != 1)
489 return;
490 if (pipein)
491 errx(1, "Cannot have multiple dumps on pipe input");
492 tcom.mt_op = MTFSF;
493 tcom.mt_count = dumpnum - 1;
494 #ifdef RRESTORE
495 if (host)
496 rmtioctl(MTFSF, dumpnum - 1);
497 else
498 #endif
499 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
500 warn("ioctl MTFSF");
501 }
502
503 void
504 printdumpinfo(void)
505 {
506 #ifdef __linux__
507 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
508 fprintf(stdout, "Dumped from: %s",
509 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
510 #else
511 fprintf(stdout, "Dump date: %s", ctime(&spcl.c_date));
512 fprintf(stdout, "Dumped from: %s",
513 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
514 #endif
515 if (spcl.c_host[0] == '\0')
516 return;
517 fprintf(stderr, "Level %d dump of %s on %s:%s\n",
518 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
519 fprintf(stderr, "Label: %s\n", spcl.c_label);
520 }
521
522 int
523 extractfile(char *name)
524 {
525 unsigned int flags;
526 mode_t mode;
527 struct timeval timep[2];
528 struct entry *ep;
529
530 curfile.name = name;
531 curfile.action = USING;
532 #ifdef __linux__
533 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
534 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
535 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
536 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
537 #else /* __linux__ */
538 timep[0].tv_sec = curfile.dip->di_atime;
539 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
540 timep[1].tv_sec = curfile.dip->di_mtime;
541 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
542 #endif /* __linux__ */
543 mode = curfile.dip->di_mode;
544 flags = curfile.dip->di_flags;
545 switch (mode & IFMT) {
546
547 default:
548 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
549 skipfile();
550 return (FAIL);
551
552 case IFSOCK:
553 Vprintf(stdout, "skipped socket %s\n", name);
554 skipfile();
555 return (GOOD);
556
557 case IFDIR:
558 if (mflag) {
559 ep = lookupname(name);
560 if (ep == NULL || ep->e_flags & EXTRACT)
561 panic("unextracted directory %s\n", name);
562 skipfile();
563 return (GOOD);
564 }
565 Vprintf(stdout, "extract file %s\n", name);
566 return (genliteraldir(name, curfile.ino));
567
568 case IFLNK:
569 {
570 #ifdef HAVE_LCHOWN
571 uid_t luid = curfile.dip->di_uid;
572 gid_t lgid = curfile.dip->di_gid;
573 #endif
574 lnkbuf[0] = '\0';
575 pathlen = 0;
576 getfile(xtrlnkfile, xtrlnkskip);
577 if (pathlen == 0) {
578 Vprintf(stdout,
579 "%s: zero length symbolic link (ignored)\n", name);
580 return (GOOD);
581 }
582 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
583 return (FAIL);
584 #ifdef HAVE_LCHOWN
585 (void) lchown(name, luid, lgid);
586 #endif
587 return (GOOD);
588 }
589
590 case IFIFO:
591 Vprintf(stdout, "extract fifo %s\n", name);
592 if (Nflag) {
593 skipfile();
594 return (GOOD);
595 }
596 if (uflag && !Nflag)
597 (void)unlink(name);
598 if (mkfifo(name, mode) < 0) {
599 warn("%s: cannot create fifo", name);
600 skipfile();
601 return (FAIL);
602 }
603 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
604 (void) chmod(name, mode);
605 if (flags)
606 #ifdef __linux__
607 (void) fsetflags(name, flags);
608 #else
609 (void) chflags(name, flags);
610 #endif
611 skipfile();
612 utimes(name, timep);
613 return (GOOD);
614
615 case IFCHR:
616 case IFBLK:
617 Vprintf(stdout, "extract special file %s\n", name);
618 if (Nflag) {
619 skipfile();
620 return (GOOD);
621 }
622 if (uflag)
623 (void)unlink(name);
624 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
625 warn("%s: cannot create special file", name);
626 skipfile();
627 return (FAIL);
628 }
629 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
630 (void) chmod(name, mode);
631 if (flags)
632 #ifdef __linux__
633 {
634 warn("%s: fsetflags called on a special file", name);
635 (void) fsetflags(name, flags);
636 }
637 #else
638 (void) chflags(name, flags);
639 #endif
640 skipfile();
641 utimes(name, timep);
642 return (GOOD);
643
644 case IFREG:
645 Vprintf(stdout, "extract file %s\n", name);
646 if (Nflag) {
647 skipfile();
648 return (GOOD);
649 }
650 if (uflag)
651 (void)unlink(name);
652 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
653 0666)) < 0) {
654 warn("%s: cannot create file", name);
655 skipfile();
656 return (FAIL);
657 }
658 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
659 (void) fchmod(ofile, mode);
660 if (flags)
661 #ifdef __linux__
662 (void) setflags(ofile, flags);
663 #else
664 (void) fchflags(ofile, flags);
665 #endif
666 getfile(xtrfile, xtrskip);
667 (void) close(ofile);
668 utimes(name, timep);
669 return (GOOD);
670 }
671 /* NOTREACHED */
672 }
673
674 /*
675 * skip over bit maps on the tape
676 */
677 void
678 skipmaps(void)
679 {
680
681 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
682 skipfile();
683 }
684
685 /*
686 * skip over a file on the tape
687 */
688 void
689 skipfile(void)
690 {
691
692 curfile.action = SKIP;
693 getfile(xtrnull, xtrnull);
694 }
695
696 /*
697 * Extract a file from the tape.
698 * When an allocated block is found it is passed to the fill function;
699 * when an unallocated block (hole) is found, a zeroed buffer is passed
700 * to the skip function.
701 */
702 void
703 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
704 {
705 register int i;
706 volatile int curblk = 0;
707 volatile quad_t size = spcl.c_dinode.di_size;
708 volatile int last_write_was_hole = 0;
709 quad_t origsize = size;
710 static char clearedbuf[MAXBSIZE];
711 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
712 char junk[TP_BSIZE];
713
714 if (spcl.c_type == TS_END)
715 panic("ran off end of tape\n");
716 if (spcl.c_magic != NFS_MAGIC)
717 panic("not at beginning of a file\n");
718 if (!gettingfile && setjmp(restart) != 0)
719 return;
720 gettingfile++;
721 loop:
722 for (i = 0; i < spcl.c_count; i++) {
723 if (readmapflag || spcl.c_addr[i]) {
724 readtape(&buf[curblk++][0]);
725 if (curblk == fssize / TP_BSIZE) {
726 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
727 fssize : (curblk - 1) * TP_BSIZE + size));
728 curblk = 0;
729 last_write_was_hole = 0;
730 }
731 } else {
732 if (curblk > 0) {
733 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
734 curblk * TP_BSIZE :
735 (curblk - 1) * TP_BSIZE + size));
736 curblk = 0;
737 }
738 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
739 TP_BSIZE : size));
740 last_write_was_hole = 1;
741 }
742 if ((size -= TP_BSIZE) <= 0) {
743 for (i++; i < spcl.c_count; i++)
744 if (readmapflag || spcl.c_addr[i])
745 readtape(junk);
746 break;
747 }
748 }
749 if (gethead(&spcl) == GOOD && size > 0) {
750 if (spcl.c_type == TS_ADDR)
751 goto loop;
752 Dprintf(stdout,
753 "Missing address (header) block for %s at %ld blocks\n",
754 curfile.name, (long)blksread);
755 }
756 if (curblk > 0) {
757 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
758 last_write_was_hole = 0;
759 }
760 if (last_write_was_hole) {
761 ftruncate(ofile, origsize);
762 }
763 findinode(&spcl);
764 gettingfile = 0;
765 }
766
767 /*
768 * Write out the next block of a file.
769 */
770 static void
771 xtrfile(char *buf, size_t size)
772 {
773
774 if (Nflag)
775 return;
776 if (write(ofile, buf, (int) size) == -1)
777 err(1, "write error extracting inode %lu, name %s\nwrite",
778 (unsigned long)curfile.ino, curfile.name);
779 }
780
781 /*
782 * Skip over a hole in a file.
783 */
784 /* ARGSUSED */
785 static void
786 xtrskip(char *buf, size_t size)
787 {
788
789 if (lseek(ofile, (off_t)size, SEEK_CUR) == -1)
790 err(1, "seek error extracting inode %lu, name %s\nlseek",
791 (unsigned long)curfile.ino, curfile.name);
792 }
793
794 /*
795 * Collect the next block of a symbolic link.
796 */
797 static void
798 xtrlnkfile(char *buf, size_t size)
799 {
800
801 pathlen += size;
802 if (pathlen > MAXPATHLEN)
803 errx(1, "symbolic link name: %s->%s%s; too long %d",
804 curfile.name, lnkbuf, buf, pathlen);
805 (void) strcat(lnkbuf, buf);
806 }
807
808 /*
809 * Skip over a hole in a symbolic link (should never happen).
810 */
811 /* ARGSUSED */
812 static void
813 xtrlnkskip(char *buf, size_t size)
814 {
815
816 errx(1, "unallocated block in symbolic link %s", curfile.name);
817 }
818
819 /*
820 * Collect the next block of a bit map.
821 */
822 static void
823 xtrmap(char *buf, size_t size)
824 {
825
826 memmove(map, buf, size);
827 map += size;
828 }
829
830 /*
831 * Skip over a hole in a bit map (should never happen).
832 */
833 /* ARGSUSED */
834 static void
835 xtrmapskip(char *buf, size_t size)
836 {
837
838 panic("hole in map\n");
839 map += size;
840 }
841
842 /*
843 * Noop, when an extraction function is not needed.
844 */
845 /* ARGSUSED */
846 void
847 xtrnull(char *buf, size_t size)
848 {
849
850 return;
851 }
852
853 static int
854 do_cmpfiles(int fd_tape, int fd_disk, long size)
855 {
856 static char buf_tape[BUFSIZ];
857 static char buf_disk[BUFSIZ];
858 ssize_t n_tape;
859 ssize_t n_disk;
860
861 while (size > 0) {
862 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
863 close(fd_tape), close(fd_disk);
864 panic("do_cmpfiles: unexpected EOF[1]");
865 }
866 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
867 close(fd_tape), close(fd_disk);
868 panic("do_cmpfiles: unexpected EOF[2]");
869 }
870 if (n_tape != n_disk) {
871 close(fd_tape), close(fd_disk);
872 panic("do_cmpfiles: sizes different!");
873 }
874 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
875 size -= n_tape;
876 }
877 return (0);
878 }
879
880 /* for debugging compare problems */
881 #undef COMPARE_FAIL_KEEP_FILE
882
883 static
884 #ifdef COMPARE_FAIL_KEEP_FILE
885 /* return true if tapefile should be unlinked after compare */
886 int
887 #else
888 void
889 #endif
890 cmpfiles(char *tapefile, char *diskfile, struct stat *sbuf_disk)
891 {
892 struct stat sbuf_tape;
893 int fd_tape, fd_disk;
894
895 if (stat(tapefile, &sbuf_tape) != 0) {
896 panic("Can't lstat tmp file %s: %s\n", tapefile,
897 strerror(errno));
898 }
899
900 if (sbuf_disk->st_size != sbuf_tape.st_size) {
901 fprintf(stderr,
902 "%s: size changed from %ld to %ld.\n",
903 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
904 #ifdef COMPARE_FAIL_KEEP_FILE
905 return (0);
906 #else
907 return;
908 #endif
909 }
910
911 if ((fd_tape = open(tapefile, O_RDONLY)) < 0) {
912 panic("Can't open %s: %s\n", tapefile, strerror(errno));
913 }
914 if ((fd_disk = open(diskfile, O_RDONLY)) < 0) {
915 close(fd_tape);
916 panic("Can't open %s: %s\n", diskfile, strerror(errno));
917 }
918
919 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
920 fprintf(stderr, "%s: tape and disk copies are different\n",
921 diskfile);
922 close(fd_tape);
923 close(fd_disk);
924 #ifdef COMPARE_FAIL_KEEP_FILE
925 /* rename the file to live in /tmp */
926 /* rename `tapefile' to /tmp/<basename of diskfile> */
927 {
928 char *p = strrchr(diskfile, '/');
929 char newname[MAXPATHLEN];
930 if (!p) {
931 panic("can't find / in %s\n", diskfile);
932 }
933 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
934 if (rename(tapefile, newname)) {
935 panic("rename from %s to %s failed: %s\n",
936 tapefile, newname,
937 strerror(errno));
938 } else {
939 fprintf(stderr, "*** %s saved to %s\n",
940 tapefile, newname);
941 }
942 }
943
944 /* don't unlink the file (it's not there anymore */
945 /* anyway) */
946 return (0);
947 #else
948 return;
949 #endif
950 }
951 close(fd_tape);
952 close(fd_disk);
953 #ifdef COMPARE_FAIL_KEEP_FILE
954 return (1);
955 #endif
956 }
957
958 static char tmpfilename[MAXPATHLEN];
959
960 void
961 comparefile(char *name)
962 {
963 static char *tmpfile = NULL;
964 int mode;
965 struct stat sb, stemp;
966 int r;
967
968 if ((r = lstat(name, &sb)) != 0) {
969 warn("%s: does not exist (%d)", name, r);
970 skipfile();
971 return;
972 }
973
974 curfile.name = name;
975 curfile.action = USING;
976 mode = curfile.dip->di_mode;
977
978 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
979 (long)sb.st_size, mode);
980
981 if (sb.st_mode != mode) {
982 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
983 name, mode & 07777, sb.st_mode & 07777);
984 }
985 switch (mode & IFMT) {
986 default:
987 skipfile();
988 return;
989
990 case IFSOCK:
991 skipfile();
992 return;
993
994 case IFDIR:
995 skipfile();
996 return;
997
998 case IFLNK: {
999 char lbuf[MAXPATHLEN + 1];
1000 int lsize;
1001
1002 if (!(sb.st_mode & S_IFLNK)) {
1003 fprintf(stderr, "%s: is no longer a symbolic link\n",
1004 name);
1005 return;
1006 }
1007 lnkbuf[0] = '\0';
1008 pathlen = 0;
1009 getfile(xtrlnkfile, xtrlnkskip);
1010 if (pathlen == 0) {
1011 fprintf(stderr,
1012 "%s: zero length symbolic link (ignored)\n",
1013 name);
1014 return;
1015 }
1016 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1017 panic("readlink of %s failed: %s", name,
1018 strerror(errno));
1019 }
1020 lbuf[lsize] = 0;
1021 if (strcmp(lbuf, lnkbuf) != 0) {
1022 fprintf(stderr,
1023 "%s: symbolic link changed from %s to %s.\n",
1024 name, lnkbuf, lbuf);
1025 return;
1026 }
1027 return;
1028 }
1029
1030 case IFCHR:
1031 case IFBLK:
1032 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1033 fprintf(stderr, "%s: no longer a special file\n",
1034 name);
1035 skipfile();
1036 return;
1037 }
1038
1039 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1040 fprintf(stderr,
1041 "%s: device changed from %d,%d to %d,%d.\n",
1042 name,
1043 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1044 (int)curfile.dip->di_rdev & 0xff,
1045 ((int)sb.st_rdev >> 8) & 0xff,
1046 (int)sb.st_rdev & 0xff);
1047 }
1048 skipfile();
1049 return;
1050
1051 case IFREG:
1052 if (tmpfile == NULL) {
1053 /* argument to mktemp() must not be in RO space: */
1054 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1055 tmpfile = mktemp(&tmpfilename[0]);
1056 }
1057 if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1058 panic("cannot delete tmp file %s: %s\n",
1059 tmpfile, strerror(errno));
1060 }
1061 if ((ofile = creat(tmpfile, 0600)) < 0) {
1062 panic("cannot create file temp file %s: %s\n",
1063 name, strerror(errno));
1064 }
1065 getfile(xtrfile, xtrskip);
1066 (void) close(ofile);
1067 #ifdef COMPARE_FAIL_KEEP_FILE
1068 if (cmpfiles(tmpfile, name, &sb))
1069 unlink(tmpfile);
1070 #else
1071 cmpfiles(tmpfile, name, &sb);
1072 unlink(tmpfile);
1073 #endif
1074 return;
1075 }
1076 /* NOTREACHED */
1077 }
1078
1079 /*
1080 * Read TP_BSIZE blocks from the input.
1081 * Handle read errors, and end of media.
1082 */
1083 static void
1084 readtape(char *buf)
1085 {
1086 ssize_t rd, newvol, i;
1087 int cnt, seek_failed;
1088
1089 if (blkcnt < numtrec) {
1090 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1091 blksread++;
1092 tpblksread++;
1093 return;
1094 }
1095 for (i = 0; i < ntrec; i++)
1096 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1097 if (numtrec == 0)
1098 numtrec = ntrec;
1099 cnt = ntrec * TP_BSIZE;
1100 rd = 0;
1101 getmore:
1102 #ifdef RRESTORE
1103 if (host)
1104 i = rmtread(&tapebuf[rd], cnt);
1105 else
1106 #endif
1107 i = read(mt, &tapebuf[rd], cnt);
1108 /*
1109 * Check for mid-tape short read error.
1110 * If found, skip rest of buffer and start with the next.
1111 */
1112 if (!pipein && numtrec < ntrec && i > 0) {
1113 Dprintf(stdout, "mid-media short read error.\n");
1114 numtrec = ntrec;
1115 }
1116 /*
1117 * Handle partial block read.
1118 */
1119 if (pipein && i == 0 && rd > 0)
1120 i = rd;
1121 else if (i > 0 && i != ntrec * TP_BSIZE) {
1122 if (pipein) {
1123 rd += i;
1124 cnt -= i;
1125 if (cnt > 0)
1126 goto getmore;
1127 i = rd;
1128 } else {
1129 /*
1130 * Short read. Process the blocks read.
1131 */
1132 if (i % TP_BSIZE != 0)
1133 Vprintf(stdout,
1134 "partial block read: %ld should be %ld\n",
1135 (long)i, ntrec * TP_BSIZE);
1136 numtrec = i / TP_BSIZE;
1137 }
1138 }
1139 /*
1140 * Handle read error.
1141 */
1142 if (i < 0) {
1143 fprintf(stderr, "Tape read error while ");
1144 switch (curfile.action) {
1145 default:
1146 fprintf(stderr, "trying to set up tape\n");
1147 break;
1148 case UNKNOWN:
1149 fprintf(stderr, "trying to resynchronize\n");
1150 break;
1151 case USING:
1152 fprintf(stderr, "restoring %s\n", curfile.name);
1153 break;
1154 case SKIP:
1155 fprintf(stderr, "skipping over inode %lu\n",
1156 (unsigned long)curfile.ino);
1157 break;
1158 }
1159 if (!yflag && !reply("continue"))
1160 exit(1);
1161 i = ntrec * TP_BSIZE;
1162 memset(tapebuf, 0, (size_t)i);
1163 #ifdef RRESTORE
1164 if (host)
1165 seek_failed = (rmtseek(i, 1) < 0);
1166 else
1167 #endif
1168 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1169
1170 if (seek_failed)
1171 err(1, "continuation failed");
1172 }
1173 /*
1174 * Handle end of tape.
1175 */
1176 if (i == 0) {
1177 Vprintf(stdout, "End-of-tape encountered\n");
1178 if (!pipein) {
1179 newvol = volno + 1;
1180 volno = 0;
1181 numtrec = 0;
1182 getvol(newvol);
1183 readtape(buf);
1184 return;
1185 }
1186 if (rd % TP_BSIZE != 0)
1187 panic("partial block read: %d should be %d\n",
1188 rd, ntrec * TP_BSIZE);
1189 terminateinput();
1190 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1191 }
1192 blkcnt = 0;
1193 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1194 blksread++;
1195 tpblksread++;
1196 }
1197
1198 static void
1199 findtapeblksize(void)
1200 {
1201 register long i;
1202
1203 for (i = 0; i < ntrec; i++)
1204 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1205 blkcnt = 0;
1206 #ifdef RRESTORE
1207 if (host)
1208 i = rmtread(tapebuf, (size_t)(ntrec * TP_BSIZE));
1209 else
1210 #endif
1211 i = read(mt, tapebuf, (size_t)(ntrec * TP_BSIZE));
1212
1213 if (i <= 0)
1214 err(1, "tape read error");
1215 if (i % TP_BSIZE != 0)
1216 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1217 (long)i, TP_BSIZE);
1218 ntrec = i / TP_BSIZE;
1219 numtrec = ntrec;
1220 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1221 }
1222
1223 void
1224 closemt(void)
1225 {
1226
1227 if (mt < 0)
1228 return;
1229 #ifdef RRESTORE
1230 if (host)
1231 rmtclose();
1232 else
1233 #endif
1234 (void) close(mt);
1235 }
1236
1237 /*
1238 * Read the next block from the tape.
1239 * Check to see if it is one of several vintage headers.
1240 * If it is an old style header, convert it to a new style header.
1241 * If it is not any valid header, return an error.
1242 */
1243 static int
1244 gethead(struct s_spcl *buf)
1245 {
1246 int32_t i;
1247 union {
1248 quad_t qval;
1249 int32_t val[2];
1250 } qcvt;
1251 union u_ospcl {
1252 char dummy[TP_BSIZE];
1253 struct s_ospcl {
1254 int32_t c_type;
1255 int32_t c_date;
1256 int32_t c_ddate;
1257 int32_t c_volume;
1258 int32_t c_tapea;
1259 u_int16_t c_inumber;
1260 int32_t c_magic;
1261 int32_t c_checksum;
1262 struct odinode {
1263 u_int16_t odi_mode;
1264 u_int16_t odi_nlink;
1265 u_int16_t odi_uid;
1266 u_int16_t odi_gid;
1267 int32_t odi_size;
1268 int32_t odi_rdev;
1269 char odi_addr[36];
1270 int32_t odi_atime;
1271 int32_t odi_mtime;
1272 int32_t odi_ctime;
1273 } c_dinode;
1274 int32_t c_count;
1275 char c_addr[256];
1276 } s_ospcl;
1277 } u_ospcl;
1278
1279 if (!cvtflag) {
1280 readtape((char *)buf);
1281 if (buf->c_magic != NFS_MAGIC) {
1282 if (swabi(buf->c_magic) != NFS_MAGIC)
1283 return (FAIL);
1284 if (!Bcvt) {
1285 Vprintf(stdout, "Note: Doing Byte swapping\n");
1286 Bcvt = 1;
1287 }
1288 }
1289 if (checksum((int *)buf) == FAIL)
1290 return (FAIL);
1291 if (Bcvt)
1292 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1293 goto good;
1294 }
1295 readtape((char *)(&u_ospcl.s_ospcl));
1296 memset((char *)buf, 0, (long)TP_BSIZE);
1297 buf->c_type = u_ospcl.s_ospcl.c_type;
1298 buf->c_date = u_ospcl.s_ospcl.c_date;
1299 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1300 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1301 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1302 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1303 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1304 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1305 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1306 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1307 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1308 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1309 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1310 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1311 #ifdef __linux__
1312 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1313 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1314 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1315 #else /* __linux__ */
1316 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1317 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1318 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1319 #endif /* __linux__ */
1320 buf->c_count = u_ospcl.s_ospcl.c_count;
1321 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1322 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1323 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1324 return(FAIL);
1325 buf->c_magic = NFS_MAGIC;
1326
1327 good:
1328 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1329 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1330 qcvt.qval = buf->c_dinode.di_size;
1331 if (qcvt.val[0] || qcvt.val[1]) {
1332 printf("Note: Doing Quad swapping\n");
1333 Qcvt = 1;
1334 }
1335 }
1336 if (Qcvt) {
1337 qcvt.qval = buf->c_dinode.di_size;
1338 i = qcvt.val[1];
1339 qcvt.val[1] = qcvt.val[0];
1340 qcvt.val[0] = i;
1341 buf->c_dinode.di_size = qcvt.qval;
1342 }
1343 readmapflag = 0;
1344
1345 switch (buf->c_type) {
1346
1347 case TS_CLRI:
1348 case TS_BITS:
1349 /*
1350 * Have to patch up missing information in bit map headers
1351 */
1352 buf->c_inumber = 0;
1353 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1354 if (buf->c_count > TP_NINDIR)
1355 readmapflag = 1;
1356 else
1357 for (i = 0; i < buf->c_count; i++)
1358 buf->c_addr[i]++;
1359 break;
1360
1361 case TS_TAPE:
1362 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1363 oldinofmt = 1;
1364 /* fall through */
1365 case TS_END:
1366 buf->c_inumber = 0;
1367 break;
1368
1369 case TS_INODE:
1370 case TS_ADDR:
1371 break;
1372
1373 default:
1374 panic("gethead: unknown inode type %d\n", buf->c_type);
1375 break;
1376 }
1377 /*
1378 * If we are restoring a filesystem with old format inodes,
1379 * copy the uid/gid to the new location.
1380 */
1381 if (oldinofmt) {
1382 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1383 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1384 }
1385 if (dflag)
1386 accthdr(buf);
1387 return(GOOD);
1388 }
1389
1390 /*
1391 * Check that a header is where it belongs and predict the next header
1392 */
1393 static void
1394 accthdr(struct s_spcl *header)
1395 {
1396 static ino_t previno = 0x7fffffff;
1397 static int prevtype;
1398 static long predict;
1399 long blks, i;
1400
1401 if (header->c_type == TS_TAPE) {
1402 fprintf(stderr, "Volume header (%s inode format) ",
1403 oldinofmt ? "old" : "new");
1404 if (header->c_firstrec)
1405 fprintf(stderr, "begins with record %d",
1406 header->c_firstrec);
1407 fprintf(stderr, "\n");
1408 previno = 0x7fffffff;
1409 return;
1410 }
1411 if (previno == 0x7fffffff)
1412 goto newcalc;
1413 switch (prevtype) {
1414 case TS_BITS:
1415 fprintf(stderr, "Dumped inodes map header");
1416 break;
1417 case TS_CLRI:
1418 fprintf(stderr, "Used inodes map header");
1419 break;
1420 case TS_INODE:
1421 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
1422 break;
1423 case TS_ADDR:
1424 fprintf(stderr, "File continuation header, ino %ld", previno);
1425 break;
1426 case TS_END:
1427 fprintf(stderr, "End of tape header");
1428 break;
1429 }
1430 if (predict != blksread - 1)
1431 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1432 predict, blksread - 1);
1433 fprintf(stderr, "\n");
1434 newcalc:
1435 blks = 0;
1436 if (header->c_type != TS_END)
1437 for (i = 0; i < header->c_count; i++)
1438 if (readmapflag || header->c_addr[i] != 0)
1439 blks++;
1440 predict = blks;
1441 blksread = 0;
1442 prevtype = header->c_type;
1443 previno = header->c_inumber;
1444 }
1445
1446 /*
1447 * Find an inode header.
1448 * Complain if had to skip, and complain is set.
1449 */
1450 static void
1451 findinode(struct s_spcl *header)
1452 {
1453 static long skipcnt = 0;
1454 long i;
1455 char buf[TP_BSIZE];
1456
1457 curfile.name = "<name unknown>";
1458 curfile.action = UNKNOWN;
1459 curfile.dip = NULL;
1460 curfile.ino = 0;
1461 do {
1462 if (header->c_magic != NFS_MAGIC) {
1463 skipcnt++;
1464 while (gethead(header) == FAIL ||
1465 header->c_date != dumpdate)
1466 skipcnt++;
1467 }
1468 switch (header->c_type) {
1469
1470 case TS_ADDR:
1471 /*
1472 * Skip up to the beginning of the next record
1473 */
1474 for (i = 0; i < header->c_count; i++)
1475 if (header->c_addr[i])
1476 readtape(buf);
1477 while (gethead(header) == FAIL ||
1478 header->c_date != dumpdate)
1479 skipcnt++;
1480 break;
1481
1482 case TS_INODE:
1483 curfile.dip = &header->c_dinode;
1484 curfile.ino = header->c_inumber;
1485 break;
1486
1487 case TS_END:
1488 curfile.ino = maxino;
1489 break;
1490
1491 case TS_CLRI:
1492 curfile.name = "<file removal list>";
1493 break;
1494
1495 case TS_BITS:
1496 curfile.name = "<file dump list>";
1497 break;
1498
1499 case TS_TAPE:
1500 panic("unexpected tape header\n");
1501 /* NOTREACHED */
1502
1503 default:
1504 panic("unknown tape header type %d\n", spcl.c_type);
1505 /* NOTREACHED */
1506
1507 }
1508 } while (header->c_type == TS_ADDR);
1509 if (skipcnt > 0)
1510 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1511 skipcnt);
1512 skipcnt = 0;
1513 }
1514
1515 static int
1516 checksum(int *buf)
1517 {
1518 register int i, j;
1519
1520 j = sizeof(union u_spcl) / sizeof(int);
1521 i = 0;
1522 if(!Bcvt) {
1523 do
1524 i += *buf++;
1525 while (--j);
1526 } else {
1527 /* What happens if we want to read restore tapes
1528 for a 16bit int machine??? */
1529 do
1530 i += swabi(*buf++);
1531 while (--j);
1532 }
1533
1534 if (i != CHECKSUM) {
1535 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
1536 (unsigned long)curfile.ino, curfile.name);
1537 return(FAIL);
1538 }
1539 return(GOOD);
1540 }
1541
1542 #ifdef RRESTORE
1543 #ifdef __STDC__
1544 #include <stdarg.h>
1545 #else
1546 #include <varargs.h>
1547 #endif
1548
1549 void
1550 #ifdef __STDC__
1551 msg(const char *fmt, ...)
1552 #else
1553 msg(fmt, va_alist)
1554 char *fmt;
1555 va_dcl
1556 #endif
1557 {
1558 va_list ap;
1559 #ifdef __STDC__
1560 va_start(ap, fmt);
1561 #else
1562 va_start(ap);
1563 #endif
1564 (void)vfprintf(stderr, fmt, ap);
1565 va_end(ap);
1566 }
1567 #endif /* RRESTORE */
1568
1569 static u_char *
1570 swab16(u_char *sp, int n)
1571 {
1572 char c;
1573
1574 while (--n >= 0) {
1575 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1576 sp += 2;
1577 }
1578 return (sp);
1579 }
1580
1581 static u_char *
1582 swab32(u_char *sp, int n)
1583 {
1584 char c;
1585
1586 while (--n >= 0) {
1587 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1588 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
1589 sp += 4;
1590 }
1591 return (sp);
1592 }
1593
1594 static u_char *
1595 swab64(u_char *sp, int n)
1596 {
1597 char c;
1598
1599 while (--n >= 0) {
1600 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1601 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1602 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1603 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1604 sp += 8;
1605 }
1606 return (sp);
1607 }
1608
1609 void
1610 swabst(u_char *cp, u_char *sp)
1611 {
1612 int n = 0;
1613
1614 while (*cp) {
1615 switch (*cp) {
1616 case '0': case '1': case '2': case '3': case '4':
1617 case '5': case '6': case '7': case '8': case '9':
1618 n = (n * 10) + (*cp++ - '0');
1619 continue;
1620
1621 case 's': case 'w': case 'h':
1622 if (n == 0)
1623 n = 1;
1624 sp = swab16(sp, n);
1625 break;
1626
1627 case 'i':
1628 if (n == 0)
1629 n = 1;
1630 sp = swab32(sp, n);
1631 break;
1632
1633 case 'l':
1634 if (n == 0)
1635 n = 1;
1636 sp = swab64(sp, n);
1637 break;
1638
1639 default: /* Any other character, like 'b' counts as byte. */
1640 if (n == 0)
1641 n = 1;
1642 sp += n;
1643 break;
1644 }
1645 cp++;
1646 n = 0;
1647 }
1648 }
1649
1650 static u_int
1651 swabi(u_int x)
1652 {
1653 swabst((u_char *)"i", (u_char *)&x);
1654 return (x);
1655 }
1656
1657 static u_long
1658 swabl(u_long x)
1659 {
1660 swabst((u_char *)"l", (u_char *)&x);
1661 return (x);
1662 }