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