redis-check-aof.c 4.5 KB
Newer Older
1
#include "fmacros.h"
P
Pieter Noordhuis 已提交
2 3 4
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
5
#include <unistd.h>
P
Pieter Noordhuis 已提交
6 7 8 9 10 11
#include <sys/stat.h>
#include "config.h"

#define ERROR(...) { \
    char __buf[1024]; \
    sprintf(__buf, __VA_ARGS__); \
12
    sprintf(error, "0x%16llx: %s", (long long)epos, __buf); \
P
Pieter Noordhuis 已提交
13 14 15
}

static char error[1024];
16
static off_t epos;
P
Pieter Noordhuis 已提交
17 18 19 20 21 22 23 24 25 26 27

int consumeNewline(char *buf) {
    if (strncmp(buf,"\r\n",2) != 0) {
        ERROR("Expected \\r\\n, got: %02x%02x",buf[0],buf[1]);
        return 0;
    }
    return 1;
}

int readLong(FILE *fp, char prefix, long *target) {
    char buf[128], *eptr;
28
    epos = ftello(fp);
P
Pieter Noordhuis 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
    if (fgets(buf,sizeof(buf),fp) == NULL) {
        return 0;
    }
    if (buf[0] != prefix) {
        ERROR("Expected prefix '%c', got: '%c'",buf[0],prefix);
        return 0;
    }
    *target = strtol(buf+1,&eptr,10);
    return consumeNewline(eptr);
}

int readBytes(FILE *fp, char *target, long length) {
    long real;
42
    epos = ftello(fp);
P
Pieter Noordhuis 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    real = fread(target,1,length,fp);
    if (real != length) {
        ERROR("Expected to read %ld bytes, got %ld bytes",length,real);
        return 0;
    }
    return 1;
}

int readString(FILE *fp, char** target) {
    long len;
    *target = NULL;
    if (!readLong(fp,'$',&len)) {
        return 0;
    }

    /* Increase length to also consume \r\n */
    len += 2;
    *target = (char*)malloc(len);
    if (!readBytes(fp,*target,len)) {
        return 0;
    }
    if (!consumeNewline(*target+len-2)) {
        return 0;
    }
    (*target)[len-2] = '\0';
    return 1;
}

int readArgc(FILE *fp, long *target) {
    return readLong(fp,'*',target);
}

75 76 77
off_t process(FILE *fp) {
    long argc;
    off_t pos = 0;
P
Pieter Noordhuis 已提交
78 79 80 81
    int i, multi = 0;
    char *str;

    while(1) {
82
        if (!multi) pos = ftello(fp);
83
        if (!readArgc(fp, &argc)) break;
P
Pieter Noordhuis 已提交
84 85

        for (i = 0; i < argc; i++) {
86
            if (!readString(fp,&str)) break;
P
Pieter Noordhuis 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
            if (i == 0) {
                if (strcasecmp(str, "multi") == 0) {
                    if (multi++) {
                        ERROR("Unexpected MULTI");
                        break;
                    }
                } else if (strcasecmp(str, "exec") == 0) {
                    if (--multi) {
                        ERROR("Unexpected EXEC");
                        break;
                    }
                }
            }
            free(str);
        }

103
        /* Stop if the loop did not finish */
P
Pieter Noordhuis 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        if (i < argc) {
            if (str) free(str);
            break;
        }
    }

    if (feof(fp) && multi && strlen(error) == 0) {
        ERROR("Reached EOF before reading EXEC for MULTI");
    }
    if (strlen(error) > 0) {
        printf("%s\n", error);
    }
    return pos;
}

int main(int argc, char **argv) {
120 121
    char *filename;
    int fix = 0;
P
Pieter Noordhuis 已提交
122 123 124 125 126 127 128

    if (argc < 2) {
        printf("Usage: %s [--fix] <file.aof>\n", argv[0]);
        exit(1);
    } else if (argc == 2) {
        filename = argv[1];
    } else if (argc == 3) {
129 130 131 132 133
        if (strcmp(argv[1],"--fix") != 0) {
            printf("Invalid argument: %s\n", argv[1]);
            exit(1);
        }
        filename = argv[2];
P
Pieter Noordhuis 已提交
134
        fix = 1;
135
    } else {
P
Pieter Noordhuis 已提交
136
        printf("Invalid arguments\n");
137 138 139 140
        exit(1);
    }

    FILE *fp = fopen(filename,"r+");
P
Pieter Noordhuis 已提交
141
    if (fp == NULL) {
142
        printf("Cannot open file: %s\n", filename);
P
Pieter Noordhuis 已提交
143 144 145 146 147
        exit(1);
    }

    struct redis_stat sb;
    if (redis_fstat(fileno(fp),&sb) == -1) {
148
        printf("Cannot stat file: %s\n", filename);
P
Pieter Noordhuis 已提交
149 150 151
        exit(1);
    }

152
    off_t size = sb.st_size;
P
Pieter Noordhuis 已提交
153
    if (size == 0) {
154
        printf("Empty file: %s\n", filename);
P
Pieter Noordhuis 已提交
155 156 157
        exit(1);
    }

158 159 160 161
    off_t pos = process(fp);
    off_t diff = size-pos;
    printf("AOF analyzed: size=%lld, ok_up_to=%lld, diff=%lld\n",
        (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 167 168 169 170 171
            printf("Continue? [y/N]: ");
            if (fgets(buf,sizeof(buf),stdin) == NULL ||
                strncasecmp(buf,"y",1) != 0) {
                    printf("Aborting...\n");
                    exit(1);
            }
172
            if (ftruncate(fileno(fp), pos) == -1) {
173
                printf("Failed to truncate AOF\n");
174 175
                exit(1);
            } else {
176
                printf("Successfully truncated AOF\n");
177 178
            }
        } else {
179 180
            printf("AOF is not valid\n");
            exit(1);
181
        }
P
Pieter Noordhuis 已提交
182
    } else {
183
        printf("AOF is valid\n");
P
Pieter Noordhuis 已提交
184 185 186 187 188
    }

    fclose(fp);
    return 0;
}