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