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