int main(int argc, char *argv[]) { int ch; ino_t ino; char *inputdev; char *symtbl = "./restoresymtable"; char *p, name[MAXPATHLEN]; /* Temp files should *not* be readable. We set permissions later. */ (void) umask(077); if (argc < 2) usage(); (void)setlocale(LC_ALL, ""); inputdev = NULL; obsolete(&argc, &argv); while ((ch = getopt(argc, argv, "b:dDf:himNP:Rrs:tuvxy")) != -1) switch(ch) { case 'b': /* Change default tape blocksize. */ bflag = 1; ntrec = strtol(optarg, &p, 10); if (*p) errx(1, "illegal blocksize -- %s", optarg); if (ntrec <= 0) errx(1, "block size must be greater than 0"); break; case 'd': dflag = 1; break; case 'D': Dflag = 1; break; case 'f': if (pipecmd) errx(1, "-P and -f options are mutually exclusive"); inputdev = optarg; break; case 'P': if (!pipecmd && inputdev) errx(1, "-P and -f options are mutually exclusive"); inputdev = optarg; pipecmd = 1; break; case 'h': hflag = 0; break; case 'i': case 'R': case 'r': case 't': case 'x': if (command != '\0') errx(1, "%c and %c options are mutually exclusive", ch, command); command = ch; break; case 'm': mflag = 0; break; case 'N': Nflag = 1; break; case 's': /* Dumpnum (skip to) for multifile dump tapes. */ dumpnum = strtol(optarg, &p, 10); if (*p) errx(1, "illegal dump number -- %s", optarg); if (dumpnum <= 0) errx(1, "dump number must be greater than 0"); break; case 'u': uflag = 1; break; case 'v': vflag = 1; break; case 'y': yflag = 1; break; default: usage(); } argc -= optind; argv += optind; if (command == '\0') errx(1, "none of i, R, r, t or x options specified"); if (signal(SIGINT, onintr) == SIG_IGN) (void) signal(SIGINT, SIG_IGN); if (signal(SIGTERM, onintr) == SIG_IGN) (void) signal(SIGTERM, SIG_IGN); setlinebuf(stderr); if (inputdev == NULL && (inputdev = getenv("TAPE")) == NULL) inputdev = _PATH_DEFTAPE; setinput(inputdev, pipecmd); if (argc == 0) { argc = 1; *--argv = "."; } switch (command) { /* * Interactive mode. */ case 'i': setup(); extractdirs(1); initsymtable(NULL); runcmdshell(); break; /* * Incremental restoration of a file system. */ case 'r': setup(); if (dumptime > 0) { /* * This is an incremental dump tape. */ vprintf(stdout, "Begin incremental restore\n"); initsymtable(symtbl); extractdirs(1); removeoldleaves(); vprintf(stdout, "Calculate node updates.\n"); treescan(".", ROOTINO, nodeupdates); findunreflinks(); removeoldnodes(); } else { /* * This is a level zero dump tape. */ vprintf(stdout, "Begin level 0 restore\n"); initsymtable((char *)0); extractdirs(1); vprintf(stdout, "Calculate extraction list.\n"); treescan(".", ROOTINO, nodeupdates); } createleaves(symtbl); createlinks(); setdirmodes(FORCE); checkrestore(); if (dflag) { vprintf(stdout, "Verify the directory structure\n"); treescan(".", ROOTINO, verifyfile); } dumpsymtable(symtbl, (long)1); break; /* * Resume an incremental file system restoration. */ case 'R': initsymtable(symtbl); skipmaps(); skipdirs(); createleaves(symtbl); createlinks(); setdirmodes(FORCE); checkrestore(); dumpsymtable(symtbl, (long)1); break; /* * List contents of tape. */ case 't': setup(); extractdirs(0); initsymtable((char *)0); while (argc--) { canon(*argv++, name, sizeof(name)); ino = dirlookup(name); if (ino == 0) continue; treescan(name, ino, listfile); } break; /* * Batch extraction of tape contents. */ case 'x': setup(); extractdirs(1); initsymtable((char *)0); while (argc--) { canon(*argv++, name, sizeof(name)); ino = dirlookup(name); if (ino == 0) continue; if (mflag) pathcheck(name); treescan(name, ino, addfile); } createfiles(); createlinks(); setdirmodes(0); if (dflag) checkrestore(); break; } done(0); /* NOTREACHED */ }
/* * This is the routine used to extract files for the 'r' command. * Extract new leaves. */ void createleaves(char *symtabfile) { struct entry *ep; ino_t first; long curvol; if (command == 'R') { vprintf(stdout, "Continue extraction of new leaves\n"); } else { vprintf(stdout, "Extract new leaves.\n"); dumpsymtable(symtabfile, volno); } first = lowerbnd(ROOTINO); curvol = volno; while (curfile.ino < maxino) { first = lowerbnd(first); /* * If the next available file is not the one which we * expect then we have missed one or more files. Since * we do not request files that were not on the tape, * the lost files must have been due to a tape read error, * or a file that was removed while the dump was in progress. */ while (first < curfile.ino) { ep = lookupino(first); if (ep == NULL) panic("%ju: bad first\n", (uintmax_t)first); fprintf(stderr, "%s: not found on tape\n", myname(ep)); ep->e_flags &= ~(NEW|EXTRACT); first = lowerbnd(first); } /* * If we find files on the tape that have no corresponding * directory entries, then we must have found a file that * was created while the dump was in progress. Since we have * no name for it, we discard it knowing that it will be * on the next incremental tape. */ if (first != curfile.ino) { fprintf(stderr, "expected next file %ju, got %ju\n", (uintmax_t)first, (uintmax_t)curfile.ino); skipfile(); goto next; } ep = lookupino(curfile.ino); if (ep == NULL) panic("unknown file on tape\n"); if ((ep->e_flags & (NEW|EXTRACT)) == 0) badentry(ep, "unexpected file on tape"); /* * If the file is to be extracted, then the old file must * be removed since its type may change from one leaf type * to another (e.g. "file" to "character special"). */ if ((ep->e_flags & EXTRACT) != 0) { removeleaf(ep); ep->e_flags &= ~REMOVED; } (void) extractfile(myname(ep)); ep->e_flags &= ~(NEW|EXTRACT); /* * We checkpoint the restore after every tape reel, so * as to simplify the amount of work required by the * 'R' command. */ next: if (curvol != volno) { dumpsymtable(symtabfile, volno); skipmaps(); curvol = volno; } } }
/* * This is the routine used to extract files for the 'x' and 'i' commands. * Efficiently extract a subset of the files on a tape. */ void createfiles(void) { ino_t first, next, last; struct entry *ep; long curvol; vprintf(stdout, "Extract requested files\n"); curfile.action = SKIP; getvol((long)1); skipmaps(); skipdirs(); first = lowerbnd(ROOTINO); last = upperbnd(maxino - 1); for (;;) { curvol = volno; first = lowerbnd(first); last = upperbnd(last); /* * Check to see if any files remain to be extracted */ if (first > last) return; if (Dflag) { if (curfile.ino == maxino) return; if((ep = lookupino(curfile.ino)) != NULL && (ep->e_flags & (NEW|EXTRACT))) { goto justgetit; } else { skipfile(); continue; } } /* * Reject any volumes with inodes greater than the last * one needed, so that we can quickly skip backwards to * a volume containing useful inodes. We can't do this * if there are no further volumes available (curfile.ino * >= maxino) or if we are already at the first tape. */ if (curfile.ino > last && curfile.ino < maxino && volno > 1) { curfile.action = SKIP; getvol((long)0); skipmaps(); skipdirs(); continue; } /* * Decide on the next inode needed. * Skip across the inodes until it is found * or a volume change is encountered */ if (curfile.ino < maxino) { next = lowerbnd(curfile.ino); while (next > curfile.ino && volno == curvol) skipfile(); if (volno != curvol) { skipmaps(); skipdirs(); continue; } } else { /* * No further volumes or inodes available. Set * `next' to the first inode, so that a warning * is emitted below for each missing file. */ next = first; } /* * If the current inode is greater than the one we were * looking for then we missed the one we were looking for. * Since we only attempt to extract files listed in the * dump map, the lost files must have been due to a tape * read error, or a file that was removed while the dump * was in progress. Thus we report all requested files * between the one we were looking for, and the one we * found as missing, and delete their request flags. */ while (next < curfile.ino) { ep = lookupino(next); if (ep == NULL) panic("corrupted symbol table\n"); fprintf(stderr, "%s: not found on tape\n", myname(ep)); ep->e_flags &= ~NEW; next = lowerbnd(next); } /* * The current inode is the one that we are looking for, * so extract it per its requested name. */ if (next == curfile.ino && next <= last) { ep = lookupino(next); if (ep == NULL) panic("corrupted symbol table\n"); justgetit: (void) extractfile(myname(ep)); ep->e_flags &= ~NEW; if (volno != curvol) skipmaps(); } } }
/* * This is the routine used to extract files for the 'x' and 'i' commands. * Efficiently extract a subset of the files on a tape. */ void createfiles(void) { ino_t first, next, last; struct entry *ep; long curvol; Vprintf(stdout, "Extract requested files\n"); curfile.action = SKIP; getvol((long)1); skipmaps(); skipdirs(); first = lowerbnd(ROOTINO); last = upperbnd(maxino - 1); for (;;) { first = lowerbnd(first); last = upperbnd(last); /* * Check to see if any files remain to be extracted */ if (first > last) return; /* * Reject any volumes with inodes greater * than the last one needed */ while (curfile.ino > last) { curfile.action = SKIP; getvol((long)0); skipmaps(); skipdirs(); } /* * Decide on the next inode needed. * Skip across the inodes until it is found * or an out of order volume change is encountered */ next = lowerbnd(curfile.ino); do { curvol = volno; while (next > curfile.ino && volno == curvol) skipfile(); skipmaps(); skipdirs(); } while (volno == curvol + 1); /* * If volume change out of order occurred the * current state must be recalculated */ if (volno != curvol) continue; /* * If the current inode is greater than the one we were * looking for then we missed the one we were looking for. * Since we only attempt to extract files listed in the * dump map, the lost files must have been due to a tape * read error, or a file that was removed while the dump * was in progress. Thus we report all requested files * between the one we were looking for, and the one we * found as missing, and delete their request flags. */ while (next < curfile.ino) { ep = lookupino(next); if (ep == NULL) panic("corrupted symbol table\n"); fprintf(stderr, "%s: not found on tape\n", myname(ep)); ep->e_flags &= ~NEW; next = lowerbnd(next); } /* * The current inode is the one that we are looking for, * so extract it per its requested name. */ if (next == curfile.ino && next <= last) { ep = lookupino(next); if (ep == NULL) panic("corrupted symbol table\n"); (void)extractfile(myname(ep)); ep->e_flags &= ~NEW; if (volno != curvol) skipmaps(); } } }