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