|
|
1.1 ! root 1: #include <libc.h> ! 2: #include "worm.h" ! 3: #include "sym.h" ! 4: ! 5: char * ! 6: lkopi(s, blk, doinodes) ! 7: register Superblock *s; ! 8: long blk; ! 9: { ! 10: register Inode *i; ! 11: short fd = s->fd; ! 12: char *b; ! 13: long nb; ! 14: char *nameb; ! 15: Inode *inodes; ! 16: static char buf[64]; ! 17: ! 18: if((b = malloc(s->blocksize)) == 0){ ! 19: sprint(buf, "couldn't malloc buffer (%d bytes)", s->blocksize); ! 20: return(buf); ! 21: } ! 22: numinodes = 0; ! 23: numnamechars = 0; ! 24: for(;;){ ! 25: if(s->magic != SMAGIC){ ! 26: fprint(2, "bad Superblock at %ld\n", blk); ! 27: exit(1); ! 28: } ! 29: if(s->ninodes){ ! 30: numinodes += s->ninodes; ! 31: numnamechars += s->ninochars; ! 32: } ! 33: if(doinodes && s->ninodes){ ! 34: nb = (s->ninodes+IPERB-1)/IPERB; ! 35: inodes = (Inode *)malloc((unsigned)(s->blocksize*nb)); ! 36: if(inodes == 0){ ! 37: sprint(buf, "inode malloc(%d) fail, sbrk=%d\n", ! 38: (s->blocksize*nb), sbrk(0)); ! 39: return(buf); ! 40: } ! 41: Seek(s, s->binodes); ! 42: if(Read(s, (char *)inodes, nb)) ! 43: goto skip; ! 44: nb = (s->ninochars+s->blocksize-1)/s->blocksize; ! 45: nameb = malloc((unsigned)(s->blocksize*nb)); ! 46: if(nameb == 0){ ! 47: sprint(buf, "name buffer malloc(%d) fail, sbrk=%d\n", ! 48: (s->blocksize*nb), sbrk(0)); ! 49: return(buf); ! 50: } ! 51: if(Read(s, nameb, nb)) ! 52: goto skip; ! 53: for(nb = 0, i = inodes; nb < s->ninodes; nb++, i++){ ! 54: i->name.n = i->name.o+nameb; ! 55: if(strcmp(i->name.n, "v14725/791")==0) print("%s at block %d\n", i->name.n, i->block); ! 56: if(i->block < 0) ! 57: (void)symdel(i->name.n, S_INODE); ! 58: else { ! 59: (void)symlook(i->name.n, S_INODE, (void *)i); ! 60: } ! 61: } ! 62: if(sym_mem_fail){ ! 63: sprint(buf, "%d inode malloc fails: %d, %d sbrk=%d\n", ! 64: sym_mem_fail, numinodes, s->ninodes, sbrk(0)); ! 65: return(buf); ! 66: } ! 67: } ! 68: skip: ! 69: blk = s->nextsb; ! 70: Seek(s, blk); ! 71: if(Read(s, b, 1L)) ! 72: break; ! 73: *s = *((Superblock *)b); ! 74: s->fd = fd; ! 75: if(s->myblock == 0) ! 76: s->myblock = blk; ! 77: } ! 78: free(b); ! 79: return((char *)0); ! 80: } ! 81: ! 82: char * ! 83: lkwri(s, i, ni, c, nc, ndata) ! 84: Superblock *s; ! 85: Inode *i; ! 86: long ni, nc, ndata; ! 87: char *c; ! 88: { ! 89: char *b; ! 90: long blk; ! 91: static char buf[256]; ! 92: long ib, ic; ! 93: ! 94: s->ninodes = ni; ! 95: s->ninochars = nc; ! 96: ib = (ni+IPERB-1)/IPERB; ! 97: ic = (nc+s->blocksize-1)/s->blocksize; ! 98: if(ndata+ib+ic+1 > s->nfree) /* one for superblock */ ! 99: return("not enough space for new files"); ! 100: s->binodes = s->nextffree+ndata; ! 101: s->nextffree += ndata+ib+ic; ! 102: s->nfree -= ndata+ib+ic; ! 103: ! 104: if((b = malloc(s->blocksize)) == 0){ ! 105: sprint(buf, "couldn't malloc buffer (%d bytes)", s->blocksize); ! 106: return(buf); ! 107: } ! 108: blk = s->nextsb; ! 109: s->nextsb = s->nextffree++; ! 110: s->nfree--; ! 111: s->myblock = blk; ! 112: time(&s->ctime); ! 113: memset(b, 0, s->blocksize); ! 114: *((Superblock *)b) = *s; ! 115: Seek(s, blk); ! 116: if(Write(s, b, 1L)){ ! 117: sprint(buf, "couldn't write superblock at %d", blk); ! 118: return(buf); ! 119: } ! 120: free(b); ! 121: Seek(s, s->binodes); ! 122: if(Write(s, (char *)i, ib)) ! 123: return("write1 error"); ! 124: if(Write(s, c, ic)) ! 125: return("write2 error"); ! 126: return((char *)0); ! 127: } ! 128: ! 129: ! 130: char * ! 131: lkwsb(s) ! 132: Superblock *s; ! 133: { ! 134: char *b; ! 135: long blk; ! 136: static char buf[64]; ! 137: ! 138: if((b = malloc(s->blocksize)) == 0){ ! 139: sprint(buf, "couldn't malloc buffer (%d bytes)", s->blocksize); ! 140: return(buf); ! 141: } ! 142: blk = s->nextsb; ! 143: s->nextsb = s->nextffree++; ! 144: s->nfree--; ! 145: memset(b, 0, s->blocksize); ! 146: s->myblock = blk; ! 147: *((Superblock *)b) = *s; ! 148: Seek(s, blk); ! 149: if(Write(s, b, 1L)) ! 150: return("couldn't write superblock"); ! 151: free(b); ! 152: return((char *)0); ! 153: } ! 154: ! 155: Inode * ! 156: vinodefn(s) ! 157: char *s; ! 158: { ! 159: return((Inode *)symlook(s, S_INODE, (void *)0)); ! 160: } ! 161: ! 162: void ! 163: vtraverse(fn) ! 164: void (*fn)(); ! 165: { ! 166: symtraverse(S_INODE, fn); ! 167: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.