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