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