redis-check-aof.c 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Redis nor the names of its contributors may be used
 *     to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

31 32 33 34
#ifdef _WIN32
#include "win32_Interop/win32_types.h"
#endif

35
#include "fmacros.h"
P
Pieter Noordhuis 已提交
36 37 38
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
H
Henry Rawas 已提交
39
#ifndef _WIN32
40
#include <unistd.h>
H
Henry Rawas 已提交
41
#endif
P
Pieter Noordhuis 已提交
42 43 44
#include <sys/stat.h>
#include "config.h"

H
Henry Rawas 已提交
45
#ifdef _WIN32
46
#include "Win32_Interop\win32fixes.h"
H
Henry Rawas 已提交
47 48 49 50
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif

P
Pieter Noordhuis 已提交
51 52 53
#define ERROR(...) { \
    char __buf[1024]; \
    sprintf(__buf, __VA_ARGS__); \
54
    sprintf(error, "0x%16llx: %s", (long long)epos, __buf); \
P
Pieter Noordhuis 已提交
55 56 57
}

static char error[1024];
H
Henry Rawas 已提交
58 59 60
#ifdef _WIN32
long long epos;
#else
61
static off_t epos;
H
Henry Rawas 已提交
62
#endif
P
Pieter Noordhuis 已提交
63 64 65 66 67 68 69 70 71 72 73

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;
74
    epos = ftello(fp);
P
Pieter Noordhuis 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87
    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;
88
    epos = ftello(fp);
H
Henry Rawas 已提交
89
    real = (long)fread(target,1,length,fp);
P
Pieter Noordhuis 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    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);
}

121 122 123
off_t process(FILE *fp) {
    long argc;
    off_t pos = 0;
P
Pieter Noordhuis 已提交
124 125 126 127
    int i, multi = 0;
    char *str;

    while(1) {
H
Henry Rawas 已提交
128
        if (!multi) pos = (off_t)ftello(fp);
129
        if (!readArgc(fp, &argc)) break;
P
Pieter Noordhuis 已提交
130 131

        for (i = 0; i < argc; i++) {
132
            if (!readString(fp,&str)) break;
P
Pieter Noordhuis 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            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);
        }

149
        /* Stop if the loop did not finish */
P
Pieter Noordhuis 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        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) {
166 167
    char *filename;
    int fix = 0;
H
Henry Rawas 已提交
168 169 170
    FILE *fp;
    struct redis_stat sb;
#ifdef _WIN32
171 172 173
    long long size;
    long long pos;
    long long diff;
H
Henry Rawas 已提交
174 175

    _fmode = _O_BINARY;
176 177 178
    setmode(_fileno(stdin), _O_BINARY);
    setmode(_fileno(stdout), _O_BINARY);
    setmode(_fileno(stderr), _O_BINARY);
179 180 181 182
#else
    off_t size;
    off_t pos;
    off_t diff;
H
Henry Rawas 已提交
183
#endif
P
Pieter Noordhuis 已提交
184 185 186 187 188 189 190

    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) {
191 192 193 194 195
        if (strcmp(argv[1],"--fix") != 0) {
            printf("Invalid argument: %s\n", argv[1]);
            exit(1);
        }
        filename = argv[2];
P
Pieter Noordhuis 已提交
196
        fix = 1;
197
    } else {
P
Pieter Noordhuis 已提交
198
        printf("Invalid arguments\n");
199 200 201
        exit(1);
    }

H
Henry Rawas 已提交
202 203 204 205 206
#ifdef _WIN32
    fp = fopen(filename,"r+b");
#else
    fp = fopen(filename,"r+");
#endif
P
Pieter Noordhuis 已提交
207
    if (fp == NULL) {
208
        printf("Cannot open file: %s\n", filename);
P
Pieter Noordhuis 已提交
209 210 211 212
        exit(1);
    }

    if (redis_fstat(fileno(fp),&sb) == -1) {
213
        printf("Cannot stat file: %s\n", filename);
P
Pieter Noordhuis 已提交
214 215 216
        exit(1);
    }

H
Henry Rawas 已提交
217
    size = sb.st_size;
P
Pieter Noordhuis 已提交
218
    if (size == 0) {
219
        printf("Empty file: %s\n", filename);
P
Pieter Noordhuis 已提交
220 221 222
        exit(1);
    }

H
Henry Rawas 已提交
223 224
    pos = process(fp);
    diff = size-pos;
225 226
    printf("AOF analyzed: size=%lld, ok_up_to=%lld, diff=%lld\n",
        (long long) size, (long long) pos, (long long) diff);
227
    if (diff > 0) {
228
        if (fix) {
229
            char buf[2];
230
            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);
231 232 233 234 235 236
            printf("Continue? [y/N]: ");
            if (fgets(buf,sizeof(buf),stdin) == NULL ||
                strncasecmp(buf,"y",1) != 0) {
                    printf("Aborting...\n");
                    exit(1);
            }
237
            if (ftruncate(fileno(fp), pos) == -1) {
238
                printf("Failed to truncate AOF\n");
239 240
                exit(1);
            } else {
241
                printf("Successfully truncated AOF\n");
242 243
            }
        } else {
244 245
            printf("AOF is not valid\n");
            exit(1);
246
        }
P
Pieter Noordhuis 已提交
247
    } else {
248
        printf("AOF is valid\n");
P
Pieter Noordhuis 已提交
249 250 251 252 253
    }

    fclose(fp);
    return 0;
}