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