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