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