Debian Codegraph @

restore/tape.c

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