111127Smckusick /* Copyright (c) 1983 Regents of the University of California */ 211127Smckusick 311127Smckusick #ifndef lint 4*11744Smckusick static char sccsid[] = "@(#)dirs.c 3.7 (Berkeley) 83/03/27"; 511127Smckusick #endif 611127Smckusick 711127Smckusick #include "restore.h" 811127Smckusick #include <dumprestor.h> 911309Smckusick #include <sys/file.h> 1011127Smckusick #include <dir.h> 1111127Smckusick 1211127Smckusick #define HASHSIZE 1000 1311127Smckusick 1411127Smckusick #define INOHASH(val) (val % HASHSIZE) 1511127Smckusick struct inotab { 1611127Smckusick struct inotab *t_next; 1711127Smckusick ino_t t_ino; 1811127Smckusick daddr_t t_seekpt; 1911127Smckusick long t_size; 2011309Smckusick }; 2111309Smckusick static struct inotab *inotab[HASHSIZE]; 2211309Smckusick extern struct inotab *inotablookup(); 2311322Smckusick extern struct inotab *allocinotab(); 2411127Smckusick 2511127Smckusick struct modeinfo { 2611127Smckusick ino_t ino; 2711127Smckusick time_t timep[2]; 2811127Smckusick short mode; 2911127Smckusick short uid; 3011127Smckusick short gid; 3111127Smckusick }; 3211127Smckusick 3311309Smckusick static daddr_t seekpt; 3411309Smckusick static FILE *df, *mf; 3511309Smckusick static DIR *dirp; 3611309Smckusick static char dirfile[] = "/tmp/rstaXXXXXX"; 3711309Smckusick extern ino_t search(); 3811127Smckusick 3911127Smckusick #define ODIRSIZ 14 4011127Smckusick struct odirect { 4111127Smckusick u_short d_ino; 4211127Smckusick char d_name[ODIRSIZ]; 4311127Smckusick }; 4411127Smckusick 4511127Smckusick /* 4611127Smckusick * Extract directory contents, building up a directory structure 4711127Smckusick * on disk for extraction by name. 4811127Smckusick * If modefile is requested, save mode, owner, and times for all 4911127Smckusick * directories on the tape. 5011127Smckusick */ 5111127Smckusick extractdirs(modefile) 5211127Smckusick char *modefile; 5311127Smckusick { 5411127Smckusick register int i; 5511127Smckusick register struct dinode *ip; 5611322Smckusick struct inotab *itp; 5711127Smckusick struct direct nulldir; 5811127Smckusick int putdir(), null(); 5911127Smckusick 6011127Smckusick vprintf(stdout, "Extract directories from tape\n"); 6111421Smckusick (void) mktemp(dirfile); 6211127Smckusick df = fopen(dirfile, "w"); 6311127Smckusick if (df == 0) { 6411127Smckusick fprintf(stderr, 6511127Smckusick "restor: %s - cannot create directory temporary\n", 6611127Smckusick dirfile); 6711127Smckusick perror("fopen"); 6811127Smckusick done(1); 6911127Smckusick } 7011127Smckusick if (modefile != NULL) { 7111127Smckusick mf = fopen(modefile, "w"); 7211309Smckusick if (mf == 0) { 7311127Smckusick fprintf(stderr, 7411127Smckusick "restor: %s - cannot create modefile \n", 7511127Smckusick modefile); 7611127Smckusick perror("fopen"); 7711127Smckusick done(1); 7811127Smckusick } 7911127Smckusick } 8011322Smckusick nulldir.d_ino = 0; 8111127Smckusick nulldir.d_namlen = 1; 8211127Smckusick strncpy(nulldir.d_name, "/", nulldir.d_namlen); 8311127Smckusick nulldir.d_reclen = DIRSIZ(&nulldir); 8411127Smckusick for (;;) { 8511127Smckusick curfile.name = "<directory file - name unknown>"; 8611127Smckusick curfile.action = USING; 8711127Smckusick ip = curfile.dip; 8811127Smckusick i = ip->di_mode & IFMT; 8911127Smckusick if (i != IFDIR) { 9011732Smckusick (void) fclose(df); 9111127Smckusick dirp = opendir(dirfile); 9211127Smckusick if (dirp == NULL) 9311127Smckusick perror("opendir"); 9411127Smckusick if (mf != NULL) 9511732Smckusick (void) fclose(mf); 9611421Smckusick if ((i = psearch(".")) == 0 || BIT(i, dumpmap) == 0) 9711421Smckusick panic("Root directory is not on tape\n"); 9811127Smckusick return; 9911127Smckusick } 10011322Smckusick itp = allocinotab(curfile.ino, ip, seekpt); 10111127Smckusick getfile(putdir, null); 10211127Smckusick putent(&nulldir); 10311127Smckusick flushent(); 10411322Smckusick itp->t_size = seekpt - itp->t_seekpt; 10511127Smckusick } 10611127Smckusick } 10711127Smckusick 10811127Smckusick /* 10911322Smckusick * skip over all the directories on the tape 11011322Smckusick */ 11111322Smckusick skipdirs() 11211322Smckusick { 11311322Smckusick 11411322Smckusick while ((curfile.dip->di_mode & IFMT) == IFDIR) { 11511322Smckusick skipfile(); 11611322Smckusick } 11711322Smckusick } 11811322Smckusick 11911322Smckusick /* 12011127Smckusick * Recursively find names and inumbers of all files in subtree 12111127Smckusick * pname and pass them off to be processed. 12211127Smckusick */ 12311127Smckusick treescan(pname, ino, todo) 12411127Smckusick char *pname; 12511127Smckusick ino_t ino; 126*11744Smckusick long (*todo)(); 12711127Smckusick { 12811127Smckusick register struct inotab *itp; 12911127Smckusick int namelen; 13011127Smckusick daddr_t bpt; 13111127Smckusick register struct direct *dp; 13211644Smckusick char locname[MAXPATHLEN + 1]; 13311127Smckusick 13411127Smckusick itp = inotablookup(ino); 13511127Smckusick if (itp == NULL) { 13611127Smckusick /* 13711127Smckusick * Pname is name of a simple file or an unchanged directory. 13811127Smckusick */ 139*11744Smckusick (void) (*todo)(pname, ino, LEAF); 14011127Smckusick return; 14111127Smckusick } 14211127Smckusick /* 14311127Smckusick * Pname is a dumped directory name. 14411127Smckusick */ 145*11744Smckusick if ((*todo)(pname, ino, NODE) == FAIL) 146*11744Smckusick return; 14711127Smckusick /* 14811127Smckusick * begin search through the directory 14911127Smckusick * skipping over "." and ".." 15011127Smckusick */ 15111644Smckusick strncpy(locname, pname, MAXPATHLEN); 15211644Smckusick strncat(locname, "/", MAXPATHLEN); 15311127Smckusick namelen = strlen(locname); 15411127Smckusick seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 15511127Smckusick dp = readdir(dirp); /* "." */ 15611127Smckusick dp = readdir(dirp); /* ".." */ 15711127Smckusick dp = readdir(dirp); /* first real entry */ 15811127Smckusick bpt = telldir(dirp); 15911127Smckusick /* 16011127Smckusick * "/" signals end of directory 16111127Smckusick */ 16211127Smckusick while (dp != NULL && !(dp->d_namlen == 1 && dp->d_name[0] == '/')) { 16311127Smckusick locname[namelen] = '\0'; 16411644Smckusick if (namelen + dp->d_namlen >= MAXPATHLEN) { 16511127Smckusick fprintf(stderr, "%s%s: name exceeds %d char\n", 16611644Smckusick locname, dp->d_name, MAXPATHLEN); 16711127Smckusick } else { 16811127Smckusick strncat(locname, dp->d_name, dp->d_namlen); 16911127Smckusick treescan(locname, dp->d_ino, todo); 17011127Smckusick seekdir(dirp, bpt, itp->t_seekpt); 17111127Smckusick } 17211127Smckusick dp = readdir(dirp); 17311127Smckusick bpt = telldir(dirp); 17411127Smckusick } 17511127Smckusick if (dp == NULL) 17611127Smckusick fprintf(stderr, "corrupted directory: %s.\n", locname); 17711127Smckusick } 17811127Smckusick 17911127Smckusick /* 18011127Smckusick * Search the directory tree rooted at inode ROOTINO 18111127Smckusick * for the path pointed at by n 18211127Smckusick */ 18311127Smckusick ino_t 18411127Smckusick psearch(n) 18511127Smckusick char *n; 18611127Smckusick { 18711127Smckusick register char *cp, *cp1; 18811127Smckusick ino_t ino; 18911127Smckusick char c; 19011127Smckusick 19111127Smckusick ino = ROOTINO; 19211127Smckusick if (*(cp = n) == '/') 19311127Smckusick cp++; 19411127Smckusick next: 19511127Smckusick cp1 = cp + 1; 19611127Smckusick while (*cp1 != '/' && *cp1) 19711127Smckusick cp1++; 19811127Smckusick c = *cp1; 19911127Smckusick *cp1 = 0; 20011127Smckusick ino = search(ino, cp); 20111127Smckusick if (ino == 0) { 20211127Smckusick *cp1 = c; 20311127Smckusick return(0); 20411127Smckusick } 20511127Smckusick *cp1 = c; 20611127Smckusick if (c == '/') { 20711127Smckusick cp = cp1+1; 20811127Smckusick goto next; 20911127Smckusick } 21011127Smckusick return(ino); 21111127Smckusick } 21211127Smckusick 21311127Smckusick /* 21411127Smckusick * search the directory inode ino 21511127Smckusick * looking for entry cp 21611127Smckusick */ 21711127Smckusick ino_t 21811127Smckusick search(inum, cp) 21911127Smckusick ino_t inum; 22011127Smckusick char *cp; 22111127Smckusick { 22211127Smckusick register struct direct *dp; 22311127Smckusick register struct inotab *itp; 22411127Smckusick int len; 22511127Smckusick 22611127Smckusick itp = inotablookup(inum); 22711127Smckusick if (itp == NULL) 22811127Smckusick return(0); 22911127Smckusick seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 23011127Smckusick len = strlen(cp); 23111127Smckusick do { 23211127Smckusick dp = readdir(dirp); 23311127Smckusick if (dp->d_namlen == 1 && dp->d_name[0] == '/') 23411127Smckusick return(0); 23511127Smckusick } while (dp->d_namlen != len || strncmp(dp->d_name, cp, len)); 23611127Smckusick return(dp->d_ino); 23711127Smckusick } 23811127Smckusick 23911127Smckusick /* 24011127Smckusick * Put the directory entries in the directory file 24111127Smckusick */ 24211127Smckusick putdir(buf, size) 24311127Smckusick char *buf; 24411127Smckusick int size; 24511127Smckusick { 24611127Smckusick struct direct cvtbuf; 24711127Smckusick register struct odirect *odp; 24811127Smckusick struct odirect *eodp; 24911127Smckusick register struct direct *dp; 25011127Smckusick long loc, i; 25111127Smckusick 25211127Smckusick if (cvtflag) { 25311127Smckusick eodp = (struct odirect *)&buf[size]; 25411127Smckusick for (odp = (struct odirect *)buf; odp < eodp; odp++) 25511127Smckusick if (odp->d_ino != 0) { 25611127Smckusick dcvt(odp, &cvtbuf); 25711127Smckusick putent(&cvtbuf); 25811127Smckusick } 25911127Smckusick } else { 26011127Smckusick for (loc = 0; loc < size; ) { 26111127Smckusick dp = (struct direct *)(buf + loc); 26211127Smckusick i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1)); 26311127Smckusick if (dp->d_reclen == 0 || dp->d_reclen > i) { 26411127Smckusick loc += i; 26511127Smckusick continue; 26611127Smckusick } 26711127Smckusick loc += dp->d_reclen; 26811127Smckusick if (dp->d_ino != 0) { 26911127Smckusick putent(dp); 27011127Smckusick } 27111127Smckusick } 27211127Smckusick } 27311127Smckusick } 27411127Smckusick 27511127Smckusick /* 27611127Smckusick * These variables are "local" to the following two functions. 27711127Smckusick */ 27811127Smckusick char dirbuf[DIRBLKSIZ]; 27911127Smckusick long dirloc = 0; 28011127Smckusick long prev = 0; 28111127Smckusick 28211127Smckusick /* 28311127Smckusick * add a new directory entry to a file. 28411127Smckusick */ 28511127Smckusick putent(dp) 28611127Smckusick struct direct *dp; 28711127Smckusick { 28811322Smckusick dp->d_reclen = DIRSIZ(dp); 28911127Smckusick if (dirloc + dp->d_reclen > DIRBLKSIZ) { 29011127Smckusick ((struct direct *)(dirbuf + prev))->d_reclen = 29111127Smckusick DIRBLKSIZ - prev; 29211732Smckusick (void) fwrite(dirbuf, 1, DIRBLKSIZ, df); 29311127Smckusick dirloc = 0; 29411127Smckusick } 29511127Smckusick bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen); 29611127Smckusick prev = dirloc; 29711127Smckusick dirloc += dp->d_reclen; 29811127Smckusick } 29911127Smckusick 30011127Smckusick /* 30111127Smckusick * flush out a directory that is finished. 30211127Smckusick */ 30311127Smckusick flushent() 30411127Smckusick { 30511127Smckusick 30611127Smckusick ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev; 30711732Smckusick (void) fwrite(dirbuf, (int)dirloc, 1, df); 30811127Smckusick seekpt = ftell(df); 30911127Smckusick dirloc = 0; 31011127Smckusick } 31111127Smckusick 31211127Smckusick dcvt(odp, ndp) 31311127Smckusick register struct odirect *odp; 31411127Smckusick register struct direct *ndp; 31511127Smckusick { 31611127Smckusick 31711127Smckusick bzero((char *)ndp, (long)(sizeof *ndp)); 31811127Smckusick ndp->d_ino = odp->d_ino; 31911127Smckusick strncpy(ndp->d_name, odp->d_name, ODIRSIZ); 32011127Smckusick ndp->d_namlen = strlen(ndp->d_name); 32111127Smckusick ndp->d_reclen = DIRSIZ(ndp); 32211127Smckusick } 32311127Smckusick 32411127Smckusick /* 32511127Smckusick * Seek to an entry in a directory. 32611127Smckusick * Only values returned by ``telldir'' should be passed to seekdir. 32711732Smckusick * This routine handles many directories in a single file. 32811732Smckusick * It takes the base of the directory in the file, plus 32911732Smckusick * the desired seek offset into it. 33011127Smckusick */ 33111127Smckusick void 33211127Smckusick seekdir(dirp, loc, base) 33311127Smckusick register DIR *dirp; 33411127Smckusick daddr_t loc, base; 33511127Smckusick { 33611127Smckusick 33711127Smckusick if (loc == telldir(dirp)) 33811127Smckusick return; 33911127Smckusick loc -= base; 34011127Smckusick if (loc < 0) 34111127Smckusick fprintf(stderr, "bad seek pointer to seekdir %d\n", loc); 34211127Smckusick (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0); 34311127Smckusick dirp->dd_loc = loc & (DIRBLKSIZ - 1); 34411127Smckusick if (dirp->dd_loc != 0) 34511127Smckusick dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ); 34611127Smckusick } 34711127Smckusick 34811127Smckusick /* 34911127Smckusick * get next entry in a directory. 35011127Smckusick */ 35111127Smckusick struct direct * 35211127Smckusick readdir(dirp) 35311127Smckusick register DIR *dirp; 35411127Smckusick { 35511127Smckusick register struct direct *dp; 35611127Smckusick 35711127Smckusick for (;;) { 35811127Smckusick if (dirp->dd_loc == 0) { 35911127Smckusick dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 36011127Smckusick DIRBLKSIZ); 36111127Smckusick if (dirp->dd_size <= 0) 36211127Smckusick return NULL; 36311127Smckusick } 36411127Smckusick if (dirp->dd_loc >= dirp->dd_size) { 36511127Smckusick dirp->dd_loc = 0; 36611127Smckusick continue; 36711127Smckusick } 36811127Smckusick dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc); 36911127Smckusick if (dp->d_reclen == 0 || 37011127Smckusick dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) 37111127Smckusick return NULL; 37211127Smckusick dirp->dd_loc += dp->d_reclen; 37311127Smckusick return (dp); 37411127Smckusick } 37511127Smckusick } 37611127Smckusick 37711127Smckusick /* 37811127Smckusick * Set the mode, owner, and times for all new or changed directories 37911127Smckusick */ 38011127Smckusick setdirmodes(modefile) 38111127Smckusick char *modefile; 38211127Smckusick { 38311127Smckusick FILE *mf; 38411127Smckusick struct modeinfo node; 38511127Smckusick struct entry *ep; 38611127Smckusick char *cp; 38711127Smckusick 38811127Smckusick vprintf(stdout, "Set directory mode, owner, and times.\n"); 38911127Smckusick mf = fopen(modefile, "r"); 39011127Smckusick if (mf == NULL) { 39111127Smckusick perror("fopen"); 39211127Smckusick panic("cannot open mode file %s\n", modefile); 39311127Smckusick } 39411127Smckusick clearerr(mf); 39511309Smckusick for (;;) { 39611732Smckusick (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf); 39711309Smckusick if (feof(mf)) 39811309Smckusick break; 39911127Smckusick ep = lookupino(node.ino); 40011309Smckusick if (ep == NIL) { 40111309Smckusick if (command == 'x') 40211309Smckusick continue; 40311127Smckusick panic("cannot find directory inode %d\n", node.ino); 40411309Smckusick } 40511127Smckusick cp = myname(ep); 40611732Smckusick (void) chown(cp, node.uid, node.gid); 40711732Smckusick (void) chmod(cp, node.mode); 40811127Smckusick utime(cp, node.timep); 40911127Smckusick } 41011127Smckusick if (ferror(mf)) 41111127Smckusick panic("error setting directory modes\n"); 41211732Smckusick (void) fclose(mf); 41311421Smckusick (void) unlink(modefile); 41411127Smckusick } 41511127Smckusick 41611127Smckusick /* 41711127Smckusick * Generate a literal copy of a directory. 41811127Smckusick */ 41911127Smckusick genliteraldir(name, ino) 42011127Smckusick char *name; 42111127Smckusick ino_t ino; 42211127Smckusick { 42311127Smckusick register struct inotab *itp; 42411127Smckusick int ofile, dp, i, size; 42511127Smckusick char buf[BUFSIZ]; 42611127Smckusick 42711127Smckusick itp = inotablookup(ino); 42811127Smckusick if (itp == NULL) 42911322Smckusick panic("Cannot find directory inode %d named %s\n", ino, name); 43011127Smckusick if ((ofile = open(name, FWRONLY|FCREATE, 0666)) < 0) { 43111127Smckusick fprintf(stderr, "%s: cannot create file\n", name); 43211127Smckusick return (FAIL); 43311127Smckusick } 43411127Smckusick seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 43511127Smckusick dp = dup(dirp->dd_fd); 43611127Smckusick for (i = itp->t_size; i > 0; i -= BUFSIZ) { 43711127Smckusick size = i < BUFSIZ ? i : BUFSIZ; 43811127Smckusick if (read(dp, buf, (int) size) == -1) { 43911127Smckusick fprintf(stderr, 44011127Smckusick "write error extracting inode %d, name %s\n", 44111127Smckusick curfile.ino, curfile.name); 44211127Smckusick perror("read"); 44311127Smckusick done(1); 44411127Smckusick } 44511127Smckusick if (write(ofile, buf, (int) size) == -1) { 44611127Smckusick fprintf(stderr, 44711127Smckusick "write error extracting inode %d, name %s\n", 44811127Smckusick curfile.ino, curfile.name); 44911127Smckusick perror("write"); 45011127Smckusick done(1); 45111127Smckusick } 45211127Smckusick } 45311732Smckusick (void) close(dp); 45411732Smckusick (void) close(ofile); 45511127Smckusick return (GOOD); 45611127Smckusick } 45711127Smckusick 45811127Smckusick /* 45911127Smckusick * Allocate and initialize a directory inode entry. 46011127Smckusick * If requested, save its pertinent mode, owner, and time info. 46111127Smckusick */ 46211322Smckusick struct inotab * 46311127Smckusick allocinotab(ino, dip, seekpt) 46411127Smckusick ino_t ino; 46511127Smckusick struct dinode *dip; 46611127Smckusick daddr_t seekpt; 46711127Smckusick { 46811127Smckusick register struct inotab *itp; 46911127Smckusick struct modeinfo node; 47011127Smckusick 47111127Smckusick itp = (struct inotab *)calloc(1, sizeof(struct inotab)); 47211127Smckusick itp->t_next = inotab[INOHASH(ino)]; 47311127Smckusick inotab[INOHASH(ino)] = itp; 47411127Smckusick itp->t_ino = ino; 47511127Smckusick itp->t_seekpt = seekpt; 47611127Smckusick if (mf == NULL) 47711322Smckusick return(itp); 47811127Smckusick node.ino = ino; 47911127Smckusick node.timep[0] = dip->di_atime; 48011127Smckusick node.timep[1] = dip->di_mtime; 48111127Smckusick node.mode = dip->di_mode; 48211127Smckusick node.uid = dip->di_uid; 48311127Smckusick node.gid = dip->di_gid; 48411732Smckusick (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf); 48511322Smckusick return(itp); 48611127Smckusick } 48711127Smckusick 48811127Smckusick /* 48911127Smckusick * Look up an inode in the table of directories 49011127Smckusick */ 49111127Smckusick struct inotab * 49211127Smckusick inotablookup(ino) 49311127Smckusick ino_t ino; 49411127Smckusick { 49511127Smckusick register struct inotab *itp; 49611127Smckusick 49711127Smckusick for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next) 49811127Smckusick if (itp->t_ino == ino) 49911127Smckusick return(itp); 50011127Smckusick return ((struct inotab *)0); 50111127Smckusick } 50211127Smckusick 50311127Smckusick /* 50411127Smckusick * Clean up and exit 50511127Smckusick */ 50611127Smckusick done(exitcode) 50711127Smckusick int exitcode; 50811127Smckusick { 50911127Smckusick 51011127Smckusick closemt(); 51111421Smckusick (void) unlink(dirfile); 51211127Smckusick exit(exitcode); 51311127Smckusick } 514