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