corrupt_rdb.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. char *filename = argv[1];
  21. cycles = atoi(argv[2]);
  22. fd = open(filename,O_RDWR);
  23. if (fd == -1) {
  24. perror("open");
  25. exit(1);
  26. }
  27. fstat(fd,&stat);
  28. while(cycles--) {
  29. unsigned char buf[32];
  30. unsigned long offset = rand()%stat.st_size;
  31. int writelen = 1+rand()%31;
  32. int j;
  33. for (j = 0; j < writelen; j++) buf[j] = (char)rand();
  34. lseek(fd,offset,SEEK_SET);
  35. printf("Writing %d bytes at offset %lu\n", writelen, offset);
  36. write(fd,buf,writelen);
  37. }
  38. return 0;
  39. }