void change(const char* path, int size) { struct stat st; if (!size) return; if (lstat(path, &st) != 0) { if (errno == ENOENT) return; /* it may be already deleted */ /* LCOV_EXCL_START */ fprintf(stderr, "Error accessing %s\n", path); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } if (S_ISLNK(st.st_mode)) { /* symlink */ if (rnd(2) == 0) { /* delete */ if (remove(path) != 0) { /* LCOV_EXCL_START */ fprintf(stderr, "Error removing %s\n", path); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } } else { /* recreate */ char linkto[PATH_MAX]; if (remove(path) != 0) { /* LCOV_EXCL_START */ fprintf(stderr, "Error removing %s\n", path); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } rnd_name(linkto); create_symlink(path, linkto); } } else if (S_ISREG(st.st_mode)) { int r; r = rnd(4); if (r == 0) { /* write */ writ(path, size, 0); } else if (r == 1) { /* append */ append(path, size); } else if (r == 2) { /* truncate */ truncat(path, size); } else { /* delete */ if (remove(path) != 0) { /* LCOV_EXCL_START */ fprintf(stderr, "Error removing %s\n", path); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } } } }
int main(int argc, char* argv[]) { int i, j, b; if (argc < 2) { help(); exit(EXIT_SUCCESS); } if (strcmp(argv[1], "generate") == 0) { int disk, file, size; if (argc != 6) { /* LCOV_EXCL_START */ help(); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } seed = atoi(argv[2]); disk = atoi(argv[3]); file = atoi(argv[4]); size = atoi(argv[5]); for(i=0;i<disk;++i) { for(j=0;j<file;++j) { if (j == 0) generate(i+1, size); else if (j == 1) generate(i+1, 0); else generate(i+1, rnd(size)); } } } else if (strcmp(argv[1], "damage") == 0 || strcmp(argv[1], "write") == 0) { int fail, size; int silent_error = strcmp(argv[1], "damage") == 0; if (argc < 6) { /* LCOV_EXCL_START */ help(); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } seed = atoi(argv[2]); fail = atoi(argv[3]); size = atoi(argv[4]); b = 5; /* sort the file names */ qsort(&argv[b], argc - b, sizeof(argv[b]), qsort_strcmp); for(i=b;i<argc;++i) { for(j=0;j<fail;++j) { writ(argv[i], rndnotzero(size), silent_error); } } } else if (strcmp(argv[1], "append") == 0) { int size; if (argc < 5) { /* LCOV_EXCL_START */ help(); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } seed = atoi(argv[2]); size = atoi(argv[3]); b = 4; /* sort the file names */ qsort(&argv[b], argc - b, sizeof(argv[b]), qsort_strcmp); for(i=b;i<argc;++i) { append(argv[i], rndnotzero(size)); } } else if (strcmp(argv[1], "truncate") == 0) { int size; if (argc < 5) { /* LCOV_EXCL_START */ help(); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } seed = atoi(argv[2]); size = atoi(argv[3]); b = 4; /* sort the file names */ qsort(&argv[b], argc - b, sizeof(argv[b]), qsort_strcmp); for(i=b;i<argc;++i) { truncat(argv[i], rnd(size)); } } else if (strcmp(argv[1], "change") == 0) { int size; if (argc < 5) { /* LCOV_EXCL_START */ help(); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } seed = atoi(argv[2]); size = atoi(argv[3]); b = 4; /* sort the file names */ qsort(&argv[b], argc - b, sizeof(argv[b]), qsort_strcmp); for(i=b;i<argc;++i) { change(argv[i], rnd(size)); } } else { /* LCOV_EXCL_START */ help(); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } return 0; }
void isDiff(char origFile[], char otherFile[],char disp[]) { unsigned long int offset = BUFFER_SIZE; fpos_t origPos; fpos_t otherPos; FILE * origReader; FILE * otherReader; unsigned char * origChar = NULL; unsigned char * otherChar = NULL; unsigned char * temp = calloc(BUFFER_SIZE,sizeof(unsigned char)); unsigned char * temp1 = calloc(BUFFER_SIZE,sizeof(unsigned char)); if(!temp || !temp1) { puts("Memory error!"); abort(); } origChar = temp; otherChar = temp1; memset(origChar,'\0',BUFFER_SIZE); memset(otherChar,'\0',BUFFER_SIZE); mpz_t counter; mpz_t difference; mpz_t origSize; mpz_t otherSize; mpz_init(counter); mpz_init(difference); mpz_init(origSize); mpz_init(otherSize); mpz_set_ui(counter,0); mpz_set_ui(origSize,0); mpz_set_ui(otherSize,0); do { getFileSize(origFile,&origSize); getFileSize(otherFile,&otherSize); mpz_sub(difference,origSize,otherSize); printf("*****"); printf("%s has ",origFile); mpz_out_str(stdout,10,origSize); printf(" bytes while %s has ", otherFile); mpz_out_str(stdout,10,otherSize); printf(" bytes."); printf("*****\n"); if(mpz_cmp(otherSize,origSize) > 0) { puts("=====Truncating File: ====="); truncat(otherFile,origSize); getFileSize(origFile,&origSize); getFileSize(origFile,&otherSize); printf("*****"); printf("%s has ",origFile); mpz_out_str(stdout,10,origSize); printf(" bytes while %s has ", otherFile); mpz_out_str(stdout,10,otherSize); printf(" bytes."); printf("*****\n"); } else if(mpz_cmp(otherSize,origSize) < 0) { fill(otherFile,origSize,otherSize); } }while(mpz_cmp(otherSize,origSize)); origReader = fopen(origFile,"r+b"); otherReader = fopen(otherFile,"r+b"); if( otherReader == NULL || origReader == NULL) { puts("Error opening file!"); perror("Error:"); return; } else { puts("=====Scanning for and merging differences====="); while(mpz_cmp(counter,origSize) < 0) { mpz_sub(difference,origSize,counter); if(fgetpos(otherReader,&otherPos)) { perror("Error getting file position: "); } if(fgetpos(origReader,&origPos)) { perror("Error getting file position: "); } if(mpz_cmp_ui(difference,BUFFER_SIZE) <= 0) { offset = mpz_get_ui(difference); mpz_out_str(stdout,10,difference); printf("\n"); mpz_out_str(stdout,10,counter); printf("\n"); } // printf("Read %s %ld\n",origFile,ftell(origReader)); if(fread(origChar,sizeof(unsigned char),offset,origReader) != offset) { perror("Error reading! "); } // printf("Read %s %ld\n",otherFile,ftell(otherReader)); if(fread(otherChar,sizeof(unsigned char),offset,otherReader) != offset) { perror("Error reading! "); } if(memcmp(origChar,otherChar,offset)) { // puts("Difference found."); //dumpArrs(origChar, otherChar); if(fsetpos(otherReader,&otherPos)) { perror("Error setting file position: "); } if(fsetpos(origReader,&origPos)) { perror("Error setting file position: "); } // printf("Writing to :%ld\n", ftell(otherReader)); if((fread(origChar,sizeof(unsigned char),offset,origReader))!= offset) { perror("Error writing differences"); } keepWritingUntilEqual(&origReader, &otherReader,origChar,otherChar,&origPos,&otherPos,offset); } // puts("======="); // puts("======="); memset(origChar,'\0',BUFFER_SIZE); memset(otherChar,'\0',BUFFER_SIZE); // printf("%u\t%u\n",origChar,otherChar); mpz_add_ui(counter,counter,offset); mySleep(0); } mpz_out_str(stdout,10,counter); printf(" / "); mpz_out_str(stdout,10,origSize); puts(""); printf("Offset :%lu\n",offset); puts("Done!"); fclose(origReader); fclose(otherReader); } mpz_clear(counter); mpz_clear(origSize); mpz_clear(otherSize); mpz_clear(difference); free(origChar); free(otherChar); }