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