1331722Seadler/* 21558Srgrimes * Copyright (c) 1983, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 4. Neither the name of the University nor the names of its contributors 141558Srgrimes * may be used to endorse or promote products derived from this software 151558Srgrimes * without specific prior written permission. 161558Srgrimes * 171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271558Srgrimes * SUCH DAMAGE. 281558Srgrimes */ 291558Srgrimes 301558Srgrimes#ifndef lint 3137906Scharnier#if 0 3223685Speterstatic char sccsid[] = "@(#)restore.c 8.3 (Berkeley) 9/13/94"; 3337906Scharnier#endif 341558Srgrimes#endif /* not lint */ 351558Srgrimes 36146754Scharnier#include <sys/cdefs.h> 37146754Scharnier__FBSDID("$FreeBSD$"); 38146754Scharnier 391558Srgrimes#include <sys/types.h> 401558Srgrimes 41103949Smike#include <limits.h> 42241013Smdf#include <stdint.h> 431558Srgrimes#include <stdio.h> 441558Srgrimes#include <string.h> 451558Srgrimes 4698542Smckusick#include <ufs/ufs/dinode.h> 4798542Smckusick 481558Srgrimes#include "restore.h" 491558Srgrimes#include "extern.h" 501558Srgrimes 5192837Simpstatic char *keyval(int); 521558Srgrimes 531558Srgrimes/* 541558Srgrimes * This implements the 't' option. 551558Srgrimes * List entries on the tape. 561558Srgrimes */ 571558Srgrimeslong 5892837Simplistfile(char *name, ino_t ino, int type) 591558Srgrimes{ 601558Srgrimes long descend = hflag ? GOOD : FAIL; 611558Srgrimes 621558Srgrimes if (TSTINO(ino, dumpmap) == 0) 631558Srgrimes return (descend); 641558Srgrimes vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir "); 65241013Smdf fprintf(stdout, "%10ju\t%s\n", (uintmax_t)ino, name); 661558Srgrimes return (descend); 671558Srgrimes} 681558Srgrimes 691558Srgrimes/* 701558Srgrimes * This implements the 'x' option. 711558Srgrimes * Request that new entries be extracted. 721558Srgrimes */ 731558Srgrimeslong 7492837Simpaddfile(char *name, ino_t ino, int type) 751558Srgrimes{ 7692806Sobrien struct entry *ep; 771558Srgrimes long descend = hflag ? GOOD : FAIL; 781558Srgrimes char buf[100]; 791558Srgrimes 801558Srgrimes if (TSTINO(ino, dumpmap) == 0) { 811558Srgrimes dprintf(stdout, "%s: not on the tape\n", name); 821558Srgrimes return (descend); 831558Srgrimes } 8423685Speter if (ino == WINO && command == 'i' && !vflag) 8523685Speter return (descend); 861558Srgrimes if (!mflag) { 87241013Smdf (void) sprintf(buf, "./%ju", (uintmax_t)ino); 881558Srgrimes name = buf; 891558Srgrimes if (type == NODE) { 901558Srgrimes (void) genliteraldir(name, ino); 911558Srgrimes return (descend); 921558Srgrimes } 931558Srgrimes } 941558Srgrimes ep = lookupino(ino); 951558Srgrimes if (ep != NULL) { 961558Srgrimes if (strcmp(name, myname(ep)) == 0) { 971558Srgrimes ep->e_flags |= NEW; 981558Srgrimes return (descend); 991558Srgrimes } 1001558Srgrimes type |= LINK; 1011558Srgrimes } 1021558Srgrimes ep = addentry(name, ino, type); 1031558Srgrimes if (type == NODE) 1041558Srgrimes newnode(ep); 1051558Srgrimes ep->e_flags |= NEW; 1061558Srgrimes return (descend); 1071558Srgrimes} 1081558Srgrimes 1091558Srgrimes/* 1101558Srgrimes * This is used by the 'i' option to undo previous requests made by addfile. 1111558Srgrimes * Delete entries from the request queue. 1121558Srgrimes */ 1131558Srgrimes/* ARGSUSED */ 1141558Srgrimeslong 11592837Simpdeletefile(char *name, ino_t ino, int type) 1161558Srgrimes{ 1171558Srgrimes long descend = hflag ? GOOD : FAIL; 1181558Srgrimes struct entry *ep; 1191558Srgrimes 1201558Srgrimes if (TSTINO(ino, dumpmap) == 0) 1211558Srgrimes return (descend); 12223685Speter ep = lookupname(name); 12323685Speter if (ep != NULL) { 1241558Srgrimes ep->e_flags &= ~NEW; 12523685Speter ep->e_flags |= REMOVED; 12623685Speter if (ep->e_type != NODE) 12723685Speter freeentry(ep); 12823685Speter } 1291558Srgrimes return (descend); 1301558Srgrimes} 1311558Srgrimes 1328871Srgrimes/* 1331558Srgrimes * The following four routines implement the incremental 1341558Srgrimes * restore algorithm. The first removes old entries, the second 1351558Srgrimes * does renames and calculates the extraction list, the third 1361558Srgrimes * cleans up link names missed by the first two, and the final 1371558Srgrimes * one deletes old directories. 1381558Srgrimes * 1391558Srgrimes * Directories cannot be immediately deleted, as they may have 1401558Srgrimes * other files in them which need to be moved out first. As 1418871Srgrimes * directories to be deleted are found, they are put on the 1421558Srgrimes * following deletion list. After all deletions and renames 1431558Srgrimes * are done, this list is actually deleted. 1441558Srgrimes */ 1451558Srgrimesstatic struct entry *removelist; 1461558Srgrimes 1471558Srgrimes/* 14823685Speter * Remove invalid whiteouts from the old tree. 1491558Srgrimes * Remove unneeded leaves from the old tree. 1501558Srgrimes * Remove directories from the lookup chains. 1511558Srgrimes */ 1521558Srgrimesvoid 15392837Simpremoveoldleaves(void) 1541558Srgrimes{ 15592806Sobrien struct entry *ep, *nextep; 15692806Sobrien ino_t i, mydirino; 1571558Srgrimes 1581558Srgrimes vprintf(stdout, "Mark entries to be removed.\n"); 15937906Scharnier if ((ep = lookupino(WINO))) { 16023685Speter vprintf(stdout, "Delete whiteouts\n"); 16123685Speter for ( ; ep != NULL; ep = nextep) { 16223685Speter nextep = ep->e_links; 16323685Speter mydirino = ep->e_parent->e_ino; 16423685Speter /* 16523685Speter * We remove all whiteouts that are in directories 16623685Speter * that have been removed or that have been dumped. 16723685Speter */ 16823685Speter if (TSTINO(mydirino, usedinomap) && 16923685Speter !TSTINO(mydirino, dumpmap)) 17023685Speter continue; 17123685Speter delwhiteout(ep); 17223685Speter freeentry(ep); 17323685Speter } 17423685Speter } 1751558Srgrimes for (i = ROOTINO + 1; i < maxino; i++) { 1761558Srgrimes ep = lookupino(i); 1771558Srgrimes if (ep == NULL) 1781558Srgrimes continue; 17923685Speter if (TSTINO(i, usedinomap)) 1801558Srgrimes continue; 1811558Srgrimes for ( ; ep != NULL; ep = ep->e_links) { 1821558Srgrimes dprintf(stdout, "%s: REMOVE\n", myname(ep)); 1831558Srgrimes if (ep->e_type == LEAF) { 1841558Srgrimes removeleaf(ep); 1851558Srgrimes freeentry(ep); 1861558Srgrimes } else { 1871558Srgrimes mktempname(ep); 1881558Srgrimes deleteino(ep->e_ino); 1891558Srgrimes ep->e_next = removelist; 1901558Srgrimes removelist = ep; 1911558Srgrimes } 1921558Srgrimes } 1931558Srgrimes } 1941558Srgrimes} 1951558Srgrimes 1961558Srgrimes/* 1971558Srgrimes * For each directory entry on the incremental tape, determine which 1981558Srgrimes * category it falls into as follows: 1991558Srgrimes * KEEP - entries that are to be left alone. 2001558Srgrimes * NEW - new entries to be added. 2011558Srgrimes * EXTRACT - files that must be updated with new contents. 2021558Srgrimes * LINK - new links to be added. 2031558Srgrimes * Renames are done at the same time. 2041558Srgrimes */ 2051558Srgrimeslong 20692837Simpnodeupdates(char *name, ino_t ino, int type) 2071558Srgrimes{ 20892806Sobrien struct entry *ep, *np, *ip; 2091558Srgrimes long descend = GOOD; 2101558Srgrimes int lookuptype = 0; 2111558Srgrimes int key = 0; 2121558Srgrimes /* key values */ 2131558Srgrimes# define ONTAPE 0x1 /* inode is on the tape */ 2141558Srgrimes# define INOFND 0x2 /* inode already exists */ 2151558Srgrimes# define NAMEFND 0x4 /* name already exists */ 2161558Srgrimes# define MODECHG 0x8 /* mode of inode changed */ 2171558Srgrimes 2181558Srgrimes /* 2198871Srgrimes * This routine is called once for each element in the 2201558Srgrimes * directory hierarchy, with a full path name. 2211558Srgrimes * The "type" value is incorrectly specified as LEAF for 2221558Srgrimes * directories that are not on the dump tape. 2231558Srgrimes * 2241558Srgrimes * Check to see if the file is on the tape. 2251558Srgrimes */ 2261558Srgrimes if (TSTINO(ino, dumpmap)) 2271558Srgrimes key |= ONTAPE; 2281558Srgrimes /* 2291558Srgrimes * Check to see if the name exists, and if the name is a link. 2301558Srgrimes */ 2311558Srgrimes np = lookupname(name); 2321558Srgrimes if (np != NULL) { 2331558Srgrimes key |= NAMEFND; 2341558Srgrimes ip = lookupino(np->e_ino); 2351558Srgrimes if (ip == NULL) 2361558Srgrimes panic("corrupted symbol table\n"); 2371558Srgrimes if (ip != np) 2381558Srgrimes lookuptype = LINK; 2391558Srgrimes } 2401558Srgrimes /* 2411558Srgrimes * Check to see if the inode exists, and if one of its links 2421558Srgrimes * corresponds to the name (if one was found). 2431558Srgrimes */ 2441558Srgrimes ip = lookupino(ino); 2451558Srgrimes if (ip != NULL) { 2461558Srgrimes key |= INOFND; 2471558Srgrimes for (ep = ip->e_links; ep != NULL; ep = ep->e_links) { 2481558Srgrimes if (ep == np) { 2491558Srgrimes ip = ep; 2501558Srgrimes break; 2511558Srgrimes } 2521558Srgrimes } 2531558Srgrimes } 2541558Srgrimes /* 2551558Srgrimes * If both a name and an inode are found, but they do not 2561558Srgrimes * correspond to the same file, then both the inode that has 2571558Srgrimes * been found and the inode corresponding to the name that 2581558Srgrimes * has been found need to be renamed. The current pathname 2591558Srgrimes * is the new name for the inode that has been found. Since 2601558Srgrimes * all files to be deleted have already been removed, the 2611558Srgrimes * named file is either a now unneeded link, or it must live 2621558Srgrimes * under a new name in this dump level. If it is a link, it 2631558Srgrimes * can be removed. If it is not a link, it is given a 2641558Srgrimes * temporary name in anticipation that it will be renamed 2651558Srgrimes * when it is later found by inode number. 2661558Srgrimes */ 2671558Srgrimes if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) { 2681558Srgrimes if (lookuptype == LINK) { 2691558Srgrimes removeleaf(np); 2701558Srgrimes freeentry(np); 2711558Srgrimes } else { 2721558Srgrimes dprintf(stdout, "name/inode conflict, mktempname %s\n", 2731558Srgrimes myname(np)); 2741558Srgrimes mktempname(np); 2751558Srgrimes } 2761558Srgrimes np = NULL; 2771558Srgrimes key &= ~NAMEFND; 2781558Srgrimes } 2791558Srgrimes if ((key & ONTAPE) && 2801558Srgrimes (((key & INOFND) && ip->e_type != type) || 2811558Srgrimes ((key & NAMEFND) && np->e_type != type))) 2821558Srgrimes key |= MODECHG; 2831558Srgrimes 2841558Srgrimes /* 2851558Srgrimes * Decide on the disposition of the file based on its flags. 2861558Srgrimes * Note that we have already handled the case in which 2871558Srgrimes * a name and inode are found that correspond to different files. 2881558Srgrimes * Thus if both NAMEFND and INOFND are set then ip == np. 2891558Srgrimes */ 2901558Srgrimes switch (key) { 2911558Srgrimes 2921558Srgrimes /* 2931558Srgrimes * A previously existing file has been found. 2941558Srgrimes * Mark it as KEEP so that other links to the inode can be 2951558Srgrimes * detected, and so that it will not be reclaimed by the search 2961558Srgrimes * for unreferenced names. 2971558Srgrimes */ 2981558Srgrimes case INOFND|NAMEFND: 2991558Srgrimes ip->e_flags |= KEEP; 3001558Srgrimes dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 3011558Srgrimes flagvalues(ip)); 3021558Srgrimes break; 3031558Srgrimes 3041558Srgrimes /* 3051558Srgrimes * A file on the tape has a name which is the same as a name 3061558Srgrimes * corresponding to a different file in the previous dump. 3071558Srgrimes * Since all files to be deleted have already been removed, 3081558Srgrimes * this file is either a now unneeded link, or it must live 3091558Srgrimes * under a new name in this dump level. If it is a link, it 3101558Srgrimes * can simply be removed. If it is not a link, it is given a 3111558Srgrimes * temporary name in anticipation that it will be renamed 3121558Srgrimes * when it is later found by inode number (see INOFND case 3131558Srgrimes * below). The entry is then treated as a new file. 3141558Srgrimes */ 3151558Srgrimes case ONTAPE|NAMEFND: 3161558Srgrimes case ONTAPE|NAMEFND|MODECHG: 3171558Srgrimes if (lookuptype == LINK) { 3181558Srgrimes removeleaf(np); 3191558Srgrimes freeentry(np); 3201558Srgrimes } else { 3211558Srgrimes mktempname(np); 3221558Srgrimes } 323102411Scharnier /* FALLTHROUGH */ 3241558Srgrimes 3251558Srgrimes /* 3261558Srgrimes * A previously non-existent file. 327102231Strhodes * Add it to the file system, and request its extraction. 3281558Srgrimes * If it is a directory, create it immediately. 3291558Srgrimes * (Since the name is unused there can be no conflict) 3301558Srgrimes */ 3311558Srgrimes case ONTAPE: 3321558Srgrimes ep = addentry(name, ino, type); 3331558Srgrimes if (type == NODE) 3341558Srgrimes newnode(ep); 3351558Srgrimes ep->e_flags |= NEW|KEEP; 3361558Srgrimes dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 3371558Srgrimes flagvalues(ep)); 3381558Srgrimes break; 3391558Srgrimes 3401558Srgrimes /* 3411558Srgrimes * A file with the same inode number, but a different 3421558Srgrimes * name has been found. If the other name has not already 3431558Srgrimes * been found (indicated by the KEEP flag, see above) then 3441558Srgrimes * this must be a new name for the file, and it is renamed. 3451558Srgrimes * If the other name has been found then this must be a 3461558Srgrimes * link to the file. Hard links to directories are not 3471558Srgrimes * permitted, and are either deleted or converted to 3481558Srgrimes * symbolic links. Finally, if the file is on the tape, 3491558Srgrimes * a request is made to extract it. 3501558Srgrimes */ 3511558Srgrimes case ONTAPE|INOFND: 3521558Srgrimes if (type == LEAF && (ip->e_flags & KEEP) == 0) 3531558Srgrimes ip->e_flags |= EXTRACT; 354102411Scharnier /* FALLTHROUGH */ 3551558Srgrimes case INOFND: 3561558Srgrimes if ((ip->e_flags & KEEP) == 0) { 3571558Srgrimes renameit(myname(ip), name); 3581558Srgrimes moveentry(ip, name); 3591558Srgrimes ip->e_flags |= KEEP; 3601558Srgrimes dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 3611558Srgrimes flagvalues(ip)); 3621558Srgrimes break; 3631558Srgrimes } 3641558Srgrimes if (ip->e_type == NODE) { 3651558Srgrimes descend = FAIL; 3661558Srgrimes fprintf(stderr, 3671558Srgrimes "deleted hard link %s to directory %s\n", 3681558Srgrimes name, myname(ip)); 3691558Srgrimes break; 3701558Srgrimes } 3711558Srgrimes ep = addentry(name, ino, type|LINK); 3721558Srgrimes ep->e_flags |= NEW; 3731558Srgrimes dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name, 3741558Srgrimes flagvalues(ep)); 3751558Srgrimes break; 3761558Srgrimes 3771558Srgrimes /* 3781558Srgrimes * A previously known file which is to be updated. If it is a link, 3791558Srgrimes * then all names referring to the previous file must be removed 3801558Srgrimes * so that the subset of them that remain can be recreated. 3811558Srgrimes */ 3821558Srgrimes case ONTAPE|INOFND|NAMEFND: 3831558Srgrimes if (lookuptype == LINK) { 3841558Srgrimes removeleaf(np); 3851558Srgrimes freeentry(np); 3861558Srgrimes ep = addentry(name, ino, type|LINK); 3871558Srgrimes if (type == NODE) 3881558Srgrimes newnode(ep); 3891558Srgrimes ep->e_flags |= NEW|KEEP; 3901558Srgrimes dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name, 3911558Srgrimes flagvalues(ep)); 3921558Srgrimes break; 3931558Srgrimes } 3941558Srgrimes if (type == LEAF && lookuptype != LINK) 3951558Srgrimes np->e_flags |= EXTRACT; 3961558Srgrimes np->e_flags |= KEEP; 3971558Srgrimes dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 3981558Srgrimes flagvalues(np)); 3991558Srgrimes break; 4001558Srgrimes 4011558Srgrimes /* 4021558Srgrimes * An inode is being reused in a completely different way. 4031558Srgrimes * Normally an extract can simply do an "unlink" followed 4041558Srgrimes * by a "creat". Here we must do effectively the same 4051558Srgrimes * thing. The complications arise because we cannot really 4061558Srgrimes * delete a directory since it may still contain files 4071558Srgrimes * that we need to rename, so we delete it from the symbol 4081558Srgrimes * table, and put it on the list to be deleted eventually. 4091558Srgrimes * Conversely if a directory is to be created, it must be 4108871Srgrimes * done immediately, rather than waiting until the 4111558Srgrimes * extraction phase. 4121558Srgrimes */ 4131558Srgrimes case ONTAPE|INOFND|MODECHG: 4141558Srgrimes case ONTAPE|INOFND|NAMEFND|MODECHG: 4151558Srgrimes if (ip->e_flags & KEEP) { 4161558Srgrimes badentry(ip, "cannot KEEP and change modes"); 4171558Srgrimes break; 4181558Srgrimes } 4191558Srgrimes if (ip->e_type == LEAF) { 4201558Srgrimes /* changing from leaf to node */ 42128034Sjoerg for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) { 42228034Sjoerg if (ip->e_type != LEAF) 42328034Sjoerg badentry(ip, "NODE and LEAF links to same inode"); 42428034Sjoerg removeleaf(ip); 42528034Sjoerg freeentry(ip); 42628034Sjoerg } 4271558Srgrimes ip = addentry(name, ino, type); 4281558Srgrimes newnode(ip); 4291558Srgrimes } else { 4301558Srgrimes /* changing from node to leaf */ 4311558Srgrimes if ((ip->e_flags & TMPNAME) == 0) 4321558Srgrimes mktempname(ip); 4331558Srgrimes deleteino(ip->e_ino); 4341558Srgrimes ip->e_next = removelist; 4351558Srgrimes removelist = ip; 4361558Srgrimes ip = addentry(name, ino, type); 4371558Srgrimes } 4381558Srgrimes ip->e_flags |= NEW|KEEP; 4391558Srgrimes dprintf(stdout, "[%s] %s: %s\n", keyval(key), name, 4401558Srgrimes flagvalues(ip)); 4411558Srgrimes break; 4421558Srgrimes 4431558Srgrimes /* 44437906Scharnier * A hard link to a directory that has been removed. 4451558Srgrimes * Ignore it. 4461558Srgrimes */ 4471558Srgrimes case NAMEFND: 4481558Srgrimes dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key), 4491558Srgrimes name); 4501558Srgrimes descend = FAIL; 4511558Srgrimes break; 4521558Srgrimes 4531558Srgrimes /* 4541558Srgrimes * If we find a directory entry for a file that is not on 4551558Srgrimes * the tape, then we must have found a file that was created 4561558Srgrimes * while the dump was in progress. Since we have no contents 4571558Srgrimes * for it, we discard the name knowing that it will be on the 4581558Srgrimes * next incremental tape. 4591558Srgrimes */ 460102232Simp case 0: 461241013Smdf fprintf(stderr, "%s: (inode %ju) not found on tape\n", 462241013Smdf name, (uintmax_t)ino); 4631558Srgrimes break; 4641558Srgrimes 4651558Srgrimes /* 4661558Srgrimes * If any of these arise, something is grievously wrong with 4671558Srgrimes * the current state of the symbol table. 4681558Srgrimes */ 4691558Srgrimes case INOFND|NAMEFND|MODECHG: 4701558Srgrimes case NAMEFND|MODECHG: 4711558Srgrimes case INOFND|MODECHG: 4721558Srgrimes fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key), 4731558Srgrimes name); 4741558Srgrimes break; 4751558Srgrimes 4761558Srgrimes /* 4771558Srgrimes * These states "cannot" arise for any state of the symbol table. 4781558Srgrimes */ 4791558Srgrimes case ONTAPE|MODECHG: 4801558Srgrimes case MODECHG: 4811558Srgrimes default: 4821558Srgrimes panic("[%s] %s: impossible state\n", keyval(key), name); 4831558Srgrimes break; 4848871Srgrimes } 4851558Srgrimes return (descend); 4861558Srgrimes} 4871558Srgrimes 4881558Srgrimes/* 4891558Srgrimes * Calculate the active flags in a key. 4901558Srgrimes */ 4911558Srgrimesstatic char * 49292837Simpkeyval(int key) 4931558Srgrimes{ 4941558Srgrimes static char keybuf[32]; 4951558Srgrimes 4961558Srgrimes (void) strcpy(keybuf, "|NIL"); 4971558Srgrimes keybuf[0] = '\0'; 4981558Srgrimes if (key & ONTAPE) 4991558Srgrimes (void) strcat(keybuf, "|ONTAPE"); 5001558Srgrimes if (key & INOFND) 5011558Srgrimes (void) strcat(keybuf, "|INOFND"); 5021558Srgrimes if (key & NAMEFND) 5031558Srgrimes (void) strcat(keybuf, "|NAMEFND"); 5041558Srgrimes if (key & MODECHG) 5051558Srgrimes (void) strcat(keybuf, "|MODECHG"); 5061558Srgrimes return (&keybuf[1]); 5071558Srgrimes} 5081558Srgrimes 5091558Srgrimes/* 5101558Srgrimes * Find unreferenced link names. 5111558Srgrimes */ 5121558Srgrimesvoid 51392837Simpfindunreflinks(void) 5141558Srgrimes{ 51592806Sobrien struct entry *ep, *np; 51692806Sobrien ino_t i; 5171558Srgrimes 5181558Srgrimes vprintf(stdout, "Find unreferenced names.\n"); 5191558Srgrimes for (i = ROOTINO; i < maxino; i++) { 5201558Srgrimes ep = lookupino(i); 5211558Srgrimes if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0) 5221558Srgrimes continue; 5231558Srgrimes for (np = ep->e_entries; np != NULL; np = np->e_sibling) { 5241558Srgrimes if (np->e_flags == 0) { 5251558Srgrimes dprintf(stdout, 5261558Srgrimes "%s: remove unreferenced name\n", 5271558Srgrimes myname(np)); 5281558Srgrimes removeleaf(np); 5291558Srgrimes freeentry(np); 5301558Srgrimes } 5311558Srgrimes } 5321558Srgrimes } 5331558Srgrimes /* 5341558Srgrimes * Any leaves remaining in removed directories is unreferenced. 5351558Srgrimes */ 5361558Srgrimes for (ep = removelist; ep != NULL; ep = ep->e_next) { 5371558Srgrimes for (np = ep->e_entries; np != NULL; np = np->e_sibling) { 5381558Srgrimes if (np->e_type == LEAF) { 5391558Srgrimes if (np->e_flags != 0) 5401558Srgrimes badentry(np, "unreferenced with flags"); 5411558Srgrimes dprintf(stdout, 5421558Srgrimes "%s: remove unreferenced name\n", 5431558Srgrimes myname(np)); 5441558Srgrimes removeleaf(np); 5451558Srgrimes freeentry(np); 5461558Srgrimes } 5471558Srgrimes } 5481558Srgrimes } 5491558Srgrimes} 5501558Srgrimes 5511558Srgrimes/* 5521558Srgrimes * Remove old nodes (directories). 5531558Srgrimes * Note that this routine runs in O(N*D) where: 5541558Srgrimes * N is the number of directory entries to be removed. 5551558Srgrimes * D is the maximum depth of the tree. 5561558Srgrimes * If N == D this can be quite slow. If the list were 5571558Srgrimes * topologically sorted, the deletion could be done in 5581558Srgrimes * time O(N). 5591558Srgrimes */ 5601558Srgrimesvoid 56192837Simpremoveoldnodes(void) 5621558Srgrimes{ 56392806Sobrien struct entry *ep, **prev; 5641558Srgrimes long change; 5651558Srgrimes 5661558Srgrimes vprintf(stdout, "Remove old nodes (directories).\n"); 5671558Srgrimes do { 5681558Srgrimes change = 0; 5691558Srgrimes prev = &removelist; 5701558Srgrimes for (ep = removelist; ep != NULL; ep = *prev) { 5711558Srgrimes if (ep->e_entries != NULL) { 5721558Srgrimes prev = &ep->e_next; 5731558Srgrimes continue; 5741558Srgrimes } 5751558Srgrimes *prev = ep->e_next; 5761558Srgrimes removenode(ep); 5771558Srgrimes freeentry(ep); 5781558Srgrimes change++; 5791558Srgrimes } 5801558Srgrimes } while (change); 5811558Srgrimes for (ep = removelist; ep != NULL; ep = ep->e_next) 5821558Srgrimes badentry(ep, "cannot remove, non-empty"); 5831558Srgrimes} 5841558Srgrimes 5851558Srgrimes/* 5861558Srgrimes * This is the routine used to extract files for the 'r' command. 5871558Srgrimes * Extract new leaves. 5881558Srgrimes */ 5891558Srgrimesvoid 59092837Simpcreateleaves(char *symtabfile) 5911558Srgrimes{ 59292806Sobrien struct entry *ep; 5931558Srgrimes ino_t first; 5941558Srgrimes long curvol; 5951558Srgrimes 5961558Srgrimes if (command == 'R') { 5971558Srgrimes vprintf(stdout, "Continue extraction of new leaves\n"); 5981558Srgrimes } else { 5991558Srgrimes vprintf(stdout, "Extract new leaves.\n"); 6001558Srgrimes dumpsymtable(symtabfile, volno); 6011558Srgrimes } 6021558Srgrimes first = lowerbnd(ROOTINO); 6031558Srgrimes curvol = volno; 6041558Srgrimes while (curfile.ino < maxino) { 6051558Srgrimes first = lowerbnd(first); 6061558Srgrimes /* 6071558Srgrimes * If the next available file is not the one which we 6081558Srgrimes * expect then we have missed one or more files. Since 6091558Srgrimes * we do not request files that were not on the tape, 6101558Srgrimes * the lost files must have been due to a tape read error, 6111558Srgrimes * or a file that was removed while the dump was in progress. 6121558Srgrimes */ 6131558Srgrimes while (first < curfile.ino) { 6141558Srgrimes ep = lookupino(first); 6151558Srgrimes if (ep == NULL) 616241013Smdf panic("%ju: bad first\n", (uintmax_t)first); 6171558Srgrimes fprintf(stderr, "%s: not found on tape\n", myname(ep)); 6181558Srgrimes ep->e_flags &= ~(NEW|EXTRACT); 6191558Srgrimes first = lowerbnd(first); 6201558Srgrimes } 6211558Srgrimes /* 6221558Srgrimes * If we find files on the tape that have no corresponding 6231558Srgrimes * directory entries, then we must have found a file that 6248871Srgrimes * was created while the dump was in progress. Since we have 6251558Srgrimes * no name for it, we discard it knowing that it will be 6261558Srgrimes * on the next incremental tape. 6271558Srgrimes */ 6281558Srgrimes if (first != curfile.ino) { 629241013Smdf fprintf(stderr, "expected next file %ju, got %ju\n", 630241013Smdf (uintmax_t)first, (uintmax_t)curfile.ino); 6311558Srgrimes skipfile(); 6321558Srgrimes goto next; 6331558Srgrimes } 6341558Srgrimes ep = lookupino(curfile.ino); 6351558Srgrimes if (ep == NULL) 6361558Srgrimes panic("unknown file on tape\n"); 6371558Srgrimes if ((ep->e_flags & (NEW|EXTRACT)) == 0) 6381558Srgrimes badentry(ep, "unexpected file on tape"); 6391558Srgrimes /* 6401558Srgrimes * If the file is to be extracted, then the old file must 6411558Srgrimes * be removed since its type may change from one leaf type 64237906Scharnier * to another (e.g. "file" to "character special"). 6431558Srgrimes */ 6441558Srgrimes if ((ep->e_flags & EXTRACT) != 0) { 6451558Srgrimes removeleaf(ep); 6461558Srgrimes ep->e_flags &= ~REMOVED; 6471558Srgrimes } 6481558Srgrimes (void) extractfile(myname(ep)); 6491558Srgrimes ep->e_flags &= ~(NEW|EXTRACT); 6501558Srgrimes /* 6511558Srgrimes * We checkpoint the restore after every tape reel, so 65237906Scharnier * as to simplify the amount of work required by the 6531558Srgrimes * 'R' command. 6541558Srgrimes */ 6551558Srgrimes next: 6561558Srgrimes if (curvol != volno) { 6571558Srgrimes dumpsymtable(symtabfile, volno); 6581558Srgrimes skipmaps(); 6591558Srgrimes curvol = volno; 6601558Srgrimes } 6611558Srgrimes } 6621558Srgrimes} 6631558Srgrimes 6641558Srgrimes/* 6651558Srgrimes * This is the routine used to extract files for the 'x' and 'i' commands. 6661558Srgrimes * Efficiently extract a subset of the files on a tape. 6671558Srgrimes */ 6681558Srgrimesvoid 66992837Simpcreatefiles(void) 6701558Srgrimes{ 67192806Sobrien ino_t first, next, last; 67292806Sobrien struct entry *ep; 6731558Srgrimes long curvol; 6741558Srgrimes 6751558Srgrimes vprintf(stdout, "Extract requested files\n"); 6761558Srgrimes curfile.action = SKIP; 6771558Srgrimes getvol((long)1); 6781558Srgrimes skipmaps(); 6791558Srgrimes skipdirs(); 6801558Srgrimes first = lowerbnd(ROOTINO); 6811558Srgrimes last = upperbnd(maxino - 1); 6821558Srgrimes for (;;) { 68390642Siedowse curvol = volno; 6841558Srgrimes first = lowerbnd(first); 6851558Srgrimes last = upperbnd(last); 6861558Srgrimes /* 6871558Srgrimes * Check to see if any files remain to be extracted 6881558Srgrimes */ 6891558Srgrimes if (first > last) 6901558Srgrimes return; 691164911Sdwmalone if (Dflag) { 692164911Sdwmalone if (curfile.ino == maxino) 693164911Sdwmalone return; 694164911Sdwmalone if((ep = lookupino(curfile.ino)) != NULL && 695164911Sdwmalone (ep->e_flags & (NEW|EXTRACT))) { 696164911Sdwmalone goto justgetit; 697164911Sdwmalone } else { 698164911Sdwmalone skipfile(); 699164911Sdwmalone continue; 700164911Sdwmalone } 701164911Sdwmalone } 7021558Srgrimes /* 70390642Siedowse * Reject any volumes with inodes greater than the last 70490642Siedowse * one needed, so that we can quickly skip backwards to 70590642Siedowse * a volume containing useful inodes. We can't do this 70690642Siedowse * if there are no further volumes available (curfile.ino 70790642Siedowse * >= maxino) or if we are already at the first tape. 7081558Srgrimes */ 70990642Siedowse if (curfile.ino > last && curfile.ino < maxino && volno > 1) { 7101558Srgrimes curfile.action = SKIP; 7111558Srgrimes getvol((long)0); 7121558Srgrimes skipmaps(); 7131558Srgrimes skipdirs(); 71490642Siedowse continue; 7151558Srgrimes } 7161558Srgrimes /* 7171558Srgrimes * Decide on the next inode needed. 7181558Srgrimes * Skip across the inodes until it is found 71990642Siedowse * or a volume change is encountered 7201558Srgrimes */ 72190642Siedowse if (curfile.ino < maxino) { 72290642Siedowse next = lowerbnd(curfile.ino); 7231558Srgrimes while (next > curfile.ino && volno == curvol) 7241558Srgrimes skipfile(); 72590642Siedowse if (volno != curvol) { 72690642Siedowse skipmaps(); 72790642Siedowse skipdirs(); 72890642Siedowse continue; 72990642Siedowse } 73090642Siedowse } else { 73190642Siedowse /* 73290642Siedowse * No further volumes or inodes available. Set 73390642Siedowse * `next' to the first inode, so that a warning 73490642Siedowse * is emitted below for each missing file. 73590642Siedowse */ 73690642Siedowse next = first; 73790642Siedowse } 7381558Srgrimes /* 7391558Srgrimes * If the current inode is greater than the one we were 7401558Srgrimes * looking for then we missed the one we were looking for. 7411558Srgrimes * Since we only attempt to extract files listed in the 7421558Srgrimes * dump map, the lost files must have been due to a tape 7431558Srgrimes * read error, or a file that was removed while the dump 7441558Srgrimes * was in progress. Thus we report all requested files 7451558Srgrimes * between the one we were looking for, and the one we 7461558Srgrimes * found as missing, and delete their request flags. 7471558Srgrimes */ 7481558Srgrimes while (next < curfile.ino) { 7491558Srgrimes ep = lookupino(next); 7501558Srgrimes if (ep == NULL) 7511558Srgrimes panic("corrupted symbol table\n"); 7521558Srgrimes fprintf(stderr, "%s: not found on tape\n", myname(ep)); 7531558Srgrimes ep->e_flags &= ~NEW; 7541558Srgrimes next = lowerbnd(next); 7551558Srgrimes } 7561558Srgrimes /* 7571558Srgrimes * The current inode is the one that we are looking for, 7581558Srgrimes * so extract it per its requested name. 7591558Srgrimes */ 7601558Srgrimes if (next == curfile.ino && next <= last) { 7611558Srgrimes ep = lookupino(next); 7621558Srgrimes if (ep == NULL) 7631558Srgrimes panic("corrupted symbol table\n"); 764164911Sdwmalonejustgetit: 7651558Srgrimes (void) extractfile(myname(ep)); 7661558Srgrimes ep->e_flags &= ~NEW; 7671558Srgrimes if (volno != curvol) 7681558Srgrimes skipmaps(); 7691558Srgrimes } 7701558Srgrimes } 7711558Srgrimes} 7721558Srgrimes 7731558Srgrimes/* 7741558Srgrimes * Add links. 7751558Srgrimes */ 7761558Srgrimesvoid 77792837Simpcreatelinks(void) 7781558Srgrimes{ 77992806Sobrien struct entry *np, *ep; 78092806Sobrien ino_t i; 7811558Srgrimes char name[BUFSIZ]; 7821558Srgrimes 78337906Scharnier if ((ep = lookupino(WINO))) { 78423685Speter vprintf(stdout, "Add whiteouts\n"); 78523685Speter for ( ; ep != NULL; ep = ep->e_links) { 78623685Speter if ((ep->e_flags & NEW) == 0) 78723685Speter continue; 78823685Speter (void) addwhiteout(myname(ep)); 78923685Speter ep->e_flags &= ~NEW; 79023685Speter } 79123685Speter } 7921558Srgrimes vprintf(stdout, "Add links\n"); 7931558Srgrimes for (i = ROOTINO; i < maxino; i++) { 7941558Srgrimes ep = lookupino(i); 7951558Srgrimes if (ep == NULL) 7961558Srgrimes continue; 7971558Srgrimes for (np = ep->e_links; np != NULL; np = np->e_links) { 7981558Srgrimes if ((np->e_flags & NEW) == 0) 7991558Srgrimes continue; 8001558Srgrimes (void) strcpy(name, myname(ep)); 8011558Srgrimes if (ep->e_type == NODE) { 8021558Srgrimes (void) linkit(name, myname(np), SYMLINK); 8031558Srgrimes } else { 8041558Srgrimes (void) linkit(name, myname(np), HARDLINK); 8051558Srgrimes } 8061558Srgrimes np->e_flags &= ~NEW; 8071558Srgrimes } 8081558Srgrimes } 8091558Srgrimes} 8101558Srgrimes 8111558Srgrimes/* 8121558Srgrimes * Check the symbol table. 8131558Srgrimes * We do this to insure that all the requested work was done, and 8141558Srgrimes * that no temporary names remain. 8151558Srgrimes */ 8161558Srgrimesvoid 81792837Simpcheckrestore(void) 8181558Srgrimes{ 81992806Sobrien struct entry *ep; 82092806Sobrien ino_t i; 8211558Srgrimes 8221558Srgrimes vprintf(stdout, "Check the symbol table.\n"); 82323685Speter for (i = WINO; i < maxino; i++) { 8241558Srgrimes for (ep = lookupino(i); ep != NULL; ep = ep->e_links) { 8251558Srgrimes ep->e_flags &= ~KEEP; 8261558Srgrimes if (ep->e_type == NODE) 8271558Srgrimes ep->e_flags &= ~(NEW|EXISTED); 82829574Sphk if (ep->e_flags != 0) 8291558Srgrimes badentry(ep, "incomplete operations"); 8301558Srgrimes } 8311558Srgrimes } 8321558Srgrimes} 8331558Srgrimes 8341558Srgrimes/* 8351558Srgrimes * Compare with the directory structure on the tape 8361558Srgrimes * A paranoid check that things are as they should be. 8371558Srgrimes */ 8381558Srgrimeslong 83992837Simpverifyfile(char *name, ino_t ino, int type) 8401558Srgrimes{ 8411558Srgrimes struct entry *np, *ep; 8421558Srgrimes long descend = GOOD; 8431558Srgrimes 8441558Srgrimes ep = lookupname(name); 8451558Srgrimes if (ep == NULL) { 8461558Srgrimes fprintf(stderr, "Warning: missing name %s\n", name); 8471558Srgrimes return (FAIL); 8481558Srgrimes } 8491558Srgrimes np = lookupino(ino); 8501558Srgrimes if (np != ep) 8511558Srgrimes descend = FAIL; 8521558Srgrimes for ( ; np != NULL; np = np->e_links) 8531558Srgrimes if (np == ep) 8541558Srgrimes break; 8551558Srgrimes if (np == NULL) 856241013Smdf panic("missing inumber %ju\n", (uintmax_t)ino); 8571558Srgrimes if (ep->e_type == LEAF && type != LEAF) 8581558Srgrimes badentry(ep, "type should be LEAF"); 8591558Srgrimes return (descend); 8601558Srgrimes} 861