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