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*25250Sbloom static char sccsid[] = "@(#)tar.c 5.4 (Berkeley) 10/22/85"; 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> 291119Sbill 301119Sbill #define TBLOCK 512 313355Swnj #define NBLOCK 20 321119Sbill #define NAMSIZ 100 3322353Skjd #define FILEBLOCK 20 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; 8322353Skjd int mtdev = 1; 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(); 1086250Sroot char *sprintf(); 1096250Sroot char *strcat(); 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 2571119Sbill if (strcmp(usefile, "-") == 0) { 2581119Sbill if (cflag == 0) { 2596250Sroot fprintf(stderr, 26013492Ssam "tar: can only create standard output archives\n"); 2611119Sbill done(1); 2621119Sbill } 26321910Skjd vfile = stderr; 26421910Skjd setlinebuf(vfile); 2651119Sbill mt = dup(1); 2666250Sroot } else if ((mt = open(usefile, 2)) < 0) { 2671119Sbill if (cflag == 0 || (mt = creat(usefile, 0666)) < 0) { 2686250Sroot fprintf(stderr, 2696250Sroot "tar: cannot open %s\n", usefile); 2701119Sbill done(1); 2711119Sbill } 2721119Sbill } 2731119Sbill dorep(argv); 2746250Sroot done(0); 2751119Sbill } 2766250Sroot if (strcmp(usefile, "-") == 0) { 2776250Sroot mt = dup(0); 27824281Smckusick Bflag++; 2796250Sroot } else if ((mt = open(usefile, 0)) < 0) { 2806250Sroot fprintf(stderr, "tar: cannot open %s\n", usefile); 2816250Sroot done(1); 2826250Sroot } 2836250Sroot if (xflag) 2841119Sbill doxtract(argv); 2856250Sroot else 2861119Sbill dotable(); 2871119Sbill done(0); 2881119Sbill } 2891119Sbill 2901119Sbill usage() 2911119Sbill { 2926250Sroot fprintf(stderr, 29321457Skjd "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n"); 2941119Sbill done(1); 2951119Sbill } 2961119Sbill 2971119Sbill dorep(argv) 2986250Sroot char *argv[]; 2991119Sbill { 3001119Sbill register char *cp, *cp2; 3019601Ssam char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent; 3021119Sbill 3031119Sbill if (!cflag) { 3041119Sbill getdir(); 3051119Sbill do { 3061119Sbill passtape(); 3071119Sbill if (term) 3081119Sbill done(0); 3091119Sbill getdir(); 3101119Sbill } while (!endtape()); 31113492Ssam backtape(); 3121119Sbill if (tfile != NULL) { 3131119Sbill char buf[200]; 3141119Sbill 3156250Sroot sprintf(buf, 3166250Sroot "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s", 3171119Sbill tname, tname, tname, tname, tname, tname); 3181119Sbill fflush(tfile); 3191119Sbill system(buf); 3201119Sbill freopen(tname, "r", tfile); 3211119Sbill fstat(fileno(tfile), &stbuf); 3221119Sbill high = stbuf.st_size; 3231119Sbill } 3241119Sbill } 3251119Sbill 32610165Ssam (void) getcwd(wdir); 3271119Sbill while (*argv && ! term) { 3281119Sbill cp2 = *argv; 3291119Sbill if (!strcmp(cp2, "-C") && argv[1]) { 3301119Sbill argv++; 3311119Sbill if (chdir(*argv) < 0) 3321119Sbill perror(*argv); 3331119Sbill else 33410165Ssam (void) getcwd(wdir); 3351119Sbill argv++; 3361119Sbill continue; 3371119Sbill } 3389601Ssam parent = wdir; 3391119Sbill for (cp = *argv; *cp; cp++) 3401119Sbill if (*cp == '/') 3411119Sbill cp2 = cp; 3421119Sbill if (cp2 != *argv) { 3431119Sbill *cp2 = '\0'; 3449601Ssam if (chdir(*argv) < 0) { 3459601Ssam perror(*argv); 3469601Ssam continue; 3479601Ssam } 34810165Ssam parent = getcwd(tempdir); 3491119Sbill *cp2 = '/'; 3501119Sbill cp2++; 3511119Sbill } 3529601Ssam putfile(*argv++, cp2, parent); 35315045Smckusick if (chdir(wdir) < 0) { 35422688Slepreau fprintf(stderr, "tar: cannot change back?: "); 35515045Smckusick perror(wdir); 35615045Smckusick } 3571119Sbill } 3581119Sbill putempty(); 3591119Sbill putempty(); 3601119Sbill flushtape(); 36122688Slepreau if (prtlinkerr == 0) 3626250Sroot return; 3636250Sroot for (; ihead != NULL; ihead = ihead->nextp) { 3646250Sroot if (ihead->count == 0) 3656250Sroot continue; 36613492Ssam fprintf(stderr, "tar: missing links to %s\n", ihead->pathname); 3676250Sroot } 3681119Sbill } 3691119Sbill 3701119Sbill endtape() 3711119Sbill { 37221457Skjd return (dblock.dbuf.name[0] == '\0'); 3731119Sbill } 3741119Sbill 3751119Sbill getdir() 3761119Sbill { 3771119Sbill register struct stat *sp; 3781119Sbill int i; 3791119Sbill 38012154Ssam top: 3816250Sroot readtape((char *)&dblock); 3821119Sbill if (dblock.dbuf.name[0] == '\0') 3831119Sbill return; 3841119Sbill sp = &stbuf; 3851119Sbill sscanf(dblock.dbuf.mode, "%o", &i); 3861119Sbill sp->st_mode = i; 3871119Sbill sscanf(dblock.dbuf.uid, "%o", &i); 3881119Sbill sp->st_uid = i; 3891119Sbill sscanf(dblock.dbuf.gid, "%o", &i); 3901119Sbill sp->st_gid = i; 3911119Sbill sscanf(dblock.dbuf.size, "%lo", &sp->st_size); 3921119Sbill sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime); 3931119Sbill sscanf(dblock.dbuf.chksum, "%o", &chksum); 39412154Ssam if (chksum != (i = checksum())) { 39513492Ssam fprintf(stderr, "tar: directory checksum error (%d != %d)\n", 39612154Ssam chksum, i); 39712154Ssam if (iflag) 39812154Ssam goto top; 3991119Sbill done(2); 4001119Sbill } 4011119Sbill if (tfile != NULL) 4021119Sbill fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime); 4031119Sbill } 4041119Sbill 4051119Sbill passtape() 4061119Sbill { 4071119Sbill long blocks; 40821457Skjd char *bufp; 4091119Sbill 4101119Sbill if (dblock.dbuf.linkflag == '1') 4111119Sbill return; 4121119Sbill blocks = stbuf.st_size; 4131119Sbill blocks += TBLOCK-1; 4141119Sbill blocks /= TBLOCK; 4151119Sbill 41621457Skjd while (blocks-- > 0) 41721457Skjd readtbuf(&bufp, TBLOCK); 4181119Sbill } 4191119Sbill 4209601Ssam putfile(longname, shortname, parent) 4216250Sroot char *longname; 4226250Sroot char *shortname; 4239601Ssam char *parent; 4241119Sbill { 42521457Skjd int infile = 0; 42621457Skjd long blocks; 4271119Sbill char buf[TBLOCK]; 42821457Skjd char *bigbuf; 42922688Slepreau register char *cp; 4305931Smckusic struct direct *dp; 4315931Smckusic DIR *dirp; 43222688Slepreau int i; 4339601Ssam char newparent[NAMSIZ+64]; 43412154Ssam extern int errno; 43521457Skjd int maxread; 43621457Skjd int hint; /* amount to write to get "in sync" */ 4371119Sbill 4389601Ssam if (!hflag) 43912154Ssam i = lstat(shortname, &stbuf); 44012154Ssam else 44112154Ssam i = stat(shortname, &stbuf); 44212154Ssam if (i < 0) { 44312154Ssam switch (errno) { 44412154Ssam case EACCES: 44512154Ssam fprintf(stderr, "tar: %s: cannot open file\n", longname); 44612154Ssam break; 44712154Ssam case ENOENT: 44812154Ssam fprintf(stderr, "tar: %s: no such file or directory\n", 44912154Ssam longname); 45012154Ssam break; 45112154Ssam default: 45212154Ssam fprintf(stderr, "tar: %s: cannot stat file\n", longname); 45312154Ssam break; 45412154Ssam } 4559601Ssam return; 4569601Ssam } 45712154Ssam if (tfile != NULL && checkupdate(longname) == 0) 4581119Sbill return; 45912154Ssam if (checkw('r', longname) == 0) 4601119Sbill return; 46112154Ssam if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0) 46212154Ssam return; 4631119Sbill 46412154Ssam switch (stbuf.st_mode & S_IFMT) { 46512154Ssam case S_IFDIR: 4666250Sroot for (i = 0, cp = buf; *cp++ = longname[i++];) 4676250Sroot ; 4681119Sbill *--cp = '/'; 4691119Sbill *++cp = 0 ; 4701119Sbill if (!oflag) { 4716250Sroot if ((cp - buf) >= NAMSIZ) { 47213492Ssam fprintf(stderr, "tar: %s: file name too long\n", 47313492Ssam longname); 4746250Sroot return; 4756250Sroot } 4766250Sroot stbuf.st_size = 0; 4776250Sroot tomodes(&stbuf); 4786250Sroot strcpy(dblock.dbuf.name,buf); 4796250Sroot sprintf(dblock.dbuf.chksum, "%6o", checksum()); 4806250Sroot writetape((char *)&dblock); 4811119Sbill } 4829601Ssam sprintf(newparent, "%s/%s", parent, shortname); 48315045Smckusick if (chdir(shortname) < 0) { 48415045Smckusick perror(shortname); 48515045Smckusick return; 48615045Smckusick } 4875931Smckusic if ((dirp = opendir(".")) == NULL) { 48813492Ssam fprintf(stderr, "tar: %s: directory read error\n", 48913492Ssam longname); 49015045Smckusick if (chdir(parent) < 0) { 49122688Slepreau fprintf(stderr, "tar: cannot change back?: "); 49215045Smckusick perror(parent); 49315045Smckusick } 4945931Smckusic return; 4955931Smckusic } 4965931Smckusic while ((dp = readdir(dirp)) != NULL && !term) { 4975931Smckusic if (dp->d_ino == 0) 4981119Sbill continue; 4996250Sroot if (!strcmp(".", dp->d_name) || 5006250Sroot !strcmp("..", dp->d_name)) 5011119Sbill continue; 5025931Smckusic strcpy(cp, dp->d_name); 5035931Smckusic i = telldir(dirp); 5045931Smckusic closedir(dirp); 5059601Ssam putfile(buf, cp, newparent); 5065931Smckusic dirp = opendir("."); 5075931Smckusic seekdir(dirp, i); 5081119Sbill } 5095931Smckusic closedir(dirp); 51015045Smckusick if (chdir(parent) < 0) { 51122688Slepreau fprintf(stderr, "tar: cannot change back?: "); 51215045Smckusick perror(parent); 51315045Smckusick } 51412154Ssam break; 51512154Ssam 51612154Ssam case S_IFLNK: 51712154Ssam tomodes(&stbuf); 51812154Ssam if (strlen(longname) >= NAMSIZ) { 51913492Ssam fprintf(stderr, "tar: %s: file name too long\n", 52013492Ssam longname); 52112154Ssam return; 52212154Ssam } 52312154Ssam strcpy(dblock.dbuf.name, longname); 5246250Sroot if (stbuf.st_size + 1 >= NAMSIZ) { 52513492Ssam fprintf(stderr, "tar: %s: symbolic link too long\n", 52613492Ssam longname); 5276250Sroot return; 5286250Sroot } 5299601Ssam i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1); 5306250Sroot if (i < 0) { 5319601Ssam perror(longname); 5326250Sroot return; 5336250Sroot } 5346250Sroot dblock.dbuf.linkname[i] = '\0'; 5356250Sroot dblock.dbuf.linkflag = '2'; 53622688Slepreau if (vflag) 53722688Slepreau fprintf(vfile, "a %s symbolic link to %s\n", 53822688Slepreau longname, dblock.dbuf.linkname); 5396250Sroot sprintf(dblock.dbuf.size, "%11lo", 0); 5406250Sroot sprintf(dblock.dbuf.chksum, "%6o", checksum()); 5416250Sroot writetape((char *)&dblock); 54212154Ssam break; 5431119Sbill 54412154Ssam case S_IFREG: 54512154Ssam if ((infile = open(shortname, 0)) < 0) { 54612154Ssam fprintf(stderr, "tar: %s: cannot open file\n", longname); 5471119Sbill return; 5481119Sbill } 54912154Ssam tomodes(&stbuf); 55012154Ssam if (strlen(longname) >= NAMSIZ) { 55113492Ssam fprintf(stderr, "tar: %s: file name too long\n", 55213492Ssam longname); 55321457Skjd close(infile); 55412154Ssam return; 55512154Ssam } 55612154Ssam strcpy(dblock.dbuf.name, longname); 55712154Ssam if (stbuf.st_nlink > 1) { 55812154Ssam struct linkbuf *lp; 55912154Ssam int found = 0; 56012154Ssam 56112154Ssam for (lp = ihead; lp != NULL; lp = lp->nextp) 56212154Ssam if (lp->inum == stbuf.st_ino && 56312154Ssam lp->devnum == stbuf.st_dev) { 56412154Ssam found++; 56512154Ssam break; 56612154Ssam } 56712154Ssam if (found) { 56812154Ssam strcpy(dblock.dbuf.linkname, lp->pathname); 56912154Ssam dblock.dbuf.linkflag = '1'; 57012154Ssam sprintf(dblock.dbuf.chksum, "%6o", checksum()); 57121457Skjd writetape( (char *) &dblock); 57222688Slepreau if (vflag) 57322688Slepreau fprintf(vfile, "a %s link to %s\n", 57422688Slepreau longname, lp->pathname); 57512154Ssam lp->count--; 57612154Ssam close(infile); 57712154Ssam return; 5781119Sbill } 57922688Slepreau lp = (struct linkbuf *) getmem(sizeof(*lp)); 58022688Slepreau if (lp != NULL) { 58112154Ssam lp->nextp = ihead; 58212154Ssam ihead = lp; 58312154Ssam lp->inum = stbuf.st_ino; 58412154Ssam lp->devnum = stbuf.st_dev; 58512154Ssam lp->count = stbuf.st_nlink - 1; 58612154Ssam strcpy(lp->pathname, longname); 58712154Ssam } 5881119Sbill } 58921457Skjd blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK; 59022688Slepreau if (vflag) 59122688Slepreau fprintf(vfile, "a %s %ld blocks\n", longname, blocks); 59212154Ssam sprintf(dblock.dbuf.chksum, "%6o", checksum()); 59321457Skjd hint = writetape((char *)&dblock); 59421457Skjd maxread = max(stbuf.st_blksize, (nblock * TBLOCK)); 59521457Skjd if ((bigbuf = malloc(maxread)) == 0) { 59621457Skjd maxread = TBLOCK; 59721457Skjd bigbuf = buf; 59821457Skjd } 5991119Sbill 60021457Skjd while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0 60121457Skjd && blocks > 0) { 60221457Skjd register int nblks; 60321457Skjd 60421457Skjd nblks = ((i-1)/TBLOCK)+1; 60521457Skjd if (nblks > blocks) 60621457Skjd nblks = blocks; 60721457Skjd hint = writetbuf(bigbuf, nblks); 60821457Skjd blocks -= nblks; 60921457Skjd } 61021457Skjd close(infile); 61121457Skjd if (bigbuf != buf) 61221457Skjd free(bigbuf); 61321457Skjd if (blocks != 0 || i != 0) 61413492Ssam fprintf(stderr, "tar: %s: file changed size\n", 61513492Ssam longname); 61612154Ssam while (--blocks >= 0) 61712154Ssam putempty(); 61812154Ssam break; 61912154Ssam 62012154Ssam default: 62112154Ssam fprintf(stderr, "tar: %s is not a file. Not dumped\n", 62213492Ssam longname); 62312154Ssam break; 6241119Sbill } 6251119Sbill } 6261119Sbill 6271119Sbill doxtract(argv) 6286250Sroot char *argv[]; 6291119Sbill { 6301119Sbill long blocks, bytes; 6311119Sbill char **cp; 6321119Sbill int ofile; 6331119Sbill 6341119Sbill for (;;) { 6351119Sbill getdir(); 6361119Sbill if (endtape()) 6371119Sbill break; 6381119Sbill if (*argv == 0) 6391119Sbill goto gotit; 6401119Sbill for (cp = argv; *cp; cp++) 6411119Sbill if (prefix(*cp, dblock.dbuf.name)) 6421119Sbill goto gotit; 6431119Sbill passtape(); 6441119Sbill continue; 6451119Sbill 6461119Sbill gotit: 6471119Sbill if (checkw('x', dblock.dbuf.name) == 0) { 6481119Sbill passtape(); 6491119Sbill continue; 6501119Sbill } 65112154Ssam if (Fflag) { 65212154Ssam char *s; 65312154Ssam 65412154Ssam if ((s = rindex(dblock.dbuf.name, '/')) == 0) 65512154Ssam s = dblock.dbuf.name; 65612154Ssam else 65712154Ssam s++; 65812154Ssam if (checkf(s, stbuf.st_mode, Fflag) == 0) { 65912154Ssam passtape(); 66012154Ssam continue; 66112154Ssam } 66212154Ssam } 66322688Slepreau if (checkdir(dblock.dbuf.name)) { /* have a directory */ 66422688Slepreau if (mflag == 0) 66522688Slepreau dodirtimes(&dblock); 6666250Sroot continue; 66722688Slepreau } 66822688Slepreau if (dblock.dbuf.linkflag == '2') { /* symlink */ 669*25250Sbloom /* 670*25250Sbloom * only unlink non directories or empty 671*25250Sbloom * directories 672*25250Sbloom */ 673*25250Sbloom if (rmdir(dblock.dbuf.name) < 0) { 674*25250Sbloom if (errno == ENOTDIR) 675*25250Sbloom unlink(dblock.dbuf.name); 676*25250Sbloom } 6776250Sroot if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) { 67813492Ssam fprintf(stderr, "tar: %s: symbolic link failed\n", 67913492Ssam dblock.dbuf.name); 6806250Sroot continue; 6816250Sroot } 6826250Sroot if (vflag) 68321910Skjd fprintf(vfile, "x %s symbolic link to %s\n", 68413492Ssam dblock.dbuf.name, dblock.dbuf.linkname); 68522353Skjd #ifdef notdef 68622353Skjd /* ignore alien orders */ 68722353Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 68822688Slepreau if (mflag == 0) 68922688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime); 69022353Skjd if (pflag) 69122353Skjd chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 69222353Skjd #endif 6931119Sbill continue; 6946250Sroot } 69522688Slepreau if (dblock.dbuf.linkflag == '1') { /* regular link */ 696*25250Sbloom /* 697*25250Sbloom * only unlink non directories or empty 698*25250Sbloom * directories 699*25250Sbloom */ 700*25250Sbloom if (rmdir(dblock.dbuf.name) < 0) { 701*25250Sbloom if (errno == ENOTDIR) 702*25250Sbloom unlink(dblock.dbuf.name); 703*25250Sbloom } 7041119Sbill if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) { 70513492Ssam fprintf(stderr, "tar: %s: cannot link\n", 70613492Ssam dblock.dbuf.name); 7071119Sbill continue; 7081119Sbill } 7091119Sbill if (vflag) 71022688Slepreau fprintf(vfile, "%s linked to %s\n", 71113492Ssam dblock.dbuf.name, dblock.dbuf.linkname); 7121119Sbill continue; 7131119Sbill } 7146250Sroot if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) { 7156250Sroot fprintf(stderr, "tar: %s - cannot create\n", 71613492Ssam dblock.dbuf.name); 7171119Sbill passtape(); 7181119Sbill continue; 7191119Sbill } 72021457Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid); 7211119Sbill blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK; 7221119Sbill if (vflag) 72322688Slepreau fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n", 72413492Ssam dblock.dbuf.name, bytes, blocks); 72521457Skjd for (; blocks > 0;) { 72621457Skjd register int nread; 72721457Skjd char *bufp; 72821457Skjd register int nwant; 72921457Skjd 73021457Skjd nwant = NBLOCK*TBLOCK; 73121457Skjd if (nwant > (blocks*TBLOCK)) 73221457Skjd nwant = (blocks*TBLOCK); 73321457Skjd nread = readtbuf(&bufp, nwant); 73422688Slepreau if (write(ofile, bufp, (int)min(nread, bytes)) < 0) { 7356250Sroot fprintf(stderr, 73613492Ssam "tar: %s: HELP - extract write error\n", 73713492Ssam dblock.dbuf.name); 7386250Sroot done(2); 7396250Sroot } 74021457Skjd bytes -= nread; 74121457Skjd blocks -= (((nread-1)/TBLOCK)+1); 7421119Sbill } 7431119Sbill close(ofile); 74422688Slepreau if (mflag == 0) 74522688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime); 7461926Swnj if (pflag) 7476250Sroot chmod(dblock.dbuf.name, stbuf.st_mode & 07777); 7481119Sbill } 74922688Slepreau if (mflag == 0) { 75022688Slepreau dblock.dbuf.name[0] = '\0'; /* process the whole stack */ 75122688Slepreau dodirtimes(&dblock); 75222688Slepreau } 7531119Sbill } 7541119Sbill 7551119Sbill dotable() 7561119Sbill { 7571119Sbill for (;;) { 7581119Sbill getdir(); 7591119Sbill if (endtape()) 7601119Sbill break; 7611119Sbill if (vflag) 7621119Sbill longt(&stbuf); 7631119Sbill printf("%s", dblock.dbuf.name); 7641119Sbill if (dblock.dbuf.linkflag == '1') 7651119Sbill printf(" linked to %s", dblock.dbuf.linkname); 7666250Sroot if (dblock.dbuf.linkflag == '2') 7676250Sroot printf(" symbolic link to %s", dblock.dbuf.linkname); 7681119Sbill printf("\n"); 7691119Sbill passtape(); 7701119Sbill } 7711119Sbill } 7721119Sbill 7731119Sbill putempty() 7741119Sbill { 7751119Sbill char buf[TBLOCK]; 7761119Sbill 77713492Ssam bzero(buf, sizeof (buf)); 7781119Sbill writetape(buf); 7791119Sbill } 7801119Sbill 7811119Sbill longt(st) 7826250Sroot register struct stat *st; 7831119Sbill { 7841119Sbill register char *cp; 7851119Sbill char *ctime(); 7861119Sbill 7871119Sbill pmode(st); 7881119Sbill printf("%3d/%1d", st->st_uid, st->st_gid); 7891119Sbill printf("%7D", st->st_size); 7901119Sbill cp = ctime(&st->st_mtime); 7911119Sbill printf(" %-12.12s %-4.4s ", cp+4, cp+20); 7921119Sbill } 7931119Sbill 7941119Sbill #define SUID 04000 7951119Sbill #define SGID 02000 7961119Sbill #define ROWN 0400 7971119Sbill #define WOWN 0200 7981119Sbill #define XOWN 0100 7991119Sbill #define RGRP 040 8001119Sbill #define WGRP 020 8011119Sbill #define XGRP 010 8021119Sbill #define ROTH 04 8031119Sbill #define WOTH 02 8041119Sbill #define XOTH 01 8051119Sbill #define STXT 01000 8061119Sbill int m1[] = { 1, ROWN, 'r', '-' }; 8071119Sbill int m2[] = { 1, WOWN, 'w', '-' }; 8081119Sbill int m3[] = { 2, SUID, 's', XOWN, 'x', '-' }; 8091119Sbill int m4[] = { 1, RGRP, 'r', '-' }; 8101119Sbill int m5[] = { 1, WGRP, 'w', '-' }; 8111119Sbill int m6[] = { 2, SGID, 's', XGRP, 'x', '-' }; 8121119Sbill int m7[] = { 1, ROTH, 'r', '-' }; 8131119Sbill int m8[] = { 1, WOTH, 'w', '-' }; 8141119Sbill int m9[] = { 2, STXT, 't', XOTH, 'x', '-' }; 8151119Sbill 8161119Sbill int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9}; 8171119Sbill 8181119Sbill pmode(st) 8196250Sroot register struct stat *st; 8201119Sbill { 8211119Sbill register int **mp; 8221119Sbill 8231119Sbill for (mp = &m[0]; mp < &m[9];) 8241119Sbill select(*mp++, st); 8251119Sbill } 8261119Sbill 8271119Sbill select(pairp, st) 8286250Sroot int *pairp; 8296250Sroot struct stat *st; 8301119Sbill { 8311119Sbill register int n, *ap; 8321119Sbill 8331119Sbill ap = pairp; 8341119Sbill n = *ap++; 8351119Sbill while (--n>=0 && (st->st_mode&*ap++)==0) 8361119Sbill ap++; 8371119Sbill printf("%c", *ap); 8381119Sbill } 8391119Sbill 84022688Slepreau /* 84122688Slepreau * Make all directories needed by `name'. If `name' is a 84222688Slepreau * directory itself on the tar tape (indicated by a trailing '/'), 84322688Slepreau * return 1; else 0. 84422688Slepreau */ 8451119Sbill checkdir(name) 8466250Sroot register char *name; 8471119Sbill { 8481119Sbill register char *cp; 8496250Sroot 85012154Ssam /* 85122688Slepreau * Quick check for existence of directory. 85212154Ssam */ 85312154Ssam if ((cp = rindex(name, '/')) == 0) 85412154Ssam return (0); 85512154Ssam *cp = '\0'; 85622688Slepreau if (access(name, 0) == 0) { /* already exists */ 85712154Ssam *cp = '/'; 85822688Slepreau return (cp[1] == '\0'); /* return (lastchar == '/') */ 85912154Ssam } 86012154Ssam *cp = '/'; 86112154Ssam 86212154Ssam /* 86312154Ssam * No luck, try to make all directories in path. 86412154Ssam */ 8651119Sbill for (cp = name; *cp; cp++) { 8666250Sroot if (*cp != '/') 8676250Sroot continue; 8686250Sroot *cp = '\0'; 86912154Ssam if (access(name, 0) < 0) { 8709844Ssam if (mkdir(name, 0777) < 0) { 8719844Ssam perror(name); 87212154Ssam *cp = '/'; 87312154Ssam return (0); 8741119Sbill } 87521457Skjd chown(name, stbuf.st_uid, stbuf.st_gid); 87614956Skarels if (pflag && cp[1] == '\0') 8779844Ssam chmod(name, stbuf.st_mode & 0777); 8781119Sbill } 8796250Sroot *cp = '/'; 8801119Sbill } 8816250Sroot return (cp[-1]=='/'); 8821119Sbill } 8831119Sbill 8841119Sbill onintr() 8851119Sbill { 8861119Sbill signal(SIGINT, SIG_IGN); 8871119Sbill term++; 8881119Sbill } 8891119Sbill 8901119Sbill onquit() 8911119Sbill { 8921119Sbill signal(SIGQUIT, SIG_IGN); 8931119Sbill term++; 8941119Sbill } 8951119Sbill 8961119Sbill onhup() 8971119Sbill { 8981119Sbill signal(SIGHUP, SIG_IGN); 8991119Sbill term++; 9001119Sbill } 9011119Sbill 90222688Slepreau #ifdef notdef 9031119Sbill onterm() 9041119Sbill { 9051119Sbill signal(SIGTERM, SIG_IGN); 9061119Sbill term++; 9071119Sbill } 90822688Slepreau #endif 9091119Sbill 9101119Sbill tomodes(sp) 9111119Sbill register struct stat *sp; 9121119Sbill { 9131119Sbill register char *cp; 9141119Sbill 9151119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 9161119Sbill *cp = '\0'; 9171119Sbill sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777); 9181119Sbill sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid); 9191119Sbill sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid); 9201119Sbill sprintf(dblock.dbuf.size, "%11lo ", sp->st_size); 9211119Sbill sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime); 9221119Sbill } 9231119Sbill 9241119Sbill checksum() 9251119Sbill { 9261119Sbill register i; 9271119Sbill register char *cp; 9281119Sbill 9296250Sroot for (cp = dblock.dbuf.chksum; 9306250Sroot cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++) 9311119Sbill *cp = ' '; 9321119Sbill i = 0; 9331119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++) 9341119Sbill i += *cp; 9356250Sroot return (i); 9361119Sbill } 9371119Sbill 9381119Sbill checkw(c, name) 9396250Sroot char *name; 9401119Sbill { 9416250Sroot if (!wflag) 9426250Sroot return (1); 9436250Sroot printf("%c ", c); 9446250Sroot if (vflag) 9456250Sroot longt(&stbuf); 9466250Sroot printf("%s: ", name); 9476250Sroot return (response() == 'y'); 9481119Sbill } 9491119Sbill 9501119Sbill response() 9511119Sbill { 9521119Sbill char c; 9531119Sbill 9541119Sbill c = getchar(); 9551119Sbill if (c != '\n') 9566250Sroot while (getchar() != '\n') 9576250Sroot ; 9586250Sroot else 9596250Sroot c = 'n'; 9606250Sroot return (c); 9611119Sbill } 9621119Sbill 96312154Ssam checkf(name, mode, howmuch) 96412154Ssam char *name; 96512154Ssam int mode, howmuch; 96612154Ssam { 96712154Ssam int l; 96812154Ssam 96921910Skjd if ((mode & S_IFMT) == S_IFDIR){ 97021910Skjd if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0)) 97121910Skjd return(0); 97221910Skjd return(1); 97321910Skjd } 97412154Ssam if ((l = strlen(name)) < 3) 97512154Ssam return (1); 97612154Ssam if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o') 97712154Ssam return (0); 97812154Ssam if (strcmp(name, "core") == 0 || 97912154Ssam strcmp(name, "errs") == 0 || 98012154Ssam (howmuch > 1 && strcmp(name, "a.out") == 0)) 98112154Ssam return (0); 98212154Ssam /* SHOULD CHECK IF IT IS EXECUTABLE */ 98312154Ssam return (1); 98412154Ssam } 98512154Ssam 98622688Slepreau /* Is the current file a new file, or the newest one of the same name? */ 9871119Sbill checkupdate(arg) 9886250Sroot char *arg; 9891119Sbill { 9901119Sbill char name[100]; 9916250Sroot long mtime; 9921119Sbill daddr_t seekp; 9931119Sbill daddr_t lookup(); 9941119Sbill 9951119Sbill rewind(tfile); 9961119Sbill for (;;) { 9971119Sbill if ((seekp = lookup(arg)) < 0) 9986250Sroot return (1); 9991119Sbill fseek(tfile, seekp, 0); 10001119Sbill fscanf(tfile, "%s %lo", name, &mtime); 10016250Sroot return (stbuf.st_mtime > mtime); 10021119Sbill } 10031119Sbill } 10041119Sbill 10051119Sbill done(n) 10061119Sbill { 100721457Skjd unlink(tname); 10081119Sbill exit(n); 10091119Sbill } 10101119Sbill 101122688Slepreau /* 101222688Slepreau * Does s2 begin with the string s1, on a directory boundary? 101322688Slepreau */ 10141119Sbill prefix(s1, s2) 10156250Sroot register char *s1, *s2; 10161119Sbill { 10171119Sbill while (*s1) 10181119Sbill if (*s1++ != *s2++) 10196250Sroot return (0); 10201119Sbill if (*s2) 10216250Sroot return (*s2 == '/'); 10226250Sroot return (1); 10231119Sbill } 10241119Sbill 10251119Sbill #define N 200 10261119Sbill int njab; 10276250Sroot 10281119Sbill daddr_t 10291119Sbill lookup(s) 10306250Sroot char *s; 10311119Sbill { 10321119Sbill register i; 10331119Sbill daddr_t a; 10341119Sbill 10351119Sbill for(i=0; s[i]; i++) 10366250Sroot if (s[i] == ' ') 10371119Sbill break; 10381119Sbill a = bsrch(s, i, low, high); 10396250Sroot return (a); 10401119Sbill } 10411119Sbill 10421119Sbill daddr_t 10431119Sbill bsrch(s, n, l, h) 10446250Sroot daddr_t l, h; 10456250Sroot char *s; 10461119Sbill { 10471119Sbill register i, j; 10481119Sbill char b[N]; 10491119Sbill daddr_t m, m1; 10501119Sbill 10511119Sbill njab = 0; 10521119Sbill 10531119Sbill loop: 10546250Sroot if (l >= h) 105522688Slepreau return ((daddr_t) -1); 10561119Sbill m = l + (h-l)/2 - N/2; 10576250Sroot if (m < l) 10581119Sbill m = l; 10591119Sbill fseek(tfile, m, 0); 10601119Sbill fread(b, 1, N, tfile); 10611119Sbill njab++; 10621119Sbill for(i=0; i<N; i++) { 10636250Sroot if (b[i] == '\n') 10641119Sbill break; 10651119Sbill m++; 10661119Sbill } 10676250Sroot if (m >= h) 106822688Slepreau return ((daddr_t) -1); 10691119Sbill m1 = m; 10701119Sbill j = i; 10711119Sbill for(i++; i<N; i++) { 10721119Sbill m1++; 10736250Sroot if (b[i] == '\n') 10741119Sbill break; 10751119Sbill } 10761119Sbill i = cmp(b+j, s, n); 10776250Sroot if (i < 0) { 10781119Sbill h = m; 10791119Sbill goto loop; 10801119Sbill } 10816250Sroot if (i > 0) { 10821119Sbill l = m1; 10831119Sbill goto loop; 10841119Sbill } 10856250Sroot return (m); 10861119Sbill } 10871119Sbill 10881119Sbill cmp(b, s, n) 10896250Sroot char *b, *s; 10901119Sbill { 10911119Sbill register i; 10921119Sbill 10936250Sroot if (b[0] != '\n') 109421457Skjd exit(2); 10951119Sbill for(i=0; i<n; i++) { 10966250Sroot if (b[i+1] > s[i]) 10976250Sroot return (-1); 10986250Sroot if (b[i+1] < s[i]) 10996250Sroot return (1); 11001119Sbill } 11016250Sroot return (b[i+1] == ' '? 0 : -1); 11021119Sbill } 11031119Sbill 110422688Slepreau readtape(buffer) 11056250Sroot char *buffer; 11061119Sbill { 110721457Skjd char *bufp; 110821457Skjd 110922688Slepreau if (first == 0) 111022688Slepreau getbuf(); 111122688Slepreau readtbuf(&bufp, TBLOCK); 111221457Skjd bcopy(bufp, buffer, TBLOCK); 111321457Skjd return(TBLOCK); 111421457Skjd } 111521457Skjd 111621457Skjd readtbuf(bufpp, size) 111721457Skjd char **bufpp; 111821457Skjd int size; 111921457Skjd { 11203457Swnj register int i; 11211119Sbill 11221119Sbill if (recno >= nblock || first == 0) { 11238737Smckusick if ((i = bread(mt, tbuf, TBLOCK*nblock)) < 0) { 112413492Ssam fprintf(stderr, "tar: tape read error\n"); 11251119Sbill done(3); 11261119Sbill } 11271119Sbill if (first == 0) { 11281119Sbill if ((i % TBLOCK) != 0) { 112913492Ssam fprintf(stderr, "tar: tape blocksize error\n"); 11301119Sbill done(3); 11311119Sbill } 11321119Sbill i /= TBLOCK; 11333457Swnj if (i != nblock) { 113413492Ssam fprintf(stderr, "tar: blocksize = %d\n", i); 11351119Sbill nblock = i; 11361119Sbill } 113722353Skjd first = 1; 11381119Sbill } 11391119Sbill recno = 0; 11401119Sbill } 114121457Skjd if (size > ((nblock-recno)*TBLOCK)) 114221457Skjd size = (nblock-recno)*TBLOCK; 114321457Skjd *bufpp = (char *)&tbuf[recno]; 114421457Skjd recno += (size/TBLOCK); 114521457Skjd return (size); 11461119Sbill } 11471119Sbill 114821457Skjd writetbuf(buffer, n) 114921457Skjd register char *buffer; 115021457Skjd register int n; 11511119Sbill { 115222688Slepreau 115322688Slepreau if (first == 0) { 115422353Skjd getbuf(); 115522353Skjd first = 1; 115622353Skjd } 11571119Sbill if (recno >= nblock) { 115822688Slepreau if (write(mt, tbuf, TBLOCK*nblock) < 0) { 115922688Slepreau fprintf(stderr,"tar: tape write error\n"); 11601119Sbill done(2); 11611119Sbill } 11621119Sbill recno = 0; 11631119Sbill } 116421457Skjd 116521457Skjd /* 116621457Skjd * Special case: We have an empty tape buffer, and the 116721457Skjd * users data size is >= the tape block size: Avoid 116821457Skjd * the bcopy and dma direct to tape. BIG WIN. Add the 116921457Skjd * residual to the tape buffer. 117021457Skjd */ 117121457Skjd while (recno == 0 && n >= nblock) { 117221457Skjd if (write(mt, buffer, TBLOCK*nblock) < 0) { 117322688Slepreau fprintf(stderr,"tar: tape write error\n"); 11741119Sbill done(2); 11751119Sbill } 117621457Skjd n -= nblock; 117721457Skjd buffer += (nblock * TBLOCK); 11781119Sbill } 117921457Skjd 118021457Skjd while (n-- > 0) { 118121457Skjd bcopy(buffer, (char *)&tbuf[recno++], TBLOCK); 118221457Skjd buffer += TBLOCK; 118321457Skjd if (recno >= nblock) { 118421457Skjd if (write(mt, tbuf, TBLOCK*nblock) < 0) { 118522353Skjd fprintf(stderr,"tar: tape write error\n"); 118622353Skjd done(2); 118721457Skjd } 118821457Skjd recno = 0; 118921457Skjd } 119021457Skjd } 119121457Skjd 119221457Skjd /* Tell the user how much to write to get in sync */ 119321457Skjd return (nblock - recno); 11941119Sbill } 11951119Sbill 11961119Sbill backtape() 11971119Sbill { 11983457Swnj static struct mtop mtop = {MTBSR, 1}; 11993457Swnj 12003457Swnj if (mtdev == 0) { 12013457Swnj if (ioctl(mt, MTIOCTOP, &mtop) < 0) { 120213492Ssam fprintf(stderr, "tar: tape backspace error\n"); 12031119Sbill done(4); 12041119Sbill } 12053457Swnj } else 120622688Slepreau lseek(mt, (daddr_t) -TBLOCK*nblock, 1); 12073457Swnj recno--; 12081119Sbill } 12091119Sbill 12101119Sbill flushtape() 12111119Sbill { 121221457Skjd write(mt, tbuf, TBLOCK*nblock); 12131119Sbill } 12141119Sbill 12158737Smckusick bread(fd, buf, size) 12168737Smckusick int fd; 12178737Smckusick char *buf; 12188737Smckusick int size; 12198737Smckusick { 12208737Smckusick int count; 12218737Smckusick static int lastread = 0; 12228737Smckusick 122322688Slepreau if (!Bflag) 122422688Slepreau return (read(fd, buf, size)); 122522353Skjd 12268737Smckusick for (count = 0; count < size; count += lastread) { 122724281Smckusick lastread = read(fd, buf, size - count); 122824281Smckusick if (lastread <= 0) { 12298737Smckusick if (count > 0) 12308737Smckusick return (count); 12318737Smckusick return (lastread); 12328737Smckusick } 12338737Smckusick buf += lastread; 12348737Smckusick } 12358737Smckusick return (count); 12368737Smckusick } 123710165Ssam 123810165Ssam char * 123910165Ssam getcwd(buf) 124010165Ssam char *buf; 124110165Ssam { 124210165Ssam if (getwd(buf) == NULL) { 124310165Ssam fprintf(stderr, "tar: %s\n", buf); 124421457Skjd exit(1); 124510165Ssam } 124610165Ssam return (buf); 124710165Ssam } 124821910Skjd 124921910Skjd getbuf() 125021910Skjd { 125122353Skjd 125222688Slepreau if (mtdev == 1) { 125321910Skjd fstat(mt, &stbuf); 125421910Skjd if ((stbuf.st_mode & S_IFMT) == S_IFCHR) 125521910Skjd mtdev = 0; 125621910Skjd else 125721910Skjd mtdev = -1; 125821910Skjd } 125922688Slepreau if (nblock == 0) { 126021910Skjd if (mtdev == 0) 126121910Skjd nblock = FILEBLOCK; 126221910Skjd else { 126321910Skjd fstat(mt, &stbuf); 126421910Skjd nblock = stbuf.st_blocks / TBLOCK; 126521910Skjd } 126621910Skjd } 126722688Slepreau if (nblock == 0) 126822688Slepreau nblock = FILEBLOCK; 126921910Skjd tbuf = (union hblock *)malloc(nblock*TBLOCK); 127021910Skjd if (tbuf == NULL) { 127121910Skjd fprintf(stderr, "tar: blocksize %d too big, can't get memory\n", 127221910Skjd nblock); 127321910Skjd done(1); 127421910Skjd } 127521910Skjd } 127622353Skjd 127722688Slepreau /* 127822688Slepreau * We are really keeping a directory stack, but since all the 127922688Slepreau * elements of it share a common prefix, we can make do with one 128022688Slepreau * string. We keep only the current directory path, with an associated 128122688Slepreau * array of mtime's, one for each '/' in the path. A negative mtime 128222688Slepreau * means no mtime. The mtime's are offset by one (first index 1, not 128322688Slepreau * 0) because calling this with the null string causes mtime[0] to be set. 128422688Slepreau * 128522688Slepreau * This stack algorithm is not guaranteed to work for tapes created 128622688Slepreau * with the 'r' option, but the vast majority of tapes with 128722688Slepreau * directories are not. This avoids saving every directory record on 128822688Slepreau * the tape and setting all the times at the end. 128922688Slepreau */ 129022688Slepreau char dirstack[NAMSIZ]; 129122688Slepreau #define NTIM (NAMSIZ/2+1) /* a/b/c/d/... */ 129222688Slepreau time_t mtime[NTIM]; 129322353Skjd 129422688Slepreau dodirtimes(hp) 129522688Slepreau union hblock *hp; 129622353Skjd { 129722688Slepreau register char *p = dirstack; 129822688Slepreau register char *q = hp->dbuf.name; 129922688Slepreau register int ndir = 0; 130022688Slepreau char *savp; 130122688Slepreau int savndir; 130222353Skjd 130322688Slepreau /* Find common prefix */ 130422688Slepreau while (*p == *q) { 130522688Slepreau if (*p++ == '/') 130622688Slepreau ++ndir; 130722688Slepreau q++; 130822688Slepreau } 130922353Skjd 131022688Slepreau savp = p; 131122688Slepreau savndir = ndir; 131222688Slepreau while (*p) { 131322688Slepreau /* 131422688Slepreau * Not a child: unwind the stack, setting the times. 131522688Slepreau * The order we do this doesn't matter, so we go "forward." 131622688Slepreau */ 131722688Slepreau if (*p++ == '/') 131822688Slepreau if (mtime[++ndir] >= 0) { 131922688Slepreau *--p = '\0'; /* zap the slash */ 132022688Slepreau setimes(dirstack, mtime[ndir]); 132122688Slepreau *p++ = '/'; 132222688Slepreau } 132322688Slepreau } 132422688Slepreau p = savp; 132522688Slepreau ndir = savndir; 132622353Skjd 132722688Slepreau /* Push this one on the "stack" */ 132822688Slepreau while (*p = *q++) /* append the rest of the new dir */ 132922688Slepreau if (*p++ == '/') 133022688Slepreau mtime[++ndir] = -1; 133122688Slepreau mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */ 133222688Slepreau } 133322353Skjd 133422688Slepreau setimes(path, mt) 133522688Slepreau char *path; 133622688Slepreau time_t mt; 133722688Slepreau { 133822688Slepreau struct timeval tv[2]; 133922353Skjd 134022688Slepreau tv[0].tv_sec = time((time_t *) 0); 134122688Slepreau tv[1].tv_sec = mt; 134222688Slepreau tv[0].tv_usec = tv[1].tv_usec = 0; 134322688Slepreau if (utimes(path, tv) < 0) 134422688Slepreau fprintf(stderr, "tar: can't set time on %s\n", path); 134522688Slepreau } 134622688Slepreau 134722688Slepreau char * 134822688Slepreau getmem(size) 134922688Slepreau { 135022688Slepreau char *p = malloc((unsigned) size); 135122688Slepreau 135222688Slepreau if (p == NULL && freemem) { 135322688Slepreau fprintf(stderr, 135422688Slepreau "tar: out of memory, link and directory modtime info lost\n"); 135522688Slepreau freemem = 0; 135622353Skjd } 135722688Slepreau return (p); 135822353Skjd } 1359