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