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