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