xref: /f-stack/app/redis-5.0.5/utils/corrupt_rdb.c (revision 572c4311)
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     char *filename = argv[1];
25     cycles = atoi(argv[2]);
26     fd = open(filename,O_RDWR);
27     if (fd == -1) {
28         perror("open");
29         exit(1);
30     }
31     fstat(fd,&stat);
32 
33     while(cycles--) {
34         unsigned char buf[32];
35         unsigned long offset = rand()%stat.st_size;
36         int writelen = 1+rand()%31;
37         int j;
38 
39         for (j = 0; j < writelen; j++) buf[j] = (char)rand();
40         lseek(fd,offset,SEEK_SET);
41         printf("Writing %d bytes at offset %lu\n", writelen, offset);
42         write(fd,buf,writelen);
43     }
44     return 0;
45 }
46