Home | History | Annotate | Line # | Download | only in restore
utilities.c revision 1.4
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1983 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.3  cgd /* from: static char sccsid[] = "@(#)utilities.c	5.10 (Berkeley) 12/2/92"; */
     36  1.4  cgd static char *rcsid = "$Id: utilities.c,v 1.4 1994/02/19 09:07:18 cgd Exp $";
     37  1.1  cgd #endif /* not lint */
     38  1.1  cgd 
     39  1.3  cgd #include <sys/param.h>
     40  1.3  cgd #include <sys/stat.h>
     41  1.3  cgd 
     42  1.3  cgd #include <ufs/dinode.h>
     43  1.3  cgd #include <ufs/dir.h>
     44  1.3  cgd #include <ufs/fs.h>
     45  1.3  cgd 
     46  1.3  cgd #include <errno.h>
     47  1.3  cgd #include <stdio.h>
     48  1.3  cgd #include <stdlib.h>
     49  1.3  cgd #include <string.h>
     50  1.3  cgd #include <unistd.h>
     51  1.3  cgd 
     52  1.1  cgd #include "restore.h"
     53  1.3  cgd #include "extern.h"
     54  1.1  cgd 
     55  1.1  cgd /*
     56  1.1  cgd  * Insure that all the components of a pathname exist.
     57  1.1  cgd  */
     58  1.3  cgd void
     59  1.1  cgd pathcheck(name)
     60  1.1  cgd 	char *name;
     61  1.1  cgd {
     62  1.1  cgd 	register char *cp;
     63  1.1  cgd 	struct entry *ep;
     64  1.1  cgd 	char *start;
     65  1.1  cgd 
     66  1.1  cgd 	start = index(name, '/');
     67  1.1  cgd 	if (start == 0)
     68  1.1  cgd 		return;
     69  1.1  cgd 	for (cp = start; *cp != '\0'; cp++) {
     70  1.1  cgd 		if (*cp != '/')
     71  1.1  cgd 			continue;
     72  1.1  cgd 		*cp = '\0';
     73  1.1  cgd 		ep = lookupname(name);
     74  1.3  cgd 		if (ep == NULL) {
     75  1.4  cgd 			/* safe; we know the pathname exists in the dump */
     76  1.3  cgd 			ep = addentry(name, pathsearch(name)->d_ino, NODE);
     77  1.1  cgd 			newnode(ep);
     78  1.1  cgd 		}
     79  1.1  cgd 		ep->e_flags |= NEW|KEEP;
     80  1.1  cgd 		*cp = '/';
     81  1.1  cgd 	}
     82  1.1  cgd }
     83  1.1  cgd 
     84  1.1  cgd /*
     85  1.1  cgd  * Change a name to a unique temporary name.
     86  1.1  cgd  */
     87  1.3  cgd void
     88  1.1  cgd mktempname(ep)
     89  1.1  cgd 	register struct entry *ep;
     90  1.1  cgd {
     91  1.1  cgd 	char oldname[MAXPATHLEN];
     92  1.1  cgd 
     93  1.1  cgd 	if (ep->e_flags & TMPNAME)
     94  1.1  cgd 		badentry(ep, "mktempname: called with TMPNAME");
     95  1.1  cgd 	ep->e_flags |= TMPNAME;
     96  1.1  cgd 	(void) strcpy(oldname, myname(ep));
     97  1.1  cgd 	freename(ep->e_name);
     98  1.1  cgd 	ep->e_name = savename(gentempname(ep));
     99  1.1  cgd 	ep->e_namlen = strlen(ep->e_name);
    100  1.1  cgd 	renameit(oldname, myname(ep));
    101  1.1  cgd }
    102  1.1  cgd 
    103  1.1  cgd /*
    104  1.1  cgd  * Generate a temporary name for an entry.
    105  1.1  cgd  */
    106  1.1  cgd char *
    107  1.1  cgd gentempname(ep)
    108  1.1  cgd 	struct entry *ep;
    109  1.1  cgd {
    110  1.1  cgd 	static char name[MAXPATHLEN];
    111  1.1  cgd 	struct entry *np;
    112  1.1  cgd 	long i = 0;
    113  1.1  cgd 
    114  1.3  cgd 	for (np = lookupino(ep->e_ino);
    115  1.3  cgd 	    np != NULL && np != ep; np = np->e_links)
    116  1.1  cgd 		i++;
    117  1.3  cgd 	if (np == NULL)
    118  1.1  cgd 		badentry(ep, "not on ino list");
    119  1.1  cgd 	(void) sprintf(name, "%s%d%d", TMPHDR, i, ep->e_ino);
    120  1.1  cgd 	return (name);
    121  1.1  cgd }
    122  1.1  cgd 
    123  1.1  cgd /*
    124  1.1  cgd  * Rename a file or directory.
    125  1.1  cgd  */
    126  1.3  cgd void
    127  1.1  cgd renameit(from, to)
    128  1.1  cgd 	char *from, *to;
    129  1.1  cgd {
    130  1.1  cgd 	if (!Nflag && rename(from, to) < 0) {
    131  1.3  cgd 		fprintf(stderr, "warning: cannot rename %s to %s: %s\n",
    132  1.3  cgd 		    from, to, strerror(errno));
    133  1.1  cgd 		return;
    134  1.1  cgd 	}
    135  1.1  cgd 	vprintf(stdout, "rename %s to %s\n", from, to);
    136  1.1  cgd }
    137  1.1  cgd 
    138  1.1  cgd /*
    139  1.1  cgd  * Create a new node (directory).
    140  1.1  cgd  */
    141  1.3  cgd void
    142  1.1  cgd newnode(np)
    143  1.1  cgd 	struct entry *np;
    144  1.1  cgd {
    145  1.1  cgd 	char *cp;
    146  1.1  cgd 
    147  1.1  cgd 	if (np->e_type != NODE)
    148  1.1  cgd 		badentry(np, "newnode: not a node");
    149  1.1  cgd 	cp = myname(np);
    150  1.1  cgd 	if (!Nflag && mkdir(cp, 0777) < 0) {
    151  1.1  cgd 		np->e_flags |= EXISTED;
    152  1.3  cgd 		fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
    153  1.1  cgd 		return;
    154  1.1  cgd 	}
    155  1.1  cgd 	vprintf(stdout, "Make node %s\n", cp);
    156  1.1  cgd }
    157  1.1  cgd 
    158  1.1  cgd /*
    159  1.1  cgd  * Remove an old node (directory).
    160  1.1  cgd  */
    161  1.3  cgd void
    162  1.1  cgd removenode(ep)
    163  1.1  cgd 	register struct entry *ep;
    164  1.1  cgd {
    165  1.1  cgd 	char *cp;
    166  1.1  cgd 
    167  1.1  cgd 	if (ep->e_type != NODE)
    168  1.1  cgd 		badentry(ep, "removenode: not a node");
    169  1.3  cgd 	if (ep->e_entries != NULL)
    170  1.1  cgd 		badentry(ep, "removenode: non-empty directory");
    171  1.1  cgd 	ep->e_flags |= REMOVED;
    172  1.1  cgd 	ep->e_flags &= ~TMPNAME;
    173  1.1  cgd 	cp = myname(ep);
    174  1.1  cgd 	if (!Nflag && rmdir(cp) < 0) {
    175  1.3  cgd 		fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
    176  1.1  cgd 		return;
    177  1.1  cgd 	}
    178  1.1  cgd 	vprintf(stdout, "Remove node %s\n", cp);
    179  1.1  cgd }
    180  1.1  cgd 
    181  1.1  cgd /*
    182  1.1  cgd  * Remove a leaf.
    183  1.1  cgd  */
    184  1.3  cgd void
    185  1.1  cgd removeleaf(ep)
    186  1.1  cgd 	register struct entry *ep;
    187  1.1  cgd {
    188  1.1  cgd 	char *cp;
    189  1.1  cgd 
    190  1.1  cgd 	if (ep->e_type != LEAF)
    191  1.1  cgd 		badentry(ep, "removeleaf: not a leaf");
    192  1.1  cgd 	ep->e_flags |= REMOVED;
    193  1.1  cgd 	ep->e_flags &= ~TMPNAME;
    194  1.1  cgd 	cp = myname(ep);
    195  1.1  cgd 	if (!Nflag && unlink(cp) < 0) {
    196  1.3  cgd 		fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
    197  1.1  cgd 		return;
    198  1.1  cgd 	}
    199  1.1  cgd 	vprintf(stdout, "Remove leaf %s\n", cp);
    200  1.1  cgd }
    201  1.1  cgd 
    202  1.1  cgd /*
    203  1.1  cgd  * Create a link.
    204  1.1  cgd  */
    205  1.3  cgd int
    206  1.1  cgd linkit(existing, new, type)
    207  1.1  cgd 	char *existing, *new;
    208  1.1  cgd 	int type;
    209  1.1  cgd {
    210  1.1  cgd 
    211  1.1  cgd 	if (type == SYMLINK) {
    212  1.1  cgd 		if (!Nflag && symlink(existing, new) < 0) {
    213  1.1  cgd 			fprintf(stderr,
    214  1.3  cgd 			    "warning: cannot create symbolic link %s->%s: %s\n",
    215  1.3  cgd 			    new, existing, strerror(errno));
    216  1.1  cgd 			return (FAIL);
    217  1.1  cgd 		}
    218  1.1  cgd 	} else if (type == HARDLINK) {
    219  1.1  cgd 		if (!Nflag && link(existing, new) < 0) {
    220  1.1  cgd 			fprintf(stderr,
    221  1.3  cgd 			    "warning: cannot create hard link %s->%s: %s\n",
    222  1.3  cgd 			    new, existing, strerror(errno));
    223  1.1  cgd 			return (FAIL);
    224  1.1  cgd 		}
    225  1.1  cgd 	} else {
    226  1.1  cgd 		panic("linkit: unknown type %d\n", type);
    227  1.1  cgd 		return (FAIL);
    228  1.1  cgd 	}
    229  1.1  cgd 	vprintf(stdout, "Create %s link %s->%s\n",
    230  1.1  cgd 		type == SYMLINK ? "symbolic" : "hard", new, existing);
    231  1.1  cgd 	return (GOOD);
    232  1.1  cgd }
    233  1.1  cgd 
    234  1.1  cgd /*
    235  1.1  cgd  * find lowest number file (above "start") that needs to be extracted
    236  1.1  cgd  */
    237  1.1  cgd ino_t
    238  1.1  cgd lowerbnd(start)
    239  1.1  cgd 	ino_t start;
    240  1.1  cgd {
    241  1.1  cgd 	register struct entry *ep;
    242  1.1  cgd 
    243  1.1  cgd 	for ( ; start < maxino; start++) {
    244  1.1  cgd 		ep = lookupino(start);
    245  1.3  cgd 		if (ep == NULL || ep->e_type == NODE)
    246  1.1  cgd 			continue;
    247  1.1  cgd 		if (ep->e_flags & (NEW|EXTRACT))
    248  1.1  cgd 			return (start);
    249  1.1  cgd 	}
    250  1.1  cgd 	return (start);
    251  1.1  cgd }
    252  1.1  cgd 
    253  1.1  cgd /*
    254  1.1  cgd  * find highest number file (below "start") that needs to be extracted
    255  1.1  cgd  */
    256  1.1  cgd ino_t
    257  1.1  cgd upperbnd(start)
    258  1.1  cgd 	ino_t start;
    259  1.1  cgd {
    260  1.1  cgd 	register struct entry *ep;
    261  1.1  cgd 
    262  1.1  cgd 	for ( ; start > ROOTINO; start--) {
    263  1.1  cgd 		ep = lookupino(start);
    264  1.3  cgd 		if (ep == NULL || ep->e_type == NODE)
    265  1.1  cgd 			continue;
    266  1.1  cgd 		if (ep->e_flags & (NEW|EXTRACT))
    267  1.1  cgd 			return (start);
    268  1.1  cgd 	}
    269  1.1  cgd 	return (start);
    270  1.1  cgd }
    271  1.1  cgd 
    272  1.1  cgd /*
    273  1.1  cgd  * report on a badly formed entry
    274  1.1  cgd  */
    275  1.3  cgd void
    276  1.1  cgd badentry(ep, msg)
    277  1.1  cgd 	register struct entry *ep;
    278  1.1  cgd 	char *msg;
    279  1.1  cgd {
    280  1.1  cgd 
    281  1.1  cgd 	fprintf(stderr, "bad entry: %s\n", msg);
    282  1.1  cgd 	fprintf(stderr, "name: %s\n", myname(ep));
    283  1.1  cgd 	fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
    284  1.3  cgd 	if (ep->e_sibling != NULL)
    285  1.1  cgd 		fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
    286  1.3  cgd 	if (ep->e_entries != NULL)
    287  1.1  cgd 		fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
    288  1.3  cgd 	if (ep->e_links != NULL)
    289  1.1  cgd 		fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
    290  1.3  cgd 	if (ep->e_next != NULL)
    291  1.3  cgd 		fprintf(stderr,
    292  1.3  cgd 		    "next hashchain name: %s\n", myname(ep->e_next));
    293  1.1  cgd 	fprintf(stderr, "entry type: %s\n",
    294  1.1  cgd 		ep->e_type == NODE ? "NODE" : "LEAF");
    295  1.1  cgd 	fprintf(stderr, "inode number: %ld\n", ep->e_ino);
    296  1.1  cgd 	panic("flags: %s\n", flagvalues(ep));
    297  1.1  cgd }
    298  1.1  cgd 
    299  1.1  cgd /*
    300  1.1  cgd  * Construct a string indicating the active flag bits of an entry.
    301  1.1  cgd  */
    302  1.1  cgd char *
    303  1.1  cgd flagvalues(ep)
    304  1.1  cgd 	register struct entry *ep;
    305  1.1  cgd {
    306  1.1  cgd 	static char flagbuf[BUFSIZ];
    307  1.1  cgd 
    308  1.1  cgd 	(void) strcpy(flagbuf, "|NIL");
    309  1.1  cgd 	flagbuf[0] = '\0';
    310  1.1  cgd 	if (ep->e_flags & REMOVED)
    311  1.1  cgd 		(void) strcat(flagbuf, "|REMOVED");
    312  1.1  cgd 	if (ep->e_flags & TMPNAME)
    313  1.1  cgd 		(void) strcat(flagbuf, "|TMPNAME");
    314  1.1  cgd 	if (ep->e_flags & EXTRACT)
    315  1.1  cgd 		(void) strcat(flagbuf, "|EXTRACT");
    316  1.1  cgd 	if (ep->e_flags & NEW)
    317  1.1  cgd 		(void) strcat(flagbuf, "|NEW");
    318  1.1  cgd 	if (ep->e_flags & KEEP)
    319  1.1  cgd 		(void) strcat(flagbuf, "|KEEP");
    320  1.1  cgd 	if (ep->e_flags & EXISTED)
    321  1.1  cgd 		(void) strcat(flagbuf, "|EXISTED");
    322  1.1  cgd 	return (&flagbuf[1]);
    323  1.1  cgd }
    324  1.1  cgd 
    325  1.1  cgd /*
    326  1.1  cgd  * Check to see if a name is on a dump tape.
    327  1.1  cgd  */
    328  1.1  cgd ino_t
    329  1.1  cgd dirlookup(name)
    330  1.1  cgd 	char *name;
    331  1.1  cgd {
    332  1.4  cgd 	register struct direct *dp;
    333  1.1  cgd 	ino_t ino;
    334  1.4  cgd 
    335  1.4  cgd 	dp = pathsearch(name);
    336  1.4  cgd 	if (dp != NULL)
    337  1.4  cgd 		ino = dp->d_ino;
    338  1.4  cgd 	else
    339  1.4  cgd 		ino = 0;
    340  1.1  cgd 
    341  1.3  cgd 	if (ino == 0 || TSTINO(ino, dumpmap) == 0)
    342  1.1  cgd 		fprintf(stderr, "%s is not on tape\n", name);
    343  1.1  cgd 	return (ino);
    344  1.1  cgd }
    345  1.1  cgd 
    346  1.1  cgd /*
    347  1.1  cgd  * Elicit a reply.
    348  1.1  cgd  */
    349  1.3  cgd int
    350  1.1  cgd reply(question)
    351  1.1  cgd 	char *question;
    352  1.1  cgd {
    353  1.1  cgd 	char c;
    354  1.1  cgd 
    355  1.1  cgd 	do	{
    356  1.1  cgd 		fprintf(stderr, "%s? [yn] ", question);
    357  1.1  cgd 		(void) fflush(stderr);
    358  1.1  cgd 		c = getc(terminal);
    359  1.1  cgd 		while (c != '\n' && getc(terminal) != '\n')
    360  1.1  cgd 			if (feof(terminal))
    361  1.1  cgd 				return (FAIL);
    362  1.1  cgd 	} while (c != 'y' && c != 'n');
    363  1.1  cgd 	if (c == 'y')
    364  1.1  cgd 		return (GOOD);
    365  1.1  cgd 	return (FAIL);
    366  1.1  cgd }
    367  1.1  cgd 
    368  1.1  cgd /*
    369  1.1  cgd  * handle unexpected inconsistencies
    370  1.1  cgd  */
    371  1.3  cgd #if __STDC__
    372  1.3  cgd #include <stdarg.h>
    373  1.3  cgd #else
    374  1.3  cgd #include <varargs.h>
    375  1.3  cgd #endif
    376  1.3  cgd 
    377  1.3  cgd void
    378  1.3  cgd #if __STDC__
    379  1.3  cgd panic(const char *fmt, ...)
    380  1.3  cgd #else
    381  1.3  cgd panic(fmt, va_alist)
    382  1.3  cgd 	char *fmt;
    383  1.3  cgd 	va_dcl
    384  1.3  cgd #endif
    385  1.3  cgd {
    386  1.3  cgd 	va_list ap;
    387  1.3  cgd #if __STDC__
    388  1.3  cgd 	va_start(ap, fmt);
    389  1.3  cgd #else
    390  1.3  cgd 	va_start(ap);
    391  1.3  cgd #endif
    392  1.1  cgd 
    393  1.3  cgd 	vfprintf(stderr, fmt, ap);
    394  1.1  cgd 	if (yflag)
    395  1.1  cgd 		return;
    396  1.1  cgd 	if (reply("abort") == GOOD) {
    397  1.1  cgd 		if (reply("dump core") == GOOD)
    398  1.1  cgd 			abort();
    399  1.1  cgd 		done(1);
    400  1.1  cgd 	}
    401  1.1  cgd }
    402