18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1983, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 68fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 78fae3551SRodney W. Grimes * are met: 88fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 98fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 108fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 128fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 138fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 148fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 158fae3551SRodney W. Grimes * without specific prior written permission. 168fae3551SRodney W. Grimes * 178fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 188fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 218fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278fae3551SRodney W. Grimes * SUCH DAMAGE. 288fae3551SRodney W. Grimes */ 298fae3551SRodney W. Grimes 308fae3551SRodney W. Grimes #ifndef lint 3191ac32e4SPhilippe Charnier #if 0 32d87d79aeSPeter Wemm static char sccsid[] = "@(#)symtab.c 8.3 (Berkeley) 4/28/95"; 3391ac32e4SPhilippe Charnier #endif 3491ac32e4SPhilippe Charnier static const char rcsid[] = 357f3dea24SPeter Wemm "$FreeBSD$"; 368fae3551SRodney W. Grimes #endif /* not lint */ 378fae3551SRodney W. Grimes 388fae3551SRodney W. Grimes /* 398fae3551SRodney W. Grimes * These routines maintain the symbol table which tracks the state 408fae3551SRodney W. Grimes * of the file system being restored. They provide lookup by either 418fae3551SRodney W. Grimes * name or inode number. They also provide for creation, deletion, 428fae3551SRodney W. Grimes * and renaming of entries. Because of the dynamic nature of pathnames, 438fae3551SRodney W. Grimes * names should not be saved, but always constructed just before they 448fae3551SRodney W. Grimes * are needed, by calling "myname". 458fae3551SRodney W. Grimes */ 468fae3551SRodney W. Grimes 478fae3551SRodney W. Grimes #include <sys/param.h> 488fae3551SRodney W. Grimes #include <sys/stat.h> 498fae3551SRodney W. Grimes 508fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h> 518fae3551SRodney W. Grimes 528fae3551SRodney W. Grimes #include <errno.h> 538fae3551SRodney W. Grimes #include <fcntl.h> 5489fdc4e1SMike Barcroft #include <limits.h> 55e25a029eSMatthew D Fleming #include <stdint.h> 568fae3551SRodney W. Grimes #include <stdio.h> 578fae3551SRodney W. Grimes #include <stdlib.h> 588fae3551SRodney W. Grimes #include <string.h> 598fae3551SRodney W. Grimes #include <unistd.h> 608fae3551SRodney W. Grimes 618fae3551SRodney W. Grimes #include "restore.h" 628fae3551SRodney W. Grimes #include "extern.h" 638fae3551SRodney W. Grimes 648fae3551SRodney W. Grimes /* 658fae3551SRodney W. Grimes * The following variables define the inode symbol table. 668fae3551SRodney W. Grimes * The primary hash table is dynamically allocated based on 678fae3551SRodney W. Grimes * the number of inodes in the file system (maxino), scaled by 688fae3551SRodney W. Grimes * HASHFACTOR. The variable "entry" points to the hash table; 698fae3551SRodney W. Grimes * the variable "entrytblsize" indicates its size (in entries). 708fae3551SRodney W. Grimes */ 718fae3551SRodney W. Grimes #define HASHFACTOR 5 728fae3551SRodney W. Grimes static struct entry **entry; 738fae3551SRodney W. Grimes static long entrytblsize; 748fae3551SRodney W. Grimes 752db673abSWarner Losh static void addino(ino_t, struct entry *); 762db673abSWarner Losh static struct entry *lookupparent(char *); 772db673abSWarner Losh static void removeentry(struct entry *); 788fae3551SRodney W. Grimes 798fae3551SRodney W. Grimes /* 808fae3551SRodney W. Grimes * Look up an entry by inode number 818fae3551SRodney W. Grimes */ 828fae3551SRodney W. Grimes struct entry * 832db673abSWarner Losh lookupino(ino_t inum) 848fae3551SRodney W. Grimes { 853d438ad6SDavid E. O'Brien struct entry *ep; 868fae3551SRodney W. Grimes 87d87d79aeSPeter Wemm if (inum < WINO || inum >= maxino) 888fae3551SRodney W. Grimes return (NULL); 898fae3551SRodney W. Grimes for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next) 908fae3551SRodney W. Grimes if (ep->e_ino == inum) 918fae3551SRodney W. Grimes return (ep); 928fae3551SRodney W. Grimes return (NULL); 938fae3551SRodney W. Grimes } 948fae3551SRodney W. Grimes 958fae3551SRodney W. Grimes /* 968fae3551SRodney W. Grimes * Add an entry into the entry table 978fae3551SRodney W. Grimes */ 988fae3551SRodney W. Grimes static void 992db673abSWarner Losh addino(ino_t inum, struct entry *np) 1008fae3551SRodney W. Grimes { 1018fae3551SRodney W. Grimes struct entry **epp; 1028fae3551SRodney W. Grimes 103d87d79aeSPeter Wemm if (inum < WINO || inum >= maxino) 104e25a029eSMatthew D Fleming panic("addino: out of range %ju\n", (uintmax_t)inum); 1058fae3551SRodney W. Grimes epp = &entry[inum % entrytblsize]; 1068fae3551SRodney W. Grimes np->e_ino = inum; 1078fae3551SRodney W. Grimes np->e_next = *epp; 1088fae3551SRodney W. Grimes *epp = np; 1098fae3551SRodney W. Grimes if (dflag) 1108fae3551SRodney W. Grimes for (np = np->e_next; np != NULL; np = np->e_next) 1118fae3551SRodney W. Grimes if (np->e_ino == inum) 1128fae3551SRodney W. Grimes badentry(np, "duplicate inum"); 1138fae3551SRodney W. Grimes } 1148fae3551SRodney W. Grimes 1158fae3551SRodney W. Grimes /* 1168fae3551SRodney W. Grimes * Delete an entry from the entry table 1178fae3551SRodney W. Grimes */ 1188fae3551SRodney W. Grimes void 1192db673abSWarner Losh deleteino(ino_t inum) 1208fae3551SRodney W. Grimes { 1213d438ad6SDavid E. O'Brien struct entry *next; 1228fae3551SRodney W. Grimes struct entry **prev; 1238fae3551SRodney W. Grimes 124d87d79aeSPeter Wemm if (inum < WINO || inum >= maxino) 125e25a029eSMatthew D Fleming panic("deleteino: out of range %ju\n", (uintmax_t)inum); 1268fae3551SRodney W. Grimes prev = &entry[inum % entrytblsize]; 1278fae3551SRodney W. Grimes for (next = *prev; next != NULL; next = next->e_next) { 1288fae3551SRodney W. Grimes if (next->e_ino == inum) { 1298fae3551SRodney W. Grimes next->e_ino = 0; 1308fae3551SRodney W. Grimes *prev = next->e_next; 1318fae3551SRodney W. Grimes return; 1328fae3551SRodney W. Grimes } 1338fae3551SRodney W. Grimes prev = &next->e_next; 1348fae3551SRodney W. Grimes } 135e25a029eSMatthew D Fleming panic("deleteino: %ju not found\n", (uintmax_t)inum); 1368fae3551SRodney W. Grimes } 1378fae3551SRodney W. Grimes 1388fae3551SRodney W. Grimes /* 1398fae3551SRodney W. Grimes * Look up an entry by name 1408fae3551SRodney W. Grimes */ 1418fae3551SRodney W. Grimes struct entry * 1422db673abSWarner Losh lookupname(char *name) 1438fae3551SRodney W. Grimes { 1443d438ad6SDavid E. O'Brien struct entry *ep; 1453d438ad6SDavid E. O'Brien char *np, *cp; 1468fae3551SRodney W. Grimes char buf[MAXPATHLEN]; 1478fae3551SRodney W. Grimes 1488fae3551SRodney W. Grimes cp = name; 1498fae3551SRodney W. Grimes for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) { 150b94f03b2SEivind Eklund for (np = buf; *cp != '/' && *cp != '\0' && 151b94f03b2SEivind Eklund np < &buf[sizeof(buf)]; ) 1528fae3551SRodney W. Grimes *np++ = *cp++; 153b94f03b2SEivind Eklund if (np == &buf[sizeof(buf)]) 154b94f03b2SEivind Eklund break; 1558fae3551SRodney W. Grimes *np = '\0'; 1568fae3551SRodney W. Grimes for ( ; ep != NULL; ep = ep->e_sibling) 1578fae3551SRodney W. Grimes if (strcmp(ep->e_name, buf) == 0) 1588fae3551SRodney W. Grimes break; 1598fae3551SRodney W. Grimes if (ep == NULL) 1608fae3551SRodney W. Grimes break; 1618fae3551SRodney W. Grimes if (*cp++ == '\0') 1628fae3551SRodney W. Grimes return (ep); 1638fae3551SRodney W. Grimes } 1648fae3551SRodney W. Grimes return (NULL); 1658fae3551SRodney W. Grimes } 1668fae3551SRodney W. Grimes 1678fae3551SRodney W. Grimes /* 1688fae3551SRodney W. Grimes * Look up the parent of a pathname 1698fae3551SRodney W. Grimes */ 1708fae3551SRodney W. Grimes static struct entry * 1712db673abSWarner Losh lookupparent(char *name) 1728fae3551SRodney W. Grimes { 1738fae3551SRodney W. Grimes struct entry *ep; 1748fae3551SRodney W. Grimes char *tailindex; 1758fae3551SRodney W. Grimes 176d87d79aeSPeter Wemm tailindex = strrchr(name, '/'); 1778fae3551SRodney W. Grimes if (tailindex == NULL) 1788fae3551SRodney W. Grimes return (NULL); 1798fae3551SRodney W. Grimes *tailindex = '\0'; 1808fae3551SRodney W. Grimes ep = lookupname(name); 1818fae3551SRodney W. Grimes *tailindex = '/'; 1828fae3551SRodney W. Grimes if (ep == NULL) 1838fae3551SRodney W. Grimes return (NULL); 1848fae3551SRodney W. Grimes if (ep->e_type != NODE) 1858fae3551SRodney W. Grimes panic("%s is not a directory\n", name); 1868fae3551SRodney W. Grimes return (ep); 1878fae3551SRodney W. Grimes } 1888fae3551SRodney W. Grimes 1898fae3551SRodney W. Grimes /* 1908fae3551SRodney W. Grimes * Determine the current pathname of a node or leaf 1918fae3551SRodney W. Grimes */ 1928fae3551SRodney W. Grimes char * 1932db673abSWarner Losh myname(struct entry *ep) 1948fae3551SRodney W. Grimes { 1953d438ad6SDavid E. O'Brien char *cp; 1968fae3551SRodney W. Grimes static char namebuf[MAXPATHLEN]; 1978fae3551SRodney W. Grimes 1988fae3551SRodney W. Grimes for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) { 1998fae3551SRodney W. Grimes cp -= ep->e_namlen; 200d87d79aeSPeter Wemm memmove(cp, ep->e_name, (long)ep->e_namlen); 2018fae3551SRodney W. Grimes if (ep == lookupino(ROOTINO)) 2028fae3551SRodney W. Grimes return (cp); 2038fae3551SRodney W. Grimes *(--cp) = '/'; 2048fae3551SRodney W. Grimes ep = ep->e_parent; 2058fae3551SRodney W. Grimes } 2068fae3551SRodney W. Grimes panic("%s: pathname too long\n", cp); 2078fae3551SRodney W. Grimes return(cp); 2088fae3551SRodney W. Grimes } 2098fae3551SRodney W. Grimes 2108fae3551SRodney W. Grimes /* 2118fae3551SRodney W. Grimes * Unused symbol table entries are linked together on a free list 2128fae3551SRodney W. Grimes * headed by the following pointer. 2138fae3551SRodney W. Grimes */ 2148fae3551SRodney W. Grimes static struct entry *freelist = NULL; 2158fae3551SRodney W. Grimes 2168fae3551SRodney W. Grimes /* 2178fae3551SRodney W. Grimes * add an entry to the symbol table 2188fae3551SRodney W. Grimes */ 2198fae3551SRodney W. Grimes struct entry * 2202db673abSWarner Losh addentry(char *name, ino_t inum, int type) 2218fae3551SRodney W. Grimes { 2223d438ad6SDavid E. O'Brien struct entry *np, *ep; 2238fae3551SRodney W. Grimes 2248fae3551SRodney W. Grimes if (freelist != NULL) { 2258fae3551SRodney W. Grimes np = freelist; 2268fae3551SRodney W. Grimes freelist = np->e_next; 227d87d79aeSPeter Wemm memset(np, 0, (long)sizeof(struct entry)); 2288fae3551SRodney W. Grimes } else { 2298fae3551SRodney W. Grimes np = (struct entry *)calloc(1, sizeof(struct entry)); 2308fae3551SRodney W. Grimes if (np == NULL) 2318fae3551SRodney W. Grimes panic("no memory to extend symbol table\n"); 2328fae3551SRodney W. Grimes } 2338fae3551SRodney W. Grimes np->e_type = type & ~LINK; 2348fae3551SRodney W. Grimes ep = lookupparent(name); 2358fae3551SRodney W. Grimes if (ep == NULL) { 2368fae3551SRodney W. Grimes if (inum != ROOTINO || lookupino(ROOTINO) != NULL) 2378fae3551SRodney W. Grimes panic("bad name to addentry %s\n", name); 2388fae3551SRodney W. Grimes np->e_name = savename(name); 2398fae3551SRodney W. Grimes np->e_namlen = strlen(name); 2408fae3551SRodney W. Grimes np->e_parent = np; 2418fae3551SRodney W. Grimes addino(ROOTINO, np); 2428fae3551SRodney W. Grimes return (np); 2438fae3551SRodney W. Grimes } 244d87d79aeSPeter Wemm np->e_name = savename(strrchr(name, '/') + 1); 2458fae3551SRodney W. Grimes np->e_namlen = strlen(np->e_name); 2468fae3551SRodney W. Grimes np->e_parent = ep; 2478fae3551SRodney W. Grimes np->e_sibling = ep->e_entries; 2488fae3551SRodney W. Grimes ep->e_entries = np; 2498fae3551SRodney W. Grimes if (type & LINK) { 2508fae3551SRodney W. Grimes ep = lookupino(inum); 2518fae3551SRodney W. Grimes if (ep == NULL) 25291ac32e4SPhilippe Charnier panic("link to non-existent name\n"); 2538fae3551SRodney W. Grimes np->e_ino = inum; 2548fae3551SRodney W. Grimes np->e_links = ep->e_links; 2558fae3551SRodney W. Grimes ep->e_links = np; 2568fae3551SRodney W. Grimes } else if (inum != 0) { 2578fae3551SRodney W. Grimes if (lookupino(inum) != NULL) 2588fae3551SRodney W. Grimes panic("duplicate entry\n"); 2598fae3551SRodney W. Grimes addino(inum, np); 2608fae3551SRodney W. Grimes } 2618fae3551SRodney W. Grimes return (np); 2628fae3551SRodney W. Grimes } 2638fae3551SRodney W. Grimes 2648fae3551SRodney W. Grimes /* 2658fae3551SRodney W. Grimes * delete an entry from the symbol table 2668fae3551SRodney W. Grimes */ 2678fae3551SRodney W. Grimes void 2682db673abSWarner Losh freeentry(struct entry *ep) 2698fae3551SRodney W. Grimes { 2703d438ad6SDavid E. O'Brien struct entry *np; 2718fae3551SRodney W. Grimes ino_t inum; 2728fae3551SRodney W. Grimes 2738fae3551SRodney W. Grimes if (ep->e_flags != REMOVED) 2748fae3551SRodney W. Grimes badentry(ep, "not marked REMOVED"); 2758fae3551SRodney W. Grimes if (ep->e_type == NODE) { 2768fae3551SRodney W. Grimes if (ep->e_links != NULL) 2778fae3551SRodney W. Grimes badentry(ep, "freeing referenced directory"); 2788fae3551SRodney W. Grimes if (ep->e_entries != NULL) 2798fae3551SRodney W. Grimes badentry(ep, "freeing non-empty directory"); 2808fae3551SRodney W. Grimes } 2818fae3551SRodney W. Grimes if (ep->e_ino != 0) { 2828fae3551SRodney W. Grimes np = lookupino(ep->e_ino); 2838fae3551SRodney W. Grimes if (np == NULL) 2848fae3551SRodney W. Grimes badentry(ep, "lookupino failed"); 2858fae3551SRodney W. Grimes if (np == ep) { 2868fae3551SRodney W. Grimes inum = ep->e_ino; 2878fae3551SRodney W. Grimes deleteino(inum); 2888fae3551SRodney W. Grimes if (ep->e_links != NULL) 2898fae3551SRodney W. Grimes addino(inum, ep->e_links); 2908fae3551SRodney W. Grimes } else { 2918fae3551SRodney W. Grimes for (; np != NULL; np = np->e_links) { 2928fae3551SRodney W. Grimes if (np->e_links == ep) { 2938fae3551SRodney W. Grimes np->e_links = ep->e_links; 2948fae3551SRodney W. Grimes break; 2958fae3551SRodney W. Grimes } 2968fae3551SRodney W. Grimes } 2978fae3551SRodney W. Grimes if (np == NULL) 2988fae3551SRodney W. Grimes badentry(ep, "link not found"); 2998fae3551SRodney W. Grimes } 3008fae3551SRodney W. Grimes } 3018fae3551SRodney W. Grimes removeentry(ep); 3028fae3551SRodney W. Grimes freename(ep->e_name); 3038fae3551SRodney W. Grimes ep->e_next = freelist; 3048fae3551SRodney W. Grimes freelist = ep; 3058fae3551SRodney W. Grimes } 3068fae3551SRodney W. Grimes 3078fae3551SRodney W. Grimes /* 3088fae3551SRodney W. Grimes * Relocate an entry in the tree structure 3098fae3551SRodney W. Grimes */ 3108fae3551SRodney W. Grimes void 3112db673abSWarner Losh moveentry(struct entry *ep, char *newname) 3128fae3551SRodney W. Grimes { 3138fae3551SRodney W. Grimes struct entry *np; 3148fae3551SRodney W. Grimes char *cp; 3158fae3551SRodney W. Grimes 3168fae3551SRodney W. Grimes np = lookupparent(newname); 3178fae3551SRodney W. Grimes if (np == NULL) 3188fae3551SRodney W. Grimes badentry(ep, "cannot move ROOT"); 3198fae3551SRodney W. Grimes if (np != ep->e_parent) { 3208fae3551SRodney W. Grimes removeentry(ep); 3218fae3551SRodney W. Grimes ep->e_parent = np; 3228fae3551SRodney W. Grimes ep->e_sibling = np->e_entries; 3238fae3551SRodney W. Grimes np->e_entries = ep; 3248fae3551SRodney W. Grimes } 325d87d79aeSPeter Wemm cp = strrchr(newname, '/') + 1; 3268fae3551SRodney W. Grimes freename(ep->e_name); 3278fae3551SRodney W. Grimes ep->e_name = savename(cp); 3288fae3551SRodney W. Grimes ep->e_namlen = strlen(cp); 3298fae3551SRodney W. Grimes if (strcmp(gentempname(ep), ep->e_name) == 0) 3308fae3551SRodney W. Grimes ep->e_flags |= TMPNAME; 3318fae3551SRodney W. Grimes else 3328fae3551SRodney W. Grimes ep->e_flags &= ~TMPNAME; 3338fae3551SRodney W. Grimes } 3348fae3551SRodney W. Grimes 3358fae3551SRodney W. Grimes /* 3368fae3551SRodney W. Grimes * Remove an entry in the tree structure 3378fae3551SRodney W. Grimes */ 3388fae3551SRodney W. Grimes static void 3392db673abSWarner Losh removeentry(struct entry *ep) 3408fae3551SRodney W. Grimes { 3413d438ad6SDavid E. O'Brien struct entry *np; 3428fae3551SRodney W. Grimes 3438fae3551SRodney W. Grimes np = ep->e_parent; 3448fae3551SRodney W. Grimes if (np->e_entries == ep) { 3458fae3551SRodney W. Grimes np->e_entries = ep->e_sibling; 3468fae3551SRodney W. Grimes } else { 3478fae3551SRodney W. Grimes for (np = np->e_entries; np != NULL; np = np->e_sibling) { 3488fae3551SRodney W. Grimes if (np->e_sibling == ep) { 3498fae3551SRodney W. Grimes np->e_sibling = ep->e_sibling; 3508fae3551SRodney W. Grimes break; 3518fae3551SRodney W. Grimes } 3528fae3551SRodney W. Grimes } 3538fae3551SRodney W. Grimes if (np == NULL) 3548fae3551SRodney W. Grimes badentry(ep, "cannot find entry in parent list"); 3558fae3551SRodney W. Grimes } 3568fae3551SRodney W. Grimes } 3578fae3551SRodney W. Grimes 3588fae3551SRodney W. Grimes /* 3598fae3551SRodney W. Grimes * Table of unused string entries, sorted by length. 3608fae3551SRodney W. Grimes * 3618fae3551SRodney W. Grimes * Entries are allocated in STRTBLINCR sized pieces so that names 3628fae3551SRodney W. Grimes * of similar lengths can use the same entry. The value of STRTBLINCR 3638fae3551SRodney W. Grimes * is chosen so that every entry has at least enough space to hold 3648fae3551SRodney W. Grimes * a "struct strtbl" header. Thus every entry can be linked onto an 36591ac32e4SPhilippe Charnier * appropriate free list. 3668fae3551SRodney W. Grimes * 3678fae3551SRodney W. Grimes * NB. The macro "allocsize" below assumes that "struct strhdr" 3688fae3551SRodney W. Grimes * has a size that is a power of two. 3698fae3551SRodney W. Grimes */ 3708fae3551SRodney W. Grimes struct strhdr { 3718fae3551SRodney W. Grimes struct strhdr *next; 3728fae3551SRodney W. Grimes }; 3738fae3551SRodney W. Grimes 3748fae3551SRodney W. Grimes #define STRTBLINCR (sizeof(struct strhdr)) 37505cfc40aSPedro F. Giffuni #define allocsize(size) roundup2((size) + 1, STRTBLINCR) 3768fae3551SRodney W. Grimes 3778fae3551SRodney W. Grimes static struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR]; 3788fae3551SRodney W. Grimes 3798fae3551SRodney W. Grimes /* 3808fae3551SRodney W. Grimes * Allocate space for a name. It first looks to see if it already 3818fae3551SRodney W. Grimes * has an appropriate sized entry, and if not allocates a new one. 3828fae3551SRodney W. Grimes */ 3838fae3551SRodney W. Grimes char * 3842db673abSWarner Losh savename(char *name) 3858fae3551SRodney W. Grimes { 3868fae3551SRodney W. Grimes struct strhdr *np; 38705cfc40aSPedro F. Giffuni size_t len; 3888fae3551SRodney W. Grimes char *cp; 3898fae3551SRodney W. Grimes 3908fae3551SRodney W. Grimes if (name == NULL) 3918fae3551SRodney W. Grimes panic("bad name\n"); 3928fae3551SRodney W. Grimes len = strlen(name); 3938fae3551SRodney W. Grimes np = strtblhdr[len / STRTBLINCR].next; 3948fae3551SRodney W. Grimes if (np != NULL) { 3958fae3551SRodney W. Grimes strtblhdr[len / STRTBLINCR].next = np->next; 3968fae3551SRodney W. Grimes cp = (char *)np; 3978fae3551SRodney W. Grimes } else { 39805cfc40aSPedro F. Giffuni cp = malloc(allocsize(len)); 3998fae3551SRodney W. Grimes if (cp == NULL) 4008fae3551SRodney W. Grimes panic("no space for string table\n"); 4018fae3551SRodney W. Grimes } 4028fae3551SRodney W. Grimes (void) strcpy(cp, name); 4038fae3551SRodney W. Grimes return (cp); 4048fae3551SRodney W. Grimes } 4058fae3551SRodney W. Grimes 4068fae3551SRodney W. Grimes /* 4078fae3551SRodney W. Grimes * Free space for a name. The resulting entry is linked onto the 4088fae3551SRodney W. Grimes * appropriate free list. 4098fae3551SRodney W. Grimes */ 4108fae3551SRodney W. Grimes void 4112db673abSWarner Losh freename(char *name) 4128fae3551SRodney W. Grimes { 4138fae3551SRodney W. Grimes struct strhdr *tp, *np; 4148fae3551SRodney W. Grimes 4158fae3551SRodney W. Grimes tp = &strtblhdr[strlen(name) / STRTBLINCR]; 4168fae3551SRodney W. Grimes np = (struct strhdr *)name; 4178fae3551SRodney W. Grimes np->next = tp->next; 4188fae3551SRodney W. Grimes tp->next = np; 4198fae3551SRodney W. Grimes } 4208fae3551SRodney W. Grimes 4218fae3551SRodney W. Grimes /* 4228fae3551SRodney W. Grimes * Useful quantities placed at the end of a dumped symbol table. 4238fae3551SRodney W. Grimes */ 4248fae3551SRodney W. Grimes struct symtableheader { 42597b465b1SDima Ruban int32_t volno; 42697b465b1SDima Ruban int32_t stringsize; 42797b465b1SDima Ruban int32_t entrytblsize; 4288fae3551SRodney W. Grimes time_t dumptime; 4298fae3551SRodney W. Grimes time_t dumpdate; 4308fae3551SRodney W. Grimes ino_t maxino; 43197b465b1SDima Ruban int32_t ntrec; 4328fae3551SRodney W. Grimes }; 4338fae3551SRodney W. Grimes 4348fae3551SRodney W. Grimes /* 4358fae3551SRodney W. Grimes * dump a snapshot of the symbol table 4368fae3551SRodney W. Grimes */ 4378fae3551SRodney W. Grimes void 4382db673abSWarner Losh dumpsymtable(char *filename, long checkpt) 4398fae3551SRodney W. Grimes { 4403d438ad6SDavid E. O'Brien struct entry *ep, *tep; 4413d438ad6SDavid E. O'Brien ino_t i; 4428fae3551SRodney W. Grimes struct entry temp, *tentry; 4438fae3551SRodney W. Grimes long mynum = 1, stroff = 0; 4448fae3551SRodney W. Grimes FILE *fd; 4458fae3551SRodney W. Grimes struct symtableheader hdr; 4468fae3551SRodney W. Grimes 4478fae3551SRodney W. Grimes vprintf(stdout, "Checkpointing the restore\n"); 4488fae3551SRodney W. Grimes if (Nflag) 4498fae3551SRodney W. Grimes return; 4508fae3551SRodney W. Grimes if ((fd = fopen(filename, "w")) == NULL) { 4518fae3551SRodney W. Grimes fprintf(stderr, "fopen: %s\n", strerror(errno)); 4528fae3551SRodney W. Grimes panic("cannot create save file %s for symbol table\n", 4538fae3551SRodney W. Grimes filename); 454646d3727SAnton Berezin done(1); 4558fae3551SRodney W. Grimes } 4568fae3551SRodney W. Grimes clearerr(fd); 4578fae3551SRodney W. Grimes /* 45891ac32e4SPhilippe Charnier * Assign indices to each entry 4598fae3551SRodney W. Grimes * Write out the string entries 4608fae3551SRodney W. Grimes */ 461d87d79aeSPeter Wemm for (i = WINO; i <= maxino; i++) { 4628fae3551SRodney W. Grimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 4638fae3551SRodney W. Grimes ep->e_index = mynum++; 4648fae3551SRodney W. Grimes (void) fwrite(ep->e_name, sizeof(char), 4658fae3551SRodney W. Grimes (int)allocsize(ep->e_namlen), fd); 4668fae3551SRodney W. Grimes } 4678fae3551SRodney W. Grimes } 4688fae3551SRodney W. Grimes /* 4698fae3551SRodney W. Grimes * Convert pointers to indexes, and output 4708fae3551SRodney W. Grimes */ 4718fae3551SRodney W. Grimes tep = &temp; 4728fae3551SRodney W. Grimes stroff = 0; 473d87d79aeSPeter Wemm for (i = WINO; i <= maxino; i++) { 4748fae3551SRodney W. Grimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 475d87d79aeSPeter Wemm memmove(tep, ep, (long)sizeof(struct entry)); 4768fae3551SRodney W. Grimes tep->e_name = (char *)stroff; 4778fae3551SRodney W. Grimes stroff += allocsize(ep->e_namlen); 4788fae3551SRodney W. Grimes tep->e_parent = (struct entry *)ep->e_parent->e_index; 4798fae3551SRodney W. Grimes if (ep->e_links != NULL) 4808fae3551SRodney W. Grimes tep->e_links = 4818fae3551SRodney W. Grimes (struct entry *)ep->e_links->e_index; 4828fae3551SRodney W. Grimes if (ep->e_sibling != NULL) 4838fae3551SRodney W. Grimes tep->e_sibling = 4848fae3551SRodney W. Grimes (struct entry *)ep->e_sibling->e_index; 4858fae3551SRodney W. Grimes if (ep->e_entries != NULL) 4868fae3551SRodney W. Grimes tep->e_entries = 4878fae3551SRodney W. Grimes (struct entry *)ep->e_entries->e_index; 4888fae3551SRodney W. Grimes if (ep->e_next != NULL) 4898fae3551SRodney W. Grimes tep->e_next = 4908fae3551SRodney W. Grimes (struct entry *)ep->e_next->e_index; 4918fae3551SRodney W. Grimes (void) fwrite((char *)tep, sizeof(struct entry), 1, fd); 4928fae3551SRodney W. Grimes } 4938fae3551SRodney W. Grimes } 4948fae3551SRodney W. Grimes /* 4958fae3551SRodney W. Grimes * Convert entry pointers to indexes, and output 4968fae3551SRodney W. Grimes */ 4978fae3551SRodney W. Grimes for (i = 0; i < entrytblsize; i++) { 4988fae3551SRodney W. Grimes if (entry[i] == NULL) 4998fae3551SRodney W. Grimes tentry = NULL; 5008fae3551SRodney W. Grimes else 5018fae3551SRodney W. Grimes tentry = (struct entry *)entry[i]->e_index; 5028fae3551SRodney W. Grimes (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd); 5038fae3551SRodney W. Grimes } 5048fae3551SRodney W. Grimes hdr.volno = checkpt; 5058fae3551SRodney W. Grimes hdr.maxino = maxino; 5068fae3551SRodney W. Grimes hdr.entrytblsize = entrytblsize; 5078fae3551SRodney W. Grimes hdr.stringsize = stroff; 5088fae3551SRodney W. Grimes hdr.dumptime = dumptime; 5098fae3551SRodney W. Grimes hdr.dumpdate = dumpdate; 5108fae3551SRodney W. Grimes hdr.ntrec = ntrec; 5118fae3551SRodney W. Grimes (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd); 5128fae3551SRodney W. Grimes if (ferror(fd)) { 5138fae3551SRodney W. Grimes fprintf(stderr, "fwrite: %s\n", strerror(errno)); 5148fae3551SRodney W. Grimes panic("output error to file %s writing symbol table\n", 5158fae3551SRodney W. Grimes filename); 5168fae3551SRodney W. Grimes } 5178fae3551SRodney W. Grimes (void) fclose(fd); 5188fae3551SRodney W. Grimes } 5198fae3551SRodney W. Grimes 5208fae3551SRodney W. Grimes /* 5218fae3551SRodney W. Grimes * Initialize a symbol table from a file 5228fae3551SRodney W. Grimes */ 5238fae3551SRodney W. Grimes void 5242db673abSWarner Losh initsymtable(char *filename) 5258fae3551SRodney W. Grimes { 5268fae3551SRodney W. Grimes char *base; 5278fae3551SRodney W. Grimes long tblsize; 5283d438ad6SDavid E. O'Brien struct entry *ep; 5298fae3551SRodney W. Grimes struct entry *baseep, *lep; 5308fae3551SRodney W. Grimes struct symtableheader hdr; 5318fae3551SRodney W. Grimes struct stat stbuf; 5323d438ad6SDavid E. O'Brien long i; 5338fae3551SRodney W. Grimes int fd; 5348fae3551SRodney W. Grimes 5358fae3551SRodney W. Grimes vprintf(stdout, "Initialize symbol table.\n"); 5368fae3551SRodney W. Grimes if (filename == NULL) { 5378fae3551SRodney W. Grimes entrytblsize = maxino / HASHFACTOR; 538*12527d70SPedro F. Giffuni entry = calloc((unsigned)entrytblsize, sizeof(struct entry *)); 539*12527d70SPedro F. Giffuni if (entry == NULL) 5408fae3551SRodney W. Grimes panic("no memory for entry table\n"); 5418fae3551SRodney W. Grimes ep = addentry(".", ROOTINO, NODE); 5428fae3551SRodney W. Grimes ep->e_flags |= NEW; 5438fae3551SRodney W. Grimes return; 5448fae3551SRodney W. Grimes } 5458fae3551SRodney W. Grimes if ((fd = open(filename, O_RDONLY, 0)) < 0) { 5468fae3551SRodney W. Grimes fprintf(stderr, "open: %s\n", strerror(errno)); 5478fae3551SRodney W. Grimes panic("cannot open symbol table file %s\n", filename); 5488fae3551SRodney W. Grimes } 5498fae3551SRodney W. Grimes if (fstat(fd, &stbuf) < 0) { 5508fae3551SRodney W. Grimes fprintf(stderr, "stat: %s\n", strerror(errno)); 5518fae3551SRodney W. Grimes panic("cannot stat symbol table file %s\n", filename); 5528fae3551SRodney W. Grimes } 5538fae3551SRodney W. Grimes tblsize = stbuf.st_size - sizeof(struct symtableheader); 5548fae3551SRodney W. Grimes base = calloc(sizeof(char), (unsigned)tblsize); 5558fae3551SRodney W. Grimes if (base == NULL) 5568fae3551SRodney W. Grimes panic("cannot allocate space for symbol table\n"); 5578fae3551SRodney W. Grimes if (read(fd, base, (int)tblsize) < 0 || 5588fae3551SRodney W. Grimes read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) { 5598fae3551SRodney W. Grimes fprintf(stderr, "read: %s\n", strerror(errno)); 5608fae3551SRodney W. Grimes panic("cannot read symbol table file %s\n", filename); 5618fae3551SRodney W. Grimes } 5628fae3551SRodney W. Grimes switch (command) { 5638fae3551SRodney W. Grimes case 'r': 5648fae3551SRodney W. Grimes /* 5658fae3551SRodney W. Grimes * For normal continuation, insure that we are using 5668fae3551SRodney W. Grimes * the next incremental tape 5678fae3551SRodney W. Grimes */ 5688fae3551SRodney W. Grimes if (hdr.dumpdate != dumptime) { 5698fae3551SRodney W. Grimes if (hdr.dumpdate < dumptime) 5708fae3551SRodney W. Grimes fprintf(stderr, "Incremental tape too low\n"); 5718fae3551SRodney W. Grimes else 5728fae3551SRodney W. Grimes fprintf(stderr, "Incremental tape too high\n"); 5738fae3551SRodney W. Grimes done(1); 5748fae3551SRodney W. Grimes } 5758fae3551SRodney W. Grimes break; 5768fae3551SRodney W. Grimes case 'R': 5778fae3551SRodney W. Grimes /* 5788fae3551SRodney W. Grimes * For restart, insure that we are using the same tape 5798fae3551SRodney W. Grimes */ 5808fae3551SRodney W. Grimes curfile.action = SKIP; 5818fae3551SRodney W. Grimes dumptime = hdr.dumptime; 5828fae3551SRodney W. Grimes dumpdate = hdr.dumpdate; 5838fae3551SRodney W. Grimes if (!bflag) 5848fae3551SRodney W. Grimes newtapebuf(hdr.ntrec); 5858fae3551SRodney W. Grimes getvol(hdr.volno); 5868fae3551SRodney W. Grimes break; 5878fae3551SRodney W. Grimes default: 5888fae3551SRodney W. Grimes panic("initsymtable called from command %c\n", command); 5898fae3551SRodney W. Grimes break; 5908fae3551SRodney W. Grimes } 5918fae3551SRodney W. Grimes maxino = hdr.maxino; 5928fae3551SRodney W. Grimes entrytblsize = hdr.entrytblsize; 5938fae3551SRodney W. Grimes entry = (struct entry **) 5948fae3551SRodney W. Grimes (base + tblsize - (entrytblsize * sizeof(struct entry *))); 5958fae3551SRodney W. Grimes baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry)); 5968fae3551SRodney W. Grimes lep = (struct entry *)entry; 5978fae3551SRodney W. Grimes for (i = 0; i < entrytblsize; i++) { 5988fae3551SRodney W. Grimes if (entry[i] == NULL) 5998fae3551SRodney W. Grimes continue; 6008fae3551SRodney W. Grimes entry[i] = &baseep[(long)entry[i]]; 6018fae3551SRodney W. Grimes } 6028fae3551SRodney W. Grimes for (ep = &baseep[1]; ep < lep; ep++) { 6038fae3551SRodney W. Grimes ep->e_name = base + (long)ep->e_name; 6048fae3551SRodney W. Grimes ep->e_parent = &baseep[(long)ep->e_parent]; 6058fae3551SRodney W. Grimes if (ep->e_sibling != NULL) 6068fae3551SRodney W. Grimes ep->e_sibling = &baseep[(long)ep->e_sibling]; 6078fae3551SRodney W. Grimes if (ep->e_links != NULL) 6088fae3551SRodney W. Grimes ep->e_links = &baseep[(long)ep->e_links]; 6098fae3551SRodney W. Grimes if (ep->e_entries != NULL) 6108fae3551SRodney W. Grimes ep->e_entries = &baseep[(long)ep->e_entries]; 6118fae3551SRodney W. Grimes if (ep->e_next != NULL) 6128fae3551SRodney W. Grimes ep->e_next = &baseep[(long)ep->e_next]; 6138fae3551SRodney W. Grimes } 6148fae3551SRodney W. Grimes } 615