utilities.c revision 1.7 1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. 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 /*static char sccsid[] = "from: @(#)utilities.c 8.4 (Berkeley) 10/18/94";*/
36 static char *rcsid = "$Id: utilities.c,v 1.7 1994/12/28 02:21:55 mycroft Exp $";
37 #endif /* not lint */
38
39 #include <sys/param.h>
40 #include <sys/stat.h>
41
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ufs/dir.h>
44
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "restore.h"
52 #include "extern.h"
53
54 /*
55 * Insure that all the components of a pathname exist.
56 */
57 void
58 pathcheck(name)
59 char *name;
60 {
61 register char *cp;
62 struct entry *ep;
63 char *start;
64
65 start = strchr(name, '/');
66 if (start == 0)
67 return;
68 for (cp = start; *cp != '\0'; cp++) {
69 if (*cp != '/')
70 continue;
71 *cp = '\0';
72 ep = lookupname(name);
73 if (ep == NULL) {
74 /* Safe; we know the pathname exists in the dump. */
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 * Create a whiteout.
235 */
236 int
237 addwhiteout(name)
238 char *name;
239 {
240
241 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
242 fprintf(stderr, "warning: cannot create whiteout %s: %s\n",
243 name, strerror(errno));
244 return (FAIL);
245 }
246 vprintf(stdout, "Create whiteout %s\n", name);
247 return (GOOD);
248 }
249
250 /*
251 * Delete a whiteout.
252 */
253 void
254 delwhiteout(ep)
255 register struct entry *ep;
256 {
257 char *name;
258
259 if (ep->e_type != LEAF)
260 badentry(ep, "delwhiteout: not a leaf");
261 ep->e_flags |= REMOVED;
262 ep->e_flags &= ~TMPNAME;
263 name = myname(ep);
264 if (!Nflag && undelete(name) < 0) {
265 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
266 name, strerror(errno));
267 return;
268 }
269 vprintf(stdout, "Delete whiteout %s\n", name);
270 }
271
272 /*
273 * find lowest number file (above "start") that needs to be extracted
274 */
275 ino_t
276 lowerbnd(start)
277 ino_t start;
278 {
279 register struct entry *ep;
280
281 for ( ; start < maxino; start++) {
282 ep = lookupino(start);
283 if (ep == NULL || ep->e_type == NODE)
284 continue;
285 if (ep->e_flags & (NEW|EXTRACT))
286 return (start);
287 }
288 return (start);
289 }
290
291 /*
292 * find highest number file (below "start") that needs to be extracted
293 */
294 ino_t
295 upperbnd(start)
296 ino_t start;
297 {
298 register struct entry *ep;
299
300 for ( ; start > ROOTINO; start--) {
301 ep = lookupino(start);
302 if (ep == NULL || ep->e_type == NODE)
303 continue;
304 if (ep->e_flags & (NEW|EXTRACT))
305 return (start);
306 }
307 return (start);
308 }
309
310 /*
311 * report on a badly formed entry
312 */
313 void
314 badentry(ep, msg)
315 register struct entry *ep;
316 char *msg;
317 {
318
319 fprintf(stderr, "bad entry: %s\n", msg);
320 fprintf(stderr, "name: %s\n", myname(ep));
321 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
322 if (ep->e_sibling != NULL)
323 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
324 if (ep->e_entries != NULL)
325 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
326 if (ep->e_links != NULL)
327 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
328 if (ep->e_next != NULL)
329 fprintf(stderr,
330 "next hashchain name: %s\n", myname(ep->e_next));
331 fprintf(stderr, "entry type: %s\n",
332 ep->e_type == NODE ? "NODE" : "LEAF");
333 fprintf(stderr, "inode number: %ld\n", ep->e_ino);
334 panic("flags: %s\n", flagvalues(ep));
335 }
336
337 /*
338 * Construct a string indicating the active flag bits of an entry.
339 */
340 char *
341 flagvalues(ep)
342 register struct entry *ep;
343 {
344 static char flagbuf[BUFSIZ];
345
346 (void) strcpy(flagbuf, "|NIL");
347 flagbuf[0] = '\0';
348 if (ep->e_flags & REMOVED)
349 (void) strcat(flagbuf, "|REMOVED");
350 if (ep->e_flags & TMPNAME)
351 (void) strcat(flagbuf, "|TMPNAME");
352 if (ep->e_flags & EXTRACT)
353 (void) strcat(flagbuf, "|EXTRACT");
354 if (ep->e_flags & NEW)
355 (void) strcat(flagbuf, "|NEW");
356 if (ep->e_flags & KEEP)
357 (void) strcat(flagbuf, "|KEEP");
358 if (ep->e_flags & EXISTED)
359 (void) strcat(flagbuf, "|EXISTED");
360 return (&flagbuf[1]);
361 }
362
363 /*
364 * Check to see if a name is on a dump tape.
365 */
366 ino_t
367 dirlookup(name)
368 const char *name;
369 {
370 struct direct *dp;
371 ino_t ino;
372
373 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
374
375 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
376 fprintf(stderr, "%s is not on the tape\n", name);
377 return (ino);
378 }
379
380 /*
381 * Elicit a reply.
382 */
383 int
384 reply(question)
385 char *question;
386 {
387 char c;
388
389 do {
390 fprintf(stderr, "%s? [yn] ", question);
391 (void) fflush(stderr);
392 c = getc(terminal);
393 while (c != '\n' && getc(terminal) != '\n')
394 if (feof(terminal))
395 return (FAIL);
396 } while (c != 'y' && c != 'n');
397 if (c == 'y')
398 return (GOOD);
399 return (FAIL);
400 }
401
402 /*
403 * handle unexpected inconsistencies
404 */
405 #if __STDC__
406 #include <stdarg.h>
407 #else
408 #include <varargs.h>
409 #endif
410
411 void
412 #if __STDC__
413 panic(const char *fmt, ...)
414 #else
415 panic(fmt, va_alist)
416 char *fmt;
417 va_dcl
418 #endif
419 {
420 va_list ap;
421 #if __STDC__
422 va_start(ap, fmt);
423 #else
424 va_start(ap);
425 #endif
426
427 vfprintf(stderr, fmt, ap);
428 if (yflag)
429 return;
430 if (reply("abort") == GOOD) {
431 if (reply("dump core") == GOOD)
432 abort();
433 done(1);
434 }
435 }
436