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