1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 #if 0 37 static char sccsid[] = "@(#)tape.c 8.9 (Berkeley) 5/1/95"; 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/file.h> 46 #include <sys/mtio.h> 47 #include <sys/stat.h> 48 #include <sys/time.h> 49 #include <sys/extattr.h> 50 #include <sys/acl.h> 51 52 #include <ufs/ufs/extattr.h> 53 #include <ufs/ufs/dinode.h> 54 #include <protocols/dumprestore.h> 55 56 #include <errno.h> 57 #include <limits.h> 58 #include <paths.h> 59 #include <setjmp.h> 60 #include <stdint.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <time.h> 65 #include <timeconv.h> 66 #include <unistd.h> 67 68 #include "restore.h" 69 #include "extern.h" 70 71 static long fssize = MAXBSIZE; 72 static int mt = -1; 73 static int pipein = 0; 74 static int pipecmdin = 0; 75 static FILE *popenfp = NULL; 76 static char *magtape; 77 static int blkcnt; 78 static int numtrec; 79 static char *tapebuf; 80 static union u_spcl endoftapemark; 81 static long byteslide = 0; 82 static long blksread; /* blocks read since last header */ 83 static int64_t tapeaddr = 0; /* current TP_BSIZE tape record */ 84 static long tapesread; 85 static jmp_buf restart; 86 static int gettingfile = 0; /* restart has a valid frame */ 87 static char *host = NULL; 88 static int readmapflag; 89 90 static int ofile; 91 static char *map; 92 static char lnkbuf[MAXPATHLEN + 1]; 93 static int pathlen; 94 95 int Bcvt; /* Swap Bytes */ 96 int oldinofmt; /* FreeBSD 1 inode format needs cvt */ 97 98 #define FLUSHTAPEBUF() blkcnt = ntrec + 1 99 100 char *namespace_names[] = EXTATTR_NAMESPACE_NAMES; 101 102 static void accthdr(struct s_spcl *); 103 static int checksum(int *); 104 static void findinode(struct s_spcl *); 105 static void findtapeblksize(void); 106 static char *setupextattr(int); 107 static void xtrattr(char *, size_t); 108 static void skiphole(void (*)(char *, size_t), size_t *); 109 static int gethead(struct s_spcl *); 110 static void readtape(char *); 111 static void setdumpnum(void); 112 static u_long swabl(u_long); 113 static u_char *swablong(u_char *, int); 114 static u_char *swabshort(u_char *, int); 115 static void terminateinput(void); 116 static void xtrfile(char *, size_t); 117 static void xtrlnkfile(char *, size_t); 118 static void xtrlnkskip(char *, size_t); 119 static void xtrmap(char *, size_t); 120 static void xtrmapskip(char *, size_t); 121 static void xtrskip(char *, size_t); 122 123 /* 124 * Set up an input source 125 */ 126 void 127 setinput(char *source, int ispipecommand) 128 { 129 FLUSHTAPEBUF(); 130 if (bflag) 131 newtapebuf(ntrec); 132 else 133 newtapebuf(MAX(NTREC, HIGHDENSITYTREC)); 134 terminal = stdin; 135 136 if (ispipecommand) 137 pipecmdin++; 138 else 139 #ifdef RRESTORE 140 if (strchr(source, ':')) { 141 host = source; 142 source = strchr(host, ':'); 143 *source++ = '\0'; 144 if (rmthost(host) == 0) 145 done(1); 146 } else 147 #endif 148 if (strcmp(source, "-") == 0) { 149 /* 150 * Since input is coming from a pipe we must establish 151 * our own connection to the terminal. 152 */ 153 terminal = fopen(_PATH_TTY, "r"); 154 if (terminal == NULL) { 155 (void)fprintf(stderr, "cannot open %s: %s\n", 156 _PATH_TTY, strerror(errno)); 157 terminal = fopen(_PATH_DEVNULL, "r"); 158 if (terminal == NULL) { 159 (void)fprintf(stderr, "cannot open %s: %s\n", 160 _PATH_DEVNULL, strerror(errno)); 161 done(1); 162 } 163 } 164 pipein++; 165 } 166 /* no longer need or want root privileges */ 167 if (setuid(getuid()) != 0) { 168 fprintf(stderr, "setuid failed\n"); 169 done(1); 170 } 171 magtape = strdup(source); 172 if (magtape == NULL) { 173 fprintf(stderr, "Cannot allocate space for magtape buffer\n"); 174 done(1); 175 } 176 } 177 178 void 179 newtapebuf(long size) 180 { 181 static int tapebufsize = -1; 182 183 ntrec = size; 184 if (size <= tapebufsize) 185 return; 186 if (tapebuf != NULL) 187 free(tapebuf - TP_BSIZE); 188 tapebuf = malloc((size+1) * TP_BSIZE); 189 if (tapebuf == NULL) { 190 fprintf(stderr, "Cannot allocate space for tape buffer\n"); 191 done(1); 192 } 193 tapebuf += TP_BSIZE; 194 tapebufsize = size; 195 } 196 197 /* 198 * Verify that the tape drive can be accessed and 199 * that it actually is a dump tape. 200 */ 201 void 202 setup(void) 203 { 204 int i, j, *ip; 205 struct stat stbuf; 206 207 vprintf(stdout, "Verify tape and initialize maps\n"); 208 if (pipecmdin) { 209 if (setenv("RESTORE_VOLUME", "1", 1) == -1) { 210 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n", 211 strerror(errno)); 212 done(1); 213 } 214 popenfp = popen(magtape, "r"); 215 mt = popenfp ? fileno(popenfp) : -1; 216 } else 217 #ifdef RRESTORE 218 if (host) 219 mt = rmtopen(magtape, 0); 220 else 221 #endif 222 if (pipein) 223 mt = 0; 224 else 225 mt = open(magtape, O_RDONLY, 0); 226 if (mt < 0) { 227 fprintf(stderr, "%s: %s\n", magtape, strerror(errno)); 228 done(1); 229 } 230 volno = 1; 231 setdumpnum(); 232 FLUSHTAPEBUF(); 233 if (!pipein && !pipecmdin && !bflag) 234 findtapeblksize(); 235 if (gethead(&spcl) == FAIL) { 236 fprintf(stderr, "Tape is not a dump tape\n"); 237 done(1); 238 } 239 if (pipein) { 240 endoftapemark.s_spcl.c_magic = FS_UFS2_MAGIC; 241 endoftapemark.s_spcl.c_type = TS_END; 242 ip = (int *)&endoftapemark; 243 j = sizeof(union u_spcl) / sizeof(int); 244 i = 0; 245 do 246 i += *ip++; 247 while (--j); 248 endoftapemark.s_spcl.c_checksum = CHECKSUM - i; 249 } 250 if (vflag || command == 't') 251 printdumpinfo(); 252 dumptime = _time64_to_time(spcl.c_ddate); 253 dumpdate = _time64_to_time(spcl.c_date); 254 if (stat(".", &stbuf) < 0) { 255 fprintf(stderr, "cannot stat .: %s\n", strerror(errno)); 256 done(1); 257 } 258 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE ) 259 fssize = TP_BSIZE; 260 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE) 261 fssize = stbuf.st_blksize; 262 if (((TP_BSIZE - 1) & stbuf.st_blksize) != 0) { 263 fprintf(stderr, "Warning: filesystem with non-multiple-of-%d " 264 "blocksize (%d);\n", TP_BSIZE, stbuf.st_blksize); 265 fssize = roundup(fssize, TP_BSIZE); 266 fprintf(stderr, "\twriting using blocksize %ld\n", fssize); 267 } 268 if (spcl.c_volume != 1) { 269 fprintf(stderr, "Tape is not volume 1 of the dump\n"); 270 done(1); 271 } 272 if (gethead(&spcl) == FAIL) { 273 dprintf(stdout, "header read failed at %ld blocks\n", blksread); 274 panic("no header after volume mark!\n"); 275 } 276 findinode(&spcl); 277 if (spcl.c_type != TS_CLRI) { 278 fprintf(stderr, "Cannot find file removal list\n"); 279 done(1); 280 } 281 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1; 282 dprintf(stdout, "maxino = %ju\n", (uintmax_t)maxino); 283 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY)); 284 if (map == NULL) 285 panic("no memory for active inode map\n"); 286 usedinomap = map; 287 curfile.action = USING; 288 getfile(xtrmap, xtrmapskip, xtrmapskip); 289 if (spcl.c_type != TS_BITS) { 290 fprintf(stderr, "Cannot find file dump list\n"); 291 done(1); 292 } 293 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY)); 294 if (map == (char *)NULL) 295 panic("no memory for file dump list\n"); 296 dumpmap = map; 297 curfile.action = USING; 298 getfile(xtrmap, xtrmapskip, xtrmapskip); 299 /* 300 * If there may be whiteout entries on the tape, pretend that the 301 * whiteout inode exists, so that the whiteout entries can be 302 * extracted. 303 */ 304 SETINO(UFS_WINO, dumpmap); 305 /* 'r' restores don't call getvol() for tape 1, so mark it as read. */ 306 if (command == 'r') 307 tapesread = 1; 308 } 309 310 /* 311 * Prompt user to load a new dump volume. 312 * "Nextvol" is the next suggested volume to use. 313 * This suggested volume is enforced when doing full 314 * or incremental restores, but can be overridden by 315 * the user when only extracting a subset of the files. 316 */ 317 void 318 getvol(long nextvol) 319 { 320 int64_t prevtapea; 321 long i, newvol, savecnt; 322 union u_spcl tmpspcl; 323 # define tmpbuf tmpspcl.s_spcl 324 char buf[TP_BSIZE]; 325 326 if (nextvol == 1) { 327 tapesread = 0; 328 gettingfile = 0; 329 } 330 prevtapea = tapeaddr; 331 savecnt = blksread; 332 if (pipein) { 333 if (nextvol != 1) { 334 panic("Changing volumes on pipe input?\n"); 335 /* Avoid looping if we couldn't ask the user. */ 336 if (yflag || ferror(terminal) || feof(terminal)) 337 done(1); 338 } 339 if (volno == 1) 340 return; 341 newvol = 0; 342 goto gethdr; 343 } 344 again: 345 if (pipein) 346 done(1); /* pipes do not get a second chance */ 347 if (command == 'R' || command == 'r' || curfile.action != SKIP) 348 newvol = nextvol; 349 else 350 newvol = 0; 351 while (newvol <= 0) { 352 if (tapesread == 0) { 353 fprintf(stderr, "%s%s%s%s%s%s%s", 354 "You have not read any tapes yet.\n", 355 "If you are extracting just a few files,", 356 " start with the last volume\n", 357 "and work towards the first; restore", 358 " can quickly skip tapes that\n", 359 "have no further files to extract.", 360 " Otherwise, begin with volume 1.\n"); 361 } else { 362 fprintf(stderr, "You have read volumes"); 363 strcpy(buf, ": "); 364 for (i = 0; i < 32; i++) 365 if (tapesread & (1 << i)) { 366 fprintf(stderr, "%s%ld", buf, i + 1); 367 strcpy(buf, ", "); 368 } 369 fprintf(stderr, "\n"); 370 } 371 do { 372 fprintf(stderr, "Specify next volume #: "); 373 (void) fflush(stderr); 374 if (fgets(buf, BUFSIZ, terminal) == NULL) 375 done(1); 376 } while (buf[0] == '\n'); 377 newvol = atoi(buf); 378 if (newvol <= 0) { 379 fprintf(stderr, 380 "Volume numbers are positive numerics\n"); 381 } 382 } 383 if (newvol == volno) { 384 tapesread |= 1 << (volno - 1); 385 return; 386 } 387 closemt(); 388 fprintf(stderr, "Mount tape volume %ld\n", newvol); 389 fprintf(stderr, "Enter ``none'' if there are no more tapes\n"); 390 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape); 391 (void) fflush(stderr); 392 if (fgets(buf, BUFSIZ, terminal) == NULL) 393 done(1); 394 if (!strcmp(buf, "none\n")) { 395 terminateinput(); 396 return; 397 } 398 if (buf[0] != '\n') { 399 (void) strcpy(magtape, buf); 400 magtape[strlen(magtape) - 1] = '\0'; 401 } 402 if (pipecmdin) { 403 char volno[sizeof("2147483647")]; 404 405 (void)sprintf(volno, "%ld", newvol); 406 if (setenv("RESTORE_VOLUME", volno, 1) == -1) { 407 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n", 408 strerror(errno)); 409 done(1); 410 } 411 popenfp = popen(magtape, "r"); 412 mt = popenfp ? fileno(popenfp) : -1; 413 } else 414 #ifdef RRESTORE 415 if (host) 416 mt = rmtopen(magtape, 0); 417 else 418 #endif 419 mt = open(magtape, O_RDONLY, 0); 420 421 if (mt == -1) { 422 fprintf(stderr, "Cannot open %s\n", magtape); 423 volno = -1; 424 goto again; 425 } 426 gethdr: 427 volno = newvol; 428 setdumpnum(); 429 FLUSHTAPEBUF(); 430 if (gethead(&tmpbuf) == FAIL) { 431 dprintf(stdout, "header read failed at %ld blocks\n", blksread); 432 fprintf(stderr, "tape is not dump tape\n"); 433 volno = 0; 434 goto again; 435 } 436 if (tmpbuf.c_volume != volno) { 437 fprintf(stderr, "Wrong volume (%jd)\n", 438 (intmax_t)tmpbuf.c_volume); 439 volno = 0; 440 goto again; 441 } 442 if (_time64_to_time(tmpbuf.c_date) != dumpdate || 443 _time64_to_time(tmpbuf.c_ddate) != dumptime) { 444 time_t t = _time64_to_time(tmpbuf.c_date); 445 fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t)); 446 fprintf(stderr, "\twanted: %s", ctime(&dumpdate)); 447 volno = 0; 448 goto again; 449 } 450 tapesread |= 1 << (volno - 1); 451 blksread = savecnt; 452 /* 453 * If continuing from the previous volume, skip over any 454 * blocks read already at the end of the previous volume. 455 * 456 * If coming to this volume at random, skip to the beginning 457 * of the next record. 458 */ 459 dprintf(stdout, "last rec %jd, tape starts with %jd\n", 460 (intmax_t)prevtapea, (intmax_t)tmpbuf.c_tapea); 461 if (tmpbuf.c_type == TS_TAPE) { 462 if (curfile.action != USING) { 463 /* 464 * XXX Dump incorrectly sets c_count to 1 in the 465 * volume header of the first tape, so ignore 466 * c_count when volno == 1. 467 */ 468 if (volno != 1) 469 for (i = tmpbuf.c_count; i > 0; i--) 470 readtape(buf); 471 } else if (tmpbuf.c_tapea <= prevtapea) { 472 /* 473 * Normally the value of c_tapea in the volume 474 * header is the record number of the header itself. 475 * However in the volume header following an EOT- 476 * terminated tape, it is the record number of the 477 * first continuation data block (dump bug?). 478 * 479 * The next record we want is `prevtapea + 1'. 480 */ 481 i = prevtapea + 1 - tmpbuf.c_tapea; 482 dprintf(stderr, "Skipping %ld duplicate record%s.\n", 483 i, i > 1 ? "s" : ""); 484 while (--i >= 0) 485 readtape(buf); 486 } 487 } 488 if (curfile.action == USING) { 489 if (volno == 1) 490 panic("active file into volume 1\n"); 491 return; 492 } 493 (void) gethead(&spcl); 494 findinode(&spcl); 495 if (gettingfile) { 496 gettingfile = 0; 497 longjmp(restart, 1); 498 } 499 } 500 501 /* 502 * Handle unexpected EOF. 503 */ 504 static void 505 terminateinput(void) 506 { 507 508 if (gettingfile && curfile.action == USING) { 509 printf("Warning: %s %s\n", 510 "End-of-input encountered while extracting", curfile.name); 511 } 512 curfile.name = "<name unknown>"; 513 curfile.action = UNKNOWN; 514 curfile.mode = 0; 515 curfile.ino = maxino; 516 if (gettingfile) { 517 gettingfile = 0; 518 longjmp(restart, 1); 519 } 520 } 521 522 /* 523 * handle multiple dumps per tape by skipping forward to the 524 * appropriate one. 525 */ 526 static void 527 setdumpnum(void) 528 { 529 struct mtop tcom; 530 531 if (dumpnum == 1 || volno != 1) 532 return; 533 if (pipein) { 534 fprintf(stderr, "Cannot have multiple dumps on pipe input\n"); 535 done(1); 536 } 537 tcom.mt_op = MTFSF; 538 tcom.mt_count = dumpnum - 1; 539 #ifdef RRESTORE 540 if (host) 541 rmtioctl(MTFSF, dumpnum - 1); 542 else 543 #endif 544 if (!pipecmdin && ioctl(mt, MTIOCTOP, (char *)&tcom) < 0) 545 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno)); 546 } 547 548 void 549 printdumpinfo(void) 550 { 551 time_t t; 552 t = _time64_to_time(spcl.c_date); 553 fprintf(stdout, "Dump date: %s", ctime(&t)); 554 t = _time64_to_time(spcl.c_ddate); 555 fprintf(stdout, "Dumped from: %s", 556 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t)); 557 if (spcl.c_host[0] == '\0') 558 return; 559 fprintf(stderr, "Level %jd dump of %s on %s:%s\n", 560 (intmax_t)spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev); 561 fprintf(stderr, "Label: %s\n", spcl.c_label); 562 } 563 564 int 565 extractfile(char *name) 566 { 567 u_int flags; 568 uid_t uid; 569 gid_t gid; 570 mode_t mode; 571 int extsize; 572 struct timespec mtimep[2], ctimep[2]; 573 struct entry *ep; 574 char *buf; 575 576 curfile.name = name; 577 curfile.action = USING; 578 mtimep[0].tv_sec = curfile.atime_sec; 579 mtimep[0].tv_nsec = curfile.atime_nsec; 580 mtimep[1].tv_sec = curfile.mtime_sec; 581 mtimep[1].tv_nsec = curfile.mtime_nsec; 582 ctimep[0].tv_sec = curfile.atime_sec; 583 ctimep[0].tv_nsec = curfile.atime_nsec; 584 ctimep[1].tv_sec = curfile.birthtime_sec; 585 ctimep[1].tv_nsec = curfile.birthtime_nsec; 586 extsize = curfile.extsize; 587 uid = getuid(); 588 if (uid == 0) 589 uid = curfile.uid; 590 gid = curfile.gid; 591 mode = curfile.mode; 592 flags = curfile.file_flags; 593 switch (mode & IFMT) { 594 595 default: 596 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode); 597 skipfile(); 598 return (FAIL); 599 600 case IFSOCK: 601 vprintf(stdout, "skipped socket %s\n", name); 602 skipfile(); 603 return (GOOD); 604 605 case IFDIR: 606 if (mflag) { 607 ep = lookupname(name); 608 if (ep == NULL || ep->e_flags & EXTRACT) 609 panic("unextracted directory %s\n", name); 610 skipfile(); 611 return (GOOD); 612 } 613 vprintf(stdout, "extract file %s\n", name); 614 return (genliteraldir(name, curfile.ino)); 615 616 case IFLNK: 617 lnkbuf[0] = '\0'; 618 pathlen = 0; 619 buf = setupextattr(extsize); 620 getfile(xtrlnkfile, xtrattr, xtrlnkskip); 621 if (pathlen == 0) { 622 vprintf(stdout, 623 "%s: zero length symbolic link (ignored)\n", name); 624 return (GOOD); 625 } 626 if (linkit(lnkbuf, name, SYMLINK) == GOOD) { 627 if (extsize > 0) 628 set_extattr(-1, name, buf, extsize, SXA_LINK); 629 (void) lchown(name, uid, gid); 630 (void) lchmod(name, mode); 631 (void) utimensat(AT_FDCWD, name, ctimep, 632 AT_SYMLINK_NOFOLLOW); 633 (void) utimensat(AT_FDCWD, name, mtimep, 634 AT_SYMLINK_NOFOLLOW); 635 (void) lchflags(name, flags); 636 return (GOOD); 637 } 638 return (FAIL); 639 640 case IFIFO: 641 vprintf(stdout, "extract fifo %s\n", name); 642 if (Nflag) { 643 skipfile(); 644 return (GOOD); 645 } 646 if (uflag) 647 (void) unlink(name); 648 if (mkfifo(name, 0600) < 0) { 649 fprintf(stderr, "%s: cannot create fifo: %s\n", 650 name, strerror(errno)); 651 skipfile(); 652 return (FAIL); 653 } 654 if (extsize == 0) { 655 skipfile(); 656 } else { 657 buf = setupextattr(extsize); 658 getfile(xtrnull, xtrattr, xtrnull); 659 set_extattr(-1, name, buf, extsize, SXA_FILE); 660 } 661 (void) chown(name, uid, gid); 662 (void) chmod(name, mode); 663 (void) utimensat(AT_FDCWD, name, ctimep, 0); 664 (void) utimensat(AT_FDCWD, name, mtimep, 0); 665 (void) chflags(name, flags); 666 return (GOOD); 667 668 case IFCHR: 669 case IFBLK: 670 vprintf(stdout, "extract special file %s\n", name); 671 if (Nflag) { 672 skipfile(); 673 return (GOOD); 674 } 675 if (uflag) 676 (void) unlink(name); 677 if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600, 678 (int)curfile.rdev) < 0) { 679 fprintf(stderr, "%s: cannot create special file: %s\n", 680 name, strerror(errno)); 681 skipfile(); 682 return (FAIL); 683 } 684 if (extsize == 0) { 685 skipfile(); 686 } else { 687 buf = setupextattr(extsize); 688 getfile(xtrnull, xtrattr, xtrnull); 689 set_extattr(-1, name, buf, extsize, SXA_FILE); 690 } 691 (void) chown(name, uid, gid); 692 (void) chmod(name, mode); 693 (void) utimensat(AT_FDCWD, name, ctimep, 0); 694 (void) utimensat(AT_FDCWD, name, mtimep, 0); 695 (void) chflags(name, flags); 696 return (GOOD); 697 698 case IFREG: 699 vprintf(stdout, "extract file %s\n", name); 700 if (Nflag) { 701 skipfile(); 702 return (GOOD); 703 } 704 if (uflag) 705 (void) unlink(name); 706 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 707 0600)) < 0) { 708 fprintf(stderr, "%s: cannot create file: %s\n", 709 name, strerror(errno)); 710 skipfile(); 711 return (FAIL); 712 } 713 buf = setupextattr(extsize); 714 getfile(xtrfile, xtrattr, xtrskip); 715 if (extsize > 0) 716 set_extattr(ofile, name, buf, extsize, SXA_FD); 717 (void) fchown(ofile, uid, gid); 718 (void) fchmod(ofile, mode); 719 (void) futimens(ofile, ctimep); 720 (void) futimens(ofile, mtimep); 721 (void) fchflags(ofile, flags); 722 (void) close(ofile); 723 return (GOOD); 724 } 725 /* NOTREACHED */ 726 } 727 728 /* 729 * Set attributes on a file descriptor, link, or file. 730 */ 731 void 732 set_extattr(int fd, char *name, void *buf, int size, enum set_extattr_mode mode) 733 { 734 struct extattr *eap, *eaend; 735 const char *method; 736 ssize_t res; 737 int error; 738 char eaname[EXTATTR_MAXNAMELEN + 1]; 739 740 vprintf(stdout, "Set attributes for %s:", name); 741 eaend = buf + size; 742 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) { 743 /* 744 * Make sure this entry is complete. 745 */ 746 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) { 747 dprintf(stdout, "\n\t%scorrupted", 748 eap == buf ? "" : "remainder "); 749 break; 750 } 751 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY) 752 continue; 753 snprintf(eaname, sizeof(eaname), "%.*s", 754 (int)eap->ea_namelength, eap->ea_name); 755 vprintf(stdout, "\n\t%s, (%d bytes), %s", 756 namespace_names[eap->ea_namespace], eap->ea_length, 757 eaname); 758 /* 759 * First we try the general attribute setting interface. 760 * However, some attributes can only be set by root or 761 * by using special interfaces (for example, ACLs). 762 */ 763 if (mode == SXA_FD) { 764 res = extattr_set_fd(fd, eap->ea_namespace, 765 eaname, EXTATTR_CONTENT(eap), 766 EXTATTR_CONTENT_SIZE(eap)); 767 method = "extattr_set_fd"; 768 } else if (mode == SXA_LINK) { 769 res = extattr_set_link(name, eap->ea_namespace, 770 eaname, EXTATTR_CONTENT(eap), 771 EXTATTR_CONTENT_SIZE(eap)); 772 method = "extattr_set_link"; 773 } else if (mode == SXA_FILE) { 774 res = extattr_set_file(name, eap->ea_namespace, 775 eaname, EXTATTR_CONTENT(eap), 776 EXTATTR_CONTENT_SIZE(eap)); 777 method = "extattr_set_file"; 778 } 779 if (res != -1) { 780 dprintf(stdout, " (set using %s)", method); 781 continue; 782 } 783 /* 784 * If the general interface refuses to set the attribute, 785 * then we try all the specialized interfaces that we 786 * know about. 787 */ 788 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM && 789 strcmp(eaname, POSIX1E_ACL_ACCESS_EXTATTR_NAME) == 0) { 790 if (mode == SXA_FD) { 791 error = acl_set_fd(fd, EXTATTR_CONTENT(eap)); 792 method = "acl_set_fd"; 793 } else if (mode == SXA_LINK) { 794 error = acl_set_link_np(name, ACL_TYPE_ACCESS, 795 EXTATTR_CONTENT(eap)); 796 method = "acl_set_link_np"; 797 } else if (mode == SXA_FILE) { 798 error = acl_set_file(name, ACL_TYPE_ACCESS, 799 EXTATTR_CONTENT(eap)); 800 method = "acl_set_file"; 801 } 802 if (error != -1) { 803 dprintf(stdout, " (set using %s)", method); 804 continue; 805 } 806 } 807 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM && 808 strcmp(eaname, POSIX1E_ACL_DEFAULT_EXTATTR_NAME) == 0) { 809 if (mode == SXA_LINK) { 810 error = acl_set_link_np(name, ACL_TYPE_DEFAULT, 811 EXTATTR_CONTENT(eap)); 812 method = "acl_set_link_np"; 813 } else { 814 error = acl_set_file(name, ACL_TYPE_DEFAULT, 815 EXTATTR_CONTENT(eap)); 816 method = "acl_set_file"; 817 } 818 if (error != -1) { 819 dprintf(stdout, " (set using %s)", method); 820 continue; 821 } 822 } 823 vprintf(stdout, " (unable to set)"); 824 } 825 vprintf(stdout, "\n"); 826 } 827 828 /* 829 * skip over bit maps on the tape 830 */ 831 void 832 skipmaps(void) 833 { 834 835 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI) 836 skipfile(); 837 } 838 839 /* 840 * skip over a file on the tape 841 */ 842 void 843 skipfile(void) 844 { 845 846 curfile.action = SKIP; 847 getfile(xtrnull, xtrnull, xtrnull); 848 } 849 850 /* 851 * Skip a hole in an output file 852 */ 853 static void 854 skiphole(void (*skip)(char *, size_t), size_t *seekpos) 855 { 856 char buf[MAXBSIZE]; 857 858 if (*seekpos > 0) { 859 (*skip)(buf, *seekpos); 860 *seekpos = 0; 861 } 862 } 863 864 /* 865 * Extract a file from the tape. 866 * When an allocated block is found it is passed to the fill function; 867 * when an unallocated block (hole) is found, a zeroed buffer is passed 868 * to the skip function. 869 */ 870 void 871 getfile(void (*datafill)(char *, size_t), void (*attrfill)(char *, size_t), 872 void (*skip)(char *, size_t)) 873 { 874 int i; 875 volatile off_t size; 876 size_t seekpos; 877 int curblk, attrsize; 878 void (*fillit)(char *, size_t); 879 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE]; 880 char junk[TP_BSIZE]; 881 882 curblk = 0; 883 size = spcl.c_size; 884 seekpos = 0; 885 attrsize = spcl.c_extsize; 886 if (spcl.c_type == TS_END) 887 panic("ran off end of tape\n"); 888 if (spcl.c_magic != FS_UFS2_MAGIC) 889 panic("not at beginning of a file\n"); 890 if (!gettingfile && setjmp(restart) != 0) 891 return; 892 gettingfile++; 893 fillit = datafill; 894 if (size == 0 && attrsize > 0) { 895 fillit = attrfill; 896 size = attrsize; 897 attrsize = 0; 898 } 899 loop: 900 for (i = 0; i < spcl.c_count; i++) { 901 if (!readmapflag && i > TP_NINDIR) { 902 if (Dflag) { 903 fprintf(stderr, "spcl.c_count = %jd\n", 904 (intmax_t)spcl.c_count); 905 break; 906 } else 907 panic("spcl.c_count = %jd\n", 908 (intmax_t)spcl.c_count); 909 } 910 if (readmapflag || spcl.c_addr[i]) { 911 readtape(&buf[curblk++][0]); 912 if (curblk == fssize / TP_BSIZE) { 913 skiphole(skip, &seekpos); 914 (*fillit)((char *)buf, (long)(size > TP_BSIZE ? 915 fssize : (curblk - 1) * TP_BSIZE + size)); 916 curblk = 0; 917 } 918 } else { 919 if (curblk > 0) { 920 skiphole(skip, &seekpos); 921 (*fillit)((char *)buf, (long)(size > TP_BSIZE ? 922 curblk * TP_BSIZE : 923 (curblk - 1) * TP_BSIZE + size)); 924 curblk = 0; 925 } 926 /* 927 * We have a block of a hole. Don't skip it 928 * now, because there may be next adjacent 929 * block of the hole in the file. Postpone the 930 * seek until next file write. 931 */ 932 seekpos += (long)MIN(TP_BSIZE, size); 933 } 934 if ((size -= TP_BSIZE) <= 0) { 935 if (size > -TP_BSIZE && curblk > 0) { 936 skiphole(skip, &seekpos); 937 (*fillit)((char *)buf, 938 (long)((curblk * TP_BSIZE) + size)); 939 curblk = 0; 940 } 941 if (attrsize > 0) { 942 fillit = attrfill; 943 size = attrsize; 944 attrsize = 0; 945 continue; 946 } 947 if (spcl.c_count - i > 1) 948 dprintf(stdout, "skipping %d junk block(s)\n", 949 spcl.c_count - i - 1); 950 for (i++; i < spcl.c_count; i++) { 951 if (!readmapflag && i > TP_NINDIR) { 952 if (Dflag) { 953 fprintf(stderr, 954 "spcl.c_count = %jd\n", 955 (intmax_t)spcl.c_count); 956 break; 957 } else 958 panic("spcl.c_count = %jd\n", 959 (intmax_t)spcl.c_count); 960 } 961 if (readmapflag || spcl.c_addr[i]) 962 readtape(junk); 963 } 964 break; 965 } 966 } 967 if (gethead(&spcl) == GOOD && size > 0) { 968 if (spcl.c_type == TS_ADDR) 969 goto loop; 970 dprintf(stdout, 971 "Missing address (header) block for %s at %ld blocks\n", 972 curfile.name, blksread); 973 } 974 if (curblk > 0) 975 panic("getfile: lost data\n"); 976 findinode(&spcl); 977 gettingfile = 0; 978 } 979 980 /* 981 * These variables are shared between the next two functions. 982 */ 983 static int extbufsize = 0; 984 static char *extbuf; 985 static int extloc; 986 987 /* 988 * Allocate a buffer into which to extract extended attributes. 989 */ 990 static char * 991 setupextattr(int extsize) 992 { 993 994 extloc = 0; 995 if (extsize <= extbufsize) 996 return (extbuf); 997 if (extbufsize > 0) 998 free(extbuf); 999 if ((extbuf = malloc(extsize)) != NULL) { 1000 extbufsize = extsize; 1001 return (extbuf); 1002 } 1003 extbufsize = 0; 1004 extbuf = NULL; 1005 fprintf(stderr, "Cannot extract %d bytes %s for inode %ju, name %s\n", 1006 extsize, "of extended attributes", (uintmax_t)curfile.ino, 1007 curfile.name); 1008 return (NULL); 1009 } 1010 1011 /* 1012 * Extract the next block of extended attributes. 1013 */ 1014 static void 1015 xtrattr(char *buf, size_t size) 1016 { 1017 1018 if (extloc + size > extbufsize) 1019 panic("overrun attribute buffer\n"); 1020 memmove(&extbuf[extloc], buf, size); 1021 extloc += size; 1022 } 1023 1024 /* 1025 * Write out the next block of a file. 1026 */ 1027 static void 1028 xtrfile(char *buf, size_t size) 1029 { 1030 1031 if (Nflag) 1032 return; 1033 if (write(ofile, buf, (int) size) == -1) { 1034 fprintf(stderr, 1035 "write error extracting inode %ju, name %s\nwrite: %s\n", 1036 (uintmax_t)curfile.ino, curfile.name, strerror(errno)); 1037 } 1038 } 1039 1040 /* 1041 * Skip over a hole in a file. 1042 */ 1043 /* ARGSUSED */ 1044 static void 1045 xtrskip(char *buf, size_t size) 1046 { 1047 1048 if (lseek(ofile, size, SEEK_CUR) == -1) { 1049 fprintf(stderr, 1050 "seek error extracting inode %ju, name %s\nlseek: %s\n", 1051 (uintmax_t)curfile.ino, curfile.name, strerror(errno)); 1052 done(1); 1053 } 1054 } 1055 1056 /* 1057 * Collect the next block of a symbolic link. 1058 */ 1059 static void 1060 xtrlnkfile(char *buf, size_t size) 1061 { 1062 1063 pathlen += size; 1064 if (pathlen > MAXPATHLEN) { 1065 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n", 1066 curfile.name, lnkbuf, buf, pathlen); 1067 done(1); 1068 } 1069 (void) strcat(lnkbuf, buf); 1070 } 1071 1072 /* 1073 * Skip over a hole in a symbolic link (should never happen). 1074 */ 1075 /* ARGSUSED */ 1076 static void 1077 xtrlnkskip(char *buf, size_t size) 1078 { 1079 1080 fprintf(stderr, "unallocated block in symbolic link %s\n", 1081 curfile.name); 1082 done(1); 1083 } 1084 1085 /* 1086 * Collect the next block of a bit map. 1087 */ 1088 static void 1089 xtrmap(char *buf, size_t size) 1090 { 1091 1092 memmove(map, buf, size); 1093 map += size; 1094 } 1095 1096 /* 1097 * Skip over a hole in a bit map (should never happen). 1098 */ 1099 /* ARGSUSED */ 1100 static void 1101 xtrmapskip(char *buf, size_t size) 1102 { 1103 1104 panic("hole in map\n"); 1105 map += size; 1106 } 1107 1108 /* 1109 * Noop, when an extraction function is not needed. 1110 */ 1111 /* ARGSUSED */ 1112 void 1113 xtrnull(char *buf, size_t size) 1114 { 1115 1116 return; 1117 } 1118 1119 /* 1120 * Read TP_BSIZE blocks from the input. 1121 * Handle read errors, and end of media. 1122 */ 1123 static void 1124 readtape(char *buf) 1125 { 1126 long rd, newvol, i, oldnumtrec; 1127 int cnt, seek_failed; 1128 1129 if (blkcnt + (byteslide > 0) < numtrec) { 1130 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE); 1131 blksread++; 1132 tapeaddr++; 1133 return; 1134 } 1135 if (numtrec > 0) 1136 memmove(&tapebuf[-TP_BSIZE], 1137 &tapebuf[(numtrec-1) * TP_BSIZE], (long)TP_BSIZE); 1138 oldnumtrec = numtrec; 1139 for (i = 0; i < ntrec; i++) 1140 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0; 1141 if (numtrec == 0) 1142 numtrec = ntrec; 1143 cnt = ntrec * TP_BSIZE; 1144 rd = 0; 1145 getmore: 1146 #ifdef RRESTORE 1147 if (host) 1148 i = rmtread(&tapebuf[rd], cnt); 1149 else 1150 #endif 1151 i = read(mt, &tapebuf[rd], cnt); 1152 /* 1153 * Check for mid-tape short read error. 1154 * If found, skip rest of buffer and start with the next. 1155 */ 1156 if (!pipein && !pipecmdin && numtrec < ntrec && i > 0) { 1157 dprintf(stdout, "mid-media short read error.\n"); 1158 numtrec = ntrec; 1159 } 1160 /* 1161 * Handle partial block read. 1162 */ 1163 if ((pipein || pipecmdin) && i == 0 && rd > 0) 1164 i = rd; 1165 else if (i > 0 && i != ntrec * TP_BSIZE) { 1166 if (pipein || pipecmdin) { 1167 rd += i; 1168 cnt -= i; 1169 if (cnt > 0) 1170 goto getmore; 1171 i = rd; 1172 } else { 1173 /* 1174 * Short read. Process the blocks read. 1175 */ 1176 if (i % TP_BSIZE != 0) 1177 vprintf(stdout, 1178 "partial block read: %ld should be %ld\n", 1179 i, ntrec * TP_BSIZE); 1180 numtrec = i / TP_BSIZE; 1181 } 1182 } 1183 /* 1184 * Handle read error. 1185 */ 1186 if (i < 0) { 1187 fprintf(stderr, "Tape read error while "); 1188 switch (curfile.action) { 1189 default: 1190 fprintf(stderr, "trying to set up tape\n"); 1191 break; 1192 case UNKNOWN: 1193 fprintf(stderr, "trying to resynchronize\n"); 1194 break; 1195 case USING: 1196 fprintf(stderr, "restoring %s\n", curfile.name); 1197 break; 1198 case SKIP: 1199 fprintf(stderr, "skipping over inode %ju\n", 1200 (uintmax_t)curfile.ino); 1201 break; 1202 } 1203 if (!yflag && !reply("continue")) 1204 done(1); 1205 i = ntrec * TP_BSIZE; 1206 memset(tapebuf, 0, i); 1207 #ifdef RRESTORE 1208 if (host) 1209 seek_failed = (rmtseek(i, 1) < 0); 1210 else 1211 #endif 1212 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1); 1213 1214 if (seek_failed) { 1215 fprintf(stderr, 1216 "continuation failed: %s\n", strerror(errno)); 1217 done(1); 1218 } 1219 } 1220 /* 1221 * Handle end of tape. 1222 */ 1223 if (i == 0) { 1224 vprintf(stdout, "End-of-tape encountered\n"); 1225 if (!pipein) { 1226 newvol = volno + 1; 1227 volno = 0; 1228 numtrec = 0; 1229 getvol(newvol); 1230 readtape(buf); 1231 return; 1232 } 1233 if (rd % TP_BSIZE != 0) 1234 panic("partial block read: %ld should be %ld\n", 1235 rd, ntrec * TP_BSIZE); 1236 terminateinput(); 1237 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE); 1238 } 1239 if (oldnumtrec == 0) 1240 blkcnt = 0; 1241 else 1242 blkcnt -= oldnumtrec; 1243 memmove(buf, 1244 &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE); 1245 blksread++; 1246 tapeaddr++; 1247 } 1248 1249 static void 1250 findtapeblksize(void) 1251 { 1252 long i; 1253 1254 for (i = 0; i < ntrec; i++) 1255 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0; 1256 blkcnt = 0; 1257 #ifdef RRESTORE 1258 if (host) 1259 i = rmtread(tapebuf, ntrec * TP_BSIZE); 1260 else 1261 #endif 1262 i = read(mt, tapebuf, ntrec * TP_BSIZE); 1263 1264 if (i <= 0) { 1265 fprintf(stderr, "tape read error: %s\n", strerror(errno)); 1266 done(1); 1267 } 1268 if (i % TP_BSIZE != 0) { 1269 fprintf(stderr, "Tape block size (%ld) %s (%d)\n", 1270 i, "is not a multiple of dump block size", TP_BSIZE); 1271 done(1); 1272 } 1273 ntrec = i / TP_BSIZE; 1274 numtrec = ntrec; 1275 vprintf(stdout, "Tape block size is %ld\n", ntrec); 1276 } 1277 1278 void 1279 closemt(void) 1280 { 1281 1282 if (mt < 0) 1283 return; 1284 if (pipecmdin) { 1285 pclose(popenfp); 1286 popenfp = NULL; 1287 } else 1288 #ifdef RRESTORE 1289 if (host) 1290 rmtclose(); 1291 else 1292 #endif 1293 (void) close(mt); 1294 } 1295 1296 /* 1297 * Read the next block from the tape. 1298 * If it is not any valid header, return an error. 1299 */ 1300 static int 1301 gethead(struct s_spcl *buf) 1302 { 1303 long i; 1304 1305 readtape((char *)buf); 1306 if (buf->c_magic != FS_UFS2_MAGIC && buf->c_magic != NFS_MAGIC) { 1307 if (buf->c_magic == OFS_MAGIC) { 1308 fprintf(stderr, 1309 "Format of dump tape is too old. Must use\n"); 1310 fprintf(stderr, 1311 "a version of restore from before 2002.\n"); 1312 return (FAIL); 1313 } 1314 if (swabl(buf->c_magic) != FS_UFS2_MAGIC && 1315 buf->c_magic != NFS_MAGIC) { 1316 if (buf->c_magic == OFS_MAGIC) { 1317 fprintf(stderr, 1318 "Format of dump tape is too old. Must use\n"); 1319 fprintf(stderr, 1320 "a version of restore from before 2002.\n"); 1321 } 1322 return (FAIL); 1323 } 1324 if (!Bcvt) { 1325 vprintf(stdout, "Note: Doing Byte swapping\n"); 1326 Bcvt = 1; 1327 } 1328 } 1329 if (checksum((int *)buf) == FAIL) 1330 return (FAIL); 1331 if (Bcvt) { 1332 swabst((u_char *)"8l4s1q8l2q17l", (u_char *)buf); 1333 swabst((u_char *)"l",(u_char *) &buf->c_level); 1334 swabst((u_char *)"2l4q",(u_char *) &buf->c_flags); 1335 } 1336 readmapflag = 0; 1337 1338 switch (buf->c_type) { 1339 1340 case TS_CLRI: 1341 case TS_BITS: 1342 /* 1343 * Have to patch up missing information in bit map headers 1344 */ 1345 buf->c_size = buf->c_count * TP_BSIZE; 1346 if (buf->c_count > TP_NINDIR) 1347 readmapflag = 1; 1348 else 1349 for (i = 0; i < buf->c_count; i++) 1350 buf->c_addr[i]++; 1351 /* FALL THROUGH */ 1352 1353 case TS_TAPE: 1354 if (buf->c_magic == NFS_MAGIC && 1355 (buf->c_flags & NFS_DR_NEWINODEFMT) == 0) 1356 oldinofmt = 1; 1357 /* FALL THROUGH */ 1358 1359 case TS_END: 1360 buf->c_inumber = 0; 1361 /* FALL THROUGH */ 1362 1363 case TS_ADDR: 1364 case TS_INODE: 1365 /* 1366 * For old dump tapes, have to copy up old fields to 1367 * new locations. 1368 */ 1369 if (buf->c_magic == NFS_MAGIC) { 1370 buf->c_tapea = buf->c_old_tapea; 1371 buf->c_firstrec = buf->c_old_firstrec; 1372 buf->c_date = _time32_to_time(buf->c_old_date); 1373 buf->c_ddate = _time32_to_time(buf->c_old_ddate); 1374 buf->c_atime = _time32_to_time(buf->c_old_atime); 1375 buf->c_mtime = _time32_to_time(buf->c_old_mtime); 1376 buf->c_birthtime = 0; 1377 buf->c_birthtimensec = 0; 1378 buf->c_extsize = 0; 1379 } 1380 break; 1381 1382 default: 1383 panic("gethead: unknown inode type %d\n", buf->c_type); 1384 break; 1385 } 1386 if (dumpdate != 0 && _time64_to_time(buf->c_date) != dumpdate) 1387 fprintf(stderr, "Header with wrong dumpdate.\n"); 1388 /* 1389 * If we're restoring a filesystem with the old (FreeBSD 1) 1390 * format inodes, copy the uid/gid to the new location 1391 */ 1392 if (oldinofmt) { 1393 buf->c_uid = buf->c_spare1[1]; 1394 buf->c_gid = buf->c_spare1[2]; 1395 } 1396 buf->c_magic = FS_UFS2_MAGIC; 1397 tapeaddr = buf->c_tapea; 1398 if (dflag) 1399 accthdr(buf); 1400 return(GOOD); 1401 } 1402 1403 /* 1404 * Check that a header is where it belongs and predict the next header 1405 */ 1406 static void 1407 accthdr(struct s_spcl *header) 1408 { 1409 static ino_t previno = 0x7fffffff; 1410 static int prevtype; 1411 static long predict; 1412 long blks, i; 1413 1414 if (header->c_type == TS_TAPE) { 1415 fprintf(stderr, "Volume header "); 1416 if (header->c_firstrec) 1417 fprintf(stderr, "begins with record %jd", 1418 (intmax_t)header->c_firstrec); 1419 fprintf(stderr, "\n"); 1420 previno = 0x7fffffff; 1421 return; 1422 } 1423 if (previno == 0x7fffffff) 1424 goto newcalc; 1425 switch (prevtype) { 1426 case TS_BITS: 1427 fprintf(stderr, "Dumped inodes map header"); 1428 break; 1429 case TS_CLRI: 1430 fprintf(stderr, "Used inodes map header"); 1431 break; 1432 case TS_INODE: 1433 fprintf(stderr, "File header, ino %ju", (uintmax_t)previno); 1434 break; 1435 case TS_ADDR: 1436 fprintf(stderr, "File continuation header, ino %ju", 1437 (uintmax_t)previno); 1438 break; 1439 case TS_END: 1440 fprintf(stderr, "End of tape header"); 1441 break; 1442 } 1443 if (predict != blksread - 1) 1444 fprintf(stderr, "; predicted %ld blocks, got %ld blocks", 1445 predict, blksread - 1); 1446 fprintf(stderr, "\n"); 1447 newcalc: 1448 blks = 0; 1449 if (header->c_type != TS_END) 1450 for (i = 0; i < header->c_count; i++) 1451 if (readmapflag || header->c_addr[i] != 0) 1452 blks++; 1453 predict = blks; 1454 blksread = 0; 1455 prevtype = header->c_type; 1456 previno = header->c_inumber; 1457 } 1458 1459 /* 1460 * Find an inode header. 1461 * Complain if had to skip. 1462 */ 1463 static void 1464 findinode(struct s_spcl *header) 1465 { 1466 static long skipcnt = 0; 1467 long i; 1468 char buf[TP_BSIZE]; 1469 int htype; 1470 1471 curfile.name = "<name unknown>"; 1472 curfile.action = UNKNOWN; 1473 curfile.mode = 0; 1474 curfile.ino = 0; 1475 do { 1476 htype = header->c_type; 1477 switch (htype) { 1478 1479 case TS_ADDR: 1480 /* 1481 * Skip up to the beginning of the next record 1482 */ 1483 for (i = 0; i < header->c_count; i++) 1484 if (header->c_addr[i]) 1485 readtape(buf); 1486 while (gethead(header) == FAIL || 1487 _time64_to_time(header->c_date) != dumpdate) { 1488 skipcnt++; 1489 if (Dflag) { 1490 byteslide++; 1491 if (byteslide < TP_BSIZE) { 1492 blkcnt--; 1493 blksread--; 1494 } else 1495 byteslide = 0; 1496 } 1497 } 1498 break; 1499 1500 case TS_INODE: 1501 curfile.mode = header->c_mode; 1502 curfile.uid = header->c_uid; 1503 curfile.gid = header->c_gid; 1504 curfile.file_flags = header->c_file_flags; 1505 curfile.rdev = header->c_rdev; 1506 curfile.atime_sec = header->c_atime; 1507 curfile.atime_nsec = header->c_atimensec; 1508 curfile.mtime_sec = header->c_mtime; 1509 curfile.mtime_nsec = header->c_mtimensec; 1510 curfile.birthtime_sec = header->c_birthtime; 1511 curfile.birthtime_nsec = header->c_birthtimensec; 1512 curfile.extsize = header->c_extsize; 1513 curfile.size = header->c_size; 1514 curfile.ino = header->c_inumber; 1515 break; 1516 1517 case TS_END: 1518 /* If we missed some tapes, get another volume. */ 1519 if (tapesread & (tapesread + 1)) { 1520 getvol(0); 1521 continue; 1522 } 1523 curfile.ino = maxino; 1524 break; 1525 1526 case TS_CLRI: 1527 curfile.name = "<file removal list>"; 1528 break; 1529 1530 case TS_BITS: 1531 curfile.name = "<file dump list>"; 1532 break; 1533 1534 case TS_TAPE: 1535 if (Dflag) 1536 fprintf(stderr, "unexpected tape header\n"); 1537 else 1538 panic("unexpected tape header\n"); 1539 1540 default: 1541 if (Dflag) 1542 fprintf(stderr, "unknown tape header type %d\n", 1543 spcl.c_type); 1544 else 1545 panic("unknown tape header type %d\n", 1546 spcl.c_type); 1547 while (gethead(header) == FAIL || 1548 _time64_to_time(header->c_date) != dumpdate) { 1549 skipcnt++; 1550 if (Dflag) { 1551 byteslide++; 1552 if (byteslide < TP_BSIZE) { 1553 blkcnt--; 1554 blksread--; 1555 } else 1556 byteslide = 0; 1557 } 1558 } 1559 1560 } 1561 } while (htype == TS_ADDR); 1562 if (skipcnt > 0) 1563 fprintf(stderr, "resync restore, skipped %ld %s\n", 1564 skipcnt, Dflag ? "bytes" : "blocks"); 1565 skipcnt = 0; 1566 } 1567 1568 static int 1569 checksum(int *buf) 1570 { 1571 int i, j; 1572 1573 j = sizeof(union u_spcl) / sizeof(int); 1574 i = 0; 1575 if (!Bcvt) { 1576 do 1577 i += *buf++; 1578 while (--j); 1579 } else { 1580 /* What happens if we want to read restore tapes 1581 for a 16bit int machine??? */ 1582 do 1583 i += swabl(*buf++); 1584 while (--j); 1585 } 1586 1587 if (i != CHECKSUM) { 1588 fprintf(stderr, "Checksum error %o, inode %ju file %s\n", i, 1589 (uintmax_t)curfile.ino, curfile.name); 1590 return(FAIL); 1591 } 1592 return(GOOD); 1593 } 1594 1595 #ifdef RRESTORE 1596 #include <stdarg.h> 1597 1598 void 1599 msg(const char *fmt, ...) 1600 { 1601 va_list ap; 1602 va_start(ap, fmt); 1603 (void)vfprintf(stderr, fmt, ap); 1604 va_end(ap); 1605 } 1606 #endif /* RRESTORE */ 1607 1608 static u_char * 1609 swabshort(u_char *sp, int n) 1610 { 1611 char c; 1612 1613 while (--n >= 0) { 1614 c = sp[0]; sp[0] = sp[1]; sp[1] = c; 1615 sp += 2; 1616 } 1617 return (sp); 1618 } 1619 1620 static u_char * 1621 swablong(u_char *sp, int n) 1622 { 1623 char c; 1624 1625 while (--n >= 0) { 1626 c = sp[0]; sp[0] = sp[3]; sp[3] = c; 1627 c = sp[2]; sp[2] = sp[1]; sp[1] = c; 1628 sp += 4; 1629 } 1630 return (sp); 1631 } 1632 1633 static u_char * 1634 swabquad(u_char *sp, int n) 1635 { 1636 char c; 1637 1638 while (--n >= 0) { 1639 c = sp[0]; sp[0] = sp[7]; sp[7] = c; 1640 c = sp[1]; sp[1] = sp[6]; sp[6] = c; 1641 c = sp[2]; sp[2] = sp[5]; sp[5] = c; 1642 c = sp[3]; sp[3] = sp[4]; sp[4] = c; 1643 sp += 8; 1644 } 1645 return (sp); 1646 } 1647 1648 void 1649 swabst(u_char *cp, u_char *sp) 1650 { 1651 int n = 0; 1652 1653 while (*cp) { 1654 switch (*cp) { 1655 case '0': case '1': case '2': case '3': case '4': 1656 case '5': case '6': case '7': case '8': case '9': 1657 n = (n * 10) + (*cp++ - '0'); 1658 continue; 1659 1660 case 's': case 'w': case 'h': 1661 if (n == 0) 1662 n = 1; 1663 sp = swabshort(sp, n); 1664 break; 1665 1666 case 'l': 1667 if (n == 0) 1668 n = 1; 1669 sp = swablong(sp, n); 1670 break; 1671 1672 case 'q': 1673 if (n == 0) 1674 n = 1; 1675 sp = swabquad(sp, n); 1676 break; 1677 1678 case 'b': 1679 if (n == 0) 1680 n = 1; 1681 sp += n; 1682 break; 1683 1684 default: 1685 fprintf(stderr, "Unknown conversion character: %c\n", 1686 *cp); 1687 done(0); 1688 break; 1689 } 1690 cp++; 1691 n = 0; 1692 } 1693 } 1694 1695 static u_long 1696 swabl(u_long x) 1697 { 1698 swabst((u_char *)"l", (u_char *)&x); 1699 return (x); 1700 } 1701