restore.c revision 1558
1129207Scognet/* 2165895Simp * Copyright (c) 1983, 1993 3165895Simp * The Regents of the University of California. All rights reserved. 4165895Simp * 5129207Scognet * Redistribution and use in source and binary forms, with or without 6165895Simp * modification, are permitted provided that the following conditions 7165895Simp * are met: 8165895Simp * 1. Redistributions of source code must retain the above copyright 9165895Simp * notice, this list of conditions and the following disclaimer. 10129207Scognet * 2. Redistributions in binary form must reproduce the above copyright 11129207Scognet * notice, this list of conditions and the following disclaimer in the 12129207Scognet * documentation and/or other materials provided with the distribution. 13129207Scognet * 3. All advertising materials mentioning features or use of this software 14129207Scognet * must display the following acknowledgement: 15129207Scognet * This product includes software developed by the University of 16129207Scognet * California, Berkeley and its contributors. 17129207Scognet * 4. Neither the name of the University nor the names of its contributors 18165895Simp * may be used to endorse or promote products derived from this software 19165895Simp * without specific prior written permission. 20165895Simp * 21129207Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22129207Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23129207Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24129207Scognet * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25129207Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26129207Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27129207Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28129207Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29129207Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30129207Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31129207Scognet * SUCH DAMAGE. 32129207Scognet */ 33129207Scognet 34129207Scognet#ifndef lint 35129207Scognetstatic char sccsid[] = "@(#)restore.c 8.1 (Berkeley) 6/5/93"; 36129207Scognet#endif /* not lint */ 37129207Scognet 38129207Scognet#include <sys/types.h> 39129207Scognet#include <sys/stat.h> 40129207Scognet 41129207Scognet#include <ufs/ufs/dinode.h> 42291406Sjhb 43291406Sjhb#include <stdio.h> 44129207Scognet#include <string.h> 45291406Sjhb 46129207Scognet#include "restore.h" 47184728Sraj#include "extern.h" 48129207Scognet 49291406Sjhbstatic char *keyval __P((int)); 50291406Sjhb 51291406Sjhb/* 52291406Sjhb * This implements the 't' option. 53129207Scognet * List entries on the tape. 54291406Sjhb */ 55129207Scognetlong 56150874Scognetlistfile(name, ino, type) 57291406Sjhb char *name; 58291406Sjhb ino_t ino; 59291406Sjhb int type; 60150874Scognet{ 61150874Scognet long descend = hflag ? GOOD : FAIL; 62150874Scognet 63150874Scognet if (TSTINO(ino, dumpmap) == 0) 64150874Scognet return (descend); 65150874Scognet vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir "); 66150874Scognet fprintf(stdout, "%10d\t%s\n", ino, name); 67150874Scognet return (descend); 68291406Sjhb} 69291406Sjhb 70291406Sjhb/* 71150874Scognet * This implements the 'x' option. 72291406Sjhb * Request that new entries be extracted. 73291406Sjhb */ 74150874Scognetlong 75150874Scognetaddfile(name, ino, type) 76150874Scognet char *name; 77150874Scognet ino_t ino; 78150874Scognet int type; 79150874Scognet{ 80150874Scognet register struct entry *ep; 81150874Scognet long descend = hflag ? GOOD : FAIL; 82150874Scognet char buf[100]; 83150874Scognet 84150874Scognet if (TSTINO(ino, dumpmap) == 0) { 85291406Sjhb dprintf(stdout, "%s: not on the tape\n", name); 86291406Sjhb return (descend); 87129207Scognet } 88291406Sjhb if (!mflag) { 89291406Sjhb (void) sprintf(buf, "./%u", ino); 90291406Sjhb name = buf; 91291406Sjhb if (type == NODE) { 92291406Sjhb (void) genliteraldir(name, ino); 93129207Scognet return (descend); 94129207Scognet } 95291406Sjhb } 96291406Sjhb ep = lookupino(ino); 97129207Scognet if (ep != NULL) { 98291406Sjhb if (strcmp(name, myname(ep)) == 0) { 99291406Sjhb ep->e_flags |= NEW; 100291406Sjhb return (descend); 101291406Sjhb } 102291406Sjhb type |= LINK; 103291406Sjhb } 104291406Sjhb ep = addentry(name, ino, type); 105291406Sjhb if (type == NODE) 106184728Sraj newnode(ep); 107291406Sjhb ep->e_flags |= NEW; 108291406Sjhb return (descend); 109291406Sjhb} 110291406Sjhb 111291406Sjhb/* 112291406Sjhb * This is used by the 'i' option to undo previous requests made by addfile. 113184728Sraj * Delete entries from the request queue. 114291406Sjhb */ 115291406Sjhb/* ARGSUSED */ 116291406Sjhblong 117184728Srajdeletefile(name, ino, type) 118184728Sraj char *name; 119184728Sraj ino_t ino; 120298485Sngie int type; 121150874Scognet{ 122150874Scognet long descend = hflag ? GOOD : FAIL; 123150874Scognet struct entry *ep; 124150874Scognet 125150874Scognet if (TSTINO(ino, dumpmap) == 0) 126291406Sjhb return (descend); 127291406Sjhb ep = lookupino(ino); 128150874Scognet if (ep != NULL) 129273284Sandrew ep->e_flags &= ~NEW; 130273284Sandrew return (descend); 131291406Sjhb} 132291406Sjhb 133291406Sjhb/* 134291406Sjhb * The following four routines implement the incremental 135273284Sandrew * restore algorithm. The first removes old entries, the second 136273284Sandrew * does renames and calculates the extraction list, the third 137273284Sandrew * cleans up link names missed by the first two, and the final 138273284Sandrew * one deletes old directories. 139273284Sandrew * 140217744Suqs * Directories cannot be immediately deleted, as they may have 141273284Sandrew * other files in them which need to be moved out first. As 142273284Sandrew * directories to be deleted are found, they are put on the 143291406Sjhb * following deletion list. After all deletions and renames 144291406Sjhb * are done, this list is actually deleted. 145273284Sandrew */ 146291406Sjhbstatic struct entry *removelist; 147291406Sjhb 148291406Sjhb/* 149291406Sjhb * Remove unneeded leaves from the old tree. 150291406Sjhb * Remove directories from the lookup chains. 151273284Sandrew */ 152150874Scognetvoid 153273284Sandrewremoveoldleaves() 154291406Sjhb{ 155273284Sandrew register struct entry *ep; 156273284Sandrew register ino_t i; 157273284Sandrew 158273284Sandrew vprintf(stdout, "Mark entries to be removed.\n"); 159150874Scognet for (i = ROOTINO + 1; i < maxino; i++) { 160217744Suqs ep = lookupino(i); 161291406Sjhb if (ep == NULL) 162150874Scognet continue; 163150874Scognet if (TSTINO(i, clrimap)) 164150874Scognet continue; 165291406Sjhb for ( ; ep != NULL; ep = ep->e_links) { 166150874Scognet dprintf(stdout, "%s: REMOVE\n", myname(ep)); 167150874Scognet if (ep->e_type == LEAF) { 168150874Scognet removeleaf(ep); 169150874Scognet freeentry(ep); 170291406Sjhb } else { 171298485Sngie mktempname(ep); 172298485Sngie deleteino(ep->e_ino); 173298485Sngie ep->e_next = removelist; 174298485Sngie removelist = ep; 175291406Sjhb } 176150874Scognet } 177150874Scognet } 178150874Scognet} 179150874Scognet 180150874Scognet/* 181129207Scognet * For each directory entry on the incremental tape, determine which 182129207Scognet * category it falls into as follows: 183129207Scognet * KEEP - entries that are to be left alone. 184150874Scognet * NEW - new entries to be added. 185291406Sjhb * EXTRACT - files that must be updated with new contents. 186150874Scognet * LINK - new links to be added. 187291406Sjhb * Renames are done at the same time. 188150874Scognet */ 189150874Scognetlong 190295752Sskranodeupdates(name, ino, type) 191150874Scognet char *name; 192150874Scognet ino_t ino; 193291406Sjhb int type; 194291406Sjhb{ 195129207Scognet register struct entry *ep, *np, *ip; 196150874Scognet long descend = GOOD; 197291406Sjhb int lookuptype = 0; 198291406Sjhb int key = 0; 199291406Sjhb /* key values */ 200291406Sjhb# define ONTAPE 0x1 /* inode is on the tape */ 201129207Scognet# define INOFND 0x2 /* inode already exists */ 202150874Scognet# define NAMEFND 0x4 /* name already exists */ 203291406Sjhb# define MODECHG 0x8 /* mode of inode changed */ 204291406Sjhb 205150874Scognet /* 206150874Scognet * This routine is called once for each element in the 207150874Scognet * directory hierarchy, with a full path name. 208150874Scognet * The "type" value is incorrectly specified as LEAF for 209291406Sjhb * directories that are not on the dump tape. 210291406Sjhb * 211150874Scognet * Check to see if the file is on the tape. 212291406Sjhb */ 213291406Sjhb if (TSTINO(ino, dumpmap)) 214291406Sjhb key |= ONTAPE; 215291406Sjhb /* 216150874Scognet * Check to see if the name exists, and if the name is a link. 217150874Scognet */ 218291406Sjhb np = lookupname(name); 219150874Scognet if (np != NULL) { 220150874Scognet key |= NAMEFND; 221150874Scognet ip = lookupino(np->e_ino); 222291406Sjhb if (ip == NULL) 223291406Sjhb panic("corrupted symbol table\n"); 224291406Sjhb if (ip != np) 225150874Scognet lookuptype = LINK; 226291406Sjhb } 227291406Sjhb /* 228150874Scognet * Check to see if the inode exists, and if one of its links 229291406Sjhb * corresponds to the name (if one was found). 230129207Scognet */ 231129207Scognet ip = lookupino(ino); 232129207Scognet if (ip != NULL) { 233129207Scognet key |= INOFND; 234129207Scognet for (ep = ip->e_links; ep != NULL; ep = ep->e_links) { 235129207Scognet if (ep == np) { 236129207Scognet ip = ep; 237129207Scognet break; 238217744Suqs } 239129207Scognet } 240217744Suqs } 241129207Scognet /* 242129207Scognet * If both a name and an inode are found, but they do not 243129207Scognet * correspond to the same file, then both the inode that has 244129207Scognet * been found and the inode corresponding to the name that 245129207Scognet * has been found need to be renamed. The current pathname 246129207Scognet * is the new name for the inode that has been found. Since 247129207Scognet * all files to be deleted have already been removed, the 248129207Scognet * named file is either a now unneeded link, or it must live 249217744Suqs * under a new name in this dump level. If it is a link, it 250291406Sjhb * can be removed. If it is not a link, it is given a 251291406Sjhb * temporary name in anticipation that it will be renamed 252291406Sjhb * when it is later found by inode number. 253291406Sjhb */ 254291406Sjhb if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) { 255291406Sjhb if (lookuptype == LINK) { 256291406Sjhb removeleaf(np); 257291406Sjhb freeentry(np); 258291406Sjhb } else { 259291406Sjhb dprintf(stdout, "name/inode conflict, mktempname %s\n", 260291406Sjhb myname(np)); 261291406Sjhb mktempname(np); 262291406Sjhb } 263291406Sjhb np = NULL; 264291406Sjhb key &= ~NAMEFND; 265291406Sjhb } 266291406Sjhb if ((key & ONTAPE) && 267291406Sjhb (((key & INOFND) && ip->e_type != type) || 268291406Sjhb ((key & NAMEFND) && np->e_type != type))) 269291406Sjhb key |= MODECHG; 270291406Sjhb 271291406Sjhb /* 272291406Sjhb * Decide on the disposition of the file based on its flags. 273291406Sjhb * Note that we have already handled the case in which 274291406Sjhb * 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 748 vprintf(stdout, "Add links\n"); 749 for (i = ROOTINO; i < maxino; i++) { 750 ep = lookupino(i); 751 if (ep == NULL) 752 continue; 753 for (np = ep->e_links; np != NULL; np = np->e_links) { 754 if ((np->e_flags & NEW) == 0) 755 continue; 756 (void) strcpy(name, myname(ep)); 757 if (ep->e_type == NODE) { 758 (void) linkit(name, myname(np), SYMLINK); 759 } else { 760 (void) linkit(name, myname(np), HARDLINK); 761 } 762 np->e_flags &= ~NEW; 763 } 764 } 765} 766 767/* 768 * Check the symbol table. 769 * We do this to insure that all the requested work was done, and 770 * that no temporary names remain. 771 */ 772void 773checkrestore() 774{ 775 register struct entry *ep; 776 register ino_t i; 777 778 vprintf(stdout, "Check the symbol table.\n"); 779 for (i = ROOTINO; i < maxino; i++) { 780 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 781 ep->e_flags &= ~KEEP; 782 if (ep->e_type == NODE) 783 ep->e_flags &= ~(NEW|EXISTED); 784 if (ep->e_flags != NULL) 785 badentry(ep, "incomplete operations"); 786 } 787 } 788} 789 790/* 791 * Compare with the directory structure on the tape 792 * A paranoid check that things are as they should be. 793 */ 794long 795verifyfile(name, ino, type) 796 char *name; 797 ino_t ino; 798 int type; 799{ 800 struct entry *np, *ep; 801 long descend = GOOD; 802 803 ep = lookupname(name); 804 if (ep == NULL) { 805 fprintf(stderr, "Warning: missing name %s\n", name); 806 return (FAIL); 807 } 808 np = lookupino(ino); 809 if (np != ep) 810 descend = FAIL; 811 for ( ; np != NULL; np = np->e_links) 812 if (np == ep) 813 break; 814 if (np == NULL) 815 panic("missing inumber %d\n", ino); 816 if (ep->e_type == LEAF && type != LEAF) 817 badentry(ep, "type should be LEAF"); 818 return (descend); 819} 820