symtab.c revision 22482
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 351558Srgrimesstatic char sccsid[] = "@(#)symtab.c 8.1 (Berkeley) 6/5/93"; 361558Srgrimes#endif /* not lint */ 371558Srgrimes 381558Srgrimes/* 391558Srgrimes * These routines maintain the symbol table which tracks the state 401558Srgrimes * 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> 541558Srgrimes#include <stdio.h> 551558Srgrimes#include <stdlib.h> 561558Srgrimes#include <string.h> 571558Srgrimes#include <unistd.h> 581558Srgrimes 591558Srgrimes#include "restore.h" 601558Srgrimes#include "extern.h" 611558Srgrimes 621558Srgrimes/* 631558Srgrimes * The following variables define the inode symbol table. 641558Srgrimes * The primary hash table is dynamically allocated based on 651558Srgrimes * the number of inodes in the file system (maxino), scaled by 661558Srgrimes * HASHFACTOR. The variable "entry" points to the hash table; 671558Srgrimes * the variable "entrytblsize" indicates its size (in entries). 681558Srgrimes */ 691558Srgrimes#define HASHFACTOR 5 701558Srgrimesstatic struct entry **entry; 711558Srgrimesstatic long entrytblsize; 721558Srgrimes 731558Srgrimesstatic void addino __P((ino_t, struct entry *)); 741558Srgrimesstatic struct entry *lookupparent __P((char *)); 751558Srgrimesstatic void removeentry __P((struct entry *)); 761558Srgrimes 771558Srgrimes/* 781558Srgrimes * Look up an entry by inode number 791558Srgrimes */ 801558Srgrimesstruct entry * 811558Srgrimeslookupino(inum) 821558Srgrimes ino_t inum; 831558Srgrimes{ 841558Srgrimes register struct entry *ep; 851558Srgrimes 861558Srgrimes if (inum < ROOTINO || 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 981558Srgrimesaddino(inum, np) 991558Srgrimes ino_t inum; 1001558Srgrimes struct entry *np; 1011558Srgrimes{ 1021558Srgrimes struct entry **epp; 1031558Srgrimes 1041558Srgrimes if (inum < ROOTINO || inum >= maxino) 1051558Srgrimes panic("addino: out of range %d\n", inum); 1061558Srgrimes epp = &entry[inum % entrytblsize]; 1071558Srgrimes np->e_ino = inum; 1081558Srgrimes np->e_next = *epp; 1091558Srgrimes *epp = np; 1101558Srgrimes if (dflag) 1111558Srgrimes for (np = np->e_next; np != NULL; np = np->e_next) 1121558Srgrimes if (np->e_ino == inum) 1131558Srgrimes badentry(np, "duplicate inum"); 1141558Srgrimes} 1151558Srgrimes 1161558Srgrimes/* 1171558Srgrimes * Delete an entry from the entry table 1181558Srgrimes */ 1191558Srgrimesvoid 1201558Srgrimesdeleteino(inum) 1211558Srgrimes ino_t inum; 1221558Srgrimes{ 1231558Srgrimes register struct entry *next; 1241558Srgrimes struct entry **prev; 1251558Srgrimes 1261558Srgrimes if (inum < ROOTINO || 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 * 1441558Srgrimeslookupname(name) 1451558Srgrimes char *name; 1461558Srgrimes{ 1471558Srgrimes register struct entry *ep; 1481558Srgrimes register char *np, *cp; 1491558Srgrimes char buf[MAXPATHLEN]; 1501558Srgrimes 1511558Srgrimes cp = name; 1521558Srgrimes for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) { 15322482Seivind for (np = buf; *cp != '/' && *cp != '\0' && 15422482Seivind np < &buf[sizeof(buf)]; ) 1551558Srgrimes *np++ = *cp++; 15622482Seivind if (np == &buf[sizeof(buf)]) 15722482Seivind break; 1581558Srgrimes *np = '\0'; 1591558Srgrimes for ( ; ep != NULL; ep = ep->e_sibling) 1601558Srgrimes if (strcmp(ep->e_name, buf) == 0) 1611558Srgrimes break; 1621558Srgrimes if (ep == NULL) 1631558Srgrimes break; 1641558Srgrimes if (*cp++ == '\0') 1651558Srgrimes return (ep); 1661558Srgrimes } 1671558Srgrimes return (NULL); 1681558Srgrimes} 1691558Srgrimes 1701558Srgrimes/* 1711558Srgrimes * Look up the parent of a pathname 1721558Srgrimes */ 1731558Srgrimesstatic struct entry * 1741558Srgrimeslookupparent(name) 1751558Srgrimes char *name; 1761558Srgrimes{ 1771558Srgrimes struct entry *ep; 1781558Srgrimes char *tailindex; 1791558Srgrimes 1801558Srgrimes tailindex = rindex(name, '/'); 1811558Srgrimes if (tailindex == NULL) 1821558Srgrimes return (NULL); 1831558Srgrimes *tailindex = '\0'; 1841558Srgrimes ep = lookupname(name); 1851558Srgrimes *tailindex = '/'; 1861558Srgrimes if (ep == NULL) 1871558Srgrimes return (NULL); 1881558Srgrimes if (ep->e_type != NODE) 1891558Srgrimes panic("%s is not a directory\n", name); 1901558Srgrimes return (ep); 1911558Srgrimes} 1921558Srgrimes 1931558Srgrimes/* 1941558Srgrimes * Determine the current pathname of a node or leaf 1951558Srgrimes */ 1961558Srgrimeschar * 1971558Srgrimesmyname(ep) 1981558Srgrimes register struct entry *ep; 1991558Srgrimes{ 2001558Srgrimes register char *cp; 2011558Srgrimes static char namebuf[MAXPATHLEN]; 2021558Srgrimes 2031558Srgrimes for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) { 2041558Srgrimes cp -= ep->e_namlen; 2051558Srgrimes bcopy(ep->e_name, cp, (long)ep->e_namlen); 2061558Srgrimes if (ep == lookupino(ROOTINO)) 2071558Srgrimes return (cp); 2081558Srgrimes *(--cp) = '/'; 2091558Srgrimes ep = ep->e_parent; 2101558Srgrimes } 2111558Srgrimes panic("%s: pathname too long\n", cp); 2121558Srgrimes return(cp); 2131558Srgrimes} 2141558Srgrimes 2151558Srgrimes/* 2161558Srgrimes * Unused symbol table entries are linked together on a freelist 2171558Srgrimes * headed by the following pointer. 2181558Srgrimes */ 2191558Srgrimesstatic struct entry *freelist = NULL; 2201558Srgrimes 2211558Srgrimes/* 2221558Srgrimes * add an entry to the symbol table 2231558Srgrimes */ 2241558Srgrimesstruct entry * 2251558Srgrimesaddentry(name, inum, type) 2261558Srgrimes char *name; 2271558Srgrimes ino_t inum; 2281558Srgrimes int type; 2291558Srgrimes{ 2301558Srgrimes register struct entry *np, *ep; 2311558Srgrimes 2321558Srgrimes if (freelist != NULL) { 2331558Srgrimes np = freelist; 2341558Srgrimes freelist = np->e_next; 2351558Srgrimes bzero((char *)np, (long)sizeof(struct entry)); 2361558Srgrimes } else { 2371558Srgrimes np = (struct entry *)calloc(1, sizeof(struct entry)); 2381558Srgrimes if (np == NULL) 2391558Srgrimes panic("no memory to extend symbol table\n"); 2401558Srgrimes } 2411558Srgrimes np->e_type = type & ~LINK; 2421558Srgrimes ep = lookupparent(name); 2431558Srgrimes if (ep == NULL) { 2441558Srgrimes if (inum != ROOTINO || lookupino(ROOTINO) != NULL) 2451558Srgrimes panic("bad name to addentry %s\n", name); 2461558Srgrimes np->e_name = savename(name); 2471558Srgrimes np->e_namlen = strlen(name); 2481558Srgrimes np->e_parent = np; 2491558Srgrimes addino(ROOTINO, np); 2501558Srgrimes return (np); 2511558Srgrimes } 2521558Srgrimes np->e_name = savename(rindex(name, '/') + 1); 2531558Srgrimes np->e_namlen = strlen(np->e_name); 2541558Srgrimes np->e_parent = ep; 2551558Srgrimes np->e_sibling = ep->e_entries; 2561558Srgrimes ep->e_entries = np; 2571558Srgrimes if (type & LINK) { 2581558Srgrimes ep = lookupino(inum); 2591558Srgrimes if (ep == NULL) 2601558Srgrimes panic("link to non-existant name\n"); 2611558Srgrimes np->e_ino = inum; 2621558Srgrimes np->e_links = ep->e_links; 2631558Srgrimes ep->e_links = np; 2641558Srgrimes } else if (inum != 0) { 2651558Srgrimes if (lookupino(inum) != NULL) 2661558Srgrimes panic("duplicate entry\n"); 2671558Srgrimes addino(inum, np); 2681558Srgrimes } 2691558Srgrimes return (np); 2701558Srgrimes} 2711558Srgrimes 2721558Srgrimes/* 2731558Srgrimes * delete an entry from the symbol table 2741558Srgrimes */ 2751558Srgrimesvoid 2761558Srgrimesfreeentry(ep) 2771558Srgrimes register struct entry *ep; 2781558Srgrimes{ 2791558Srgrimes register struct entry *np; 2801558Srgrimes ino_t inum; 2811558Srgrimes 2821558Srgrimes if (ep->e_flags != REMOVED) 2831558Srgrimes badentry(ep, "not marked REMOVED"); 2841558Srgrimes if (ep->e_type == NODE) { 2851558Srgrimes if (ep->e_links != NULL) 2861558Srgrimes badentry(ep, "freeing referenced directory"); 2871558Srgrimes if (ep->e_entries != NULL) 2881558Srgrimes badentry(ep, "freeing non-empty directory"); 2891558Srgrimes } 2901558Srgrimes if (ep->e_ino != 0) { 2911558Srgrimes np = lookupino(ep->e_ino); 2921558Srgrimes if (np == NULL) 2931558Srgrimes badentry(ep, "lookupino failed"); 2941558Srgrimes if (np == ep) { 2951558Srgrimes inum = ep->e_ino; 2961558Srgrimes deleteino(inum); 2971558Srgrimes if (ep->e_links != NULL) 2981558Srgrimes addino(inum, ep->e_links); 2991558Srgrimes } else { 3001558Srgrimes for (; np != NULL; np = np->e_links) { 3011558Srgrimes if (np->e_links == ep) { 3021558Srgrimes np->e_links = ep->e_links; 3031558Srgrimes break; 3041558Srgrimes } 3051558Srgrimes } 3061558Srgrimes if (np == NULL) 3071558Srgrimes badentry(ep, "link not found"); 3081558Srgrimes } 3091558Srgrimes } 3101558Srgrimes removeentry(ep); 3111558Srgrimes freename(ep->e_name); 3121558Srgrimes ep->e_next = freelist; 3131558Srgrimes freelist = ep; 3141558Srgrimes} 3151558Srgrimes 3161558Srgrimes/* 3171558Srgrimes * Relocate an entry in the tree structure 3181558Srgrimes */ 3191558Srgrimesvoid 3201558Srgrimesmoveentry(ep, newname) 3211558Srgrimes register struct entry *ep; 3221558Srgrimes char *newname; 3231558Srgrimes{ 3241558Srgrimes struct entry *np; 3251558Srgrimes char *cp; 3261558Srgrimes 3271558Srgrimes np = lookupparent(newname); 3281558Srgrimes if (np == NULL) 3291558Srgrimes badentry(ep, "cannot move ROOT"); 3301558Srgrimes if (np != ep->e_parent) { 3311558Srgrimes removeentry(ep); 3321558Srgrimes ep->e_parent = np; 3331558Srgrimes ep->e_sibling = np->e_entries; 3341558Srgrimes np->e_entries = ep; 3351558Srgrimes } 3361558Srgrimes cp = rindex(newname, '/') + 1; 3371558Srgrimes freename(ep->e_name); 3381558Srgrimes ep->e_name = savename(cp); 3391558Srgrimes ep->e_namlen = strlen(cp); 3401558Srgrimes if (strcmp(gentempname(ep), ep->e_name) == 0) 3411558Srgrimes ep->e_flags |= TMPNAME; 3421558Srgrimes else 3431558Srgrimes ep->e_flags &= ~TMPNAME; 3441558Srgrimes} 3451558Srgrimes 3461558Srgrimes/* 3471558Srgrimes * Remove an entry in the tree structure 3481558Srgrimes */ 3491558Srgrimesstatic void 3501558Srgrimesremoveentry(ep) 3511558Srgrimes register struct entry *ep; 3521558Srgrimes{ 3531558Srgrimes register struct entry *np; 3541558Srgrimes 3551558Srgrimes np = ep->e_parent; 3561558Srgrimes if (np->e_entries == ep) { 3571558Srgrimes np->e_entries = ep->e_sibling; 3581558Srgrimes } else { 3591558Srgrimes for (np = np->e_entries; np != NULL; np = np->e_sibling) { 3601558Srgrimes if (np->e_sibling == ep) { 3611558Srgrimes np->e_sibling = ep->e_sibling; 3621558Srgrimes break; 3631558Srgrimes } 3641558Srgrimes } 3651558Srgrimes if (np == NULL) 3661558Srgrimes badentry(ep, "cannot find entry in parent list"); 3671558Srgrimes } 3681558Srgrimes} 3691558Srgrimes 3701558Srgrimes/* 3711558Srgrimes * Table of unused string entries, sorted by length. 3728871Srgrimes * 3731558Srgrimes * Entries are allocated in STRTBLINCR sized pieces so that names 3741558Srgrimes * of similar lengths can use the same entry. The value of STRTBLINCR 3751558Srgrimes * is chosen so that every entry has at least enough space to hold 3761558Srgrimes * a "struct strtbl" header. Thus every entry can be linked onto an 3771558Srgrimes * apprpriate free list. 3781558Srgrimes * 3791558Srgrimes * NB. The macro "allocsize" below assumes that "struct strhdr" 3801558Srgrimes * has a size that is a power of two. 3811558Srgrimes */ 3821558Srgrimesstruct strhdr { 3831558Srgrimes struct strhdr *next; 3841558Srgrimes}; 3851558Srgrimes 3861558Srgrimes#define STRTBLINCR (sizeof(struct strhdr)) 3871558Srgrimes#define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1)) 3881558Srgrimes 3891558Srgrimesstatic struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR]; 3901558Srgrimes 3911558Srgrimes/* 3921558Srgrimes * Allocate space for a name. It first looks to see if it already 3931558Srgrimes * has an appropriate sized entry, and if not allocates a new one. 3941558Srgrimes */ 3951558Srgrimeschar * 3961558Srgrimessavename(name) 3971558Srgrimes char *name; 3981558Srgrimes{ 3991558Srgrimes struct strhdr *np; 4001558Srgrimes long len; 4011558Srgrimes char *cp; 4021558Srgrimes 4031558Srgrimes if (name == NULL) 4041558Srgrimes panic("bad name\n"); 4051558Srgrimes len = strlen(name); 4061558Srgrimes np = strtblhdr[len / STRTBLINCR].next; 4071558Srgrimes if (np != NULL) { 4081558Srgrimes strtblhdr[len / STRTBLINCR].next = np->next; 4091558Srgrimes cp = (char *)np; 4101558Srgrimes } else { 4111558Srgrimes cp = malloc((unsigned)allocsize(len)); 4121558Srgrimes if (cp == NULL) 4131558Srgrimes panic("no space for string table\n"); 4141558Srgrimes } 4151558Srgrimes (void) strcpy(cp, name); 4161558Srgrimes return (cp); 4171558Srgrimes} 4181558Srgrimes 4191558Srgrimes/* 4201558Srgrimes * Free space for a name. The resulting entry is linked onto the 4211558Srgrimes * appropriate free list. 4221558Srgrimes */ 4231558Srgrimesvoid 4241558Srgrimesfreename(name) 4251558Srgrimes char *name; 4261558Srgrimes{ 4271558Srgrimes struct strhdr *tp, *np; 4288871Srgrimes 4291558Srgrimes tp = &strtblhdr[strlen(name) / STRTBLINCR]; 4301558Srgrimes np = (struct strhdr *)name; 4311558Srgrimes np->next = tp->next; 4321558Srgrimes tp->next = np; 4331558Srgrimes} 4341558Srgrimes 4351558Srgrimes/* 4361558Srgrimes * Useful quantities placed at the end of a dumped symbol table. 4371558Srgrimes */ 4381558Srgrimesstruct symtableheader { 4391558Srgrimes long volno; 4401558Srgrimes long stringsize; 4411558Srgrimes long entrytblsize; 4421558Srgrimes time_t dumptime; 4431558Srgrimes time_t dumpdate; 4441558Srgrimes ino_t maxino; 4451558Srgrimes long ntrec; 4461558Srgrimes}; 4471558Srgrimes 4481558Srgrimes/* 4491558Srgrimes * dump a snapshot of the symbol table 4501558Srgrimes */ 4511558Srgrimesvoid 4521558Srgrimesdumpsymtable(filename, checkpt) 4531558Srgrimes char *filename; 4541558Srgrimes long checkpt; 4551558Srgrimes{ 4561558Srgrimes register struct entry *ep, *tep; 4571558Srgrimes register ino_t i; 4581558Srgrimes struct entry temp, *tentry; 4591558Srgrimes long mynum = 1, stroff = 0; 4601558Srgrimes FILE *fd; 4611558Srgrimes struct symtableheader hdr; 4621558Srgrimes 4631558Srgrimes vprintf(stdout, "Check pointing the restore\n"); 4641558Srgrimes if (Nflag) 4651558Srgrimes return; 4661558Srgrimes if ((fd = fopen(filename, "w")) == NULL) { 4671558Srgrimes fprintf(stderr, "fopen: %s\n", strerror(errno)); 4681558Srgrimes panic("cannot create save file %s for symbol table\n", 4691558Srgrimes filename); 4701558Srgrimes } 4711558Srgrimes clearerr(fd); 4721558Srgrimes /* 4731558Srgrimes * Assign indicies to each entry 4741558Srgrimes * Write out the string entries 4751558Srgrimes */ 4761558Srgrimes for (i = ROOTINO; i < maxino; i++) { 4771558Srgrimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 4781558Srgrimes ep->e_index = mynum++; 4791558Srgrimes (void) fwrite(ep->e_name, sizeof(char), 4801558Srgrimes (int)allocsize(ep->e_namlen), fd); 4811558Srgrimes } 4821558Srgrimes } 4831558Srgrimes /* 4841558Srgrimes * Convert pointers to indexes, and output 4851558Srgrimes */ 4861558Srgrimes tep = &temp; 4871558Srgrimes stroff = 0; 4881558Srgrimes for (i = ROOTINO; i < maxino; i++) { 4891558Srgrimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 4901558Srgrimes bcopy((char *)ep, (char *)tep, 4911558Srgrimes (long)sizeof(struct entry)); 4921558Srgrimes tep->e_name = (char *)stroff; 4931558Srgrimes stroff += allocsize(ep->e_namlen); 4941558Srgrimes tep->e_parent = (struct entry *)ep->e_parent->e_index; 4951558Srgrimes if (ep->e_links != NULL) 4961558Srgrimes tep->e_links = 4971558Srgrimes (struct entry *)ep->e_links->e_index; 4981558Srgrimes if (ep->e_sibling != NULL) 4991558Srgrimes tep->e_sibling = 5001558Srgrimes (struct entry *)ep->e_sibling->e_index; 5011558Srgrimes if (ep->e_entries != NULL) 5021558Srgrimes tep->e_entries = 5031558Srgrimes (struct entry *)ep->e_entries->e_index; 5041558Srgrimes if (ep->e_next != NULL) 5051558Srgrimes tep->e_next = 5061558Srgrimes (struct entry *)ep->e_next->e_index; 5071558Srgrimes (void) fwrite((char *)tep, sizeof(struct entry), 1, fd); 5081558Srgrimes } 5091558Srgrimes } 5101558Srgrimes /* 5111558Srgrimes * Convert entry pointers to indexes, and output 5121558Srgrimes */ 5131558Srgrimes for (i = 0; i < entrytblsize; i++) { 5141558Srgrimes if (entry[i] == NULL) 5151558Srgrimes tentry = NULL; 5161558Srgrimes else 5171558Srgrimes tentry = (struct entry *)entry[i]->e_index; 5181558Srgrimes (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd); 5191558Srgrimes } 5201558Srgrimes hdr.volno = checkpt; 5211558Srgrimes hdr.maxino = maxino; 5221558Srgrimes hdr.entrytblsize = entrytblsize; 5231558Srgrimes hdr.stringsize = stroff; 5241558Srgrimes hdr.dumptime = dumptime; 5251558Srgrimes hdr.dumpdate = dumpdate; 5261558Srgrimes hdr.ntrec = ntrec; 5271558Srgrimes (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd); 5281558Srgrimes if (ferror(fd)) { 5291558Srgrimes fprintf(stderr, "fwrite: %s\n", strerror(errno)); 5301558Srgrimes panic("output error to file %s writing symbol table\n", 5311558Srgrimes filename); 5321558Srgrimes } 5331558Srgrimes (void) fclose(fd); 5341558Srgrimes} 5351558Srgrimes 5361558Srgrimes/* 5371558Srgrimes * Initialize a symbol table from a file 5381558Srgrimes */ 5391558Srgrimesvoid 5401558Srgrimesinitsymtable(filename) 5411558Srgrimes char *filename; 5421558Srgrimes{ 5431558Srgrimes char *base; 5441558Srgrimes long tblsize; 5451558Srgrimes register struct entry *ep; 5461558Srgrimes struct entry *baseep, *lep; 5471558Srgrimes struct symtableheader hdr; 5481558Srgrimes struct stat stbuf; 5491558Srgrimes register long i; 5501558Srgrimes int fd; 5511558Srgrimes 5521558Srgrimes vprintf(stdout, "Initialize symbol table.\n"); 5531558Srgrimes if (filename == NULL) { 5541558Srgrimes entrytblsize = maxino / HASHFACTOR; 5551558Srgrimes entry = (struct entry **) 5561558Srgrimes calloc((unsigned)entrytblsize, sizeof(struct entry *)); 5571558Srgrimes if (entry == (struct entry **)NULL) 5581558Srgrimes panic("no memory for entry table\n"); 5591558Srgrimes ep = addentry(".", ROOTINO, NODE); 5601558Srgrimes ep->e_flags |= NEW; 5611558Srgrimes return; 5621558Srgrimes } 5631558Srgrimes if ((fd = open(filename, O_RDONLY, 0)) < 0) { 5641558Srgrimes fprintf(stderr, "open: %s\n", strerror(errno)); 5651558Srgrimes panic("cannot open symbol table file %s\n", filename); 5661558Srgrimes } 5671558Srgrimes if (fstat(fd, &stbuf) < 0) { 5681558Srgrimes fprintf(stderr, "stat: %s\n", strerror(errno)); 5691558Srgrimes panic("cannot stat symbol table file %s\n", filename); 5701558Srgrimes } 5711558Srgrimes tblsize = stbuf.st_size - sizeof(struct symtableheader); 5721558Srgrimes base = calloc(sizeof(char), (unsigned)tblsize); 5731558Srgrimes if (base == NULL) 5741558Srgrimes panic("cannot allocate space for symbol table\n"); 5751558Srgrimes if (read(fd, base, (int)tblsize) < 0 || 5761558Srgrimes read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) { 5771558Srgrimes fprintf(stderr, "read: %s\n", strerror(errno)); 5781558Srgrimes panic("cannot read symbol table file %s\n", filename); 5791558Srgrimes } 5801558Srgrimes switch (command) { 5811558Srgrimes case 'r': 5821558Srgrimes /* 5831558Srgrimes * For normal continuation, insure that we are using 5841558Srgrimes * the next incremental tape 5851558Srgrimes */ 5861558Srgrimes if (hdr.dumpdate != dumptime) { 5871558Srgrimes if (hdr.dumpdate < dumptime) 5881558Srgrimes fprintf(stderr, "Incremental tape too low\n"); 5891558Srgrimes else 5901558Srgrimes fprintf(stderr, "Incremental tape too high\n"); 5911558Srgrimes done(1); 5921558Srgrimes } 5931558Srgrimes break; 5941558Srgrimes case 'R': 5951558Srgrimes /* 5961558Srgrimes * For restart, insure that we are using the same tape 5971558Srgrimes */ 5981558Srgrimes curfile.action = SKIP; 5991558Srgrimes dumptime = hdr.dumptime; 6001558Srgrimes dumpdate = hdr.dumpdate; 6011558Srgrimes if (!bflag) 6021558Srgrimes newtapebuf(hdr.ntrec); 6031558Srgrimes getvol(hdr.volno); 6041558Srgrimes break; 6051558Srgrimes default: 6061558Srgrimes panic("initsymtable called from command %c\n", command); 6071558Srgrimes break; 6081558Srgrimes } 6091558Srgrimes maxino = hdr.maxino; 6101558Srgrimes entrytblsize = hdr.entrytblsize; 6111558Srgrimes entry = (struct entry **) 6121558Srgrimes (base + tblsize - (entrytblsize * sizeof(struct entry *))); 6131558Srgrimes baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry)); 6141558Srgrimes lep = (struct entry *)entry; 6151558Srgrimes for (i = 0; i < entrytblsize; i++) { 6161558Srgrimes if (entry[i] == NULL) 6171558Srgrimes continue; 6181558Srgrimes entry[i] = &baseep[(long)entry[i]]; 6191558Srgrimes } 6201558Srgrimes for (ep = &baseep[1]; ep < lep; ep++) { 6211558Srgrimes ep->e_name = base + (long)ep->e_name; 6221558Srgrimes ep->e_parent = &baseep[(long)ep->e_parent]; 6231558Srgrimes if (ep->e_sibling != NULL) 6241558Srgrimes ep->e_sibling = &baseep[(long)ep->e_sibling]; 6251558Srgrimes if (ep->e_links != NULL) 6261558Srgrimes ep->e_links = &baseep[(long)ep->e_links]; 6271558Srgrimes if (ep->e_entries != NULL) 6281558Srgrimes ep->e_entries = &baseep[(long)ep->e_entries]; 6291558Srgrimes if (ep->e_next != NULL) 6301558Srgrimes ep->e_next = &baseep[(long)ep->e_next]; 6311558Srgrimes } 6321558Srgrimes} 633