|
|
1.1.1.2 ! root 1: /* ! 2: * Copyright (c) 1983 Regents of the University of California. ! 3: * All rights reserved. The Berkeley software License Agreement ! 4: * specifies the terms and conditions for redistribution. ! 5: */ 1.1 root 6: 1.1.1.2 ! root 7: #ifndef lint ! 8: static char sccsid[] = "@(#)dirs.c 5.2 (Berkeley) 6/18/85"; ! 9: #endif not lint 1.1 root 10: 11: #include "restore.h" 12: #include <dumprestor.h> 13: #include <sys/file.h> 14: 15: /* 16: * Symbol table of directories read from tape. 17: */ 18: #define HASHSIZE 1000 19: #define INOHASH(val) (val % HASHSIZE) 20: struct inotab { 21: struct inotab *t_next; 22: ino_t t_ino; 23: daddr_t t_seekpt; 24: long t_size; 25: }; 26: static struct inotab *inotab[HASHSIZE]; 27: extern struct inotab *inotablookup(); 28: extern struct inotab *allocinotab(); 29: 30: /* 31: * Information retained about directories. 32: */ 33: struct modeinfo { 34: ino_t ino; 35: time_t timep[2]; 36: short mode; 37: short uid; 38: short gid; 39: }; 40: 41: /* 42: * Global variables for this file. 43: */ 44: static daddr_t seekpt; 45: static FILE *df, *mf; 46: static DIR *dirp; 47: static char dirfile[32] = "#"; /* No file */ 48: static char modefile[32] = "#"; /* No file */ 49: extern ino_t search(); 50: struct direct *rst_readdir(); 51: extern void rst_seekdir(); 52: 53: /* 54: * Format of old style directories. 55: */ 56: #define ODIRSIZ 14 57: struct odirect { 58: u_short d_ino; 59: char d_name[ODIRSIZ]; 60: }; 61: 62: /* 63: * Extract directory contents, building up a directory structure 64: * on disk for extraction by name. 65: * If genmode is requested, save mode, owner, and times for all 66: * directories on the tape. 67: */ 68: extractdirs(genmode) 69: int genmode; 70: { 71: register int i; 72: register struct dinode *ip; 73: struct inotab *itp; 74: struct direct nulldir; 75: int putdir(), null(); 76: 77: vprintf(stdout, "Extract directories from tape\n"); 78: (void) sprintf(dirfile, "/tmp/rstdir%d", dumpdate); 79: df = fopen(dirfile, "w"); 80: if (df == 0) { 81: fprintf(stderr, 1.1.1.2 ! root 82: "restore: %s - cannot create directory temporary\n", 1.1 root 83: dirfile); 84: perror("fopen"); 85: done(1); 86: } 87: if (genmode != 0) { 88: (void) sprintf(modefile, "/tmp/rstmode%d", dumpdate); 89: mf = fopen(modefile, "w"); 90: if (mf == 0) { 91: fprintf(stderr, 1.1.1.2 ! root 92: "restore: %s - cannot create modefile \n", 1.1 root 93: modefile); 94: perror("fopen"); 95: done(1); 96: } 97: } 98: nulldir.d_ino = 0; 99: nulldir.d_namlen = 1; 100: (void) strcpy(nulldir.d_name, "/"); 101: nulldir.d_reclen = DIRSIZ(&nulldir); 102: for (;;) { 103: curfile.name = "<directory file - name unknown>"; 104: curfile.action = USING; 105: ip = curfile.dip; 1.1.1.2 ! root 106: if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) { 1.1 root 107: (void) fclose(df); 108: dirp = opendir(dirfile); 109: if (dirp == NULL) 110: perror("opendir"); 111: if (mf != NULL) 112: (void) fclose(mf); 113: i = dirlookup("."); 114: if (i == 0) 115: panic("Root directory is not on tape\n"); 116: return; 117: } 118: itp = allocinotab(curfile.ino, ip, seekpt); 119: getfile(putdir, null); 120: putent(&nulldir); 121: flushent(); 122: itp->t_size = seekpt - itp->t_seekpt; 123: } 124: } 125: 126: /* 127: * skip over all the directories on the tape 128: */ 129: skipdirs() 130: { 131: 132: while ((curfile.dip->di_mode & IFMT) == IFDIR) { 133: skipfile(); 134: } 135: } 136: 137: /* 138: * Recursively find names and inumbers of all files in subtree 139: * pname and pass them off to be processed. 140: */ 141: treescan(pname, ino, todo) 142: char *pname; 143: ino_t ino; 144: long (*todo)(); 145: { 146: register struct inotab *itp; 147: register struct direct *dp; 148: register struct entry *np; 149: int namelen; 150: daddr_t bpt; 151: char locname[MAXPATHLEN + 1]; 152: 153: itp = inotablookup(ino); 154: if (itp == NULL) { 155: /* 156: * Pname is name of a simple file or an unchanged directory. 157: */ 158: (void) (*todo)(pname, ino, LEAF); 159: return; 160: } 161: /* 162: * Pname is a dumped directory name. 163: */ 164: if ((*todo)(pname, ino, NODE) == FAIL) 165: return; 166: /* 167: * begin search through the directory 168: * skipping over "." and ".." 169: */ 170: (void) strncpy(locname, pname, MAXPATHLEN); 171: (void) strncat(locname, "/", MAXPATHLEN); 172: namelen = strlen(locname); 173: rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 1.1.1.2 ! root 174: dp = rst_readdir(dirp); /* "." */ ! 175: namelen = strlen(locname); ! 176: rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); ! 177: dp = rst_readdir(dirp); /* "." or ".." */ ! 178: if (dp != NULL && ! 179: (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)) { ! 180: dp = rst_readdir(dirp); /* "." or ".." */ ! 181: } else { 1.1 root 182: np = lookupino(ino); 183: if (np == NULL) 184: panic("corrupted symbol table\n"); 1.1.1.2 ! root 185: fprintf(stderr, ". missing from directory %s\n", myname(np)); 1.1 root 186: } 1.1.1.2 ! root 187: if (dp != NULL && ! 188: (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)) { 1.1 root 189: dp = rst_readdir(dirp); /* first real entry */ 190: } else { 191: np = lookupino(ino); 1.1.1.2 ! root 192: if (np == NULL) ! 193: panic("corrupted symbol table\n"); ! 194: fprintf(stderr, ". and .. are not the first two entries in directory %s\n", myname(np)); ! 195: dp = (struct direct *)NULL; ! 196: } 1.1 root 197: bpt = telldir(dirp); 198: /* 199: * a zero inode signals end of directory 200: */ 201: while (dp != NULL && dp->d_ino != 0) { 202: locname[namelen] = '\0'; 203: if (namelen + dp->d_namlen >= MAXPATHLEN) { 204: fprintf(stderr, "%s%s: name exceeds %d char\n", 205: locname, dp->d_name, MAXPATHLEN); 206: } else { 207: (void) strncat(locname, dp->d_name, (int)dp->d_namlen); 208: treescan(locname, dp->d_ino, todo); 209: rst_seekdir(dirp, bpt, itp->t_seekpt); 210: } 211: dp = rst_readdir(dirp); 212: bpt = telldir(dirp); 213: } 214: if (dp == NULL) 215: fprintf(stderr, "corrupted directory: %s.\n", locname); 216: } 217: 218: /* 219: * Search the directory tree rooted at inode ROOTINO 220: * for the path pointed at by n 221: */ 222: ino_t 223: psearch(n) 224: char *n; 225: { 226: register char *cp, *cp1; 227: ino_t ino; 228: char c; 229: 230: ino = ROOTINO; 231: if (*(cp = n) == '/') 232: cp++; 233: next: 234: cp1 = cp + 1; 235: while (*cp1 != '/' && *cp1) 236: cp1++; 237: c = *cp1; 238: *cp1 = 0; 239: ino = search(ino, cp); 240: if (ino == 0) { 241: *cp1 = c; 242: return(0); 243: } 244: *cp1 = c; 245: if (c == '/') { 246: cp = cp1+1; 247: goto next; 248: } 249: return(ino); 250: } 251: 252: /* 253: * search the directory inode ino 254: * looking for entry cp 255: */ 256: ino_t 257: search(inum, cp) 258: ino_t inum; 259: char *cp; 260: { 261: register struct direct *dp; 262: register struct inotab *itp; 263: int len; 264: 265: itp = inotablookup(inum); 266: if (itp == NULL) 267: return(0); 268: rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 269: len = strlen(cp); 270: do { 271: dp = rst_readdir(dirp); 272: if (dp == NULL || dp->d_ino == 0) 273: return (0); 274: } while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0); 275: return(dp->d_ino); 276: } 277: 278: /* 279: * Put the directory entries in the directory file 280: */ 281: putdir(buf, size) 282: char *buf; 283: int size; 284: { 285: struct direct cvtbuf; 286: register struct odirect *odp; 287: struct odirect *eodp; 288: register struct direct *dp; 289: long loc, i; 290: 291: if (cvtflag) { 292: eodp = (struct odirect *)&buf[size]; 293: for (odp = (struct odirect *)buf; odp < eodp; odp++) 294: if (odp->d_ino != 0) { 295: dcvt(odp, &cvtbuf); 296: putent(&cvtbuf); 297: } 298: } else { 299: for (loc = 0; loc < size; ) { 300: dp = (struct direct *)(buf + loc); 301: i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1)); 302: if (dp->d_reclen == 0 || dp->d_reclen > i) { 303: loc += i; 304: continue; 305: } 306: loc += dp->d_reclen; 307: if (dp->d_ino != 0) { 308: putent(dp); 309: } 310: } 311: } 312: } 313: 314: /* 315: * These variables are "local" to the following two functions. 316: */ 317: char dirbuf[DIRBLKSIZ]; 318: long dirloc = 0; 319: long prev = 0; 320: 321: /* 322: * add a new directory entry to a file. 323: */ 324: putent(dp) 325: struct direct *dp; 326: { 327: dp->d_reclen = DIRSIZ(dp); 328: if (dirloc + dp->d_reclen > DIRBLKSIZ) { 329: ((struct direct *)(dirbuf + prev))->d_reclen = 330: DIRBLKSIZ - prev; 331: (void) fwrite(dirbuf, 1, DIRBLKSIZ, df); 332: dirloc = 0; 333: } 334: bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen); 335: prev = dirloc; 336: dirloc += dp->d_reclen; 337: } 338: 339: /* 340: * flush out a directory that is finished. 341: */ 342: flushent() 343: { 344: 345: ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev; 346: (void) fwrite(dirbuf, (int)dirloc, 1, df); 347: seekpt = ftell(df); 348: dirloc = 0; 349: } 350: 351: dcvt(odp, ndp) 352: register struct odirect *odp; 353: register struct direct *ndp; 354: { 355: 356: bzero((char *)ndp, (long)(sizeof *ndp)); 357: ndp->d_ino = odp->d_ino; 358: (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ); 359: ndp->d_namlen = strlen(ndp->d_name); 360: ndp->d_reclen = DIRSIZ(ndp); 361: } 362: 363: /* 364: * Seek to an entry in a directory. 365: * Only values returned by ``telldir'' should be passed to rst_seekdir. 366: * This routine handles many directories in a single file. 367: * It takes the base of the directory in the file, plus 368: * the desired seek offset into it. 369: */ 370: void 371: rst_seekdir(dirp, loc, base) 372: register DIR *dirp; 373: daddr_t loc, base; 374: { 375: 376: if (loc == telldir(dirp)) 377: return; 378: loc -= base; 379: if (loc < 0) 380: fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc); 381: (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0); 382: dirp->dd_loc = loc & (DIRBLKSIZ - 1); 383: if (dirp->dd_loc != 0) 384: dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ); 385: } 386: 387: /* 388: * get next entry in a directory. 389: */ 390: struct direct * 391: rst_readdir(dirp) 392: register DIR *dirp; 393: { 394: register struct direct *dp; 395: 396: for (;;) { 397: if (dirp->dd_loc == 0) { 398: dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 399: DIRBLKSIZ); 400: if (dirp->dd_size <= 0) { 401: dprintf(stderr, "error reading directory\n"); 402: return NULL; 403: } 404: } 405: if (dirp->dd_loc >= dirp->dd_size) { 406: dirp->dd_loc = 0; 407: continue; 408: } 409: dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc); 410: if (dp->d_reclen == 0 || 411: dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) { 412: dprintf(stderr, "corrupted directory: bad reclen %d\n", 413: dp->d_reclen); 414: return NULL; 415: } 416: dirp->dd_loc += dp->d_reclen; 417: if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0) 418: continue; 419: if (dp->d_ino >= maxino) { 420: dprintf(stderr, "corrupted directory: bad inum %d\n", 421: dp->d_ino); 422: continue; 423: } 424: return (dp); 425: } 426: } 427: 428: /* 1.1.1.2 ! root 429: * Simulate the opening of a directory ! 430: */ ! 431: DIR * ! 432: rst_opendir(name) ! 433: char *name; ! 434: { ! 435: struct inotab *itp; ! 436: ino_t ino; ! 437: ! 438: if ((ino = dirlookup(name)) > 0 && ! 439: (itp = inotablookup(ino)) != NULL) { ! 440: rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); ! 441: return (dirp); ! 442: } ! 443: return (0); ! 444: } ! 445: ! 446: /* 1.1 root 447: * Set the mode, owner, and times for all new or changed directories 448: */ 449: setdirmodes() 450: { 451: FILE *mf; 452: struct modeinfo node; 453: struct entry *ep; 454: char *cp; 455: 456: vprintf(stdout, "Set directory mode, owner, and times.\n"); 1.1.1.2 ! root 457: (void) sprintf(modefile, "/tmp/rstmode%d", dumpdate); 1.1 root 458: mf = fopen(modefile, "r"); 459: if (mf == NULL) { 460: perror("fopen"); 1.1.1.2 ! root 461: fprintf(stderr, "cannot open mode file %s\n", modefile); ! 462: fprintf(stderr, "directory mode, owner, and times not set\n"); ! 463: return; 1.1 root 464: } 465: clearerr(mf); 466: for (;;) { 467: (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf); 468: if (feof(mf)) 469: break; 470: ep = lookupino(node.ino); 1.1.1.2 ! root 471: if (command == 'i' || command == 'x' || command == 'X'){ 1.1 root 472: if (ep == NIL) 473: continue; 1.1.1.2 ! root 474: if (ep->e_flags & EXISTED) { ! 475: ep->e_flags &= ~NEW; ! 476: continue; ! 477: } 1.1 root 478: if (node.ino == ROOTINO && 479: reply("set owner/mode for '.'") == FAIL) 480: continue; 481: } 482: if (ep == NIL) 483: panic("cannot find directory inode %d\n", node.ino); 484: cp = myname(ep); 485: (void) chown(cp, node.uid, node.gid); 486: (void) chmod(cp, node.mode); 487: utime(cp, node.timep); 488: ep->e_flags &= ~NEW; 489: } 490: if (ferror(mf)) 491: panic("error setting directory modes\n"); 492: (void) fclose(mf); 493: } 494: 495: /* 496: * Generate a literal copy of a directory. 497: */ 498: genliteraldir(name, ino) 499: char *name; 500: ino_t ino; 501: { 502: register struct inotab *itp; 503: int ofile, dp, i, size; 504: char buf[BUFSIZ]; 505: 506: itp = inotablookup(ino); 507: if (itp == NULL) 508: panic("Cannot find directory inode %d named %s\n", ino, name); 509: if ((ofile = creat(name, 0666)) < 0) { 510: fprintf(stderr, "%s: ", name); 511: (void) fflush(stderr); 512: perror("cannot create file"); 513: return (FAIL); 514: } 515: rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 516: dp = dup(dirp->dd_fd); 517: for (i = itp->t_size; i > 0; i -= BUFSIZ) { 518: size = i < BUFSIZ ? i : BUFSIZ; 519: if (read(dp, buf, (int) size) == -1) { 520: fprintf(stderr, 521: "write error extracting inode %d, name %s\n", 522: curfile.ino, curfile.name); 523: perror("read"); 524: done(1); 525: } 526: if (write(ofile, buf, (int) size) == -1) { 527: fprintf(stderr, 528: "write error extracting inode %d, name %s\n", 529: curfile.ino, curfile.name); 530: perror("write"); 531: done(1); 532: } 533: } 534: (void) close(dp); 535: (void) close(ofile); 536: return (GOOD); 537: } 538: 539: /* 540: * Determine the type of an inode 541: */ 542: inodetype(ino) 543: ino_t ino; 544: { 545: struct inotab *itp; 546: 547: itp = inotablookup(ino); 548: if (itp == NULL) 549: return (LEAF); 550: return (NODE); 551: } 552: 553: /* 554: * Allocate and initialize a directory inode entry. 555: * If requested, save its pertinent mode, owner, and time info. 556: */ 557: struct inotab * 558: allocinotab(ino, dip, seekpt) 559: ino_t ino; 560: struct dinode *dip; 561: daddr_t seekpt; 562: { 563: register struct inotab *itp; 564: struct modeinfo node; 565: 566: itp = (struct inotab *)calloc(1, sizeof(struct inotab)); 567: if (itp == 0) 568: panic("no memory directory table\n"); 569: itp->t_next = inotab[INOHASH(ino)]; 570: inotab[INOHASH(ino)] = itp; 571: itp->t_ino = ino; 572: itp->t_seekpt = seekpt; 573: if (mf == NULL) 574: return(itp); 575: node.ino = ino; 576: node.timep[0] = dip->di_atime; 577: node.timep[1] = dip->di_mtime; 578: node.mode = dip->di_mode; 579: node.uid = dip->di_uid; 580: node.gid = dip->di_gid; 581: (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf); 582: return(itp); 583: } 584: 585: /* 586: * Look up an inode in the table of directories 587: */ 588: struct inotab * 589: inotablookup(ino) 590: ino_t ino; 591: { 592: register struct inotab *itp; 593: 594: for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next) 595: if (itp->t_ino == ino) 596: return(itp); 597: return ((struct inotab *)0); 598: } 599: 600: /* 601: * Clean up and exit 602: */ 603: done(exitcode) 604: int exitcode; 605: { 606: 607: closemt(); 608: if (modefile[0] != '#') 609: (void) unlink(modefile); 610: if (dirfile[0] != '#') 611: (void) unlink(dirfile); 612: exit(exitcode); 613: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.