Home | History | Annotate | Line # | Download | only in restore
dirs.c revision 1.24
      1  1.24       pk /*	$NetBSD: dirs.c,v 1.24 1997/05/17 19:38:12 pk Exp $	*/
      2  1.13      cgd 
      3   1.6      cgd /*
      4   1.7  mycroft  * Copyright (c) 1983, 1993
      5   1.7  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.6      cgd  * (c) UNIX System Laboratories, Inc.
      7   1.6      cgd  * All or some portions of this file are derived from material licensed
      8   1.6      cgd  * to the University of California by American Telephone and Telegraph
      9   1.6      cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10   1.6      cgd  * the permission of UNIX System Laboratories, Inc.
     11   1.6      cgd  *
     12   1.6      cgd  * Redistribution and use in source and binary forms, with or without
     13   1.6      cgd  * modification, are permitted provided that the following conditions
     14   1.6      cgd  * are met:
     15   1.6      cgd  * 1. Redistributions of source code must retain the above copyright
     16   1.6      cgd  *    notice, this list of conditions and the following disclaimer.
     17   1.6      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.6      cgd  *    notice, this list of conditions and the following disclaimer in the
     19   1.6      cgd  *    documentation and/or other materials provided with the distribution.
     20   1.6      cgd  * 3. All advertising materials mentioning features or use of this software
     21   1.6      cgd  *    must display the following acknowledgement:
     22   1.6      cgd  *	This product includes software developed by the University of
     23   1.6      cgd  *	California, Berkeley and its contributors.
     24   1.6      cgd  * 4. Neither the name of the University nor the names of its contributors
     25   1.6      cgd  *    may be used to endorse or promote products derived from this software
     26   1.6      cgd  *    without specific prior written permission.
     27   1.6      cgd  *
     28   1.6      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.6      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.6      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.6      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.6      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.6      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.6      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.6      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.6      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.6      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.6      cgd  * SUCH DAMAGE.
     39   1.6      cgd  */
     40   1.6      cgd 
     41   1.6      cgd #ifndef lint
     42  1.13      cgd #if 0
     43  1.13      cgd static char sccsid[] = "@(#)dirs.c	8.5 (Berkeley) 8/31/94";
     44  1.13      cgd #else
     45  1.24       pk static char rcsid[] = "$NetBSD: dirs.c,v 1.24 1997/05/17 19:38:12 pk Exp $";
     46  1.13      cgd #endif
     47   1.6      cgd #endif /* not lint */
     48   1.6      cgd 
     49   1.6      cgd #include <sys/param.h>
     50   1.6      cgd #include <sys/file.h>
     51   1.6      cgd #include <sys/stat.h>
     52   1.6      cgd #include <sys/time.h>
     53   1.6      cgd 
     54   1.7  mycroft #include <ufs/ffs/fs.h>
     55   1.7  mycroft #include <ufs/ufs/dinode.h>
     56   1.7  mycroft #include <ufs/ufs/dir.h>
     57   1.6      cgd #include <protocols/dumprestore.h>
     58   1.6      cgd 
     59   1.6      cgd #include <errno.h>
     60  1.23    lukem #include <paths.h>
     61   1.6      cgd #include <stdio.h>
     62   1.6      cgd #include <stdlib.h>
     63   1.6      cgd #include <string.h>
     64   1.6      cgd #include <unistd.h>
     65   1.6      cgd 
     66   1.8  mycroft #include <machine/endian.h>
     67   1.8  mycroft 
     68   1.6      cgd #include "restore.h"
     69   1.6      cgd #include "extern.h"
     70   1.6      cgd 
     71   1.6      cgd /*
     72   1.6      cgd  * Symbol table of directories read from tape.
     73   1.6      cgd  */
     74   1.6      cgd #define HASHSIZE	1000
     75   1.6      cgd #define INOHASH(val) (val % HASHSIZE)
     76   1.6      cgd struct inotab {
     77   1.6      cgd 	struct	inotab *t_next;
     78   1.6      cgd 	ino_t	t_ino;
     79  1.21      cgd 	int32_t	t_seekpt;
     80  1.21      cgd 	int32_t	t_size;
     81   1.6      cgd };
     82   1.6      cgd static struct inotab *inotab[HASHSIZE];
     83   1.6      cgd 
     84   1.6      cgd /*
     85   1.6      cgd  * Information retained about directories.
     86   1.6      cgd  */
     87   1.6      cgd struct modeinfo {
     88   1.6      cgd 	ino_t ino;
     89   1.6      cgd 	struct timeval timep[2];
     90  1.11  mycroft 	mode_t mode;
     91  1.11  mycroft 	uid_t uid;
     92  1.11  mycroft 	gid_t gid;
     93  1.11  mycroft 	int flags;
     94   1.6      cgd };
     95   1.6      cgd 
     96   1.6      cgd /*
     97   1.6      cgd  * Definitions for library routines operating on directories.
     98   1.6      cgd  */
     99   1.6      cgd #undef DIRBLKSIZ
    100   1.6      cgd #define DIRBLKSIZ 1024
    101   1.6      cgd struct rstdirdesc {
    102   1.6      cgd 	int	dd_fd;
    103  1.21      cgd 	int32_t	dd_loc;
    104  1.21      cgd 	int32_t	dd_size;
    105   1.6      cgd 	char	dd_buf[DIRBLKSIZ];
    106   1.6      cgd };
    107   1.6      cgd 
    108   1.6      cgd /*
    109   1.6      cgd  * Global variables for this file.
    110   1.6      cgd  */
    111   1.6      cgd static long	seekpt;
    112   1.6      cgd static FILE	*df, *mf;
    113   1.6      cgd static RST_DIR	*dirp;
    114  1.20    lukem static char	dirfile[MAXPATHLEN] = "#";	/* No file */
    115  1.20    lukem static char	modefile[MAXPATHLEN] = "#";	/* No file */
    116  1.20    lukem static char	dot[2] = ".";			/* So it can be modified */
    117   1.6      cgd 
    118   1.6      cgd /*
    119   1.6      cgd  * Format of old style directories.
    120   1.6      cgd  */
    121   1.6      cgd #define ODIRSIZ 14
    122   1.6      cgd struct odirect {
    123   1.6      cgd 	u_short	d_ino;
    124   1.6      cgd 	char	d_name[ODIRSIZ];
    125   1.6      cgd };
    126   1.6      cgd 
    127   1.6      cgd static struct inotab	*allocinotab __P((ino_t, struct dinode *, long));
    128   1.6      cgd static void		 dcvt __P((struct odirect *, struct direct *));
    129   1.6      cgd static void		 flushent __P((void));
    130   1.6      cgd static struct inotab	*inotablookup __P((ino_t));
    131   1.7  mycroft static RST_DIR		*opendirfile __P((const char *));
    132   1.6      cgd static void		 putdir __P((char *, long));
    133   1.6      cgd static void		 putent __P((struct direct *));
    134   1.6      cgd static void		 rst_seekdir __P((RST_DIR *, long, long));
    135   1.6      cgd static long		 rst_telldir __P((RST_DIR *));
    136   1.6      cgd static struct direct	*searchdir __P((ino_t, char *));
    137   1.6      cgd 
    138   1.6      cgd /*
    139   1.6      cgd  *	Extract directory contents, building up a directory structure
    140   1.6      cgd  *	on disk for extraction by name.
    141   1.6      cgd  *	If genmode is requested, save mode, owner, and times for all
    142   1.6      cgd  *	directories on the tape.
    143   1.6      cgd  */
    144   1.6      cgd void
    145   1.6      cgd extractdirs(genmode)
    146   1.6      cgd 	int genmode;
    147   1.6      cgd {
    148  1.22    lukem 	int i, dfd, mfd;
    149  1.22    lukem 	struct dinode *ip;
    150   1.6      cgd 	struct inotab *itp;
    151   1.6      cgd 	struct direct nulldir;
    152   1.6      cgd 
    153   1.6      cgd 	vprintf(stdout, "Extract directories from tape\n");
    154  1.19  thorpej 	(void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%d",
    155  1.19  thorpej 	    _PATH_TMP, dumpdate);
    156  1.20    lukem 	if (command != 'r' && command != 'R') {
    157  1.20    lukem 		(void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%d-XXXXXX",
    158  1.20    lukem 		    _PATH_TMP, dumpdate);
    159  1.22    lukem 		if ((dfd = mkstemp(dirfile)) == -1)
    160  1.22    lukem 			err(1, "cannot mkstemp temporary file %s", dirfile);
    161  1.22    lukem 		df = fdopen(dfd, "w");
    162   1.6      cgd 	}
    163  1.22    lukem 	else
    164  1.22    lukem 		df = fopen(dirfile, "w");
    165  1.22    lukem 	if (df == NULL)
    166  1.22    lukem 		err(1, "cannot open temporary file %s", dirfile);
    167  1.22    lukem 
    168   1.6      cgd 	if (genmode != 0) {
    169  1.19  thorpej 		(void) snprintf(modefile, sizeof(modefile), "%s/rstmode%d",
    170  1.19  thorpej 		    _PATH_TMP, dumpdate);
    171  1.20    lukem 		if (command != 'r' && command != 'R') {
    172  1.20    lukem 			(void) snprintf(modefile, sizeof(modefile),
    173  1.20    lukem 			    "%s/rstmode%d-XXXXXX", _PATH_TMP, dumpdate);
    174  1.22    lukem 			if ((mfd = mkstemp(modefile)) == -1)
    175  1.22    lukem 				err(1, "cannot mkstemp temporary file %s",
    176  1.22    lukem 				    modefile);
    177  1.22    lukem 			mf = fdopen(mfd, "w");
    178  1.22    lukem 		}
    179  1.22    lukem 		else
    180  1.22    lukem 			mf = fopen(modefile, "w");
    181  1.22    lukem 		if (mf == NULL)
    182  1.22    lukem 			err(1, "cannot open temporary file %s", modefile);
    183   1.6      cgd 	}
    184   1.6      cgd 	nulldir.d_ino = 0;
    185   1.6      cgd 	nulldir.d_type = DT_DIR;
    186   1.6      cgd 	nulldir.d_namlen = 1;
    187   1.6      cgd 	(void) strcpy(nulldir.d_name, "/");
    188   1.6      cgd 	nulldir.d_reclen = DIRSIZ(0, &nulldir);
    189   1.6      cgd 	for (;;) {
    190   1.6      cgd 		curfile.name = "<directory file - name unknown>";
    191   1.6      cgd 		curfile.action = USING;
    192   1.6      cgd 		ip = curfile.dip;
    193   1.6      cgd 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
    194   1.6      cgd 			(void) fclose(df);
    195   1.6      cgd 			dirp = opendirfile(dirfile);
    196   1.6      cgd 			if (dirp == NULL)
    197   1.6      cgd 				fprintf(stderr, "opendirfile: %s\n",
    198   1.6      cgd 				    strerror(errno));
    199   1.6      cgd 			if (mf != NULL)
    200   1.6      cgd 				(void) fclose(mf);
    201   1.6      cgd 			i = dirlookup(dot);
    202   1.6      cgd 			if (i == 0)
    203   1.6      cgd 				panic("Root directory is not on tape\n");
    204   1.6      cgd 			return;
    205   1.6      cgd 		}
    206   1.6      cgd 		itp = allocinotab(curfile.ino, ip, seekpt);
    207   1.6      cgd 		getfile(putdir, xtrnull);
    208   1.6      cgd 		putent(&nulldir);
    209   1.6      cgd 		flushent();
    210   1.6      cgd 		itp->t_size = seekpt - itp->t_seekpt;
    211   1.6      cgd 	}
    212   1.6      cgd }
    213   1.6      cgd 
    214   1.6      cgd /*
    215   1.6      cgd  * skip over all the directories on the tape
    216   1.6      cgd  */
    217   1.6      cgd void
    218   1.6      cgd skipdirs()
    219   1.6      cgd {
    220   1.6      cgd 
    221  1.22    lukem 	while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
    222   1.6      cgd 		skipfile();
    223   1.6      cgd 	}
    224   1.6      cgd }
    225   1.6      cgd 
    226   1.6      cgd /*
    227  1.24       pk  *	Recursively find names and inumbers of all files in subtree
    228   1.6      cgd  *	pname and pass them off to be processed.
    229   1.6      cgd  */
    230   1.6      cgd void
    231   1.6      cgd treescan(pname, ino, todo)
    232   1.6      cgd 	char *pname;
    233   1.6      cgd 	ino_t ino;
    234   1.6      cgd 	long (*todo) __P((char *, ino_t, int));
    235   1.6      cgd {
    236  1.22    lukem 	struct inotab *itp;
    237  1.22    lukem 	struct direct *dp;
    238   1.6      cgd 	int namelen;
    239   1.6      cgd 	long bpt;
    240   1.6      cgd 	char locname[MAXPATHLEN + 1];
    241   1.6      cgd 
    242   1.6      cgd 	itp = inotablookup(ino);
    243   1.6      cgd 	if (itp == NULL) {
    244   1.6      cgd 		/*
    245   1.6      cgd 		 * Pname is name of a simple file or an unchanged directory.
    246   1.6      cgd 		 */
    247   1.6      cgd 		(void) (*todo)(pname, ino, LEAF);
    248   1.6      cgd 		return;
    249   1.6      cgd 	}
    250   1.6      cgd 	/*
    251   1.6      cgd 	 * Pname is a dumped directory name.
    252   1.6      cgd 	 */
    253   1.6      cgd 	if ((*todo)(pname, ino, NODE) == FAIL)
    254   1.6      cgd 		return;
    255   1.6      cgd 	/*
    256   1.6      cgd 	 * begin search through the directory
    257   1.6      cgd 	 * skipping over "." and ".."
    258   1.6      cgd 	 */
    259  1.22    lukem 	(void) snprintf(locname, sizeof(locname), "%s/", pname);
    260   1.6      cgd 	namelen = strlen(locname);
    261   1.6      cgd 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
    262   1.6      cgd 	dp = rst_readdir(dirp); /* "." */
    263   1.6      cgd 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
    264   1.6      cgd 		dp = rst_readdir(dirp); /* ".." */
    265   1.6      cgd 	else
    266   1.6      cgd 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
    267   1.6      cgd 			pname);
    268   1.6      cgd 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
    269   1.6      cgd 		dp = rst_readdir(dirp); /* first real entry */
    270   1.6      cgd 	else
    271   1.6      cgd 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
    272   1.6      cgd 			pname);
    273   1.6      cgd 	bpt = rst_telldir(dirp);
    274   1.6      cgd 	/*
    275   1.6      cgd 	 * a zero inode signals end of directory
    276   1.6      cgd 	 */
    277  1.11  mycroft 	while (dp != NULL) {
    278   1.6      cgd 		locname[namelen] = '\0';
    279  1.22    lukem 		if (namelen + dp->d_namlen >= sizeof(locname)) {
    280   1.6      cgd 			fprintf(stderr, "%s%s: name exceeds %d char\n",
    281  1.22    lukem 				locname, dp->d_name, sizeof(locname) - 1);
    282   1.6      cgd 		} else {
    283   1.6      cgd 			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
    284   1.6      cgd 			treescan(locname, dp->d_ino, todo);
    285   1.6      cgd 			rst_seekdir(dirp, bpt, itp->t_seekpt);
    286   1.6      cgd 		}
    287   1.6      cgd 		dp = rst_readdir(dirp);
    288   1.6      cgd 		bpt = rst_telldir(dirp);
    289   1.6      cgd 	}
    290   1.6      cgd }
    291   1.6      cgd 
    292   1.6      cgd /*
    293   1.6      cgd  * Lookup a pathname which is always assumed to start from the ROOTINO.
    294   1.6      cgd  */
    295   1.6      cgd struct direct *
    296   1.6      cgd pathsearch(pathname)
    297   1.6      cgd 	const char *pathname;
    298   1.6      cgd {
    299   1.6      cgd 	ino_t ino;
    300   1.6      cgd 	struct direct *dp;
    301   1.6      cgd 	char *path, *name, buffer[MAXPATHLEN];
    302   1.6      cgd 
    303   1.6      cgd 	strcpy(buffer, pathname);
    304   1.6      cgd 	path = buffer;
    305   1.6      cgd 	ino = ROOTINO;
    306   1.6      cgd 	while (*path == '/')
    307   1.6      cgd 		path++;
    308   1.7  mycroft 	dp = NULL;
    309  1.24       pk 	while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
    310   1.7  mycroft 		if ((dp = searchdir(ino, name)) == NULL)
    311   1.6      cgd 			return (NULL);
    312   1.6      cgd 		ino = dp->d_ino;
    313   1.6      cgd 	}
    314   1.6      cgd 	return (dp);
    315   1.6      cgd }
    316   1.6      cgd 
    317   1.6      cgd /*
    318   1.6      cgd  * Lookup the requested name in directory inum.
    319   1.6      cgd  * Return its inode number if found, zero if it does not exist.
    320   1.6      cgd  */
    321   1.6      cgd static struct direct *
    322   1.6      cgd searchdir(inum, name)
    323   1.6      cgd 	ino_t	inum;
    324   1.6      cgd 	char	*name;
    325   1.6      cgd {
    326  1.22    lukem 	struct direct *dp;
    327  1.22    lukem 	struct inotab *itp;
    328   1.6      cgd 	int len;
    329   1.6      cgd 
    330   1.6      cgd 	itp = inotablookup(inum);
    331   1.6      cgd 	if (itp == NULL)
    332   1.7  mycroft 		return (NULL);
    333   1.6      cgd 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
    334   1.6      cgd 	len = strlen(name);
    335   1.6      cgd 	do {
    336   1.6      cgd 		dp = rst_readdir(dirp);
    337  1.11  mycroft 		if (dp == NULL)
    338   1.6      cgd 			return (NULL);
    339   1.6      cgd 	} while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
    340   1.6      cgd 	return (dp);
    341   1.6      cgd }
    342   1.6      cgd 
    343   1.6      cgd /*
    344   1.6      cgd  * Put the directory entries in the directory file
    345   1.6      cgd  */
    346   1.6      cgd static void
    347   1.6      cgd putdir(buf, size)
    348   1.6      cgd 	char *buf;
    349   1.6      cgd 	long size;
    350   1.6      cgd {
    351   1.6      cgd 	struct direct cvtbuf;
    352  1.22    lukem 	struct odirect *odp;
    353   1.6      cgd 	struct odirect *eodp;
    354  1.22    lukem 	struct direct *dp;
    355   1.6      cgd 	long loc, i;
    356   1.6      cgd 
    357   1.6      cgd 	if (cvtflag) {
    358   1.6      cgd 		eodp = (struct odirect *)&buf[size];
    359   1.6      cgd 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
    360   1.6      cgd 			if (odp->d_ino != 0) {
    361   1.6      cgd 				dcvt(odp, &cvtbuf);
    362   1.6      cgd 				putent(&cvtbuf);
    363   1.6      cgd 			}
    364   1.6      cgd 	} else {
    365   1.6      cgd 		for (loc = 0; loc < size; ) {
    366   1.6      cgd 			dp = (struct direct *)(buf + loc);
    367   1.9  mycroft 			if (Bcvt)
    368   1.9  mycroft 				swabst((u_char *)"ls", (u_char *) dp);
    369   1.9  mycroft 			if (oldinofmt && dp->d_ino != 0) {
    370  1.11  mycroft #				if BYTE_ORDER == BIG_ENDIAN
    371  1.11  mycroft 					if (Bcvt)
    372  1.11  mycroft 						dp->d_namlen = dp->d_type;
    373  1.11  mycroft #				else
    374  1.11  mycroft 					if (!Bcvt)
    375  1.11  mycroft 						dp->d_namlen = dp->d_type;
    376  1.11  mycroft #				endif
    377   1.9  mycroft 				dp->d_type = DT_UNKNOWN;
    378   1.6      cgd 			}
    379   1.6      cgd 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
    380   1.6      cgd 			if ((dp->d_reclen & 0x3) != 0 ||
    381   1.6      cgd 			    dp->d_reclen > i ||
    382   1.6      cgd 			    dp->d_reclen < DIRSIZ(0, dp) ||
    383   1.6      cgd 			    dp->d_namlen > NAME_MAX) {
    384   1.6      cgd 				vprintf(stdout, "Mangled directory: ");
    385   1.6      cgd 				if ((dp->d_reclen & 0x3) != 0)
    386   1.6      cgd 					vprintf(stdout,
    387   1.6      cgd 					   "reclen not multiple of 4 ");
    388   1.6      cgd 				if (dp->d_reclen < DIRSIZ(0, dp))
    389   1.6      cgd 					vprintf(stdout,
    390   1.6      cgd 					   "reclen less than DIRSIZ (%d < %d) ",
    391   1.6      cgd 					   dp->d_reclen, DIRSIZ(0, dp));
    392   1.6      cgd 				if (dp->d_namlen > NAME_MAX)
    393   1.6      cgd 					vprintf(stdout,
    394   1.6      cgd 					   "reclen name too big (%d > %d) ",
    395   1.6      cgd 					   dp->d_namlen, NAME_MAX);
    396   1.6      cgd 				vprintf(stdout, "\n");
    397   1.6      cgd 				loc += i;
    398   1.6      cgd 				continue;
    399   1.6      cgd 			}
    400   1.6      cgd 			loc += dp->d_reclen;
    401   1.6      cgd 			if (dp->d_ino != 0) {
    402   1.6      cgd 				putent(dp);
    403   1.6      cgd 			}
    404   1.6      cgd 		}
    405   1.6      cgd 	}
    406   1.6      cgd }
    407   1.6      cgd 
    408   1.6      cgd /*
    409   1.6      cgd  * These variables are "local" to the following two functions.
    410   1.6      cgd  */
    411   1.6      cgd char dirbuf[DIRBLKSIZ];
    412   1.6      cgd long dirloc = 0;
    413   1.6      cgd long prev = 0;
    414   1.6      cgd 
    415   1.6      cgd /*
    416   1.6      cgd  * add a new directory entry to a file.
    417   1.6      cgd  */
    418   1.6      cgd static void
    419   1.6      cgd putent(dp)
    420   1.6      cgd 	struct direct *dp;
    421   1.6      cgd {
    422   1.6      cgd 	dp->d_reclen = DIRSIZ(0, dp);
    423   1.6      cgd 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
    424   1.6      cgd 		((struct direct *)(dirbuf + prev))->d_reclen =
    425   1.6      cgd 		    DIRBLKSIZ - prev;
    426   1.6      cgd 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
    427   1.6      cgd 		dirloc = 0;
    428   1.6      cgd 	}
    429  1.10  mycroft 	memcpy(dirbuf + dirloc, dp, (long)dp->d_reclen);
    430   1.6      cgd 	prev = dirloc;
    431   1.6      cgd 	dirloc += dp->d_reclen;
    432   1.6      cgd }
    433   1.6      cgd 
    434   1.6      cgd /*
    435   1.6      cgd  * flush out a directory that is finished.
    436   1.6      cgd  */
    437   1.6      cgd static void
    438   1.6      cgd flushent()
    439   1.6      cgd {
    440   1.6      cgd 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
    441   1.6      cgd 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
    442   1.6      cgd 	seekpt = ftell(df);
    443   1.6      cgd 	dirloc = 0;
    444   1.6      cgd }
    445   1.6      cgd 
    446   1.6      cgd static void
    447   1.6      cgd dcvt(odp, ndp)
    448  1.22    lukem 	struct odirect *odp;
    449  1.22    lukem 	struct direct *ndp;
    450   1.6      cgd {
    451   1.6      cgd 
    452  1.22    lukem 	memset(ndp, 0, (size_t)(sizeof *ndp));
    453   1.6      cgd 	ndp->d_ino =  odp->d_ino;
    454   1.6      cgd 	ndp->d_type = DT_UNKNOWN;
    455   1.6      cgd 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
    456   1.6      cgd 	ndp->d_namlen = strlen(ndp->d_name);
    457   1.6      cgd 	ndp->d_reclen = DIRSIZ(0, ndp);
    458   1.6      cgd }
    459   1.6      cgd 
    460   1.6      cgd /*
    461   1.6      cgd  * Seek to an entry in a directory.
    462   1.6      cgd  * Only values returned by rst_telldir should be passed to rst_seekdir.
    463   1.6      cgd  * This routine handles many directories in a single file.
    464   1.6      cgd  * It takes the base of the directory in the file, plus
    465   1.6      cgd  * the desired seek offset into it.
    466   1.6      cgd  */
    467   1.6      cgd static void
    468   1.6      cgd rst_seekdir(dirp, loc, base)
    469  1.22    lukem 	RST_DIR *dirp;
    470   1.6      cgd 	long loc, base;
    471   1.6      cgd {
    472   1.6      cgd 
    473   1.6      cgd 	if (loc == rst_telldir(dirp))
    474   1.6      cgd 		return;
    475   1.6      cgd 	loc -= base;
    476   1.6      cgd 	if (loc < 0)
    477   1.6      cgd 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
    478   1.6      cgd 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
    479   1.6      cgd 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
    480   1.6      cgd 	if (dirp->dd_loc != 0)
    481   1.6      cgd 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
    482   1.6      cgd }
    483   1.6      cgd 
    484   1.6      cgd /*
    485   1.6      cgd  * get next entry in a directory.
    486   1.6      cgd  */
    487   1.6      cgd struct direct *
    488   1.6      cgd rst_readdir(dirp)
    489  1.22    lukem 	RST_DIR *dirp;
    490   1.6      cgd {
    491  1.22    lukem 	struct direct *dp;
    492   1.6      cgd 
    493   1.6      cgd 	for (;;) {
    494   1.6      cgd 		if (dirp->dd_loc == 0) {
    495  1.24       pk 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
    496   1.6      cgd 			    DIRBLKSIZ);
    497   1.6      cgd 			if (dirp->dd_size <= 0) {
    498   1.6      cgd 				dprintf(stderr, "error reading directory\n");
    499   1.6      cgd 				return (NULL);
    500   1.6      cgd 			}
    501   1.6      cgd 		}
    502   1.6      cgd 		if (dirp->dd_loc >= dirp->dd_size) {
    503   1.6      cgd 			dirp->dd_loc = 0;
    504   1.6      cgd 			continue;
    505   1.6      cgd 		}
    506   1.6      cgd 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
    507   1.6      cgd 		if (dp->d_reclen == 0 ||
    508   1.6      cgd 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
    509   1.6      cgd 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
    510   1.6      cgd 				dp->d_reclen);
    511   1.6      cgd 			return (NULL);
    512   1.6      cgd 		}
    513   1.6      cgd 		dirp->dd_loc += dp->d_reclen;
    514  1.11  mycroft 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
    515  1.11  mycroft 			return (NULL);
    516   1.6      cgd 		if (dp->d_ino >= maxino) {
    517   1.6      cgd 			dprintf(stderr, "corrupted directory: bad inum %d\n",
    518   1.6      cgd 				dp->d_ino);
    519   1.6      cgd 			continue;
    520   1.6      cgd 		}
    521   1.6      cgd 		return (dp);
    522   1.6      cgd 	}
    523   1.6      cgd }
    524   1.6      cgd 
    525   1.6      cgd /*
    526   1.6      cgd  * Simulate the opening of a directory
    527   1.6      cgd  */
    528   1.6      cgd RST_DIR *
    529   1.6      cgd rst_opendir(name)
    530   1.7  mycroft 	const char *name;
    531   1.6      cgd {
    532   1.6      cgd 	struct inotab *itp;
    533   1.6      cgd 	RST_DIR *dirp;
    534   1.6      cgd 	ino_t ino;
    535   1.6      cgd 
    536   1.6      cgd 	if ((ino = dirlookup(name)) > 0 &&
    537   1.6      cgd 	    (itp = inotablookup(ino)) != NULL) {
    538   1.6      cgd 		dirp = opendirfile(dirfile);
    539   1.6      cgd 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
    540   1.6      cgd 		return (dirp);
    541   1.6      cgd 	}
    542   1.7  mycroft 	return (NULL);
    543   1.6      cgd }
    544   1.6      cgd 
    545   1.6      cgd /*
    546   1.6      cgd  * In our case, there is nothing to do when closing a directory.
    547   1.6      cgd  */
    548   1.6      cgd void
    549   1.6      cgd rst_closedir(dirp)
    550   1.6      cgd 	RST_DIR *dirp;
    551   1.6      cgd {
    552   1.6      cgd 
    553   1.7  mycroft 	(void)close(dirp->dd_fd);
    554   1.6      cgd 	free(dirp);
    555   1.6      cgd 	return;
    556   1.6      cgd }
    557   1.6      cgd 
    558   1.6      cgd /*
    559   1.6      cgd  * Simulate finding the current offset in the directory.
    560   1.6      cgd  */
    561   1.6      cgd static long
    562   1.6      cgd rst_telldir(dirp)
    563   1.6      cgd 	RST_DIR *dirp;
    564   1.6      cgd {
    565   1.6      cgd 	return ((long)lseek(dirp->dd_fd,
    566   1.6      cgd 	    (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
    567   1.6      cgd }
    568   1.6      cgd 
    569   1.6      cgd /*
    570   1.6      cgd  * Open a directory file.
    571   1.6      cgd  */
    572   1.6      cgd static RST_DIR *
    573   1.6      cgd opendirfile(name)
    574   1.7  mycroft 	const char *name;
    575   1.6      cgd {
    576  1.22    lukem 	RST_DIR *dirp;
    577  1.22    lukem 	int fd;
    578   1.6      cgd 
    579   1.6      cgd 	if ((fd = open(name, O_RDONLY)) == -1)
    580   1.6      cgd 		return (NULL);
    581   1.6      cgd 	if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
    582   1.6      cgd 		(void)close(fd);
    583   1.6      cgd 		return (NULL);
    584   1.6      cgd 	}
    585   1.6      cgd 	dirp->dd_fd = fd;
    586   1.6      cgd 	dirp->dd_loc = 0;
    587   1.6      cgd 	return (dirp);
    588   1.6      cgd }
    589   1.6      cgd 
    590   1.6      cgd /*
    591   1.6      cgd  * Set the mode, owner, and times for all new or changed directories
    592   1.6      cgd  */
    593   1.6      cgd void
    594   1.6      cgd setdirmodes(flags)
    595   1.6      cgd 	int flags;
    596   1.6      cgd {
    597   1.6      cgd 	FILE *mf;
    598   1.6      cgd 	struct modeinfo node;
    599   1.6      cgd 	struct entry *ep;
    600   1.6      cgd 	char *cp;
    601  1.24       pk 
    602   1.6      cgd 	vprintf(stdout, "Set directory mode, owner, and times.\n");
    603  1.20    lukem 	if (command == 'r' || command == 'R')
    604  1.20    lukem 		(void) snprintf(modefile, sizeof(modefile), "%s/rstmode%d",
    605  1.20    lukem 		    _PATH_TMP, dumpdate);
    606  1.20    lukem 	if (modefile[0] == '#') {
    607  1.20    lukem 		panic("modefile not defined\n");
    608  1.20    lukem 		fprintf(stderr, "directory mode, owner, and times not set\n");
    609  1.20    lukem 		return;
    610  1.20    lukem 	}
    611   1.6      cgd 	mf = fopen(modefile, "r");
    612   1.6      cgd 	if (mf == NULL) {
    613   1.6      cgd 		fprintf(stderr, "fopen: %s\n", strerror(errno));
    614   1.6      cgd 		fprintf(stderr, "cannot open mode file %s\n", modefile);
    615   1.6      cgd 		fprintf(stderr, "directory mode, owner, and times not set\n");
    616   1.6      cgd 		return;
    617   1.6      cgd 	}
    618   1.6      cgd 	clearerr(mf);
    619   1.6      cgd 	for (;;) {
    620   1.6      cgd 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
    621   1.6      cgd 		if (feof(mf))
    622   1.6      cgd 			break;
    623   1.6      cgd 		ep = lookupino(node.ino);
    624   1.6      cgd 		if (command == 'i' || command == 'x') {
    625   1.6      cgd 			if (ep == NULL)
    626   1.6      cgd 				continue;
    627   1.6      cgd 			if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
    628   1.6      cgd 				ep->e_flags &= ~NEW;
    629   1.6      cgd 				continue;
    630   1.6      cgd 			}
    631   1.6      cgd 			if (node.ino == ROOTINO &&
    632   1.6      cgd 		   	    reply("set owner/mode for '.'") == FAIL)
    633   1.6      cgd 				continue;
    634   1.6      cgd 		}
    635   1.6      cgd 		if (ep == NULL) {
    636   1.6      cgd 			panic("cannot find directory inode %d\n", node.ino);
    637   1.6      cgd 		} else {
    638   1.6      cgd 			cp = myname(ep);
    639   1.6      cgd 			(void) chown(cp, node.uid, node.gid);
    640   1.6      cgd 			(void) chmod(cp, node.mode);
    641  1.11  mycroft 			(void) chflags(cp, node.flags);
    642   1.6      cgd 			utimes(cp, node.timep);
    643   1.6      cgd 			ep->e_flags &= ~NEW;
    644   1.6      cgd 		}
    645   1.6      cgd 	}
    646   1.6      cgd 	if (ferror(mf))
    647   1.6      cgd 		panic("error setting directory modes\n");
    648   1.6      cgd 	(void) fclose(mf);
    649   1.6      cgd }
    650   1.6      cgd 
    651   1.6      cgd /*
    652   1.6      cgd  * Generate a literal copy of a directory.
    653   1.6      cgd  */
    654   1.6      cgd int
    655   1.6      cgd genliteraldir(name, ino)
    656   1.6      cgd 	char *name;
    657   1.6      cgd 	ino_t ino;
    658   1.6      cgd {
    659  1.22    lukem 	struct inotab *itp;
    660   1.6      cgd 	int ofile, dp, i, size;
    661   1.6      cgd 	char buf[BUFSIZ];
    662   1.6      cgd 
    663   1.6      cgd 	itp = inotablookup(ino);
    664   1.6      cgd 	if (itp == NULL)
    665   1.6      cgd 		panic("Cannot find directory inode %d named %s\n", ino, name);
    666  1.10  mycroft 	if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
    667   1.6      cgd 		fprintf(stderr, "%s: ", name);
    668   1.6      cgd 		(void) fflush(stderr);
    669   1.6      cgd 		fprintf(stderr, "cannot create file: %s\n", strerror(errno));
    670   1.6      cgd 		return (FAIL);
    671   1.6      cgd 	}
    672   1.6      cgd 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
    673   1.6      cgd 	dp = dup(dirp->dd_fd);
    674   1.6      cgd 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
    675   1.6      cgd 		size = i < BUFSIZ ? i : BUFSIZ;
    676   1.6      cgd 		if (read(dp, buf, (int) size) == -1) {
    677   1.6      cgd 			fprintf(stderr,
    678   1.6      cgd 				"write error extracting inode %d, name %s\n",
    679   1.6      cgd 				curfile.ino, curfile.name);
    680   1.6      cgd 			fprintf(stderr, "read: %s\n", strerror(errno));
    681  1.12  mycroft 			exit(1);
    682   1.6      cgd 		}
    683   1.6      cgd 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
    684   1.6      cgd 			fprintf(stderr,
    685   1.6      cgd 				"write error extracting inode %d, name %s\n",
    686   1.6      cgd 				curfile.ino, curfile.name);
    687   1.6      cgd 			fprintf(stderr, "write: %s\n", strerror(errno));
    688  1.12  mycroft 			exit(1);
    689   1.6      cgd 		}
    690   1.6      cgd 	}
    691   1.6      cgd 	(void) close(dp);
    692   1.6      cgd 	(void) close(ofile);
    693   1.6      cgd 	return (GOOD);
    694   1.6      cgd }
    695   1.6      cgd 
    696   1.6      cgd /*
    697   1.6      cgd  * Determine the type of an inode
    698   1.6      cgd  */
    699   1.6      cgd int
    700   1.6      cgd inodetype(ino)
    701   1.6      cgd 	ino_t ino;
    702   1.6      cgd {
    703   1.6      cgd 	struct inotab *itp;
    704   1.6      cgd 
    705   1.6      cgd 	itp = inotablookup(ino);
    706   1.6      cgd 	if (itp == NULL)
    707   1.6      cgd 		return (LEAF);
    708   1.6      cgd 	return (NODE);
    709   1.6      cgd }
    710   1.6      cgd 
    711   1.6      cgd /*
    712   1.6      cgd  * Allocate and initialize a directory inode entry.
    713   1.6      cgd  * If requested, save its pertinent mode, owner, and time info.
    714   1.6      cgd  */
    715   1.6      cgd static struct inotab *
    716   1.6      cgd allocinotab(ino, dip, seekpt)
    717   1.6      cgd 	ino_t ino;
    718   1.6      cgd 	struct dinode *dip;
    719   1.6      cgd 	long seekpt;
    720   1.6      cgd {
    721  1.22    lukem 	struct inotab	*itp;
    722   1.6      cgd 	struct modeinfo node;
    723   1.6      cgd 
    724   1.6      cgd 	itp = calloc(1, sizeof(struct inotab));
    725   1.6      cgd 	if (itp == NULL)
    726   1.6      cgd 		panic("no memory directory table\n");
    727   1.6      cgd 	itp->t_next = inotab[INOHASH(ino)];
    728   1.6      cgd 	inotab[INOHASH(ino)] = itp;
    729   1.6      cgd 	itp->t_ino = ino;
    730   1.6      cgd 	itp->t_seekpt = seekpt;
    731   1.6      cgd 	if (mf == NULL)
    732   1.7  mycroft 		return (itp);
    733   1.6      cgd 	node.ino = ino;
    734  1.16      cgd 	node.timep[0].tv_sec = dip->di_atime;
    735  1.16      cgd 	node.timep[0].tv_usec = dip->di_atimensec / 1000;
    736  1.16      cgd 	node.timep[1].tv_sec = dip->di_mtime;
    737  1.16      cgd 	node.timep[1].tv_usec = dip->di_mtimensec / 1000;
    738   1.6      cgd 	node.mode = dip->di_mode;
    739  1.11  mycroft 	node.flags = dip->di_flags;
    740   1.6      cgd 	node.uid = dip->di_uid;
    741   1.6      cgd 	node.gid = dip->di_gid;
    742   1.6      cgd 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
    743   1.7  mycroft 	return (itp);
    744   1.6      cgd }
    745   1.6      cgd 
    746   1.6      cgd /*
    747   1.6      cgd  * Look up an inode in the table of directories
    748   1.6      cgd  */
    749   1.6      cgd static struct inotab *
    750   1.6      cgd inotablookup(ino)
    751   1.6      cgd 	ino_t	ino;
    752   1.6      cgd {
    753  1.22    lukem 	struct inotab *itp;
    754   1.6      cgd 
    755   1.6      cgd 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
    756   1.6      cgd 		if (itp->t_ino == ino)
    757   1.7  mycroft 			return (itp);
    758   1.6      cgd 	return (NULL);
    759   1.6      cgd }
    760   1.6      cgd 
    761   1.6      cgd /*
    762   1.6      cgd  * Clean up and exit
    763   1.6      cgd  */
    764  1.12  mycroft void
    765  1.12  mycroft cleanup()
    766   1.6      cgd {
    767   1.6      cgd 
    768   1.6      cgd 	closemt();
    769   1.6      cgd 	if (modefile[0] != '#')
    770   1.6      cgd 		(void) unlink(modefile);
    771   1.6      cgd 	if (dirfile[0] != '#')
    772   1.6      cgd 		(void) unlink(dirfile);
    773   1.6      cgd }
    774