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