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"; 1133082Sbostic #endif /* not lint */ 126250Sroot 1322502Sdist #ifndef lint 14*34429Sbostic static char sccsid[] = "@(#)tar.c 5.12 (Berkeley) 05/23/88"; 1533082Sbostic #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; 68*34429Sbostic int sflag; 696250Sroot int xflag; 706250Sroot int vflag; 716250Sroot int tflag; 726250Sroot int cflag; 736250Sroot int mflag; 746250Sroot int fflag; 7512154Ssam int iflag; 766250Sroot int oflag; 776250Sroot int pflag; 786250Sroot int wflag; 796250Sroot int hflag; 808737Smckusick int Bflag; 8112154Ssam int Fflag; 826250Sroot 836250Sroot int mt; 846250Sroot int term; 856250Sroot int chksum; 866250Sroot int recno; 8722688Slepreau int first; 8822688Slepreau int prtlinkerr; 891119Sbill int freemem = 1; 9022353Skjd int nblock = 0; 916250Sroot int onintr(); 926250Sroot int onquit(); 936250Sroot int onhup(); 9422688Slepreau #ifdef notdef 956250Sroot int onterm(); 9622688Slepreau #endif 971119Sbill 981119Sbill daddr_t low; 991119Sbill daddr_t high; 1006250Sroot daddr_t bsrch(); 1011119Sbill 10221910Skjd FILE *vfile = stdout; 1031119Sbill FILE *tfile; 1041119Sbill char tname[] = "/tmp/tarXXXXXX"; 1051119Sbill char *usefile; 1066250Sroot char magtape[] = "/dev/rmt8"; 1071119Sbill char *malloc(); 10827445Slepreau long time(); 10927445Slepreau off_t lseek(); 11027445Slepreau char *mktemp(); 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 172*34429Sbostic case 's': 173*34429Sbostic sflag++; 174*34429Sbostic break; 175*34429Sbostic 1761119Sbill case 'v': 1771119Sbill vflag++; 1781119Sbill break; 1796250Sroot 1801119Sbill case 'w': 1811119Sbill wflag++; 1821119Sbill break; 1836250Sroot 1841119Sbill case 'x': 1851119Sbill xflag++; 1861119Sbill break; 1876250Sroot 1881119Sbill case 't': 1891119Sbill tflag++; 1901119Sbill break; 1916250Sroot 1921119Sbill case 'm': 1931119Sbill mflag++; 1941119Sbill break; 1956250Sroot 1961119Sbill case '-': 1971119Sbill break; 1986250Sroot 1991119Sbill case '0': 2001119Sbill case '1': 2011119Sbill case '4': 2021119Sbill case '5': 20321457Skjd case '7': 2041119Sbill case '8': 2051119Sbill magtape[8] = *cp; 2061119Sbill usefile = magtape; 2071119Sbill break; 2086250Sroot 2091119Sbill case 'b': 21013492Ssam if (*argv == 0) { 21113492Ssam fprintf(stderr, 21213492Ssam "tar: blocksize must be specified with 'b' option\n"); 21313492Ssam usage(); 21413492Ssam } 21513492Ssam nblock = atoi(*argv); 21613492Ssam if (nblock <= 0) { 21713492Ssam fprintf(stderr, 21813492Ssam "tar: invalid blocksize \"%s\"\n", *argv); 2191119Sbill done(1); 2201119Sbill } 22113492Ssam argv++; 2221119Sbill break; 2236250Sroot 2241119Sbill case 'l': 22522688Slepreau prtlinkerr++; 2261119Sbill break; 2276250Sroot 2286250Sroot case 'h': 2296250Sroot hflag++; 2306250Sroot break; 2316250Sroot 23212154Ssam case 'i': 23312154Ssam iflag++; 23412154Ssam break; 23512154Ssam 2368737Smckusick case 'B': 2378737Smckusick Bflag++; 2388737Smckusick break; 2398737Smckusick 24012154Ssam case 'F': 24112154Ssam Fflag++; 24212154Ssam break; 24312154Ssam 2441119Sbill default: 2451119Sbill fprintf(stderr, "tar: %c: unknown option\n", *cp); 2461119Sbill usage(); 2471119Sbill } 2481119Sbill 2496250Sroot if (!rflag && !xflag && !tflag) 2506250Sroot usage(); 2511119Sbill if (rflag) { 2526250Sroot if (cflag && tfile != NULL) 2531119Sbill usage(); 2541119Sbill if (signal(SIGINT, SIG_IGN) != SIG_IGN) 25527445Slepreau (void) signal(SIGINT, onintr); 2561119Sbill if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 25727445Slepreau (void) signal(SIGHUP, onhup); 2581119Sbill if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) 25927445Slepreau (void) signal(SIGQUIT, onquit); 2606250Sroot #ifdef notdef 2611119Sbill if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 26227445Slepreau (void) signal(SIGTERM, onterm); 2636250Sroot #endif 26427058Smckusick mt = openmt(usefile, 1); 2651119Sbill dorep(argv); 2666250Sroot done(0); 2671119Sbill } 26827058Smckusick mt = openmt(usefile, 0); 2696250Sroot if (xflag) 2701119Sbill doxtract(argv); 2716250Sroot else 27227445Slepreau dotable(argv); 2731119Sbill done(0); 2741119Sbill } 2751119Sbill 2761119Sbill usage() 2771119Sbill { 2786250Sroot fprintf(stderr, 27921457Skjd "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n"); 2801119Sbill done(1); 2811119Sbill } 2821119Sbill 28327058Smckusick int 28427058Smckusick openmt(tape, writing) 28527058Smckusick char *tape; 28627058Smckusick int writing; 28727058Smckusick { 28827058Smckusick 28927058Smckusick if (strcmp(tape, "-") == 0) { 29027058Smckusick /* 29127058Smckusick * Read from standard input or write to standard output. 29227058Smckusick */ 29327058Smckusick if (writing) { 29427058Smckusick if (cflag == 0) { 29527058Smckusick fprintf(stderr, 29627058Smckusick "tar: can only create standard output archives\n"); 29727058Smckusick done(1); 29827058Smckusick } 29927058Smckusick vfile = stderr; 30027058Smckusick setlinebuf(vfile); 30127058Smckusick mt = dup(1); 30227058Smckusick } else { 30327058Smckusick mt = dup(0); 30427058Smckusick Bflag++; 30527058Smckusick } 30627058Smckusick } else { 30727058Smckusick /* 30827058Smckusick * Use file or tape on local machine. 30927058Smckusick */ 31027058Smckusick if (writing) { 31127058Smckusick if (cflag) 31227445Slepreau mt = open(tape, O_RDWR|O_CREAT|O_TRUNC, 0666); 31327058Smckusick else 31427058Smckusick mt = open(tape, O_RDWR); 31527058Smckusick } else 31627058Smckusick mt = open(tape, O_RDONLY); 31727058Smckusick if (mt < 0) { 31827058Smckusick fprintf(stderr, "tar: "); 31927058Smckusick perror(tape); 32027058Smckusick done(1); 32127058Smckusick } 32227058Smckusick } 32327058Smckusick return(mt); 32427058Smckusick } 32527058Smckusick 3261119Sbill dorep(argv) 3276250Sroot char *argv[]; 3281119Sbill { 3291119Sbill register char *cp, *cp2; 3309601Ssam char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent; 3311119Sbill 3321119Sbill if (!cflag) { 3331119Sbill getdir(); 3341119Sbill do { 3351119Sbill passtape(); 3361119Sbill if (term) 3371119Sbill done(0); 3381119Sbill getdir(); 3391119Sbill } while (!endtape()); 34013492Ssam backtape(); 3411119Sbill if (tfile != NULL) { 3421119Sbill char buf[200]; 3431119Sbill 34432420Sbostic (void)sprintf(buf, 3456250Sroot "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s", 3461119Sbill tname, tname, tname, tname, tname, tname); 3471119Sbill fflush(tfile); 3481119Sbill system(buf); 3491119Sbill freopen(tname, "r", tfile); 3501119Sbill fstat(fileno(tfile), &stbuf); 3511119Sbill high = stbuf.st_size; 3521119Sbill } 3531119Sbill } 3541119Sbill 35510165Ssam (void) getcwd(wdir); 3561119Sbill while (*argv && ! term) { 3571119Sbill cp2 = *argv; 3581119Sbill if (!strcmp(cp2, "-C") && argv[1]) { 3591119Sbill argv++; 36027058Smckusick if (chdir(*argv) < 0) { 36127058Smckusick fprintf(stderr, "tar: can't change directories to "); 3621119Sbill perror(*argv); 36327058Smckusick } else 36410165Ssam (void) getcwd(wdir); 3651119Sbill argv++; 3661119Sbill continue; 3671119Sbill } 3689601Ssam parent = wdir; 3691119Sbill for (cp = *argv; *cp; cp++) 3701119Sbill if (*cp == '/') 3711119Sbill cp2 = cp; 3721119Sbill if (cp2 != *argv) { 3731119Sbill *cp2 = '\0'; 3749601Ssam if (chdir(*argv) < 0) { 37527058Smckusick fprintf(stderr, "tar: can't change directories to "); 3769601Ssam perror(*argv); 3779601Ssam continue; 3789601Ssam } 37910165Ssam parent = getcwd(tempdir); 3801119Sbill *cp2 = '/'; 3811119Sbill cp2++; 3821119Sbill } 3839601Ssam putfile(*argv++, cp2, parent); 38415045Smckusick if (chdir(wdir) < 0) { 38522688Slepreau fprintf(stderr, "tar: cannot change back?: "); 38615045Smckusick perror(wdir); 38715045Smckusick } 3881119Sbill } 3891119Sbill putempty(); 3901119Sbill putempty(); 3911119Sbill flushtape(); 39222688Slepreau if (prtlinkerr == 0) 3936250Sroot return; 3946250Sroot for (; ihead != NULL; ihead = ihead->nextp) { 3956250Sroot if (ihead->count == 0) 3966250Sroot continue; 39713492Ssam fprintf(stderr, "tar: missing links to %s\n", ihead->pathname); 3986250Sroot } 3991119Sbill } 4001119Sbill 4011119Sbill endtape() 4021119Sbill { 40321457Skjd return (dblock.dbuf.name[0] == '\0'); 4041119Sbill } 4051119Sbill 4061119Sbill getdir() 4071119Sbill { 4081119Sbill register struct stat *sp; 4091119Sbill int i; 4101119Sbill 41112154Ssam top: 4126250Sroot readtape((char *)&dblock); 4131119Sbill if (dblock.dbuf.name[0] == '\0') 4141119Sbill return; 4151119Sbill sp = &stbuf; 4161119Sbill sscanf(dblock.dbuf.mode, "%o", &i); 4171119Sbill sp->st_mode = i; 4181119Sbill sscanf(dblock.dbuf.uid, "%o", &i); 4191119Sbill sp->st_uid = i; 4201119Sbill sscanf(dblock.dbuf.gid, "%o", &i); 4211119Sbill sp->st_gid = i; 4221119Sbill sscanf(dblock.dbuf.size, "%lo", &sp->st_size); 4231119Sbill sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime); 4241119Sbill sscanf(dblock.dbuf.chksum, "%o", &chksum); 42512154Ssam if (chksum != (i = checksum())) { 42613492Ssam fprintf(stderr, "tar: directory checksum error (%d != %d)\n", 42712154Ssam chksum, i); 42812154Ssam if (iflag) 42912154Ssam goto top; 4301119Sbill done(2); 4311119Sbill } 432*34429Sbostic /* strip off leading "/" if present */ 433*34429Sbostic if (sflag && dblock.dbuf.name[0] == '/') { 434*34429Sbostic register char *cp1, *cp2; 435*34429Sbostic for (cp1 = cp2 = dblock.dbuf.name; *cp2 && *cp2 == '/'; ++cp2); 436*34429Sbostic if (!*cp2) 437*34429Sbostic goto top; 438*34429Sbostic while (*cp1++ = *cp2++); 439*34429Sbostic } 4401119Sbill if (tfile != NULL) 4411119Sbill fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime); 4421119Sbill } 4431119Sbill 4441119Sbill passtape() 4451119Sbill { 4461119Sbill long blocks; 44721457Skjd char *bufp; 4481119Sbill 4491119Sbill if (dblock.dbuf.linkflag == '1') 4501119Sbill return; 4511119Sbill blocks = stbuf.st_size; 4521119Sbill blocks += TBLOCK-1; 4531119Sbill blocks /= TBLOCK; 4541119Sbill 45521457Skjd while (blocks-- > 0) 45627445Slepreau (void) readtbuf(&bufp, TBLOCK); 4571119Sbill } 4581119Sbill 4599601Ssam putfile(longname, shortname, parent) 4606250Sroot char *longname; 4616250Sroot char *shortname; 4629601Ssam char *parent; 4631119Sbill { 46421457Skjd int infile = 0; 46521457Skjd long blocks; 4661119Sbill char buf[TBLOCK]; 46721457Skjd char *bigbuf; 46822688Slepreau register char *cp; 4695931Smckusic struct direct *dp; 4705931Smckusic DIR *dirp; 47127445Slepreau register int i; 47227445Slepreau long l; 4739601Ssam char newparent[NAMSIZ+64]; 47421457Skjd int maxread; 47521457Skjd int hint; /* amount to write to get "in sync" */ 4761119Sbill 4779601Ssam if (!hflag) 47812154Ssam i = lstat(shortname, &stbuf); 47912154Ssam else 48012154Ssam i = stat(shortname, &stbuf); 48112154Ssam if (i < 0) { 48227058Smckusick fprintf(stderr, "tar: "); 48327058Smckusick perror(longname); 4849601Ssam return; 4859601Ssam } 48612154Ssam if (tfile != NULL && checkupdate(longname) == 0) 4871119Sbill return; 48812154Ssam if (checkw('r', longname) == 0) 4891119Sbill return; 49012154Ssam if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0) 49112154Ssam return; 4921119Sbill 49312154Ssam switch (stbuf.st_mode & S_IFMT) { 49412154Ssam case S_IFDIR: 4956250Sroot for (i = 0, cp = buf; *cp++ = longname[i++];) 4966250Sroot ; 4971119Sbill *--cp = '/'; 4981119Sbill *++cp = 0 ; 4991119Sbill if (!oflag) { 5006250Sroot if ((cp - buf) >= NAMSIZ) { 50113492Ssam fprintf(stderr, "tar: %s: file name too long\n", 50213492Ssam longname); 5036250Sroot return; 5046250Sroot } 5056250Sroot stbuf.st_size = 0; 5066250Sroot tomodes(&stbuf); 5076250Sroot strcpy(dblock.dbuf.name,buf); 50832420Sbostic (void)sprintf(dblock.dbuf.chksum, "%6o", checksum()); 50927445Slepreau (void) writetape((char *)&dblock); 5101119Sbill } 51132420Sbostic (void)sprintf(newparent, "%s/%s", parent, shortname); 51215045Smckusick if (chdir(shortname) < 0) { 51315045Smckusick perror(shortname); 51415045Smckusick return; 51515045Smckusick } 5165931Smckusic if ((dirp = opendir(".")) == NULL) { 51713492Ssam fprintf(stderr, "tar: %s: directory read error\n", 51813492Ssam longname); 51915045Smckusick if (chdir(parent) < 0) { 52022688Slepreau fprintf(stderr, "tar: cannot change back?: "); 52115045Smckusick perror(parent); 52215045Smckusick } 5235931Smckusic return; 5245931Smckusic } 5255931Smckusic while ((dp = readdir(dirp)) != NULL && !term) { 5266250Sroot if (!strcmp(".", dp->d_name) || 5276250Sroot !strcmp("..", dp->d_name)) 5281119Sbill continue; 5295931Smckusic strcpy(cp, dp->d_name); 53027445Slepreau l = telldir(dirp); 5315931Smckusic closedir(dirp); 5329601Ssam putfile(buf, cp, newparent); 5335931Smckusic dirp = opendir("."); 53427445Slepreau seekdir(dirp, l); 5351119Sbill } 5365931Smckusic closedir(dirp); 53715045Smckusick if (chdir(parent) < 0) { 53822688Slepreau fprintf(stderr, "tar: cannot change back?: "); 53915045Smckusick perror(parent); 54015045Smckusick } 54112154Ssam break; 54212154Ssam 54312154Ssam case S_IFLNK: 54412154Ssam tomodes(&stbuf); 54512154Ssam if (strlen(longname) >= NAMSIZ) { 54613492Ssam fprintf(stderr, "tar: %s: file name too long\n", 54713492Ssam longname); 54812154Ssam return; 54912154Ssam } 55012154Ssam strcpy(dblock.dbuf.name, longname); 5516250Sroot if (stbuf.st_size + 1 >= NAMSIZ) { 55213492Ssam fprintf(stderr, "tar: %s: symbolic link too long\n", 55313492Ssam longname); 5546250Sroot return; 5556250Sroot } 5569601Ssam i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1); 5576250Sroot if (i < 0) { 55827058Smckusick fprintf(stderr, "tar: can't read symbolic link "); 5599601Ssam perror(longname); 5606250Sroot return; 5616250Sroot } 5626250Sroot dblock.dbuf.linkname[i] = '\0'; 5636250Sroot dblock.dbuf.linkflag = '2'; 56422688Slepreau if (vflag) 56522688Slepreau fprintf(vfile, "a %s symbolic link to %s\n", 56622688Slepreau longname, dblock.dbuf.linkname); 56732420Sbostic (void)sprintf(dblock.dbuf.size, "%11lo", 0L); 56832420Sbostic (void)sprintf(dblock.dbuf.chksum, "%6o", checksum()); 56927445Slepreau (void) writetape((char *)&dblock); 57012154Ssam break; 5711119Sbill 57212154Ssam case S_IFREG: 57312154Ssam if ((infile = open(shortname, 0)) < 0) { 57427058Smckusick fprintf(stderr, "tar: "); 57527058Smckusick perror(longname); 5761119Sbill return; 5771119Sbill } 57812154Ssam tomodes(&stbuf); 57912154Ssam if (strlen(longname) >= NAMSIZ) { 58013492Ssam fprintf(stderr, "tar: %s: file name too long\n", 58113492Ssam longname); 58221457Skjd close(infile); 58312154Ssam return; 58412154Ssam } 58512154Ssam strcpy(dblock.dbuf.name, longname); 58612154Ssam if (stbuf.st_nlink > 1) { 58712154Ssam struct linkbuf *lp; 58812154Ssam int found = 0; 58912154Ssam 59012154Ssam for (lp = ihead; lp != NULL; lp = lp->nextp) 59112154Ssam if (lp->inum == stbuf.st_ino && 59212154Ssam lp->devnum == stbuf.st_dev) { 59312154Ssam found++; 59412154Ssam break; 59512154Ssam } 59612154Ssam if (found) { 59712154Ssam strcpy(dblock.dbuf.linkname, lp->pathname); 59812154Ssam dblock.dbuf.linkflag = '1'; 59932420Sbostic (void)sprintf(dblock.dbuf.chksum, "%6o", checksum()); 60027445Slepreau (void) writetape( (char *) &dblock); 60122688Slepreau if (vflag) 60222688Slepreau fprintf(vfile, "a %s link to %s\n", 60322688Slepreau longname, lp->pathname); 60412154Ssam lp->count--; 60512154Ssam close(infile); 60612154Ssam return; 6071119Sbill } 60822688Slepreau lp = (struct linkbuf *) getmem(sizeof(*lp)); 60922688Slepreau if (lp != NULL) { 61012154Ssam lp->nextp = ihead; 61112154Ssam ihead = lp; 61212154Ssam lp->inum = stbuf.st_ino; 61312154Ssam lp->devnum = stbuf.st_dev; 61412154Ssam lp->count = stbuf.st_nlink - 1; 61512154Ssam strcpy(lp->pathname, longname); 61612154Ssam } 6171119Sbill } 61821457Skjd blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK; 61922688Slepreau if (vflag) 62022688Slepreau fprintf(vfile, "a %s %ld blocks\n", longname, blocks); 62132420Sbostic (void)sprintf(dblock.dbuf.chksum, "%6o", checksum()); 62221457Skjd hint = writetape((char *)&dblock); 62321457Skjd maxread = max(stbuf.st_blksize, (nblock * TBLOCK)); 62427445Slepreau if ((bigbuf = malloc((unsigned)maxread)) == 0) { 62521457Skjd maxread = TBLOCK; 62621457Skjd bigbuf = buf; 62721457Skjd } 6281119Sbill 62921457Skjd while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0 63021457Skjd && blocks > 0) { 63121457Skjd register int nblks; 63221457Skjd 63321457Skjd nblks = ((i-1)/TBLOCK)+1; 63421457Skjd if (nblks > blocks) 63521457Skjd nblks = blocks; 63621457Skjd hint = writetbuf(bigbuf, nblks); 63721457Skjd blocks -= nblks; 63821457Skjd } 63921457Skjd close(infile); 64021457Skjd if (bigbuf != buf) 64121457Skjd free(bigbuf); 64227058Smckusick if (i < 0) { 64327445Slepreau fprintf(stderr, "tar: Read error on "); 64427058Smckusick perror(longname); 64527058Smckusick } else if (blocks != 0 || i != 0) 64613492Ssam fprintf(stderr, "tar: %s: file changed size\n", 64713492Ssam longname); 64812154Ssam while (--blocks >= 0) 64912154Ssam putempty(); 65012154Ssam break; 65112154Ssam 65212154Ssam default: 65312154Ssam fprintf(stderr, "tar: %s is not a file. Not dumped\n", 65413492Ssam longname); 65512154Ssam break; 6561119Sbill } 6571119Sbill } 6581119Sbill 6591119Sbill doxtract(argv) 6606250Sroot char *argv[]; 6611119Sbill { 66233082Sbostic extern int errno; 6631119Sbill long blocks, bytes; 66427445Slepreau int ofile, i; 6651119Sbill 6661119Sbill for (;;) { 66727445Slepreau if ((i = wantit(argv)) == 0) 66827445Slepreau continue; 66927445Slepreau if (i == -1) 67027445Slepreau break; /* end of tape */ 6711119Sbill if (checkw('x', dblock.dbuf.name) == 0) { 6721119Sbill passtape(); 6731119Sbill continue; 6741119Sbill } 67512154Ssam if (Fflag) { 67612154Ssam char *s; 67712154Ssam 67812154Ssam if ((s = rindex(dblock.dbuf.name, '/')) == 0) 67912154Ssam s = dblock.dbuf.name; 68012154Ssam else 68112154Ssam s++; 68212154Ssam if (checkf(s, stbuf.st_mode, Fflag) == 0) { 68312154Ssam passtape(); 68412154Ssam continue; 68512154Ssam } 68612154Ssam } 68722688Slepreau if (checkdir(dblock.dbuf.name)) { /* have a directory */ 68822688Slepreau if (mflag == 0) 68922688Slepreau dodirtimes(&dblock); 6906250Sroot continue; 69122688Slepreau } 69222688Slepreau if (dblock.dbuf.linkflag == '2') { /* symlink */ 69325250Sbloom /* 69425250Sbloom * only unlink non directories or empty 69525250Sbloom * directories 69625250Sbloom */ 69725250Sbloom if (rmdir(dblock.dbuf.name) < 0) { 69825250Sbloom if (errno == ENOTDIR) 69925250Sbloom unlink(dblock.dbuf.name); 70025250Sbloom } 7016250Sroot if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) { 70227058Smckusick fprintf(stderr, "tar: %s: symbolic link failed: ", 70313492Ssam dblock.dbuf.name); 70427058Smckusick perror(""); 7056250Sroot continue; 7066250Sroot } 7076250Sroot if (vflag) 70821910Skjd fprintf(vfile, "x %s symbolic link to %s\n", 70913492Ssam dblock.dbuf.name, dblock.dbuf.linkname); 71022353Skjd #ifdef notdef 71122353Skjd /* ignore alien orders */ 71222353Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 71322688Slepreau if (mflag == 0) 71422688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime); 71522353Skjd if (pflag) 71622353Skjd chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 71722353Skjd #endif 7181119Sbill continue; 7196250Sroot } 72022688Slepreau if (dblock.dbuf.linkflag == '1') { /* regular link */ 72125250Sbloom /* 72225250Sbloom * only unlink non directories or empty 72325250Sbloom * directories 72425250Sbloom */ 72525250Sbloom if (rmdir(dblock.dbuf.name) < 0) { 72625250Sbloom if (errno == ENOTDIR) 72725250Sbloom unlink(dblock.dbuf.name); 72825250Sbloom } 7291119Sbill if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) { 73027058Smckusick fprintf(stderr, "tar: can't link %s to %s: ", 73127058Smckusick dblock.dbuf.name, dblock.dbuf.linkname); 73227058Smckusick perror(""); 7331119Sbill continue; 7341119Sbill } 7351119Sbill if (vflag) 73622688Slepreau fprintf(vfile, "%s linked to %s\n", 73713492Ssam dblock.dbuf.name, dblock.dbuf.linkname); 7381119Sbill continue; 7391119Sbill } 7406250Sroot if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) { 74127058Smckusick fprintf(stderr, "tar: can't create %s: ", 74213492Ssam dblock.dbuf.name); 74327058Smckusick perror(""); 7441119Sbill passtape(); 7451119Sbill continue; 7461119Sbill } 74721457Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 7481119Sbill blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK; 7491119Sbill if (vflag) 75022688Slepreau fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n", 75113492Ssam dblock.dbuf.name, bytes, blocks); 75221457Skjd for (; blocks > 0;) { 75321457Skjd register int nread; 75421457Skjd char *bufp; 75521457Skjd register int nwant; 75621457Skjd 75721457Skjd nwant = NBLOCK*TBLOCK; 75821457Skjd if (nwant > (blocks*TBLOCK)) 75921457Skjd nwant = (blocks*TBLOCK); 76021457Skjd nread = readtbuf(&bufp, nwant); 76122688Slepreau if (write(ofile, bufp, (int)min(nread, bytes)) < 0) { 7626250Sroot fprintf(stderr, 76330090Sbostic "tar: %s: HELP - extract write error: ", 76413492Ssam dblock.dbuf.name); 76527058Smckusick perror(""); 7666250Sroot done(2); 7676250Sroot } 76821457Skjd bytes -= nread; 76921457Skjd blocks -= (((nread-1)/TBLOCK)+1); 7701119Sbill } 7711119Sbill close(ofile); 77222688Slepreau if (mflag == 0) 77322688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime); 7741926Swnj if (pflag) 7756250Sroot chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 7761119Sbill } 77722688Slepreau if (mflag == 0) { 77822688Slepreau dblock.dbuf.name[0] = '\0'; /* process the whole stack */ 77922688Slepreau dodirtimes(&dblock); 78022688Slepreau } 7811119Sbill } 7821119Sbill 78327445Slepreau dotable(argv) 78427445Slepreau char *argv[]; 7851119Sbill { 78627445Slepreau register int i; 78727445Slepreau 7881119Sbill for (;;) { 78927445Slepreau if ((i = wantit(argv)) == 0) 79027445Slepreau continue; 79127445Slepreau if (i == -1) 79227445Slepreau break; /* end of tape */ 7931119Sbill if (vflag) 7941119Sbill longt(&stbuf); 7951119Sbill printf("%s", dblock.dbuf.name); 7961119Sbill if (dblock.dbuf.linkflag == '1') 7971119Sbill printf(" linked to %s", dblock.dbuf.linkname); 7986250Sroot if (dblock.dbuf.linkflag == '2') 7996250Sroot printf(" symbolic link to %s", dblock.dbuf.linkname); 8001119Sbill printf("\n"); 8011119Sbill passtape(); 8021119Sbill } 8031119Sbill } 8041119Sbill 8051119Sbill putempty() 8061119Sbill { 8071119Sbill char buf[TBLOCK]; 8081119Sbill 80913492Ssam bzero(buf, sizeof (buf)); 81027445Slepreau (void) writetape(buf); 8111119Sbill } 8121119Sbill 8131119Sbill longt(st) 8146250Sroot register struct stat *st; 8151119Sbill { 8161119Sbill register char *cp; 8171119Sbill char *ctime(); 8181119Sbill 8191119Sbill pmode(st); 8201119Sbill printf("%3d/%1d", st->st_uid, st->st_gid); 82127445Slepreau printf("%7ld", st->st_size); 8221119Sbill cp = ctime(&st->st_mtime); 8231119Sbill printf(" %-12.12s %-4.4s ", cp+4, cp+20); 8241119Sbill } 8251119Sbill 8261119Sbill #define SUID 04000 8271119Sbill #define SGID 02000 8281119Sbill #define ROWN 0400 8291119Sbill #define WOWN 0200 8301119Sbill #define XOWN 0100 8311119Sbill #define RGRP 040 8321119Sbill #define WGRP 020 8331119Sbill #define XGRP 010 8341119Sbill #define ROTH 04 8351119Sbill #define WOTH 02 8361119Sbill #define XOTH 01 8371119Sbill #define STXT 01000 8381119Sbill int m1[] = { 1, ROWN, 'r', '-' }; 8391119Sbill int m2[] = { 1, WOWN, 'w', '-' }; 8401119Sbill int m3[] = { 2, SUID, 's', XOWN, 'x', '-' }; 8411119Sbill int m4[] = { 1, RGRP, 'r', '-' }; 8421119Sbill int m5[] = { 1, WGRP, 'w', '-' }; 8431119Sbill int m6[] = { 2, SGID, 's', XGRP, 'x', '-' }; 8441119Sbill int m7[] = { 1, ROTH, 'r', '-' }; 8451119Sbill int m8[] = { 1, WOTH, 'w', '-' }; 8461119Sbill int m9[] = { 2, STXT, 't', XOTH, 'x', '-' }; 8471119Sbill 8481119Sbill int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9}; 8491119Sbill 8501119Sbill pmode(st) 8516250Sroot register struct stat *st; 8521119Sbill { 8531119Sbill register int **mp; 8541119Sbill 8551119Sbill for (mp = &m[0]; mp < &m[9];) 85627058Smckusick selectbits(*mp++, st); 8571119Sbill } 8581119Sbill 85927058Smckusick selectbits(pairp, st) 8606250Sroot int *pairp; 8616250Sroot struct stat *st; 8621119Sbill { 8631119Sbill register int n, *ap; 8641119Sbill 8651119Sbill ap = pairp; 8661119Sbill n = *ap++; 8671119Sbill while (--n>=0 && (st->st_mode&*ap++)==0) 8681119Sbill ap++; 86927445Slepreau putchar(*ap); 8701119Sbill } 8711119Sbill 87222688Slepreau /* 87327442Slepreau * Make all directories needed by `name'. If `name' is itself 87427442Slepreau * a directory on the tar tape (indicated by a trailing '/'), 87522688Slepreau * return 1; else 0. 87622688Slepreau */ 8771119Sbill checkdir(name) 8786250Sroot register char *name; 8791119Sbill { 8801119Sbill register char *cp; 8816250Sroot 88212154Ssam /* 88322688Slepreau * Quick check for existence of directory. 88412154Ssam */ 88512154Ssam if ((cp = rindex(name, '/')) == 0) 88612154Ssam return (0); 88712154Ssam *cp = '\0'; 88822688Slepreau if (access(name, 0) == 0) { /* already exists */ 88912154Ssam *cp = '/'; 89022688Slepreau return (cp[1] == '\0'); /* return (lastchar == '/') */ 89112154Ssam } 89212154Ssam *cp = '/'; 89312154Ssam 89412154Ssam /* 89512154Ssam * No luck, try to make all directories in path. 89612154Ssam */ 8971119Sbill for (cp = name; *cp; cp++) { 8986250Sroot if (*cp != '/') 8996250Sroot continue; 9006250Sroot *cp = '\0'; 90112154Ssam if (access(name, 0) < 0) { 9029844Ssam if (mkdir(name, 0777) < 0) { 9039844Ssam perror(name); 90412154Ssam *cp = '/'; 90512154Ssam return (0); 9061119Sbill } 90721457Skjd chown(name, stbuf.st_uid, stbuf.st_gid); 90827442Slepreau if (pflag && cp[1] == '\0') /* dir on the tape */ 90927442Slepreau chmod(name, stbuf.st_mode & 07777); 9101119Sbill } 9116250Sroot *cp = '/'; 9121119Sbill } 9136250Sroot return (cp[-1]=='/'); 9141119Sbill } 9151119Sbill 9161119Sbill onintr() 9171119Sbill { 91827445Slepreau (void) signal(SIGINT, SIG_IGN); 9191119Sbill term++; 9201119Sbill } 9211119Sbill 9221119Sbill onquit() 9231119Sbill { 92427445Slepreau (void) signal(SIGQUIT, SIG_IGN); 9251119Sbill term++; 9261119Sbill } 9271119Sbill 9281119Sbill onhup() 9291119Sbill { 93027445Slepreau (void) signal(SIGHUP, SIG_IGN); 9311119Sbill term++; 9321119Sbill } 9331119Sbill 93422688Slepreau #ifdef notdef 9351119Sbill onterm() 9361119Sbill { 93727445Slepreau (void) signal(SIGTERM, SIG_IGN); 9381119Sbill term++; 9391119Sbill } 94022688Slepreau #endif 9411119Sbill 9421119Sbill tomodes(sp) 9431119Sbill register struct stat *sp; 9441119Sbill { 9451119Sbill register char *cp; 9461119Sbill 9471119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 9481119Sbill *cp = '\0'; 94932420Sbostic (void)sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777); 95032420Sbostic (void)sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid); 95132420Sbostic (void)sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid); 95232420Sbostic (void)sprintf(dblock.dbuf.size, "%11lo ", sp->st_size); 95332420Sbostic (void)sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime); 9541119Sbill } 9551119Sbill 9561119Sbill checksum() 9571119Sbill { 9581119Sbill register i; 9591119Sbill register char *cp; 9601119Sbill 9616250Sroot for (cp = dblock.dbuf.chksum; 9626250Sroot cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++) 9631119Sbill *cp = ' '; 9641119Sbill i = 0; 9651119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 9661119Sbill i += *cp; 9676250Sroot return (i); 9681119Sbill } 9691119Sbill 9701119Sbill checkw(c, name) 9716250Sroot char *name; 9721119Sbill { 9736250Sroot if (!wflag) 9746250Sroot return (1); 9756250Sroot printf("%c ", c); 9766250Sroot if (vflag) 9776250Sroot longt(&stbuf); 9786250Sroot printf("%s: ", name); 9796250Sroot return (response() == 'y'); 9801119Sbill } 9811119Sbill 9821119Sbill response() 9831119Sbill { 9841119Sbill char c; 9851119Sbill 9861119Sbill c = getchar(); 9871119Sbill if (c != '\n') 9886250Sroot while (getchar() != '\n') 9896250Sroot ; 9906250Sroot else 9916250Sroot c = 'n'; 9926250Sroot return (c); 9931119Sbill } 9941119Sbill 99512154Ssam checkf(name, mode, howmuch) 99612154Ssam char *name; 99712154Ssam int mode, howmuch; 99812154Ssam { 99912154Ssam int l; 100012154Ssam 100121910Skjd if ((mode & S_IFMT) == S_IFDIR){ 100221910Skjd if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0)) 100321910Skjd return(0); 100421910Skjd return(1); 100521910Skjd } 100612154Ssam if ((l = strlen(name)) < 3) 100712154Ssam return (1); 100812154Ssam if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o') 100912154Ssam return (0); 101012154Ssam if (strcmp(name, "core") == 0 || 101112154Ssam strcmp(name, "errs") == 0 || 101212154Ssam (howmuch > 1 && strcmp(name, "a.out") == 0)) 101312154Ssam return (0); 101412154Ssam /* SHOULD CHECK IF IT IS EXECUTABLE */ 101512154Ssam return (1); 101612154Ssam } 101712154Ssam 101822688Slepreau /* Is the current file a new file, or the newest one of the same name? */ 10191119Sbill checkupdate(arg) 10206250Sroot char *arg; 10211119Sbill { 10221119Sbill char name[100]; 10236250Sroot long mtime; 10241119Sbill daddr_t seekp; 10251119Sbill daddr_t lookup(); 10261119Sbill 10271119Sbill rewind(tfile); 10281119Sbill for (;;) { 10291119Sbill if ((seekp = lookup(arg)) < 0) 10306250Sroot return (1); 10311119Sbill fseek(tfile, seekp, 0); 10321119Sbill fscanf(tfile, "%s %lo", name, &mtime); 10336250Sroot return (stbuf.st_mtime > mtime); 10341119Sbill } 10351119Sbill } 10361119Sbill 10371119Sbill done(n) 10381119Sbill { 103921457Skjd unlink(tname); 10401119Sbill exit(n); 10411119Sbill } 10421119Sbill 104327445Slepreau /* 104427445Slepreau * Do we want the next entry on the tape, i.e. is it selected? If 104527445Slepreau * not, skip over the entire entry. Return -1 if reached end of tape. 104627445Slepreau */ 104727445Slepreau wantit(argv) 104827445Slepreau char *argv[]; 104927445Slepreau { 105027445Slepreau register char **cp; 105127445Slepreau 105227445Slepreau getdir(); 105327445Slepreau if (endtape()) 105427445Slepreau return (-1); 105527445Slepreau if (*argv == 0) 105627445Slepreau return (1); 105727445Slepreau for (cp = argv; *cp; cp++) 105827445Slepreau if (prefix(*cp, dblock.dbuf.name)) 105927445Slepreau return (1); 106027445Slepreau passtape(); 106127445Slepreau return (0); 106227445Slepreau } 106327445Slepreau 106422688Slepreau /* 106522688Slepreau * Does s2 begin with the string s1, on a directory boundary? 106622688Slepreau */ 10671119Sbill prefix(s1, s2) 10686250Sroot register char *s1, *s2; 10691119Sbill { 10701119Sbill while (*s1) 10711119Sbill if (*s1++ != *s2++) 10726250Sroot return (0); 10731119Sbill if (*s2) 10746250Sroot return (*s2 == '/'); 10756250Sroot return (1); 10761119Sbill } 10771119Sbill 10781119Sbill #define N 200 10791119Sbill int njab; 10806250Sroot 10811119Sbill daddr_t 10821119Sbill lookup(s) 10836250Sroot char *s; 10841119Sbill { 10851119Sbill register i; 10861119Sbill daddr_t a; 10871119Sbill 10881119Sbill for(i=0; s[i]; i++) 10896250Sroot if (s[i] == ' ') 10901119Sbill break; 10911119Sbill a = bsrch(s, i, low, high); 10926250Sroot return (a); 10931119Sbill } 10941119Sbill 10951119Sbill daddr_t 10961119Sbill bsrch(s, n, l, h) 10976250Sroot daddr_t l, h; 10986250Sroot char *s; 10991119Sbill { 11001119Sbill register i, j; 11011119Sbill char b[N]; 11021119Sbill daddr_t m, m1; 11031119Sbill 11041119Sbill njab = 0; 11051119Sbill 11061119Sbill loop: 11076250Sroot if (l >= h) 110822688Slepreau return ((daddr_t) -1); 11091119Sbill m = l + (h-l)/2 - N/2; 11106250Sroot if (m < l) 11111119Sbill m = l; 11121119Sbill fseek(tfile, m, 0); 11131119Sbill fread(b, 1, N, tfile); 11141119Sbill njab++; 11151119Sbill for(i=0; i<N; i++) { 11166250Sroot if (b[i] == '\n') 11171119Sbill break; 11181119Sbill m++; 11191119Sbill } 11206250Sroot if (m >= h) 112122688Slepreau return ((daddr_t) -1); 11221119Sbill m1 = m; 11231119Sbill j = i; 11241119Sbill for(i++; i<N; i++) { 11251119Sbill m1++; 11266250Sroot if (b[i] == '\n') 11271119Sbill break; 11281119Sbill } 11291119Sbill i = cmp(b+j, s, n); 11306250Sroot if (i < 0) { 11311119Sbill h = m; 11321119Sbill goto loop; 11331119Sbill } 11346250Sroot if (i > 0) { 11351119Sbill l = m1; 11361119Sbill goto loop; 11371119Sbill } 11386250Sroot return (m); 11391119Sbill } 11401119Sbill 11411119Sbill cmp(b, s, n) 11426250Sroot char *b, *s; 11431119Sbill { 11441119Sbill register i; 11451119Sbill 11466250Sroot if (b[0] != '\n') 114721457Skjd exit(2); 11481119Sbill for(i=0; i<n; i++) { 11496250Sroot if (b[i+1] > s[i]) 11506250Sroot return (-1); 11516250Sroot if (b[i+1] < s[i]) 11526250Sroot return (1); 11531119Sbill } 11546250Sroot return (b[i+1] == ' '? 0 : -1); 11551119Sbill } 11561119Sbill 115722688Slepreau readtape(buffer) 11586250Sroot char *buffer; 11591119Sbill { 116021457Skjd char *bufp; 116121457Skjd 116222688Slepreau if (first == 0) 116322688Slepreau getbuf(); 116427445Slepreau (void) readtbuf(&bufp, TBLOCK); 116521457Skjd bcopy(bufp, buffer, TBLOCK); 116621457Skjd return(TBLOCK); 116721457Skjd } 116821457Skjd 116921457Skjd readtbuf(bufpp, size) 117021457Skjd char **bufpp; 117121457Skjd int size; 117221457Skjd { 11733457Swnj register int i; 11741119Sbill 11751119Sbill if (recno >= nblock || first == 0) { 117627445Slepreau if ((i = bread(mt, (char *)tbuf, TBLOCK*nblock)) < 0) 117727058Smckusick mterr("read", i, 3); 11781119Sbill if (first == 0) { 11791119Sbill if ((i % TBLOCK) != 0) { 118013492Ssam fprintf(stderr, "tar: tape blocksize error\n"); 11811119Sbill done(3); 11821119Sbill } 11831119Sbill i /= TBLOCK; 11843457Swnj if (i != nblock) { 118513492Ssam fprintf(stderr, "tar: blocksize = %d\n", i); 11861119Sbill nblock = i; 11871119Sbill } 118822353Skjd first = 1; 11891119Sbill } 11901119Sbill recno = 0; 11911119Sbill } 119221457Skjd if (size > ((nblock-recno)*TBLOCK)) 119321457Skjd size = (nblock-recno)*TBLOCK; 119421457Skjd *bufpp = (char *)&tbuf[recno]; 119521457Skjd recno += (size/TBLOCK); 119621457Skjd return (size); 11971119Sbill } 11981119Sbill 119921457Skjd writetbuf(buffer, n) 120021457Skjd register char *buffer; 120121457Skjd register int n; 12021119Sbill { 120327058Smckusick int i; 120422688Slepreau 120522688Slepreau if (first == 0) { 120622353Skjd getbuf(); 120722353Skjd first = 1; 120822353Skjd } 12091119Sbill if (recno >= nblock) { 121027445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock); 121127058Smckusick if (i != TBLOCK*nblock) 121227058Smckusick mterr("write", i, 2); 12131119Sbill recno = 0; 12141119Sbill } 121521457Skjd 121621457Skjd /* 121721457Skjd * Special case: We have an empty tape buffer, and the 121821457Skjd * users data size is >= the tape block size: Avoid 121921457Skjd * the bcopy and dma direct to tape. BIG WIN. Add the 122021457Skjd * residual to the tape buffer. 122121457Skjd */ 122221457Skjd while (recno == 0 && n >= nblock) { 122327058Smckusick i = write(mt, buffer, TBLOCK*nblock); 122427058Smckusick if (i != TBLOCK*nblock) 122527058Smckusick mterr("write", i, 2); 122621457Skjd n -= nblock; 122721457Skjd buffer += (nblock * TBLOCK); 12281119Sbill } 122921457Skjd 123021457Skjd while (n-- > 0) { 123121457Skjd bcopy(buffer, (char *)&tbuf[recno++], TBLOCK); 123221457Skjd buffer += TBLOCK; 123321457Skjd if (recno >= nblock) { 123427445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock); 123527058Smckusick if (i != TBLOCK*nblock) 123627058Smckusick mterr("write", i, 2); 123721457Skjd recno = 0; 123821457Skjd } 123921457Skjd } 124021457Skjd 124121457Skjd /* Tell the user how much to write to get in sync */ 124221457Skjd return (nblock - recno); 12431119Sbill } 12441119Sbill 12451119Sbill backtape() 12461119Sbill { 124727058Smckusick static int mtdev = 1; 12483457Swnj static struct mtop mtop = {MTBSR, 1}; 124927058Smckusick struct mtget mtget; 125027058Smckusick 125127058Smckusick if (mtdev == 1) 125227445Slepreau mtdev = ioctl(mt, MTIOCGET, (char *)&mtget); 12533457Swnj if (mtdev == 0) { 125427445Slepreau if (ioctl(mt, MTIOCTOP, (char *)&mtop) < 0) { 125527058Smckusick fprintf(stderr, "tar: tape backspace error: "); 125627058Smckusick perror(""); 12571119Sbill done(4); 12581119Sbill } 12593457Swnj } else 126022688Slepreau lseek(mt, (daddr_t) -TBLOCK*nblock, 1); 12613457Swnj recno--; 12621119Sbill } 12631119Sbill 12641119Sbill flushtape() 12651119Sbill { 126627058Smckusick int i; 126727058Smckusick 126827445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock); 126927058Smckusick if (i != TBLOCK*nblock) 127027058Smckusick mterr("write", i, 2); 12711119Sbill } 12721119Sbill 127327058Smckusick mterr(operation, i, exitcode) 127427058Smckusick char *operation; 127527058Smckusick int i; 127627058Smckusick { 127727058Smckusick fprintf(stderr, "tar: tape %s error: ", operation); 127827058Smckusick if (i < 0) 127927058Smckusick perror(""); 128027058Smckusick else 128127058Smckusick fprintf(stderr, "unexpected EOF\n"); 128227058Smckusick done(exitcode); 128327058Smckusick } 128427058Smckusick 12858737Smckusick bread(fd, buf, size) 12868737Smckusick int fd; 12878737Smckusick char *buf; 12888737Smckusick int size; 12898737Smckusick { 12908737Smckusick int count; 12918737Smckusick static int lastread = 0; 12928737Smckusick 129322688Slepreau if (!Bflag) 129422688Slepreau return (read(fd, buf, size)); 129522353Skjd 12968737Smckusick for (count = 0; count < size; count += lastread) { 129724281Smckusick lastread = read(fd, buf, size - count); 129824281Smckusick if (lastread <= 0) { 12998737Smckusick if (count > 0) 13008737Smckusick return (count); 13018737Smckusick return (lastread); 13028737Smckusick } 13038737Smckusick buf += lastread; 13048737Smckusick } 13058737Smckusick return (count); 13068737Smckusick } 130710165Ssam 130810165Ssam char * 130910165Ssam getcwd(buf) 131010165Ssam char *buf; 131110165Ssam { 131210165Ssam if (getwd(buf) == NULL) { 131310165Ssam fprintf(stderr, "tar: %s\n", buf); 131421457Skjd exit(1); 131510165Ssam } 131610165Ssam return (buf); 131710165Ssam } 131821910Skjd 131921910Skjd getbuf() 132021910Skjd { 132122353Skjd 132227058Smckusick if (nblock == 0) { 132321910Skjd fstat(mt, &stbuf); 132421910Skjd if ((stbuf.st_mode & S_IFMT) == S_IFCHR) 132527058Smckusick nblock = NBLOCK; 132621910Skjd else { 132727058Smckusick nblock = stbuf.st_blksize / TBLOCK; 132827058Smckusick if (nblock == 0) 132927058Smckusick nblock = NBLOCK; 133021910Skjd } 133121910Skjd } 133227445Slepreau tbuf = (union hblock *)malloc((unsigned)nblock*TBLOCK); 133321910Skjd if (tbuf == NULL) { 133421910Skjd fprintf(stderr, "tar: blocksize %d too big, can't get memory\n", 133521910Skjd nblock); 133621910Skjd done(1); 133721910Skjd } 133821910Skjd } 133922353Skjd 134022688Slepreau /* 134127442Slepreau * Save this directory and its mtime on the stack, popping and setting 134227442Slepreau * the mtimes of any stacked dirs which aren't parents of this one. 134327442Slepreau * A null directory causes the entire stack to be unwound and set. 134422688Slepreau * 134527442Slepreau * Since all the elements of the directory "stack" share a common 134627442Slepreau * prefix, we can make do with one string. We keep only the current 134727442Slepreau * directory path, with an associated array of mtime's, one for each 134827442Slepreau * '/' in the path. A negative mtime means no mtime. The mtime's are 134927442Slepreau * offset by one (first index 1, not 0) because calling this with a null 135027442Slepreau * directory causes mtime[0] to be set. 135127442Slepreau * 135222688Slepreau * This stack algorithm is not guaranteed to work for tapes created 135322688Slepreau * with the 'r' option, but the vast majority of tapes with 135422688Slepreau * directories are not. This avoids saving every directory record on 135522688Slepreau * the tape and setting all the times at the end. 135622688Slepreau */ 135722688Slepreau char dirstack[NAMSIZ]; 135822688Slepreau #define NTIM (NAMSIZ/2+1) /* a/b/c/d/... */ 135922688Slepreau time_t mtime[NTIM]; 136022353Skjd 136122688Slepreau dodirtimes(hp) 136222688Slepreau union hblock *hp; 136322353Skjd { 136422688Slepreau register char *p = dirstack; 136522688Slepreau register char *q = hp->dbuf.name; 136622688Slepreau register int ndir = 0; 136722688Slepreau char *savp; 136822688Slepreau int savndir; 136922353Skjd 137022688Slepreau /* Find common prefix */ 137130090Sbostic while (*p == *q && *p) { 137222688Slepreau if (*p++ == '/') 137322688Slepreau ++ndir; 137422688Slepreau q++; 137522688Slepreau } 137622353Skjd 137722688Slepreau savp = p; 137822688Slepreau savndir = ndir; 137922688Slepreau while (*p) { 138022688Slepreau /* 138122688Slepreau * Not a child: unwind the stack, setting the times. 138222688Slepreau * The order we do this doesn't matter, so we go "forward." 138322688Slepreau */ 138422688Slepreau if (*p++ == '/') 138522688Slepreau if (mtime[++ndir] >= 0) { 138622688Slepreau *--p = '\0'; /* zap the slash */ 138722688Slepreau setimes(dirstack, mtime[ndir]); 138822688Slepreau *p++ = '/'; 138922688Slepreau } 139022688Slepreau } 139122688Slepreau p = savp; 139222688Slepreau ndir = savndir; 139322353Skjd 139422688Slepreau /* Push this one on the "stack" */ 139522688Slepreau while (*p = *q++) /* append the rest of the new dir */ 139622688Slepreau if (*p++ == '/') 139722688Slepreau mtime[++ndir] = -1; 139822688Slepreau mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */ 139922688Slepreau } 140022353Skjd 140122688Slepreau setimes(path, mt) 140222688Slepreau char *path; 140322688Slepreau time_t mt; 140422688Slepreau { 140522688Slepreau struct timeval tv[2]; 140622353Skjd 140722688Slepreau tv[0].tv_sec = time((time_t *) 0); 140822688Slepreau tv[1].tv_sec = mt; 140922688Slepreau tv[0].tv_usec = tv[1].tv_usec = 0; 141027058Smckusick if (utimes(path, tv) < 0) { 141127058Smckusick fprintf(stderr, "tar: can't set time on %s: ", path); 141227058Smckusick perror(""); 141327058Smckusick } 141422688Slepreau } 141522688Slepreau 141622688Slepreau char * 141722688Slepreau getmem(size) 141822688Slepreau { 141922688Slepreau char *p = malloc((unsigned) size); 142022688Slepreau 142122688Slepreau if (p == NULL && freemem) { 142222688Slepreau fprintf(stderr, 142322688Slepreau "tar: out of memory, link and directory modtime info lost\n"); 142422688Slepreau freemem = 0; 142522353Skjd } 142622688Slepreau return (p); 142722353Skjd } 1428