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