Debian Codegraph @

restore/restore.c

raw symbols SCIP
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <[email protected]>, 1994-1997
5 * Stelian Pop <[email protected]>, 1999-2000
6 * Stelian Pop <[email protected]> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include "config.h" // IWYU pragma: keep
39 #include "restore.h"
40
41 #include <sys/types.h>
42
43 #include "compat/include/bsdcompat.h"
44
45 #include "compat/include/protocols/dumprestore.h"
46
47 #include "compat/include/compaterr.h" // IWYU pragma: keep
48 #include <stdio.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <stdint.h>
52 #include <time.h>
53
54 #include "readtape.h"
55 #include "restore_globals.h"
56 #include "utilities.h"
57 #include "dirs.h"
58 #include "symtab.h"
59 #include "tape.h"
60
61 static char *keyval (int);
62
63 /*
64 * This implements the 't' option.
65 * List entries on the tape.
66 */
67 long
68 listfile(const char *name, dump_ino_t ino, int type)
69 {
70 long descend = hflag ? GOOD : FAIL;
71 #ifdef USE_QFA
72 long tnum = 0;
73 long long tpos = 0;
74 #endif
75
76 if (TSTINO(ino, dumpinomap) == 0)
77 return (descend);
78 Vmsg("%s", type == LEAF ? "leaf" : "dir ");
79 #ifdef USE_QFA
80 if (tapeposflag) { /* add QFA positions to output */
81 if (type == LEAF)
82 (void)Inode2Tapepos(ino, &tnum, &tpos, 1);
83 fprintf(stdout, "%10u\t%ld\t%lld\t%s%c", ino,
84 tnum, tpos, name, nullflag?'\0':'\n');
85 }
86 else
87 #endif
88 fprintf(stdout, "%10u\t%s%c", ino, name,
89 nullflag?'\0':'\n');
90 return (descend);
91 }
92
93 /*
94 * This implements the 'x' option.
95 * Request that new entries be extracted.
96 */
97 long
98 addfile(const char *name, dump_ino_t ino, int type)
99 {
100 struct entry *ep, *np;
101 long descend = hflag ? GOOD : FAIL;
102 char buf[100];
103
104 if (TSTINO(ino, dumpinomap) == 0) {
105 Dmsg(DEFAULT_DEBUG, "%s: not on the tape\n", name);
106 return (descend);
107 }
108 if (ino == WINO && command == 'i' && !vflag)
109 return (descend);
110 if (!mflag) {
111 (void) snprintf(buf, sizeof(buf), "./%u", ino);
112 name = buf;
113 if (type == NODE) {
114 (void) genliteraldir(name, ino);
115 return (descend);
116 }
117 }
118 ep = lookupino(ino);
119 if (ep != NULL) {
120 if (strcmp(name, myname(ep)) == 0) {
121 ep->e_flags |= NEW;
122 return (descend);
123 }
124 type |= LINK;
125 for (np = ep->e_links; np; np = np->e_links)
126 if (strcmp(name, myname(np)) == 0) {
127 np->e_flags |= NEW;
128 return (descend);
129 }
130 }
131 ep = addentry(name, ino, type);
132 #ifdef USE_QFA
133 if ((type == NODE) && (!createtapeposflag))
134 #else
135 if (type == NODE)
136 #endif
137 newnode(ep);
138 ep->e_flags |= NEW;
139 return (descend);
140 }
141
142 /*
143 * This is used by the 'i' option to undo previous requests made by addfile.
144 * Delete entries from the request queue.
145 */
146 /* ARGSUSED */
147 long
148 deletefile(const char *name, dump_ino_t ino, UNUSED(int type))
149 {
150 long descend = hflag ? GOOD : FAIL;
151 struct entry *ep;
152
153 if (TSTINO(ino, dumpinomap) == 0)
154 return (descend);
155 ep = lookupname(name);
156 if (ep != NULL) {
157 ep->e_flags &= ~NEW;
158 ep->e_flags |= REMOVED;
159 if (ep->e_type != NODE)
160 freeentry(ep);
161 }
162 return (descend);
163 }
164
165 /*
166 * The following four routines implement the incremental
167 * restore algorithm. The first removes old entries, the second
168 * does renames and calculates the extraction list, the third
169 * cleans up link names missed by the first two, and the final
170 * one deletes old directories.
171 *
172 * Directories cannot be immediately deleted, as they may have
173 * other files in them which need to be moved out first. As
174 * directories to be deleted are found, they are put on the
175 * following deletion list. After all deletions and renames
176 * are done, this list is actually deleted.
177 */
178 static struct entry *removelist;
179
180 /*
181 * Remove invalid whiteouts from the old tree.
182 * Remove unneeded leaves from the old tree.
183 * Remove directories from the lookup chains.
184 */
185 void
186 removeoldleaves(void)
187 {
188 struct entry *ep, *nextep;
189 dump_ino_t i, mydirino;
190
191 Vmsg("Mark entries to be removed.\n");
192 if ((ep = lookupino(WINO))) {
193 Vmsg("Delete whiteouts\n");
194 for ( ; ep != NULL; ep = nextep) {
195 nextep = ep->e_links;
196 mydirino = ep->e_parent->e_ino;
197 /*
198 * We remove all whiteouts that are in directories
199 * that have been removed or that have been dumped.
200 */
201 if (TSTINO(mydirino, usedinomap) &&
202 !TSTINO(mydirino, dumpinomap))
203 continue;
204 (void)fprintf(stderr, "BUG! Should call delwhiteout\n");
205 freeentry(ep);
206 }
207 }
208 for (i = ROOTINO + 1; i < maxino; i++) {
209 ep = lookupino(i);
210 if (ep == NULL)
211 continue;
212 if (TSTINO(i, usedinomap))
213 continue;
214 for ( ; ep != NULL; ep = ep->e_links) {
215 Dmsg(DEFAULT_DEBUG, "%s: REMOVE\n", myname(ep));
216 if (ep->e_type == LEAF) {
217 removeleaf(ep);
218 freeentry(ep);
219 } else {
220 mktempname(ep);
221 deleteino(ep->e_ino);
222 ep->e_next = removelist;
223 removelist = ep;
224 }
225 }
226 }
227 }
228
229 /*
230 * For each directory entry on the incremental tape, determine which
231 * category it falls into as follows:
232 * KEEP - entries that are to be left alone.
233 * NEW - new entries to be added.
234 * EXTRACT - files that must be updated with new contents.
235 * LINK - new links to be added.
236 * Renames are done at the same time.
237 */
238 long
239 nodeupdates(const char *name, dump_ino_t ino, int type)
240 {
241 struct entry *ep, *np, *ip;
242 long descend = GOOD;
243 int lookuptype = 0;
244 int key = 0;
245 /* key values */
246 # define ONTAPE 0x1 /* inode is on the tape */
247 # define INOFND 0x2 /* inode already exists */
248 # define NAMEFND 0x4 /* name already exists */
249 # define MODECHG 0x8 /* mode of inode changed */
250
251 /*
252 * This routine is called once for each element in the
253 * directory hierarchy, with a full path name.
254 * The "type" value is incorrectly specified as LEAF for
255 * directories that are not on the dump tape.
256 *
257 * Check to see if the file is on the tape.
258 */
259 if (TSTINO(ino, dumpinomap))
260 key |= ONTAPE;
261 /*
262 * Check to see if the name exists, and if the name is a link.
263 */
264 np = lookupname(name);
265 if (np != NULL) {
266 key |= NAMEFND;
267 ip = lookupino(np->e_ino);
268 if (ip == NULL)
269 panic("corrupted symbol table\n");
270 if (ip != np)
271 lookuptype = LINK;
272 }
273 /*
274 * Check to see if the inode exists, and if one of its links
275 * corresponds to the name (if one was found).
276 */
277 ip = lookupino(ino);
278 if (ip != NULL) {
279 key |= INOFND;
280 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
281 if (ep == np) {
282 ip = ep;
283 break;
284 }
285 }
286 }
287 /*
288 * If both a name and an inode are found, but they do not
289 * correspond to the same file, then both the inode that has
290 * been found and the inode corresponding to the name that
291 * has been found need to be renamed. The current pathname
292 * is the new name for the inode that has been found. Since
293 * all files to be deleted have already been removed, the
294 * named file is either a now unneeded link, or it must live
295 * under a new name in this dump level. If it is a link, it
296 * can be removed. If it is not a link, it is given a
297 * temporary name in anticipation that it will be renamed
298 * when it is later found by inode number.
299 */
300 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
301 if (lookuptype == LINK) {
302 removeleaf(np);
303 freeentry(np);
304 } else {
305 Dmsg(DEFAULT_DEBUG, "name/inode conflict, mktempname %s\n",
306 myname(np));
307 mktempname(np);
308 }
309 np = NULL;
310 key &= ~NAMEFND;
311 }
312 if ((key & ONTAPE) &&
313 (((key & INOFND) && ip->e_type != type) ||
314 ((key & NAMEFND) && np->e_type != type)))
315 key |= MODECHG;
316
317 /*
318 * Decide on the disposition of the file based on its flags.
319 * Note that we have already handled the case in which
320 * a name and inode are found that correspond to different files.
321 * Thus if both NAMEFND and INOFND are set then ip == np.
322 */
323 switch (key) {
324
325 /*
326 * A previously existing file has been found.
327 * Mark it as KEEP so that other links to the inode can be
328 * detected, and so that it will not be reclaimed by the search
329 * for unreferenced names.
330 */
331 case INOFND|NAMEFND:
332 ip->e_flags |= KEEP;
333 Dmsg(DEFAULT_DEBUG, "[%s] %s: %s\n", keyval(key), name,
334 flagvalues(ip));
335 break;
336
337 /*
338 * A file on the tape has a name which is the same as a name
339 * corresponding to a different file in the previous dump.
340 * Since all files to be deleted have already been removed,
341 * this file is either a now unneeded link, or it must live
342 * under a new name in this dump level. If it is a link, it
343 * can simply be removed. If it is not a link, it is given a
344 * temporary name in anticipation that it will be renamed
345 * when it is later found by inode number (see INOFND case
346 * below). The entry is then treated as a new file.
347 */
348 case ONTAPE|NAMEFND:
349 case ONTAPE|NAMEFND|MODECHG:
350 if (lookuptype == LINK) {
351 removeleaf(np);
352 freeentry(np);
353 } else {
354 mktempname(np);
355 }
356 /* fall through */
357
358 /*
359 * A previously non-existent file.
360 * Add it to the file system, and request its extraction.
361 * If it is a directory, create it immediately.
362 * (Since the name is unused there can be no conflict)
363 */
364 case ONTAPE:
365 ep = addentry(name, ino, type);
366 if (type == NODE)
367 newnode(ep);
368 ep->e_flags |= NEW|KEEP;
369 Dmsg(DEFAULT_DEBUG, "[%s] %s: %s\n", keyval(key), name,
370 flagvalues(ep));
371 break;
372
373 /*
374 * A file with the same inode number, but a different
375 * name has been found. If the other name has not already
376 * been found (indicated by the KEEP flag, see above) then
377 * this must be a new name for the file, and it is renamed.
378 * If the other name has been found then this must be a
379 * link to the file. Hard links to directories are not
380 * permitted, and are either deleted or converted to
381 * symbolic links. Finally, if the file is on the tape,
382 * a request is made to extract it.
383 */
384 case ONTAPE|INOFND:
385 if (type == LEAF && (ip->e_flags & KEEP) == 0)
386 ip->e_flags |= EXTRACT;
387 /* fall through */
388 case INOFND:
389 if ((ip->e_flags & KEEP) == 0) {
390 renameit(myname(ip), name);
391 moveentry(ip, name);
392 ip->e_flags |= KEEP;
393 Dmsg(DEFAULT_DEBUG, "[%s] %s: %s\n", keyval(key), name,
394 flagvalues(ip));
395 break;
396 }
397 if (ip->e_type == NODE) {
398 descend = FAIL;
399 fprintf(stderr,
400 "deleted hard link %s to directory %s\n",
401 name, myname(ip));
402 break;
403 }
404 ep = addentry(name, ino, type|LINK);
405 ep->e_flags |= NEW;
406 Dmsg(DEFAULT_DEBUG, "[%s] %s: %s|LINK\n", keyval(key), name,
407 flagvalues(ep));
408 break;
409
410 /*
411 * A previously known file which is to be updated. If it is a link,
412 * then all names referring to the previous file must be removed
413 * so that the subset of them that remain can be recreated.
414 */
415 case ONTAPE|INOFND|NAMEFND:
416 if (lookuptype == LINK) {
417 removeleaf(np);
418 freeentry(np);
419 ep = addentry(name, ino, type|LINK);
420 if (type == NODE)
421 newnode(ep);
422 ep->e_flags |= NEW|KEEP;
423 Dmsg(DEFAULT_DEBUG, "[%s] %s: %s|LINK\n", keyval(key), name,
424 flagvalues(ep));
425 break;
426 }
427 if (type == LEAF && lookuptype != LINK)
428 np->e_flags |= EXTRACT;
429 np->e_flags |= KEEP;
430 Dmsg(DEFAULT_DEBUG, "[%s] %s: %s\n", keyval(key), name,
431 flagvalues(np));
432 break;
433
434 /*
435 * An inode is being reused in a completely different way.
436 * Normally an extract can simply do an "unlink" followed
437 * by a "creat". Here we must do effectively the same
438 * thing. The complications arise because we cannot really
439 * delete a directory since it may still contain files
440 * that we need to rename, so we delete it from the symbol
441 * table, and put it on the list to be deleted eventually.
442 * Conversely if a directory is to be created, it must be
443 * done immediately, rather than waiting until the
444 * extraction phase.
445 */
446 case ONTAPE|INOFND|MODECHG:
447 case ONTAPE|INOFND|NAMEFND|MODECHG:
448 if (ip->e_flags & KEEP) {
449 badentry(ip, "cannot KEEP and change modes");
450 break;
451 }
452 if (ip->e_type == LEAF) {
453 /* changing from leaf to node */
454 for ( ; ip != NULL; ip = ip->e_links) {
455 if (ip->e_type != LEAF)
456 badentry(ip, "NODE and LEAF links to same inode");
457 removeleaf(ip);
458 freeentry(ip);
459 }
460 ip = addentry(name, ino, type);
461 newnode(ip);
462 } else {
463 /* changing from node to leaf */
464 if ((ip->e_flags & TMPNAME) == 0)
465 mktempname(ip);
466 deleteino(ip->e_ino);
467 ip->e_next = removelist;
468 removelist = ip;
469 ip = addentry(name, ino, type);
470 }
471 ip->e_flags |= NEW|KEEP;
472 Dmsg(DEFAULT_DEBUG, "[%s] %s: %s\n", keyval(key), name,
473 flagvalues(ip));
474 break;
475
476 /*
477 * A hard link to a directory that has been removed.
478 * Ignore it.
479 */
480 case NAMEFND:
481 Dmsg(DEFAULT_DEBUG, "[%s] %s: Extraneous name\n", keyval(key),
482 name);
483 descend = FAIL;
484 break;
485
486 /*
487 * If we find a directory entry for a file that is not on
488 * the tape, then we must have found a file that was created
489 * while the dump was in progress. Since we have no contents
490 * for it, we discard the name knowing that it will be on the
491 * next incremental tape.
492 */
493 case 0:
494 if (compare_ignore_not_found) break;
495 fprintf(stderr, "%s: (inode %u) not found on tape\n",
496 name, ino);
497 do_compare_error;
498 break;
499
500 /*
501 * If any of these arise, something is grievously wrong with
502 * the current state of the symbol table.
503 */
504 case INOFND|NAMEFND|MODECHG:
505 case NAMEFND|MODECHG:
506 case INOFND|MODECHG:
507 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
508 name);
509 break;
510
511 /*
512 * These states "cannot" arise for any state of the symbol table.
513 */
514 case ONTAPE|MODECHG:
515 case MODECHG:
516 default:
517 panic("[%s] %s: impossible state\n", keyval(key), name);
518 break;
519 }
520 return (descend);
521 }
522
523 /*
524 * Calculate the active flags in a key.
525 */
526 static char *
527 keyval(int key)
528 {
529 static char keybuf[32];
530
531 (void) strcpy(keybuf, "|NIL");
532 keybuf[0] = '\0';
533 if (key & ONTAPE)
534 (void) strcat(keybuf, "|ONTAPE");
535 if (key & INOFND)
536 (void) strcat(keybuf, "|INOFND");
537 if (key & NAMEFND)
538 (void) strcat(keybuf, "|NAMEFND");
539 if (key & MODECHG)
540 (void) strcat(keybuf, "|MODECHG");
541 return (&keybuf[1]);
542 }
543
544 /*
545 * Find unreferenced link names.
546 *
547 * This also takes care of directories which were missed by removeoldleaves(),
548 * because their inode has been reused, but excluded from the dump.
549 */
550 void
551 findunreflinks(void)
552 {
553 struct entry *ep, *np;
554 dump_ino_t i;
555 int j;
556
557 Vmsg("Find unreferenced names.\n");
558 for (i = ROOTINO; i < maxino; i++) {
559 ep = lookupino(i);
560 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpinomap) == 0)
561 continue;
562 if (ep->e_entries == NULL)
563 continue;
564 for (j = 0; j < dirhash_size; j++) {
565 for (np = ep->e_entries[j]; np != NULL; np = np->e_sibling) {
566 if ((np->e_flags & ~TMPNAME) == 0) {
567 Dmsg(DEFAULT_DEBUG,
568 "%s: remove unreferenced name\n",
569 myname(np));
570 if (np->e_type == LEAF) {
571 removeleaf(np);
572 freeentry(np);
573 } else if (np->e_ino != 0) {
574 np->e_flags |= TMPNAME;
575 deleteino(np->e_ino);
576 np->e_next = removelist;
577 removelist = np;
578 }
579 }
580 }
581 }
582 }
583 /*
584 * Any leaves remaining in removed directories is unreferenced.
585 */
586 for (ep = removelist; ep != NULL; ep = ep->e_next) {
587 if (ep->e_entries == NULL)
588 continue;
589 for (j = 0; j < dirhash_size; j++) {
590 for (np = ep->e_entries[j]; np != NULL; np = np->e_sibling) {
591 if (np->e_type == LEAF) {
592 if (np->e_flags != 0)
593 badentry(np, "unreferenced with flags");
594 Dmsg(DEFAULT_DEBUG,
595 "%s: remove unreferenced name\n",
596 myname(np));
597 removeleaf(np);
598 freeentry(np);
599 } else {
600 if ((np->e_flags & ~TMPNAME) != 0)
601 badentry(np, "unreferenced with flags");
602
603 if (np->e_flags == 0) {
604 Dmsg(DEFAULT_DEBUG,
605 "%s: remove unreferenced name\n",
606 myname(np));
607 np->e_next = ep->e_next;
608 ep->e_next = np;
609 }
610 }
611 }
612 }
613 }
614 }
615
616 /*
617 * Remove old nodes (directories).
618 * Note that this routine runs in O(N*D) where:
619 * N is the number of directory entries to be removed.
620 * D is the maximum depth of the tree.
621 * If N == D this can be quite slow. If the list were
622 * topologically sorted, the deletion could be done in
623 * time O(N).
624 */
625 void
626 removeoldnodes(void)
627 {
628 struct entry *ep, **prev;
629 long change;
630
631 Vmsg("Remove old nodes (directories).\n");
632 do {
633 change = 0;
634 prev = &removelist;
635 for (ep = removelist; ep != NULL; ep = *prev) {
636 int docont = 0;
637 if (ep->e_entries != NULL) {
638 int i;
639 for (i = 0; i < dirhash_size; i++) {
640 if (ep->e_entries[i] != NULL) {
641 prev = &ep->e_next;
642 docont = 1;
643 break;
644 }
645 }
646 }
647 if (docont)
648 continue;
649 *prev = ep->e_next;
650 removenode(ep);
651 freeentry(ep);
652 change++;
653 }
654 } while (change);
655 for (ep = removelist; ep != NULL; ep = ep->e_next)
656 badentry(ep, "cannot remove, non-empty");
657 }
658
659 /* Compare the file specified in `ep' (which is on tape) to the */
660 /* current copy of this file on disk. If do_compare is 0, then just */
661 /* make our caller think we did it--this is used to handle hard links */
662 /* to files and devices. */
663 static void
664 compare_entry(struct entry *ep, int do_compare)
665 {
666 if ((ep->e_flags & (NEW|EXTRACT)) == 0) {
667 badentry(ep, "unexpected file on tape");
668 do_compare_error;
669 }
670 if (do_compare) {
671 (void) comparefile(myname(ep));
672 skipxattr();
673 }
674 ep->e_flags &= ~(NEW|EXTRACT);
675 }
676
677 /*
678 * This is the routine used to compare files for the 'C' command.
679 */
680 void
681 compareleaves(void)
682 {
683 struct entry *ep;
684 dump_ino_t first;
685 long curvol;
686
687 first = lowerbnd(ROOTINO);
688 curvol = volno;
689 while (curfile.ino < maxino) {
690 first = lowerbnd(first);
691 /*
692 * If the next available file is not the one which we
693 * expect then we have missed one or more files. Since
694 * we do not request files that were not on the tape,
695 * the lost files must have been due to a tape read error,
696 * or a file that was removed while the dump was in progress.
697 */
698 while (first < curfile.ino) {
699 ep = lookupino(first);
700 if (ep == NULL)
701 panic("%d: bad first\n", first);
702 fprintf(stderr, "%s: not found on tape\n", myname(ep));
703 do_compare_error;
704 ep->e_flags &= ~(NEW|EXTRACT);
705 first = lowerbnd(first);
706 }
707 /*
708 * If we find files on the tape that have no corresponding
709 * directory entries, then we must have found a file that
710 * was created while the dump was in progress. Since we have
711 * no name for it, we discard it knowing that it will be
712 * on the next incremental tape.
713 */
714 if (first != curfile.ino) {
715 fprintf(stderr, "expected next file %u, got %u\n",
716 first, curfile.ino);
717 skipfile();
718 goto next;
719 }
720 ep = lookupino(curfile.ino);
721 if (ep == NULL) {
722 panic("unknown file on tape\n");
723 do_compare_error;
724 }
725 compare_entry(ep, 1);
726 for (ep = ep->e_links; ep != NULL; ep = ep->e_links) {
727 compare_entry(ep, 0);
728 }
729
730 /*
731 * We checkpoint the restore after every tape reel, so
732 * as to simplify the amount of work re quired by the
733 * 'R' command.
734 */
735 next:
736 if (curvol != volno) {
737 skipmaps();
738 curvol = volno;
739 }
740 }
741 /*
742 * If we encounter the end of the tape and the next available
743 * file is not the one which we expect then we have missed one
744 * or more files. Since we do not request files that were not
745 * on the tape, the lost files must have been due to a tape
746 * read error, or a file that was removed while the dump was
747 * in progress.
748 */
749 first = lowerbnd(first);
750 while (first < curfile.ino) {
751 ep = lookupino(first);
752 if (ep == NULL)
753 panic("%d: bad first\n", first);
754 fprintf(stderr, "%s: (inode %u) not found on tape\n",
755 myname(ep), first);
756 do_compare_error;
757 ep->e_flags &= ~(NEW|EXTRACT);
758 first = lowerbnd(first);
759 }
760 }
761
762 /*
763 * This is the routine used to extract files for the 'r' command.
764 * Extract new leaves.
765 */
766 void
767 createleaves(const char *symtabfile)
768 {
769 struct entry *ep;
770 dump_ino_t first;
771 long curvol;
772 int doremove;
773
774 if (command == 'R') {
775 Vmsg("Continue extraction of new leaves\n");
776 } else {
777 Vmsg("Extract new leaves.\n");
778 dumpsymtable(symtabfile, volno);
779 }
780 first = lowerbnd(ROOTINO);
781 curvol = volno;
782 while (curfile.ino < maxino) {
783 first = lowerbnd(first);
784 /*
785 * If the next available file is not the one which we
786 * expect then we have missed one or more files. Since
787 * we do not request files that were not on the tape,
788 * the lost files must have been due to a tape read error,
789 * or a file that was removed while the dump was in progress.
790 */
791 while (first < curfile.ino) {
792 ep = lookupino(first);
793 if (ep == NULL)
794 panic("%d: bad first\n", first);
795 fprintf(stderr, "%s: (inode %u) not found on tape\n",
796 myname(ep), first);
797 ep->e_flags &= ~(NEW|EXTRACT);
798 first = lowerbnd(first);
799 }
800 /*
801 * If we find files on the tape that have no corresponding
802 * directory entries, then we must have found a file that
803 * was created while the dump was in progress. Since we have
804 * no name for it, we discard it knowing that it will be
805 * on the next incremental tape.
806 */
807 if (first != curfile.ino) {
808 fprintf(stderr, "expected next file %u, got %u\n",
809 first, curfile.ino);
810 skipfile();
811 goto next;
812 }
813 ep = lookupino(curfile.ino);
814 if (ep == NULL)
815 panic("unknown file on tape\n");
816 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
817 badentry(ep, "unexpected file on tape");
818 /*
819 * If the file is to be extracted, then the old file must
820 * be removed since its type may change from one leaf type
821 * to another (e.g. "file" to "character special").
822 */
823 if ((ep->e_flags & EXTRACT) != 0)
824 doremove = 1;
825 else
826 doremove = 0;
827 (void) extractfile(ep, doremove);
828 skipxattr();
829 ep->e_flags &= ~(NEW|EXTRACT);
830
831 /*
832 * We checkpoint the restore after every tape reel, so
833 * as to simplify the amount of work required by the
834 * 'R' command.
835 */
836 next:
837 if (curvol != volno) {
838 dumpsymtable(symtabfile, volno);
839 skipmaps();
840 curvol = volno;
841 }
842 }
843 /*
844 * If we encounter the end of the tape and the next available
845 * file is not the one which we expect then we have missed one
846 * or more files. Since we do not request files that were not
847 * on the tape, the lost files must have been due to a tape
848 * read error, or a file that was removed while the dump was
849 * in progress.
850 */
851 first = lowerbnd(first);
852 while (first < curfile.ino) {
853 ep = lookupino(first);
854 if (ep == NULL)
855 panic("%d: bad first\n", first);
856 fprintf(stderr, "%s: (inode %u) not found on tape\n",
857 myname(ep), first);
858 do_compare_error;
859 ep->e_flags &= ~(NEW|EXTRACT);
860 first = lowerbnd(first);
861 }
862 }
863
864 /*
865 * This is the routine used to extract files for the 'x' and 'i' commands.
866 * Efficiently extract a subset of the files on a tape.
867 */
868 void
869 createfiles(void)
870 {
871 dump_ino_t first, next, last;
872 struct entry *ep;
873 long curvol;
874 #ifdef USE_QFA
875 long tnum;
876 long long tpos;
877 off_t curtpos = 0;
878 long tmpcnt;
879 time_t tistart, tiend, titaken;
880 int volChg;
881 #endif
882
883 Vmsg("Extract requested files\n");
884 curfile.action = SKIP;
885 #ifdef USE_QFA
886 if (!tapeposflag) {
887 #endif
888 if (volinfo[1] == ROOTINO)
889 curfile.ino = 0;
890 else
891 getvol(1);
892 #ifdef USE_QFA
893 }
894 #endif
895 skipmaps();
896 skipdirs();
897 first = lowerbnd(ROOTINO);
898 last = upperbnd(maxino - 1);
899 for (;;) {
900 tmpcnt = 1;
901 first = lowerbnd(first);
902 last = upperbnd(last);
903 /*
904 * Check to see if any files remain to be extracted
905 */
906 if (first > last)
907 return;
908 /*
909 * Reject any volumes with inodes greater
910 * than the last one needed
911 */
912 while (curfile.ino > last) {
913 curfile.action = SKIP;
914 if (!pipein)
915 getvol(0);
916 if (curfile.ino == maxino) {
917 next = lowerbnd(first);
918 while (next < curfile.ino) {
919 ep = lookupino(next);
920 if (ep == NULL)
921 panic("corrupted symbol table\n");
922 fprintf(stderr, "%s: (inode %u) not found on tape\n",
923 myname(ep), next);
924 ep->e_flags &= ~NEW;
925 next = lowerbnd(next);
926 }
927 return;
928 }
929 skipmaps();
930 skipdirs();
931 }
932 /*
933 * Decide on the next inode needed.
934 * Skip across the inodes until it is found
935 * or an out of order volume change is encountered
936 */
937 next = lowerbnd(curfile.ino);
938 #ifdef USE_QFA
939 tistart = time(NULL);
940 if (tapeposflag) {
941 /* get tape position for inode */
942 (void)Inode2Tapepos(next, &tnum, &tpos, 0);
943 if (tpos != 0) {
944 if (tnum != volno) {
945 (void)RequestVol(tnum);
946 volChg = 1;
947 } else {
948 volChg = 0;
949 }
950 if (getpos_tape(&curtpos) == 0) {
951 /* curtpos +1000 ???, some drives
952 * might be too slow */
953 if (((tpos > (curtpos + 1000)) && (volChg == 0)) || ((tpos != curtpos) && (volChg == 1))) {
954 volChg = 0;
955 Dmsg(DEBUG_QFA, "positioning tape %ld from %jd to %lld for inode %u ...\n", volno, (intmax_t)curtpos, tpos, next);
956 if (setpos_tape(tpos) == 0) {
957 if (getpos_tape(&curtpos) == 0) {
958 Dmsg(DEBUG_QFA, "before resnyc at tape position %jd (%ud, %ud, %s)\n", (intmax_t)curtpos, next, curfile.ino, curfile.name);
959 }
960 ReReadInodeFromTape(next);
961 if (getpos_tape(&curtpos) == 0) {
962 Dmsg(DEBUG_QFA, "after resnyc at tape position %jd (%ud, %ud, %s)\n", (intmax_t)curtpos, next, curfile.ino, curfile.name);
963 }
964 }
965 } else {
966 Dmsg(DEBUG_QFA, "already at tape %ld position %lld for inode %u ...\n", volno, tpos, next);
967 }
968 }
969 }
970 }
971 else
972 #endif /* USA_QFA */
973 if (volinfo[1] == ROOTINO) {
974 int i, goodvol = 1;
975
976 for (i = 1; i < (int)TP_NINOS && volinfo[i] != 0; ++i)
977 if (volinfo[i] < next)
978 goodvol = i;
979
980 if (goodvol != volno)
981 RequestVol(goodvol);
982 }
983
984 do {
985 curvol = volno;
986 while (next > curfile.ino && volno == curvol) {
987 ++tmpcnt;
988 skipfile();
989 }
990 skipmaps();
991 skipdirs();
992 } while (volno == curvol + 1);
993 #ifdef USE_QFA
994 tiend = time(NULL);
995 titaken = tiend - tistart;
996 if (titaken / 60 > 0)
997 Dmsg(DEBUG_QFA, "%ld reads took %jd:%02jd:%02jd\n",
998 tmpcnt, (intmax_t)titaken / 3600,
999 ((intmax_t)titaken % 3600) / 60, (intmax_t)titaken % 60);
1000 #endif /* USE_QFA */
1001
1002 /*
1003 * If volume change out of order occurred the
1004 * current state must be recalculated
1005 */
1006 if (volno != curvol)
1007 continue;
1008 /*
1009 * If the current inode is greater than the one we were
1010 * looking for then we missed the one we were looking for.
1011 * Since we only attempt to extract files listed in the
1012 * dump map, the lost files must have been due to a tape
1013 * read error, or a file that was removed while the dump
1014 * was in progress. Thus we report all requested files
1015 * between the one we were looking for, and the one we
1016 * found as missing, and delete their request flags.
1017 */
1018 while (next < curfile.ino) {
1019 ep = lookupino(next);
1020 if (ep == NULL)
1021 panic("corrupted symbol table\n");
1022 #ifdef USE_QFA
1023 if (!createtapeposflag)
1024 #endif
1025 fprintf(stderr, "%s: (inode %u) not found on tape\n",
1026 myname(ep), next);
1027 ep->e_flags &= ~NEW;
1028 next = lowerbnd(next);
1029 }
1030 /*
1031 * The current inode is the one that we are looking for,
1032 * so extract it per its requested name.
1033 */
1034 if (next == curfile.ino && next <= last) {
1035 ep = lookupino(next);
1036 if (ep == NULL)
1037 panic("corrupted symbol table\n");
1038 #ifdef USE_QFA
1039 if (createtapeposflag) {
1040 Dmsg(DEBUG_QFA, "inode %ud at tapepos %ld\n", curfile.ino, curtapepos);
1041 sprintf(gTps, "%u\t%ld\t%jd\n", curfile.ino, volno, (intmax_t)curtapepos);
1042 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
1043 warn("error writing tapepos file.\n");
1044 skipfile();
1045 } else {
1046 #endif /* USE_QFA */
1047 (void) extractfile(ep, 0);
1048 skipxattr();
1049
1050 #ifdef USE_QFA
1051 }
1052 #endif /* USE_QFA */
1053 ep->e_flags &= ~NEW;
1054 if (volno != curvol)
1055 skipmaps();
1056 }
1057 }
1058 }
1059
1060 /*
1061 * Add links.
1062 */
1063 void
1064 createlinks(void)
1065 {
1066 struct entry *np, *ep;
1067 dump_ino_t i;
1068 char name[BUFSIZ];
1069
1070 if ((ep = lookupino(WINO))) {
1071 Vmsg("Add whiteouts\n");
1072 for ( ; ep != NULL; ep = ep->e_links) {
1073 if ((ep->e_flags & NEW) == 0)
1074 continue;
1075 (void)fprintf(stderr, "BUG! Should call addwhiteout\n");
1076 ep->e_flags &= ~NEW;
1077 }
1078 }
1079 Vmsg("Add links\n");
1080 for (i = ROOTINO; i < maxino; i++) {
1081 ep = lookupino(i);
1082 if (ep == NULL)
1083 continue;
1084 for (np = ep->e_links; np != NULL; np = np->e_links) {
1085 if ((np->e_flags & NEW) == 0)
1086 continue;
1087 (void) strcpy(name, myname(ep));
1088 if (ep->e_type == NODE) {
1089 (void) linkit(name, myname(np), SYMLINK);
1090 } else {
1091 (void) linkit(name, myname(np), HARDLINK);
1092 }
1093 np->e_flags &= ~NEW;
1094 }
1095 }
1096 }
1097
1098 /*
1099 * Check the symbol table.
1100 * We do this to insure that all the requested work was done, and
1101 * that no temporary names remain.
1102 */
1103 void
1104 checkrestore(void)
1105 {
1106 struct entry *ep;
1107 dump_ino_t i;
1108
1109 Vmsg("Check the symbol table.\n");
1110 for (i = WINO; i < maxino; i++) {
1111 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
1112 ep->e_flags &= ~KEEP;
1113 if (ep->e_type == NODE)
1114 ep->e_flags &= ~(NEW|EXISTED);
1115 if (ep->e_flags /* != NULL */)
1116 badentry(ep, "incomplete operations");
1117 }
1118 }
1119 }
1120
1121 /*
1122 * Compare with the directory structure on the tape
1123 * A paranoid check that things are as they should be.
1124 */
1125 long
1126 verifyfile(const char *name, dump_ino_t ino, int type)
1127 {
1128 struct entry *np, *ep;
1129 long descend = GOOD;
1130
1131 ep = lookupname(name);
1132 if (ep == NULL) {
1133 fprintf(stderr, "Warning: missing name %s\n", name);
1134 return (FAIL);
1135 }
1136 np = lookupino(ino);
1137 if (np != ep)
1138 descend = FAIL;
1139 for ( ; np != NULL; np = np->e_links)
1140 if (np == ep)
1141 break;
1142 if (np == NULL)
1143 panic("missing inumber %d\n", ino);
1144 if (ep->e_type == LEAF && type != LEAF)
1145 badentry(ep, "type should be LEAF");
1146 return (descend);
1147 }