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