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