]> git.wh0rd.org - dump.git/blob - restore/tape.c
94ceab46fdfa9a706cac851acb3108936dd05fcf
[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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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.57 2002/01/31 10:25:55 stelian Exp $";
50 #endif /* not lint */
51
52 #include <config.h>
53 #include <compatlfs.h>
54 #include <errno.h>
55 #include <compaterr.h>
56 #include <system.h>
57 #include <setjmp.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include <sys/param.h>
64 #include <sys/file.h>
65 #include <sys/mtio.h>
66 #include <sys/stat.h>
67
68 #ifdef __linux__
69 #include <sys/time.h>
70 #include <time.h>
71 #ifdef HAVE_EXT2FS_EXT2_FS_H
72 #include <ext2fs/ext2_fs.h>
73 #else
74 #include <linux/ext2_fs.h>
75 #endif
76 #include <ext2fs/ext2fs.h>
77 #include <bsdcompat.h>
78 #else /* __linux__ */
79 #include <ufs/ufs/dinode.h>
80 #endif /* __linux__ */
81 #include <protocols/dumprestore.h>
82
83 #ifdef HAVE_ZLIB
84 #include <zlib.h>
85 #endif /* HAVE_ZLIB */
86
87 #ifdef HAVE_BZLIB
88 #include <bzlib.h>
89 #endif /* HAVE_BZLIB */
90
91 #include "restore.h"
92 #include "extern.h"
93 #include "pathnames.h"
94
95 #ifdef USE_QFA
96 int noresyncmesg = 0;
97 #endif /* USE_QFA */
98 static long fssize = MAXBSIZE;
99 static int mt = -1;
100 int pipein = 0;
101 static int magtapein = 0; /* input is from magtape */
102 static char magtape[MAXPATHLEN];
103 static char magtapeprefix[MAXPATHLEN];
104 static int blkcnt;
105 static int numtrec;
106 static char *tapebuf; /* input buffer for read */
107 static int bufsize; /* buffer size without prefix */
108 static char *tbufptr = NULL; /* active tape buffer */
109 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
110 static char *comprbuf; /* uncompress work buf */
111 static size_t comprlen; /* size including prefix */
112 #endif
113 static union u_spcl endoftapemark;
114 static long blksread; /* blocks read since last header */
115 static long tpblksread = 0; /* TP_BSIZE blocks read */
116 static long tapesread;
117 static sigjmp_buf restart;
118 static int gettingfile = 0; /* restart has a valid frame */
119 char *host = NULL;
120
121 static int ofile;
122 static char *map;
123 static char lnkbuf[MAXPATHLEN + 1];
124 static int pathlen;
125
126 int oldinofmt; /* old inode format conversion required */
127 int Bcvt; /* Swap Bytes (for CCI or sun) */
128 static int Qcvt; /* Swap quads (for sun) */
129
130 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
131
132 static void accthdr __P((struct s_spcl *));
133 static int checksum __P((int *));
134 static void findinode __P((struct s_spcl *));
135 static void findtapeblksize __P((void));
136 static int gethead __P((struct s_spcl *));
137 static void readtape __P((char *));
138 static void setdumpnum __P((void));
139 static u_int swabi __P((u_int));
140 #if 0
141 static u_long swabl __P((u_long));
142 #endif
143 static u_char *swab64 __P((u_char *, int));
144 static u_char *swab32 __P((u_char *, int));
145 static u_char *swab16 __P((u_char *, int));
146 static void terminateinput __P((void));
147 static void xtrfile __P((char *, size_t));
148 static void xtrlnkfile __P((char *, size_t));
149 static void xtrlnkskip __P((char *, size_t));
150 static void xtrmap __P((char *, size_t));
151 static void xtrmapskip __P((char *, size_t));
152 static void xtrskip __P((char *, size_t));
153 static void setmagtapein __P((void));
154
155 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
156 static void newcomprbuf __P((int));
157 static void (*readtape_func) __P((char *));
158 static void readtape_set __P((char *));
159 static void readtape_uncompr __P((char *));
160 static void readtape_comprfile __P((char *));
161 static void readtape_comprtape __P((char *));
162 static char *decompress_tapebuf __P((struct tapebuf *, int));
163 static void msg_read_error __P((char *));
164 #endif
165 static int read_a_block __P((int, char *, size_t, long *));
166 #define PREFIXSIZE sizeof(struct tapebuf)
167
168 #define COMPARE_ONTHEFLY 1
169
170 #if COMPARE_ONTHEFLY
171 static int ifile; /* input file for compare */
172 static int cmperror; /* compare error */
173 static void xtrcmpfile __P((char *, size_t));
174 static void xtrcmpskip __P((char *, size_t));
175 #endif
176
177 static int readmapflag;
178
179 /*
180 * Set up an input source. This is called from main.c before setup() is.
181 */
182 void
183 setinput(char *source)
184 {
185 FLUSHTAPEBUF();
186 if (bflag)
187 newtapebuf(ntrec);
188 else
189 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
190 terminal = stdin;
191
192 #ifdef RRESTORE
193 if (strchr(source, ':')) {
194 host = source;
195 source = strchr(host, ':');
196 *source++ = '\0';
197 if (rmthost(host) == 0)
198 exit(1);
199 } else
200 #endif
201 if (strcmp(source, "-") == 0) {
202 /*
203 * Since input is coming from a pipe we must establish
204 * our own connection to the terminal.
205 */
206 terminal = fopen(_PATH_TTY, "r");
207 if (terminal == NULL) {
208 warn("cannot open %s", _PATH_TTY);
209 terminal = fopen(_PATH_DEVNULL, "r");
210 if (terminal == NULL)
211 err(1, "cannot open %s", _PATH_DEVNULL);
212 }
213 pipein++;
214 }
215 setuid(getuid()); /* no longer need or want root privileges */
216 if (Mflag) {
217 strncpy(magtapeprefix, source, MAXPATHLEN);
218 magtapeprefix[MAXPATHLEN-1] = '\0';
219 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
220 }
221 else
222 strncpy(magtape, source, MAXPATHLEN);
223 magtape[MAXPATHLEN - 1] = '\0';
224 }
225
226 void
227 newtapebuf(long size)
228 {
229 static int tapebufsize = -1;
230
231 ntrec = size;
232 bufsize = ntrec * TP_BSIZE;
233 if (size <= tapebufsize)
234 return;
235 if (tapebuf != NULL)
236 free(tapebuf);
237 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
238 if (tapebuf == NULL)
239 errx(1, "Cannot allocate space for tape buffer");
240 tapebufsize = size;
241 }
242
243 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
244 static void
245 newcomprbuf(int size)
246 {
247 size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
248 if (buf_size <= comprlen)
249 return;
250 comprlen = buf_size;
251 if (comprbuf != NULL)
252 free(comprbuf);
253 comprbuf = malloc(comprlen);
254 if (comprbuf == NULL)
255 errx(1, "Cannot allocate space for decompress buffer");
256 }
257 #endif /* HAVE_ZLIB || HAVE_BZLIB */
258
259 /*
260 * Verify that the tape drive can be accessed and
261 * that it actually is a dump tape.
262 */
263 void
264 setup(void)
265 {
266 int i, j, *ip, bot_code;
267 struct STAT stbuf;
268 char *temptape;
269
270 Vprintf(stdout, "Verify tape and initialize maps\n");
271 if (Afile == NULL && bot_script) {
272 msg("Launching %s\n", bot_script);
273 bot_code = system_command(bot_script, magtape, 1);
274 if (bot_code != 0 && bot_code != 1) {
275 msg("Restore aborted by the beginning of tape script\n");
276 exit(1);
277 }
278 }
279
280 if (Afile)
281 temptape = Afile;
282 else
283 temptape = magtape;
284
285 #ifdef RRESTORE
286 if (host)
287 mt = rmtopen(temptape, 0);
288 else
289 #endif
290 if (pipein)
291 mt = 0;
292 else
293 mt = OPEN(temptape, O_RDONLY, 0);
294 if (mt < 0)
295 err(1, "%s", magtape);
296 if (!Afile) {
297 volno = 1;
298 setmagtapein();
299 setdumpnum();
300 }
301 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
302 readtape_func = readtape_set;
303 #endif
304 FLUSHTAPEBUF();
305 findtapeblksize();
306 if (gethead(&spcl) == FAIL) {
307 blkcnt--; /* push back this block */
308 blksread--;
309 tpblksread--;
310 cvtflag++;
311 if (gethead(&spcl) == FAIL)
312 errx(1, "Tape is not a dump tape");
313 fprintf(stderr, "Converting to new file system format.\n");
314 }
315
316 if (zflag) {
317 fprintf(stderr, "Dump tape is compressed.\n");
318 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
319 errx(1,"This restore version doesn't support decompression");
320 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
321 }
322 if (pipein) {
323 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
324 endoftapemark.s_spcl.c_type = TS_END;
325 ip = (int *)&endoftapemark;
326 j = sizeof(union u_spcl) / sizeof(int);
327 i = 0;
328 do
329 i += *ip++;
330 while (--j);
331 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
332 }
333 if (vflag || command == 't' || command == 'C')
334 printdumpinfo();
335 if (filesys[0] == '\0') {
336 char *dirptr;
337 strncpy(filesys, spcl.c_filesys, NAMELEN);
338 filesys[NAMELEN - 1] = '\0';
339 dirptr = strstr(filesys, " (dir");
340 if (dirptr != NULL)
341 *dirptr = '\0';
342 }
343 dumptime = spcl.c_ddate;
344 dumpdate = spcl.c_date;
345 if (STAT(".", &stbuf) < 0)
346 err(1, "cannot stat .");
347 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
348 fssize = TP_BSIZE;
349 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
350 fssize = stbuf.st_blksize;
351 if (((fssize - 1) & fssize) != 0)
352 errx(1, "bad block size %ld", fssize);
353 if (spcl.c_volume != 1)
354 errx(1, "Tape is not volume 1 of the dump");
355 if (gethead(&spcl) == FAIL) {
356 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
357 panic("no header after volume mark!\n");
358 }
359 findinode(&spcl);
360 if (spcl.c_type != TS_CLRI)
361 errx(1, "Cannot find file removal list");
362 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
363 Dprintf(stdout, "maxino = %ld\n", (long)maxino);
364 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
365 if (map == NULL)
366 errx(1, "no memory for active inode map");
367 usedinomap = map;
368 curfile.action = USING;
369 getfile(xtrmap, xtrmapskip);
370 findinode(&spcl);
371 if (spcl.c_type != TS_BITS)
372 errx(1, "Cannot find file dump list");
373 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
374 if (map == (char *)NULL)
375 errx(1, "no memory for file dump list");
376 dumpmap = map;
377 curfile.action = USING;
378 getfile(xtrmap, xtrmapskip);
379 /*
380 * If there may be whiteout entries on the tape, pretend that the
381 * whiteout inode exists, so that the whiteout entries can be
382 * extracted.
383 */
384 if (oldinofmt == 0)
385 SETINO(WINO, dumpmap);
386 findinode(&spcl);
387 }
388
389 /*
390 * Prompt user to load a new dump volume.
391 * "Nextvol" is the next suggested volume to use.
392 * This suggested volume is enforced when doing full
393 * or incremental restores, but can be overridden by
394 * the user when only extracting a subset of the files.
395 */
396 void
397 getvol(long nextvol)
398 {
399 long newvol = 0, wantnext = 0, i;
400 long saved_blksread = 0, saved_tpblksread = 0;
401 union u_spcl tmpspcl;
402 # define tmpbuf tmpspcl.s_spcl
403 char buf[TP_BSIZE];
404 int haderror = 0, bot_code = 1;
405
406 if (nextvol == 1) {
407 tapesread = 0;
408 gettingfile = 0;
409 }
410 if (pipein) {
411 if (nextvol != 1)
412 panic("Changing volumes on pipe input?\n");
413 if (volno == 1)
414 return;
415 goto gethdr;
416 }
417 saved_blksread = blksread;
418 saved_tpblksread = tpblksread;
419 again:
420 if (pipein)
421 exit(1); /* pipes do not get a second chance */
422 if (aflag || curfile.action != SKIP) {
423 newvol = nextvol;
424 wantnext = 1;
425 } else {
426 newvol = 0;
427 wantnext = 0;
428 }
429 while (newvol <= 0) {
430 if (tapesread == 0) {
431 fprintf(stderr, "%s%s%s%s%s",
432 "You have not read any tapes yet.\n",
433 "Unless you know which volume your",
434 " file(s) are on you should start\n",
435 "with the last volume and work",
436 " towards the first.\n");
437 } else {
438 fprintf(stderr, "You have read volumes");
439 strcpy(buf, ": ");
440 for (i = 1; i < 32; i++)
441 if (tapesread & (1 << i)) {
442 fprintf(stderr, "%s%ld", buf, (long)i);
443 strcpy(buf, ", ");
444 }
445 fprintf(stderr, "\n");
446 }
447 do {
448 fprintf(stderr, "Specify next volume # (none if no more volumes): ");
449 (void) fflush(stderr);
450 (void) fgets(buf, TP_BSIZE, terminal);
451 } while (!feof(terminal) && buf[0] == '\n');
452 if (feof(terminal))
453 exit(1);
454 if (!strcmp(buf, "none\n")) {
455 terminateinput();
456 return;
457 }
458 newvol = atoi(buf);
459 if (newvol <= 0) {
460 fprintf(stderr,
461 "Volume numbers are positive numerics\n");
462 }
463 }
464 if (newvol == volno) {
465 tapesread |= 1 << volno;
466 return;
467 }
468 closemt();
469 if (Mflag) {
470 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
471 magtape[MAXPATHLEN - 1] = '\0';
472 }
473 if (bot_script && !haderror) {
474 msg("Launching %s\n", bot_script);
475 bot_code = system_command(bot_script, magtape, newvol);
476 if (bot_code != 0 && bot_code != 1) {
477 msg("Restore aborted by the beginning of tape script\n");
478 exit(1);
479 }
480 }
481 if (haderror || (bot_code && !Mflag)) {
482 haderror = 0;
483 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
484 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
485 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
486 (void) fflush(stderr);
487 (void) fgets(buf, TP_BSIZE, terminal);
488 if (feof(terminal))
489 exit(1);
490 if (!strcmp(buf, "none\n")) {
491 terminateinput();
492 return;
493 }
494 if (buf[0] != '\n') {
495 char *pos;
496 (void) strncpy(magtape, buf, sizeof(magtape));
497 magtape[sizeof(magtape) - 1] = '\0';
498 if ((pos = strchr(magtape, '\n')))
499 magtape[pos - magtape] = '\0';
500 }
501 }
502 #ifdef RRESTORE
503 if (host)
504 mt = rmtopen(magtape, 0);
505 else
506 #endif
507 mt = OPEN(magtape, O_RDONLY, 0);
508
509 if (mt == -1) {
510 fprintf(stderr, "Cannot open %s\n", magtape);
511 volno = -1;
512 haderror = 1;
513 goto again;
514 }
515 gethdr:
516 setmagtapein();
517 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
518 readtape_func = readtape_set;
519 #endif
520 volno = newvol;
521 setdumpnum();
522 FLUSHTAPEBUF();
523 findtapeblksize();
524 if (gethead(&tmpbuf) == FAIL) {
525 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
526 fprintf(stderr, "tape is not dump tape\n");
527 volno = 0;
528 haderror = 1;
529 blksread = saved_blksread;
530 tpblksread = saved_tpblksread;
531 goto again;
532 }
533 if (tmpbuf.c_volume != volno) {
534 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
535 volno = 0;
536 haderror = 1;
537 blksread = saved_blksread;
538 tpblksread = saved_tpblksread;
539 goto again;
540 }
541 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
542 fprintf(stderr, "Wrong dump date\n\tgot: %s",
543 ctime4(&tmpbuf.c_date));
544 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
545 volno = 0;
546 haderror = 1;
547 blksread = saved_blksread;
548 tpblksread = saved_tpblksread;
549 goto again;
550 }
551 tapesread |= 1 << volno;
552 blksread = saved_blksread;
553 tpblksread = saved_tpblksread;
554 /*
555 * If continuing from the previous volume, skip over any
556 * blocks read already at the end of the previous volume.
557 *
558 * If coming to this volume at random, skip to the beginning
559 * of the next record.
560 */
561 if (zflag) {
562 fprintf(stderr, "Dump tape is compressed.\n");
563 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB)
564 errx(1,"This restore version doesn't support decompression");
565 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
566 }
567 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
568 tpblksread, (long)tmpbuf.c_firstrec);
569 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
570 if (!wantnext) {
571 tpblksread = tmpbuf.c_firstrec;
572 for (i = tmpbuf.c_count; i > 0; i--)
573 readtape(buf);
574 } else if (tmpbuf.c_firstrec > 0 &&
575 tmpbuf.c_firstrec < tpblksread - 1) {
576 /*
577 * -1 since we've read the volume header
578 */
579 i = tpblksread - tmpbuf.c_firstrec - 1;
580 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
581 (long)i, i > 1 ? "s" : "");
582 while (--i >= 0)
583 readtape(buf);
584 }
585 }
586 if (curfile.action == USING) {
587 if (volno == 1)
588 panic("active file into volume 1\n");
589 return;
590 }
591 /*
592 * Skip up to the beginning of the next record
593 */
594 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
595 for (i = tmpbuf.c_count; i > 0; i--)
596 readtape(buf);
597 (void) gethead(&spcl);
598 findinode(&spcl);
599 if (gettingfile) {
600 gettingfile = 0;
601 siglongjmp(restart, 1);
602 }
603 }
604
605 /*
606 * Handle unexpected EOF.
607 */
608 static void
609 terminateinput(void)
610 {
611
612 if (gettingfile && curfile.action == USING) {
613 printf("Warning: %s %s\n",
614 "End-of-input encountered while extracting", curfile.name);
615 }
616 curfile.name = "<name unknown>";
617 curfile.action = UNKNOWN;
618 curfile.dip = NULL;
619 curfile.ino = maxino;
620 if (gettingfile) {
621 gettingfile = 0;
622 siglongjmp(restart, 1);
623 }
624 }
625
626 /*
627 * handle multiple dumps per tape by skipping forward to the
628 * appropriate one.
629 */
630 static void
631 setdumpnum(void)
632 {
633 struct mtop tcom;
634
635 if (dumpnum == 1 || volno != 1)
636 return;
637 if (pipein)
638 errx(1, "Cannot have multiple dumps on pipe input");
639 tcom.mt_op = MTFSF;
640 tcom.mt_count = dumpnum - 1;
641 #ifdef RRESTORE
642 if (host)
643 rmtioctl(MTFSF, dumpnum - 1);
644 else
645 #endif
646 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
647 warn("ioctl MTFSF");
648 }
649
650 void
651 printdumpinfo(void)
652 {
653 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
654 fprintf(stdout, "Dumped from: %s",
655 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
656 if (spcl.c_host[0] == '\0')
657 return;
658 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
659 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
660 fprintf(stdout, "Label: %s\n", spcl.c_label);
661 }
662
663 void
664 printvolinfo(void)
665 {
666 int i;
667
668 if (volinfo[1] == ROOTINO) {
669 printf("Starting inode numbers by volume:\n");
670 for (i = 1; i < TP_NINOS && volinfo[i] != 0; ++i)
671 printf("\tVolume %d: %lu\n", i, (unsigned long)volinfo[i]);
672 }
673 }
674
675 int
676 extractfile(char *name)
677 {
678 unsigned int flags;
679 mode_t mode;
680 struct timeval timep[2];
681 struct entry *ep;
682
683 curfile.name = name;
684 curfile.action = USING;
685 #ifdef __linux__
686 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
687 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
688 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
689 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
690 #else /* __linux__ */
691 timep[0].tv_sec = curfile.dip->di_atime;
692 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
693 timep[1].tv_sec = curfile.dip->di_mtime;
694 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
695 #endif /* __linux__ */
696 mode = curfile.dip->di_mode;
697 flags = curfile.dip->di_flags;
698 switch (mode & IFMT) {
699
700 default:
701 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
702 skipfile();
703 return (FAIL);
704
705 case IFSOCK:
706 Vprintf(stdout, "skipped socket %s\n", name);
707 skipfile();
708 return (GOOD);
709
710 case IFDIR:
711 if (mflag) {
712 ep = lookupname(name);
713 if (ep == NULL || ep->e_flags & EXTRACT)
714 panic("unextracted directory %s\n", name);
715 skipfile();
716 return (GOOD);
717 }
718 Vprintf(stdout, "extract file %s\n", name);
719 return (genliteraldir(name, curfile.ino));
720
721 case IFLNK:
722 {
723 #ifdef HAVE_LCHOWN
724 uid_t luid = curfile.dip->di_uid;
725 gid_t lgid = curfile.dip->di_gid;
726 #endif
727 lnkbuf[0] = '\0';
728 pathlen = 0;
729 getfile(xtrlnkfile, xtrlnkskip);
730 if (pathlen == 0) {
731 Vprintf(stdout,
732 "%s: zero length symbolic link (ignored)\n", name);
733 return (GOOD);
734 }
735 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
736 return (FAIL);
737 #ifdef HAVE_LCHOWN
738 (void) lchown(name, luid, lgid);
739 #endif
740 return (GOOD);
741 }
742
743 case IFIFO:
744 Vprintf(stdout, "extract fifo %s\n", name);
745 if (Nflag) {
746 skipfile();
747 return (GOOD);
748 }
749 if (uflag && !Nflag)
750 (void)unlink(name);
751 if (mkfifo(name, mode) < 0) {
752 warn("%s: cannot create fifo", name);
753 skipfile();
754 return (FAIL);
755 }
756 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
757 (void) chmod(name, mode);
758 if (flags)
759 #ifdef __linux__
760 (void) fsetflags(name, flags);
761 #else
762 (void) chflags(name, flags);
763 #endif
764 skipfile();
765 utimes(name, timep);
766 return (GOOD);
767
768 case IFCHR:
769 case IFBLK:
770 Vprintf(stdout, "extract special file %s\n", name);
771 if (Nflag) {
772 skipfile();
773 return (GOOD);
774 }
775 if (uflag)
776 (void)unlink(name);
777 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
778 warn("%s: cannot create special file", name);
779 skipfile();
780 return (FAIL);
781 }
782 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
783 (void) chmod(name, mode);
784 if (flags)
785 #ifdef __linux__
786 {
787 warn("%s: fsetflags called on a special file", name);
788 (void) fsetflags(name, flags);
789 }
790 #else
791 (void) chflags(name, flags);
792 #endif
793 skipfile();
794 utimes(name, timep);
795 return (GOOD);
796
797 case IFREG:
798 Vprintf(stdout, "extract file %s\n", name);
799 if (Nflag) {
800 skipfile();
801 return (GOOD);
802 }
803 if (uflag)
804 (void)unlink(name);
805 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
806 0666)) < 0) {
807 warn("%s: cannot create file", name);
808 skipfile();
809 return (FAIL);
810 }
811 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
812 (void) fchmod(ofile, mode);
813 if (flags)
814 #ifdef __linux__
815 (void) setflags(ofile, flags);
816 #else
817 (void) fchflags(ofile, flags);
818 #endif
819 getfile(xtrfile, xtrskip);
820 (void) close(ofile);
821 utimes(name, timep);
822 return (GOOD);
823 }
824 /* NOTREACHED */
825 }
826
827 /*
828 * skip over bit maps on the tape
829 */
830 void
831 skipmaps(void)
832 {
833
834 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
835 skipfile();
836 }
837
838 /*
839 * skip over a file on the tape
840 */
841 void
842 skipfile(void)
843 {
844
845 curfile.action = SKIP;
846 getfile(xtrnull, xtrnull);
847 }
848
849 /*
850 * Extract a file from the tape.
851 * When an allocated block is found it is passed to the fill function;
852 * when an unallocated block (hole) is found, a zeroed buffer is passed
853 * to the skip function.
854 */
855 void
856 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
857 {
858 int i;
859 volatile int curblk = 0;
860 volatile quad_t size = spcl.c_dinode.di_size;
861 volatile int last_write_was_hole = 0;
862 quad_t origsize = size;
863 static char clearedbuf[MAXBSIZE];
864 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
865 char junk[TP_BSIZE];
866
867 if (spcl.c_type == TS_END)
868 panic("ran off end of tape\n");
869 if (spcl.c_magic != NFS_MAGIC)
870 panic("not at beginning of a file\n");
871 if (!gettingfile && setjmp(restart) != 0)
872 return;
873 gettingfile++;
874 loop:
875 for (i = 0; i < spcl.c_count; i++) {
876 if (readmapflag || spcl.c_addr[i]) {
877 readtape(&buf[curblk++][0]);
878 if (curblk == fssize / TP_BSIZE) {
879 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
880 fssize : (curblk - 1) * TP_BSIZE + size));
881 curblk = 0;
882 last_write_was_hole = 0;
883 }
884 } else {
885 if (curblk > 0) {
886 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
887 curblk * TP_BSIZE :
888 (curblk - 1) * TP_BSIZE + size));
889 curblk = 0;
890 }
891 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
892 TP_BSIZE : size));
893 last_write_was_hole = 1;
894 }
895 if ((size -= TP_BSIZE) <= 0) {
896 for (i++; i < spcl.c_count; i++)
897 if (readmapflag || spcl.c_addr[i])
898 readtape(junk);
899 break;
900 }
901 }
902 if (gethead(&spcl) == GOOD && size > 0) {
903 if (spcl.c_type == TS_ADDR)
904 goto loop;
905 Dprintf(stdout,
906 "Missing address (header) block for %s at %ld blocks\n",
907 curfile.name, (long)blksread);
908 }
909 if (curblk > 0) {
910 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
911 last_write_was_hole = 0;
912 }
913 if (size > 0) {
914 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
915 while (size > 0) {
916 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
917 (*skip)(clearedbuf, skp);
918 size -= skp;
919 }
920 last_write_was_hole = 1;
921 }
922 if (last_write_was_hole) {
923 FTRUNCATE(ofile, origsize);
924 }
925 findinode(&spcl);
926 gettingfile = 0;
927 }
928
929 /*
930 * Write out the next block of a file.
931 */
932 static void
933 xtrfile(char *buf, size_t size)
934 {
935
936 if (Nflag)
937 return;
938 if (write(ofile, buf, (int) size) == -1)
939 err(1, "write error extracting inode %lu, name %s\nwrite",
940 (unsigned long)curfile.ino, curfile.name);
941 }
942
943 /*
944 * Skip over a hole in a file.
945 */
946 /* ARGSUSED */
947 static void
948 xtrskip(char *buf, size_t size)
949 {
950
951 if (LSEEK(ofile, (off_t)size, SEEK_CUR) == -1)
952 err(1, "seek error extracting inode %lu, name %s\nlseek",
953 (unsigned long)curfile.ino, curfile.name);
954 }
955
956 /*
957 * Collect the next block of a symbolic link.
958 */
959 static void
960 xtrlnkfile(char *buf, size_t size)
961 {
962
963 pathlen += size;
964 if (pathlen > MAXPATHLEN)
965 errx(1, "symbolic link name: %s->%s%s; too long %d",
966 curfile.name, lnkbuf, buf, pathlen);
967 (void) strcat(lnkbuf, buf);
968 }
969
970 /*
971 * Skip over a hole in a symbolic link (should never happen).
972 */
973 /* ARGSUSED */
974 static void
975 xtrlnkskip(char *buf, size_t size)
976 {
977
978 errx(1, "unallocated block in symbolic link %s", curfile.name);
979 }
980
981 /*
982 * Collect the next block of a bit map.
983 */
984 static void
985 xtrmap(char *buf, size_t size)
986 {
987
988 memmove(map, buf, size);
989 map += size;
990 }
991
992 /*
993 * Skip over a hole in a bit map (should never happen).
994 */
995 /* ARGSUSED */
996 static void
997 xtrmapskip(char *buf, size_t size)
998 {
999
1000 panic("hole in map\n");
1001 map += size;
1002 }
1003
1004 /*
1005 * Noop, when an extraction function is not needed.
1006 */
1007 /* ARGSUSED */
1008 void
1009 xtrnull(char *buf, size_t size)
1010 {
1011
1012 return;
1013 }
1014
1015 #if COMPARE_ONTHEFLY
1016 /*
1017 * Compare the next block of a file.
1018 */
1019 static void
1020 xtrcmpfile(char *buf, size_t size)
1021 {
1022 static char cmpbuf[MAXBSIZE];
1023
1024 if (cmperror)
1025 return;
1026
1027 if (read(ifile, cmpbuf, size) != size) {
1028 fprintf(stderr, "%s: size has changed.\n",
1029 curfile.name);
1030 cmperror = 1;
1031 return;
1032 }
1033
1034 if (memcmp(buf, cmpbuf, size) != 0) {
1035 fprintf(stderr, "%s: tape and disk copies are different\n",
1036 curfile.name);
1037 cmperror = 1;
1038 return;
1039 }
1040 }
1041
1042 /*
1043 * Skip over a hole in a file.
1044 */
1045 static void
1046 xtrcmpskip(char *buf, size_t size)
1047 {
1048 static char cmpbuf[MAXBSIZE];
1049 int i;
1050
1051 if (cmperror)
1052 return;
1053
1054 if (read(ifile, cmpbuf, size) != size) {
1055 fprintf(stderr, "%s: size has changed.\n",
1056 curfile.name);
1057 cmperror = 1;
1058 return;
1059 }
1060
1061 for (i = 0; i < size; ++i)
1062 if (cmpbuf[i] != '\0') {
1063 fprintf(stderr, "%s: tape and disk copies are different\n",
1064 curfile.name);
1065 cmperror = 1;
1066 return;
1067 }
1068 }
1069 #endif /* COMPARE_ONTHEFLY */
1070
1071 #if !COMPARE_ONTHEFLY
1072 static int
1073 do_cmpfiles(int fd_tape, int fd_disk, long size)
1074 {
1075 static char buf_tape[BUFSIZ];
1076 static char buf_disk[BUFSIZ];
1077 ssize_t n_tape;
1078 ssize_t n_disk;
1079
1080 while (size > 0) {
1081 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1082 close(fd_tape), close(fd_disk);
1083 panic("do_cmpfiles: unexpected EOF[1]");
1084 }
1085 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1086 close(fd_tape), close(fd_disk);
1087 panic("do_cmpfiles: unexpected EOF[2]");
1088 }
1089 if (n_tape != n_disk) {
1090 close(fd_tape), close(fd_disk);
1091 panic("do_cmpfiles: sizes different!");
1092 }
1093 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1094 size -= n_tape;
1095 }
1096 return (0);
1097 }
1098
1099 /* for debugging compare problems */
1100 #undef COMPARE_FAIL_KEEP_FILE
1101
1102 static
1103 #ifdef COMPARE_FAIL_KEEP_FILE
1104 /* return true if tapefile should be unlinked after compare */
1105 int
1106 #else
1107 void
1108 #endif
1109 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1110 {
1111 struct STAT sbuf_tape;
1112 int fd_tape, fd_disk;
1113
1114 if (STAT(tapefile, &sbuf_tape) != 0) {
1115 panic("Can't lstat tmp file %s: %s\n", tapefile,
1116 strerror(errno));
1117 do_compare_error;
1118 }
1119
1120 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1121 fprintf(stderr,
1122 "%s: size changed from %ld to %ld.\n",
1123 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1124 do_compare_error;
1125 #ifdef COMPARE_FAIL_KEEP_FILE
1126 return (0);
1127 #else
1128 return;
1129 #endif
1130 }
1131
1132 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1133 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1134 do_compare_error;
1135 }
1136 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1137 close(fd_tape);
1138 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1139 do_compare_error;
1140 }
1141
1142 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1143 fprintf(stderr, "%s: tape and disk copies are different\n",
1144 diskfile);
1145 close(fd_tape);
1146 close(fd_disk);
1147 do_compare_error;
1148 #ifdef COMPARE_FAIL_KEEP_FILE
1149 /* rename the file to live in /tmp */
1150 /* rename `tapefile' to /tmp/<basename of diskfile> */
1151 {
1152 char *p = strrchr(diskfile, '/');
1153 char newname[MAXPATHLEN];
1154 if (!p) {
1155 panic("can't find / in %s\n", diskfile);
1156 }
1157 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1158 if (rename(tapefile, newname)) {
1159 panic("rename from %s to %s failed: %s\n",
1160 tapefile, newname,
1161 strerror(errno));
1162 } else {
1163 fprintf(stderr, "*** %s saved to %s\n",
1164 tapefile, newname);
1165 }
1166 }
1167
1168 /* don't unlink the file (it's not there anymore */
1169 /* anyway) */
1170 return (0);
1171 #else
1172 return;
1173 #endif
1174 }
1175 close(fd_tape);
1176 close(fd_disk);
1177 #ifdef COMPARE_FAIL_KEEP_FILE
1178 return (1);
1179 #endif
1180 }
1181 #endif /* !COMPARE_ONTHEFLY */
1182
1183 #if !COMPARE_ONTHEFLY
1184 static char tmpfilename[MAXPATHLEN];
1185 #endif
1186
1187 void
1188 comparefile(char *name)
1189 {
1190 int mode;
1191 struct STAT sb;
1192 int r;
1193 #if !COMPARE_ONTHEFLY
1194 static char *tmpfile = NULL;
1195 struct STAT stemp;
1196 #endif
1197
1198 if ((r = LSTAT(name, &sb)) != 0) {
1199 warn("%s: does not exist (%d)", name, r);
1200 do_compare_error;
1201 skipfile();
1202 return;
1203 }
1204
1205 curfile.name = name;
1206 curfile.action = USING;
1207 mode = curfile.dip->di_mode;
1208
1209 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1210 (long)sb.st_size, mode);
1211
1212 if (sb.st_mode != mode) {
1213 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1214 name, mode & 07777, sb.st_mode & 07777);
1215 do_compare_error;
1216 }
1217 switch (mode & IFMT) {
1218 default:
1219 skipfile();
1220 return;
1221
1222 case IFSOCK:
1223 skipfile();
1224 return;
1225
1226 case IFDIR:
1227 skipfile();
1228 return;
1229
1230 case IFLNK: {
1231 char lbuf[MAXPATHLEN + 1];
1232 int lsize;
1233
1234 if (!(sb.st_mode & S_IFLNK)) {
1235 fprintf(stderr, "%s: is no longer a symbolic link\n",
1236 name);
1237 do_compare_error;
1238 return;
1239 }
1240 lnkbuf[0] = '\0';
1241 pathlen = 0;
1242 getfile(xtrlnkfile, xtrlnkskip);
1243 if (pathlen == 0) {
1244 fprintf(stderr,
1245 "%s: zero length symbolic link (ignored)\n",
1246 name);
1247 do_compare_error;
1248 return;
1249 }
1250 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1251 panic("readlink of %s failed: %s", name,
1252 strerror(errno));
1253 do_compare_error;
1254 }
1255 lbuf[lsize] = 0;
1256 if (strcmp(lbuf, lnkbuf) != 0) {
1257 fprintf(stderr,
1258 "%s: symbolic link changed from %s to %s.\n",
1259 name, lnkbuf, lbuf);
1260 do_compare_error;
1261 return;
1262 }
1263 return;
1264 }
1265
1266 case IFCHR:
1267 case IFBLK:
1268 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1269 fprintf(stderr, "%s: no longer a special file\n",
1270 name);
1271 do_compare_error;
1272 skipfile();
1273 return;
1274 }
1275
1276 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1277 fprintf(stderr,
1278 "%s: device changed from %d,%d to %d,%d.\n",
1279 name,
1280 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1281 (int)curfile.dip->di_rdev & 0xff,
1282 ((int)sb.st_rdev >> 8) & 0xff,
1283 (int)sb.st_rdev & 0xff);
1284 do_compare_error;
1285 }
1286 skipfile();
1287 return;
1288
1289 case IFREG:
1290 #if COMPARE_ONTHEFLY
1291 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1292 panic("Can't open %s: %s\n", name, strerror(errno));
1293 skipfile();
1294 do_compare_error;
1295 }
1296 else {
1297 cmperror = 0;
1298 getfile(xtrcmpfile, xtrcmpskip);
1299 if (!cmperror) {
1300 char c;
1301 if (read(ifile, &c, 1) != 0) {
1302 fprintf(stderr, "%s: size has changed.\n",
1303 name);
1304 cmperror = 1;
1305 }
1306 }
1307 if (cmperror)
1308 do_compare_error;
1309 close(ifile);
1310 }
1311 #else
1312 if (tmpfile == NULL) {
1313 /* argument to mktemp() must not be in RO space: */
1314 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1315 tmpfile = mktemp(&tmpfilename[0]);
1316 }
1317 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1318 panic("cannot delete tmp file %s: %s\n",
1319 tmpfile, strerror(errno));
1320 }
1321 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1322 panic("cannot create file temp file %s: %s\n",
1323 name, strerror(errno));
1324 }
1325 getfile(xtrfile, xtrskip);
1326 (void) close(ofile);
1327 #ifdef COMPARE_FAIL_KEEP_FILE
1328 if (cmpfiles(tmpfile, name, &sb))
1329 unlink(tmpfile);
1330 #else
1331 cmpfiles(tmpfile, name, &sb);
1332 unlink(tmpfile);
1333 #endif
1334 #endif /* COMPARE_ONTHEFLY */
1335 return;
1336 }
1337 /* NOTREACHED */
1338 }
1339
1340 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1341 static void (*readtape_func)(char *) = readtape_set;
1342
1343 /*
1344 * Read TP_BSIZE blocks from the input.
1345 * Handle read errors, and end of media.
1346 * Decompress compressed blocks.
1347 */
1348 static void
1349 readtape(char *buf)
1350 {
1351 (*readtape_func)(buf); /* call the actual processing routine */
1352 }
1353
1354 /*
1355 * Set function pointer for readtape() routine. zflag and magtapein must
1356 * be correctly set before the first call to readtape().
1357 */
1358 static void
1359 readtape_set(char *buf)
1360 {
1361 if (!zflag)
1362 readtape_func = readtape_uncompr;
1363 else {
1364 newcomprbuf(ntrec);
1365 if (magtapein)
1366 readtape_func = readtape_comprtape;
1367 else
1368 readtape_func = readtape_comprfile;
1369 }
1370 readtape(buf);
1371 }
1372
1373 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1374
1375 /*
1376 * This is the original readtape(), it's used for reading uncompressed input.
1377 * Read TP_BSIZE blocks from the input.
1378 * Handle read errors, and end of media.
1379 */
1380 static void
1381 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1382 readtape_uncompr(char *buf)
1383 #else
1384 readtape(char *buf)
1385 #endif
1386 {
1387 ssize_t rd, newvol, i;
1388 int cnt, seek_failed;
1389
1390 if (blkcnt < numtrec) {
1391 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1392 blksread++;
1393 tpblksread++;
1394 return;
1395 }
1396 tbufptr = tapebuf;
1397 for (i = 0; i < ntrec; i++)
1398 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1399 if (numtrec == 0)
1400 numtrec = ntrec;
1401 cnt = ntrec * TP_BSIZE;
1402 rd = 0;
1403 getmore:
1404 #ifdef RRESTORE
1405 if (host)
1406 i = rmtread(&tapebuf[rd], cnt);
1407 else
1408 #endif
1409 i = read(mt, &tapebuf[rd], cnt);
1410
1411 /*
1412 * Check for mid-tape short read error.
1413 * If found, skip rest of buffer and start with the next.
1414 */
1415 if (!pipein && numtrec < ntrec && i > 0) {
1416 Dprintf(stdout, "mid-media short read error.\n");
1417 numtrec = ntrec;
1418 }
1419 /*
1420 * Handle partial block read.
1421 */
1422 if (pipein && i == 0 && rd > 0)
1423 i = rd;
1424 else if (i > 0 && i != ntrec * TP_BSIZE) {
1425 if (pipein) {
1426 rd += i;
1427 cnt -= i;
1428 if (cnt > 0)
1429 goto getmore;
1430 i = rd;
1431 } else {
1432 /*
1433 * Short read. Process the blocks read.
1434 */
1435 if (i % TP_BSIZE != 0)
1436 Vprintf(stdout,
1437 "partial block read: %ld should be %ld\n",
1438 (long)i, ntrec * TP_BSIZE);
1439 numtrec = i / TP_BSIZE;
1440 }
1441 }
1442 /*
1443 * Handle read error.
1444 */
1445 if (i < 0) {
1446 fprintf(stderr, "Tape read error while ");
1447 switch (curfile.action) {
1448 default:
1449 fprintf(stderr, "trying to set up tape\n");
1450 break;
1451 case UNKNOWN:
1452 fprintf(stderr, "trying to resynchronize\n");
1453 break;
1454 case USING:
1455 fprintf(stderr, "restoring %s\n", curfile.name);
1456 break;
1457 case SKIP:
1458 fprintf(stderr, "skipping over inode %lu\n",
1459 (unsigned long)curfile.ino);
1460 break;
1461 }
1462 if (!yflag && !reply("continue"))
1463 exit(1);
1464 i = ntrec * TP_BSIZE;
1465 memset(tapebuf, 0, (size_t)i);
1466 #ifdef RRESTORE
1467 if (host)
1468 seek_failed = (rmtseek(i, 1) < 0);
1469 else
1470 #endif
1471 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (off_t)-1);
1472
1473 if (seek_failed) {
1474 warn("continuation failed");
1475 if (!yflag && !reply("assume end-of-tape and continue"))
1476 exit(1);
1477 i = 0;
1478 }
1479 }
1480 /*
1481 * Handle end of tape.
1482 */
1483 if (i == 0) {
1484 Vprintf(stdout, "End-of-tape encountered\n");
1485 if (!pipein) {
1486 newvol = volno + 1;
1487 volno = 0;
1488 numtrec = 0;
1489 getvol(newvol);
1490 readtape(buf);
1491 return;
1492 }
1493 if (rd % TP_BSIZE != 0)
1494 panic("partial block read: %d should be %d\n",
1495 rd, ntrec * TP_BSIZE);
1496 terminateinput();
1497 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1498 }
1499 blkcnt = 0;
1500 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1501 blksread++;
1502 tpblksread++;
1503 }
1504
1505 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1506
1507 /*
1508 * Read a compressed format block from a file or pipe and uncompress it.
1509 * Attempt to handle read errors, and end of file.
1510 */
1511 static void
1512 readtape_comprfile(char *buf)
1513 {
1514 long rl, size, i, ret;
1515 int newvol;
1516 struct tapebuf *tpb;
1517
1518 if (blkcnt < numtrec) {
1519 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1520 blksread++;
1521 tpblksread++;
1522 return;
1523 }
1524 /* need to read the next block */
1525 tbufptr = tapebuf;
1526 for (i = 0; i < ntrec; i++)
1527 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1528 numtrec = ntrec;
1529 tpb = (struct tapebuf *) tapebuf;
1530
1531 /* read the block prefix */
1532 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1533
1534 if (Vflag && (ret == 0 || rl < PREFIXSIZE || tpb->length == 0))
1535 ret = 0;
1536 if (ret <= 0)
1537 goto readerr;
1538
1539 /* read the data */
1540 size = tpb->length;
1541 if (size > bufsize) {
1542 /* something's wrong */
1543 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1544 bufsize, size);
1545 size = bufsize;
1546 tpb->length = bufsize;
1547 }
1548 ret = read_a_block(mt, tpb->buf, size, &rl);
1549 if (ret <= 0)
1550 goto readerr;
1551
1552 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1553 if (tbufptr == NULL) {
1554 msg_read_error("File decompression error while");
1555 if (!yflag && !reply("continue"))
1556 exit(1);
1557 memset(tapebuf, 0, bufsize);
1558 tbufptr = tapebuf;
1559 }
1560
1561 blkcnt = 0;
1562 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1563 blksread++;
1564 tpblksread++;
1565 return;
1566
1567 readerr:
1568 /* Errors while reading from a file or pipe are catastrophic. Since
1569 * there are no block boundaries, it's impossible to bypass the
1570 * block in error and find the start of the next block.
1571 */
1572 if (ret == 0) {
1573 /* It's possible to have multiple input files using -M
1574 * and -f file1,file2...
1575 */
1576 Vprintf(stdout, "End-of-File encountered\n");
1577 if (!pipein) {
1578 newvol = volno + 1;
1579 volno = 0;
1580 numtrec = 0;
1581 getvol(newvol);
1582 readtape(buf);
1583 return;
1584 }
1585 }
1586 msg_read_error("Read error while");
1587 /* if (!yflag && !reply("continue")) */
1588 exit(1);
1589 }
1590
1591 /*
1592 * Read compressed data from a tape and uncompress it.
1593 * Handle read errors, and end of media.
1594 * Since a tape consists of separate physical blocks, we try
1595 * to recover from errors by repositioning the tape to the next
1596 * block.
1597 */
1598 static void
1599 readtape_comprtape(char *buf)
1600 {
1601 long rl, size, i;
1602 int ret, newvol;
1603 struct tapebuf *tpb;
1604 struct mtop tcom;
1605
1606 if (blkcnt < numtrec) {
1607 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1608 blksread++;
1609 tpblksread++;
1610 return;
1611 }
1612 /* need to read the next block */
1613 tbufptr = tapebuf;
1614 for (i = 0; i < ntrec; i++)
1615 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1616 numtrec = ntrec;
1617 tpb = (struct tapebuf *) tapebuf;
1618
1619 /* read the block */
1620 size = bufsize + PREFIXSIZE;
1621 ret = read_a_block(mt, tapebuf, size, &rl);
1622 if (ret <= 0)
1623 goto readerr;
1624
1625 tbufptr = decompress_tapebuf(tpb, rl);
1626 if (tbufptr == NULL) {
1627 msg_read_error("Tape decompression error while");
1628 if (!yflag && !reply("continue"))
1629 exit(1);
1630 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1631 tbufptr = tapebuf;
1632 }
1633 goto moverecord;
1634
1635 readerr:
1636 /* Handle errors: EOT switches to the next volume, other errors
1637 * attempt to position the tape to the next block.
1638 */
1639 if (ret == 0) {
1640 Vprintf(stdout, "End-of-tape encountered\n");
1641 newvol = volno + 1;
1642 volno = 0;
1643 numtrec = 0;
1644 getvol(newvol);
1645 readtape(buf);
1646 return;
1647 }
1648
1649 msg_read_error("Tape read error while");
1650 if (!yflag && !reply("continue"))
1651 exit(1);
1652 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1653 tbufptr = tapebuf;
1654
1655 #ifdef RRESTORE
1656 if (host)
1657 rl = rmtioctl(MTFSR, 1);
1658 else
1659 #endif
1660 {
1661 tcom.mt_op = MTFSR;
1662 tcom.mt_count = 1;
1663 rl = ioctl(mt, MTIOCTOP, &tcom);
1664 }
1665
1666 if (rl < 0) {
1667 warn("continuation failed");
1668 if (!yflag && !reply("assume end-of-tape and continue"))
1669 exit(1);
1670 ret = 0; /* end of tape */
1671 goto readerr;
1672 }
1673
1674 moverecord:
1675 blkcnt = 0;
1676 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1677 blksread++;
1678 tpblksread++;
1679 }
1680
1681 /*
1682 * Decompress a struct tapebuf into a buffer. readsize is the size read
1683 * from the tape/file and is used for error messages. Returns a pointer
1684 * to the location of the uncompressed buffer or NULL on errors.
1685 * Adjust numtrec and complain for a short block.
1686 */
1687 static char *
1688 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
1689 {
1690 /* If zflag is on, all blocks have a struct tapebuf prefix */
1691 /* zflag gets set in setup() from the dump header */
1692 int cresult, blocklen;
1693 unsigned long worklen;
1694 #ifdef HAVE_BZLIB
1695 unsigned int worklen2;
1696 #endif
1697 char *output = NULL,*reason = NULL, *lengtherr = NULL;
1698
1699 /* build a length error message */
1700 blocklen = tpbin->length;
1701 if (readsize < blocklen + PREFIXSIZE)
1702 lengtherr = "short";
1703 else
1704 if (readsize > blocklen + PREFIXSIZE)
1705 lengtherr = "long";
1706
1707 worklen = comprlen;
1708 cresult = 1;
1709 if (tpbin->compressed) {
1710 /* uncompress whatever we read, if it fails, complain later */
1711 if (tpbin->flags == COMPRESS_ZLIB) {
1712 #ifndef HAVE_ZLIB
1713 errx(1,"This restore version doesn't support zlib decompression");
1714 #else
1715 cresult = uncompress(comprbuf, &worklen,
1716 tpbin->buf, blocklen);
1717 output = comprbuf;
1718 switch (cresult) {
1719 case Z_OK:
1720 break;
1721 case Z_MEM_ERROR:
1722 reason = "not enough memory";
1723 break;
1724 case Z_BUF_ERROR:
1725 reason = "buffer too small";
1726 break;
1727 case Z_DATA_ERROR:
1728 reason = "data error";
1729 break;
1730 default:
1731 reason = "unknown";
1732 }
1733 if (cresult == Z_OK)
1734 cresult = 1;
1735 else
1736 cresult = 0;
1737 #endif /* HAVE_ZLIB */
1738 }
1739 if (tpbin->flags == COMPRESS_BZLIB) {
1740 #ifndef HAVE_BZLIB
1741 errx(1,"This restore version doesn't support bzlib decompression");
1742 #else
1743 worklen2 = worklen;
1744 cresult = BZ2_bzBuffToBuffDecompress(
1745 comprbuf, &worklen2,
1746 tpbin->buf, blocklen, 0, 0);
1747 worklen = worklen2;
1748 output = comprbuf;
1749 switch (cresult) {
1750 case BZ_OK:
1751 break;
1752 case BZ_MEM_ERROR:
1753 reason = "not enough memory";
1754 break;
1755 case BZ_OUTBUFF_FULL:
1756 reason = "buffer too small";
1757 break;
1758 case BZ_DATA_ERROR:
1759 case BZ_DATA_ERROR_MAGIC:
1760 case BZ_UNEXPECTED_EOF:
1761 reason = "data error";
1762 break;
1763 default:
1764 reason = "unknown";
1765 }
1766 if (cresult == BZ_OK)
1767 cresult = 1;
1768 else
1769 cresult = 0;
1770 #endif /* HAVE_BZLIB */
1771 }
1772 }
1773 else {
1774 output = tpbin->buf;
1775 worklen = blocklen;
1776 }
1777 if (cresult) {
1778 numtrec = worklen / TP_BSIZE;
1779 if (worklen % TP_BSIZE != 0)
1780 reason = "length mismatch";
1781 }
1782 if (reason) {
1783 if (lengtherr)
1784 fprintf(stderr, "%s compressed block: %d expected: %d\n",
1785 lengtherr, readsize, tpbin->length + PREFIXSIZE);
1786 fprintf(stderr, "decompression error, block %ld: %s\n",
1787 tpblksread+1, reason);
1788 if (!cresult)
1789 output = NULL;
1790 }
1791 return output;
1792 }
1793
1794 /*
1795 * Print an error message for a read error.
1796 * This was exteracted from the original readtape().
1797 */
1798 static void
1799 msg_read_error(char *m)
1800 {
1801 switch (curfile.action) {
1802 default:
1803 fprintf(stderr, "%s trying to set up tape\n", m);
1804 break;
1805 case UNKNOWN:
1806 fprintf(stderr, "%s trying to resynchronize\n", m);
1807 break;
1808 case USING:
1809 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
1810 break;
1811 case SKIP:
1812 fprintf(stderr, "%s skipping over inode %lu\n", m,
1813 (unsigned long)curfile.ino);
1814 break;
1815 }
1816 }
1817 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1818
1819 /*
1820 * Read the first block and get the blocksize from it. Test
1821 * for a compressed dump tape/file. setup() will make the final
1822 * determination by checking the compressed flag if gethead()
1823 * finds a valid header. The test here is necessary to offset the buffer
1824 * by the size of the compressed prefix. zflag is set here so that
1825 * readtape_set can set the correct function pointer for readtape().
1826 * Note that the first block of each tape/file is not compressed
1827 * and does not have a prefix.
1828 */
1829 static void
1830 findtapeblksize(void)
1831 {
1832 long i;
1833 size_t len;
1834 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1835 struct s_spcl *spclpt = (struct s_spcl *) tapebuf;
1836
1837 for (i = 0; i < ntrec; i++)
1838 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1839 blkcnt = 0;
1840 tbufptr = tapebuf;
1841 /*
1842 * For a pipe or file, read in the first record. For a tape, read
1843 * the first block.
1844 */
1845 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
1846
1847 if (read_a_block(mt, tapebuf, len, &i) <= 0)
1848 errx(1, "Tape read error on first record");
1849
1850 /*
1851 * If the input is from a file or a pipe, we read TP_BSIZE
1852 * bytes looking for a dump header. If the dump is compressed
1853 * we need to read in the rest of the block, as determined
1854 * by c_ntrec in the dump header. The first block of the
1855 * dump is not compressed and does not have a prefix.
1856 */
1857 if (!magtapein) {
1858 if (spclpt->c_type == TS_TAPE
1859 && spclpt->c_flags & DR_COMPRESSED) {
1860 /* It's a compressed dump file, read in the */
1861 /* rest of the block based on spclpt->c_ntrec. */
1862 if (spclpt->c_ntrec > ntrec)
1863 errx(1, "Tape blocksize is too large, use "
1864 "\'-b %d\' ", spclpt->c_ntrec);
1865 ntrec = spclpt->c_ntrec;
1866 len = (ntrec - 1) * TP_BSIZE;
1867 zflag = 1;
1868 }
1869 else {
1870 /* read in the rest of the block based on bufsize */
1871 len = bufsize - TP_BSIZE;
1872 }
1873 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1874 || (i != len && i % TP_BSIZE != 0))
1875 errx(1,"Error reading dump file header");
1876 tbufptr = tapebuf;
1877 numtrec = ntrec;
1878 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1879 return;
1880 } /* if (!magtapein) */
1881
1882 /*
1883 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1884 * If the value of ntrec is too large, we read less than
1885 * what we asked for; adjust the value of ntrec and test for
1886 * a compressed dump tape.
1887 */
1888
1889 if (i % TP_BSIZE != 0) {
1890 /* may be old format compressed dump tape with a prefix */
1891 spclpt = (struct s_spcl *) tpb->buf;
1892 if (i % TP_BSIZE == PREFIXSIZE
1893 && tpb->compressed == 0
1894 && spclpt->c_type == TS_TAPE
1895 && spclpt->c_flags & DR_COMPRESSED) {
1896 zflag = 1;
1897 tbufptr = tpb->buf;
1898 if (tpb->length > bufsize)
1899 errx(1, "Tape blocksize is too large, use "
1900 "\'-b %d\' ", tpb->length / TP_BSIZE);
1901 }
1902 else
1903 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1904 i, TP_BSIZE);
1905 }
1906 ntrec = i / TP_BSIZE;
1907 if (spclpt->c_type == TS_TAPE) {
1908 if (spclpt->c_flags & DR_COMPRESSED)
1909 zflag = 1;
1910 if (spclpt->c_ntrec > ntrec)
1911 errx(1, "Tape blocksize is too large, use "
1912 "\'-b %d\' ", spclpt->c_ntrec);
1913 }
1914 numtrec = ntrec;
1915 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1916 }
1917
1918 /*
1919 * Read a block of data handling all of the messy details.
1920 */
1921 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1922 {
1923 long i = 1, size;
1924
1925 size = len;
1926 while (size > 0) {
1927 #ifdef RRESTORE
1928 if (host)
1929 i = rmtread(buf, size);
1930 else
1931 #endif
1932 i = read(fd, buf, size);
1933
1934 if (i <= 0)
1935 break; /* EOD or error */
1936 size -= i;
1937 if (magtapein)
1938 break; /* block at a time for mt */
1939 buf += i;
1940 }
1941 *lengthread = len - size;
1942 return i;
1943 }
1944
1945 void
1946 closemt(void)
1947 {
1948
1949 if (mt < 0)
1950 return;
1951 #ifdef RRESTORE
1952 if (host)
1953 rmtclose();
1954 else
1955 #endif
1956 (void) close(mt);
1957 }
1958
1959 static void
1960 setmagtapein(void) {
1961 struct mtget mt_stat;
1962 static int done = 0;
1963 if (done)
1964 return;
1965 done = 1;
1966 if (!pipein) {
1967 /* need to know if input is really from a tape */
1968 #ifdef RRESTORE
1969 if (host)
1970 magtapein = !lflag;
1971 else
1972 #endif
1973 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
1974 }
1975
1976 Vprintf(stdout,"Input is from %s\n",
1977 magtapein ? "tape" :
1978 Vflag ? "multi-volume (no tape)" : "file/pipe");
1979 }
1980
1981 /*
1982 * Read the next block from the tape.
1983 * Check to see if it is one of several vintage headers.
1984 * If it is an old style header, convert it to a new style header.
1985 * If it is not any valid header, return an error.
1986 */
1987 static int
1988 gethead(struct s_spcl *buf)
1989 {
1990 int32_t i;
1991 union {
1992 quad_t qval;
1993 int32_t val[2];
1994 } qcvt;
1995 union u_ospcl {
1996 char dummy[TP_BSIZE];
1997 struct s_ospcl {
1998 int32_t c_type;
1999 int32_t c_date;
2000 int32_t c_ddate;
2001 int32_t c_volume;
2002 int32_t c_tapea;
2003 u_int16_t c_inumber;
2004 int32_t c_magic;
2005 int32_t c_checksum;
2006 struct odinode {
2007 u_int16_t odi_mode;
2008 u_int16_t odi_nlink;
2009 u_int16_t odi_uid;
2010 u_int16_t odi_gid;
2011 int32_t odi_size;
2012 int32_t odi_rdev;
2013 char odi_addr[36];
2014 int32_t odi_atime;
2015 int32_t odi_mtime;
2016 int32_t odi_ctime;
2017 } c_dinode;
2018 int32_t c_count;
2019 char c_fill[256];
2020 } s_ospcl;
2021 } u_ospcl;
2022
2023 if (!cvtflag) {
2024 readtape((char *)buf);
2025 if (buf->c_magic != NFS_MAGIC) {
2026 if (swabi(buf->c_magic) != NFS_MAGIC)
2027 return (FAIL);
2028 if (!Bcvt) {
2029 Vprintf(stdout, "Note: Doing Byte swapping\n");
2030 Bcvt = 1;
2031 }
2032 }
2033 if (checksum((int *)buf) == FAIL)
2034 return (FAIL);
2035 if (Bcvt)
2036 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
2037 goto good;
2038 }
2039 readtape((char *)(&u_ospcl.s_ospcl));
2040 memset((char *)buf, 0, (long)TP_BSIZE);
2041 buf->c_type = u_ospcl.s_ospcl.c_type;
2042 buf->c_date = u_ospcl.s_ospcl.c_date;
2043 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
2044 buf->c_volume = u_ospcl.s_ospcl.c_volume;
2045 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
2046 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
2047 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
2048 buf->c_magic = u_ospcl.s_ospcl.c_magic;
2049 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
2050 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
2051 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
2052 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
2053 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
2054 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
2055 #ifdef __linux__
2056 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
2057 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2058 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2059 #else /* __linux__ */
2060 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
2061 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2062 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2063 #endif /* __linux__ */
2064 buf->c_count = u_ospcl.s_ospcl.c_count;
2065 memmove(buf->c_addr, u_ospcl.s_ospcl.c_fill, (long)256);
2066 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
2067 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
2068 return(FAIL);
2069 buf->c_magic = NFS_MAGIC;
2070
2071 good:
2072 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
2073 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
2074 qcvt.qval = buf->c_dinode.di_size;
2075 if (qcvt.val[0] || qcvt.val[1]) {
2076 printf("Note: Doing Quad swapping\n");
2077 Qcvt = 1;
2078 }
2079 }
2080 if (Qcvt) {
2081 qcvt.qval = buf->c_dinode.di_size;
2082 i = qcvt.val[1];
2083 qcvt.val[1] = qcvt.val[0];
2084 qcvt.val[0] = i;
2085 buf->c_dinode.di_size = qcvt.qval;
2086 }
2087 readmapflag = 0;
2088
2089 switch (buf->c_type) {
2090
2091 case TS_CLRI:
2092 case TS_BITS:
2093 /*
2094 * Have to patch up missing information in bit map headers
2095 */
2096 buf->c_inumber = 0;
2097 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
2098 if (buf->c_count > TP_NINDIR)
2099 readmapflag = 1;
2100 else
2101 for (i = 0; i < buf->c_count; i++)
2102 buf->c_addr[i]++;
2103 break;
2104
2105 case TS_TAPE:
2106 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
2107 oldinofmt = 1;
2108 /* fall through */
2109 case TS_END:
2110 buf->c_inumber = 0;
2111 if (buf->c_flags & DR_INODEINFO) {
2112 memcpy(volinfo, buf->c_inos, TP_NINOS * sizeof(dump_ino_t));
2113 if (Bcvt)
2114 swabst((u_char *)"128i", (u_char *)volinfo);
2115 }
2116 break;
2117
2118 case TS_INODE:
2119 case TS_ADDR:
2120 break;
2121
2122 default:
2123 panic("gethead: unknown inode type %d\n", buf->c_type);
2124 break;
2125 }
2126 /*
2127 * If we are restoring a filesystem with old format inodes,
2128 * copy the uid/gid to the new location.
2129 */
2130 if (oldinofmt) {
2131 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2132 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2133 }
2134 if (dflag)
2135 accthdr(buf);
2136 return(GOOD);
2137 }
2138
2139 /*
2140 * Check that a header is where it belongs and predict the next header
2141 */
2142 static void
2143 accthdr(struct s_spcl *header)
2144 {
2145 static dump_ino_t previno = 0x7fffffff;
2146 static int prevtype;
2147 static long predict;
2148 long blks, i;
2149
2150 if (header->c_type == TS_TAPE) {
2151 fprintf(stderr, "Volume header (%s inode format) ",
2152 oldinofmt ? "old" : "new");
2153 if (header->c_firstrec)
2154 fprintf(stderr, "begins with record %d",
2155 header->c_firstrec);
2156 fprintf(stderr, "\n");
2157 previno = 0x7fffffff;
2158 return;
2159 }
2160 if (previno == 0x7fffffff)
2161 goto newcalc;
2162 switch (prevtype) {
2163 case TS_BITS:
2164 fprintf(stderr, "Dumped inodes map header");
2165 break;
2166 case TS_CLRI:
2167 fprintf(stderr, "Used inodes map header");
2168 break;
2169 case TS_INODE:
2170 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2171 break;
2172 case TS_ADDR:
2173 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2174 break;
2175 case TS_END:
2176 fprintf(stderr, "End of tape header");
2177 break;
2178 }
2179 if (predict != blksread - 1)
2180 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2181 predict, blksread - 1);
2182 fprintf(stderr, "\n");
2183 newcalc:
2184 blks = 0;
2185 if (header->c_type != TS_END)
2186 for (i = 0; i < header->c_count; i++)
2187 if (readmapflag || header->c_addr[i] != 0)
2188 blks++;
2189 predict = blks;
2190 blksread = 0;
2191 prevtype = header->c_type;
2192 previno = header->c_inumber;
2193 }
2194
2195 /*
2196 * Find an inode header.
2197 * Complain if had to skip, and complain is set.
2198 */
2199 static void
2200 findinode(struct s_spcl *header)
2201 {
2202 static long skipcnt = 0;
2203 long i;
2204 char buf[TP_BSIZE];
2205
2206 curfile.name = "<name unknown>";
2207 curfile.action = UNKNOWN;
2208 curfile.dip = NULL;
2209 curfile.ino = 0;
2210 do {
2211 if (header->c_magic != NFS_MAGIC) {
2212 skipcnt++;
2213 while (gethead(header) == FAIL ||
2214 header->c_date != dumpdate)
2215 skipcnt++;
2216 }
2217 switch (header->c_type) {
2218
2219 case TS_ADDR:
2220 /*
2221 * Skip up to the beginning of the next record
2222 */
2223 for (i = 0; i < header->c_count; i++)
2224 if (header->c_addr[i])
2225 readtape(buf);
2226 while (gethead(header) == FAIL ||
2227 header->c_date != dumpdate)
2228 skipcnt++;
2229 break;
2230
2231 case TS_INODE:
2232 curfile.dip = &header->c_dinode;
2233 curfile.ino = header->c_inumber;
2234 break;
2235
2236 case TS_END:
2237 curfile.ino = maxino;
2238 break;
2239
2240 case TS_CLRI:
2241 curfile.name = "<file removal list>";
2242 break;
2243
2244 case TS_BITS:
2245 curfile.name = "<file dump list>";
2246 break;
2247
2248 case TS_TAPE:
2249 panic("unexpected tape header\n");
2250 /* NOTREACHED */
2251
2252 default:
2253 panic("unknown tape header type %d\n", spcl.c_type);
2254 /* NOTREACHED */
2255
2256 }
2257 } while (header->c_type == TS_ADDR);
2258 if (skipcnt > 0)
2259 #ifdef USE_QFA
2260 if (!noresyncmesg)
2261 #endif
2262 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2263 skipcnt);
2264 skipcnt = 0;
2265 }
2266
2267 static int
2268 checksum(int *buf)
2269 {
2270 int i, j;
2271
2272 j = sizeof(union u_spcl) / sizeof(int);
2273 i = 0;
2274 if(!Bcvt) {
2275 do
2276 i += *buf++;
2277 while (--j);
2278 } else {
2279 /* What happens if we want to read restore tapes
2280 for a 16bit int machine??? */
2281 do
2282 i += swabi(*buf++);
2283 while (--j);
2284 }
2285
2286 if (i != CHECKSUM) {
2287 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2288 (unsigned long)curfile.ino, curfile.name);
2289 return(FAIL);
2290 }
2291 return(GOOD);
2292 }
2293
2294 #ifdef RRESTORE
2295 #ifdef __STDC__
2296 #include <stdarg.h>
2297 #else
2298 #include <varargs.h>
2299 #endif
2300
2301 void
2302 #ifdef __STDC__
2303 msg(const char *fmt, ...)
2304 #else
2305 msg(fmt, va_alist)
2306 char *fmt;
2307 va_dcl
2308 #endif
2309 {
2310 va_list ap;
2311 #ifdef __STDC__
2312 va_start(ap, fmt);
2313 #else
2314 va_start(ap);
2315 #endif
2316 (void)vfprintf(stderr, fmt, ap);
2317 va_end(ap);
2318 }
2319 #endif /* RRESTORE */
2320
2321 static u_char *
2322 swab16(u_char *sp, int n)
2323 {
2324 char c;
2325
2326 while (--n >= 0) {
2327 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2328 sp += 2;
2329 }
2330 return (sp);
2331 }
2332
2333 static u_char *
2334 swab32(u_char *sp, int n)
2335 {
2336 char c;
2337
2338 while (--n >= 0) {
2339 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2340 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2341 sp += 4;
2342 }
2343 return (sp);
2344 }
2345
2346 static u_char *
2347 swab64(u_char *sp, int n)
2348 {
2349 char c;
2350
2351 while (--n >= 0) {
2352 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2353 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2354 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2355 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2356 sp += 8;
2357 }
2358 return (sp);
2359 }
2360
2361 void
2362 swabst(u_char *cp, u_char *sp)
2363 {
2364 int n = 0;
2365
2366 while (*cp) {
2367 switch (*cp) {
2368 case '0': case '1': case '2': case '3': case '4':
2369 case '5': case '6': case '7': case '8': case '9':
2370 n = (n * 10) + (*cp++ - '0');
2371 continue;
2372
2373 case 's': case 'w': case 'h':
2374 if (n == 0)
2375 n = 1;
2376 sp = swab16(sp, n);
2377 break;
2378
2379 case 'i':
2380 if (n == 0)
2381 n = 1;
2382 sp = swab32(sp, n);
2383 break;
2384
2385 case 'l':
2386 if (n == 0)
2387 n = 1;
2388 sp = swab64(sp, n);
2389 break;
2390
2391 default: /* Any other character, like 'b' counts as byte. */
2392 if (n == 0)
2393 n = 1;
2394 sp += n;
2395 break;
2396 }
2397 cp++;
2398 n = 0;
2399 }
2400 }
2401
2402 static u_int
2403 swabi(u_int x)
2404 {
2405 swabst((u_char *)"i", (u_char *)&x);
2406 return (x);
2407 }
2408
2409 #if 0
2410 static u_long
2411 swabl(u_long x)
2412 {
2413 swabst((u_char *)"l", (u_char *)&x);
2414 return (x);
2415 }
2416 #endif
2417
2418 #ifdef USE_QFA
2419 /*
2420 * get the current position of the tape
2421 */
2422 int
2423 GetTapePos(long long *pos)
2424 {
2425 int err = 0;
2426
2427 #ifdef RDUMP
2428 if (host) {
2429 *pos = (long long) rmtseek(0, SEEK_CUR);
2430 err = *pos < 0;
2431 }
2432 else
2433 #endif
2434 {
2435 if (magtapein) {
2436 long mtpos;
2437 *pos = 0;
2438 err = (ioctl(mt, MTIOCPOS, &mtpos) < 0);
2439 *pos = (long long)mtpos;
2440 }
2441 else {
2442 *pos = LSEEK(mt, 0, SEEK_CUR);
2443 err = (*pos < 0);
2444 }
2445 }
2446 if (err) {
2447 err = errno;
2448 fprintf(stdout, "[%ld] error: %d (getting tapepos: %lld)\n",
2449 (unsigned long)getpid(), err, *pos);
2450 return err;
2451 }
2452 return err;
2453 }
2454
2455 typedef struct mt_pos {
2456 short mt_op;
2457 int mt_count;
2458 } MTPosRec, *MTPosPtr;
2459
2460 /*
2461 * go to specified position on tape
2462 */
2463 int
2464 GotoTapePos(long long pos)
2465 {
2466 int err = 0;
2467
2468 #ifdef RDUMP
2469 if (host)
2470 err = (rmtseek((long)pos, SEEK_SET) < 0);
2471 else
2472 #endif
2473 {
2474 if (magtapein) {
2475 struct mt_pos buf;
2476 buf.mt_op = MTSEEK;
2477 buf.mt_count = (int) pos;
2478 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
2479 }
2480 else {
2481 pos = LSEEK(mt, pos, SEEK_SET);
2482 err = (pos < 0);
2483 }
2484 }
2485 if (err) {
2486 err = errno;
2487 fprintf(stdout, "[%ld] error: %d (setting tapepos: %lld)\n",
2488 (unsigned long)getpid(), err, pos);
2489 return err;
2490 }
2491 return err;
2492 }
2493
2494 /*
2495 * read next data from tape to re-sync
2496 */
2497 void
2498 ReReadFromTape(void)
2499 {
2500 FLUSHTAPEBUF();
2501 noresyncmesg = 1;
2502 if (gethead(&spcl) == FAIL) {
2503 #ifdef DEBUG_QFA
2504 fprintf(stdout, "DEBUG 1 gethead failed\n");
2505 #endif
2506 }
2507 findinode(&spcl);
2508 noresyncmesg = 0;
2509 }
2510 #endif /* USE_QFA */
2511
2512 void
2513 RequestVol(long tnum)
2514 {
2515 FLUSHTAPEBUF();
2516 getvol(tnum);
2517 }