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> 551558Srgrimes#include <stdio.h> 561558Srgrimes#include <stdlib.h> 571558Srgrimes#include <string.h> 581558Srgrimes#include <unistd.h> 591558Srgrimes 601558Srgrimes#include "restore.h" 611558Srgrimes#include "extern.h" 621558Srgrimes 631558Srgrimes/* 641558Srgrimes * The following variables define the inode symbol table. 651558Srgrimes * The primary hash table is dynamically allocated based on 66102231Strhodes * the number of inodes in the file system (maxino), scaled by 671558Srgrimes * HASHFACTOR. The variable "entry" points to the hash table; 681558Srgrimes * the variable "entrytblsize" indicates its size (in entries). 691558Srgrimes */ 701558Srgrimes#define HASHFACTOR 5 711558Srgrimesstatic struct entry **entry; 721558Srgrimesstatic long entrytblsize; 731558Srgrimes 7492837Simpstatic void addino(ino_t, struct entry *); 7592837Simpstatic struct entry *lookupparent(char *); 7692837Simpstatic void removeentry(struct entry *); 771558Srgrimes 781558Srgrimes/* 791558Srgrimes * Look up an entry by inode number 801558Srgrimes */ 811558Srgrimesstruct entry * 8292837Simplookupino(ino_t inum) 831558Srgrimes{ 8492806Sobrien struct entry *ep; 851558Srgrimes 8623685Speter if (inum < WINO || inum >= maxino) 871558Srgrimes return (NULL); 881558Srgrimes for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next) 891558Srgrimes if (ep->e_ino == inum) 901558Srgrimes return (ep); 911558Srgrimes return (NULL); 921558Srgrimes} 931558Srgrimes 941558Srgrimes/* 951558Srgrimes * Add an entry into the entry table 961558Srgrimes */ 971558Srgrimesstatic void 9892837Simpaddino(ino_t inum, struct entry *np) 991558Srgrimes{ 1001558Srgrimes struct entry **epp; 1011558Srgrimes 10223685Speter if (inum < WINO || inum >= maxino) 1031558Srgrimes panic("addino: out of range %d\n", inum); 1041558Srgrimes epp = &entry[inum % entrytblsize]; 1051558Srgrimes np->e_ino = inum; 1061558Srgrimes np->e_next = *epp; 1071558Srgrimes *epp = np; 1081558Srgrimes if (dflag) 1091558Srgrimes for (np = np->e_next; np != NULL; np = np->e_next) 1101558Srgrimes if (np->e_ino == inum) 1111558Srgrimes badentry(np, "duplicate inum"); 1121558Srgrimes} 1131558Srgrimes 1141558Srgrimes/* 1151558Srgrimes * Delete an entry from the entry table 1161558Srgrimes */ 1171558Srgrimesvoid 11892837Simpdeleteino(ino_t inum) 1191558Srgrimes{ 12092806Sobrien struct entry *next; 1211558Srgrimes struct entry **prev; 1221558Srgrimes 12323685Speter if (inum < WINO || inum >= maxino) 1241558Srgrimes panic("deleteino: out of range %d\n", inum); 1251558Srgrimes prev = &entry[inum % entrytblsize]; 1261558Srgrimes for (next = *prev; next != NULL; next = next->e_next) { 1271558Srgrimes if (next->e_ino == inum) { 1281558Srgrimes next->e_ino = 0; 1291558Srgrimes *prev = next->e_next; 1301558Srgrimes return; 1311558Srgrimes } 1321558Srgrimes prev = &next->e_next; 1331558Srgrimes } 1341558Srgrimes panic("deleteino: %d not found\n", inum); 1351558Srgrimes} 1361558Srgrimes 1371558Srgrimes/* 1381558Srgrimes * Look up an entry by name 1391558Srgrimes */ 1401558Srgrimesstruct entry * 14192837Simplookupname(char *name) 1421558Srgrimes{ 14392806Sobrien struct entry *ep; 14492806Sobrien char *np, *cp; 1451558Srgrimes char buf[MAXPATHLEN]; 1461558Srgrimes 1471558Srgrimes cp = name; 1481558Srgrimes for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) { 14922482Seivind for (np = buf; *cp != '/' && *cp != '\0' && 15022482Seivind np < &buf[sizeof(buf)]; ) 1511558Srgrimes *np++ = *cp++; 15222482Seivind if (np == &buf[sizeof(buf)]) 15322482Seivind break; 1541558Srgrimes *np = '\0'; 1551558Srgrimes for ( ; ep != NULL; ep = ep->e_sibling) 1561558Srgrimes if (strcmp(ep->e_name, buf) == 0) 1571558Srgrimes break; 1581558Srgrimes if (ep == NULL) 1591558Srgrimes break; 1601558Srgrimes if (*cp++ == '\0') 1611558Srgrimes return (ep); 1621558Srgrimes } 1631558Srgrimes return (NULL); 1641558Srgrimes} 1651558Srgrimes 1661558Srgrimes/* 1671558Srgrimes * Look up the parent of a pathname 1681558Srgrimes */ 1691558Srgrimesstatic struct entry * 17092837Simplookupparent(char *name) 1711558Srgrimes{ 1721558Srgrimes struct entry *ep; 1731558Srgrimes char *tailindex; 1741558Srgrimes 17523685Speter tailindex = strrchr(name, '/'); 1761558Srgrimes if (tailindex == NULL) 1771558Srgrimes return (NULL); 1781558Srgrimes *tailindex = '\0'; 1791558Srgrimes ep = lookupname(name); 1801558Srgrimes *tailindex = '/'; 1811558Srgrimes if (ep == NULL) 1821558Srgrimes return (NULL); 1831558Srgrimes if (ep->e_type != NODE) 1841558Srgrimes panic("%s is not a directory\n", name); 1851558Srgrimes return (ep); 1861558Srgrimes} 1871558Srgrimes 1881558Srgrimes/* 1891558Srgrimes * Determine the current pathname of a node or leaf 1901558Srgrimes */ 1911558Srgrimeschar * 19292837Simpmyname(struct entry *ep) 1931558Srgrimes{ 19492806Sobrien char *cp; 1951558Srgrimes static char namebuf[MAXPATHLEN]; 1961558Srgrimes 1971558Srgrimes for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) { 1981558Srgrimes cp -= ep->e_namlen; 19923685Speter memmove(cp, ep->e_name, (long)ep->e_namlen); 2001558Srgrimes if (ep == lookupino(ROOTINO)) 2011558Srgrimes return (cp); 2021558Srgrimes *(--cp) = '/'; 2031558Srgrimes ep = ep->e_parent; 2041558Srgrimes } 2051558Srgrimes panic("%s: pathname too long\n", cp); 2061558Srgrimes return(cp); 2071558Srgrimes} 2081558Srgrimes 2091558Srgrimes/* 21037906Scharnier * Unused symbol table entries are linked together on a free list 2111558Srgrimes * headed by the following pointer. 2121558Srgrimes */ 2131558Srgrimesstatic struct entry *freelist = NULL; 2141558Srgrimes 2151558Srgrimes/* 2161558Srgrimes * add an entry to the symbol table 2171558Srgrimes */ 2181558Srgrimesstruct entry * 21992837Simpaddentry(char *name, ino_t inum, int type) 2201558Srgrimes{ 22192806Sobrien struct entry *np, *ep; 2221558Srgrimes 2231558Srgrimes if (freelist != NULL) { 2241558Srgrimes np = freelist; 2251558Srgrimes freelist = np->e_next; 22623685Speter memset(np, 0, (long)sizeof(struct entry)); 2271558Srgrimes } else { 2281558Srgrimes np = (struct entry *)calloc(1, sizeof(struct entry)); 2291558Srgrimes if (np == NULL) 2301558Srgrimes panic("no memory to extend symbol table\n"); 2311558Srgrimes } 2321558Srgrimes np->e_type = type & ~LINK; 2331558Srgrimes ep = lookupparent(name); 2341558Srgrimes if (ep == NULL) { 2351558Srgrimes if (inum != ROOTINO || lookupino(ROOTINO) != NULL) 2361558Srgrimes panic("bad name to addentry %s\n", name); 2371558Srgrimes np->e_name = savename(name); 2381558Srgrimes np->e_namlen = strlen(name); 2391558Srgrimes np->e_parent = np; 2401558Srgrimes addino(ROOTINO, np); 2411558Srgrimes return (np); 2421558Srgrimes } 24323685Speter np->e_name = savename(strrchr(name, '/') + 1); 2441558Srgrimes np->e_namlen = strlen(np->e_name); 2451558Srgrimes np->e_parent = ep; 2461558Srgrimes np->e_sibling = ep->e_entries; 2471558Srgrimes ep->e_entries = np; 2481558Srgrimes if (type & LINK) { 2491558Srgrimes ep = lookupino(inum); 2501558Srgrimes if (ep == NULL) 25137906Scharnier panic("link to non-existent name\n"); 2521558Srgrimes np->e_ino = inum; 2531558Srgrimes np->e_links = ep->e_links; 2541558Srgrimes ep->e_links = np; 2551558Srgrimes } else if (inum != 0) { 2561558Srgrimes if (lookupino(inum) != NULL) 2571558Srgrimes panic("duplicate entry\n"); 2581558Srgrimes addino(inum, np); 2591558Srgrimes } 2601558Srgrimes return (np); 2611558Srgrimes} 2621558Srgrimes 2631558Srgrimes/* 2641558Srgrimes * delete an entry from the symbol table 2651558Srgrimes */ 2661558Srgrimesvoid 26792837Simpfreeentry(struct entry *ep) 2681558Srgrimes{ 26992806Sobrien struct entry *np; 2701558Srgrimes ino_t inum; 2711558Srgrimes 2721558Srgrimes if (ep->e_flags != REMOVED) 2731558Srgrimes badentry(ep, "not marked REMOVED"); 2741558Srgrimes if (ep->e_type == NODE) { 2751558Srgrimes if (ep->e_links != NULL) 2761558Srgrimes badentry(ep, "freeing referenced directory"); 2771558Srgrimes if (ep->e_entries != NULL) 2781558Srgrimes badentry(ep, "freeing non-empty directory"); 2791558Srgrimes } 2801558Srgrimes if (ep->e_ino != 0) { 2811558Srgrimes np = lookupino(ep->e_ino); 2821558Srgrimes if (np == NULL) 2831558Srgrimes badentry(ep, "lookupino failed"); 2841558Srgrimes if (np == ep) { 2851558Srgrimes inum = ep->e_ino; 2861558Srgrimes deleteino(inum); 2871558Srgrimes if (ep->e_links != NULL) 2881558Srgrimes addino(inum, ep->e_links); 2891558Srgrimes } else { 2901558Srgrimes for (; np != NULL; np = np->e_links) { 2911558Srgrimes if (np->e_links == ep) { 2921558Srgrimes np->e_links = ep->e_links; 2931558Srgrimes break; 2941558Srgrimes } 2951558Srgrimes } 2961558Srgrimes if (np == NULL) 2971558Srgrimes badentry(ep, "link not found"); 2981558Srgrimes } 2991558Srgrimes } 3001558Srgrimes removeentry(ep); 3011558Srgrimes freename(ep->e_name); 3021558Srgrimes ep->e_next = freelist; 3031558Srgrimes freelist = ep; 3041558Srgrimes} 3051558Srgrimes 3061558Srgrimes/* 3071558Srgrimes * Relocate an entry in the tree structure 3081558Srgrimes */ 3091558Srgrimesvoid 31092837Simpmoveentry(struct entry *ep, char *newname) 3111558Srgrimes{ 3121558Srgrimes struct entry *np; 3131558Srgrimes char *cp; 3141558Srgrimes 3151558Srgrimes np = lookupparent(newname); 3161558Srgrimes if (np == NULL) 3171558Srgrimes badentry(ep, "cannot move ROOT"); 3181558Srgrimes if (np != ep->e_parent) { 3191558Srgrimes removeentry(ep); 3201558Srgrimes ep->e_parent = np; 3211558Srgrimes ep->e_sibling = np->e_entries; 3221558Srgrimes np->e_entries = ep; 3231558Srgrimes } 32423685Speter cp = strrchr(newname, '/') + 1; 3251558Srgrimes freename(ep->e_name); 3261558Srgrimes ep->e_name = savename(cp); 3271558Srgrimes ep->e_namlen = strlen(cp); 3281558Srgrimes if (strcmp(gentempname(ep), ep->e_name) == 0) 3291558Srgrimes ep->e_flags |= TMPNAME; 3301558Srgrimes else 3311558Srgrimes ep->e_flags &= ~TMPNAME; 3321558Srgrimes} 3331558Srgrimes 3341558Srgrimes/* 3351558Srgrimes * Remove an entry in the tree structure 3361558Srgrimes */ 3371558Srgrimesstatic void 33892837Simpremoveentry(struct entry *ep) 3391558Srgrimes{ 34092806Sobrien struct entry *np; 3411558Srgrimes 3421558Srgrimes np = ep->e_parent; 3431558Srgrimes if (np->e_entries == ep) { 3441558Srgrimes np->e_entries = ep->e_sibling; 3451558Srgrimes } else { 3461558Srgrimes for (np = np->e_entries; np != NULL; np = np->e_sibling) { 3471558Srgrimes if (np->e_sibling == ep) { 3481558Srgrimes np->e_sibling = ep->e_sibling; 3491558Srgrimes break; 3501558Srgrimes } 3511558Srgrimes } 3521558Srgrimes if (np == NULL) 3531558Srgrimes badentry(ep, "cannot find entry in parent list"); 3541558Srgrimes } 3551558Srgrimes} 3561558Srgrimes 3571558Srgrimes/* 3581558Srgrimes * Table of unused string entries, sorted by length. 3598871Srgrimes * 3601558Srgrimes * Entries are allocated in STRTBLINCR sized pieces so that names 3611558Srgrimes * of similar lengths can use the same entry. The value of STRTBLINCR 3621558Srgrimes * is chosen so that every entry has at least enough space to hold 3631558Srgrimes * a "struct strtbl" header. Thus every entry can be linked onto an 36437906Scharnier * appropriate free list. 3651558Srgrimes * 3661558Srgrimes * NB. The macro "allocsize" below assumes that "struct strhdr" 3671558Srgrimes * has a size that is a power of two. 3681558Srgrimes */ 3691558Srgrimesstruct strhdr { 3701558Srgrimes struct strhdr *next; 3711558Srgrimes}; 3721558Srgrimes 3731558Srgrimes#define STRTBLINCR (sizeof(struct strhdr)) 3741558Srgrimes#define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1)) 3751558Srgrimes 3761558Srgrimesstatic struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR]; 3771558Srgrimes 3781558Srgrimes/* 3791558Srgrimes * Allocate space for a name. It first looks to see if it already 3801558Srgrimes * has an appropriate sized entry, and if not allocates a new one. 3811558Srgrimes */ 3821558Srgrimeschar * 38392837Simpsavename(char *name) 3841558Srgrimes{ 3851558Srgrimes struct strhdr *np; 3861558Srgrimes long len; 3871558Srgrimes char *cp; 3881558Srgrimes 3891558Srgrimes if (name == NULL) 3901558Srgrimes panic("bad name\n"); 3911558Srgrimes len = strlen(name); 3921558Srgrimes np = strtblhdr[len / STRTBLINCR].next; 3931558Srgrimes if (np != NULL) { 3941558Srgrimes strtblhdr[len / STRTBLINCR].next = np->next; 3951558Srgrimes cp = (char *)np; 3961558Srgrimes } else { 3971558Srgrimes cp = malloc((unsigned)allocsize(len)); 3981558Srgrimes if (cp == NULL) 3991558Srgrimes panic("no space for string table\n"); 4001558Srgrimes } 4011558Srgrimes (void) strcpy(cp, name); 4021558Srgrimes return (cp); 4031558Srgrimes} 4041558Srgrimes 4051558Srgrimes/* 4061558Srgrimes * Free space for a name. The resulting entry is linked onto the 4071558Srgrimes * appropriate free list. 4081558Srgrimes */ 4091558Srgrimesvoid 41092837Simpfreename(char *name) 4111558Srgrimes{ 4121558Srgrimes struct strhdr *tp, *np; 4138871Srgrimes 4141558Srgrimes tp = &strtblhdr[strlen(name) / STRTBLINCR]; 4151558Srgrimes np = (struct strhdr *)name; 4161558Srgrimes np->next = tp->next; 4171558Srgrimes tp->next = np; 4181558Srgrimes} 4191558Srgrimes 4201558Srgrimes/* 4211558Srgrimes * Useful quantities placed at the end of a dumped symbol table. 4221558Srgrimes */ 4231558Srgrimesstruct symtableheader { 42440668Sdima int32_t volno; 42540668Sdima int32_t stringsize; 42640668Sdima int32_t entrytblsize; 4271558Srgrimes time_t dumptime; 4281558Srgrimes time_t dumpdate; 4291558Srgrimes ino_t maxino; 43040668Sdima int32_t ntrec; 4311558Srgrimes}; 4321558Srgrimes 4331558Srgrimes/* 4341558Srgrimes * dump a snapshot of the symbol table 4351558Srgrimes */ 4361558Srgrimesvoid 43792837Simpdumpsymtable(char *filename, long checkpt) 4381558Srgrimes{ 43992806Sobrien struct entry *ep, *tep; 44092806Sobrien ino_t i; 4411558Srgrimes struct entry temp, *tentry; 4421558Srgrimes long mynum = 1, stroff = 0; 4431558Srgrimes FILE *fd; 4441558Srgrimes struct symtableheader hdr; 4451558Srgrimes 446207998Sbrueffer vprintf(stdout, "Checkpointing the restore\n"); 4471558Srgrimes if (Nflag) 4481558Srgrimes return; 4491558Srgrimes if ((fd = fopen(filename, "w")) == NULL) { 4501558Srgrimes fprintf(stderr, "fopen: %s\n", strerror(errno)); 4511558Srgrimes panic("cannot create save file %s for symbol table\n", 4521558Srgrimes filename); 45385746Stobez done(1); 4541558Srgrimes } 4551558Srgrimes clearerr(fd); 4561558Srgrimes /* 45737906Scharnier * Assign indices to each entry 4581558Srgrimes * Write out the string entries 4591558Srgrimes */ 46023685Speter for (i = WINO; i <= maxino; i++) { 4611558Srgrimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 4621558Srgrimes ep->e_index = mynum++; 4631558Srgrimes (void) fwrite(ep->e_name, sizeof(char), 4641558Srgrimes (int)allocsize(ep->e_namlen), fd); 4651558Srgrimes } 4661558Srgrimes } 4671558Srgrimes /* 4681558Srgrimes * Convert pointers to indexes, and output 4691558Srgrimes */ 4701558Srgrimes tep = &temp; 4711558Srgrimes stroff = 0; 47223685Speter for (i = WINO; i <= maxino; i++) { 4731558Srgrimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 47423685Speter memmove(tep, ep, (long)sizeof(struct entry)); 4751558Srgrimes tep->e_name = (char *)stroff; 4761558Srgrimes stroff += allocsize(ep->e_namlen); 4771558Srgrimes tep->e_parent = (struct entry *)ep->e_parent->e_index; 4781558Srgrimes if (ep->e_links != NULL) 4791558Srgrimes tep->e_links = 4801558Srgrimes (struct entry *)ep->e_links->e_index; 4811558Srgrimes if (ep->e_sibling != NULL) 4821558Srgrimes tep->e_sibling = 4831558Srgrimes (struct entry *)ep->e_sibling->e_index; 4841558Srgrimes if (ep->e_entries != NULL) 4851558Srgrimes tep->e_entries = 4861558Srgrimes (struct entry *)ep->e_entries->e_index; 4871558Srgrimes if (ep->e_next != NULL) 4881558Srgrimes tep->e_next = 4891558Srgrimes (struct entry *)ep->e_next->e_index; 4901558Srgrimes (void) fwrite((char *)tep, sizeof(struct entry), 1, fd); 4911558Srgrimes } 4921558Srgrimes } 4931558Srgrimes /* 4941558Srgrimes * Convert entry pointers to indexes, and output 4951558Srgrimes */ 4961558Srgrimes for (i = 0; i < entrytblsize; i++) { 4971558Srgrimes if (entry[i] == NULL) 4981558Srgrimes tentry = NULL; 4991558Srgrimes else 5001558Srgrimes tentry = (struct entry *)entry[i]->e_index; 5011558Srgrimes (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd); 5021558Srgrimes } 5031558Srgrimes hdr.volno = checkpt; 5041558Srgrimes hdr.maxino = maxino; 5051558Srgrimes hdr.entrytblsize = entrytblsize; 5061558Srgrimes hdr.stringsize = stroff; 5071558Srgrimes hdr.dumptime = dumptime; 5081558Srgrimes hdr.dumpdate = dumpdate; 5091558Srgrimes hdr.ntrec = ntrec; 5101558Srgrimes (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd); 5111558Srgrimes if (ferror(fd)) { 5121558Srgrimes fprintf(stderr, "fwrite: %s\n", strerror(errno)); 5131558Srgrimes panic("output error to file %s writing symbol table\n", 5141558Srgrimes filename); 5151558Srgrimes } 5161558Srgrimes (void) fclose(fd); 5171558Srgrimes} 5181558Srgrimes 5191558Srgrimes/* 5201558Srgrimes * Initialize a symbol table from a file 5211558Srgrimes */ 5221558Srgrimesvoid 52392837Simpinitsymtable(char *filename) 5241558Srgrimes{ 5251558Srgrimes char *base; 5261558Srgrimes long tblsize; 52792806Sobrien struct entry *ep; 5281558Srgrimes struct entry *baseep, *lep; 5291558Srgrimes struct symtableheader hdr; 5301558Srgrimes struct stat stbuf; 53192806Sobrien long i; 5321558Srgrimes int fd; 5331558Srgrimes 5341558Srgrimes vprintf(stdout, "Initialize symbol table.\n"); 5351558Srgrimes if (filename == NULL) { 5361558Srgrimes entrytblsize = maxino / HASHFACTOR; 5371558Srgrimes entry = (struct entry **) 5381558Srgrimes calloc((unsigned)entrytblsize, sizeof(struct entry *)); 5391558Srgrimes if (entry == (struct entry **)NULL) 5401558Srgrimes panic("no memory for entry table\n"); 5411558Srgrimes ep = addentry(".", ROOTINO, NODE); 5421558Srgrimes ep->e_flags |= NEW; 5431558Srgrimes return; 5441558Srgrimes } 5451558Srgrimes if ((fd = open(filename, O_RDONLY, 0)) < 0) { 5461558Srgrimes fprintf(stderr, "open: %s\n", strerror(errno)); 5471558Srgrimes panic("cannot open symbol table file %s\n", filename); 5481558Srgrimes } 5491558Srgrimes if (fstat(fd, &stbuf) < 0) { 5501558Srgrimes fprintf(stderr, "stat: %s\n", strerror(errno)); 5511558Srgrimes panic("cannot stat symbol table file %s\n", filename); 5521558Srgrimes } 5531558Srgrimes tblsize = stbuf.st_size - sizeof(struct symtableheader); 5541558Srgrimes base = calloc(sizeof(char), (unsigned)tblsize); 5551558Srgrimes if (base == NULL) 5561558Srgrimes panic("cannot allocate space for symbol table\n"); 5571558Srgrimes if (read(fd, base, (int)tblsize) < 0 || 5581558Srgrimes read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) { 5591558Srgrimes fprintf(stderr, "read: %s\n", strerror(errno)); 5601558Srgrimes panic("cannot read symbol table file %s\n", filename); 5611558Srgrimes } 5621558Srgrimes switch (command) { 5631558Srgrimes case 'r': 5641558Srgrimes /* 5651558Srgrimes * For normal continuation, insure that we are using 5661558Srgrimes * the next incremental tape 5671558Srgrimes */ 5681558Srgrimes if (hdr.dumpdate != dumptime) { 5691558Srgrimes if (hdr.dumpdate < dumptime) 5701558Srgrimes fprintf(stderr, "Incremental tape too low\n"); 5711558Srgrimes else 5721558Srgrimes fprintf(stderr, "Incremental tape too high\n"); 5731558Srgrimes done(1); 5741558Srgrimes } 5751558Srgrimes break; 5761558Srgrimes case 'R': 5771558Srgrimes /* 5781558Srgrimes * For restart, insure that we are using the same tape 5791558Srgrimes */ 5801558Srgrimes curfile.action = SKIP; 5811558Srgrimes dumptime = hdr.dumptime; 5821558Srgrimes dumpdate = hdr.dumpdate; 5831558Srgrimes if (!bflag) 5841558Srgrimes newtapebuf(hdr.ntrec); 5851558Srgrimes getvol(hdr.volno); 5861558Srgrimes break; 5871558Srgrimes default: 5881558Srgrimes panic("initsymtable called from command %c\n", command); 5891558Srgrimes break; 5901558Srgrimes } 5911558Srgrimes maxino = hdr.maxino; 5921558Srgrimes entrytblsize = hdr.entrytblsize; 5931558Srgrimes entry = (struct entry **) 5941558Srgrimes (base + tblsize - (entrytblsize * sizeof(struct entry *))); 5951558Srgrimes baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry)); 5961558Srgrimes lep = (struct entry *)entry; 5971558Srgrimes for (i = 0; i < entrytblsize; i++) { 5981558Srgrimes if (entry[i] == NULL) 5991558Srgrimes continue; 6001558Srgrimes entry[i] = &baseep[(long)entry[i]]; 6011558Srgrimes } 6021558Srgrimes for (ep = &baseep[1]; ep < lep; ep++) { 6031558Srgrimes ep->e_name = base + (long)ep->e_name; 6041558Srgrimes ep->e_parent = &baseep[(long)ep->e_parent]; 6051558Srgrimes if (ep->e_sibling != NULL) 6061558Srgrimes ep->e_sibling = &baseep[(long)ep->e_sibling]; 6071558Srgrimes if (ep->e_links != NULL) 6081558Srgrimes ep->e_links = &baseep[(long)ep->e_links]; 6091558Srgrimes if (ep->e_entries != NULL) 6101558Srgrimes ep->e_entries = &baseep[(long)ep->e_entries]; 6111558Srgrimes if (ep->e_next != NULL) 6121558Srgrimes ep->e_next = &baseep[(long)ep->e_next]; 6131558Srgrimes } 6141558Srgrimes} 615