|
|
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: main(argc, argv) ! 9: char **argv; ! 10: { ! 11: Superblock s; ! 12: char *e; ! 13: char buf[4096]; ! 14: int n; ! 15: int c; ! 16: char *dev = "/dev/worm0"; ! 17: extern char *optarg; ! 18: extern int optind; ! 19: ! 20: argout = argv[0]; ! 21: while((c = getopt(argc, argv, "f:")) != -1) ! 22: switch(c) ! 23: { ! 24: case 'f': dev = optarg; break; ! 25: case '?': usage(); ! 26: } ! 27: ! 28: if(optind >= argc) ! 29: usage(); ! 30: dev = mapdev(dev); ! 31: if((s.fd = open(dev, 2)) < 0){ ! 32: perror(*argv); ! 33: exit(1); ! 34: } ! 35: if(e = openinode(&s, SPIN_DOWN)){ ! 36: fprint(2, "%s: %s\n", *argv, e); ! 37: exit(1); ! 38: } ! 39: if(strcmp(s.vol_id, argv[optind])){ ! 40: fprint(2, "vol_id mismatch: wanted %s, got %s\n", argv[optind], s.vol_id); ! 41: exit(1); ! 42: } ! 43: if(s.nfree == 0){ ! 44: fprint(2, "%s: can't write any more!\n", dev); ! 45: exit(1); ! 46: } ! 47: if(s.version != VLINK){ ! 48: fprint(2, "%s: can't write on a b-tree disk\n", s.vol_id); ! 49: exit(1); ! 50: } ! 51: for(n = 1; n <= NSIG; n++) ! 52: signal(n, SIG_IGN); ! 53: ininit(); ! 54: if(++optind < argc) ! 55: while(optind < argc) ! 56: proc(&s, argv[optind++]); ! 57: else ! 58: while(e = Frdline(0)) ! 59: proc(&s, e); ! 60: if(bad) ! 61: exit(1); ! 62: nfiles = nbytes = 0; ! 63: inwrite(&s, (void *)0); ! 64: if(bad) ! 65: exit(1); ! 66: fprint(1, "%d files, %.6fMb\n", nfiles, nbytes/1e6); ! 67: exit(0); ! 68: } ! 69: ! 70: usage() ! 71: { ! 72: fprint(2, "Usage: worm write [-fdevice] vol_id [files]\n"); ! 73: exit(1); ! 74: } ! 75: ! 76: proc(s, file) ! 77: Superblock *s; ! 78: char *file; ! 79: { ! 80: struct stat sbuf; ! 81: unsigned short mode; ! 82: Inode i; ! 83: ! 84: memset((char *)&i, 0, sizeof(i)); ! 85: if(stat(file, &sbuf) < 0){ ! 86: perror(file); ! 87: return; ! 88: } ! 89: mode = sbuf.st_mode&S_IFMT; ! 90: if((mode == S_IFREG) || (mode == S_IFDIR)){ ! 91: i.magic = DMAGIC; ! 92: i.block = 0; ! 93: i.nbytes = sbuf.st_size; ! 94: nbytes += i.nbytes; ! 95: i.ctime = sbuf.st_ctime; ! 96: i.name.n = file; ! 97: i.mode = sbuf.st_mode; ! 98: i.uid = sbuf.st_uid; ! 99: i.gid = sbuf.st_gid; ! 100: if(inadd(s, &i)) ! 101: bad = 1; ! 102: } else ! 103: fprint(2, "%s is not a file\n", file); ! 104: } ! 105: ! 106: writeout(s, i, blk) ! 107: Superblock *s; ! 108: Inode *i; ! 109: long *blk; ! 110: { ! 111: char b[63*1024L]; ! 112: int fd; ! 113: long n, len, blen; ! 114: char *name; ! 115: ! 116: n = (i->nbytes+s->blocksize-1)/s->blocksize; ! 117: *blk += n; ! 118: blkdone += n; ! 119: blen = sizeof b/s->blocksize; ! 120: len = blen*s->blocksize; ! 121: nbytes += i->nbytes; ! 122: nfiles++; ! 123: name = i->name.n; ! 124: if((fd = open(name, 0)) < 0) ! 125: goto out; ! 126: for(n = i->nbytes; n > len; n -= len){ ! 127: if(read(fd, (char *)b, (int)len) != len){ ! 128: out: ! 129: perror(name); ! 130: bad = 1; ! 131: return; ! 132: } ! 133: if(Write(s, b, blen)){ ! 134: fprint(2, "nb=%d, n=%d len=%d blen=%d\n", i->nbytes, n, len, blen); ! 135: perror("data write"); ! 136: exit(1); ! 137: } ! 138: } ! 139: if(n){ ! 140: memset(b, 0, sizeof b); ! 141: if(read(fd, (char *)b, (int)n) != n) ! 142: goto out; ! 143: n += s->blocksize-1; ! 144: n /= s->blocksize; ! 145: if(Write(s, b, n)){ ! 146: perror("data write"); ! 147: exit(1); ! 148: } ! 149: } ! 150: close(fd); ! 151: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.