symtab.c revision 102231
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1983, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3537906Scharnier#if 0
3623685Speterstatic char sccsid[] = "@(#)symtab.c	8.3 (Berkeley) 4/28/95";
3737906Scharnier#endif
3837906Scharnierstatic const char rcsid[] =
3950476Speter  "$FreeBSD: head/sbin/restore/symtab.c 102231 2002-08-21 18:11:48Z trhodes $";
401558Srgrimes#endif /* not lint */
411558Srgrimes
421558Srgrimes/*
431558Srgrimes * These routines maintain the symbol table which tracks the state
44102231Strhodes * of the file system being restored. They provide lookup by either
451558Srgrimes * name or inode number. They also provide for creation, deletion,
461558Srgrimes * and renaming of entries. Because of the dynamic nature of pathnames,
471558Srgrimes * names should not be saved, but always constructed just before they
481558Srgrimes * are needed, by calling "myname".
491558Srgrimes */
501558Srgrimes
511558Srgrimes#include <sys/param.h>
521558Srgrimes#include <sys/stat.h>
531558Srgrimes
541558Srgrimes#include <ufs/ufs/dinode.h>
551558Srgrimes
561558Srgrimes#include <errno.h>
571558Srgrimes#include <fcntl.h>
581558Srgrimes#include <stdio.h>
591558Srgrimes#include <stdlib.h>
601558Srgrimes#include <string.h>
611558Srgrimes#include <unistd.h>
621558Srgrimes
631558Srgrimes#include "restore.h"
641558Srgrimes#include "extern.h"
651558Srgrimes
661558Srgrimes/*
671558Srgrimes * The following variables define the inode symbol table.
681558Srgrimes * The primary hash table is dynamically allocated based on
69102231Strhodes * the number of inodes in the file system (maxino), scaled by
701558Srgrimes * HASHFACTOR. The variable "entry" points to the hash table;
711558Srgrimes * the variable "entrytblsize" indicates its size (in entries).
721558Srgrimes */
731558Srgrimes#define HASHFACTOR 5
741558Srgrimesstatic struct entry **entry;
751558Srgrimesstatic long entrytblsize;
761558Srgrimes
7792837Simpstatic void		 addino(ino_t, struct entry *);
7892837Simpstatic struct entry	*lookupparent(char *);
7992837Simpstatic void		 removeentry(struct entry *);
801558Srgrimes
811558Srgrimes/*
821558Srgrimes * Look up an entry by inode number
831558Srgrimes */
841558Srgrimesstruct entry *
8592837Simplookupino(ino_t inum)
861558Srgrimes{
8792806Sobrien	struct entry *ep;
881558Srgrimes
8923685Speter	if (inum < WINO || inum >= maxino)
901558Srgrimes		return (NULL);
911558Srgrimes	for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next)
921558Srgrimes		if (ep->e_ino == inum)
931558Srgrimes			return (ep);
941558Srgrimes	return (NULL);
951558Srgrimes}
961558Srgrimes
971558Srgrimes/*
981558Srgrimes * Add an entry into the entry table
991558Srgrimes */
1001558Srgrimesstatic void
10192837Simpaddino(ino_t inum, struct entry *np)
1021558Srgrimes{
1031558Srgrimes	struct entry **epp;
1041558Srgrimes
10523685Speter	if (inum < WINO || inum >= maxino)
1061558Srgrimes		panic("addino: out of range %d\n", inum);
1071558Srgrimes	epp = &entry[inum % entrytblsize];
1081558Srgrimes	np->e_ino = inum;
1091558Srgrimes	np->e_next = *epp;
1101558Srgrimes	*epp = np;
1111558Srgrimes	if (dflag)
1121558Srgrimes		for (np = np->e_next; np != NULL; np = np->e_next)
1131558Srgrimes			if (np->e_ino == inum)
1141558Srgrimes				badentry(np, "duplicate inum");
1151558Srgrimes}
1161558Srgrimes
1171558Srgrimes/*
1181558Srgrimes * Delete an entry from the entry table
1191558Srgrimes */
1201558Srgrimesvoid
12192837Simpdeleteino(ino_t inum)
1221558Srgrimes{
12392806Sobrien	struct entry *next;
1241558Srgrimes	struct entry **prev;
1251558Srgrimes
12623685Speter	if (inum < WINO || inum >= maxino)
1271558Srgrimes		panic("deleteino: out of range %d\n", inum);
1281558Srgrimes	prev = &entry[inum % entrytblsize];
1291558Srgrimes	for (next = *prev; next != NULL; next = next->e_next) {
1301558Srgrimes		if (next->e_ino == inum) {
1311558Srgrimes			next->e_ino = 0;
1321558Srgrimes			*prev = next->e_next;
1331558Srgrimes			return;
1341558Srgrimes		}
1351558Srgrimes		prev = &next->e_next;
1361558Srgrimes	}
1371558Srgrimes	panic("deleteino: %d not found\n", inum);
1381558Srgrimes}
1391558Srgrimes
1401558Srgrimes/*
1411558Srgrimes * Look up an entry by name
1421558Srgrimes */
1431558Srgrimesstruct entry *
14492837Simplookupname(char *name)
1451558Srgrimes{
14692806Sobrien	struct entry *ep;
14792806Sobrien	char *np, *cp;
1481558Srgrimes	char buf[MAXPATHLEN];
1491558Srgrimes
1501558Srgrimes	cp = name;
1511558Srgrimes	for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) {
15222482Seivind		for (np = buf; *cp != '/' && *cp != '\0' &&
15322482Seivind				np < &buf[sizeof(buf)]; )
1541558Srgrimes			*np++ = *cp++;
15522482Seivind		if (np == &buf[sizeof(buf)])
15622482Seivind			break;
1571558Srgrimes		*np = '\0';
1581558Srgrimes		for ( ; ep != NULL; ep = ep->e_sibling)
1591558Srgrimes			if (strcmp(ep->e_name, buf) == 0)
1601558Srgrimes				break;
1611558Srgrimes		if (ep == NULL)
1621558Srgrimes			break;
1631558Srgrimes		if (*cp++ == '\0')
1641558Srgrimes			return (ep);
1651558Srgrimes	}
1661558Srgrimes	return (NULL);
1671558Srgrimes}
1681558Srgrimes
1691558Srgrimes/*
1701558Srgrimes * Look up the parent of a pathname
1711558Srgrimes */
1721558Srgrimesstatic struct entry *
17392837Simplookupparent(char *name)
1741558Srgrimes{
1751558Srgrimes	struct entry *ep;
1761558Srgrimes	char *tailindex;
1771558Srgrimes
17823685Speter	tailindex = strrchr(name, '/');
1791558Srgrimes	if (tailindex == NULL)
1801558Srgrimes		return (NULL);
1811558Srgrimes	*tailindex = '\0';
1821558Srgrimes	ep = lookupname(name);
1831558Srgrimes	*tailindex = '/';
1841558Srgrimes	if (ep == NULL)
1851558Srgrimes		return (NULL);
1861558Srgrimes	if (ep->e_type != NODE)
1871558Srgrimes		panic("%s is not a directory\n", name);
1881558Srgrimes	return (ep);
1891558Srgrimes}
1901558Srgrimes
1911558Srgrimes/*
1921558Srgrimes * Determine the current pathname of a node or leaf
1931558Srgrimes */
1941558Srgrimeschar *
19592837Simpmyname(struct entry *ep)
1961558Srgrimes{
19792806Sobrien	char *cp;
1981558Srgrimes	static char namebuf[MAXPATHLEN];
1991558Srgrimes
2001558Srgrimes	for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
2011558Srgrimes		cp -= ep->e_namlen;
20223685Speter		memmove(cp, ep->e_name, (long)ep->e_namlen);
2031558Srgrimes		if (ep == lookupino(ROOTINO))
2041558Srgrimes			return (cp);
2051558Srgrimes		*(--cp) = '/';
2061558Srgrimes		ep = ep->e_parent;
2071558Srgrimes	}
2081558Srgrimes	panic("%s: pathname too long\n", cp);
2091558Srgrimes	return(cp);
2101558Srgrimes}
2111558Srgrimes
2121558Srgrimes/*
21337906Scharnier * Unused symbol table entries are linked together on a free list
2141558Srgrimes * headed by the following pointer.
2151558Srgrimes */
2161558Srgrimesstatic struct entry *freelist = NULL;
2171558Srgrimes
2181558Srgrimes/*
2191558Srgrimes * add an entry to the symbol table
2201558Srgrimes */
2211558Srgrimesstruct entry *
22292837Simpaddentry(char *name, ino_t inum, int type)
2231558Srgrimes{
22492806Sobrien	struct entry *np, *ep;
2251558Srgrimes
2261558Srgrimes	if (freelist != NULL) {
2271558Srgrimes		np = freelist;
2281558Srgrimes		freelist = np->e_next;
22923685Speter		memset(np, 0, (long)sizeof(struct entry));
2301558Srgrimes	} else {
2311558Srgrimes		np = (struct entry *)calloc(1, sizeof(struct entry));
2321558Srgrimes		if (np == NULL)
2331558Srgrimes			panic("no memory to extend symbol table\n");
2341558Srgrimes	}
2351558Srgrimes	np->e_type = type & ~LINK;
2361558Srgrimes	ep = lookupparent(name);
2371558Srgrimes	if (ep == NULL) {
2381558Srgrimes		if (inum != ROOTINO || lookupino(ROOTINO) != NULL)
2391558Srgrimes			panic("bad name to addentry %s\n", name);
2401558Srgrimes		np->e_name = savename(name);
2411558Srgrimes		np->e_namlen = strlen(name);
2421558Srgrimes		np->e_parent = np;
2431558Srgrimes		addino(ROOTINO, np);
2441558Srgrimes		return (np);
2451558Srgrimes	}
24623685Speter	np->e_name = savename(strrchr(name, '/') + 1);
2471558Srgrimes	np->e_namlen = strlen(np->e_name);
2481558Srgrimes	np->e_parent = ep;
2491558Srgrimes	np->e_sibling = ep->e_entries;
2501558Srgrimes	ep->e_entries = np;
2511558Srgrimes	if (type & LINK) {
2521558Srgrimes		ep = lookupino(inum);
2531558Srgrimes		if (ep == NULL)
25437906Scharnier			panic("link to non-existent name\n");
2551558Srgrimes		np->e_ino = inum;
2561558Srgrimes		np->e_links = ep->e_links;
2571558Srgrimes		ep->e_links = np;
2581558Srgrimes	} else if (inum != 0) {
2591558Srgrimes		if (lookupino(inum) != NULL)
2601558Srgrimes			panic("duplicate entry\n");
2611558Srgrimes		addino(inum, np);
2621558Srgrimes	}
2631558Srgrimes	return (np);
2641558Srgrimes}
2651558Srgrimes
2661558Srgrimes/*
2671558Srgrimes * delete an entry from the symbol table
2681558Srgrimes */
2691558Srgrimesvoid
27092837Simpfreeentry(struct entry *ep)
2711558Srgrimes{
27292806Sobrien	struct entry *np;
2731558Srgrimes	ino_t inum;
2741558Srgrimes
2751558Srgrimes	if (ep->e_flags != REMOVED)
2761558Srgrimes		badentry(ep, "not marked REMOVED");
2771558Srgrimes	if (ep->e_type == NODE) {
2781558Srgrimes		if (ep->e_links != NULL)
2791558Srgrimes			badentry(ep, "freeing referenced directory");
2801558Srgrimes		if (ep->e_entries != NULL)
2811558Srgrimes			badentry(ep, "freeing non-empty directory");
2821558Srgrimes	}
2831558Srgrimes	if (ep->e_ino != 0) {
2841558Srgrimes		np = lookupino(ep->e_ino);
2851558Srgrimes		if (np == NULL)
2861558Srgrimes			badentry(ep, "lookupino failed");
2871558Srgrimes		if (np == ep) {
2881558Srgrimes			inum = ep->e_ino;
2891558Srgrimes			deleteino(inum);
2901558Srgrimes			if (ep->e_links != NULL)
2911558Srgrimes				addino(inum, ep->e_links);
2921558Srgrimes		} else {
2931558Srgrimes			for (; np != NULL; np = np->e_links) {
2941558Srgrimes				if (np->e_links == ep) {
2951558Srgrimes					np->e_links = ep->e_links;
2961558Srgrimes					break;
2971558Srgrimes				}
2981558Srgrimes			}
2991558Srgrimes			if (np == NULL)
3001558Srgrimes				badentry(ep, "link not found");
3011558Srgrimes		}
3021558Srgrimes	}
3031558Srgrimes	removeentry(ep);
3041558Srgrimes	freename(ep->e_name);
3051558Srgrimes	ep->e_next = freelist;
3061558Srgrimes	freelist = ep;
3071558Srgrimes}
3081558Srgrimes
3091558Srgrimes/*
3101558Srgrimes * Relocate an entry in the tree structure
3111558Srgrimes */
3121558Srgrimesvoid
31392837Simpmoveentry(struct entry *ep, char *newname)
3141558Srgrimes{
3151558Srgrimes	struct entry *np;
3161558Srgrimes	char *cp;
3171558Srgrimes
3181558Srgrimes	np = lookupparent(newname);
3191558Srgrimes	if (np == NULL)
3201558Srgrimes		badentry(ep, "cannot move ROOT");
3211558Srgrimes	if (np != ep->e_parent) {
3221558Srgrimes		removeentry(ep);
3231558Srgrimes		ep->e_parent = np;
3241558Srgrimes		ep->e_sibling = np->e_entries;
3251558Srgrimes		np->e_entries = ep;
3261558Srgrimes	}
32723685Speter	cp = strrchr(newname, '/') + 1;
3281558Srgrimes	freename(ep->e_name);
3291558Srgrimes	ep->e_name = savename(cp);
3301558Srgrimes	ep->e_namlen = strlen(cp);
3311558Srgrimes	if (strcmp(gentempname(ep), ep->e_name) == 0)
3321558Srgrimes		ep->e_flags |= TMPNAME;
3331558Srgrimes	else
3341558Srgrimes		ep->e_flags &= ~TMPNAME;
3351558Srgrimes}
3361558Srgrimes
3371558Srgrimes/*
3381558Srgrimes * Remove an entry in the tree structure
3391558Srgrimes */
3401558Srgrimesstatic void
34192837Simpremoveentry(struct entry *ep)
3421558Srgrimes{
34392806Sobrien	struct entry *np;
3441558Srgrimes
3451558Srgrimes	np = ep->e_parent;
3461558Srgrimes	if (np->e_entries == ep) {
3471558Srgrimes		np->e_entries = ep->e_sibling;
3481558Srgrimes	} else {
3491558Srgrimes		for (np = np->e_entries; np != NULL; np = np->e_sibling) {
3501558Srgrimes			if (np->e_sibling == ep) {
3511558Srgrimes				np->e_sibling = ep->e_sibling;
3521558Srgrimes				break;
3531558Srgrimes			}
3541558Srgrimes		}
3551558Srgrimes		if (np == NULL)
3561558Srgrimes			badentry(ep, "cannot find entry in parent list");
3571558Srgrimes	}
3581558Srgrimes}
3591558Srgrimes
3601558Srgrimes/*
3611558Srgrimes * Table of unused string entries, sorted by length.
3628871Srgrimes *
3631558Srgrimes * Entries are allocated in STRTBLINCR sized pieces so that names
3641558Srgrimes * of similar lengths can use the same entry. The value of STRTBLINCR
3651558Srgrimes * is chosen so that every entry has at least enough space to hold
3661558Srgrimes * a "struct strtbl" header. Thus every entry can be linked onto an
36737906Scharnier * appropriate free list.
3681558Srgrimes *
3691558Srgrimes * NB. The macro "allocsize" below assumes that "struct strhdr"
3701558Srgrimes *     has a size that is a power of two.
3711558Srgrimes */
3721558Srgrimesstruct strhdr {
3731558Srgrimes	struct strhdr *next;
3741558Srgrimes};
3751558Srgrimes
3761558Srgrimes#define STRTBLINCR	(sizeof(struct strhdr))
3771558Srgrimes#define allocsize(size)	(((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
3781558Srgrimes
3791558Srgrimesstatic struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR];
3801558Srgrimes
3811558Srgrimes/*
3821558Srgrimes * Allocate space for a name. It first looks to see if it already
3831558Srgrimes * has an appropriate sized entry, and if not allocates a new one.
3841558Srgrimes */
3851558Srgrimeschar *
38692837Simpsavename(char *name)
3871558Srgrimes{
3881558Srgrimes	struct strhdr *np;
3891558Srgrimes	long len;
3901558Srgrimes	char *cp;
3911558Srgrimes
3921558Srgrimes	if (name == NULL)
3931558Srgrimes		panic("bad name\n");
3941558Srgrimes	len = strlen(name);
3951558Srgrimes	np = strtblhdr[len / STRTBLINCR].next;
3961558Srgrimes	if (np != NULL) {
3971558Srgrimes		strtblhdr[len / STRTBLINCR].next = np->next;
3981558Srgrimes		cp = (char *)np;
3991558Srgrimes	} else {
4001558Srgrimes		cp = malloc((unsigned)allocsize(len));
4011558Srgrimes		if (cp == NULL)
4021558Srgrimes			panic("no space for string table\n");
4031558Srgrimes	}
4041558Srgrimes	(void) strcpy(cp, name);
4051558Srgrimes	return (cp);
4061558Srgrimes}
4071558Srgrimes
4081558Srgrimes/*
4091558Srgrimes * Free space for a name. The resulting entry is linked onto the
4101558Srgrimes * appropriate free list.
4111558Srgrimes */
4121558Srgrimesvoid
41392837Simpfreename(char *name)
4141558Srgrimes{
4151558Srgrimes	struct strhdr *tp, *np;
4168871Srgrimes
4171558Srgrimes	tp = &strtblhdr[strlen(name) / STRTBLINCR];
4181558Srgrimes	np = (struct strhdr *)name;
4191558Srgrimes	np->next = tp->next;
4201558Srgrimes	tp->next = np;
4211558Srgrimes}
4221558Srgrimes
4231558Srgrimes/*
4241558Srgrimes * Useful quantities placed at the end of a dumped symbol table.
4251558Srgrimes */
4261558Srgrimesstruct symtableheader {
42740668Sdima	int32_t	volno;
42840668Sdima	int32_t	stringsize;
42940668Sdima	int32_t	entrytblsize;
4301558Srgrimes	time_t	dumptime;
4311558Srgrimes	time_t	dumpdate;
4321558Srgrimes	ino_t	maxino;
43340668Sdima	int32_t	ntrec;
4341558Srgrimes};
4351558Srgrimes
4361558Srgrimes/*
4371558Srgrimes * dump a snapshot of the symbol table
4381558Srgrimes */
4391558Srgrimesvoid
44092837Simpdumpsymtable(char *filename, long checkpt)
4411558Srgrimes{
44292806Sobrien	struct entry *ep, *tep;
44392806Sobrien	ino_t i;
4441558Srgrimes	struct entry temp, *tentry;
4451558Srgrimes	long mynum = 1, stroff = 0;
4461558Srgrimes	FILE *fd;
4471558Srgrimes	struct symtableheader hdr;
4481558Srgrimes
4491558Srgrimes	vprintf(stdout, "Check pointing the restore\n");
4501558Srgrimes	if (Nflag)
4511558Srgrimes		return;
4521558Srgrimes	if ((fd = fopen(filename, "w")) == NULL) {
4531558Srgrimes		fprintf(stderr, "fopen: %s\n", strerror(errno));
4541558Srgrimes		panic("cannot create save file %s for symbol table\n",
4551558Srgrimes			filename);
45685746Stobez		done(1);
4571558Srgrimes	}
4581558Srgrimes	clearerr(fd);
4591558Srgrimes	/*
46037906Scharnier	 * Assign indices to each entry
4611558Srgrimes	 * Write out the string entries
4621558Srgrimes	 */
46323685Speter	for (i = WINO; i <= maxino; i++) {
4641558Srgrimes		for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
4651558Srgrimes			ep->e_index = mynum++;
4661558Srgrimes			(void) fwrite(ep->e_name, sizeof(char),
4671558Srgrimes			       (int)allocsize(ep->e_namlen), fd);
4681558Srgrimes		}
4691558Srgrimes	}
4701558Srgrimes	/*
4711558Srgrimes	 * Convert pointers to indexes, and output
4721558Srgrimes	 */
4731558Srgrimes	tep = &temp;
4741558Srgrimes	stroff = 0;
47523685Speter	for (i = WINO; i <= maxino; i++) {
4761558Srgrimes		for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
47723685Speter			memmove(tep, ep, (long)sizeof(struct entry));
4781558Srgrimes			tep->e_name = (char *)stroff;
4791558Srgrimes			stroff += allocsize(ep->e_namlen);
4801558Srgrimes			tep->e_parent = (struct entry *)ep->e_parent->e_index;
4811558Srgrimes			if (ep->e_links != NULL)
4821558Srgrimes				tep->e_links =
4831558Srgrimes					(struct entry *)ep->e_links->e_index;
4841558Srgrimes			if (ep->e_sibling != NULL)
4851558Srgrimes				tep->e_sibling =
4861558Srgrimes					(struct entry *)ep->e_sibling->e_index;
4871558Srgrimes			if (ep->e_entries != NULL)
4881558Srgrimes				tep->e_entries =
4891558Srgrimes					(struct entry *)ep->e_entries->e_index;
4901558Srgrimes			if (ep->e_next != NULL)
4911558Srgrimes				tep->e_next =
4921558Srgrimes					(struct entry *)ep->e_next->e_index;
4931558Srgrimes			(void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
4941558Srgrimes		}
4951558Srgrimes	}
4961558Srgrimes	/*
4971558Srgrimes	 * Convert entry pointers to indexes, and output
4981558Srgrimes	 */
4991558Srgrimes	for (i = 0; i < entrytblsize; i++) {
5001558Srgrimes		if (entry[i] == NULL)
5011558Srgrimes			tentry = NULL;
5021558Srgrimes		else
5031558Srgrimes			tentry = (struct entry *)entry[i]->e_index;
5041558Srgrimes		(void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
5051558Srgrimes	}
5061558Srgrimes	hdr.volno = checkpt;
5071558Srgrimes	hdr.maxino = maxino;
5081558Srgrimes	hdr.entrytblsize = entrytblsize;
5091558Srgrimes	hdr.stringsize = stroff;
5101558Srgrimes	hdr.dumptime = dumptime;
5111558Srgrimes	hdr.dumpdate = dumpdate;
5121558Srgrimes	hdr.ntrec = ntrec;
5131558Srgrimes	(void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
5141558Srgrimes	if (ferror(fd)) {
5151558Srgrimes		fprintf(stderr, "fwrite: %s\n", strerror(errno));
5161558Srgrimes		panic("output error to file %s writing symbol table\n",
5171558Srgrimes			filename);
5181558Srgrimes	}
5191558Srgrimes	(void) fclose(fd);
5201558Srgrimes}
5211558Srgrimes
5221558Srgrimes/*
5231558Srgrimes * Initialize a symbol table from a file
5241558Srgrimes */
5251558Srgrimesvoid
52692837Simpinitsymtable(char *filename)
5271558Srgrimes{
5281558Srgrimes	char *base;
5291558Srgrimes	long tblsize;
53092806Sobrien	struct entry *ep;
5311558Srgrimes	struct entry *baseep, *lep;
5321558Srgrimes	struct symtableheader hdr;
5331558Srgrimes	struct stat stbuf;
53492806Sobrien	long i;
5351558Srgrimes	int fd;
5361558Srgrimes
5371558Srgrimes	vprintf(stdout, "Initialize symbol table.\n");
5381558Srgrimes	if (filename == NULL) {
5391558Srgrimes		entrytblsize = maxino / HASHFACTOR;
5401558Srgrimes		entry = (struct entry **)
5411558Srgrimes			calloc((unsigned)entrytblsize, sizeof(struct entry *));
5421558Srgrimes		if (entry == (struct entry **)NULL)
5431558Srgrimes			panic("no memory for entry table\n");
5441558Srgrimes		ep = addentry(".", ROOTINO, NODE);
5451558Srgrimes		ep->e_flags |= NEW;
5461558Srgrimes		return;
5471558Srgrimes	}
5481558Srgrimes	if ((fd = open(filename, O_RDONLY, 0)) < 0) {
5491558Srgrimes		fprintf(stderr, "open: %s\n", strerror(errno));
5501558Srgrimes		panic("cannot open symbol table file %s\n", filename);
5511558Srgrimes	}
5521558Srgrimes	if (fstat(fd, &stbuf) < 0) {
5531558Srgrimes		fprintf(stderr, "stat: %s\n", strerror(errno));
5541558Srgrimes		panic("cannot stat symbol table file %s\n", filename);
5551558Srgrimes	}
5561558Srgrimes	tblsize = stbuf.st_size - sizeof(struct symtableheader);
5571558Srgrimes	base = calloc(sizeof(char), (unsigned)tblsize);
5581558Srgrimes	if (base == NULL)
5591558Srgrimes		panic("cannot allocate space for symbol table\n");
5601558Srgrimes	if (read(fd, base, (int)tblsize) < 0 ||
5611558Srgrimes	    read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
5621558Srgrimes		fprintf(stderr, "read: %s\n", strerror(errno));
5631558Srgrimes		panic("cannot read symbol table file %s\n", filename);
5641558Srgrimes	}
5651558Srgrimes	switch (command) {
5661558Srgrimes	case 'r':
5671558Srgrimes		/*
5681558Srgrimes		 * For normal continuation, insure that we are using
5691558Srgrimes		 * the next incremental tape
5701558Srgrimes		 */
5711558Srgrimes		if (hdr.dumpdate != dumptime) {
5721558Srgrimes			if (hdr.dumpdate < dumptime)
5731558Srgrimes				fprintf(stderr, "Incremental tape too low\n");
5741558Srgrimes			else
5751558Srgrimes				fprintf(stderr, "Incremental tape too high\n");
5761558Srgrimes			done(1);
5771558Srgrimes		}
5781558Srgrimes		break;
5791558Srgrimes	case 'R':
5801558Srgrimes		/*
5811558Srgrimes		 * For restart, insure that we are using the same tape
5821558Srgrimes		 */
5831558Srgrimes		curfile.action = SKIP;
5841558Srgrimes		dumptime = hdr.dumptime;
5851558Srgrimes		dumpdate = hdr.dumpdate;
5861558Srgrimes		if (!bflag)
5871558Srgrimes			newtapebuf(hdr.ntrec);
5881558Srgrimes		getvol(hdr.volno);
5891558Srgrimes		break;
5901558Srgrimes	default:
5911558Srgrimes		panic("initsymtable called from command %c\n", command);
5921558Srgrimes		break;
5931558Srgrimes	}
5941558Srgrimes	maxino = hdr.maxino;
5951558Srgrimes	entrytblsize = hdr.entrytblsize;
5961558Srgrimes	entry = (struct entry **)
5971558Srgrimes		(base + tblsize - (entrytblsize * sizeof(struct entry *)));
5981558Srgrimes	baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
5991558Srgrimes	lep = (struct entry *)entry;
6001558Srgrimes	for (i = 0; i < entrytblsize; i++) {
6011558Srgrimes		if (entry[i] == NULL)
6021558Srgrimes			continue;
6031558Srgrimes		entry[i] = &baseep[(long)entry[i]];
6041558Srgrimes	}
6051558Srgrimes	for (ep = &baseep[1]; ep < lep; ep++) {
6061558Srgrimes		ep->e_name = base + (long)ep->e_name;
6071558Srgrimes		ep->e_parent = &baseep[(long)ep->e_parent];
6081558Srgrimes		if (ep->e_sibling != NULL)
6091558Srgrimes			ep->e_sibling = &baseep[(long)ep->e_sibling];
6101558Srgrimes		if (ep->e_links != NULL)
6111558Srgrimes			ep->e_links = &baseep[(long)ep->e_links];
6121558Srgrimes		if (ep->e_entries != NULL)
6131558Srgrimes			ep->e_entries = &baseep[(long)ep->e_entries];
6141558Srgrimes		if (ep->e_next != NULL)
6151558Srgrimes			ep->e_next = &baseep[(long)ep->e_next];
6161558Srgrimes	}
6171558Srgrimes}
618