164 continue; 165 for ( ; ep != NULL; ep = ep->e_links) { 166 dprintf(stdout, "%s: REMOVE\n", myname(ep)); 167 if (ep->e_type == LEAF) { 168 removeleaf(ep); 169 freeentry(ep); 170 } else { 171 mktempname(ep); 172 deleteino(ep->e_ino); 173 ep->e_next = removelist; 174 removelist = ep; 175 } 176 } 177 } 178} 179 180/* 181 * For each directory entry on the incremental tape, determine which 182 * category it falls into as follows: 183 * KEEP - entries that are to be left alone. 184 * NEW - new entries to be added. 185 * EXTRACT - files that must be updated with new contents. 186 * LINK - new links to be added. 187 * Renames are done at the same time. 188 */ 189long 190nodeupdates(name, ino, type) 191 char *name; 192 ino_t ino; 193 int type; 194{ 195 register struct entry *ep, *np, *ip; 196 long descend = GOOD; 197 int lookuptype = 0; 198 int key = 0; 199 /* key values */ 200# define ONTAPE 0x1 /* inode is on the tape */ 201# define INOFND 0x2 /* inode already exists */ 202# define NAMEFND 0x4 /* name already exists */ 203# define MODECHG 0x8 /* mode of inode changed */ 204 205 /* 206 * This routine is called once for each element in the 207 * directory hierarchy, with a full path name. 208 * The "type" value is incorrectly specified as LEAF for 209 * directories that are not on the dump tape. 210 * 211 * Check to see if the file is on the tape. 212 */ 213 if (TSTINO(ino, dumpmap)) 214 key |= ONTAPE; 215 /* 216 * Check to see if the name exists, and if the name is a link. 217 */ 218 np = lookupname(name); 219 if (np != NULL) { 220 key |= NAMEFND; 221 ip = lookupino(np->e_ino); 222 if (ip == NULL) 223 panic("corrupted symbol table\n"); 224 if (ip != np) 225 lookuptype = LINK; 226 } 227 /* 228 * Check to see if the inode exists, and if one of its links 229 * corresponds to the name (if one was found). 230 */ 231 ip = lookupino(ino); 232 if (ip != NULL) { 233 key |= INOFND; 234 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) { 235 if (ep == np) { 236 ip = ep; 237 break; 238 } 239 } 240 } 241 /* 242 * If both a name and an inode are found, but they do not 243 * correspond to the same file, then both the inode that has 244 * been found and the inode corresponding to the name that 245 * has been found need to be renamed. The current pathname 246 * is the new name for the inode that has been found. Since 247 * all files to be deleted have already been removed, the 248 * named file is either a now unneeded link, or it must live 249 * under a new name in this dump level. If it is a link, it 250 * can be removed. If it is not a link, it is given a 251 * temporary name in anticipation that it will be renamed 252 * when it is later found by inode number. 253 */ 254 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) { 255 if (lookuptype == LINK) { 256 removeleaf(np); 257 freeentry(np); 258 } else { 259 dprintf(stdout, "name/inode conflict, mktempname %s\n", 260 myname(np)); 261 mktempname(np); 262 } 263 np = NULL; 264 key &= ~NAMEFND; 265 } 266 if ((key & ONTAPE) && 267 (((key & INOFND) && ip->e_type != type) || 268 ((key & NAMEFND) && np->e_type != type))) 269 key |= MODECHG; 270 271 /* 272 * Decide on the disposition of the file based on its flags. 273 * Note that we have already handled the case in which 274 * a name and inode are found that correspond to different files. 275 * Thus if both NAMEFND and INOFND are set then ip == np. 276 */ 277 switch (key) { 278 279 /* 280 * A previously existing file has been found. 281 * Mark it as KEEP so that other links to the inode can be 282 * detected, and so that it will not be reclaimed by the search 283 * for unreferenced names. 284 */ 285 case INOFND|NAMEFND: 286 ip->e_flags |= KEEP; 287 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 288 flagvalues(ip)); 289 break; 290 291 /* 292 * A file on the tape has a name which is the same as a name 293 * corresponding to a different file in the previous dump. 294 * Since all files to be deleted have already been removed, 295 * this file is either a now unneeded link, or it must live 296 * under a new name in this dump level. If it is a link, it 297 * can simply be removed. If it is not a link, it is given a 298 * temporary name in anticipation that it will be renamed 299 * when it is later found by inode number (see INOFND case 300 * below). The entry is then treated as a new file. 301 */ 302 case ONTAPE|NAMEFND: 303 case ONTAPE|NAMEFND|MODECHG: 304 if (lookuptype == LINK) { 305 removeleaf(np); 306 freeentry(np); 307 } else { 308 mktempname(np); 309 } 310 /* fall through */ 311 312 /* 313 * A previously non-existent file. 314 * Add it to the file system, and request its extraction. 315 * If it is a directory, create it immediately. 316 * (Since the name is unused there can be no conflict) 317 */ 318 case ONTAPE: 319 ep = addentry(name, ino, type); 320 if (type == NODE) 321 newnode(ep); 322 ep->e_flags |= NEW|KEEP; 323 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 324 flagvalues(ep)); 325 break; 326 327 /* 328 * A file with the same inode number, but a different 329 * name has been found. If the other name has not already 330 * been found (indicated by the KEEP flag, see above) then 331 * this must be a new name for the file, and it is renamed. 332 * If the other name has been found then this must be a 333 * link to the file. Hard links to directories are not 334 * permitted, and are either deleted or converted to 335 * symbolic links. Finally, if the file is on the tape, 336 * a request is made to extract it. 337 */ 338 case ONTAPE|INOFND: 339 if (type == LEAF && (ip->e_flags & KEEP) == 0) 340 ip->e_flags |= EXTRACT; 341 /* fall through */ 342 case INOFND: 343 if ((ip->e_flags & KEEP) == 0) { 344 renameit(myname(ip), name); 345 moveentry(ip, name); 346 ip->e_flags |= KEEP; 347 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 348 flagvalues(ip)); 349 break; 350 } 351 if (ip->e_type == NODE) { 352 descend = FAIL; 353 fprintf(stderr, 354 "deleted hard link %s to directory %s\n", 355 name, myname(ip)); 356 break; 357 } 358 ep = addentry(name, ino, type|LINK); 359 ep->e_flags |= NEW; 360 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name, 361 flagvalues(ep)); 362 break; 363 364 /* 365 * A previously known file which is to be updated. If it is a link, 366 * then all names referring to the previous file must be removed 367 * so that the subset of them that remain can be recreated. 368 */ 369 case ONTAPE|INOFND|NAMEFND: 370 if (lookuptype == LINK) { 371 removeleaf(np); 372 freeentry(np); 373 ep = addentry(name, ino, type|LINK); 374 if (type == NODE) 375 newnode(ep); 376 ep->e_flags |= NEW|KEEP; 377 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name, 378 flagvalues(ep)); 379 break; 380 } 381 if (type == LEAF && lookuptype != LINK) 382 np->e_flags |= EXTRACT; 383 np->e_flags |= KEEP; 384 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 385 flagvalues(np)); 386 break; 387 388 /* 389 * An inode is being reused in a completely different way. 390 * Normally an extract can simply do an "unlink" followed 391 * by a "creat". Here we must do effectively the same 392 * thing. The complications arise because we cannot really 393 * delete a directory since it may still contain files 394 * that we need to rename, so we delete it from the symbol 395 * table, and put it on the list to be deleted eventually. 396 * Conversely if a directory is to be created, it must be 397 * done immediately, rather than waiting until the 398 * extraction phase. 399 */ 400 case ONTAPE|INOFND|MODECHG: 401 case ONTAPE|INOFND|NAMEFND|MODECHG: 402 if (ip->e_flags & KEEP) { 403 badentry(ip, "cannot KEEP and change modes"); 404 break; 405 } 406 if (ip->e_type == LEAF) { 407 /* changing from leaf to node */ 408 removeleaf(ip); 409 freeentry(ip); 410 ip = addentry(name, ino, type); 411 newnode(ip); 412 } else { 413 /* changing from node to leaf */ 414 if ((ip->e_flags & TMPNAME) == 0) 415 mktempname(ip); 416 deleteino(ip->e_ino); 417 ip->e_next = removelist; 418 removelist = ip; 419 ip = addentry(name, ino, type); 420 } 421 ip->e_flags |= NEW|KEEP; 422 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 423 flagvalues(ip)); 424 break; 425 426 /* 427 * A hard link to a diirectory that has been removed. 428 * Ignore it. 429 */ 430 case NAMEFND: 431 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key), 432 name); 433 descend = FAIL; 434 break; 435 436 /* 437 * If we find a directory entry for a file that is not on 438 * the tape, then we must have found a file that was created 439 * while the dump was in progress. Since we have no contents 440 * for it, we discard the name knowing that it will be on the 441 * next incremental tape. 442 */ 443 case NULL: 444 fprintf(stderr, "%s: (inode %d) not found on tape\n", 445 name, ino); 446 break; 447 448 /* 449 * If any of these arise, something is grievously wrong with 450 * the current state of the symbol table. 451 */ 452 case INOFND|NAMEFND|MODECHG: 453 case NAMEFND|MODECHG: 454 case INOFND|MODECHG: 455 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key), 456 name); 457 break; 458 459 /* 460 * These states "cannot" arise for any state of the symbol table. 461 */ 462 case ONTAPE|MODECHG: 463 case MODECHG: 464 default: 465 panic("[%s] %s: impossible state\n", keyval(key), name); 466 break; 467 } 468 return (descend); 469} 470 471/* 472 * Calculate the active flags in a key. 473 */ 474static char * 475keyval(key) 476 int key; 477{ 478 static char keybuf[32]; 479 480 (void) strcpy(keybuf, "|NIL"); 481 keybuf[0] = '\0'; 482 if (key & ONTAPE) 483 (void) strcat(keybuf, "|ONTAPE"); 484 if (key & INOFND) 485 (void) strcat(keybuf, "|INOFND"); 486 if (key & NAMEFND) 487 (void) strcat(keybuf, "|NAMEFND"); 488 if (key & MODECHG) 489 (void) strcat(keybuf, "|MODECHG"); 490 return (&keybuf[1]); 491} 492 493/* 494 * Find unreferenced link names. 495 */ 496void 497findunreflinks() 498{ 499 register struct entry *ep, *np; 500 register ino_t i; 501 502 vprintf(stdout, "Find unreferenced names.\n"); 503 for (i = ROOTINO; i < maxino; i++) { 504 ep = lookupino(i); 505 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0) 506 continue; 507 for (np = ep->e_entries; np != NULL; np = np->e_sibling) { 508 if (np->e_flags == 0) { 509 dprintf(stdout, 510 "%s: remove unreferenced name\n", 511 myname(np)); 512 removeleaf(np); 513 freeentry(np); 514 } 515 } 516 } 517 /* 518 * Any leaves remaining in removed directories is unreferenced. 519 */ 520 for (ep = removelist; ep != NULL; ep = ep->e_next) { 521 for (np = ep->e_entries; np != NULL; np = np->e_sibling) { 522 if (np->e_type == LEAF) { 523 if (np->e_flags != 0) 524 badentry(np, "unreferenced with flags"); 525 dprintf(stdout, 526 "%s: remove unreferenced name\n", 527 myname(np)); 528 removeleaf(np); 529 freeentry(np); 530 } 531 } 532 } 533} 534 535/* 536 * Remove old nodes (directories). 537 * Note that this routine runs in O(N*D) where: 538 * N is the number of directory entries to be removed. 539 * D is the maximum depth of the tree. 540 * If N == D this can be quite slow. If the list were 541 * topologically sorted, the deletion could be done in 542 * time O(N). 543 */ 544void 545removeoldnodes() 546{ 547 register struct entry *ep, **prev; 548 long change; 549 550 vprintf(stdout, "Remove old nodes (directories).\n"); 551 do { 552 change = 0; 553 prev = &removelist; 554 for (ep = removelist; ep != NULL; ep = *prev) { 555 if (ep->e_entries != NULL) { 556 prev = &ep->e_next; 557 continue; 558 } 559 *prev = ep->e_next; 560 removenode(ep); 561 freeentry(ep); 562 change++; 563 } 564 } while (change); 565 for (ep = removelist; ep != NULL; ep = ep->e_next) 566 badentry(ep, "cannot remove, non-empty"); 567} 568 569/* 570 * This is the routine used to extract files for the 'r' command. 571 * Extract new leaves. 572 */ 573void 574createleaves(symtabfile) 575 char *symtabfile; 576{ 577 register struct entry *ep; 578 ino_t first; 579 long curvol; 580 581 if (command == 'R') { 582 vprintf(stdout, "Continue extraction of new leaves\n"); 583 } else { 584 vprintf(stdout, "Extract new leaves.\n"); 585 dumpsymtable(symtabfile, volno); 586 } 587 first = lowerbnd(ROOTINO); 588 curvol = volno; 589 while (curfile.ino < maxino) { 590 first = lowerbnd(first); 591 /* 592 * If the next available file is not the one which we 593 * expect then we have missed one or more files. Since 594 * we do not request files that were not on the tape, 595 * the lost files must have been due to a tape read error, 596 * or a file that was removed while the dump was in progress. 597 */ 598 while (first < curfile.ino) { 599 ep = lookupino(first); 600 if (ep == NULL) 601 panic("%d: bad first\n", first); 602 fprintf(stderr, "%s: not found on tape\n", myname(ep)); 603 ep->e_flags &= ~(NEW|EXTRACT); 604 first = lowerbnd(first); 605 } 606 /* 607 * If we find files on the tape that have no corresponding 608 * directory entries, then we must have found a file that 609 * was created while the dump was in progress. Since we have 610 * no name for it, we discard it knowing that it will be 611 * on the next incremental tape. 612 */ 613 if (first != curfile.ino) { 614 fprintf(stderr, "expected next file %d, got %d\n", 615 first, curfile.ino); 616 skipfile(); 617 goto next; 618 } 619 ep = lookupino(curfile.ino); 620 if (ep == NULL) 621 panic("unknown file on tape\n"); 622 if ((ep->e_flags & (NEW|EXTRACT)) == 0) 623 badentry(ep, "unexpected file on tape"); 624 /* 625 * If the file is to be extracted, then the old file must 626 * be removed since its type may change from one leaf type 627 * to another (eg "file" to "character special"). 628 */ 629 if ((ep->e_flags & EXTRACT) != 0) { 630 removeleaf(ep); 631 ep->e_flags &= ~REMOVED; 632 } 633 (void) extractfile(myname(ep)); 634 ep->e_flags &= ~(NEW|EXTRACT); 635 /* 636 * We checkpoint the restore after every tape reel, so 637 * as to simplify the amount of work re quired by the 638 * 'R' command. 639 */ 640 next: 641 if (curvol != volno) { 642 dumpsymtable(symtabfile, volno); 643 skipmaps(); 644 curvol = volno; 645 } 646 } 647} 648 649/* 650 * This is the routine used to extract files for the 'x' and 'i' commands. 651 * Efficiently extract a subset of the files on a tape. 652 */ 653void 654createfiles() 655{ 656 register ino_t first, next, last; 657 register struct entry *ep; 658 long curvol; 659 660 vprintf(stdout, "Extract requested files\n"); 661 curfile.action = SKIP; 662 getvol((long)1); 663 skipmaps(); 664 skipdirs(); 665 first = lowerbnd(ROOTINO); 666 last = upperbnd(maxino - 1); 667 for (;;) { 668 first = lowerbnd(first); 669 last = upperbnd(last); 670 /* 671 * Check to see if any files remain to be extracted 672 */ 673 if (first > last) 674 return; 675 /* 676 * Reject any volumes with inodes greater 677 * than the last one needed 678 */ 679 while (curfile.ino > last) { 680 curfile.action = SKIP; 681 getvol((long)0); 682 skipmaps(); 683 skipdirs(); 684 } 685 /* 686 * Decide on the next inode needed. 687 * Skip across the inodes until it is found 688 * or an out of order volume change is encountered 689 */ 690 next = lowerbnd(curfile.ino); 691 do { 692 curvol = volno; 693 while (next > curfile.ino && volno == curvol) 694 skipfile(); 695 skipmaps(); 696 skipdirs(); 697 } while (volno == curvol + 1); 698 /* 699 * If volume change out of order occurred the 700 * current state must be recalculated 701 */ 702 if (volno != curvol) 703 continue; 704 /* 705 * If the current inode is greater than the one we were 706 * looking for then we missed the one we were looking for. 707 * Since we only attempt to extract files listed in the 708 * dump map, the lost files must have been due to a tape 709 * read error, or a file that was removed while the dump 710 * was in progress. Thus we report all requested files 711 * between the one we were looking for, and the one we 712 * found as missing, and delete their request flags. 713 */ 714 while (next < curfile.ino) { 715 ep = lookupino(next); 716 if (ep == NULL) 717 panic("corrupted symbol table\n"); 718 fprintf(stderr, "%s: not found on tape\n", myname(ep)); 719 ep->e_flags &= ~NEW; 720 next = lowerbnd(next); 721 } 722 /* 723 * The current inode is the one that we are looking for, 724 * so extract it per its requested name. 725 */ 726 if (next == curfile.ino && next <= last) { 727 ep = lookupino(next); 728 if (ep == NULL) 729 panic("corrupted symbol table\n"); 730 (void) extractfile(myname(ep)); 731 ep->e_flags &= ~NEW; 732 if (volno != curvol) 733 skipmaps(); 734 } 735 } 736} 737 738/* 739 * Add links. 740 */ 741void 742createlinks() 743{ 744 register struct entry *np, *ep; 745 register ino_t i; 746 char name[BUFSIZ]; 747
| 187 continue; 188 for ( ; ep != NULL; ep = ep->e_links) { 189 dprintf(stdout, "%s: REMOVE\n", myname(ep)); 190 if (ep->e_type == LEAF) { 191 removeleaf(ep); 192 freeentry(ep); 193 } else { 194 mktempname(ep); 195 deleteino(ep->e_ino); 196 ep->e_next = removelist; 197 removelist = ep; 198 } 199 } 200 } 201} 202 203/* 204 * For each directory entry on the incremental tape, determine which 205 * category it falls into as follows: 206 * KEEP - entries that are to be left alone. 207 * NEW - new entries to be added. 208 * EXTRACT - files that must be updated with new contents. 209 * LINK - new links to be added. 210 * Renames are done at the same time. 211 */ 212long 213nodeupdates(name, ino, type) 214 char *name; 215 ino_t ino; 216 int type; 217{ 218 register struct entry *ep, *np, *ip; 219 long descend = GOOD; 220 int lookuptype = 0; 221 int key = 0; 222 /* key values */ 223# define ONTAPE 0x1 /* inode is on the tape */ 224# define INOFND 0x2 /* inode already exists */ 225# define NAMEFND 0x4 /* name already exists */ 226# define MODECHG 0x8 /* mode of inode changed */ 227 228 /* 229 * This routine is called once for each element in the 230 * directory hierarchy, with a full path name. 231 * The "type" value is incorrectly specified as LEAF for 232 * directories that are not on the dump tape. 233 * 234 * Check to see if the file is on the tape. 235 */ 236 if (TSTINO(ino, dumpmap)) 237 key |= ONTAPE; 238 /* 239 * Check to see if the name exists, and if the name is a link. 240 */ 241 np = lookupname(name); 242 if (np != NULL) { 243 key |= NAMEFND; 244 ip = lookupino(np->e_ino); 245 if (ip == NULL) 246 panic("corrupted symbol table\n"); 247 if (ip != np) 248 lookuptype = LINK; 249 } 250 /* 251 * Check to see if the inode exists, and if one of its links 252 * corresponds to the name (if one was found). 253 */ 254 ip = lookupino(ino); 255 if (ip != NULL) { 256 key |= INOFND; 257 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) { 258 if (ep == np) { 259 ip = ep; 260 break; 261 } 262 } 263 } 264 /* 265 * If both a name and an inode are found, but they do not 266 * correspond to the same file, then both the inode that has 267 * been found and the inode corresponding to the name that 268 * has been found need to be renamed. The current pathname 269 * is the new name for the inode that has been found. Since 270 * all files to be deleted have already been removed, the 271 * named file is either a now unneeded link, or it must live 272 * under a new name in this dump level. If it is a link, it 273 * can be removed. If it is not a link, it is given a 274 * temporary name in anticipation that it will be renamed 275 * when it is later found by inode number. 276 */ 277 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) { 278 if (lookuptype == LINK) { 279 removeleaf(np); 280 freeentry(np); 281 } else { 282 dprintf(stdout, "name/inode conflict, mktempname %s\n", 283 myname(np)); 284 mktempname(np); 285 } 286 np = NULL; 287 key &= ~NAMEFND; 288 } 289 if ((key & ONTAPE) && 290 (((key & INOFND) && ip->e_type != type) || 291 ((key & NAMEFND) && np->e_type != type))) 292 key |= MODECHG; 293 294 /* 295 * Decide on the disposition of the file based on its flags. 296 * Note that we have already handled the case in which 297 * a name and inode are found that correspond to different files. 298 * Thus if both NAMEFND and INOFND are set then ip == np. 299 */ 300 switch (key) { 301 302 /* 303 * A previously existing file has been found. 304 * Mark it as KEEP so that other links to the inode can be 305 * detected, and so that it will not be reclaimed by the search 306 * for unreferenced names. 307 */ 308 case INOFND|NAMEFND: 309 ip->e_flags |= KEEP; 310 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 311 flagvalues(ip)); 312 break; 313 314 /* 315 * A file on the tape has a name which is the same as a name 316 * corresponding to a different file in the previous dump. 317 * Since all files to be deleted have already been removed, 318 * this file is either a now unneeded link, or it must live 319 * under a new name in this dump level. If it is a link, it 320 * can simply be removed. If it is not a link, it is given a 321 * temporary name in anticipation that it will be renamed 322 * when it is later found by inode number (see INOFND case 323 * below). The entry is then treated as a new file. 324 */ 325 case ONTAPE|NAMEFND: 326 case ONTAPE|NAMEFND|MODECHG: 327 if (lookuptype == LINK) { 328 removeleaf(np); 329 freeentry(np); 330 } else { 331 mktempname(np); 332 } 333 /* fall through */ 334 335 /* 336 * A previously non-existent file. 337 * Add it to the file system, and request its extraction. 338 * If it is a directory, create it immediately. 339 * (Since the name is unused there can be no conflict) 340 */ 341 case ONTAPE: 342 ep = addentry(name, ino, type); 343 if (type == NODE) 344 newnode(ep); 345 ep->e_flags |= NEW|KEEP; 346 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 347 flagvalues(ep)); 348 break; 349 350 /* 351 * A file with the same inode number, but a different 352 * name has been found. If the other name has not already 353 * been found (indicated by the KEEP flag, see above) then 354 * this must be a new name for the file, and it is renamed. 355 * If the other name has been found then this must be a 356 * link to the file. Hard links to directories are not 357 * permitted, and are either deleted or converted to 358 * symbolic links. Finally, if the file is on the tape, 359 * a request is made to extract it. 360 */ 361 case ONTAPE|INOFND: 362 if (type == LEAF && (ip->e_flags & KEEP) == 0) 363 ip->e_flags |= EXTRACT; 364 /* fall through */ 365 case INOFND: 366 if ((ip->e_flags & KEEP) == 0) { 367 renameit(myname(ip), name); 368 moveentry(ip, name); 369 ip->e_flags |= KEEP; 370 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 371 flagvalues(ip)); 372 break; 373 } 374 if (ip->e_type == NODE) { 375 descend = FAIL; 376 fprintf(stderr, 377 "deleted hard link %s to directory %s\n", 378 name, myname(ip)); 379 break; 380 } 381 ep = addentry(name, ino, type|LINK); 382 ep->e_flags |= NEW; 383 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name, 384 flagvalues(ep)); 385 break; 386 387 /* 388 * A previously known file which is to be updated. If it is a link, 389 * then all names referring to the previous file must be removed 390 * so that the subset of them that remain can be recreated. 391 */ 392 case ONTAPE|INOFND|NAMEFND: 393 if (lookuptype == LINK) { 394 removeleaf(np); 395 freeentry(np); 396 ep = addentry(name, ino, type|LINK); 397 if (type == NODE) 398 newnode(ep); 399 ep->e_flags |= NEW|KEEP; 400 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name, 401 flagvalues(ep)); 402 break; 403 } 404 if (type == LEAF && lookuptype != LINK) 405 np->e_flags |= EXTRACT; 406 np->e_flags |= KEEP; 407 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 408 flagvalues(np)); 409 break; 410 411 /* 412 * An inode is being reused in a completely different way. 413 * Normally an extract can simply do an "unlink" followed 414 * by a "creat". Here we must do effectively the same 415 * thing. The complications arise because we cannot really 416 * delete a directory since it may still contain files 417 * that we need to rename, so we delete it from the symbol 418 * table, and put it on the list to be deleted eventually. 419 * Conversely if a directory is to be created, it must be 420 * done immediately, rather than waiting until the 421 * extraction phase. 422 */ 423 case ONTAPE|INOFND|MODECHG: 424 case ONTAPE|INOFND|NAMEFND|MODECHG: 425 if (ip->e_flags & KEEP) { 426 badentry(ip, "cannot KEEP and change modes"); 427 break; 428 } 429 if (ip->e_type == LEAF) { 430 /* changing from leaf to node */ 431 removeleaf(ip); 432 freeentry(ip); 433 ip = addentry(name, ino, type); 434 newnode(ip); 435 } else { 436 /* changing from node to leaf */ 437 if ((ip->e_flags & TMPNAME) == 0) 438 mktempname(ip); 439 deleteino(ip->e_ino); 440 ip->e_next = removelist; 441 removelist = ip; 442 ip = addentry(name, ino, type); 443 } 444 ip->e_flags |= NEW|KEEP; 445 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 446 flagvalues(ip)); 447 break; 448 449 /* 450 * A hard link to a diirectory that has been removed. 451 * Ignore it. 452 */ 453 case NAMEFND: 454 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key), 455 name); 456 descend = FAIL; 457 break; 458 459 /* 460 * If we find a directory entry for a file that is not on 461 * the tape, then we must have found a file that was created 462 * while the dump was in progress. Since we have no contents 463 * for it, we discard the name knowing that it will be on the 464 * next incremental tape. 465 */ 466 case NULL: 467 fprintf(stderr, "%s: (inode %d) not found on tape\n", 468 name, ino); 469 break; 470 471 /* 472 * If any of these arise, something is grievously wrong with 473 * the current state of the symbol table. 474 */ 475 case INOFND|NAMEFND|MODECHG: 476 case NAMEFND|MODECHG: 477 case INOFND|MODECHG: 478 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key), 479 name); 480 break; 481 482 /* 483 * These states "cannot" arise for any state of the symbol table. 484 */ 485 case ONTAPE|MODECHG: 486 case MODECHG: 487 default: 488 panic("[%s] %s: impossible state\n", keyval(key), name); 489 break; 490 } 491 return (descend); 492} 493 494/* 495 * Calculate the active flags in a key. 496 */ 497static char * 498keyval(key) 499 int key; 500{ 501 static char keybuf[32]; 502 503 (void) strcpy(keybuf, "|NIL"); 504 keybuf[0] = '\0'; 505 if (key & ONTAPE) 506 (void) strcat(keybuf, "|ONTAPE"); 507 if (key & INOFND) 508 (void) strcat(keybuf, "|INOFND"); 509 if (key & NAMEFND) 510 (void) strcat(keybuf, "|NAMEFND"); 511 if (key & MODECHG) 512 (void) strcat(keybuf, "|MODECHG"); 513 return (&keybuf[1]); 514} 515 516/* 517 * Find unreferenced link names. 518 */ 519void 520findunreflinks() 521{ 522 register struct entry *ep, *np; 523 register ino_t i; 524 525 vprintf(stdout, "Find unreferenced names.\n"); 526 for (i = ROOTINO; i < maxino; i++) { 527 ep = lookupino(i); 528 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0) 529 continue; 530 for (np = ep->e_entries; np != NULL; np = np->e_sibling) { 531 if (np->e_flags == 0) { 532 dprintf(stdout, 533 "%s: remove unreferenced name\n", 534 myname(np)); 535 removeleaf(np); 536 freeentry(np); 537 } 538 } 539 } 540 /* 541 * Any leaves remaining in removed directories is unreferenced. 542 */ 543 for (ep = removelist; ep != NULL; ep = ep->e_next) { 544 for (np = ep->e_entries; np != NULL; np = np->e_sibling) { 545 if (np->e_type == LEAF) { 546 if (np->e_flags != 0) 547 badentry(np, "unreferenced with flags"); 548 dprintf(stdout, 549 "%s: remove unreferenced name\n", 550 myname(np)); 551 removeleaf(np); 552 freeentry(np); 553 } 554 } 555 } 556} 557 558/* 559 * Remove old nodes (directories). 560 * Note that this routine runs in O(N*D) where: 561 * N is the number of directory entries to be removed. 562 * D is the maximum depth of the tree. 563 * If N == D this can be quite slow. If the list were 564 * topologically sorted, the deletion could be done in 565 * time O(N). 566 */ 567void 568removeoldnodes() 569{ 570 register struct entry *ep, **prev; 571 long change; 572 573 vprintf(stdout, "Remove old nodes (directories).\n"); 574 do { 575 change = 0; 576 prev = &removelist; 577 for (ep = removelist; ep != NULL; ep = *prev) { 578 if (ep->e_entries != NULL) { 579 prev = &ep->e_next; 580 continue; 581 } 582 *prev = ep->e_next; 583 removenode(ep); 584 freeentry(ep); 585 change++; 586 } 587 } while (change); 588 for (ep = removelist; ep != NULL; ep = ep->e_next) 589 badentry(ep, "cannot remove, non-empty"); 590} 591 592/* 593 * This is the routine used to extract files for the 'r' command. 594 * Extract new leaves. 595 */ 596void 597createleaves(symtabfile) 598 char *symtabfile; 599{ 600 register struct entry *ep; 601 ino_t first; 602 long curvol; 603 604 if (command == 'R') { 605 vprintf(stdout, "Continue extraction of new leaves\n"); 606 } else { 607 vprintf(stdout, "Extract new leaves.\n"); 608 dumpsymtable(symtabfile, volno); 609 } 610 first = lowerbnd(ROOTINO); 611 curvol = volno; 612 while (curfile.ino < maxino) { 613 first = lowerbnd(first); 614 /* 615 * If the next available file is not the one which we 616 * expect then we have missed one or more files. Since 617 * we do not request files that were not on the tape, 618 * the lost files must have been due to a tape read error, 619 * or a file that was removed while the dump was in progress. 620 */ 621 while (first < curfile.ino) { 622 ep = lookupino(first); 623 if (ep == NULL) 624 panic("%d: bad first\n", first); 625 fprintf(stderr, "%s: not found on tape\n", myname(ep)); 626 ep->e_flags &= ~(NEW|EXTRACT); 627 first = lowerbnd(first); 628 } 629 /* 630 * If we find files on the tape that have no corresponding 631 * directory entries, then we must have found a file that 632 * was created while the dump was in progress. Since we have 633 * no name for it, we discard it knowing that it will be 634 * on the next incremental tape. 635 */ 636 if (first != curfile.ino) { 637 fprintf(stderr, "expected next file %d, got %d\n", 638 first, curfile.ino); 639 skipfile(); 640 goto next; 641 } 642 ep = lookupino(curfile.ino); 643 if (ep == NULL) 644 panic("unknown file on tape\n"); 645 if ((ep->e_flags & (NEW|EXTRACT)) == 0) 646 badentry(ep, "unexpected file on tape"); 647 /* 648 * If the file is to be extracted, then the old file must 649 * be removed since its type may change from one leaf type 650 * to another (eg "file" to "character special"). 651 */ 652 if ((ep->e_flags & EXTRACT) != 0) { 653 removeleaf(ep); 654 ep->e_flags &= ~REMOVED; 655 } 656 (void) extractfile(myname(ep)); 657 ep->e_flags &= ~(NEW|EXTRACT); 658 /* 659 * We checkpoint the restore after every tape reel, so 660 * as to simplify the amount of work re quired by the 661 * 'R' command. 662 */ 663 next: 664 if (curvol != volno) { 665 dumpsymtable(symtabfile, volno); 666 skipmaps(); 667 curvol = volno; 668 } 669 } 670} 671 672/* 673 * This is the routine used to extract files for the 'x' and 'i' commands. 674 * Efficiently extract a subset of the files on a tape. 675 */ 676void 677createfiles() 678{ 679 register ino_t first, next, last; 680 register struct entry *ep; 681 long curvol; 682 683 vprintf(stdout, "Extract requested files\n"); 684 curfile.action = SKIP; 685 getvol((long)1); 686 skipmaps(); 687 skipdirs(); 688 first = lowerbnd(ROOTINO); 689 last = upperbnd(maxino - 1); 690 for (;;) { 691 first = lowerbnd(first); 692 last = upperbnd(last); 693 /* 694 * Check to see if any files remain to be extracted 695 */ 696 if (first > last) 697 return; 698 /* 699 * Reject any volumes with inodes greater 700 * than the last one needed 701 */ 702 while (curfile.ino > last) { 703 curfile.action = SKIP; 704 getvol((long)0); 705 skipmaps(); 706 skipdirs(); 707 } 708 /* 709 * Decide on the next inode needed. 710 * Skip across the inodes until it is found 711 * or an out of order volume change is encountered 712 */ 713 next = lowerbnd(curfile.ino); 714 do { 715 curvol = volno; 716 while (next > curfile.ino && volno == curvol) 717 skipfile(); 718 skipmaps(); 719 skipdirs(); 720 } while (volno == curvol + 1); 721 /* 722 * If volume change out of order occurred the 723 * current state must be recalculated 724 */ 725 if (volno != curvol) 726 continue; 727 /* 728 * If the current inode is greater than the one we were 729 * looking for then we missed the one we were looking for. 730 * Since we only attempt to extract files listed in the 731 * dump map, the lost files must have been due to a tape 732 * read error, or a file that was removed while the dump 733 * was in progress. Thus we report all requested files 734 * between the one we were looking for, and the one we 735 * found as missing, and delete their request flags. 736 */ 737 while (next < curfile.ino) { 738 ep = lookupino(next); 739 if (ep == NULL) 740 panic("corrupted symbol table\n"); 741 fprintf(stderr, "%s: not found on tape\n", myname(ep)); 742 ep->e_flags &= ~NEW; 743 next = lowerbnd(next); 744 } 745 /* 746 * The current inode is the one that we are looking for, 747 * so extract it per its requested name. 748 */ 749 if (next == curfile.ino && next <= last) { 750 ep = lookupino(next); 751 if (ep == NULL) 752 panic("corrupted symbol table\n"); 753 (void) extractfile(myname(ep)); 754 ep->e_flags &= ~NEW; 755 if (volno != curvol) 756 skipmaps(); 757 } 758 } 759} 760 761/* 762 * Add links. 763 */ 764void 765createlinks() 766{ 767 register struct entry *np, *ep; 768 register ino_t i; 769 char name[BUFSIZ]; 770
|