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