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