Changeset View
Changeset View
Standalone View
Standalone View
sbin/restore/symtab.c
Show First 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* Look up an entry by inode number | * Look up an entry by inode number | ||||
*/ | */ | ||||
struct entry * | struct entry * | ||||
lookupino(ino_t inum) | lookupino(ino_t inum) | ||||
{ | { | ||||
struct entry *ep; | struct entry *ep; | ||||
if (inum < WINO || inum >= maxino) | if (inum < UFS_WINO || inum >= maxino) | ||||
return (NULL); | return (NULL); | ||||
for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next) | for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next) | ||||
if (ep->e_ino == inum) | if (ep->e_ino == inum) | ||||
return (ep); | return (ep); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
/* | /* | ||||
* Add an entry into the entry table | * Add an entry into the entry table | ||||
*/ | */ | ||||
static void | static void | ||||
addino(ino_t inum, struct entry *np) | addino(ino_t inum, struct entry *np) | ||||
{ | { | ||||
struct entry **epp; | struct entry **epp; | ||||
if (inum < WINO || inum >= maxino) | if (inum < UFS_WINO || inum >= maxino) | ||||
panic("addino: out of range %ju\n", (uintmax_t)inum); | panic("addino: out of range %ju\n", (uintmax_t)inum); | ||||
epp = &entry[inum % entrytblsize]; | epp = &entry[inum % entrytblsize]; | ||||
np->e_ino = inum; | np->e_ino = inum; | ||||
np->e_next = *epp; | np->e_next = *epp; | ||||
*epp = np; | *epp = np; | ||||
if (dflag) | if (dflag) | ||||
for (np = np->e_next; np != NULL; np = np->e_next) | for (np = np->e_next; np != NULL; np = np->e_next) | ||||
if (np->e_ino == inum) | if (np->e_ino == inum) | ||||
badentry(np, "duplicate inum"); | badentry(np, "duplicate inum"); | ||||
} | } | ||||
/* | /* | ||||
* Delete an entry from the entry table | * Delete an entry from the entry table | ||||
*/ | */ | ||||
void | void | ||||
deleteino(ino_t inum) | deleteino(ino_t inum) | ||||
{ | { | ||||
struct entry *next; | struct entry *next; | ||||
struct entry **prev; | struct entry **prev; | ||||
if (inum < WINO || inum >= maxino) | if (inum < UFS_WINO || inum >= maxino) | ||||
panic("deleteino: out of range %ju\n", (uintmax_t)inum); | panic("deleteino: out of range %ju\n", (uintmax_t)inum); | ||||
prev = &entry[inum % entrytblsize]; | prev = &entry[inum % entrytblsize]; | ||||
for (next = *prev; next != NULL; next = next->e_next) { | for (next = *prev; next != NULL; next = next->e_next) { | ||||
if (next->e_ino == inum) { | if (next->e_ino == inum) { | ||||
next->e_ino = 0; | next->e_ino = 0; | ||||
*prev = next->e_next; | *prev = next->e_next; | ||||
return; | return; | ||||
} | } | ||||
prev = &next->e_next; | prev = &next->e_next; | ||||
} | } | ||||
panic("deleteino: %ju not found\n", (uintmax_t)inum); | panic("deleteino: %ju not found\n", (uintmax_t)inum); | ||||
} | } | ||||
/* | /* | ||||
* Look up an entry by name | * Look up an entry by name | ||||
*/ | */ | ||||
struct entry * | struct entry * | ||||
lookupname(char *name) | lookupname(char *name) | ||||
{ | { | ||||
struct entry *ep; | struct entry *ep; | ||||
char *np, *cp; | char *np, *cp; | ||||
char buf[MAXPATHLEN]; | char buf[MAXPATHLEN]; | ||||
cp = name; | cp = name; | ||||
for (ep = lookupino(ROOTINO); ep != NULL; ep = ep->e_entries) { | for (ep = lookupino(UFS_ROOTINO); ep != NULL; ep = ep->e_entries) { | ||||
for (np = buf; *cp != '/' && *cp != '\0' && | for (np = buf; *cp != '/' && *cp != '\0' && | ||||
np < &buf[sizeof(buf)]; ) | np < &buf[sizeof(buf)]; ) | ||||
*np++ = *cp++; | *np++ = *cp++; | ||||
if (np == &buf[sizeof(buf)]) | if (np == &buf[sizeof(buf)]) | ||||
break; | break; | ||||
*np = '\0'; | *np = '\0'; | ||||
for ( ; ep != NULL; ep = ep->e_sibling) | for ( ; ep != NULL; ep = ep->e_sibling) | ||||
if (strcmp(ep->e_name, buf) == 0) | if (strcmp(ep->e_name, buf) == 0) | ||||
Show All 35 Lines | |||||
myname(struct entry *ep) | myname(struct entry *ep) | ||||
{ | { | ||||
char *cp; | char *cp; | ||||
static char namebuf[MAXPATHLEN]; | static char namebuf[MAXPATHLEN]; | ||||
for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) { | for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) { | ||||
cp -= ep->e_namlen; | cp -= ep->e_namlen; | ||||
memmove(cp, ep->e_name, (long)ep->e_namlen); | memmove(cp, ep->e_name, (long)ep->e_namlen); | ||||
if (ep == lookupino(ROOTINO)) | if (ep == lookupino(UFS_ROOTINO)) | ||||
return (cp); | return (cp); | ||||
*(--cp) = '/'; | *(--cp) = '/'; | ||||
ep = ep->e_parent; | ep = ep->e_parent; | ||||
} | } | ||||
panic("%s: pathname too long\n", cp); | panic("%s: pathname too long\n", cp); | ||||
return(cp); | return(cp); | ||||
} | } | ||||
Show All 18 Lines | addentry(char *name, ino_t inum, int type) | ||||
} else { | } else { | ||||
np = (struct entry *)calloc(1, sizeof(struct entry)); | np = (struct entry *)calloc(1, sizeof(struct entry)); | ||||
if (np == NULL) | if (np == NULL) | ||||
panic("no memory to extend symbol table\n"); | panic("no memory to extend symbol table\n"); | ||||
} | } | ||||
np->e_type = type & ~LINK; | np->e_type = type & ~LINK; | ||||
ep = lookupparent(name); | ep = lookupparent(name); | ||||
if (ep == NULL) { | if (ep == NULL) { | ||||
if (inum != ROOTINO || lookupino(ROOTINO) != NULL) | if (inum != UFS_ROOTINO || lookupino(UFS_ROOTINO) != NULL) | ||||
panic("bad name to addentry %s\n", name); | panic("bad name to addentry %s\n", name); | ||||
np->e_name = savename(name); | np->e_name = savename(name); | ||||
np->e_namlen = strlen(name); | np->e_namlen = strlen(name); | ||||
np->e_parent = np; | np->e_parent = np; | ||||
addino(ROOTINO, np); | addino(UFS_ROOTINO, np); | ||||
return (np); | return (np); | ||||
} | } | ||||
np->e_name = savename(strrchr(name, '/') + 1); | np->e_name = savename(strrchr(name, '/') + 1); | ||||
np->e_namlen = strlen(np->e_name); | np->e_namlen = strlen(np->e_name); | ||||
np->e_parent = ep; | np->e_parent = ep; | ||||
np->e_sibling = ep->e_entries; | np->e_sibling = ep->e_entries; | ||||
ep->e_entries = np; | ep->e_entries = np; | ||||
if (type & LINK) { | if (type & LINK) { | ||||
▲ Show 20 Lines • Show All 203 Lines • ▼ Show 20 Lines | panic("cannot create save file %s for symbol table\n", | ||||
filename); | filename); | ||||
done(1); | done(1); | ||||
} | } | ||||
clearerr(fd); | clearerr(fd); | ||||
/* | /* | ||||
* Assign indices to each entry | * Assign indices to each entry | ||||
* Write out the string entries | * Write out the string entries | ||||
*/ | */ | ||||
for (i = WINO; i <= maxino; i++) { | for (i = UFS_WINO; i <= maxino; i++) { | ||||
for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { | for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { | ||||
ep->e_index = mynum++; | ep->e_index = mynum++; | ||||
(void) fwrite(ep->e_name, sizeof(char), | (void) fwrite(ep->e_name, sizeof(char), | ||||
(int)allocsize(ep->e_namlen), fd); | (int)allocsize(ep->e_namlen), fd); | ||||
} | } | ||||
} | } | ||||
/* | /* | ||||
* Convert pointers to indexes, and output | * Convert pointers to indexes, and output | ||||
*/ | */ | ||||
tep = &temp; | tep = &temp; | ||||
stroff = 0; | stroff = 0; | ||||
for (i = WINO; i <= maxino; i++) { | for (i = UFS_WINO; i <= maxino; i++) { | ||||
for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { | for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { | ||||
memmove(tep, ep, (long)sizeof(struct entry)); | memmove(tep, ep, (long)sizeof(struct entry)); | ||||
tep->e_name = (char *)stroff; | tep->e_name = (char *)stroff; | ||||
stroff += allocsize(ep->e_namlen); | stroff += allocsize(ep->e_namlen); | ||||
tep->e_parent = (struct entry *)ep->e_parent->e_index; | tep->e_parent = (struct entry *)ep->e_parent->e_index; | ||||
if (ep->e_links != NULL) | if (ep->e_links != NULL) | ||||
tep->e_links = | tep->e_links = | ||||
(struct entry *)ep->e_links->e_index; | (struct entry *)ep->e_links->e_index; | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | initsymtable(char *filename) | ||||
int fd; | int fd; | ||||
vprintf(stdout, "Initialize symbol table.\n"); | vprintf(stdout, "Initialize symbol table.\n"); | ||||
if (filename == NULL) { | if (filename == NULL) { | ||||
entrytblsize = maxino / HASHFACTOR; | entrytblsize = maxino / HASHFACTOR; | ||||
entry = calloc((unsigned)entrytblsize, sizeof(struct entry *)); | entry = calloc((unsigned)entrytblsize, sizeof(struct entry *)); | ||||
if (entry == NULL) | if (entry == NULL) | ||||
panic("no memory for entry table\n"); | panic("no memory for entry table\n"); | ||||
ep = addentry(".", ROOTINO, NODE); | ep = addentry(".", UFS_ROOTINO, NODE); | ||||
ep->e_flags |= NEW; | ep->e_flags |= NEW; | ||||
return; | return; | ||||
} | } | ||||
if ((fd = open(filename, O_RDONLY, 0)) < 0) { | if ((fd = open(filename, O_RDONLY, 0)) < 0) { | ||||
fprintf(stderr, "open: %s\n", strerror(errno)); | fprintf(stderr, "open: %s\n", strerror(errno)); | ||||
panic("cannot open symbol table file %s\n", filename); | panic("cannot open symbol table file %s\n", filename); | ||||
} | } | ||||
if (fstat(fd, &stbuf) < 0) { | if (fstat(fd, &stbuf) < 0) { | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |