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