Home | History | Annotate | Line # | Download | only in restore
restore.c revision 1.12
      1  1.12    lukem /*	$NetBSD: restore.c,v 1.12 1997/09/15 08:04:35 lukem Exp $	*/
      2   1.6      cgd 
      3   1.1      cgd /*
      4   1.4  mycroft  * Copyright (c) 1983, 1993
      5   1.4  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  */
     35   1.1      cgd 
     36  1.12    lukem #include <sys/cdefs.h>
     37   1.1      cgd #ifndef lint
     38   1.6      cgd #if 0
     39   1.6      cgd static char sccsid[] = "@(#)restore.c	8.3 (Berkeley) 9/13/94";
     40   1.6      cgd #else
     41  1.12    lukem __RCSID("$NetBSD: restore.c,v 1.12 1997/09/15 08:04:35 lukem Exp $");
     42   1.6      cgd #endif
     43   1.1      cgd #endif /* not lint */
     44   1.1      cgd 
     45   1.3      cgd #include <sys/types.h>
     46   1.3      cgd #include <sys/stat.h>
     47   1.3      cgd 
     48   1.4  mycroft #include <ufs/ufs/dinode.h>
     49   1.3      cgd 
     50   1.3      cgd #include <stdio.h>
     51   1.3      cgd #include <string.h>
     52   1.3      cgd 
     53   1.1      cgd #include "restore.h"
     54   1.3      cgd #include "extern.h"
     55   1.3      cgd 
     56   1.3      cgd static char *keyval __P((int));
     57   1.1      cgd 
     58   1.1      cgd /*
     59   1.1      cgd  * This implements the 't' option.
     60   1.1      cgd  * List entries on the tape.
     61   1.1      cgd  */
     62   1.1      cgd long
     63   1.1      cgd listfile(name, ino, type)
     64   1.1      cgd 	char *name;
     65   1.1      cgd 	ino_t ino;
     66   1.1      cgd 	int type;
     67   1.1      cgd {
     68   1.1      cgd 	long descend = hflag ? GOOD : FAIL;
     69   1.1      cgd 
     70   1.3      cgd 	if (TSTINO(ino, dumpmap) == 0)
     71   1.1      cgd 		return (descend);
     72   1.1      cgd 	vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
     73   1.1      cgd 	fprintf(stdout, "%10d\t%s\n", ino, name);
     74   1.1      cgd 	return (descend);
     75   1.1      cgd }
     76   1.1      cgd 
     77   1.1      cgd /*
     78   1.1      cgd  * This implements the 'x' option.
     79   1.1      cgd  * Request that new entries be extracted.
     80   1.1      cgd  */
     81   1.1      cgd long
     82   1.1      cgd addfile(name, ino, type)
     83   1.1      cgd 	char *name;
     84   1.1      cgd 	ino_t ino;
     85   1.1      cgd 	int type;
     86   1.1      cgd {
     87   1.8    lukem 	struct entry *ep;
     88   1.1      cgd 	long descend = hflag ? GOOD : FAIL;
     89   1.1      cgd 	char buf[100];
     90   1.1      cgd 
     91   1.3      cgd 	if (TSTINO(ino, dumpmap) == 0) {
     92   1.1      cgd 		dprintf(stdout, "%s: not on the tape\n", name);
     93   1.1      cgd 		return (descend);
     94   1.1      cgd 	}
     95   1.5  mycroft 	if (ino == WINO && command == 'i' && !vflag)
     96   1.5  mycroft 		return (descend);
     97   1.1      cgd 	if (!mflag) {
     98   1.7  thorpej 		(void) snprintf(buf, sizeof(buf), "./%u", ino);
     99   1.1      cgd 		name = buf;
    100   1.1      cgd 		if (type == NODE) {
    101   1.1      cgd 			(void) genliteraldir(name, ino);
    102   1.1      cgd 			return (descend);
    103   1.1      cgd 		}
    104   1.1      cgd 	}
    105   1.1      cgd 	ep = lookupino(ino);
    106   1.3      cgd 	if (ep != NULL) {
    107   1.1      cgd 		if (strcmp(name, myname(ep)) == 0) {
    108   1.1      cgd 			ep->e_flags |= NEW;
    109   1.1      cgd 			return (descend);
    110   1.1      cgd 		}
    111   1.1      cgd 		type |= LINK;
    112   1.1      cgd 	}
    113   1.1      cgd 	ep = addentry(name, ino, type);
    114   1.1      cgd 	if (type == NODE)
    115   1.1      cgd 		newnode(ep);
    116   1.1      cgd 	ep->e_flags |= NEW;
    117   1.1      cgd 	return (descend);
    118   1.1      cgd }
    119   1.1      cgd 
    120   1.1      cgd /*
    121   1.1      cgd  * This is used by the 'i' option to undo previous requests made by addfile.
    122   1.1      cgd  * Delete entries from the request queue.
    123   1.1      cgd  */
    124   1.1      cgd /* ARGSUSED */
    125   1.1      cgd long
    126   1.1      cgd deletefile(name, ino, type)
    127   1.1      cgd 	char *name;
    128   1.1      cgd 	ino_t ino;
    129   1.1      cgd 	int type;
    130   1.1      cgd {
    131   1.1      cgd 	long descend = hflag ? GOOD : FAIL;
    132   1.1      cgd 	struct entry *ep;
    133   1.1      cgd 
    134   1.3      cgd 	if (TSTINO(ino, dumpmap) == 0)
    135   1.1      cgd 		return (descend);
    136   1.5  mycroft 	ep = lookupname(name);
    137   1.5  mycroft 	if (ep != NULL) {
    138   1.1      cgd 		ep->e_flags &= ~NEW;
    139   1.5  mycroft 		ep->e_flags |= REMOVED;
    140   1.5  mycroft 		if (ep->e_type != NODE)
    141   1.5  mycroft 			freeentry(ep);
    142   1.5  mycroft 	}
    143   1.1      cgd 	return (descend);
    144   1.1      cgd }
    145   1.1      cgd 
    146   1.1      cgd /*
    147   1.1      cgd  * The following four routines implement the incremental
    148   1.1      cgd  * restore algorithm. The first removes old entries, the second
    149   1.1      cgd  * does renames and calculates the extraction list, the third
    150   1.1      cgd  * cleans up link names missed by the first two, and the final
    151   1.1      cgd  * one deletes old directories.
    152   1.1      cgd  *
    153   1.1      cgd  * Directories cannot be immediately deleted, as they may have
    154   1.1      cgd  * other files in them which need to be moved out first. As
    155   1.1      cgd  * directories to be deleted are found, they are put on the
    156   1.1      cgd  * following deletion list. After all deletions and renames
    157   1.1      cgd  * are done, this list is actually deleted.
    158   1.1      cgd  */
    159   1.1      cgd static struct entry *removelist;
    160   1.1      cgd 
    161   1.1      cgd /*
    162   1.5  mycroft  *	Remove invalid whiteouts from the old tree.
    163   1.1      cgd  *	Remove unneeded leaves from the old tree.
    164   1.1      cgd  *	Remove directories from the lookup chains.
    165   1.1      cgd  */
    166   1.3      cgd void
    167   1.1      cgd removeoldleaves()
    168   1.1      cgd {
    169   1.8    lukem 	struct entry *ep, *nextep;
    170   1.8    lukem 	ino_t i, mydirino;
    171   1.1      cgd 
    172   1.1      cgd 	vprintf(stdout, "Mark entries to be removed.\n");
    173  1.11    lukem 	if ((ep = lookupino(WINO)) != NULL) {
    174   1.5  mycroft 		vprintf(stdout, "Delete whiteouts\n");
    175   1.5  mycroft 		for ( ; ep != NULL; ep = nextep) {
    176   1.5  mycroft 			nextep = ep->e_links;
    177   1.5  mycroft 			mydirino = ep->e_parent->e_ino;
    178   1.5  mycroft 			/*
    179   1.5  mycroft 			 * We remove all whiteouts that are in directories
    180   1.5  mycroft 			 * that have been removed or that have been dumped.
    181   1.5  mycroft 			 */
    182   1.5  mycroft 			if (TSTINO(mydirino, usedinomap) &&
    183   1.5  mycroft 			    !TSTINO(mydirino, dumpmap))
    184   1.5  mycroft 				continue;
    185   1.5  mycroft 			delwhiteout(ep);
    186   1.5  mycroft 			freeentry(ep);
    187   1.5  mycroft 		}
    188   1.5  mycroft 	}
    189   1.1      cgd 	for (i = ROOTINO + 1; i < maxino; i++) {
    190   1.1      cgd 		ep = lookupino(i);
    191   1.3      cgd 		if (ep == NULL)
    192   1.1      cgd 			continue;
    193   1.5  mycroft 		if (TSTINO(i, usedinomap))
    194   1.1      cgd 			continue;
    195   1.3      cgd 		for ( ; ep != NULL; ep = ep->e_links) {
    196   1.1      cgd 			dprintf(stdout, "%s: REMOVE\n", myname(ep));
    197   1.1      cgd 			if (ep->e_type == LEAF) {
    198   1.1      cgd 				removeleaf(ep);
    199   1.1      cgd 				freeentry(ep);
    200   1.1      cgd 			} else {
    201   1.1      cgd 				mktempname(ep);
    202   1.1      cgd 				deleteino(ep->e_ino);
    203   1.1      cgd 				ep->e_next = removelist;
    204   1.1      cgd 				removelist = ep;
    205   1.1      cgd 			}
    206   1.1      cgd 		}
    207   1.1      cgd 	}
    208   1.1      cgd }
    209   1.1      cgd 
    210   1.1      cgd /*
    211   1.1      cgd  *	For each directory entry on the incremental tape, determine which
    212   1.1      cgd  *	category it falls into as follows:
    213   1.1      cgd  *	KEEP - entries that are to be left alone.
    214   1.1      cgd  *	NEW - new entries to be added.
    215   1.1      cgd  *	EXTRACT - files that must be updated with new contents.
    216   1.1      cgd  *	LINK - new links to be added.
    217   1.1      cgd  *	Renames are done at the same time.
    218   1.1      cgd  */
    219   1.1      cgd long
    220   1.1      cgd nodeupdates(name, ino, type)
    221   1.1      cgd 	char *name;
    222   1.1      cgd 	ino_t ino;
    223   1.1      cgd 	int type;
    224   1.1      cgd {
    225   1.8    lukem 	struct entry *ep, *np, *ip;
    226   1.1      cgd 	long descend = GOOD;
    227   1.1      cgd 	int lookuptype = 0;
    228   1.1      cgd 	int key = 0;
    229   1.1      cgd 		/* key values */
    230   1.1      cgd #		define ONTAPE	0x1	/* inode is on the tape */
    231   1.1      cgd #		define INOFND	0x2	/* inode already exists */
    232   1.1      cgd #		define NAMEFND	0x4	/* name already exists */
    233   1.1      cgd #		define MODECHG	0x8	/* mode of inode changed */
    234   1.1      cgd 
    235   1.1      cgd 	/*
    236   1.1      cgd 	 * This routine is called once for each element in the
    237   1.1      cgd 	 * directory hierarchy, with a full path name.
    238   1.1      cgd 	 * The "type" value is incorrectly specified as LEAF for
    239   1.1      cgd 	 * directories that are not on the dump tape.
    240   1.1      cgd 	 *
    241   1.1      cgd 	 * Check to see if the file is on the tape.
    242   1.1      cgd 	 */
    243   1.3      cgd 	if (TSTINO(ino, dumpmap))
    244   1.1      cgd 		key |= ONTAPE;
    245   1.1      cgd 	/*
    246   1.1      cgd 	 * Check to see if the name exists, and if the name is a link.
    247   1.1      cgd 	 */
    248   1.1      cgd 	np = lookupname(name);
    249   1.3      cgd 	if (np != NULL) {
    250   1.1      cgd 		key |= NAMEFND;
    251   1.1      cgd 		ip = lookupino(np->e_ino);
    252   1.1      cgd 		if (ip == NULL)
    253   1.1      cgd 			panic("corrupted symbol table\n");
    254   1.1      cgd 		if (ip != np)
    255   1.1      cgd 			lookuptype = LINK;
    256   1.1      cgd 	}
    257   1.1      cgd 	/*
    258   1.1      cgd 	 * Check to see if the inode exists, and if one of its links
    259   1.1      cgd 	 * corresponds to the name (if one was found).
    260   1.1      cgd 	 */
    261   1.1      cgd 	ip = lookupino(ino);
    262   1.3      cgd 	if (ip != NULL) {
    263   1.1      cgd 		key |= INOFND;
    264   1.3      cgd 		for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
    265   1.1      cgd 			if (ep == np) {
    266   1.1      cgd 				ip = ep;
    267   1.1      cgd 				break;
    268   1.1      cgd 			}
    269   1.1      cgd 		}
    270   1.1      cgd 	}
    271   1.1      cgd 	/*
    272   1.1      cgd 	 * If both a name and an inode are found, but they do not
    273   1.1      cgd 	 * correspond to the same file, then both the inode that has
    274   1.1      cgd 	 * been found and the inode corresponding to the name that
    275   1.1      cgd 	 * has been found need to be renamed. The current pathname
    276   1.1      cgd 	 * is the new name for the inode that has been found. Since
    277   1.1      cgd 	 * all files to be deleted have already been removed, the
    278   1.1      cgd 	 * named file is either a now unneeded link, or it must live
    279   1.1      cgd 	 * under a new name in this dump level. If it is a link, it
    280   1.1      cgd 	 * can be removed. If it is not a link, it is given a
    281   1.1      cgd 	 * temporary name in anticipation that it will be renamed
    282   1.1      cgd 	 * when it is later found by inode number.
    283   1.1      cgd 	 */
    284   1.1      cgd 	if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
    285   1.1      cgd 		if (lookuptype == LINK) {
    286   1.1      cgd 			removeleaf(np);
    287   1.1      cgd 			freeentry(np);
    288   1.1      cgd 		} else {
    289   1.1      cgd 			dprintf(stdout, "name/inode conflict, mktempname %s\n",
    290   1.1      cgd 				myname(np));
    291   1.1      cgd 			mktempname(np);
    292   1.1      cgd 		}
    293   1.3      cgd 		np = NULL;
    294   1.1      cgd 		key &= ~NAMEFND;
    295   1.1      cgd 	}
    296   1.1      cgd 	if ((key & ONTAPE) &&
    297   1.1      cgd 	  (((key & INOFND) && ip->e_type != type) ||
    298   1.1      cgd 	   ((key & NAMEFND) && np->e_type != type)))
    299   1.1      cgd 		key |= MODECHG;
    300   1.1      cgd 
    301   1.1      cgd 	/*
    302   1.1      cgd 	 * Decide on the disposition of the file based on its flags.
    303   1.1      cgd 	 * Note that we have already handled the case in which
    304   1.1      cgd 	 * a name and inode are found that correspond to different files.
    305   1.1      cgd 	 * Thus if both NAMEFND and INOFND are set then ip == np.
    306   1.1      cgd 	 */
    307   1.1      cgd 	switch (key) {
    308   1.1      cgd 
    309   1.1      cgd 	/*
    310   1.1      cgd 	 * A previously existing file has been found.
    311   1.1      cgd 	 * Mark it as KEEP so that other links to the inode can be
    312   1.1      cgd 	 * detected, and so that it will not be reclaimed by the search
    313   1.1      cgd 	 * for unreferenced names.
    314   1.1      cgd 	 */
    315   1.1      cgd 	case INOFND|NAMEFND:
    316   1.1      cgd 		ip->e_flags |= KEEP;
    317   1.1      cgd 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
    318   1.1      cgd 			flagvalues(ip));
    319   1.1      cgd 		break;
    320   1.1      cgd 
    321   1.1      cgd 	/*
    322   1.1      cgd 	 * A file on the tape has a name which is the same as a name
    323   1.1      cgd 	 * corresponding to a different file in the previous dump.
    324   1.1      cgd 	 * Since all files to be deleted have already been removed,
    325   1.1      cgd 	 * this file is either a now unneeded link, or it must live
    326   1.1      cgd 	 * under a new name in this dump level. If it is a link, it
    327   1.1      cgd 	 * can simply be removed. If it is not a link, it is given a
    328   1.1      cgd 	 * temporary name in anticipation that it will be renamed
    329   1.1      cgd 	 * when it is later found by inode number (see INOFND case
    330   1.1      cgd 	 * below). The entry is then treated as a new file.
    331   1.1      cgd 	 */
    332   1.1      cgd 	case ONTAPE|NAMEFND:
    333   1.1      cgd 	case ONTAPE|NAMEFND|MODECHG:
    334   1.1      cgd 		if (lookuptype == LINK) {
    335   1.1      cgd 			removeleaf(np);
    336   1.1      cgd 			freeentry(np);
    337   1.1      cgd 		} else {
    338   1.1      cgd 			mktempname(np);
    339   1.1      cgd 		}
    340   1.1      cgd 		/* fall through */
    341   1.1      cgd 
    342   1.1      cgd 	/*
    343   1.1      cgd 	 * A previously non-existent file.
    344   1.1      cgd 	 * Add it to the file system, and request its extraction.
    345   1.1      cgd 	 * If it is a directory, create it immediately.
    346   1.1      cgd 	 * (Since the name is unused there can be no conflict)
    347   1.1      cgd 	 */
    348   1.1      cgd 	case ONTAPE:
    349   1.1      cgd 		ep = addentry(name, ino, type);
    350   1.1      cgd 		if (type == NODE)
    351   1.1      cgd 			newnode(ep);
    352   1.1      cgd 		ep->e_flags |= NEW|KEEP;
    353   1.1      cgd 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
    354   1.1      cgd 			flagvalues(ep));
    355   1.1      cgd 		break;
    356   1.1      cgd 
    357   1.1      cgd 	/*
    358   1.1      cgd 	 * A file with the same inode number, but a different
    359   1.1      cgd 	 * name has been found. If the other name has not already
    360   1.1      cgd 	 * been found (indicated by the KEEP flag, see above) then
    361   1.1      cgd 	 * this must be a new name for the file, and it is renamed.
    362   1.1      cgd 	 * If the other name has been found then this must be a
    363   1.1      cgd 	 * link to the file. Hard links to directories are not
    364   1.1      cgd 	 * permitted, and are either deleted or converted to
    365   1.1      cgd 	 * symbolic links. Finally, if the file is on the tape,
    366   1.1      cgd 	 * a request is made to extract it.
    367   1.1      cgd 	 */
    368   1.1      cgd 	case ONTAPE|INOFND:
    369   1.1      cgd 		if (type == LEAF && (ip->e_flags & KEEP) == 0)
    370   1.1      cgd 			ip->e_flags |= EXTRACT;
    371   1.1      cgd 		/* fall through */
    372   1.1      cgd 	case INOFND:
    373   1.1      cgd 		if ((ip->e_flags & KEEP) == 0) {
    374   1.1      cgd 			renameit(myname(ip), name);
    375   1.1      cgd 			moveentry(ip, name);
    376   1.1      cgd 			ip->e_flags |= KEEP;
    377   1.1      cgd 			dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
    378   1.1      cgd 				flagvalues(ip));
    379   1.1      cgd 			break;
    380   1.1      cgd 		}
    381   1.1      cgd 		if (ip->e_type == NODE) {
    382   1.1      cgd 			descend = FAIL;
    383   1.1      cgd 			fprintf(stderr,
    384   1.1      cgd 				"deleted hard link %s to directory %s\n",
    385   1.1      cgd 				name, myname(ip));
    386   1.1      cgd 			break;
    387   1.1      cgd 		}
    388   1.1      cgd 		ep = addentry(name, ino, type|LINK);
    389   1.1      cgd 		ep->e_flags |= NEW;
    390   1.1      cgd 		dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
    391   1.1      cgd 			flagvalues(ep));
    392   1.1      cgd 		break;
    393   1.1      cgd 
    394   1.1      cgd 	/*
    395   1.3      cgd 	 * A previously known file which is to be updated. If it is a link,
    396   1.3      cgd 	 * then all names referring to the previous file must be removed
    397   1.3      cgd 	 * so that the subset of them that remain can be recreated.
    398   1.1      cgd 	 */
    399   1.1      cgd 	case ONTAPE|INOFND|NAMEFND:
    400   1.3      cgd 		if (lookuptype == LINK) {
    401   1.3      cgd 			removeleaf(np);
    402   1.3      cgd 			freeentry(np);
    403   1.3      cgd 			ep = addentry(name, ino, type|LINK);
    404   1.3      cgd 			if (type == NODE)
    405   1.3      cgd 			        newnode(ep);
    406   1.3      cgd 			ep->e_flags |= NEW|KEEP;
    407   1.3      cgd 			dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
    408   1.3      cgd 				flagvalues(ep));
    409   1.3      cgd 			break;
    410   1.3      cgd 		}
    411   1.1      cgd 		if (type == LEAF && lookuptype != LINK)
    412   1.1      cgd 			np->e_flags |= EXTRACT;
    413   1.1      cgd 		np->e_flags |= KEEP;
    414   1.1      cgd 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
    415   1.1      cgd 			flagvalues(np));
    416   1.1      cgd 		break;
    417   1.1      cgd 
    418   1.1      cgd 	/*
    419   1.1      cgd 	 * An inode is being reused in a completely different way.
    420   1.1      cgd 	 * Normally an extract can simply do an "unlink" followed
    421   1.1      cgd 	 * by a "creat". Here we must do effectively the same
    422   1.1      cgd 	 * thing. The complications arise because we cannot really
    423   1.1      cgd 	 * delete a directory since it may still contain files
    424   1.1      cgd 	 * that we need to rename, so we delete it from the symbol
    425   1.1      cgd 	 * table, and put it on the list to be deleted eventually.
    426   1.1      cgd 	 * Conversely if a directory is to be created, it must be
    427   1.1      cgd 	 * done immediately, rather than waiting until the
    428   1.1      cgd 	 * extraction phase.
    429   1.1      cgd 	 */
    430   1.1      cgd 	case ONTAPE|INOFND|MODECHG:
    431   1.1      cgd 	case ONTAPE|INOFND|NAMEFND|MODECHG:
    432   1.1      cgd 		if (ip->e_flags & KEEP) {
    433   1.1      cgd 			badentry(ip, "cannot KEEP and change modes");
    434   1.1      cgd 			break;
    435   1.1      cgd 		}
    436   1.1      cgd 		if (ip->e_type == LEAF) {
    437   1.1      cgd 			/* changing from leaf to node */
    438   1.9    lukem 			for ( ; ip != NULL; ip = ip->e_links) {
    439   1.9    lukem 				if (ip->e_type != LEAF)
    440   1.9    lukem 					badentry(ip,
    441   1.9    lukem 					    "NODE and LEAF links to same inode");
    442   1.9    lukem 				removeleaf(ip);
    443   1.9    lukem 				freeentry(ip);
    444   1.9    lukem 			}
    445   1.1      cgd 			ip = addentry(name, ino, type);
    446   1.1      cgd 			newnode(ip);
    447   1.1      cgd 		} else {
    448   1.1      cgd 			/* changing from node to leaf */
    449   1.1      cgd 			if ((ip->e_flags & TMPNAME) == 0)
    450   1.1      cgd 				mktempname(ip);
    451   1.1      cgd 			deleteino(ip->e_ino);
    452   1.1      cgd 			ip->e_next = removelist;
    453   1.1      cgd 			removelist = ip;
    454   1.1      cgd 			ip = addentry(name, ino, type);
    455   1.1      cgd 		}
    456   1.1      cgd 		ip->e_flags |= NEW|KEEP;
    457   1.1      cgd 		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
    458   1.1      cgd 			flagvalues(ip));
    459   1.1      cgd 		break;
    460   1.1      cgd 
    461   1.1      cgd 	/*
    462   1.1      cgd 	 * A hard link to a diirectory that has been removed.
    463   1.1      cgd 	 * Ignore it.
    464   1.1      cgd 	 */
    465   1.1      cgd 	case NAMEFND:
    466   1.1      cgd 		dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
    467   1.1      cgd 			name);
    468   1.1      cgd 		descend = FAIL;
    469   1.1      cgd 		break;
    470   1.1      cgd 
    471   1.1      cgd 	/*
    472   1.1      cgd 	 * If we find a directory entry for a file that is not on
    473   1.1      cgd 	 * the tape, then we must have found a file that was created
    474   1.1      cgd 	 * while the dump was in progress. Since we have no contents
    475   1.1      cgd 	 * for it, we discard the name knowing that it will be on the
    476   1.1      cgd 	 * next incremental tape.
    477   1.1      cgd 	 */
    478   1.3      cgd 	case NULL:
    479   1.1      cgd 		fprintf(stderr, "%s: (inode %d) not found on tape\n",
    480   1.1      cgd 			name, ino);
    481   1.1      cgd 		break;
    482   1.1      cgd 
    483   1.1      cgd 	/*
    484   1.1      cgd 	 * If any of these arise, something is grievously wrong with
    485   1.1      cgd 	 * the current state of the symbol table.
    486   1.1      cgd 	 */
    487   1.1      cgd 	case INOFND|NAMEFND|MODECHG:
    488   1.1      cgd 	case NAMEFND|MODECHG:
    489   1.1      cgd 	case INOFND|MODECHG:
    490   1.1      cgd 		fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
    491   1.1      cgd 			name);
    492   1.1      cgd 		break;
    493   1.1      cgd 
    494   1.1      cgd 	/*
    495   1.1      cgd 	 * These states "cannot" arise for any state of the symbol table.
    496   1.1      cgd 	 */
    497   1.1      cgd 	case ONTAPE|MODECHG:
    498   1.1      cgd 	case MODECHG:
    499   1.1      cgd 	default:
    500   1.1      cgd 		panic("[%s] %s: impossible state\n", keyval(key), name);
    501   1.1      cgd 		break;
    502   1.1      cgd 	}
    503   1.1      cgd 	return (descend);
    504   1.1      cgd }
    505   1.1      cgd 
    506   1.1      cgd /*
    507   1.1      cgd  * Calculate the active flags in a key.
    508   1.1      cgd  */
    509   1.3      cgd static char *
    510   1.1      cgd keyval(key)
    511   1.1      cgd 	int key;
    512   1.1      cgd {
    513   1.1      cgd 	static char keybuf[32];
    514   1.1      cgd 
    515   1.1      cgd 	(void) strcpy(keybuf, "|NIL");
    516   1.1      cgd 	keybuf[0] = '\0';
    517   1.1      cgd 	if (key & ONTAPE)
    518   1.1      cgd 		(void) strcat(keybuf, "|ONTAPE");
    519   1.1      cgd 	if (key & INOFND)
    520   1.1      cgd 		(void) strcat(keybuf, "|INOFND");
    521   1.1      cgd 	if (key & NAMEFND)
    522   1.1      cgd 		(void) strcat(keybuf, "|NAMEFND");
    523   1.1      cgd 	if (key & MODECHG)
    524   1.1      cgd 		(void) strcat(keybuf, "|MODECHG");
    525   1.1      cgd 	return (&keybuf[1]);
    526   1.1      cgd }
    527   1.1      cgd 
    528   1.1      cgd /*
    529   1.1      cgd  * Find unreferenced link names.
    530   1.1      cgd  */
    531   1.3      cgd void
    532   1.1      cgd findunreflinks()
    533   1.1      cgd {
    534   1.8    lukem 	struct entry *ep, *np;
    535   1.8    lukem 	ino_t i;
    536   1.1      cgd 
    537   1.1      cgd 	vprintf(stdout, "Find unreferenced names.\n");
    538   1.1      cgd 	for (i = ROOTINO; i < maxino; i++) {
    539   1.1      cgd 		ep = lookupino(i);
    540   1.3      cgd 		if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
    541   1.1      cgd 			continue;
    542   1.3      cgd 		for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
    543   1.1      cgd 			if (np->e_flags == 0) {
    544   1.1      cgd 				dprintf(stdout,
    545   1.1      cgd 				    "%s: remove unreferenced name\n",
    546   1.1      cgd 				    myname(np));
    547   1.1      cgd 				removeleaf(np);
    548   1.1      cgd 				freeentry(np);
    549   1.1      cgd 			}
    550   1.1      cgd 		}
    551   1.1      cgd 	}
    552   1.1      cgd 	/*
    553   1.1      cgd 	 * Any leaves remaining in removed directories is unreferenced.
    554   1.1      cgd 	 */
    555   1.3      cgd 	for (ep = removelist; ep != NULL; ep = ep->e_next) {
    556   1.3      cgd 		for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
    557   1.1      cgd 			if (np->e_type == LEAF) {
    558   1.1      cgd 				if (np->e_flags != 0)
    559   1.1      cgd 					badentry(np, "unreferenced with flags");
    560   1.1      cgd 				dprintf(stdout,
    561   1.1      cgd 				    "%s: remove unreferenced name\n",
    562   1.1      cgd 				    myname(np));
    563   1.1      cgd 				removeleaf(np);
    564   1.1      cgd 				freeentry(np);
    565   1.1      cgd 			}
    566   1.1      cgd 		}
    567   1.1      cgd 	}
    568   1.1      cgd }
    569   1.1      cgd 
    570   1.1      cgd /*
    571   1.1      cgd  * Remove old nodes (directories).
    572   1.1      cgd  * Note that this routine runs in O(N*D) where:
    573   1.1      cgd  *	N is the number of directory entries to be removed.
    574   1.1      cgd  *	D is the maximum depth of the tree.
    575   1.1      cgd  * If N == D this can be quite slow. If the list were
    576   1.1      cgd  * topologically sorted, the deletion could be done in
    577   1.1      cgd  * time O(N).
    578   1.1      cgd  */
    579   1.3      cgd void
    580   1.1      cgd removeoldnodes()
    581   1.1      cgd {
    582   1.8    lukem 	struct entry *ep, **prev;
    583   1.1      cgd 	long change;
    584   1.1      cgd 
    585   1.1      cgd 	vprintf(stdout, "Remove old nodes (directories).\n");
    586   1.1      cgd 	do	{
    587   1.1      cgd 		change = 0;
    588   1.1      cgd 		prev = &removelist;
    589   1.3      cgd 		for (ep = removelist; ep != NULL; ep = *prev) {
    590   1.3      cgd 			if (ep->e_entries != NULL) {
    591   1.1      cgd 				prev = &ep->e_next;
    592   1.1      cgd 				continue;
    593   1.1      cgd 			}
    594   1.1      cgd 			*prev = ep->e_next;
    595   1.1      cgd 			removenode(ep);
    596   1.1      cgd 			freeentry(ep);
    597   1.1      cgd 			change++;
    598   1.1      cgd 		}
    599   1.1      cgd 	} while (change);
    600   1.3      cgd 	for (ep = removelist; ep != NULL; ep = ep->e_next)
    601   1.1      cgd 		badentry(ep, "cannot remove, non-empty");
    602   1.1      cgd }
    603   1.1      cgd 
    604   1.1      cgd /*
    605   1.1      cgd  * This is the routine used to extract files for the 'r' command.
    606   1.1      cgd  * Extract new leaves.
    607   1.1      cgd  */
    608   1.3      cgd void
    609   1.1      cgd createleaves(symtabfile)
    610   1.1      cgd 	char *symtabfile;
    611   1.1      cgd {
    612   1.8    lukem 	struct entry *ep;
    613   1.1      cgd 	ino_t first;
    614   1.1      cgd 	long curvol;
    615   1.1      cgd 
    616   1.1      cgd 	if (command == 'R') {
    617   1.1      cgd 		vprintf(stdout, "Continue extraction of new leaves\n");
    618   1.1      cgd 	} else {
    619   1.1      cgd 		vprintf(stdout, "Extract new leaves.\n");
    620   1.1      cgd 		dumpsymtable(symtabfile, volno);
    621   1.1      cgd 	}
    622   1.1      cgd 	first = lowerbnd(ROOTINO);
    623   1.1      cgd 	curvol = volno;
    624   1.1      cgd 	while (curfile.ino < maxino) {
    625   1.1      cgd 		first = lowerbnd(first);
    626   1.1      cgd 		/*
    627   1.1      cgd 		 * If the next available file is not the one which we
    628   1.1      cgd 		 * expect then we have missed one or more files. Since
    629   1.1      cgd 		 * we do not request files that were not on the tape,
    630   1.1      cgd 		 * the lost files must have been due to a tape read error,
    631   1.1      cgd 		 * or a file that was removed while the dump was in progress.
    632   1.1      cgd 		 */
    633   1.1      cgd 		while (first < curfile.ino) {
    634   1.1      cgd 			ep = lookupino(first);
    635   1.3      cgd 			if (ep == NULL)
    636   1.1      cgd 				panic("%d: bad first\n", first);
    637   1.1      cgd 			fprintf(stderr, "%s: not found on tape\n", myname(ep));
    638   1.1      cgd 			ep->e_flags &= ~(NEW|EXTRACT);
    639   1.1      cgd 			first = lowerbnd(first);
    640   1.1      cgd 		}
    641   1.1      cgd 		/*
    642   1.1      cgd 		 * If we find files on the tape that have no corresponding
    643   1.1      cgd 		 * directory entries, then we must have found a file that
    644   1.1      cgd 		 * was created while the dump was in progress. Since we have
    645   1.1      cgd 		 * no name for it, we discard it knowing that it will be
    646   1.1      cgd 		 * on the next incremental tape.
    647   1.1      cgd 		 */
    648   1.1      cgd 		if (first != curfile.ino) {
    649   1.1      cgd 			fprintf(stderr, "expected next file %d, got %d\n",
    650   1.1      cgd 				first, curfile.ino);
    651   1.1      cgd 			skipfile();
    652   1.1      cgd 			goto next;
    653   1.1      cgd 		}
    654   1.1      cgd 		ep = lookupino(curfile.ino);
    655   1.3      cgd 		if (ep == NULL)
    656   1.1      cgd 			panic("unknown file on tape\n");
    657   1.1      cgd 		if ((ep->e_flags & (NEW|EXTRACT)) == 0)
    658   1.1      cgd 			badentry(ep, "unexpected file on tape");
    659   1.1      cgd 		/*
    660   1.1      cgd 		 * If the file is to be extracted, then the old file must
    661   1.1      cgd 		 * be removed since its type may change from one leaf type
    662   1.1      cgd 		 * to another (eg "file" to "character special").
    663   1.1      cgd 		 */
    664   1.1      cgd 		if ((ep->e_flags & EXTRACT) != 0) {
    665   1.1      cgd 			removeleaf(ep);
    666   1.1      cgd 			ep->e_flags &= ~REMOVED;
    667   1.1      cgd 		}
    668   1.1      cgd 		(void) extractfile(myname(ep));
    669   1.1      cgd 		ep->e_flags &= ~(NEW|EXTRACT);
    670   1.1      cgd 		/*
    671   1.1      cgd 		 * We checkpoint the restore after every tape reel, so
    672   1.1      cgd 		 * as to simplify the amount of work re quired by the
    673   1.1      cgd 		 * 'R' command.
    674   1.1      cgd 		 */
    675   1.1      cgd 	next:
    676   1.1      cgd 		if (curvol != volno) {
    677   1.1      cgd 			dumpsymtable(symtabfile, volno);
    678   1.1      cgd 			skipmaps();
    679   1.1      cgd 			curvol = volno;
    680   1.1      cgd 		}
    681   1.1      cgd 	}
    682   1.1      cgd }
    683   1.1      cgd 
    684   1.1      cgd /*
    685   1.1      cgd  * This is the routine used to extract files for the 'x' and 'i' commands.
    686   1.1      cgd  * Efficiently extract a subset of the files on a tape.
    687   1.1      cgd  */
    688   1.3      cgd void
    689   1.1      cgd createfiles()
    690   1.1      cgd {
    691   1.8    lukem 	ino_t first, next, last;
    692   1.8    lukem 	struct entry *ep;
    693   1.1      cgd 	long curvol;
    694   1.1      cgd 
    695   1.1      cgd 	vprintf(stdout, "Extract requested files\n");
    696   1.1      cgd 	curfile.action = SKIP;
    697   1.1      cgd 	getvol((long)1);
    698   1.1      cgd 	skipmaps();
    699   1.1      cgd 	skipdirs();
    700   1.1      cgd 	first = lowerbnd(ROOTINO);
    701   1.1      cgd 	last = upperbnd(maxino - 1);
    702   1.1      cgd 	for (;;) {
    703   1.1      cgd 		first = lowerbnd(first);
    704   1.1      cgd 		last = upperbnd(last);
    705   1.1      cgd 		/*
    706   1.1      cgd 		 * Check to see if any files remain to be extracted
    707   1.1      cgd 		 */
    708   1.1      cgd 		if (first > last)
    709   1.1      cgd 			return;
    710   1.1      cgd 		/*
    711   1.1      cgd 		 * Reject any volumes with inodes greater
    712   1.1      cgd 		 * than the last one needed
    713   1.1      cgd 		 */
    714   1.1      cgd 		while (curfile.ino > last) {
    715   1.1      cgd 			curfile.action = SKIP;
    716   1.1      cgd 			getvol((long)0);
    717   1.1      cgd 			skipmaps();
    718   1.1      cgd 			skipdirs();
    719   1.1      cgd 		}
    720   1.1      cgd 		/*
    721   1.1      cgd 		 * Decide on the next inode needed.
    722   1.1      cgd 		 * Skip across the inodes until it is found
    723   1.1      cgd 		 * or an out of order volume change is encountered
    724   1.1      cgd 		 */
    725   1.1      cgd 		next = lowerbnd(curfile.ino);
    726   1.1      cgd 		do	{
    727   1.1      cgd 			curvol = volno;
    728   1.1      cgd 			while (next > curfile.ino && volno == curvol)
    729   1.1      cgd 				skipfile();
    730   1.1      cgd 			skipmaps();
    731   1.1      cgd 			skipdirs();
    732   1.1      cgd 		} while (volno == curvol + 1);
    733   1.1      cgd 		/*
    734   1.1      cgd 		 * If volume change out of order occurred the
    735   1.1      cgd 		 * current state must be recalculated
    736   1.1      cgd 		 */
    737   1.1      cgd 		if (volno != curvol)
    738   1.1      cgd 			continue;
    739   1.1      cgd 		/*
    740   1.1      cgd 		 * If the current inode is greater than the one we were
    741   1.1      cgd 		 * looking for then we missed the one we were looking for.
    742   1.1      cgd 		 * Since we only attempt to extract files listed in the
    743   1.1      cgd 		 * dump map, the lost files must have been due to a tape
    744   1.1      cgd 		 * read error, or a file that was removed while the dump
    745   1.1      cgd 		 * was in progress. Thus we report all requested files
    746   1.1      cgd 		 * between the one we were looking for, and the one we
    747   1.1      cgd 		 * found as missing, and delete their request flags.
    748   1.1      cgd 		 */
    749   1.1      cgd 		while (next < curfile.ino) {
    750   1.1      cgd 			ep = lookupino(next);
    751   1.3      cgd 			if (ep == NULL)
    752   1.1      cgd 				panic("corrupted symbol table\n");
    753   1.1      cgd 			fprintf(stderr, "%s: not found on tape\n", myname(ep));
    754   1.1      cgd 			ep->e_flags &= ~NEW;
    755   1.1      cgd 			next = lowerbnd(next);
    756   1.1      cgd 		}
    757   1.1      cgd 		/*
    758   1.1      cgd 		 * The current inode is the one that we are looking for,
    759   1.1      cgd 		 * so extract it per its requested name.
    760   1.1      cgd 		 */
    761   1.1      cgd 		if (next == curfile.ino && next <= last) {
    762   1.1      cgd 			ep = lookupino(next);
    763   1.3      cgd 			if (ep == NULL)
    764   1.1      cgd 				panic("corrupted symbol table\n");
    765   1.1      cgd 			(void) extractfile(myname(ep));
    766   1.1      cgd 			ep->e_flags &= ~NEW;
    767   1.1      cgd 			if (volno != curvol)
    768   1.1      cgd 				skipmaps();
    769   1.1      cgd 		}
    770   1.1      cgd 	}
    771   1.1      cgd }
    772   1.1      cgd 
    773   1.1      cgd /*
    774   1.1      cgd  * Add links.
    775   1.1      cgd  */
    776   1.3      cgd void
    777   1.1      cgd createlinks()
    778   1.1      cgd {
    779   1.8    lukem 	struct entry *np, *ep;
    780   1.8    lukem 	ino_t i;
    781   1.1      cgd 	char name[BUFSIZ];
    782   1.1      cgd 
    783  1.11    lukem 	if ((ep = lookupino(WINO)) != NULL) {
    784   1.5  mycroft 		vprintf(stdout, "Add whiteouts\n");
    785   1.5  mycroft 		for ( ; ep != NULL; ep = ep->e_links) {
    786   1.5  mycroft 			if ((ep->e_flags & NEW) == 0)
    787   1.5  mycroft 				continue;
    788   1.5  mycroft 			(void) addwhiteout(myname(ep));
    789   1.5  mycroft 			ep->e_flags &= ~NEW;
    790   1.5  mycroft 		}
    791   1.5  mycroft 	}
    792   1.1      cgd 	vprintf(stdout, "Add links\n");
    793   1.1      cgd 	for (i = ROOTINO; i < maxino; i++) {
    794   1.1      cgd 		ep = lookupino(i);
    795   1.3      cgd 		if (ep == NULL)
    796   1.1      cgd 			continue;
    797   1.3      cgd 		for (np = ep->e_links; np != NULL; np = np->e_links) {
    798   1.1      cgd 			if ((np->e_flags & NEW) == 0)
    799   1.1      cgd 				continue;
    800   1.1      cgd 			(void) strcpy(name, myname(ep));
    801   1.1      cgd 			if (ep->e_type == NODE) {
    802   1.1      cgd 				(void) linkit(name, myname(np), SYMLINK);
    803   1.1      cgd 			} else {
    804   1.1      cgd 				(void) linkit(name, myname(np), HARDLINK);
    805   1.1      cgd 			}
    806   1.1      cgd 			np->e_flags &= ~NEW;
    807   1.1      cgd 		}
    808   1.1      cgd 	}
    809   1.1      cgd }
    810   1.1      cgd 
    811   1.1      cgd /*
    812   1.1      cgd  * Check the symbol table.
    813   1.1      cgd  * We do this to insure that all the requested work was done, and
    814   1.1      cgd  * that no temporary names remain.
    815   1.1      cgd  */
    816   1.3      cgd void
    817   1.1      cgd checkrestore()
    818   1.1      cgd {
    819   1.8    lukem 	struct entry *ep;
    820   1.8    lukem 	ino_t i;
    821   1.1      cgd 
    822   1.1      cgd 	vprintf(stdout, "Check the symbol table.\n");
    823   1.5  mycroft 	for (i = WINO; i < maxino; i++) {
    824   1.3      cgd 		for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
    825   1.1      cgd 			ep->e_flags &= ~KEEP;
    826   1.1      cgd 			if (ep->e_type == NODE)
    827   1.1      cgd 				ep->e_flags &= ~(NEW|EXISTED);
    828  1.10       pk 			if (ep->e_flags != 0)
    829   1.1      cgd 				badentry(ep, "incomplete operations");
    830   1.1      cgd 		}
    831   1.1      cgd 	}
    832   1.1      cgd }
    833   1.1      cgd 
    834   1.1      cgd /*
    835   1.1      cgd  * Compare with the directory structure on the tape
    836   1.1      cgd  * A paranoid check that things are as they should be.
    837   1.1      cgd  */
    838   1.1      cgd long
    839   1.1      cgd verifyfile(name, ino, type)
    840   1.1      cgd 	char *name;
    841   1.1      cgd 	ino_t ino;
    842   1.1      cgd 	int type;
    843   1.1      cgd {
    844   1.1      cgd 	struct entry *np, *ep;
    845   1.1      cgd 	long descend = GOOD;
    846   1.1      cgd 
    847   1.1      cgd 	ep = lookupname(name);
    848   1.3      cgd 	if (ep == NULL) {
    849   1.1      cgd 		fprintf(stderr, "Warning: missing name %s\n", name);
    850   1.1      cgd 		return (FAIL);
    851   1.1      cgd 	}
    852   1.1      cgd 	np = lookupino(ino);
    853   1.1      cgd 	if (np != ep)
    854   1.1      cgd 		descend = FAIL;
    855   1.3      cgd 	for ( ; np != NULL; np = np->e_links)
    856   1.1      cgd 		if (np == ep)
    857   1.1      cgd 			break;
    858   1.3      cgd 	if (np == NULL)
    859   1.1      cgd 		panic("missing inumber %d\n", ino);
    860   1.1      cgd 	if (ep->e_type == LEAF && type != LEAF)
    861   1.1      cgd 		badentry(ep, "type should be LEAF");
    862   1.1      cgd 	return (descend);
    863   1.1      cgd }
    864