122502Sdist /* 222502Sdist * Copyright (c) 1980 Regents of the University of California. 322502Sdist * All rights reserved. The Berkeley software License Agreement 422502Sdist * specifies the terms and conditions for redistribution. 522502Sdist */ 622502Sdist 712154Ssam #ifndef lint 822502Sdist char copyright[] = 922502Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1022502Sdist All rights reserved.\n"; 1122502Sdist #endif not lint 126250Sroot 1322502Sdist #ifndef lint 14*27442Slepreau static char sccsid[] = "@(#)tar.c 5.6 (Berkeley) 04/26/86"; 1522502Sdist #endif not lint 1622502Sdist 176250Sroot /* 186250Sroot * Tape Archival Program 196250Sroot */ 201119Sbill #include <stdio.h> 216413Smckusic #include <sys/param.h> 221119Sbill #include <sys/stat.h> 2312154Ssam #include <sys/dir.h> 248150Smckusick #include <sys/ioctl.h> 253457Swnj #include <sys/mtio.h> 2612983Ssam #include <sys/time.h> 271119Sbill #include <signal.h> 2812154Ssam #include <errno.h> 2927058Smckusick #include <fcntl.h> 301119Sbill 311119Sbill #define TBLOCK 512 323355Swnj #define NBLOCK 20 331119Sbill #define NAMSIZ 100 346250Sroot 3521457Skjd #define writetape(b) writetbuf(b, 1) 3621457Skjd #define min(a,b) ((a) < (b) ? (a) : (b)) 3721457Skjd #define max(a,b) ((a) > (b) ? (a) : (b)) 3821457Skjd 391119Sbill union hblock { 401119Sbill char dummy[TBLOCK]; 411119Sbill struct header { 421119Sbill char name[NAMSIZ]; 431119Sbill char mode[8]; 441119Sbill char uid[8]; 451119Sbill char gid[8]; 461119Sbill char size[12]; 471119Sbill char mtime[12]; 481119Sbill char chksum[8]; 491119Sbill char linkflag; 501119Sbill char linkname[NAMSIZ]; 511119Sbill } dbuf; 526250Sroot }; 531119Sbill 541119Sbill struct linkbuf { 551119Sbill ino_t inum; 561119Sbill dev_t devnum; 571119Sbill int count; 581119Sbill char pathname[NAMSIZ]; 591119Sbill struct linkbuf *nextp; 606250Sroot }; 611119Sbill 626250Sroot union hblock dblock; 6313492Ssam union hblock *tbuf; 646250Sroot struct linkbuf *ihead; 656250Sroot struct stat stbuf; 661119Sbill 676250Sroot int rflag; 686250Sroot int xflag; 696250Sroot int vflag; 706250Sroot int tflag; 716250Sroot int cflag; 726250Sroot int mflag; 736250Sroot int fflag; 7412154Ssam int iflag; 756250Sroot int oflag; 766250Sroot int pflag; 776250Sroot int wflag; 786250Sroot int hflag; 798737Smckusick int Bflag; 8012154Ssam int Fflag; 816250Sroot 826250Sroot int mt; 836250Sroot int term; 846250Sroot int chksum; 856250Sroot int recno; 8622688Slepreau int first; 8722688Slepreau int prtlinkerr; 881119Sbill int freemem = 1; 8922353Skjd int nblock = 0; 906250Sroot int onintr(); 916250Sroot int onquit(); 926250Sroot int onhup(); 9322688Slepreau #ifdef notdef 946250Sroot int onterm(); 9522688Slepreau #endif 961119Sbill 971119Sbill daddr_t low; 981119Sbill daddr_t high; 996250Sroot daddr_t bsrch(); 1001119Sbill 10121910Skjd FILE *vfile = stdout; 1021119Sbill FILE *tfile; 1031119Sbill char tname[] = "/tmp/tarXXXXXX"; 1041119Sbill char *usefile; 1056250Sroot char magtape[] = "/dev/rmt8"; 1061119Sbill char *malloc(); 1076250Sroot char *sprintf(); 1086250Sroot char *strcat(); 10927058Smckusick char *strcpy(); 11012154Ssam char *rindex(); 11110165Ssam char *getcwd(); 1129844Ssam char *getwd(); 11322688Slepreau char *getmem(); 1141119Sbill 1151119Sbill main(argc, argv) 1161119Sbill int argc; 1171119Sbill char *argv[]; 1181119Sbill { 1191119Sbill char *cp; 1201119Sbill 1211119Sbill if (argc < 2) 1221119Sbill usage(); 1231119Sbill 1241119Sbill tfile = NULL; 1251119Sbill usefile = magtape; 1261119Sbill argv[argc] = 0; 1271119Sbill argv++; 1281119Sbill for (cp = *argv++; *cp; cp++) 1291119Sbill switch(*cp) { 1306250Sroot 1311119Sbill case 'f': 13212154Ssam if (*argv == 0) { 13312154Ssam fprintf(stderr, 13412154Ssam "tar: tapefile must be specified with 'f' option\n"); 13512154Ssam usage(); 13612154Ssam } 1371119Sbill usefile = *argv++; 1381119Sbill fflag++; 1391119Sbill break; 1406250Sroot 1411119Sbill case 'c': 1421119Sbill cflag++; 1431119Sbill rflag++; 1441119Sbill break; 1456250Sroot 1461119Sbill case 'o': 1471119Sbill oflag++; 1481119Sbill break; 1496250Sroot 1501119Sbill case 'p': 1511119Sbill pflag++; 1521119Sbill break; 1536250Sroot 1541119Sbill case 'u': 1551119Sbill mktemp(tname); 1561119Sbill if ((tfile = fopen(tname, "w")) == NULL) { 1576250Sroot fprintf(stderr, 15822688Slepreau "tar: cannot create temporary file (%s)\n", 1596250Sroot tname); 1601119Sbill done(1); 1611119Sbill } 1621119Sbill fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n"); 1636250Sroot /*FALL THRU*/ 1646250Sroot 1651119Sbill case 'r': 1661119Sbill rflag++; 1671119Sbill break; 1686250Sroot 1691119Sbill case 'v': 1701119Sbill vflag++; 1711119Sbill break; 1726250Sroot 1731119Sbill case 'w': 1741119Sbill wflag++; 1751119Sbill break; 1766250Sroot 1771119Sbill case 'x': 1781119Sbill xflag++; 1791119Sbill break; 1806250Sroot 1811119Sbill case 't': 1821119Sbill tflag++; 1831119Sbill break; 1846250Sroot 1851119Sbill case 'm': 1861119Sbill mflag++; 1871119Sbill break; 1886250Sroot 1891119Sbill case '-': 1901119Sbill break; 1916250Sroot 1921119Sbill case '0': 1931119Sbill case '1': 1941119Sbill case '4': 1951119Sbill case '5': 19621457Skjd case '7': 1971119Sbill case '8': 1981119Sbill magtape[8] = *cp; 1991119Sbill usefile = magtape; 2001119Sbill break; 2016250Sroot 2021119Sbill case 'b': 20313492Ssam if (*argv == 0) { 20413492Ssam fprintf(stderr, 20513492Ssam "tar: blocksize must be specified with 'b' option\n"); 20613492Ssam usage(); 20713492Ssam } 20813492Ssam nblock = atoi(*argv); 20913492Ssam if (nblock <= 0) { 21013492Ssam fprintf(stderr, 21113492Ssam "tar: invalid blocksize \"%s\"\n", *argv); 2121119Sbill done(1); 2131119Sbill } 21413492Ssam argv++; 2151119Sbill break; 2166250Sroot 2171119Sbill case 'l': 21822688Slepreau prtlinkerr++; 2191119Sbill break; 2206250Sroot 2216250Sroot case 'h': 2226250Sroot hflag++; 2236250Sroot break; 2246250Sroot 22512154Ssam case 'i': 22612154Ssam iflag++; 22712154Ssam break; 22812154Ssam 2298737Smckusick case 'B': 2308737Smckusick Bflag++; 2318737Smckusick break; 2328737Smckusick 23312154Ssam case 'F': 23412154Ssam Fflag++; 23512154Ssam break; 23612154Ssam 2371119Sbill default: 2381119Sbill fprintf(stderr, "tar: %c: unknown option\n", *cp); 2391119Sbill usage(); 2401119Sbill } 2411119Sbill 2426250Sroot if (!rflag && !xflag && !tflag) 2436250Sroot usage(); 2441119Sbill if (rflag) { 2456250Sroot if (cflag && tfile != NULL) 2461119Sbill usage(); 2471119Sbill if (signal(SIGINT, SIG_IGN) != SIG_IGN) 2481119Sbill signal(SIGINT, onintr); 2491119Sbill if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 2501119Sbill signal(SIGHUP, onhup); 2511119Sbill if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) 2521119Sbill signal(SIGQUIT, onquit); 2536250Sroot #ifdef notdef 2541119Sbill if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 2551119Sbill signal(SIGTERM, onterm); 2566250Sroot #endif 25727058Smckusick mt = openmt(usefile, 1); 2581119Sbill dorep(argv); 2596250Sroot done(0); 2601119Sbill } 26127058Smckusick mt = openmt(usefile, 0); 2626250Sroot if (xflag) 2631119Sbill doxtract(argv); 2646250Sroot else 2651119Sbill dotable(); 2661119Sbill done(0); 2671119Sbill } 2681119Sbill 2691119Sbill usage() 2701119Sbill { 2716250Sroot fprintf(stderr, 27221457Skjd "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n"); 2731119Sbill done(1); 2741119Sbill } 2751119Sbill 27627058Smckusick int 27727058Smckusick openmt(tape, writing) 27827058Smckusick char *tape; 27927058Smckusick int writing; 28027058Smckusick { 28127058Smckusick register char *rmtape; 28227058Smckusick extern char *rmterr; 28327058Smckusick 28427058Smckusick if (strcmp(tape, "-") == 0) { 28527058Smckusick /* 28627058Smckusick * Read from standard input or write to standard output. 28727058Smckusick */ 28827058Smckusick if (writing) { 28927058Smckusick if (cflag == 0) { 29027058Smckusick fprintf(stderr, 29127058Smckusick "tar: can only create standard output archives\n"); 29227058Smckusick done(1); 29327058Smckusick } 29427058Smckusick vfile = stderr; 29527058Smckusick setlinebuf(vfile); 29627058Smckusick mt = dup(1); 29727058Smckusick } else { 29827058Smckusick mt = dup(0); 29927058Smckusick Bflag++; 30027058Smckusick } 30127058Smckusick } else { 30227058Smckusick /* 30327058Smckusick * Use file or tape on local machine. 30427058Smckusick */ 30527058Smckusick if (writing) { 30627058Smckusick if (cflag) 30727058Smckusick mt = open(tape, O_RDWR|O_CREAT|O_TRUNC, 30827058Smckusick 0666); 30927058Smckusick else 31027058Smckusick mt = open(tape, O_RDWR); 31127058Smckusick } else 31227058Smckusick mt = open(tape, O_RDONLY); 31327058Smckusick if (mt < 0) { 31427058Smckusick fprintf(stderr, "tar: "); 31527058Smckusick perror(tape); 31627058Smckusick done(1); 31727058Smckusick } 31827058Smckusick } 31927058Smckusick return(mt); 32027058Smckusick } 32127058Smckusick 3221119Sbill dorep(argv) 3236250Sroot char *argv[]; 3241119Sbill { 3251119Sbill register char *cp, *cp2; 3269601Ssam char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent; 3271119Sbill 3281119Sbill if (!cflag) { 3291119Sbill getdir(); 3301119Sbill do { 3311119Sbill passtape(); 3321119Sbill if (term) 3331119Sbill done(0); 3341119Sbill getdir(); 3351119Sbill } while (!endtape()); 33613492Ssam backtape(); 3371119Sbill if (tfile != NULL) { 3381119Sbill char buf[200]; 3391119Sbill 3406250Sroot sprintf(buf, 3416250Sroot "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s", 3421119Sbill tname, tname, tname, tname, tname, tname); 3431119Sbill fflush(tfile); 3441119Sbill system(buf); 3451119Sbill freopen(tname, "r", tfile); 3461119Sbill fstat(fileno(tfile), &stbuf); 3471119Sbill high = stbuf.st_size; 3481119Sbill } 3491119Sbill } 3501119Sbill 35110165Ssam (void) getcwd(wdir); 3521119Sbill while (*argv && ! term) { 3531119Sbill cp2 = *argv; 3541119Sbill if (!strcmp(cp2, "-C") && argv[1]) { 3551119Sbill argv++; 35627058Smckusick if (chdir(*argv) < 0) { 35727058Smckusick fprintf(stderr, "tar: can't change directories to "); 3581119Sbill perror(*argv); 35927058Smckusick } else 36010165Ssam (void) getcwd(wdir); 3611119Sbill argv++; 3621119Sbill continue; 3631119Sbill } 3649601Ssam parent = wdir; 3651119Sbill for (cp = *argv; *cp; cp++) 3661119Sbill if (*cp == '/') 3671119Sbill cp2 = cp; 3681119Sbill if (cp2 != *argv) { 3691119Sbill *cp2 = '\0'; 3709601Ssam if (chdir(*argv) < 0) { 37127058Smckusick fprintf(stderr, "tar: can't change directories to "); 3729601Ssam perror(*argv); 3739601Ssam continue; 3749601Ssam } 37510165Ssam parent = getcwd(tempdir); 3761119Sbill *cp2 = '/'; 3771119Sbill cp2++; 3781119Sbill } 3799601Ssam putfile(*argv++, cp2, parent); 38015045Smckusick if (chdir(wdir) < 0) { 38122688Slepreau fprintf(stderr, "tar: cannot change back?: "); 38215045Smckusick perror(wdir); 38315045Smckusick } 3841119Sbill } 3851119Sbill putempty(); 3861119Sbill putempty(); 3871119Sbill flushtape(); 38822688Slepreau if (prtlinkerr == 0) 3896250Sroot return; 3906250Sroot for (; ihead != NULL; ihead = ihead->nextp) { 3916250Sroot if (ihead->count == 0) 3926250Sroot continue; 39313492Ssam fprintf(stderr, "tar: missing links to %s\n", ihead->pathname); 3946250Sroot } 3951119Sbill } 3961119Sbill 3971119Sbill endtape() 3981119Sbill { 39921457Skjd return (dblock.dbuf.name[0] == '\0'); 4001119Sbill } 4011119Sbill 4021119Sbill getdir() 4031119Sbill { 4041119Sbill register struct stat *sp; 4051119Sbill int i; 4061119Sbill 40712154Ssam top: 4086250Sroot readtape((char *)&dblock); 4091119Sbill if (dblock.dbuf.name[0] == '\0') 4101119Sbill return; 4111119Sbill sp = &stbuf; 4121119Sbill sscanf(dblock.dbuf.mode, "%o", &i); 4131119Sbill sp->st_mode = i; 4141119Sbill sscanf(dblock.dbuf.uid, "%o", &i); 4151119Sbill sp->st_uid = i; 4161119Sbill sscanf(dblock.dbuf.gid, "%o", &i); 4171119Sbill sp->st_gid = i; 4181119Sbill sscanf(dblock.dbuf.size, "%lo", &sp->st_size); 4191119Sbill sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime); 4201119Sbill sscanf(dblock.dbuf.chksum, "%o", &chksum); 42112154Ssam if (chksum != (i = checksum())) { 42213492Ssam fprintf(stderr, "tar: directory checksum error (%d != %d)\n", 42312154Ssam chksum, i); 42412154Ssam if (iflag) 42512154Ssam goto top; 4261119Sbill done(2); 4271119Sbill } 4281119Sbill if (tfile != NULL) 4291119Sbill fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime); 4301119Sbill } 4311119Sbill 4321119Sbill passtape() 4331119Sbill { 4341119Sbill long blocks; 43521457Skjd char *bufp; 4361119Sbill 4371119Sbill if (dblock.dbuf.linkflag == '1') 4381119Sbill return; 4391119Sbill blocks = stbuf.st_size; 4401119Sbill blocks += TBLOCK-1; 4411119Sbill blocks /= TBLOCK; 4421119Sbill 44321457Skjd while (blocks-- > 0) 44421457Skjd readtbuf(&bufp, TBLOCK); 4451119Sbill } 4461119Sbill 4479601Ssam putfile(longname, shortname, parent) 4486250Sroot char *longname; 4496250Sroot char *shortname; 4509601Ssam char *parent; 4511119Sbill { 45221457Skjd int infile = 0; 45321457Skjd long blocks; 4541119Sbill char buf[TBLOCK]; 45521457Skjd char *bigbuf; 45622688Slepreau register char *cp; 4575931Smckusic struct direct *dp; 4585931Smckusic DIR *dirp; 45922688Slepreau int i; 4609601Ssam char newparent[NAMSIZ+64]; 46112154Ssam extern int errno; 46221457Skjd int maxread; 46321457Skjd int hint; /* amount to write to get "in sync" */ 4641119Sbill 4659601Ssam if (!hflag) 46612154Ssam i = lstat(shortname, &stbuf); 46712154Ssam else 46812154Ssam i = stat(shortname, &stbuf); 46912154Ssam if (i < 0) { 47027058Smckusick fprintf(stderr, "tar: "); 47127058Smckusick perror(longname); 4729601Ssam return; 4739601Ssam } 47412154Ssam if (tfile != NULL && checkupdate(longname) == 0) 4751119Sbill return; 47612154Ssam if (checkw('r', longname) == 0) 4771119Sbill return; 47812154Ssam if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0) 47912154Ssam return; 4801119Sbill 48112154Ssam switch (stbuf.st_mode & S_IFMT) { 48212154Ssam case S_IFDIR: 4836250Sroot for (i = 0, cp = buf; *cp++ = longname[i++];) 4846250Sroot ; 4851119Sbill *--cp = '/'; 4861119Sbill *++cp = 0 ; 4871119Sbill if (!oflag) { 4886250Sroot if ((cp - buf) >= NAMSIZ) { 48913492Ssam fprintf(stderr, "tar: %s: file name too long\n", 49013492Ssam longname); 4916250Sroot return; 4926250Sroot } 4936250Sroot stbuf.st_size = 0; 4946250Sroot tomodes(&stbuf); 4956250Sroot strcpy(dblock.dbuf.name,buf); 4966250Sroot sprintf(dblock.dbuf.chksum, "%6o", checksum()); 4976250Sroot writetape((char *)&dblock); 4981119Sbill } 4999601Ssam sprintf(newparent, "%s/%s", parent, shortname); 50015045Smckusick if (chdir(shortname) < 0) { 50115045Smckusick perror(shortname); 50215045Smckusick return; 50315045Smckusick } 5045931Smckusic if ((dirp = opendir(".")) == NULL) { 50513492Ssam fprintf(stderr, "tar: %s: directory read error\n", 50613492Ssam longname); 50715045Smckusick if (chdir(parent) < 0) { 50822688Slepreau fprintf(stderr, "tar: cannot change back?: "); 50915045Smckusick perror(parent); 51015045Smckusick } 5115931Smckusic return; 5125931Smckusic } 5135931Smckusic while ((dp = readdir(dirp)) != NULL && !term) { 5145931Smckusic if (dp->d_ino == 0) 5151119Sbill continue; 5166250Sroot if (!strcmp(".", dp->d_name) || 5176250Sroot !strcmp("..", dp->d_name)) 5181119Sbill continue; 5195931Smckusic strcpy(cp, dp->d_name); 5205931Smckusic i = telldir(dirp); 5215931Smckusic closedir(dirp); 5229601Ssam putfile(buf, cp, newparent); 5235931Smckusic dirp = opendir("."); 5245931Smckusic seekdir(dirp, i); 5251119Sbill } 5265931Smckusic closedir(dirp); 52715045Smckusick if (chdir(parent) < 0) { 52822688Slepreau fprintf(stderr, "tar: cannot change back?: "); 52915045Smckusick perror(parent); 53015045Smckusick } 53112154Ssam break; 53212154Ssam 53312154Ssam case S_IFLNK: 53412154Ssam tomodes(&stbuf); 53512154Ssam if (strlen(longname) >= NAMSIZ) { 53613492Ssam fprintf(stderr, "tar: %s: file name too long\n", 53713492Ssam longname); 53812154Ssam return; 53912154Ssam } 54012154Ssam strcpy(dblock.dbuf.name, longname); 5416250Sroot if (stbuf.st_size + 1 >= NAMSIZ) { 54213492Ssam fprintf(stderr, "tar: %s: symbolic link too long\n", 54313492Ssam longname); 5446250Sroot return; 5456250Sroot } 5469601Ssam i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1); 5476250Sroot if (i < 0) { 54827058Smckusick fprintf(stderr, "tar: can't read symbolic link "); 5499601Ssam perror(longname); 5506250Sroot return; 5516250Sroot } 5526250Sroot dblock.dbuf.linkname[i] = '\0'; 5536250Sroot dblock.dbuf.linkflag = '2'; 55422688Slepreau if (vflag) 55522688Slepreau fprintf(vfile, "a %s symbolic link to %s\n", 55622688Slepreau longname, dblock.dbuf.linkname); 5576250Sroot sprintf(dblock.dbuf.size, "%11lo", 0); 5586250Sroot sprintf(dblock.dbuf.chksum, "%6o", checksum()); 5596250Sroot writetape((char *)&dblock); 56012154Ssam break; 5611119Sbill 56212154Ssam case S_IFREG: 56312154Ssam if ((infile = open(shortname, 0)) < 0) { 56427058Smckusick fprintf(stderr, "tar: "); 56527058Smckusick perror(longname); 5661119Sbill return; 5671119Sbill } 56812154Ssam tomodes(&stbuf); 56912154Ssam if (strlen(longname) >= NAMSIZ) { 57013492Ssam fprintf(stderr, "tar: %s: file name too long\n", 57113492Ssam longname); 57221457Skjd close(infile); 57312154Ssam return; 57412154Ssam } 57512154Ssam strcpy(dblock.dbuf.name, longname); 57612154Ssam if (stbuf.st_nlink > 1) { 57712154Ssam struct linkbuf *lp; 57812154Ssam int found = 0; 57912154Ssam 58012154Ssam for (lp = ihead; lp != NULL; lp = lp->nextp) 58112154Ssam if (lp->inum == stbuf.st_ino && 58212154Ssam lp->devnum == stbuf.st_dev) { 58312154Ssam found++; 58412154Ssam break; 58512154Ssam } 58612154Ssam if (found) { 58712154Ssam strcpy(dblock.dbuf.linkname, lp->pathname); 58812154Ssam dblock.dbuf.linkflag = '1'; 58912154Ssam sprintf(dblock.dbuf.chksum, "%6o", checksum()); 59021457Skjd writetape( (char *) &dblock); 59122688Slepreau if (vflag) 59222688Slepreau fprintf(vfile, "a %s link to %s\n", 59322688Slepreau longname, lp->pathname); 59412154Ssam lp->count--; 59512154Ssam close(infile); 59612154Ssam return; 5971119Sbill } 59822688Slepreau lp = (struct linkbuf *) getmem(sizeof(*lp)); 59922688Slepreau if (lp != NULL) { 60012154Ssam lp->nextp = ihead; 60112154Ssam ihead = lp; 60212154Ssam lp->inum = stbuf.st_ino; 60312154Ssam lp->devnum = stbuf.st_dev; 60412154Ssam lp->count = stbuf.st_nlink - 1; 60512154Ssam strcpy(lp->pathname, longname); 60612154Ssam } 6071119Sbill } 60821457Skjd blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK; 60922688Slepreau if (vflag) 61022688Slepreau fprintf(vfile, "a %s %ld blocks\n", longname, blocks); 61112154Ssam sprintf(dblock.dbuf.chksum, "%6o", checksum()); 61221457Skjd hint = writetape((char *)&dblock); 61321457Skjd maxread = max(stbuf.st_blksize, (nblock * TBLOCK)); 61421457Skjd if ((bigbuf = malloc(maxread)) == 0) { 61521457Skjd maxread = TBLOCK; 61621457Skjd bigbuf = buf; 61721457Skjd } 6181119Sbill 61921457Skjd while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0 62021457Skjd && blocks > 0) { 62121457Skjd register int nblks; 62221457Skjd 62321457Skjd nblks = ((i-1)/TBLOCK)+1; 62421457Skjd if (nblks > blocks) 62521457Skjd nblks = blocks; 62621457Skjd hint = writetbuf(bigbuf, nblks); 62721457Skjd blocks -= nblks; 62821457Skjd } 62921457Skjd close(infile); 63021457Skjd if (bigbuf != buf) 63121457Skjd free(bigbuf); 63227058Smckusick if (i < 0) { 63327058Smckusick fprintf("tar: Read error on "); 63427058Smckusick perror(longname); 63527058Smckusick } else if (blocks != 0 || i != 0) 63613492Ssam fprintf(stderr, "tar: %s: file changed size\n", 63713492Ssam longname); 63812154Ssam while (--blocks >= 0) 63912154Ssam putempty(); 64012154Ssam break; 64112154Ssam 64212154Ssam default: 64312154Ssam fprintf(stderr, "tar: %s is not a file. Not dumped\n", 64413492Ssam longname); 64512154Ssam break; 6461119Sbill } 6471119Sbill } 6481119Sbill 6491119Sbill doxtract(argv) 6506250Sroot char *argv[]; 6511119Sbill { 6521119Sbill long blocks, bytes; 6531119Sbill char **cp; 6541119Sbill int ofile; 6551119Sbill 6561119Sbill for (;;) { 6571119Sbill getdir(); 6581119Sbill if (endtape()) 6591119Sbill break; 6601119Sbill if (*argv == 0) 6611119Sbill goto gotit; 6621119Sbill for (cp = argv; *cp; cp++) 6631119Sbill if (prefix(*cp, dblock.dbuf.name)) 6641119Sbill goto gotit; 6651119Sbill passtape(); 6661119Sbill continue; 6671119Sbill 6681119Sbill gotit: 6691119Sbill if (checkw('x', dblock.dbuf.name) == 0) { 6701119Sbill passtape(); 6711119Sbill continue; 6721119Sbill } 67312154Ssam if (Fflag) { 67412154Ssam char *s; 67512154Ssam 67612154Ssam if ((s = rindex(dblock.dbuf.name, '/')) == 0) 67712154Ssam s = dblock.dbuf.name; 67812154Ssam else 67912154Ssam s++; 68012154Ssam if (checkf(s, stbuf.st_mode, Fflag) == 0) { 68112154Ssam passtape(); 68212154Ssam continue; 68312154Ssam } 68412154Ssam } 68522688Slepreau if (checkdir(dblock.dbuf.name)) { /* have a directory */ 68622688Slepreau if (mflag == 0) 68722688Slepreau dodirtimes(&dblock); 6886250Sroot continue; 68922688Slepreau } 69022688Slepreau if (dblock.dbuf.linkflag == '2') { /* symlink */ 69125250Sbloom /* 69225250Sbloom * only unlink non directories or empty 69325250Sbloom * directories 69425250Sbloom */ 69525250Sbloom if (rmdir(dblock.dbuf.name) < 0) { 69625250Sbloom if (errno == ENOTDIR) 69725250Sbloom unlink(dblock.dbuf.name); 69825250Sbloom } 6996250Sroot if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) { 70027058Smckusick fprintf(stderr, "tar: %s: symbolic link failed: ", 70113492Ssam dblock.dbuf.name); 70227058Smckusick perror(""); 7036250Sroot continue; 7046250Sroot } 7056250Sroot if (vflag) 70621910Skjd fprintf(vfile, "x %s symbolic link to %s\n", 70713492Ssam dblock.dbuf.name, dblock.dbuf.linkname); 70822353Skjd #ifdef notdef 70922353Skjd /* ignore alien orders */ 71022353Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 71122688Slepreau if (mflag == 0) 71222688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime); 71322353Skjd if (pflag) 71422353Skjd chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 71522353Skjd #endif 7161119Sbill continue; 7176250Sroot } 71822688Slepreau if (dblock.dbuf.linkflag == '1') { /* regular link */ 71925250Sbloom /* 72025250Sbloom * only unlink non directories or empty 72125250Sbloom * directories 72225250Sbloom */ 72325250Sbloom if (rmdir(dblock.dbuf.name) < 0) { 72425250Sbloom if (errno == ENOTDIR) 72525250Sbloom unlink(dblock.dbuf.name); 72625250Sbloom } 7271119Sbill if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) { 72827058Smckusick fprintf(stderr, "tar: can't link %s to %s: ", 72927058Smckusick dblock.dbuf.name, dblock.dbuf.linkname); 73027058Smckusick perror(""); 7311119Sbill continue; 7321119Sbill } 7331119Sbill if (vflag) 73422688Slepreau fprintf(vfile, "%s linked to %s\n", 73513492Ssam dblock.dbuf.name, dblock.dbuf.linkname); 7361119Sbill continue; 7371119Sbill } 7386250Sroot if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) { 73927058Smckusick fprintf(stderr, "tar: can't create %s: ", 74013492Ssam dblock.dbuf.name); 74127058Smckusick perror(""); 7421119Sbill passtape(); 7431119Sbill continue; 7441119Sbill } 74521457Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 7461119Sbill blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK; 7471119Sbill if (vflag) 74822688Slepreau fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n", 74913492Ssam dblock.dbuf.name, bytes, blocks); 75021457Skjd for (; blocks > 0;) { 75121457Skjd register int nread; 75221457Skjd char *bufp; 75321457Skjd register int nwant; 75421457Skjd 75521457Skjd nwant = NBLOCK*TBLOCK; 75621457Skjd if (nwant > (blocks*TBLOCK)) 75721457Skjd nwant = (blocks*TBLOCK); 75821457Skjd nread = readtbuf(&bufp, nwant); 75922688Slepreau if (write(ofile, bufp, (int)min(nread, bytes)) < 0) { 7606250Sroot fprintf(stderr, 76127058Smckusick "tar: %s: HELP - extract write error", 76213492Ssam dblock.dbuf.name); 76327058Smckusick perror(""); 7646250Sroot done(2); 7656250Sroot } 76621457Skjd bytes -= nread; 76721457Skjd blocks -= (((nread-1)/TBLOCK)+1); 7681119Sbill } 7691119Sbill close(ofile); 77022688Slepreau if (mflag == 0) 77122688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime); 7721926Swnj if (pflag) 7736250Sroot chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 7741119Sbill } 77522688Slepreau if (mflag == 0) { 77622688Slepreau dblock.dbuf.name[0] = '\0'; /* process the whole stack */ 77722688Slepreau dodirtimes(&dblock); 77822688Slepreau } 7791119Sbill } 7801119Sbill 7811119Sbill dotable() 7821119Sbill { 7831119Sbill for (;;) { 7841119Sbill getdir(); 7851119Sbill if (endtape()) 7861119Sbill break; 7871119Sbill if (vflag) 7881119Sbill longt(&stbuf); 7891119Sbill printf("%s", dblock.dbuf.name); 7901119Sbill if (dblock.dbuf.linkflag == '1') 7911119Sbill printf(" linked to %s", dblock.dbuf.linkname); 7926250Sroot if (dblock.dbuf.linkflag == '2') 7936250Sroot printf(" symbolic link to %s", dblock.dbuf.linkname); 7941119Sbill printf("\n"); 7951119Sbill passtape(); 7961119Sbill } 7971119Sbill } 7981119Sbill 7991119Sbill putempty() 8001119Sbill { 8011119Sbill char buf[TBLOCK]; 8021119Sbill 80313492Ssam bzero(buf, sizeof (buf)); 8041119Sbill writetape(buf); 8051119Sbill } 8061119Sbill 8071119Sbill longt(st) 8086250Sroot register struct stat *st; 8091119Sbill { 8101119Sbill register char *cp; 8111119Sbill char *ctime(); 8121119Sbill 8131119Sbill pmode(st); 8141119Sbill printf("%3d/%1d", st->st_uid, st->st_gid); 8151119Sbill printf("%7D", st->st_size); 8161119Sbill cp = ctime(&st->st_mtime); 8171119Sbill printf(" %-12.12s %-4.4s ", cp+4, cp+20); 8181119Sbill } 8191119Sbill 8201119Sbill #define SUID 04000 8211119Sbill #define SGID 02000 8221119Sbill #define ROWN 0400 8231119Sbill #define WOWN 0200 8241119Sbill #define XOWN 0100 8251119Sbill #define RGRP 040 8261119Sbill #define WGRP 020 8271119Sbill #define XGRP 010 8281119Sbill #define ROTH 04 8291119Sbill #define WOTH 02 8301119Sbill #define XOTH 01 8311119Sbill #define STXT 01000 8321119Sbill int m1[] = { 1, ROWN, 'r', '-' }; 8331119Sbill int m2[] = { 1, WOWN, 'w', '-' }; 8341119Sbill int m3[] = { 2, SUID, 's', XOWN, 'x', '-' }; 8351119Sbill int m4[] = { 1, RGRP, 'r', '-' }; 8361119Sbill int m5[] = { 1, WGRP, 'w', '-' }; 8371119Sbill int m6[] = { 2, SGID, 's', XGRP, 'x', '-' }; 8381119Sbill int m7[] = { 1, ROTH, 'r', '-' }; 8391119Sbill int m8[] = { 1, WOTH, 'w', '-' }; 8401119Sbill int m9[] = { 2, STXT, 't', XOTH, 'x', '-' }; 8411119Sbill 8421119Sbill int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9}; 8431119Sbill 8441119Sbill pmode(st) 8456250Sroot register struct stat *st; 8461119Sbill { 8471119Sbill register int **mp; 8481119Sbill 8491119Sbill for (mp = &m[0]; mp < &m[9];) 85027058Smckusick selectbits(*mp++, st); 8511119Sbill } 8521119Sbill 85327058Smckusick selectbits(pairp, st) 8546250Sroot int *pairp; 8556250Sroot struct stat *st; 8561119Sbill { 8571119Sbill register int n, *ap; 8581119Sbill 8591119Sbill ap = pairp; 8601119Sbill n = *ap++; 8611119Sbill while (--n>=0 && (st->st_mode&*ap++)==0) 8621119Sbill ap++; 8631119Sbill printf("%c", *ap); 8641119Sbill } 8651119Sbill 86622688Slepreau /* 867*27442Slepreau * Make all directories needed by `name'. If `name' is itself 868*27442Slepreau * a directory on the tar tape (indicated by a trailing '/'), 86922688Slepreau * return 1; else 0. 87022688Slepreau */ 8711119Sbill checkdir(name) 8726250Sroot register char *name; 8731119Sbill { 8741119Sbill register char *cp; 8756250Sroot 87612154Ssam /* 87722688Slepreau * Quick check for existence of directory. 87812154Ssam */ 87912154Ssam if ((cp = rindex(name, '/')) == 0) 88012154Ssam return (0); 88112154Ssam *cp = '\0'; 88222688Slepreau if (access(name, 0) == 0) { /* already exists */ 88312154Ssam *cp = '/'; 88422688Slepreau return (cp[1] == '\0'); /* return (lastchar == '/') */ 88512154Ssam } 88612154Ssam *cp = '/'; 88712154Ssam 88812154Ssam /* 88912154Ssam * No luck, try to make all directories in path. 89012154Ssam */ 8911119Sbill for (cp = name; *cp; cp++) { 8926250Sroot if (*cp != '/') 8936250Sroot continue; 8946250Sroot *cp = '\0'; 89512154Ssam if (access(name, 0) < 0) { 8969844Ssam if (mkdir(name, 0777) < 0) { 8979844Ssam perror(name); 89812154Ssam *cp = '/'; 89912154Ssam return (0); 9001119Sbill } 90121457Skjd chown(name, stbuf.st_uid, stbuf.st_gid); 902*27442Slepreau if (pflag && cp[1] == '\0') /* dir on the tape */ 903*27442Slepreau chmod(name, stbuf.st_mode & 07777); 9041119Sbill } 9056250Sroot *cp = '/'; 9061119Sbill } 9076250Sroot return (cp[-1]=='/'); 9081119Sbill } 9091119Sbill 9101119Sbill onintr() 9111119Sbill { 9121119Sbill signal(SIGINT, SIG_IGN); 9131119Sbill term++; 9141119Sbill } 9151119Sbill 9161119Sbill onquit() 9171119Sbill { 9181119Sbill signal(SIGQUIT, SIG_IGN); 9191119Sbill term++; 9201119Sbill } 9211119Sbill 9221119Sbill onhup() 9231119Sbill { 9241119Sbill signal(SIGHUP, SIG_IGN); 9251119Sbill term++; 9261119Sbill } 9271119Sbill 92822688Slepreau #ifdef notdef 9291119Sbill onterm() 9301119Sbill { 9311119Sbill signal(SIGTERM, SIG_IGN); 9321119Sbill term++; 9331119Sbill } 93422688Slepreau #endif 9351119Sbill 9361119Sbill tomodes(sp) 9371119Sbill register struct stat *sp; 9381119Sbill { 9391119Sbill register char *cp; 9401119Sbill 9411119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 9421119Sbill *cp = '\0'; 9431119Sbill sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777); 9441119Sbill sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid); 9451119Sbill sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid); 9461119Sbill sprintf(dblock.dbuf.size, "%11lo ", sp->st_size); 9471119Sbill sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime); 9481119Sbill } 9491119Sbill 9501119Sbill checksum() 9511119Sbill { 9521119Sbill register i; 9531119Sbill register char *cp; 9541119Sbill 9556250Sroot for (cp = dblock.dbuf.chksum; 9566250Sroot cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++) 9571119Sbill *cp = ' '; 9581119Sbill i = 0; 9591119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 9601119Sbill i += *cp; 9616250Sroot return (i); 9621119Sbill } 9631119Sbill 9641119Sbill checkw(c, name) 9656250Sroot char *name; 9661119Sbill { 9676250Sroot if (!wflag) 9686250Sroot return (1); 9696250Sroot printf("%c ", c); 9706250Sroot if (vflag) 9716250Sroot longt(&stbuf); 9726250Sroot printf("%s: ", name); 9736250Sroot return (response() == 'y'); 9741119Sbill } 9751119Sbill 9761119Sbill response() 9771119Sbill { 9781119Sbill char c; 9791119Sbill 9801119Sbill c = getchar(); 9811119Sbill if (c != '\n') 9826250Sroot while (getchar() != '\n') 9836250Sroot ; 9846250Sroot else 9856250Sroot c = 'n'; 9866250Sroot return (c); 9871119Sbill } 9881119Sbill 98912154Ssam checkf(name, mode, howmuch) 99012154Ssam char *name; 99112154Ssam int mode, howmuch; 99212154Ssam { 99312154Ssam int l; 99412154Ssam 99521910Skjd if ((mode & S_IFMT) == S_IFDIR){ 99621910Skjd if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0)) 99721910Skjd return(0); 99821910Skjd return(1); 99921910Skjd } 100012154Ssam if ((l = strlen(name)) < 3) 100112154Ssam return (1); 100212154Ssam if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o') 100312154Ssam return (0); 100412154Ssam if (strcmp(name, "core") == 0 || 100512154Ssam strcmp(name, "errs") == 0 || 100612154Ssam (howmuch > 1 && strcmp(name, "a.out") == 0)) 100712154Ssam return (0); 100812154Ssam /* SHOULD CHECK IF IT IS EXECUTABLE */ 100912154Ssam return (1); 101012154Ssam } 101112154Ssam 101222688Slepreau /* Is the current file a new file, or the newest one of the same name? */ 10131119Sbill checkupdate(arg) 10146250Sroot char *arg; 10151119Sbill { 10161119Sbill char name[100]; 10176250Sroot long mtime; 10181119Sbill daddr_t seekp; 10191119Sbill daddr_t lookup(); 10201119Sbill 10211119Sbill rewind(tfile); 10221119Sbill for (;;) { 10231119Sbill if ((seekp = lookup(arg)) < 0) 10246250Sroot return (1); 10251119Sbill fseek(tfile, seekp, 0); 10261119Sbill fscanf(tfile, "%s %lo", name, &mtime); 10276250Sroot return (stbuf.st_mtime > mtime); 10281119Sbill } 10291119Sbill } 10301119Sbill 10311119Sbill done(n) 10321119Sbill { 103321457Skjd unlink(tname); 10341119Sbill exit(n); 10351119Sbill } 10361119Sbill 103722688Slepreau /* 103822688Slepreau * Does s2 begin with the string s1, on a directory boundary? 103922688Slepreau */ 10401119Sbill prefix(s1, s2) 10416250Sroot register char *s1, *s2; 10421119Sbill { 10431119Sbill while (*s1) 10441119Sbill if (*s1++ != *s2++) 10456250Sroot return (0); 10461119Sbill if (*s2) 10476250Sroot return (*s2 == '/'); 10486250Sroot return (1); 10491119Sbill } 10501119Sbill 10511119Sbill #define N 200 10521119Sbill int njab; 10536250Sroot 10541119Sbill daddr_t 10551119Sbill lookup(s) 10566250Sroot char *s; 10571119Sbill { 10581119Sbill register i; 10591119Sbill daddr_t a; 10601119Sbill 10611119Sbill for(i=0; s[i]; i++) 10626250Sroot if (s[i] == ' ') 10631119Sbill break; 10641119Sbill a = bsrch(s, i, low, high); 10656250Sroot return (a); 10661119Sbill } 10671119Sbill 10681119Sbill daddr_t 10691119Sbill bsrch(s, n, l, h) 10706250Sroot daddr_t l, h; 10716250Sroot char *s; 10721119Sbill { 10731119Sbill register i, j; 10741119Sbill char b[N]; 10751119Sbill daddr_t m, m1; 10761119Sbill 10771119Sbill njab = 0; 10781119Sbill 10791119Sbill loop: 10806250Sroot if (l >= h) 108122688Slepreau return ((daddr_t) -1); 10821119Sbill m = l + (h-l)/2 - N/2; 10836250Sroot if (m < l) 10841119Sbill m = l; 10851119Sbill fseek(tfile, m, 0); 10861119Sbill fread(b, 1, N, tfile); 10871119Sbill njab++; 10881119Sbill for(i=0; i<N; i++) { 10896250Sroot if (b[i] == '\n') 10901119Sbill break; 10911119Sbill m++; 10921119Sbill } 10936250Sroot if (m >= h) 109422688Slepreau return ((daddr_t) -1); 10951119Sbill m1 = m; 10961119Sbill j = i; 10971119Sbill for(i++; i<N; i++) { 10981119Sbill m1++; 10996250Sroot if (b[i] == '\n') 11001119Sbill break; 11011119Sbill } 11021119Sbill i = cmp(b+j, s, n); 11036250Sroot if (i < 0) { 11041119Sbill h = m; 11051119Sbill goto loop; 11061119Sbill } 11076250Sroot if (i > 0) { 11081119Sbill l = m1; 11091119Sbill goto loop; 11101119Sbill } 11116250Sroot return (m); 11121119Sbill } 11131119Sbill 11141119Sbill cmp(b, s, n) 11156250Sroot char *b, *s; 11161119Sbill { 11171119Sbill register i; 11181119Sbill 11196250Sroot if (b[0] != '\n') 112021457Skjd exit(2); 11211119Sbill for(i=0; i<n; i++) { 11226250Sroot if (b[i+1] > s[i]) 11236250Sroot return (-1); 11246250Sroot if (b[i+1] < s[i]) 11256250Sroot return (1); 11261119Sbill } 11276250Sroot return (b[i+1] == ' '? 0 : -1); 11281119Sbill } 11291119Sbill 113022688Slepreau readtape(buffer) 11316250Sroot char *buffer; 11321119Sbill { 113321457Skjd char *bufp; 113421457Skjd 113522688Slepreau if (first == 0) 113622688Slepreau getbuf(); 113722688Slepreau readtbuf(&bufp, TBLOCK); 113821457Skjd bcopy(bufp, buffer, TBLOCK); 113921457Skjd return(TBLOCK); 114021457Skjd } 114121457Skjd 114221457Skjd readtbuf(bufpp, size) 114321457Skjd char **bufpp; 114421457Skjd int size; 114521457Skjd { 11463457Swnj register int i; 11471119Sbill 11481119Sbill if (recno >= nblock || first == 0) { 114927058Smckusick if ((i = bread(mt, tbuf, TBLOCK*nblock)) < 0) 115027058Smckusick mterr("read", i, 3); 11511119Sbill if (first == 0) { 11521119Sbill if ((i % TBLOCK) != 0) { 115313492Ssam fprintf(stderr, "tar: tape blocksize error\n"); 11541119Sbill done(3); 11551119Sbill } 11561119Sbill i /= TBLOCK; 11573457Swnj if (i != nblock) { 115813492Ssam fprintf(stderr, "tar: blocksize = %d\n", i); 11591119Sbill nblock = i; 11601119Sbill } 116122353Skjd first = 1; 11621119Sbill } 11631119Sbill recno = 0; 11641119Sbill } 116521457Skjd if (size > ((nblock-recno)*TBLOCK)) 116621457Skjd size = (nblock-recno)*TBLOCK; 116721457Skjd *bufpp = (char *)&tbuf[recno]; 116821457Skjd recno += (size/TBLOCK); 116921457Skjd return (size); 11701119Sbill } 11711119Sbill 117221457Skjd writetbuf(buffer, n) 117321457Skjd register char *buffer; 117421457Skjd register int n; 11751119Sbill { 117627058Smckusick int i; 117722688Slepreau 117822688Slepreau if (first == 0) { 117922353Skjd getbuf(); 118022353Skjd first = 1; 118122353Skjd } 11821119Sbill if (recno >= nblock) { 118327058Smckusick i = write(mt, tbuf, TBLOCK*nblock); 118427058Smckusick if (i != TBLOCK*nblock) 118527058Smckusick mterr("write", i, 2); 11861119Sbill recno = 0; 11871119Sbill } 118821457Skjd 118921457Skjd /* 119021457Skjd * Special case: We have an empty tape buffer, and the 119121457Skjd * users data size is >= the tape block size: Avoid 119221457Skjd * the bcopy and dma direct to tape. BIG WIN. Add the 119321457Skjd * residual to the tape buffer. 119421457Skjd */ 119521457Skjd while (recno == 0 && n >= nblock) { 119627058Smckusick i = write(mt, buffer, TBLOCK*nblock); 119727058Smckusick if (i != TBLOCK*nblock) 119827058Smckusick mterr("write", i, 2); 119921457Skjd n -= nblock; 120021457Skjd buffer += (nblock * TBLOCK); 12011119Sbill } 120221457Skjd 120321457Skjd while (n-- > 0) { 120421457Skjd bcopy(buffer, (char *)&tbuf[recno++], TBLOCK); 120521457Skjd buffer += TBLOCK; 120621457Skjd if (recno >= nblock) { 120727058Smckusick i = write(mt, tbuf, TBLOCK*nblock); 120827058Smckusick if (i != TBLOCK*nblock) 120927058Smckusick mterr("write", i, 2); 121021457Skjd recno = 0; 121121457Skjd } 121221457Skjd } 121321457Skjd 121421457Skjd /* Tell the user how much to write to get in sync */ 121521457Skjd return (nblock - recno); 12161119Sbill } 12171119Sbill 12181119Sbill backtape() 12191119Sbill { 122027058Smckusick static int mtdev = 1; 12213457Swnj static struct mtop mtop = {MTBSR, 1}; 122227058Smckusick struct mtget mtget; 122327058Smckusick 122427058Smckusick if (mtdev == 1) 122527058Smckusick mtdev = ioctl(mt, MTIOCGET, &mtget); 12263457Swnj if (mtdev == 0) { 12273457Swnj if (ioctl(mt, MTIOCTOP, &mtop) < 0) { 122827058Smckusick fprintf(stderr, "tar: tape backspace error: "); 122927058Smckusick perror(""); 12301119Sbill done(4); 12311119Sbill } 12323457Swnj } else 123322688Slepreau lseek(mt, (daddr_t) -TBLOCK*nblock, 1); 12343457Swnj recno--; 12351119Sbill } 12361119Sbill 12371119Sbill flushtape() 12381119Sbill { 123927058Smckusick int i; 124027058Smckusick 124127058Smckusick i = write(mt, tbuf, TBLOCK*nblock); 124227058Smckusick if (i != TBLOCK*nblock) 124327058Smckusick mterr("write", i, 2); 12441119Sbill } 12451119Sbill 124627058Smckusick mterr(operation, i, exitcode) 124727058Smckusick char *operation; 124827058Smckusick int i; 124927058Smckusick { 125027058Smckusick fprintf(stderr, "tar: tape %s error: ", operation); 125127058Smckusick if (i < 0) 125227058Smckusick perror(""); 125327058Smckusick else 125427058Smckusick fprintf(stderr, "unexpected EOF\n"); 125527058Smckusick done(exitcode); 125627058Smckusick } 125727058Smckusick 12588737Smckusick bread(fd, buf, size) 12598737Smckusick int fd; 12608737Smckusick char *buf; 12618737Smckusick int size; 12628737Smckusick { 12638737Smckusick int count; 12648737Smckusick static int lastread = 0; 12658737Smckusick 126622688Slepreau if (!Bflag) 126722688Slepreau return (read(fd, buf, size)); 126822353Skjd 12698737Smckusick for (count = 0; count < size; count += lastread) { 127024281Smckusick lastread = read(fd, buf, size - count); 127124281Smckusick if (lastread <= 0) { 12728737Smckusick if (count > 0) 12738737Smckusick return (count); 12748737Smckusick return (lastread); 12758737Smckusick } 12768737Smckusick buf += lastread; 12778737Smckusick } 12788737Smckusick return (count); 12798737Smckusick } 128010165Ssam 128110165Ssam char * 128210165Ssam getcwd(buf) 128310165Ssam char *buf; 128410165Ssam { 128510165Ssam if (getwd(buf) == NULL) { 128610165Ssam fprintf(stderr, "tar: %s\n", buf); 128721457Skjd exit(1); 128810165Ssam } 128910165Ssam return (buf); 129010165Ssam } 129121910Skjd 129221910Skjd getbuf() 129321910Skjd { 129422353Skjd 129527058Smckusick if (nblock == 0) { 129621910Skjd fstat(mt, &stbuf); 129721910Skjd if ((stbuf.st_mode & S_IFMT) == S_IFCHR) 129827058Smckusick nblock = NBLOCK; 129921910Skjd else { 130027058Smckusick nblock = stbuf.st_blksize / TBLOCK; 130127058Smckusick if (nblock == 0) 130227058Smckusick nblock = NBLOCK; 130321910Skjd } 130421910Skjd } 130521910Skjd tbuf = (union hblock *)malloc(nblock*TBLOCK); 130621910Skjd if (tbuf == NULL) { 130721910Skjd fprintf(stderr, "tar: blocksize %d too big, can't get memory\n", 130821910Skjd nblock); 130921910Skjd done(1); 131021910Skjd } 131121910Skjd } 131222353Skjd 131322688Slepreau /* 1314*27442Slepreau * Save this directory and its mtime on the stack, popping and setting 1315*27442Slepreau * the mtimes of any stacked dirs which aren't parents of this one. 1316*27442Slepreau * A null directory causes the entire stack to be unwound and set. 131722688Slepreau * 1318*27442Slepreau * Since all the elements of the directory "stack" share a common 1319*27442Slepreau * prefix, we can make do with one string. We keep only the current 1320*27442Slepreau * directory path, with an associated array of mtime's, one for each 1321*27442Slepreau * '/' in the path. A negative mtime means no mtime. The mtime's are 1322*27442Slepreau * offset by one (first index 1, not 0) because calling this with a null 1323*27442Slepreau * directory causes mtime[0] to be set. 1324*27442Slepreau * 132522688Slepreau * This stack algorithm is not guaranteed to work for tapes created 132622688Slepreau * with the 'r' option, but the vast majority of tapes with 132722688Slepreau * directories are not. This avoids saving every directory record on 132822688Slepreau * the tape and setting all the times at the end. 132922688Slepreau */ 133022688Slepreau char dirstack[NAMSIZ]; 133122688Slepreau #define NTIM (NAMSIZ/2+1) /* a/b/c/d/... */ 133222688Slepreau time_t mtime[NTIM]; 133322353Skjd 133422688Slepreau dodirtimes(hp) 133522688Slepreau union hblock *hp; 133622353Skjd { 133722688Slepreau register char *p = dirstack; 133822688Slepreau register char *q = hp->dbuf.name; 133922688Slepreau register int ndir = 0; 134022688Slepreau char *savp; 134122688Slepreau int savndir; 134222353Skjd 134322688Slepreau /* Find common prefix */ 134422688Slepreau while (*p == *q) { 134522688Slepreau if (*p++ == '/') 134622688Slepreau ++ndir; 134722688Slepreau q++; 134822688Slepreau } 134922353Skjd 135022688Slepreau savp = p; 135122688Slepreau savndir = ndir; 135222688Slepreau while (*p) { 135322688Slepreau /* 135422688Slepreau * Not a child: unwind the stack, setting the times. 135522688Slepreau * The order we do this doesn't matter, so we go "forward." 135622688Slepreau */ 135722688Slepreau if (*p++ == '/') 135822688Slepreau if (mtime[++ndir] >= 0) { 135922688Slepreau *--p = '\0'; /* zap the slash */ 136022688Slepreau setimes(dirstack, mtime[ndir]); 136122688Slepreau *p++ = '/'; 136222688Slepreau } 136322688Slepreau } 136422688Slepreau p = savp; 136522688Slepreau ndir = savndir; 136622353Skjd 136722688Slepreau /* Push this one on the "stack" */ 136822688Slepreau while (*p = *q++) /* append the rest of the new dir */ 136922688Slepreau if (*p++ == '/') 137022688Slepreau mtime[++ndir] = -1; 137122688Slepreau mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */ 137222688Slepreau } 137322353Skjd 137422688Slepreau setimes(path, mt) 137522688Slepreau char *path; 137622688Slepreau time_t mt; 137722688Slepreau { 137822688Slepreau struct timeval tv[2]; 137922353Skjd 138022688Slepreau tv[0].tv_sec = time((time_t *) 0); 138122688Slepreau tv[1].tv_sec = mt; 138222688Slepreau tv[0].tv_usec = tv[1].tv_usec = 0; 138327058Smckusick if (utimes(path, tv) < 0) { 138427058Smckusick fprintf(stderr, "tar: can't set time on %s: ", path); 138527058Smckusick perror(""); 138627058Smckusick } 138722688Slepreau } 138822688Slepreau 138922688Slepreau char * 139022688Slepreau getmem(size) 139122688Slepreau { 139222688Slepreau char *p = malloc((unsigned) size); 139322688Slepreau 139422688Slepreau if (p == NULL && freemem) { 139522688Slepreau fprintf(stderr, 139622688Slepreau "tar: out of memory, link and directory modtime info lost\n"); 139722688Slepreau freemem = 0; 139822353Skjd } 139922688Slepreau return (p); 140022353Skjd } 1401