]> git.saurik.com Git - redis.git/blame_incremental - src/redis-check-aof.c
Fixed some spelling errors in the comments
[redis.git] / src / redis-check-aof.c
... / ...
CommitLineData
1#include "fmacros.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5#include <unistd.h>
6#include <sys/stat.h>
7#include "config.h"
8
9#define ERROR(...) { \
10 char __buf[1024]; \
11 sprintf(__buf, __VA_ARGS__); \
12 sprintf(error, "0x%16llx: %s", (long long)epos, __buf); \
13}
14
15static char error[1024];
16static off_t epos;
17
18int consumeNewline(char *buf) {
19 if (strncmp(buf,"\r\n",2) != 0) {
20 ERROR("Expected \\r\\n, got: %02x%02x",buf[0],buf[1]);
21 return 0;
22 }
23 return 1;
24}
25
26int readLong(FILE *fp, char prefix, long *target) {
27 char buf[128], *eptr;
28 epos = ftello(fp);
29 if (fgets(buf,sizeof(buf),fp) == NULL) {
30 return 0;
31 }
32 if (buf[0] != prefix) {
33 ERROR("Expected prefix '%c', got: '%c'",buf[0],prefix);
34 return 0;
35 }
36 *target = strtol(buf+1,&eptr,10);
37 return consumeNewline(eptr);
38}
39
40int readBytes(FILE *fp, char *target, long length) {
41 long real;
42 epos = ftello(fp);
43 real = fread(target,1,length,fp);
44 if (real != length) {
45 ERROR("Expected to read %ld bytes, got %ld bytes",length,real);
46 return 0;
47 }
48 return 1;
49}
50
51int readString(FILE *fp, char** target) {
52 long len;
53 *target = NULL;
54 if (!readLong(fp,'$',&len)) {
55 return 0;
56 }
57
58 /* Increase length to also consume \r\n */
59 len += 2;
60 *target = (char*)malloc(len);
61 if (!readBytes(fp,*target,len)) {
62 return 0;
63 }
64 if (!consumeNewline(*target+len-2)) {
65 return 0;
66 }
67 (*target)[len-2] = '\0';
68 return 1;
69}
70
71int readArgc(FILE *fp, long *target) {
72 return readLong(fp,'*',target);
73}
74
75off_t process(FILE *fp) {
76 long argc;
77 off_t pos = 0;
78 int i, multi = 0;
79 char *str;
80
81 while(1) {
82 if (!multi) pos = ftello(fp);
83 if (!readArgc(fp, &argc)) break;
84
85 for (i = 0; i < argc; i++) {
86 if (!readString(fp,&str)) break;
87 if (i == 0) {
88 if (strcasecmp(str, "multi") == 0) {
89 if (multi++) {
90 ERROR("Unexpected MULTI");
91 break;
92 }
93 } else if (strcasecmp(str, "exec") == 0) {
94 if (--multi) {
95 ERROR("Unexpected EXEC");
96 break;
97 }
98 }
99 }
100 free(str);
101 }
102
103 /* Stop if the loop did not finish */
104 if (i < argc) {
105 if (str) free(str);
106 break;
107 }
108 }
109
110 if (feof(fp) && multi && strlen(error) == 0) {
111 ERROR("Reached EOF before reading EXEC for MULTI");
112 }
113 if (strlen(error) > 0) {
114 printf("%s\n", error);
115 }
116 return pos;
117}
118
119int main(int argc, char **argv) {
120 char *filename;
121 int fix = 0;
122
123 if (argc < 2) {
124 printf("Usage: %s [--fix] <file.aof>\n", argv[0]);
125 exit(1);
126 } else if (argc == 2) {
127 filename = argv[1];
128 } else if (argc == 3) {
129 if (strcmp(argv[1],"--fix") != 0) {
130 printf("Invalid argument: %s\n", argv[1]);
131 exit(1);
132 }
133 filename = argv[2];
134 fix = 1;
135 } else {
136 printf("Invalid arguments\n");
137 exit(1);
138 }
139
140 FILE *fp = fopen(filename,"r+");
141 if (fp == NULL) {
142 printf("Cannot open file: %s\n", filename);
143 exit(1);
144 }
145
146 struct redis_stat sb;
147 if (redis_fstat(fileno(fp),&sb) == -1) {
148 printf("Cannot stat file: %s\n", filename);
149 exit(1);
150 }
151
152 off_t size = sb.st_size;
153 if (size == 0) {
154 printf("Empty file: %s\n", filename);
155 exit(1);
156 }
157
158 off_t pos = process(fp);
159 off_t diff = size-pos;
160 printf("AOF analyzed: size=%lld, ok_up_to=%lld, diff=%lld\n",
161 (long long) size, (long long) pos, (long long) diff);
162 if (diff > 0) {
163 if (fix) {
164 char buf[2];
165 printf("This will shrink the AOF from %lld bytes, with %lld bytes, to %lld bytes\n",(long long)size,(long long)diff,(long long)pos);
166 printf("Continue? [y/N]: ");
167 if (fgets(buf,sizeof(buf),stdin) == NULL ||
168 strncasecmp(buf,"y",1) != 0) {
169 printf("Aborting...\n");
170 exit(1);
171 }
172 if (ftruncate(fileno(fp), pos) == -1) {
173 printf("Failed to truncate AOF\n");
174 exit(1);
175 } else {
176 printf("Successfully truncated AOF\n");
177 }
178 } else {
179 printf("AOF is not valid\n");
180 exit(1);
181 }
182 } else {
183 printf("AOF is valid\n");
184 }
185
186 fclose(fp);
187 return 0;
188}