redis-check-aof.c 6.7 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
#include "fmacros.h"
P
Pieter Noordhuis 已提交
32 33 34
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
H
Henry Rawas 已提交
35
#ifndef _WIN32
36
#include <unistd.h>
H
Henry Rawas 已提交
37
#endif
P
Pieter Noordhuis 已提交
38 39 40
#include <sys/stat.h>
#include "config.h"

H
Henry Rawas 已提交
41
#ifdef _WIN32
42
#include "Win32_Interop\win32fixes.h"
H
Henry Rawas 已提交
43 44 45 46
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif

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

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

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;
70
    epos = ftello(fp);
P
Pieter Noordhuis 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
    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;
84
    epos = ftello(fp);
H
Henry Rawas 已提交
85
    real = (long)fread(target,1,length,fp);
P
Pieter Noordhuis 已提交
86 87 88 89 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
    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);
}

117 118 119
off_t process(FILE *fp) {
    long argc;
    off_t pos = 0;
P
Pieter Noordhuis 已提交
120 121 122 123
    int i, multi = 0;
    char *str;

    while(1) {
H
Henry Rawas 已提交
124
        if (!multi) pos = (off_t)ftello(fp);
125
        if (!readArgc(fp, &argc)) break;
P
Pieter Noordhuis 已提交
126 127

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

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

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

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

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

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

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

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

    fclose(fp);
    return 0;
}