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 * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
301558Srgrimes#ifndef lint
3137906Scharnier#if 0
3223685Speterstatic char sccsid[] = "@(#)symtab.c	8.3 (Berkeley) 4/28/95";
3337906Scharnier#endif
3437906Scharnierstatic const char rcsid[] =
3550476Speter  "$FreeBSD$";
361558Srgrimes#endif /* not lint */
371558Srgrimes
381558Srgrimes/*
391558Srgrimes * These routines maintain the symbol table which tracks the state
40102231Strhodes * of the file system being restored. They provide lookup by either
411558Srgrimes * name or inode number. They also provide for creation, deletion,
421558Srgrimes * and renaming of entries. Because of the dynamic nature of pathnames,
431558Srgrimes * names should not be saved, but always constructed just before they
441558Srgrimes * are needed, by calling "myname".
451558Srgrimes */
461558Srgrimes
471558Srgrimes#include <sys/param.h>
481558Srgrimes#include <sys/stat.h>
491558Srgrimes
501558Srgrimes#include <ufs/ufs/dinode.h>
511558Srgrimes
521558Srgrimes#include <errno.h>
531558Srgrimes#include <fcntl.h>
54103949Smike#include <limits.h>
55241013Smdf#include <stdint.h>
561558Srgrimes#include <stdio.h>
571558Srgrimes#include <stdlib.h>
581558Srgrimes#include <string.h>
591558Srgrimes#include <unistd.h>
601558Srgrimes
611558Srgrimes#include "restore.h"
621558Srgrimes#include "extern.h"
631558Srgrimes
641558Srgrimes/*
651558Srgrimes * The following variables define the inode symbol table.
661558Srgrimes * The primary hash table is dynamically allocated based on
67102231Strhodes * the number of inodes in the file system (maxino), scaled by
681558Srgrimes * HASHFACTOR. The variable "entry" points to the hash table;
691558Srgrimes * the variable "entrytblsize" indicates its size (in entries).
701558Srgrimes */
711558Srgrimes#define HASHFACTOR 5
721558Srgrimesstatic struct entry **entry;
731558Srgrimesstatic long entrytblsize;
741558Srgrimes
7592837Simpstatic void		 addino(ino_t, struct entry *);
7692837Simpstatic struct entry	*lookupparent(char *);
7792837Simpstatic void		 removeentry(struct entry *);
781558Srgrimes
791558Srgrimes/*
801558Srgrimes * Look up an entry by inode number
811558Srgrimes */
821558Srgrimesstruct entry *
8392837Simplookupino(ino_t inum)
841558Srgrimes{
8592806Sobrien	struct entry *ep;
861558Srgrimes
8723685Speter	if (inum < WINO || inum >= maxino)
881558Srgrimes		return (NULL);
891558Srgrimes	for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next)
901558Srgrimes		if (ep->e_ino == inum)
911558Srgrimes			return (ep);
921558Srgrimes	return (NULL);
931558Srgrimes}
941558Srgrimes
951558Srgrimes/*
961558Srgrimes * Add an entry into the entry table
971558Srgrimes */
981558Srgrimesstatic void
9992837Simpaddino(ino_t inum, struct entry *np)
1001558Srgrimes{
1011558Srgrimes	struct entry **epp;
1021558Srgrimes
10323685Speter	if (inum < WINO || inum >= maxino)
104241013Smdf		panic("addino: out of range %ju\n", (uintmax_t)inum);
1051558Srgrimes	epp = &entry[inum % entrytblsize];
1061558Srgrimes	np->e_ino = inum;
1071558Srgrimes	np->e_next = *epp;
1081558Srgrimes	*epp = np;
1091558Srgrimes	if (dflag)
1101558Srgrimes		for (np = np->e_next; np != NULL; np = np->e_next)
1111558Srgrimes			if (np->e_ino == inum)
1121558Srgrimes				badentry(np, "duplicate inum");
1131558Srgrimes}
1141558Srgrimes
1151558Srgrimes/*
1161558Srgrimes * Delete an entry from the entry table
1171558Srgrimes */
1181558Srgrimesvoid
11992837Simpdeleteino(ino_t inum)
1201558Srgrimes{
12192806Sobrien	struct entry *next;
1221558Srgrimes	struct entry **prev;
1231558Srgrimes
12423685Speter	if (inum < WINO || inum >= maxino)
125241013Smdf		panic("deleteino: out of range %ju\n", (uintmax_t)inum);
1261558Srgrimes	prev = &entry[inum % entrytblsize];
1271558Srgrimes	for (next = *prev; next != NULL; next = next->e_next) {
1281558Srgrimes		if (next->e_ino == inum) {
1291558Srgrimes			next->e_ino = 0;
1301558Srgrimes			*prev = next->e_next;
1311558Srgrimes			return;
1321558Srgrimes		}
1331558Srgrimes		prev = &next->e_next;
1341558Srgrimes	}
135241013Smdf	panic("deleteino: %ju not found\n", (uintmax_t)inum);
1361558Srgrimes}
1371558Srgrimes
1381558Srgrimes/*
1391558Srgrimes * Look up an entry by name
1401558Srgrimes */
1411558Srgrimesstruct entry *
14292837Simplookupname(char *name)
1431558Srgrimes{
14492806Sobrien	struct entry *ep;
14592806Sobrien	char *np, *cp;
1461558Srgrimes	char buf[MAXPATHLEN];
1471558Srgrimes
1481558Srgrimes	cp = name;
1491558Srgrimes	for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) {
15022482Seivind		for (np = buf; *cp != '/' && *cp != '\0' &&
15122482Seivind				np < &buf[sizeof(buf)]; )
1521558Srgrimes			*np++ = *cp++;
15322482Seivind		if (np == &buf[sizeof(buf)])
15422482Seivind			break;
1551558Srgrimes		*np = '\0';
1561558Srgrimes		for ( ; ep != NULL; ep = ep->e_sibling)
1571558Srgrimes			if (strcmp(ep->e_name, buf) == 0)
1581558Srgrimes				break;
1591558Srgrimes		if (ep == NULL)
1601558Srgrimes			break;
1611558Srgrimes		if (*cp++ == '\0')
1621558Srgrimes			return (ep);
1631558Srgrimes	}
1641558Srgrimes	return (NULL);
1651558Srgrimes}
1661558Srgrimes
1671558Srgrimes/*
1681558Srgrimes * Look up the parent of a pathname
1691558Srgrimes */
1701558Srgrimesstatic struct entry *
17192837Simplookupparent(char *name)
1721558Srgrimes{
1731558Srgrimes	struct entry *ep;
1741558Srgrimes	char *tailindex;
1751558Srgrimes
17623685Speter	tailindex = strrchr(name, '/');
1771558Srgrimes	if (tailindex == NULL)
1781558Srgrimes		return (NULL);
1791558Srgrimes	*tailindex = '\0';
1801558Srgrimes	ep = lookupname(name);
1811558Srgrimes	*tailindex = '/';
1821558Srgrimes	if (ep == NULL)
1831558Srgrimes		return (NULL);
1841558Srgrimes	if (ep->e_type != NODE)
1851558Srgrimes		panic("%s is not a directory\n", name);
1861558Srgrimes	return (ep);
1871558Srgrimes}
1881558Srgrimes
1891558Srgrimes/*
1901558Srgrimes * Determine the current pathname of a node or leaf
1911558Srgrimes */
1921558Srgrimeschar *
19392837Simpmyname(struct entry *ep)
1941558Srgrimes{
19592806Sobrien	char *cp;
1961558Srgrimes	static char namebuf[MAXPATHLEN];
1971558Srgrimes
1981558Srgrimes	for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
1991558Srgrimes		cp -= ep->e_namlen;
20023685Speter		memmove(cp, ep->e_name, (long)ep->e_namlen);
2011558Srgrimes		if (ep == lookupino(ROOTINO))
2021558Srgrimes			return (cp);
2031558Srgrimes		*(--cp) = '/';
2041558Srgrimes		ep = ep->e_parent;
2051558Srgrimes	}
2061558Srgrimes	panic("%s: pathname too long\n", cp);
2071558Srgrimes	return(cp);
2081558Srgrimes}
2091558Srgrimes
2101558Srgrimes/*
21137906Scharnier * Unused symbol table entries are linked together on a free list
2121558Srgrimes * headed by the following pointer.
2131558Srgrimes */
2141558Srgrimesstatic struct entry *freelist = NULL;
2151558Srgrimes
2161558Srgrimes/*
2171558Srgrimes * add an entry to the symbol table
2181558Srgrimes */
2191558Srgrimesstruct entry *
22092837Simpaddentry(char *name, ino_t inum, int type)
2211558Srgrimes{
22292806Sobrien	struct entry *np, *ep;
2231558Srgrimes
2241558Srgrimes	if (freelist != NULL) {
2251558Srgrimes		np = freelist;
2261558Srgrimes		freelist = np->e_next;
22723685Speter		memset(np, 0, (long)sizeof(struct entry));
2281558Srgrimes	} else {
2291558Srgrimes		np = (struct entry *)calloc(1, sizeof(struct entry));
2301558Srgrimes		if (np == NULL)
2311558Srgrimes			panic("no memory to extend symbol table\n");
2321558Srgrimes	}
2331558Srgrimes	np->e_type = type & ~LINK;
2341558Srgrimes	ep = lookupparent(name);
2351558Srgrimes	if (ep == NULL) {
2361558Srgrimes		if (inum != ROOTINO || lookupino(ROOTINO) != NULL)
2371558Srgrimes			panic("bad name to addentry %s\n", name);
2381558Srgrimes		np->e_name = savename(name);
2391558Srgrimes		np->e_namlen = strlen(name);
2401558Srgrimes		np->e_parent = np;
2411558Srgrimes		addino(ROOTINO, np);
2421558Srgrimes		return (np);
2431558Srgrimes	}
24423685Speter	np->e_name = savename(strrchr(name, '/') + 1);
2451558Srgrimes	np->e_namlen = strlen(np->e_name);
2461558Srgrimes	np->e_parent = ep;
2471558Srgrimes	np->e_sibling = ep->e_entries;
2481558Srgrimes	ep->e_entries = np;
2491558Srgrimes	if (type & LINK) {
2501558Srgrimes		ep = lookupino(inum);
2511558Srgrimes		if (ep == NULL)
25237906Scharnier			panic("link to non-existent name\n");
2531558Srgrimes		np->e_ino = inum;
2541558Srgrimes		np->e_links = ep->e_links;
2551558Srgrimes		ep->e_links = np;
2561558Srgrimes	} else if (inum != 0) {
2571558Srgrimes		if (lookupino(inum) != NULL)
2581558Srgrimes			panic("duplicate entry\n");
2591558Srgrimes		addino(inum, np);
2601558Srgrimes	}
2611558Srgrimes	return (np);
2621558Srgrimes}
2631558Srgrimes
2641558Srgrimes/*
2651558Srgrimes * delete an entry from the symbol table
2661558Srgrimes */
2671558Srgrimesvoid
26892837Simpfreeentry(struct entry *ep)
2691558Srgrimes{
27092806Sobrien	struct entry *np;
2711558Srgrimes	ino_t inum;
2721558Srgrimes
2731558Srgrimes	if (ep->e_flags != REMOVED)
2741558Srgrimes		badentry(ep, "not marked REMOVED");
2751558Srgrimes	if (ep->e_type == NODE) {
2761558Srgrimes		if (ep->e_links != NULL)
2771558Srgrimes			badentry(ep, "freeing referenced directory");
2781558Srgrimes		if (ep->e_entries != NULL)
2791558Srgrimes			badentry(ep, "freeing non-empty directory");
2801558Srgrimes	}
2811558Srgrimes	if (ep->e_ino != 0) {
2821558Srgrimes		np = lookupino(ep->e_ino);
2831558Srgrimes		if (np == NULL)
2841558Srgrimes			badentry(ep, "lookupino failed");
2851558Srgrimes		if (np == ep) {
2861558Srgrimes			inum = ep->e_ino;
2871558Srgrimes			deleteino(inum);
2881558Srgrimes			if (ep->e_links != NULL)
2891558Srgrimes				addino(inum, ep->e_links);
2901558Srgrimes		} else {
2911558Srgrimes			for (; np != NULL; np = np->e_links) {
2921558Srgrimes				if (np->e_links == ep) {
2931558Srgrimes					np->e_links = ep->e_links;
2941558Srgrimes					break;
2951558Srgrimes				}
2961558Srgrimes			}
2971558Srgrimes			if (np == NULL)
2981558Srgrimes				badentry(ep, "link not found");
2991558Srgrimes		}
3001558Srgrimes	}
3011558Srgrimes	removeentry(ep);
3021558Srgrimes	freename(ep->e_name);
3031558Srgrimes	ep->e_next = freelist;
3041558Srgrimes	freelist = ep;
3051558Srgrimes}
3061558Srgrimes
3071558Srgrimes/*
3081558Srgrimes * Relocate an entry in the tree structure
3091558Srgrimes */
3101558Srgrimesvoid
31192837Simpmoveentry(struct entry *ep, char *newname)
3121558Srgrimes{
3131558Srgrimes	struct entry *np;
3141558Srgrimes	char *cp;
3151558Srgrimes
3161558Srgrimes	np = lookupparent(newname);
3171558Srgrimes	if (np == NULL)
3181558Srgrimes		badentry(ep, "cannot move ROOT");
3191558Srgrimes	if (np != ep->e_parent) {
3201558Srgrimes		removeentry(ep);
3211558Srgrimes		ep->e_parent = np;
3221558Srgrimes		ep->e_sibling = np->e_entries;
3231558Srgrimes		np->e_entries = ep;
3241558Srgrimes	}
32523685Speter	cp = strrchr(newname, '/') + 1;
3261558Srgrimes	freename(ep->e_name);
3271558Srgrimes	ep->e_name = savename(cp);
3281558Srgrimes	ep->e_namlen = strlen(cp);
3291558Srgrimes	if (strcmp(gentempname(ep), ep->e_name) == 0)
3301558Srgrimes		ep->e_flags |= TMPNAME;
3311558Srgrimes	else
3321558Srgrimes		ep->e_flags &= ~TMPNAME;
3331558Srgrimes}
3341558Srgrimes
3351558Srgrimes/*
3361558Srgrimes * Remove an entry in the tree structure
3371558Srgrimes */
3381558Srgrimesstatic void
33992837Simpremoveentry(struct entry *ep)
3401558Srgrimes{
34192806Sobrien	struct entry *np;
3421558Srgrimes
3431558Srgrimes	np = ep->e_parent;
3441558Srgrimes	if (np->e_entries == ep) {
3451558Srgrimes		np->e_entries = ep->e_sibling;
3461558Srgrimes	} else {
3471558Srgrimes		for (np = np->e_entries; np != NULL; np = np->e_sibling) {
3481558Srgrimes			if (np->e_sibling == ep) {
3491558Srgrimes				np->e_sibling = ep->e_sibling;
3501558Srgrimes				break;
3511558Srgrimes			}
3521558Srgrimes		}
3531558Srgrimes		if (np == NULL)
3541558Srgrimes			badentry(ep, "cannot find entry in parent list");
3551558Srgrimes	}
3561558Srgrimes}
3571558Srgrimes
3581558Srgrimes/*
3591558Srgrimes * Table of unused string entries, sorted by length.
3608871Srgrimes *
3611558Srgrimes * Entries are allocated in STRTBLINCR sized pieces so that names
3621558Srgrimes * of similar lengths can use the same entry. The value of STRTBLINCR
3631558Srgrimes * is chosen so that every entry has at least enough space to hold
3641558Srgrimes * a "struct strtbl" header. Thus every entry can be linked onto an
36537906Scharnier * appropriate free list.
3661558Srgrimes *
3671558Srgrimes * NB. The macro "allocsize" below assumes that "struct strhdr"
3681558Srgrimes *     has a size that is a power of two.
3691558Srgrimes */
3701558Srgrimesstruct strhdr {
3711558Srgrimes	struct strhdr *next;
3721558Srgrimes};
3731558Srgrimes
3741558Srgrimes#define STRTBLINCR	(sizeof(struct strhdr))
3751558Srgrimes#define allocsize(size)	(((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))
3761558Srgrimes
3771558Srgrimesstatic struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR];
3781558Srgrimes
3791558Srgrimes/*
3801558Srgrimes * Allocate space for a name. It first looks to see if it already
3811558Srgrimes * has an appropriate sized entry, and if not allocates a new one.
3821558Srgrimes */
3831558Srgrimeschar *
38492837Simpsavename(char *name)
3851558Srgrimes{
3861558Srgrimes	struct strhdr *np;
3871558Srgrimes	long len;
3881558Srgrimes	char *cp;
3891558Srgrimes
3901558Srgrimes	if (name == NULL)
3911558Srgrimes		panic("bad name\n");
3921558Srgrimes	len = strlen(name);
3931558Srgrimes	np = strtblhdr[len / STRTBLINCR].next;
3941558Srgrimes	if (np != NULL) {
3951558Srgrimes		strtblhdr[len / STRTBLINCR].next = np->next;
3961558Srgrimes		cp = (char *)np;
3971558Srgrimes	} else {
3981558Srgrimes		cp = malloc((unsigned)allocsize(len));
3991558Srgrimes		if (cp == NULL)
4001558Srgrimes			panic("no space for string table\n");
4011558Srgrimes	}
4021558Srgrimes	(void) strcpy(cp, name);
4031558Srgrimes	return (cp);
4041558Srgrimes}
4051558Srgrimes
4061558Srgrimes/*
4071558Srgrimes * Free space for a name. The resulting entry is linked onto the
4081558Srgrimes * appropriate free list.
4091558Srgrimes */
4101558Srgrimesvoid
41192837Simpfreename(char *name)
4121558Srgrimes{
4131558Srgrimes	struct strhdr *tp, *np;
4148871Srgrimes
4151558Srgrimes	tp = &strtblhdr[strlen(name) / STRTBLINCR];
4161558Srgrimes	np = (struct strhdr *)name;
4171558Srgrimes	np->next = tp->next;
4181558Srgrimes	tp->next = np;
4191558Srgrimes}
4201558Srgrimes
4211558Srgrimes/*
4221558Srgrimes * Useful quantities placed at the end of a dumped symbol table.
4231558Srgrimes */
4241558Srgrimesstruct symtableheader {
42540668Sdima	int32_t	volno;
42640668Sdima	int32_t	stringsize;
42740668Sdima	int32_t	entrytblsize;
4281558Srgrimes	time_t	dumptime;
4291558Srgrimes	time_t	dumpdate;
4301558Srgrimes	ino_t	maxino;
43140668Sdima	int32_t	ntrec;
4321558Srgrimes};
4331558Srgrimes
4341558Srgrimes/*
4351558Srgrimes * dump a snapshot of the symbol table
4361558Srgrimes */
4371558Srgrimesvoid
43892837Simpdumpsymtable(char *filename, long checkpt)
4391558Srgrimes{
44092806Sobrien	struct entry *ep, *tep;
44192806Sobrien	ino_t i;
4421558Srgrimes	struct entry temp, *tentry;
4431558Srgrimes	long mynum = 1, stroff = 0;
4441558Srgrimes	FILE *fd;
4451558Srgrimes	struct symtableheader hdr;
4461558Srgrimes
447207998Sbrueffer	vprintf(stdout, "Checkpointing the restore\n");
4481558Srgrimes	if (Nflag)
4491558Srgrimes		return;
4501558Srgrimes	if ((fd = fopen(filename, "w")) == NULL) {
4511558Srgrimes		fprintf(stderr, "fopen: %s\n", strerror(errno));
4521558Srgrimes		panic("cannot create save file %s for symbol table\n",
4531558Srgrimes			filename);
45485746Stobez		done(1);
4551558Srgrimes	}
4561558Srgrimes	clearerr(fd);
4571558Srgrimes	/*
45837906Scharnier	 * Assign indices to each entry
4591558Srgrimes	 * Write out the string entries
4601558Srgrimes	 */
46123685Speter	for (i = WINO; i <= maxino; i++) {
4621558Srgrimes		for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
4631558Srgrimes			ep->e_index = mynum++;
4641558Srgrimes			(void) fwrite(ep->e_name, sizeof(char),
4651558Srgrimes			       (int)allocsize(ep->e_namlen), fd);
4661558Srgrimes		}
4671558Srgrimes	}
4681558Srgrimes	/*
4691558Srgrimes	 * Convert pointers to indexes, and output
4701558Srgrimes	 */
4711558Srgrimes	tep = &temp;
4721558Srgrimes	stroff = 0;
47323685Speter	for (i = WINO; i <= maxino; i++) {
4741558Srgrimes		for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
47523685Speter			memmove(tep, ep, (long)sizeof(struct entry));
4761558Srgrimes			tep->e_name = (char *)stroff;
4771558Srgrimes			stroff += allocsize(ep->e_namlen);
4781558Srgrimes			tep->e_parent = (struct entry *)ep->e_parent->e_index;
4791558Srgrimes			if (ep->e_links != NULL)
4801558Srgrimes				tep->e_links =
4811558Srgrimes					(struct entry *)ep->e_links->e_index;
4821558Srgrimes			if (ep->e_sibling != NULL)
4831558Srgrimes				tep->e_sibling =
4841558Srgrimes					(struct entry *)ep->e_sibling->e_index;
4851558Srgrimes			if (ep->e_entries != NULL)
4861558Srgrimes				tep->e_entries =
4871558Srgrimes					(struct entry *)ep->e_entries->e_index;
4881558Srgrimes			if (ep->e_next != NULL)
4891558Srgrimes				tep->e_next =
4901558Srgrimes					(struct entry *)ep->e_next->e_index;
4911558Srgrimes			(void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
4921558Srgrimes		}
4931558Srgrimes	}
4941558Srgrimes	/*
4951558Srgrimes	 * Convert entry pointers to indexes, and output
4961558Srgrimes	 */
4971558Srgrimes	for (i = 0; i < entrytblsize; i++) {
4981558Srgrimes		if (entry[i] == NULL)
4991558Srgrimes			tentry = NULL;
5001558Srgrimes		else
5011558Srgrimes			tentry = (struct entry *)entry[i]->e_index;
5021558Srgrimes		(void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
5031558Srgrimes	}
5041558Srgrimes	hdr.volno = checkpt;
5051558Srgrimes	hdr.maxino = maxino;
5061558Srgrimes	hdr.entrytblsize = entrytblsize;
5071558Srgrimes	hdr.stringsize = stroff;
5081558Srgrimes	hdr.dumptime = dumptime;
5091558Srgrimes	hdr.dumpdate = dumpdate;
5101558Srgrimes	hdr.ntrec = ntrec;
5111558Srgrimes	(void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
5121558Srgrimes	if (ferror(fd)) {
5131558Srgrimes		fprintf(stderr, "fwrite: %s\n", strerror(errno));
5141558Srgrimes		panic("output error to file %s writing symbol table\n",
5151558Srgrimes			filename);
5161558Srgrimes	}
5171558Srgrimes	(void) fclose(fd);
5181558Srgrimes}
5191558Srgrimes
5201558Srgrimes/*
5211558Srgrimes * Initialize a symbol table from a file
5221558Srgrimes */
5231558Srgrimesvoid
52492837Simpinitsymtable(char *filename)
5251558Srgrimes{
5261558Srgrimes	char *base;
5271558Srgrimes	long tblsize;
52892806Sobrien	struct entry *ep;
5291558Srgrimes	struct entry *baseep, *lep;
5301558Srgrimes	struct symtableheader hdr;
5311558Srgrimes	struct stat stbuf;
53292806Sobrien	long i;
5331558Srgrimes	int fd;
5341558Srgrimes
5351558Srgrimes	vprintf(stdout, "Initialize symbol table.\n");
5361558Srgrimes	if (filename == NULL) {
5371558Srgrimes		entrytblsize = maxino / HASHFACTOR;
5381558Srgrimes		entry = (struct entry **)
5391558Srgrimes			calloc((unsigned)entrytblsize, sizeof(struct entry *));
5401558Srgrimes		if (entry == (struct entry **)NULL)
5411558Srgrimes			panic("no memory for entry table\n");
5421558Srgrimes		ep = addentry(".", ROOTINO, NODE);
5431558Srgrimes		ep->e_flags |= NEW;
5441558Srgrimes		return;
5451558Srgrimes	}
5461558Srgrimes	if ((fd = open(filename, O_RDONLY, 0)) < 0) {
5471558Srgrimes		fprintf(stderr, "open: %s\n", strerror(errno));
5481558Srgrimes		panic("cannot open symbol table file %s\n", filename);
5491558Srgrimes	}
5501558Srgrimes	if (fstat(fd, &stbuf) < 0) {
5511558Srgrimes		fprintf(stderr, "stat: %s\n", strerror(errno));
5521558Srgrimes		panic("cannot stat symbol table file %s\n", filename);
5531558Srgrimes	}
5541558Srgrimes	tblsize = stbuf.st_size - sizeof(struct symtableheader);
5551558Srgrimes	base = calloc(sizeof(char), (unsigned)tblsize);
5561558Srgrimes	if (base == NULL)
5571558Srgrimes		panic("cannot allocate space for symbol table\n");
5581558Srgrimes	if (read(fd, base, (int)tblsize) < 0 ||
5591558Srgrimes	    read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
5601558Srgrimes		fprintf(stderr, "read: %s\n", strerror(errno));
5611558Srgrimes		panic("cannot read symbol table file %s\n", filename);
5621558Srgrimes	}
5631558Srgrimes	switch (command) {
5641558Srgrimes	case 'r':
5651558Srgrimes		/*
5661558Srgrimes		 * For normal continuation, insure that we are using
5671558Srgrimes		 * the next incremental tape
5681558Srgrimes		 */
5691558Srgrimes		if (hdr.dumpdate != dumptime) {
5701558Srgrimes			if (hdr.dumpdate < dumptime)
5711558Srgrimes				fprintf(stderr, "Incremental tape too low\n");
5721558Srgrimes			else
5731558Srgrimes				fprintf(stderr, "Incremental tape too high\n");
5741558Srgrimes			done(1);
5751558Srgrimes		}
5761558Srgrimes		break;
5771558Srgrimes	case 'R':
5781558Srgrimes		/*
5791558Srgrimes		 * For restart, insure that we are using the same tape
5801558Srgrimes		 */
5811558Srgrimes		curfile.action = SKIP;
5821558Srgrimes		dumptime = hdr.dumptime;
5831558Srgrimes		dumpdate = hdr.dumpdate;
5841558Srgrimes		if (!bflag)
5851558Srgrimes			newtapebuf(hdr.ntrec);
5861558Srgrimes		getvol(hdr.volno);
5871558Srgrimes		break;
5881558Srgrimes	default:
5891558Srgrimes		panic("initsymtable called from command %c\n", command);
5901558Srgrimes		break;
5911558Srgrimes	}
5921558Srgrimes	maxino = hdr.maxino;
5931558Srgrimes	entrytblsize = hdr.entrytblsize;
5941558Srgrimes	entry = (struct entry **)
5951558Srgrimes		(base + tblsize - (entrytblsize * sizeof(struct entry *)));
5961558Srgrimes	baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
5971558Srgrimes	lep = (struct entry *)entry;
5981558Srgrimes	for (i = 0; i < entrytblsize; i++) {
5991558Srgrimes		if (entry[i] == NULL)
6001558Srgrimes			continue;
6011558Srgrimes		entry[i] = &baseep[(long)entry[i]];
6021558Srgrimes	}
6031558Srgrimes	for (ep = &baseep[1]; ep < lep; ep++) {
6041558Srgrimes		ep->e_name = base + (long)ep->e_name;
6051558Srgrimes		ep->e_parent = &baseep[(long)ep->e_parent];
6061558Srgrimes		if (ep->e_sibling != NULL)
6071558Srgrimes			ep->e_sibling = &baseep[(long)ep->e_sibling];
6081558Srgrimes		if (ep->e_links != NULL)
6091558Srgrimes			ep->e_links = &baseep[(long)ep->e_links];
6101558Srgrimes		if (ep->e_entries != NULL)
6111558Srgrimes			ep->e_entries = &baseep[(long)ep->e_entries];
6121558Srgrimes		if (ep->e_next != NULL)
6131558Srgrimes			ep->e_next = &baseep[(long)ep->e_next];
6141558Srgrimes	}
6151558Srgrimes}
616