Home | History | Annotate | Line # | Download | only in restore
utilities.c revision 1.1
      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.1  cgd static char sccsid[] = "@(#)utilities.c	5.6 (Berkeley) 6/1/90";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd #include "restore.h"
     39  1.1  cgd 
     40  1.1  cgd /*
     41  1.1  cgd  * Insure that all the components of a pathname exist.
     42  1.1  cgd  */
     43  1.1  cgd pathcheck(name)
     44  1.1  cgd 	char *name;
     45  1.1  cgd {
     46  1.1  cgd 	register char *cp;
     47  1.1  cgd 	struct entry *ep;
     48  1.1  cgd 	char *start;
     49  1.1  cgd 
     50  1.1  cgd 	start = index(name, '/');
     51  1.1  cgd 	if (start == 0)
     52  1.1  cgd 		return;
     53  1.1  cgd 	for (cp = start; *cp != '\0'; cp++) {
     54  1.1  cgd 		if (*cp != '/')
     55  1.1  cgd 			continue;
     56  1.1  cgd 		*cp = '\0';
     57  1.1  cgd 		ep = lookupname(name);
     58  1.1  cgd 		if (ep == NIL) {
     59  1.1  cgd 			ep = addentry(name, psearch(name), NODE);
     60  1.1  cgd 			newnode(ep);
     61  1.1  cgd 		}
     62  1.1  cgd 		ep->e_flags |= NEW|KEEP;
     63  1.1  cgd 		*cp = '/';
     64  1.1  cgd 	}
     65  1.1  cgd }
     66  1.1  cgd 
     67  1.1  cgd /*
     68  1.1  cgd  * Change a name to a unique temporary name.
     69  1.1  cgd  */
     70  1.1  cgd mktempname(ep)
     71  1.1  cgd 	register struct entry *ep;
     72  1.1  cgd {
     73  1.1  cgd 	char oldname[MAXPATHLEN];
     74  1.1  cgd 
     75  1.1  cgd 	if (ep->e_flags & TMPNAME)
     76  1.1  cgd 		badentry(ep, "mktempname: called with TMPNAME");
     77  1.1  cgd 	ep->e_flags |= TMPNAME;
     78  1.1  cgd 	(void) strcpy(oldname, myname(ep));
     79  1.1  cgd 	freename(ep->e_name);
     80  1.1  cgd 	ep->e_name = savename(gentempname(ep));
     81  1.1  cgd 	ep->e_namlen = strlen(ep->e_name);
     82  1.1  cgd 	renameit(oldname, myname(ep));
     83  1.1  cgd }
     84  1.1  cgd 
     85  1.1  cgd /*
     86  1.1  cgd  * Generate a temporary name for an entry.
     87  1.1  cgd  */
     88  1.1  cgd char *
     89  1.1  cgd gentempname(ep)
     90  1.1  cgd 	struct entry *ep;
     91  1.1  cgd {
     92  1.1  cgd 	static char name[MAXPATHLEN];
     93  1.1  cgd 	struct entry *np;
     94  1.1  cgd 	long i = 0;
     95  1.1  cgd 
     96  1.1  cgd 	for (np = lookupino(ep->e_ino); np != NIL && np != ep; np = np->e_links)
     97  1.1  cgd 		i++;
     98  1.1  cgd 	if (np == NIL)
     99  1.1  cgd 		badentry(ep, "not on ino list");
    100  1.1  cgd 	(void) sprintf(name, "%s%d%d", TMPHDR, i, ep->e_ino);
    101  1.1  cgd 	return (name);
    102  1.1  cgd }
    103  1.1  cgd 
    104  1.1  cgd /*
    105  1.1  cgd  * Rename a file or directory.
    106  1.1  cgd  */
    107  1.1  cgd renameit(from, to)
    108  1.1  cgd 	char *from, *to;
    109  1.1  cgd {
    110  1.1  cgd 	if (!Nflag && rename(from, to) < 0) {
    111  1.1  cgd 		fprintf(stderr, "Warning: cannot rename %s to %s", from, to);
    112  1.1  cgd 		(void) fflush(stderr);
    113  1.1  cgd 		perror("");
    114  1.1  cgd 		return;
    115  1.1  cgd 	}
    116  1.1  cgd 	vprintf(stdout, "rename %s to %s\n", from, to);
    117  1.1  cgd }
    118  1.1  cgd 
    119  1.1  cgd /*
    120  1.1  cgd  * Create a new node (directory).
    121  1.1  cgd  */
    122  1.1  cgd newnode(np)
    123  1.1  cgd 	struct entry *np;
    124  1.1  cgd {
    125  1.1  cgd 	char *cp;
    126  1.1  cgd 
    127  1.1  cgd 	if (np->e_type != NODE)
    128  1.1  cgd 		badentry(np, "newnode: not a node");
    129  1.1  cgd 	cp = myname(np);
    130  1.1  cgd 	if (!Nflag && mkdir(cp, 0777) < 0) {
    131  1.1  cgd 		np->e_flags |= EXISTED;
    132  1.1  cgd 		fprintf(stderr, "Warning: ");
    133  1.1  cgd 		(void) fflush(stderr);
    134  1.1  cgd 		perror(cp);
    135  1.1  cgd 		return;
    136  1.1  cgd 	}
    137  1.1  cgd 	vprintf(stdout, "Make node %s\n", cp);
    138  1.1  cgd }
    139  1.1  cgd 
    140  1.1  cgd /*
    141  1.1  cgd  * Remove an old node (directory).
    142  1.1  cgd  */
    143  1.1  cgd removenode(ep)
    144  1.1  cgd 	register struct entry *ep;
    145  1.1  cgd {
    146  1.1  cgd 	char *cp;
    147  1.1  cgd 
    148  1.1  cgd 	if (ep->e_type != NODE)
    149  1.1  cgd 		badentry(ep, "removenode: not a node");
    150  1.1  cgd 	if (ep->e_entries != NIL)
    151  1.1  cgd 		badentry(ep, "removenode: non-empty directory");
    152  1.1  cgd 	ep->e_flags |= REMOVED;
    153  1.1  cgd 	ep->e_flags &= ~TMPNAME;
    154  1.1  cgd 	cp = myname(ep);
    155  1.1  cgd 	if (!Nflag && rmdir(cp) < 0) {
    156  1.1  cgd 		fprintf(stderr, "Warning: ");
    157  1.1  cgd 		(void) fflush(stderr);
    158  1.1  cgd 		perror(cp);
    159  1.1  cgd 		return;
    160  1.1  cgd 	}
    161  1.1  cgd 	vprintf(stdout, "Remove node %s\n", cp);
    162  1.1  cgd }
    163  1.1  cgd 
    164  1.1  cgd /*
    165  1.1  cgd  * Remove a leaf.
    166  1.1  cgd  */
    167  1.1  cgd removeleaf(ep)
    168  1.1  cgd 	register struct entry *ep;
    169  1.1  cgd {
    170  1.1  cgd 	char *cp;
    171  1.1  cgd 
    172  1.1  cgd 	if (ep->e_type != LEAF)
    173  1.1  cgd 		badentry(ep, "removeleaf: not a leaf");
    174  1.1  cgd 	ep->e_flags |= REMOVED;
    175  1.1  cgd 	ep->e_flags &= ~TMPNAME;
    176  1.1  cgd 	cp = myname(ep);
    177  1.1  cgd 	if (!Nflag && unlink(cp) < 0) {
    178  1.1  cgd 		fprintf(stderr, "Warning: ");
    179  1.1  cgd 		(void) fflush(stderr);
    180  1.1  cgd 		perror(cp);
    181  1.1  cgd 		return;
    182  1.1  cgd 	}
    183  1.1  cgd 	vprintf(stdout, "Remove leaf %s\n", cp);
    184  1.1  cgd }
    185  1.1  cgd 
    186  1.1  cgd /*
    187  1.1  cgd  * Create a link.
    188  1.1  cgd  */
    189  1.1  cgd linkit(existing, new, type)
    190  1.1  cgd 	char *existing, *new;
    191  1.1  cgd 	int type;
    192  1.1  cgd {
    193  1.1  cgd 
    194  1.1  cgd 	if (type == SYMLINK) {
    195  1.1  cgd 		if (!Nflag && symlink(existing, new) < 0) {
    196  1.1  cgd 			fprintf(stderr,
    197  1.1  cgd 				"Warning: cannot create symbolic link %s->%s: ",
    198  1.1  cgd 				new, existing);
    199  1.1  cgd 			(void) fflush(stderr);
    200  1.1  cgd 			perror("");
    201  1.1  cgd 			return (FAIL);
    202  1.1  cgd 		}
    203  1.1  cgd 	} else if (type == HARDLINK) {
    204  1.1  cgd 		if (!Nflag && link(existing, new) < 0) {
    205  1.1  cgd 			fprintf(stderr,
    206  1.1  cgd 				"Warning: cannot create hard link %s->%s: ",
    207  1.1  cgd 				new, existing);
    208  1.1  cgd 			(void) fflush(stderr);
    209  1.1  cgd 			perror("");
    210  1.1  cgd 			return (FAIL);
    211  1.1  cgd 		}
    212  1.1  cgd 	} else {
    213  1.1  cgd 		panic("linkit: unknown type %d\n", type);
    214  1.1  cgd 		return (FAIL);
    215  1.1  cgd 	}
    216  1.1  cgd 	vprintf(stdout, "Create %s link %s->%s\n",
    217  1.1  cgd 		type == SYMLINK ? "symbolic" : "hard", new, existing);
    218  1.1  cgd 	return (GOOD);
    219  1.1  cgd }
    220  1.1  cgd 
    221  1.1  cgd /*
    222  1.1  cgd  * find lowest number file (above "start") that needs to be extracted
    223  1.1  cgd  */
    224  1.1  cgd ino_t
    225  1.1  cgd lowerbnd(start)
    226  1.1  cgd 	ino_t start;
    227  1.1  cgd {
    228  1.1  cgd 	register struct entry *ep;
    229  1.1  cgd 
    230  1.1  cgd 	for ( ; start < maxino; start++) {
    231  1.1  cgd 		ep = lookupino(start);
    232  1.1  cgd 		if (ep == NIL || ep->e_type == NODE)
    233  1.1  cgd 			continue;
    234  1.1  cgd 		if (ep->e_flags & (NEW|EXTRACT))
    235  1.1  cgd 			return (start);
    236  1.1  cgd 	}
    237  1.1  cgd 	return (start);
    238  1.1  cgd }
    239  1.1  cgd 
    240  1.1  cgd /*
    241  1.1  cgd  * find highest number file (below "start") that needs to be extracted
    242  1.1  cgd  */
    243  1.1  cgd ino_t
    244  1.1  cgd upperbnd(start)
    245  1.1  cgd 	ino_t start;
    246  1.1  cgd {
    247  1.1  cgd 	register struct entry *ep;
    248  1.1  cgd 
    249  1.1  cgd 	for ( ; start > ROOTINO; start--) {
    250  1.1  cgd 		ep = lookupino(start);
    251  1.1  cgd 		if (ep == NIL || ep->e_type == NODE)
    252  1.1  cgd 			continue;
    253  1.1  cgd 		if (ep->e_flags & (NEW|EXTRACT))
    254  1.1  cgd 			return (start);
    255  1.1  cgd 	}
    256  1.1  cgd 	return (start);
    257  1.1  cgd }
    258  1.1  cgd 
    259  1.1  cgd /*
    260  1.1  cgd  * report on a badly formed entry
    261  1.1  cgd  */
    262  1.1  cgd badentry(ep, msg)
    263  1.1  cgd 	register struct entry *ep;
    264  1.1  cgd 	char *msg;
    265  1.1  cgd {
    266  1.1  cgd 
    267  1.1  cgd 	fprintf(stderr, "bad entry: %s\n", msg);
    268  1.1  cgd 	fprintf(stderr, "name: %s\n", myname(ep));
    269  1.1  cgd 	fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
    270  1.1  cgd 	if (ep->e_sibling != NIL)
    271  1.1  cgd 		fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
    272  1.1  cgd 	if (ep->e_entries != NIL)
    273  1.1  cgd 		fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
    274  1.1  cgd 	if (ep->e_links != NIL)
    275  1.1  cgd 		fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
    276  1.1  cgd 	if (ep->e_next != NIL)
    277  1.1  cgd 		fprintf(stderr, "next hashchain name: %s\n", myname(ep->e_next));
    278  1.1  cgd 	fprintf(stderr, "entry type: %s\n",
    279  1.1  cgd 		ep->e_type == NODE ? "NODE" : "LEAF");
    280  1.1  cgd 	fprintf(stderr, "inode number: %ld\n", ep->e_ino);
    281  1.1  cgd 	panic("flags: %s\n", flagvalues(ep));
    282  1.1  cgd }
    283  1.1  cgd 
    284  1.1  cgd /*
    285  1.1  cgd  * Construct a string indicating the active flag bits of an entry.
    286  1.1  cgd  */
    287  1.1  cgd char *
    288  1.1  cgd flagvalues(ep)
    289  1.1  cgd 	register struct entry *ep;
    290  1.1  cgd {
    291  1.1  cgd 	static char flagbuf[BUFSIZ];
    292  1.1  cgd 
    293  1.1  cgd 	(void) strcpy(flagbuf, "|NIL");
    294  1.1  cgd 	flagbuf[0] = '\0';
    295  1.1  cgd 	if (ep->e_flags & REMOVED)
    296  1.1  cgd 		(void) strcat(flagbuf, "|REMOVED");
    297  1.1  cgd 	if (ep->e_flags & TMPNAME)
    298  1.1  cgd 		(void) strcat(flagbuf, "|TMPNAME");
    299  1.1  cgd 	if (ep->e_flags & EXTRACT)
    300  1.1  cgd 		(void) strcat(flagbuf, "|EXTRACT");
    301  1.1  cgd 	if (ep->e_flags & NEW)
    302  1.1  cgd 		(void) strcat(flagbuf, "|NEW");
    303  1.1  cgd 	if (ep->e_flags & KEEP)
    304  1.1  cgd 		(void) strcat(flagbuf, "|KEEP");
    305  1.1  cgd 	if (ep->e_flags & EXISTED)
    306  1.1  cgd 		(void) strcat(flagbuf, "|EXISTED");
    307  1.1  cgd 	return (&flagbuf[1]);
    308  1.1  cgd }
    309  1.1  cgd 
    310  1.1  cgd /*
    311  1.1  cgd  * Check to see if a name is on a dump tape.
    312  1.1  cgd  */
    313  1.1  cgd ino_t
    314  1.1  cgd dirlookup(name)
    315  1.1  cgd 	char *name;
    316  1.1  cgd {
    317  1.1  cgd 	ino_t ino;
    318  1.1  cgd 
    319  1.1  cgd 	ino = psearch(name);
    320  1.1  cgd 	if (ino == 0 || BIT(ino, dumpmap) == 0)
    321  1.1  cgd 		fprintf(stderr, "%s is not on tape\n", name);
    322  1.1  cgd 	return (ino);
    323  1.1  cgd }
    324  1.1  cgd 
    325  1.1  cgd /*
    326  1.1  cgd  * Elicit a reply.
    327  1.1  cgd  */
    328  1.1  cgd reply(question)
    329  1.1  cgd 	char *question;
    330  1.1  cgd {
    331  1.1  cgd 	char c;
    332  1.1  cgd 
    333  1.1  cgd 	do	{
    334  1.1  cgd 		fprintf(stderr, "%s? [yn] ", question);
    335  1.1  cgd 		(void) fflush(stderr);
    336  1.1  cgd 		c = getc(terminal);
    337  1.1  cgd 		while (c != '\n' && getc(terminal) != '\n')
    338  1.1  cgd 			if (feof(terminal))
    339  1.1  cgd 				return (FAIL);
    340  1.1  cgd 	} while (c != 'y' && c != 'n');
    341  1.1  cgd 	if (c == 'y')
    342  1.1  cgd 		return (GOOD);
    343  1.1  cgd 	return (FAIL);
    344  1.1  cgd }
    345  1.1  cgd 
    346  1.1  cgd /*
    347  1.1  cgd  * handle unexpected inconsistencies
    348  1.1  cgd  */
    349  1.1  cgd /* VARARGS1 */
    350  1.1  cgd panic(msg, d1, d2)
    351  1.1  cgd 	char *msg;
    352  1.1  cgd 	long d1, d2;
    353  1.1  cgd {
    354  1.1  cgd 
    355  1.1  cgd 	fprintf(stderr, msg, d1, d2);
    356  1.1  cgd 	if (yflag)
    357  1.1  cgd 		return;
    358  1.1  cgd 	if (reply("abort") == GOOD) {
    359  1.1  cgd 		if (reply("dump core") == GOOD)
    360  1.1  cgd 			abort();
    361  1.1  cgd 		done(1);
    362  1.1  cgd 	}
    363  1.1  cgd }
    364