]> git.wh0rd.org - dump.git/blob - restore/tape.c
Fix restoration of extended attributes for fifos and device nodes.
[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.94 2009/12/03 12:46:30 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 skipfile();
896 if (Nflag)
897 return (GOOD);
898 if (! (spcl.c_flags & DR_METAONLY)) {
899 if (uflag && !Nflag)
900 (void)unlink(name);
901 if (mkfifo(name, mode) < 0) {
902 warn("%s: cannot create fifo", name);
903 return (FAIL);
904 }
905 }
906 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
907 (void) chmod(name, mode);
908 extractattr(name);
909 utimes(name, timep);
910 if (flags)
911 #ifdef __linux__
912 (void) lsetflags(name, flags);
913 #else
914 #ifdef sunos
915 {
916 warn("%s: cannot call chflags", name);
917 /* (void) chflags(name, flags); */
918 }
919 #else
920 (void) chflags(name, flags);
921 #endif
922 #endif
923 return (GOOD);
924
925 case IFCHR:
926 case IFBLK:
927 Vprintf(stdout, "extract special file %s\n", name);
928 skipfile();
929 if (Nflag)
930 return (GOOD);
931 if (! (spcl.c_flags & DR_METAONLY)) {
932 if (uflag)
933 (void)unlink(name);
934 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
935 warn("%s: cannot create special file", name);
936 return (FAIL);
937 }
938 }
939 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
940 (void) chmod(name, mode);
941 extractattr(name);
942 utimes(name, timep);
943 if (flags)
944 #ifdef __linux__
945 {
946 warn("%s: lsetflags called on a special file", name);
947 (void) lsetflags(name, flags);
948 }
949 #else
950 #ifdef sunos
951 {
952 warn("%s: cannot call chflags on a special file", name);
953 /* (void) chflags(name, flags); */
954 }
955 #else
956 {
957 warn("%s: chflags called on a special file", name);
958 (void) chflags(name, flags);
959 }
960 #endif
961 #endif
962 return (GOOD);
963
964 case IFREG:
965 {
966 uid_t luid = curfile.dip->di_uid;
967 gid_t lgid = curfile.dip->di_gid;
968
969 Vprintf(stdout, "extract file %s\n", name);
970 if (Nflag) {
971 skipfile();
972 return (GOOD);
973 }
974 if (! (spcl.c_flags & DR_METAONLY)) {
975 if (uflag)
976 (void)unlink(name);
977 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
978 0666)) < 0) {
979 warn("%s: cannot create file", name);
980 skipfile();
981 return (FAIL);
982 }
983 getfile(xtrfile, xtrskip);
984 (void) close(ofile);
985 }
986 else
987 skipfile();
988 (void) chown(name, luid, lgid);
989 (void) chmod(name, mode);
990 extractattr(name);
991 utimes(name, timep);
992 if (flags)
993 #ifdef __linux__
994 (void) lsetflags(name, flags);
995 #else
996 #ifdef sunos
997 {
998 warn("%s: cannot call chflags", name);
999 /* (void) chflags(name, flags); */
1000 }
1001 #else
1002 (void) chflags(name, flags);
1003 #endif
1004 #endif
1005 return (GOOD);
1006 }
1007 }
1008 /* NOTREACHED */
1009 }
1010
1011 static int
1012 extractattr(char *path)
1013 {
1014 while (spcl.c_flags & DR_EXTATTRIBUTES) {
1015 switch (spcl.c_extattributes) {
1016 case EXT_MACOSFNDRINFO:
1017 #ifdef DUMP_MACOSX
1018 (void)extractfinderinfoufs(path);
1019 #else
1020 msg("MacOSX not supported in this version, skipping\n");
1021 skipfile();
1022 #endif
1023 break;
1024 case EXT_MACOSRESFORK:
1025 #ifdef DUMP_MACOSX
1026 (void)extractresourceufs(path);
1027 #else
1028 msg("MacOSX not supported in this version, skipping\n");
1029 skipfile();
1030 #endif
1031 break;
1032 case EXT_XATTR: {
1033 char xattr[XATTR_MAXSIZE];
1034
1035 if (readxattr(xattr) == GOOD) {
1036 xattr_extract(path, xattr);
1037 break;
1038 }
1039 }
1040 default:
1041 msg("unexpected inode extension %ld, skipping\n", spcl.c_extattributes);
1042 skipfile();
1043 break;
1044 }
1045 }
1046 return GOOD;
1047 }
1048
1049 #ifdef DUMP_MACOSX
1050 int
1051 extractfinderinfoufs(char *name)
1052 {
1053 int err;
1054 char oFileRsrc[MAXPATHLEN];
1055 int flags;
1056 mode_t mode;
1057 struct timeval timep[2];
1058 u_int32_t uid;
1059 u_int32_t gid;
1060 char path[MAXPATHLEN], fname[MAXPATHLEN];
1061
1062 curfile.name = name;
1063 curfile.action = USING;
1064 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
1065 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
1066 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
1067 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
1068 mode = curfile.dip->di_mode;
1069 flags = curfile.dip->di_flags;
1070 uid = curfile.dip->di_uid;
1071 gid = curfile.dip->di_gid;
1072
1073 switch (mode & IFMT) {
1074
1075 default:
1076 fprintf(stderr, "%s: (extr. finfoufs) unknown file mode 0%o\n", name, mode);
1077 skipfile();
1078 return (FAIL);
1079
1080 case IFDIR:
1081 fprintf(stderr, "%s: (extr. finfoufs[IFDIR]) unknown file mode 0%o\n", name, mode);
1082 skipfile();
1083 return (FAIL);
1084
1085 case IFLNK:
1086 skipfile();
1087 return (GOOD);
1088
1089 case IFREG:
1090 Vprintf(stdout, "extract finderinfo file %s\n", name);
1091 if (Nflag) {
1092 skipfile();
1093 return (GOOD);
1094 }
1095 getfile(xtrfilefinderinfo, xtrskip);
1096
1097 GetPathFile(name, path, fname);
1098 strcpy(oFileRsrc, path);
1099 strcat(oFileRsrc, "._");
1100 strcat(oFileRsrc, fname);
1101
1102 if ((err = CreateAppleDoubleFileRes(oFileRsrc, &gFndrInfo.fndrinfo,
1103 mode, flags, timep, uid, gid)) != 0) {
1104 fprintf(stderr, "%s: cannot create finderinfo: %s\n",
1105 name, strerror(errno));
1106 skipfile();
1107 return (FAIL);
1108 }
1109 return (GOOD);
1110 }
1111 /* NOTREACHED */
1112 }
1113
1114
1115 int
1116 extractresourceufs(char *name)
1117 {
1118 char oFileRsrc[MAXPATHLEN];
1119 int flags;
1120 mode_t mode;
1121 struct timeval timep[2];
1122 char path[MAXPATHLEN], fname[MAXPATHLEN];
1123 ASDHeaderPtr hp;
1124 ASDEntryPtr ep;
1125 u_long loff;
1126 u_int32_t uid;
1127 u_int32_t gid;
1128 u_int64_t di_size;
1129 char *p;
1130 char buf[1024];
1131
1132 curfile.name = name;
1133 curfile.action = USING;
1134 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
1135 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
1136 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
1137 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
1138 mode = curfile.dip->di_mode;
1139 flags = curfile.dip->di_flags;
1140 uid = curfile.dip->di_uid;
1141 gid = curfile.dip->di_gid;
1142 di_size = curfile.dip->di_size;
1143
1144 switch (mode & IFMT) {
1145
1146 default:
1147 fprintf(stderr, "%s: (extr. resufs) unknown file mode 0%o\n", name, mode);
1148 skipfile();
1149 return (FAIL);
1150
1151 case IFDIR:
1152 fprintf(stderr, "%s: (extr. resufs [IFDIR]) unknown file mode 0%o\n", name, mode);
1153 skipfile();
1154 return (FAIL);
1155
1156 case IFLNK:
1157 skipfile();
1158 return (GOOD);
1159
1160 case IFREG:
1161 Vprintf(stdout, "extract resource file %s\n", name);
1162 if (Nflag) {
1163 skipfile();
1164 return (GOOD);
1165 }
1166
1167 GetPathFile(name, path, fname);
1168 strcpy(oFileRsrc, path);
1169 strcat(oFileRsrc, "._");
1170 strcat(oFileRsrc, fname);
1171
1172 if ((ofile = open(oFileRsrc, O_RDONLY, 0)) < 0) {
1173 fprintf(stderr, "%s: cannot read finderinfo: %s\n",
1174 name, strerror(errno));
1175 skipfile();
1176 return (FAIL);
1177 }
1178 read(ofile, buf, 70);
1179 (void) close(ofile);
1180 p = buf;
1181 hp = (ASDHeaderPtr)p;
1182 /* the header */
1183 hp->entries++;
1184 p += sizeof(ASDHeader) - CORRECT;
1185 ep = (ASDEntryPtr)p;
1186 /* the finderinfo entry */
1187 ep->offset += sizeof(ASDEntry);
1188 loff = ep->offset;
1189
1190 p += sizeof(ASDEntry);
1191 /* the finderinfo data */
1192 bcopy(p, p + sizeof(ASDEntry), INFOLEN);
1193 ep = (ASDEntryPtr)p;
1194 /* the new resourcefork entry */
1195 ep->entryID = EntryRSRCFork;
1196 ep->offset = loff + INFOLEN;
1197 ep->len = di_size;
1198 /* write the new appledouble entries to the file */
1199 if ((ofile = open(oFileRsrc, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
1200 fprintf(stderr, "%s: cannot create resource file: %s\n",
1201 name, strerror(errno));
1202 skipfile();
1203 return (FAIL);
1204 }
1205 write(ofile, buf, 70 + sizeof(ASDEntry));
1206 /* and add the resource data from tape */
1207 getfile(xtrfile, xtrskip);
1208
1209 (void) fchown(ofile, uid, gid);
1210 (void) fchmod(ofile, mode);
1211 (void) close(ofile);
1212 utimes(oFileRsrc, timep);
1213 (void) lsetflags(oFileRsrc, flags);
1214 return (GOOD);
1215 }
1216 /* NOTREACHED */
1217 }
1218 #endif /* DUMP_MACOSX */
1219
1220 int
1221 readxattr(char *buffer)
1222 {
1223 if (dflag)
1224 msg("reading EA data for inode %lu\n", curfile.ino);
1225
1226 curfile.name = "EA block";
1227 if (curfile.dip->di_size > XATTR_MAXSIZE) {
1228 fprintf(stderr, "EA size too big (%ld)", (long)curfile.dip->di_size);
1229 skipfile();
1230 return (FAIL);
1231 }
1232
1233 memset(xattrbuf, 0, XATTR_MAXSIZE);
1234 xattrlen = 0;
1235
1236 getfile(xtrxattr, xtrnull);
1237
1238 memcpy(buffer, xattrbuf, XATTR_MAXSIZE);
1239
1240 return (GOOD);
1241 }
1242
1243 /*
1244 * skip over bit maps on the tape
1245 */
1246 void
1247 skipmaps(void)
1248 {
1249
1250 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
1251 skipfile();
1252 }
1253
1254 /*
1255 * skip over a file on the tape
1256 */
1257 void
1258 skipfile(void)
1259 {
1260
1261 curfile.action = SKIP;
1262 getfile(xtrnull, xtrnull);
1263 }
1264
1265 /*
1266 * skip over any extended attributes.
1267 */
1268 void
1269 skipxattr(void)
1270 {
1271
1272 while (spcl.c_flags & DR_EXTATTRIBUTES)
1273 skipfile();
1274 }
1275
1276 /*
1277 * Extract a file from the tape.
1278 * When an allocated block is found it is passed to the fill function;
1279 * when an unallocated block (hole) is found, a zeroed buffer is passed
1280 * to the skip function.
1281 */
1282 void
1283 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
1284 {
1285 int i;
1286 volatile int curblk = 0;
1287 volatile quad_t size = spcl.c_dinode.di_size;
1288 volatile int last_write_was_hole = 0;
1289 quad_t origsize = size;
1290 static char clearedbuf[MAXBSIZE];
1291 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
1292 char junk[TP_BSIZE];
1293
1294 if (spcl.c_type == TS_END)
1295 panic("ran off end of tape\n");
1296 if (spcl.c_magic != NFS_MAGIC)
1297 panic("not at beginning of a file\n");
1298 if (!gettingfile && setjmp(restart) != 0)
1299 return;
1300 gettingfile++;
1301 loop:
1302 for (i = 0; i < spcl.c_count; i++) {
1303 if (readmapflag || spcl.c_addr[i]) {
1304 readtape(&buf[curblk++][0]);
1305 if (curblk == fssize / TP_BSIZE) {
1306 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1307 fssize : (curblk - 1) * TP_BSIZE + size));
1308 curblk = 0;
1309 last_write_was_hole = 0;
1310 }
1311 } else {
1312 if (curblk > 0) {
1313 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1314 curblk * TP_BSIZE :
1315 (curblk - 1) * TP_BSIZE + size));
1316 curblk = 0;
1317 }
1318 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
1319 TP_BSIZE : size));
1320 last_write_was_hole = 1;
1321 }
1322 if ((size -= TP_BSIZE) <= 0) {
1323 for (i++; i < spcl.c_count; i++)
1324 if (readmapflag || spcl.c_addr[i])
1325 readtape(junk);
1326 break;
1327 }
1328 }
1329 while (gethead(&spcl) != GOOD) {
1330 fprintf(stderr, "Incorrect block for %s at %ld blocks\n",
1331 curfile.name, (long)blksread);
1332 }
1333 if (size > 0) {
1334 if (spcl.c_type == TS_ADDR)
1335 goto loop;
1336 Dprintf(stdout,
1337 "Missing address (header) block for %s at %ld blocks\n",
1338 curfile.name, (long)blksread);
1339 }
1340 if (curblk > 0) {
1341 (*fill)((char *)buf, (size_t)((curblk * TP_BSIZE) + size));
1342 last_write_was_hole = 0;
1343 }
1344 if (size > 0) {
1345 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
1346 while (size > 0) {
1347 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
1348 (*skip)(clearedbuf, skp);
1349 size -= skp;
1350 }
1351 last_write_was_hole = 1;
1352 }
1353 if (last_write_was_hole) {
1354 FTRUNCATE(ofile, origsize);
1355 }
1356 if (!readingmaps)
1357 findinode(&spcl);
1358 gettingfile = 0;
1359 }
1360
1361 /*
1362 * Write out the next block of a file.
1363 */
1364 static void
1365 xtrfile(char *buf, size_t size)
1366 {
1367
1368 if (Nflag)
1369 return;
1370 if (write(ofile, buf, (int) size) == -1)
1371 err(1, "write error extracting inode %lu, name %s\nwrite",
1372 (unsigned long)curfile.ino, curfile.name);
1373 }
1374
1375 #ifdef DUMP_MACOSX
1376 static void
1377 xtrfilefinderinfo(char *buf, size_t size)
1378 {
1379 bcopy(buf, &gFndrInfo, size);
1380 }
1381 #endif /* DUMP_MACOSX */
1382
1383 /*
1384 * Skip over a hole in a file.
1385 */
1386 /* ARGSUSED */
1387 static void
1388 xtrskip(UNUSED(char *buf), size_t size)
1389 {
1390
1391 if (LSEEK(ofile, (OFF_T)size, SEEK_CUR) == -1)
1392 err(1, "seek error extracting inode %lu, name %s\nlseek",
1393 (unsigned long)curfile.ino, curfile.name);
1394 }
1395
1396 /*
1397 * Collect the next block of a symbolic link.
1398 */
1399 static void
1400 xtrlnkfile(char *buf, size_t size)
1401 {
1402
1403 pathlen += size;
1404 if (pathlen > MAXPATHLEN) {
1405 buf[size - 1] = '\0';
1406 errx(1, "symbolic link name: %s->%s%s; too long %d",
1407 curfile.name, lnkbuf, buf, pathlen);
1408 }
1409 (void) strcat(lnkbuf, buf);
1410 lnkbuf[pathlen] = '\0';
1411 }
1412
1413 /*
1414 * Skip over a hole in a symbolic link (should never happen).
1415 */
1416 /* ARGSUSED */
1417 static void
1418 xtrlnkskip(UNUSED(char *buf), UNUSED(size_t size))
1419 {
1420
1421 errx(1, "unallocated block in symbolic link %s", curfile.name);
1422 }
1423
1424 /*
1425 * Collect the next block of a bit map.
1426 */
1427 static void
1428 xtrmap(char *buf, size_t size)
1429 {
1430
1431 memmove(map, buf, size);
1432 map += size;
1433 }
1434
1435 /*
1436 * Skip over a hole in a bit map (should never happen).
1437 */
1438 /* ARGSUSED */
1439 static void
1440 xtrmapskip(UNUSED(char *buf), size_t size)
1441 {
1442
1443 panic("hole in map\n");
1444 map += size;
1445 }
1446
1447 /*
1448 * Noop, when an extraction function is not needed.
1449 */
1450 /* ARGSUSED */
1451 void
1452 xtrnull(UNUSED(char *buf), UNUSED(size_t size))
1453 {
1454
1455 return;
1456 }
1457
1458 #if COMPARE_ONTHEFLY
1459 /*
1460 * Compare the next block of a file.
1461 */
1462 static void
1463 xtrcmpfile(char *buf, size_t size)
1464 {
1465 static char cmpbuf[MAXBSIZE];
1466
1467 if (cmperror)
1468 return;
1469
1470 if (read(ifile, cmpbuf, size) != (ssize_t)size) {
1471 fprintf(stderr, "%s: size has changed.\n",
1472 curfile.name);
1473 cmperror = 1;
1474 return;
1475 }
1476
1477 if (memcmp(buf, cmpbuf, size) != 0) {
1478 fprintf(stderr, "%s: tape and disk copies are different\n",
1479 curfile.name);
1480 cmperror = 1;
1481 return;
1482 }
1483 }
1484
1485 /*
1486 * Skip over a hole in a file.
1487 */
1488 static void
1489 xtrcmpskip(UNUSED(char *buf), size_t size)
1490 {
1491 static char cmpbuf[MAXBSIZE];
1492 int i;
1493
1494 if (cmperror)
1495 return;
1496
1497 if (read(ifile, cmpbuf, size) != (ssize_t)size) {
1498 fprintf(stderr, "%s: size has changed.\n",
1499 curfile.name);
1500 cmperror = 1;
1501 return;
1502 }
1503
1504 for (i = 0; i < (int)size; ++i)
1505 if (cmpbuf[i] != '\0') {
1506 fprintf(stderr, "%s: tape and disk copies are different\n",
1507 curfile.name);
1508 cmperror = 1;
1509 return;
1510 }
1511 }
1512 #endif /* COMPARE_ONTHEFLY */
1513
1514 static void
1515 xtrxattr(char *buf, size_t size)
1516 {
1517 if (xattrlen + size > XATTR_MAXSIZE) {
1518 fprintf(stderr, "EA size too big (%ld)", (long)xattrlen + size);
1519 return;
1520 }
1521 memcpy(xattrbuf + xattrlen, buf, size);
1522 xattrlen += size;
1523 }
1524
1525 #if !COMPARE_ONTHEFLY
1526 static int
1527 do_cmpfiles(int fd_tape, int fd_disk, OFF_T size)
1528 {
1529 static char buf_tape[BUFSIZ];
1530 static char buf_disk[BUFSIZ];
1531 ssize_t n_tape;
1532 ssize_t n_disk;
1533
1534 while (size > 0) {
1535 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1536 close(fd_tape), close(fd_disk);
1537 panic("do_cmpfiles: unexpected EOF[1]");
1538 }
1539 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1540 close(fd_tape), close(fd_disk);
1541 panic("do_cmpfiles: unexpected EOF[2]");
1542 }
1543 if (n_tape != n_disk) {
1544 close(fd_tape), close(fd_disk);
1545 panic("do_cmpfiles: sizes different!");
1546 }
1547 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1548 size -= n_tape;
1549 }
1550 return (0);
1551 }
1552
1553 /* for debugging compare problems */
1554 #undef COMPARE_FAIL_KEEP_FILE
1555
1556 static
1557 #ifdef COMPARE_FAIL_KEEP_FILE
1558 /* return true if tapefile should be unlinked after compare */
1559 int
1560 #else
1561 void
1562 #endif
1563 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1564 {
1565 struct STAT sbuf_tape;
1566 int fd_tape, fd_disk;
1567
1568 if (STAT(tapefile, &sbuf_tape) != 0) {
1569 panic("can't lstat tmp file %s: %s\n", tapefile,
1570 strerror(errno));
1571 do_compare_error;
1572 }
1573
1574 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1575 fprintf(stderr,
1576 "%s: size changed from %lld to %lld.\n",
1577 diskfile, (long long)sbuf_tape.st_size, (long long)sbuf_disk->st_size);
1578 do_compare_error;
1579 #ifdef COMPARE_FAIL_KEEP_FILE
1580 return (0);
1581 #else
1582 return;
1583 #endif
1584 }
1585
1586 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1587 panic("can't open %s: %s\n", tapefile, strerror(errno));
1588 do_compare_error;
1589 }
1590 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1591 close(fd_tape);
1592 panic("can't open %s: %s\n", diskfile, strerror(errno));
1593 do_compare_error;
1594 }
1595
1596 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1597 fprintf(stderr, "%s: tape and disk copies are different\n",
1598 diskfile);
1599 close(fd_tape);
1600 close(fd_disk);
1601 do_compare_error;
1602 #ifdef COMPARE_FAIL_KEEP_FILE
1603 /* rename the file to live in /tmp */
1604 /* rename `tapefile' to /tmp/<basename of diskfile> */
1605 {
1606 char *p = strrchr(diskfile, '/');
1607 char newname[MAXPATHLEN];
1608 if (!p) {
1609 panic("can't find / in %s\n", diskfile);
1610 }
1611 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1612 if (rename(tapefile, newname)) {
1613 panic("rename from %s to %s failed: %s\n",
1614 tapefile, newname,
1615 strerror(errno));
1616 } else {
1617 fprintf(stderr, "*** %s saved to %s\n",
1618 tapefile, newname);
1619 }
1620 }
1621
1622 /* don't unlink the file (it's not there anymore */
1623 /* anyway) */
1624 return (0);
1625 #else
1626 return;
1627 #endif
1628 }
1629 close(fd_tape);
1630 close(fd_disk);
1631 #ifdef COMPARE_FAIL_KEEP_FILE
1632 return (1);
1633 #endif
1634 }
1635 #endif /* !COMPARE_ONTHEFLY */
1636
1637 static void
1638 compareattr(char *name)
1639 {
1640 int xattr_done = 0;
1641
1642 while (spcl.c_flags & DR_EXTATTRIBUTES) {
1643 switch (spcl.c_extattributes) {
1644 case EXT_MACOSFNDRINFO:
1645 msg("MacOSX not supported for comparision in this version, skipping\n");
1646 skipfile();
1647 break;
1648 case EXT_MACOSRESFORK:
1649 msg("MacOSX not supported for comparision in this version, skipping\n");
1650 skipfile();
1651 break;
1652 case EXT_XATTR: {
1653 char xattr[XATTR_MAXSIZE];
1654
1655 if (readxattr(xattr) == GOOD) {
1656 if (xattr_compare(name, xattr) == FAIL)
1657 do_compare_error;
1658 xattr_done = 1;
1659 }
1660 else
1661 do_compare_error;
1662 break;
1663 }
1664 default:
1665 msg("unexpected inode extension %ld, skipping\n", spcl.c_extattributes);
1666 skipfile();
1667 break;
1668 }
1669 }
1670 if (!xattr_done && xattr_compare(name, NULL) == FAIL)
1671 do_compare_error;
1672 }
1673
1674 #if !COMPARE_ONTHEFLY
1675 static char tmpfilename[MAXPATHLEN];
1676 #endif
1677
1678 void
1679 comparefile(char *name)
1680 {
1681 mode_t mode;
1682 uid_t uid;
1683 uid_t gid;
1684 unsigned int flags;
1685 unsigned long newflags;
1686 struct STAT sb;
1687 int r;
1688 #if !COMPARE_ONTHEFLY
1689 static char *tmpfile = NULL;
1690 struct STAT stemp;
1691 #endif
1692 curfile.name = name;
1693 curfile.action = USING;
1694 mode = curfile.dip->di_mode;
1695 flags = curfile.dip->di_flags;
1696 uid = curfile.dip->di_uid;
1697 gid = curfile.dip->di_gid;
1698
1699 if ((mode & IFMT) == IFSOCK) {
1700 Vprintf(stdout, "skipped socket %s\n", name);
1701 skipfile();
1702 return;
1703 }
1704
1705 if ((r = LSTAT(name, &sb)) != 0) {
1706 warn("unable to stat %s", name);
1707 do_compare_error;
1708 skipfile();
1709 return;
1710 }
1711
1712 Vprintf(stdout, "comparing %s (size: %lld, mode: 0%o)\n", name,
1713 (long long)sb.st_size, mode);
1714
1715 if (sb.st_mode != mode) {
1716 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1717 name, mode & 07777, sb.st_mode & 07777);
1718 do_compare_error;
1719 }
1720 if (sb.st_uid != uid) {
1721 fprintf(stderr, "%s: uid changed from %d to %d.\n",
1722 name, uid, sb.st_uid);
1723 do_compare_error;
1724 }
1725 if (sb.st_gid != gid) {
1726 fprintf(stderr, "%s: gid changed from %d to %d.\n",
1727 name, gid, sb.st_gid);
1728 do_compare_error;
1729 }
1730 #ifdef __linux__
1731 if (lgetflags(name, &newflags) < 0) {
1732 if (flags != 0) {
1733 warn("%s: lgetflags failed", name);
1734 do_compare_error;
1735 }
1736 }
1737 else {
1738 if (newflags != flags) {
1739 fprintf(stderr, "%s: flags changed from 0x%08x to 0x%08lx.\n",
1740 name, flags, newflags);
1741 do_compare_error;
1742 }
1743 }
1744 #endif
1745 if (spcl.c_flags & DR_METAONLY) {
1746 skipfile();
1747 return;
1748 }
1749 switch (mode & IFMT) {
1750 default:
1751 skipfile();
1752 return;
1753
1754 case IFSOCK:
1755 skipfile();
1756 return;
1757
1758 case IFDIR:
1759 skipfile();
1760 compareattr(name);
1761 return;
1762
1763 case IFLNK: {
1764 char lbuf[MAXPATHLEN + 1];
1765 int lsize;
1766
1767 if (!(sb.st_mode & S_IFLNK)) {
1768 fprintf(stderr, "%s: is no longer a symbolic link\n",
1769 name);
1770 do_compare_error;
1771 return;
1772 }
1773 lnkbuf[0] = '\0';
1774 pathlen = 0;
1775 getfile(xtrlnkfile, xtrlnkskip);
1776 if (pathlen == 0) {
1777 fprintf(stderr,
1778 "%s: zero length symbolic link (ignored)\n",
1779 name);
1780 do_compare_error;
1781 return;
1782 }
1783 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1784 panic("readlink of %s failed: %s\n", name,
1785 strerror(errno));
1786 do_compare_error;
1787 }
1788 lbuf[lsize] = 0;
1789 if (strcmp(lbuf, lnkbuf) != 0) {
1790 fprintf(stderr,
1791 "%s: symbolic link changed from %s to %s.\n",
1792 name, lnkbuf, lbuf);
1793 do_compare_error;
1794 return;
1795 }
1796 compareattr(name);
1797 return;
1798 }
1799
1800 case IFCHR:
1801 case IFBLK:
1802 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1803 fprintf(stderr, "%s: no longer a special file\n",
1804 name);
1805 do_compare_error;
1806 skipfile();
1807 return;
1808 }
1809
1810 if (sb.st_rdev != (dev_t)curfile.dip->di_rdev) {
1811 fprintf(stderr,
1812 "%s: device changed from %d,%d to %d,%d.\n",
1813 name,
1814 major(curfile.dip->di_rdev),
1815 minor(curfile.dip->di_rdev),
1816 major(sb.st_rdev),
1817 minor(sb.st_rdev));
1818 do_compare_error;
1819 }
1820 skipfile();
1821 compareattr(name);
1822 return;
1823
1824 case IFREG:
1825 #if COMPARE_ONTHEFLY
1826 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1827 warn("can't open %s", name);
1828 skipfile();
1829 do_compare_error;
1830 }
1831 else {
1832 cmperror = 0;
1833 getfile(xtrcmpfile, xtrcmpskip);
1834 if (!cmperror) {
1835 char c;
1836 if (read(ifile, &c, 1) != 0) {
1837 fprintf(stderr, "%s: size has changed.\n",
1838 name);
1839 cmperror = 1;
1840 }
1841 }
1842 if (cmperror)
1843 do_compare_error;
1844 close(ifile);
1845 }
1846 #else
1847 if (tmpfile == NULL) {
1848 /* argument to mktemp() must not be in RO space: */
1849 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1850 tmpfile = mktemp(&tmpfilename[0]);
1851 }
1852 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1853 panic("cannot delete tmp file %s: %s\n",
1854 tmpfile, strerror(errno));
1855 }
1856 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1857 panic("cannot create file temp file %s: %s\n",
1858 name, strerror(errno));
1859 }
1860 getfile(xtrfile, xtrskip);
1861 (void) close(ofile);
1862 #ifdef COMPARE_FAIL_KEEP_FILE
1863 if (cmpfiles(tmpfile, name, &sb))
1864 unlink(tmpfile);
1865 #else
1866 cmpfiles(tmpfile, name, &sb);
1867 unlink(tmpfile);
1868 #endif
1869 #endif /* COMPARE_ONTHEFLY */
1870 compareattr(name);
1871 return;
1872 }
1873 /* NOTREACHED */
1874 }
1875
1876 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1877 static void (*readtape_func)(char *) = readtape_set;
1878
1879 /*
1880 * Read TP_BSIZE blocks from the input.
1881 * Handle read errors, and end of media.
1882 * Decompress compressed blocks.
1883 */
1884 static void
1885 readtape(char *buf)
1886 {
1887 (*readtape_func)(buf); /* call the actual processing routine */
1888 }
1889
1890 /*
1891 * Set function pointer for readtape() routine. zflag and magtapein must
1892 * be correctly set before the first call to readtape().
1893 */
1894 static void
1895 readtape_set(char *buf)
1896 {
1897 if (!zflag)
1898 readtape_func = readtape_uncompr;
1899 else {
1900 newcomprbuf(ntrec);
1901 if (magtapein)
1902 readtape_func = readtape_comprtape;
1903 else
1904 readtape_func = readtape_comprfile;
1905 }
1906 readtape(buf);
1907 }
1908
1909 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1910
1911 /*
1912 * This is the original readtape(), it's used for reading uncompressed input.
1913 * Read TP_BSIZE blocks from the input.
1914 * Handle read errors, and end of media.
1915 */
1916 static void
1917 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1918 readtape_uncompr(char *buf)
1919 #else
1920 readtape(char *buf)
1921 #endif
1922 {
1923 ssize_t rd, newvol, i;
1924 int cnt, seek_failed;
1925
1926 if (blkcnt < numtrec) {
1927 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1928 blksread++;
1929 tpblksread++;
1930 return;
1931 }
1932 tbufptr = tapebuf;
1933 for (i = 0; i < ntrec; i++)
1934 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1935 if (numtrec == 0)
1936 numtrec = ntrec;
1937 cnt = ntrec * TP_BSIZE;
1938 rd = 0;
1939 #ifdef USE_QFA
1940 if (createtapeposflag)
1941 (void)GetTapePos(&curtapepos);
1942 #endif
1943 getmore:
1944 #ifdef RRESTORE
1945 if (!Afile && host)
1946 i = rmtread(&tapebuf[rd], cnt);
1947 else
1948 #endif
1949 i = read(mt, &tapebuf[rd], cnt);
1950
1951 /*
1952 * Check for mid-tape short read error.
1953 * If found, skip rest of buffer and start with the next.
1954 */
1955 if (!pipein && numtrec < ntrec && i > 0) {
1956 Dprintf(stdout, "mid-media short read error.\n");
1957 numtrec = ntrec;
1958 }
1959 /*
1960 * Handle partial block read.
1961 */
1962 if (pipein && i == 0 && rd > 0)
1963 i = rd;
1964 else if (i > 0 && i != ntrec * TP_BSIZE) {
1965 if (pipein) {
1966 rd += i;
1967 cnt -= i;
1968 if (cnt > 0)
1969 goto getmore;
1970 i = rd;
1971 } else {
1972 /*
1973 * Short read. Process the blocks read.
1974 */
1975 if (i % TP_BSIZE != 0)
1976 Vprintf(stdout,
1977 "partial block read: %ld should be %ld\n",
1978 (long)i, ntrec * TP_BSIZE);
1979 numtrec = i / TP_BSIZE;
1980 }
1981 }
1982 /*
1983 * Handle read error.
1984 */
1985 if (i < 0) {
1986 fprintf(stderr, "Tape read error while ");
1987 switch (curfile.action) {
1988 default:
1989 fprintf(stderr, "trying to set up tape\n");
1990 break;
1991 case UNKNOWN:
1992 fprintf(stderr, "trying to resynchronize\n");
1993 break;
1994 case USING:
1995 fprintf(stderr, "restoring %s\n", curfile.name);
1996 break;
1997 case SKIP:
1998 fprintf(stderr, "skipping over inode %lu\n",
1999 (unsigned long)curfile.ino);
2000 break;
2001 }
2002 if (!yflag && !reply("continue"))
2003 exit(1);
2004 i = ntrec * TP_BSIZE;
2005 memset(tapebuf, 0, (size_t)i);
2006 #ifdef RRESTORE
2007 if (!Afile && host)
2008 seek_failed = (rmtseek(i, 1) < 0);
2009 else
2010 #endif
2011 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (OFF_T)-1);
2012
2013 if (seek_failed) {
2014 warn("continuation failed");
2015 if (!yflag && !reply("assume end-of-tape and continue"))
2016 exit(1);
2017 i = 0;
2018 }
2019 }
2020 /*
2021 * Handle end of tape.
2022 */
2023 if (i == 0) {
2024 Vprintf(stdout, "End-of-tape encountered\n");
2025 if (!pipein) {
2026 newvol = volno + 1;
2027 volno = 0;
2028 numtrec = 0;
2029 getvol(newvol);
2030 readtape(buf);
2031 return;
2032 }
2033 if (rd % TP_BSIZE != 0)
2034 panic("partial block read: %d should be %d\n",
2035 rd, ntrec * TP_BSIZE);
2036 terminateinput();
2037 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
2038 }
2039 blkcnt = 0;
2040 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2041 blksread++;
2042 tpblksread++;
2043 }
2044
2045 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
2046
2047 /*
2048 * Read a compressed format block from a file or pipe and uncompress it.
2049 * Attempt to handle read errors, and end of file.
2050 */
2051 static void
2052 readtape_comprfile(char *buf)
2053 {
2054 long rl, size, i, ret;
2055 int newvol;
2056 struct tapebuf *tpb;
2057
2058 if (blkcnt < numtrec) {
2059 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2060 blksread++;
2061 tpblksread++;
2062 return;
2063 }
2064 /* need to read the next block */
2065 tbufptr = tapebuf;
2066 for (i = 0; i < ntrec; i++)
2067 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
2068 numtrec = ntrec;
2069 tpb = (struct tapebuf *) tapebuf;
2070
2071 /* read the block prefix */
2072 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
2073 converttapebuf(tpb);
2074
2075 if (Vflag && (ret == 0 || rl < (int)PREFIXSIZE || tpb->length == 0))
2076 ret = 0;
2077 if (ret <= 0)
2078 goto readerr;
2079
2080 /* read the data */
2081 size = tpb->length;
2082 if (size > bufsize) {
2083 /* something's wrong */
2084 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
2085 bufsize, size);
2086 size = bufsize;
2087 tpb->length = bufsize;
2088 }
2089 ret = read_a_block(mt, tpb->buf, size, &rl);
2090 if (ret <= 0)
2091 goto readerr;
2092
2093 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
2094 if (tbufptr == NULL) {
2095 msg_read_error("File decompression error while");
2096 if (!yflag && !reply("continue"))
2097 exit(1);
2098 memset(tapebuf, 0, bufsize);
2099 tbufptr = tapebuf;
2100 }
2101
2102 blkcnt = 0;
2103 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2104 blksread++;
2105 tpblksread++;
2106 return;
2107
2108 readerr:
2109 /* Errors while reading from a file or pipe are catastrophic. Since
2110 * there are no block boundaries, it's impossible to bypass the
2111 * block in error and find the start of the next block.
2112 */
2113 if (ret == 0) {
2114 /* It's possible to have multiple input files using -M
2115 * and -f file1,file2...
2116 */
2117 Vprintf(stdout, "End-of-File encountered\n");
2118 if (!pipein) {
2119 newvol = volno + 1;
2120 volno = 0;
2121 numtrec = 0;
2122 getvol(newvol);
2123 readtape(buf);
2124 return;
2125 }
2126 }
2127 msg_read_error("Read error while");
2128 /* if (!yflag && !reply("continue")) */
2129 exit(1);
2130 }
2131
2132 /*
2133 * Read compressed data from a tape and uncompress it.
2134 * Handle read errors, and end of media.
2135 * Since a tape consists of separate physical blocks, we try
2136 * to recover from errors by repositioning the tape to the next
2137 * block.
2138 */
2139 static void
2140 readtape_comprtape(char *buf)
2141 {
2142 long rl, size, i;
2143 int ret, newvol;
2144 struct tapebuf *tpb;
2145 struct mtop tcom;
2146
2147 if (blkcnt < numtrec) {
2148 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2149 blksread++;
2150 tpblksread++;
2151 return;
2152 }
2153 /* need to read the next block */
2154 tbufptr = tapebuf;
2155 for (i = 0; i < ntrec; i++)
2156 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
2157 numtrec = ntrec;
2158 tpb = (struct tapebuf *) tapebuf;
2159
2160 /* read the block */
2161 size = bufsize + PREFIXSIZE;
2162 ret = read_a_block(mt, tapebuf, size, &rl);
2163 if (ret <= 0)
2164 goto readerr;
2165
2166 converttapebuf(tpb);
2167 tbufptr = decompress_tapebuf(tpb, rl);
2168 if (tbufptr == NULL) {
2169 msg_read_error("Tape decompression error while");
2170 if (!yflag && !reply("continue"))
2171 exit(1);
2172 memset(tapebuf, 0, PREFIXSIZE + bufsize);
2173 tbufptr = tapebuf;
2174 }
2175 goto moverecord;
2176
2177 readerr:
2178 /* Handle errors: EOT switches to the next volume, other errors
2179 * attempt to position the tape to the next block.
2180 */
2181 if (ret == 0) {
2182 Vprintf(stdout, "End-of-tape encountered\n");
2183 newvol = volno + 1;
2184 volno = 0;
2185 numtrec = 0;
2186 getvol(newvol);
2187 readtape(buf);
2188 return;
2189 }
2190
2191 msg_read_error("Tape read error while");
2192 if (!yflag && !reply("continue"))
2193 exit(1);
2194 memset(tapebuf, 0, PREFIXSIZE + bufsize);
2195 tbufptr = tapebuf;
2196
2197 #ifdef RRESTORE
2198 if (host)
2199 rl = rmtioctl(MTFSR, 1);
2200 else
2201 #endif
2202 {
2203 tcom.mt_op = MTFSR;
2204 tcom.mt_count = 1;
2205 rl = ioctl(mt, MTIOCTOP, &tcom);
2206 }
2207
2208 if (rl < 0) {
2209 warn("continuation failed");
2210 if (!yflag && !reply("assume end-of-tape and continue"))
2211 exit(1);
2212 ret = 0; /* end of tape */
2213 goto readerr;
2214 }
2215
2216 moverecord:
2217 blkcnt = 0;
2218 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2219 blksread++;
2220 tpblksread++;
2221 }
2222
2223 /*
2224 * Decompress a struct tapebuf into a buffer. readsize is the size read
2225 * from the tape/file and is used for error messages. Returns a pointer
2226 * to the location of the uncompressed buffer or NULL on errors.
2227 * Adjust numtrec and complain for a short block.
2228 */
2229 static char *
2230 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
2231 {
2232 /* If zflag is on, all blocks have a struct tapebuf prefix */
2233 /* zflag gets set in setup() from the dump header */
2234 int cresult, blocklen;
2235 unsigned long worklen;
2236 char *output = NULL,*reason = NULL, *lengtherr = NULL;
2237
2238 /* build a length error message */
2239 blocklen = tpbin->length;
2240 if (readsize < blocklen + (int)PREFIXSIZE)
2241 lengtherr = "short";
2242 else
2243 if (readsize > blocklen + (int)PREFIXSIZE)
2244 lengtherr = "long";
2245
2246 worklen = comprlen;
2247 cresult = 1;
2248 if (tpbin->compressed) {
2249 /* uncompress whatever we read, if it fails, complain later */
2250 if (tpbin->flags == COMPRESS_ZLIB) {
2251 #ifndef HAVE_ZLIB
2252 errx(1,"This restore version doesn't support zlib decompression");
2253 #else
2254 cresult = uncompress(comprbuf, &worklen,
2255 tpbin->buf, blocklen);
2256 output = comprbuf;
2257 switch (cresult) {
2258 case Z_OK:
2259 break;
2260 case Z_MEM_ERROR:
2261 reason = "not enough memory";
2262 break;
2263 case Z_BUF_ERROR:
2264 reason = "buffer too small";
2265 break;
2266 case Z_DATA_ERROR:
2267 reason = "data error";
2268 break;
2269 default:
2270 reason = "unknown";
2271 }
2272 if (cresult == Z_OK)
2273 cresult = 1;
2274 else
2275 cresult = 0;
2276 #endif /* HAVE_ZLIB */
2277 }
2278 if (tpbin->flags == COMPRESS_BZLIB) {
2279 #ifndef HAVE_BZLIB
2280 errx(1,"This restore version doesn't support bzlib decompression");
2281 #else
2282 unsigned int worklen2 = worklen;
2283 cresult = BZ2_bzBuffToBuffDecompress(
2284 comprbuf, &worklen2,
2285 tpbin->buf, blocklen, 0, 0);
2286 worklen = worklen2;
2287 output = comprbuf;
2288 switch (cresult) {
2289 case BZ_OK:
2290 break;
2291 case BZ_MEM_ERROR:
2292 reason = "not enough memory";
2293 break;
2294 case BZ_OUTBUFF_FULL:
2295 reason = "buffer too small";
2296 break;
2297 case BZ_DATA_ERROR:
2298 case BZ_DATA_ERROR_MAGIC:
2299 case BZ_UNEXPECTED_EOF:
2300 reason = "data error";
2301 break;
2302 default:
2303 reason = "unknown";
2304 }
2305 if (cresult == BZ_OK)
2306 cresult = 1;
2307 else
2308 cresult = 0;
2309 #endif /* HAVE_BZLIB */
2310 }
2311 if (tpbin->flags == COMPRESS_LZO) {
2312 #ifndef HAVE_LZO
2313 errx(1,"This restore version doesn't support lzo decompression");
2314 #else
2315 lzo_uint worklen2 = worklen;
2316 cresult = lzo1x_decompress(tpbin->buf, blocklen,
2317 comprbuf, &worklen2, NULL);
2318 worklen = worklen2;
2319 output = comprbuf;
2320 switch (cresult) {
2321 case LZO_E_OK:
2322 break;
2323 case LZO_E_ERROR:
2324 case LZO_E_EOF_NOT_FOUND:
2325 reason = "data error";
2326 break;
2327 default:
2328 reason = "unknown";
2329 }
2330 if (cresult == LZO_E_OK)
2331 cresult = 1;
2332 else
2333 cresult = 0;
2334 #endif /* HAVE_LZO */
2335 }
2336 }
2337 else {
2338 output = tpbin->buf;
2339 worklen = blocklen;
2340 }
2341 if (cresult) {
2342 numtrec = worklen / TP_BSIZE;
2343 if (worklen % TP_BSIZE != 0)
2344 reason = "length mismatch";
2345 }
2346 if (reason) {
2347 if (lengtherr)
2348 fprintf(stderr, "%s compressed block: %d expected: %u\n",
2349 lengtherr, readsize, tpbin->length + PREFIXSIZE);
2350 fprintf(stderr, "decompression error, block %ld: %s\n",
2351 tpblksread+1, reason);
2352 if (!cresult)
2353 output = NULL;
2354 }
2355 return output;
2356 }
2357
2358 /*
2359 * Print an error message for a read error.
2360 * This was exteracted from the original readtape().
2361 */
2362 static void
2363 msg_read_error(char *m)
2364 {
2365 switch (curfile.action) {
2366 default:
2367 fprintf(stderr, "%s trying to set up tape\n", m);
2368 break;
2369 case UNKNOWN:
2370 fprintf(stderr, "%s trying to resynchronize\n", m);
2371 break;
2372 case USING:
2373 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
2374 break;
2375 case SKIP:
2376 fprintf(stderr, "%s skipping over inode %lu\n", m,
2377 (unsigned long)curfile.ino);
2378 break;
2379 }
2380 }
2381 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
2382
2383 /*
2384 * Read the first block and get the blocksize from it. Test
2385 * for a compressed dump tape/file. setup() will make the final
2386 * determination by checking the compressed flag if gethead()
2387 * finds a valid header. The test here is necessary to offset the buffer
2388 * by the size of the compressed prefix. zflag is set here so that
2389 * readtape_set can set the correct function pointer for readtape().
2390 * Note that the first block of each tape/file is not compressed
2391 * and does not have a prefix.
2392 */
2393 static void
2394 findtapeblksize(void)
2395 {
2396 long i;
2397 size_t len;
2398 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
2399 struct s_spcl spclpt;
2400
2401 for (i = 0; i < ntrec; i++)
2402 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
2403 blkcnt = 0;
2404 tbufptr = tapebuf;
2405 /*
2406 * For a pipe or file, read in the first record. For a tape, read
2407 * the first block.
2408 */
2409 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
2410
2411 if (read_a_block(mt, tapebuf, len, &i) <= 0)
2412 errx(1, "Tape read error on first record");
2413
2414 memcpy(&spclpt, tapebuf, TP_BSIZE);
2415 cvtflag = 0;
2416 if (converthead(&spclpt) == FAIL) {
2417 cvtflag++;
2418 if (converthead(&spclpt) == FAIL) {
2419 /* Special case for old compressed tapes with prefix */
2420 if (magtapein && (i % TP_BSIZE != 0))
2421 goto oldformat;
2422 errx(1, "Tape is not a dump tape");
2423 }
2424 fprintf(stderr, "Converting to new file system format.\n");
2425 }
2426 /*
2427 * If the input is from a file or a pipe, we read TP_BSIZE
2428 * bytes looking for a dump header. If the dump is compressed
2429 * we need to read in the rest of the block, as determined
2430 * by c_ntrec in the dump header. The first block of the
2431 * dump is not compressed and does not have a prefix.
2432 */
2433 if (!magtapein) {
2434 if (spclpt.c_type == TS_TAPE
2435 && spclpt.c_flags & DR_COMPRESSED) {
2436 /* It's a compressed dump file, read in the */
2437 /* rest of the block based on spclpt.c_ntrec. */
2438 if (spclpt.c_ntrec > ntrec)
2439 errx(1, "Tape blocksize is too large, use "
2440 "\'-b %d\' ", spclpt.c_ntrec);
2441 ntrec = spclpt.c_ntrec;
2442 len = (ntrec - 1) * TP_BSIZE;
2443 zflag = 1;
2444 }
2445 else {
2446 /* read in the rest of the block based on bufsize */
2447 len = bufsize - TP_BSIZE;
2448 }
2449 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
2450 || (i != (long)len && i % TP_BSIZE != 0))
2451 errx(1,"Error reading dump file header");
2452 tbufptr = tapebuf;
2453 numtrec = ntrec;
2454 Vprintf(stdout, "Input block size is %ld\n", ntrec);
2455 return;
2456 } /* if (!magtapein) */
2457
2458 /*
2459 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
2460 * If the value of ntrec is too large, we read less than
2461 * what we asked for; adjust the value of ntrec and test for
2462 * a compressed dump tape.
2463 */
2464 if (i % TP_BSIZE != 0) {
2465 oldformat:
2466 /* may be old format compressed dump tape with a prefix */
2467 memcpy(&spclpt, tpb->buf, TP_BSIZE);
2468 cvtflag = 0;
2469 if (converthead(&spclpt) == FAIL) {
2470 cvtflag++;
2471 if (converthead(&spclpt) == FAIL)
2472 errx(1, "Tape is not a dump tape");
2473 fprintf(stderr, "Converting to new file system format.\n");
2474 }
2475 if (i % TP_BSIZE == PREFIXSIZE
2476 && tpb->compressed == 0
2477 && spclpt.c_type == TS_TAPE
2478 && spclpt.c_flags & DR_COMPRESSED) {
2479 zflag = 1;
2480 tbufptr = tpb->buf;
2481 if (tpb->length > bufsize)
2482 errx(1, "Tape blocksize is too large, use "
2483 "\'-b %d\' ", tpb->length / TP_BSIZE);
2484 }
2485 else
2486 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2487 i, TP_BSIZE);
2488 }
2489 ntrec = i / TP_BSIZE;
2490 if (spclpt.c_type == TS_TAPE) {
2491 if (spclpt.c_flags & DR_COMPRESSED)
2492 zflag = 1;
2493 if (spclpt.c_ntrec > ntrec)
2494 errx(1, "Tape blocksize is too large, use "
2495 "\'-b %d\' ", spclpt.c_ntrec);
2496 }
2497 numtrec = ntrec;
2498 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
2499 }
2500
2501 /*
2502 * Read a block of data handling all of the messy details.
2503 */
2504 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
2505 {
2506 long i = 1, size;
2507
2508 size = len;
2509 while (size > 0) {
2510 #ifdef RRESTORE
2511 if (!Afile && host)
2512 i = rmtread(buf, size);
2513 else
2514 #endif
2515 i = read(fd, buf, size);
2516
2517 if (i <= 0)
2518 break; /* EOD or error */
2519 size -= i;
2520 if (magtapein)
2521 break; /* block at a time for mt */
2522 buf += i;
2523 }
2524 *lengthread = len - size;
2525 return i;
2526 }
2527
2528 void
2529 closemt(void)
2530 {
2531
2532 if (mt < 0)
2533 return;
2534 #ifdef RRESTORE
2535 if (!Afile && host)
2536 rmtclose();
2537 else
2538 #endif
2539 (void) close(mt);
2540 }
2541
2542 static void
2543 setmagtapein(void) {
2544 struct mtget mt_stat;
2545 static int done = 0;
2546 if (done)
2547 return;
2548 done = 1;
2549 if (!pipein) {
2550 /* need to know if input is really from a tape */
2551 #ifdef RRESTORE
2552 if (host)
2553 magtapein = !lflag;
2554 else
2555 #endif
2556 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
2557 }
2558
2559 Vprintf(stdout,"Input is from a %s %s\n",
2560 host ? "remote" : "local",
2561 magtapein ? "tape" :
2562 Vflag ? "multi-volume (no tape)" : "file/pipe");
2563 }
2564
2565 /*
2566 * Read the next block from the tape.
2567 * Check to see if it is one of several vintage headers.
2568 * If it is an old style header, convert it to a new style header.
2569 * If it is not any valid header, return an error.
2570 */
2571 static int
2572 gethead(struct s_spcl *buf)
2573 {
2574 readtape((char *)buf);
2575 return converthead(buf);
2576 }
2577
2578 static int
2579 converthead(struct s_spcl *buf)
2580 {
2581 int32_t i;
2582 union {
2583 quad_t qval;
2584 int32_t val[2];
2585 } qcvt;
2586 union u_ospcl {
2587 char dummy[TP_BSIZE];
2588 struct s_ospcl {
2589 int32_t c_type;
2590 int32_t c_date;
2591 int32_t c_ddate;
2592 int32_t c_volume;
2593 int32_t c_tapea;
2594 u_int16_t c_inumber;
2595 int32_t c_magic;
2596 int32_t c_checksum;
2597 struct odinode {
2598 u_int16_t odi_mode;
2599 u_int16_t odi_nlink;
2600 u_int16_t odi_uid;
2601 u_int16_t odi_gid;
2602 int32_t odi_size;
2603 int32_t odi_rdev;
2604 char odi_addr[36];
2605 int32_t odi_atime;
2606 int32_t odi_mtime;
2607 int32_t odi_ctime;
2608 } c_dinode;
2609 int32_t c_count;
2610 char c_fill[256];
2611 } s_ospcl;
2612 } u_ospcl;
2613
2614 if (!cvtflag) {
2615 if (buf->c_magic != NFS_MAGIC) {
2616 if (swabi(buf->c_magic) != NFS_MAGIC)
2617 return (FAIL);
2618 if (!Bcvt) {
2619 Vprintf(stdout, "Note: Doing Byte swapping\n");
2620 Bcvt = 1;
2621 }
2622 }
2623 if (checksum((int *)buf) == FAIL)
2624 return (FAIL);
2625 if (Bcvt)
2626 swabst((u_char *)"8i4s1l29i528bi192b4i", (u_char *)buf);
2627 goto good;
2628 }
2629 memcpy(&u_ospcl.s_ospcl, buf, TP_BSIZE);
2630 if (checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
2631 return(FAIL);
2632 if (u_ospcl.s_ospcl.c_magic == OFS_MAGIC) {
2633 memset((char *)buf, 0, (long)TP_BSIZE);
2634 buf->c_type = u_ospcl.s_ospcl.c_type;
2635 buf->c_date = u_ospcl.s_ospcl.c_date;
2636 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
2637 buf->c_volume = u_ospcl.s_ospcl.c_volume;
2638 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
2639 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
2640 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
2641 buf->c_magic = u_ospcl.s_ospcl.c_magic;
2642 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
2643 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
2644 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
2645 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
2646 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
2647 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
2648 #if defined(__linux__) || defined(sunos)
2649 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
2650 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2651 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2652 #else /* __linux__ || sunos */
2653 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
2654 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2655 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2656 #endif /* __linux__ || sunos */
2657 buf->c_count = u_ospcl.s_ospcl.c_count;
2658 memmove(buf->c_addr, u_ospcl.s_ospcl.c_fill, (long)256);
2659 }
2660 else if (u_ospcl.s_ospcl.c_magic == FS_UFS2_MAGIC) {
2661 buf->c_date = (int32_t)(*(int64_t *)&u_ospcl.dummy[896]);
2662 buf->c_ddate = (int32_t)(*(int64_t *)&u_ospcl.dummy[904]);
2663 buf->c_tapea = (int32_t)(*(int64_t *)&u_ospcl.dummy[912]);
2664 buf->c_firstrec = (int32_t)(*(int64_t *)&u_ospcl.dummy[920]);
2665 buf->c_ntrec = 0;
2666 buf->c_extattributes = 0;
2667 buf->c_flags |= DR_NEWINODEFMT;
2668 ufs2flag = 1;
2669 }
2670 else
2671 return(FAIL);
2672 buf->c_magic = NFS_MAGIC;
2673
2674 good:
2675 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
2676 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
2677 qcvt.qval = buf->c_dinode.di_size;
2678 if (qcvt.val[0] || qcvt.val[1]) {
2679 Vprintf(stdout, "Note: Doing Quad swapping\n");
2680 Qcvt = 1;
2681 }
2682 }
2683 if (Qcvt) {
2684 qcvt.qval = buf->c_dinode.di_size;
2685 i = qcvt.val[1];
2686 qcvt.val[1] = qcvt.val[0];
2687 qcvt.val[0] = i;
2688 buf->c_dinode.di_size = qcvt.qval;
2689 }
2690 readmapflag = 0;
2691
2692 switch (buf->c_type) {
2693
2694 case TS_CLRI:
2695 case TS_BITS:
2696 /*
2697 * Have to patch up missing information in bit map headers
2698 */
2699 buf->c_inumber = 0;
2700 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
2701 if (buf->c_count > TP_NINDIR)
2702 readmapflag = 1;
2703 else
2704 for (i = 0; i < buf->c_count; i++)
2705 buf->c_addr[i]++;
2706 break;
2707
2708 case TS_TAPE:
2709 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
2710 oldinofmt = 1;
2711 /* fall through */
2712 case TS_END:
2713 buf->c_inumber = 0;
2714 if (buf->c_flags & DR_INODEINFO) {
2715 memcpy(volinfo, buf->c_inos, TP_NINOS * sizeof(dump_ino_t));
2716 if (Bcvt)
2717 swabst((u_char *)"128i", (u_char *)volinfo);
2718 }
2719 break;
2720
2721 case TS_INODE:
2722 case TS_ADDR:
2723 break;
2724
2725 default:
2726 panic("gethead: unknown inode type %d\n", buf->c_type);
2727 break;
2728 }
2729 /*
2730 * If we are restoring a filesystem with old format inodes,
2731 * copy the uid/gid to the new location.
2732 */
2733 if (oldinofmt) {
2734 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2735 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2736 }
2737 if (dflag)
2738 accthdr(buf);
2739 return(GOOD);
2740 }
2741
2742 static void
2743 converttapebuf(struct tapebuf *tpb)
2744 {
2745 if (Bcvt) {
2746 struct tb {
2747 unsigned int length:28;
2748 unsigned int flags:3;
2749 unsigned int compressed:1;
2750 } tb;
2751 swabst((u_char *)"i", (u_char *)tpb);
2752 memcpy(&tb, tpb, 4);
2753 tpb->length = tb.length;
2754 tpb->flags = tb.flags;
2755 tpb->compressed = tb.compressed;
2756 }
2757 }
2758
2759 /*
2760 * Check that a header is where it belongs and predict the next header
2761 */
2762 static void
2763 accthdr(struct s_spcl *header)
2764 {
2765 static dump_ino_t previno = 0x7fffffff;
2766 static int prevtype;
2767 static long predict;
2768 long blks, i;
2769
2770 if (header->c_type == TS_TAPE) {
2771 fprintf(stderr, "Volume header (%s inode format) ",
2772 oldinofmt ? "old" : "new");
2773 if (header->c_firstrec)
2774 fprintf(stderr, "begins with record %d",
2775 header->c_firstrec);
2776 fprintf(stderr, "\n");
2777 previno = 0x7fffffff;
2778 return;
2779 }
2780 if (previno == 0x7fffffff)
2781 goto newcalc;
2782 switch (prevtype) {
2783 case TS_BITS:
2784 fprintf(stderr, "Dumped inodes map header");
2785 break;
2786 case TS_CLRI:
2787 fprintf(stderr, "Used inodes map header");
2788 break;
2789 case TS_INODE:
2790 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2791 break;
2792 case TS_ADDR:
2793 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2794 break;
2795 case TS_END:
2796 fprintf(stderr, "End of tape header");
2797 break;
2798 }
2799 if (predict != blksread - 1)
2800 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2801 predict, blksread - 1);
2802 fprintf(stderr, "\n");
2803 newcalc:
2804 blks = 0;
2805 if (header->c_type != TS_END)
2806 for (i = 0; i < header->c_count; i++)
2807 if (readmapflag || header->c_addr[i] != 0)
2808 blks++;
2809 predict = blks;
2810 blksread = 0;
2811 prevtype = header->c_type;
2812 previno = header->c_inumber;
2813 }
2814
2815 /*
2816 * Find an inode header.
2817 * Complain if had to skip, and complain is set.
2818 */
2819 static void
2820 findinode(struct s_spcl *header)
2821 {
2822 static long skipcnt = 0;
2823 long i;
2824 char buf[TP_BSIZE];
2825
2826 curfile.name = "<name unknown>";
2827 curfile.action = UNKNOWN;
2828 curfile.dip = NULL;
2829 curfile.ino = 0;
2830 do {
2831 if (header->c_magic != NFS_MAGIC) {
2832 skipcnt++;
2833 while (gethead(header) == FAIL ||
2834 header->c_date != dumpdate)
2835 skipcnt++;
2836 }
2837 switch (header->c_type) {
2838
2839 case TS_ADDR:
2840 /*
2841 * Skip up to the beginning of the next record
2842 */
2843 for (i = 0; i < header->c_count; i++)
2844 if (header->c_addr[i])
2845 readtape(buf);
2846 while (gethead(header) == FAIL ||
2847 header->c_date != dumpdate)
2848 skipcnt++;
2849 break;
2850
2851 case TS_INODE:
2852 curfile.dip = &header->c_dinode;
2853 curfile.ino = header->c_inumber;
2854 break;
2855
2856 case TS_END:
2857 curfile.ino = maxino;
2858 break;
2859
2860 case TS_CLRI:
2861 curfile.name = "<file removal list>";
2862 break;
2863
2864 case TS_BITS:
2865 curfile.name = "<file dump list>";
2866 break;
2867
2868 case TS_TAPE:
2869 panic("unexpected tape header\n");
2870 /* NOTREACHED */
2871
2872 default:
2873 panic("unknown tape header type %d\n", spcl.c_type);
2874 /* NOTREACHED */
2875
2876 }
2877 } while (header->c_type == TS_ADDR);
2878 if (skipcnt > 0)
2879 #ifdef USE_QFA
2880 if (!noresyncmesg)
2881 #endif
2882 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2883 skipcnt);
2884 skipcnt = 0;
2885 }
2886
2887 static int
2888 checksum(int *buf)
2889 {
2890 int i, j;
2891
2892 j = sizeof(union u_spcl) / sizeof(int);
2893 i = 0;
2894 if(!Bcvt) {
2895 do
2896 i += *buf++;
2897 while (--j);
2898 } else {
2899 /* What happens if we want to read restore tapes
2900 for a 16bit int machine??? */
2901 do
2902 i += swabi(*buf++);
2903 while (--j);
2904 }
2905
2906 if (i != CHECKSUM) {
2907 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2908 (unsigned long)curfile.ino, curfile.name);
2909 return(FAIL);
2910 }
2911 return(GOOD);
2912 }
2913
2914 #ifdef RRESTORE
2915 #ifdef __STDC__
2916 #include <stdarg.h>
2917 #else
2918 #include <varargs.h>
2919 #endif
2920
2921 void
2922 #ifdef __STDC__
2923 msg(const char *fmt, ...)
2924 #else
2925 msg(fmt, va_alist)
2926 char *fmt;
2927 va_dcl
2928 #endif
2929 {
2930 va_list ap;
2931 #ifdef __STDC__
2932 va_start(ap, fmt);
2933 #else
2934 va_start(ap);
2935 #endif
2936 (void)vfprintf(stderr, fmt, ap);
2937 va_end(ap);
2938 }
2939 #endif /* RRESTORE */
2940
2941 static u_char *
2942 swab16(u_char *sp, int n)
2943 {
2944 char c;
2945
2946 while (--n >= 0) {
2947 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2948 sp += 2;
2949 }
2950 return (sp);
2951 }
2952
2953 static u_char *
2954 swab32(u_char *sp, int n)
2955 {
2956 char c;
2957
2958 while (--n >= 0) {
2959 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2960 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2961 sp += 4;
2962 }
2963 return (sp);
2964 }
2965
2966 static u_char *
2967 swab64(u_char *sp, int n)
2968 {
2969 char c;
2970
2971 while (--n >= 0) {
2972 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2973 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2974 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2975 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2976 sp += 8;
2977 }
2978 return (sp);
2979 }
2980
2981 void
2982 swabst(u_char *cp, u_char *sp)
2983 {
2984 int n = 0;
2985
2986 while (*cp) {
2987 switch (*cp) {
2988 case '0': case '1': case '2': case '3': case '4':
2989 case '5': case '6': case '7': case '8': case '9':
2990 n = (n * 10) + (*cp++ - '0');
2991 continue;
2992
2993 case 's': case 'w': case 'h':
2994 if (n == 0)
2995 n = 1;
2996 sp = swab16(sp, n);
2997 break;
2998
2999 case 'i':
3000 if (n == 0)
3001 n = 1;
3002 sp = swab32(sp, n);
3003 break;
3004
3005 case 'l':
3006 if (n == 0)
3007 n = 1;
3008 sp = swab64(sp, n);
3009 break;
3010
3011 default: /* Any other character, like 'b' counts as byte. */
3012 if (n == 0)
3013 n = 1;
3014 sp += n;
3015 break;
3016 }
3017 cp++;
3018 n = 0;
3019 }
3020 }
3021
3022 static u_int
3023 swabi(u_int x)
3024 {
3025 swabst((u_char *)"i", (u_char *)&x);
3026 return (x);
3027 }
3028
3029 #if 0
3030 static u_long
3031 swabl(u_long x)
3032 {
3033 swabst((u_char *)"l", (u_char *)&x);
3034 return (x);
3035 }
3036 #endif
3037
3038 void
3039 RequestVol(long tnum)
3040 {
3041 FLUSHTAPEBUF();
3042 getvol(tnum);
3043 }
3044
3045 #ifdef USE_QFA
3046 #ifdef sunos
3047 extern int fdsmtc;
3048
3049 struct uscsi_cmd {
3050 int uscsi_flags; /* read, write, etc. see below */
3051 short uscsi_status; /* resulting status */
3052 short uscsi_timeout; /* Command Timeout */
3053 caddr_t uscsi_cdb; /* cdb to send to target */
3054 caddr_t uscsi_bufaddr; /* i/o source/destination */
3055 u_int uscsi_buflen; /* size of i/o to take place */
3056 u_int uscsi_resid; /* resid from i/o operation */
3057 u_char uscsi_cdblen; /* # of valid cdb bytes */
3058 u_char uscsi_rqlen; /* size of uscsi_rqbuf */
3059 u_char uscsi_rqstatus; /* status of request sense cmd */
3060 u_char uscsi_rqresid; /* resid of request sense cmd */
3061 caddr_t uscsi_rqbuf; /* request sense buffer */
3062 void *uscsi_reserved_5; /* Reserved for Future Use */
3063 };
3064
3065 #define CDB_GROUP0 6 /* 6-byte cdb's */
3066 #define CDB_GROUP1 10 /* 10-byte cdb's */
3067 #define CDB_GROUP2 10 /* 10-byte cdb's */
3068 #define CDB_GROUP3 0 /* reserved */
3069 #define CDB_GROUP4 16 /* 16-byte cdb's */
3070 #define CDB_GROUP5 12 /* 12-byte cdb's */
3071 #define CDB_GROUP6 0 /* reserved */
3072 #define CDB_GROUP7 0 /* reserved */
3073
3074 #define USCSI_WRITE 0x00000 /* send data to device */
3075 #define USCSI_SILENT 0x00001 /* no error messages */
3076 #define USCSI_DIAGNOSE 0x00002 /* fail if any error occurs */
3077 #define USCSI_ISOLATE 0x00004 /* isolate from normal commands */
3078 #define USCSI_READ 0x00008 /* get data from device */
3079 #define USCSI_RESET 0x04000 /* Reset target */
3080 #define USCSI_RESET_ALL 0x08000 /* Reset all targets */
3081 #define USCSI_RQENABLE 0x10000 /* Enable Request Sense extensions */
3082
3083 #define USCSIIOC (0x04 << 8)
3084 #define USCSICMD (USCSIIOC|201) /* user scsi command */
3085
3086 #define USCSI_TIMEOUT 30
3087 #define USCSI_SHORT_TIMEOUT 900
3088 #define USCSI_LONG_TIMEOUT 14000
3089
3090 #define B(s,i) ((unsigned char)((s) >> i))
3091 #define B1(s) ((unsigned char)(s))
3092
3093 #define MSB4(s,v) *(s)=B(v,24),(s)[1]=B(v,16), (s)[2]=B(v,8), (s)[3]=B1(v)
3094
3095
3096 int
3097 GetTapePos(long long *pos)
3098 {
3099 int err = 0;
3100 struct uscsi_cmd scmd;
3101 char buf[512];
3102 char parm[512 * 8];
3103 long lpos;
3104
3105 (void)memset((void *)buf, 0, sizeof(buf));
3106 (void)memset((void *)&scmd, 0, sizeof(scmd));
3107 scmd.uscsi_flags = USCSI_READ|USCSI_SILENT;
3108 scmd.uscsi_timeout = USCSI_TIMEOUT;
3109 scmd.uscsi_cdb = buf;
3110 scmd.uscsi_cdblen = CDB_GROUP1;
3111 buf[0] = 0x34; /* read position */
3112 buf[1] = 0;
3113 (void)memset((void *)parm, 0, 512);
3114 scmd.uscsi_bufaddr = parm;
3115 scmd.uscsi_buflen = 56;
3116 if (ioctl(fdsmtc, USCSICMD, &scmd) == -1) {
3117 err = errno;
3118 return err;
3119 }
3120 (void)memcpy(&lpos, &parm[4], sizeof(long));
3121 *pos = lpos;
3122 return err;
3123 }
3124
3125 int
3126 GotoTapePos(long long pos)
3127 {
3128 int err = 0;
3129 struct uscsi_cmd scmd;
3130 char buf[512];
3131 char parm[512 * 8];
3132 long lpos = (long)pos;
3133
3134 (void)memset((void *)buf, 0, sizeof(buf));
3135 (void)memset((void *)&scmd, 0, sizeof(scmd));
3136 scmd.uscsi_flags = USCSI_WRITE|USCSI_SILENT;
3137 scmd.uscsi_timeout = 360; /* 5 Minutes */
3138 scmd.uscsi_cdb = buf;
3139 scmd.uscsi_cdblen = CDB_GROUP1;
3140 buf[0] = 0x2b; /* locate */
3141 buf[1] = 0;
3142 MSB4(&buf[3], lpos);
3143 (void)memset((void *)parm, 0, 512);
3144 scmd.uscsi_bufaddr = NULL;
3145 scmd.uscsi_buflen = 0;
3146 if (ioctl(fdsmtc, USCSICMD, &scmd) == -1) {
3147 err = errno;
3148 return err;
3149 }
3150 return err;
3151 }
3152 #endif
3153
3154 #define LSEEK_GET_TAPEPOS 10
3155 #define LSEEK_GO2_TAPEPOS 11
3156
3157 #ifdef __linux__
3158 typedef struct mt_pos {
3159 short mt_op;
3160 int mt_count;
3161 } MTPosRec, *MTPosPtr;
3162
3163
3164 /*
3165 * get the current position of the tape
3166 */
3167 int
3168 GetTapePos(long long *pos)
3169 {
3170 int err = 0;
3171
3172 #ifdef RDUMP
3173 if (host) {
3174 *pos = (long long) rmtseek((OFF_T)0, (int)LSEEK_GET_TAPEPOS);
3175 err = *pos < 0;
3176 }
3177 else
3178 #endif
3179 {
3180 if (magtapein) {
3181 long mtpos;
3182 *pos = 0;
3183 err = (ioctl(mt, MTIOCPOS, &mtpos) < 0);
3184 *pos = (long long)mtpos;
3185 }
3186 else {
3187 *pos = LSEEK(mt, 0, SEEK_CUR);
3188 err = (*pos < 0);
3189 }
3190 }
3191 if (err) {
3192 err = errno;
3193 fprintf(stdout, "[%ld] error: %d (getting tapepos: %lld)\n",
3194 (unsigned long)getpid(), err, *pos);
3195 return err;
3196 }
3197 return err;
3198 }
3199
3200 /*
3201 * go to specified position on tape
3202 */
3203 int
3204 GotoTapePos(long long pos)
3205 {
3206 int err = 0;
3207
3208 #ifdef RDUMP
3209 if (host)
3210 err = (rmtseek((OFF_T)pos, (int)LSEEK_GO2_TAPEPOS) < 0);
3211 else
3212 #endif
3213 {
3214 if (magtapein) {
3215 struct mt_pos buf;
3216 buf.mt_op = MTSEEK;
3217 buf.mt_count = (int) pos;
3218 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
3219 }
3220 else {
3221 pos = LSEEK(mt, pos, SEEK_SET);
3222 err = (pos < 0);
3223 }
3224 }
3225 if (err) {
3226 err = errno;
3227 fprintf(stdout, "[%ld] error: %d (setting tapepos: %lld)\n",
3228 (unsigned long)getpid(), err, pos);
3229 return err;
3230 }
3231 return err;
3232 }
3233 #endif /* __linux__ */
3234
3235 /*
3236 * read next data from tape to re-sync
3237 */
3238 void
3239 ReReadFromTape(void)
3240 {
3241 FLUSHTAPEBUF();
3242 noresyncmesg = 1;
3243 if (gethead(&spcl) == FAIL) {
3244 #ifdef DEBUG_QFA
3245 fprintf(stdout, "DEBUG 1 gethead failed\n");
3246 #endif
3247 }
3248 findinode(&spcl);
3249 noresyncmesg = 0;
3250 }
3251
3252 void
3253 ReReadInodeFromTape(dump_ino_t theino)
3254 {
3255 long cntloop = 0;
3256
3257 FLUSHTAPEBUF();
3258 noresyncmesg = 1;
3259 do {
3260 cntloop++;
3261 gethead(&spcl);
3262 } while (!(spcl.c_inumber == theino && spcl.c_type == TS_INODE && spcl.c_date == dumpdate));
3263 #ifdef DEBUG_QFA
3264 fprintf(stderr, "DEBUG: %ld reads\n", cntloop);
3265 fprintf(stderr, "DEBUG: bufsize %ld\n", bufsize);
3266 fprintf(stderr, "DEBUG: ntrec %ld\n", ntrec);
3267 fprintf(stderr, "DEBUG: %ld reads\n", cntloop);
3268 #endif
3269 findinode(&spcl);
3270 noresyncmesg = 0;
3271 }
3272
3273 #ifdef sunos
3274 int
3275 OpenSMTCmt(char *themagtape)
3276 {
3277 if (GetSCSIIDFromPath(themagtape, &scsiid)) {
3278 fprintf(stderr, "can't get SCSI-ID for %s\n", themagtape);
3279 return -1;
3280 }
3281 if (scsiid < 0) {
3282 fprintf(stderr, "can't get SCSI-ID for %s\n", themagtape);
3283 return -1;
3284 }
3285 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
3286 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
3287 fprintf(stderr, "can't open smtc device: %s, %d\n", smtcpath, errno);
3288 return -1;
3289 }
3290 return 0;
3291 }
3292 #endif /* sunos */
3293 #endif /* USE_QFA */