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