]> git.saurik.com Git - redis.git/blame - src/redis-check-aof.c
add -f flag to cp when installing, otherwise stopping the server is
[redis.git] / src / redis-check-aof.c
CommitLineData
418807d2 1#include "fmacros.h"
b4bd0524
PN
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
cb8ae3c8 5#include <unistd.h>
b4bd0524
PN
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%08lx: %s", epos, __buf); \
13}
14
15static char error[1024];
16static long 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 = ftell(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 = ftell(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)) {
b4bd0524
PN
62 return 0;
63 }
64 if (!consumeNewline(*target+len-2)) {
b4bd0524
PN
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
75long process(FILE *fp) {
76 long argc, pos = 0;
77 int i, multi = 0;
78 char *str;
79
80 while(1) {
81 if (!multi) pos = ftell(fp);
e51fa063 82 if (!readArgc(fp, &argc)) break;
b4bd0524
PN
83
84 for (i = 0; i < argc; i++) {
e51fa063 85 if (!readString(fp,&str)) break;
b4bd0524
PN
86 if (i == 0) {
87 if (strcasecmp(str, "multi") == 0) {
88 if (multi++) {
89 ERROR("Unexpected MULTI");
90 break;
91 }
92 } else if (strcasecmp(str, "exec") == 0) {
93 if (--multi) {
94 ERROR("Unexpected EXEC");
95 break;
96 }
97 }
98 }
99 free(str);
100 }
101
e51fa063 102 /* Stop if the loop did not finish */
b4bd0524
PN
103 if (i < argc) {
104 if (str) free(str);
105 break;
106 }
107 }
108
109 if (feof(fp) && multi && strlen(error) == 0) {
110 ERROR("Reached EOF before reading EXEC for MULTI");
111 }
b4bd0524
PN
112 if (strlen(error) > 0) {
113 printf("%s\n", error);
114 }
b4bd0524
PN
115 return pos;
116}
117
118int main(int argc, char **argv) {
cb8ae3c8
PN
119 char *filename;
120 int fix = 0;
57ca68ac
PN
121
122 if (argc < 2) {
123 printf("Usage: %s [--fix] <file.aof>\n", argv[0]);
124 exit(1);
125 } else if (argc == 2) {
126 filename = argv[1];
127 } else if (argc == 3) {
cb8ae3c8
PN
128 if (strcmp(argv[1],"--fix") != 0) {
129 printf("Invalid argument: %s\n", argv[1]);
130 exit(1);
131 }
cb8ae3c8 132 filename = argv[2];
57ca68ac 133 fix = 1;
cb8ae3c8 134 } else {
57ca68ac 135 printf("Invalid arguments\n");
cb8ae3c8
PN
136 exit(1);
137 }
138
139 FILE *fp = fopen(filename,"r+");
b4bd0524 140 if (fp == NULL) {
cb8ae3c8 141 printf("Cannot open file: %s\n", filename);
b4bd0524
PN
142 exit(1);
143 }
144
145 struct redis_stat sb;
146 if (redis_fstat(fileno(fp),&sb) == -1) {
cb8ae3c8 147 printf("Cannot stat file: %s\n", filename);
b4bd0524
PN
148 exit(1);
149 }
150
151 long size = sb.st_size;
152 if (size == 0) {
cb8ae3c8 153 printf("Empty file: %s\n", filename);
b4bd0524
PN
154 exit(1);
155 }
156
157 long pos = process(fp);
81330149
PN
158 long diff = size-pos;
159 if (diff > 0) {
cb8ae3c8 160 if (fix) {
81330149
PN
161 char buf[2];
162 printf("This will shrink the AOF from %ld bytes, with %ld bytes, to %ld bytes\n",size,diff,pos);
163 printf("Continue? [y/N]: ");
164 if (fgets(buf,sizeof(buf),stdin) == NULL ||
165 strncasecmp(buf,"y",1) != 0) {
166 printf("Aborting...\n");
167 exit(1);
168 }
cb8ae3c8 169 if (ftruncate(fileno(fp), pos) == -1) {
81330149 170 printf("Failed to truncate AOF\n");
cb8ae3c8
PN
171 exit(1);
172 } else {
81330149 173 printf("Successfully truncated AOF\n");
cb8ae3c8
PN
174 }
175 } else {
81330149
PN
176 printf("AOF is not valid\n");
177 exit(1);
cb8ae3c8 178 }
b4bd0524 179 } else {
cb8ae3c8 180 printf("AOF is valid\n");
b4bd0524
PN
181 }
182
183 fclose(fp);
184 return 0;
185}