| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
2 |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#include "config.h" |
| 48 |
#include "symtab.h" |
| 49 |
|
| 50 |
#include <sys/param.h> |
| 51 |
#include <sys/stat.h> |
| 52 |
|
| 53 |
#include <time.h> |
| 54 |
#include "compat/include/bsdcompat.h" |
| 55 |
|
| 56 |
#include "compat/include/compaterr.h" |
| 57 |
#include <fcntl.h> |
| 58 |
#include <stdio.h> |
| 59 |
#include <stdlib.h> |
| 60 |
#include <string.h> |
| 61 |
#include <unistd.h> |
| 62 |
#include <dirent.h> |
| 63 |
#include <stdint.h> |
| 64 |
#include "compat/include/protocols/dumprestore.h" |
| 65 |
|
| 66 |
#include "restore.h" |
| 67 |
#include "utilities.h" |
| 68 |
#include "restore_globals.h" |
| 69 |
#include "tape.h" |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#define HASHFACTOR 5 |
| 79 |
static struct entry **entry; |
| 80 |
static void* entry_mem; |
| 81 |
static void* entry_mem_end; |
| 82 |
static size_t entrytblsize; |
| 83 |
|
| 84 |
static void addino (dump_ino_t, struct entry *); |
| 85 |
static struct entry *lookupparent (const char *); |
| 86 |
static void removeentry (struct entry *); |
| 87 |
static int dir_hash (char *); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static int |
| 93 |
dir_hash(char *name) |
| 94 |
{ |
| 95 |
unsigned long hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9; |
| 96 |
int len = strlen(name); |
| 97 |
|
| 98 |
while (len--) { |
| 99 |
unsigned long hash = hash1 + (hash0 ^ (*name++ * 7152373)); |
| 100 |
if (hash & 0x80000000) hash -= 0x7fffffff; |
| 101 |
hash1 = hash0; |
| 102 |
hash0 = hash; |
| 103 |
} |
| 104 |
return hash0 % dirhash_size; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
struct entry * |
| 111 |
lookupino(dump_ino_t inum) |
| 112 |
{ |
| 113 |
struct entry *ep; |
| 114 |
|
| 115 |
if (inum < WINO || inum >= maxino) |
| 116 |
return (NULL); |
| 117 |
for (ep = entry[inum % entrytblsize]; ep != NULL; ep = ep->e_next) |
| 118 |
if (ep->e_ino == inum) |
| 119 |
return (ep); |
| 120 |
return (NULL); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static void |
| 127 |
addino(dump_ino_t inum, struct entry *np) |
| 128 |
{ |
| 129 |
struct entry **epp; |
| 130 |
|
| 131 |
if (inum < WINO || inum >= maxino) |
| 132 |
panic("addino: out of range %d\n", inum); |
| 133 |
epp = &entry[inum % entrytblsize]; |
| 134 |
np->e_ino = inum; |
| 135 |
np->e_next = *epp; |
| 136 |
*epp = np; |
| 137 |
if (dflag) |
| 138 |
for (np = np->e_next; np != NULL; np = np->e_next) |
| 139 |
if (np->e_ino == inum) |
| 140 |
badentry(np, "duplicate inum"); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
void |
| 147 |
deleteino(dump_ino_t inum) |
| 148 |
{ |
| 149 |
struct entry *next; |
| 150 |
struct entry **prev; |
| 151 |
|
| 152 |
if (inum < WINO || inum >= maxino) |
| 153 |
panic("deleteino: out of range %d\n", inum); |
| 154 |
prev = &entry[inum % entrytblsize]; |
| 155 |
for (next = *prev; next != NULL; next = next->e_next) { |
| 156 |
if (next->e_ino == inum) { |
| 157 |
next->e_ino = 0; |
| 158 |
*prev = next->e_next; |
| 159 |
return; |
| 160 |
} |
| 161 |
prev = &next->e_next; |
| 162 |
} |
| 163 |
panic("deleteino: %d not found\n", inum); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
struct entry * |
| 170 |
lookupname(const char *name) |
| 171 |
{ |
| 172 |
struct entry *ep, *oldep; |
| 173 |
char *np; |
| 174 |
const char *cp; |
| 175 |
char buf[MAXPATHLEN]; |
| 176 |
|
| 177 |
ep = lookupino(ROOTINO); |
| 178 |
|
| 179 |
cp = name; |
| 180 |
if (*cp == '.') |
| 181 |
++cp; |
| 182 |
if (*cp == '/') |
| 183 |
++cp; |
| 184 |
if (*cp == '\0') |
| 185 |
return ep; |
| 186 |
|
| 187 |
while (ep != NULL) { |
| 188 |
for (np = buf; *cp != '/' && *cp != '\0' && |
| 189 |
np < &buf[sizeof(buf)]; ) |
| 190 |
*np++ = *cp++; |
| 191 |
if (np == &buf[sizeof(buf)]) |
| 192 |
break; |
| 193 |
*np = '\0'; |
| 194 |
|
| 195 |
oldep = ep; |
| 196 |
|
| 197 |
if (ep->e_entries != NULL) { |
| 198 |
|
| 199 |
ep = ep->e_entries[dir_hash(buf)]; |
| 200 |
for ( ; ep != NULL; ep = ep->e_sibling) |
| 201 |
if (strcmp(ep->e_name, buf) == 0) |
| 202 |
break; |
| 203 |
|
| 204 |
|
| 205 |
if (ep == NULL) { |
| 206 |
int j; |
| 207 |
for (j = 0; j < dirhash_size; j++) { |
| 208 |
ep = oldep->e_entries[j]; |
| 209 |
for ( ; ep != NULL; ep = ep->e_sibling) |
| 210 |
if (strcmp(ep->e_name, buf) == 0) |
| 211 |
break; |
| 212 |
if (ep != NULL) |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if (ep == NULL) |
| 219 |
break; |
| 220 |
if (*cp++ == '\0') |
| 221 |
return (ep); |
| 222 |
} |
| 223 |
return (NULL); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
static struct entry * |
| 230 |
lookupparent(const char *name) |
| 231 |
{ |
| 232 |
struct entry *ep; |
| 233 |
char *tailindex; |
| 234 |
|
| 235 |
tailindex = strrchr(name, '/'); |
| 236 |
if (tailindex == NULL) |
| 237 |
return (NULL); |
| 238 |
*tailindex = '\0'; |
| 239 |
ep = lookupname(name); |
| 240 |
*tailindex = '/'; |
| 241 |
if (ep == NULL) |
| 242 |
return (NULL); |
| 243 |
if (ep->e_type != NODE) |
| 244 |
panic("%s is not a directory\n", name); |
| 245 |
return (ep); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
char * |
| 252 |
myname(struct entry *ep) |
| 253 |
{ |
| 254 |
char *cp; |
| 255 |
static char namebuf[MAXPATHLEN]; |
| 256 |
|
| 257 |
for (cp = &namebuf[MAXPATHLEN - 2]; cp > &namebuf[ep->e_namlen]; ) { |
| 258 |
cp -= ep->e_namlen; |
| 259 |
memmove(cp, ep->e_name, (size_t)ep->e_namlen); |
| 260 |
if (ep == lookupino(ROOTINO)) |
| 261 |
return (cp); |
| 262 |
*(--cp) = '/'; |
| 263 |
ep = ep->e_parent; |
| 264 |
} |
| 265 |
panic("%s: pathname too long\n", cp); |
| 266 |
return(cp); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static struct entry *freelist = NULL; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
struct entry * |
| 279 |
addentry(const char *name, dump_ino_t inum, int type) |
| 280 |
{ |
| 281 |
struct entry *np, *ep; |
| 282 |
|
| 283 |
if (freelist != NULL) { |
| 284 |
np = freelist; |
| 285 |
freelist = np->e_next; |
| 286 |
memset(np, 0, sizeof(struct entry)); |
| 287 |
} else { |
| 288 |
np = (struct entry *)calloc(1, sizeof(struct entry)); |
| 289 |
if (np == NULL) |
| 290 |
errx(1, "no memory to extend symbol table"); |
| 291 |
} |
| 292 |
np->e_type = type & ~LINK; |
| 293 |
if (type & NODE) { |
| 294 |
np->e_entries = calloc(1, dirhash_size * sizeof(struct entry *)); |
| 295 |
if (np->e_entries == NULL) |
| 296 |
panic("unable to allocate directory entries\n"); |
| 297 |
} |
| 298 |
ep = lookupparent(name); |
| 299 |
if (ep == NULL) { |
| 300 |
if (inum != ROOTINO || lookupino(ROOTINO) != NULL) |
| 301 |
panic("bad name to addentry %s\n", name); |
| 302 |
np->e_name = savename(name); |
| 303 |
np->e_namlen = strlen(name); |
| 304 |
np->e_parent = np; |
| 305 |
addino(ROOTINO, np); |
| 306 |
return (np); |
| 307 |
} |
| 308 |
np->e_name = savename(strrchr(name, '/') + 1); |
| 309 |
np->e_namlen = strlen(np->e_name); |
| 310 |
np->e_parent = ep; |
| 311 |
np->e_sibling = ep->e_entries[dir_hash(np->e_name)]; |
| 312 |
ep->e_entries[dir_hash(np->e_name)] = np; |
| 313 |
if (type & LINK) { |
| 314 |
ep = lookupino(inum); |
| 315 |
if (ep == NULL) |
| 316 |
panic("link to non-existent name\n"); |
| 317 |
np->e_ino = inum; |
| 318 |
np->e_links = ep->e_links; |
| 319 |
ep->e_links = np; |
| 320 |
} else if (inum != 0) { |
| 321 |
if (lookupino(inum) != NULL) |
| 322 |
panic("duplicate entry\n"); |
| 323 |
addino(inum, np); |
| 324 |
} |
| 325 |
return (np); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
void |
| 332 |
freeentry(struct entry *ep) |
| 333 |
{ |
| 334 |
struct entry *np; |
| 335 |
dump_ino_t inum; |
| 336 |
|
| 337 |
if (ep->e_flags != REMOVED) |
| 338 |
badentry(ep, "not marked REMOVED"); |
| 339 |
if (ep->e_type == NODE) { |
| 340 |
if (ep->e_links != NULL) |
| 341 |
badentry(ep, "freeing referenced directory"); |
| 342 |
if (ep->e_entries != NULL) { |
| 343 |
int i; |
| 344 |
for (i = 0; i < dirhash_size; i++) { |
| 345 |
if (ep->e_entries[i] != NULL) |
| 346 |
badentry(ep, "freeing non-empty directory"); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
if (ep->e_ino != 0) { |
| 351 |
np = lookupino(ep->e_ino); |
| 352 |
if (np == NULL) |
| 353 |
badentry(ep, "lookupino failed"); |
| 354 |
if (np == ep) { |
| 355 |
inum = ep->e_ino; |
| 356 |
deleteino(inum); |
| 357 |
if (ep->e_links != NULL) |
| 358 |
addino(inum, ep->e_links); |
| 359 |
} else { |
| 360 |
for (; np != NULL; np = np->e_links) { |
| 361 |
if (np->e_links == ep) { |
| 362 |
np->e_links = ep->e_links; |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
if (np == NULL) |
| 367 |
badentry(ep, "link not found"); |
| 368 |
} |
| 369 |
} |
| 370 |
removeentry(ep); |
| 371 |
freename(ep->e_name); |
| 372 |
ep->e_next = freelist; |
| 373 |
freelist = ep; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
void |
| 380 |
moveentry(struct entry *ep, const char *newname) |
| 381 |
{ |
| 382 |
struct entry *np; |
| 383 |
const char *cp; |
| 384 |
|
| 385 |
np = lookupparent(newname); |
| 386 |
if (np == NULL) |
| 387 |
badentry(ep, "cannot move ROOT"); |
| 388 |
if (np != ep->e_parent) { |
| 389 |
removeentry(ep); |
| 390 |
ep->e_parent = np; |
| 391 |
ep->e_sibling = np->e_entries[dir_hash(ep->e_name)]; |
| 392 |
np->e_entries[dir_hash(ep->e_name)] = ep; |
| 393 |
} |
| 394 |
cp = strrchr(newname, '/') + 1; |
| 395 |
freename(ep->e_name); |
| 396 |
ep->e_name = savename(cp); |
| 397 |
ep->e_namlen = strlen(cp); |
| 398 |
if (strcmp(gentempname(ep), ep->e_name) == 0) |
| 399 |
ep->e_flags |= TMPNAME; |
| 400 |
else |
| 401 |
ep->e_flags &= ~TMPNAME; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
static void |
| 408 |
removeentry(struct entry *ep) |
| 409 |
{ |
| 410 |
struct entry *np = ep->e_parent; |
| 411 |
struct entry *entry = np->e_entries[dir_hash(ep->e_name)]; |
| 412 |
|
| 413 |
if (entry == ep) { |
| 414 |
np->e_entries[dir_hash(ep->e_name)] = ep->e_sibling; |
| 415 |
} else { |
| 416 |
for (np = entry; np != NULL; np = np->e_sibling) { |
| 417 |
if (np->e_sibling == ep) { |
| 418 |
np->e_sibling = ep->e_sibling; |
| 419 |
break; |
| 420 |
} |
| 421 |
} |
| 422 |
if (np == NULL) { |
| 423 |
|
| 424 |
|
| 425 |
int j; |
| 426 |
for (j = 0; j < dirhash_size; j++) { |
| 427 |
np = ep->e_parent; |
| 428 |
entry = np->e_entries[j]; |
| 429 |
if (entry == ep) { |
| 430 |
np->e_entries[j] = ep->e_sibling; |
| 431 |
return; |
| 432 |
} |
| 433 |
else { |
| 434 |
for (np = entry; np != NULL; np = np->e_sibling) { |
| 435 |
if (np->e_sibling == ep) { |
| 436 |
np->e_sibling = ep->e_sibling; |
| 437 |
return; |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
badentry(ep, "cannot find entry in parent list"); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
struct strhdr { |
| 460 |
struct strhdr *next; |
| 461 |
}; |
| 462 |
|
| 463 |
#define STRTBLINCR (sizeof(struct strhdr)) |
| 464 |
#define allocsize(size) (((size) + 1 + STRTBLINCR - 1) & ~(STRTBLINCR - 1)) |
| 465 |
|
| 466 |
static struct strhdr strtblhdr[allocsize(MAXNAMLEN) / STRTBLINCR]; |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
char * |
| 473 |
savename(const char *name) |
| 474 |
{ |
| 475 |
struct strhdr *np; |
| 476 |
long len; |
| 477 |
char *cp; |
| 478 |
|
| 479 |
if (name == NULL) |
| 480 |
panic("bad name\n"); |
| 481 |
len = strlen(name); |
| 482 |
np = strtblhdr[len / STRTBLINCR].next; |
| 483 |
if (np != NULL) { |
| 484 |
strtblhdr[len / STRTBLINCR].next = np->next; |
| 485 |
cp = (char *)np; |
| 486 |
} else { |
| 487 |
cp = malloc(allocsize(len)); |
| 488 |
if (cp == NULL) |
| 489 |
errx(1, "no space for string table"); |
| 490 |
memset(cp, 0, allocsize(len)); |
| 491 |
} |
| 492 |
(void) strcpy(cp, name); |
| 493 |
return (cp); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
void |
| 501 |
freename(char *name) |
| 502 |
{ |
| 503 |
struct strhdr *tp, *np; |
| 504 |
|
| 505 |
tp = &strtblhdr[strlen(name) / STRTBLINCR]; |
| 506 |
np = (struct strhdr *)name; |
| 507 |
np->next = tp->next; |
| 508 |
tp->next = np; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
struct symtableheader { |
| 515 |
int32_t volno; |
| 516 |
int32_t stringsize; |
| 517 |
int32_t hashsize; |
| 518 |
int32_t entrytblsize; |
| 519 |
time_t dumptime; |
| 520 |
time_t dumpdate; |
| 521 |
dump_ino_t maxino; |
| 522 |
int32_t ntrec; |
| 523 |
int32_t zflag; |
| 524 |
}; |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
void |
| 530 |
dumpsymtable(const char *filename, long checkpt) |
| 531 |
{ |
| 532 |
struct entry *ep, *tep; |
| 533 |
dump_ino_t i; |
| 534 |
struct entry temp = {}, *tentry; |
| 535 |
long mynum = 1, stroff = 0, hashoff = 0; |
| 536 |
FILE *fd; |
| 537 |
struct symtableheader hdr = {}; |
| 538 |
struct entry **temphash; |
| 539 |
|
| 540 |
Vmsg("Check pointing the restore\n"); |
| 541 |
if (Nflag) |
| 542 |
return; |
| 543 |
if ((fd = fopen(filename, "w")) == NULL) { |
| 544 |
warn("fopen"); |
| 545 |
panic("cannot create save file %s for symbol table\n", |
| 546 |
filename); |
| 547 |
} |
| 548 |
clearerr(fd); |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
for (i = WINO; i <= maxino; i++) { |
| 554 |
for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { |
| 555 |
ep->e_index = mynum++; |
| 556 |
(void) fwrite(ep->e_name, (int)allocsize(ep->e_namlen), |
| 557 |
sizeof(char), fd); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
temphash = calloc(1, dirhash_size * sizeof(struct entry *)); |
| 564 |
if (temphash == NULL) |
| 565 |
errx(1, "no memory for saving hashtable"); |
| 566 |
for (i = WINO; i <= maxino; i++) { |
| 567 |
for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { |
| 568 |
if (ep->e_entries != NULL) { |
| 569 |
int j; |
| 570 |
memcpy(temphash, ep->e_entries, dirhash_size * sizeof(struct entry *)); |
| 571 |
for (j = 0; j < dirhash_size; j++) { |
| 572 |
if (temphash[j]) |
| 573 |
temphash[j] = (struct entry *)ep->e_entries[j]->e_index; |
| 574 |
} |
| 575 |
fwrite(temphash, sizeof(struct entry *), dirhash_size, fd); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
free(temphash); |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
tep = &temp; |
| 585 |
stroff = 0; |
| 586 |
hashoff = 0; |
| 587 |
for (i = WINO; i <= maxino; i++) { |
| 588 |
for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { |
| 589 |
memmove(tep, ep, sizeof(struct entry)); |
| 590 |
tep->e_name = (char *)stroff; |
| 591 |
stroff += allocsize(ep->e_namlen); |
| 592 |
tep->e_parent = (struct entry *)ep->e_parent->e_index; |
| 593 |
if (ep->e_links != NULL) |
| 594 |
tep->e_links = |
| 595 |
(struct entry *)ep->e_links->e_index; |
| 596 |
if (ep->e_sibling != NULL) |
| 597 |
tep->e_sibling = |
| 598 |
(struct entry *)ep->e_sibling->e_index; |
| 599 |
if (ep->e_entries != NULL) { |
| 600 |
tep->e_entries = (struct entry **)hashoff; |
| 601 |
hashoff += dirhash_size * sizeof(struct entry *); |
| 602 |
} |
| 603 |
if (ep->e_next != NULL) |
| 604 |
tep->e_next = |
| 605 |
(struct entry *)ep->e_next->e_index; |
| 606 |
(void) fwrite((char *)tep, sizeof(struct entry), 1, fd); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
for (i = 0; i < entrytblsize; i++) { |
| 613 |
if (entry[i] == NULL) |
| 614 |
tentry = NULL; |
| 615 |
else |
| 616 |
tentry = (struct entry *)entry[i]->e_index; |
| 617 |
(void) fwrite((char *)&tentry, sizeof(struct entry *), 1, fd); |
| 618 |
} |
| 619 |
hdr.volno = checkpt; |
| 620 |
hdr.maxino = maxino; |
| 621 |
hdr.entrytblsize = entrytblsize; |
| 622 |
hdr.stringsize = stroff; |
| 623 |
hdr.hashsize = hashoff; |
| 624 |
hdr.dumptime = dumptime; |
| 625 |
hdr.dumpdate = dumpdate; |
| 626 |
hdr.ntrec = ntrec; |
| 627 |
hdr.zflag = 0; |
| 628 |
(void) fwrite((char *)&hdr, sizeof(struct symtableheader), 1, fd); |
| 629 |
if (ferror(fd)) { |
| 630 |
warn("fwrite"); |
| 631 |
panic("output error to file %s writing symbol table\n", |
| 632 |
filename); |
| 633 |
} |
| 634 |
(void) fclose(fd); |
| 635 |
} |
| 636 |
|
| 637 |
static void |
| 638 |
freemem(void* ptr) { |
| 639 |
if(ptr < entry_mem || ptr > entry_mem_end) { |
| 640 |
free(ptr); |
| 641 |
} |
| 642 |
} |
| 643 |
static void |
| 644 |
freenode(struct entry* ep) { |
| 645 |
if(ep->e_type & NODE) { |
| 646 |
freemem(ep->e_entries); |
| 647 |
} |
| 648 |
if (ep->e_links) |
| 649 |
freenode(ep->e_links); |
| 650 |
freemem(ep->e_name); |
| 651 |
freemem(ep); |
| 652 |
} |
| 653 |
void |
| 654 |
freesymtable(void) { |
| 655 |
void* ptr; |
| 656 |
while((ptr = freelist)) { |
| 657 |
freelist = freelist->e_next; |
| 658 |
freemem(ptr); |
| 659 |
} |
| 660 |
|
| 661 |
for(size_t i = 0; i < entrytblsize; i++) { |
| 662 |
struct entry *ep = entry[i]; |
| 663 |
while((ptr = ep)) { |
| 664 |
ep = ep->e_next; |
| 665 |
freenode(ptr); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
free(entry_mem); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
void |
| 676 |
initsymtable(const char *filename) |
| 677 |
{ |
| 678 |
char *base; |
| 679 |
long tblsize; |
| 680 |
struct entry *ep; |
| 681 |
struct entry *baseep, *lep; |
| 682 |
struct symtableheader hdr; |
| 683 |
struct stat stbuf; |
| 684 |
int fd; |
| 685 |
|
| 686 |
Vmsg("Initialize symbol table.\n"); |
| 687 |
if (filename == NULL) { |
| 688 |
entrytblsize = maxino / HASHFACTOR; |
| 689 |
entry_mem = entry = (struct entry **) |
| 690 |
calloc(entrytblsize, sizeof(struct entry *)); |
| 691 |
entry_mem_end = (char *)entry_mem + entrytblsize * sizeof(struct entry *); |
| 692 |
if (entry == (struct entry **)NULL) |
| 693 |
errx(1, "no memory for entry table"); |
| 694 |
ep = addentry(".", ROOTINO, NODE); |
| 695 |
ep->e_flags |= NEW; |
| 696 |
return; |
| 697 |
} |
| 698 |
if ((fd = open(filename, O_RDONLY, 0)) < 0) { |
| 699 |
warn("open"); |
| 700 |
errx(1, "cannot open symbol table file %s", filename); |
| 701 |
} |
| 702 |
if (fstat(fd, &stbuf) < 0) { |
| 703 |
warn("stat"); |
| 704 |
errx(1, "cannot stat symbol table file %s", filename); |
| 705 |
} |
| 706 |
tblsize = stbuf.st_size - sizeof(struct symtableheader); |
| 707 |
entry_mem = base = calloc(tblsize, sizeof(char)); |
| 708 |
entry_mem_end = base + tblsize; |
| 709 |
if (base == NULL) |
| 710 |
errx(1, "cannot allocate space for symbol table"); |
| 711 |
if (read(fd, base, (int)tblsize) < 0 || |
| 712 |
read(fd, (char *)&hdr, sizeof(struct symtableheader)) < 0) { |
| 713 |
warn("read"); |
| 714 |
errx(1, "cannot read symbol table file %s", filename); |
| 715 |
} |
| 716 |
switch (command) { |
| 717 |
case 'r': |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
if (hdr.dumpdate != dumptime) |
| 723 |
errx(1, "Incremental tape too %s", |
| 724 |
(hdr.dumpdate < dumptime) ? "low" : "high"); |
| 725 |
break; |
| 726 |
case 'R': |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
curfile.action = SKIP; |
| 731 |
dumptime = hdr.dumptime; |
| 732 |
dumpdate = hdr.dumpdate; |
| 733 |
if (!bflag) |
| 734 |
newtapebuf(hdr.ntrec); |
| 735 |
getvol(hdr.volno); |
| 736 |
break; |
| 737 |
default: |
| 738 |
panic("initsymtable called from command %c\n", command); |
| 739 |
break; |
| 740 |
} |
| 741 |
if (hdr.maxino > maxino) { |
| 742 |
resizemaps(maxino, hdr.maxino); |
| 743 |
maxino = hdr.maxino; |
| 744 |
} |
| 745 |
entrytblsize = hdr.entrytblsize; |
| 746 |
entry = (struct entry **) |
| 747 |
(base + tblsize - (entrytblsize * sizeof(struct entry *))); |
| 748 |
baseep = (struct entry *)(base + hdr.stringsize + hdr.hashsize - sizeof(struct entry)); |
| 749 |
lep = (struct entry *)entry; |
| 750 |
for (size_t i = 0; i < entrytblsize; i++) { |
| 751 |
if (entry[i] == NULL) |
| 752 |
continue; |
| 753 |
entry[i] = &baseep[(intptr_t)entry[i]]; |
| 754 |
} |
| 755 |
for (ep = &baseep[1]; ep < lep; ep++) { |
| 756 |
ep->e_name = base + (intptr_t)ep->e_name; |
| 757 |
ep->e_parent = &baseep[(intptr_t)ep->e_parent]; |
| 758 |
if (ep->e_sibling != NULL) |
| 759 |
ep->e_sibling = &baseep[(intptr_t)ep->e_sibling]; |
| 760 |
if (ep->e_links != NULL) |
| 761 |
ep->e_links = &baseep[(intptr_t)ep->e_links]; |
| 762 |
if (ep->e_type == NODE) { |
| 763 |
int i; |
| 764 |
ep->e_entries = (struct entry **)(base + hdr.stringsize + (intptr_t)ep->e_entries); |
| 765 |
for (i = 0; i < dirhash_size; i++) { |
| 766 |
if (ep->e_entries[i]) |
| 767 |
ep->e_entries[i] = &baseep[(intptr_t)ep->e_entries[i]]; |
| 768 |
} |
| 769 |
} |
| 770 |
else |
| 771 |
ep->e_entries = NULL; |
| 772 |
if (ep->e_next != NULL) |
| 773 |
ep->e_next = &baseep[(intptr_t)ep->e_next]; |
| 774 |
} |
| 775 |
} |