]> git.wh0rd.org - dump.git/blob - restore/tape.c
From Uwe Gohlke:
[dump.git] / restore / tape.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43 #ifndef lint
44 static const char rcsid[] =
45 "$Id: tape.c,v 1.75 2003/10/26 16:05:48 stelian Exp $";
46 #endif /* not lint */
47
48 #include <config.h>
49 #include <compatlfs.h>
50 #include <sys/types.h>
51 #include <errno.h>
52 #include <compaterr.h>
53 #include <system.h>
54 #include <setjmp.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #include <sys/param.h>
61 #include <sys/file.h>
62 #include <sys/mtio.h>
63 #include <sys/stat.h>
64
65 #ifdef __linux__
66 #include <sys/time.h>
67 #include <time.h>
68 #ifdef HAVE_EXT2FS_EXT2_FS_H
69 #include <ext2fs/ext2_fs.h>
70 #else
71 #include <linux/ext2_fs.h>
72 #endif
73 #include <ext2fs/ext2fs.h>
74 #include <bsdcompat.h>
75 #else /* __linux__ */
76 #ifdef sunos
77 #define quad_t int64_t
78 #include <sys/time.h>
79 #include <sys/fcntl.h>
80 #include <bsdcompat.h>
81 #else
82 #include <ufs/ufs/dinode.h>
83 #endif
84 #endif /* __linux__ */
85 #ifdef DUMP_MACOSX
86 #include "darwin.h"
87 #endif
88 #include <protocols/dumprestore.h>
89
90 #ifdef HAVE_ZLIB
91 #include <zlib.h>
92 #endif /* HAVE_ZLIB */
93
94 #ifdef HAVE_BZLIB
95 #include <bzlib.h>
96 #endif /* HAVE_BZLIB */
97
98 #ifdef HAVE_LZO
99 #include <minilzo.h>
100 #endif /* HAVE_LZO */
101
102 #include "restore.h"
103 #include "extern.h"
104 #include "pathnames.h"
105
106 #ifdef USE_QFA
107 int noresyncmesg = 0;
108 #endif /* USE_QFA */
109 static long fssize = MAXBSIZE;
110 static int mt = -1;
111 int pipein = 0;
112 static int magtapein = 0; /* input is from magtape */
113 static char magtape[MAXPATHLEN];
114 static char magtapeprefix[MAXPATHLEN];
115 static int blkcnt;
116 static int numtrec;
117 static char *tapebuf; /* input buffer for read */
118 static int bufsize; /* buffer size without prefix */
119 static char *tbufptr = NULL; /* active tape buffer */
120 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
121 static char *comprbuf; /* uncompress work buf */
122 static size_t comprlen; /* size including prefix */
123 #endif
124 static union u_spcl endoftapemark;
125 static long blksread; /* blocks read since last header */
126 static long tpblksread = 0; /* TP_BSIZE blocks read */
127 static long tapesread;
128 static sigjmp_buf restart;
129 static int gettingfile = 0; /* restart has a valid frame */
130 char *host = NULL;
131
132 static int ofile;
133 static char *map;
134 static char lnkbuf[MAXPATHLEN + 1];
135 static int pathlen;
136
137 int oldinofmt; /* old inode format conversion required */
138 int Bcvt; /* Swap Bytes (for CCI or sun) */
139 static int Qcvt; /* Swap quads (for sun) */
140
141 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
142
143 static void accthdr __P((struct s_spcl *));
144 static int checksum __P((int *));
145 static void findinode __P((struct s_spcl *));
146 static void findtapeblksize __P((void));
147 static int gethead __P((struct s_spcl *));
148 static int converthead __P((struct s_spcl *));
149 static void converttapebuf __P((struct tapebuf *));
150 static void readtape __P((char *));
151 static void setdumpnum __P((void));
152 #ifdef DUMP_MACOSX
153 static void xtrfilefinderinfo __P((char *, size_t));
154 #endif
155
156 static u_int swabi __P((u_int));
157 #if 0
158 static u_long swabl __P((u_long));
159 #endif
160 static u_char *swab64 __P((u_char *, int));
161 static u_char *swab32 __P((u_char *, int));
162 static u_char *swab16 __P((u_char *, int));
163 static void terminateinput __P((void));
164 static void xtrfile __P((char *, size_t));
165 static void xtrlnkfile __P((char *, size_t));
166 static void xtrlnkskip __P((char *, size_t));
167 static void xtrmap __P((char *, size_t));
168 static void xtrmapskip __P((char *, size_t));
169 static void xtrskip __P((char *, size_t));
170 static void setmagtapein __P((void));
171
172 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
173 static void newcomprbuf __P((int));
174 static void (*readtape_func) __P((char *));
175 static void readtape_set __P((char *));
176 static void readtape_uncompr __P((char *));
177 static void readtape_comprfile __P((char *));
178 static void readtape_comprtape __P((char *));
179 static char *decompress_tapebuf __P((struct tapebuf *, int));
180 static void msg_read_error __P((char *));
181 #endif
182 static int read_a_block __P((int, char *, size_t, long *));
183 #define PREFIXSIZE sizeof(struct tapebuf)
184
185 #define COMPARE_ONTHEFLY 1
186
187 #if COMPARE_ONTHEFLY
188 static int ifile; /* input file for compare */
189 static int cmperror; /* compare error */
190 static void xtrcmpfile __P((char *, size_t));
191 static void xtrcmpskip __P((char *, size_t));
192 #endif
193
194 static int readmapflag;
195 static int readingmaps; /* set to 1 while reading the maps */
196
197 #ifdef DUMP_MACOSX
198 static DumpFinderInfo gFndrInfo;
199 #endif
200
201 /*
202 * Set up an input source. This is called from main.c before setup() is.
203 */
204 void
205 setinput(char *source)
206 {
207 int i;
208 char *n;
209
210 FLUSHTAPEBUF();
211 if (bflag)
212 newtapebuf(ntrec);
213 else
214 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
215 terminal = stdin;
216
217 #ifdef RRESTORE
218 if ((n = strchr(source, ':'))) {
219 for (i = 0; i < (n - source); i++) {
220 if (source[i] == '/')
221 break;
222 }
223 if (source[i] != '/') {
224 host = source;
225 source = strchr(host, ':');
226 *source++ = '\0';
227 if (rmthost(host) == 0)
228 exit(1);
229 }
230 } else
231 #endif
232 if (strcmp(source, "-") == 0) {
233 /*
234 * Since input is coming from a pipe we must establish
235 * our own connection to the terminal.
236 */
237 terminal = fopen(_PATH_TTY, "r");
238 if (terminal == NULL) {
239 warn("cannot open %s", _PATH_TTY);
240 terminal = fopen(_PATH_DEVNULL, "r");
241 if (terminal == NULL)
242 err(1, "cannot open %s", _PATH_DEVNULL);
243 }
244 pipein++;
245 }
246 setuid(getuid()); /* no longer need or want root privileges */
247 if (Mflag) {
248 strncpy(magtapeprefix, source, MAXPATHLEN);
249 magtapeprefix[MAXPATHLEN-1] = '\0';
250 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
251 }
252 else
253 strncpy(magtape, source, MAXPATHLEN);
254 magtape[MAXPATHLEN - 1] = '\0';
255 }
256
257 void
258 newtapebuf(long size)
259 {
260 static int tapebufsize = -1;
261
262 ntrec = size;
263 bufsize = ntrec * TP_BSIZE;
264 if (size <= tapebufsize)
265 return;
266 if (tapebuf != NULL)
267 free(tapebuf);
268 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
269 if (tapebuf == NULL)
270 errx(1, "Cannot allocate space for tape buffer");
271 tapebufsize = size;
272 }
273
274 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
275 static void
276 newcomprbuf(int size)
277 {
278 size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
279 if (buf_size <= comprlen)
280 return;
281 comprlen = buf_size;
282 if (comprbuf != NULL)
283 free(comprbuf);
284 comprbuf = malloc(comprlen);
285 if (comprbuf == NULL)
286 errx(1, "Cannot allocate space for decompress buffer");
287 }
288 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
289
290 /*
291 * Verify that the tape drive can be accessed and
292 * that it actually is a dump tape.
293 */
294 void
295 setup(void)
296 {
297 int i, j, *ip, bot_code;
298 struct STAT stbuf;
299 char *temptape;
300
301 Vprintf(stdout, "Verify tape and initialize maps\n");
302 if (Afile == NULL && bot_script) {
303 msg("Launching %s\n", bot_script);
304 bot_code = system_command(bot_script, magtape, 1);
305 if (bot_code != 0 && bot_code != 1) {
306 msg("Restore aborted by the beginning of tape script\n");
307 exit(1);
308 }
309 }
310
311 if (Afile)
312 temptape = Afile;
313 else
314 temptape = magtape;
315
316 #ifdef RRESTORE
317 if (!Afile && host)
318 mt = rmtopen(temptape, O_RDONLY);
319 else
320 #endif
321 if (pipein)
322 mt = 0;
323 else
324 mt = OPEN(temptape, O_RDONLY, 0);
325 if (mt < 0)
326 err(1, "%s", temptape);
327 if (!Afile) {
328 volno = 1;
329 setmagtapein();
330 setdumpnum();
331 }
332 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
333 readtape_func = readtape_set;
334 #if defined(HAVE_LZO)
335 if (lzo_init() != LZO_E_OK) {
336 msg("internal error - lzo_init failed \n");
337 exit(1);
338 }
339 #endif
340 #endif
341 FLUSHTAPEBUF();
342 findtapeblksize();
343 if (gethead(&spcl) == FAIL) {
344 blkcnt--; /* push back this block */
345 blksread--;
346 tpblksread--;
347 cvtflag++;
348 if (gethead(&spcl) == FAIL)
349 errx(1, "Tape is not a dump tape");
350 fprintf(stderr, "Converting to new file system format.\n");
351 }
352
353 if (zflag) {
354 fprintf(stderr, "Dump tape is compressed.\n");
355 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
356 errx(1,"This restore version doesn't support decompression");
357 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
358 }
359 if (pipein) {
360 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
361 endoftapemark.s_spcl.c_type = TS_END;
362 ip = (int *)&endoftapemark;
363 j = sizeof(union u_spcl) / sizeof(int);
364 i = 0;
365 do
366 i += *ip++;
367 while (--j);
368 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
369 }
370 if (vflag || command == 't' || command == 'C')
371 printdumpinfo();
372 #ifdef USE_QFA
373 if (tapeposflag && (unsigned long)spcl.c_date != qfadumpdate)
374 errx(1, "different QFA/dumpdates detected\n");
375 #endif
376 if (filesys[0] == '\0') {
377 char *dirptr;
378 strncpy(filesys, spcl.c_filesys, NAMELEN);
379 filesys[NAMELEN - 1] = '\0';
380 dirptr = strstr(filesys, " (dir");
381 if (dirptr != NULL)
382 *dirptr = '\0';
383 }
384 dumptime = spcl.c_ddate;
385 dumpdate = spcl.c_date;
386 if (STAT(".", &stbuf) < 0)
387 err(1, "cannot stat .");
388 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
389 fssize = TP_BSIZE;
390 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
391 fssize = stbuf.st_blksize;
392 if (((fssize - 1) & fssize) != 0)
393 errx(1, "bad block size %ld", fssize);
394 if (spcl.c_volume != 1)
395 errx(1, "Tape is not volume 1 of the dump");
396 if (gethead(&spcl) == FAIL) {
397 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
398 panic("no header after volume mark!\n");
399 }
400 readingmaps = 1;
401 findinode(&spcl);
402 if (spcl.c_type != TS_CLRI)
403 errx(1, "Cannot find file removal list");
404 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
405 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
406 if (map == NULL)
407 errx(1, "no memory for active inode map");
408 usedinomap = map;
409 curfile.action = USING;
410 getfile(xtrmap, xtrmapskip);
411 while (spcl.c_type == TS_ADDR) {
412 /* Recompute maxino and the map */
413 dump_ino_t oldmaxino = maxino;
414 maxino += (spcl.c_count * TP_BSIZE * NBBY) + 1;
415 resizemaps(oldmaxino, maxino);
416
417 spcl.c_dinode.di_size = spcl.c_count * TP_BSIZE;
418 getfile(xtrmap, xtrmapskip);
419 }
420 Dprintf(stdout, "maxino = %lu\n", (unsigned long)maxino);
421 if (spcl.c_type != TS_BITS)
422 errx(1, "Cannot find file dump list");
423 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
424 if (map == (char *)NULL)
425 errx(1, "no memory for file dump list");
426 dumpmap = map;
427 curfile.action = USING;
428 getfile(xtrmap, xtrmapskip);
429 while (spcl.c_type == TS_ADDR) {
430 spcl.c_dinode.di_size = spcl.c_count * TP_BSIZE;
431 getfile(xtrmap, xtrmapskip);
432 }
433 /*
434 * If there may be whiteout entries on the tape, pretend that the
435 * whiteout inode exists, so that the whiteout entries can be
436 * extracted.
437 */
438 if (oldinofmt == 0)
439 SETINO(WINO, dumpmap);
440 readingmaps = 0;
441 findinode(&spcl);
442 }
443
444 /*
445 * Prompt user to load a new dump volume.
446 * "Nextvol" is the next suggested volume to use.
447 * This suggested volume is enforced when doing full
448 * or incremental restores, but can be overridden by
449 * the user when only extracting a subset of the files.
450 */
451 void
452 getvol(long nextvol)
453 {
454 long newvol = 0, wantnext = 0, i;
455 long saved_blksread = 0, saved_tpblksread = 0;
456 union u_spcl tmpspcl;
457 # define tmpbuf tmpspcl.s_spcl
458 char buf[TP_BSIZE];
459 int haderror = 0, bot_code = 1;
460
461 if (nextvol == 1) {
462 tapesread = 0;
463 gettingfile = 0;
464 tpblksread = 0;
465 blksread = 0;
466 }
467 if (pipein) {
468 if (nextvol != 1)
469 panic("Changing volumes on pipe input?\n");
470 if (volno == 1)
471 return;
472 goto gethdr;
473 }
474 saved_blksread = blksread;
475 saved_tpblksread = tpblksread;
476 #if defined(USE_QFA) && defined(sunos)
477 if (createtapeposflag || tapeposflag)
478 close(fdsmtc);
479 #endif
480 again:
481 if (pipein)
482 exit(1); /* pipes do not get a second chance */
483 if (aflag || curfile.action != SKIP) {
484 newvol = nextvol;
485 wantnext = 1;
486 } else {
487 newvol = 0;
488 wantnext = 0;
489 }
490 while (newvol <= 0) {
491 if (tapesread == 0) {
492 fprintf(stderr, "%s%s%s%s%s",
493 "You have not read any volumes yet.\n",
494 "Unless you know which volume your",
495 " file(s) are on you should start\n",
496 "with the last volume and work",
497 " towards the first.\n");
498 } else {
499 fprintf(stderr, "You have read volumes");
500 strcpy(buf, ": ");
501 for (i = 1; i < 32; i++)
502 if (tapesread & (1 << i)) {
503 fprintf(stderr, "%s%ld", buf, (long)i);
504 strcpy(buf, ", ");
505 }
506 fprintf(stderr, "\n");
507 }
508 do {
509 fprintf(stderr, "Specify next volume # (none if no more volumes): ");
510 (void) fflush(stderr);
511 (void) fgets(buf, TP_BSIZE, terminal);
512 } while (!feof(terminal) && buf[0] == '\n');
513 if (feof(terminal))
514 exit(1);
515 if (!strcmp(buf, "none\n")) {
516 terminateinput();
517 return;
518 }
519 newvol = atoi(buf);
520 if (newvol <= 0) {
521 fprintf(stderr,
522 "Volume numbers are positive numerics\n");
523 }
524 }
525 if (newvol == volno) {
526 tapesread |= 1 << volno;
527 #if defined(USE_QFA) && defined(sunos)
528 if (createtapeposflag || tapeposflag) {
529 if (OpenSMTCmt(magtape) < 0) {
530 volno = -1;
531 haderror = 1;
532 goto again;
533 }
534 }
535 #endif /* USE_QFA */
536 return;
537 }
538 closemt();
539
540 /*
541 * if using an archive file, reset its name so readtape()
542 * could properly use remote access.
543 */
544 Afile = NULL;
545
546 if (Mflag) {
547 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
548 magtape[MAXPATHLEN - 1] = '\0';
549 }
550 if (bot_script && !haderror) {
551 msg("Launching %s\n", bot_script);
552 bot_code = system_command(bot_script, magtape, newvol);
553 if (bot_code != 0 && bot_code != 1) {
554 msg("Restore aborted by the beginning of tape script\n");
555 exit(1);
556 }
557 }
558 if (haderror || (bot_code && !Mflag)) {
559 haderror = 0;
560 #ifdef sunos
561 fprintf(stderr, "Mount volume %ld\n", (long)newvol);
562 #else
563 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
564 #endif
565 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
566 #ifdef sunos
567 fprintf(stderr, "then enter volume name (default: %s) ", magtape);
568 #else
569 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
570 #endif
571 (void) fflush(stderr);
572 (void) fgets(buf, TP_BSIZE, terminal);
573 if (feof(terminal))
574 exit(1);
575 if (!strcmp(buf, "none\n")) {
576 terminateinput();
577 return;
578 }
579 if (buf[0] != '\n') {
580 char *pos;
581 (void) strncpy(magtape, buf, sizeof(magtape));
582 magtape[sizeof(magtape) - 1] = '\0';
583 if ((pos = strchr(magtape, '\n')))
584 magtape[pos - magtape] = '\0';
585 }
586 }
587 #if defined(USE_QFA) && defined(sunos)
588 if (createtapeposflag || tapeposflag) {
589 if (OpenSMTCmt(magtape) < 0) {
590 volno = -1;
591 haderror = 1;
592 goto again;
593 }
594 }
595 #endif /* USE_QFA */
596 #ifdef RRESTORE
597 if (host)
598 mt = rmtopen(magtape, O_RDONLY);
599 else
600 #endif
601 mt = OPEN(magtape, O_RDONLY, 0);
602
603 if (mt == -1) {
604 fprintf(stderr, "Cannot open %s\n", magtape);
605 volno = -1;
606 haderror = 1;
607 goto again;
608 }
609 gethdr:
610 setmagtapein();
611 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
612 readtape_func = readtape_set;
613 #endif
614 volno = newvol;
615 setdumpnum();
616 FLUSHTAPEBUF();
617 findtapeblksize();
618 if (gethead(&tmpbuf) == FAIL) {
619 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
620 fprintf(stderr, "tape is not dump tape\n");
621 volno = 0;
622 haderror = 1;
623 blksread = saved_blksread;
624 tpblksread = saved_tpblksread;
625 goto again;
626 }
627 if (tmpbuf.c_volume != volno) {
628 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
629 volno = 0;
630 haderror = 1;
631 blksread = saved_blksread;
632 tpblksread = saved_tpblksread;
633 goto again;
634 }
635 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
636 fprintf(stderr, "Wrong dump date\n\tgot: %s",
637 #ifdef sunos
638 ctime(&tmpbuf.c_date));
639 #else
640 ctime4(&tmpbuf.c_date));
641 #endif
642 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
643 volno = 0;
644 haderror = 1;
645 blksread = saved_blksread;
646 tpblksread = saved_tpblksread;
647 goto again;
648 }
649 tapesread |= 1 << volno;
650 /*
651 * If continuing from the previous volume, skip over any
652 * blocks read already at the end of the previous volume.
653 *
654 * If coming to this volume at random, skip to the beginning
655 * of the next record.
656 */
657 if (zflag) {
658 fprintf(stderr, "Dump tape is compressed.\n");
659 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
660 errx(1,"This restore version doesn't support decompression");
661 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
662 }
663 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
664 tpblksread - 1, (long)tmpbuf.c_firstrec);
665 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
666 if (!wantnext) {
667 tpblksread = tmpbuf.c_firstrec + 1;
668 for (i = tmpbuf.c_count; i > 0; i--)
669 readtape(buf);
670 } else if (tmpbuf.c_firstrec > 0 &&
671 tmpbuf.c_firstrec < tpblksread - 1) {
672 /*
673 * -1 since we've read the volume header
674 */
675 i = tpblksread - tmpbuf.c_firstrec - 1;
676 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
677 (long)i, i > 1 ? "s" : "");
678 while (--i >= 0)
679 readtape(buf);
680 }
681 }
682 if (curfile.action == USING) {
683 if (volno == 1)
684 panic("active file into volume 1\n");
685 return;
686 }
687 /*
688 * Skip up to the beginning of the next record
689 */
690 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
691 for (i = tmpbuf.c_count; i > 0; i--)
692 readtape(buf);
693 (void) gethead(&spcl);
694 findinode(&spcl);
695 if (gettingfile) {
696 gettingfile = 0;
697 siglongjmp(restart, 1);
698 }
699 }
700
701 /*
702 * Handle unexpected EOF.
703 */
704 static void
705 terminateinput(void)
706 {
707
708 if (gettingfile && curfile.action == USING) {
709 printf("Warning: %s %s\n",
710 "End-of-input encountered while extracting", curfile.name);
711 }
712 curfile.name = "<name unknown>";
713 curfile.action = UNKNOWN;
714 curfile.dip = NULL;
715 curfile.ino = maxino;
716 if (gettingfile) {
717 gettingfile = 0;
718 siglongjmp(restart, 1);
719 }
720 }
721
722 /*
723 * handle multiple dumps per tape by skipping forward to the
724 * appropriate one.
725 */
726 static void
727 setdumpnum(void)
728 {
729 struct mtop tcom;
730
731 if (dumpnum == 1 || volno != 1)
732 return;
733 if (pipein)
734 errx(1, "Cannot have multiple dumps on pipe input");
735 tcom.mt_op = MTFSF;
736 tcom.mt_count = dumpnum - 1;
737 #ifdef RRESTORE
738 if (host) {
739 if (rmtioctl(MTFSF, dumpnum - 1) < 0) {
740 fprintf(stderr, "rmtioctl MTFSF: %s\n", strerror(errno));
741 exit(1);
742 }
743 } else
744 #endif
745 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0) {
746 fprintf(stderr, "rmtioctl MTFSF: %s\n", strerror(errno));
747 exit(1);
748 /* warn("ioctl MTFSF"); */
749 }
750 }
751
752 void
753 printdumpinfo(void)
754 {
755 #ifdef sunos
756 Vprintf(stdout, "Dump date: %s", ctime(&spcl.c_date));
757 Vprintf(stdout, "Dumped from: %s",
758 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
759 if (spcl.c_host[0] == '\0')
760 return;
761 Vprintf(stdout, "Level %d dump of %s on %s:%s\n",
762 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
763 Vprintf(stdout, "Label: %s\n", spcl.c_label);
764 #else
765 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
766 fprintf(stdout, "Dumped from: %s",
767 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
768 if (spcl.c_host[0] == '\0')
769 return;
770 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
771 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
772 fprintf(stdout, "Label: %s\n", spcl.c_label);
773 #endif
774 }
775
776 void
777 printvolinfo(void)
778 {
779 int i;
780
781 if (volinfo[1] == ROOTINO) {
782 printf("Starting inode numbers by volume:\n");
783 for (i = 1; i < (int)TP_NINOS && volinfo[i] != 0; ++i)
784 printf("\tVolume %d: %lu\n", i, (unsigned long)volinfo[i]);
785 }
786 }
787
788
789 int
790 extractfile(struct entry *ep, int doremove)
791 {
792 unsigned int flags;
793 mode_t mode;
794 struct timeval timep[2];
795 char *name = myname(ep);
796
797 /* If removal is requested (-r mode) do remove it unless
798 * we are extracting a metadata only inode */
799 if (spcl.c_flags & DR_METAONLY) {
800 Vprintf(stdout, "file %s is metadata only\n", name);
801 }
802 else {
803 if (doremove) {
804 removeleaf(ep);
805 ep->e_flags &= ~REMOVED;
806 }
807 }
808
809 curfile.name = name;
810 curfile.action = USING;
811 #if defined(__linux__) || defined(sunos)
812 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
813 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
814 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
815 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
816 #else /* __linux__ || sunos */
817 timep[0].tv_sec = curfile.dip->di_atime;
818 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
819 timep[1].tv_sec = curfile.dip->di_mtime;
820 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
821 #endif /* __linux__ */
822 mode = curfile.dip->di_mode;
823 flags = curfile.dip->di_flags;
824 switch (mode & IFMT) {
825
826 default:
827 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
828 skipfile();
829 return (FAIL);
830
831 case IFSOCK:
832 Vprintf(stdout, "skipped socket %s\n", name);
833 skipfile();
834 return (GOOD);
835
836 case IFDIR:
837 if (mflag) {
838 if (ep == NULL || ep->e_flags & EXTRACT)
839 panic("unextracted directory %s\n", name);
840 skipfile();
841 return (GOOD);
842 }
843 Vprintf(stdout, "extract file %s\n", name);
844 return (genliteraldir(name, curfile.ino));
845
846 case IFLNK:
847 {
848 #ifdef HAVE_LCHOWN
849 uid_t luid = curfile.dip->di_uid;
850 gid_t lgid = curfile.dip->di_gid;
851 #endif
852 if (! (spcl.c_flags & DR_METAONLY)) {
853 lnkbuf[0] = '\0';
854 pathlen = 0;
855 getfile(xtrlnkfile, xtrlnkskip);
856 if (pathlen == 0) {
857 Vprintf(stdout,
858 "%s: zero length symbolic link (ignored)\n", name);
859 return (GOOD);
860 }
861 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
862 return (FAIL);
863 }
864 else
865 skipfile();
866
867 #ifdef HAVE_LCHOWN
868 (void) lchown(name, luid, lgid);
869 #endif
870 return (GOOD);
871 }
872
873 case IFIFO:
874 Vprintf(stdout, "extract fifo %s\n", name);
875 if (Nflag) {
876 skipfile();
877 return (GOOD);
878 }
879 if (! (spcl.c_flags & DR_METAONLY)) {
880 if (uflag && !Nflag)
881 (void)unlink(name);
882 if (mkfifo(name, mode) < 0) {
883 warn("%s: cannot create fifo", name);
884 skipfile();
885 return (FAIL);
886 }
887 }
888 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
889 (void) chmod(name, mode);
890 if (flags)
891 #ifdef __linux__
892 (void) fsetflags(name, flags);
893 #else
894 #ifdef sunos
895 {
896 warn("%s: cannot call chflags", name);
897 /* (void) chflags(name, flags); */
898 }
899 #else
900 (void) chflags(name, flags);
901 #endif
902 #endif
903 skipfile();
904 utimes(name, timep);
905 return (GOOD);
906
907 case IFCHR:
908 case IFBLK:
909 Vprintf(stdout, "extract special file %s\n", name);
910 if (Nflag) {
911 skipfile();
912 return (GOOD);
913 }
914 if (! (spcl.c_flags & DR_METAONLY)) {
915 if (uflag)
916 (void)unlink(name);
917 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
918 warn("%s: cannot create special file", name);
919 skipfile();
920 return (FAIL);
921 }
922 }
923 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
924 (void) chmod(name, mode);
925 if (flags)
926 #ifdef __linux__
927 {
928 warn("%s: fsetflags called on a special file", name);
929 (void) fsetflags(name, flags);
930 }
931 #else
932 #ifdef sunos
933 {
934 warn("%s: cannot call chflags on a special file", name);
935 /* (void) chflags(name, flags); */
936 }
937 #else
938 {
939 warn("%s: chflags called on a special file", name);
940 (void) chflags(name, flags);
941 }
942 #endif
943 #endif
944 skipfile();
945 utimes(name, timep);
946 return (GOOD);
947
948 case IFREG:
949 {
950 uid_t luid = curfile.dip->di_uid;
951 gid_t lgid = curfile.dip->di_gid;
952
953 Vprintf(stdout, "extract file %s\n", name);
954 if (Nflag) {
955 skipfile();
956 return (GOOD);
957 }
958 if (! (spcl.c_flags & DR_METAONLY)) {
959 if (uflag)
960 (void)unlink(name);
961 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
962 0666)) < 0) {
963 warn("%s: cannot create file", name);
964 skipfile();
965 return (FAIL);
966 }
967 getfile(xtrfile, xtrskip);
968 (void) close(ofile);
969 }
970 else
971 skipfile();
972 (void) chown(name, luid, lgid);
973 (void) chmod(name, mode);
974 if (flags)
975 #ifdef __linux__
976 (void) fsetflags(name, flags);
977 #else
978 #ifdef sunos
979 {
980 warn("%s: cannot call chflags", name);
981 /* (void) chflags(name, flags); */
982 }
983 #else
984 (void) chflags(name, flags);
985 #endif
986 #endif
987 utimes(name, timep);
988 return (GOOD);
989 }
990 }
991 /* NOTREACHED */
992 }
993
994 #ifdef DUMP_MACOSX
995 int
996 extractfinderinfoufs(char *name)
997 {
998 int err;
999 char oFileRsrc[MAXPATHLEN];
1000 int flags;
1001 mode_t mode;
1002 struct timeval timep[2];
1003 struct entry *ep;
1004 int sz;
1005 attrinfo_block_t gABuf;
1006 u_int32_t uid;
1007 u_int32_t gid;
1008 char path[MAXPATHLEN], fname[MAXPATHLEN];
1009
1010 curfile.name = name;
1011 curfile.action = USING;
1012 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
1013 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
1014 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
1015 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
1016 mode = curfile.dip->di_mode;
1017 flags = curfile.dip->di_flags;
1018 uid = curfile.dip->di_uid;
1019 gid = curfile.dip->di_gid;
1020
1021 switch (mode & IFMT) {
1022
1023 default:
1024 fprintf(stderr, "%s: (extr. finfoufs) unknown file mode 0%o\n", name, mode);
1025 skipfile();
1026 return (FAIL);
1027
1028 case IFDIR:
1029 fprintf(stderr, "%s: (extr. finfoufs[IFDIR]) unknown file mode 0%o\n", name, mode);
1030 skipfile();
1031 return (FAIL);
1032
1033 case IFLNK:
1034 skipfile();
1035 return (GOOD);
1036
1037 case IFREG:
1038 Vprintf(stdout, "extract finderinfo file %s\n", name);
1039 if (Nflag) {
1040 skipfile();
1041 return (GOOD);
1042 }
1043 getfile(xtrfilefinderinfo, xtrskip);
1044
1045 GetPathFile(name, path, fname);
1046 strcpy(oFileRsrc, path);
1047 strcat(oFileRsrc, "._");
1048 strcat(oFileRsrc, fname);
1049
1050 if ((err = CreateAppleDoubleFileRes(oFileRsrc, &gFndrInfo.fndrinfo,
1051 mode, flags, timep, uid, gid)) != 0) {
1052 fprintf(stderr, "%s: cannot create finderinfo: %s\n",
1053 name, strerror(errno));
1054 skipfile();
1055 return (FAIL);
1056 }
1057 return (GOOD);
1058 }
1059 /* NOTREACHED */
1060 }
1061
1062
1063 int
1064 extractresourceufs(char *name)
1065 {
1066 char oFileRsrc[MAXPATHLEN];
1067 int flags;
1068 mode_t mode;
1069 struct timeval timep[2];
1070 char path[MAXPATHLEN], fname[MAXPATHLEN];
1071 ASDHeaderPtr hp;
1072 ASDEntryPtr ep;
1073 u_long loff;
1074 u_int32_t uid;
1075 u_int32_t gid;
1076 u_int64_t di_size;
1077 char *p;
1078 char buf[1024];
1079
1080 curfile.name = name;
1081 curfile.action = USING;
1082 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
1083 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
1084 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
1085 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
1086 mode = curfile.dip->di_mode;
1087 flags = curfile.dip->di_flags;
1088 uid = curfile.dip->di_uid;
1089 gid = curfile.dip->di_gid;
1090 di_size = curfile.dip->di_size;
1091
1092 switch (mode & IFMT) {
1093
1094 default:
1095 fprintf(stderr, "%s: (extr. resufs) unknown file mode 0%o\n", name, mode);
1096 skipfile();
1097 return (FAIL);
1098
1099 case IFDIR:
1100 fprintf(stderr, "%s: (extr. resufs [IFDIR]) unknown file mode 0%o\n", name, mode);
1101 skipfile();
1102 return (FAIL);
1103
1104 case IFLNK:
1105 skipfile();
1106 return (GOOD);
1107
1108 case IFREG:
1109 Vprintf(stdout, "extract resource file %s\n", name);
1110 if (Nflag) {
1111 skipfile();
1112 return (GOOD);
1113 }
1114
1115 GetPathFile(name, path, fname);
1116 strcpy(oFileRsrc, path);
1117 strcat(oFileRsrc, "._");
1118 strcat(oFileRsrc, fname);
1119
1120 if ((ofile = open(oFileRsrc, O_RDONLY, 0)) < 0) {
1121 fprintf(stderr, "%s: cannot read finderinfo: %s\n",
1122 name, strerror(errno));
1123 skipfile();
1124 return (FAIL);
1125 }
1126 read(ofile, buf, 70);
1127 (void) close(ofile);
1128 p = buf;
1129 hp = (ASDHeaderPtr)p;
1130 /* the header */
1131 hp->entries++;
1132 p += sizeof(ASDHeader) - CORRECT;
1133 ep = (ASDEntryPtr)p;
1134 /* the finderinfo entry */
1135 ep->offset += sizeof(ASDEntry);
1136 loff = ep->offset;
1137
1138 p += sizeof(ASDEntry);
1139 /* the finderinfo data */
1140 bcopy(p, p + sizeof(ASDEntry), INFOLEN);
1141 ep = (ASDEntryPtr)p;
1142 /* the new resourcefork entry */
1143 ep->entryID = EntryRSRCFork;
1144 ep->offset = loff + INFOLEN;
1145 ep->len = di_size;
1146 /* write the new appledouble entries to the file */
1147 if ((ofile = open(oFileRsrc, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
1148 fprintf(stderr, "%s: cannot create resource file: %s\n",
1149 name, strerror(errno));
1150 skipfile();
1151 return (FAIL);
1152 }
1153 write(ofile, buf, 70 + sizeof(ASDEntry));
1154 /* and add the resource data from tape */
1155 getfile(xtrfile, xtrskip);
1156
1157 (void) fchown(ofile, uid, gid);
1158 (void) fchmod(ofile, mode);
1159 (void) close(ofile);
1160 (void) fsetflags(oFileRsrc, flags);
1161 utimes(oFileRsrc, timep);
1162 return (GOOD);
1163 }
1164 /* NOTREACHED */
1165 }
1166 #endif /* DUMP_MACOSX */
1167
1168 /*
1169 * skip over bit maps on the tape
1170 */
1171 void
1172 skipmaps(void)
1173 {
1174
1175 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
1176 skipfile();
1177 }
1178
1179 /*
1180 * skip over a file on the tape
1181 */
1182 void
1183 skipfile(void)
1184 {
1185
1186 curfile.action = SKIP;
1187 getfile(xtrnull, xtrnull);
1188 }
1189
1190 /*
1191 * Extract a file from the tape.
1192 * When an allocated block is found it is passed to the fill function;
1193 * when an unallocated block (hole) is found, a zeroed buffer is passed
1194 * to the skip function.
1195 */
1196 void
1197 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
1198 {
1199 int i;
1200 volatile int curblk = 0;
1201 volatile quad_t size = spcl.c_dinode.di_size;
1202 volatile int last_write_was_hole = 0;
1203 quad_t origsize = size;
1204 static char clearedbuf[MAXBSIZE];
1205 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
1206 char junk[TP_BSIZE];
1207
1208 if (spcl.c_type == TS_END)
1209 panic("ran off end of tape\n");
1210 if (spcl.c_magic != NFS_MAGIC)
1211 panic("not at beginning of a file\n");
1212 if (!gettingfile && setjmp(restart) != 0)
1213 return;
1214 gettingfile++;
1215 loop:
1216 for (i = 0; i < spcl.c_count; i++) {
1217 if (readmapflag || spcl.c_addr[i]) {
1218 readtape(&buf[curblk++][0]);
1219 if (curblk == fssize / TP_BSIZE) {
1220 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1221 fssize : (curblk - 1) * TP_BSIZE + size));
1222 curblk = 0;
1223 last_write_was_hole = 0;
1224 }
1225 } else {
1226 if (curblk > 0) {
1227 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1228 curblk * TP_BSIZE :
1229 (curblk - 1) * TP_BSIZE + size));
1230 curblk = 0;
1231 }
1232 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
1233 TP_BSIZE : size));
1234 last_write_was_hole = 1;
1235 }
1236 if ((size -= TP_BSIZE) <= 0) {
1237 for (i++; i < spcl.c_count; i++)
1238 if (readmapflag || spcl.c_addr[i])
1239 readtape(junk);
1240 break;
1241 }
1242 }
1243 if (gethead(&spcl) == GOOD && size > 0) {
1244 if (spcl.c_type == TS_ADDR)
1245 goto loop;
1246 Dprintf(stdout,
1247 "Missing address (header) block for %s at %ld blocks\n",
1248 curfile.name, (long)blksread);
1249 }
1250 if (curblk > 0) {
1251 (*fill)((char *)buf, (size_t)((curblk * TP_BSIZE) + size));
1252 last_write_was_hole = 0;
1253 }
1254 if (size > 0) {
1255 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
1256 while (size > 0) {
1257 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
1258 (*skip)(clearedbuf, skp);
1259 size -= skp;
1260 }
1261 last_write_was_hole = 1;
1262 }
1263 if (last_write_was_hole) {
1264 FTRUNCATE(ofile, origsize);
1265 }
1266 if (!readingmaps)
1267 findinode(&spcl);
1268 gettingfile = 0;
1269 }
1270
1271 /*
1272 * Write out the next block of a file.
1273 */
1274 static void
1275 xtrfile(char *buf, size_t size)
1276 {
1277
1278 if (Nflag)
1279 return;
1280 if (write(ofile, buf, (int) size) == -1)
1281 err(1, "write error extracting inode %lu, name %s\nwrite",
1282 (unsigned long)curfile.ino, curfile.name);
1283 }
1284
1285 #ifdef DUMP_MACOSX
1286 static void
1287 xtrfilefinderinfo(char *buf, size_t size)
1288 {
1289 if (Nflag)
1290 return;
1291 bcopy(buf, &gFndrInfo, size);
1292 }
1293 #endif /* DUMP_MACOSX */
1294
1295 /*
1296 * Skip over a hole in a file.
1297 */
1298 /* ARGSUSED */
1299 static void
1300 xtrskip(UNUSED(char *buf), size_t size)
1301 {
1302
1303 if (LSEEK(ofile, (OFF_T)size, SEEK_CUR) == -1)
1304 err(1, "seek error extracting inode %lu, name %s\nlseek",
1305 (unsigned long)curfile.ino, curfile.name);
1306 }
1307
1308 /*
1309 * Collect the next block of a symbolic link.
1310 */
1311 static void
1312 xtrlnkfile(char *buf, size_t size)
1313 {
1314
1315 pathlen += size;
1316 if (pathlen > MAXPATHLEN)
1317 errx(1, "symbolic link name: %s->%s%s; too long %d",
1318 curfile.name, lnkbuf, buf, pathlen);
1319 (void) strcat(lnkbuf, buf);
1320 }
1321
1322 /*
1323 * Skip over a hole in a symbolic link (should never happen).
1324 */
1325 /* ARGSUSED */
1326 static void
1327 xtrlnkskip(UNUSED(char *buf), UNUSED(size_t size))
1328 {
1329
1330 errx(1, "unallocated block in symbolic link %s", curfile.name);
1331 }
1332
1333 /*
1334 * Collect the next block of a bit map.
1335 */
1336 static void
1337 xtrmap(char *buf, size_t size)
1338 {
1339
1340 memmove(map, buf, size);
1341 map += size;
1342 }
1343
1344 /*
1345 * Skip over a hole in a bit map (should never happen).
1346 */
1347 /* ARGSUSED */
1348 static void
1349 xtrmapskip(UNUSED(char *buf), size_t size)
1350 {
1351
1352 panic("hole in map\n");
1353 map += size;
1354 }
1355
1356 /*
1357 * Noop, when an extraction function is not needed.
1358 */
1359 /* ARGSUSED */
1360 void
1361 xtrnull(UNUSED(char *buf), UNUSED(size_t size))
1362 {
1363
1364 return;
1365 }
1366
1367 #if COMPARE_ONTHEFLY
1368 /*
1369 * Compare the next block of a file.
1370 */
1371 static void
1372 xtrcmpfile(char *buf, size_t size)
1373 {
1374 static char cmpbuf[MAXBSIZE];
1375
1376 if (cmperror)
1377 return;
1378
1379 if (read(ifile, cmpbuf, size) != (ssize_t)size) {
1380 fprintf(stderr, "%s: size has changed.\n",
1381 curfile.name);
1382 cmperror = 1;
1383 return;
1384 }
1385
1386 if (memcmp(buf, cmpbuf, size) != 0) {
1387 fprintf(stderr, "%s: tape and disk copies are different\n",
1388 curfile.name);
1389 cmperror = 1;
1390 return;
1391 }
1392 }
1393
1394 /*
1395 * Skip over a hole in a file.
1396 */
1397 static void
1398 xtrcmpskip(UNUSED(char *buf), size_t size)
1399 {
1400 static char cmpbuf[MAXBSIZE];
1401 int i;
1402
1403 if (cmperror)
1404 return;
1405
1406 if (read(ifile, cmpbuf, size) != (ssize_t)size) {
1407 fprintf(stderr, "%s: size has changed.\n",
1408 curfile.name);
1409 cmperror = 1;
1410 return;
1411 }
1412
1413 for (i = 0; i < (int)size; ++i)
1414 if (cmpbuf[i] != '\0') {
1415 fprintf(stderr, "%s: tape and disk copies are different\n",
1416 curfile.name);
1417 cmperror = 1;
1418 return;
1419 }
1420 }
1421 #endif /* COMPARE_ONTHEFLY */
1422
1423 #if !COMPARE_ONTHEFLY
1424 static int
1425 do_cmpfiles(int fd_tape, int fd_disk, long size)
1426 {
1427 static char buf_tape[BUFSIZ];
1428 static char buf_disk[BUFSIZ];
1429 ssize_t n_tape;
1430 ssize_t n_disk;
1431
1432 while (size > 0) {
1433 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1434 close(fd_tape), close(fd_disk);
1435 panic("do_cmpfiles: unexpected EOF[1]");
1436 }
1437 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1438 close(fd_tape), close(fd_disk);
1439 panic("do_cmpfiles: unexpected EOF[2]");
1440 }
1441 if (n_tape != n_disk) {
1442 close(fd_tape), close(fd_disk);
1443 panic("do_cmpfiles: sizes different!");
1444 }
1445 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1446 size -= n_tape;
1447 }
1448 return (0);
1449 }
1450
1451 /* for debugging compare problems */
1452 #undef COMPARE_FAIL_KEEP_FILE
1453
1454 static
1455 #ifdef COMPARE_FAIL_KEEP_FILE
1456 /* return true if tapefile should be unlinked after compare */
1457 int
1458 #else
1459 void
1460 #endif
1461 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1462 {
1463 struct STAT sbuf_tape;
1464 int fd_tape, fd_disk;
1465
1466 if (STAT(tapefile, &sbuf_tape) != 0) {
1467 panic("Can't lstat tmp file %s: %s\n", tapefile,
1468 strerror(errno));
1469 do_compare_error;
1470 }
1471
1472 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1473 fprintf(stderr,
1474 "%s: size changed from %ld to %ld.\n",
1475 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1476 do_compare_error;
1477 #ifdef COMPARE_FAIL_KEEP_FILE
1478 return (0);
1479 #else
1480 return;
1481 #endif
1482 }
1483
1484 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1485 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1486 do_compare_error;
1487 }
1488 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1489 close(fd_tape);
1490 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1491 do_compare_error;
1492 }
1493
1494 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1495 fprintf(stderr, "%s: tape and disk copies are different\n",
1496 diskfile);
1497 close(fd_tape);
1498 close(fd_disk);
1499 do_compare_error;
1500 #ifdef COMPARE_FAIL_KEEP_FILE
1501 /* rename the file to live in /tmp */
1502 /* rename `tapefile' to /tmp/<basename of diskfile> */
1503 {
1504 char *p = strrchr(diskfile, '/');
1505 char newname[MAXPATHLEN];
1506 if (!p) {
1507 panic("can't find / in %s\n", diskfile);
1508 }
1509 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1510 if (rename(tapefile, newname)) {
1511 panic("rename from %s to %s failed: %s\n",
1512 tapefile, newname,
1513 strerror(errno));
1514 } else {
1515 fprintf(stderr, "*** %s saved to %s\n",
1516 tapefile, newname);
1517 }
1518 }
1519
1520 /* don't unlink the file (it's not there anymore */
1521 /* anyway) */
1522 return (0);
1523 #else
1524 return;
1525 #endif
1526 }
1527 close(fd_tape);
1528 close(fd_disk);
1529 #ifdef COMPARE_FAIL_KEEP_FILE
1530 return (1);
1531 #endif
1532 }
1533 #endif /* !COMPARE_ONTHEFLY */
1534
1535 #if !COMPARE_ONTHEFLY
1536 static char tmpfilename[MAXPATHLEN];
1537 #endif
1538
1539 void
1540 comparefile(char *name)
1541 {
1542 unsigned int mode;
1543 struct STAT sb;
1544 int r;
1545 #if !COMPARE_ONTHEFLY
1546 static char *tmpfile = NULL;
1547 struct STAT stemp;
1548 #endif
1549
1550 curfile.name = name;
1551 curfile.action = USING;
1552 mode = curfile.dip->di_mode;
1553
1554 if ((mode & IFMT) == IFSOCK) {
1555 Vprintf(stdout, "skipped socket %s\n", name);
1556 skipfile();
1557 return;
1558 }
1559
1560 if ((r = LSTAT(name, &sb)) != 0) {
1561 warn("%s: does not exist (%d)", name, r);
1562 do_compare_error;
1563 skipfile();
1564 return;
1565 }
1566
1567 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1568 (long)sb.st_size, mode);
1569
1570 if (sb.st_mode != mode) {
1571 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1572 name, mode & 07777, sb.st_mode & 07777);
1573 do_compare_error;
1574 }
1575 if (spcl.c_flags & DR_METAONLY) {
1576 skipfile();
1577 return;
1578 }
1579 switch (mode & IFMT) {
1580 default:
1581 skipfile();
1582 return;
1583
1584 case IFSOCK:
1585 skipfile();
1586 return;
1587
1588 case IFDIR:
1589 skipfile();
1590 return;
1591
1592 case IFLNK: {
1593 char lbuf[MAXPATHLEN + 1];
1594 int lsize;
1595
1596 if (!(sb.st_mode & S_IFLNK)) {
1597 fprintf(stderr, "%s: is no longer a symbolic link\n",
1598 name);
1599 do_compare_error;
1600 return;
1601 }
1602 lnkbuf[0] = '\0';
1603 pathlen = 0;
1604 getfile(xtrlnkfile, xtrlnkskip);
1605 if (pathlen == 0) {
1606 fprintf(stderr,
1607 "%s: zero length symbolic link (ignored)\n",
1608 name);
1609 do_compare_error;
1610 return;
1611 }
1612 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1613 panic("readlink of %s failed: %s", name,
1614 strerror(errno));
1615 do_compare_error;
1616 }
1617 lbuf[lsize] = 0;
1618 if (strcmp(lbuf, lnkbuf) != 0) {
1619 fprintf(stderr,
1620 "%s: symbolic link changed from %s to %s.\n",
1621 name, lnkbuf, lbuf);
1622 do_compare_error;
1623 return;
1624 }
1625 return;
1626 }
1627
1628 case IFCHR:
1629 case IFBLK:
1630 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1631 fprintf(stderr, "%s: no longer a special file\n",
1632 name);
1633 do_compare_error;
1634 skipfile();
1635 return;
1636 }
1637
1638 if (sb.st_rdev != (dev_t)curfile.dip->di_rdev) {
1639 fprintf(stderr,
1640 "%s: device changed from %d,%d to %d,%d.\n",
1641 name,
1642 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1643 (int)curfile.dip->di_rdev & 0xff,
1644 ((int)sb.st_rdev >> 8) & 0xff,
1645 (int)sb.st_rdev & 0xff);
1646 do_compare_error;
1647 }
1648 skipfile();
1649 return;
1650
1651 case IFREG:
1652 #if COMPARE_ONTHEFLY
1653 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1654 panic("Can't open %s: %s\n", name, strerror(errno));
1655 skipfile();
1656 do_compare_error;
1657 }
1658 else {
1659 cmperror = 0;
1660 getfile(xtrcmpfile, xtrcmpskip);
1661 if (!cmperror) {
1662 char c;
1663 if (read(ifile, &c, 1) != 0) {
1664 fprintf(stderr, "%s: size has changed.\n",
1665 name);
1666 cmperror = 1;
1667 }
1668 }
1669 if (cmperror)
1670 do_compare_error;
1671 close(ifile);
1672 }
1673 #else
1674 if (tmpfile == NULL) {
1675 /* argument to mktemp() must not be in RO space: */
1676 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1677 tmpfile = mktemp(&tmpfilename[0]);
1678 }
1679 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1680 panic("cannot delete tmp file %s: %s\n",
1681 tmpfile, strerror(errno));
1682 }
1683 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1684 panic("cannot create file temp file %s: %s\n",
1685 name, strerror(errno));
1686 }
1687 getfile(xtrfile, xtrskip);
1688 (void) close(ofile);
1689 #ifdef COMPARE_FAIL_KEEP_FILE
1690 if (cmpfiles(tmpfile, name, &sb))
1691 unlink(tmpfile);
1692 #else
1693 cmpfiles(tmpfile, name, &sb);
1694 unlink(tmpfile);
1695 #endif
1696 #endif /* COMPARE_ONTHEFLY */
1697 return;
1698 }
1699 /* NOTREACHED */
1700 }
1701
1702 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1703 static void (*readtape_func)(char *) = readtape_set;
1704
1705 /*
1706 * Read TP_BSIZE blocks from the input.
1707 * Handle read errors, and end of media.
1708 * Decompress compressed blocks.
1709 */
1710 static void
1711 readtape(char *buf)
1712 {
1713 (*readtape_func)(buf); /* call the actual processing routine */
1714 }
1715
1716 /*
1717 * Set function pointer for readtape() routine. zflag and magtapein must
1718 * be correctly set before the first call to readtape().
1719 */
1720 static void
1721 readtape_set(char *buf)
1722 {
1723 if (!zflag)
1724 readtape_func = readtape_uncompr;
1725 else {
1726 newcomprbuf(ntrec);
1727 if (magtapein)
1728 readtape_func = readtape_comprtape;
1729 else
1730 readtape_func = readtape_comprfile;
1731 }
1732 readtape(buf);
1733 }
1734
1735 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1736
1737 /*
1738 * This is the original readtape(), it's used for reading uncompressed input.
1739 * Read TP_BSIZE blocks from the input.
1740 * Handle read errors, and end of media.
1741 */
1742 static void
1743 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1744 readtape_uncompr(char *buf)
1745 #else
1746 readtape(char *buf)
1747 #endif
1748 {
1749 ssize_t rd, newvol, i;
1750 int cnt, seek_failed;
1751
1752 if (blkcnt < numtrec) {
1753 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1754 blksread++;
1755 tpblksread++;
1756 return;
1757 }
1758 tbufptr = tapebuf;
1759 for (i = 0; i < ntrec; i++)
1760 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1761 if (numtrec == 0)
1762 numtrec = ntrec;
1763 cnt = ntrec * TP_BSIZE;
1764 rd = 0;
1765 #ifdef USE_QFA
1766 if (createtapeposflag)
1767 (void)GetTapePos(&curtapepos);
1768 #endif
1769 getmore:
1770 #ifdef RRESTORE
1771 if (!Afile && host)
1772 i = rmtread(&tapebuf[rd], cnt);
1773 else
1774 #endif
1775 i = read(mt, &tapebuf[rd], cnt);
1776
1777 /*
1778 * Check for mid-tape short read error.
1779 * If found, skip rest of buffer and start with the next.
1780 */
1781 if (!pipein && numtrec < ntrec && i > 0) {
1782 Dprintf(stdout, "mid-media short read error.\n");
1783 numtrec = ntrec;
1784 }
1785 /*
1786 * Handle partial block read.
1787 */
1788 if (pipein && i == 0 && rd > 0)
1789 i = rd;
1790 else if (i > 0 && i != ntrec * TP_BSIZE) {
1791 if (pipein) {
1792 rd += i;
1793 cnt -= i;
1794 if (cnt > 0)
1795 goto getmore;
1796 i = rd;
1797 } else {
1798 /*
1799 * Short read. Process the blocks read.
1800 */
1801 if (i % TP_BSIZE != 0)
1802 Vprintf(stdout,
1803 "partial block read: %ld should be %ld\n",
1804 (long)i, ntrec * TP_BSIZE);
1805 numtrec = i / TP_BSIZE;
1806 }
1807 }
1808 /*
1809 * Handle read error.
1810 */
1811 if (i < 0) {
1812 fprintf(stderr, "Tape read error while ");
1813 switch (curfile.action) {
1814 default:
1815 fprintf(stderr, "trying to set up tape\n");
1816 break;
1817 case UNKNOWN:
1818 fprintf(stderr, "trying to resynchronize\n");
1819 break;
1820 case USING:
1821 fprintf(stderr, "restoring %s\n", curfile.name);
1822 break;
1823 case SKIP:
1824 fprintf(stderr, "skipping over inode %lu\n",
1825 (unsigned long)curfile.ino);
1826 break;
1827 }
1828 if (!yflag && !reply("continue"))
1829 exit(1);
1830 i = ntrec * TP_BSIZE;
1831 memset(tapebuf, 0, (size_t)i);
1832 #ifdef RRESTORE
1833 if (!Afile && host)
1834 seek_failed = (rmtseek(i, 1) < 0);
1835 else
1836 #endif
1837 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (OFF_T)-1);
1838
1839 if (seek_failed) {
1840 warn("continuation failed");
1841 if (!yflag && !reply("assume end-of-tape and continue"))
1842 exit(1);
1843 i = 0;
1844 }
1845 }
1846 /*
1847 * Handle end of tape.
1848 */
1849 if (i == 0) {
1850 Vprintf(stdout, "End-of-tape encountered\n");
1851 if (!pipein) {
1852 newvol = volno + 1;
1853 volno = 0;
1854 numtrec = 0;
1855 getvol(newvol);
1856 readtape(buf);
1857 return;
1858 }
1859 if (rd % TP_BSIZE != 0)
1860 panic("partial block read: %d should be %d\n",
1861 rd, ntrec * TP_BSIZE);
1862 terminateinput();
1863 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1864 }
1865 blkcnt = 0;
1866 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1867 blksread++;
1868 tpblksread++;
1869 }
1870
1871 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1872
1873 /*
1874 * Read a compressed format block from a file or pipe and uncompress it.
1875 * Attempt to handle read errors, and end of file.
1876 */
1877 static void
1878 readtape_comprfile(char *buf)
1879 {
1880 long rl, size, i, ret;
1881 int newvol;
1882 struct tapebuf *tpb;
1883
1884 if (blkcnt < numtrec) {
1885 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1886 blksread++;
1887 tpblksread++;
1888 return;
1889 }
1890 /* need to read the next block */
1891 tbufptr = tapebuf;
1892 for (i = 0; i < ntrec; i++)
1893 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1894 numtrec = ntrec;
1895 tpb = (struct tapebuf *) tapebuf;
1896
1897 /* read the block prefix */
1898 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1899 converttapebuf(tpb);
1900
1901 if (Vflag && (ret == 0 || rl < (int)PREFIXSIZE || tpb->length == 0))
1902 ret = 0;
1903 if (ret <= 0)
1904 goto readerr;
1905
1906 /* read the data */
1907 size = tpb->length;
1908 if (size > bufsize) {
1909 /* something's wrong */
1910 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1911 bufsize, size);
1912 size = bufsize;
1913 tpb->length = bufsize;
1914 }
1915 ret = read_a_block(mt, tpb->buf, size, &rl);
1916 if (ret <= 0)
1917 goto readerr;
1918
1919 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1920 if (tbufptr == NULL) {
1921 msg_read_error("File decompression error while");
1922 if (!yflag && !reply("continue"))
1923 exit(1);
1924 memset(tapebuf, 0, bufsize);
1925 tbufptr = tapebuf;
1926 }
1927
1928 blkcnt = 0;
1929 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1930 blksread++;
1931 tpblksread++;
1932 return;
1933
1934 readerr:
1935 /* Errors while reading from a file or pipe are catastrophic. Since
1936 * there are no block boundaries, it's impossible to bypass the
1937 * block in error and find the start of the next block.
1938 */
1939 if (ret == 0) {
1940 /* It's possible to have multiple input files using -M
1941 * and -f file1,file2...
1942 */
1943 Vprintf(stdout, "End-of-File encountered\n");
1944 if (!pipein) {
1945 newvol = volno + 1;
1946 volno = 0;
1947 numtrec = 0;
1948 getvol(newvol);
1949 readtape(buf);
1950 return;
1951 }
1952 }
1953 msg_read_error("Read error while");
1954 /* if (!yflag && !reply("continue")) */
1955 exit(1);
1956 }
1957
1958 /*
1959 * Read compressed data from a tape and uncompress it.
1960 * Handle read errors, and end of media.
1961 * Since a tape consists of separate physical blocks, we try
1962 * to recover from errors by repositioning the tape to the next
1963 * block.
1964 */
1965 static void
1966 readtape_comprtape(char *buf)
1967 {
1968 long rl, size, i;
1969 int ret, newvol;
1970 struct tapebuf *tpb;
1971 struct mtop tcom;
1972
1973 if (blkcnt < numtrec) {
1974 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1975 blksread++;
1976 tpblksread++;
1977 return;
1978 }
1979 /* need to read the next block */
1980 tbufptr = tapebuf;
1981 for (i = 0; i < ntrec; i++)
1982 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1983 numtrec = ntrec;
1984 tpb = (struct tapebuf *) tapebuf;
1985
1986 /* read the block */
1987 size = bufsize + PREFIXSIZE;
1988 ret = read_a_block(mt, tapebuf, size, &rl);
1989 if (ret <= 0)
1990 goto readerr;
1991
1992 converttapebuf(tpb);
1993 tbufptr = decompress_tapebuf(tpb, rl);
1994 if (tbufptr == NULL) {
1995 msg_read_error("Tape decompression error while");
1996 if (!yflag && !reply("continue"))
1997 exit(1);
1998 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1999 tbufptr = tapebuf;
2000 }
2001 goto moverecord;
2002
2003 readerr:
2004 /* Handle errors: EOT switches to the next volume, other errors
2005 * attempt to position the tape to the next block.
2006 */
2007 if (ret == 0) {
2008 Vprintf(stdout, "End-of-tape encountered\n");
2009 newvol = volno + 1;
2010 volno = 0;
2011 numtrec = 0;
2012 getvol(newvol);
2013 readtape(buf);
2014 return;
2015 }
2016
2017 msg_read_error("Tape read error while");
2018 if (!yflag && !reply("continue"))
2019 exit(1);
2020 memset(tapebuf, 0, PREFIXSIZE + bufsize);
2021 tbufptr = tapebuf;
2022
2023 #ifdef RRESTORE
2024 if (host)
2025 rl = rmtioctl(MTFSR, 1);
2026 else
2027 #endif
2028 {
2029 tcom.mt_op = MTFSR;
2030 tcom.mt_count = 1;
2031 rl = ioctl(mt, MTIOCTOP, &tcom);
2032 }
2033
2034 if (rl < 0) {
2035 warn("continuation failed");
2036 if (!yflag && !reply("assume end-of-tape and continue"))
2037 exit(1);
2038 ret = 0; /* end of tape */
2039 goto readerr;
2040 }
2041
2042 moverecord:
2043 blkcnt = 0;
2044 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2045 blksread++;
2046 tpblksread++;
2047 }
2048
2049 /*
2050 * Decompress a struct tapebuf into a buffer. readsize is the size read
2051 * from the tape/file and is used for error messages. Returns a pointer
2052 * to the location of the uncompressed buffer or NULL on errors.
2053 * Adjust numtrec and complain for a short block.
2054 */
2055 static char *
2056 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
2057 {
2058 /* If zflag is on, all blocks have a struct tapebuf prefix */
2059 /* zflag gets set in setup() from the dump header */
2060 int cresult, blocklen;
2061 unsigned long worklen;
2062 #ifdef HAVE_BZLIB
2063 unsigned int worklen2;
2064 #endif
2065 char *output = NULL,*reason = NULL, *lengtherr = NULL;
2066
2067 /* build a length error message */
2068 blocklen = tpbin->length;
2069 if (readsize < blocklen + (int)PREFIXSIZE)
2070 lengtherr = "short";
2071 else
2072 if (readsize > blocklen + (int)PREFIXSIZE)
2073 lengtherr = "long";
2074
2075 worklen = comprlen;
2076 cresult = 1;
2077 if (tpbin->compressed) {
2078 /* uncompress whatever we read, if it fails, complain later */
2079 if (tpbin->flags == COMPRESS_ZLIB) {
2080 #ifndef HAVE_ZLIB
2081 errx(1,"This restore version doesn't support zlib decompression");
2082 #else
2083 cresult = uncompress(comprbuf, &worklen,
2084 tpbin->buf, blocklen);
2085 output = comprbuf;
2086 switch (cresult) {
2087 case Z_OK:
2088 break;
2089 case Z_MEM_ERROR:
2090 reason = "not enough memory";
2091 break;
2092 case Z_BUF_ERROR:
2093 reason = "buffer too small";
2094 break;
2095 case Z_DATA_ERROR:
2096 reason = "data error";
2097 break;
2098 default:
2099 reason = "unknown";
2100 }
2101 if (cresult == Z_OK)
2102 cresult = 1;
2103 else
2104 cresult = 0;
2105 #endif /* HAVE_ZLIB */
2106 }
2107 if (tpbin->flags == COMPRESS_BZLIB) {
2108 #ifndef HAVE_BZLIB
2109 errx(1,"This restore version doesn't support bzlib decompression");
2110 #else
2111 worklen2 = worklen;
2112 cresult = BZ2_bzBuffToBuffDecompress(
2113 comprbuf, &worklen2,
2114 tpbin->buf, blocklen, 0, 0);
2115 worklen = worklen2;
2116 output = comprbuf;
2117 switch (cresult) {
2118 case BZ_OK:
2119 break;
2120 case BZ_MEM_ERROR:
2121 reason = "not enough memory";
2122 break;
2123 case BZ_OUTBUFF_FULL:
2124 reason = "buffer too small";
2125 break;
2126 case BZ_DATA_ERROR:
2127 case BZ_DATA_ERROR_MAGIC:
2128 case BZ_UNEXPECTED_EOF:
2129 reason = "data error";
2130 break;
2131 default:
2132 reason = "unknown";
2133 }
2134 if (cresult == BZ_OK)
2135 cresult = 1;
2136 else
2137 cresult = 0;
2138 #endif /* HAVE_BZLIB */
2139 }
2140 if (tpbin->flags == COMPRESS_LZO) {
2141 #ifndef HAVE_LZO
2142 errx(1,"This restore version doesn't support lzo decompression");
2143 #else
2144 cresult = lzo1x_decompress(tpbin->buf, blocklen,
2145 comprbuf, (lzo_uintp) &worklen,NULL);
2146 output = comprbuf;
2147 switch (cresult) {
2148 case LZO_E_OK:
2149 break;
2150 case LZO_E_ERROR:
2151 case LZO_E_EOF_NOT_FOUND:
2152 reason = "data error";
2153 break;
2154 default:
2155 reason = "unknown";
2156 }
2157 if (cresult == LZO_E_OK)
2158 cresult = 1;
2159 else
2160 cresult = 0;
2161 #endif /* HAVE_LZO */
2162 }
2163 }
2164 else {
2165 output = tpbin->buf;
2166 worklen = blocklen;
2167 }
2168 if (cresult) {
2169 numtrec = worklen / TP_BSIZE;
2170 if (worklen % TP_BSIZE != 0)
2171 reason = "length mismatch";
2172 }
2173 if (reason) {
2174 if (lengtherr)
2175 fprintf(stderr, "%s compressed block: %d expected: %d\n",
2176 lengtherr, readsize, tpbin->length + PREFIXSIZE);
2177 fprintf(stderr, "decompression error, block %ld: %s\n",
2178 tpblksread+1, reason);
2179 if (!cresult)
2180 output = NULL;
2181 }
2182 return output;
2183 }
2184
2185 /*
2186 * Print an error message for a read error.
2187 * This was exteracted from the original readtape().
2188 */
2189 static void
2190 msg_read_error(char *m)
2191 {
2192 switch (curfile.action) {
2193 default:
2194 fprintf(stderr, "%s trying to set up tape\n", m);
2195 break;
2196 case UNKNOWN:
2197 fprintf(stderr, "%s trying to resynchronize\n", m);
2198 break;
2199 case USING:
2200 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
2201 break;
2202 case SKIP:
2203 fprintf(stderr, "%s skipping over inode %lu\n", m,
2204 (unsigned long)curfile.ino);
2205 break;
2206 }
2207 }
2208 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
2209
2210 /*
2211 * Read the first block and get the blocksize from it. Test
2212 * for a compressed dump tape/file. setup() will make the final
2213 * determination by checking the compressed flag if gethead()
2214 * finds a valid header. The test here is necessary to offset the buffer
2215 * by the size of the compressed prefix. zflag is set here so that
2216 * readtape_set can set the correct function pointer for readtape().
2217 * Note that the first block of each tape/file is not compressed
2218 * and does not have a prefix.
2219 */
2220 static void
2221 findtapeblksize(void)
2222 {
2223 long i;
2224 size_t len;
2225 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
2226 struct s_spcl spclpt;
2227
2228 for (i = 0; i < ntrec; i++)
2229 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
2230 blkcnt = 0;
2231 tbufptr = tapebuf;
2232 /*
2233 * For a pipe or file, read in the first record. For a tape, read
2234 * the first block.
2235 */
2236 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
2237
2238 if (read_a_block(mt, tapebuf, len, &i) <= 0)
2239 errx(1, "Tape read error on first record");
2240
2241 memcpy(&spclpt, tapebuf, TP_BSIZE);
2242 if (converthead(&spclpt) == FAIL) {
2243 cvtflag++;
2244 if (converthead(&spclpt) == FAIL) {
2245 /* Special case for old compressed tapes with prefix */
2246 if (magtapein && (i % TP_BSIZE != 0))
2247 goto oldformat;
2248 errx(1, "Tape is not a dump tape");
2249 }
2250 fprintf(stderr, "Converting to new file system format.\n");
2251 }
2252 /*
2253 * If the input is from a file or a pipe, we read TP_BSIZE
2254 * bytes looking for a dump header. If the dump is compressed
2255 * we need to read in the rest of the block, as determined
2256 * by c_ntrec in the dump header. The first block of the
2257 * dump is not compressed and does not have a prefix.
2258 */
2259 if (!magtapein) {
2260 if (spclpt.c_type == TS_TAPE
2261 && spclpt.c_flags & DR_COMPRESSED) {
2262 /* It's a compressed dump file, read in the */
2263 /* rest of the block based on spclpt.c_ntrec. */
2264 if (spclpt.c_ntrec > ntrec)
2265 errx(1, "Tape blocksize is too large, use "
2266 "\'-b %d\' ", spclpt.c_ntrec);
2267 ntrec = spclpt.c_ntrec;
2268 len = (ntrec - 1) * TP_BSIZE;
2269 zflag = 1;
2270 }
2271 else {
2272 /* read in the rest of the block based on bufsize */
2273 len = bufsize - TP_BSIZE;
2274 }
2275 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
2276 || (i != (long)len && i % TP_BSIZE != 0))
2277 errx(1,"Error reading dump file header");
2278 tbufptr = tapebuf;
2279 numtrec = ntrec;
2280 Vprintf(stdout, "Input block size is %ld\n", ntrec);
2281 return;
2282 } /* if (!magtapein) */
2283
2284 /*
2285 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
2286 * If the value of ntrec is too large, we read less than
2287 * what we asked for; adjust the value of ntrec and test for
2288 * a compressed dump tape.
2289 */
2290 if (i % TP_BSIZE != 0) {
2291 oldformat:
2292 /* may be old format compressed dump tape with a prefix */
2293 memcpy(&spclpt, tpb->buf, TP_BSIZE);
2294 cvtflag = 0;
2295 if (converthead(&spclpt) == FAIL) {
2296 cvtflag++;
2297 if (converthead(&spclpt) == FAIL)
2298 errx(1, "Tape is not a dump tape");
2299 fprintf(stderr, "Converting to new file system format.\n");
2300 }
2301 if (i % TP_BSIZE == PREFIXSIZE
2302 && tpb->compressed == 0
2303 && spclpt.c_type == TS_TAPE
2304 && spclpt.c_flags & DR_COMPRESSED) {
2305 zflag = 1;
2306 tbufptr = tpb->buf;
2307 if (tpb->length > bufsize)
2308 errx(1, "Tape blocksize is too large, use "
2309 "\'-b %d\' ", tpb->length / TP_BSIZE);
2310 }
2311 else
2312 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2313 i, TP_BSIZE);
2314 }
2315 ntrec = i / TP_BSIZE;
2316 if (spclpt.c_type == TS_TAPE) {
2317 if (spclpt.c_flags & DR_COMPRESSED)
2318 zflag = 1;
2319 if (spclpt.c_ntrec > ntrec)
2320 errx(1, "Tape blocksize is too large, use "
2321 "\'-b %d\' ", spclpt.c_ntrec);
2322 }
2323 numtrec = ntrec;
2324 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
2325 }
2326
2327 /*
2328 * Read a block of data handling all of the messy details.
2329 */
2330 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
2331 {
2332 long i = 1, size;
2333
2334 size = len;
2335 while (size > 0) {
2336 #ifdef RRESTORE
2337 if (!Afile && host)
2338 i = rmtread(buf, size);
2339 else
2340 #endif
2341 i = read(fd, buf, size);
2342
2343 if (i <= 0)
2344 break; /* EOD or error */
2345 size -= i;
2346 if (magtapein)
2347 break; /* block at a time for mt */
2348 buf += i;
2349 }
2350 *lengthread = len - size;
2351 return i;
2352 }
2353
2354 void
2355 closemt(void)
2356 {
2357
2358 if (mt < 0)
2359 return;
2360 #ifdef RRESTORE
2361 if (!Afile && host)
2362 rmtclose();
2363 else
2364 #endif
2365 (void) close(mt);
2366 }
2367
2368 static void
2369 setmagtapein(void) {
2370 struct mtget mt_stat;
2371 static int done = 0;
2372 if (done)
2373 return;
2374 done = 1;
2375 if (!pipein) {
2376 /* need to know if input is really from a tape */
2377 #ifdef RRESTORE
2378 if (host)
2379 magtapein = !lflag;
2380 else
2381 #endif
2382 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
2383 }
2384
2385 Vprintf(stdout,"Input is from a %s %s\n",
2386 host ? "remote" : "local",
2387 magtapein ? "tape" :
2388 Vflag ? "multi-volume (no tape)" : "file/pipe");
2389 }
2390
2391 /*
2392 * Read the next block from the tape.
2393 * Check to see if it is one of several vintage headers.
2394 * If it is an old style header, convert it to a new style header.
2395 * If it is not any valid header, return an error.
2396 */
2397 static int
2398 gethead(struct s_spcl *buf)
2399 {
2400 readtape((char *)buf);
2401 return converthead(buf);
2402 }
2403
2404 static int
2405 converthead(struct s_spcl *buf)
2406 {
2407 int32_t i;
2408 union {
2409 quad_t qval;
2410 int32_t val[2];
2411 } qcvt;
2412 union u_ospcl {
2413 char dummy[TP_BSIZE];
2414 struct s_ospcl {
2415 int32_t c_type;
2416 int32_t c_date;
2417 int32_t c_ddate;
2418 int32_t c_volume;
2419 int32_t c_tapea;
2420 u_int16_t c_inumber;
2421 int32_t c_magic;
2422 int32_t c_checksum;
2423 struct odinode {
2424 u_int16_t odi_mode;
2425 u_int16_t odi_nlink;
2426 u_int16_t odi_uid;
2427 u_int16_t odi_gid;
2428 int32_t odi_size;
2429 int32_t odi_rdev;
2430 char odi_addr[36];
2431 int32_t odi_atime;
2432 int32_t odi_mtime;
2433 int32_t odi_ctime;
2434 } c_dinode;
2435 int32_t c_count;
2436 char c_fill[256];
2437 } s_ospcl;
2438 } u_ospcl;
2439
2440 if (!cvtflag) {
2441 if (buf->c_magic != NFS_MAGIC) {
2442 if (swabi(buf->c_magic) != NFS_MAGIC)
2443 return (FAIL);
2444 if (!Bcvt) {
2445 Vprintf(stdout, "Note: Doing Byte swapping\n");
2446 Bcvt = 1;
2447 }
2448 }
2449 if (checksum((int *)buf) == FAIL)
2450 return (FAIL);
2451 if (Bcvt)
2452 swabst((u_char *)"8i4s31i528bi192b3i", (u_char *)buf);
2453 goto good;
2454 }
2455 memcpy(&u_ospcl.s_ospcl, buf, TP_BSIZE);
2456 memset((char *)buf, 0, (long)TP_BSIZE);
2457 buf->c_type = u_ospcl.s_ospcl.c_type;
2458 buf->c_date = u_ospcl.s_ospcl.c_date;
2459 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
2460 buf->c_volume = u_ospcl.s_ospcl.c_volume;
2461 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
2462 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
2463 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
2464 buf->c_magic = u_ospcl.s_ospcl.c_magic;
2465 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
2466 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
2467 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
2468 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
2469 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
2470 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
2471 #if defined(__linux__) || defined(sunos)
2472 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
2473 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2474 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2475 #else /* __linux__ || sunos */
2476 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
2477 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2478 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2479 #endif /* __linux__ || sunos */
2480 buf->c_count = u_ospcl.s_ospcl.c_count;
2481 memmove(buf->c_addr, u_ospcl.s_ospcl.c_fill, (long)256);
2482 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
2483 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
2484 return(FAIL);
2485 buf->c_magic = NFS_MAGIC;
2486
2487 good:
2488 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
2489 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
2490 qcvt.qval = buf->c_dinode.di_size;
2491 if (qcvt.val[0] || qcvt.val[1]) {
2492 Vprintf(stdout, "Note: Doing Quad swapping\n");
2493 Qcvt = 1;
2494 }
2495 }
2496 if (Qcvt) {
2497 qcvt.qval = buf->c_dinode.di_size;
2498 i = qcvt.val[1];
2499 qcvt.val[1] = qcvt.val[0];
2500 qcvt.val[0] = i;
2501 buf->c_dinode.di_size = qcvt.qval;
2502 }
2503 readmapflag = 0;
2504
2505 switch (buf->c_type) {
2506
2507 case TS_CLRI:
2508 case TS_BITS:
2509 /*
2510 * Have to patch up missing information in bit map headers
2511 */
2512 buf->c_inumber = 0;
2513 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
2514 if (buf->c_count > TP_NINDIR)
2515 readmapflag = 1;
2516 else
2517 for (i = 0; i < buf->c_count; i++)
2518 buf->c_addr[i]++;
2519 break;
2520
2521 case TS_TAPE:
2522 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
2523 oldinofmt = 1;
2524 /* fall through */
2525 case TS_END:
2526 buf->c_inumber = 0;
2527 if (buf->c_flags & DR_INODEINFO) {
2528 memcpy(volinfo, buf->c_inos, TP_NINOS * sizeof(dump_ino_t));
2529 if (Bcvt)
2530 swabst((u_char *)"128i", (u_char *)volinfo);
2531 }
2532 break;
2533
2534 case TS_INODE:
2535 case TS_ADDR:
2536 break;
2537
2538 default:
2539 panic("gethead: unknown inode type %d\n", buf->c_type);
2540 break;
2541 }
2542 /*
2543 * If we are restoring a filesystem with old format inodes,
2544 * copy the uid/gid to the new location.
2545 */
2546 if (oldinofmt) {
2547 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2548 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2549 }
2550 if (dflag)
2551 accthdr(buf);
2552 return(GOOD);
2553 }
2554
2555 static void
2556 converttapebuf(struct tapebuf *tpb)
2557 {
2558 if (Bcvt) {
2559 struct tb {
2560 unsigned int length:28;
2561 unsigned int flags:3;
2562 unsigned int compressed:1;
2563 } tb;
2564 swabst((u_char *)"i", (u_char *)tpb);
2565 memcpy(&tb, tpb, 4);
2566 tpb->length = tb.length;
2567 tpb->flags = tb.flags;
2568 tpb->compressed = tb.compressed;
2569 }
2570 }
2571
2572 /*
2573 * Check that a header is where it belongs and predict the next header
2574 */
2575 static void
2576 accthdr(struct s_spcl *header)
2577 {
2578 static dump_ino_t previno = 0x7fffffff;
2579 static int prevtype;
2580 static long predict;
2581 long blks, i;
2582
2583 if (header->c_type == TS_TAPE) {
2584 fprintf(stderr, "Volume header (%s inode format) ",
2585 oldinofmt ? "old" : "new");
2586 if (header->c_firstrec)
2587 fprintf(stderr, "begins with record %d",
2588 header->c_firstrec);
2589 fprintf(stderr, "\n");
2590 previno = 0x7fffffff;
2591 return;
2592 }
2593 if (previno == 0x7fffffff)
2594 goto newcalc;
2595 switch (prevtype) {
2596 case TS_BITS:
2597 fprintf(stderr, "Dumped inodes map header");
2598 break;
2599 case TS_CLRI:
2600 fprintf(stderr, "Used inodes map header");
2601 break;
2602 case TS_INODE:
2603 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2604 break;
2605 case TS_ADDR:
2606 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2607 break;
2608 case TS_END:
2609 fprintf(stderr, "End of tape header");
2610 break;
2611 }
2612 if (predict != blksread - 1)
2613 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2614 predict, blksread - 1);
2615 fprintf(stderr, "\n");
2616 newcalc:
2617 blks = 0;
2618 if (header->c_type != TS_END)
2619 for (i = 0; i < header->c_count; i++)
2620 if (readmapflag || header->c_addr[i] != 0)
2621 blks++;
2622 predict = blks;
2623 blksread = 0;
2624 prevtype = header->c_type;
2625 previno = header->c_inumber;
2626 }
2627
2628 /*
2629 * Find an inode header.
2630 * Complain if had to skip, and complain is set.
2631 */
2632 static void
2633 findinode(struct s_spcl *header)
2634 {
2635 static long skipcnt = 0;
2636 long i;
2637 char buf[TP_BSIZE];
2638
2639 curfile.name = "<name unknown>";
2640 curfile.action = UNKNOWN;
2641 curfile.dip = NULL;
2642 curfile.ino = 0;
2643 do {
2644 if (header->c_magic != NFS_MAGIC) {
2645 skipcnt++;
2646 while (gethead(header) == FAIL ||
2647 header->c_date != dumpdate)
2648 skipcnt++;
2649 }
2650 switch (header->c_type) {
2651
2652 case TS_ADDR:
2653 /*
2654 * Skip up to the beginning of the next record
2655 */
2656 for (i = 0; i < header->c_count; i++)
2657 if (header->c_addr[i])
2658 readtape(buf);
2659 while (gethead(header) == FAIL ||
2660 header->c_date != dumpdate)
2661 skipcnt++;
2662 break;
2663
2664 case TS_INODE:
2665 curfile.dip = &header->c_dinode;
2666 curfile.ino = header->c_inumber;
2667 break;
2668
2669 case TS_END:
2670 curfile.ino = maxino;
2671 break;
2672
2673 case TS_CLRI:
2674 curfile.name = "<file removal list>";
2675 break;
2676
2677 case TS_BITS:
2678 curfile.name = "<file dump list>";
2679 break;
2680
2681 case TS_TAPE:
2682 panic("unexpected tape header\n");
2683 /* NOTREACHED */
2684
2685 default:
2686 panic("unknown tape header type %d\n", spcl.c_type);
2687 /* NOTREACHED */
2688
2689 }
2690 } while (header->c_type == TS_ADDR);
2691 if (skipcnt > 0)
2692 #ifdef USE_QFA
2693 if (!noresyncmesg)
2694 #endif
2695 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2696 skipcnt);
2697 skipcnt = 0;
2698 }
2699
2700 static int
2701 checksum(int *buf)
2702 {
2703 int i, j;
2704
2705 j = sizeof(union u_spcl) / sizeof(int);
2706 i = 0;
2707 if(!Bcvt) {
2708 do
2709 i += *buf++;
2710 while (--j);
2711 } else {
2712 /* What happens if we want to read restore tapes
2713 for a 16bit int machine??? */
2714 do
2715 i += swabi(*buf++);
2716 while (--j);
2717 }
2718
2719 if (i != CHECKSUM) {
2720 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2721 (unsigned long)curfile.ino, curfile.name);
2722 return(FAIL);
2723 }
2724 return(GOOD);
2725 }
2726
2727 #ifdef RRESTORE
2728 #ifdef __STDC__
2729 #include <stdarg.h>
2730 #else
2731 #include <varargs.h>
2732 #endif
2733
2734 void
2735 #ifdef __STDC__
2736 msg(const char *fmt, ...)
2737 #else
2738 msg(fmt, va_alist)
2739 char *fmt;
2740 va_dcl
2741 #endif
2742 {
2743 va_list ap;
2744 #ifdef __STDC__
2745 va_start(ap, fmt);
2746 #else
2747 va_start(ap);
2748 #endif
2749 (void)vfprintf(stderr, fmt, ap);
2750 va_end(ap);
2751 }
2752 #endif /* RRESTORE */
2753
2754 static u_char *
2755 swab16(u_char *sp, int n)
2756 {
2757 char c;
2758
2759 while (--n >= 0) {
2760 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2761 sp += 2;
2762 }
2763 return (sp);
2764 }
2765
2766 static u_char *
2767 swab32(u_char *sp, int n)
2768 {
2769 char c;
2770
2771 while (--n >= 0) {
2772 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2773 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2774 sp += 4;
2775 }
2776 return (sp);
2777 }
2778
2779 static u_char *
2780 swab64(u_char *sp, int n)
2781 {
2782 char c;
2783
2784 while (--n >= 0) {
2785 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2786 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2787 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2788 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2789 sp += 8;
2790 }
2791 return (sp);
2792 }
2793
2794 void
2795 swabst(u_char *cp, u_char *sp)
2796 {
2797 int n = 0;
2798
2799 while (*cp) {
2800 switch (*cp) {
2801 case '0': case '1': case '2': case '3': case '4':
2802 case '5': case '6': case '7': case '8': case '9':
2803 n = (n * 10) + (*cp++ - '0');
2804 continue;
2805
2806 case 's': case 'w': case 'h':
2807 if (n == 0)
2808 n = 1;
2809 sp = swab16(sp, n);
2810 break;
2811
2812 case 'i':
2813 if (n == 0)
2814 n = 1;
2815 sp = swab32(sp, n);
2816 break;
2817
2818 case 'l':
2819 if (n == 0)
2820 n = 1;
2821 sp = swab64(sp, n);
2822 break;
2823
2824 default: /* Any other character, like 'b' counts as byte. */
2825 if (n == 0)
2826 n = 1;
2827 sp += n;
2828 break;
2829 }
2830 cp++;
2831 n = 0;
2832 }
2833 }
2834
2835 static u_int
2836 swabi(u_int x)
2837 {
2838 swabst((u_char *)"i", (u_char *)&x);
2839 return (x);
2840 }
2841
2842 #if 0
2843 static u_long
2844 swabl(u_long x)
2845 {
2846 swabst((u_char *)"l", (u_char *)&x);
2847 return (x);
2848 }
2849 #endif
2850
2851 void
2852 RequestVol(long tnum)
2853 {
2854 FLUSHTAPEBUF();
2855 getvol(tnum);
2856 }
2857
2858 #ifdef USE_QFA
2859 #ifdef sunos
2860 extern int fdsmtc;
2861
2862 struct uscsi_cmd {
2863 int uscsi_flags; /* read, write, etc. see below */
2864 short uscsi_status; /* resulting status */
2865 short uscsi_timeout; /* Command Timeout */
2866 caddr_t uscsi_cdb; /* cdb to send to target */
2867 caddr_t uscsi_bufaddr; /* i/o source/destination */
2868 u_int uscsi_buflen; /* size of i/o to take place */
2869 u_int uscsi_resid; /* resid from i/o operation */
2870 u_char uscsi_cdblen; /* # of valid cdb bytes */
2871 u_char uscsi_rqlen; /* size of uscsi_rqbuf */
2872 u_char uscsi_rqstatus; /* status of request sense cmd */
2873 u_char uscsi_rqresid; /* resid of request sense cmd */
2874 caddr_t uscsi_rqbuf; /* request sense buffer */
2875 void *uscsi_reserved_5; /* Reserved for Future Use */
2876 };
2877
2878 #define CDB_GROUP0 6 /* 6-byte cdb's */
2879 #define CDB_GROUP1 10 /* 10-byte cdb's */
2880 #define CDB_GROUP2 10 /* 10-byte cdb's */
2881 #define CDB_GROUP3 0 /* reserved */
2882 #define CDB_GROUP4 16 /* 16-byte cdb's */
2883 #define CDB_GROUP5 12 /* 12-byte cdb's */
2884 #define CDB_GROUP6 0 /* reserved */
2885 #define CDB_GROUP7 0 /* reserved */
2886
2887 #define USCSI_WRITE 0x00000 /* send data to device */
2888 #define USCSI_SILENT 0x00001 /* no error messages */
2889 #define USCSI_DIAGNOSE 0x00002 /* fail if any error occurs */
2890 #define USCSI_ISOLATE 0x00004 /* isolate from normal commands */
2891 #define USCSI_READ 0x00008 /* get data from device */
2892 #define USCSI_RESET 0x04000 /* Reset target */
2893 #define USCSI_RESET_ALL 0x08000 /* Reset all targets */
2894 #define USCSI_RQENABLE 0x10000 /* Enable Request Sense extensions */
2895
2896 #define USCSIIOC (0x04 << 8)
2897 #define USCSICMD (USCSIIOC|201) /* user scsi command */
2898
2899 #define USCSI_TIMEOUT 30
2900 #define USCSI_SHORT_TIMEOUT 900
2901 #define USCSI_LONG_TIMEOUT 14000
2902
2903 #define B(s,i) ((unsigned char)((s) >> i))
2904 #define B1(s) ((unsigned char)(s))
2905
2906 #define MSB4(s,v) *(s)=B(v,24),(s)[1]=B(v,16), (s)[2]=B(v,8), (s)[3]=B1(v)
2907
2908
2909 int
2910 GetTapePos(long long *pos)
2911 {
2912 int err = 0;
2913 struct uscsi_cmd scmd;
2914 char buf[512];
2915 char parm[512 * 8];
2916 long lpos;
2917
2918 (void)memset((void *)buf, 0, sizeof(buf));
2919 (void)memset((void *)&scmd, 0, sizeof(scmd));
2920 scmd.uscsi_flags = USCSI_READ|USCSI_SILENT;
2921 scmd.uscsi_timeout = USCSI_TIMEOUT;
2922 scmd.uscsi_cdb = buf;
2923 scmd.uscsi_cdblen = CDB_GROUP1;
2924 buf[0] = 0x34; /* read position */
2925 buf[1] = 0;
2926 (void)memset((void *)parm, 0, 512);
2927 scmd.uscsi_bufaddr = parm;
2928 scmd.uscsi_buflen = 56;
2929 if (ioctl(fdsmtc, USCSICMD, &scmd) == -1) {
2930 err = errno;
2931 return err;
2932 }
2933 (void)memcpy(&lpos, &parm[4], sizeof(long));
2934 *pos = lpos;
2935 return err;
2936 }
2937
2938 int
2939 GotoTapePos(long long pos)
2940 {
2941 int err = 0;
2942 struct uscsi_cmd scmd;
2943 char buf[512];
2944 char parm[512 * 8];
2945 long lpos = (long)pos;
2946
2947 (void)memset((void *)buf, 0, sizeof(buf));
2948 (void)memset((void *)&scmd, 0, sizeof(scmd));
2949 scmd.uscsi_flags = USCSI_WRITE|USCSI_SILENT;
2950 scmd.uscsi_timeout = 360; /* 5 Minutes */
2951 scmd.uscsi_cdb = buf;
2952 scmd.uscsi_cdblen = CDB_GROUP1;
2953 buf[0] = 0x2b; /* locate */
2954 buf[1] = 0;
2955 MSB4(&buf[3], lpos);
2956 (void)memset((void *)parm, 0, 512);
2957 scmd.uscsi_bufaddr = NULL;
2958 scmd.uscsi_buflen = 0;
2959 if (ioctl(fdsmtc, USCSICMD, &scmd) == -1) {
2960 err = errno;
2961 return err;
2962 }
2963 return err;
2964 }
2965 #endif
2966
2967 #define LSEEK_GET_TAPEPOS 10
2968 #define LSEEK_GO2_TAPEPOS 11
2969
2970 #ifdef __linux__
2971 typedef struct mt_pos {
2972 short mt_op;
2973 int mt_count;
2974 } MTPosRec, *MTPosPtr;
2975
2976
2977 /*
2978 * get the current position of the tape
2979 */
2980 int
2981 GetTapePos(long long *pos)
2982 {
2983 int err = 0;
2984
2985 #ifdef RDUMP
2986 if (host) {
2987 *pos = (long long) rmtseek((OFF_T)0, (int)LSEEK_GET_TAPEPOS);
2988 err = *pos < 0;
2989 }
2990 else
2991 #endif
2992 {
2993 if (magtapein) {
2994 long mtpos;
2995 *pos = 0;
2996 err = (ioctl(mt, MTIOCPOS, &mtpos) < 0);
2997 *pos = (long long)mtpos;
2998 }
2999 else {
3000 *pos = LSEEK(mt, 0, SEEK_CUR);
3001 err = (*pos < 0);
3002 }
3003 }
3004 if (err) {
3005 err = errno;
3006 fprintf(stdout, "[%ld] error: %d (getting tapepos: %lld)\n",
3007 (unsigned long)getpid(), err, *pos);
3008 return err;
3009 }
3010 return err;
3011 }
3012
3013 /*
3014 * go to specified position on tape
3015 */
3016 int
3017 GotoTapePos(long long pos)
3018 {
3019 int err = 0;
3020
3021 #ifdef RDUMP
3022 if (host)
3023 err = (rmtseek((OFF_T)pos, (int)LSEEK_GO2_TAPEPOS) < 0);
3024 else
3025 #endif
3026 {
3027 if (magtapein) {
3028 struct mt_pos buf;
3029 buf.mt_op = MTSEEK;
3030 buf.mt_count = (int) pos;
3031 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
3032 }
3033 else {
3034 pos = LSEEK(mt, pos, SEEK_SET);
3035 err = (pos < 0);
3036 }
3037 }
3038 if (err) {
3039 err = errno;
3040 fprintf(stdout, "[%ld] error: %d (setting tapepos: %lld)\n",
3041 (unsigned long)getpid(), err, pos);
3042 return err;
3043 }
3044 return err;
3045 }
3046 #endif /* __linux__ */
3047
3048 /*
3049 * read next data from tape to re-sync
3050 */
3051 void
3052 ReReadFromTape(void)
3053 {
3054 FLUSHTAPEBUF();
3055 noresyncmesg = 1;
3056 if (gethead(&spcl) == FAIL) {
3057 #ifdef DEBUG_QFA
3058 fprintf(stdout, "DEBUG 1 gethead failed\n");
3059 #endif
3060 }
3061 findinode(&spcl);
3062 noresyncmesg = 0;
3063 }
3064
3065 void
3066 ReReadInodeFromTape(dump_ino_t theino)
3067 {
3068 long cntloop = 0;
3069
3070 FLUSHTAPEBUF();
3071 noresyncmesg = 1;
3072 do {
3073 cntloop++;
3074 gethead(&spcl);
3075 } while (!(spcl.c_inumber == theino && spcl.c_type == TS_INODE && spcl.c_date == dumpdate));
3076 #ifdef DEBUG_QFA
3077 fprintf(stderr, "DEBUG: %ld reads\n", cntloop);
3078 fprintf(stderr, "DEBUG: bufsize %ld\n", bufsize);
3079 fprintf(stderr, "DEBUG: ntrec %ld\n", ntrec);
3080 fprintf(stderr, "DEBUG: %ld reads\n", cntloop);
3081 #endif
3082 findinode(&spcl);
3083 noresyncmesg = 0;
3084 }
3085
3086 #ifdef sunos
3087 int
3088 OpenSMTCmt(char *themagtape)
3089 {
3090 if (GetSCSIIDFromPath(themagtape, &scsiid)) {
3091 fprintf(stderr, "can't get SCSI-ID for %s\n", themagtape);
3092 return -1;
3093 }
3094 if (scsiid < 0) {
3095 fprintf(stderr, "can't get SCSI-ID for %s\n", themagtape);
3096 return -1;
3097 }
3098 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
3099 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
3100 fprintf(stderr, "can't open smtc device: %s, %d\n", smtcpath, errno);
3101 return -1;
3102 }
3103 return 0;
3104 }
3105 #endif /* sunos */
3106 #endif /* USE_QFA */