111127Smckusick /* Copyright (c) 1983 Regents of the University of California */ 211127Smckusick 311127Smckusick #ifndef lint 4*11732Smckusick static char sccsid[] = "@(#)dirs.c 3.6 (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) { 90*11732Smckusick (void) fclose(df); 9111127Smckusick dirp = opendir(dirfile); 9211127Smckusick if (dirp == NULL) 9311127Smckusick perror("opendir"); 9411127Smckusick if (mf != NULL) 95*11732Smckusick (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; 12611127Smckusick void (*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 */ 13911127Smckusick (*todo)(pname, ino, LEAF); 14011127Smckusick return; 14111127Smckusick } 14211127Smckusick /* 14311127Smckusick * Pname is a dumped directory name. 14411127Smckusick */ 14511127Smckusick (*todo)(pname, ino, NODE); 14611127Smckusick /* 14711127Smckusick * begin search through the directory 14811127Smckusick * skipping over "." and ".." 14911127Smckusick */ 15011644Smckusick strncpy(locname, pname, MAXPATHLEN); 15111644Smckusick strncat(locname, "/", MAXPATHLEN); 15211127Smckusick namelen = strlen(locname); 15311127Smckusick seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 15411127Smckusick dp = readdir(dirp); /* "." */ 15511127Smckusick dp = readdir(dirp); /* ".." */ 15611127Smckusick dp = readdir(dirp); /* first real entry */ 15711127Smckusick bpt = telldir(dirp); 15811127Smckusick /* 15911127Smckusick * "/" signals end of directory 16011127Smckusick */ 16111127Smckusick while (dp != NULL && !(dp->d_namlen == 1 && dp->d_name[0] == '/')) { 16211127Smckusick locname[namelen] = '\0'; 16311644Smckusick if (namelen + dp->d_namlen >= MAXPATHLEN) { 16411127Smckusick fprintf(stderr, "%s%s: name exceeds %d char\n", 16511644Smckusick locname, dp->d_name, MAXPATHLEN); 16611127Smckusick } else { 16711127Smckusick strncat(locname, dp->d_name, dp->d_namlen); 16811127Smckusick treescan(locname, dp->d_ino, todo); 16911127Smckusick seekdir(dirp, bpt, itp->t_seekpt); 17011127Smckusick } 17111127Smckusick dp = readdir(dirp); 17211127Smckusick bpt = telldir(dirp); 17311127Smckusick } 17411127Smckusick if (dp == NULL) 17511127Smckusick fprintf(stderr, "corrupted directory: %s.\n", locname); 17611127Smckusick } 17711127Smckusick 17811127Smckusick /* 17911127Smckusick * Search the directory tree rooted at inode ROOTINO 18011127Smckusick * for the path pointed at by n 18111127Smckusick */ 18211127Smckusick ino_t 18311127Smckusick psearch(n) 18411127Smckusick char *n; 18511127Smckusick { 18611127Smckusick register char *cp, *cp1; 18711127Smckusick ino_t ino; 18811127Smckusick char c; 18911127Smckusick 19011127Smckusick ino = ROOTINO; 19111127Smckusick if (*(cp = n) == '/') 19211127Smckusick cp++; 19311127Smckusick next: 19411127Smckusick cp1 = cp + 1; 19511127Smckusick while (*cp1 != '/' && *cp1) 19611127Smckusick cp1++; 19711127Smckusick c = *cp1; 19811127Smckusick *cp1 = 0; 19911127Smckusick ino = search(ino, cp); 20011127Smckusick if (ino == 0) { 20111127Smckusick *cp1 = c; 20211127Smckusick return(0); 20311127Smckusick } 20411127Smckusick *cp1 = c; 20511127Smckusick if (c == '/') { 20611127Smckusick cp = cp1+1; 20711127Smckusick goto next; 20811127Smckusick } 20911127Smckusick return(ino); 21011127Smckusick } 21111127Smckusick 21211127Smckusick /* 21311127Smckusick * search the directory inode ino 21411127Smckusick * looking for entry cp 21511127Smckusick */ 21611127Smckusick ino_t 21711127Smckusick search(inum, cp) 21811127Smckusick ino_t inum; 21911127Smckusick char *cp; 22011127Smckusick { 22111127Smckusick register struct direct *dp; 22211127Smckusick register struct inotab *itp; 22311127Smckusick int len; 22411127Smckusick 22511127Smckusick itp = inotablookup(inum); 22611127Smckusick if (itp == NULL) 22711127Smckusick return(0); 22811127Smckusick seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 22911127Smckusick len = strlen(cp); 23011127Smckusick do { 23111127Smckusick dp = readdir(dirp); 23211127Smckusick if (dp->d_namlen == 1 && dp->d_name[0] == '/') 23311127Smckusick return(0); 23411127Smckusick } while (dp->d_namlen != len || strncmp(dp->d_name, cp, len)); 23511127Smckusick return(dp->d_ino); 23611127Smckusick } 23711127Smckusick 23811127Smckusick /* 23911127Smckusick * Put the directory entries in the directory file 24011127Smckusick */ 24111127Smckusick putdir(buf, size) 24211127Smckusick char *buf; 24311127Smckusick int size; 24411127Smckusick { 24511127Smckusick struct direct cvtbuf; 24611127Smckusick register struct odirect *odp; 24711127Smckusick struct odirect *eodp; 24811127Smckusick register struct direct *dp; 24911127Smckusick long loc, i; 25011127Smckusick 25111127Smckusick if (cvtflag) { 25211127Smckusick eodp = (struct odirect *)&buf[size]; 25311127Smckusick for (odp = (struct odirect *)buf; odp < eodp; odp++) 25411127Smckusick if (odp->d_ino != 0) { 25511127Smckusick dcvt(odp, &cvtbuf); 25611127Smckusick putent(&cvtbuf); 25711127Smckusick } 25811127Smckusick } else { 25911127Smckusick for (loc = 0; loc < size; ) { 26011127Smckusick dp = (struct direct *)(buf + loc); 26111127Smckusick i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1)); 26211127Smckusick if (dp->d_reclen == 0 || dp->d_reclen > i) { 26311127Smckusick loc += i; 26411127Smckusick continue; 26511127Smckusick } 26611127Smckusick loc += dp->d_reclen; 26711127Smckusick if (dp->d_ino != 0) { 26811127Smckusick putent(dp); 26911127Smckusick } 27011127Smckusick } 27111127Smckusick } 27211127Smckusick } 27311127Smckusick 27411127Smckusick /* 27511127Smckusick * These variables are "local" to the following two functions. 27611127Smckusick */ 27711127Smckusick char dirbuf[DIRBLKSIZ]; 27811127Smckusick long dirloc = 0; 27911127Smckusick long prev = 0; 28011127Smckusick 28111127Smckusick /* 28211127Smckusick * add a new directory entry to a file. 28311127Smckusick */ 28411127Smckusick putent(dp) 28511127Smckusick struct direct *dp; 28611127Smckusick { 28711322Smckusick dp->d_reclen = DIRSIZ(dp); 28811127Smckusick if (dirloc + dp->d_reclen > DIRBLKSIZ) { 28911127Smckusick ((struct direct *)(dirbuf + prev))->d_reclen = 29011127Smckusick DIRBLKSIZ - prev; 291*11732Smckusick (void) fwrite(dirbuf, 1, DIRBLKSIZ, df); 29211127Smckusick dirloc = 0; 29311127Smckusick } 29411127Smckusick bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen); 29511127Smckusick prev = dirloc; 29611127Smckusick dirloc += dp->d_reclen; 29711127Smckusick } 29811127Smckusick 29911127Smckusick /* 30011127Smckusick * flush out a directory that is finished. 30111127Smckusick */ 30211127Smckusick flushent() 30311127Smckusick { 30411127Smckusick 30511127Smckusick ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev; 306*11732Smckusick (void) fwrite(dirbuf, (int)dirloc, 1, df); 30711127Smckusick seekpt = ftell(df); 30811127Smckusick dirloc = 0; 30911127Smckusick } 31011127Smckusick 31111127Smckusick dcvt(odp, ndp) 31211127Smckusick register struct odirect *odp; 31311127Smckusick register struct direct *ndp; 31411127Smckusick { 31511127Smckusick 31611127Smckusick bzero((char *)ndp, (long)(sizeof *ndp)); 31711127Smckusick ndp->d_ino = odp->d_ino; 31811127Smckusick strncpy(ndp->d_name, odp->d_name, ODIRSIZ); 31911127Smckusick ndp->d_namlen = strlen(ndp->d_name); 32011127Smckusick ndp->d_reclen = DIRSIZ(ndp); 32111127Smckusick } 32211127Smckusick 32311127Smckusick /* 32411127Smckusick * Seek to an entry in a directory. 32511127Smckusick * Only values returned by ``telldir'' should be passed to seekdir. 326*11732Smckusick * This routine handles many directories in a single file. 327*11732Smckusick * It takes the base of the directory in the file, plus 328*11732Smckusick * the desired seek offset into it. 32911127Smckusick */ 33011127Smckusick void 33111127Smckusick seekdir(dirp, loc, base) 33211127Smckusick register DIR *dirp; 33311127Smckusick daddr_t loc, base; 33411127Smckusick { 33511127Smckusick 33611127Smckusick if (loc == telldir(dirp)) 33711127Smckusick return; 33811127Smckusick loc -= base; 33911127Smckusick if (loc < 0) 34011127Smckusick fprintf(stderr, "bad seek pointer to seekdir %d\n", loc); 34111127Smckusick (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0); 34211127Smckusick dirp->dd_loc = loc & (DIRBLKSIZ - 1); 34311127Smckusick if (dirp->dd_loc != 0) 34411127Smckusick dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ); 34511127Smckusick } 34611127Smckusick 34711127Smckusick /* 34811127Smckusick * get next entry in a directory. 34911127Smckusick */ 35011127Smckusick struct direct * 35111127Smckusick readdir(dirp) 35211127Smckusick register DIR *dirp; 35311127Smckusick { 35411127Smckusick register struct direct *dp; 35511127Smckusick 35611127Smckusick for (;;) { 35711127Smckusick if (dirp->dd_loc == 0) { 35811127Smckusick dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 35911127Smckusick DIRBLKSIZ); 36011127Smckusick if (dirp->dd_size <= 0) 36111127Smckusick return NULL; 36211127Smckusick } 36311127Smckusick if (dirp->dd_loc >= dirp->dd_size) { 36411127Smckusick dirp->dd_loc = 0; 36511127Smckusick continue; 36611127Smckusick } 36711127Smckusick dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc); 36811127Smckusick if (dp->d_reclen == 0 || 36911127Smckusick dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) 37011127Smckusick return NULL; 37111127Smckusick dirp->dd_loc += dp->d_reclen; 37211127Smckusick return (dp); 37311127Smckusick } 37411127Smckusick } 37511127Smckusick 37611127Smckusick /* 37711127Smckusick * Set the mode, owner, and times for all new or changed directories 37811127Smckusick */ 37911127Smckusick setdirmodes(modefile) 38011127Smckusick char *modefile; 38111127Smckusick { 38211127Smckusick FILE *mf; 38311127Smckusick struct modeinfo node; 38411127Smckusick struct entry *ep; 38511127Smckusick char *cp; 38611127Smckusick 38711127Smckusick vprintf(stdout, "Set directory mode, owner, and times.\n"); 38811127Smckusick mf = fopen(modefile, "r"); 38911127Smckusick if (mf == NULL) { 39011127Smckusick perror("fopen"); 39111127Smckusick panic("cannot open mode file %s\n", modefile); 39211127Smckusick } 39311127Smckusick clearerr(mf); 39411309Smckusick for (;;) { 395*11732Smckusick (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf); 39611309Smckusick if (feof(mf)) 39711309Smckusick break; 39811127Smckusick ep = lookupino(node.ino); 39911309Smckusick if (ep == NIL) { 40011309Smckusick if (command == 'x') 40111309Smckusick continue; 40211127Smckusick panic("cannot find directory inode %d\n", node.ino); 40311309Smckusick } 40411127Smckusick cp = myname(ep); 405*11732Smckusick (void) chown(cp, node.uid, node.gid); 406*11732Smckusick (void) chmod(cp, node.mode); 40711127Smckusick utime(cp, node.timep); 40811127Smckusick } 40911127Smckusick if (ferror(mf)) 41011127Smckusick panic("error setting directory modes\n"); 411*11732Smckusick (void) fclose(mf); 41211421Smckusick (void) unlink(modefile); 41311127Smckusick } 41411127Smckusick 41511127Smckusick /* 41611127Smckusick * Generate a literal copy of a directory. 41711127Smckusick */ 41811127Smckusick genliteraldir(name, ino) 41911127Smckusick char *name; 42011127Smckusick ino_t ino; 42111127Smckusick { 42211127Smckusick register struct inotab *itp; 42311127Smckusick int ofile, dp, i, size; 42411127Smckusick char buf[BUFSIZ]; 42511127Smckusick 42611127Smckusick itp = inotablookup(ino); 42711127Smckusick if (itp == NULL) 42811322Smckusick panic("Cannot find directory inode %d named %s\n", ino, name); 42911127Smckusick if ((ofile = open(name, FWRONLY|FCREATE, 0666)) < 0) { 43011127Smckusick fprintf(stderr, "%s: cannot create file\n", name); 43111127Smckusick return (FAIL); 43211127Smckusick } 43311127Smckusick seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 43411127Smckusick dp = dup(dirp->dd_fd); 43511127Smckusick for (i = itp->t_size; i > 0; i -= BUFSIZ) { 43611127Smckusick size = i < BUFSIZ ? i : BUFSIZ; 43711127Smckusick if (read(dp, buf, (int) size) == -1) { 43811127Smckusick fprintf(stderr, 43911127Smckusick "write error extracting inode %d, name %s\n", 44011127Smckusick curfile.ino, curfile.name); 44111127Smckusick perror("read"); 44211127Smckusick done(1); 44311127Smckusick } 44411127Smckusick if (write(ofile, buf, (int) size) == -1) { 44511127Smckusick fprintf(stderr, 44611127Smckusick "write error extracting inode %d, name %s\n", 44711127Smckusick curfile.ino, curfile.name); 44811127Smckusick perror("write"); 44911127Smckusick done(1); 45011127Smckusick } 45111127Smckusick } 452*11732Smckusick (void) close(dp); 453*11732Smckusick (void) close(ofile); 45411127Smckusick return (GOOD); 45511127Smckusick } 45611127Smckusick 45711127Smckusick /* 45811322Smckusick * Determine the type of an inode 45911322Smckusick */ 46011322Smckusick inodetype(ino) 46111322Smckusick ino_t ino; 46211322Smckusick { 46311322Smckusick struct inotab *itp; 46411322Smckusick 46511322Smckusick itp = inotablookup(ino); 46611322Smckusick if (itp == NULL) 46711322Smckusick return (LEAF); 46811322Smckusick return (NODE); 46911322Smckusick } 47011322Smckusick 47111322Smckusick /* 47211127Smckusick * Allocate and initialize a directory inode entry. 47311127Smckusick * If requested, save its pertinent mode, owner, and time info. 47411127Smckusick */ 47511322Smckusick struct inotab * 47611127Smckusick allocinotab(ino, dip, seekpt) 47711127Smckusick ino_t ino; 47811127Smckusick struct dinode *dip; 47911127Smckusick daddr_t seekpt; 48011127Smckusick { 48111127Smckusick register struct inotab *itp; 48211127Smckusick struct modeinfo node; 48311127Smckusick 48411127Smckusick itp = (struct inotab *)calloc(1, sizeof(struct inotab)); 48511127Smckusick itp->t_next = inotab[INOHASH(ino)]; 48611127Smckusick inotab[INOHASH(ino)] = itp; 48711127Smckusick itp->t_ino = ino; 48811127Smckusick itp->t_seekpt = seekpt; 48911127Smckusick if (mf == NULL) 49011322Smckusick return(itp); 49111127Smckusick node.ino = ino; 49211127Smckusick node.timep[0] = dip->di_atime; 49311127Smckusick node.timep[1] = dip->di_mtime; 49411127Smckusick node.mode = dip->di_mode; 49511127Smckusick node.uid = dip->di_uid; 49611127Smckusick node.gid = dip->di_gid; 497*11732Smckusick (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf); 49811322Smckusick return(itp); 49911127Smckusick } 50011127Smckusick 50111127Smckusick /* 50211127Smckusick * Look up an inode in the table of directories 50311127Smckusick */ 50411127Smckusick struct inotab * 50511127Smckusick inotablookup(ino) 50611127Smckusick ino_t ino; 50711127Smckusick { 50811127Smckusick register struct inotab *itp; 50911127Smckusick 51011127Smckusick for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next) 51111127Smckusick if (itp->t_ino == ino) 51211127Smckusick return(itp); 51311127Smckusick return ((struct inotab *)0); 51411127Smckusick } 51511127Smckusick 51611127Smckusick /* 51711127Smckusick * Clean up and exit 51811127Smckusick */ 51911127Smckusick done(exitcode) 52011127Smckusick int exitcode; 52111127Smckusick { 52211127Smckusick 52311127Smckusick closemt(); 52411421Smckusick (void) unlink(dirfile); 52511127Smckusick exit(exitcode); 52611127Smckusick } 527