restore.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1983 The Regents of the University of California.
3 1.1 cgd * 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.1 cgd static char sccsid[] = "@(#)restore.c 5.7 (Berkeley) 6/1/90";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #include "restore.h"
39 1.1 cgd
40 1.1 cgd /*
41 1.1 cgd * This implements the 't' option.
42 1.1 cgd * List entries on the tape.
43 1.1 cgd */
44 1.1 cgd long
45 1.1 cgd listfile(name, ino, type)
46 1.1 cgd char *name;
47 1.1 cgd ino_t ino;
48 1.1 cgd int type;
49 1.1 cgd {
50 1.1 cgd long descend = hflag ? GOOD : FAIL;
51 1.1 cgd
52 1.1 cgd if (BIT(ino, dumpmap) == 0) {
53 1.1 cgd return (descend);
54 1.1 cgd }
55 1.1 cgd vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
56 1.1 cgd fprintf(stdout, "%10d\t%s\n", ino, name);
57 1.1 cgd return (descend);
58 1.1 cgd }
59 1.1 cgd
60 1.1 cgd /*
61 1.1 cgd * This implements the 'x' option.
62 1.1 cgd * Request that new entries be extracted.
63 1.1 cgd */
64 1.1 cgd long
65 1.1 cgd addfile(name, ino, type)
66 1.1 cgd char *name;
67 1.1 cgd ino_t ino;
68 1.1 cgd int type;
69 1.1 cgd {
70 1.1 cgd register struct entry *ep;
71 1.1 cgd long descend = hflag ? GOOD : FAIL;
72 1.1 cgd char buf[100];
73 1.1 cgd
74 1.1 cgd if (BIT(ino, dumpmap) == 0) {
75 1.1 cgd dprintf(stdout, "%s: not on the tape\n", name);
76 1.1 cgd return (descend);
77 1.1 cgd }
78 1.1 cgd if (!mflag) {
79 1.1 cgd (void) sprintf(buf, "./%u", ino);
80 1.1 cgd name = buf;
81 1.1 cgd if (type == NODE) {
82 1.1 cgd (void) genliteraldir(name, ino);
83 1.1 cgd return (descend);
84 1.1 cgd }
85 1.1 cgd }
86 1.1 cgd ep = lookupino(ino);
87 1.1 cgd if (ep != NIL) {
88 1.1 cgd if (strcmp(name, myname(ep)) == 0) {
89 1.1 cgd ep->e_flags |= NEW;
90 1.1 cgd return (descend);
91 1.1 cgd }
92 1.1 cgd type |= LINK;
93 1.1 cgd }
94 1.1 cgd ep = addentry(name, ino, type);
95 1.1 cgd if (type == NODE)
96 1.1 cgd newnode(ep);
97 1.1 cgd ep->e_flags |= NEW;
98 1.1 cgd return (descend);
99 1.1 cgd }
100 1.1 cgd
101 1.1 cgd /*
102 1.1 cgd * This is used by the 'i' option to undo previous requests made by addfile.
103 1.1 cgd * Delete entries from the request queue.
104 1.1 cgd */
105 1.1 cgd /* ARGSUSED */
106 1.1 cgd long
107 1.1 cgd deletefile(name, ino, type)
108 1.1 cgd char *name;
109 1.1 cgd ino_t ino;
110 1.1 cgd int type;
111 1.1 cgd {
112 1.1 cgd long descend = hflag ? GOOD : FAIL;
113 1.1 cgd struct entry *ep;
114 1.1 cgd
115 1.1 cgd if (BIT(ino, dumpmap) == 0) {
116 1.1 cgd return (descend);
117 1.1 cgd }
118 1.1 cgd ep = lookupino(ino);
119 1.1 cgd if (ep != NIL)
120 1.1 cgd ep->e_flags &= ~NEW;
121 1.1 cgd return (descend);
122 1.1 cgd }
123 1.1 cgd
124 1.1 cgd /*
125 1.1 cgd * The following four routines implement the incremental
126 1.1 cgd * restore algorithm. The first removes old entries, the second
127 1.1 cgd * does renames and calculates the extraction list, the third
128 1.1 cgd * cleans up link names missed by the first two, and the final
129 1.1 cgd * one deletes old directories.
130 1.1 cgd *
131 1.1 cgd * Directories cannot be immediately deleted, as they may have
132 1.1 cgd * other files in them which need to be moved out first. As
133 1.1 cgd * directories to be deleted are found, they are put on the
134 1.1 cgd * following deletion list. After all deletions and renames
135 1.1 cgd * are done, this list is actually deleted.
136 1.1 cgd */
137 1.1 cgd static struct entry *removelist;
138 1.1 cgd
139 1.1 cgd /*
140 1.1 cgd * Remove unneeded leaves from the old tree.
141 1.1 cgd * Remove directories from the lookup chains.
142 1.1 cgd */
143 1.1 cgd removeoldleaves()
144 1.1 cgd {
145 1.1 cgd register struct entry *ep;
146 1.1 cgd register ino_t i;
147 1.1 cgd
148 1.1 cgd vprintf(stdout, "Mark entries to be removed.\n");
149 1.1 cgd for (i = ROOTINO + 1; i < maxino; i++) {
150 1.1 cgd ep = lookupino(i);
151 1.1 cgd if (ep == NIL)
152 1.1 cgd continue;
153 1.1 cgd if (BIT(i, clrimap))
154 1.1 cgd continue;
155 1.1 cgd for ( ; ep != NIL; ep = ep->e_links) {
156 1.1 cgd dprintf(stdout, "%s: REMOVE\n", myname(ep));
157 1.1 cgd if (ep->e_type == LEAF) {
158 1.1 cgd removeleaf(ep);
159 1.1 cgd freeentry(ep);
160 1.1 cgd } else {
161 1.1 cgd mktempname(ep);
162 1.1 cgd deleteino(ep->e_ino);
163 1.1 cgd ep->e_next = removelist;
164 1.1 cgd removelist = ep;
165 1.1 cgd }
166 1.1 cgd }
167 1.1 cgd }
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd /*
171 1.1 cgd * For each directory entry on the incremental tape, determine which
172 1.1 cgd * category it falls into as follows:
173 1.1 cgd * KEEP - entries that are to be left alone.
174 1.1 cgd * NEW - new entries to be added.
175 1.1 cgd * EXTRACT - files that must be updated with new contents.
176 1.1 cgd * LINK - new links to be added.
177 1.1 cgd * Renames are done at the same time.
178 1.1 cgd */
179 1.1 cgd long
180 1.1 cgd nodeupdates(name, ino, type)
181 1.1 cgd char *name;
182 1.1 cgd ino_t ino;
183 1.1 cgd int type;
184 1.1 cgd {
185 1.1 cgd register struct entry *ep, *np, *ip;
186 1.1 cgd long descend = GOOD;
187 1.1 cgd int lookuptype = 0;
188 1.1 cgd int key = 0;
189 1.1 cgd /* key values */
190 1.1 cgd # define ONTAPE 0x1 /* inode is on the tape */
191 1.1 cgd # define INOFND 0x2 /* inode already exists */
192 1.1 cgd # define NAMEFND 0x4 /* name already exists */
193 1.1 cgd # define MODECHG 0x8 /* mode of inode changed */
194 1.1 cgd extern char *keyval();
195 1.1 cgd
196 1.1 cgd /*
197 1.1 cgd * This routine is called once for each element in the
198 1.1 cgd * directory hierarchy, with a full path name.
199 1.1 cgd * The "type" value is incorrectly specified as LEAF for
200 1.1 cgd * directories that are not on the dump tape.
201 1.1 cgd *
202 1.1 cgd * Check to see if the file is on the tape.
203 1.1 cgd */
204 1.1 cgd if (BIT(ino, dumpmap))
205 1.1 cgd key |= ONTAPE;
206 1.1 cgd /*
207 1.1 cgd * Check to see if the name exists, and if the name is a link.
208 1.1 cgd */
209 1.1 cgd np = lookupname(name);
210 1.1 cgd if (np != NIL) {
211 1.1 cgd key |= NAMEFND;
212 1.1 cgd ip = lookupino(np->e_ino);
213 1.1 cgd if (ip == NULL)
214 1.1 cgd panic("corrupted symbol table\n");
215 1.1 cgd if (ip != np)
216 1.1 cgd lookuptype = LINK;
217 1.1 cgd }
218 1.1 cgd /*
219 1.1 cgd * Check to see if the inode exists, and if one of its links
220 1.1 cgd * corresponds to the name (if one was found).
221 1.1 cgd */
222 1.1 cgd ip = lookupino(ino);
223 1.1 cgd if (ip != NIL) {
224 1.1 cgd key |= INOFND;
225 1.1 cgd for (ep = ip->e_links; ep != NIL; ep = ep->e_links) {
226 1.1 cgd if (ep == np) {
227 1.1 cgd ip = ep;
228 1.1 cgd break;
229 1.1 cgd }
230 1.1 cgd }
231 1.1 cgd }
232 1.1 cgd /*
233 1.1 cgd * If both a name and an inode are found, but they do not
234 1.1 cgd * correspond to the same file, then both the inode that has
235 1.1 cgd * been found and the inode corresponding to the name that
236 1.1 cgd * has been found need to be renamed. The current pathname
237 1.1 cgd * is the new name for the inode that has been found. Since
238 1.1 cgd * all files to be deleted have already been removed, the
239 1.1 cgd * named file is either a now unneeded link, or it must live
240 1.1 cgd * under a new name in this dump level. If it is a link, it
241 1.1 cgd * can be removed. If it is not a link, it is given a
242 1.1 cgd * temporary name in anticipation that it will be renamed
243 1.1 cgd * when it is later found by inode number.
244 1.1 cgd */
245 1.1 cgd if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
246 1.1 cgd if (lookuptype == LINK) {
247 1.1 cgd removeleaf(np);
248 1.1 cgd freeentry(np);
249 1.1 cgd } else {
250 1.1 cgd dprintf(stdout, "name/inode conflict, mktempname %s\n",
251 1.1 cgd myname(np));
252 1.1 cgd mktempname(np);
253 1.1 cgd }
254 1.1 cgd np = NIL;
255 1.1 cgd key &= ~NAMEFND;
256 1.1 cgd }
257 1.1 cgd if ((key & ONTAPE) &&
258 1.1 cgd (((key & INOFND) && ip->e_type != type) ||
259 1.1 cgd ((key & NAMEFND) && np->e_type != type)))
260 1.1 cgd key |= MODECHG;
261 1.1 cgd
262 1.1 cgd /*
263 1.1 cgd * Decide on the disposition of the file based on its flags.
264 1.1 cgd * Note that we have already handled the case in which
265 1.1 cgd * a name and inode are found that correspond to different files.
266 1.1 cgd * Thus if both NAMEFND and INOFND are set then ip == np.
267 1.1 cgd */
268 1.1 cgd switch (key) {
269 1.1 cgd
270 1.1 cgd /*
271 1.1 cgd * A previously existing file has been found.
272 1.1 cgd * Mark it as KEEP so that other links to the inode can be
273 1.1 cgd * detected, and so that it will not be reclaimed by the search
274 1.1 cgd * for unreferenced names.
275 1.1 cgd */
276 1.1 cgd case INOFND|NAMEFND:
277 1.1 cgd ip->e_flags |= KEEP;
278 1.1 cgd dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
279 1.1 cgd flagvalues(ip));
280 1.1 cgd break;
281 1.1 cgd
282 1.1 cgd /*
283 1.1 cgd * A file on the tape has a name which is the same as a name
284 1.1 cgd * corresponding to a different file in the previous dump.
285 1.1 cgd * Since all files to be deleted have already been removed,
286 1.1 cgd * this file is either a now unneeded link, or it must live
287 1.1 cgd * under a new name in this dump level. If it is a link, it
288 1.1 cgd * can simply be removed. If it is not a link, it is given a
289 1.1 cgd * temporary name in anticipation that it will be renamed
290 1.1 cgd * when it is later found by inode number (see INOFND case
291 1.1 cgd * below). The entry is then treated as a new file.
292 1.1 cgd */
293 1.1 cgd case ONTAPE|NAMEFND:
294 1.1 cgd case ONTAPE|NAMEFND|MODECHG:
295 1.1 cgd if (lookuptype == LINK) {
296 1.1 cgd removeleaf(np);
297 1.1 cgd freeentry(np);
298 1.1 cgd } else {
299 1.1 cgd mktempname(np);
300 1.1 cgd }
301 1.1 cgd /* fall through */
302 1.1 cgd
303 1.1 cgd /*
304 1.1 cgd * A previously non-existent file.
305 1.1 cgd * Add it to the file system, and request its extraction.
306 1.1 cgd * If it is a directory, create it immediately.
307 1.1 cgd * (Since the name is unused there can be no conflict)
308 1.1 cgd */
309 1.1 cgd case ONTAPE:
310 1.1 cgd ep = addentry(name, ino, type);
311 1.1 cgd if (type == NODE)
312 1.1 cgd newnode(ep);
313 1.1 cgd ep->e_flags |= NEW|KEEP;
314 1.1 cgd dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
315 1.1 cgd flagvalues(ep));
316 1.1 cgd break;
317 1.1 cgd
318 1.1 cgd /*
319 1.1 cgd * A file with the same inode number, but a different
320 1.1 cgd * name has been found. If the other name has not already
321 1.1 cgd * been found (indicated by the KEEP flag, see above) then
322 1.1 cgd * this must be a new name for the file, and it is renamed.
323 1.1 cgd * If the other name has been found then this must be a
324 1.1 cgd * link to the file. Hard links to directories are not
325 1.1 cgd * permitted, and are either deleted or converted to
326 1.1 cgd * symbolic links. Finally, if the file is on the tape,
327 1.1 cgd * a request is made to extract it.
328 1.1 cgd */
329 1.1 cgd case ONTAPE|INOFND:
330 1.1 cgd if (type == LEAF && (ip->e_flags & KEEP) == 0)
331 1.1 cgd ip->e_flags |= EXTRACT;
332 1.1 cgd /* fall through */
333 1.1 cgd case INOFND:
334 1.1 cgd if ((ip->e_flags & KEEP) == 0) {
335 1.1 cgd renameit(myname(ip), name);
336 1.1 cgd moveentry(ip, name);
337 1.1 cgd ip->e_flags |= KEEP;
338 1.1 cgd dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
339 1.1 cgd flagvalues(ip));
340 1.1 cgd break;
341 1.1 cgd }
342 1.1 cgd if (ip->e_type == NODE) {
343 1.1 cgd descend = FAIL;
344 1.1 cgd fprintf(stderr,
345 1.1 cgd "deleted hard link %s to directory %s\n",
346 1.1 cgd name, myname(ip));
347 1.1 cgd break;
348 1.1 cgd }
349 1.1 cgd ep = addentry(name, ino, type|LINK);
350 1.1 cgd ep->e_flags |= NEW;
351 1.1 cgd dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
352 1.1 cgd flagvalues(ep));
353 1.1 cgd break;
354 1.1 cgd
355 1.1 cgd /*
356 1.1 cgd * A previously known file which is to be updated.
357 1.1 cgd */
358 1.1 cgd case ONTAPE|INOFND|NAMEFND:
359 1.1 cgd if (type == LEAF && lookuptype != LINK)
360 1.1 cgd np->e_flags |= EXTRACT;
361 1.1 cgd np->e_flags |= KEEP;
362 1.1 cgd dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
363 1.1 cgd flagvalues(np));
364 1.1 cgd break;
365 1.1 cgd
366 1.1 cgd /*
367 1.1 cgd * An inode is being reused in a completely different way.
368 1.1 cgd * Normally an extract can simply do an "unlink" followed
369 1.1 cgd * by a "creat". Here we must do effectively the same
370 1.1 cgd * thing. The complications arise because we cannot really
371 1.1 cgd * delete a directory since it may still contain files
372 1.1 cgd * that we need to rename, so we delete it from the symbol
373 1.1 cgd * table, and put it on the list to be deleted eventually.
374 1.1 cgd * Conversely if a directory is to be created, it must be
375 1.1 cgd * done immediately, rather than waiting until the
376 1.1 cgd * extraction phase.
377 1.1 cgd */
378 1.1 cgd case ONTAPE|INOFND|MODECHG:
379 1.1 cgd case ONTAPE|INOFND|NAMEFND|MODECHG:
380 1.1 cgd if (ip->e_flags & KEEP) {
381 1.1 cgd badentry(ip, "cannot KEEP and change modes");
382 1.1 cgd break;
383 1.1 cgd }
384 1.1 cgd if (ip->e_type == LEAF) {
385 1.1 cgd /* changing from leaf to node */
386 1.1 cgd removeleaf(ip);
387 1.1 cgd freeentry(ip);
388 1.1 cgd ip = addentry(name, ino, type);
389 1.1 cgd newnode(ip);
390 1.1 cgd } else {
391 1.1 cgd /* changing from node to leaf */
392 1.1 cgd if ((ip->e_flags & TMPNAME) == 0)
393 1.1 cgd mktempname(ip);
394 1.1 cgd deleteino(ip->e_ino);
395 1.1 cgd ip->e_next = removelist;
396 1.1 cgd removelist = ip;
397 1.1 cgd ip = addentry(name, ino, type);
398 1.1 cgd }
399 1.1 cgd ip->e_flags |= NEW|KEEP;
400 1.1 cgd dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
401 1.1 cgd flagvalues(ip));
402 1.1 cgd break;
403 1.1 cgd
404 1.1 cgd /*
405 1.1 cgd * A hard link to a diirectory that has been removed.
406 1.1 cgd * Ignore it.
407 1.1 cgd */
408 1.1 cgd case NAMEFND:
409 1.1 cgd dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
410 1.1 cgd name);
411 1.1 cgd descend = FAIL;
412 1.1 cgd break;
413 1.1 cgd
414 1.1 cgd /*
415 1.1 cgd * If we find a directory entry for a file that is not on
416 1.1 cgd * the tape, then we must have found a file that was created
417 1.1 cgd * while the dump was in progress. Since we have no contents
418 1.1 cgd * for it, we discard the name knowing that it will be on the
419 1.1 cgd * next incremental tape.
420 1.1 cgd */
421 1.1 cgd case NIL:
422 1.1 cgd fprintf(stderr, "%s: (inode %d) not found on tape\n",
423 1.1 cgd name, ino);
424 1.1 cgd break;
425 1.1 cgd
426 1.1 cgd /*
427 1.1 cgd * If any of these arise, something is grievously wrong with
428 1.1 cgd * the current state of the symbol table.
429 1.1 cgd */
430 1.1 cgd case INOFND|NAMEFND|MODECHG:
431 1.1 cgd case NAMEFND|MODECHG:
432 1.1 cgd case INOFND|MODECHG:
433 1.1 cgd fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
434 1.1 cgd name);
435 1.1 cgd break;
436 1.1 cgd
437 1.1 cgd /*
438 1.1 cgd * These states "cannot" arise for any state of the symbol table.
439 1.1 cgd */
440 1.1 cgd case ONTAPE|MODECHG:
441 1.1 cgd case MODECHG:
442 1.1 cgd default:
443 1.1 cgd panic("[%s] %s: impossible state\n", keyval(key), name);
444 1.1 cgd break;
445 1.1 cgd }
446 1.1 cgd return (descend);
447 1.1 cgd }
448 1.1 cgd
449 1.1 cgd /*
450 1.1 cgd * Calculate the active flags in a key.
451 1.1 cgd */
452 1.1 cgd char *
453 1.1 cgd keyval(key)
454 1.1 cgd int key;
455 1.1 cgd {
456 1.1 cgd static char keybuf[32];
457 1.1 cgd
458 1.1 cgd (void) strcpy(keybuf, "|NIL");
459 1.1 cgd keybuf[0] = '\0';
460 1.1 cgd if (key & ONTAPE)
461 1.1 cgd (void) strcat(keybuf, "|ONTAPE");
462 1.1 cgd if (key & INOFND)
463 1.1 cgd (void) strcat(keybuf, "|INOFND");
464 1.1 cgd if (key & NAMEFND)
465 1.1 cgd (void) strcat(keybuf, "|NAMEFND");
466 1.1 cgd if (key & MODECHG)
467 1.1 cgd (void) strcat(keybuf, "|MODECHG");
468 1.1 cgd return (&keybuf[1]);
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd /*
472 1.1 cgd * Find unreferenced link names.
473 1.1 cgd */
474 1.1 cgd findunreflinks()
475 1.1 cgd {
476 1.1 cgd register struct entry *ep, *np;
477 1.1 cgd register ino_t i;
478 1.1 cgd
479 1.1 cgd vprintf(stdout, "Find unreferenced names.\n");
480 1.1 cgd for (i = ROOTINO; i < maxino; i++) {
481 1.1 cgd ep = lookupino(i);
482 1.1 cgd if (ep == NIL || ep->e_type == LEAF || BIT(i, dumpmap) == 0)
483 1.1 cgd continue;
484 1.1 cgd for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
485 1.1 cgd if (np->e_flags == 0) {
486 1.1 cgd dprintf(stdout,
487 1.1 cgd "%s: remove unreferenced name\n",
488 1.1 cgd myname(np));
489 1.1 cgd removeleaf(np);
490 1.1 cgd freeentry(np);
491 1.1 cgd }
492 1.1 cgd }
493 1.1 cgd }
494 1.1 cgd /*
495 1.1 cgd * Any leaves remaining in removed directories is unreferenced.
496 1.1 cgd */
497 1.1 cgd for (ep = removelist; ep != NIL; ep = ep->e_next) {
498 1.1 cgd for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
499 1.1 cgd if (np->e_type == LEAF) {
500 1.1 cgd if (np->e_flags != 0)
501 1.1 cgd badentry(np, "unreferenced with flags");
502 1.1 cgd dprintf(stdout,
503 1.1 cgd "%s: remove unreferenced name\n",
504 1.1 cgd myname(np));
505 1.1 cgd removeleaf(np);
506 1.1 cgd freeentry(np);
507 1.1 cgd }
508 1.1 cgd }
509 1.1 cgd }
510 1.1 cgd }
511 1.1 cgd
512 1.1 cgd /*
513 1.1 cgd * Remove old nodes (directories).
514 1.1 cgd * Note that this routine runs in O(N*D) where:
515 1.1 cgd * N is the number of directory entries to be removed.
516 1.1 cgd * D is the maximum depth of the tree.
517 1.1 cgd * If N == D this can be quite slow. If the list were
518 1.1 cgd * topologically sorted, the deletion could be done in
519 1.1 cgd * time O(N).
520 1.1 cgd */
521 1.1 cgd removeoldnodes()
522 1.1 cgd {
523 1.1 cgd register struct entry *ep, **prev;
524 1.1 cgd long change;
525 1.1 cgd
526 1.1 cgd vprintf(stdout, "Remove old nodes (directories).\n");
527 1.1 cgd do {
528 1.1 cgd change = 0;
529 1.1 cgd prev = &removelist;
530 1.1 cgd for (ep = removelist; ep != NIL; ep = *prev) {
531 1.1 cgd if (ep->e_entries != NIL) {
532 1.1 cgd prev = &ep->e_next;
533 1.1 cgd continue;
534 1.1 cgd }
535 1.1 cgd *prev = ep->e_next;
536 1.1 cgd removenode(ep);
537 1.1 cgd freeentry(ep);
538 1.1 cgd change++;
539 1.1 cgd }
540 1.1 cgd } while (change);
541 1.1 cgd for (ep = removelist; ep != NIL; ep = ep->e_next)
542 1.1 cgd badentry(ep, "cannot remove, non-empty");
543 1.1 cgd }
544 1.1 cgd
545 1.1 cgd /*
546 1.1 cgd * This is the routine used to extract files for the 'r' command.
547 1.1 cgd * Extract new leaves.
548 1.1 cgd */
549 1.1 cgd createleaves(symtabfile)
550 1.1 cgd char *symtabfile;
551 1.1 cgd {
552 1.1 cgd register struct entry *ep;
553 1.1 cgd ino_t first;
554 1.1 cgd long curvol;
555 1.1 cgd
556 1.1 cgd if (command == 'R') {
557 1.1 cgd vprintf(stdout, "Continue extraction of new leaves\n");
558 1.1 cgd } else {
559 1.1 cgd vprintf(stdout, "Extract new leaves.\n");
560 1.1 cgd dumpsymtable(symtabfile, volno);
561 1.1 cgd }
562 1.1 cgd first = lowerbnd(ROOTINO);
563 1.1 cgd curvol = volno;
564 1.1 cgd while (curfile.ino < maxino) {
565 1.1 cgd first = lowerbnd(first);
566 1.1 cgd /*
567 1.1 cgd * If the next available file is not the one which we
568 1.1 cgd * expect then we have missed one or more files. Since
569 1.1 cgd * we do not request files that were not on the tape,
570 1.1 cgd * the lost files must have been due to a tape read error,
571 1.1 cgd * or a file that was removed while the dump was in progress.
572 1.1 cgd */
573 1.1 cgd while (first < curfile.ino) {
574 1.1 cgd ep = lookupino(first);
575 1.1 cgd if (ep == NIL)
576 1.1 cgd panic("%d: bad first\n", first);
577 1.1 cgd fprintf(stderr, "%s: not found on tape\n", myname(ep));
578 1.1 cgd ep->e_flags &= ~(NEW|EXTRACT);
579 1.1 cgd first = lowerbnd(first);
580 1.1 cgd }
581 1.1 cgd /*
582 1.1 cgd * If we find files on the tape that have no corresponding
583 1.1 cgd * directory entries, then we must have found a file that
584 1.1 cgd * was created while the dump was in progress. Since we have
585 1.1 cgd * no name for it, we discard it knowing that it will be
586 1.1 cgd * on the next incremental tape.
587 1.1 cgd */
588 1.1 cgd if (first != curfile.ino) {
589 1.1 cgd fprintf(stderr, "expected next file %d, got %d\n",
590 1.1 cgd first, curfile.ino);
591 1.1 cgd skipfile();
592 1.1 cgd goto next;
593 1.1 cgd }
594 1.1 cgd ep = lookupino(curfile.ino);
595 1.1 cgd if (ep == NIL)
596 1.1 cgd panic("unknown file on tape\n");
597 1.1 cgd if ((ep->e_flags & (NEW|EXTRACT)) == 0)
598 1.1 cgd badentry(ep, "unexpected file on tape");
599 1.1 cgd /*
600 1.1 cgd * If the file is to be extracted, then the old file must
601 1.1 cgd * be removed since its type may change from one leaf type
602 1.1 cgd * to another (eg "file" to "character special").
603 1.1 cgd */
604 1.1 cgd if ((ep->e_flags & EXTRACT) != 0) {
605 1.1 cgd removeleaf(ep);
606 1.1 cgd ep->e_flags &= ~REMOVED;
607 1.1 cgd }
608 1.1 cgd (void) extractfile(myname(ep));
609 1.1 cgd ep->e_flags &= ~(NEW|EXTRACT);
610 1.1 cgd /*
611 1.1 cgd * We checkpoint the restore after every tape reel, so
612 1.1 cgd * as to simplify the amount of work re quired by the
613 1.1 cgd * 'R' command.
614 1.1 cgd */
615 1.1 cgd next:
616 1.1 cgd if (curvol != volno) {
617 1.1 cgd dumpsymtable(symtabfile, volno);
618 1.1 cgd skipmaps();
619 1.1 cgd curvol = volno;
620 1.1 cgd }
621 1.1 cgd }
622 1.1 cgd }
623 1.1 cgd
624 1.1 cgd /*
625 1.1 cgd * This is the routine used to extract files for the 'x' and 'i' commands.
626 1.1 cgd * Efficiently extract a subset of the files on a tape.
627 1.1 cgd */
628 1.1 cgd createfiles()
629 1.1 cgd {
630 1.1 cgd register ino_t first, next, last;
631 1.1 cgd register struct entry *ep;
632 1.1 cgd long curvol;
633 1.1 cgd
634 1.1 cgd vprintf(stdout, "Extract requested files\n");
635 1.1 cgd curfile.action = SKIP;
636 1.1 cgd getvol((long)1);
637 1.1 cgd skipmaps();
638 1.1 cgd skipdirs();
639 1.1 cgd first = lowerbnd(ROOTINO);
640 1.1 cgd last = upperbnd(maxino - 1);
641 1.1 cgd for (;;) {
642 1.1 cgd first = lowerbnd(first);
643 1.1 cgd last = upperbnd(last);
644 1.1 cgd /*
645 1.1 cgd * Check to see if any files remain to be extracted
646 1.1 cgd */
647 1.1 cgd if (first > last)
648 1.1 cgd return;
649 1.1 cgd /*
650 1.1 cgd * Reject any volumes with inodes greater
651 1.1 cgd * than the last one needed
652 1.1 cgd */
653 1.1 cgd while (curfile.ino > last) {
654 1.1 cgd curfile.action = SKIP;
655 1.1 cgd getvol((long)0);
656 1.1 cgd skipmaps();
657 1.1 cgd skipdirs();
658 1.1 cgd }
659 1.1 cgd /*
660 1.1 cgd * Decide on the next inode needed.
661 1.1 cgd * Skip across the inodes until it is found
662 1.1 cgd * or an out of order volume change is encountered
663 1.1 cgd */
664 1.1 cgd next = lowerbnd(curfile.ino);
665 1.1 cgd do {
666 1.1 cgd curvol = volno;
667 1.1 cgd while (next > curfile.ino && volno == curvol)
668 1.1 cgd skipfile();
669 1.1 cgd skipmaps();
670 1.1 cgd skipdirs();
671 1.1 cgd } while (volno == curvol + 1);
672 1.1 cgd /*
673 1.1 cgd * If volume change out of order occurred the
674 1.1 cgd * current state must be recalculated
675 1.1 cgd */
676 1.1 cgd if (volno != curvol)
677 1.1 cgd continue;
678 1.1 cgd /*
679 1.1 cgd * If the current inode is greater than the one we were
680 1.1 cgd * looking for then we missed the one we were looking for.
681 1.1 cgd * Since we only attempt to extract files listed in the
682 1.1 cgd * dump map, the lost files must have been due to a tape
683 1.1 cgd * read error, or a file that was removed while the dump
684 1.1 cgd * was in progress. Thus we report all requested files
685 1.1 cgd * between the one we were looking for, and the one we
686 1.1 cgd * found as missing, and delete their request flags.
687 1.1 cgd */
688 1.1 cgd while (next < curfile.ino) {
689 1.1 cgd ep = lookupino(next);
690 1.1 cgd if (ep == NIL)
691 1.1 cgd panic("corrupted symbol table\n");
692 1.1 cgd fprintf(stderr, "%s: not found on tape\n", myname(ep));
693 1.1 cgd ep->e_flags &= ~NEW;
694 1.1 cgd next = lowerbnd(next);
695 1.1 cgd }
696 1.1 cgd /*
697 1.1 cgd * The current inode is the one that we are looking for,
698 1.1 cgd * so extract it per its requested name.
699 1.1 cgd */
700 1.1 cgd if (next == curfile.ino && next <= last) {
701 1.1 cgd ep = lookupino(next);
702 1.1 cgd if (ep == NIL)
703 1.1 cgd panic("corrupted symbol table\n");
704 1.1 cgd (void) extractfile(myname(ep));
705 1.1 cgd ep->e_flags &= ~NEW;
706 1.1 cgd if (volno != curvol)
707 1.1 cgd skipmaps();
708 1.1 cgd }
709 1.1 cgd }
710 1.1 cgd }
711 1.1 cgd
712 1.1 cgd /*
713 1.1 cgd * Add links.
714 1.1 cgd */
715 1.1 cgd createlinks()
716 1.1 cgd {
717 1.1 cgd register struct entry *np, *ep;
718 1.1 cgd register ino_t i;
719 1.1 cgd char name[BUFSIZ];
720 1.1 cgd
721 1.1 cgd vprintf(stdout, "Add links\n");
722 1.1 cgd for (i = ROOTINO; i < maxino; i++) {
723 1.1 cgd ep = lookupino(i);
724 1.1 cgd if (ep == NIL)
725 1.1 cgd continue;
726 1.1 cgd for (np = ep->e_links; np != NIL; np = np->e_links) {
727 1.1 cgd if ((np->e_flags & NEW) == 0)
728 1.1 cgd continue;
729 1.1 cgd (void) strcpy(name, myname(ep));
730 1.1 cgd if (ep->e_type == NODE) {
731 1.1 cgd (void) linkit(name, myname(np), SYMLINK);
732 1.1 cgd } else {
733 1.1 cgd (void) linkit(name, myname(np), HARDLINK);
734 1.1 cgd }
735 1.1 cgd np->e_flags &= ~NEW;
736 1.1 cgd }
737 1.1 cgd }
738 1.1 cgd }
739 1.1 cgd
740 1.1 cgd /*
741 1.1 cgd * Check the symbol table.
742 1.1 cgd * We do this to insure that all the requested work was done, and
743 1.1 cgd * that no temporary names remain.
744 1.1 cgd */
745 1.1 cgd checkrestore()
746 1.1 cgd {
747 1.1 cgd register struct entry *ep;
748 1.1 cgd register ino_t i;
749 1.1 cgd
750 1.1 cgd vprintf(stdout, "Check the symbol table.\n");
751 1.1 cgd for (i = ROOTINO; i < maxino; i++) {
752 1.1 cgd for (ep = lookupino(i); ep != NIL; ep = ep->e_links) {
753 1.1 cgd ep->e_flags &= ~KEEP;
754 1.1 cgd if (ep->e_type == NODE)
755 1.1 cgd ep->e_flags &= ~(NEW|EXISTED);
756 1.1 cgd if (ep->e_flags != NULL)
757 1.1 cgd badentry(ep, "incomplete operations");
758 1.1 cgd }
759 1.1 cgd }
760 1.1 cgd }
761 1.1 cgd
762 1.1 cgd /*
763 1.1 cgd * Compare with the directory structure on the tape
764 1.1 cgd * A paranoid check that things are as they should be.
765 1.1 cgd */
766 1.1 cgd long
767 1.1 cgd verifyfile(name, ino, type)
768 1.1 cgd char *name;
769 1.1 cgd ino_t ino;
770 1.1 cgd int type;
771 1.1 cgd {
772 1.1 cgd struct entry *np, *ep;
773 1.1 cgd long descend = GOOD;
774 1.1 cgd
775 1.1 cgd ep = lookupname(name);
776 1.1 cgd if (ep == NIL) {
777 1.1 cgd fprintf(stderr, "Warning: missing name %s\n", name);
778 1.1 cgd return (FAIL);
779 1.1 cgd }
780 1.1 cgd np = lookupino(ino);
781 1.1 cgd if (np != ep)
782 1.1 cgd descend = FAIL;
783 1.1 cgd for ( ; np != NIL; np = np->e_links)
784 1.1 cgd if (np == ep)
785 1.1 cgd break;
786 1.1 cgd if (np == NIL)
787 1.1 cgd panic("missing inumber %d\n", ino);
788 1.1 cgd if (ep->e_type == LEAF && type != LEAF)
789 1.1 cgd badentry(ep, "type should be LEAF");
790 1.1 cgd return (descend);
791 1.1 cgd }
792