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