corrupt_rdb.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* Trivia program to corrupt an RDB file in order to check the RDB check
  2. * program behavior and effectiveness.
  3. *
  4. * Copyright (C) 2016 Salvatore Sanfilippo.
  5. * This software is released in the 3-clause BSD license. */
  6. #include <stdio.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12. int main(int argc, char **argv) {
  13. struct stat stat;
  14. int fd, cycles;
  15. if (argc != 3) {
  16. fprintf(stderr,"Usage: <filename> <cycles>\n");
  17. exit(1);
  18. }
  19. srand(time(NULL));
  20. cycles = atoi(argv[2]);
  21. fd = open("dump.rdb",O_RDWR);
  22. if (fd == -1) {
  23. perror("open");
  24. exit(1);
  25. }
  26. fstat(fd,&stat);
  27. while(cycles--) {
  28. unsigned char buf[32];
  29. unsigned long offset = rand()%stat.st_size;
  30. int writelen = 1+rand()%31;
  31. int j;
  32. for (j = 0; j < writelen; j++) buf[j] = (char)rand();
  33. lseek(fd,offset,SEEK_SET);
  34. printf("Writing %d bytes at offset %lu\n", writelen, offset);
  35. write(fd,buf,writelen);
  36. }
  37. return 0;
  38. }