Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

blockdestroyer.cc

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 #include "options.h"
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <sys/time.h>
00010 
00011 int main(int argc, char * const argv[])
00012 {
00013   if (argc!=3) {
00014     fprintf(stderr, "%s: usage: %s destroycount filename\n", argv[0], argv[0]);
00015     abort();
00016   }
00017 
00018   struct timeval tv; gettimeofday(&tv,NULL);
00019   srandom(tv.tv_sec-tv.tv_usec);
00020 
00021   int destroycount=atol(argv[1]);
00022   int i;
00023   FILE* file=fopen(argv[2],"rb+");
00024   if (NULL == file) { perror(argv[2]); abort(); }
00025   fseek(file, 0, SEEK_END);
00026   long fsize=ftell(file);
00027   long fblocks=fsize/blklen;
00028   if ( fblocks*blklen < fsize ) ++fblocks;
00029   bool *destroy = new bool[fblocks]; // tells whether block no. i will be destroyed
00030   for (i=0; i<fblocks; ++i) destroy[i]=false;
00031   while ( destroycount > 0 ) {
00032     int rblk = random()%fblocks;
00033     if (!destroy[rblk]) {
00034       destroy[rblk]=true;
00035       --destroycount;
00036     }
00037   }
00038   char buf[blklen]; for(int i=0; i<blklen; ++i) buf[i]='e';
00039   for (i=0; i<fblocks; ++i) if (destroy[i]) {
00040     // std::cout << "destroying: " << i << "\n";
00041     fseek(file, i*blklen, SEEK_SET);
00042     if ( fblocks-1 == i ) {
00043       int rest = fsize-i*blklen; 
00044       if ( 0 != rest - fwrite(buf, sizeof(char), rest, file) ) {
00045         perror(argv[2]); abort();
00046       }
00047     } else {
00048       if ( 1 != fwrite(buf, sizeof(buf), 1, file) ) {
00049         perror(argv[2]); abort();
00050       }
00051     }
00052   }
00053   return 0;
00054 }

Generated on Tue Dec 30 12:07:35 2003 for ErrorProtect by doxygen 1.3.2