utilities.c revision 1.1 1 /*
2 * Copyright (c) 1983 The Regents of the University of California.
3 * 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[] = "@(#)utilities.c 5.6 (Berkeley) 6/1/90";
36 #endif /* not lint */
37
38 #include "restore.h"
39
40 /*
41 * Insure that all the components of a pathname exist.
42 */
43 pathcheck(name)
44 char *name;
45 {
46 register char *cp;
47 struct entry *ep;
48 char *start;
49
50 start = index(name, '/');
51 if (start == 0)
52 return;
53 for (cp = start; *cp != '\0'; cp++) {
54 if (*cp != '/')
55 continue;
56 *cp = '\0';
57 ep = lookupname(name);
58 if (ep == NIL) {
59 ep = addentry(name, psearch(name), NODE);
60 newnode(ep);
61 }
62 ep->e_flags |= NEW|KEEP;
63 *cp = '/';
64 }
65 }
66
67 /*
68 * Change a name to a unique temporary name.
69 */
70 mktempname(ep)
71 register struct entry *ep;
72 {
73 char oldname[MAXPATHLEN];
74
75 if (ep->e_flags & TMPNAME)
76 badentry(ep, "mktempname: called with TMPNAME");
77 ep->e_flags |= TMPNAME;
78 (void) strcpy(oldname, myname(ep));
79 freename(ep->e_name);
80 ep->e_name = savename(gentempname(ep));
81 ep->e_namlen = strlen(ep->e_name);
82 renameit(oldname, myname(ep));
83 }
84
85 /*
86 * Generate a temporary name for an entry.
87 */
88 char *
89 gentempname(ep)
90 struct entry *ep;
91 {
92 static char name[MAXPATHLEN];
93 struct entry *np;
94 long i = 0;
95
96 for (np = lookupino(ep->e_ino); np != NIL && np != ep; np = np->e_links)
97 i++;
98 if (np == NIL)
99 badentry(ep, "not on ino list");
100 (void) sprintf(name, "%s%d%d", TMPHDR, i, ep->e_ino);
101 return (name);
102 }
103
104 /*
105 * Rename a file or directory.
106 */
107 renameit(from, to)
108 char *from, *to;
109 {
110 if (!Nflag && rename(from, to) < 0) {
111 fprintf(stderr, "Warning: cannot rename %s to %s", from, to);
112 (void) fflush(stderr);
113 perror("");
114 return;
115 }
116 vprintf(stdout, "rename %s to %s\n", from, to);
117 }
118
119 /*
120 * Create a new node (directory).
121 */
122 newnode(np)
123 struct entry *np;
124 {
125 char *cp;
126
127 if (np->e_type != NODE)
128 badentry(np, "newnode: not a node");
129 cp = myname(np);
130 if (!Nflag && mkdir(cp, 0777) < 0) {
131 np->e_flags |= EXISTED;
132 fprintf(stderr, "Warning: ");
133 (void) fflush(stderr);
134 perror(cp);
135 return;
136 }
137 vprintf(stdout, "Make node %s\n", cp);
138 }
139
140 /*
141 * Remove an old node (directory).
142 */
143 removenode(ep)
144 register struct entry *ep;
145 {
146 char *cp;
147
148 if (ep->e_type != NODE)
149 badentry(ep, "removenode: not a node");
150 if (ep->e_entries != NIL)
151 badentry(ep, "removenode: non-empty directory");
152 ep->e_flags |= REMOVED;
153 ep->e_flags &= ~TMPNAME;
154 cp = myname(ep);
155 if (!Nflag && rmdir(cp) < 0) {
156 fprintf(stderr, "Warning: ");
157 (void) fflush(stderr);
158 perror(cp);
159 return;
160 }
161 vprintf(stdout, "Remove node %s\n", cp);
162 }
163
164 /*
165 * Remove a leaf.
166 */
167 removeleaf(ep)
168 register struct entry *ep;
169 {
170 char *cp;
171
172 if (ep->e_type != LEAF)
173 badentry(ep, "removeleaf: not a leaf");
174 ep->e_flags |= REMOVED;
175 ep->e_flags &= ~TMPNAME;
176 cp = myname(ep);
177 if (!Nflag && unlink(cp) < 0) {
178 fprintf(stderr, "Warning: ");
179 (void) fflush(stderr);
180 perror(cp);
181 return;
182 }
183 vprintf(stdout, "Remove leaf %s\n", cp);
184 }
185
186 /*
187 * Create a link.
188 */
189 linkit(existing, new, type)
190 char *existing, *new;
191 int type;
192 {
193
194 if (type == SYMLINK) {
195 if (!Nflag && symlink(existing, new) < 0) {
196 fprintf(stderr,
197 "Warning: cannot create symbolic link %s->%s: ",
198 new, existing);
199 (void) fflush(stderr);
200 perror("");
201 return (FAIL);
202 }
203 } else if (type == HARDLINK) {
204 if (!Nflag && link(existing, new) < 0) {
205 fprintf(stderr,
206 "Warning: cannot create hard link %s->%s: ",
207 new, existing);
208 (void) fflush(stderr);
209 perror("");
210 return (FAIL);
211 }
212 } else {
213 panic("linkit: unknown type %d\n", type);
214 return (FAIL);
215 }
216 vprintf(stdout, "Create %s link %s->%s\n",
217 type == SYMLINK ? "symbolic" : "hard", new, existing);
218 return (GOOD);
219 }
220
221 /*
222 * find lowest number file (above "start") that needs to be extracted
223 */
224 ino_t
225 lowerbnd(start)
226 ino_t start;
227 {
228 register struct entry *ep;
229
230 for ( ; start < maxino; start++) {
231 ep = lookupino(start);
232 if (ep == NIL || ep->e_type == NODE)
233 continue;
234 if (ep->e_flags & (NEW|EXTRACT))
235 return (start);
236 }
237 return (start);
238 }
239
240 /*
241 * find highest number file (below "start") that needs to be extracted
242 */
243 ino_t
244 upperbnd(start)
245 ino_t start;
246 {
247 register struct entry *ep;
248
249 for ( ; start > ROOTINO; start--) {
250 ep = lookupino(start);
251 if (ep == NIL || ep->e_type == NODE)
252 continue;
253 if (ep->e_flags & (NEW|EXTRACT))
254 return (start);
255 }
256 return (start);
257 }
258
259 /*
260 * report on a badly formed entry
261 */
262 badentry(ep, msg)
263 register struct entry *ep;
264 char *msg;
265 {
266
267 fprintf(stderr, "bad entry: %s\n", msg);
268 fprintf(stderr, "name: %s\n", myname(ep));
269 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
270 if (ep->e_sibling != NIL)
271 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
272 if (ep->e_entries != NIL)
273 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
274 if (ep->e_links != NIL)
275 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
276 if (ep->e_next != NIL)
277 fprintf(stderr, "next hashchain name: %s\n", myname(ep->e_next));
278 fprintf(stderr, "entry type: %s\n",
279 ep->e_type == NODE ? "NODE" : "LEAF");
280 fprintf(stderr, "inode number: %ld\n", ep->e_ino);
281 panic("flags: %s\n", flagvalues(ep));
282 }
283
284 /*
285 * Construct a string indicating the active flag bits of an entry.
286 */
287 char *
288 flagvalues(ep)
289 register struct entry *ep;
290 {
291 static char flagbuf[BUFSIZ];
292
293 (void) strcpy(flagbuf, "|NIL");
294 flagbuf[0] = '\0';
295 if (ep->e_flags & REMOVED)
296 (void) strcat(flagbuf, "|REMOVED");
297 if (ep->e_flags & TMPNAME)
298 (void) strcat(flagbuf, "|TMPNAME");
299 if (ep->e_flags & EXTRACT)
300 (void) strcat(flagbuf, "|EXTRACT");
301 if (ep->e_flags & NEW)
302 (void) strcat(flagbuf, "|NEW");
303 if (ep->e_flags & KEEP)
304 (void) strcat(flagbuf, "|KEEP");
305 if (ep->e_flags & EXISTED)
306 (void) strcat(flagbuf, "|EXISTED");
307 return (&flagbuf[1]);
308 }
309
310 /*
311 * Check to see if a name is on a dump tape.
312 */
313 ino_t
314 dirlookup(name)
315 char *name;
316 {
317 ino_t ino;
318
319 ino = psearch(name);
320 if (ino == 0 || BIT(ino, dumpmap) == 0)
321 fprintf(stderr, "%s is not on tape\n", name);
322 return (ino);
323 }
324
325 /*
326 * Elicit a reply.
327 */
328 reply(question)
329 char *question;
330 {
331 char c;
332
333 do {
334 fprintf(stderr, "%s? [yn] ", question);
335 (void) fflush(stderr);
336 c = getc(terminal);
337 while (c != '\n' && getc(terminal) != '\n')
338 if (feof(terminal))
339 return (FAIL);
340 } while (c != 'y' && c != 'n');
341 if (c == 'y')
342 return (GOOD);
343 return (FAIL);
344 }
345
346 /*
347 * handle unexpected inconsistencies
348 */
349 /* VARARGS1 */
350 panic(msg, d1, d2)
351 char *msg;
352 long d1, d2;
353 {
354
355 fprintf(stderr, msg, d1, d2);
356 if (yflag)
357 return;
358 if (reply("abort") == GOOD) {
359 if (reply("dump core") == GOOD)
360 abort();
361 done(1);
362 }
363 }
364