|
|
1.1 ! root 1: #include <libc.h> ! 2: #include <fio.h> ! 3: #include <sys/types.h> ! 4: #include <sys/stat.h> ! 5: #include <signal.h> ! 6: #include "worm.h" ! 7: ! 8: static int bad = 0; ! 9: static long nbytes; ! 10: static long nfiles; ! 11: char *argout; ! 12: ! 13: main(argc, argv) ! 14: char **argv; ! 15: { ! 16: Superblock s; ! 17: char *e; ! 18: char buf[4096]; ! 19: int n; ! 20: int c; ! 21: char *dev = "/dev/worm0"; ! 22: extern char *optarg; ! 23: extern int optind; ! 24: ! 25: argout = argv[0]; ! 26: while((c = getopt(argc, argv, "f:")) != -1) ! 27: switch(c) ! 28: { ! 29: case 'f': dev = optarg; break; ! 30: case '?': usage(); ! 31: } ! 32: ! 33: if(optind >= argc) ! 34: usage(); ! 35: dev = mapdev(dev); ! 36: if((s.fd = open(dev, 2)) < 0){ ! 37: perror(*argv); ! 38: exit(1); ! 39: } ! 40: if(e = openinode(&s, DO_INODE|SPIN_DOWN)){ ! 41: fprint(2, "%s: %s\n", *argv, e); ! 42: exit(1); ! 43: } ! 44: if(strcmp(s.vol_id, argv[optind])){ ! 45: fprint(2, "vol_id mismatch: wanted %s, got %s\n", argv[optind], s.vol_id); ! 46: exit(1); ! 47: } ! 48: if(s.nfree == 0){ ! 49: fprint(2, "%s: can't write any more!\n", dev); ! 50: exit(1); ! 51: } ! 52: if(s.version != VLINK){ ! 53: fprint(2, "%s: can't write on a b-tree disk\n", s.vol_id); ! 54: exit(1); ! 55: } ! 56: for(n = 1; n <= NSIG; n++) ! 57: signal(n, SIG_IGN); ! 58: ininit(); ! 59: if(++optind < argc) ! 60: while(optind < argc) ! 61: proc(&s, argv[optind++]); ! 62: else ! 63: while(e = Frdline(0)) ! 64: proc(&s, e); ! 65: if(bad) ! 66: exit(1); ! 67: inwrite(&s); ! 68: if(bad) ! 69: exit(1); ! 70: exit(0); ! 71: } ! 72: ! 73: usage() ! 74: { ! 75: fprint(2, "Usage: worm rm [-fdevice] vol_id [files]\n"); ! 76: exit(1); ! 77: } ! 78: ! 79: proc(s, file) ! 80: Superblock *s; ! 81: char *file; ! 82: { ! 83: Inode i; ! 84: ! 85: if(inodeof(file) == 0){ ! 86: fprint(2, "%s: not on worm\n", file); ! 87: return; ! 88: } ! 89: i.magic = DMAGIC; ! 90: i.block = -1; ! 91: i.name.n = file; ! 92: if(inadd(s, &i)) ! 93: bad = 1; ! 94: } ! 95: ! 96: static Inode *inodes; ! 97: static long ip; ! 98: static long ninodes = 0; ! 99: static char *nameb; ! 100: static long np; ! 101: static long nnameb = 0; ! 102: static long nblocks; ! 103: #define IINC 1024 ! 104: #define NINC (64*IINC) ! 105: ! 106: ininit() ! 107: { ! 108: if(nnameb == 0){ ! 109: nameb = malloc((unsigned)(nnameb = NINC)); ! 110: if(nameb == 0){ ! 111: fprint(2, "wrm: malloc fail, %d bytes\n", nnameb); ! 112: exit(1); ! 113: } ! 114: } ! 115: np = 0; ! 116: if(ninodes == 0){ ! 117: inodes = (Inode *)malloc(sizeof(Inode)*(unsigned)(ninodes = IINC)); ! 118: if(inodes == 0){ ! 119: fprint(2, "wrm: malloc fail, %d inodes %d bytes\n", ninodes, ninodes*sizeof(Inode)); ! 120: exit(1); ! 121: } ! 122: } ! 123: ip = 0; ! 124: } ! 125: ! 126: inadd(s, i) ! 127: Superblock *s; ! 128: register Inode *i; ! 129: { ! 130: register long len; ! 131: ! 132: len = strlen(i->name.n)+1; ! 133: if(np+len > nnameb){ ! 134: while(np+len > nnameb) ! 135: nnameb += NINC; ! 136: nameb = realloc(nameb, (unsigned)nnameb); ! 137: if(nameb == 0){ ! 138: fprint(2, "wrm: realloc fail, %d bytes\n", nnameb); ! 139: exit(1); ! 140: } ! 141: } ! 142: strcpy(nameb+np, i->name.n); ! 143: i->name.o = np; ! 144: np += len; ! 145: if(ip == ninodes){ ! 146: ninodes += IINC; ! 147: inodes = (Inode *)realloc((char *)inodes, (unsigned)ninodes*sizeof(Inode)); ! 148: if(inodes == 0){ ! 149: fprint(2, "wrm: realloc fail, %d inodes %d bytes\n", ninodes, ninodes*sizeof(Inode)); ! 150: exit(1); ! 151: } ! 152: } ! 153: inodes[ip++] = *i; ! 154: return(0); ! 155: } ! 156: ! 157: inwrite(s) ! 158: Superblock *s; ! 159: { ! 160: char *e; ! 161: ! 162: if(e = lkwri(s, inodes, ip, nameb, np, 0L)){ ! 163: fprint(2, "%s\n", e); ! 164: bad = 1; ! 165: return; ! 166: } ! 167: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.