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