1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 char copyright[] = 9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)tar.c 5.9 (Berkeley) 04/10/87"; 15 #endif not lint 16 17 /* 18 * Tape Archival Program 19 */ 20 #include <stdio.h> 21 #include <sys/param.h> 22 #include <sys/stat.h> 23 #include <sys/dir.h> 24 #include <sys/ioctl.h> 25 #include <sys/mtio.h> 26 #include <sys/time.h> 27 #include <signal.h> 28 #include <errno.h> 29 #include <fcntl.h> 30 31 #define TBLOCK 512 32 #define NBLOCK 20 33 #define NAMSIZ 100 34 35 #define writetape(b) writetbuf(b, 1) 36 #define min(a,b) ((a) < (b) ? (a) : (b)) 37 #define max(a,b) ((a) > (b) ? (a) : (b)) 38 39 union hblock { 40 char dummy[TBLOCK]; 41 struct header { 42 char name[NAMSIZ]; 43 char mode[8]; 44 char uid[8]; 45 char gid[8]; 46 char size[12]; 47 char mtime[12]; 48 char chksum[8]; 49 char linkflag; 50 char linkname[NAMSIZ]; 51 } dbuf; 52 }; 53 54 struct linkbuf { 55 ino_t inum; 56 dev_t devnum; 57 int count; 58 char pathname[NAMSIZ]; 59 struct linkbuf *nextp; 60 }; 61 62 union hblock dblock; 63 union hblock *tbuf; 64 struct linkbuf *ihead; 65 struct stat stbuf; 66 67 int rflag; 68 int xflag; 69 int vflag; 70 int tflag; 71 int cflag; 72 int mflag; 73 int fflag; 74 int iflag; 75 int oflag; 76 int pflag; 77 int wflag; 78 int hflag; 79 int Bflag; 80 int Fflag; 81 82 int mt; 83 int term; 84 int chksum; 85 int recno; 86 int first; 87 int prtlinkerr; 88 int freemem = 1; 89 int nblock = 0; 90 int onintr(); 91 int onquit(); 92 int onhup(); 93 #ifdef notdef 94 int onterm(); 95 #endif 96 97 daddr_t low; 98 daddr_t high; 99 daddr_t bsrch(); 100 101 FILE *vfile = stdout; 102 FILE *tfile; 103 char tname[] = "/tmp/tarXXXXXX"; 104 char *usefile; 105 char magtape[] = "/dev/rmt8"; 106 char *malloc(); 107 long time(); 108 off_t lseek(); 109 char *mktemp(); 110 char *sprintf(); 111 char *strcat(); 112 char *strcpy(); 113 char *rindex(); 114 char *getcwd(); 115 char *getwd(); 116 char *getmem(); 117 118 main(argc, argv) 119 int argc; 120 char *argv[]; 121 { 122 char *cp; 123 124 if (argc < 2) 125 usage(); 126 127 tfile = NULL; 128 usefile = magtape; 129 argv[argc] = 0; 130 argv++; 131 for (cp = *argv++; *cp; cp++) 132 switch(*cp) { 133 134 case 'f': 135 if (*argv == 0) { 136 fprintf(stderr, 137 "tar: tapefile must be specified with 'f' option\n"); 138 usage(); 139 } 140 usefile = *argv++; 141 fflag++; 142 break; 143 144 case 'c': 145 cflag++; 146 rflag++; 147 break; 148 149 case 'o': 150 oflag++; 151 break; 152 153 case 'p': 154 pflag++; 155 break; 156 157 case 'u': 158 mktemp(tname); 159 if ((tfile = fopen(tname, "w")) == NULL) { 160 fprintf(stderr, 161 "tar: cannot create temporary file (%s)\n", 162 tname); 163 done(1); 164 } 165 fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n"); 166 /*FALL THRU*/ 167 168 case 'r': 169 rflag++; 170 break; 171 172 case 'v': 173 vflag++; 174 break; 175 176 case 'w': 177 wflag++; 178 break; 179 180 case 'x': 181 xflag++; 182 break; 183 184 case 't': 185 tflag++; 186 break; 187 188 case 'm': 189 mflag++; 190 break; 191 192 case '-': 193 break; 194 195 case '0': 196 case '1': 197 case '4': 198 case '5': 199 case '7': 200 case '8': 201 magtape[8] = *cp; 202 usefile = magtape; 203 break; 204 205 case 'b': 206 if (*argv == 0) { 207 fprintf(stderr, 208 "tar: blocksize must be specified with 'b' option\n"); 209 usage(); 210 } 211 nblock = atoi(*argv); 212 if (nblock <= 0) { 213 fprintf(stderr, 214 "tar: invalid blocksize \"%s\"\n", *argv); 215 done(1); 216 } 217 argv++; 218 break; 219 220 case 'l': 221 prtlinkerr++; 222 break; 223 224 case 'h': 225 hflag++; 226 break; 227 228 case 'i': 229 iflag++; 230 break; 231 232 case 'B': 233 Bflag++; 234 break; 235 236 case 'F': 237 Fflag++; 238 break; 239 240 default: 241 fprintf(stderr, "tar: %c: unknown option\n", *cp); 242 usage(); 243 } 244 245 if (!rflag && !xflag && !tflag) 246 usage(); 247 if (rflag) { 248 if (cflag && tfile != NULL) 249 usage(); 250 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 251 (void) signal(SIGINT, onintr); 252 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 253 (void) signal(SIGHUP, onhup); 254 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) 255 (void) signal(SIGQUIT, onquit); 256 #ifdef notdef 257 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 258 (void) signal(SIGTERM, onterm); 259 #endif 260 mt = openmt(usefile, 1); 261 dorep(argv); 262 done(0); 263 } 264 mt = openmt(usefile, 0); 265 if (xflag) 266 doxtract(argv); 267 else 268 dotable(argv); 269 done(0); 270 } 271 272 usage() 273 { 274 fprintf(stderr, 275 "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n"); 276 done(1); 277 } 278 279 int 280 openmt(tape, writing) 281 char *tape; 282 int writing; 283 { 284 285 if (strcmp(tape, "-") == 0) { 286 /* 287 * Read from standard input or write to standard output. 288 */ 289 if (writing) { 290 if (cflag == 0) { 291 fprintf(stderr, 292 "tar: can only create standard output archives\n"); 293 done(1); 294 } 295 vfile = stderr; 296 setlinebuf(vfile); 297 mt = dup(1); 298 } else { 299 mt = dup(0); 300 Bflag++; 301 } 302 } else { 303 /* 304 * Use file or tape on local machine. 305 */ 306 if (writing) { 307 if (cflag) 308 mt = open(tape, O_RDWR|O_CREAT|O_TRUNC, 0666); 309 else 310 mt = open(tape, O_RDWR); 311 } else 312 mt = open(tape, O_RDONLY); 313 if (mt < 0) { 314 fprintf(stderr, "tar: "); 315 perror(tape); 316 done(1); 317 } 318 } 319 return(mt); 320 } 321 322 dorep(argv) 323 char *argv[]; 324 { 325 register char *cp, *cp2; 326 char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent; 327 328 if (!cflag) { 329 getdir(); 330 do { 331 passtape(); 332 if (term) 333 done(0); 334 getdir(); 335 } while (!endtape()); 336 backtape(); 337 if (tfile != NULL) { 338 char buf[200]; 339 340 sprintf(buf, 341 "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s", 342 tname, tname, tname, tname, tname, tname); 343 fflush(tfile); 344 system(buf); 345 freopen(tname, "r", tfile); 346 fstat(fileno(tfile), &stbuf); 347 high = stbuf.st_size; 348 } 349 } 350 351 (void) getcwd(wdir); 352 while (*argv && ! term) { 353 cp2 = *argv; 354 if (!strcmp(cp2, "-C") && argv[1]) { 355 argv++; 356 if (chdir(*argv) < 0) { 357 fprintf(stderr, "tar: can't change directories to "); 358 perror(*argv); 359 } else 360 (void) getcwd(wdir); 361 argv++; 362 continue; 363 } 364 parent = wdir; 365 for (cp = *argv; *cp; cp++) 366 if (*cp == '/') 367 cp2 = cp; 368 if (cp2 != *argv) { 369 *cp2 = '\0'; 370 if (chdir(*argv) < 0) { 371 fprintf(stderr, "tar: can't change directories to "); 372 perror(*argv); 373 continue; 374 } 375 parent = getcwd(tempdir); 376 *cp2 = '/'; 377 cp2++; 378 } 379 putfile(*argv++, cp2, parent); 380 if (chdir(wdir) < 0) { 381 fprintf(stderr, "tar: cannot change back?: "); 382 perror(wdir); 383 } 384 } 385 putempty(); 386 putempty(); 387 flushtape(); 388 if (prtlinkerr == 0) 389 return; 390 for (; ihead != NULL; ihead = ihead->nextp) { 391 if (ihead->count == 0) 392 continue; 393 fprintf(stderr, "tar: missing links to %s\n", ihead->pathname); 394 } 395 } 396 397 endtape() 398 { 399 return (dblock.dbuf.name[0] == '\0'); 400 } 401 402 getdir() 403 { 404 register struct stat *sp; 405 int i; 406 407 top: 408 readtape((char *)&dblock); 409 if (dblock.dbuf.name[0] == '\0') 410 return; 411 sp = &stbuf; 412 sscanf(dblock.dbuf.mode, "%o", &i); 413 sp->st_mode = i; 414 sscanf(dblock.dbuf.uid, "%o", &i); 415 sp->st_uid = i; 416 sscanf(dblock.dbuf.gid, "%o", &i); 417 sp->st_gid = i; 418 sscanf(dblock.dbuf.size, "%lo", &sp->st_size); 419 sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime); 420 sscanf(dblock.dbuf.chksum, "%o", &chksum); 421 if (chksum != (i = checksum())) { 422 fprintf(stderr, "tar: directory checksum error (%d != %d)\n", 423 chksum, i); 424 if (iflag) 425 goto top; 426 done(2); 427 } 428 if (tfile != NULL) 429 fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime); 430 } 431 432 passtape() 433 { 434 long blocks; 435 char *bufp; 436 437 if (dblock.dbuf.linkflag == '1') 438 return; 439 blocks = stbuf.st_size; 440 blocks += TBLOCK-1; 441 blocks /= TBLOCK; 442 443 while (blocks-- > 0) 444 (void) readtbuf(&bufp, TBLOCK); 445 } 446 447 putfile(longname, shortname, parent) 448 char *longname; 449 char *shortname; 450 char *parent; 451 { 452 int infile = 0; 453 long blocks; 454 char buf[TBLOCK]; 455 char *bigbuf; 456 register char *cp; 457 struct direct *dp; 458 DIR *dirp; 459 register int i; 460 long l; 461 char newparent[NAMSIZ+64]; 462 extern int errno; 463 int maxread; 464 int hint; /* amount to write to get "in sync" */ 465 466 if (!hflag) 467 i = lstat(shortname, &stbuf); 468 else 469 i = stat(shortname, &stbuf); 470 if (i < 0) { 471 fprintf(stderr, "tar: "); 472 perror(longname); 473 return; 474 } 475 if (tfile != NULL && checkupdate(longname) == 0) 476 return; 477 if (checkw('r', longname) == 0) 478 return; 479 if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0) 480 return; 481 482 switch (stbuf.st_mode & S_IFMT) { 483 case S_IFDIR: 484 for (i = 0, cp = buf; *cp++ = longname[i++];) 485 ; 486 *--cp = '/'; 487 *++cp = 0 ; 488 if (!oflag) { 489 if ((cp - buf) >= NAMSIZ) { 490 fprintf(stderr, "tar: %s: file name too long\n", 491 longname); 492 return; 493 } 494 stbuf.st_size = 0; 495 tomodes(&stbuf); 496 strcpy(dblock.dbuf.name,buf); 497 sprintf(dblock.dbuf.chksum, "%6o", checksum()); 498 (void) writetape((char *)&dblock); 499 } 500 sprintf(newparent, "%s/%s", parent, shortname); 501 if (chdir(shortname) < 0) { 502 perror(shortname); 503 return; 504 } 505 if ((dirp = opendir(".")) == NULL) { 506 fprintf(stderr, "tar: %s: directory read error\n", 507 longname); 508 if (chdir(parent) < 0) { 509 fprintf(stderr, "tar: cannot change back?: "); 510 perror(parent); 511 } 512 return; 513 } 514 while ((dp = readdir(dirp)) != NULL && !term) { 515 if (!strcmp(".", dp->d_name) || 516 !strcmp("..", dp->d_name)) 517 continue; 518 strcpy(cp, dp->d_name); 519 l = telldir(dirp); 520 closedir(dirp); 521 putfile(buf, cp, newparent); 522 dirp = opendir("."); 523 seekdir(dirp, l); 524 } 525 closedir(dirp); 526 if (chdir(parent) < 0) { 527 fprintf(stderr, "tar: cannot change back?: "); 528 perror(parent); 529 } 530 break; 531 532 case S_IFLNK: 533 tomodes(&stbuf); 534 if (strlen(longname) >= NAMSIZ) { 535 fprintf(stderr, "tar: %s: file name too long\n", 536 longname); 537 return; 538 } 539 strcpy(dblock.dbuf.name, longname); 540 if (stbuf.st_size + 1 >= NAMSIZ) { 541 fprintf(stderr, "tar: %s: symbolic link too long\n", 542 longname); 543 return; 544 } 545 i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1); 546 if (i < 0) { 547 fprintf(stderr, "tar: can't read symbolic link "); 548 perror(longname); 549 return; 550 } 551 dblock.dbuf.linkname[i] = '\0'; 552 dblock.dbuf.linkflag = '2'; 553 if (vflag) 554 fprintf(vfile, "a %s symbolic link to %s\n", 555 longname, dblock.dbuf.linkname); 556 sprintf(dblock.dbuf.size, "%11lo", 0L); 557 sprintf(dblock.dbuf.chksum, "%6o", checksum()); 558 (void) writetape((char *)&dblock); 559 break; 560 561 case S_IFREG: 562 if ((infile = open(shortname, 0)) < 0) { 563 fprintf(stderr, "tar: "); 564 perror(longname); 565 return; 566 } 567 tomodes(&stbuf); 568 if (strlen(longname) >= NAMSIZ) { 569 fprintf(stderr, "tar: %s: file name too long\n", 570 longname); 571 close(infile); 572 return; 573 } 574 strcpy(dblock.dbuf.name, longname); 575 if (stbuf.st_nlink > 1) { 576 struct linkbuf *lp; 577 int found = 0; 578 579 for (lp = ihead; lp != NULL; lp = lp->nextp) 580 if (lp->inum == stbuf.st_ino && 581 lp->devnum == stbuf.st_dev) { 582 found++; 583 break; 584 } 585 if (found) { 586 strcpy(dblock.dbuf.linkname, lp->pathname); 587 dblock.dbuf.linkflag = '1'; 588 sprintf(dblock.dbuf.chksum, "%6o", checksum()); 589 (void) writetape( (char *) &dblock); 590 if (vflag) 591 fprintf(vfile, "a %s link to %s\n", 592 longname, lp->pathname); 593 lp->count--; 594 close(infile); 595 return; 596 } 597 lp = (struct linkbuf *) getmem(sizeof(*lp)); 598 if (lp != NULL) { 599 lp->nextp = ihead; 600 ihead = lp; 601 lp->inum = stbuf.st_ino; 602 lp->devnum = stbuf.st_dev; 603 lp->count = stbuf.st_nlink - 1; 604 strcpy(lp->pathname, longname); 605 } 606 } 607 blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK; 608 if (vflag) 609 fprintf(vfile, "a %s %ld blocks\n", longname, blocks); 610 sprintf(dblock.dbuf.chksum, "%6o", checksum()); 611 hint = writetape((char *)&dblock); 612 maxread = max(stbuf.st_blksize, (nblock * TBLOCK)); 613 if ((bigbuf = malloc((unsigned)maxread)) == 0) { 614 maxread = TBLOCK; 615 bigbuf = buf; 616 } 617 618 while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0 619 && blocks > 0) { 620 register int nblks; 621 622 nblks = ((i-1)/TBLOCK)+1; 623 if (nblks > blocks) 624 nblks = blocks; 625 hint = writetbuf(bigbuf, nblks); 626 blocks -= nblks; 627 } 628 close(infile); 629 if (bigbuf != buf) 630 free(bigbuf); 631 if (i < 0) { 632 fprintf(stderr, "tar: Read error on "); 633 perror(longname); 634 } else if (blocks != 0 || i != 0) 635 fprintf(stderr, "tar: %s: file changed size\n", 636 longname); 637 while (--blocks >= 0) 638 putempty(); 639 break; 640 641 default: 642 fprintf(stderr, "tar: %s is not a file. Not dumped\n", 643 longname); 644 break; 645 } 646 } 647 648 doxtract(argv) 649 char *argv[]; 650 { 651 long blocks, bytes; 652 int ofile, i; 653 654 for (;;) { 655 if ((i = wantit(argv)) == 0) 656 continue; 657 if (i == -1) 658 break; /* end of tape */ 659 if (checkw('x', dblock.dbuf.name) == 0) { 660 passtape(); 661 continue; 662 } 663 if (Fflag) { 664 char *s; 665 666 if ((s = rindex(dblock.dbuf.name, '/')) == 0) 667 s = dblock.dbuf.name; 668 else 669 s++; 670 if (checkf(s, stbuf.st_mode, Fflag) == 0) { 671 passtape(); 672 continue; 673 } 674 } 675 if (checkdir(dblock.dbuf.name)) { /* have a directory */ 676 if (mflag == 0) 677 dodirtimes(&dblock); 678 continue; 679 } 680 if (dblock.dbuf.linkflag == '2') { /* symlink */ 681 /* 682 * only unlink non directories or empty 683 * directories 684 */ 685 if (rmdir(dblock.dbuf.name) < 0) { 686 if (errno == ENOTDIR) 687 unlink(dblock.dbuf.name); 688 } 689 if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) { 690 fprintf(stderr, "tar: %s: symbolic link failed: ", 691 dblock.dbuf.name); 692 perror(""); 693 continue; 694 } 695 if (vflag) 696 fprintf(vfile, "x %s symbolic link to %s\n", 697 dblock.dbuf.name, dblock.dbuf.linkname); 698 #ifdef notdef 699 /* ignore alien orders */ 700 chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 701 if (mflag == 0) 702 setimes(dblock.dbuf.name, stbuf.st_mtime); 703 if (pflag) 704 chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 705 #endif 706 continue; 707 } 708 if (dblock.dbuf.linkflag == '1') { /* regular link */ 709 /* 710 * only unlink non directories or empty 711 * directories 712 */ 713 if (rmdir(dblock.dbuf.name) < 0) { 714 if (errno == ENOTDIR) 715 unlink(dblock.dbuf.name); 716 } 717 if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) { 718 fprintf(stderr, "tar: can't link %s to %s: ", 719 dblock.dbuf.name, dblock.dbuf.linkname); 720 perror(""); 721 continue; 722 } 723 if (vflag) 724 fprintf(vfile, "%s linked to %s\n", 725 dblock.dbuf.name, dblock.dbuf.linkname); 726 continue; 727 } 728 if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) { 729 fprintf(stderr, "tar: can't create %s: ", 730 dblock.dbuf.name); 731 perror(""); 732 passtape(); 733 continue; 734 } 735 chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 736 blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK; 737 if (vflag) 738 fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n", 739 dblock.dbuf.name, bytes, blocks); 740 for (; blocks > 0;) { 741 register int nread; 742 char *bufp; 743 register int nwant; 744 745 nwant = NBLOCK*TBLOCK; 746 if (nwant > (blocks*TBLOCK)) 747 nwant = (blocks*TBLOCK); 748 nread = readtbuf(&bufp, nwant); 749 if (write(ofile, bufp, (int)min(nread, bytes)) < 0) { 750 fprintf(stderr, 751 "tar: %s: HELP - extract write error: ", 752 dblock.dbuf.name); 753 perror(""); 754 done(2); 755 } 756 bytes -= nread; 757 blocks -= (((nread-1)/TBLOCK)+1); 758 } 759 close(ofile); 760 if (mflag == 0) 761 setimes(dblock.dbuf.name, stbuf.st_mtime); 762 if (pflag) 763 chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 764 } 765 if (mflag == 0) { 766 dblock.dbuf.name[0] = '\0'; /* process the whole stack */ 767 dodirtimes(&dblock); 768 } 769 } 770 771 dotable(argv) 772 char *argv[]; 773 { 774 register int i; 775 776 for (;;) { 777 if ((i = wantit(argv)) == 0) 778 continue; 779 if (i == -1) 780 break; /* end of tape */ 781 if (vflag) 782 longt(&stbuf); 783 printf("%s", dblock.dbuf.name); 784 if (dblock.dbuf.linkflag == '1') 785 printf(" linked to %s", dblock.dbuf.linkname); 786 if (dblock.dbuf.linkflag == '2') 787 printf(" symbolic link to %s", dblock.dbuf.linkname); 788 printf("\n"); 789 passtape(); 790 } 791 } 792 793 putempty() 794 { 795 char buf[TBLOCK]; 796 797 bzero(buf, sizeof (buf)); 798 (void) writetape(buf); 799 } 800 801 longt(st) 802 register struct stat *st; 803 { 804 register char *cp; 805 char *ctime(); 806 807 pmode(st); 808 printf("%3d/%1d", st->st_uid, st->st_gid); 809 printf("%7ld", st->st_size); 810 cp = ctime(&st->st_mtime); 811 printf(" %-12.12s %-4.4s ", cp+4, cp+20); 812 } 813 814 #define SUID 04000 815 #define SGID 02000 816 #define ROWN 0400 817 #define WOWN 0200 818 #define XOWN 0100 819 #define RGRP 040 820 #define WGRP 020 821 #define XGRP 010 822 #define ROTH 04 823 #define WOTH 02 824 #define XOTH 01 825 #define STXT 01000 826 int m1[] = { 1, ROWN, 'r', '-' }; 827 int m2[] = { 1, WOWN, 'w', '-' }; 828 int m3[] = { 2, SUID, 's', XOWN, 'x', '-' }; 829 int m4[] = { 1, RGRP, 'r', '-' }; 830 int m5[] = { 1, WGRP, 'w', '-' }; 831 int m6[] = { 2, SGID, 's', XGRP, 'x', '-' }; 832 int m7[] = { 1, ROTH, 'r', '-' }; 833 int m8[] = { 1, WOTH, 'w', '-' }; 834 int m9[] = { 2, STXT, 't', XOTH, 'x', '-' }; 835 836 int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9}; 837 838 pmode(st) 839 register struct stat *st; 840 { 841 register int **mp; 842 843 for (mp = &m[0]; mp < &m[9];) 844 selectbits(*mp++, st); 845 } 846 847 selectbits(pairp, st) 848 int *pairp; 849 struct stat *st; 850 { 851 register int n, *ap; 852 853 ap = pairp; 854 n = *ap++; 855 while (--n>=0 && (st->st_mode&*ap++)==0) 856 ap++; 857 putchar(*ap); 858 } 859 860 /* 861 * Make all directories needed by `name'. If `name' is itself 862 * a directory on the tar tape (indicated by a trailing '/'), 863 * return 1; else 0. 864 */ 865 checkdir(name) 866 register char *name; 867 { 868 register char *cp; 869 870 /* 871 * Quick check for existence of directory. 872 */ 873 if ((cp = rindex(name, '/')) == 0) 874 return (0); 875 *cp = '\0'; 876 if (access(name, 0) == 0) { /* already exists */ 877 *cp = '/'; 878 return (cp[1] == '\0'); /* return (lastchar == '/') */ 879 } 880 *cp = '/'; 881 882 /* 883 * No luck, try to make all directories in path. 884 */ 885 for (cp = name; *cp; cp++) { 886 if (*cp != '/') 887 continue; 888 *cp = '\0'; 889 if (access(name, 0) < 0) { 890 if (mkdir(name, 0777) < 0) { 891 perror(name); 892 *cp = '/'; 893 return (0); 894 } 895 chown(name, stbuf.st_uid, stbuf.st_gid); 896 if (pflag && cp[1] == '\0') /* dir on the tape */ 897 chmod(name, stbuf.st_mode & 07777); 898 } 899 *cp = '/'; 900 } 901 return (cp[-1]=='/'); 902 } 903 904 onintr() 905 { 906 (void) signal(SIGINT, SIG_IGN); 907 term++; 908 } 909 910 onquit() 911 { 912 (void) signal(SIGQUIT, SIG_IGN); 913 term++; 914 } 915 916 onhup() 917 { 918 (void) signal(SIGHUP, SIG_IGN); 919 term++; 920 } 921 922 #ifdef notdef 923 onterm() 924 { 925 (void) signal(SIGTERM, SIG_IGN); 926 term++; 927 } 928 #endif 929 930 tomodes(sp) 931 register struct stat *sp; 932 { 933 register char *cp; 934 935 for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 936 *cp = '\0'; 937 sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777); 938 sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid); 939 sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid); 940 sprintf(dblock.dbuf.size, "%11lo ", sp->st_size); 941 sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime); 942 } 943 944 checksum() 945 { 946 register i; 947 register char *cp; 948 949 for (cp = dblock.dbuf.chksum; 950 cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++) 951 *cp = ' '; 952 i = 0; 953 for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 954 i += *cp; 955 return (i); 956 } 957 958 checkw(c, name) 959 char *name; 960 { 961 if (!wflag) 962 return (1); 963 printf("%c ", c); 964 if (vflag) 965 longt(&stbuf); 966 printf("%s: ", name); 967 return (response() == 'y'); 968 } 969 970 response() 971 { 972 char c; 973 974 c = getchar(); 975 if (c != '\n') 976 while (getchar() != '\n') 977 ; 978 else 979 c = 'n'; 980 return (c); 981 } 982 983 checkf(name, mode, howmuch) 984 char *name; 985 int mode, howmuch; 986 { 987 int l; 988 989 if ((mode & S_IFMT) == S_IFDIR){ 990 if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0)) 991 return(0); 992 return(1); 993 } 994 if ((l = strlen(name)) < 3) 995 return (1); 996 if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o') 997 return (0); 998 if (strcmp(name, "core") == 0 || 999 strcmp(name, "errs") == 0 || 1000 (howmuch > 1 && strcmp(name, "a.out") == 0)) 1001 return (0); 1002 /* SHOULD CHECK IF IT IS EXECUTABLE */ 1003 return (1); 1004 } 1005 1006 /* Is the current file a new file, or the newest one of the same name? */ 1007 checkupdate(arg) 1008 char *arg; 1009 { 1010 char name[100]; 1011 long mtime; 1012 daddr_t seekp; 1013 daddr_t lookup(); 1014 1015 rewind(tfile); 1016 for (;;) { 1017 if ((seekp = lookup(arg)) < 0) 1018 return (1); 1019 fseek(tfile, seekp, 0); 1020 fscanf(tfile, "%s %lo", name, &mtime); 1021 return (stbuf.st_mtime > mtime); 1022 } 1023 } 1024 1025 done(n) 1026 { 1027 unlink(tname); 1028 exit(n); 1029 } 1030 1031 /* 1032 * Do we want the next entry on the tape, i.e. is it selected? If 1033 * not, skip over the entire entry. Return -1 if reached end of tape. 1034 */ 1035 wantit(argv) 1036 char *argv[]; 1037 { 1038 register char **cp; 1039 1040 getdir(); 1041 if (endtape()) 1042 return (-1); 1043 if (*argv == 0) 1044 return (1); 1045 for (cp = argv; *cp; cp++) 1046 if (prefix(*cp, dblock.dbuf.name)) 1047 return (1); 1048 passtape(); 1049 return (0); 1050 } 1051 1052 /* 1053 * Does s2 begin with the string s1, on a directory boundary? 1054 */ 1055 prefix(s1, s2) 1056 register char *s1, *s2; 1057 { 1058 while (*s1) 1059 if (*s1++ != *s2++) 1060 return (0); 1061 if (*s2) 1062 return (*s2 == '/'); 1063 return (1); 1064 } 1065 1066 #define N 200 1067 int njab; 1068 1069 daddr_t 1070 lookup(s) 1071 char *s; 1072 { 1073 register i; 1074 daddr_t a; 1075 1076 for(i=0; s[i]; i++) 1077 if (s[i] == ' ') 1078 break; 1079 a = bsrch(s, i, low, high); 1080 return (a); 1081 } 1082 1083 daddr_t 1084 bsrch(s, n, l, h) 1085 daddr_t l, h; 1086 char *s; 1087 { 1088 register i, j; 1089 char b[N]; 1090 daddr_t m, m1; 1091 1092 njab = 0; 1093 1094 loop: 1095 if (l >= h) 1096 return ((daddr_t) -1); 1097 m = l + (h-l)/2 - N/2; 1098 if (m < l) 1099 m = l; 1100 fseek(tfile, m, 0); 1101 fread(b, 1, N, tfile); 1102 njab++; 1103 for(i=0; i<N; i++) { 1104 if (b[i] == '\n') 1105 break; 1106 m++; 1107 } 1108 if (m >= h) 1109 return ((daddr_t) -1); 1110 m1 = m; 1111 j = i; 1112 for(i++; i<N; i++) { 1113 m1++; 1114 if (b[i] == '\n') 1115 break; 1116 } 1117 i = cmp(b+j, s, n); 1118 if (i < 0) { 1119 h = m; 1120 goto loop; 1121 } 1122 if (i > 0) { 1123 l = m1; 1124 goto loop; 1125 } 1126 return (m); 1127 } 1128 1129 cmp(b, s, n) 1130 char *b, *s; 1131 { 1132 register i; 1133 1134 if (b[0] != '\n') 1135 exit(2); 1136 for(i=0; i<n; i++) { 1137 if (b[i+1] > s[i]) 1138 return (-1); 1139 if (b[i+1] < s[i]) 1140 return (1); 1141 } 1142 return (b[i+1] == ' '? 0 : -1); 1143 } 1144 1145 readtape(buffer) 1146 char *buffer; 1147 { 1148 char *bufp; 1149 1150 if (first == 0) 1151 getbuf(); 1152 (void) readtbuf(&bufp, TBLOCK); 1153 bcopy(bufp, buffer, TBLOCK); 1154 return(TBLOCK); 1155 } 1156 1157 readtbuf(bufpp, size) 1158 char **bufpp; 1159 int size; 1160 { 1161 register int i; 1162 1163 if (recno >= nblock || first == 0) { 1164 if ((i = bread(mt, (char *)tbuf, TBLOCK*nblock)) < 0) 1165 mterr("read", i, 3); 1166 if (first == 0) { 1167 if ((i % TBLOCK) != 0) { 1168 fprintf(stderr, "tar: tape blocksize error\n"); 1169 done(3); 1170 } 1171 i /= TBLOCK; 1172 if (i != nblock) { 1173 fprintf(stderr, "tar: blocksize = %d\n", i); 1174 nblock = i; 1175 } 1176 first = 1; 1177 } 1178 recno = 0; 1179 } 1180 if (size > ((nblock-recno)*TBLOCK)) 1181 size = (nblock-recno)*TBLOCK; 1182 *bufpp = (char *)&tbuf[recno]; 1183 recno += (size/TBLOCK); 1184 return (size); 1185 } 1186 1187 writetbuf(buffer, n) 1188 register char *buffer; 1189 register int n; 1190 { 1191 int i; 1192 1193 if (first == 0) { 1194 getbuf(); 1195 first = 1; 1196 } 1197 if (recno >= nblock) { 1198 i = write(mt, (char *)tbuf, TBLOCK*nblock); 1199 if (i != TBLOCK*nblock) 1200 mterr("write", i, 2); 1201 recno = 0; 1202 } 1203 1204 /* 1205 * Special case: We have an empty tape buffer, and the 1206 * users data size is >= the tape block size: Avoid 1207 * the bcopy and dma direct to tape. BIG WIN. Add the 1208 * residual to the tape buffer. 1209 */ 1210 while (recno == 0 && n >= nblock) { 1211 i = write(mt, buffer, TBLOCK*nblock); 1212 if (i != TBLOCK*nblock) 1213 mterr("write", i, 2); 1214 n -= nblock; 1215 buffer += (nblock * TBLOCK); 1216 } 1217 1218 while (n-- > 0) { 1219 bcopy(buffer, (char *)&tbuf[recno++], TBLOCK); 1220 buffer += TBLOCK; 1221 if (recno >= nblock) { 1222 i = write(mt, (char *)tbuf, TBLOCK*nblock); 1223 if (i != TBLOCK*nblock) 1224 mterr("write", i, 2); 1225 recno = 0; 1226 } 1227 } 1228 1229 /* Tell the user how much to write to get in sync */ 1230 return (nblock - recno); 1231 } 1232 1233 backtape() 1234 { 1235 static int mtdev = 1; 1236 static struct mtop mtop = {MTBSR, 1}; 1237 struct mtget mtget; 1238 1239 if (mtdev == 1) 1240 mtdev = ioctl(mt, MTIOCGET, (char *)&mtget); 1241 if (mtdev == 0) { 1242 if (ioctl(mt, MTIOCTOP, (char *)&mtop) < 0) { 1243 fprintf(stderr, "tar: tape backspace error: "); 1244 perror(""); 1245 done(4); 1246 } 1247 } else 1248 lseek(mt, (daddr_t) -TBLOCK*nblock, 1); 1249 recno--; 1250 } 1251 1252 flushtape() 1253 { 1254 int i; 1255 1256 i = write(mt, (char *)tbuf, TBLOCK*nblock); 1257 if (i != TBLOCK*nblock) 1258 mterr("write", i, 2); 1259 } 1260 1261 mterr(operation, i, exitcode) 1262 char *operation; 1263 int i; 1264 { 1265 fprintf(stderr, "tar: tape %s error: ", operation); 1266 if (i < 0) 1267 perror(""); 1268 else 1269 fprintf(stderr, "unexpected EOF\n"); 1270 done(exitcode); 1271 } 1272 1273 bread(fd, buf, size) 1274 int fd; 1275 char *buf; 1276 int size; 1277 { 1278 int count; 1279 static int lastread = 0; 1280 1281 if (!Bflag) 1282 return (read(fd, buf, size)); 1283 1284 for (count = 0; count < size; count += lastread) { 1285 lastread = read(fd, buf, size - count); 1286 if (lastread <= 0) { 1287 if (count > 0) 1288 return (count); 1289 return (lastread); 1290 } 1291 buf += lastread; 1292 } 1293 return (count); 1294 } 1295 1296 char * 1297 getcwd(buf) 1298 char *buf; 1299 { 1300 if (getwd(buf) == NULL) { 1301 fprintf(stderr, "tar: %s\n", buf); 1302 exit(1); 1303 } 1304 return (buf); 1305 } 1306 1307 getbuf() 1308 { 1309 1310 if (nblock == 0) { 1311 fstat(mt, &stbuf); 1312 if ((stbuf.st_mode & S_IFMT) == S_IFCHR) 1313 nblock = NBLOCK; 1314 else { 1315 nblock = stbuf.st_blksize / TBLOCK; 1316 if (nblock == 0) 1317 nblock = NBLOCK; 1318 } 1319 } 1320 tbuf = (union hblock *)malloc((unsigned)nblock*TBLOCK); 1321 if (tbuf == NULL) { 1322 fprintf(stderr, "tar: blocksize %d too big, can't get memory\n", 1323 nblock); 1324 done(1); 1325 } 1326 } 1327 1328 /* 1329 * Save this directory and its mtime on the stack, popping and setting 1330 * the mtimes of any stacked dirs which aren't parents of this one. 1331 * A null directory causes the entire stack to be unwound and set. 1332 * 1333 * Since all the elements of the directory "stack" share a common 1334 * prefix, we can make do with one string. We keep only the current 1335 * directory path, with an associated array of mtime's, one for each 1336 * '/' in the path. A negative mtime means no mtime. The mtime's are 1337 * offset by one (first index 1, not 0) because calling this with a null 1338 * directory causes mtime[0] to be set. 1339 * 1340 * This stack algorithm is not guaranteed to work for tapes created 1341 * with the 'r' option, but the vast majority of tapes with 1342 * directories are not. This avoids saving every directory record on 1343 * the tape and setting all the times at the end. 1344 */ 1345 char dirstack[NAMSIZ]; 1346 #define NTIM (NAMSIZ/2+1) /* a/b/c/d/... */ 1347 time_t mtime[NTIM]; 1348 1349 dodirtimes(hp) 1350 union hblock *hp; 1351 { 1352 register char *p = dirstack; 1353 register char *q = hp->dbuf.name; 1354 register int ndir = 0; 1355 char *savp; 1356 int savndir; 1357 1358 /* Find common prefix */ 1359 while (*p == *q && *p) { 1360 if (*p++ == '/') 1361 ++ndir; 1362 q++; 1363 } 1364 1365 savp = p; 1366 savndir = ndir; 1367 while (*p) { 1368 /* 1369 * Not a child: unwind the stack, setting the times. 1370 * The order we do this doesn't matter, so we go "forward." 1371 */ 1372 if (*p++ == '/') 1373 if (mtime[++ndir] >= 0) { 1374 *--p = '\0'; /* zap the slash */ 1375 setimes(dirstack, mtime[ndir]); 1376 *p++ = '/'; 1377 } 1378 } 1379 p = savp; 1380 ndir = savndir; 1381 1382 /* Push this one on the "stack" */ 1383 while (*p = *q++) /* append the rest of the new dir */ 1384 if (*p++ == '/') 1385 mtime[++ndir] = -1; 1386 mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */ 1387 } 1388 1389 setimes(path, mt) 1390 char *path; 1391 time_t mt; 1392 { 1393 struct timeval tv[2]; 1394 1395 tv[0].tv_sec = time((time_t *) 0); 1396 tv[1].tv_sec = mt; 1397 tv[0].tv_usec = tv[1].tv_usec = 0; 1398 if (utimes(path, tv) < 0) { 1399 fprintf(stderr, "tar: can't set time on %s: ", path); 1400 perror(""); 1401 } 1402 } 1403 1404 char * 1405 getmem(size) 1406 { 1407 char *p = malloc((unsigned) size); 1408 1409 if (p == NULL && freemem) { 1410 fprintf(stderr, 1411 "tar: out of memory, link and directory modtime info lost\n"); 1412 freemem = 0; 1413 } 1414 return (p); 1415 } 1416