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*30090Sbostic static char sccsid[] = "@(#)tar.c 5.8 (Berkeley) 11/17/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(); 10727445Slepreau long time(); 10827445Slepreau off_t lseek(); 10927445Slepreau char *mktemp(); 1106250Sroot char *sprintf(); 1116250Sroot char *strcat(); 11227058Smckusick char *strcpy(); 11312154Ssam char *rindex(); 11410165Ssam char *getcwd(); 1159844Ssam char *getwd(); 11622688Slepreau char *getmem(); 1171119Sbill 1181119Sbill main(argc, argv) 1191119Sbill int argc; 1201119Sbill char *argv[]; 1211119Sbill { 1221119Sbill char *cp; 1231119Sbill 1241119Sbill if (argc < 2) 1251119Sbill usage(); 1261119Sbill 1271119Sbill tfile = NULL; 1281119Sbill usefile = magtape; 1291119Sbill argv[argc] = 0; 1301119Sbill argv++; 1311119Sbill for (cp = *argv++; *cp; cp++) 1321119Sbill switch(*cp) { 1336250Sroot 1341119Sbill case 'f': 13512154Ssam if (*argv == 0) { 13612154Ssam fprintf(stderr, 13712154Ssam "tar: tapefile must be specified with 'f' option\n"); 13812154Ssam usage(); 13912154Ssam } 1401119Sbill usefile = *argv++; 1411119Sbill fflag++; 1421119Sbill break; 1436250Sroot 1441119Sbill case 'c': 1451119Sbill cflag++; 1461119Sbill rflag++; 1471119Sbill break; 1486250Sroot 1491119Sbill case 'o': 1501119Sbill oflag++; 1511119Sbill break; 1526250Sroot 1531119Sbill case 'p': 1541119Sbill pflag++; 1551119Sbill break; 1566250Sroot 1571119Sbill case 'u': 1581119Sbill mktemp(tname); 1591119Sbill if ((tfile = fopen(tname, "w")) == NULL) { 1606250Sroot fprintf(stderr, 16122688Slepreau "tar: cannot create temporary file (%s)\n", 1626250Sroot tname); 1631119Sbill done(1); 1641119Sbill } 1651119Sbill fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n"); 1666250Sroot /*FALL THRU*/ 1676250Sroot 1681119Sbill case 'r': 1691119Sbill rflag++; 1701119Sbill break; 1716250Sroot 1721119Sbill case 'v': 1731119Sbill vflag++; 1741119Sbill break; 1756250Sroot 1761119Sbill case 'w': 1771119Sbill wflag++; 1781119Sbill break; 1796250Sroot 1801119Sbill case 'x': 1811119Sbill xflag++; 1821119Sbill break; 1836250Sroot 1841119Sbill case 't': 1851119Sbill tflag++; 1861119Sbill break; 1876250Sroot 1881119Sbill case 'm': 1891119Sbill mflag++; 1901119Sbill break; 1916250Sroot 1921119Sbill case '-': 1931119Sbill break; 1946250Sroot 1951119Sbill case '0': 1961119Sbill case '1': 1971119Sbill case '4': 1981119Sbill case '5': 19921457Skjd case '7': 2001119Sbill case '8': 2011119Sbill magtape[8] = *cp; 2021119Sbill usefile = magtape; 2031119Sbill break; 2046250Sroot 2051119Sbill case 'b': 20613492Ssam if (*argv == 0) { 20713492Ssam fprintf(stderr, 20813492Ssam "tar: blocksize must be specified with 'b' option\n"); 20913492Ssam usage(); 21013492Ssam } 21113492Ssam nblock = atoi(*argv); 21213492Ssam if (nblock <= 0) { 21313492Ssam fprintf(stderr, 21413492Ssam "tar: invalid blocksize \"%s\"\n", *argv); 2151119Sbill done(1); 2161119Sbill } 21713492Ssam argv++; 2181119Sbill break; 2196250Sroot 2201119Sbill case 'l': 22122688Slepreau prtlinkerr++; 2221119Sbill break; 2236250Sroot 2246250Sroot case 'h': 2256250Sroot hflag++; 2266250Sroot break; 2276250Sroot 22812154Ssam case 'i': 22912154Ssam iflag++; 23012154Ssam break; 23112154Ssam 2328737Smckusick case 'B': 2338737Smckusick Bflag++; 2348737Smckusick break; 2358737Smckusick 23612154Ssam case 'F': 23712154Ssam Fflag++; 23812154Ssam break; 23912154Ssam 2401119Sbill default: 2411119Sbill fprintf(stderr, "tar: %c: unknown option\n", *cp); 2421119Sbill usage(); 2431119Sbill } 2441119Sbill 2456250Sroot if (!rflag && !xflag && !tflag) 2466250Sroot usage(); 2471119Sbill if (rflag) { 2486250Sroot if (cflag && tfile != NULL) 2491119Sbill usage(); 2501119Sbill if (signal(SIGINT, SIG_IGN) != SIG_IGN) 25127445Slepreau (void) signal(SIGINT, onintr); 2521119Sbill if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 25327445Slepreau (void) signal(SIGHUP, onhup); 2541119Sbill if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) 25527445Slepreau (void) signal(SIGQUIT, onquit); 2566250Sroot #ifdef notdef 2571119Sbill if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 25827445Slepreau (void) signal(SIGTERM, onterm); 2596250Sroot #endif 26027058Smckusick mt = openmt(usefile, 1); 2611119Sbill dorep(argv); 2626250Sroot done(0); 2631119Sbill } 26427058Smckusick mt = openmt(usefile, 0); 2656250Sroot if (xflag) 2661119Sbill doxtract(argv); 2676250Sroot else 26827445Slepreau dotable(argv); 2691119Sbill done(0); 2701119Sbill } 2711119Sbill 2721119Sbill usage() 2731119Sbill { 2746250Sroot fprintf(stderr, 27521457Skjd "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n"); 2761119Sbill done(1); 2771119Sbill } 2781119Sbill 27927058Smckusick int 28027058Smckusick openmt(tape, writing) 28127058Smckusick char *tape; 28227058Smckusick int writing; 28327058Smckusick { 28427058Smckusick 28527058Smckusick if (strcmp(tape, "-") == 0) { 28627058Smckusick /* 28727058Smckusick * Read from standard input or write to standard output. 28827058Smckusick */ 28927058Smckusick if (writing) { 29027058Smckusick if (cflag == 0) { 29127058Smckusick fprintf(stderr, 29227058Smckusick "tar: can only create standard output archives\n"); 29327058Smckusick done(1); 29427058Smckusick } 29527058Smckusick vfile = stderr; 29627058Smckusick setlinebuf(vfile); 29727058Smckusick mt = dup(1); 29827058Smckusick } else { 29927058Smckusick mt = dup(0); 30027058Smckusick Bflag++; 30127058Smckusick } 30227058Smckusick } else { 30327058Smckusick /* 30427058Smckusick * Use file or tape on local machine. 30527058Smckusick */ 30627058Smckusick if (writing) { 30727058Smckusick if (cflag) 30827445Slepreau mt = open(tape, O_RDWR|O_CREAT|O_TRUNC, 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) 44427445Slepreau (void) 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; 45927445Slepreau register int i; 46027445Slepreau long l; 4619601Ssam char newparent[NAMSIZ+64]; 46212154Ssam extern int errno; 46321457Skjd int maxread; 46421457Skjd int hint; /* amount to write to get "in sync" */ 4651119Sbill 4669601Ssam if (!hflag) 46712154Ssam i = lstat(shortname, &stbuf); 46812154Ssam else 46912154Ssam i = stat(shortname, &stbuf); 47012154Ssam if (i < 0) { 47127058Smckusick fprintf(stderr, "tar: "); 47227058Smckusick perror(longname); 4739601Ssam return; 4749601Ssam } 47512154Ssam if (tfile != NULL && checkupdate(longname) == 0) 4761119Sbill return; 47712154Ssam if (checkw('r', longname) == 0) 4781119Sbill return; 47912154Ssam if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0) 48012154Ssam return; 4811119Sbill 48212154Ssam switch (stbuf.st_mode & S_IFMT) { 48312154Ssam case S_IFDIR: 4846250Sroot for (i = 0, cp = buf; *cp++ = longname[i++];) 4856250Sroot ; 4861119Sbill *--cp = '/'; 4871119Sbill *++cp = 0 ; 4881119Sbill if (!oflag) { 4896250Sroot if ((cp - buf) >= NAMSIZ) { 49013492Ssam fprintf(stderr, "tar: %s: file name too long\n", 49113492Ssam longname); 4926250Sroot return; 4936250Sroot } 4946250Sroot stbuf.st_size = 0; 4956250Sroot tomodes(&stbuf); 4966250Sroot strcpy(dblock.dbuf.name,buf); 4976250Sroot sprintf(dblock.dbuf.chksum, "%6o", checksum()); 49827445Slepreau (void) writetape((char *)&dblock); 4991119Sbill } 5009601Ssam sprintf(newparent, "%s/%s", parent, shortname); 50115045Smckusick if (chdir(shortname) < 0) { 50215045Smckusick perror(shortname); 50315045Smckusick return; 50415045Smckusick } 5055931Smckusic if ((dirp = opendir(".")) == NULL) { 50613492Ssam fprintf(stderr, "tar: %s: directory read error\n", 50713492Ssam longname); 50815045Smckusick if (chdir(parent) < 0) { 50922688Slepreau fprintf(stderr, "tar: cannot change back?: "); 51015045Smckusick perror(parent); 51115045Smckusick } 5125931Smckusic return; 5135931Smckusic } 5145931Smckusic while ((dp = readdir(dirp)) != NULL && !term) { 5156250Sroot if (!strcmp(".", dp->d_name) || 5166250Sroot !strcmp("..", dp->d_name)) 5171119Sbill continue; 5185931Smckusic strcpy(cp, dp->d_name); 51927445Slepreau l = telldir(dirp); 5205931Smckusic closedir(dirp); 5219601Ssam putfile(buf, cp, newparent); 5225931Smckusic dirp = opendir("."); 52327445Slepreau seekdir(dirp, l); 5241119Sbill } 5255931Smckusic closedir(dirp); 52615045Smckusick if (chdir(parent) < 0) { 52722688Slepreau fprintf(stderr, "tar: cannot change back?: "); 52815045Smckusick perror(parent); 52915045Smckusick } 53012154Ssam break; 53112154Ssam 53212154Ssam case S_IFLNK: 53312154Ssam tomodes(&stbuf); 53412154Ssam if (strlen(longname) >= NAMSIZ) { 53513492Ssam fprintf(stderr, "tar: %s: file name too long\n", 53613492Ssam longname); 53712154Ssam return; 53812154Ssam } 53912154Ssam strcpy(dblock.dbuf.name, longname); 5406250Sroot if (stbuf.st_size + 1 >= NAMSIZ) { 54113492Ssam fprintf(stderr, "tar: %s: symbolic link too long\n", 54213492Ssam longname); 5436250Sroot return; 5446250Sroot } 5459601Ssam i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1); 5466250Sroot if (i < 0) { 54727058Smckusick fprintf(stderr, "tar: can't read symbolic link "); 5489601Ssam perror(longname); 5496250Sroot return; 5506250Sroot } 5516250Sroot dblock.dbuf.linkname[i] = '\0'; 5526250Sroot dblock.dbuf.linkflag = '2'; 55322688Slepreau if (vflag) 55422688Slepreau fprintf(vfile, "a %s symbolic link to %s\n", 55522688Slepreau longname, dblock.dbuf.linkname); 5566250Sroot sprintf(dblock.dbuf.size, "%11lo", 0); 5576250Sroot sprintf(dblock.dbuf.chksum, "%6o", checksum()); 55827445Slepreau (void) writetape((char *)&dblock); 55912154Ssam break; 5601119Sbill 56112154Ssam case S_IFREG: 56212154Ssam if ((infile = open(shortname, 0)) < 0) { 56327058Smckusick fprintf(stderr, "tar: "); 56427058Smckusick perror(longname); 5651119Sbill return; 5661119Sbill } 56712154Ssam tomodes(&stbuf); 56812154Ssam if (strlen(longname) >= NAMSIZ) { 56913492Ssam fprintf(stderr, "tar: %s: file name too long\n", 57013492Ssam longname); 57121457Skjd close(infile); 57212154Ssam return; 57312154Ssam } 57412154Ssam strcpy(dblock.dbuf.name, longname); 57512154Ssam if (stbuf.st_nlink > 1) { 57612154Ssam struct linkbuf *lp; 57712154Ssam int found = 0; 57812154Ssam 57912154Ssam for (lp = ihead; lp != NULL; lp = lp->nextp) 58012154Ssam if (lp->inum == stbuf.st_ino && 58112154Ssam lp->devnum == stbuf.st_dev) { 58212154Ssam found++; 58312154Ssam break; 58412154Ssam } 58512154Ssam if (found) { 58612154Ssam strcpy(dblock.dbuf.linkname, lp->pathname); 58712154Ssam dblock.dbuf.linkflag = '1'; 58812154Ssam sprintf(dblock.dbuf.chksum, "%6o", checksum()); 58927445Slepreau (void) writetape( (char *) &dblock); 59022688Slepreau if (vflag) 59122688Slepreau fprintf(vfile, "a %s link to %s\n", 59222688Slepreau longname, lp->pathname); 59312154Ssam lp->count--; 59412154Ssam close(infile); 59512154Ssam return; 5961119Sbill } 59722688Slepreau lp = (struct linkbuf *) getmem(sizeof(*lp)); 59822688Slepreau if (lp != NULL) { 59912154Ssam lp->nextp = ihead; 60012154Ssam ihead = lp; 60112154Ssam lp->inum = stbuf.st_ino; 60212154Ssam lp->devnum = stbuf.st_dev; 60312154Ssam lp->count = stbuf.st_nlink - 1; 60412154Ssam strcpy(lp->pathname, longname); 60512154Ssam } 6061119Sbill } 60721457Skjd blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK; 60822688Slepreau if (vflag) 60922688Slepreau fprintf(vfile, "a %s %ld blocks\n", longname, blocks); 61012154Ssam sprintf(dblock.dbuf.chksum, "%6o", checksum()); 61121457Skjd hint = writetape((char *)&dblock); 61221457Skjd maxread = max(stbuf.st_blksize, (nblock * TBLOCK)); 61327445Slepreau if ((bigbuf = malloc((unsigned)maxread)) == 0) { 61421457Skjd maxread = TBLOCK; 61521457Skjd bigbuf = buf; 61621457Skjd } 6171119Sbill 61821457Skjd while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0 61921457Skjd && blocks > 0) { 62021457Skjd register int nblks; 62121457Skjd 62221457Skjd nblks = ((i-1)/TBLOCK)+1; 62321457Skjd if (nblks > blocks) 62421457Skjd nblks = blocks; 62521457Skjd hint = writetbuf(bigbuf, nblks); 62621457Skjd blocks -= nblks; 62721457Skjd } 62821457Skjd close(infile); 62921457Skjd if (bigbuf != buf) 63021457Skjd free(bigbuf); 63127058Smckusick if (i < 0) { 63227445Slepreau fprintf(stderr, "tar: Read error on "); 63327058Smckusick perror(longname); 63427058Smckusick } else if (blocks != 0 || i != 0) 63513492Ssam fprintf(stderr, "tar: %s: file changed size\n", 63613492Ssam longname); 63712154Ssam while (--blocks >= 0) 63812154Ssam putempty(); 63912154Ssam break; 64012154Ssam 64112154Ssam default: 64212154Ssam fprintf(stderr, "tar: %s is not a file. Not dumped\n", 64313492Ssam longname); 64412154Ssam break; 6451119Sbill } 6461119Sbill } 6471119Sbill 6481119Sbill doxtract(argv) 6496250Sroot char *argv[]; 6501119Sbill { 6511119Sbill long blocks, bytes; 65227445Slepreau int ofile, i; 6531119Sbill 6541119Sbill for (;;) { 65527445Slepreau if ((i = wantit(argv)) == 0) 65627445Slepreau continue; 65727445Slepreau if (i == -1) 65827445Slepreau break; /* end of tape */ 6591119Sbill if (checkw('x', dblock.dbuf.name) == 0) { 6601119Sbill passtape(); 6611119Sbill continue; 6621119Sbill } 66312154Ssam if (Fflag) { 66412154Ssam char *s; 66512154Ssam 66612154Ssam if ((s = rindex(dblock.dbuf.name, '/')) == 0) 66712154Ssam s = dblock.dbuf.name; 66812154Ssam else 66912154Ssam s++; 67012154Ssam if (checkf(s, stbuf.st_mode, Fflag) == 0) { 67112154Ssam passtape(); 67212154Ssam continue; 67312154Ssam } 67412154Ssam } 67522688Slepreau if (checkdir(dblock.dbuf.name)) { /* have a directory */ 67622688Slepreau if (mflag == 0) 67722688Slepreau dodirtimes(&dblock); 6786250Sroot continue; 67922688Slepreau } 68022688Slepreau if (dblock.dbuf.linkflag == '2') { /* symlink */ 68125250Sbloom /* 68225250Sbloom * only unlink non directories or empty 68325250Sbloom * directories 68425250Sbloom */ 68525250Sbloom if (rmdir(dblock.dbuf.name) < 0) { 68625250Sbloom if (errno == ENOTDIR) 68725250Sbloom unlink(dblock.dbuf.name); 68825250Sbloom } 6896250Sroot if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) { 69027058Smckusick fprintf(stderr, "tar: %s: symbolic link failed: ", 69113492Ssam dblock.dbuf.name); 69227058Smckusick perror(""); 6936250Sroot continue; 6946250Sroot } 6956250Sroot if (vflag) 69621910Skjd fprintf(vfile, "x %s symbolic link to %s\n", 69713492Ssam dblock.dbuf.name, dblock.dbuf.linkname); 69822353Skjd #ifdef notdef 69922353Skjd /* ignore alien orders */ 70022353Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 70122688Slepreau if (mflag == 0) 70222688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime); 70322353Skjd if (pflag) 70422353Skjd chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 70522353Skjd #endif 7061119Sbill continue; 7076250Sroot } 70822688Slepreau if (dblock.dbuf.linkflag == '1') { /* regular link */ 70925250Sbloom /* 71025250Sbloom * only unlink non directories or empty 71125250Sbloom * directories 71225250Sbloom */ 71325250Sbloom if (rmdir(dblock.dbuf.name) < 0) { 71425250Sbloom if (errno == ENOTDIR) 71525250Sbloom unlink(dblock.dbuf.name); 71625250Sbloom } 7171119Sbill if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) { 71827058Smckusick fprintf(stderr, "tar: can't link %s to %s: ", 71927058Smckusick dblock.dbuf.name, dblock.dbuf.linkname); 72027058Smckusick perror(""); 7211119Sbill continue; 7221119Sbill } 7231119Sbill if (vflag) 72422688Slepreau fprintf(vfile, "%s linked to %s\n", 72513492Ssam dblock.dbuf.name, dblock.dbuf.linkname); 7261119Sbill continue; 7271119Sbill } 7286250Sroot if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) { 72927058Smckusick fprintf(stderr, "tar: can't create %s: ", 73013492Ssam dblock.dbuf.name); 73127058Smckusick perror(""); 7321119Sbill passtape(); 7331119Sbill continue; 7341119Sbill } 73521457Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 7361119Sbill blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK; 7371119Sbill if (vflag) 73822688Slepreau fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n", 73913492Ssam dblock.dbuf.name, bytes, blocks); 74021457Skjd for (; blocks > 0;) { 74121457Skjd register int nread; 74221457Skjd char *bufp; 74321457Skjd register int nwant; 74421457Skjd 74521457Skjd nwant = NBLOCK*TBLOCK; 74621457Skjd if (nwant > (blocks*TBLOCK)) 74721457Skjd nwant = (blocks*TBLOCK); 74821457Skjd nread = readtbuf(&bufp, nwant); 74922688Slepreau if (write(ofile, bufp, (int)min(nread, bytes)) < 0) { 7506250Sroot fprintf(stderr, 751*30090Sbostic "tar: %s: HELP - extract write error: ", 75213492Ssam dblock.dbuf.name); 75327058Smckusick perror(""); 7546250Sroot done(2); 7556250Sroot } 75621457Skjd bytes -= nread; 75721457Skjd blocks -= (((nread-1)/TBLOCK)+1); 7581119Sbill } 7591119Sbill close(ofile); 76022688Slepreau if (mflag == 0) 76122688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime); 7621926Swnj if (pflag) 7636250Sroot chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 7641119Sbill } 76522688Slepreau if (mflag == 0) { 76622688Slepreau dblock.dbuf.name[0] = '\0'; /* process the whole stack */ 76722688Slepreau dodirtimes(&dblock); 76822688Slepreau } 7691119Sbill } 7701119Sbill 77127445Slepreau dotable(argv) 77227445Slepreau char *argv[]; 7731119Sbill { 77427445Slepreau register int i; 77527445Slepreau 7761119Sbill for (;;) { 77727445Slepreau if ((i = wantit(argv)) == 0) 77827445Slepreau continue; 77927445Slepreau if (i == -1) 78027445Slepreau break; /* end of tape */ 7811119Sbill if (vflag) 7821119Sbill longt(&stbuf); 7831119Sbill printf("%s", dblock.dbuf.name); 7841119Sbill if (dblock.dbuf.linkflag == '1') 7851119Sbill printf(" linked to %s", dblock.dbuf.linkname); 7866250Sroot if (dblock.dbuf.linkflag == '2') 7876250Sroot printf(" symbolic link to %s", dblock.dbuf.linkname); 7881119Sbill printf("\n"); 7891119Sbill passtape(); 7901119Sbill } 7911119Sbill } 7921119Sbill 7931119Sbill putempty() 7941119Sbill { 7951119Sbill char buf[TBLOCK]; 7961119Sbill 79713492Ssam bzero(buf, sizeof (buf)); 79827445Slepreau (void) writetape(buf); 7991119Sbill } 8001119Sbill 8011119Sbill longt(st) 8026250Sroot register struct stat *st; 8031119Sbill { 8041119Sbill register char *cp; 8051119Sbill char *ctime(); 8061119Sbill 8071119Sbill pmode(st); 8081119Sbill printf("%3d/%1d", st->st_uid, st->st_gid); 80927445Slepreau printf("%7ld", st->st_size); 8101119Sbill cp = ctime(&st->st_mtime); 8111119Sbill printf(" %-12.12s %-4.4s ", cp+4, cp+20); 8121119Sbill } 8131119Sbill 8141119Sbill #define SUID 04000 8151119Sbill #define SGID 02000 8161119Sbill #define ROWN 0400 8171119Sbill #define WOWN 0200 8181119Sbill #define XOWN 0100 8191119Sbill #define RGRP 040 8201119Sbill #define WGRP 020 8211119Sbill #define XGRP 010 8221119Sbill #define ROTH 04 8231119Sbill #define WOTH 02 8241119Sbill #define XOTH 01 8251119Sbill #define STXT 01000 8261119Sbill int m1[] = { 1, ROWN, 'r', '-' }; 8271119Sbill int m2[] = { 1, WOWN, 'w', '-' }; 8281119Sbill int m3[] = { 2, SUID, 's', XOWN, 'x', '-' }; 8291119Sbill int m4[] = { 1, RGRP, 'r', '-' }; 8301119Sbill int m5[] = { 1, WGRP, 'w', '-' }; 8311119Sbill int m6[] = { 2, SGID, 's', XGRP, 'x', '-' }; 8321119Sbill int m7[] = { 1, ROTH, 'r', '-' }; 8331119Sbill int m8[] = { 1, WOTH, 'w', '-' }; 8341119Sbill int m9[] = { 2, STXT, 't', XOTH, 'x', '-' }; 8351119Sbill 8361119Sbill int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9}; 8371119Sbill 8381119Sbill pmode(st) 8396250Sroot register struct stat *st; 8401119Sbill { 8411119Sbill register int **mp; 8421119Sbill 8431119Sbill for (mp = &m[0]; mp < &m[9];) 84427058Smckusick selectbits(*mp++, st); 8451119Sbill } 8461119Sbill 84727058Smckusick selectbits(pairp, st) 8486250Sroot int *pairp; 8496250Sroot struct stat *st; 8501119Sbill { 8511119Sbill register int n, *ap; 8521119Sbill 8531119Sbill ap = pairp; 8541119Sbill n = *ap++; 8551119Sbill while (--n>=0 && (st->st_mode&*ap++)==0) 8561119Sbill ap++; 85727445Slepreau putchar(*ap); 8581119Sbill } 8591119Sbill 86022688Slepreau /* 86127442Slepreau * Make all directories needed by `name'. If `name' is itself 86227442Slepreau * a directory on the tar tape (indicated by a trailing '/'), 86322688Slepreau * return 1; else 0. 86422688Slepreau */ 8651119Sbill checkdir(name) 8666250Sroot register char *name; 8671119Sbill { 8681119Sbill register char *cp; 8696250Sroot 87012154Ssam /* 87122688Slepreau * Quick check for existence of directory. 87212154Ssam */ 87312154Ssam if ((cp = rindex(name, '/')) == 0) 87412154Ssam return (0); 87512154Ssam *cp = '\0'; 87622688Slepreau if (access(name, 0) == 0) { /* already exists */ 87712154Ssam *cp = '/'; 87822688Slepreau return (cp[1] == '\0'); /* return (lastchar == '/') */ 87912154Ssam } 88012154Ssam *cp = '/'; 88112154Ssam 88212154Ssam /* 88312154Ssam * No luck, try to make all directories in path. 88412154Ssam */ 8851119Sbill for (cp = name; *cp; cp++) { 8866250Sroot if (*cp != '/') 8876250Sroot continue; 8886250Sroot *cp = '\0'; 88912154Ssam if (access(name, 0) < 0) { 8909844Ssam if (mkdir(name, 0777) < 0) { 8919844Ssam perror(name); 89212154Ssam *cp = '/'; 89312154Ssam return (0); 8941119Sbill } 89521457Skjd chown(name, stbuf.st_uid, stbuf.st_gid); 89627442Slepreau if (pflag && cp[1] == '\0') /* dir on the tape */ 89727442Slepreau chmod(name, stbuf.st_mode & 07777); 8981119Sbill } 8996250Sroot *cp = '/'; 9001119Sbill } 9016250Sroot return (cp[-1]=='/'); 9021119Sbill } 9031119Sbill 9041119Sbill onintr() 9051119Sbill { 90627445Slepreau (void) signal(SIGINT, SIG_IGN); 9071119Sbill term++; 9081119Sbill } 9091119Sbill 9101119Sbill onquit() 9111119Sbill { 91227445Slepreau (void) signal(SIGQUIT, SIG_IGN); 9131119Sbill term++; 9141119Sbill } 9151119Sbill 9161119Sbill onhup() 9171119Sbill { 91827445Slepreau (void) signal(SIGHUP, SIG_IGN); 9191119Sbill term++; 9201119Sbill } 9211119Sbill 92222688Slepreau #ifdef notdef 9231119Sbill onterm() 9241119Sbill { 92527445Slepreau (void) signal(SIGTERM, SIG_IGN); 9261119Sbill term++; 9271119Sbill } 92822688Slepreau #endif 9291119Sbill 9301119Sbill tomodes(sp) 9311119Sbill register struct stat *sp; 9321119Sbill { 9331119Sbill register char *cp; 9341119Sbill 9351119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 9361119Sbill *cp = '\0'; 9371119Sbill sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777); 9381119Sbill sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid); 9391119Sbill sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid); 9401119Sbill sprintf(dblock.dbuf.size, "%11lo ", sp->st_size); 9411119Sbill sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime); 9421119Sbill } 9431119Sbill 9441119Sbill checksum() 9451119Sbill { 9461119Sbill register i; 9471119Sbill register char *cp; 9481119Sbill 9496250Sroot for (cp = dblock.dbuf.chksum; 9506250Sroot cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++) 9511119Sbill *cp = ' '; 9521119Sbill i = 0; 9531119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 9541119Sbill i += *cp; 9556250Sroot return (i); 9561119Sbill } 9571119Sbill 9581119Sbill checkw(c, name) 9596250Sroot char *name; 9601119Sbill { 9616250Sroot if (!wflag) 9626250Sroot return (1); 9636250Sroot printf("%c ", c); 9646250Sroot if (vflag) 9656250Sroot longt(&stbuf); 9666250Sroot printf("%s: ", name); 9676250Sroot return (response() == 'y'); 9681119Sbill } 9691119Sbill 9701119Sbill response() 9711119Sbill { 9721119Sbill char c; 9731119Sbill 9741119Sbill c = getchar(); 9751119Sbill if (c != '\n') 9766250Sroot while (getchar() != '\n') 9776250Sroot ; 9786250Sroot else 9796250Sroot c = 'n'; 9806250Sroot return (c); 9811119Sbill } 9821119Sbill 98312154Ssam checkf(name, mode, howmuch) 98412154Ssam char *name; 98512154Ssam int mode, howmuch; 98612154Ssam { 98712154Ssam int l; 98812154Ssam 98921910Skjd if ((mode & S_IFMT) == S_IFDIR){ 99021910Skjd if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0)) 99121910Skjd return(0); 99221910Skjd return(1); 99321910Skjd } 99412154Ssam if ((l = strlen(name)) < 3) 99512154Ssam return (1); 99612154Ssam if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o') 99712154Ssam return (0); 99812154Ssam if (strcmp(name, "core") == 0 || 99912154Ssam strcmp(name, "errs") == 0 || 100012154Ssam (howmuch > 1 && strcmp(name, "a.out") == 0)) 100112154Ssam return (0); 100212154Ssam /* SHOULD CHECK IF IT IS EXECUTABLE */ 100312154Ssam return (1); 100412154Ssam } 100512154Ssam 100622688Slepreau /* Is the current file a new file, or the newest one of the same name? */ 10071119Sbill checkupdate(arg) 10086250Sroot char *arg; 10091119Sbill { 10101119Sbill char name[100]; 10116250Sroot long mtime; 10121119Sbill daddr_t seekp; 10131119Sbill daddr_t lookup(); 10141119Sbill 10151119Sbill rewind(tfile); 10161119Sbill for (;;) { 10171119Sbill if ((seekp = lookup(arg)) < 0) 10186250Sroot return (1); 10191119Sbill fseek(tfile, seekp, 0); 10201119Sbill fscanf(tfile, "%s %lo", name, &mtime); 10216250Sroot return (stbuf.st_mtime > mtime); 10221119Sbill } 10231119Sbill } 10241119Sbill 10251119Sbill done(n) 10261119Sbill { 102721457Skjd unlink(tname); 10281119Sbill exit(n); 10291119Sbill } 10301119Sbill 103127445Slepreau /* 103227445Slepreau * Do we want the next entry on the tape, i.e. is it selected? If 103327445Slepreau * not, skip over the entire entry. Return -1 if reached end of tape. 103427445Slepreau */ 103527445Slepreau wantit(argv) 103627445Slepreau char *argv[]; 103727445Slepreau { 103827445Slepreau register char **cp; 103927445Slepreau 104027445Slepreau getdir(); 104127445Slepreau if (endtape()) 104227445Slepreau return (-1); 104327445Slepreau if (*argv == 0) 104427445Slepreau return (1); 104527445Slepreau for (cp = argv; *cp; cp++) 104627445Slepreau if (prefix(*cp, dblock.dbuf.name)) 104727445Slepreau return (1); 104827445Slepreau passtape(); 104927445Slepreau return (0); 105027445Slepreau } 105127445Slepreau 105222688Slepreau /* 105322688Slepreau * Does s2 begin with the string s1, on a directory boundary? 105422688Slepreau */ 10551119Sbill prefix(s1, s2) 10566250Sroot register char *s1, *s2; 10571119Sbill { 10581119Sbill while (*s1) 10591119Sbill if (*s1++ != *s2++) 10606250Sroot return (0); 10611119Sbill if (*s2) 10626250Sroot return (*s2 == '/'); 10636250Sroot return (1); 10641119Sbill } 10651119Sbill 10661119Sbill #define N 200 10671119Sbill int njab; 10686250Sroot 10691119Sbill daddr_t 10701119Sbill lookup(s) 10716250Sroot char *s; 10721119Sbill { 10731119Sbill register i; 10741119Sbill daddr_t a; 10751119Sbill 10761119Sbill for(i=0; s[i]; i++) 10776250Sroot if (s[i] == ' ') 10781119Sbill break; 10791119Sbill a = bsrch(s, i, low, high); 10806250Sroot return (a); 10811119Sbill } 10821119Sbill 10831119Sbill daddr_t 10841119Sbill bsrch(s, n, l, h) 10856250Sroot daddr_t l, h; 10866250Sroot char *s; 10871119Sbill { 10881119Sbill register i, j; 10891119Sbill char b[N]; 10901119Sbill daddr_t m, m1; 10911119Sbill 10921119Sbill njab = 0; 10931119Sbill 10941119Sbill loop: 10956250Sroot if (l >= h) 109622688Slepreau return ((daddr_t) -1); 10971119Sbill m = l + (h-l)/2 - N/2; 10986250Sroot if (m < l) 10991119Sbill m = l; 11001119Sbill fseek(tfile, m, 0); 11011119Sbill fread(b, 1, N, tfile); 11021119Sbill njab++; 11031119Sbill for(i=0; i<N; i++) { 11046250Sroot if (b[i] == '\n') 11051119Sbill break; 11061119Sbill m++; 11071119Sbill } 11086250Sroot if (m >= h) 110922688Slepreau return ((daddr_t) -1); 11101119Sbill m1 = m; 11111119Sbill j = i; 11121119Sbill for(i++; i<N; i++) { 11131119Sbill m1++; 11146250Sroot if (b[i] == '\n') 11151119Sbill break; 11161119Sbill } 11171119Sbill i = cmp(b+j, s, n); 11186250Sroot if (i < 0) { 11191119Sbill h = m; 11201119Sbill goto loop; 11211119Sbill } 11226250Sroot if (i > 0) { 11231119Sbill l = m1; 11241119Sbill goto loop; 11251119Sbill } 11266250Sroot return (m); 11271119Sbill } 11281119Sbill 11291119Sbill cmp(b, s, n) 11306250Sroot char *b, *s; 11311119Sbill { 11321119Sbill register i; 11331119Sbill 11346250Sroot if (b[0] != '\n') 113521457Skjd exit(2); 11361119Sbill for(i=0; i<n; i++) { 11376250Sroot if (b[i+1] > s[i]) 11386250Sroot return (-1); 11396250Sroot if (b[i+1] < s[i]) 11406250Sroot return (1); 11411119Sbill } 11426250Sroot return (b[i+1] == ' '? 0 : -1); 11431119Sbill } 11441119Sbill 114522688Slepreau readtape(buffer) 11466250Sroot char *buffer; 11471119Sbill { 114821457Skjd char *bufp; 114921457Skjd 115022688Slepreau if (first == 0) 115122688Slepreau getbuf(); 115227445Slepreau (void) readtbuf(&bufp, TBLOCK); 115321457Skjd bcopy(bufp, buffer, TBLOCK); 115421457Skjd return(TBLOCK); 115521457Skjd } 115621457Skjd 115721457Skjd readtbuf(bufpp, size) 115821457Skjd char **bufpp; 115921457Skjd int size; 116021457Skjd { 11613457Swnj register int i; 11621119Sbill 11631119Sbill if (recno >= nblock || first == 0) { 116427445Slepreau if ((i = bread(mt, (char *)tbuf, TBLOCK*nblock)) < 0) 116527058Smckusick mterr("read", i, 3); 11661119Sbill if (first == 0) { 11671119Sbill if ((i % TBLOCK) != 0) { 116813492Ssam fprintf(stderr, "tar: tape blocksize error\n"); 11691119Sbill done(3); 11701119Sbill } 11711119Sbill i /= TBLOCK; 11723457Swnj if (i != nblock) { 117313492Ssam fprintf(stderr, "tar: blocksize = %d\n", i); 11741119Sbill nblock = i; 11751119Sbill } 117622353Skjd first = 1; 11771119Sbill } 11781119Sbill recno = 0; 11791119Sbill } 118021457Skjd if (size > ((nblock-recno)*TBLOCK)) 118121457Skjd size = (nblock-recno)*TBLOCK; 118221457Skjd *bufpp = (char *)&tbuf[recno]; 118321457Skjd recno += (size/TBLOCK); 118421457Skjd return (size); 11851119Sbill } 11861119Sbill 118721457Skjd writetbuf(buffer, n) 118821457Skjd register char *buffer; 118921457Skjd register int n; 11901119Sbill { 119127058Smckusick int i; 119222688Slepreau 119322688Slepreau if (first == 0) { 119422353Skjd getbuf(); 119522353Skjd first = 1; 119622353Skjd } 11971119Sbill if (recno >= nblock) { 119827445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock); 119927058Smckusick if (i != TBLOCK*nblock) 120027058Smckusick mterr("write", i, 2); 12011119Sbill recno = 0; 12021119Sbill } 120321457Skjd 120421457Skjd /* 120521457Skjd * Special case: We have an empty tape buffer, and the 120621457Skjd * users data size is >= the tape block size: Avoid 120721457Skjd * the bcopy and dma direct to tape. BIG WIN. Add the 120821457Skjd * residual to the tape buffer. 120921457Skjd */ 121021457Skjd while (recno == 0 && n >= nblock) { 121127058Smckusick i = write(mt, buffer, TBLOCK*nblock); 121227058Smckusick if (i != TBLOCK*nblock) 121327058Smckusick mterr("write", i, 2); 121421457Skjd n -= nblock; 121521457Skjd buffer += (nblock * TBLOCK); 12161119Sbill } 121721457Skjd 121821457Skjd while (n-- > 0) { 121921457Skjd bcopy(buffer, (char *)&tbuf[recno++], TBLOCK); 122021457Skjd buffer += TBLOCK; 122121457Skjd if (recno >= nblock) { 122227445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock); 122327058Smckusick if (i != TBLOCK*nblock) 122427058Smckusick mterr("write", i, 2); 122521457Skjd recno = 0; 122621457Skjd } 122721457Skjd } 122821457Skjd 122921457Skjd /* Tell the user how much to write to get in sync */ 123021457Skjd return (nblock - recno); 12311119Sbill } 12321119Sbill 12331119Sbill backtape() 12341119Sbill { 123527058Smckusick static int mtdev = 1; 12363457Swnj static struct mtop mtop = {MTBSR, 1}; 123727058Smckusick struct mtget mtget; 123827058Smckusick 123927058Smckusick if (mtdev == 1) 124027445Slepreau mtdev = ioctl(mt, MTIOCGET, (char *)&mtget); 12413457Swnj if (mtdev == 0) { 124227445Slepreau if (ioctl(mt, MTIOCTOP, (char *)&mtop) < 0) { 124327058Smckusick fprintf(stderr, "tar: tape backspace error: "); 124427058Smckusick perror(""); 12451119Sbill done(4); 12461119Sbill } 12473457Swnj } else 124822688Slepreau lseek(mt, (daddr_t) -TBLOCK*nblock, 1); 12493457Swnj recno--; 12501119Sbill } 12511119Sbill 12521119Sbill flushtape() 12531119Sbill { 125427058Smckusick int i; 125527058Smckusick 125627445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock); 125727058Smckusick if (i != TBLOCK*nblock) 125827058Smckusick mterr("write", i, 2); 12591119Sbill } 12601119Sbill 126127058Smckusick mterr(operation, i, exitcode) 126227058Smckusick char *operation; 126327058Smckusick int i; 126427058Smckusick { 126527058Smckusick fprintf(stderr, "tar: tape %s error: ", operation); 126627058Smckusick if (i < 0) 126727058Smckusick perror(""); 126827058Smckusick else 126927058Smckusick fprintf(stderr, "unexpected EOF\n"); 127027058Smckusick done(exitcode); 127127058Smckusick } 127227058Smckusick 12738737Smckusick bread(fd, buf, size) 12748737Smckusick int fd; 12758737Smckusick char *buf; 12768737Smckusick int size; 12778737Smckusick { 12788737Smckusick int count; 12798737Smckusick static int lastread = 0; 12808737Smckusick 128122688Slepreau if (!Bflag) 128222688Slepreau return (read(fd, buf, size)); 128322353Skjd 12848737Smckusick for (count = 0; count < size; count += lastread) { 128524281Smckusick lastread = read(fd, buf, size - count); 128624281Smckusick if (lastread <= 0) { 12878737Smckusick if (count > 0) 12888737Smckusick return (count); 12898737Smckusick return (lastread); 12908737Smckusick } 12918737Smckusick buf += lastread; 12928737Smckusick } 12938737Smckusick return (count); 12948737Smckusick } 129510165Ssam 129610165Ssam char * 129710165Ssam getcwd(buf) 129810165Ssam char *buf; 129910165Ssam { 130010165Ssam if (getwd(buf) == NULL) { 130110165Ssam fprintf(stderr, "tar: %s\n", buf); 130221457Skjd exit(1); 130310165Ssam } 130410165Ssam return (buf); 130510165Ssam } 130621910Skjd 130721910Skjd getbuf() 130821910Skjd { 130922353Skjd 131027058Smckusick if (nblock == 0) { 131121910Skjd fstat(mt, &stbuf); 131221910Skjd if ((stbuf.st_mode & S_IFMT) == S_IFCHR) 131327058Smckusick nblock = NBLOCK; 131421910Skjd else { 131527058Smckusick nblock = stbuf.st_blksize / TBLOCK; 131627058Smckusick if (nblock == 0) 131727058Smckusick nblock = NBLOCK; 131821910Skjd } 131921910Skjd } 132027445Slepreau tbuf = (union hblock *)malloc((unsigned)nblock*TBLOCK); 132121910Skjd if (tbuf == NULL) { 132221910Skjd fprintf(stderr, "tar: blocksize %d too big, can't get memory\n", 132321910Skjd nblock); 132421910Skjd done(1); 132521910Skjd } 132621910Skjd } 132722353Skjd 132822688Slepreau /* 132927442Slepreau * Save this directory and its mtime on the stack, popping and setting 133027442Slepreau * the mtimes of any stacked dirs which aren't parents of this one. 133127442Slepreau * A null directory causes the entire stack to be unwound and set. 133222688Slepreau * 133327442Slepreau * Since all the elements of the directory "stack" share a common 133427442Slepreau * prefix, we can make do with one string. We keep only the current 133527442Slepreau * directory path, with an associated array of mtime's, one for each 133627442Slepreau * '/' in the path. A negative mtime means no mtime. The mtime's are 133727442Slepreau * offset by one (first index 1, not 0) because calling this with a null 133827442Slepreau * directory causes mtime[0] to be set. 133927442Slepreau * 134022688Slepreau * This stack algorithm is not guaranteed to work for tapes created 134122688Slepreau * with the 'r' option, but the vast majority of tapes with 134222688Slepreau * directories are not. This avoids saving every directory record on 134322688Slepreau * the tape and setting all the times at the end. 134422688Slepreau */ 134522688Slepreau char dirstack[NAMSIZ]; 134622688Slepreau #define NTIM (NAMSIZ/2+1) /* a/b/c/d/... */ 134722688Slepreau time_t mtime[NTIM]; 134822353Skjd 134922688Slepreau dodirtimes(hp) 135022688Slepreau union hblock *hp; 135122353Skjd { 135222688Slepreau register char *p = dirstack; 135322688Slepreau register char *q = hp->dbuf.name; 135422688Slepreau register int ndir = 0; 135522688Slepreau char *savp; 135622688Slepreau int savndir; 135722353Skjd 135822688Slepreau /* Find common prefix */ 1359*30090Sbostic while (*p == *q && *p) { 136022688Slepreau if (*p++ == '/') 136122688Slepreau ++ndir; 136222688Slepreau q++; 136322688Slepreau } 136422353Skjd 136522688Slepreau savp = p; 136622688Slepreau savndir = ndir; 136722688Slepreau while (*p) { 136822688Slepreau /* 136922688Slepreau * Not a child: unwind the stack, setting the times. 137022688Slepreau * The order we do this doesn't matter, so we go "forward." 137122688Slepreau */ 137222688Slepreau if (*p++ == '/') 137322688Slepreau if (mtime[++ndir] >= 0) { 137422688Slepreau *--p = '\0'; /* zap the slash */ 137522688Slepreau setimes(dirstack, mtime[ndir]); 137622688Slepreau *p++ = '/'; 137722688Slepreau } 137822688Slepreau } 137922688Slepreau p = savp; 138022688Slepreau ndir = savndir; 138122353Skjd 138222688Slepreau /* Push this one on the "stack" */ 138322688Slepreau while (*p = *q++) /* append the rest of the new dir */ 138422688Slepreau if (*p++ == '/') 138522688Slepreau mtime[++ndir] = -1; 138622688Slepreau mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */ 138722688Slepreau } 138822353Skjd 138922688Slepreau setimes(path, mt) 139022688Slepreau char *path; 139122688Slepreau time_t mt; 139222688Slepreau { 139322688Slepreau struct timeval tv[2]; 139422353Skjd 139522688Slepreau tv[0].tv_sec = time((time_t *) 0); 139622688Slepreau tv[1].tv_sec = mt; 139722688Slepreau tv[0].tv_usec = tv[1].tv_usec = 0; 139827058Smckusick if (utimes(path, tv) < 0) { 139927058Smckusick fprintf(stderr, "tar: can't set time on %s: ", path); 140027058Smckusick perror(""); 140127058Smckusick } 140222688Slepreau } 140322688Slepreau 140422688Slepreau char * 140522688Slepreau getmem(size) 140622688Slepreau { 140722688Slepreau char *p = malloc((unsigned) size); 140822688Slepreau 140922688Slepreau if (p == NULL && freemem) { 141022688Slepreau fprintf(stderr, 141122688Slepreau "tar: out of memory, link and directory modtime info lost\n"); 141222688Slepreau freemem = 0; 141322353Skjd } 141422688Slepreau return (p); 141522353Skjd } 1416