xref: /redis-3.2.3/utils/corrupt_rdb.c (revision a117dfa8)
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 
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <time.h>
13 
main(int argc,char ** argv)14 int main(int argc, char **argv) {
15     struct stat stat;
16     int fd, cycles;
17 
18     if (argc != 3) {
19         fprintf(stderr,"Usage: <filename> <cycles>\n");
20         exit(1);
21     }
22 
23     srand(time(NULL));
24     cycles = atoi(argv[2]);
25     fd = open("dump.rdb",O_RDWR);
26     if (fd == -1) {
27         perror("open");
28         exit(1);
29     }
30     fstat(fd,&stat);
31 
32     while(cycles--) {
33         unsigned char buf[32];
34         unsigned long offset = rand()%stat.st_size;
35         int writelen = 1+rand()%31;
36         int j;
37 
38         for (j = 0; j < writelen; j++) buf[j] = (char)rand();
39         lseek(fd,offset,SEEK_SET);
40         printf("Writing %d bytes at offset %lu\n", writelen, offset);
41         write(fd,buf,writelen);
42     }
43     return 0;
44 }
45