redis-check-aof.c 6.2 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>
35
#include <unistd.h>
P
Pieter Noordhuis 已提交
36 37 38 39 40 41
#include <sys/stat.h>
#include "config.h"

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

static char error[1024];
46
static off_t epos;
P
Pieter Noordhuis 已提交
47 48 49 50 51 52 53 54 55 56 57

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;
58
    epos = ftello(fp);
P
Pieter Noordhuis 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71
    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;
72
    epos = ftello(fp);
P
Pieter Noordhuis 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    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);
}

105 106 107
off_t process(FILE *fp) {
    long argc;
    off_t pos = 0;
P
Pieter Noordhuis 已提交
108 109 110 111
    int i, multi = 0;
    char *str;

    while(1) {
112
        if (!multi) pos = ftello(fp);
113
        if (!readArgc(fp, &argc)) break;
P
Pieter Noordhuis 已提交
114 115

        for (i = 0; i < argc; i++) {
116
            if (!readString(fp,&str)) break;
P
Pieter Noordhuis 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            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);
        }

133
        /* Stop if the loop did not finish */
P
Pieter Noordhuis 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        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) {
150 151
    char *filename;
    int fix = 0;
P
Pieter Noordhuis 已提交
152 153 154 155 156 157 158

    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) {
159 160 161 162 163
        if (strcmp(argv[1],"--fix") != 0) {
            printf("Invalid argument: %s\n", argv[1]);
            exit(1);
        }
        filename = argv[2];
P
Pieter Noordhuis 已提交
164
        fix = 1;
165
    } else {
P
Pieter Noordhuis 已提交
166
        printf("Invalid arguments\n");
167 168 169 170
        exit(1);
    }

    FILE *fp = fopen(filename,"r+");
P
Pieter Noordhuis 已提交
171
    if (fp == NULL) {
172
        printf("Cannot open file: %s\n", filename);
P
Pieter Noordhuis 已提交
173 174 175 176 177
        exit(1);
    }

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

182
    off_t size = sb.st_size;
P
Pieter Noordhuis 已提交
183
    if (size == 0) {
184
        printf("Empty file: %s\n", filename);
P
Pieter Noordhuis 已提交
185 186 187
        exit(1);
    }

188 189 190 191
    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);
192
    if (diff > 0) {
193
        if (fix) {
194
            char buf[2];
195
            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);
196 197 198 199 200 201
            printf("Continue? [y/N]: ");
            if (fgets(buf,sizeof(buf),stdin) == NULL ||
                strncasecmp(buf,"y",1) != 0) {
                    printf("Aborting...\n");
                    exit(1);
            }
202
            if (ftruncate(fileno(fp), pos) == -1) {
203
                printf("Failed to truncate AOF\n");
204 205
                exit(1);
            } else {
206
                printf("Successfully truncated AOF\n");
207 208
            }
        } else {
209 210
            printf("AOF is not valid\n");
            exit(1);
211
        }
P
Pieter Noordhuis 已提交
212
    } else {
213
        printf("AOF is valid\n");
P
Pieter Noordhuis 已提交
214 215 216 217 218
    }

    fclose(fp);
    return 0;
}