Home | History | Annotate | Line # | Download | only in restore
symtab.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1983 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)symtab.c	5.5 (Berkeley) 6/1/90";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd /*
     39  1.1  cgd  * These routines maintain the symbol table which tracks the state
     40  1.1  cgd  * of the file system being restored. They provide lookup by either
     41  1.1  cgd  * name or inode number. They also provide for creation, deletion,
     42  1.1  cgd  * and renaming of entries. Because of the dynamic nature of pathnames,
     43  1.1  cgd  * names should not be saved, but always constructed just before they
     44  1.1  cgd  * are needed, by calling "myname".
     45  1.1  cgd  */
     46  1.1  cgd 
     47  1.1  cgd #include "restore.h"
     48  1.1  cgd #include <sys/stat.h>
     49  1.1  cgd #include <ufs/dir.h>
     50  1.1  cgd 
     51  1.1  cgd /*
     52  1.1  cgd  * The following variables define the inode symbol table.
     53  1.1  cgd  * The primary hash table is dynamically allocated based on
     54  1.1  cgd  * the number of inodes in the file system (maxino), scaled by
     55  1.1  cgd  * HASHFACTOR. The variable "entry" points to the hash table;
     56  1.1  cgd  * the variable "entrytblsize" indicates its size (in entries).
     57  1.1  cgd  */
     58  1.1  cgd #define HASHFACTOR 5
     59  1.1  cgd static struct entry **entry;
     60  1.1  cgd static long entrytblsize;
     61  1.1  cgd 
     62  1.1  cgd /*
     63  1.1  cgd  * Look up an entry by inode number
     64  1.1  cgd  */
     65  1.1  cgd struct entry *
     66  1.1  cgd lookupino(inum)
     67  1.1  cgd 	ino_t inum;
     68  1.1  cgd {
     69  1.1  cgd 	register struct entry *ep;
     70  1.1  cgd 
     71  1.1  cgd 	if (inum < ROOTINO || inum >= maxino)
     72  1.1  cgd 		return (NIL);
     73  1.1  cgd 	for (ep = entry[inum % entrytblsize]; ep != NIL; ep = ep->e_next)
     74  1.1  cgd 		if (ep->e_ino == inum)
     75  1.1  cgd 			return (ep);
     76  1.1  cgd 	return (NIL);
     77  1.1  cgd }
     78  1.1  cgd 
     79  1.1  cgd /*
     80  1.1  cgd  * Add an entry into the entry table
     81  1.1  cgd  */
     82  1.1  cgd addino(inum, np)
     83  1.1  cgd 	ino_t inum;
     84  1.1  cgd 	struct entry *np;
     85  1.1  cgd {
     86  1.1  cgd 	struct entry **epp;
     87  1.1  cgd 
     88  1.1  cgd 	if (inum < ROOTINO || inum >= maxino)
     89  1.1  cgd 		panic("addino: out of range %d\n", inum);
     90  1.1  cgd 	epp = &entry[inum % entrytblsize];
     91  1.1  cgd 	np->e_ino = inum;
     92  1.1  cgd 	np->e_next = *epp;
     93  1.1  cgd 	*epp = np;
     94  1.1  cgd 	if (dflag)
     95  1.1  cgd 		for (np = np->e_next; np != NIL; np = np->e_next)
     96  1.1  cgd 			if (np->e_ino == inum)
     97  1.1  cgd 				badentry(np, "duplicate inum");
     98  1.1  cgd }
     99  1.1  cgd 
    100  1.1  cgd /*
    101  1.1  cgd  * Delete an entry from the entry table
    102  1.1  cgd  */
    103  1.1  cgd deleteino(inum)
    104  1.1  cgd 	ino_t inum;
    105  1.1  cgd {
    106  1.1  cgd 	register struct entry *next;
    107  1.1  cgd 	struct entry **prev;
    108  1.1  cgd 
    109  1.1  cgd 	if (inum < ROOTINO || inum >= maxino)
    110  1.1  cgd 		panic("deleteino: out of range %d\n", inum);
    111  1.1  cgd 	prev = &entry[inum % entrytblsize];
    112  1.1  cgd 	for (next = *prev; next != NIL; next = next->e_next) {
    113  1.1  cgd 		if (next->e_ino == inum) {
    114  1.1  cgd 			next->e_ino = 0;
    115  1.1  cgd 			*prev = next->e_next;
    116  1.1  cgd 			return;
    117  1.1  cgd 		}
    118  1.1  cgd 		prev = &next->e_next;
    119  1.1  cgd 	}
    120  1.1  cgd 	panic("deleteino: %d not found\n", inum);
    121  1.1  cgd }
    122  1.1  cgd 
    123  1.1  cgd /*
    124  1.1  cgd  * Look up an entry by name
    125  1.1  cgd  */
    126  1.1  cgd struct entry *
    127  1.1  cgd lookupname(name)
    128  1.1  cgd 	char *name;
    129  1.1  cgd {
    130  1.1  cgd 	register struct entry *ep;
    131  1.1  cgd 	register char *np, *cp;
    132  1.1  cgd 	char buf[MAXPATHLEN];
    133  1.1  cgd 
    134  1.1  cgd 	cp = name;
    135  1.1  cgd 	for (ep = lookupino(ROOTINO); ep != NIL; ep = ep->e_entries) {
    136  1.1  cgd 		for (np = buf; *cp != '/' && *cp != '\0'; )
    137  1.1  cgd 			*np++ = *cp++;
    138  1.1  cgd 		*np = '\0';
    139  1.1  cgd 		for ( ; ep != NIL; ep = ep->e_sibling)
    140  1.1  cgd 			if (strcmp(ep->e_name, buf) == 0)
    141  1.1  cgd 				break;
    142  1.1  cgd 		if (ep == NIL)
    143  1.1  cgd 			break;
    144  1.1  cgd 		if (*cp++ == '\0')
    145  1.1  cgd 			return (ep);
    146  1.1  cgd 	}
    147  1.1  cgd 	return (NIL);
    148  1.1  cgd }
    149  1.1  cgd 
    150  1.1  cgd /*
    151  1.1  cgd  * Look up the parent of a pathname
    152  1.1  cgd  */
    153  1.1  cgd struct entry *
    154  1.1  cgd lookupparent(name)
    155  1.1  cgd 	char *name;
    156  1.1  cgd {
    157  1.1  cgd 	struct entry *ep;
    158  1.1  cgd 	char *tailindex;
    159  1.1  cgd 
    160  1.1  cgd 	tailindex = rindex(name, '/');
    161  1.1  cgd 	if (tailindex == 0)
    162  1.1  cgd 		return (NIL);
    163  1.1  cgd 	*tailindex = '\0';
    164  1.1  cgd 	ep = lookupname(name);
    165  1.1  cgd 	*tailindex = '/';
    166  1.1  cgd 	if (ep == NIL)
    167  1.1  cgd 		return (NIL);
    168  1.1  cgd 	if (ep->e_type != NODE)
    169  1.1  cgd 		panic("%s is not a directory\n", name);
    170  1.1  cgd 	return (ep);
    171  1.1  cgd }
    172  1.1  cgd 
    173  1.1  cgd /*
    174  1.1  cgd  * Determine the current pathname of a node or leaf
    175  1.1  cgd  */
    176  1.1  cgd char *
    177  1.1  cgd myname(ep)
    178  1.1  cgd 	register struct entry *ep;
    179  1.1  cgd {
    180  1.1  cgd 	register char *cp;
    181  1.1  cgd 	static char namebuf[MAXPATHLEN];
    182  1.1  cgd 
    183  1.1  cgd 	for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
    184  1.1  cgd 		cp -= ep->e_namlen;
    185  1.1  cgd 		bcopy(ep->e_name, cp, (long)ep->e_namlen);
    186  1.1  cgd 		if (ep == lookupino(ROOTINO))
    187  1.1  cgd 			return (cp);
    188  1.1  cgd 		*(--cp) = '/';
    189  1.1  cgd 		ep = ep->e_parent;
    190  1.1  cgd 	}
    191  1.1  cgd 	panic("%s: pathname too long\n", cp);
    192  1.1  cgd 	return(cp);
    193  1.1  cgd }
    194  1.1  cgd 
    195  1.1  cgd /*
    196  1.1  cgd  * Unused symbol table entries are linked together on a freelist
    197  1.1  cgd  * headed by the following pointer.
    198  1.1  cgd  */
    199  1.1  cgd static struct entry *freelist = NIL;
    200  1.1  cgd 
    201  1.1  cgd /*
    202  1.1  cgd  * add an entry to the symbol table
    203  1.1  cgd  */
    204  1.1  cgd struct entry *
    205  1.1  cgd addentry(name, inum, type)
    206  1.1  cgd 	char *name;
    207  1.1  cgd 	ino_t inum;
    208  1.1  cgd 	int type;
    209  1.1  cgd {
    210  1.1  cgd 	register struct entry *np, *ep;
    211  1.1  cgd 
    212  1.1  cgd 	if (freelist != NIL) {
    213  1.1  cgd 		np = freelist;
    214  1.1  cgd 		freelist = np->e_next;
    215  1.1  cgd 		bzero((char *)np, (long)sizeof(struct entry));
    216  1.1  cgd 	} else {
    217  1.1  cgd 		np = (struct entry *)calloc(1, sizeof(struct entry));
    218  1.1  cgd 		if (np == NIL)
    219  1.1  cgd 			panic("no memory to extend symbol table\n");
    220  1.1  cgd 	}
    221  1.1  cgd 	np->e_type = type & ~LINK;
    222  1.1  cgd 	ep = lookupparent(name);
    223  1.1  cgd 	if (ep == NIL) {
    224  1.1  cgd 		if (inum != ROOTINO || lookupino(ROOTINO) != NIL)
    225  1.1  cgd 			panic("bad name to addentry %s\n", name);
    226  1.1  cgd 		np->e_name = savename(name);
    227  1.1  cgd 		np->e_namlen = strlen(name);
    228  1.1  cgd 		np->e_parent = np;
    229  1.1  cgd 		addino(ROOTINO, np);
    230  1.1  cgd 		return (np);
    231  1.1  cgd 	}
    232  1.1  cgd 	np->e_name = savename(rindex(name, '/') + 1);
    233  1.1  cgd 	np->e_namlen = strlen(np->e_name);
    234  1.1  cgd 	np->e_parent = ep;
    235  1.1  cgd 	np->e_sibling = ep->e_entries;
    236  1.1  cgd 	ep->e_entries = np;
    237  1.1  cgd 	if (type & LINK) {
    238  1.1  cgd 		ep = lookupino(inum);
    239  1.1  cgd 		if (ep == NIL)
    240  1.1  cgd 			panic("link to non-existant name\n");
    241  1.1  cgd 		np->e_ino = inum;
    242  1.1  cgd 		np->e_links = ep->e_links;
    243  1.1  cgd 		ep->e_links = np;
    244  1.1  cgd 	} else if (inum != 0) {
    245  1.1  cgd 		if (lookupino(inum) != NIL)
    246  1.1  cgd 			panic("duplicate entry\n");
    247  1.1  cgd 		addino(inum, np);
    248  1.1  cgd 	}
    249  1.1  cgd 	return (np);
    250  1.1  cgd }
    251  1.1  cgd 
    252  1.1  cgd /*
    253  1.1  cgd  * delete an entry from the symbol table
    254  1.1  cgd  */
    255  1.1  cgd freeentry(ep)
    256  1.1  cgd 	register struct entry *ep;
    257  1.1  cgd {
    258  1.1  cgd 	register struct entry *np;
    259  1.1  cgd 	ino_t inum;
    260  1.1  cgd 
    261  1.1  cgd 	if (ep->e_flags != REMOVED)
    262  1.1  cgd 		badentry(ep, "not marked REMOVED");
    263  1.1  cgd 	if (ep->e_type == NODE) {
    264  1.1  cgd 		if (ep->e_links != NIL)
    265  1.1  cgd 			badentry(ep, "freeing referenced directory");
    266  1.1  cgd 		if (ep->e_entries != NIL)
    267  1.1  cgd 			badentry(ep, "freeing non-empty directory");
    268  1.1  cgd 	}
    269  1.1  cgd 	if (ep->e_ino != 0) {
    270  1.1  cgd 		np = lookupino(ep->e_ino);
    271  1.1  cgd 		if (np == NIL)
    272  1.1  cgd 			badentry(ep, "lookupino failed");
    273  1.1  cgd 		if (np == ep) {
    274  1.1  cgd 			inum = ep->e_ino;
    275  1.1  cgd 			deleteino(inum);
    276  1.1  cgd 			if (ep->e_links != NIL)
    277  1.1  cgd 				addino(inum, ep->e_links);
    278  1.1  cgd 		} else {
    279  1.1  cgd 			for (; np != NIL; np = np->e_links) {
    280  1.1  cgd 				if (np->e_links == ep) {
    281  1.1  cgd 					np->e_links = ep->e_links;
    282  1.1  cgd 					break;
    283  1.1  cgd 				}
    284  1.1  cgd 			}
    285  1.1  cgd 			if (np == NIL)
    286  1.1  cgd 				badentry(ep, "link not found");
    287  1.1  cgd 		}
    288  1.1  cgd 	}
    289  1.1  cgd 	removeentry(ep);
    290  1.1  cgd 	freename(ep->e_name);
    291  1.1  cgd 	ep->e_next = freelist;
    292  1.1  cgd 	freelist = ep;
    293  1.1  cgd }
    294  1.1  cgd 
    295  1.1  cgd /*
    296  1.1  cgd  * Relocate an entry in the tree structure
    297  1.1  cgd  */
    298  1.1  cgd moveentry(ep, newname)
    299  1.1  cgd 	register struct entry *ep;
    300  1.1  cgd 	char *newname;
    301  1.1  cgd {
    302  1.1  cgd 	struct entry *np;
    303  1.1  cgd 	char *cp;
    304  1.1  cgd 
    305  1.1  cgd 	np = lookupparent(newname);
    306  1.1  cgd 	if (np == NIL)
    307  1.1  cgd 		badentry(ep, "cannot move ROOT");
    308  1.1  cgd 	if (np != ep->e_parent) {
    309  1.1  cgd 		removeentry(ep);
    310  1.1  cgd 		ep->e_parent = np;
    311  1.1  cgd 		ep->e_sibling = np->e_entries;
    312  1.1  cgd 		np->e_entries = ep;
    313  1.1  cgd 	}
    314  1.1  cgd 	cp = rindex(newname, '/') + 1;
    315  1.1  cgd 	freename(ep->e_name);
    316  1.1  cgd 	ep->e_name = savename(cp);
    317  1.1  cgd 	ep->e_namlen = strlen(cp);
    318  1.1  cgd 	if (strcmp(gentempname(ep), ep->e_name) == 0)
    319  1.1  cgd 		ep->e_flags |= TMPNAME;
    320  1.1  cgd 	else
    321  1.1  cgd 		ep->e_flags &= ~TMPNAME;
    322  1.1  cgd }
    323  1.1  cgd 
    324  1.1  cgd /*
    325  1.1  cgd  * Remove an entry in the tree structure
    326  1.1  cgd  */
    327  1.1  cgd removeentry(ep)
    328  1.1  cgd 	register struct entry *ep;
    329  1.1  cgd {
    330  1.1  cgd 	register struct entry *np;
    331  1.1  cgd 
    332  1.1  cgd 	np = ep->e_parent;
    333  1.1  cgd 	if (np->e_entries == ep) {
    334  1.1  cgd 		np->e_entries = ep->e_sibling;
    335  1.1  cgd 	} else {
    336  1.1  cgd 		for (np = np->e_entries; np != NIL; np = np->e_sibling) {
    337  1.1  cgd 			if (np->e_sibling == ep) {
    338  1.1  cgd 				np->e_sibling = ep->e_sibling;
    339  1.1  cgd 				break;
    340  1.1  cgd 			}
    341  1.1  cgd 		}
    342  1.1  cgd 		if (np == NIL)
    343  1.1  cgd 			badentry(ep, "cannot find entry in parent list");
    344  1.1  cgd 	}
    345  1.1  cgd }
    346  1.1  cgd 
    347  1.1  cgd /*
    348  1.1  cgd  * Table of unused string entries, sorted by length.
    349  1.1  cgd  *
    350  1.1  cgd  * Entries are allocated in STRTBLINCR sized pieces so that names
    351  1.1  cgd  * of similar lengths can use the same entry. The value of STRTBLINCR
    352  1.1  cgd  * is chosen so that every entry has at least enough space to hold
    353  1.1  cgd  * a "struct strtbl" header. Thus every entry can be linked onto an
    354  1.1  cgd  * apprpriate free list.
    355  1.1  cgd  *
    356  1.1  cgd  * NB. The macro "allocsize" below assumes that "struct strhdr"
    357  1.1  cgd  *     has a size that is a power of two.
    358  1.1  cgd  */
    359  1.1  cgd struct strhdr {
    360  1.1  cgd 	struct strhdr *next;
    361  1.1  cgd };
    362  1.1  cgd 
    363  1.1  cgd #define STRTBLINCR	(sizeof(struct strhdr))
    364  1.1  cgd #define allocsize(size)	(((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
    365  1.1  cgd 
    366  1.1  cgd static struct strhdr strtblhdr[allocsize(MAXNAMLEN) / STRTBLINCR];
    367  1.1  cgd 
    368  1.1  cgd /*
    369  1.1  cgd  * Allocate space for a name. It first looks to see if it already
    370  1.1  cgd  * has an appropriate sized entry, and if not allocates a new one.
    371  1.1  cgd  */
    372  1.1  cgd char *
    373  1.1  cgd savename(name)
    374  1.1  cgd 	char *name;
    375  1.1  cgd {
    376  1.1  cgd 	struct strhdr *np;
    377  1.1  cgd 	long len;
    378  1.1  cgd 	char *cp;
    379  1.1  cgd 
    380  1.1  cgd 	if (name == NULL)
    381  1.1  cgd 		panic("bad name\n");
    382  1.1  cgd 	len = strlen(name);
    383  1.1  cgd 	np = strtblhdr[len / STRTBLINCR].next;
    384  1.1  cgd 	if (np != NULL) {
    385  1.1  cgd 		strtblhdr[len / STRTBLINCR].next = np->next;
    386  1.1  cgd 		cp = (char *)np;
    387  1.1  cgd 	} else {
    388  1.1  cgd 		cp = malloc((unsigned)allocsize(len));
    389  1.1  cgd 		if (cp == NULL)
    390  1.1  cgd 			panic("no space for string table\n");
    391  1.1  cgd 	}
    392  1.1  cgd 	(void) strcpy(cp, name);
    393  1.1  cgd 	return (cp);
    394  1.1  cgd }
    395  1.1  cgd 
    396  1.1  cgd /*
    397  1.1  cgd  * Free space for a name. The resulting entry is linked onto the
    398  1.1  cgd  * appropriate free list.
    399  1.1  cgd  */
    400  1.1  cgd freename(name)
    401  1.1  cgd 	char *name;
    402  1.1  cgd {
    403  1.1  cgd 	struct strhdr *tp, *np;
    404  1.1  cgd 
    405  1.1  cgd 	tp = &strtblhdr[strlen(name) / STRTBLINCR];
    406  1.1  cgd 	np = (struct strhdr *)name;
    407  1.1  cgd 	np->next = tp->next;
    408  1.1  cgd 	tp->next = np;
    409  1.1  cgd }
    410  1.1  cgd 
    411  1.1  cgd /*
    412  1.1  cgd  * Useful quantities placed at the end of a dumped symbol table.
    413  1.1  cgd  */
    414  1.1  cgd struct symtableheader {
    415  1.1  cgd 	long	volno;
    416  1.1  cgd 	long	stringsize;
    417  1.1  cgd 	long	entrytblsize;
    418  1.1  cgd 	time_t	dumptime;
    419  1.1  cgd 	time_t	dumpdate;
    420  1.1  cgd 	ino_t	maxino;
    421  1.1  cgd 	long	ntrec;
    422  1.1  cgd };
    423  1.1  cgd 
    424  1.1  cgd /*
    425  1.1  cgd  * dump a snapshot of the symbol table
    426  1.1  cgd  */
    427  1.1  cgd dumpsymtable(filename, checkpt)
    428  1.1  cgd 	char *filename;
    429  1.1  cgd 	long checkpt;
    430  1.1  cgd {
    431  1.1  cgd 	register struct entry *ep, *tep;
    432  1.1  cgd 	register ino_t i;
    433  1.1  cgd 	struct entry temp, *tentry;
    434  1.1  cgd 	long mynum = 1, stroff = 0;
    435  1.1  cgd 	FILE *fd;
    436  1.1  cgd 	struct symtableheader hdr;
    437  1.1  cgd 
    438  1.1  cgd 	vprintf(stdout, "Check pointing the restore\n");
    439  1.1  cgd 	if (Nflag)
    440  1.1  cgd 		return;
    441  1.1  cgd 	if ((fd = fopen(filename, "w")) == NULL) {
    442  1.1  cgd 		perror("fopen");
    443  1.1  cgd 		panic("cannot create save file %s for symbol table\n",
    444  1.1  cgd 			filename);
    445  1.1  cgd 	}
    446  1.1  cgd 	clearerr(fd);
    447  1.1  cgd 	/*
    448  1.1  cgd 	 * Assign indicies to each entry
    449  1.1  cgd 	 * Write out the string entries
    450  1.1  cgd 	 */
    451  1.1  cgd 	for (i = ROOTINO; i < maxino; i++) {
    452  1.1  cgd 		for (ep = lookupino(i); ep != NIL; ep = ep->e_links) {
    453  1.1  cgd 			ep->e_index = mynum++;
    454  1.1  cgd 			(void) fwrite(ep->e_name, sizeof(char),
    455  1.1  cgd 			       (int)allocsize(ep->e_namlen), fd);
    456  1.1  cgd 		}
    457  1.1  cgd 	}
    458  1.1  cgd 	/*
    459  1.1  cgd 	 * Convert pointers to indexes, and output
    460  1.1  cgd 	 */
    461  1.1  cgd 	tep = &temp;
    462  1.1  cgd 	stroff = 0;
    463  1.1  cgd 	for (i = ROOTINO; i < maxino; i++) {
    464  1.1  cgd 		for (ep = lookupino(i); ep != NIL; ep = ep->e_links) {
    465  1.1  cgd 			bcopy((char *)ep, (char *)tep,
    466  1.1  cgd 				(long)sizeof(struct entry));
    467  1.1  cgd 			tep->e_name = (char *)stroff;
    468  1.1  cgd 			stroff += allocsize(ep->e_namlen);
    469  1.1  cgd 			tep->e_parent = (struct entry *)ep->e_parent->e_index;
    470  1.1  cgd 			if (ep->e_links != NIL)
    471  1.1  cgd 				tep->e_links =
    472  1.1  cgd 					(struct entry *)ep->e_links->e_index;
    473  1.1  cgd 			if (ep->e_sibling != NIL)
    474  1.1  cgd 				tep->e_sibling =
    475  1.1  cgd 					(struct entry *)ep->e_sibling->e_index;
    476  1.1  cgd 			if (ep->e_entries != NIL)
    477  1.1  cgd 				tep->e_entries =
    478  1.1  cgd 					(struct entry *)ep->e_entries->e_index;
    479  1.1  cgd 			if (ep->e_next != NIL)
    480  1.1  cgd 				tep->e_next =
    481  1.1  cgd 					(struct entry *)ep->e_next->e_index;
    482  1.1  cgd 			(void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
    483  1.1  cgd 		}
    484  1.1  cgd 	}
    485  1.1  cgd 	/*
    486  1.1  cgd 	 * Convert entry pointers to indexes, and output
    487  1.1  cgd 	 */
    488  1.1  cgd 	for (i = 0; i < entrytblsize; i++) {
    489  1.1  cgd 		if (entry[i] == NIL)
    490  1.1  cgd 			tentry = NIL;
    491  1.1  cgd 		else
    492  1.1  cgd 			tentry = (struct entry *)entry[i]->e_index;
    493  1.1  cgd 		(void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
    494  1.1  cgd 	}
    495  1.1  cgd 	hdr.volno = checkpt;
    496  1.1  cgd 	hdr.maxino = maxino;
    497  1.1  cgd 	hdr.entrytblsize = entrytblsize;
    498  1.1  cgd 	hdr.stringsize = stroff;
    499  1.1  cgd 	hdr.dumptime = dumptime;
    500  1.1  cgd 	hdr.dumpdate = dumpdate;
    501  1.1  cgd 	hdr.ntrec = ntrec;
    502  1.1  cgd 	(void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
    503  1.1  cgd 	if (ferror(fd)) {
    504  1.1  cgd 		perror("fwrite");
    505  1.1  cgd 		panic("output error to file %s writing symbol table\n",
    506  1.1  cgd 			filename);
    507  1.1  cgd 	}
    508  1.1  cgd 	(void) fclose(fd);
    509  1.1  cgd }
    510  1.1  cgd 
    511  1.1  cgd /*
    512  1.1  cgd  * Initialize a symbol table from a file
    513  1.1  cgd  */
    514  1.1  cgd initsymtable(filename)
    515  1.1  cgd 	char *filename;
    516  1.1  cgd {
    517  1.1  cgd 	char *base;
    518  1.1  cgd 	long tblsize;
    519  1.1  cgd 	register struct entry *ep;
    520  1.1  cgd 	struct entry *baseep, *lep;
    521  1.1  cgd 	struct symtableheader hdr;
    522  1.1  cgd 	struct stat stbuf;
    523  1.1  cgd 	register long i;
    524  1.1  cgd 	int fd;
    525  1.1  cgd 
    526  1.1  cgd 	vprintf(stdout, "Initialize symbol table.\n");
    527  1.1  cgd 	if (filename == NULL) {
    528  1.1  cgd 		entrytblsize = maxino / HASHFACTOR;
    529  1.1  cgd 		entry = (struct entry **)
    530  1.1  cgd 			calloc((unsigned)entrytblsize, sizeof(struct entry *));
    531  1.1  cgd 		if (entry == (struct entry **)NIL)
    532  1.1  cgd 			panic("no memory for entry table\n");
    533  1.1  cgd 		ep = addentry(".", ROOTINO, NODE);
    534  1.1  cgd 		ep->e_flags |= NEW;
    535  1.1  cgd 		return;
    536  1.1  cgd 	}
    537  1.1  cgd 	if ((fd = open(filename, 0)) < 0) {
    538  1.1  cgd 		perror("open");
    539  1.1  cgd 		panic("cannot open symbol table file %s\n", filename);
    540  1.1  cgd 	}
    541  1.1  cgd 	if (fstat(fd, &stbuf) < 0) {
    542  1.1  cgd 		perror("stat");
    543  1.1  cgd 		panic("cannot stat symbol table file %s\n", filename);
    544  1.1  cgd 	}
    545  1.1  cgd 	tblsize = stbuf.st_size - sizeof(struct symtableheader);
    546  1.1  cgd 	base = calloc(sizeof(char), (unsigned)tblsize);
    547  1.1  cgd 	if (base == NULL)
    548  1.1  cgd 		panic("cannot allocate space for symbol table\n");
    549  1.1  cgd 	if (read(fd, base, (int)tblsize) < 0 ||
    550  1.1  cgd 	    read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
    551  1.1  cgd 		perror("read");
    552  1.1  cgd 		panic("cannot read symbol table file %s\n", filename);
    553  1.1  cgd 	}
    554  1.1  cgd 	switch (command) {
    555  1.1  cgd 	case 'r':
    556  1.1  cgd 		/*
    557  1.1  cgd 		 * For normal continuation, insure that we are using
    558  1.1  cgd 		 * the next incremental tape
    559  1.1  cgd 		 */
    560  1.1  cgd 		if (hdr.dumpdate != dumptime) {
    561  1.1  cgd 			if (hdr.dumpdate < dumptime)
    562  1.1  cgd 				fprintf(stderr, "Incremental tape too low\n");
    563  1.1  cgd 			else
    564  1.1  cgd 				fprintf(stderr, "Incremental tape too high\n");
    565  1.1  cgd 			done(1);
    566  1.1  cgd 		}
    567  1.1  cgd 		break;
    568  1.1  cgd 	case 'R':
    569  1.1  cgd 		/*
    570  1.1  cgd 		 * For restart, insure that we are using the same tape
    571  1.1  cgd 		 */
    572  1.1  cgd 		curfile.action = SKIP;
    573  1.1  cgd 		dumptime = hdr.dumptime;
    574  1.1  cgd 		dumpdate = hdr.dumpdate;
    575  1.1  cgd 		if (!bflag)
    576  1.1  cgd 			newtapebuf(hdr.ntrec);
    577  1.1  cgd 		getvol(hdr.volno);
    578  1.1  cgd 		break;
    579  1.1  cgd 	default:
    580  1.1  cgd 		panic("initsymtable called from command %c\n", command);
    581  1.1  cgd 		break;
    582  1.1  cgd 	}
    583  1.1  cgd 	maxino = hdr.maxino;
    584  1.1  cgd 	entrytblsize = hdr.entrytblsize;
    585  1.1  cgd 	entry = (struct entry **)
    586  1.1  cgd 		(base + tblsize - (entrytblsize * sizeof(struct entry *)));
    587  1.1  cgd 	baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
    588  1.1  cgd 	lep = (struct entry *)entry;
    589  1.1  cgd 	for (i = 0; i < entrytblsize; i++) {
    590  1.1  cgd 		if (entry[i] == NIL)
    591  1.1  cgd 			continue;
    592  1.1  cgd 		entry[i] = &baseep[(long)entry[i]];
    593  1.1  cgd 	}
    594  1.1  cgd 	for (ep = &baseep[1]; ep < lep; ep++) {
    595  1.1  cgd 		ep->e_name = base + (long)ep->e_name;
    596  1.1  cgd 		ep->e_parent = &baseep[(long)ep->e_parent];
    597  1.1  cgd 		if (ep->e_sibling != NIL)
    598  1.1  cgd 			ep->e_sibling = &baseep[(long)ep->e_sibling];
    599  1.1  cgd 		if (ep->e_links != NIL)
    600  1.1  cgd 			ep->e_links = &baseep[(long)ep->e_links];
    601  1.1  cgd 		if (ep->e_entries != NIL)
    602  1.1  cgd 			ep->e_entries = &baseep[(long)ep->e_entries];
    603  1.1  cgd 		if (ep->e_next != NIL)
    604  1.1  cgd 			ep->e_next = &baseep[(long)ep->e_next];
    605  1.1  cgd 	}
    606  1.1  cgd }
    607