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