121165Sdist /* 2*61536Sbostic * Copyright (c) 1983, 1993 3*61536Sbostic * The Regents of the University of California. All rights reserved. 436105Sbostic * 542708Sbostic * %sccs.include.redist.c% 621165Sdist */ 721165Sdist 811127Smckusick #ifndef lint 9*61536Sbostic static char sccsid[] = "@(#)dirs.c 8.1 (Berkeley) 06/05/93"; 1036105Sbostic #endif /* not lint */ 1111127Smckusick 1256567Sbostic #include <sys/param.h> 1311309Smckusick #include <sys/file.h> 1456567Sbostic #include <sys/stat.h> 1556567Sbostic #include <sys/time.h> 1656567Sbostic 1756567Sbostic #include <ufs/ffs/fs.h> 1856567Sbostic #include <ufs/ufs/dinode.h> 1951625Sbostic #include <ufs/ufs/dir.h> 2056567Sbostic #include <protocols/dumprestore.h> 2156567Sbostic 2256567Sbostic #include <errno.h> 2356567Sbostic #include <stdio.h> 2456567Sbostic #include <stdlib.h> 2556567Sbostic #include <string.h> 2656567Sbostic #include <unistd.h> 2756567Sbostic 2837952Sbostic #include "pathnames.h" 2956567Sbostic #include "restore.h" 3056567Sbostic #include "extern.h" 3111127Smckusick 3211992Smckusick /* 3311992Smckusick * Symbol table of directories read from tape. 3411992Smckusick */ 3511127Smckusick #define HASHSIZE 1000 3611127Smckusick #define INOHASH(val) (val % HASHSIZE) 3711127Smckusick struct inotab { 3850662Smckusick struct inotab *t_next; 3911127Smckusick ino_t t_ino; 4050662Smckusick long t_seekpt; 4150662Smckusick long t_size; 4211309Smckusick }; 4311309Smckusick static struct inotab *inotab[HASHSIZE]; 4411127Smckusick 4511992Smckusick /* 4611992Smckusick * Information retained about directories. 4711992Smckusick */ 4811127Smckusick struct modeinfo { 4911127Smckusick ino_t ino; 5039471Smckusick struct timeval timep[2]; 5111127Smckusick short mode; 5211127Smckusick short uid; 5311127Smckusick short gid; 5411127Smckusick }; 5511127Smckusick 5611992Smckusick /* 5740094Smckusick * Definitions for library routines operating on directories. 5840094Smckusick */ 5946564Smckusick #undef DIRBLKSIZ 6046564Smckusick #define DIRBLKSIZ 1024 6150657Smckusick struct rstdirdesc { 6240094Smckusick int dd_fd; 6340094Smckusick long dd_loc; 6440094Smckusick long dd_size; 6540094Smckusick char dd_buf[DIRBLKSIZ]; 6640094Smckusick }; 6740094Smckusick 6840094Smckusick /* 6911992Smckusick * Global variables for this file. 7011992Smckusick */ 7150662Smckusick static long seekpt; 7211309Smckusick static FILE *df, *mf; 7350657Smckusick static RST_DIR *dirp; 7411992Smckusick static char dirfile[32] = "#"; /* No file */ 7511992Smckusick static char modefile[32] = "#"; /* No file */ 7646566Smckusick static char dot[2] = "."; /* So it can be modified */ 7711127Smckusick 7811992Smckusick /* 7911992Smckusick * Format of old style directories. 8011992Smckusick */ 8111127Smckusick #define ODIRSIZ 14 8211127Smckusick struct odirect { 8311127Smckusick u_short d_ino; 8411127Smckusick char d_name[ODIRSIZ]; 8511127Smckusick }; 8611127Smckusick 8756567Sbostic static struct inotab *allocinotab __P((ino_t, struct dinode *, long)); 8856567Sbostic static void dcvt __P((struct odirect *, struct direct *)); 8956567Sbostic static void flushent __P((void)); 9056567Sbostic static struct inotab *inotablookup __P((ino_t)); 9157896Sbostic static RST_DIR *opendirfile __P((const char *)); 9256567Sbostic static void putdir __P((char *, long)); 9356567Sbostic static void putent __P((struct direct *)); 9456567Sbostic static void rst_seekdir __P((RST_DIR *, long, long)); 9556567Sbostic static long rst_telldir __P((RST_DIR *)); 9656946Smckusick static struct direct *searchdir __P((ino_t, char *)); 9756567Sbostic 9811127Smckusick /* 9911127Smckusick * Extract directory contents, building up a directory structure 10011127Smckusick * on disk for extraction by name. 10111992Smckusick * If genmode is requested, save mode, owner, and times for all 10211127Smckusick * directories on the tape. 10311127Smckusick */ 10456567Sbostic void 10511992Smckusick extractdirs(genmode) 10611992Smckusick int genmode; 10711127Smckusick { 10811127Smckusick register int i; 10911127Smckusick register struct dinode *ip; 11011322Smckusick struct inotab *itp; 11111127Smckusick struct direct nulldir; 11211127Smckusick 11311127Smckusick vprintf(stdout, "Extract directories from tape\n"); 11437952Sbostic (void) sprintf(dirfile, "%s/rstdir%d", _PATH_TMP, dumpdate); 11511127Smckusick df = fopen(dirfile, "w"); 11657930Sbostic if (df == NULL) { 11711127Smckusick fprintf(stderr, 11815779Smckusick "restore: %s - cannot create directory temporary\n", 11911127Smckusick dirfile); 12056567Sbostic fprintf(stderr, "fopen: %s\n", strerror(errno)); 12111127Smckusick done(1); 12211127Smckusick } 12311992Smckusick if (genmode != 0) { 12437952Sbostic (void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate); 12511127Smckusick mf = fopen(modefile, "w"); 12657930Sbostic if (mf == NULL) { 12711127Smckusick fprintf(stderr, 12815779Smckusick "restore: %s - cannot create modefile \n", 12911127Smckusick modefile); 13056567Sbostic fprintf(stderr, "fopen: %s\n", strerror(errno)); 13111127Smckusick done(1); 13211127Smckusick } 13311127Smckusick } 13411322Smckusick nulldir.d_ino = 0; 13554582Smckusick nulldir.d_type = DT_DIR; 13611127Smckusick nulldir.d_namlen = 1; 13712453Smckusick (void) strcpy(nulldir.d_name, "/"); 13854582Smckusick nulldir.d_reclen = DIRSIZ(0, &nulldir); 13911127Smckusick for (;;) { 14011127Smckusick curfile.name = "<directory file - name unknown>"; 14111127Smckusick curfile.action = USING; 14211127Smckusick ip = curfile.dip; 14317948Smckusick if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) { 14411732Smckusick (void) fclose(df); 14540064Smckusick dirp = opendirfile(dirfile); 14611127Smckusick if (dirp == NULL) 14756567Sbostic fprintf(stderr, "opendirfile: %s\n", 14856567Sbostic strerror(errno)); 14911127Smckusick if (mf != NULL) 15011732Smckusick (void) fclose(mf); 15146566Smckusick i = dirlookup(dot); 15211992Smckusick if (i == 0) 15311421Smckusick panic("Root directory is not on tape\n"); 15411127Smckusick return; 15511127Smckusick } 15611322Smckusick itp = allocinotab(curfile.ino, ip, seekpt); 15756434Smckusick getfile(putdir, xtrnull); 15811127Smckusick putent(&nulldir); 15911127Smckusick flushent(); 16011322Smckusick itp->t_size = seekpt - itp->t_seekpt; 16111127Smckusick } 16211127Smckusick } 16311127Smckusick 16411127Smckusick /* 16511322Smckusick * skip over all the directories on the tape 16611322Smckusick */ 16756567Sbostic void 16811322Smckusick skipdirs() 16911322Smckusick { 17011322Smckusick 17111322Smckusick while ((curfile.dip->di_mode & IFMT) == IFDIR) { 17211322Smckusick skipfile(); 17311322Smckusick } 17411322Smckusick } 17511322Smckusick 17611322Smckusick /* 17711127Smckusick * Recursively find names and inumbers of all files in subtree 17811127Smckusick * pname and pass them off to be processed. 17911127Smckusick */ 18056567Sbostic void 18111127Smckusick treescan(pname, ino, todo) 18211127Smckusick char *pname; 18311127Smckusick ino_t ino; 18456567Sbostic long (*todo) __P((char *, ino_t, int)); 18511127Smckusick { 18611127Smckusick register struct inotab *itp; 18712453Smckusick register struct direct *dp; 18811127Smckusick int namelen; 18950662Smckusick long bpt; 19011644Smckusick char locname[MAXPATHLEN + 1]; 19111127Smckusick 19211127Smckusick itp = inotablookup(ino); 19311127Smckusick if (itp == NULL) { 19411127Smckusick /* 19511127Smckusick * Pname is name of a simple file or an unchanged directory. 19611127Smckusick */ 19711744Smckusick (void) (*todo)(pname, ino, LEAF); 19811127Smckusick return; 19911127Smckusick } 20011127Smckusick /* 20111127Smckusick * Pname is a dumped directory name. 20211127Smckusick */ 20311744Smckusick if ((*todo)(pname, ino, NODE) == FAIL) 20411744Smckusick return; 20511127Smckusick /* 20611127Smckusick * begin search through the directory 20711127Smckusick * skipping over "." and ".." 20811127Smckusick */ 20911992Smckusick (void) strncpy(locname, pname, MAXPATHLEN); 21011992Smckusick (void) strncat(locname, "/", MAXPATHLEN); 21111127Smckusick namelen = strlen(locname); 21212556Smckusick rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 21312556Smckusick dp = rst_readdir(dirp); /* "." */ 21427263Smckusick if (dp != NULL && strcmp(dp->d_name, ".") == 0) 21512556Smckusick dp = rst_readdir(dirp); /* ".." */ 21627263Smckusick else 21727263Smckusick fprintf(stderr, "Warning: `.' missing from directory %s\n", 21827263Smckusick pname); 21927263Smckusick if (dp != NULL && strcmp(dp->d_name, "..") == 0) 22012556Smckusick dp = rst_readdir(dirp); /* first real entry */ 22127263Smckusick else 22227263Smckusick fprintf(stderr, "Warning: `..' missing from directory %s\n", 22327263Smckusick pname); 22440064Smckusick bpt = rst_telldir(dirp); 22511127Smckusick /* 22612453Smckusick * a zero inode signals end of directory 22711127Smckusick */ 22812453Smckusick while (dp != NULL && dp->d_ino != 0) { 22911127Smckusick locname[namelen] = '\0'; 23011644Smckusick if (namelen + dp->d_namlen >= MAXPATHLEN) { 23111127Smckusick fprintf(stderr, "%s%s: name exceeds %d char\n", 23211644Smckusick locname, dp->d_name, MAXPATHLEN); 23311127Smckusick } else { 23411992Smckusick (void) strncat(locname, dp->d_name, (int)dp->d_namlen); 23511127Smckusick treescan(locname, dp->d_ino, todo); 23612556Smckusick rst_seekdir(dirp, bpt, itp->t_seekpt); 23711127Smckusick } 23812556Smckusick dp = rst_readdir(dirp); 23940064Smckusick bpt = rst_telldir(dirp); 24011127Smckusick } 24111127Smckusick if (dp == NULL) 24211127Smckusick fprintf(stderr, "corrupted directory: %s.\n", locname); 24311127Smckusick } 24411127Smckusick 24511127Smckusick /* 24656434Smckusick * Lookup a pathname which is always assumed to start from the ROOTINO. 24711127Smckusick */ 24856946Smckusick struct direct * 24956434Smckusick pathsearch(pathname) 25057896Sbostic const char *pathname; 25111127Smckusick { 25211127Smckusick ino_t ino; 25356946Smckusick struct direct *dp; 25457896Sbostic char *path, *name, buffer[MAXPATHLEN]; 25511127Smckusick 25656434Smckusick strcpy(buffer, pathname); 25757896Sbostic path = buffer; 25811127Smckusick ino = ROOTINO; 25957896Sbostic while (*path == '/') 26057896Sbostic path++; 26157930Sbostic dp = NULL; 26257896Sbostic while ((name = strsep(&path, "/")) != NULL && *name != NULL) { 26357930Sbostic if ((dp = searchdir(ino, name)) == NULL) 26456946Smckusick return (NULL); 26556946Smckusick ino = dp->d_ino; 26656946Smckusick } 26756946Smckusick return (dp); 26811127Smckusick } 26911127Smckusick 27011127Smckusick /* 27156434Smckusick * Lookup the requested name in directory inum. 27256434Smckusick * Return its inode number if found, zero if it does not exist. 27311127Smckusick */ 27456946Smckusick static struct direct * 27556434Smckusick searchdir(inum, name) 27611127Smckusick ino_t inum; 27756434Smckusick char *name; 27811127Smckusick { 27911127Smckusick register struct direct *dp; 28011127Smckusick register struct inotab *itp; 28111127Smckusick int len; 28211127Smckusick 28311127Smckusick itp = inotablookup(inum); 28411127Smckusick if (itp == NULL) 28557930Sbostic return (NULL); 28612556Smckusick rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 28756434Smckusick len = strlen(name); 28811127Smckusick do { 28912556Smckusick dp = rst_readdir(dirp); 29012453Smckusick if (dp == NULL || dp->d_ino == 0) 29156946Smckusick return (NULL); 29256434Smckusick } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0); 29356946Smckusick return (dp); 29411127Smckusick } 29511127Smckusick 29611127Smckusick /* 29711127Smckusick * Put the directory entries in the directory file 29811127Smckusick */ 29956567Sbostic static void 30011127Smckusick putdir(buf, size) 30111127Smckusick char *buf; 30256567Sbostic long size; 30311127Smckusick { 30411127Smckusick struct direct cvtbuf; 30511127Smckusick register struct odirect *odp; 30611127Smckusick struct odirect *eodp; 30711127Smckusick register struct direct *dp; 30811127Smckusick long loc, i; 30911127Smckusick 31011127Smckusick if (cvtflag) { 31111127Smckusick eodp = (struct odirect *)&buf[size]; 31211127Smckusick for (odp = (struct odirect *)buf; odp < eodp; odp++) 31311127Smckusick if (odp->d_ino != 0) { 31411127Smckusick dcvt(odp, &cvtbuf); 31511127Smckusick putent(&cvtbuf); 31611127Smckusick } 31711127Smckusick } else { 31811127Smckusick for (loc = 0; loc < size; ) { 31911127Smckusick dp = (struct direct *)(buf + loc); 32054582Smckusick if (oldinofmt) { 32154582Smckusick if (Bcvt) { 32256567Sbostic swabst((u_char *)"l2s", (u_char *) dp); 32354582Smckusick } 32454582Smckusick } else { 32554582Smckusick if (Bcvt) { 32656567Sbostic swabst((u_char *)"ls", (u_char *) dp); 32754582Smckusick } 32826941Ssklower } 32911127Smckusick i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1)); 33046564Smckusick if ((dp->d_reclen & 0x3) != 0 || 33146564Smckusick dp->d_reclen > i || 33254582Smckusick dp->d_reclen < DIRSIZ(0, dp) || 33350662Smckusick dp->d_namlen > NAME_MAX) { 33450662Smckusick vprintf(stdout, "Mangled directory: "); 33550662Smckusick if ((dp->d_reclen & 0x3) != 0) 33650662Smckusick vprintf(stdout, 33750662Smckusick "reclen not multiple of 4 "); 33854582Smckusick if (dp->d_reclen < DIRSIZ(0, dp)) 33950662Smckusick vprintf(stdout, 34050662Smckusick "reclen less than DIRSIZ (%d < %d) ", 34154582Smckusick dp->d_reclen, DIRSIZ(0, dp)); 34250662Smckusick if (dp->d_namlen > NAME_MAX) 34350662Smckusick vprintf(stdout, 34450662Smckusick "reclen name too big (%d > %d) ", 34550662Smckusick dp->d_namlen, NAME_MAX); 34650662Smckusick vprintf(stdout, "\n"); 34711127Smckusick loc += i; 34811127Smckusick continue; 34911127Smckusick } 35011127Smckusick loc += dp->d_reclen; 35111127Smckusick if (dp->d_ino != 0) { 35211127Smckusick putent(dp); 35311127Smckusick } 35411127Smckusick } 35511127Smckusick } 35611127Smckusick } 35711127Smckusick 35811127Smckusick /* 35911127Smckusick * These variables are "local" to the following two functions. 36011127Smckusick */ 36111127Smckusick char dirbuf[DIRBLKSIZ]; 36211127Smckusick long dirloc = 0; 36311127Smckusick long prev = 0; 36411127Smckusick 36511127Smckusick /* 36611127Smckusick * add a new directory entry to a file. 36711127Smckusick */ 36856567Sbostic static void 36911127Smckusick putent(dp) 37011127Smckusick struct direct *dp; 37111127Smckusick { 37254582Smckusick dp->d_reclen = DIRSIZ(0, dp); 37311127Smckusick if (dirloc + dp->d_reclen > DIRBLKSIZ) { 37411127Smckusick ((struct direct *)(dirbuf + prev))->d_reclen = 37511127Smckusick DIRBLKSIZ - prev; 37611732Smckusick (void) fwrite(dirbuf, 1, DIRBLKSIZ, df); 37711127Smckusick dirloc = 0; 37811127Smckusick } 37911127Smckusick bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen); 38011127Smckusick prev = dirloc; 38111127Smckusick dirloc += dp->d_reclen; 38211127Smckusick } 38311127Smckusick 38411127Smckusick /* 38511127Smckusick * flush out a directory that is finished. 38611127Smckusick */ 38756567Sbostic static void 38811127Smckusick flushent() 38911127Smckusick { 39011127Smckusick ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev; 39111732Smckusick (void) fwrite(dirbuf, (int)dirloc, 1, df); 39211127Smckusick seekpt = ftell(df); 39311127Smckusick dirloc = 0; 39411127Smckusick } 39511127Smckusick 39656567Sbostic static void 39711127Smckusick dcvt(odp, ndp) 39811127Smckusick register struct odirect *odp; 39911127Smckusick register struct direct *ndp; 40011127Smckusick { 40111127Smckusick 40211127Smckusick bzero((char *)ndp, (long)(sizeof *ndp)); 40311127Smckusick ndp->d_ino = odp->d_ino; 40454582Smckusick ndp->d_type = DT_UNKNOWN; 40511992Smckusick (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ); 40611127Smckusick ndp->d_namlen = strlen(ndp->d_name); 40754582Smckusick ndp->d_reclen = DIRSIZ(0, ndp); 40811127Smckusick } 40911127Smckusick 41011127Smckusick /* 41111127Smckusick * Seek to an entry in a directory. 41240064Smckusick * Only values returned by rst_telldir should be passed to rst_seekdir. 41311732Smckusick * This routine handles many directories in a single file. 41411732Smckusick * It takes the base of the directory in the file, plus 41511732Smckusick * the desired seek offset into it. 41611127Smckusick */ 41756567Sbostic static void 41812556Smckusick rst_seekdir(dirp, loc, base) 41950657Smckusick register RST_DIR *dirp; 42050662Smckusick long loc, base; 42111127Smckusick { 42211127Smckusick 42340064Smckusick if (loc == rst_telldir(dirp)) 42411127Smckusick return; 42511127Smckusick loc -= base; 42611127Smckusick if (loc < 0) 42712556Smckusick fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc); 42856567Sbostic (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET); 42911127Smckusick dirp->dd_loc = loc & (DIRBLKSIZ - 1); 43011127Smckusick if (dirp->dd_loc != 0) 43111127Smckusick dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ); 43211127Smckusick } 43311127Smckusick 43411127Smckusick /* 43511127Smckusick * get next entry in a directory. 43611127Smckusick */ 43711127Smckusick struct direct * 43812556Smckusick rst_readdir(dirp) 43950657Smckusick register RST_DIR *dirp; 44011127Smckusick { 44111127Smckusick register struct direct *dp; 44211127Smckusick 44311127Smckusick for (;;) { 44411127Smckusick if (dirp->dd_loc == 0) { 44511127Smckusick dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 44611127Smckusick DIRBLKSIZ); 44712453Smckusick if (dirp->dd_size <= 0) { 44812453Smckusick dprintf(stderr, "error reading directory\n"); 44956567Sbostic return (NULL); 45012453Smckusick } 45111127Smckusick } 45211127Smckusick if (dirp->dd_loc >= dirp->dd_size) { 45311127Smckusick dirp->dd_loc = 0; 45411127Smckusick continue; 45511127Smckusick } 45611127Smckusick dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc); 45711127Smckusick if (dp->d_reclen == 0 || 45812453Smckusick dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) { 45912453Smckusick dprintf(stderr, "corrupted directory: bad reclen %d\n", 46012453Smckusick dp->d_reclen); 46156567Sbostic return (NULL); 46212453Smckusick } 46311127Smckusick dirp->dd_loc += dp->d_reclen; 46412453Smckusick if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0) 46512453Smckusick continue; 46612556Smckusick if (dp->d_ino >= maxino) { 46712453Smckusick dprintf(stderr, "corrupted directory: bad inum %d\n", 46812453Smckusick dp->d_ino); 46912453Smckusick continue; 47012453Smckusick } 47111127Smckusick return (dp); 47211127Smckusick } 47311127Smckusick } 47411127Smckusick 47511127Smckusick /* 47617753Smckusick * Simulate the opening of a directory 47717753Smckusick */ 47850657Smckusick RST_DIR * 47917753Smckusick rst_opendir(name) 48057896Sbostic const char *name; 48117753Smckusick { 48217753Smckusick struct inotab *itp; 48356946Smckusick RST_DIR *dirp; 48417753Smckusick ino_t ino; 48517753Smckusick 48617753Smckusick if ((ino = dirlookup(name)) > 0 && 48717753Smckusick (itp = inotablookup(ino)) != NULL) { 48856946Smckusick dirp = opendirfile(dirfile); 48917753Smckusick rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 49017753Smckusick return (dirp); 49117753Smckusick } 49257930Sbostic return (NULL); 49317753Smckusick } 49417753Smckusick 49517753Smckusick /* 49656946Smckusick * In our case, there is nothing to do when closing a directory. 49756946Smckusick */ 49856946Smckusick void 49956946Smckusick rst_closedir(dirp) 50056946Smckusick RST_DIR *dirp; 50156946Smckusick { 50256946Smckusick 50357896Sbostic (void)close(dirp->dd_fd); 50456946Smckusick free(dirp); 50556946Smckusick return; 50656946Smckusick } 50756946Smckusick 50856946Smckusick /* 50940064Smckusick * Simulate finding the current offset in the directory. 51040064Smckusick */ 51156567Sbostic static long 51240064Smckusick rst_telldir(dirp) 51350657Smckusick RST_DIR *dirp; 51440064Smckusick { 51556567Sbostic return ((long)lseek(dirp->dd_fd, 51656567Sbostic (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc); 51740064Smckusick } 51840064Smckusick 51940064Smckusick /* 52040064Smckusick * Open a directory file. 52140064Smckusick */ 52256567Sbostic static RST_DIR * 52340064Smckusick opendirfile(name) 52457896Sbostic const char *name; 52540064Smckusick { 52650657Smckusick register RST_DIR *dirp; 52740064Smckusick register int fd; 52840064Smckusick 52956567Sbostic if ((fd = open(name, O_RDONLY)) == -1) 53056567Sbostic return (NULL); 53156567Sbostic if ((dirp = malloc(sizeof(RST_DIR))) == NULL) { 53256567Sbostic (void)close(fd); 53356567Sbostic return (NULL); 53440064Smckusick } 53540064Smckusick dirp->dd_fd = fd; 53640064Smckusick dirp->dd_loc = 0; 53756567Sbostic return (dirp); 53840064Smckusick } 53940064Smckusick 54040064Smckusick /* 54111127Smckusick * Set the mode, owner, and times for all new or changed directories 54211127Smckusick */ 54356567Sbostic void 54455880Smckusick setdirmodes(flags) 54555880Smckusick int flags; 54611127Smckusick { 54711127Smckusick FILE *mf; 54811127Smckusick struct modeinfo node; 54911127Smckusick struct entry *ep; 55011127Smckusick char *cp; 55111127Smckusick 55211127Smckusick vprintf(stdout, "Set directory mode, owner, and times.\n"); 55337952Sbostic (void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate); 55411127Smckusick mf = fopen(modefile, "r"); 55511127Smckusick if (mf == NULL) { 55656567Sbostic fprintf(stderr, "fopen: %s\n", strerror(errno)); 55715779Smckusick fprintf(stderr, "cannot open mode file %s\n", modefile); 55815779Smckusick fprintf(stderr, "directory mode, owner, and times not set\n"); 55915779Smckusick return; 56011127Smckusick } 56111127Smckusick clearerr(mf); 56211309Smckusick for (;;) { 56311732Smckusick (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf); 56411309Smckusick if (feof(mf)) 56511309Smckusick break; 56611127Smckusick ep = lookupino(node.ino); 56714453Smckusick if (command == 'i' || command == 'x') { 56856567Sbostic if (ep == NULL) 56911309Smckusick continue; 57055880Smckusick if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) { 57118008Smckusick ep->e_flags &= ~NEW; 57218008Smckusick continue; 57318008Smckusick } 57414453Smckusick if (node.ino == ROOTINO && 57514453Smckusick reply("set owner/mode for '.'") == FAIL) 57614453Smckusick continue; 57714453Smckusick } 57856567Sbostic if (ep == NULL) { 57911127Smckusick panic("cannot find directory inode %d\n", node.ino); 58042862Smckusick } else { 58142862Smckusick cp = myname(ep); 58242862Smckusick (void) chown(cp, node.uid, node.gid); 58342862Smckusick (void) chmod(cp, node.mode); 58442862Smckusick utimes(cp, node.timep); 58542862Smckusick ep->e_flags &= ~NEW; 58642862Smckusick } 58711127Smckusick } 58811127Smckusick if (ferror(mf)) 58911127Smckusick panic("error setting directory modes\n"); 59011732Smckusick (void) fclose(mf); 59111127Smckusick } 59211127Smckusick 59311127Smckusick /* 59411127Smckusick * Generate a literal copy of a directory. 59511127Smckusick */ 59656567Sbostic int 59711127Smckusick genliteraldir(name, ino) 59811127Smckusick char *name; 59911127Smckusick ino_t ino; 60011127Smckusick { 60111127Smckusick register struct inotab *itp; 60211127Smckusick int ofile, dp, i, size; 60311127Smckusick char buf[BUFSIZ]; 60411127Smckusick 60511127Smckusick itp = inotablookup(ino); 60611127Smckusick if (itp == NULL) 60711322Smckusick panic("Cannot find directory inode %d named %s\n", ino, name); 60812893Ssam if ((ofile = creat(name, 0666)) < 0) { 60912556Smckusick fprintf(stderr, "%s: ", name); 61012556Smckusick (void) fflush(stderr); 61156567Sbostic fprintf(stderr, "cannot create file: %s\n", strerror(errno)); 61211127Smckusick return (FAIL); 61311127Smckusick } 61412556Smckusick rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 61511127Smckusick dp = dup(dirp->dd_fd); 61611127Smckusick for (i = itp->t_size; i > 0; i -= BUFSIZ) { 61711127Smckusick size = i < BUFSIZ ? i : BUFSIZ; 61811127Smckusick if (read(dp, buf, (int) size) == -1) { 61911127Smckusick fprintf(stderr, 62011127Smckusick "write error extracting inode %d, name %s\n", 62111127Smckusick curfile.ino, curfile.name); 62256567Sbostic fprintf(stderr, "read: %s\n", strerror(errno)); 62311127Smckusick done(1); 62411127Smckusick } 62534268Smckusick if (!Nflag && write(ofile, buf, (int) size) == -1) { 62611127Smckusick fprintf(stderr, 62711127Smckusick "write error extracting inode %d, name %s\n", 62811127Smckusick curfile.ino, curfile.name); 62956567Sbostic fprintf(stderr, "write: %s\n", strerror(errno)); 63011127Smckusick done(1); 63111127Smckusick } 63211127Smckusick } 63311732Smckusick (void) close(dp); 63411732Smckusick (void) close(ofile); 63511127Smckusick return (GOOD); 63611127Smckusick } 63711127Smckusick 63811127Smckusick /* 63911992Smckusick * Determine the type of an inode 64011992Smckusick */ 64156567Sbostic int 64211992Smckusick inodetype(ino) 64311992Smckusick ino_t ino; 64411992Smckusick { 64511992Smckusick struct inotab *itp; 64611992Smckusick 64711992Smckusick itp = inotablookup(ino); 64811992Smckusick if (itp == NULL) 64911992Smckusick return (LEAF); 65011992Smckusick return (NODE); 65111992Smckusick } 65211992Smckusick 65311992Smckusick /* 65411127Smckusick * Allocate and initialize a directory inode entry. 65511127Smckusick * If requested, save its pertinent mode, owner, and time info. 65611127Smckusick */ 65756567Sbostic static struct inotab * 65811127Smckusick allocinotab(ino, dip, seekpt) 65911127Smckusick ino_t ino; 66011127Smckusick struct dinode *dip; 66150662Smckusick long seekpt; 66211127Smckusick { 66311127Smckusick register struct inotab *itp; 66411127Smckusick struct modeinfo node; 66511127Smckusick 66656567Sbostic itp = calloc(1, sizeof(struct inotab)); 66756567Sbostic if (itp == NULL) 66813859Smckusick panic("no memory directory table\n"); 66911127Smckusick itp->t_next = inotab[INOHASH(ino)]; 67011127Smckusick inotab[INOHASH(ino)] = itp; 67111127Smckusick itp->t_ino = ino; 67211127Smckusick itp->t_seekpt = seekpt; 67311127Smckusick if (mf == NULL) 67457930Sbostic return (itp); 67511127Smckusick node.ino = ino; 67654156Smckusick node.timep[0].tv_sec = dip->di_atime.ts_sec; 67754156Smckusick node.timep[0].tv_usec = dip->di_atime.ts_nsec / 1000; 67854156Smckusick node.timep[1].tv_sec = dip->di_mtime.ts_sec; 67954156Smckusick node.timep[1].tv_usec = dip->di_mtime.ts_nsec / 1000; 68011127Smckusick node.mode = dip->di_mode; 68111127Smckusick node.uid = dip->di_uid; 68211127Smckusick node.gid = dip->di_gid; 68311732Smckusick (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf); 68457930Sbostic return (itp); 68511127Smckusick } 68611127Smckusick 68711127Smckusick /* 68811127Smckusick * Look up an inode in the table of directories 68911127Smckusick */ 69056567Sbostic static struct inotab * 69111127Smckusick inotablookup(ino) 69211127Smckusick ino_t ino; 69311127Smckusick { 69411127Smckusick register struct inotab *itp; 69511127Smckusick 69611127Smckusick for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next) 69711127Smckusick if (itp->t_ino == ino) 69857930Sbostic return (itp); 69956567Sbostic return (NULL); 70011127Smckusick } 70111127Smckusick 70211127Smckusick /* 70311127Smckusick * Clean up and exit 70411127Smckusick */ 70557930Sbostic __dead void 70611127Smckusick done(exitcode) 70711127Smckusick int exitcode; 70811127Smckusick { 70911127Smckusick 71011127Smckusick closemt(); 71111992Smckusick if (modefile[0] != '#') 71211992Smckusick (void) unlink(modefile); 71311992Smckusick if (dirfile[0] != '#') 71411992Smckusick (void) unlink(dirfile); 71511127Smckusick exit(exitcode); 71611127Smckusick } 717