1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
48fae3551SRodney W. Grimes * Copyright (c) 1983, 1993
58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved.
68fae3551SRodney W. Grimes *
78fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes * are met:
108fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes * without specific prior written permission.
188fae3551SRodney W. Grimes *
198fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes * SUCH DAMAGE.
308fae3551SRodney W. Grimes */
318fae3551SRodney W. Grimes
328fae3551SRodney W. Grimes /*
338fae3551SRodney W. Grimes * These routines maintain the symbol table which tracks the state
348fae3551SRodney W. Grimes * of the file system being restored. They provide lookup by either
358fae3551SRodney W. Grimes * name or inode number. They also provide for creation, deletion,
368fae3551SRodney W. Grimes * and renaming of entries. Because of the dynamic nature of pathnames,
378fae3551SRodney W. Grimes * names should not be saved, but always constructed just before they
388fae3551SRodney W. Grimes * are needed, by calling "myname".
398fae3551SRodney W. Grimes */
408fae3551SRodney W. Grimes
418fae3551SRodney W. Grimes #include <sys/param.h>
428fae3551SRodney W. Grimes #include <sys/stat.h>
438fae3551SRodney W. Grimes
448fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
458fae3551SRodney W. Grimes
468fae3551SRodney W. Grimes #include <errno.h>
478fae3551SRodney W. Grimes #include <fcntl.h>
4889fdc4e1SMike Barcroft #include <limits.h>
49e25a029eSMatthew D Fleming #include <stdint.h>
508fae3551SRodney W. Grimes #include <stdio.h>
518fae3551SRodney W. Grimes #include <stdlib.h>
528fae3551SRodney W. Grimes #include <string.h>
538fae3551SRodney W. Grimes #include <unistd.h>
548fae3551SRodney W. Grimes
558fae3551SRodney W. Grimes #include "restore.h"
568fae3551SRodney W. Grimes #include "extern.h"
578fae3551SRodney W. Grimes
588fae3551SRodney W. Grimes /*
598fae3551SRodney W. Grimes * The following variables define the inode symbol table.
608fae3551SRodney W. Grimes * The primary hash table is dynamically allocated based on
618fae3551SRodney W. Grimes * the number of inodes in the file system (maxino), scaled by
628fae3551SRodney W. Grimes * HASHFACTOR. The variable "entry" points to the hash table;
638fae3551SRodney W. Grimes * the variable "entrytblsize" indicates its size (in entries).
648fae3551SRodney W. Grimes */
658fae3551SRodney W. Grimes #define HASHFACTOR 5
668fae3551SRodney W. Grimes static struct entry **entry;
678fae3551SRodney W. Grimes static long entrytblsize;
688fae3551SRodney W. Grimes
692db673abSWarner Losh static void addino(ino_t, struct entry *);
702db673abSWarner Losh static struct entry *lookupparent(char *);
712db673abSWarner Losh static void removeentry(struct entry *);
728fae3551SRodney W. Grimes
738fae3551SRodney W. Grimes /*
748fae3551SRodney W. Grimes * Look up an entry by inode number
758fae3551SRodney W. Grimes */
768fae3551SRodney W. Grimes struct entry *
lookupino(ino_t inum)772db673abSWarner Losh lookupino(ino_t inum)
788fae3551SRodney W. Grimes {
793d438ad6SDavid E. O'Brien struct entry *ep;
808fae3551SRodney W. Grimes
811dc349abSEd Maste if (inum < UFS_WINO || inum >= maxino)
828fae3551SRodney W. Grimes return (NULL);
838fae3551SRodney W. Grimes for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next)
848fae3551SRodney W. Grimes if (ep->e_ino == inum)
858fae3551SRodney W. Grimes return (ep);
868fae3551SRodney W. Grimes return (NULL);
878fae3551SRodney W. Grimes }
888fae3551SRodney W. Grimes
898fae3551SRodney W. Grimes /*
908fae3551SRodney W. Grimes * Add an entry into the entry table
918fae3551SRodney W. Grimes */
928fae3551SRodney W. Grimes static void
addino(ino_t inum,struct entry * np)932db673abSWarner Losh addino(ino_t inum, struct entry *np)
948fae3551SRodney W. Grimes {
958fae3551SRodney W. Grimes struct entry **epp;
968fae3551SRodney W. Grimes
971dc349abSEd Maste if (inum < UFS_WINO || inum >= maxino)
98e25a029eSMatthew D Fleming panic("addino: out of range %ju\n", (uintmax_t)inum);
998fae3551SRodney W. Grimes epp = &entry[inum % entrytblsize];
1008fae3551SRodney W. Grimes np->e_ino = inum;
1018fae3551SRodney W. Grimes np->e_next = *epp;
1028fae3551SRodney W. Grimes *epp = np;
1038fae3551SRodney W. Grimes if (dflag)
1048fae3551SRodney W. Grimes for (np = np->e_next; np != NULL; np = np->e_next)
1058fae3551SRodney W. Grimes if (np->e_ino == inum)
1068fae3551SRodney W. Grimes badentry(np, "duplicate inum");
1078fae3551SRodney W. Grimes }
1088fae3551SRodney W. Grimes
1098fae3551SRodney W. Grimes /*
1108fae3551SRodney W. Grimes * Delete an entry from the entry table
1118fae3551SRodney W. Grimes */
1128fae3551SRodney W. Grimes void
deleteino(ino_t inum)1132db673abSWarner Losh deleteino(ino_t inum)
1148fae3551SRodney W. Grimes {
1153d438ad6SDavid E. O'Brien struct entry *next;
1168fae3551SRodney W. Grimes struct entry **prev;
1178fae3551SRodney W. Grimes
1181dc349abSEd Maste if (inum < UFS_WINO || inum >= maxino)
119e25a029eSMatthew D Fleming panic("deleteino: out of range %ju\n", (uintmax_t)inum);
1208fae3551SRodney W. Grimes prev = &entry[inum % entrytblsize];
1218fae3551SRodney W. Grimes for (next = *prev; next != NULL; next = next->e_next) {
1228fae3551SRodney W. Grimes if (next->e_ino == inum) {
1238fae3551SRodney W. Grimes next->e_ino = 0;
1248fae3551SRodney W. Grimes *prev = next->e_next;
1258fae3551SRodney W. Grimes return;
1268fae3551SRodney W. Grimes }
1278fae3551SRodney W. Grimes prev = &next->e_next;
1288fae3551SRodney W. Grimes }
129e25a029eSMatthew D Fleming panic("deleteino: %ju not found\n", (uintmax_t)inum);
1308fae3551SRodney W. Grimes }
1318fae3551SRodney W. Grimes
1328fae3551SRodney W. Grimes /*
1338fae3551SRodney W. Grimes * Look up an entry by name
1348fae3551SRodney W. Grimes */
1358fae3551SRodney W. Grimes struct entry *
lookupname(char * name)1362db673abSWarner Losh lookupname(char *name)
1378fae3551SRodney W. Grimes {
1383d438ad6SDavid E. O'Brien struct entry *ep;
1393d438ad6SDavid E. O'Brien char *np, *cp;
1408fae3551SRodney W. Grimes char buf[MAXPATHLEN];
1418fae3551SRodney W. Grimes
1428fae3551SRodney W. Grimes cp = name;
1431dc349abSEd Maste for (ep = lookupino(UFS_ROOTINO); ep != NULL; ep = ep->e_entries) {
144b94f03b2SEivind Eklund for (np = buf; *cp != '/' && *cp != '\0' &&
145b94f03b2SEivind Eklund np < &buf[sizeof(buf)]; )
1468fae3551SRodney W. Grimes *np++ = *cp++;
147b94f03b2SEivind Eklund if (np == &buf[sizeof(buf)])
148b94f03b2SEivind Eklund break;
1498fae3551SRodney W. Grimes *np = '\0';
1508fae3551SRodney W. Grimes for ( ; ep != NULL; ep = ep->e_sibling)
1518fae3551SRodney W. Grimes if (strcmp(ep->e_name, buf) == 0)
1528fae3551SRodney W. Grimes break;
1538fae3551SRodney W. Grimes if (ep == NULL)
1548fae3551SRodney W. Grimes break;
1558fae3551SRodney W. Grimes if (*cp++ == '\0')
1568fae3551SRodney W. Grimes return (ep);
1578fae3551SRodney W. Grimes }
1588fae3551SRodney W. Grimes return (NULL);
1598fae3551SRodney W. Grimes }
1608fae3551SRodney W. Grimes
1618fae3551SRodney W. Grimes /*
1628fae3551SRodney W. Grimes * Look up the parent of a pathname
1638fae3551SRodney W. Grimes */
1648fae3551SRodney W. Grimes static struct entry *
lookupparent(char * name)1652db673abSWarner Losh lookupparent(char *name)
1668fae3551SRodney W. Grimes {
1678fae3551SRodney W. Grimes struct entry *ep;
1688fae3551SRodney W. Grimes char *tailindex;
1698fae3551SRodney W. Grimes
170d87d79aeSPeter Wemm tailindex = strrchr(name, '/');
1718fae3551SRodney W. Grimes if (tailindex == NULL)
1728fae3551SRodney W. Grimes return (NULL);
1738fae3551SRodney W. Grimes *tailindex = '\0';
1748fae3551SRodney W. Grimes ep = lookupname(name);
1758fae3551SRodney W. Grimes *tailindex = '/';
1768fae3551SRodney W. Grimes if (ep == NULL)
1778fae3551SRodney W. Grimes return (NULL);
1788fae3551SRodney W. Grimes if (ep->e_type != NODE)
1798fae3551SRodney W. Grimes panic("%s is not a directory\n", name);
1808fae3551SRodney W. Grimes return (ep);
1818fae3551SRodney W. Grimes }
1828fae3551SRodney W. Grimes
1838fae3551SRodney W. Grimes /*
1848fae3551SRodney W. Grimes * Determine the current pathname of a node or leaf
1858fae3551SRodney W. Grimes */
1868fae3551SRodney W. Grimes char *
myname(struct entry * ep)1872db673abSWarner Losh myname(struct entry *ep)
1888fae3551SRodney W. Grimes {
1893d438ad6SDavid E. O'Brien char *cp;
1908fae3551SRodney W. Grimes static char namebuf[MAXPATHLEN];
1918fae3551SRodney W. Grimes
1928fae3551SRodney W. Grimes for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) {
1938fae3551SRodney W. Grimes cp -= ep->e_namlen;
194d87d79aeSPeter Wemm memmove(cp, ep->e_name, (long)ep->e_namlen);
1951dc349abSEd Maste if (ep == lookupino(UFS_ROOTINO))
1968fae3551SRodney W. Grimes return (cp);
1978fae3551SRodney W. Grimes *(--cp) = '/';
1988fae3551SRodney W. Grimes ep = ep->e_parent;
1998fae3551SRodney W. Grimes }
2008fae3551SRodney W. Grimes panic("%s: pathname too long\n", cp);
2018fae3551SRodney W. Grimes return(cp);
2028fae3551SRodney W. Grimes }
2038fae3551SRodney W. Grimes
2048fae3551SRodney W. Grimes /*
2058fae3551SRodney W. Grimes * Unused symbol table entries are linked together on a free list
2068fae3551SRodney W. Grimes * headed by the following pointer.
2078fae3551SRodney W. Grimes */
2088fae3551SRodney W. Grimes static struct entry *freelist = NULL;
2098fae3551SRodney W. Grimes
2108fae3551SRodney W. Grimes /*
2118fae3551SRodney W. Grimes * add an entry to the symbol table
2128fae3551SRodney W. Grimes */
2138fae3551SRodney W. Grimes struct entry *
addentry(char * name,ino_t inum,int type)2142db673abSWarner Losh addentry(char *name, ino_t inum, int type)
2158fae3551SRodney W. Grimes {
2163d438ad6SDavid E. O'Brien struct entry *np, *ep;
2178fae3551SRodney W. Grimes
2188fae3551SRodney W. Grimes if (freelist != NULL) {
2198fae3551SRodney W. Grimes np = freelist;
2208fae3551SRodney W. Grimes freelist = np->e_next;
221d87d79aeSPeter Wemm memset(np, 0, (long)sizeof(struct entry));
2228fae3551SRodney W. Grimes } else {
2238fae3551SRodney W. Grimes np = (struct entry *)calloc(1, sizeof(struct entry));
2248fae3551SRodney W. Grimes if (np == NULL)
2258fae3551SRodney W. Grimes panic("no memory to extend symbol table\n");
2268fae3551SRodney W. Grimes }
2278fae3551SRodney W. Grimes np->e_type = type & ~LINK;
2288fae3551SRodney W. Grimes ep = lookupparent(name);
2298fae3551SRodney W. Grimes if (ep == NULL) {
2301dc349abSEd Maste if (inum != UFS_ROOTINO || lookupino(UFS_ROOTINO) != NULL)
2318fae3551SRodney W. Grimes panic("bad name to addentry %s\n", name);
2328fae3551SRodney W. Grimes np->e_name = savename(name);
2338fae3551SRodney W. Grimes np->e_namlen = strlen(name);
2348fae3551SRodney W. Grimes np->e_parent = np;
2351dc349abSEd Maste addino(UFS_ROOTINO, np);
2368fae3551SRodney W. Grimes return (np);
2378fae3551SRodney W. Grimes }
238d87d79aeSPeter Wemm np->e_name = savename(strrchr(name, '/') + 1);
2398fae3551SRodney W. Grimes np->e_namlen = strlen(np->e_name);
2408fae3551SRodney W. Grimes np->e_parent = ep;
2418fae3551SRodney W. Grimes np->e_sibling = ep->e_entries;
2428fae3551SRodney W. Grimes ep->e_entries = np;
2438fae3551SRodney W. Grimes if (type & LINK) {
2448fae3551SRodney W. Grimes ep = lookupino(inum);
2458fae3551SRodney W. Grimes if (ep == NULL)
24691ac32e4SPhilippe Charnier panic("link to non-existent name\n");
2478fae3551SRodney W. Grimes np->e_ino = inum;
2488fae3551SRodney W. Grimes np->e_links = ep->e_links;
2498fae3551SRodney W. Grimes ep->e_links = np;
2508fae3551SRodney W. Grimes } else if (inum != 0) {
2518fae3551SRodney W. Grimes if (lookupino(inum) != NULL)
2528fae3551SRodney W. Grimes panic("duplicate entry\n");
2538fae3551SRodney W. Grimes addino(inum, np);
2548fae3551SRodney W. Grimes }
2558fae3551SRodney W. Grimes return (np);
2568fae3551SRodney W. Grimes }
2578fae3551SRodney W. Grimes
2588fae3551SRodney W. Grimes /*
2598fae3551SRodney W. Grimes * delete an entry from the symbol table
2608fae3551SRodney W. Grimes */
2618fae3551SRodney W. Grimes void
freeentry(struct entry * ep)2622db673abSWarner Losh freeentry(struct entry *ep)
2638fae3551SRodney W. Grimes {
2643d438ad6SDavid E. O'Brien struct entry *np;
2658fae3551SRodney W. Grimes ino_t inum;
2668fae3551SRodney W. Grimes
2678fae3551SRodney W. Grimes if (ep->e_flags != REMOVED)
2688fae3551SRodney W. Grimes badentry(ep, "not marked REMOVED");
2698fae3551SRodney W. Grimes if (ep->e_type == NODE) {
2708fae3551SRodney W. Grimes if (ep->e_links != NULL)
2718fae3551SRodney W. Grimes badentry(ep, "freeing referenced directory");
2728fae3551SRodney W. Grimes if (ep->e_entries != NULL)
2738fae3551SRodney W. Grimes badentry(ep, "freeing non-empty directory");
2748fae3551SRodney W. Grimes }
2758fae3551SRodney W. Grimes if (ep->e_ino != 0) {
2768fae3551SRodney W. Grimes np = lookupino(ep->e_ino);
2778fae3551SRodney W. Grimes if (np == NULL)
2788fae3551SRodney W. Grimes badentry(ep, "lookupino failed");
2798fae3551SRodney W. Grimes if (np == ep) {
2808fae3551SRodney W. Grimes inum = ep->e_ino;
2818fae3551SRodney W. Grimes deleteino(inum);
2828fae3551SRodney W. Grimes if (ep->e_links != NULL)
2838fae3551SRodney W. Grimes addino(inum, ep->e_links);
2848fae3551SRodney W. Grimes } else {
2858fae3551SRodney W. Grimes for (; np != NULL; np = np->e_links) {
2868fae3551SRodney W. Grimes if (np->e_links == ep) {
2878fae3551SRodney W. Grimes np->e_links = ep->e_links;
2888fae3551SRodney W. Grimes break;
2898fae3551SRodney W. Grimes }
2908fae3551SRodney W. Grimes }
2918fae3551SRodney W. Grimes if (np == NULL)
2928fae3551SRodney W. Grimes badentry(ep, "link not found");
2938fae3551SRodney W. Grimes }
2948fae3551SRodney W. Grimes }
2958fae3551SRodney W. Grimes removeentry(ep);
2968fae3551SRodney W. Grimes freename(ep->e_name);
2978fae3551SRodney W. Grimes ep->e_next = freelist;
2988fae3551SRodney W. Grimes freelist = ep;
2998fae3551SRodney W. Grimes }
3008fae3551SRodney W. Grimes
3018fae3551SRodney W. Grimes /*
3028fae3551SRodney W. Grimes * Relocate an entry in the tree structure
3038fae3551SRodney W. Grimes */
3048fae3551SRodney W. Grimes void
moveentry(struct entry * ep,char * newname)3052db673abSWarner Losh moveentry(struct entry *ep, char *newname)
3068fae3551SRodney W. Grimes {
3078fae3551SRodney W. Grimes struct entry *np;
3088fae3551SRodney W. Grimes char *cp;
3098fae3551SRodney W. Grimes
3108fae3551SRodney W. Grimes np = lookupparent(newname);
3118fae3551SRodney W. Grimes if (np == NULL)
3128fae3551SRodney W. Grimes badentry(ep, "cannot move ROOT");
3138fae3551SRodney W. Grimes if (np != ep->e_parent) {
3148fae3551SRodney W. Grimes removeentry(ep);
3158fae3551SRodney W. Grimes ep->e_parent = np;
3168fae3551SRodney W. Grimes ep->e_sibling = np->e_entries;
3178fae3551SRodney W. Grimes np->e_entries = ep;
3188fae3551SRodney W. Grimes }
319d87d79aeSPeter Wemm cp = strrchr(newname, '/') + 1;
3208fae3551SRodney W. Grimes freename(ep->e_name);
3218fae3551SRodney W. Grimes ep->e_name = savename(cp);
3228fae3551SRodney W. Grimes ep->e_namlen = strlen(cp);
3238fae3551SRodney W. Grimes if (strcmp(gentempname(ep), ep->e_name) == 0)
3248fae3551SRodney W. Grimes ep->e_flags |= TMPNAME;
3258fae3551SRodney W. Grimes else
3268fae3551SRodney W. Grimes ep->e_flags &= ~TMPNAME;
3278fae3551SRodney W. Grimes }
3288fae3551SRodney W. Grimes
3298fae3551SRodney W. Grimes /*
3308fae3551SRodney W. Grimes * Remove an entry in the tree structure
3318fae3551SRodney W. Grimes */
3328fae3551SRodney W. Grimes static void
removeentry(struct entry * ep)3332db673abSWarner Losh removeentry(struct entry *ep)
3348fae3551SRodney W. Grimes {
3353d438ad6SDavid E. O'Brien struct entry *np;
3368fae3551SRodney W. Grimes
3378fae3551SRodney W. Grimes np = ep->e_parent;
3388fae3551SRodney W. Grimes if (np->e_entries == ep) {
3398fae3551SRodney W. Grimes np->e_entries = ep->e_sibling;
3408fae3551SRodney W. Grimes } else {
3418fae3551SRodney W. Grimes for (np = np->e_entries; np != NULL; np = np->e_sibling) {
3428fae3551SRodney W. Grimes if (np->e_sibling == ep) {
3438fae3551SRodney W. Grimes np->e_sibling = ep->e_sibling;
3448fae3551SRodney W. Grimes break;
3458fae3551SRodney W. Grimes }
3468fae3551SRodney W. Grimes }
3478fae3551SRodney W. Grimes if (np == NULL)
3488fae3551SRodney W. Grimes badentry(ep, "cannot find entry in parent list");
3498fae3551SRodney W. Grimes }
3508fae3551SRodney W. Grimes }
3518fae3551SRodney W. Grimes
3528fae3551SRodney W. Grimes /*
3538fae3551SRodney W. Grimes * Table of unused string entries, sorted by length.
3548fae3551SRodney W. Grimes *
3558fae3551SRodney W. Grimes * Entries are allocated in STRTBLINCR sized pieces so that names
3568fae3551SRodney W. Grimes * of similar lengths can use the same entry. The value of STRTBLINCR
3578fae3551SRodney W. Grimes * is chosen so that every entry has at least enough space to hold
3588fae3551SRodney W. Grimes * a "struct strtbl" header. Thus every entry can be linked onto an
35991ac32e4SPhilippe Charnier * appropriate free list.
3608fae3551SRodney W. Grimes *
3618fae3551SRodney W. Grimes * NB. The macro "allocsize" below assumes that "struct strhdr"
3628fae3551SRodney W. Grimes * has a size that is a power of two.
3638fae3551SRodney W. Grimes */
3648fae3551SRodney W. Grimes struct strhdr {
3658fae3551SRodney W. Grimes struct strhdr *next;
3668fae3551SRodney W. Grimes };
3678fae3551SRodney W. Grimes
3688fae3551SRodney W. Grimes #define STRTBLINCR (sizeof(struct strhdr))
36905cfc40aSPedro F. Giffuni #define allocsize(size) roundup2((size) + 1, STRTBLINCR)
3708fae3551SRodney W. Grimes
3718fae3551SRodney W. Grimes static struct strhdr strtblhdr[allocsize(NAME_MAX) / STRTBLINCR];
3728fae3551SRodney W. Grimes
3738fae3551SRodney W. Grimes /*
3748fae3551SRodney W. Grimes * Allocate space for a name. It first looks to see if it already
3758fae3551SRodney W. Grimes * has an appropriate sized entry, and if not allocates a new one.
3768fae3551SRodney W. Grimes */
3778fae3551SRodney W. Grimes char *
savename(char * name)3782db673abSWarner Losh savename(char *name)
3798fae3551SRodney W. Grimes {
3808fae3551SRodney W. Grimes struct strhdr *np;
38105cfc40aSPedro F. Giffuni size_t len;
3828fae3551SRodney W. Grimes char *cp;
3838fae3551SRodney W. Grimes
3848fae3551SRodney W. Grimes if (name == NULL)
3858fae3551SRodney W. Grimes panic("bad name\n");
3868fae3551SRodney W. Grimes len = strlen(name);
3878fae3551SRodney W. Grimes np = strtblhdr[len / STRTBLINCR].next;
3888fae3551SRodney W. Grimes if (np != NULL) {
3898fae3551SRodney W. Grimes strtblhdr[len / STRTBLINCR].next = np->next;
3908fae3551SRodney W. Grimes cp = (char *)np;
3918fae3551SRodney W. Grimes } else {
39205cfc40aSPedro F. Giffuni cp = malloc(allocsize(len));
3938fae3551SRodney W. Grimes if (cp == NULL)
3948fae3551SRodney W. Grimes panic("no space for string table\n");
3958fae3551SRodney W. Grimes }
3968fae3551SRodney W. Grimes (void) strcpy(cp, name);
3978fae3551SRodney W. Grimes return (cp);
3988fae3551SRodney W. Grimes }
3998fae3551SRodney W. Grimes
4008fae3551SRodney W. Grimes /*
4018fae3551SRodney W. Grimes * Free space for a name. The resulting entry is linked onto the
4028fae3551SRodney W. Grimes * appropriate free list.
4038fae3551SRodney W. Grimes */
4048fae3551SRodney W. Grimes void
freename(char * name)4052db673abSWarner Losh freename(char *name)
4068fae3551SRodney W. Grimes {
4078fae3551SRodney W. Grimes struct strhdr *tp, *np;
4088fae3551SRodney W. Grimes
4098fae3551SRodney W. Grimes tp = &strtblhdr[strlen(name) / STRTBLINCR];
4108fae3551SRodney W. Grimes np = (struct strhdr *)name;
4118fae3551SRodney W. Grimes np->next = tp->next;
4128fae3551SRodney W. Grimes tp->next = np;
4138fae3551SRodney W. Grimes }
4148fae3551SRodney W. Grimes
4158fae3551SRodney W. Grimes /*
4168fae3551SRodney W. Grimes * Useful quantities placed at the end of a dumped symbol table.
4178fae3551SRodney W. Grimes */
4188fae3551SRodney W. Grimes struct symtableheader {
41997b465b1SDima Ruban int32_t volno;
42097b465b1SDima Ruban int32_t stringsize;
42197b465b1SDima Ruban int32_t entrytblsize;
4228fae3551SRodney W. Grimes time_t dumptime;
4238fae3551SRodney W. Grimes time_t dumpdate;
4248fae3551SRodney W. Grimes ino_t maxino;
42597b465b1SDima Ruban int32_t ntrec;
4268fae3551SRodney W. Grimes };
4278fae3551SRodney W. Grimes
4288fae3551SRodney W. Grimes /*
4298fae3551SRodney W. Grimes * dump a snapshot of the symbol table
4308fae3551SRodney W. Grimes */
4318fae3551SRodney W. Grimes void
dumpsymtable(char * filename,long checkpt)4322db673abSWarner Losh dumpsymtable(char *filename, long checkpt)
4338fae3551SRodney W. Grimes {
4343d438ad6SDavid E. O'Brien struct entry *ep, *tep;
4353d438ad6SDavid E. O'Brien ino_t i;
4368fae3551SRodney W. Grimes struct entry temp, *tentry;
4378fae3551SRodney W. Grimes long mynum = 1, stroff = 0;
4388fae3551SRodney W. Grimes FILE *fd;
4398fae3551SRodney W. Grimes struct symtableheader hdr;
4408fae3551SRodney W. Grimes
4418fae3551SRodney W. Grimes vprintf(stdout, "Checkpointing the restore\n");
4428fae3551SRodney W. Grimes if (Nflag)
4438fae3551SRodney W. Grimes return;
4448fae3551SRodney W. Grimes if ((fd = fopen(filename, "w")) == NULL) {
4458fae3551SRodney W. Grimes fprintf(stderr, "fopen: %s\n", strerror(errno));
4468fae3551SRodney W. Grimes panic("cannot create save file %s for symbol table\n",
4478fae3551SRodney W. Grimes filename);
448646d3727SAnton Berezin done(1);
4498fae3551SRodney W. Grimes }
4508fae3551SRodney W. Grimes clearerr(fd);
4518fae3551SRodney W. Grimes /*
45291ac32e4SPhilippe Charnier * Assign indices to each entry
4538fae3551SRodney W. Grimes * Write out the string entries
4548fae3551SRodney W. Grimes */
4551dc349abSEd Maste for (i = UFS_WINO; i <= maxino; i++) {
4568fae3551SRodney W. Grimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
4578fae3551SRodney W. Grimes ep->e_index = mynum++;
4588fae3551SRodney W. Grimes (void) fwrite(ep->e_name, sizeof(char),
4598fae3551SRodney W. Grimes (int)allocsize(ep->e_namlen), fd);
4608fae3551SRodney W. Grimes }
4618fae3551SRodney W. Grimes }
4628fae3551SRodney W. Grimes /*
4638fae3551SRodney W. Grimes * Convert pointers to indexes, and output
4648fae3551SRodney W. Grimes */
4658fae3551SRodney W. Grimes tep = &temp;
4668fae3551SRodney W. Grimes stroff = 0;
4671dc349abSEd Maste for (i = UFS_WINO; i <= maxino; i++) {
4688fae3551SRodney W. Grimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
469d87d79aeSPeter Wemm memmove(tep, ep, (long)sizeof(struct entry));
4708fae3551SRodney W. Grimes tep->e_name = (char *)stroff;
4718fae3551SRodney W. Grimes stroff += allocsize(ep->e_namlen);
4728fae3551SRodney W. Grimes tep->e_parent = (struct entry *)ep->e_parent->e_index;
4738fae3551SRodney W. Grimes if (ep->e_links != NULL)
4748fae3551SRodney W. Grimes tep->e_links =
4758fae3551SRodney W. Grimes (struct entry *)ep->e_links->e_index;
4768fae3551SRodney W. Grimes if (ep->e_sibling != NULL)
4778fae3551SRodney W. Grimes tep->e_sibling =
4788fae3551SRodney W. Grimes (struct entry *)ep->e_sibling->e_index;
4798fae3551SRodney W. Grimes if (ep->e_entries != NULL)
4808fae3551SRodney W. Grimes tep->e_entries =
4818fae3551SRodney W. Grimes (struct entry *)ep->e_entries->e_index;
4828fae3551SRodney W. Grimes if (ep->e_next != NULL)
4838fae3551SRodney W. Grimes tep->e_next =
4848fae3551SRodney W. Grimes (struct entry *)ep->e_next->e_index;
4858fae3551SRodney W. Grimes (void) fwrite((char *)tep, sizeof(struct entry), 1, fd);
4868fae3551SRodney W. Grimes }
4878fae3551SRodney W. Grimes }
4888fae3551SRodney W. Grimes /*
4898fae3551SRodney W. Grimes * Convert entry pointers to indexes, and output
4908fae3551SRodney W. Grimes */
4918fae3551SRodney W. Grimes for (i = 0; i < entrytblsize; i++) {
4928fae3551SRodney W. Grimes if (entry[i] == NULL)
4938fae3551SRodney W. Grimes tentry = NULL;
4948fae3551SRodney W. Grimes else
4958fae3551SRodney W. Grimes tentry = (struct entry *)entry[i]->e_index;
4968fae3551SRodney W. Grimes (void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd);
4978fae3551SRodney W. Grimes }
4988fae3551SRodney W. Grimes hdr.volno = checkpt;
4998fae3551SRodney W. Grimes hdr.maxino = maxino;
5008fae3551SRodney W. Grimes hdr.entrytblsize = entrytblsize;
5018fae3551SRodney W. Grimes hdr.stringsize = stroff;
5028fae3551SRodney W. Grimes hdr.dumptime = dumptime;
5038fae3551SRodney W. Grimes hdr.dumpdate = dumpdate;
5048fae3551SRodney W. Grimes hdr.ntrec = ntrec;
5058fae3551SRodney W. Grimes (void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd);
5068fae3551SRodney W. Grimes if (ferror(fd)) {
5078fae3551SRodney W. Grimes fprintf(stderr, "fwrite: %s\n", strerror(errno));
5088fae3551SRodney W. Grimes panic("output error to file %s writing symbol table\n",
5098fae3551SRodney W. Grimes filename);
5108fae3551SRodney W. Grimes }
5118fae3551SRodney W. Grimes (void) fclose(fd);
5128fae3551SRodney W. Grimes }
5138fae3551SRodney W. Grimes
5148fae3551SRodney W. Grimes /*
5158fae3551SRodney W. Grimes * Initialize a symbol table from a file
5168fae3551SRodney W. Grimes */
5178fae3551SRodney W. Grimes void
initsymtable(char * filename)5182db673abSWarner Losh initsymtable(char *filename)
5198fae3551SRodney W. Grimes {
5208fae3551SRodney W. Grimes char *base;
5218fae3551SRodney W. Grimes long tblsize;
5223d438ad6SDavid E. O'Brien struct entry *ep;
5238fae3551SRodney W. Grimes struct entry *baseep, *lep;
5248fae3551SRodney W. Grimes struct symtableheader hdr;
5258fae3551SRodney W. Grimes struct stat stbuf;
5263d438ad6SDavid E. O'Brien long i;
5278fae3551SRodney W. Grimes int fd;
5288fae3551SRodney W. Grimes
5298fae3551SRodney W. Grimes vprintf(stdout, "Initialize symbol table.\n");
5308fae3551SRodney W. Grimes if (filename == NULL) {
5318fae3551SRodney W. Grimes entrytblsize = maxino / HASHFACTOR;
53212527d70SPedro F. Giffuni entry = calloc((unsigned)entrytblsize, sizeof(struct entry *));
53312527d70SPedro F. Giffuni if (entry == NULL)
5348fae3551SRodney W. Grimes panic("no memory for entry table\n");
5351dc349abSEd Maste ep = addentry(".", UFS_ROOTINO, NODE);
5368fae3551SRodney W. Grimes ep->e_flags |= NEW;
5378fae3551SRodney W. Grimes return;
5388fae3551SRodney W. Grimes }
5398fae3551SRodney W. Grimes if ((fd = open(filename, O_RDONLY, 0)) < 0) {
5408fae3551SRodney W. Grimes fprintf(stderr, "open: %s\n", strerror(errno));
5418fae3551SRodney W. Grimes panic("cannot open symbol table file %s\n", filename);
5428fae3551SRodney W. Grimes }
5438fae3551SRodney W. Grimes if (fstat(fd, &stbuf) < 0) {
5448fae3551SRodney W. Grimes fprintf(stderr, "stat: %s\n", strerror(errno));
5458fae3551SRodney W. Grimes panic("cannot stat symbol table file %s\n", filename);
5468fae3551SRodney W. Grimes }
5478fae3551SRodney W. Grimes tblsize = stbuf.st_size - sizeof(struct symtableheader);
5488fae3551SRodney W. Grimes base = calloc(sizeof(char), (unsigned)tblsize);
5498fae3551SRodney W. Grimes if (base == NULL)
5508fae3551SRodney W. Grimes panic("cannot allocate space for symbol table\n");
5518fae3551SRodney W. Grimes if (read(fd, base, (int)tblsize) < 0 ||
5528fae3551SRodney W. Grimes read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) {
5538fae3551SRodney W. Grimes fprintf(stderr, "read: %s\n", strerror(errno));
5548fae3551SRodney W. Grimes panic("cannot read symbol table file %s\n", filename);
5558fae3551SRodney W. Grimes }
55624229eebSPedro F. Giffuni (void)close(fd);
5578fae3551SRodney W. Grimes switch (command) {
5588fae3551SRodney W. Grimes case 'r':
5598fae3551SRodney W. Grimes /*
5608fae3551SRodney W. Grimes * For normal continuation, insure that we are using
5618fae3551SRodney W. Grimes * the next incremental tape
5628fae3551SRodney W. Grimes */
5638fae3551SRodney W. Grimes if (hdr.dumpdate != dumptime) {
5648fae3551SRodney W. Grimes if (hdr.dumpdate < dumptime)
5658fae3551SRodney W. Grimes fprintf(stderr, "Incremental tape too low\n");
5668fae3551SRodney W. Grimes else
5678fae3551SRodney W. Grimes fprintf(stderr, "Incremental tape too high\n");
5688fae3551SRodney W. Grimes done(1);
5698fae3551SRodney W. Grimes }
5708fae3551SRodney W. Grimes break;
5718fae3551SRodney W. Grimes case 'R':
5728fae3551SRodney W. Grimes /*
5738fae3551SRodney W. Grimes * For restart, insure that we are using the same tape
5748fae3551SRodney W. Grimes */
5758fae3551SRodney W. Grimes curfile.action = SKIP;
5768fae3551SRodney W. Grimes dumptime = hdr.dumptime;
5778fae3551SRodney W. Grimes dumpdate = hdr.dumpdate;
5788fae3551SRodney W. Grimes if (!bflag)
5798fae3551SRodney W. Grimes newtapebuf(hdr.ntrec);
5808fae3551SRodney W. Grimes getvol(hdr.volno);
5818fae3551SRodney W. Grimes break;
5828fae3551SRodney W. Grimes default:
5838fae3551SRodney W. Grimes panic("initsymtable called from command %c\n", command);
5848fae3551SRodney W. Grimes break;
5858fae3551SRodney W. Grimes }
5868fae3551SRodney W. Grimes maxino = hdr.maxino;
5878fae3551SRodney W. Grimes entrytblsize = hdr.entrytblsize;
5888fae3551SRodney W. Grimes entry = (struct entry **)
5898fae3551SRodney W. Grimes (base + tblsize - (entrytblsize * sizeof(struct entry *)));
5908fae3551SRodney W. Grimes baseep = (struct entry *)(base + hdr.stringsize - sizeof(struct entry));
5918fae3551SRodney W. Grimes lep = (struct entry *)entry;
5928fae3551SRodney W. Grimes for (i = 0; i < entrytblsize; i++) {
5938fae3551SRodney W. Grimes if (entry[i] == NULL)
5948fae3551SRodney W. Grimes continue;
5958fae3551SRodney W. Grimes entry[i] = &baseep[(long)entry[i]];
5968fae3551SRodney W. Grimes }
5978fae3551SRodney W. Grimes for (ep = &baseep[1]; ep < lep; ep++) {
5988fae3551SRodney W. Grimes ep->e_name = base + (long)ep->e_name;
5998fae3551SRodney W. Grimes ep->e_parent = &baseep[(long)ep->e_parent];
6008fae3551SRodney W. Grimes if (ep->e_sibling != NULL)
6018fae3551SRodney W. Grimes ep->e_sibling = &baseep[(long)ep->e_sibling];
6028fae3551SRodney W. Grimes if (ep->e_links != NULL)
6038fae3551SRodney W. Grimes ep->e_links = &baseep[(long)ep->e_links];
6048fae3551SRodney W. Grimes if (ep->e_entries != NULL)
6058fae3551SRodney W. Grimes ep->e_entries = &baseep[(long)ep->e_entries];
6068fae3551SRodney W. Grimes if (ep->e_next != NULL)
6078fae3551SRodney W. Grimes ep->e_next = &baseep[(long)ep->e_next];
6088fae3551SRodney W. Grimes }
6098fae3551SRodney W. Grimes }
610