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